import rules not applicable in map

This commit is contained in:
denisdulici 2020-02-25 14:56:48 +03:00
parent 695a3ed0b1
commit 34c339ca8d
13 changed files with 82 additions and 12 deletions

View File

@ -3,6 +3,7 @@
namespace App\Abstracts; namespace App\Abstracts;
use App\Traits\Import as ImportHelper; use App\Traits\Import as ImportHelper;
use Illuminate\Support\Arr;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Jenssegers\Date\Date; use Jenssegers\Date\Date;
use Maatwebsite\Excel\Concerns\Importable; use Maatwebsite\Excel\Concerns\Importable;
@ -15,6 +16,7 @@ use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMapping; use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithValidation; use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Validators\Failure; use Maatwebsite\Excel\Validators\Failure;
use Illuminate\Support\Facades\Validator;
abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatchInserts, WithChunkReading, WithHeadingRow, WithMapping, WithValidation 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(); 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;
}
} }

View File

@ -11,7 +11,7 @@ class BillHistories extends Import
{ {
public function model(array $row) 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) { if ($row['bill_number'] == $this->empty_field) {
return null; return null;
} }
@ -21,9 +21,13 @@ class BillHistories extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'bill_number')) {
return [];
}
$row = parent::map($row); $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']; $row['notify'] = (int) $row['notify'];

View File

@ -13,7 +13,7 @@ class BillItemTaxes extends Import
{ {
public function model(array $row) 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) { if ($row['bill_number'] == $this->empty_field) {
return null; return null;
} }
@ -23,9 +23,13 @@ class BillItemTaxes extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'bill_number')) {
return [];
}
$row = parent::map($row); $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'])) { if (empty($row['invoice_item_id']) && !empty($row['item_name'])) {
$item_id = Item::name($row['item_name'])->pluck('id')->first(); $item_id = Item::name($row['item_name'])->pluck('id')->first();

View File

@ -16,9 +16,13 @@ class BillItems extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'bill_number')) {
return [];
}
$row = parent::map($row); $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'])) { if (empty($row['item_id']) && !empty($row['item_name'])) {
$row['item_id'] = $this->getItemIdFromName($row); $row['item_id'] = $this->getItemIdFromName($row);

View File

@ -16,9 +16,13 @@ class BillTotals extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'bill_number')) {
return [];
}
$row = parent::map($row); $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; return $row;
} }

View File

@ -15,6 +15,10 @@ class BillTransactions extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'bill_number')) {
return [];
}
$row = parent::map($row); $row = parent::map($row);
$row['type'] = 'expense'; $row['type'] = 'expense';

View File

@ -15,6 +15,10 @@ class Bills extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'bill_number')) {
return [];
}
$row = parent::map($row); $row = parent::map($row);
$row['category_id'] = $this->getCategoryId($row, 'expense'); $row['category_id'] = $this->getCategoryId($row, 'expense');

View File

@ -11,7 +11,7 @@ class InvoiceHistories extends Import
{ {
public function model(array $row) 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) { if ($row['invoice_number'] == $this->empty_field) {
return null; return null;
} }
@ -21,9 +21,13 @@ class InvoiceHistories extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'invoice_number')) {
return [];
}
$row = parent::map($row); $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']; $row['notify'] = (int) $row['notify'];

View File

@ -13,7 +13,7 @@ class InvoiceItemTaxes extends Import
{ {
public function model(array $row) 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) { if ($row['invoice_number'] == $this->empty_field) {
return null; return null;
} }
@ -23,9 +23,13 @@ class InvoiceItemTaxes extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'invoice_number')) {
return [];
}
$row = parent::map($row); $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'])) { if (empty($row['invoice_item_id']) && !empty($row['item_name'])) {
$item_id = Item::name($row['item_name'])->pluck('id')->first(); $item_id = Item::name($row['item_name'])->pluck('id')->first();

View File

@ -16,9 +16,13 @@ class InvoiceItems extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'invoice_number')) {
return [];
}
$row = parent::map($row); $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'])) { if (empty($row['item_id']) && !empty($row['item_name'])) {
$row['item_id'] = $this->getItemIdFromName($row); $row['item_id'] = $this->getItemIdFromName($row);

View File

@ -16,9 +16,13 @@ class InvoiceTotals extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'invoice_number')) {
return [];
}
$row = parent::map($row); $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; return $row;
} }

View File

@ -15,6 +15,10 @@ class InvoiceTransactions extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'invoice_number')) {
return [];
}
$row = parent::map($row); $row = parent::map($row);
$row['type'] = 'income'; $row['type'] = 'income';

View File

@ -15,6 +15,10 @@ class Invoices extends Import
public function map($row): array public function map($row): array
{ {
if ($this->isEmpty($row, 'invoice_number')) {
return [];
}
$row = parent::map($row); $row = parent::map($row);
$row['category_id'] = $this->getCategoryId($row, 'income'); $row['category_id'] = $this->getCategoryId($row, 'income');