Skip to content

Commit

Permalink
Merge pull request #1287 from denisdulici/master
Browse files Browse the repository at this point in the history
Import rules not applicable in map
  • Loading branch information
denisdulici committed Feb 25, 2020
2 parents 695a3ed + 34c339c commit 084b597
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 12 deletions.
22 changes: 22 additions & 0 deletions app/Abstracts/Import.php
Expand Up @@ -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;
Expand All @@ -15,6 +16,7 @@
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
{
Expand Down Expand Up @@ -87,4 +89,24 @@ public function onError(\Throwable $e)
{
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;
}
}
8 changes: 6 additions & 2 deletions app/Imports/Purchases/Sheets/BillHistories.php
Expand Up @@ -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;
}
Expand All @@ -21,9 +21,13 @@ public function model(array $row)

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'];

Expand Down
8 changes: 6 additions & 2 deletions app/Imports/Purchases/Sheets/BillItemTaxes.php
Expand Up @@ -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;
}
Expand All @@ -23,9 +23,13 @@ public function model(array $row)

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();
Expand Down
6 changes: 5 additions & 1 deletion app/Imports/Purchases/Sheets/BillItems.php
Expand Up @@ -16,9 +16,13 @@ public function model(array $row)

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);
Expand Down
6 changes: 5 additions & 1 deletion app/Imports/Purchases/Sheets/BillTotals.php
Expand Up @@ -16,9 +16,13 @@ public function model(array $row)

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;
}
Expand Down
4 changes: 4 additions & 0 deletions app/Imports/Purchases/Sheets/BillTransactions.php
Expand Up @@ -15,6 +15,10 @@ public function model(array $row)

public function map($row): array
{
if ($this->isEmpty($row, 'bill_number')) {
return [];
}

$row = parent::map($row);

$row['type'] = 'expense';
Expand Down
4 changes: 4 additions & 0 deletions app/Imports/Purchases/Sheets/Bills.php
Expand Up @@ -15,6 +15,10 @@ public function model(array $row)

public function map($row): array
{
if ($this->isEmpty($row, 'bill_number')) {
return [];
}

$row = parent::map($row);

$row['category_id'] = $this->getCategoryId($row, 'expense');
Expand Down
8 changes: 6 additions & 2 deletions app/Imports/Sales/Sheets/InvoiceHistories.php
Expand Up @@ -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;
}
Expand All @@ -21,9 +21,13 @@ public function model(array $row)

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'];

Expand Down
8 changes: 6 additions & 2 deletions app/Imports/Sales/Sheets/InvoiceItemTaxes.php
Expand Up @@ -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;
}
Expand All @@ -23,9 +23,13 @@ public function model(array $row)

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();
Expand Down
6 changes: 5 additions & 1 deletion app/Imports/Sales/Sheets/InvoiceItems.php
Expand Up @@ -16,9 +16,13 @@ public function model(array $row)

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);
Expand Down
6 changes: 5 additions & 1 deletion app/Imports/Sales/Sheets/InvoiceTotals.php
Expand Up @@ -16,9 +16,13 @@ public function model(array $row)

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;
}
Expand Down
4 changes: 4 additions & 0 deletions app/Imports/Sales/Sheets/InvoiceTransactions.php
Expand Up @@ -15,6 +15,10 @@ public function model(array $row)

public function map($row): array
{
if ($this->isEmpty($row, 'invoice_number')) {
return [];
}

$row = parent::map($row);

$row['type'] = 'income';
Expand Down
4 changes: 4 additions & 0 deletions app/Imports/Sales/Sheets/Invoices.php
Expand Up @@ -15,6 +15,10 @@ public function model(array $row)

public function map($row): array
{
if ($this->isEmpty($row, 'invoice_number')) {
return [];
}

$row = parent::map($row);

$row['category_id'] = $this->getCategoryId($row, 'income');
Expand Down

0 comments on commit 084b597

Please sign in to comment.