From f7d5a132d4084ad41761b8440454b8c0981ba2b6 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 30 Nov 2017 12:49:54 +0200 Subject: [PATCH] Support filtering invoice/product reports by multiple statuses --- app/Http/Controllers/ReportController.php | 2 +- app/Models/Invoice.php | 32 ++++++++++++++ app/Ninja/Reports/InvoiceReport.php | 17 ++------ app/Ninja/Reports/ProductReport.php | 15 ++----- .../views/reports/report_builder.blade.php | 42 ++++++++++++++++--- 5 files changed, 76 insertions(+), 32 deletions(-) diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index a475cd04f5..21a83f0818 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -99,7 +99,7 @@ class ReportController extends BaseController $isExport = $action == 'export'; $config = [ 'date_field' => $dateField, - 'invoice_status' => request()->invoice_status, + 'status_ids' => request()->status_ids, 'group_dates_by' => request()->group_dates_by, 'document_filter' => request()->document_filter, 'currency_type' => request()->currency_type, diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 184b32f1d7..5ba815430d 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -460,6 +460,38 @@ class Invoice extends EntityModel implements BalanceAffecting return $query->where('invoice_type_id', '=', $typeId); } + /** + * @param $query + * @param $typeId + * + * @return mixed + */ + public function scopeStatusIds($query, $statusIds) + { + if (! $statusIds || (is_array($statusIds) && ! count($statusIds))) { + return $query; + } + + return $query->where(function ($query) use ($statusIds) { + foreach ($statusIds as $statusId) { + $query->orWhere('invoice_status_id', '=', $statusId); + } + if (in_array(INVOICE_STATUS_UNPAID, $statusIds)) { + $query->orWhere(function ($query) { + $query->where('balance', '>', 0) + ->where('is_public', '=', true); + }); + } + if (in_array(INVOICE_STATUS_OVERDUE, $statusIds)) { + $query->orWhere(function ($query) { + $query->where('balance', '>', 0) + ->where('due_date', '<', date('Y-m-d')) + ->where('is_public', '=', true); + }); + } + }); + } + /** * @param $typeId * diff --git a/app/Ninja/Reports/InvoiceReport.php b/app/Ninja/Reports/InvoiceReport.php index 2341ae6f2d..f8f5e24ffa 100644 --- a/app/Ninja/Reports/InvoiceReport.php +++ b/app/Ninja/Reports/InvoiceReport.php @@ -22,21 +22,17 @@ class InvoiceReport extends AbstractReport public function run() { $account = Auth::user()->account; - $status = $this->options['invoice_status']; + $statusIds = $this->options['status_ids']; $exportFormat = $this->options['export_format']; $clients = Client::scope() ->orderBy('name') ->withArchived() ->with('contacts') - ->with(['invoices' => function ($query) use ($status) { - if ($status == 'draft') { - $query->whereIsPublic(false); - } elseif (in_array($status, ['paid', 'unpaid', 'sent'])) { - $query->whereIsPublic(true); - } + ->with(['invoices' => function ($query) use ($statusIds) { $query->invoices() ->withArchived() + ->statusIds($statusIds) ->where('invoice_date', '>=', $this->startDate) ->where('invoice_date', '<=', $this->endDate) ->with(['payments' => function ($query) { @@ -65,17 +61,12 @@ class InvoiceReport extends AbstractReport foreach ($client->invoices as $invoice) { $payments = count($invoice->payments) ? $invoice->payments : [false]; foreach ($payments as $payment) { - if (! $payment && $status == 'paid') { - continue; - } elseif ($payment && $status == 'unpaid') { - continue; - } $this->data[] = [ $this->isExport ? $client->getDisplayName() : $client->present()->link, $this->isExport ? $invoice->invoice_number : $invoice->present()->link, $invoice->present()->invoice_date, $account->formatMoney($invoice->amount, $client), - $invoice->present()->status(), + $invoice->statusLabel(), $payment ? $payment->present()->payment_date : '', $payment ? $account->formatMoney($payment->getCompletedAmount(), $client) : '', $payment ? $payment->present()->method : '', diff --git a/app/Ninja/Reports/ProductReport.php b/app/Ninja/Reports/ProductReport.php index 8f1cf85a13..f9dd811adc 100644 --- a/app/Ninja/Reports/ProductReport.php +++ b/app/Ninja/Reports/ProductReport.php @@ -22,20 +22,16 @@ class ProductReport extends AbstractReport public function run() { $account = Auth::user()->account; - $status = $this->options['invoice_status']; + $statusIds = $this->options['status_ids']; $clients = Client::scope() ->orderBy('name') ->withArchived() ->with('contacts') - ->with(['invoices' => function ($query) use ($status) { - if ($status == 'draft') { - $query->whereIsPublic(false); - } elseif (in_array($status, ['paid', 'unpaid', 'sent'])) { - $query->whereIsPublic(true); - } + ->with(['invoices' => function ($query) use ($statusIds) { $query->invoices() ->withArchived() + ->statusIds($statusIds) ->where('invoice_date', '>=', $this->startDate) ->where('invoice_date', '<=', $this->endDate) ->with(['invoice_items']); @@ -43,11 +39,6 @@ class ProductReport extends AbstractReport foreach ($clients->get() as $client) { foreach ($client->invoices as $invoice) { - if (! $invoice->isPaid() && $status == 'paid') { - continue; - } elseif ($invoice->isPaid() && $status == 'unpaid') { - continue; - } foreach ($invoice->invoice_items as $item) { $this->data[] = [ $this->isExport ? $client->getDisplayName() : $client->present()->link, diff --git a/resources/views/reports/report_builder.blade.php b/resources/views/reports/report_builder.blade.php index ba6f51e922..df3a08627a 100644 --- a/resources/views/reports/report_builder.blade.php +++ b/resources/views/reports/report_builder.blade.php @@ -9,11 +9,19 @@ + + + @stop @@ -146,12 +154,17 @@