2017-01-22 11:09:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ninja\Reports;
|
|
|
|
|
|
|
|
use App\Models\Client;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Auth;
|
2017-10-19 10:18:32 +02:00
|
|
|
use Barracuda\ArchiveStream\Archive;
|
2018-01-24 19:19:53 +01:00
|
|
|
use App\Models\TaxRate;
|
2017-01-22 11:09:29 +01:00
|
|
|
|
|
|
|
class InvoiceReport extends AbstractReport
|
|
|
|
{
|
2018-01-21 14:53:43 +01:00
|
|
|
public function getColumns()
|
|
|
|
{
|
2018-01-21 15:05:35 +01:00
|
|
|
$columns = [
|
2018-01-23 19:32:11 +01:00
|
|
|
'client' => [],
|
|
|
|
'invoice_number' => [],
|
|
|
|
'invoice_date' => [],
|
|
|
|
'amount' => [],
|
|
|
|
'status' => [],
|
|
|
|
'payment_date' => [],
|
|
|
|
'paid' => [],
|
|
|
|
'method' => [],
|
2018-02-13 20:55:33 +01:00
|
|
|
'po_number' => ['columnSelector-false'],
|
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
|
|
|
];
|
2018-01-21 15:05:35 +01:00
|
|
|
|
2018-01-24 19:19:53 +01:00
|
|
|
if (TaxRate::scope()->count()) {
|
|
|
|
$columns['tax'] = ['columnSelector-false'];
|
|
|
|
}
|
|
|
|
|
2018-01-21 15:05:35 +01:00
|
|
|
$account = auth()->user()->account;
|
|
|
|
|
2018-04-04 15:24:59 +02:00
|
|
|
if ($account->customLabel('invoice_text1')) {
|
|
|
|
$columns[$account->present()->customLabel('invoice_text1')] = ['columnSelector-false', 'custom'];
|
2018-01-21 15:05:35 +01:00
|
|
|
}
|
2018-04-04 15:24:59 +02:00
|
|
|
if ($account->customLabel('invoice_text1')) {
|
|
|
|
$columns[$account->present()->customLabel('invoice_text2')] = ['columnSelector-false', 'custom'];
|
2018-01-21 15:05:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $columns;
|
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-30 11:49:54 +01:00
|
|
|
$statusIds = $this->options['status_ids'];
|
2017-10-19 10:18:32 +02:00
|
|
|
$exportFormat = $this->options['export_format'];
|
2018-02-28 15:57:10 +01:00
|
|
|
$subgroup = $this->options['subgroup'];
|
2018-01-24 19:34:14 +01:00
|
|
|
$hasTaxRates = TaxRate::scope()->count();
|
2017-01-22 11:09:29 +01:00
|
|
|
|
|
|
|
$clients = Client::scope()
|
2017-03-31 08:01:07 +02:00
|
|
|
->orderBy('name')
|
2017-01-22 11:09:29 +01:00
|
|
|
->withArchived()
|
2018-01-26 13:11:23 +01:00
|
|
|
->with('contacts', 'user')
|
2017-11-30 11:49:54 +01:00
|
|
|
->with(['invoices' => function ($query) use ($statusIds) {
|
2017-01-22 11:09:29 +01:00
|
|
|
$query->invoices()
|
|
|
|
->withArchived()
|
2017-11-30 11:49:54 +01:00
|
|
|
->statusIds($statusIds)
|
2017-01-22 11:09:29 +01:00
|
|
|
->where('invoice_date', '>=', $this->startDate)
|
|
|
|
->where('invoice_date', '<=', $this->endDate)
|
2017-01-30 17:05:31 +01:00
|
|
|
->with(['payments' => function ($query) {
|
|
|
|
$query->withArchived()
|
2017-01-22 11:09:29 +01:00
|
|
|
->excludeFailed()
|
|
|
|
->with('payment_type', 'account_gateway.gateway');
|
2018-01-24 19:34:14 +01:00
|
|
|
}, 'invoice_items', 'invoice_status']);
|
2017-01-22 11:09:29 +01:00
|
|
|
}]);
|
|
|
|
|
2017-10-19 10:18:32 +02:00
|
|
|
|
|
|
|
if ($this->isExport && $exportFormat == 'zip') {
|
2018-02-21 14:49:18 +01:00
|
|
|
if (! extension_loaded('GMP')) {
|
|
|
|
die(trans('texts.gmp_required'));
|
|
|
|
}
|
|
|
|
|
2017-10-19 10:18:32 +02:00
|
|
|
$zip = Archive::instance_by_useragent(date('Y-m-d') . '_' . str_replace(' ', '_', trans('texts.invoice_documents')));
|
|
|
|
foreach ($clients->get() as $client) {
|
|
|
|
foreach ($client->invoices as $invoice) {
|
|
|
|
foreach ($invoice->documents as $document) {
|
2018-01-02 21:54:10 +01:00
|
|
|
$name = sprintf('%s_%s_%s', $invoice->invoice_date ?: date('Y-m-d'), $invoice->present()->titledName, $document->name);
|
2017-10-19 10:18:32 +02:00
|
|
|
$zip->add_file($name, $document->getRaw());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$zip->finish();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2017-01-22 11:09:29 +01:00
|
|
|
foreach ($clients->get() as $client) {
|
|
|
|
foreach ($client->invoices as $invoice) {
|
2018-01-24 19:19:53 +01:00
|
|
|
$isFirst = true;
|
2018-01-16 14:48:56 +01:00
|
|
|
$payments = $invoice->payments->count() ? $invoice->payments : [false];
|
2017-01-22 11:09:29 +01:00
|
|
|
foreach ($payments as $payment) {
|
2018-01-21 15:05:35 +01:00
|
|
|
$row = [
|
2017-01-22 11:09:29 +01:00
|
|
|
$this->isExport ? $client->getDisplayName() : $client->present()->link,
|
|
|
|
$this->isExport ? $invoice->invoice_number : $invoice->present()->link,
|
|
|
|
$invoice->present()->invoice_date,
|
2018-01-24 19:19:53 +01:00
|
|
|
$isFirst ? $account->formatMoney($invoice->amount, $client) : '',
|
2017-11-30 11:49:54 +01:00
|
|
|
$invoice->statusLabel(),
|
2017-01-22 11:09:29 +01:00
|
|
|
$payment ? $payment->present()->payment_date : '',
|
|
|
|
$payment ? $account->formatMoney($payment->getCompletedAmount(), $client) : '',
|
|
|
|
$payment ? $payment->present()->method : '',
|
2018-02-13 20:55:33 +01:00
|
|
|
$invoice->po_number,
|
2018-01-21 14:53:43 +01:00
|
|
|
$invoice->private_notes,
|
2018-01-26 13:11:23 +01:00
|
|
|
$invoice->user->getDisplayName(),
|
2017-01-22 11:09:29 +01:00
|
|
|
];
|
|
|
|
|
2018-01-24 19:34:14 +01:00
|
|
|
if ($hasTaxRates) {
|
2018-01-24 19:21:30 +01:00
|
|
|
$row[] = $isFirst ? $account->formatMoney($invoice->getTaxTotal(), $client) : '';
|
2018-01-24 19:19:53 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 15:24:59 +02:00
|
|
|
if ($account->customLabel('invoice_text1')) {
|
2018-01-21 15:05:35 +01:00
|
|
|
$row[] = $invoice->custom_text_value1;
|
|
|
|
}
|
2018-04-04 15:24:59 +02:00
|
|
|
if ($account->customLabel('invoice_text2')) {
|
2018-01-21 15:05:35 +01:00
|
|
|
$row[] = $invoice->custom_text_value2;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data[] = $row;
|
|
|
|
|
2017-01-22 11:09:29 +01:00
|
|
|
$this->addToTotals($client->currency_id, 'paid', $payment ? $payment->getCompletedAmount() : 0);
|
2018-01-24 19:19:53 +01:00
|
|
|
$isFirst = false;
|
2017-01-22 11:09:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->addToTotals($client->currency_id, 'amount', $invoice->amount);
|
|
|
|
$this->addToTotals($client->currency_id, 'balance', $invoice->balance);
|
2018-02-28 15:57:10 +01:00
|
|
|
|
|
|
|
if ($subgroup == 'status') {
|
|
|
|
$dimension = $invoice->statusLabel();
|
|
|
|
} else {
|
|
|
|
$dimension = $this->getDimension($client);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addChartData($dimension, $invoice->invoice_date, $invoice->amount);
|
2017-01-22 11:09:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|