From efc5811403075d41a171e64d386df4b238b03590 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Wed, 16 Feb 2022 15:09:52 +0800 Subject: [PATCH] filter --- .../General/Eloquent/Filters/Frequency.php | 19 ++++++++++++++ .../General/Eloquent/Filters/Monetary.php | 25 +++++++++++++++++++ .../General/Eloquent/Filters/Recency.php | 22 ++++++++++++++++ .../Eloquent/Filters/WithTotalPayments.php | 16 +++++++++--- .../ControllersLogic/ListCompaniesLogic.php | 7 ++++-- app/Http/Resources/CompanyResource.php | 1 + .../views/pages/customers/index.blade.php | 8 ++++-- 7 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 app/Classes/General/Eloquent/Filters/Frequency.php create mode 100644 app/Classes/General/Eloquent/Filters/Monetary.php create mode 100644 app/Classes/General/Eloquent/Filters/Recency.php diff --git a/app/Classes/General/Eloquent/Filters/Frequency.php b/app/Classes/General/Eloquent/Filters/Frequency.php new file mode 100644 index 00000000..3eb74340 --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/Frequency.php @@ -0,0 +1,19 @@ +whereHas('bookings', function ($query){}, '>=', $value); + } +} \ No newline at end of file diff --git a/app/Classes/General/Eloquent/Filters/Monetary.php b/app/Classes/General/Eloquent/Filters/Monetary.php new file mode 100644 index 00000000..5c91b248 --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/Monetary.php @@ -0,0 +1,25 @@ +whereHas('transactions', function ($query) use ($value){ + $query->where('transactions.type', TransactionType::PAYMENT)->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + $query->havingRaw('SUM(amount) >= ' . $value); + }); + } +} \ No newline at end of file diff --git a/app/Classes/General/Eloquent/Filters/Recency.php b/app/Classes/General/Eloquent/Filters/Recency.php new file mode 100644 index 00000000..75e9ea34 --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/Recency.php @@ -0,0 +1,22 @@ +where('transactions.created_at', '>=', $value); + } +} \ No newline at end of file diff --git a/app/Classes/General/Eloquent/Filters/WithTotalPayments.php b/app/Classes/General/Eloquent/Filters/WithTotalPayments.php index 9721f8d7..015221fd 100644 --- a/app/Classes/General/Eloquent/Filters/WithTotalPayments.php +++ b/app/Classes/General/Eloquent/Filters/WithTotalPayments.php @@ -19,8 +19,18 @@ class WithTotalPayments implements Filter */ public static function apply(Builder $builder, $value) { - return $builder->leftJoin('bookings', 'companies.id', '=', 'bookings.company_id')->rightJoin('transactions', function ($join) { - $join->on('bookings.id', '=', 'transactions.owner_id')->where('owner_type', '=', Booking::class)->where('transactions.type', TransactionType::PAYMENT)->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); - })->addSelect(['companies.*', DB::raw('SUM(transactions.amount) as total_payments'), DB::raw('SUM(transactions.amount / DATEDIFF(companies.created_at, CURRENT_DATE)) as spending_average_per_day'), DB::raw('SUM(transactions.amount) / count(distinct bookings.id) as spending_average_booking')])->groupBy(['companies.id']); + return $builder->leftJoin('bookings', 'companies.id', '=', 'bookings.company_id') + ->rightJoin('transactions', function ($join) { + $join->on('bookings.id', '=', 'transactions.owner_id') + ->where('owner_type', '=', Booking::class) + ->where('transactions.type', TransactionType::PAYMENT) + ->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + })->addSelect([ + 'companies.*', + DB::raw('SUM(transactions.amount) as total_payments'), + DB::raw('SUM(transactions.amount / DATEDIFF(companies.created_at, CURRENT_DATE)) as spending_average_per_day'), + DB::raw('SUM(transactions.amount) / count(distinct bookings.id) as spending_average_booking') + ]) + ->groupBy(['companies.id']); } } \ No newline at end of file diff --git a/app/Classes/Modules/Companies/ControllersLogic/ListCompaniesLogic.php b/app/Classes/Modules/Companies/ControllersLogic/ListCompaniesLogic.php index 80b15c1c..8e19c737 100644 --- a/app/Classes/Modules/Companies/ControllersLogic/ListCompaniesLogic.php +++ b/app/Classes/Modules/Companies/ControllersLogic/ListCompaniesLogic.php @@ -53,9 +53,12 @@ class ListCompaniesLogic extends AbstractControllerLogic { $this->canListCompanies->passes(); + // dd($request->input('filters')); + // frequency = active bookings + // monetary = total spending/payment $query = $this->listsCompanies->execute($this->listsCompanies->deserializeFilters($request->input('filters'))); + + // dd($query); return $this->collectionResponse(CompanyResource::collection($query)); - } - } \ No newline at end of file diff --git a/app/Http/Resources/CompanyResource.php b/app/Http/Resources/CompanyResource.php index ee1b96a0..80ae4a57 100644 --- a/app/Http/Resources/CompanyResource.php +++ b/app/Http/Resources/CompanyResource.php @@ -28,6 +28,7 @@ class CompanyResource extends JsonResource { $lastPayment = $this->transactions()->where('transactions.type', TransactionType::PAYMENT)->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->orderBy('id', 'DESC')->first(); $totalPayments = $this->transactions()->where('transactions.type', TransactionType::PAYMENT)->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->sum('amount'); + return [ 'id' => $this->id, 'name' => $this->name, diff --git a/resources/views/pages/customers/index.blade.php b/resources/views/pages/customers/index.blade.php index c5807fd5..79dd6982 100644 --- a/resources/views/pages/customers/index.blade.php +++ b/resources/views/pages/customers/index.blade.php @@ -76,14 +76,18 @@
- +
- +