From 2293ab1c168952c8824aa7bf437f7e7496cd74bc Mon Sep 17 00:00:00 2001 From: omair saleh Date: Thu, 23 Dec 2021 14:41:04 +0800 Subject: [PATCH] update customer reports --- .../Eloquent/Filters/WithTotalPayments.php | 2 +- app/Http/Resources/CompanyResource.php | 7 ++++- .../companies/elements/CompanyComponent.vue | 20 ++++++++++--- .../views/pages/customers/index.blade.php | 28 ++++++++++++++++++- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/app/Classes/General/Eloquent/Filters/WithTotalPayments.php b/app/Classes/General/Eloquent/Filters/WithTotalPayments.php index 48ca169a..9721f8d7 100644 --- a/app/Classes/General/Eloquent/Filters/WithTotalPayments.php +++ b/app/Classes/General/Eloquent/Filters/WithTotalPayments.php @@ -21,6 +21,6 @@ class WithTotalPayments implements Filter { 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')])->groupBy(['companies.id']); + })->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/Http/Resources/CompanyResource.php b/app/Http/Resources/CompanyResource.php index 19d6d04f..54ab047d 100644 --- a/app/Http/Resources/CompanyResource.php +++ b/app/Http/Resources/CompanyResource.php @@ -40,9 +40,14 @@ class CompanyResource extends JsonResource 'employee' => new UserResource(Auth::user()->type === RoleTypes::USER ? $this->employees()->where('email', '=', Auth::user()->email)->first() : $this->employees()->orderBy('id', 'DESC')->first()), 'identification' => new DocumentResource($this->documents->whereIn('document_type', DocumentType::IDENTIFICATION_DOCUMENTS)->first()), 'bookings' => $this->whenLoaded('bookings', $this->bookings()->orderBy('id', 'DESC')->get(), []), + 'confirmed_bookings' => $this->bookings()->whereHas('transactions', function ($query){ + $query->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + })->count(), 'total_payments' => (float) $totalPayments, 'average_spending_per_day' => (float) $totalPayments / ($this->created_at->diff(Carbon::now())->days === 0 ? 1 : $this->created_at->diff(Carbon::now())->days), - 'average_spending_per_booking' => (float) $totalPayments / ($this->created_at->diff(Carbon::now())->days === 0 ? 1 : $this->created_at->diff(Carbon::now())->days), + 'average_spending_per_booking' => (float) $totalPayments > 0 ? $totalPayments / $this->bookings()->whereHas('transactions', function ($query){ + $query->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + })->count() : $totalPayments, 'last_payment' => $lastPayment ? $lastPayment->created_at->diffForHumans() : 'No Payments', 'personal_banks' => BankResource::collection($this->banks->where('type', BankAccountType::PERSONAL)), 'recipient_banks' => [ diff --git a/resources/assets/vue/components/companies/elements/CompanyComponent.vue b/resources/assets/vue/components/companies/elements/CompanyComponent.vue index 7848cb91..4e194aba 100644 --- a/resources/assets/vue/components/companies/elements/CompanyComponent.vue +++ b/resources/assets/vue/components/companies/elements/CompanyComponent.vue @@ -12,7 +12,7 @@ -
+
Payments
@@ -27,7 +27,7 @@
-
Average Spending
+
Average Daily Spending
@@ -37,6 +37,18 @@
+
+
+
Average Booking Spending
+
+
+
+
+
MYR {{(Math.round((this.item.average_spending_per_booking + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
+
+
+
+
Last Payment
@@ -48,7 +60,7 @@
-
+
Bookings
@@ -56,7 +68,7 @@
-
{{this.item.bookings.length}}
+
{{this.item.confirmed_bookings}} / {{this.item.bookings.length}}
diff --git a/resources/views/pages/customers/index.blade.php b/resources/views/pages/customers/index.blade.php index 93a05ac5..c5807fd5 100644 --- a/resources/views/pages/customers/index.blade.php +++ b/resources/views/pages/customers/index.blade.php @@ -43,7 +43,26 @@
-
Average Spending
+
Average Daily Spending
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
Average Booking Spending
@@ -70,6 +89,13 @@
+
+ + + +