From 5c0960d33bab040333557a19abdb998cfc3ea32b Mon Sep 17 00:00:00 2001 From: nocode Date: Thu, 18 Jul 2024 17:26:27 +0700 Subject: [PATCH] add customer --- Modules/Agents/Services/AgentService.php | 27 ++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Modules/Agents/Services/AgentService.php b/Modules/Agents/Services/AgentService.php index ec1abb8..bd4e4e8 100644 --- a/Modules/Agents/Services/AgentService.php +++ b/Modules/Agents/Services/AgentService.php @@ -48,7 +48,8 @@ class AgentService return array('status' => false, 'msg' => $this->getMessageReturn('no_data_agent')); } $agentId = $checkAgentUser->agent_id; - + $today = Carbon::today(); + \DB::beginTransaction(); try { $dataInsert = Arr::except($dataInput, ['agents_code']); // remove agents_code array @@ -67,18 +68,22 @@ class AgentService $this->modelCustomer->byPhone($dataInput['phone'])->notAgent($agentId)->delete(); } else { // co DS 15 ngay $agentIdBelonging = $isInAgentDistance[0]->agent_id; + $customerAgentAllTime = $this->modelCustomer->byPhone($dataInput['phone'])->orderBy('created_at','asc')->get(); - // if($agentIdBelonging == $agentId){ - // $idAgentInsert = $agentIdBelonging; - // }else{ - // $idAgentInsert = $agentId; - // $this->modelCustomer->byPhone($dataInput['phone'])->notAgent($agentIdBelonging)->delete(); - // } - $idAgentInsert = $agentId; - $dataInsert['agent_id'] = $agentIdBelonging; - $this->modelCustomer->insert($dataInsert); - $this->modelCustomer->byPhone($dataInput['phone'])->notAgent($agentIdBelonging)->delete(); + if(!empty($customerAgentAllTime[0])){ + $differenceInDays = $today->diffInDays($customerAgentAllTime[0]->created_at); + if($differenceInDays > self::DISTANCE_DAY_DEFAULT){ + $dataInsert['agent_id'] = $agentId; + $this->modelCustomer->insert($dataInsert); + }else{ + $dataInsert['agent_id'] = $customerAgentAllTime[0]->agent_id; + $this->modelCustomer->insert($dataInsert); + } + }else{ + $dataInsert['agent_id'] = $agentId; + $this->modelCustomer->insert($dataInsert); + } }