File: //var/www/quadcode/database/migrations/2025_02_27_142000_hydrate_sync.php
<?php
use App\Helpers\BitrixHelper;
use App\Models\BitrixAcSyncSetting;
use App\Services\ActiveCampaignService;
use App\Services\BitrixService;
use Illuminate\Database\Migrations\Migration;
class HydrateSync extends Migration
{
public function up()
{
$bitrixService = app(BitrixService::class);
$activeCampaignService = app(ActiveCampaignService::class);
if (!$bitrixService->check()) {
throw new Exception('Need to fill BITRIX24_HOST, BITRIX24_USER_ID or BITRIX24_TOKEN in .env');
}
if (!$bitrixService->syncStatuses()) {
throw new Exception($bitrixService->lastError);
}
if (!$activeCampaignService->check()) {
throw new Exception('Need to fill ACTIVE_CAMPAIGN_HOST and ACTIVE_CAMPAIGN_TOKEN in .env');
}
if (!$activeCampaignService->syncLists()) {
throw new Exception($activeCampaignService->lastError);
}
$listId = getenv('ACTIVE_CAMPAIGN_BITRIX_NOT_ANSWERED_LIST_ID');
if (!empty($listId)) {
$bitrixAcSyncSetting = new BitrixAcSyncSetting();
$bitrixAcSyncSetting->entity_id = 'STATUS';
$bitrixAcSyncSetting->bitrix_status = BitrixHelper::STATUS_JUNK;
$bitrixAcSyncSetting->active_campaign_list_id = $listId;
$bitrixAcSyncSetting->save();
}
$listId = getenv('ACTIVE_CAMPAIGN_BITRIX_TOO_EXPENSIVE_LIST_ID');
if (!empty($listId)) {
$bitrixAcSyncSetting = new BitrixAcSyncSetting();
$bitrixAcSyncSetting->entity_id = 'DEAL_STAGE';
$bitrixAcSyncSetting->bitrix_status = BitrixHelper::DEAL_STAGE_APOLOGY;
$bitrixAcSyncSetting->active_campaign_list_id = $listId;
$bitrixAcSyncSetting->save();
}
$listId = getenv('ACTIVE_CAMPAIGN_BITRIX_NOT_ANSWERED_DEALS_LIST_ID');
if (!empty($listId)) {
$bitrixAcSyncSetting = new BitrixAcSyncSetting();
$bitrixAcSyncSetting->entity_id = 'DEAL_STAGE';
$bitrixAcSyncSetting->bitrix_status = BitrixHelper::DEAL_STAGE_NOT_ANSWERED;
$bitrixAcSyncSetting->active_campaign_list_id = $listId;
$bitrixAcSyncSetting->save();
}
$listId = getenv('ACTIVE_CAMPAIGN_BITRIX_ONBOARDED_CLIENTS_LIST_ID');
if (!empty($listId)) {
$bitrixAcSyncSetting = new BitrixAcSyncSetting();
$bitrixAcSyncSetting->entity_id = 'DEAL_STAGE';
$bitrixAcSyncSetting->bitrix_status = BitrixHelper::DEAL_STAGE_WON;
$bitrixAcSyncSetting->active_campaign_list_id = $listId;
$bitrixAcSyncSetting->with_unsubscribe = false;
$bitrixAcSyncSetting->save();
}
}
public function down()
{
}
}