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-21 09:56:45 +03:00
|
|
|
use App\Traits\Import as ImportHelper;
|
2020-03-25 20:21:42 +03:00
|
|
|
use App\Utilities\Date;
|
2021-01-28 15:50:45 +03:00
|
|
|
use Carbon\Exceptions\InvalidFormatException;
|
2020-02-25 14:56:48 +03:00
|
|
|
use Illuminate\Support\Arr;
|
2021-01-28 15:50:45 +03:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
2020-01-20 01:26:35 +03:00
|
|
|
use Illuminate\Support\Str;
|
2020-01-20 22:58:49 +03:00
|
|
|
use Maatwebsite\Excel\Concerns\Importable;
|
|
|
|
use Maatwebsite\Excel\Concerns\SkipsOnError;
|
|
|
|
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
|
2021-01-28 15:50:45 +03:00
|
|
|
use Maatwebsite\Excel\Concerns\ToModel;
|
2020-01-20 01:26:35 +03:00
|
|
|
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-10-08 15:22:13 +03:00
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-02-20 20:31:18 +03:00
|
|
|
abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithChunkReading, WithHeadingRow, WithMapping, WithValidation
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2020-01-21 09:56:45 +03:00
|
|
|
use Importable, ImportHelper;
|
2020-01-20 22:58:49 +03:00
|
|
|
|
|
|
|
public $empty_field = 'empty---';
|
|
|
|
|
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'];
|
|
|
|
}
|
|
|
|
|
2020-11-13 03:07:30 +03:00
|
|
|
$date_fields = ['paid_at', 'invoiced_at', 'billed_at', 'due_at', 'issued_at', 'created_at', 'transferred_at'];
|
2020-01-20 11:12:14 +03:00
|
|
|
foreach ($date_fields as $date_field) {
|
|
|
|
if (!isset($row[$date_field])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:50:45 +03:00
|
|
|
try {
|
|
|
|
$row[$date_field] = Date::parse(ExcelDate::excelToDateTimeObject($row[$date_field]))
|
|
|
|
->format('Y-m-d H:i:s');
|
|
|
|
} catch (InvalidFormatException | \Exception $e) {
|
|
|
|
Log::info($e->getMessage());
|
|
|
|
}
|
2020-01-20 11:12:14 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rules(): array
|
|
|
|
{
|
2020-01-20 01:26:35 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-01-20 22:58:49 +03:00
|
|
|
// @todo remove after 3.2 release https://github.com/Maatwebsite/Laravel-Excel/issues/1834#issuecomment-474340743
|
|
|
|
if (collect($failure->values())->first() == $this->empty_field) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = trans('messages.error.import_column', [
|
2020-01-20 22:58:49 +03:00
|
|
|
'message' => collect($failure->errors())->first(),
|
2020-01-20 01:26:35 +03:00
|
|
|
'sheet' => $sheet,
|
2020-01-20 22:58:49 +03:00
|
|
|
'line' => $failure->row(),
|
2019-11-16 10:21:14 +03:00
|
|
|
]);
|
2020-01-20 01:26:35 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->error()->important();
|
|
|
|
}
|
|
|
|
}
|
2020-01-20 22:58:49 +03:00
|
|
|
|
|
|
|
public function onError(\Throwable $e)
|
|
|
|
{
|
|
|
|
flash($e->getMessage())->error()->important();
|
|
|
|
}
|
2020-02-25 14:56:48 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-01-20 01:26:35 +03:00
|
|
|
}
|