diff --git a/app/Exports/Common/Items.php b/app/Exports/Common/Items.php index 24ed3a51c..aaad65306 100644 --- a/app/Exports/Common/Items.php +++ b/app/Exports/Common/Items.php @@ -2,40 +2,24 @@ namespace App\Exports\Common; -use App\Abstracts\Export; -use App\Models\Common\Item as Model; +use App\Exports\Common\Sheets\Items as Base; +use App\Exports\Common\Sheets\ItemTaxes; +use Maatwebsite\Excel\Concerns\WithMultipleSheets; -class Items extends Export +class Items implements WithMultipleSheets { - public function collection() + public $ids; + + public function __construct($ids = null) { - $model = Model::with('category', 'tax')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + $this->ids = $ids; } - public function map($model): array - { - $model->category_name = $model->category->name; - $model->tax_rate = $model->tax->rate; - - return parent::map($model); - } - - public function fields(): array + public function sheets(): array { return [ - 'name', - 'description', - 'sale_price', - 'purchase_price', - 'category_name', - 'tax_rate', - 'enabled', + 'items' => new Base($this->ids), + 'item_taxes' => new ItemTaxes($this->ids), ]; } } diff --git a/app/Exports/Common/Sheets/ItemTaxes.php b/app/Exports/Common/Sheets/ItemTaxes.php new file mode 100644 index 000000000..8420d6eb1 --- /dev/null +++ b/app/Exports/Common/Sheets/ItemTaxes.php @@ -0,0 +1,42 @@ +usingSearchString(request('search')); + + if (!empty($this->ids)) { + $model->whereIn('item_id', (array) $this->ids); + } + + return $model->cursor(); + } + + public function map($model): array + { + $item = $model->item; + + if (empty($item)) { + return []; + } + + $model->item_name = $model->item->name; + $model->tax_rate = $model->tax->rate; + + return parent::map($model); + } + + public function fields(): array + { + return [ + 'item_name', + 'tax_rate', + ]; + } +} diff --git a/app/Exports/Common/Sheets/Items.php b/app/Exports/Common/Sheets/Items.php new file mode 100644 index 000000000..bc7601ced --- /dev/null +++ b/app/Exports/Common/Sheets/Items.php @@ -0,0 +1,40 @@ +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; + + return parent::map($model); + } + + public function fields(): array + { + return [ + 'name', + 'description', + 'sale_price', + 'purchase_price', + 'category_name', + 'enabled', + ]; + } +}