Merge Invoice and Bill into Document
This commit is contained in:
40
app/Exports/Document/Documents.php
Normal file
40
app/Exports/Document/Documents.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\Document;
|
||||
|
||||
use App\Exports\Document\Sheets\Documents as Base;
|
||||
use App\Exports\Document\Sheets\DocumentItems;
|
||||
use App\Exports\Document\Sheets\DocumentItemTaxes;
|
||||
use App\Exports\Document\Sheets\DocumentHistories;
|
||||
use App\Exports\Document\Sheets\DocumentTotals;
|
||||
use App\Exports\Document\Sheets\DocumentTransactions;
|
||||
use Illuminate\Support\Str;
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
|
||||
class Documents implements WithMultipleSheets
|
||||
{
|
||||
public $ids;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
public function __construct($ids = null, string $type = '')
|
||||
{
|
||||
$this->ids = $ids;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function sheets(): array
|
||||
{
|
||||
return [
|
||||
Str::plural($this->type) => new Base($this->ids, $this->type),
|
||||
$this->type . '_items' => new DocumentItems($this->ids, $this->type),
|
||||
$this->type . '_item_taxes' => new DocumentItemTaxes($this->ids, $this->type),
|
||||
$this->type . '_histories' => new DocumentHistories($this->ids, $this->type),
|
||||
$this->type . '_totals' => new DocumentTotals($this->ids, $this->type),
|
||||
$this->type . '_transactions' => new DocumentTransactions($this->ids, $this->type),
|
||||
];
|
||||
}
|
||||
}
|
||||
54
app/Exports/Document/Sheets/DocumentHistories.php
Normal file
54
app/Exports/Document/Sheets/DocumentHistories.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\Document\Sheets;
|
||||
|
||||
use App\Abstracts\Export;
|
||||
use App\Models\Document\Document;
|
||||
use App\Models\Document\DocumentHistory as Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class DocumentHistories extends Export
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
$model = Model::{$this->type}()->with('document')->usingSearchString(request('search'));
|
||||
|
||||
if (!empty($this->ids)) {
|
||||
$model->whereIn('document_id', (array) $this->ids);
|
||||
}
|
||||
|
||||
return $model->cursor();
|
||||
}
|
||||
|
||||
public function map($model): array
|
||||
{
|
||||
$document = $model->document;
|
||||
|
||||
if (empty($document)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->type === Document::INVOICE_TYPE) {
|
||||
$model->invoice_number = $document->document_number;
|
||||
} else {
|
||||
$model->bill_number = $document->document_number;
|
||||
}
|
||||
|
||||
return parent::map($model);
|
||||
}
|
||||
|
||||
public function fields(): array
|
||||
{
|
||||
return [
|
||||
$this->type === Document::INVOICE_TYPE ? 'invoice_number' : 'bill_number',
|
||||
'status',
|
||||
'notify',
|
||||
'description',
|
||||
];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return Str::replaceFirst('document', $this->type, parent::title());
|
||||
}
|
||||
}
|
||||
57
app/Exports/Document/Sheets/DocumentItemTaxes.php
Normal file
57
app/Exports/Document/Sheets/DocumentItemTaxes.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\Document\Sheets;
|
||||
|
||||
use App\Abstracts\Export;
|
||||
use App\Models\Document\Document;
|
||||
use App\Models\Document\DocumentItemTax as Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class DocumentItemTaxes extends Export
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
$model = Model::{$this->type}()->with('document', 'item', 'tax')->usingSearchString(request('search'));
|
||||
|
||||
if (!empty($this->ids)) {
|
||||
$model->whereIn('document_id', (array) $this->ids);
|
||||
}
|
||||
|
||||
return $model->cursor();
|
||||
}
|
||||
|
||||
public function map($model): array
|
||||
{
|
||||
$document = $model->document;
|
||||
|
||||
if (empty($document)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->type === Document::INVOICE_TYPE) {
|
||||
$model->invoice_number = $document->document_number;
|
||||
} else {
|
||||
$model->bill_number = $document->document_number;
|
||||
}
|
||||
|
||||
$model->item_name = $model->item->name;
|
||||
$model->tax_rate = $model->tax->rate;
|
||||
|
||||
return parent::map($model);
|
||||
}
|
||||
|
||||
public function fields(): array
|
||||
{
|
||||
return [
|
||||
$this->type === Document::INVOICE_TYPE ? 'invoice_number' : 'bill_number',
|
||||
'item_name',
|
||||
'tax_rate',
|
||||
'amount',
|
||||
];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return Str::replaceFirst('document', $this->type, parent::title());
|
||||
}
|
||||
}
|
||||
58
app/Exports/Document/Sheets/DocumentItems.php
Normal file
58
app/Exports/Document/Sheets/DocumentItems.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\Document\Sheets;
|
||||
|
||||
use App\Abstracts\Export;
|
||||
use App\Models\Document\Document;
|
||||
use App\Models\Document\DocumentItem as Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class DocumentItems extends Export
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
$model = Model::{$this->type}()->with('document', 'item')->usingSearchString(request('search'));
|
||||
|
||||
if (!empty($this->ids)) {
|
||||
$model->whereIn('document_id', (array) $this->ids);
|
||||
}
|
||||
|
||||
return $model->cursor();
|
||||
}
|
||||
|
||||
public function map($model): array
|
||||
{
|
||||
$document = $model->document;
|
||||
|
||||
if (empty($document)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->type === Document::INVOICE_TYPE) {
|
||||
$model->invoice_number = $document->document_number;
|
||||
} else {
|
||||
$model->bill_number = $document->document_number;
|
||||
}
|
||||
|
||||
$model->item_name = $model->item->name;
|
||||
|
||||
return parent::map($model);
|
||||
}
|
||||
|
||||
public function fields(): array
|
||||
{
|
||||
return [
|
||||
$this->type === Document::INVOICE_TYPE ? 'invoice_number' : 'bill_number',
|
||||
'item_name',
|
||||
'quantity',
|
||||
'price',
|
||||
'total',
|
||||
'tax',
|
||||
];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return Str::replaceFirst('document', $this->type, parent::title());
|
||||
}
|
||||
}
|
||||
55
app/Exports/Document/Sheets/DocumentTotals.php
Normal file
55
app/Exports/Document/Sheets/DocumentTotals.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\Document\Sheets;
|
||||
|
||||
use App\Abstracts\Export;
|
||||
use App\Models\Document\Document;
|
||||
use App\Models\Document\DocumentTotal as Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class DocumentTotals extends Export
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
$model = Model::{$this->type}()->with('document')->usingSearchString(request('search'));
|
||||
|
||||
if (!empty($this->ids)) {
|
||||
$model->whereIn('document_id', (array) $this->ids);
|
||||
}
|
||||
|
||||
return $model->cursor();
|
||||
}
|
||||
|
||||
public function map($model): array
|
||||
{
|
||||
$document = $model->document;
|
||||
|
||||
if (empty($document)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->type === Document::INVOICE_TYPE) {
|
||||
$model->invoice_number = $document->document_number;
|
||||
} else {
|
||||
$model->bill_number = $document->document_number;
|
||||
}
|
||||
|
||||
return parent::map($model);
|
||||
}
|
||||
|
||||
public function fields(): array
|
||||
{
|
||||
return [
|
||||
$this->type === Document::INVOICE_TYPE ? 'invoice_number' : 'bill_number',
|
||||
'code',
|
||||
'name',
|
||||
'amount',
|
||||
'sort_order',
|
||||
];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return Str::replaceFirst('document', $this->type, parent::title());
|
||||
}
|
||||
}
|
||||
75
app/Exports/Document/Sheets/DocumentTransactions.php
Normal file
75
app/Exports/Document/Sheets/DocumentTransactions.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\Document\Sheets;
|
||||
|
||||
use App\Abstracts\Export;
|
||||
use App\Models\Banking\Transaction as Model;
|
||||
use App\Models\Document\Document;
|
||||
use Illuminate\Support\Str;
|
||||
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
class DocumentTransactions extends Export implements WithColumnFormatting
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
$model = Model::with('account', 'category', 'contact', 'document')->income()->isDocument()->usingSearchString(request('search'));
|
||||
|
||||
if (!empty($this->ids)) {
|
||||
$model->whereIn('document_id', (array) $this->ids);
|
||||
}
|
||||
|
||||
return $model->cursor();
|
||||
}
|
||||
|
||||
public function map($model): array
|
||||
{
|
||||
$document = $model->document;
|
||||
|
||||
if (empty($document)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->type === Document::INVOICE_TYPE) {
|
||||
$model->invoice_number = $document->document_number;
|
||||
} else {
|
||||
$model->bill_number = $document->document_number;
|
||||
}
|
||||
|
||||
$model->account_name = $model->account->name;
|
||||
$model->category_name = $model->category->name;
|
||||
$model->contact_email = $model->contact->email;
|
||||
|
||||
return parent::map($model);
|
||||
}
|
||||
|
||||
public function fields(): array
|
||||
{
|
||||
return [
|
||||
$this->type === Document::INVOICE_TYPE ? 'invoice_number' : 'bill_number',
|
||||
'paid_at',
|
||||
'amount',
|
||||
'currency_code',
|
||||
'currency_rate',
|
||||
'account_name',
|
||||
'contact_email',
|
||||
'category_name',
|
||||
'description',
|
||||
'payment_method',
|
||||
'reference',
|
||||
'reconciled',
|
||||
];
|
||||
}
|
||||
|
||||
public function columnFormats(): array
|
||||
{
|
||||
return [
|
||||
'B' => NumberFormat::FORMAT_DATE_YYYYMMDD,
|
||||
];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return Str::replaceFirst('document', $this->type, parent::title());
|
||||
}
|
||||
}
|
||||
72
app/Exports/Document/Sheets/Documents.php
Normal file
72
app/Exports/Document/Sheets/Documents.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\Document\Sheets;
|
||||
|
||||
use App\Abstracts\Export;
|
||||
use App\Models\Document\Document as Model;
|
||||
use Illuminate\Support\Str;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
class Documents extends Export
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
$model = Model::{$this->type}()->with('category')->usingSearchString(request('search'));
|
||||
|
||||
if (!empty($this->ids)) {
|
||||
$model->whereIn('id', (array) $this->ids);
|
||||
}
|
||||
|
||||
return $model->cursor();
|
||||
}
|
||||
|
||||
public function map($model): array
|
||||
{
|
||||
$model->category_name = $model->category->name;
|
||||
|
||||
if ($this->type === Model::INVOICE_TYPE) {
|
||||
$model->invoice_number = $model->document_number;
|
||||
$model->invoiced_at = $model->issued_at;
|
||||
} else {
|
||||
$model->bill_number = $model->document_number;
|
||||
$model->billed_at = $model->issued_at;
|
||||
}
|
||||
|
||||
return parent::map($model);
|
||||
}
|
||||
|
||||
public function fields(): array
|
||||
{
|
||||
return [
|
||||
$this->type === Model::INVOICE_TYPE ? 'invoice_number' : 'bill_number',
|
||||
'order_number',
|
||||
'status',
|
||||
$this->type === Model::INVOICE_TYPE ? 'invoiced_at' : 'billed_at',
|
||||
'due_at',
|
||||
'amount',
|
||||
'currency_code',
|
||||
'currency_rate',
|
||||
'category_name',
|
||||
'contact_name',
|
||||
'contact_email',
|
||||
'contact_tax_number',
|
||||
'contact_phone',
|
||||
'contact_address',
|
||||
'notes',
|
||||
'footer',
|
||||
];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return Str::replaceFirst('document', $this->type, parent::title());
|
||||
}
|
||||
|
||||
public function columnFormats(): array
|
||||
{
|
||||
return [
|
||||
'D' => NumberFormat::FORMAT_DATE_YYYYMMDD,
|
||||
'E' => NumberFormat::FORMAT_DATE_YYYYMMDD,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user