1, 'inactive' => 2, 'de_active' => 2, 'pending' => 0, 'closed' => 2, 'suspended' => 3, 'banned' => 3, ]; public function getModel() { return Customer::class; } public function filter( array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'], bool $returnQuery = false ) { $model = $this->model->with($relationships)->select($select); if (isset($data['filter']) && ! empty($data['filter'])) { $model = $model->where(function ($query) use ($data) { $filter = trim($data['filter']); $query->where('fullname', 'LIKE', "%{$filter}%") ->orWhere('username', 'LIKE', "%{$filter}%") ->orWhere('company', 'LIKE', "%{$filter}%") ->orWhere('email', 'LIKE', "%{$filter}%") ->orWhere('phone', 'LIKE', "%{$filter}%"); }); } if (isset($data['name']) && ! empty($data['name'])) { $model = $model->where(function ($query) use ($data) { $name = trim($data['name']); $query->where("fullname", "LIKE", "%$name%"); $query->orWhere("company", "LIKE", "%$name%"); }); } if (isset($data['email']) && ! empty($data['email'])) { $model = $model->where("email", "LIKE", "%" . $data['email'] . "%"); } if (isset($data['phone']) && ! empty($data['phone'])) { $model = $model->where("phone", "LIKE", "%" . $data['phone'] . "%"); } if (isset($data['company']) && ! empty($data['company'])) { $model = $model->where('company', 'LIKE', '%' . $data['company'] . '%'); } if (isset($data['city']) && ! empty($data['city'])) { $model = $model->where('city', 'LIKE', '%' . $data['city'] . '%'); } if (isset($data['country']) && ! empty($data['country'])) { $model = $model->where('country', 'LIKE', '%' . $data['country'] . '%'); } if (isset($data['note']) && ! empty($data['note'])) { $model = $model->where('note', 'LIKE', '%' . $data['note'] . '%'); } if (isset($data['total_min']) && $data['total_min'] !== '') { $model = $model->where('total', '>=', (int) $data['total_min']); } if (isset($data['total_max']) && $data['total_max'] !== '') { $model = $model->where('total', '<=', (int) $data['total_max']); } if (isset($data['created_from']) && ! empty($data['created_from'])) { $model = $model->whereDate('created_at', '>=', $data['created_from']); } if (isset($data['created_to']) && ! empty($data['created_to'])) { $model = $model->whereDate('created_at', '<=', $data['created_to']); } if (isset($data['service_type']) && ! empty($data['service_type'])) { $types = is_array($data['service_type']) ? $data['service_type'] : array_filter(array_map('trim', explode(',', (string) $data['service_type']))); $simpleMap = [ 'hosting' => 'hosting', 'vps' => 'vps', 'domain' => 'domain', 'service' => 'service', ]; $simpleRelations = array_values(array_intersect_key($simpleMap, array_flip($types))); $hasDns = in_array('dns', $types, true); if (! empty($simpleRelations) || $hasDns) { $dnsIds = $hasDns ? \Illuminate\Support\Facades\DB::table('dns_domain_user')->pluck('user_id') : collect(); $model = $model->where(function ($q) use ($simpleRelations, $dnsIds) { foreach ($simpleRelations as $relation) { $q->orWhereHas($relation); } if ($dnsIds->isNotEmpty()) { $q->orWhereIn('customers.id', $dnsIds); } }); } } if (isset($data['domain']) && ! empty($data['domain'])) { $domain = trim((string) $data['domain']); $dnsCustomerIds = \Illuminate\Support\Facades\DB::table('dns_domain_user') ->join('dns_manager_domain', 'dns_manager_domain.id', '=', 'dns_domain_user.domain_id') ->where('dns_manager_domain.domain_name', 'like', "%{$domain}%") ->pluck('dns_domain_user.user_id'); $model = $model->where(function ($q) use ($domain, $dnsCustomerIds) { $q->whereHas('domain', function ($sub) use ($domain) { $sub->where('name', 'like', "%{$domain}%"); }); if ($dnsCustomerIds->isNotEmpty()) { $q->orWhereIn('customers.id', $dnsCustomerIds); } }); } if (isset($data['status']) && ! empty($data['status']) && $data['status'] != 'full') { $status = (string) $data['status']; $statusValue = is_numeric($status) ? (int) $status : (self::STATUS_MAP[$status] ?? null); if ($statusValue !== null) { $model = $model->where('status', $statusValue); } } if (isset($data['relations'])) { foreach ($data['relations'] as $key => $value) { $model = $model->whereHas($key, function ($query) use ($value) { if (isset($value['condition'])) { switch (strtoupper($value['condition'])) { case "IN": $query->whereIn($value['key'], $value['value']); break; case "NotIn": $query->whereNotIn($value['key'], $value['value']); break; default: $query->where($value['key'], $value['condition'], $value['value']); break; } } else { $query->where($value['key'], $value['value']); } }); } } if (count($withCount) > 0) { $model = $model->withCount($withCount); } if ($returnQuery) { return $model; } if (isset($data['page'])) { return $model->paginate(15); } return $model->get(); } public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = []) { $model = $this->model; if (count($relationships) > 0) { foreach ($relationships as $relationship) { if (method_exists($model, $relationship)) { $model = $model->with($relationship); } } } // Xử lý withCount if (count($withCount) > 0) { foreach ($withCount as $key => $items) { if (is_array($items)) { foreach ($items as $alias => $item) { $model = $model->withCount([ $key . ' as ' . $alias => function ($query) use ($item) { $query->where($item['key'], $item['condition'], $item['value']); } ]); } } else { $model = $model->withCount($key); } } } // Xử lý withSum if (count($withSum) > 0) { foreach ($withSum as $key => $items) { if (is_array($items)) { foreach ($items as $alias => $item) { $model = $model->withSum([ $key . ' as ' . $alias => function ($query) use ($item) { $query->where($item['key'], $item['condition'], $item['value']); } ], $item['sum']); } } else { $model = $model->withSum($key); } } } return $model->find($id); } public function findBy(string $key = '', string $value = '') { if (empty($key) || empty($value)) { return false; } return $this->model->where($key, $value)->first(); } public function onlyTrashed(array $relationships = [], array $withCount = []) { $query = $this->model->with($relationships); if (count($withCount) > 0) { $query = $query->withCount($withCount); } return $query->onlyTrashed(); } }