|
|
|
@ -57,21 +57,24 @@ class AgentService |
|
|
|
|
$dateNotToo = Carbon::now()->subDays(self::DISTANCE_DAY_DEFAULT); |
|
|
|
|
// $customerAgent = $this->modelCustomer->where('agent_id', $agentId)->where('phone', $dataInput['phone'])->latest()->first(); |
|
|
|
|
|
|
|
|
|
// tìm các dữ liệu của đại lý từ mã gửi lên từ ngày hiện tại trừ đi co trong 15 ngày |
|
|
|
|
$customerInAgent = $this->modelCustomer->byPhone($dataInput['phone'])->inAgent($agentId)->whereDate('created_at', '>', $dateNotToo)->orderBy('created_at','asc')->first(); |
|
|
|
|
|
|
|
|
|
if (!empty($customerInAgent)) { |
|
|
|
|
// chưa quá 15 ngày thì lấy cái agent_id cũ, đang trực thuộc |
|
|
|
|
$dataInsert['agent_id'] = $customerInAgent->agent_id; |
|
|
|
|
$result = $this->modelCustomer->insert($dataInsert); |
|
|
|
|
$this->modelCustomer->where('phone', $dataInput['phone'])->where('agent_id', '<>', $customerInAgent->agent_id)->delete(); |
|
|
|
|
} else { |
|
|
|
|
// quá 15 ngày thì lấy agent_id check ra từ mã đại lý |
|
|
|
|
// |
|
|
|
|
$isInAgentDistance = $this->modelCustomer->byPhone($dataInput['phone'])->whereDate('created_at', '>', $dateNotToo)->orderBy('created_at','asc')->get(); |
|
|
|
|
|
|
|
|
|
if (empty($isInAgentDistance)) { // ko co trong DS 15 ngay |
|
|
|
|
$dataInsert['agent_id'] = $agentId; |
|
|
|
|
$result = $this->modelCustomer->insert($dataInsert); |
|
|
|
|
// nếu agent id cũ != cái hiện tại check |
|
|
|
|
$this->modelCustomer->where('phone', $dataInput['phone'])->where('agent_id', '<>', $agentId)->delete(); |
|
|
|
|
$this->modelCustomer->insert($dataInsert); |
|
|
|
|
$this->modelCustomer->byPhone($dataInput['phone'])->notAgent($agentId)->delete(); |
|
|
|
|
} else { // co DS 15 ngay |
|
|
|
|
$agentIdBelonging = $isInAgentDistance[0]->agent_id; |
|
|
|
|
|
|
|
|
|
if($agentIdBelonging == $agentId){ |
|
|
|
|
$dataInsert['agent_id'] = $agentId; |
|
|
|
|
$this->modelCustomer->insert($dataInsert); |
|
|
|
|
}else{ |
|
|
|
|
$dataInsert['agent_id'] = $agentIdBelonging; |
|
|
|
|
$this->modelCustomer->insert($dataInsert); |
|
|
|
|
$this->modelCustomer->byPhone($dataInput['phone'])->notAgent($agentIdBelonging)->delete(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
\DB::commit(); |
|
|
|
|