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.
408 lines
15 KiB
408 lines
15 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
|
|
$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();
|
|
|
|
//
|
|
$isInAgentDistance = $this->modelCustomer->byPhone($dataInput['phone'])->whereDate('created_at', '>', $dateNotToo)->orderBy('created_at','asc')->get();
|
|
// dd(empty($isInAgentDistance[0]));
|
|
if (empty($isInAgentDistance[0])) { // ko co trong DS 15 ngay
|
|
dd(1);
|
|
$dataInsert['agent_id'] = $agentId;
|
|
$this->modelCustomer->insert($dataInsert);
|
|
$this->modelCustomer->byPhone($dataInput['phone'])->notAgent($agentId)->delete();
|
|
} else { // co DS 15 ngay
|
|
$agentIdBelonging = $isInAgentDistance[0]->agent_id;
|
|
|
|
// if($agentIdBelonging == $agentId){
|
|
// $idAgentInsert = $agentIdBelonging;
|
|
// }else{
|
|
// $idAgentInsert = $agentId;
|
|
// $this->modelCustomer->byPhone($dataInput['phone'])->notAgent($agentIdBelonging)->delete();
|
|
// }
|
|
$idAgentInsert = $agentId;
|
|
|
|
$dataInsert['agent_id'] = $agentIdBelonging;
|
|
$this->modelCustomer->insert($dataInsert);
|
|
$this->modelCustomer->byPhone($dataInput['phone'])->notAgent($agentIdBelonging)->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 = [];
|
|
$finalAgentData = [];
|
|
$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'])) {
|
|
$startDate = Carbon::createFromFormat('Y-m-d', $input['start_date']);
|
|
$endDate = Carbon::createFromFormat('Y-m-d', $input['end_date'])->addDay();
|
|
|
|
$queryAgent->whereBetween('created_at', [$startDate->toDateString(), $endDate->toDateString()]);
|
|
}
|
|
if (!empty($input['keyword'])) {
|
|
$queryAgent->search($input['keyword']);
|
|
}
|
|
// dd($queryAgent->toSql(),$queryAgent->getBindings());
|
|
$listCustomer = $queryAgent->orderBy('created_at', 'desc')->get();
|
|
if (!empty($listCustomer)) {
|
|
foreach ($listCustomer as $listC) {
|
|
$finalAgentData[] = [
|
|
'id' => $listC->id ?? '',
|
|
'guest_name' => $listC->name ?? '',
|
|
'phone' => $listC->phone ?? '',
|
|
'email' => $listC->email ?? '',
|
|
'grade' => $listC->grade ?? '',
|
|
'date_receive' => $listC->created_at ?? '',
|
|
'message' => $listC->msg ?? '',
|
|
'agent_id' => $listC->agent_id ?? ''
|
|
];
|
|
}
|
|
}
|
|
return ['status' => true, 'data' => $finalAgentData, 'msg' => $this->getMessageReturn('success')];
|
|
}
|
|
/**
|
|
* Summary of listAgentOfGeneral
|
|
* @param mixed $input
|
|
* @return array
|
|
*/
|
|
public function listAgentOfGeneral($input)
|
|
{
|
|
$data = [];
|
|
$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')];
|
|
} else {
|
|
$myAgentInfo = $this->modelAgent->find($myAgent->agent_id);
|
|
if (empty($myAgentInfo)) {
|
|
return ['status' => false, 'msg' => $this->getMessageReturn('no_agent')];
|
|
}
|
|
}
|
|
$agentId = $myAgentInfo->id;
|
|
$myAgentDown = $this->modelAgentUser->byAgentRoot($agentId)->joinAgent();
|
|
|
|
if (!empty($input['start_date']) && !empty($input['end_date'])) {
|
|
$startDate = Carbon::createFromFormat('Y-m-d', $input['start_date']);
|
|
$endDate = Carbon::createFromFormat('Y-m-d', $input['end_date'])->addDay();
|
|
|
|
$myAgentDown->betweenDates($startDate->toDateString(), $endDate->toDateString());
|
|
}
|
|
if (!empty($input['keyword'])) {
|
|
$myAgentDown->searchAgentName($input['keyword']);
|
|
}
|
|
if (!empty($input['grant'])) {
|
|
$myAgentDown->onlyGrant($input['grant']);
|
|
}
|
|
$listAgentGet = $myAgentDown->get();
|
|
$allAgentInGeneral = $this->modelAgentUser->byAgentRoot($agentId)->joinAgent()->get();
|
|
|
|
$AgentById = [];
|
|
if(!empty($allAgentInGeneral)){
|
|
foreach($allAgentInGeneral as $vAgent){
|
|
$AgentById[$vAgent->agent_id] = $vAgent;
|
|
}
|
|
}
|
|
|
|
if (!empty($listAgentGet)) {
|
|
$listAgentById = [];
|
|
foreach ($listAgentGet as $kAgent) {
|
|
// get parent id by path
|
|
$kAgent->grant_parent_id = $this->getParentAgent($kAgent->parent_path);
|
|
$listAgentById[$kAgent->agent_id] = $kAgent;
|
|
}
|
|
}
|
|
$finalDataTemp = [];
|
|
if (!empty($listAgentById)) {
|
|
foreach ($listAgentById as $vByAgent) {
|
|
|
|
if (!empty($AgentById[$vByAgent->grant_parent_id])) {
|
|
$agentParent = $AgentById[$vByAgent->grant_parent_id];
|
|
$upGrant = $agentParent->code ?? null;
|
|
}
|
|
if ($agentId == $vByAgent->agent_id)
|
|
continue; // skip agent root
|
|
|
|
if (!empty($input['up_grant'])) { // if existing up_geant
|
|
if (strtolower($upGrant) != strtolower($input['up_grant']))
|
|
continue; // skip
|
|
}
|
|
if (!empty($input['agents_code'])) { // if existing up_geant
|
|
if (strtolower($vAgent->code) != strtolower($input['agents_code']))
|
|
continue; // skip
|
|
}
|
|
$finalDataTemp[] = [
|
|
'id' => $vByAgent->id ?? '',
|
|
'name' => $vByAgent->agent_name ?? '',
|
|
'agents_code' => $vByAgent->code ?? '',
|
|
'created_date' => $vByAgent->created_at ?? '',
|
|
'phone' => $vByAgent->agent_phone ?? '',
|
|
'email' => $vByAgent->agent_email ?? '',
|
|
'grant' => $vByAgent->grant ?? '',
|
|
'up_grant' => $upGrant ?? ''
|
|
];
|
|
}
|
|
}
|
|
$data = $finalDataTemp;
|
|
return ['status' => true, 'data' => $data, 'msg' => $this->getMessageReturn('success')];
|
|
}
|
|
/**
|
|
* Summary of getChildAgent
|
|
* @param mixed $input
|
|
* @return array
|
|
*/
|
|
public function getParentAgent($parentPath)
|
|
{
|
|
$parts = explode('-', $parentPath);
|
|
|
|
// remove element null
|
|
$parts = array_filter($parts, fn($part) => $part !== '');
|
|
|
|
if (count($parts) == 1) {
|
|
return min($parts);
|
|
} else {
|
|
return $parts[count($parts) - 1] ?? null;
|
|
}
|
|
}
|
|
/**
|
|
* Summary of myAgentList
|
|
* @param mixed $input
|
|
* @return array
|
|
*/
|
|
public function myAgentList($input)
|
|
{
|
|
$data = [];
|
|
//get data general
|
|
$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')];
|
|
} else {
|
|
$myAgentInfo = $this->modelAgent->find($myAgent->agent_id);
|
|
if (empty($myAgentInfo)) {
|
|
return ['status' => false, 'msg' => $this->getMessageReturn('no_agent')];
|
|
}
|
|
}
|
|
$agentId = $myAgentInfo->id;
|
|
//init query
|
|
$myAgentDown = $this->modelAgentUser->byParentPath($agentId)->joinAgent();
|
|
|
|
//condition startdate & enddate
|
|
if (!empty($input['start_date']) && !empty($input['end_date'])) {
|
|
$startDate = Carbon::createFromFormat('Y-m-d', $input['start_date']);
|
|
$endDate = Carbon::createFromFormat('Y-m-d', $input['end_date'])->addDay();
|
|
|
|
$myAgentDown->betweenDates($startDate->toDateString(), $endDate->toDateString());
|
|
}
|
|
//condition keyword search
|
|
if (!empty($input['keyword'])) {
|
|
$myAgentDown->searchAgentName($input['keyword']);
|
|
}
|
|
if (!empty($input['grant'])) {
|
|
$myAgentDown->onlyGrant($input['grant']);
|
|
}
|
|
//execute query get list
|
|
$listAgentGet = $myAgentDown->get();
|
|
|
|
// get all by parent path with root
|
|
$allAgentInGeneral = $this->modelAgentUser->byParentPath($myAgent->agent_root_id)->joinAgent()->get();
|
|
$AgentById = [];
|
|
if(!empty($allAgentInGeneral)){
|
|
foreach($allAgentInGeneral as $vAgent){
|
|
$AgentById[$vAgent->agent_id] = $vAgent;
|
|
}
|
|
}
|
|
$finalData = [];
|
|
if (!empty($listAgentGet)) {
|
|
$listAgentById = [];
|
|
//array by agent_id and get parent agent
|
|
foreach ($listAgentGet as $kAgent) {
|
|
// get parent id by path
|
|
$kAgent->grant_parent_id = $this->getParentAgent($kAgent->parent_path);
|
|
$listAgentById[$kAgent->agent_id] = $kAgent;
|
|
}
|
|
foreach ($listAgentGet as $vAgent) {
|
|
if (!empty($AgentById[$vAgent->grant_parent_id])) {
|
|
$agentParent = $AgentById[$vAgent->grant_parent_id];
|
|
$upGrant = $agentParent->code ?? null;
|
|
}
|
|
if (!empty($input['up_grant'])) { // if existing up_geant
|
|
if (strtolower($upGrant) != strtolower($input['up_grant']))
|
|
continue; // skip
|
|
}
|
|
if (!empty($input['agents_code'])) { // if existing up_geant
|
|
if (strtolower($vAgent->code) != strtolower($input['agents_code']))
|
|
continue; // skip
|
|
}
|
|
// lay danh sach 2 con cua parent
|
|
$processPath = $this->processParentPath($agentId, $vAgent->parent_path);
|
|
if (!empty($processPath) && in_array($vAgent->agent_id, $processPath)) {
|
|
// $finalData[] = $vAgent;
|
|
$finalData[] = [
|
|
'id' => $vAgent->id ?? '',
|
|
'name' => $vAgent->agent_name ?? '',
|
|
'agents_code' => $vAgent->code ?? '',
|
|
'created_date' => $vAgent->created_at ?? '',
|
|
'phone' => $vAgent->agent_phone ?? '',
|
|
'email' => $vAgent->agent_email ?? '',
|
|
'grant' => $vAgent->grant ?? '',
|
|
'up_grant' => $upGrant ?? ''
|
|
];
|
|
}
|
|
}
|
|
}
|
|
$data = $finalData;
|
|
return ['status' => true, 'data' => $data, 'msg' => $this->getMessageReturn('success')];
|
|
}
|
|
/**
|
|
* Summary of processPath
|
|
* @param mixed $parentPath
|
|
* @param mixed $agentId
|
|
* @return array
|
|
*/
|
|
public function processParentPath($agentId, $parentPath)
|
|
{
|
|
|
|
// find index agentId
|
|
$position = strpos($parentPath, '-' . $agentId . '-');
|
|
|
|
if ($position !== false) {
|
|
// cat chuoi tu vi tri ngay sau agentId
|
|
$substring = substr($parentPath, $position + strlen('-' . $agentId . '-'));
|
|
if (empty($substring)) {
|
|
return [];
|
|
}
|
|
// tach chuoi thanh cac mang phan tu
|
|
$numbers = explode('-', trim($substring, '-'));
|
|
|
|
// lay 2 mang dau tien sau agentId
|
|
$result = array_slice($numbers, 0, 2);
|
|
|
|
// ghep lai thanh chuoi
|
|
// $final_path = '-' . implode('-', $result) . '-';
|
|
$final_path = $result;
|
|
} else {
|
|
return [];
|
|
}
|
|
|
|
return $final_path;
|
|
}
|
|
/**
|
|
* 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.',
|
|
'no_agent' => 'Không tìm thấy đại lý.',
|
|
];
|
|
return $msgArr[$errorKey] ?? "Không có dữ liệu.";
|
|
}
|
|
} |