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/quadcode/app/Console/Commands/SyncBitrixLeadsToRoistat.php
<?php

namespace App\Console\Commands;

use App\Helpers\BitrixHelper;
use App\Helpers\RoistatHelper;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;

class SyncBitrixLeadsToRoistat extends Command
{

    protected $signature = 'bitrix:sync-to-roistat';

    protected $description = 'Sync Bitrix leads to Roistat';

    public function handle(BitrixHelper $bitrixHelper, RoistatHelper $roistatHelper): int
    {
        $this->info($this->description);

        if (!$bitrixHelper->check()) {
            $this->error('Need to fill BITRIX24_HOST, BITRIX24_USER_ID or BITRIX24_TOKEN in .env');

            return CommandAlias::FAILURE;
        }

        if (!$roistatHelper->check()) {
            $this->error('Need to fill ROISTAT_INTEGRATION_HOST, ROISTAT_INTEGRATION_TOKEN, ROISTAT_API_HOST, ROISTAT_API_PROJECT_ID or ROISTAT_API_TOKEN in .env');

            return CommandAlias::FAILURE;
        }

        $today = date('Y-m-d');
        $yesterday = date('Y-m-d', strtotime('-1 day'));

        $filter = [
            '>' . BitrixHelper::CUSTOM_ROISTAT_ID => 0,
            '>=DATE_CREATE' => $yesterday,
        ];
        $select = ['ID', 'TITLE', 'NAME', 'SECOND_NAME', 'LAST_NAME', 'COMMENTS', 'EMAIL', 'PHONE', BitrixHelper::CUSTOM_ROISTAT_ID, BitrixHelper::CUSTOM_FIELD_LANDING_URL];
        $leads = $bitrixHelper->leadList($filter, $select);
        if (empty($leads)) {
            return CommandAlias::SUCCESS;
        }
        $this->info('Leads found: ' . count($leads));

        $period = $yesterday . '-' . $today;
        $response = $roistatHelper->getLeads(compact('period'));
        if (!$response->successful()) {
            $this->warn('Response error: ' . $response->status());

            return CommandAlias::FAILURE;
        }
        $roistatLeads = $response->json()['ProxyLeads'];

        $this->info('Roistat leads found: ' . count($leads));
        $bitrixIds = [];
        foreach ($roistatLeads as $roistatLead) {
            if (empty($roistatLead['order_fields']['bitrix_id'])) {
                continue;
            }
            $bitrixIds[] = $roistatLead['order_fields']['bitrix_id'];
        }
        $bitrixIds = array_unique($bitrixIds);

        foreach ($leads as $lead) {
            if (in_array($lead['ID'], $bitrixIds) || empty($lead[BitrixHelper::CUSTOM_ROISTAT_ID])) {
                $this->line('Skipped bitrix_id: ' . $lead['ID']);
                continue;
            }

            $name = join(' ', array_filter([$lead['NAME'], $lead['SECOND_NAME'], $lead['LAST_NAME']]));
            $data = [
                'roistat' => $lead[BitrixHelper::CUSTOM_ROISTAT_ID],
                'title' => $lead['TITLE'],
                'comment' => $lead['COMMENTS'] ?? null,
                'name' => $name,
                'email' => $lead['EMAIL'][0]['VALUE'] ?? null,
                'phone' => $lead['PHONE'][0]['VALUE'] ?? null,
                'is_skip_sending' => 1,
                'fields' => [
                    'bitrix_id' => $lead['ID'],
                    'landing_url' => $lead[BitrixHelper::CUSTOM_FIELD_LANDING_URL],
                ],
            ];
            $data = array_filter($data);
            $response = $roistatHelper->add($data);
            if (!$response->successful()) {
                $this->warn('Response error: ' . $response->status());

                return CommandAlias::FAILURE;
            }
        }

        return CommandAlias::SUCCESS;
    }

}