main
nocode 1 year ago
parent 6af6ded6ad
commit 0326d60d87
  1. 21
      Modules/Agents/Http/Controllers/AgentActionApi.php
  2. 1
      Modules/Agents/Routes/api.php
  3. 90
      Modules/Agents/Services/AgentService.php
  4. 51
      app/Models/AgentUser.php

@ -12,6 +12,8 @@ use Modules\Agents\Services\AgentService;
class AgentActionApi extends BaseController class AgentActionApi extends BaseController
{ {
public $roleGeneral = "general";
public $roleAgents = "agents";
protected $agentService; protected $agentService;
/** /**
* Summary of __construct * Summary of __construct
@ -46,4 +48,23 @@ class AgentActionApi extends BaseController
} }
return response()->json(['status' => $result['status'], 'msg' => $result['msg'], 'data' => $result['data']]); return response()->json(['status' => $result['status'], 'msg' => $result['msg'], 'data' => $result['data']]);
} }
/**
* Summary of agentByGeneral
* @param \Illuminate\Http\Request $request
* @return mixed|\Illuminate\Http\JsonResponse
*/
public function agentByGeneral(Request $request){
if (auth()->user()->user_type == $this->roleGeneral) {
$result = $this->agentService->listAgentOfGeneral($request->all());
}
// get data agents
if (auth()->user()->user_type == $this->roleAgents) {
$result = $this->agentService->myAgentList($request->all());
}
if (!$result['status']) {
return response()->json(['status' => $result['status'], 'msg' => $result['msg']]);
}
return response()->json(['status' => $result['status'], 'msg' => $result['msg'], 'data' => $result['data']]);
}
} }

@ -15,5 +15,6 @@ use Modules\Agents\Http\Controllers\AgentActionApi;
Route::group(['prefix' => 'agent','middleware' => ['auth:api']], function() { Route::group(['prefix' => 'agent','middleware' => ['auth:api']], function() {
Route::get('/get_customer', [AgentActionApi::class, 'myCustomer'])->name('agent_get_customer'); Route::get('/get_customer', [AgentActionApi::class, 'myCustomer'])->name('agent_get_customer');
Route::get('/agent_of_general', [AgentActionApi::class, 'agentByGeneral'])->name('agent_of_general');
}); });
Route::post('/agent/add_customer', [AgentActionApi::class, 'createCustomer'])->name('agent_add_customer'); Route::post('/agent/add_customer', [AgentActionApi::class, 'createCustomer'])->name('agent_add_customer');

