From 043f854a3734317f8785c45251db6c1029486035 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 14 Apr 2023 11:18:50 +1000 Subject: [PATCH] Reporting season --- app/Export/CSV/BaseExport.php | 4 + app/Services/Report/ARDetail.php | 24 ++++++ app/Services/Report/ARSummary.php | 23 +++++ app/Services/Report/ClientSales.php | 21 +++++ app/Services/Report/CustomerBalance.php | 20 +++++ app/Services/Report/UserSales.php | 108 ++++++++++++++++++++++++ lang/en/texts.php | 1 + 7 files changed, 201 insertions(+) create mode 100644 app/Services/Report/ARDetail.php create mode 100644 app/Services/Report/ARSummary.php create mode 100644 app/Services/Report/ClientSales.php create mode 100644 app/Services/Report/CustomerBalance.php create mode 100644 app/Services/Report/UserSales.php diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 89af8e654ab8..7dd4b5a9091e 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -91,6 +91,10 @@ class BaseExport $this->start_date = (new \Carbon\Carbon('-6 months'))->firstOfQuarter()->format('Y-m-d'); $this->end_date = (new \Carbon\Carbon('-6 months'))->lastOfQuarter()->format('Y-m-d'); return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-6 months'))->firstOfQuarter(), (new \Carbon\Carbon('-6 months'))->lastOfQuarter()])->orderBy($this->date_key, 'ASC'); + case 'last365_days': + $this->start_date = now()->startOfDay()->subDays(365)->format('Y-m-d'); + $this->end_date = now()->startOfDay()->format('Y-m-d'); + return $query->whereBetween($this->date_key, [now()->subDays(365), now()])->orderBy($this->date_key, 'ASC'); case 'this_year': $this->start_date = now()->startOfYear()->format('Y-m-d'); $this->end_date = now()->format('Y-m-d'); diff --git a/app/Services/Report/ARDetail.php b/app/Services/Report/ARDetail.php new file mode 100644 index 000000000000..d26e3febe67b --- /dev/null +++ b/app/Services/Report/ARDetail.php @@ -0,0 +1,24 @@ +company->db); + App::forgetInstance('translator'); + App::setLocale($this->company->locale()); + $t = app('translator'); + $t->replace(Ninja::transformTranslations($this->company->settings)); + + $this->csv = Writer::createFromString(); + + $query = Invoice::query() + ->withTrashed() + ->where('company_id', $this->company->id) + ->where('is_deleted', 0) + ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID]); + + $query = $this->addDateRange($query); + + $query = $this->filterByClients($query); + + $this->csv->insertOne([ctrans('texts.sales_report_header', ['client' => $this->client_description, 'start_date' => $this->start_date, 'end_date' => $this->end_date])]); + $this->csv->insertOne($this->buildHeader()); + + $users = $this->company->users; + + $report = $users->map(function ($user) use($query){ + + $new_query = $query; + $new_query->where('user_id', $user->id); + + return [ + $user->present()->name(), + $new_query->count(), + Number::formatMoney($new_query->sum('amount'), $this->company), + Number::formatMoney($new_query->sum('total_taxes'), $this->company), + ]; + + })->toArray(); + + $key_values = array_column($report, 1); + array_multisort($key_values, SORT_DESC, $report); + + $this->csv->insertAll($report); + + return $this->csv->toString(); + + } + + public function buildHeader(): array + { + return [ + ctrans('texts.name'), + ctrans('texts.invoices'), + ctrans('texts.invoice_amount'), + ctrans('texts.total_taxes'), + ]; + } + +} diff --git a/lang/en/texts.php b/lang/en/texts.php index bb34678bfb24..cdaa431c3f11 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -5032,6 +5032,7 @@ $LANG = array( 'quote_product_columns' => 'Quote Product Columns', 'vendors' => 'Vendors', 'product_sales' => 'Product Sales', + 'sales_report_header' => 'User sales report for client/s :client from :start_date to :end_date', );