From d09bb3b8519592d8a1c70fe21814f736c96b7a78 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 28 Aug 2016 13:20:16 +0300 Subject: [PATCH] Remove failed/refunded payments from reports --- app/Http/Controllers/ReportController.php | 28 +++++++++++------------ app/Models/Invoice.php | 5 +++- app/Models/Payment.php | 7 ++++++ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index a0ab9ba114cd..486fe832bcc2 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -188,7 +188,8 @@ class ReportController extends BaseController ->where('is_recurring', '=', false); } elseif ($entityType == ENTITY_PAYMENT) { $records->join('invoices', 'invoices.id', '=', 'payments.invoice_id') - ->where('invoices.is_deleted', '=', false); + ->where('invoices.is_deleted', '=', false) + ->whereNotIn('payment_status_id', [PAYMENT_STATUS_VOIDED, PAYMENT_STATUS_FAILED]); } $totals = $records->lists('total'); @@ -361,8 +362,8 @@ class ReportController extends BaseController $reportTotals = []; $payments = Payment::scope() - ->withTrashed() - ->where('is_deleted', '=', false) + ->withArchived() + ->excludeFailed() ->whereHas('client', function($query) { $query->where('is_deleted', '=', false); }) @@ -382,12 +383,12 @@ class ReportController extends BaseController $invoice->present()->invoice_date, $account->formatMoney($invoice->amount, $client), $payment->present()->payment_date, - $account->formatMoney($payment->amount, $client), + $account->formatMoney($payment->getCompletedAmount(), $client), $payment->present()->method, ]; $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $invoice->amount); - $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $payment->amount); + $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $payment->getCompletedAmount()); } return [ @@ -416,15 +417,14 @@ class ReportController extends BaseController ->with('contacts') ->where('is_deleted', '=', false) ->with(['invoices' => function($query) use ($startDate, $endDate) { - $query->where('invoice_date', '>=', $startDate) + $query->invoices() + ->withArchived() + ->where('invoice_date', '>=', $startDate) ->where('invoice_date', '<=', $endDate) - ->where('is_deleted', '=', false) - ->where('invoice_type_id', '=', INVOICE_TYPE_STANDARD) - ->where('is_recurring', '=', false) ->with(['payments' => function($query) { - $query->withTrashed() - ->with('payment_type', 'account_gateway.gateway') - ->where('is_deleted', '=', false); + $query->withArchived() + ->excludeFailed() + ->with('payment_type', 'account_gateway.gateway'); }, 'invoice_items']) ->withTrashed(); }]); @@ -440,10 +440,10 @@ class ReportController extends BaseController $invoice->present()->invoice_date, $account->formatMoney($invoice->amount, $client), $payment ? $payment->present()->payment_date : '', - $payment ? $account->formatMoney($payment->amount, $client) : '', + $payment ? $account->formatMoney($payment->getCompletedAmount(), $client) : '', $payment ? $payment->present()->method : '', ]; - $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $payment ? $payment->amount : 0); + $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $payment ? $payment->getCompletedAmount() : 0); } $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $invoice->amount); diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 9ad5a5d68f2d..e0f26bd132a4 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -216,7 +216,10 @@ class Invoice extends EntityModel implements BalanceAffecting if ($calculate) { $amount = 0; foreach ($this->payments as $payment) { - $amount += $payment->amount; + if ($payment->payment_status_id == PAYMENT_STATUS_VOIDED || $payment->payment_status_id == PAYMENT_STATUS_FAILED) { + continue; + } + $amount += $payment->getCompletedAmount(); } return $amount; } else { diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 04d0b2cca516..fbca9dce8317 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -121,6 +121,13 @@ class Payment extends EntityModel } */ + public function scopeExcludeFailed($query) + { + $query->whereNotIn('payment_status_id', [PAYMENT_STATUS_VOIDED, PAYMENT_STATUS_FAILED]); + + return $query; + } + /** * @return mixed */