@ -57,18 +57,18 @@ class AgentService
$customerAgent = $this->modelCustomer->where('agent_id', $agentId)->where('phone', $dataInput['phone'])->latest()->first(); $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 // 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(); $customerAgentAll = $this->modelCustomer->where('phone', $dataInput['phone'])->where('agent_id', '<>', $agentId)->where('created_at', '>=', $dateNotToo)->latest()->first();
if(!empty($customerAgentAll)){ if (!empty($customerAgentAll)) {
// chưa quá 15 ngày thì lấy cái agent_id cũ, đang trực thuộc // 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'] = $customerAgentAll->agent_id;
$result = $this->modelCustomer->insert($dataInsert); $result = $this->modelCustomer->insert($dataInsert);
}else{ } else {
// quá 15 ngày thì lấy agent_id check ra từ mã đại lý // 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);
// nếu agent id cũ != cái hiện tại check // nếu agent id cũ != cái hiện tại check
$this->modelCustomer->where('phone', $dataInput['phone'])->where('agent_id', '<>',$agentId)->delete(); $this->modelCustomer->where('phone', $dataInput['phone'])->where('agent_id', '<>', $agentId)->delete();
} }
@ -85,37 +85,88 @@ class AgentService
* @param array $input * @param array $input
* @return array * @return array
*/ */
public function myCustomer($input){ public function myCustomer($input)
{
$listCustomer = []; $listCustomer = [];
$finalAgentData = []; $finalAgentData = [];
$myAgent = $this->modelAgentUser->where('user_id', auth()->user()->id)->latest()->first(); $myAgent = $this->modelAgentUser->where('user_id', auth()->user()->id)->latest()->first();
if(empty($myAgent) || empty($myAgent->agent_id)){ if (empty($myAgent) || empty($myAgent->agent_id)) {
return ['status' => false, 'msg' => $this->getMessageReturn('no_data_agent')]; return ['status' => false, 'msg' => $this->getMessageReturn('no_data_agent')];
} }
$agentId = $myAgent->agent_id; $agentId = $myAgent->agent_id;
$queryAgent = $this->modelCustomer->where('agent_id', $agentId); $queryAgent = $this->modelCustomer->where('agent_id', $agentId);
if(!empty($input['start_date']) && !empty($input['end_date'])){ if (!empty($input['start_date']) && !empty($input['end_date'])) {
$queryAgent->whereBetween('created_at', [$input['start_date'], $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'])){ if (!empty($input['keyword'])) {
$queryAgent->where('name', 'LIKE', '%' . $input['keyword'] . '%'); $queryAgent->where('name', 'LIKE', '%' . $input['keyword'] . '%');
} }
$listCustomer = $queryAgent->orderBy('name', 'asc')->get(); $listCustomer = $queryAgent->orderBy('name', 'asc')->get();
if(!empty($listCustomer)){ if (!empty($listCustomer)) {
foreach($listCustomer as $listC){ foreach ($listCustomer as $listC) {
$finalAgentData[] = [ $finalAgentData[] = [
'id' => $listC->id??'', 'id' => $listC->id ?? '',
'guest_name' => $listC->name??'', 'guest_name' => $listC->name ?? '',
'phone' => $listC->phone??'', 'phone' => $listC->phone ?? '',
'email' => $listC->email??'', 'email' => $listC->email ?? '',
'grade' => $listC->grade??'', 'grade' => $listC->grade ?? '',
'date_receive' => $listC->created_at??'', 'date_receive' => $listC->created_at ?? '',
'message' => $listC->msg??'' 'message' => $listC->msg ?? ''
]; ];
} }
} }
return ['status' => true, 'data' => $finalAgentData, 'msg' => $this->getMessageReturn('success')]; 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'])) {
$myAgentDown->betweenDates($input['start_date'], $input['end_date']);
}
if (!empty($input['keyword'])) {
$myAgentDown->searchAgentName($input['keyword']);
}
if (!empty($input['grant'])) {
$myAgentDown->onlyGrant($input['grant']);
}
dd($myAgentDown->get());
return ['status' => true, 'data' => $data, 'msg' => $this->getMessageReturn('success')];
}
/**
* Summary of myAgentList
* @param mixed $input
* @return array
*/
public function myAgentList($input)
{
$data = [];
//get data general
return ['status' => true, 'data' => $data, 'msg' => $this->getMessageReturn('success')];
}
/** /**
* Summary of formatCreateCustomer * Summary of formatCreateCustomer
* @param array $data * @param array $data
@ -158,7 +209,8 @@ class AgentService
'no_data_agent' => 'Đại lý không tồn tại.', 'no_data_agent' => 'Đại lý không tồn tại.',
'success_insert_customer' => 'Thêm khách hàng thành công.', '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.', 'failed_insert' => 'Thêm khách hàng thành công.',
'success' => 'Load data 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."; return $msgArr[$errorKey] ?? "Không có dữ liệu.";
} }

@ -24,4 +24,55 @@ class AgentUser extends Model
'deleted', 'deleted',
'deleted_at' 'deleted_at'
]; ];
/**
* Summary of scopeJoinAgent
* @param mixed $query
* @return mixed
*/
public function scopeJoinAgent($query)
{
return $query->join('se_agent', 'se_agent.id', '=', 'se_agent_user.agent_id')
->select('se_agent_user.*', 'se_agent.name as agent_name', 'se_agent.parent_path', 'se_agent.grant', 'se_agent.type');
}
/**
* Summary of scopeBetweenDates
* @param mixed $query
* @param mixed $startDate
* @param mixed $endDate
* @return mixed
*/
public function scopeBetweenDates($query, $startDate, $endDate)
{
return $query->whereBetween($this->table . '.created_at', [$startDate, $endDate]);
}
/**
* Summary of scopeByAgentRoot
* @param mixed $query
* @param mixed $agentRootId
* @return mixed
*/
public function scopeByAgentRoot($query, $agentRootId)
{
return $query->where($this->table . '.agent_root_id', $agentRootId)->where($this->table . '.agent_id', "<>",$agentRootId);
}
/**
* Summary of scopeSearchAgentName
* @param mixed $query
* @param mixed $keyword
* @return mixed
*/
public function scopeSearchAgentName($query, $keyword)
{
return $query->where('name', 'LIKE', '%' . $keyword . '%');
}
/**
* Summary of scopeOnlyGrant
* @param mixed $query
* @param mixed $grant
* @return mixed
*/
public function scopeOnlyGrant($query, $grant)
{
return $query->where('se_agent.grant', $grant);
}
} }

Loading…
Cancel
Save