File: /var/www/ipsremont-demo/app/Console/Commands/ImportDetails.php
<?php
namespace App\Console\Commands;
use App\Http\Requests\Part\ImportPartRequest;
use App\Models\Part;
use App\Models\PartAnalog;
use App\Services\ImportService;
use App\Services\Part\PartService;
use App\Traits\Base1c;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\Console\Command\Command as CommandAlias;
class ImportDetails extends Command
{
use Base1c;
private string $method = 'GetDetails';
protected $channel = 'importDetails';
protected $signature = 'import:details {--e|echo}';
protected $description = 'Import details from 1c';
public function handle(): int
{
$this->isVerbose = (bool) $this->option('echo');
$this->info('Импортируем данные по деталям из 1С ' . date('Y-m-d H:i:s'));
$this->connect();
if (!$this->response) {
$this->error('Нет ответа');
return CommandAlias::FAILURE;
}
$data = $this->response->body();
$this->logResponse('importDetailsResponse', iconv('Windows-1251', 'UTF-8', trim($data)));
$data = $this->formatJsonData($data);
if (json_last_error() != JSON_ERROR_NONE) {
$this->error('Ошибка преобразования данных');
}
if ($this->isVerbose) {
$this->line('');
$this->warn('Данные из 1С:');
print_r($data);
$this->line('');
}
if (!$data) {
$this->info('Новых данных нет');
return Artisan::call('import:all-details' . ($this->isVerbose ? ' -e' : ''));
}
$resultArray = [];
$packageNumber = (int) $data[0]->PackageNumber;
$this->packageNumber = $packageNumber;
$this->date = date('Y-m-d H:i:s', strtotime($data[0]->DateRequest));
$this->info('Method: ' . $this->method);
$this->info('Package number: ' . $packageNumber);
if (empty($data[0]->МассивПоДеталям)) {
$this->info('Нечего импортировать');
return Artisan::call('import:all-details' . ($this->isVerbose ? ' -e' : ''));
}
$errors = [];
$data = $data[0]->МассивПоДеталям;
foreach ($data as $item) {
$this->total++;
$request = new ImportPartRequest();
$price = trim((string) $item->price);
$price = preg_replace('/[^0-9,.]/', '', $price);
$price = str_replace(',', '.', $price);
$price = (float) $price;
$innerData = [
'code' => $item->code,
'name' => $item->name,
'unit' => $item->BaseUnitOfMeasure,
'price' => $price,
'foto' => $item->image ?? '',
'action' => $item->action,
'storage' => $item->storage ?? [],
'analog' => $item->analogs ?? [],
];
$validator = Validator::make($innerData, $request->rules());
$dataObject = (object) $innerData;
if ($validator->fails()) {
$result = $validator->errors() . ' ';
$errors[] = $validator->errors();
$item->date = $this->date;
$this->failedItems[] = $item;
} else {
switch ($dataObject->action) {
case ImportService::NEW:
PartService::saveFromImport($dataObject);
/** @var Part $part */
$part = Part::query()->where('external_id', $dataObject->code)->first();
if (!empty($part)) {
if (isset($dataObject->storage) && $dataObject->storage) {
foreach ($dataObject->storage as $externalId => $amount) {
PartService::fillStorage($part, $externalId, (int) $amount);
}
}
if (isset($dataObject->analog) && $dataObject->analog) {
foreach ($dataObject->analog as $analog_external_id) {
PartAnalog::create(['part_external_id' => $part->external_id, 'analog_external_id' => $analog_external_id]);
}
}
}
break;
case ImportService::UPDATE:
PartService::saveFromImport($dataObject);
/** @var Part $part */
$part = Part::query()->where('external_id', $dataObject->code)->first();
if (!empty($part)) {
if (isset($dataObject->storage) && $dataObject->storage) {
foreach ($dataObject->storage as $externalId => $amount) {
PartService::fillStorage($part, $externalId, (int) $amount);
}
}
if (isset($dataObject->analog) && $dataObject->analog) {
PartAnalog::where('part_external_id', $part->external_id)->delete();
foreach ($dataObject->analog as $analog_external_id) {
PartAnalog::create([
'part_external_id' => $part->external_id,
'analog_external_id' => $analog_external_id,
]);
}
}
}
break;
case ImportService::DELETE:
case ImportService::DELETED:
Part::query()->where('external_id', $dataObject->code)->delete();
break;
}
$result = 1;
$this->success++;
}
$resultArray[] = ['code' => $dataObject->code, 'result' => $result];
}
$this->sendResult();
if (!empty($errors)) {
$this->error('Ошибки:');
foreach ($errors as $error) {
print_r($error->toArray());
}
}
$this->info('Готово!');
if (!empty($this->failedItems)) {
$this->warn('C ошибками: ' . count($this->failedItems));
}
$this->info('Итого: ' . $this->success . '/' . $this->total);
$this->formatResult($resultArray);
$res = Artisan::call('import:all-details' . ($this->isVerbose ? ' -e' : ''));
$this->line('');
print_r(Artisan::output());
return $res;
}
}