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/ImportServices.php
<?php

namespace App\Console\Commands;

use App\Models\Device;
use App\Models\DeviceParts;
use App\Models\DevicesSchema;
use App\Models\Order;
use App\Models\Schema;
use App\Models\SchemaParts;
use App\Services\Device\DeviceService;
use App\Services\Order\OrderService;
use App\Services\Part\PartService;
use App\Services\Schema\SchemaService;
use App\Services\Service\ServiceService;
use App\Traits\Base1c;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Command\Command as CommandAlias;

class ImportServices extends Command
{
    use Base1c;

    protected $channel = 'importServices';

    private string $method = 'GetCustomers';

    /**
     * Load orders from 1c
     *
     * @var string
     */
    protected $signature = 'import:services {--e|echo}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import services from 1c';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param string $data
     * @return string
     */
    protected function formatData($data)
    {
        $data = trim($data);
        $data = str_replace('&', '', $data);

        if (empty($data)) return false;

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $isVerbose = (bool) $this->option('echo');

        $this->connect();
        if (!$this->response) {
            echo 'no response' . PHP_EOL;
            echo 'exit';
            return CommandAlias::FAILURE;
        }

        if ($this->response->status() !== 200) {
            echo 'response status: ' . $this->response->status() . PHP_EOL;
            echo 'exit';
            return CommandAlias::FAILURE;
        }

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

        if ($isVerbose) {
            print_r($data);
        }

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

        /*
         * Ожидаемый формат
         * [
         *   {
         *     "NameMethod": "GetSubdivision",
         *     "DateRequest": "01.03.2023 17:15:25",
         *     "PackageNumber": 1,
         *     "МассивПоКонтрагентам": [
         *       {
         *         "name": "",
         *         "INN": "",
         *         "FullName": "",
         *         "id": "",
         *         "code": "",
         *         "email": "",
         *         "phone": "",
         *         "country": "",
         *         "city": "",
         *         "adres": ""
         *       },
         *       ...
         *     ]
         *   }
         * ]
         *
         */
        $resultArray = [];

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

        $this->line('Method: ' . $this->method);
        $this->line('Package number: ' . $this->packageNumber);

        $errors = [];
        $failedItems = [];
        $total = 0;
        $success = 0;
        foreach ($data[0]->МассивПоКонтрагентам as $item) {
            $total++;
            $result = [];
            $dResult = ServiceService::import($item);
            $result[] = $dResult;
            if (!empty($dResult['id'])) {
                $success++;
            } else {
                $failedItems[] = $item;
                $this->info($dResult['result']);
                $errors[] = $dResult['result'];
            }
            $resultArray[] = $result;
        }

        if ($total) {
            switch ($success) {
                case $total:
                    $response = $this->sendSuccess();
                    $this->info('Send Success');
                    break;
                case 0:
                    $response = $this->sendNothing();
                    $this->info('Send Nothing');
                    break;
                default:
                    $response = $this->sendPartly($failedItems);
                    $this->info('Send Partly');
                    break;
            }

            if (!empty($response->SereveAnswers) && false !== strpos((string) $response->SereveAnswers, 'success')) {
                $this->info('Success message sent');
            } else {
                var_dump($response);
                $this->info('No correct response');
            }
        }

        foreach ($errors as $error) {
            $this->error($error);
        }

        $this->info('Done! Sync items ' . $success);
        $this->info('Total ' . $total);

        $this->formatResult($resultArray);

        return CommandAlias::SUCCESS;
    }


}