2017-01-22 11:09:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ninja\Reports;
|
|
|
|
|
|
|
|
use App\Models\Payment;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Auth;
|
2017-11-19 10:55:01 +01:00
|
|
|
use Utils;
|
2017-01-22 11:09:29 +01:00
|
|
|
|
|
|
|
class PaymentReport extends AbstractReport
|
|
|
|
{
|
|
|
|
public $columns = [
|
|
|
|
'client',
|
|
|
|
'invoice_number',
|
|
|
|
'invoice_date',
|
|
|
|
'amount',
|
|
|
|
'payment_date',
|
|
|
|
'paid',
|
|
|
|
'method',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$account = Auth::user()->account;
|
2017-11-19 10:55:01 +01:00
|
|
|
$currencyType = $this->options['currency_type'];
|
2017-10-17 13:15:34 +02:00
|
|
|
$invoiceMap = [];
|
2017-01-22 11:09:29 +01:00
|
|
|
|
|
|
|
$payments = Payment::scope()
|
2017-03-31 08:01:07 +02:00
|
|
|
->orderBy('payment_date', 'desc')
|
2017-01-22 11:09:29 +01:00
|
|
|
->withArchived()
|
|
|
|
->excludeFailed()
|
2017-01-30 17:05:31 +01:00
|
|
|
->whereHas('client', function ($query) {
|
2017-01-22 11:09:29 +01:00
|
|
|
$query->where('is_deleted', '=', false);
|
|
|
|
})
|
2017-01-30 17:05:31 +01:00
|
|
|
->whereHas('invoice', function ($query) {
|
2017-01-22 11:09:29 +01:00
|
|
|
$query->where('is_deleted', '=', false);
|
|
|
|
})
|
|
|
|
->with('client.contacts', 'invoice', 'payment_type', 'account_gateway.gateway')
|
|
|
|
->where('payment_date', '>=', $this->startDate)
|
|
|
|
->where('payment_date', '<=', $this->endDate);
|
|
|
|
|
|
|
|
foreach ($payments->get() as $payment) {
|
|
|
|
$invoice = $payment->invoice;
|
|
|
|
$client = $payment->client;
|
2017-11-19 10:55:01 +01:00
|
|
|
$amount = $payment->getCompletedAmount();
|
|
|
|
|
|
|
|
if ($currencyType == 'converted') {
|
|
|
|
$amount *= $payment->exchange_rate;
|
|
|
|
$this->addToTotals($payment->exchange_currency_id, 'paid', $amount);
|
|
|
|
$amount = Utils::formatMoney($amount, $payment->exchange_currency_id);
|
|
|
|
} else {
|
|
|
|
$this->addToTotals($client->currency_id, 'paid', $amount);
|
|
|
|
$amount = $account->formatMoney($amount, $client);
|
|
|
|
}
|
|
|
|
|
2017-01-22 11:09:29 +01:00
|
|
|
$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),
|
|
|
|
$payment->present()->payment_date,
|
2017-11-19 10:55:01 +01:00
|
|
|
$amount,
|
2017-01-22 11:09:29 +01:00
|
|
|
$payment->present()->method,
|
|
|
|
];
|
|
|
|
|
2017-10-17 13:15:34 +02:00
|
|
|
if (! isset($invoiceMap[$invoice->id])) {
|
|
|
|
$invoiceMap[$invoice->id] = true;
|
|
|
|
|
2017-11-19 10:55:01 +01:00
|
|
|
if ($currencyType == 'converted') {
|
|
|
|
$this->addToTotals($payment->exchange_currency_id, 'amount', $invoice->amount * $payment->exchange_rate);
|
|
|
|
} else {
|
|
|
|
$this->addToTotals($client->currency_id, 'amount', $invoice->amount);
|
|
|
|
}
|
|
|
|
}
|
2017-01-22 11:09:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|