]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Очистка от телефонов стоплиста перед рассылкой
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 21 Apr 2025 08:10:00 +0000 (11:10 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 21 Apr 2025 08:10:00 +0000 (11:10 +0300)
erp24/commands/CronController.php

index eb43965d22f4ed981164e1b247c50c4d71473a45..830fa07a8172f436143c688bb3d70fd22849a4f6 100644 (file)
@@ -11,12 +11,14 @@ use Yii;
 use yii\console\Controller;
 use yii\console\ExitCode;
 use yii\db\Expression;
+use yii\helpers\ArrayHelper;
 use yii\helpers\BaseConsole;
 use yii\helpers\Json;
 use yii_app\helpers\ClientHelper;
 use yii_app\helpers\DataHelper;
 use yii_app\records\BonusLevels;
 use yii_app\records\EqualizationRemains;
+use yii_app\records\KogortStopList;
 use yii_app\records\LPTrackerApi;
 use yii_app\records\Product1cReplacement;
 use yii_app\records\Products1c;
@@ -413,7 +415,14 @@ class CronController extends Controller
                 ->asArray()
                 ->all();
 
-            $phonesArray = array_diff(array_column($telegramUsers, 'phone'), $sentStatusKogort);
+            $kogortStopListPhones = KogortStopList::find()->select('phone')->asArray()->all();
+            $phonesArray = array_diff(
+                array_diff(
+                    array_column($telegramUsers, 'phone'),
+                    $sentStatusKogort
+                ),
+                array_column($kogortStopListPhones, 'phone')
+            );
 
             $chatIdsArray = array_column($telegramUsers, 'chat_id');
             $countTelegramPhones = count($phonesArray);
@@ -694,7 +703,17 @@ class CronController extends Controller
                 ->select(['phone', 'chat_id'])
                 ->asArray()
                 ->all();
-            $phonesArray = array_diff(array_column($telegramUsers, 'phone'), $sentStatusKogort);
+
+            $kogortStopListPhones = KogortStopList::find()->select('phone')->asArray()->all();
+            $phonesArray = array_diff(
+                array_diff(
+                    array_column($telegramUsers, 'phone'),
+                    $sentStatusKogort
+                ),
+                array_column($kogortStopListPhones, 'phone')
+            );
+
+
             $chatIdsArray = array_column($telegramUsers, 'chat_id');
             $countTelegramPhones = count($phonesArray);
             $this->stdout(
@@ -820,7 +839,13 @@ class CronController extends Controller
                 ->replaceShortcodes($messagesSettings->offer_whatsapp, $targetDate);
             $phonesSentArray = [];
 
-            foreach ($kogortPhones as $index => $phone) {
+            $kogortStopListPhones = KogortStopList::find()->select('phone')->asArray()->all();
+            $phonesArray = array_diff(
+                $kogortPhones,
+                array_column($kogortStopListPhones, 'phone')
+            );
+
+            foreach ($phonesArray as $index => $phone) {
                 if ($index >= $limit) {
                     break;
                 }
@@ -1038,7 +1063,12 @@ class CronController extends Controller
         }
         try {
 
-            $numbers = SentKogort::findAll(['kogort_number' => SentKogort::CALL, 'kogort_date' => date('Y-m-d'), 'status' => SentKogort::READY_TO_UPLOAD_LPTRACKER_STATUS]);
+            $numbers = SentKogort::findAll(
+                ['kogort_number' => SentKogort::CALL,
+                    'kogort_date' => date('Y-m-d'),
+                    'status' => SentKogort::READY_TO_UPLOAD_LPTRACKER_STATUS]
+            );
+            $kogortPhones = ArrayHelper::getColumn(ArrayHelper::toArray($numbers), 'phone');
 
             if (!empty($messagesSettings->test_phones_list)) {
                 $testPhones = array_map('trim', explode(',', $messagesSettings->test_phones_list));
@@ -1047,23 +1077,29 @@ class CronController extends Controller
                     &&
                     $messagesSettings->test_phones_active == 1
                 ) {
-                    $numbers = $testPhones;
+                    $kogortPhones = $testPhones;
                 }
             }
             $this->stdout(
                 "Отправка лидов в LPTrackerApi.\n",
                 BaseConsole::FG_RED
             );
-            foreach ($numbers as $number) {
+
+            $kogortStopListPhones = KogortStopList::find()->select('phone')->asArray()->all();
+            $phonesArray = array_diff(
+                $kogortPhones,
+                array_column($kogortStopListPhones, 'phone')
+            );
+            foreach ($phonesArray as $phone) {
                 $api = new LPTrackerApi();
                 $response = $api->post('lead', [
                     'contact' => [
                         'project_id' => LPTrackerApi::SERVICE,
-                        'name' => $number->user?->name,
+                        'name' => Users::find()->where(['phone' => $phone])->name ?? 'Клиент',
                         'details' => [
                             [
                                 'type' => 'phone',
-                                'data' => $number->phone,
+                                'data' => $phone,
                             ]
                         ]
                     ],
@@ -1085,6 +1121,11 @@ class CronController extends Controller
                     ? SentKogort::SUCCESS_UPLOAD_TO_LPTRACKER_STATUS
                     : SentKogort::ERROR_UPLOAD_TO_LPTRACKER_STATUS;
 
+                $matches = array_filter($numbers, function($item) use ($phone) {
+                    return $item->phone === $phone;
+                });
+
+                $number = reset($matches);
                 SentKogort::updateAll(['status' => $status], ['id' => $number->id]);
 
                 if ($status === SentKogort::ERROR_UPLOAD_TO_LPTRACKER_STATUS) {