|
|
|
@ -52,17 +52,17 @@ class AgentService |
|
|
|
|
\DB::beginTransaction(); |
|
|
|
|
try { |
|
|
|
|
$dataInsert = Arr::except($dataInput, ['agents_code']); // remove agents_code array |
|
|
|
|
$dataInsert['created_at'] = now(); |
|
|
|
|
|
|
|
|
|
$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 từ ngày hiện tại trừ đi không được quá 15 ngày |
|
|
|
|
$customerAgentAll = $this->modelCustomer->where('phone', $dataInput['phone'])->where('agent_id', '<>', $agentId)->where('created_at', '>=', $dateNotToo)->latest()->first(); |
|
|
|
|
$dataInsert['created_at'] = now(); |
|
|
|
|
// $customerAgent = $this->modelCustomer->where('agent_id', $agentId)->where('phone', $dataInput['phone'])->latest()->first(); |
|
|
|
|
|
|
|
|
|
if (!empty($customerAgentAll)) { |
|
|
|
|
// 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'] = $customerAgentAll->agent_id; |
|
|
|
|
$dataInsert['agent_id'] = $customerInAgent->agent_id; |
|
|
|
|
$result = $this->modelCustomer->insert($dataInsert); |
|
|
|
|
} else { |
|
|
|
|
// quá 15 ngày thì lấy agent_id check ra từ mã đại lý |
|
|
|
|