]> gitweb.erp-flowers.ru Git - yii-erp24/.git/commitdiff
add missing file amo_sync_crm.php
authorAlexander Smirnov <fredeom@mail.ru>
Fri, 29 Dec 2023 09:40:32 +0000 (12:40 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Fri, 29 Dec 2023 09:40:32 +0000 (12:40 +0300)
erp24/inc/amo/amo_sync_crm.php [new file with mode: 0644]

diff --git a/erp24/inc/amo/amo_sync_crm.php b/erp24/inc/amo/amo_sync_crm.php
new file mode 100644 (file)
index 0000000..eb906d6
--- /dev/null
@@ -0,0 +1,224 @@
+<?php
+
+include_once(dirname(__DIR__, 2) . "/inc/db.php");
+
+if($_REQUEST["p"]!="CJVKLT00eytudeDFBr6") exit();
+
+if(!empty($_REQUEST["amo_id"])) $amo_id=(int)$_REQUEST["amo_id"];
+// https://amo.bazacvetov24.biocrm.ru/amo_sync_crm.php?p=CJVKLT00eytudeDFBr6&amo_id=24645767
+
+
+if(!empty($amo_id)) {
+    if(empty($ACCESS_TOKEN)) include_once(dirname(__DIR__, 2) . "/inc/amo/amo_inc.php");
+//header('Content-Type: text/html; charset=utf-8');
+
+
+    $deal = amo_rest($ACCESS_TOKEN, '/api/v4/leads/'.$amo_id.'?with=contacts','GET', []);
+    $id_contact = $deal['_embedded']['contacts'][0]['id'];
+    $custom_fields = $deal['custom_fields_values'];
+
+    $i=1;
+    foreach($deal['_embedded']["tags"] as $ud => $tag) {
+        if($i<count($deal['_embedded']["tags"])) $z=","; else $z="";
+        $tags.="".$tag["name"]."$z";
+        $tags_arr .="".$tag["id"]."$z";
+        $i++;
+    }
+
+    $users[6247372]="Менеджер 1";
+    $users[6807019]="Менеджер 2";
+    $users[6807028]="РОП";
+    $users[5992531]="Логист";
+    $users[3450793]="Тех директор";
+    $users[5992531]="Опреатор";
+    $row=array();
+    $row["amo_id"]=$amo_id;
+    $row["price"]=$deal['price'];
+    $row["responsible_user_id"]=$deal['responsible_user_id'];
+
+
+    $row["created_at"]=date("Y-m-d H:i:s",$deal['created_at']);
+    $row["updated_at"]=date("Y-m-d H:i:s",$deal['updated_at']);
+    $row["closed_at"]=date("Y-m-d H:i:s",$deal['closed_at']);
+    $row["pipeline_id"]=$deal['pipeline_id'];
+    if(empty($status_id)) $row["status_id"]=$deal['status_id'];
+    else $row["status_id"]=$status_id;
+    $row["tags"]=$tags;
+    $row["tags_arr"]=$tags_arr;
+    $row["name"]=$deal['name'];;
+    $row["responsible_user"]=$users[$row["responsible_user_id"]];
+
+    $massiv=[
+        655357=>"id",
+        647945=>"pol_name",
+        657129=>"text_card",
+        647947=>"pol_phone",
+        774807=>"delivery_time",
+//647937=>"delivery_time_old",
+        647935 =>"delivery_date",
+        774805 =>"delivery_time_extend",
+        676231=>"delivery_oblast",
+        647949=>"delivery_adress",
+        676229=>"delivery_rayon",
+        647931=>"pay",
+        651887=>"payment_type_id",
+        647927=>"delivery",
+        647929=>"store_id",
+        647929=>"store",
+        647955=>"comment",
+        661535=>"order_text",
+        659607=>"toper",
+        658099 =>"shariki",
+        768387 =>"povod",
+        647953 =>"istochnik",
+        775235 =>"courier_id",
+        775237 =>"courier",
+        775239 =>"courier_zabor",
+        775241 =>"courier_dostavil",
+        775245 =>"florist_id",
+        775247 =>"florist",
+        775491 =>"florist_peredano",
+        775289 =>"florist_zabor",
+        775343 =>"florist_foto",
+        775539 =>"dostavka_comment",
+        775507 =>"price_dostavka",
+        775495 =>"price_dostavka_zatrat",
+        775513 =>"dostavka_povtor_tip",
+        775505 =>"price_dostavka_povtor_zatrat",
+        775509 =>"price_dostavka_povtor",
+        658125 =>"otkaz_id",
+        663239 =>"otkaz_text",
+        775001 =>"otkaz_manager",
+        775287 =>"otkaz_id_rop",
+        775355 =>"client_comment_foto",
+        775499 =>"client_adress_zamena",
+        775501 =>"client_adress_zamena_cnt",
+        775351 =>"client_ip",
+        775357 =>"client_pravki_cnt",
+        775349 =>"client_foro_prosmotr",
+        774987 =>"uber_time",
+        775545 =>"manager_time",
+        775287 =>"otkaz_id_rop",
+        776319 =>"manager_id",
+        776321 =>"manager",
+        776329 =>"phone_client",
+        775955 =>"courier_gps",
+        776259 =>"manager_courier_time",
+        775365 =>"florist_sborka_night",
+        795353 =>"products_json",
+//786559 =>"brend",
+        775083 =>"key_code",
+    ];
+
+
+    /*if($ma["id"]==786559) $brend=trim($valuecustom);
+    if($ma["id"]==655357) $lid_id=$valuecustom;
+    $json_new["custom_".$ma["id"].""]=array("name"=>$ma["name"], "value"=>$valuecustom);
+    }
+
+    $custom_brend='';
+    if($brend=='') $brend='БазаЦветов24.ру';
+    //$brend='Регион';$brend='ДомРоз24.ру';$brend='24-База-Цветов.ру';
+
+    $custom_brend = ', {"field_id": 786559, "values": [ {"value":"'.$brend.'"}]}';*/
+
+
+
+    $formapole=[
+        "delivery_date"=>"date",
+        "florist_peredano"=>"datetime",
+        "florist_zabor"=>"datetime",
+        "florist_foto"=>"datetime",
+        "courier_zabor"=>"datetime",
+        "courier_dostavil"=>"datetime",
+        "manager_courier_time"=>"datetime",
+
+
+    ];
+
+    if (!function_exists('func_format')) {
+        function func_format($tip="datetime",$pole) {
+
+            if($tip=="datetime") $pole=date("Y-m-d H:i:s",$pole);
+            if($tip=="date") $pole=date("Y-m-d",$pole);
+            return $pole;
+
+        }
+    }
+
+
+    foreach($custom_fields as $if => $ma) {
+        if(!empty($massiv[$ma["field_id"]])) {
+            if(!empty($formapole[$massiv[$ma["field_id"]]])) $ma["values"][0]["value"]=func_format($formapole[$massiv[$ma["field_id"]]],$ma["values"][0]["value"]);
+
+            $row[$massiv[$ma["field_id"]]]=$ma["values"][0]["value"];
+        }
+    }
+
+//if($row["delivery_time"]==$row["delivery_time_old"]) $row["delivery_time_old"]="";
+    if($row["delivery_time_extend"]==$row["delivery_time"]) $row["delivery_time_extend"]="";
+    if($row["delivery_time_extend"]==$row["delivery_time_old"]) $row["delivery_time_extend"]="";
+
+//foreach($row as $k => $v) echo"<br>$k => $v";
+//print'<pre>';print_r($deal);echo'</pre>';
+
+    $contact = amo_rest($ACCESS_TOKEN, '/api/v4/contacts/' . $id_contact, $method = 'GET', []);
+//$arr = ['lead' => $deal,     'contact' => $contact];
+    $phone=$contact["custom_fields_values"]["0"]["values"]["0"]["value"];
+    $email=$contact["custom_fields_values"]["1"]["values"]["0"]["value"];
+    $name=$contact["name"];
+    $row["client"]=$name;
+    $row["phone"]=$phone;
+    $row["email"]=$email;
+    $row["amo_sync_time"]=date("Y-m-d H:i:s",time());
+
+    if(empty($row["phone"])) $row["phone"]=$row["phone_client"];
+
+
+//echo "id= ".$contact["id"]." $name phone =$phone $email";
+
+//print_r($contact);
+
+
+
+    $i=0;
+    $sql1 =""; $sql2 ="";
+    foreach($row as $n => $value) {
+
+        $value=str_replace("'","",$value);
+        if($i>0) { $sql1 .=","; $sql2 .=","; }
+        $sql1 .=" $n ";
+        $sql2 .="'$value'";
+
+//echo" <br> $n => $value ";
+        $i++;
+    }
+
+
+    if(!empty($row["id"])) {
+
+
+        $data_cnt=$db::getRows("SELECT id FROM orders_amo  WHERE id=?",[$row["id"]]);
+
+        if($update_amo==1 and count($data_cnt)>0) {
+
+            echo"НЕ удаляем cтарую информацию";
+        }  else $db::sql("DELETE FROM orders_amo WHERE id=?",[$row["id"]]);
+
+
+
+        if($row["status_id"]==142 or $row["status_id"]==143) $db::sql("DELETE FROM orders_amo WHERE id=?",[$row["id"]]);
+
+        $s="INSERT INTO  orders_amo ($sql1) VALUES ($sql2);"; echo $s;
+        $db::sql($s);
+
+        $data=$db::getRows("SELECT id, adress_amo FROM city_store  WHERE 1");
+        foreach ($data as $row2) {
+            if(!empty($row2["id"])) $db::sql("UPDATE  orders_amo SET store_id='".$row2["id"]."' WHERE store='".$row2["adress_amo"]."' AND store_id=''");
+        }
+
+//file_put_contents('sunc_142.txt', ''.$s.'   ', FILE_APPEND);
+    }
+
+
+}