mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-12 14:12:44 +01:00
93 lines
3.8 KiB
PHP
93 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Ninja\Reports;
|
|
|
|
use App\Models\Client;
|
|
use Auth;
|
|
use Barracuda\ArchiveStream\Archive;
|
|
|
|
class InvoiceReport extends AbstractReport
|
|
{
|
|
public $columns = [
|
|
'client',
|
|
'invoice_number',
|
|
'invoice_date',
|
|
'amount',
|
|
'status',
|
|
'payment_date',
|
|
'paid',
|
|
'method',
|
|
];
|
|
|
|
public function run()
|
|
{
|
|
$account = Auth::user()->account;
|
|
$status = $this->options['invoice_status'];
|
|
$exportFormat = $this->options['export_format'];
|
|
|
|
$clients = Client::scope()
|
|
->orderBy('name')
|
|
->withArchived()
|
|
->with('contacts')
|
|
->with(['invoices' => function ($query) use ($status) {
|
|
if ($status == 'draft') {
|
|
$query->whereIsPublic(false);
|
|
} elseif (in_array($status, ['paid', 'unpaid', 'sent'])) {
|
|
$query->whereIsPublic(true);
|
|
}
|
|
$query->invoices()
|
|
->withArchived()
|
|
->where('invoice_date', '>=', $this->startDate)
|
|
->where('invoice_date', '<=', $this->endDate)
|
|
->with(['payments' => function ($query) {
|
|
$query->withArchived()
|
|
->excludeFailed()
|
|
->with('payment_type', 'account_gateway.gateway');
|
|
}, 'invoice_items']);
|
|
}]);
|
|
|
|
|
|
if ($this->isExport && $exportFormat == 'zip') {
|
|
$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) {
|
|
$name = sprintf('%s_%s_%s', date('Y-m-d'), $invoice->present()->titledName, $document->name);
|
|
$zip->add_file($name, $document->getRaw());
|
|
}
|
|
}
|
|
}
|
|
$zip->finish();
|
|
exit;
|
|
}
|
|
|
|
foreach ($clients->get() as $client) {
|
|
foreach ($client->invoices as $invoice) {
|
|
$payments = count($invoice->payments) ? $invoice->payments : [false];
|
|
foreach ($payments as $payment) {
|
|
if (! $payment && $status == 'paid') {
|
|
continue;
|
|
} elseif ($payment && $status == 'unpaid') {
|
|
continue;
|
|
}
|
|
$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),
|
|
$invoice->present()->status(),
|
|
$payment ? $payment->present()->payment_date : '',
|
|
$payment ? $account->formatMoney($payment->getCompletedAmount(), $client) : '',
|
|
$payment ? $payment->present()->method : '',
|
|
];
|
|
|
|
$this->addToTotals($client->currency_id, 'paid', $payment ? $payment->getCompletedAmount() : 0);
|
|
}
|
|
|
|
$this->addToTotals($client->currency_id, 'amount', $invoice->amount);
|
|
$this->addToTotals($client->currency_id, 'balance', $invoice->balance);
|
|
}
|
|
}
|
|
}
|
|
}
|