2017-10-19 10:18:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ninja\Reports;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Expense;
|
|
|
|
use Barracuda\ArchiveStream\Archive;
|
|
|
|
|
|
|
|
class DocumentReport extends AbstractReport
|
|
|
|
{
|
2018-01-21 14:53:43 +01:00
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
return [
|
2018-01-23 19:32:11 +01:00
|
|
|
'document' => [],
|
|
|
|
'client' => [],
|
|
|
|
'invoice_or_expense' => [],
|
|
|
|
'date' => [],
|
2018-01-21 14:53:43 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-10-19 10:18:53 +02:00
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$account = auth()->user()->account;
|
|
|
|
$filter = $this->options['document_filter'];
|
|
|
|
$exportFormat = $this->options['export_format'];
|
2018-02-28 13:43:15 +01:00
|
|
|
$subgroup = $this->options['subgroup'];
|
2017-10-19 10:18:53 +02:00
|
|
|
$records = false;
|
|
|
|
|
|
|
|
if (! $filter || $filter == ENTITY_INVOICE) {
|
|
|
|
$records = Invoice::scope()
|
|
|
|
->withArchived()
|
|
|
|
->with(['documents'])
|
|
|
|
->where('invoice_date', '>=', $this->startDate)
|
|
|
|
->where('invoice_date', '<=', $this->endDate)
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $filter || $filter == ENTITY_EXPENSE){
|
|
|
|
$expenses = Expense::scope()
|
|
|
|
->withArchived()
|
|
|
|
->with(['documents'])
|
|
|
|
->where('expense_date', '>=', $this->startDate)
|
|
|
|
->where('expense_date', '<=', $this->endDate)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
if ($records) {
|
|
|
|
$records = $records->merge($expenses);
|
|
|
|
} else {
|
|
|
|
$records = $expenses;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:53 +02:00
|
|
|
$zip = Archive::instance_by_useragent(date('Y-m-d') . '_' . str_replace(' ', '_', trans('texts.documents')));
|
|
|
|
foreach ($records as $record) {
|
|
|
|
foreach ($record->documents as $document) {
|
2018-01-02 21:54:10 +01:00
|
|
|
$name = sprintf('%s_%s_%s', $document->created_at->format('Y-m-d'), $record->present()->titledName, $document->name);
|
2017-10-19 10:18:53 +02:00
|
|
|
$name = str_replace(' ', '_', $name);
|
|
|
|
$name = str_replace('#', '', $name);
|
|
|
|
$zip->add_file($name, $document->getRaw());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$zip->finish();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($records as $record) {
|
|
|
|
foreach ($record->documents as $document) {
|
2018-02-28 13:43:15 +01:00
|
|
|
$date = $record->getEntityType() == ENTITY_INVOICE ? $record->invoice_date : $record->expense_date;
|
2017-10-19 10:18:53 +02:00
|
|
|
$this->data[] = [
|
|
|
|
$this->isExport ? $document->name : link_to($document->getUrl(), $document->name),
|
|
|
|
$record->client ? ($this->isExport ? $record->client->getDisplayName() : $record->client->present()->link) : '',
|
|
|
|
$this->isExport ? $record->present()->titledName : ($filter ? $record->present()->link : link_to($record->present()->url, $record->present()->titledName)),
|
2018-02-28 13:43:15 +01:00
|
|
|
$date,
|
2017-10-19 10:18:53 +02:00
|
|
|
];
|
2018-02-28 13:43:15 +01:00
|
|
|
|
|
|
|
$dimension = $this->getDimension($record);
|
|
|
|
$this->addChartData($dimension, $date, 1);
|
2017-10-19 10:18:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|