1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Ninja/Reports/InvoiceReport.php

161 lines
6.6 KiB
PHP
Raw Normal View History

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;
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' => [],
'due_date' => ['columnSelector-false'],
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-07-08 15:38:45 +02:00
'vat_number' => ['columnSelector-false'],
2018-01-26 13:11:23 +01:00
'user' => ['columnSelector-false'],
'billing_address' => ['columnSelector-false'],
'shipping_address' => ['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
}
2019-01-30 11:45:46 +01:00
if ($account->customLabel('invoice_text2')) {
2018-04-04 15:24:59 +02:00
$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;
$statusIds = $this->options['status_ids'];
$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')
->with(['invoices' => function ($query) use ($statusIds) {
2017-01-22 11:09:29 +01:00
$query->invoices()
->withArchived()
->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
}]);
if ($this->isExport && $exportFormat == 'zip') {
2018-02-21 14:49:18 +01:00
if (! extension_loaded('GMP')) {
die(trans('texts.gmp_required'));
}
$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);
$zip->add_file($name, $document->getRaw());
}
}
}
$zip->finish();
exit;
}
2019-01-30 11:45:46 +01:00
if ($this->isExport && $exportFormat == 'zip-invoices') {
if (! extension_loaded('GMP')) {
die(trans('texts.gmp_required'));
}
$zip = Archive::instance_by_useragent(date('Y-m-d') . '_' . str_replace(' ', '_', trans('texts.invoices')));
foreach ($clients->get() as $client) {
foreach ($client->invoices as $invoice) {
$zip->add_file($invoice->getFileName(), $invoice->getPDFString());
}
}
$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;
$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,
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
$isFirst ? $account->formatMoney($invoice->amount, $client) : '',
$invoice->statusLabel(),
2019-01-30 11:45:46 +01:00
$payment ? ($this->isExport ? $payment->payment_date : $payment->present()->payment_date) : '',
2017-01-22 11:09:29 +01:00
$payment ? $account->formatMoney($payment->getCompletedAmount(), $client) : '',
$payment ? $payment->present()->method : '',
2019-01-30 11:45:46 +01:00
$this->isExport ? $invoice->due_date : $invoice->present()->due_date,
2018-02-13 20:55:33 +01:00
$invoice->po_number,
2018-01-21 14:53:43 +01:00
$invoice->private_notes,
2018-07-08 15:38:45 +02:00
$client->vat_number,
2018-01-26 13:11:23 +01:00
$invoice->user->getDisplayName(),
trim(str_replace('<br/>', ', ', $client->present()->address()), ', '),
trim(str_replace('<br/>', ', ', $client->present()->address(ADDRESS_SHIPPING)), ', '),
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
}
}
}
}