From 9b308e165aa1d19e6bac883d8a03fed07ce08ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Sun, 27 Jun 2021 11:40:46 +0300 Subject: [PATCH] added scope for collection export --- app/Abstracts/Model.php | 31 +++++++++++++++++++ app/Exports/Banking/Transactions.php | 8 +---- app/Exports/Banking/Transfers.php | 10 ++---- app/Exports/Common/Sheets/ItemTaxes.php | 8 +---- app/Exports/Common/Sheets/Items.php | 8 +---- app/Exports/Purchases/Payments.php | 8 +---- .../Purchases/Sheets/BillHistories.php | 8 +---- .../Purchases/Sheets/BillItemTaxes.php | 8 +---- app/Exports/Purchases/Sheets/BillItems.php | 8 +---- app/Exports/Purchases/Sheets/BillTotals.php | 8 +---- .../Purchases/Sheets/BillTransactions.php | 8 +---- app/Exports/Purchases/Sheets/Bills.php | 8 +---- app/Exports/Purchases/Vendors.php | 8 +---- app/Exports/Sales/Customers.php | 8 +---- app/Exports/Sales/Revenues.php | 8 +---- app/Exports/Sales/Sheets/InvoiceHistories.php | 8 +---- app/Exports/Sales/Sheets/InvoiceItemTaxes.php | 8 +---- app/Exports/Sales/Sheets/InvoiceItems.php | 8 +---- app/Exports/Sales/Sheets/InvoiceTotals.php | 8 +---- .../Sales/Sheets/InvoiceTransactions.php | 10 +----- app/Exports/Sales/Sheets/Invoices.php | 8 +---- app/Exports/Settings/Categories.php | 8 +---- resources/lang/en-GB/messages.php | 2 +- 23 files changed, 54 insertions(+), 151 deletions(-) diff --git a/app/Abstracts/Model.php b/app/Abstracts/Model.php index 5a88999e8..c8d2c1396 100644 --- a/app/Abstracts/Model.php +++ b/app/Abstracts/Model.php @@ -118,6 +118,37 @@ abstract class Model extends Eloquent implements Ownable return $query->paginate($limit); } + /** + * Scope to export the rows of the current page filtered and sorted. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param $ids + * @param $sort + * @param $id_field + * + * @return \Illuminate\Support\LazyCollection + */ + public function scopeCollectForExport($query, $ids = [], $sort = 'name', $id_field = 'id') + { + $request = request(); + + if (!empty($ids)) { + $query->whereIn($id_field, (array) $ids); + } + + $search = $request->get('search'); + + $query->usingSearchString($search)->sortable($sort); + + $page = (int) $request->get('page'); + $limit = (int) $request->get('limit', setting('default.list_limit', '25')); + $offset = $page ? ($page - 1) * $limit : 0; + + $query->offset($offset)->limit($limit); + + return $query->cursor(); + } + /** * Scope to only include active models. * diff --git a/app/Exports/Banking/Transactions.php b/app/Exports/Banking/Transactions.php index 01354dd74..12ddc3b54 100644 --- a/app/Exports/Banking/Transactions.php +++ b/app/Exports/Banking/Transactions.php @@ -11,13 +11,7 @@ class Transactions extends Export implements WithColumnFormatting { public function collection() { - $model = Model::with('account', 'category', 'contact', 'document')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('account', 'category', 'contact', 'document')->collectForExport($this->ids, ['paid_at' => 'desc']); } public function map($model): array diff --git a/app/Exports/Banking/Transfers.php b/app/Exports/Banking/Transfers.php index 19166d1b3..fbcbb9183 100644 --- a/app/Exports/Banking/Transfers.php +++ b/app/Exports/Banking/Transfers.php @@ -12,18 +12,12 @@ class Transfers extends Export implements WithColumnFormatting { public function collection() { - $model = Model::with( + return Model::with( 'expense_transaction', 'expense_transaction.account', 'income_transaction', 'income_transaction.account' - )->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + )->collectForExport($this->ids); } public function map($model): array diff --git a/app/Exports/Common/Sheets/ItemTaxes.php b/app/Exports/Common/Sheets/ItemTaxes.php index 8420d6eb1..68a36b62a 100644 --- a/app/Exports/Common/Sheets/ItemTaxes.php +++ b/app/Exports/Common/Sheets/ItemTaxes.php @@ -9,13 +9,7 @@ class ItemTaxes extends Export { public function collection() { - $model = Model::with('item', 'tax')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('item_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('item', 'tax')->collectForExport($this->ids, null, 'item_id'); } public function map($model): array diff --git a/app/Exports/Common/Sheets/Items.php b/app/Exports/Common/Sheets/Items.php index 4d28506c2..3f3a3ed0d 100644 --- a/app/Exports/Common/Sheets/Items.php +++ b/app/Exports/Common/Sheets/Items.php @@ -9,13 +9,7 @@ class Items extends Export { public function collection() { - $model = Model::with('category')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('category')->collectForExport($this->ids); } public function map($model): array diff --git a/app/Exports/Purchases/Payments.php b/app/Exports/Purchases/Payments.php index 8acdbec9f..d60550006 100644 --- a/app/Exports/Purchases/Payments.php +++ b/app/Exports/Purchases/Payments.php @@ -11,13 +11,7 @@ class Payments extends Export implements WithColumnFormatting { public function collection() { - $model = Model::with('account', 'bill', 'category', 'contact')->expense()->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('account', 'bill', 'category', 'contact')->expense()->collectForExport($this->ids, ['paid_at' => 'desc']); } public function map($model): array diff --git a/app/Exports/Purchases/Sheets/BillHistories.php b/app/Exports/Purchases/Sheets/BillHistories.php index df1e70cf2..fd55354e8 100644 --- a/app/Exports/Purchases/Sheets/BillHistories.php +++ b/app/Exports/Purchases/Sheets/BillHistories.php @@ -9,13 +9,7 @@ class BillHistories extends Export { public function collection() { - $model = Model::bill()->with('document')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('document_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('document')->bill()->collectForExport($this->ids, null, 'document_id'); } public function map($model): array diff --git a/app/Exports/Purchases/Sheets/BillItemTaxes.php b/app/Exports/Purchases/Sheets/BillItemTaxes.php index 13e24d9ba..1b47396c4 100644 --- a/app/Exports/Purchases/Sheets/BillItemTaxes.php +++ b/app/Exports/Purchases/Sheets/BillItemTaxes.php @@ -9,13 +9,7 @@ class BillItemTaxes extends Export { public function collection() { - $model = Model::bill()->with('document', 'item', 'tax')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('document_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('document', 'item', 'tax')->bill()->collectForExport($this->ids, null, 'document_id'); } public function map($model): array diff --git a/app/Exports/Purchases/Sheets/BillItems.php b/app/Exports/Purchases/Sheets/BillItems.php index cc468f29a..ce8fd7fbc 100644 --- a/app/Exports/Purchases/Sheets/BillItems.php +++ b/app/Exports/Purchases/Sheets/BillItems.php @@ -9,13 +9,7 @@ class BillItems extends Export { public function collection() { - $model = Model::bill()->with('document', 'item')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('document_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('document', 'item')->bill()->collectForExport($this->ids, null, 'document_id'); } public function map($model): array diff --git a/app/Exports/Purchases/Sheets/BillTotals.php b/app/Exports/Purchases/Sheets/BillTotals.php index 6dc80b71a..0d88c5c8e 100644 --- a/app/Exports/Purchases/Sheets/BillTotals.php +++ b/app/Exports/Purchases/Sheets/BillTotals.php @@ -9,13 +9,7 @@ class BillTotals extends Export { public function collection() { - $model = Model::bill()->with('document')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('document_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('document')->bill()->collectForExport($this->ids, null, 'document_id'); } public function map($model): array diff --git a/app/Exports/Purchases/Sheets/BillTransactions.php b/app/Exports/Purchases/Sheets/BillTransactions.php index d295a1e10..c0ea7d917 100644 --- a/app/Exports/Purchases/Sheets/BillTransactions.php +++ b/app/Exports/Purchases/Sheets/BillTransactions.php @@ -11,13 +11,7 @@ class BillTransactions extends Export implements WithColumnFormatting { public function collection() { - $model = Model::with('account', 'category', 'contact', 'document')->expense()->isDocument()->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('document_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('account', 'category', 'contact', 'document')->expense()->isDocument()->collectForExport($this->ids, ['paid_at' => 'desc'], 'document_id'); } public function map($model): array diff --git a/app/Exports/Purchases/Sheets/Bills.php b/app/Exports/Purchases/Sheets/Bills.php index 63097e4fc..02aaf3fa7 100644 --- a/app/Exports/Purchases/Sheets/Bills.php +++ b/app/Exports/Purchases/Sheets/Bills.php @@ -11,13 +11,7 @@ class Bills extends Export implements WithColumnFormatting { public function collection() { - $model = Model::bill()->with('category')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('category')->bill()->collectForExport($this->ids, ['document_number' => 'desc']); } public function map($model): array diff --git a/app/Exports/Purchases/Vendors.php b/app/Exports/Purchases/Vendors.php index 8ebfd1c91..8048f9827 100644 --- a/app/Exports/Purchases/Vendors.php +++ b/app/Exports/Purchases/Vendors.php @@ -9,13 +9,7 @@ class Vendors extends Export { public function collection() { - $model = Model::type('vendor')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + return Model::vendor()->collectForExport($this->ids); } public function fields(): array diff --git a/app/Exports/Sales/Customers.php b/app/Exports/Sales/Customers.php index 9ae1d2a1e..7cbf9468b 100644 --- a/app/Exports/Sales/Customers.php +++ b/app/Exports/Sales/Customers.php @@ -9,13 +9,7 @@ class Customers extends Export { public function collection() { - $model = Model::type('customer')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + return Model::customer()->collectForExport($this->ids); } public function fields(): array diff --git a/app/Exports/Sales/Revenues.php b/app/Exports/Sales/Revenues.php index 47e7c6dfe..da284b120 100644 --- a/app/Exports/Sales/Revenues.php +++ b/app/Exports/Sales/Revenues.php @@ -11,13 +11,7 @@ class Revenues extends Export implements WithColumnFormatting { public function collection() { - $model = Model::with('account', 'category', 'contact', 'invoice')->income()->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('account', 'category', 'contact', 'invoice')->income()->collectForExport($this->ids, ['paid_at' => 'desc']); } public function map($model): array diff --git a/app/Exports/Sales/Sheets/InvoiceHistories.php b/app/Exports/Sales/Sheets/InvoiceHistories.php index c0c0bf9c3..72c9756bc 100644 --- a/app/Exports/Sales/Sheets/InvoiceHistories.php +++ b/app/Exports/Sales/Sheets/InvoiceHistories.php @@ -9,13 +9,7 @@ class InvoiceHistories extends Export { public function collection() { - $model = Model::invoice()->with('document')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('document_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('document')->invoice()->collectForExport($this->ids, null, 'document_id'); } public function map($model): array diff --git a/app/Exports/Sales/Sheets/InvoiceItemTaxes.php b/app/Exports/Sales/Sheets/InvoiceItemTaxes.php index 666432cbf..dd0032bce 100644 --- a/app/Exports/Sales/Sheets/InvoiceItemTaxes.php +++ b/app/Exports/Sales/Sheets/InvoiceItemTaxes.php @@ -9,13 +9,7 @@ class InvoiceItemTaxes extends Export { public function collection() { - $model = Model::invoice()->with('document', 'item', 'tax')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('document_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('document', 'item', 'tax')->invoice()->collectForExport($this->ids, null, 'document_id'); } public function map($model): array diff --git a/app/Exports/Sales/Sheets/InvoiceItems.php b/app/Exports/Sales/Sheets/InvoiceItems.php index 2c6c1c045..bb0c93c12 100644 --- a/app/Exports/Sales/Sheets/InvoiceItems.php +++ b/app/Exports/Sales/Sheets/InvoiceItems.php @@ -9,13 +9,7 @@ class InvoiceItems extends Export { public function collection() { - $model = Model::invoice()->with('document', 'item')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('document_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('document', 'item')->invoice()->collectForExport($this->ids, null, 'document_id'); } public function map($model): array diff --git a/app/Exports/Sales/Sheets/InvoiceTotals.php b/app/Exports/Sales/Sheets/InvoiceTotals.php index 11e098ad4..a12133a04 100644 --- a/app/Exports/Sales/Sheets/InvoiceTotals.php +++ b/app/Exports/Sales/Sheets/InvoiceTotals.php @@ -9,13 +9,7 @@ class InvoiceTotals extends Export { public function collection() { - $model = Model::invoice()->with('document')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('document_id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('document')->invoice()->collectForExport($this->ids, null, 'document_id'); } public function map($model): array diff --git a/app/Exports/Sales/Sheets/InvoiceTransactions.php b/app/Exports/Sales/Sheets/InvoiceTransactions.php index b5aceb1d4..5a422ae9d 100644 --- a/app/Exports/Sales/Sheets/InvoiceTransactions.php +++ b/app/Exports/Sales/Sheets/InvoiceTransactions.php @@ -4,8 +4,6 @@ namespace App\Exports\Sales\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; @@ -13,13 +11,7 @@ class InvoiceTransactions 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(); + return Model::with('account', 'category', 'contact', 'document')->income()->isDocument()->collectForExport($this->ids, ['paid_at' => 'desc'], 'document_id'); } public function map($model): array diff --git a/app/Exports/Sales/Sheets/Invoices.php b/app/Exports/Sales/Sheets/Invoices.php index 9dd7c8165..3782abcef 100644 --- a/app/Exports/Sales/Sheets/Invoices.php +++ b/app/Exports/Sales/Sheets/Invoices.php @@ -11,13 +11,7 @@ class Invoices extends Export implements WithColumnFormatting { public function collection() { - $model = Model::invoice()->with('category')->usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + return Model::with('category')->invoice()->collectForExport($this->ids, ['document_number' => 'desc']); } public function map($model): array diff --git a/app/Exports/Settings/Categories.php b/app/Exports/Settings/Categories.php index b82fc958f..b00f6e86d 100644 --- a/app/Exports/Settings/Categories.php +++ b/app/Exports/Settings/Categories.php @@ -9,13 +9,7 @@ class Categories extends Export { public function collection() { - $model = Model::usingSearchString(request('search')); - - if (!empty($this->ids)) { - $model->whereIn('id', (array) $this->ids); - } - - return $model->cursor(); + return Model::collectForExport($this->ids); } public function fields(): array diff --git a/resources/lang/en-GB/messages.php b/resources/lang/en-GB/messages.php index 366d9f8dd..dd4e953f9 100644 --- a/resources/lang/en-GB/messages.php +++ b/resources/lang/en-GB/messages.php @@ -10,7 +10,7 @@ return [ 'imported' => ':type imported!', 'import_queued' => ':type import has been scheduled! You will receive an email when it is finished.', 'exported' => ':type exported!', - 'export_queued' => ':type export has been scheduled! You will receive an email when it is ready to download.', + 'export_queued' => ':type export of the current page has been scheduled! You will receive an email when it is ready to download.', 'enabled' => ':type enabled!', 'disabled' => ':type disabled!', ],