akaunting/app/Abstracts/Import.php

63 lines
1.6 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
2020-01-20 01:26:35 +03:00
namespace App\Abstracts;
2019-11-16 10:21:14 +03:00
2020-01-20 01:26:35 +03:00
use Illuminate\Support\Str;
2019-11-16 10:21:14 +03:00
use Maatwebsite\Excel\Concerns\ToModel;
2020-01-20 01:26:35 +03:00
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
2019-11-16 10:21:14 +03:00
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Validators\Failure;
2020-01-20 01:26:35 +03:00
abstract class Import implements ToModel, WithBatchInserts, WithChunkReading, WithHeadingRow, WithMapping, WithValidation
2019-11-16 10:21:14 +03:00
{
public function map($row): array
{
$row['company_id'] = session('company_id');
2020-01-20 01:26:35 +03:00
// Make enabled field integer
if (isset($row['enabled'])) {
$row['enabled'] = (int) $row['enabled'];
}
2019-11-16 10:21:14 +03:00
// Make reconciled field integer
if (isset($row['reconciled'])) {
$row['reconciled'] = (int) $row['reconciled'];
}
return $row;
}
public function rules(): array
{
2020-01-20 01:26:35 +03:00
return [];
}
public function batchSize(): int
{
return 100;
}
public function chunkSize(): int
{
return 100;
2019-11-16 10:21:14 +03:00
}
public function onFailure(Failure ...$failures)
{
2020-01-20 01:26:35 +03:00
$sheet = Str::snake((new \ReflectionClass($this))->getShortName());
2019-11-16 10:21:14 +03:00
foreach ($failures as $failure) {
$message = trans('messages.error.import_column', [
'message' => $failure->errors()->first(),
2020-01-20 01:26:35 +03:00
'sheet' => $sheet,
2019-11-16 10:21:14 +03:00
'line' => $failure->attribute(),
]);
2020-01-20 01:26:35 +03:00
2019-11-16 10:21:14 +03:00
flash($message)->error()->important();
}
}
2020-01-20 01:26:35 +03:00
}