diff --git a/app/Abstracts/Import.php b/app/Abstracts/Import.php index a84907497..4e639ceeb 100644 --- a/app/Abstracts/Import.php +++ b/app/Abstracts/Import.php @@ -3,6 +3,7 @@ namespace App\Abstracts; use App\Traits\Import as ImportHelper; +use Illuminate\Support\Arr; use Illuminate\Support\Str; use Jenssegers\Date\Date; use Maatwebsite\Excel\Concerns\Importable; @@ -15,6 +16,7 @@ use Maatwebsite\Excel\Concerns\WithHeadingRow; use Maatwebsite\Excel\Concerns\WithMapping; use Maatwebsite\Excel\Concerns\WithValidation; use Maatwebsite\Excel\Validators\Failure; +use Illuminate\Support\Facades\Validator; abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatchInserts, WithChunkReading, WithHeadingRow, WithMapping, WithValidation { @@ -87,4 +89,24 @@ abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatc { flash($e->getMessage())->error()->important(); } + + public function isNotValid($row) + { + return Validator::make($row, $this->rules())->fails(); + } + + public function isEmpty($row, $fields) + { + $fields = Arr::wrap($fields); + + foreach ($fields as $field) { + if (!empty($row[$field])) { + continue; + } + + return true; + } + + return false; + } } diff --git a/app/Imports/Purchases/Sheets/BillHistories.php b/app/Imports/Purchases/Sheets/BillHistories.php index b534ef5cf..c42bc7b9f 100644 --- a/app/Imports/Purchases/Sheets/BillHistories.php +++ b/app/Imports/Purchases/Sheets/BillHistories.php @@ -11,7 +11,7 @@ class BillHistories extends Import { public function model(array $row) { - // @todo remove after 3.2 release + // @todo remove after laravel-excel 3.2 release if ($row['bill_number'] == $this->empty_field) { return null; } @@ -21,9 +21,13 @@ class BillHistories extends Import public function map($row): array { + if ($this->isEmpty($row, 'bill_number')) { + return []; + } + $row = parent::map($row); - $row['bill_id'] = Bill::number($row['bill_number'])->pluck('id')->first(); + $row['bill_id'] = (int) Bill::number($row['bill_number'])->pluck('id')->first(); $row['notify'] = (int) $row['notify']; diff --git a/app/Imports/Purchases/Sheets/BillItemTaxes.php b/app/Imports/Purchases/Sheets/BillItemTaxes.php index 817ccb955..21b0062ba 100644 --- a/app/Imports/Purchases/Sheets/BillItemTaxes.php +++ b/app/Imports/Purchases/Sheets/BillItemTaxes.php @@ -13,7 +13,7 @@ class BillItemTaxes extends Import { public function model(array $row) { - // @todo remove after 3.2 release + // @todo remove after laravel-excel 3.2 release if ($row['bill_number'] == $this->empty_field) { return null; } @@ -23,9 +23,13 @@ class BillItemTaxes extends Import public function map($row): array { + if ($this->isEmpty($row, 'bill_number')) { + return []; + } + $row = parent::map($row); - $row['bill_id'] = Bill::number($row['bill_number'])->pluck('id')->first(); + $row['bill_id'] = (int) Bill::number($row['bill_number'])->pluck('id')->first(); if (empty($row['invoice_item_id']) && !empty($row['item_name'])) { $item_id = Item::name($row['item_name'])->pluck('id')->first(); diff --git a/app/Imports/Purchases/Sheets/BillItems.php b/app/Imports/Purchases/Sheets/BillItems.php index 5e1f254e9..c3ddc5c0d 100644 --- a/app/Imports/Purchases/Sheets/BillItems.php +++ b/app/Imports/Purchases/Sheets/BillItems.php @@ -16,9 +16,13 @@ class BillItems extends Import public function map($row): array { + if ($this->isEmpty($row, 'bill_number')) { + return []; + } + $row = parent::map($row); - $row['bill_id'] = Bill::number($row['bill_number'])->pluck('id')->first(); + $row['bill_id'] = (int) Bill::number($row['bill_number'])->pluck('id')->first(); if (empty($row['item_id']) && !empty($row['item_name'])) { $row['item_id'] = $this->getItemIdFromName($row); diff --git a/app/Imports/Purchases/Sheets/BillTotals.php b/app/Imports/Purchases/Sheets/BillTotals.php index b425d9f26..6dfbb591c 100644 --- a/app/Imports/Purchases/Sheets/BillTotals.php +++ b/app/Imports/Purchases/Sheets/BillTotals.php @@ -16,9 +16,13 @@ class BillTotals extends Import public function map($row): array { + if ($this->isEmpty($row, 'bill_number')) { + return []; + } + $row = parent::map($row); - $row['bill_id'] = Bill::number($row['bill_number'])->pluck('id')->first(); + $row['bill_id'] = (int) Bill::number($row['bill_number'])->pluck('id')->first(); return $row; } diff --git a/app/Imports/Purchases/Sheets/BillTransactions.php b/app/Imports/Purchases/Sheets/BillTransactions.php index 6f6952687..b5e08c265 100644 --- a/app/Imports/Purchases/Sheets/BillTransactions.php +++ b/app/Imports/Purchases/Sheets/BillTransactions.php @@ -15,6 +15,10 @@ class BillTransactions extends Import public function map($row): array { + if ($this->isEmpty($row, 'bill_number')) { + return []; + } + $row = parent::map($row); $row['type'] = 'expense'; diff --git a/app/Imports/Purchases/Sheets/Bills.php b/app/Imports/Purchases/Sheets/Bills.php index 768b1fa97..38202527e 100644 --- a/app/Imports/Purchases/Sheets/Bills.php +++ b/app/Imports/Purchases/Sheets/Bills.php @@ -15,6 +15,10 @@ class Bills extends Import public function map($row): array { + if ($this->isEmpty($row, 'bill_number')) { + return []; + } + $row = parent::map($row); $row['category_id'] = $this->getCategoryId($row, 'expense'); diff --git a/app/Imports/Sales/Sheets/InvoiceHistories.php b/app/Imports/Sales/Sheets/InvoiceHistories.php index a40afcdd2..aa5422647 100644 --- a/app/Imports/Sales/Sheets/InvoiceHistories.php +++ b/app/Imports/Sales/Sheets/InvoiceHistories.php @@ -11,7 +11,7 @@ class InvoiceHistories extends Import { public function model(array $row) { - // @todo remove after 3.2 release + // @todo remove after laravel-excel 3.2 release if ($row['invoice_number'] == $this->empty_field) { return null; } @@ -21,9 +21,13 @@ class InvoiceHistories extends Import public function map($row): array { + if ($this->isEmpty($row, 'invoice_number')) { + return []; + } + $row = parent::map($row); - $row['invoice_id'] = Invoice::number($row['invoice_number'])->pluck('id')->first(); + $row['invoice_id'] = (int) Invoice::number($row['invoice_number'])->pluck('id')->first(); $row['notify'] = (int) $row['notify']; diff --git a/app/Imports/Sales/Sheets/InvoiceItemTaxes.php b/app/Imports/Sales/Sheets/InvoiceItemTaxes.php index 68cfaf91a..79f77d169 100644 --- a/app/Imports/Sales/Sheets/InvoiceItemTaxes.php +++ b/app/Imports/Sales/Sheets/InvoiceItemTaxes.php @@ -13,7 +13,7 @@ class InvoiceItemTaxes extends Import { public function model(array $row) { - // @todo remove after 3.2 release + // @todo remove after laravel-excel 3.2 release if ($row['invoice_number'] == $this->empty_field) { return null; } @@ -23,9 +23,13 @@ class InvoiceItemTaxes extends Import public function map($row): array { + if ($this->isEmpty($row, 'invoice_number')) { + return []; + } + $row = parent::map($row); - $row['invoice_id'] = Invoice::number($row['invoice_number'])->pluck('id')->first(); + $row['invoice_id'] = (int) Invoice::number($row['invoice_number'])->pluck('id')->first(); if (empty($row['invoice_item_id']) && !empty($row['item_name'])) { $item_id = Item::name($row['item_name'])->pluck('id')->first(); diff --git a/app/Imports/Sales/Sheets/InvoiceItems.php b/app/Imports/Sales/Sheets/InvoiceItems.php index 0222bf547..b0162eab1 100644 --- a/app/Imports/Sales/Sheets/InvoiceItems.php +++ b/app/Imports/Sales/Sheets/InvoiceItems.php @@ -16,9 +16,13 @@ class InvoiceItems extends Import public function map($row): array { + if ($this->isEmpty($row, 'invoice_number')) { + return []; + } + $row = parent::map($row); - $row['invoice_id'] = Invoice::number($row['invoice_number'])->pluck('id')->first(); + $row['invoice_id'] = (int) Invoice::number($row['invoice_number'])->pluck('id')->first(); if (empty($row['item_id']) && !empty($row['item_name'])) { $row['item_id'] = $this->getItemIdFromName($row); diff --git a/app/Imports/Sales/Sheets/InvoiceTotals.php b/app/Imports/Sales/Sheets/InvoiceTotals.php index 3d8606497..b3d1d5424 100644 --- a/app/Imports/Sales/Sheets/InvoiceTotals.php +++ b/app/Imports/Sales/Sheets/InvoiceTotals.php @@ -16,9 +16,13 @@ class InvoiceTotals extends Import public function map($row): array { + if ($this->isEmpty($row, 'invoice_number')) { + return []; + } + $row = parent::map($row); - $row['invoice_id'] = Invoice::number($row['invoice_number'])->pluck('id')->first(); + $row['invoice_id'] = (int) Invoice::number($row['invoice_number'])->pluck('id')->first(); return $row; } diff --git a/app/Imports/Sales/Sheets/InvoiceTransactions.php b/app/Imports/Sales/Sheets/InvoiceTransactions.php index 1b4ec7e9f..2c5e94b11 100644 --- a/app/Imports/Sales/Sheets/InvoiceTransactions.php +++ b/app/Imports/Sales/Sheets/InvoiceTransactions.php @@ -15,6 +15,10 @@ class InvoiceTransactions extends Import public function map($row): array { + if ($this->isEmpty($row, 'invoice_number')) { + return []; + } + $row = parent::map($row); $row['type'] = 'income'; diff --git a/app/Imports/Sales/Sheets/Invoices.php b/app/Imports/Sales/Sheets/Invoices.php index d13377113..43b986dfd 100644 --- a/app/Imports/Sales/Sheets/Invoices.php +++ b/app/Imports/Sales/Sheets/Invoices.php @@ -15,6 +15,10 @@ class Invoices extends Import public function map($row): array { + if ($this->isEmpty($row, 'invoice_number')) { + return []; + } + $row = parent::map($row); $row['category_id'] = $this->getCategoryId($row, 'income');