invoiceninja/app/Ninja/Reports/ExpenseReport.php

150 lines
5.5 KiB
PHP
Raw Permalink Normal View History

2017-01-22 12:09:29 +02:00
<?php
namespace App\Ninja\Reports;
use Barracuda\ArchiveStream\Archive;
2017-01-30 21:40:43 +02:00
use App\Models\Expense;
2018-01-24 09:59:16 +02:00
use App\Models\TaxRate;
2017-01-22 12:09:29 +02:00
use Auth;
use Utils;
class ExpenseReport extends AbstractReport
{
2018-01-21 15:53:43 +02:00
public function getColumns()
{
2018-01-24 09:59:16 +02:00
$columns = [
2019-01-30 22:00:26 +11:00
'id_number' => [],
2018-01-23 20:32:11 +02:00
'vendor' => [],
'client' => [],
'date' => [],
'category' => [],
'amount' => [],
2018-01-21 15:53:43 +02:00
'public_notes' => ['columnSelector-false'],
'private_notes' => ['columnSelector-false'],
2018-01-26 14:11:23 +02:00
'user' => ['columnSelector-false'],
2019-02-18 16:08:16 +02:00
'payment_date' => ['columnSelector-false'],
'payment_type' => ['columnSelector-false'],
'payment_reference' => ['columnSelector-false'],
2018-01-21 15:53:43 +02:00
];
2018-01-24 09:59:16 +02:00
2018-04-04 22:21:15 +03:00
$user = auth()->user();
$account = $user->account;
if ($account->customLabel('expense1')) {
$columns[$account->present()->customLabel('expense1')] = ['columnSelector-false', 'custom'];
}
if ($account->customLabel('expense2')) {
$columns[$account->present()->customLabel('expense2')] = ['columnSelector-false', 'custom'];
}
2018-01-24 09:59:16 +02:00
if (TaxRate::scope()->count()) {
$columns['tax'] = ['columnSelector-false'];
}
if ($this->isExport) {
$columns['currency'] = ['columnSelector-false'];
}
2019-01-30 22:00:26 +11:00
$columns['documents'] = ['columnSelector-false'];
2018-01-24 09:59:16 +02:00
return $columns;
2018-01-21 15:53:43 +02:00
}
2017-01-22 12:09:29 +02:00
public function run()
{
$account = Auth::user()->account;
$exportFormat = $this->options['export_format'];
2018-02-28 14:43:15 +02:00
$subgroup = $this->options['subgroup'];
$with = ['client.contacts', 'vendor'];
2018-01-24 20:34:14 +02:00
$hasTaxRates = TaxRate::scope()->count();
if ($exportFormat == 'zip') {
$with[] = ['documents'];
}
2017-01-22 12:09:29 +02:00
$expenses = Expense::scope()
2017-03-31 09:01:07 +03:00
->orderBy('expense_date', 'desc')
2017-01-22 12:09:29 +02:00
->withArchived()
2019-02-18 16:10:05 +02:00
->with('client.contacts', 'vendor', 'expense_category', 'user')
2017-01-22 12:09:29 +02:00
->where('expense_date', '>=', $this->startDate)
->where('expense_date', '<=', $this->endDate);
if ($this->isExport && $exportFormat == 'zip') {
2018-02-21 15:49:18 +02:00
if (! extension_loaded('GMP')) {
die(trans('texts.gmp_required'));
}
$zip = Archive::instance_by_useragent(date('Y-m-d') . '_' . str_replace(' ', '_', trans('texts.expense_documents')));
foreach ($expenses->get() as $expense) {
foreach ($expense->documents as $document) {
$expenseId = str_pad($expense->public_id, $account->invoice_number_padding, '0', STR_PAD_LEFT);
2018-01-02 22:54:10 +02:00
$name = sprintf('%s_%s_%s_%s', $expense->expense_date ?: date('Y-m-d'), trans('texts.expense'), $expenseId, $document->name);
$name = str_replace(' ', '_', $name);
$zip->add_file($name, $document->getRaw());
}
}
$zip->finish();
exit;
}
2017-01-22 12:09:29 +02:00
foreach ($expenses->get() as $expense) {
$amount = $expense->amountWithTax();
2018-01-24 09:59:16 +02:00
$row = [
2019-01-30 22:00:26 +11:00
str_pad($expense->public_id, $account->invoice_number_padding, '0', STR_PAD_LEFT),
2017-01-22 12:09:29 +02:00
$expense->vendor ? ($this->isExport ? $expense->vendor->name : $expense->vendor->present()->link) : '',
$expense->client ? ($this->isExport ? $expense->client->getDisplayName() : $expense->client->present()->link) : '',
2019-01-30 22:00:26 +11:00
$this->isExport ? $expense->expense_date : link_to($expense->present()->url, $expense->present()->expense_date),
2017-01-22 12:09:29 +02:00
$expense->present()->category,
2018-03-10 21:09:32 +02:00
Utils::formatMoney($amount, $expense->expense_currency_id),
2018-01-21 15:53:43 +02:00
$expense->public_notes,
$expense->private_notes,
2018-01-26 14:11:23 +02:00
$expense->user->getDisplayName(),
2019-02-18 16:08:16 +02:00
$expense->present()->payment_date(),
2019-02-18 16:10:05 +02:00
$expense->present()->payment_type(),
2019-02-18 16:08:16 +02:00
$expense->transaction_reference,
2017-01-22 12:09:29 +02:00
];
2018-04-04 22:21:15 +03:00
if ($account->customLabel('expense1')) {
$row[] = $expense->custom_value1;
}
if ($account->customLabel('expense2')) {
$row[] = $expense->custom_value2;
}
2018-01-24 20:34:14 +02:00
if ($hasTaxRates) {
2018-01-24 09:59:16 +02:00
$row[] = $expense->present()->taxAmount;
}
if ($this->isExport) {
$row[] = $expense->present()->currencyCode;
}
2019-01-30 22:00:26 +11:00
$documents = '';
foreach ($expense->documents as $document) {
$expenseId = $row[0];
$name = sprintf('%s_%s_%s_%s', $expense->expense_date ?: date('Y-m-d'), trans('texts.expense'), $expenseId, $document->name);
$name = str_replace(' ', '_', $name);
$documents[] = $name;
}
$row[] = join(', ', $documents);
2018-01-24 09:59:16 +02:00
$this->data[] = $row;
2017-01-22 12:09:29 +02:00
$this->addToTotals($expense->expense_currency_id, 'amount', $amount);
$this->addToTotals($expense->invoice_currency_id, 'amount', 0);
2018-02-28 16:57:10 +02:00
if ($subgroup == 'category') {
$dimension = $expense->present()->category;
} elseif ($subgroup == 'vendor') {
$dimension = $expense->vendor ? $expense->vendor->name : trans('texts.unset');
} else {
$dimension = $this->getDimension($expense);
}
$this->addChartData($dimension, $expense->expense_date, $amount);
2017-01-22 12:09:29 +02:00
}
}
}