]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-355 Автоматизация звонков роботом из когорт
authormarina <m.zozirova@gmail.com>
Fri, 7 Mar 2025 06:55:05 +0000 (09:55 +0300)
committermarina <m.zozirova@gmail.com>
Fri, 7 Mar 2025 06:55:05 +0000 (09:55 +0300)
erp24/api2/config/api2.config.php
erp24/api2/controllers/LPTrackerController.php [new file with mode: 0644]
erp24/api2/records/LPTrackerApi.php [new file with mode: 0644]
erp24/records/SentKogort.php
erp24/scripts/tasks/task_35_sent_file_to_lptracker.php [new file with mode: 0644]

index 8cba7a248e2640b216b38afbd61657368ac3afdc..439db79ed1acbbaab3161099dd4c1ee9cfc5ebb4 100644 (file)
@@ -39,7 +39,8 @@ return [
                 'auth' => 'auth/login',
                 'delivery/admin-auth' => 'delivery/admin-auth',
                 ['class' => 'yii\rest\UrlRule', 'controller' => ['task']],
-                'POST data-buh/request/<inn:\d+>' => 'data-buh/request'
+                'POST data-buh/request/<inn:\d+>' => 'data-buh/request',
+                'lptracker/<action>' => 'l-p-tracker/<action>',
             ],
         ],
         'request' => [
diff --git a/erp24/api2/controllers/LPTrackerController.php b/erp24/api2/controllers/LPTrackerController.php
new file mode 100644 (file)
index 0000000..b458cab
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+namespace app\controllers;
+
+use app\records\LPTrackerApi;
+
+class LPTrackerController extends BaseController
+{
+    public function actionGetProjects()
+    {
+        $api = new LPTrackerApi();
+
+        $response = $api->get('/projects');
+        var_dump($response);
+    }
+
+    public function actionGetFunnels()
+    {
+//        $api = new LPTrackerApi();
+//        //GET contact/search?project_id=[project_id]&phone=[phone]&email=[email]
+//        $response = $api->get("/contact/search?project_id=117605&phone=79036571587&email=");
+//
+//        echo '<pre>';
+//        var_dump($response);
+//        echo '</pre>';
+    }
+
+    public function actionTest()
+    {
+//        $api = new LPTrackerApi();
+
+//        var_dump($response);
+//        die();
+
+    }
+
+}
\ No newline at end of file
diff --git a/erp24/api2/records/LPTrackerApi.php b/erp24/api2/records/LPTrackerApi.php
new file mode 100644 (file)
index 0000000..1dc04bd
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+
+namespace app\records;
+
+use Exception;
+use GuzzleHttp\Client;
+
+class LPTrackerApi
+{
+
+    private const LOGIN = 'Zakaz-bazacvetov24@yandex.ru1';
+    private const PASSWORD = 'B8-YY7d3K2ekNdK';
+    public const SERVICE = 117605;
+    private const TIMEOUT = 10;
+
+    public const SUCCESS_STATUS = 'success';
+    public const ERROR_STATUS = 'error';
+
+    private $token;
+    public $client;
+
+    public const BASE_URI = 'https://direct.lptracker.ru';
+
+    public function __construct()
+    {
+        try {
+            $this->client = new Client([
+                'base_uri' => self::BASE_URI,
+                'timeout' => self::TIMEOUT,
+            ]);
+
+            if (empty($this->token)) {
+                $this->auth();
+            }
+        } catch (Exception $exception) {
+            throw  new Exception($exception);
+        }
+    }
+
+
+    private function auth()
+    {
+        $response = $this->post('/login', [
+            'login' => self::LOGIN,
+            'password' => self::PASSWORD,
+            'service' => self::SERVICE,
+            'version' => '1.0'
+        ]);
+
+        if (!empty($response) && $response['status'] == self::SUCCESS_STATUS && $response['result']['token']) {
+            $this->token = $response['result']['token'];
+        } else {
+            throw new Exception('Не удалось получить токен!');
+        }
+    }
+
+    public function get($endpoint)
+    {
+        $response = $this->client->get($endpoint, [
+            'headers' => [
+                'token' => $this->token,
+                'Content-type' => 'application/json',
+            ]
+        ]);
+
+        return json_decode($response->getBody(), true);
+    }
+
+    public function post($endpoint, array $data = [])
+    {
+        $response = $this->client->post($endpoint, [
+            'json' => $data,
+            'headers' => [
+                'token' => $this->token,
+                'Content-Type' => 'application/json',
+            ]
+        ]);
+
+        return json_decode($response->getBody(), true);
+    }
+
+}
\ No newline at end of file
index 2fa033dc7f778ff270b0da6a831df6ae76a1b522..c0de3a42f8e8e3314b9048d593524cf5c3b238d2 100644 (file)
@@ -20,10 +20,14 @@ use Yii;
  */
 class SentKogort extends \yii\db\ActiveRecord
 {
+    public const CALL = 3;
+    public const SUCCESS_UPLOAD_TO_LPTRACKER_STATUS = 1;
+    public const ERROR_UPLOAD_TO_LPTRACKER_STATUS = 2;
+
     public const KOGORT_NUMBERS = [
         'target' => 1,
         'whatsapp' => 2,
-        'call' => 3,
+        'call' => self::CALL,
     ];
 
     public const STATUSES = [
@@ -87,4 +91,10 @@ class SentKogort extends \yii\db\ActiveRecord
             $this->updateAttributes(['updated_at' => date('Y-m-d H:i:s')]);
         }
     }
+
+    public function getUser()
+    {
+        return $this->hasOne(Users::class, ['phone' => 'phone']);
+
+    }
 }
