Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //IP-6523 Cập nhật thông tin Phân loại KH từ data BI cung cấp
- set_time_limit(0);
- ini_set('display_errors', 1);
- error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_WARNING);
- chdir(dirname(__FILE__));
- chdir('../../');
- //
- require_once 'modules/Users/Users.php';
- require_once 'includes/Loader.php';
- require_once 'includes/runtime/EntryPoint.php';
- require_once('include/utils/utils.php');
- //
- ini_set('max_execution_time', 0);
- ini_set('memory_limit', '1024M');
- //@date_default_timezone_set('Asia/Ho_Chi_Minh');
- global $adb;
- $current_user = new Users();
- $current_user->retrieveCurrentUserInfoFromFile(1);
- $adb = PearDatabase::getInstance();
- $adb->setDieOnError(1);
- ini_set('display_errors', 'on');
- error_reporting(E_ERROR);
- //user
- $_REQUEST['yen'] = 6; //yen.hh (miền bắc)
- $_REQUEST['huong'] = 5; //huong.nt (Miền nam)
- $_REQUEST['minh'] = 142; //minh.bn
- $_REQUEST['lan'] = 256; //lan.nth
- $_REQUEST['new_acc'] = 283; // acc DVKH
- //$json = '{"segmentation": "New", "kvc_industry_modified": "Mẹ & bé"}';
- $limit = $_REQUEST['limit'] ?? 100;
- $isFirstTime = isset($_REQUEST['first_time']) && $_REQUEST['first_time'] == 1;
- $condition = "";
- if (isset($_REQUEST['id'])) {
- $condition = " AND dataid = ".$_REQUEST['id'];
- }
- $isStop = false;
- $sql = "SELECT dataid, datas, retailer_id
- FROM receive_data_bi
- WHERE `status` = 0
- AND `retailer_id` != 0
- AND `action` = 'customer-segmentation'
- $condition
- ORDER BY dataid ASC
- LIMIT $limit";
- $count = 0;
- while (!$isStop) {
- // Lấy toàn bộ dữ liệu từ bảng receive_data_bi
- $result = $adb->pquery($sql);
- $total = $adb->num_rows($result);
- // Nếu không có dữ liệu thì die :()
- if (!$total) die("Done!!!");
- // Nếu dữ liệu nhỏ hơn limit thì dừng
- if ($total < $limit) $isStop = true;
- // Đưa dữ liệu vừa fetch vào 1 associative array
- $rawData = [];
- while ($row = $adb->fetchByAssoc($result)) {
- $dataStatus = json_decode(html_entity_decode($row['datas']), true);
- $rawData[$row['retailer_id']] = [
- 'retailer_id' => $row['retailer_id'],
- 'data' => $dataStatus,
- 'data_id' => $row['dataid'],
- ];
- }
- // Lấy các retailer_id sẽ chạy lần này
- // NOTE: sao không dùng array_keys(rawData) ????????
- $retailerIds = array_unique(array_filter(array_column($rawData, 'retailer_id')));
- // Kiểm tra các retailer_id này có nằm ở cả 2 bảng vtiger_potentialscf và vtiger_crmentity không
- $optRes = $adb->pquery("SELECT
- potentialid,
- cf_potential_retailerid,
- cf_potential_currentguest_file,
- smownerid,
- cf_industry_kvc_opt,
- cf_potential_city
- FROM vtiger_potentialscf pot
- INNER JOIN vtiger_crmentity ent
- ON pot.potentialid = ent.crmid
- WHERE pot.cf_potential_retailerid IN (".implode(',', $retailerIds).")");
- // Nếu không có retailer_id nào như thế thì báo lỗi và loop tiếp
- if (!$adb->num_rows($optRes)) {
- setError(array_column($rawData, 'data_id'));
- continue;
- }
- while ($row = $adb->fetchByAssoc($optRes)) {
- // Lấy dữ liệu trong rawData của từng retailerid
- $optRawData = $rawData[$row['cf_potential_retailerid']];
- // ???
- if (!$isFirstTime &&
- html_entity_decode($optRawData['data']['segmentation']) == html_entity_decode($row['cf_potential_currentguest_file'])
- ) {
- setError([$optRawData['data_id']]);
- continue;
- }
- updateData($optRawData, $row);
- $count++;
- $adb->pquery("UPDATE receive_data_bi SET status = 1 WHERE dataid = ?", [$optRawData['data_id']]);
- echo "Updated Opt id {$row['potentialid']} retailer id {$row['cf_potential_retailerid']} !!!\n";
- }
- echo "Updated $count data !!! \n";
- }
- function setError(array $dataIds)
- {
- global $adb;
- $adb->pquery("UPDATE receive_data_bi SET status = 2 WHERE dataid IN (".implode(',',$dataIds).")", []);
- }
- function updateData(array $rawData, array $currentData)
- {
- global $adb;
- $rawData = $rawData['data'];
- $rawData['kvc_industry_modified'] = industries()[strtolower(html_entity_decode($rawData['kvc_industry_modified']))] ?? html_entity_decode($rawData['kvc_industry_modified']);
- // 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
- $needUpdateSegment = html_entity_decode($rawData['segmentation']) !=
- html_entity_decode($currentData['cf_potential_currentguest_file']);
- $needUpdateKVCIndustry = $rawData['kvc_industry_modified'] !=
- html_entity_decode($currentData['cf_industry_kvc_opt']);
- $newAssignUser = getAssignUser($rawData, $currentData);
- $needUpdateAssignUser = $newAssignUser != $currentData['smownerid'];
- // cf_1497: tệp KH sale nhận
- $adb->pquery("UPDATE vtiger_potentialscf
- SET cf_1497 = ?,
- cf_potential_update_time = ?
- WHERE potentialid = ?", [
- html_entity_decode($rawData['start_segmentation']),
- date('Y-m-d H:i:s'),
- $currentData['potentialid']
- ]);
- if (!$needUpdateSegment && !$needUpdateKVCIndustry && !$needUpdateAssignUser) {
- return;
- }
- if ($needUpdateAssignUser) {
- $adb->pquery("UPDATE vtiger_crmentity SET smownerid = ? WHERE crmid = ?",
- [$newAssignUser, $currentData['potentialid']]);
- logModTrackerDetail(
- null,
- 'assigned_user_id',
- $currentData['smownerid'],
- $newAssignUser,
- $currentData['potentialid'],
- 'Potentials',
- 1
- );
- }
- if ($needUpdateSegment) {
- $adb->pquery("UPDATE vtiger_potentialscf SET
- cf_potential_currentguest_file = ?,
- cf_potential_oldclient_file = ?,
- cf_potential_update_time = ? WHERE potentialid = ?", [
- html_entity_decode($rawData['segmentation']),
- html_entity_decode($currentData['cf_potential_currentguest_file']),
- date('Y-m-d H:i:s'),
- $currentData['potentialid']
- ]);
- logModTrackerDetail(
- null,
- 'cf_potential_currentguest_file',
- html_entity_decode($currentData['cf_potential_currentguest_file']),
- html_entity_decode($rawData['segmentation']),
- $currentData['potentialid'],
- 'Potentials',
- 1
- );
- }
- if ($needUpdateKVCIndustry) {
- $adb->pquery("UPDATE vtiger_potentialscf SET cf_industry_kvc_opt = ? WHERE potentialid = ?", [
- html_entity_decode($rawData['kvc_industry_modified']), $currentData['potentialid']
- ]);
- logModTrackerDetail(
- null,
- 'cf_industry_kvc_opt',
- html_entity_decode($currentData['cf_industry_kvc_opt']),
- html_entity_decode($rawData['kvc_industry_modified']),
- $currentData['potentialid'],
- 'Potentials',
- 1
- );
- }
- $adb->pquery("UPDATE vtiger_crmentity SET modifiedtime = ? WHERE crmid = ?", [
- date('Y-m-d H:i:s'), $currentData['potentialid']
- ]);
- }
- function getAssignUser(array $rawData, array $currentData)
- {
- $currentAssignUser = $currentData['smownerid'];
- $city = html_entity_decode($currentData['cf_potential_city']);
- $segmentation = trim(html_entity_decode($rawData['segmentation']));
- $industry = trim(html_entity_decode($rawData['kvc_industry_modified']));
- if (in_array($segmentation, ['New Merchant', 'Re-active Merchant', 'VIP Merchant'])) {
- if (in_array($industry, ['Mỹ phẩm', 'Mẹ & bé'])) {
- if (in_array($city, mienBac())) {
- return $_REQUEST['yen'];
- }
- return $_REQUEST['huong'];
- } elseif (in_array($industry, ['Điện tử viễn thông'])) {
- return $_REQUEST['minh'];
- } elseif ($industry == 'FMCG - Tiêu dùng nhanh') {
- return $_REQUEST['lan'];
- } else {
- return $currentAssignUser;
- }
- } elseif ($segmentation == 'DIY Merchant') {
- if (in_array($industry, ['Mỹ phẩm', 'Mẹ & bé'])) {
- return $_REQUEST['new_acc'];
- } elseif (in_array($industry, ['Điện tử viễn thông'])) {
- return $_REQUEST['minh'];
- } elseif ($industry == 'FMCG - Tiêu dùng nhanh') {
- return $_REQUEST['lan'];
- } else {
- return $currentAssignUser;
- }
- }
- return $currentAssignUser;
- }
- function industries(): array
- {
- return [
- 'mỹ phẩm' => 'Mỹ phẩm',
- 'điện tử viễn thông' => 'Điện tử viễn thông',
- 'thời trang' => 'Thời trang',
- 'fmcg - tiêu dùng nhanh' => 'FMCG - Tiêu dùng nhanh',
- 'mẹ & bé' => 'Mẹ & bé',
- 'ngành khác' => 'Ngành khác',
- ];
- }
- function mienBac(): array
- {
- return array(
- 'Hà Nội',
- 'Hưng Yên',
- 'Hải Dương',
- 'Hải Phòng',
- 'Tp.Hải Phòng',
- 'Quảng Ninh',
- 'Bắc Ninh',
- 'Bắc Giang',
- 'Lạng Sơn',
- 'Thái Nguyên',
- 'Bắc Kạn',
- 'Cao Bằng',
- 'Vĩnh Phúc',
- 'Phú Thọ',
- 'Tuyên Quang',
- 'Hà Giang',
- 'Yên Bái',
- 'Lào Cai',
- 'Hòa Bình',
- 'Sơn La',
- 'Điện Biên',
- 'Lai Châu',
- 'Hà Nam',
- 'Thái Bình',
- 'Nam Định',
- 'Ninh Bình',
- 'Thanh Hóa',
- 'Nghệ An',
- 'Hà Tĩnh',
- 'Quảng Bình',
- 'Quảng Trị',
- 'Thừa Thiên Huế'
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement