import rules not applicable in map
This commit is contained in:
parent
695a3ed0b1
commit
34c339ca8d
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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'];
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
@ -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);
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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';
|
||||||
|
@ -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');
|
||||||
|
@ -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'];
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
@ -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);
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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';
|
||||||
|
@ -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');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user