akaunting/app/Exports/Document/Sheets/DocumentTransactions.php

76 lines
1.9 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
2020-12-24 01:28:38 +03:00
namespace App\Exports\Document\Sheets;
2019-11-16 10:21:14 +03:00
2020-01-20 02:05:40 +03:00
use App\Abstracts\Export;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Transaction as Model;
2020-12-24 01:28:38 +03:00
use App\Models\Document\Document;
use Illuminate\Support\Str;
2020-11-13 03:07:30 +03:00
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
2019-11-16 10:21:14 +03:00
2020-12-24 01:28:38 +03:00
class DocumentTransactions extends Export implements WithColumnFormatting
2019-11-16 10:21:14 +03:00
{
public function collection()
{
2020-12-24 01:28:38 +03:00
$model = Model::with('account', 'category', 'contact', 'document')->income()->isDocument()->usingSearchString(request('search'));
2019-11-16 10:21:14 +03:00
2020-01-20 02:05:40 +03:00
if (!empty($this->ids)) {
$model->whereIn('document_id', (array) $this->ids);
2019-11-16 10:21:14 +03:00
}
2020-03-02 23:13:36 +03:00
return $model->cursor();
2019-11-16 10:21:14 +03:00
}
2020-01-21 09:55:12 +03:00
public function map($model): array
{
2020-12-24 01:28:38 +03:00
$document = $model->document;
2020-02-21 21:37:48 +03:00
2020-12-24 01:28:38 +03:00
if (empty($document)) {
2020-02-21 21:37:48 +03:00
return [];
}
2020-12-24 01:28:38 +03:00
if ($this->type === Document::INVOICE_TYPE) {
$model->invoice_number = $document->document_number;
} else {
$model->bill_number = $document->document_number;
}
2020-01-21 09:55:12 +03:00
$model->account_name = $model->account->name;
$model->category_name = $model->category->name;
$model->contact_email = $model->contact->email;
return parent::map($model);
}
2020-01-20 02:05:40 +03:00
public function fields(): array
2019-11-16 10:21:14 +03:00
{
return [
2020-12-24 01:28:38 +03:00
$this->type === Document::INVOICE_TYPE ? 'invoice_number' : 'bill_number',
2019-11-16 10:21:14 +03:00
'paid_at',
'amount',
'currency_code',
'currency_rate',
2020-01-21 09:55:12 +03:00
'account_name',
'contact_email',
'category_name',
2020-01-20 00:21:37 +03:00
'description',
2019-11-16 10:21:14 +03:00
'payment_method',
2020-01-20 00:21:37 +03:00
'reference',
2019-11-16 10:21:14 +03:00
'reconciled',
];
}
2020-11-13 03:07:30 +03:00
public function columnFormats(): array
{
return [
'B' => NumberFormat::FORMAT_DATE_YYYYMMDD,
];
}
2020-12-24 01:28:38 +03:00
public function title(): string
{
return Str::replaceFirst('document', $this->type, parent::title());
}
2020-01-20 00:21:37 +03:00
}