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-site/app/Console/Commands/SyncBitrixNotAnsweredLeads.php
<?php

namespace App\Console\Commands;

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

class SyncBitrixNotAnsweredLeads extends AbstractSyncBitrixAndActiveCampaign
{

    protected $signature = 'bitrix:sync-not-answered-leads';

    protected $description = 'Sync Bitrix "not-answered" leads to Active Campaign';

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

        $this->activeCampaignHelper = $activeCampaignHelper;

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

            return CommandAlias::FAILURE;
        }

        $listId = getenv('ACTIVE_CAMPAIGN_BITRIX_NOT_ANSWERED_LIST_ID');
        if (!$activeCampaignHelper->check() || empty($listId)) {
            $this->error('Need to fill ACTIVE_CAMPAIGN_HOST, ACTIVE_CAMPAIGN_TOKEN or ACTIVE_CAMPAIGN_BITRIX_NOT_ANSWERED_LIST_ID in .env');

            return CommandAlias::FAILURE;
        }

        $filter = [
            'STATUS_ID' => BitrixHelper::STATUS_JUNK,
            '>LAST_ACTIVITY_TIME' => date('Y-m-d', strtotime('-2 week')),
        ];
        $select = ['NAME', 'EMAIL', 'PHONE'];
        $leads = $bitrixHelper->leadList($filter, select: $select);

        $contacts = [];
        foreach ($leads as $lead) {
            $contact = $this->convertBitrixEntityToActiveCampaignContact($lead);
            if (empty($contact)) {
                continue;
            }

            $contacts[$contact['email']] = $contact;
        }
        $uniqueEmails = array_unique(array_column($contacts, 'email'));

        $this->line('Contacts found: ' . count($contacts));
        $this->line('Unique contacts found: ' . count($uniqueEmails));

        $this->syncContactsToActiveCampaign($listId, $contacts);

        $this->info('Done');

        return CommandAlias::SUCCESS;
    }

}