akaunting/app/Abstracts/Import.php

161 lines
4.2 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-21 09:56:45 +03:00
use App\Traits\Import as ImportHelper;
2020-03-25 20:21:42 +03:00
use App\Utilities\Date;
use Carbon\Exceptions\InvalidFormatException;
2021-04-16 00:59:43 +03:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Translation\HasLocalePreference;
2020-02-25 14:56:48 +03:00
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
2020-01-20 22:58:49 +03:00
use Maatwebsite\Excel\Concerns\Importable;
2021-04-16 00:59:43 +03:00
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
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;
2021-06-27 03:27:19 +03:00
use Maatwebsite\Excel\Concerns\WithLimit;
2019-11-16 10:21:14 +03:00
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithValidation;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
2019-11-16 10:21:14 +03:00
2021-06-27 03:27:19 +03:00
abstract class Import implements HasLocalePreference, ShouldQueue, SkipsEmptyRows, WithChunkReading, WithHeadingRow, WithLimit, WithMapping, WithValidation, ToModel
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
2021-04-16 00:59:43 +03:00
public $user;
public function __construct()
{
$this->user = user();
}
2020-01-20 22:58:49 +03:00
2019-11-16 10:21:14 +03:00
public function map($row): array
{
2021-04-16 00:59:43 +03:00
$row['company_id'] = company_id();
2021-06-17 10:59:07 +03:00
$row['created_by'] = $this->user->id;
2021-09-07 10:33:34 +03:00
$row['created_from'] = 'import';
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;
}
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
{
2021-06-27 03:27:19 +03:00
return config('excel.imports.chunk_size');
}
public function limit(): int
{
return config('excel.imports.row_limit');
2019-11-16 10:21:14 +03:00
}
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;
}
2021-04-16 00:59:43 +03:00
public function preferredLocale()
{
return $this->user->locale;
}
protected function replaceForBatchRules(array $rules): array
{
$dependent_rules = [
'after:',
'after_or_equal:',
'before:',
'before_or_equal:',
'different:',
'exclude_if:',
'exclude_unless:',
'gt:',
'gte:',
'in_array:',
'lt:',
'lte:',
'prohibited_if:',
'prohibited_unless:',
'required_if:',
'required_unless:',
'required_with:',
'required_with_all:',
'required_without:',
'required_without_all:',
'same:',
];
$batch_rules = [
'after:*.',
'after_or_equal:*.',
'before:*.',
'before_or_equal:*.',
'different:*.',
'exclude_if:*.',
'exclude_unless:*.',
'gt:*.',
'gte:*.',
'in_array:*.',
'lt:*.',
'lte:*.',
'prohibited_if:*.',
'prohibited_unless:*.',
'required_if:*.',
'required_unless:*.',
'required_with:*.',
'required_with_all:*.',
'required_without:*.',
'required_without_all:*.',
'same:*.',
];
return str_replace($dependent_rules, $batch_rules, $rules);
}
2020-01-20 01:26:35 +03:00
}