|
|
@ -5,9 +5,11 @@ use Illuminate\Support\Arr; |
|
|
|
use Modules\Agents\Entities\Customer; |
|
|
|
use Modules\Agents\Entities\Customer; |
|
|
|
use App\Models\Agent; |
|
|
|
use App\Models\Agent; |
|
|
|
use App\Models\AgentUser; |
|
|
|
use App\Models\AgentUser; |
|
|
|
|
|
|
|
use Carbon\Carbon; |
|
|
|
|
|
|
|
|
|
|
|
class AgentService |
|
|
|
class AgentService |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
const DISTANCE_DAY_DEFAULT = 15; |
|
|
|
protected $modelCustomer; |
|
|
|
protected $modelCustomer; |
|
|
|
protected $modelAgent; |
|
|
|
protected $modelAgent; |
|
|
|
protected $modelAgentUser; |
|
|
|
protected $modelAgentUser; |
|
|
@ -40,6 +42,8 @@ class AgentService |
|
|
|
// check agent user with code input |
|
|
|
// check agent user with code input |
|
|
|
$checkAgentUser = $this->modelAgentUser->where('code', $dataInput['agents_code'])->first(); |
|
|
|
$checkAgentUser = $this->modelAgentUser->where('code', $dataInput['agents_code'])->first(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$now = Carbon::now(); |
|
|
|
|
|
|
|
|
|
|
|
if (empty($checkAgentUser)) { |
|
|
|
if (empty($checkAgentUser)) { |
|
|
|
return array('status' => false, 'msg' => $this->getMessageReturn('no_data_agent')); |
|
|
|
return array('status' => false, 'msg' => $this->getMessageReturn('no_data_agent')); |
|
|
|
} |
|
|
|
} |
|
|
@ -47,23 +51,32 @@ class AgentService |
|
|
|
|
|
|
|
|
|
|
|
\DB::beginTransaction(); |
|
|
|
\DB::beginTransaction(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
$checkHasAgent = $this->modelCustomer->where('agent_id', $agentId)->latest()->first(); |
|
|
|
$dataInsert = Arr::except($dataInput, ['agents_code']); // remove agents_code array |
|
|
|
|
|
|
|
|
|
|
|
if (empty($checkHasAgent)) { |
|
|
|
$dateNotToo = Carbon::now()->subDays(self::DISTANCE_DAY_DEFAULT); |
|
|
|
$dataInsert = Arr::except($dataInput, ['agents_code']); |
|
|
|
$customerAgent = $this->modelCustomer->where('agent_id', $agentId)->where('phone', $dataInput['phone'])->latest()->first(); |
|
|
|
$dataInsert['agent_id'] = $agentId; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!empty($customerAgentAll)){ |
|
|
|
|
|
|
|
// 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; |
|
|
|
$result = $this->modelCustomer->insert($dataInsert); |
|
|
|
$result = $this->modelCustomer->insert($dataInsert); |
|
|
|
\DB::commit(); |
|
|
|
|
|
|
|
return ['status' => true, 'msg' => $this->getMessageReturn('success_insert_customer')]; |
|
|
|
|
|
|
|
}else{ |
|
|
|
}else{ |
|
|
|
$dataInsert = Arr::except($dataInput, ['agents_code']); |
|
|
|
// quá 15 ngày thì lấy agent_id check ra từ mã đại lý |
|
|
|
$dataInsert['agent_id'] = $agentId; |
|
|
|
$dataInsert['agent_id'] = $agentId; |
|
|
|
|
|
|
|
|
|
|
|
$result = $this->modelCustomer->insert($dataInsert); |
|
|
|
$result = $this->modelCustomer->insert($dataInsert); |
|
|
|
\DB::commit(); |
|
|
|
// nếu agent id cũ != cái hiện tại check |
|
|
|
return ['status' => true, 'msg' => $this->getMessageReturn('success_insert_customer')]; |
|
|
|
$checkOldCustomer = $this->modelCustomer->where('phone', $dataInput['phone'])->where('agent_id', '=',$agentId)->latest()->first(); |
|
|
|
|
|
|
|
if($agentId != $checkOldCustomer->agent_id){ |
|
|
|
|
|
|
|
$this->modelCustomer->where('phone', $dataInput['phone'])->where('agent_id', '<>',$agentId)->where('created_at', '<=', $dateNotToo)->delete(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\DB::commit(); |
|
|
|
|
|
|
|
return ['status' => true, 'msg' => $this->getMessageReturn('success_insert_customer')]; |
|
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) { |
|
|
|
} catch (\Exception $e) { |
|
|
|
\DB::rollBack(); |
|
|
|
\DB::rollBack(); |
|
|
|
} |
|
|
|
} |
|
|
|