Update API add Customer

main
nocode 1 year ago
parent af95623724
commit 579da1e816
  1. 3
      Modules/Agents/Entities/Customer.php
  2. 31
      Modules/Agents/Services/AgentService.php

@ -7,9 +7,10 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
class Customer extends Model
{
use HasFactory;
use HasFactory, \Illuminate\Database\Eloquent\SoftDeletes;
protected $table = 'se_customers';
protected $fillable = ['id', 'name', 'phone', 'email', 'grade', 'msg', 'agent_id', 'created_at', 'created_by', 'updated_at', 'updated_by', 'deleted_at'];
protected $dates = ['deleted_at'];
// protected static function newFactory()
// {

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

Loading…
Cancel
Save