where('phone_number', null)->where('owned_by_id', null) ->whereDate('created_at', '=', date($date->format('Y-m-d'))) ->get(); $linkedids =[]; foreach ($calls as $call){ $linkedids [] = $call->linkedid; } // echo json_encode($linkedids); $ids_not_in_cdr = Call::select('calls.linkedid') ->leftJoin('asteriskcdrdb.cdr as cdr', 'calls.linkedid', 'cdr.linkedid') ->whereIn("calls.linkedid", $linkedids) ->whereNull('cdr.linkedid') ->toSql(); // $ids_not_in_cdr = Call::select('calls.linkedid') // ->whereIn('calls.linkedid', $linkedids) // ->whereNotIn('calls.linkedid', function($query) { // $query->select('asteriskcdrdb.cdr.linkedid') // ->from('asteriskcdrdb.cdr'); // }) // ->toSql(); echo $ids_not_in_cdr; return 0; // $ids_not_in_cdr = []; // foreach ($calls as $call) { // $cdr_d = \DB::table('asteriskcdrdb.cdr')->where('linkedid', $call->linkedid)->first(); // if (!$cdr_d) { // $ids_not_in_cdr[] = $call->id; // // Call::where('id', $call->id)->delete(); // // echo $call->id . ' deleted not found in cdr '.PHP_EOL; // } else { // $call->direction_text = $call->direction->name; // $call->makeHidden('direction'); // $res = $this->setCallData($call->id, $call->linkedid, $call->direction_id, $call->direction_text); // echo $res . PHP_EOL; // } // } // Call::whereIn('id', $ids_not_in_cdr)->delete(); // echo json_encode($ids_not_in_cdr); } public function setCallData($id, $linkedid, $direction_id, $direction_text) { $call = Call::where('id', $id)->first(); $cdr_data = \DB::table('asteriskcdrdb.cdr') ->select('duration', 'billsec', 'src', 'dst', 'cnum', 'cnam', 'calldate', 'disposition') ->where('linkedid', $linkedid) ->where('disposition', 'ANSWERED') ->where('duration', '!=', '0') ->where('billsec', '!=', '0'); // return $direction_text; $data = null; if ($direction_text == 'Incoming') { $data = $cdr_data->where('lastapp', 'Dial') ->where('dcontext', 'from-queue-exten-only') ->first(); if (!$data) { $call->delete(); return $call->id . ' no Incoming answer data for this call ' . PHP_EOL; } $call->phone_number = $data->src; $call->owned_by_id = $data->dst; } elseif ($direction_text == 'Outgoing') { $data = $cdr_data->where('lastapp', 'Dial') ->where('dcontext', 'from-internal') ->first(); if (!$data) { $call->delete(); return $call->id . ' no Outgoing answer data for this call ' . PHP_EOL; } $call->phone_number = $data->dst; $call->owned_by_id = $data->cnum; } $lead_id = NULL; $possible_lead_id = Lead::select('id')->where('phone', $call->phone_number)->orWhere('mobile', $call->phone_number)->first(); if (!is_null($possible_lead_id)) { $lead_id = $possible_lead_id->id; } $contact_id = NULL; $possible_contact_id = Contact::select('id')->where('phone', $call->phone_number)->orWhere('mobile', $call->phone_number)->first(); if (!is_null($possible_contact_id)) { $contact_id = $possible_contact_id->id; } $company_id = NULL; $possible_company_id = User::select('company_id')->where('id', $call->owned_by_id)->first(); if (!is_null($possible_company_id)) { $company_id = $possible_company_id->company_id; } $call->company_id = $company_id; $call->lead_id = $lead_id; $call->contact_id = $contact_id; $call->duration = $data->duration; // $call->linkedid = $linkedid; $call->ringtime = (int)$data->duration - $data->billsec; $call->talktime = $data->billsec; $call->owned_by_type = 'Modules\Platform\User\Entities\User'; $call->call_date = $data->calldate; // $call->direction_id = $direction_id; if ($call->save()) { return $call->id; } return false; } }