diff --git a/Modules/Agents/Entities/Customer.php b/Modules/Agents/Entities/Customer.php index 6f43120..f04dfc5 100755 --- a/Modules/Agents/Entities/Customer.php +++ b/Modules/Agents/Entities/Customer.php @@ -16,4 +16,32 @@ class Customer extends Model // { // return \Modules\Agents\Database\factories\CustomerFactory::new(); // } + + /** + * Summary of scopeByPhone + * @param mixed $query + * @param mixed $phone + * @return mixed + */ + public function scopeByPhone($query, $phone){ + return $query->where('phone', $phone); + } + /** + * Summary of scopeNotAgent + * @param mixed $query + * @param mixed $agentId + * @return mixed + */ + public function scopeNotAgent($query, $agentId){ + return $query->where('agent_id', '<>', $agentId); + } + /** + * Summary of scopeInAgent + * @param mixed $query + * @param mixed $agentId + * @return mixed + * **/ + public function scopeInAgent($query, $agentId){ + return $query->where('agent_id', '=', $agentId); + } } diff --git a/Modules/Agents/Services/AgentService.php b/Modules/Agents/Services/AgentService.php index e2030e0..1105e78 100644 --- a/Modules/Agents/Services/AgentService.php +++ b/Modules/Agents/Services/AgentService.php @@ -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ý