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
|
|
|
|
{
|
2018-01-21 14:53:43 +01:00
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
return [
|
2018-01-23 19:32:11 +01:00
|
|
|
'client' => [],
|
|
|
|
'invoice_number' => [],
|
|
|
|
'invoice_date' => [],
|
|
|
|
'amount' => [],
|
|
|
|
'payment_date' => [],
|
|
|
|
'paid' => [],
|
|
|
|
'method' => [],
|
2018-01-21 14:53:43 +01:00
|
|
|
'private_notes' => ['columnSelector-false'],
|
2018-01-26 13:11:23 +01:00
|
|
|
'user' => ['columnSelector-false'],
|
2018-01-21 14:53:43 +01:00
|
|
|
];
|
|
|
|
}
|
2017-01-22 11:09:29 +01:00
|
|
|
|
|
|
|
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 = [];
|
2018-02-28 15:57:10 +01:00
|
|
|
$subgroup = $this->options['subgroup'];
|
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);
|
|
|
|
})
|
2018-01-26 13:11:23 +01:00
|
|
|
->with('client.contacts', 'invoice', 'payment_type', 'account_gateway.gateway', 'user')
|
2017-01-22 11:09:29 +01:00
|
|
|
->where('payment_date', '>=', $this->startDate)
|
|
|
|
->where('payment_date', '<=', $this->endDate);
|
|
|
|
|
2018-01-24 19:19:53 +01:00
|
|
|
$lastInvoiceId = 0;
|
2017-01-22 11:09:29 +01:00
|
|
|
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,
|
2019-01-30 11:45:46 +01:00
|
|
|
$this->isExport ? $invoice->invoice_date : $invoice->present()->invoice_date,
|
2018-01-24 19:19:53 +01:00
|
|
|
$lastInvoiceId == $invoice->id ? '' : $account->formatMoney($invoice->amount, $client),
|
2019-01-30 11:45:46 +01:00
|
|
|
$this->isExport ? $payment->payment_date : $payment->present()->payment_date,
|
2017-11-19 10:55:01 +01:00
|
|
|
$amount,
|
2017-01-22 11:09:29 +01:00
|
|
|
$payment->present()->method,
|
2018-01-21 14:53:43 +01:00
|
|
|
$payment->private_notes,
|
2018-01-26 13:11:23 +01:00
|
|
|
$payment->user->getDisplayName(),
|
2017-01-22 11:09:29 +01:00
|
|
|
];
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 19:19:53 +01:00
|
|
|
|
2018-02-28 15:57:10 +01:00
|
|
|
if ($subgroup == 'method') {
|
|
|
|
$dimension = $payment->present()->method;
|
|
|
|
} else {
|
|
|
|
$dimension = $this->getDimension($payment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$convertedAmount = $currencyType == 'converted' ? ($invoice->amount * $payment->exchange_rate) : $invoice->amount;
|
|
|
|
$this->addChartData($dimension, $payment->payment_date, $convertedAmount);
|
|
|
|
|
2018-01-24 19:19:53 +01:00
|
|
|
$lastInvoiceId = $invoice->id;
|
2017-01-22 11:09:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|