Remove failed/refunded payments from reports

This commit is contained in:
Hillel Coren 2016-08-28 13:20:16 +03:00
parent fda1fc439f
commit d09bb3b851
3 changed files with 25 additions and 15 deletions

View File

@ -188,7 +188,8 @@ class ReportController extends BaseController
->where('is_recurring', '=', false); ->where('is_recurring', '=', false);
} elseif ($entityType == ENTITY_PAYMENT) { } elseif ($entityType == ENTITY_PAYMENT) {
$records->join('invoices', 'invoices.id', '=', 'payments.invoice_id') $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'); $totals = $records->lists('total');
@ -361,8 +362,8 @@ class ReportController extends BaseController
$reportTotals = []; $reportTotals = [];
$payments = Payment::scope() $payments = Payment::scope()
->withTrashed() ->withArchived()
->where('is_deleted', '=', false) ->excludeFailed()
->whereHas('client', function($query) { ->whereHas('client', function($query) {
$query->where('is_deleted', '=', false); $query->where('is_deleted', '=', false);
}) })
@ -382,12 +383,12 @@ class ReportController extends BaseController
$invoice->present()->invoice_date, $invoice->present()->invoice_date,
$account->formatMoney($invoice->amount, $client), $account->formatMoney($invoice->amount, $client),
$payment->present()->payment_date, $payment->present()->payment_date,
$account->formatMoney($payment->amount, $client), $account->formatMoney($payment->getCompletedAmount(), $client),
$payment->present()->method, $payment->present()->method,
]; ];
$reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $invoice->amount); $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 [ return [
@ -416,15 +417,14 @@ class ReportController extends BaseController
->with('contacts') ->with('contacts')
->where('is_deleted', '=', false) ->where('is_deleted', '=', false)
->with(['invoices' => function($query) use ($startDate, $endDate) { ->with(['invoices' => function($query) use ($startDate, $endDate) {
$query->where('invoice_date', '>=', $startDate) $query->invoices()
->withArchived()
->where('invoice_date', '>=', $startDate)
->where('invoice_date', '<=', $endDate) ->where('invoice_date', '<=', $endDate)
->where('is_deleted', '=', false)
->where('invoice_type_id', '=', INVOICE_TYPE_STANDARD)
->where('is_recurring', '=', false)
->with(['payments' => function($query) { ->with(['payments' => function($query) {
$query->withTrashed() $query->withArchived()
->with('payment_type', 'account_gateway.gateway') ->excludeFailed()
->where('is_deleted', '=', false); ->with('payment_type', 'account_gateway.gateway');
}, 'invoice_items']) }, 'invoice_items'])
->withTrashed(); ->withTrashed();
}]); }]);
@ -440,10 +440,10 @@ class ReportController extends BaseController
$invoice->present()->invoice_date, $invoice->present()->invoice_date,
$account->formatMoney($invoice->amount, $client), $account->formatMoney($invoice->amount, $client),
$payment ? $payment->present()->payment_date : '', $payment ? $payment->present()->payment_date : '',
$payment ? $account->formatMoney($payment->amount, $client) : '', $payment ? $account->formatMoney($payment->getCompletedAmount(), $client) : '',
$payment ? $payment->present()->method : '', $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); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $invoice->amount);

View File

@ -216,7 +216,10 @@ class Invoice extends EntityModel implements BalanceAffecting
if ($calculate) { if ($calculate) {
$amount = 0; $amount = 0;
foreach ($this->payments as $payment) { 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; return $amount;
} else { } else {

View File

@ -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 * @return mixed
*/ */