HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/ipsremont-demo/app/Console/Commands/AbstractImportDocument.php
<?php

namespace App\Console\Commands;

use App\Models\Region;
use App\Services\Document\DocumentService;
use Exception;
use SimpleXMLElement;
use Symfony\Component\Console\Command\Command as CommandAlias;

abstract class AbstractImportDocument extends AbstractImport
{

    protected string $title = '';

    protected string $documentName;

    public function __construct(bool $isWeb = false)
    {
        parent::__construct();

        $this->isWeb = $isWeb;
    }

    protected function formatData(string $data)
    {
        $data = str_replace('&', '', trim($data));
        if (empty($data)) {
            return false;
        }

        $data = iconv('Windows-1251', 'UTF-8', $data);

        return new SimpleXMLElement($data);
    }

    abstract protected function getArray($data);

    public function executeCommand(Region $region)
    {
        $forceSuccess = false;
        $this->isVerbose = false;
        if (!$this->isWeb) {
            $this->info($this->title);
            $this->isVerbose = (bool) $this->option('echo');
            $forceSuccess = (bool) $this->option('force');
        }

        if ($this->isDebug) {
            $this->warn('Данные из 1С:');
            $data = $this->loadFromFile();
        } else {
            try {
                $this->connect(false, $region->external_database_code);
            } catch (Exception $exception) {
                if ($this->isWeb) {
                    return $exception->getMessage();
                }

                $this->error($exception->getMessage());

                return CommandAlias::FAILURE;
            }

            if (!$this->response) {
                $error = __('errors.noResponse');
                if ($this->isWeb) {
                    return $error;
                }
                $this->warn('Нет ответа от сервера 1С');

                return CommandAlias::FAILURE;
            }

            if ($this->response->status() !== 200) {
                $error = __('errors.responseStatusIs');
                if ($this->isWeb) {
                    return $error . $this->response->status() . '<br/>' . $this->response->body();
                }
                $this->error($error . $this->response->status());

                return CommandAlias::FAILURE;
            }

            $data = $this->response->body();
        }

        if ($this->isVerbose) {
            $this->line('');
            $this->warn('Данные из 1С:');
            print_r($data);
            $this->line('');
        }

        $data = $this->formatData($data);

        $resultArray = [];

        $this->packageNumber = (int) $data->PackageNumber;
        $this->date = date('Y-m-d H:i:s', strtotime($data->DateOfRequest));

        $method = trim($data->NameMethod);
        if ($this->isVerbose) {
            $this->line('Метод: ' . $method);
            $this->line('Номер пакета: ' . $this->packageNumber);
        }

        $data = static::getArray($data);
        // TODO Что-то странное
        if (CommandAlias::FAILURE === $data) {
            return CommandAlias::FAILURE;
        }

        if ($forceSuccess) {
            $this->total = 1;
            $this->success = 1;
        } else {
            // Для тестов
            $count = 0;
            foreach ($data as $item) {
                $count++;
                $this->total++;
                $result = [];
                $document = [
                    'method' => trim($method),
                    'date' => trim((string) $item->Date),
                    'number' => trim((string) $item->Number),
                    'sum' => trim((string) $item->Summa),
                    'inn' => trim((string) $item->Inn),
                    'code' => trim((string) $item->Code),
                ];
                $dResult = DocumentService::import($document);
                $result[] = $dResult;
                if (empty($dResult['id'])) {                      // Нормальное
                    // if (true || empty($dResult['id'])) {       // Вернуть все
                    // if ($count > 1 || empty($dResult['id'])) { // Вернуть все кроме первой
                    $item->date = trim((string) $item->Date);
                    // Тут надо брать Number потому что Code это код контрагента
                    $item->code = trim((string) $item->Number);
                    $item->name = $this->documentName . ' ' . trim($item->code) . ' от ' . date('Y-m-d H:i:s', strtotime($item->date));
                    $this->failedItems[] = $item;
                    if (!$this->isWeb) {
                        $this->error($dResult['result'] ?? 'Тест');
                    }

                    continue;
                }

                $this->success++;

                $resultArray[] = $result;
            }
        }

        $this->sendResult();

        if (!$this->isWeb) {
            $this->info('Готово!');
            if (!$forceSuccess) {
                $this->info('Итого: ' . $this->success . '/' . $this->total);
            }

            return $this->formatResult($resultArray);
        }

        return CommandAlias::SUCCESS;
    }

}