From 000512bd28e7f0aeaceb330332365d8da8f0225c Mon Sep 17 00:00:00 2001 From: nocode Date: Tue, 16 Jul 2024 14:22:31 +0700 Subject: [PATCH] finish api load --- Modules/Agents/Services/AgentService.php | 53 ++++++++++++++++++++++-- app/Models/AgentUser.php | 10 ++++- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/Modules/Agents/Services/AgentService.php b/Modules/Agents/Services/AgentService.php index 7ed034b..d4cb856 100644 --- a/Modules/Agents/Services/AgentService.php +++ b/Modules/Agents/Services/AgentService.php @@ -97,12 +97,15 @@ class AgentService $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(); + $endDate = Carbon::createFromFormat('Y-m-d', $input['end_date'])->addDay(); $queryAgent->whereBetween('created_at', [$startDate->toDateString(), $endDate->toDateString()]); } if (!empty($input['keyword'])) { - $queryAgent->where('name', 'LIKE', '%' . $input['keyword'] . '%'); + $queryAgent->where('name', 'LIKE', '%' . $input['keyword'] . '%') + ->orWhere('phone', 'LIKE', '%' . $input['keyword'] . '%') + ->orWhere('email', 'LIKE', '%' . $input['keyword'] . '%'); + ; } $listCustomer = $queryAgent->orderBy('name', 'asc')->get(); if (!empty($listCustomer)) { @@ -150,10 +153,54 @@ class AgentService if (!empty($input['grant'])) { $myAgentDown->onlyGrant($input['grant']); } + $listAgentGet = $myAgentDown->get(); + 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; + } + } + if (!empty($listAgentById)) { + foreach ($listAgentById as $vByAgent) { - dd($myAgentDown->get()); + if (!empty($listAgentById[$vByAgent->grant_parent_id])) { + $agentParent = $listAgentById[$vByAgent->grant_parent_id]; + $upGrant = $agentParent->code ?? null; + } + $data[] = [ + 'id' => $vByAgent->id ?? '', + 'name' => $vByAgent->agent_name ?? '', + 'agents_code' => $vByAgent->code ?? '', + 'created_date' => $vByAgent->created_at ?? '', + 'phone' => $vByAgent->agent_phone ?? '', + 'email' => $vByAgent->email ?? '', + 'grant' => $vByAgent->grant ?? '', + 'up_grant' => $upGrant ?? '' + ]; + } + } 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 diff --git a/app/Models/AgentUser.php b/app/Models/AgentUser.php index 17ce61b..0ab82fe 100755 --- a/app/Models/AgentUser.php +++ b/app/Models/AgentUser.php @@ -32,7 +32,13 @@ class AgentUser extends Model 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'); + ->select('se_agent_user.*', + 'se_agent.name as agent_name', + 'se_agent.parent_path', + 'se_agent.grant', + 'se_agent.phone as agent_phone', + // 'se_agent.email as agent_email', + 'se_agent.type'); } /** * Summary of scopeBetweenDates @@ -53,7 +59,7 @@ class AgentUser extends Model */ public function scopeByAgentRoot($query, $agentRootId) { - return $query->where($this->table . '.agent_root_id', $agentRootId)->where($this->table . '.agent_id', "<>",$agentRootId); + return $query->where($this->table . '.agent_root_id', $agentRootId); } /** * Summary of scopeSearchAgentName