diff --git a/erp24/scripts/tasks/task_35_sent_file_to_lptracker.php b/erp24/scripts/tasks/task_35_sent_file_to_lptracker.php
new file mode 100644 (file)
index 0000000..79243b0
--- /dev/null
@@ -0,0 +1,156 @@
+<?php
+
+/**
+ * @var $time integer|null
+ */
+
+use app\records\LPTrackerApi;
+use yii\helpers\Json;
+use yii_app\records\Admin;
+use yii_app\records\AdminGroup;
+use yii_app\records\ApiCronBuh;
+use yii_app\records\Firms;
+use yii_app\records\Motivation;
+use yii_app\records\SchedulerTaskLog;
+use yii_app\records\SentKogort;
+
+ini_set('max_execution_time', (string)(60 * 60 * 1)); // 1 час
+ini_set('display_errors', 'on');
+ini_set('display_errors', 1);
+ini_set('display_startup_errors', 1);
+error_reporting(E_ALL);
+
+$time = $time ?? time(); // Использовать текущее время, если параметр не передан
+
+echo "time2_" . $time . "_time2 ";
+$taskNum = 35;
+$dateTask = date('Y-m-d H:i:s', $time);
+$dateTaskStart = null;
+$dateTaskStop = null;
+$log = '';
+$error = '';
+$infoError = '';
+$infoText = '';
+$description = '';
+$result = 0;
+
+$enable = true;
+$start = false;
+$force = true;
+
+$minuetTimeInTask = date('i', $time);
+$fullTimeInTask = date('Y-m-d H:i:s', $time);
+
+try {
+    if (
+        (
+            (
+                date('H:i', $time) == "10:00"
+            )
+            || $force
+        )
+        && $enable
+    ) {
+
+        $dateTaskStart = date('Y-m-d H:i:s', $time);
+        $info = ' ================ test Task ' . $taskNum . ' start ================';
+        echo $info;
+        $log .= $info;
+        $log .= $time;
+
+        $schedulerTaskLog = new SchedulerTaskLog();
+        $schedulerTaskLog->setTaskNum($taskNum)
+            ->setName('Task ' . $taskNum)
+            ->setDate($dateTask)
+            ->setDateStart($dateTaskStart);
+        $validate = $schedulerTaskLog->validate();
+        if ($validate) {
+            $schedulerTaskLog->save();
+        }
+
+        $numbers = SentKogort::findOne(['number' => '79036571587']);
+
+        foreach ($numbers as $number) {
+            $api = new LPTrackerApi();
+            $response = $api->post('lead', [
+                'contact' => [
+                    'project_id' => LPTrackerApi::SERVICE,
+                    'name' => "",
+//                    'name' => $number->users->name,
+                    'profession' => '',
+                    'site' => '',
+                    'details' => [
+                        [
+                            'type' => 'phone',
+//                            'data' => $number->phone,
+                            'data' => "+71234567890",
+                        ]
+                    ]
+                ],
+                'callback' => false,
+                'funnel' => LPTrackerApi::SERVICE,
+                'lead_date' => date('d.m.Y H:i'),
+                'deal_date' => date('d.m.Y H:i'),
+                'owner' => 0,
+                'payments' => [
+                ],
+            ]);
+        }
+
+        $info = ' ================ test Task ' . $taskNum . ' stop ================';
+        echo $info;
+        $log .= $info;
+        $log .= ' date >= ' . strtotime("-1 week", $time);
+        $dateTaskStop = date('Y-m-d H:i:s', $time);
+    } else {
+        $info = '   Task ' . $taskNum . ' skip   ';
+        echo $info;
+        $log .= $info;
+    }
+} catch (Exception $e) {
+    $error = 'Exception: ' . $e->getMessage() . ' ' . $e->getFile() . ' >>> ' . $e->getLine();
+}
+
+if (empty($schedulerTaskLog)) {
+    $schedulerTaskLog = new SchedulerTaskLog();
+    $schedulerTaskLog->setTaskNum($taskNum)
+        ->setName('Task ' . $taskNum)
+        ->setDate($dateTask)
+        ->setDateStart($dateTaskStart)
+        ->setDateStop($dateTaskStop)
+        ->setDescription($description)
+        ->setError($error)
+        ->setInfo($infoText)
+        ->setLog($log);
+} else {
+    $schedulerTaskLog->setDateStop($dateTaskStop)
+        ->setDescription($description)
+        ->setError($error)
+        ->setInfo($infoText)
+        ->setLog($log);
+}
+$validate = $schedulerTaskLog->validate();
+if ($validate) {
+    $schedulerTaskLog->save();
+}
+
+function createApiCron($key, $weekRange)
+{
+    $model = new ApiCronBuh();
+    $model->date = date('Y-m-d H:i:s');
+    $model->request_id = strval(strtotime($model->date) . '_' . $key);
+    $model->json_post = Json::encode([
+        'request_id' => $model->request_id,
+        'cost_items' => [
+            'start_time' => date('Y-m-d 00:00:00', strtotime($weekRange['start_time'])),
+            'end_time' => date('Y-m-d 23:59:59', strtotime($weekRange['end_time'])),
+        ]
+    ]);
+    $model->inn = $key;
+    try {
+        $model->save();
+    } catch (Exception $e) {
+        throw new Exception($e);
+    }
+}
+