You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
5.4 KiB
151 lines
5.4 KiB
<?php
|
|
namespace Modules\Agents\Services;
|
|
|
|
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;
|
|
|
|
/**
|
|
* Summary of __construct
|
|
* @param \Modules\Agents\Entities\Customer $modelCustomer
|
|
* @param \App\Models\Agent $modelAgent
|
|
* @param \App\Models\AgentUser $modelAgentUser
|
|
*/
|
|
public function __construct(
|
|
Customer $modelCustomer,
|
|
Agent $modelAgent,
|
|
AgentUser $modelAgentUser
|
|
) {
|
|
$this->modelCustomer = $modelCustomer;
|
|
$this->modelAgent = $modelAgent;
|
|
$this->modelAgentUser = $modelAgentUser;
|
|
}
|
|
|
|
/**
|
|
* Summary of createCustomer
|
|
* @param array $input
|
|
* @return array
|
|
*/
|
|
public function createCustomer($input)
|
|
{
|
|
// format data input
|
|
$dataInput = $this->formatCreateCustomer($input);
|
|
// 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'));
|
|
}
|
|
$agentId = $checkAgentUser->agent_id;
|
|
|
|
\DB::beginTransaction();
|
|
try {
|
|
$dataInsert = Arr::except($dataInput, ['agents_code']); // remove agents_code array
|
|
|
|
$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);
|
|
}else{
|
|
// 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
|
|
$this->modelCustomer->where('phone', $dataInput['phone'])->where('agent_id', '<>',$agentId)->delete();
|
|
|
|
}
|
|
|
|
\DB::commit();
|
|
return ['status' => true, 'msg' => $this->getMessageReturn('success_insert_customer')];
|
|
|
|
} catch (\Exception $e) {
|
|
\DB::rollBack();
|
|
}
|
|
return ['status' => false, 'msg' => $this->getMessageReturn('failed_insert')];
|
|
}
|
|
/**
|
|
* Summary of myCustomer
|
|
* @param array $input
|
|
* @return array
|
|
*/
|
|
public function myCustomer($input){
|
|
$listCustomer = [];
|
|
$myAgent = $this->modelAgentUser->where('user_id', auth()->user()->id)->latest()->first();
|
|
if(empty($myAgent) || empty($myAgent->agent_id)){
|
|
return ['status' => false, 'msg' => $this->getMessageReturn('no_data_agent')];
|
|
}
|
|
$agentId = $myAgent->agent_id;
|
|
$queryAgent = $this->modelCustomer->where('agent_id', $agentId);
|
|
if(!empty($input['start_date']) && !empty($input['end_date'])){
|
|
$queryAgent->whereBetween('created_at', [$input['start_date'], $input['end_date']]);
|
|
}
|
|
if(!empty($input['keyword'])){
|
|
$queryAgent->where('name', 'LIKE', '%' . $input['keyword'] . '%');
|
|
}
|
|
$listCustomer = $queryAgent->orderBy('name', 'asc')->get();
|
|
return ['status' => true, 'data' => $listCustomer, 'msg' => $this->getMessageReturn('success')];
|
|
}
|
|
/**
|
|
* Summary of formatCreateCustomer
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
public function formatCreateCustomer($data)
|
|
{
|
|
$format = [];
|
|
if (!empty($data['agents_code'])) {
|
|
$format['agents_code'] = $data['agents_code'];
|
|
}
|
|
if (!empty($data['guest_name'])) {
|
|
$format['name'] = $data['guest_name'];
|
|
}
|
|
if (!empty($data['phone'])) {
|
|
$format['phone'] = $data['phone'];
|
|
}
|
|
if (!empty($data['email'])) {
|
|
$format['email'] = $data['email'];
|
|
}
|
|
if (!empty($data['grade'])) {
|
|
$format['grade'] = $data['grade'];
|
|
}
|
|
if (!empty($data['grade'])) {
|
|
$format['grade'] = $data['grade'];
|
|
}
|
|
if (!empty($data['message'])) {
|
|
$format['msg'] = $data['message'];
|
|
}
|
|
return $format;
|
|
}
|
|
/**
|
|
* Summary of getMessageReturn
|
|
* @param mixed $errorKey
|
|
* @return array
|
|
*/
|
|
public function getMessageReturn($errorKey)
|
|
{
|
|
$msgArr = [
|
|
'no_data_agent' => 'Đại lý không tồn tại.',
|
|
'success_insert_customer' => 'Thêm khách hàng thành công.',
|
|
'failed_insert' => 'Thêm khách hàng thành công.',
|
|
'success' => 'Load data thành công.'
|
|
];
|
|
return $msgArr[$errorKey] ?? "Không có dữ liệu.";
|
|
}
|
|
} |