Advertisement
JulianoLoren

Untitled

Mar 23rd, 2023
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.05 KB | Fixit | 0 0
  1. <?php
  2. //IP-6523 Cập nhật thông tin Phân loại KH từ data BI cung cấp
  3. set_time_limit(0);
  4. ini_set('display_errors', 1);
  5. error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_WARNING);
  6. chdir(dirname(__FILE__));
  7. chdir('../../');
  8. //
  9. require_once 'modules/Users/Users.php';
  10. require_once 'includes/Loader.php';
  11. require_once 'includes/runtime/EntryPoint.php';
  12. require_once('include/utils/utils.php');
  13.  
  14. //
  15. ini_set('max_execution_time', 0);
  16. ini_set('memory_limit', '1024M');
  17. //@date_default_timezone_set('Asia/Ho_Chi_Minh');
  18.  
  19. global $adb;
  20. $current_user = new Users();
  21. $current_user->retrieveCurrentUserInfoFromFile(1);
  22. $adb = PearDatabase::getInstance();
  23. $adb->setDieOnError(1);
  24. ini_set('display_errors', 'on');
  25. error_reporting(E_ERROR);
  26.  
  27. //user
  28. $_REQUEST['yen'] = 6; //yen.hh (miền bắc)
  29. $_REQUEST['huong'] = 5; //huong.nt (Miền nam)
  30. $_REQUEST['minh'] = 142; //minh.bn
  31. $_REQUEST['lan'] = 256; //lan.nth
  32. $_REQUEST['new_acc'] = 283; // acc DVKH
  33.  
  34. //$json = '{"segmentation": "New", "kvc_industry_modified": "Mẹ & bé"}';
  35.  
  36. $limit = $_REQUEST['limit'] ?? 100;
  37. $isFirstTime = isset($_REQUEST['first_time']) && $_REQUEST['first_time'] == 1;
  38. $condition = "";
  39. if (isset($_REQUEST['id'])) {
  40.     $condition = " AND dataid = ".$_REQUEST['id'];
  41. }
  42. $isStop = false;
  43. $sql = "SELECT dataid, datas, retailer_id
  44.    FROM receive_data_bi
  45.    WHERE `status` = 0
  46.    AND `retailer_id` != 0
  47.    AND `action` = 'customer-segmentation'
  48.     $condition
  49.     ORDER BY dataid ASC
  50.    LIMIT $limit";
  51.  
  52. $count = 0;
  53. while (!$isStop) {
  54.     // Lấy toàn bộ dữ liệu từ bảng receive_data_bi
  55.     $result = $adb->pquery($sql);
  56.     $total = $adb->num_rows($result);
  57.  
  58.  
  59.     // Nếu không có dữ liệu thì die :()
  60.     if (!$total) die("Done!!!");
  61.  
  62.     // Nếu dữ liệu nhỏ hơn  limit thì dừng
  63.     if ($total < $limit) $isStop = true;
  64.  
  65.     // Đưa dữ liệu vừa fetch vào 1 associative array
  66.     $rawData = [];
  67.     while ($row = $adb->fetchByAssoc($result)) {
  68.         $dataStatus = json_decode(html_entity_decode($row['datas']), true);
  69.         $rawData[$row['retailer_id']] = [
  70.             'retailer_id' => $row['retailer_id'],
  71.             'data' => $dataStatus,
  72.             'data_id' => $row['dataid'],
  73.         ];
  74.     }
  75.  
  76.     // Lấy các retailer_id sẽ chạy lần này
  77.     // NOTE: sao không dùng array_keys(rawData) ????????
  78.     $retailerIds = array_unique(array_filter(array_column($rawData, 'retailer_id')));
  79.  
  80.     // Kiểm tra các retailer_id này có nằm ở cả 2 bảng vtiger_potentialscf và vtiger_crmentity không
  81.     $optRes = $adb->pquery("SELECT
  82.                potentialid,
  83.                cf_potential_retailerid,
  84.                cf_potential_currentguest_file,
  85.                smownerid,
  86.                cf_industry_kvc_opt,
  87.                cf_potential_city
  88.            FROM vtiger_potentialscf pot
  89.            INNER JOIN vtiger_crmentity ent
  90.            ON pot.potentialid = ent.crmid
  91.            WHERE pot.cf_potential_retailerid IN (".implode(',', $retailerIds).")");
  92.  
  93.     // Nếu không có retailer_id nào như thế thì báo lỗi và loop tiếp
  94.     if (!$adb->num_rows($optRes)) {
  95.         setError(array_column($rawData, 'data_id'));
  96.         continue;
  97.     }
  98.  
  99.     while ($row = $adb->fetchByAssoc($optRes)) {
  100.         // Lấy dữ liệu trong rawData của từng retailerid
  101.         $optRawData = $rawData[$row['cf_potential_retailerid']];
  102.  
  103.         // ???
  104.         if (!$isFirstTime &&
  105.             html_entity_decode($optRawData['data']['segmentation']) == html_entity_decode($row['cf_potential_currentguest_file'])
  106.         ) {
  107.             setError([$optRawData['data_id']]);
  108.             continue;
  109.         }
  110.         updateData($optRawData, $row);
  111.         $count++;
  112.         $adb->pquery("UPDATE receive_data_bi SET status = 1 WHERE dataid = ?", [$optRawData['data_id']]);
  113.         echo "Updated Opt id {$row['potentialid']} retailer id {$row['cf_potential_retailerid']} !!!\n";
  114.     }
  115.     echo "Updated $count data !!! \n";
  116. }
  117.  
  118. function setError(array $dataIds)
  119. {
  120.     global $adb;
  121.     $adb->pquery("UPDATE receive_data_bi SET status = 2 WHERE dataid IN (".implode(',',$dataIds).")", []);
  122. }
  123.  
  124. function updateData(array $rawData, array $currentData)
  125. {
  126.     global $adb;
  127.     $rawData = $rawData['data'];
  128.     $rawData['kvc_industry_modified'] = industries()[strtolower(html_entity_decode($rawData['kvc_industry_modified']))] ?? html_entity_decode($rawData['kvc_industry_modified']);
  129.     // Nếu data nhận được và DB khác nhau mới update. Đoạn này chắc crm db ghi nhiều quá nên sợ toang
  130.     $needUpdateSegment = html_entity_decode($rawData['segmentation']) !=
  131.         html_entity_decode($currentData['cf_potential_currentguest_file']);
  132.     $needUpdateKVCIndustry = $rawData['kvc_industry_modified'] !=
  133.         html_entity_decode($currentData['cf_industry_kvc_opt']);
  134.     $newAssignUser = getAssignUser($rawData, $currentData);
  135.     $needUpdateAssignUser = $newAssignUser != $currentData['smownerid'];
  136.  
  137.     // cf_1497: tệp KH sale nhận
  138.     $adb->pquery("UPDATE vtiger_potentialscf
  139.                    SET cf_1497 = ?,
  140.                        cf_potential_update_time = ?
  141.                    WHERE potentialid = ?", [
  142.                                                 html_entity_decode($rawData['start_segmentation']),
  143.                                                 date('Y-m-d H:i:s'),
  144.                                                 $currentData['potentialid']
  145.                                             ]);
  146.  
  147.     if (!$needUpdateSegment && !$needUpdateKVCIndustry && !$needUpdateAssignUser) {
  148.         return;
  149.     }
  150.  
  151.     if ($needUpdateAssignUser) {
  152.         $adb->pquery("UPDATE vtiger_crmentity SET smownerid = ? WHERE crmid = ?",
  153.             [$newAssignUser, $currentData['potentialid']]);
  154.         logModTrackerDetail(
  155.             null,
  156.             'assigned_user_id',
  157.             $currentData['smownerid'],
  158.             $newAssignUser,
  159.             $currentData['potentialid'],
  160.             'Potentials',
  161.             1
  162.         );
  163.     }
  164.  
  165.     if ($needUpdateSegment) {
  166.         $adb->pquery("UPDATE vtiger_potentialscf SET
  167.            cf_potential_currentguest_file = ?,
  168.            cf_potential_oldclient_file = ?,
  169.            cf_potential_update_time = ? WHERE potentialid = ?", [
  170.             html_entity_decode($rawData['segmentation']),
  171.             html_entity_decode($currentData['cf_potential_currentguest_file']),
  172.             date('Y-m-d H:i:s'),
  173.             $currentData['potentialid']
  174.         ]);
  175.         logModTrackerDetail(
  176.             null,
  177.             'cf_potential_currentguest_file',
  178.             html_entity_decode($currentData['cf_potential_currentguest_file']),
  179.             html_entity_decode($rawData['segmentation']),
  180.             $currentData['potentialid'],
  181.             'Potentials',
  182.             1
  183.         );
  184.     }
  185.  
  186.     if ($needUpdateKVCIndustry) {
  187.         $adb->pquery("UPDATE vtiger_potentialscf SET cf_industry_kvc_opt = ? WHERE potentialid = ?", [
  188.             html_entity_decode($rawData['kvc_industry_modified']), $currentData['potentialid']
  189.         ]);
  190.         logModTrackerDetail(
  191.             null,
  192.             'cf_industry_kvc_opt',
  193.             html_entity_decode($currentData['cf_industry_kvc_opt']),
  194.             html_entity_decode($rawData['kvc_industry_modified']),
  195.             $currentData['potentialid'],
  196.             'Potentials',
  197.             1
  198.         );
  199.     }
  200.  
  201.     $adb->pquery("UPDATE vtiger_crmentity SET modifiedtime = ? WHERE crmid = ?", [
  202.         date('Y-m-d H:i:s'), $currentData['potentialid']
  203.     ]);
  204. }
  205.  
  206. function getAssignUser(array $rawData, array $currentData)
  207. {
  208.     $currentAssignUser = $currentData['smownerid'];
  209.     $city = html_entity_decode($currentData['cf_potential_city']);
  210.     $segmentation = trim(html_entity_decode($rawData['segmentation']));
  211.     $industry = trim(html_entity_decode($rawData['kvc_industry_modified']));
  212.     if (in_array($segmentation, ['New Merchant', 'Re-active Merchant', 'VIP Merchant'])) {
  213.         if (in_array($industry, ['Mỹ phẩm', 'Mẹ & bé'])) {
  214.             if (in_array($city, mienBac())) {
  215.                 return $_REQUEST['yen'];
  216.             }
  217.             return $_REQUEST['huong'];
  218.         } elseif (in_array($industry, ['Điện tử viễn thông'])) {
  219.             return $_REQUEST['minh'];
  220.         } elseif ($industry == 'FMCG - Tiêu dùng nhanh') {
  221.             return $_REQUEST['lan'];
  222.         } else {
  223.             return $currentAssignUser;
  224.         }
  225.     } elseif ($segmentation == 'DIY Merchant') {
  226.         if (in_array($industry, ['Mỹ phẩm', 'Mẹ & bé'])) {
  227.             return $_REQUEST['new_acc'];
  228.         } elseif (in_array($industry, ['Điện tử viễn thông'])) {
  229.             return $_REQUEST['minh'];
  230.         } elseif ($industry == 'FMCG - Tiêu dùng nhanh') {
  231.             return $_REQUEST['lan'];
  232.         } else {
  233.             return $currentAssignUser;
  234.         }
  235.     }
  236.  
  237.     return $currentAssignUser;
  238. }
  239.  
  240. function industries(): array
  241. {
  242.     return [
  243.         'mỹ phẩm' => 'Mỹ phẩm',
  244.         'điện tử viễn thông' => 'Điện tử viễn thông',
  245.         'thời trang' => 'Thời trang',
  246.         'fmcg - tiêu dùng nhanh' => 'FMCG - Tiêu dùng nhanh',
  247.         'mẹ & bé' => 'Mẹ & bé',
  248.         'ngành khác' => 'Ngành khác',
  249.     ];
  250. }
  251.  
  252. function mienBac(): array
  253. {
  254.     return array(
  255.         'Hà Nội',
  256.         'Hưng Yên',
  257.         'Hải Dương',
  258.         'Hải Phòng',
  259.         'Tp.Hải Phòng',
  260.         'Quảng Ninh',
  261.         'Bắc Ninh',
  262.         'Bắc Giang',
  263.         'Lạng Sơn',
  264.         'Thái Nguyên',
  265.         'Bắc Kạn',
  266.         'Cao Bằng',
  267.         'Vĩnh Phúc',
  268.         'Phú Thọ',
  269.         'Tuyên Quang',
  270.         'Hà Giang',
  271.         'Yên Bái',
  272.         'Lào Cai',
  273.         'Hòa Bình',
  274.         'Sơn La',
  275.         'Điện Biên',
  276.         'Lai Châu',
  277.         'Hà Nam',
  278.         'Thái Bình',
  279.         'Nam Định',
  280.         'Ninh Bình',
  281.         'Thanh Hóa',
  282.         'Nghệ An',
  283.         'Hà Tĩnh',
  284.         'Quảng Bình',
  285.         'Quảng Trị',
  286.         'Thừa Thiên Huế'
  287.     );
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement