| Server IP : 138.197.176.125 / Your IP : 216.73.217.122 Web Server : Apache/2.4.41 (Ubuntu) System : Linux SuiteCRM-8 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.3.19 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/mequitecbeta.crmtiger.com/ |
Upload File : |
<?php
require_once('data/CRMEntity.php');
include_once 'include/utils/VtlibUtils.php';
require_once('include/utils/utils.php');
class SyncWCProductsHandler{
function process(){
//echo "Script Execution Start!"."<br>";
$page_details = $this->getPageDetails();
//echo "<pre>";print_r($page_details);exit;
$page_no = $page_details['page_no'];
$page_limit = $page_details['page_limit'];
$sino = $page_details['sino'];
/*$page_no = 2;
$page_limit = 1;*/
$curlUrl = "https://maquitewp.crmtiger.com/getWooProducts.php?page=$page_no&perPage=$page_limit";
//$curlUrl = "https://maquitewp.crmtiger.com/getWooProducts.php?page=8&perPage=1";
$productList_json = $this->getProductList($curlUrl, 'GET', $data=array());
$productList = json_decode($productList_json, true);
//echo "<pre>";print_r($productList);exit;
$product_records_count_info = $productList['productRecordPageCount'];
$productsData = $productList['productRecordsData'];
$total_records = $product_records_count_info['totalRecord'];
$total_pages = $product_records_count_info['totalPages'];
$created_count = 0;
$updated_count = 0;
foreach($productsData as $productInfo){
$product_wp_id = $productInfo['wpProductId'];
$product_name = $productInfo['productname'];
$unit_price = $productInfo['unit_price'];
$purchase_cost = $productInfo['purchase_cost'];
$Image_details = $productInfo['imagename'];
$taxStatus = $productInfo['taxStatus'];
$tax1 = '';
if($taxStatus == 'taxable'){
$tax1 = $productInfo['tax1'];
}
$url = $productInfo['url'];
$video1 = $productInfo['video1'];
$video2 = $productInfo['video2'];
$productcode = $productInfo['productcode'];
$website = $productInfo['website'];
$manufacturer_name = $productInfo['manufacturer_name'];
$tags_label = $productInfo['tags_label'];
$qtyinstock = $productInfo['qtyinstock'];
$qtyinstock = $qtyinstock?$qtyinstock:0;
$discontinued = $productInfo['discontinued'];
$woo_product_category = $productInfo['woo_product_category'];
$description = $productInfo['description'];
//echo $woo_product_category;exit;
if($discontinued == 'visible'){
$discontinued_value = 1;
}else{
$discontinued_value = 0;
}
$type = $productInfo['type'];
if($type == 'variable'){
$variationProducts = $productInfo['variationProducts'];
$this->VariableProductsHandler($variationProducts);
}
$data = array(
'productname' => $product_name,
'purchase_cost' => $purchase_cost,
'unit_price' => $unit_price,
'qtyinstock' => $qtyinstock,
'discontinued' => $discontinued_value,
'wp_product_id' => $product_wp_id,
'url' => $url,
'tags_label' => $tags_label,
'tax1' => $tax1,
'video1' => $video1,
'video2' => $video2,
'manufacturer_name' => $manufacturer_name,
'woo_product_category' => $woo_product_category,
'productcode' => $productcode,
'website' => $website,
'description' => $description,
'assigned_user_id' => '19x1', // 19=Users Module ID, 1=First user Entity ID
);
//echo "<pre>";print_r($data);exit;
$is_new = $this->IsNewProduct($product_wp_id);
if ($is_new != 0) {
$data['id'] = $is_new;
$product_wsid = $this->UpdateProductInCRM($data);
$productid = $is_new;
$updated_count++;
$sync_log_data = array(
'product_relation' => $product_wsid,
'wp_product_id' => $product_wp_id,
'sync_type' => 'WPTOCRM',
'action_type' => 'Update',
'sync_status' => 'Success',
'description' => '',
'assigned_user_id' => '19x1',
);
$this->CreateProductSyncLogInCRM($sync_log_data);
} else {
$product_wsid = $this->CreateProductInCRM($data);
$sync_log_data = array(
'product_relation' => $product_wsid,
'wp_product_id' => $product_wp_id,
'sync_type' => 'WPTOCRM',
'action_type' => 'Create',
'sync_status' => 'Success',
'description' => '',
'assigned_user_id' => '19x1',
);
$this->CreateProductSyncLogInCRM($sync_log_data);
$productid = explode('x', $product_wsid);
$productid = $productid[1];
$created_count++;
}
$image_id = $Image_details['id'];
$image_date_created = $Image_details['date_created'];
$image_date_created_gmt = $Image_details['date_created_gmt'];
$image_date_modified = $Image_details['date_modified'];
$image_date_modified_gmt = $Image_details['date_modified_gmt'];
$image_src = $Image_details['src'];
$image_name = $Image_details['name'];
$image_alt = $Image_details['alt'];
$imagepath = $image_src;
$path = parse_url($imagepath, PHP_URL_PATH);
$extension = pathinfo($path, PATHINFO_EXTENSION);
if($extension == 'webp'){
$this->insertProductImage($productid, 'Products', $imagepath, $extension);
}else{
$base64_image = $this->convertImage2base64($imagepath);
$this->insertProductImage($productid, 'Products', $base64_image, $extension);
}//end of else
//$base64_image = $this->convertImage2base64($imagepath);
//$this->insertProductImage($productid, 'Products', $imagepath);
/*foreach($Image_details as $Image_info){
$image_id = $Image_info['id'];
$image_date_created = $Image_info['date_created'];
$image_date_created_gmt = $Image_info['date_created_gmt'];
$image_date_modified = $Image_info['date_modified'];
$image_date_modified_gmt = $Image_info['date_modified_gmt'];
$image_src = $Image_info['src'];
$image_name = $Image_info['name'];
$image_alt = $Image_info['alt'];
$imagepath = $image_src;
$base64_image = $this->convertImage2base64($imagepath);
$this->insertProductImage($productid, 'Products', $base64_image);
}*/
}
$next_page_no = $page_no + 1;
$next_page_limit = 10;
if($sino == ''){
$sino = $this->getPageSINO();
}
$scanned_records = $this->getScannedRecords();
if($scanned_records == $total_records){
$trigger_status = 'Completed';
}else{
$trigger_status = 'Processed';
}
$this->UpdateNextTriggerInformation($next_page_no,$next_page_limit,$total_records,$scanned_records,$trigger_status,$sino);
$result = array('success'=>true,'total_count' => $total_records, 'created_count' => $created_count, 'updated_count' => $updated_count);
$response = json_encode($result);
echo $response;
//echo "Script Execution End!"."<br>";
}
function VariableProductsHandler($variationProducts){
foreach($variationProducts as $v_id => $v_product){
//echo "v-Productid".$v_id.'<br>';
//echo 'v-productName'..'<br>';
$v_product_data = array(
'productname' => $v_product['name'],
'productcode' => $v_product['productcode'],
'unit_price' => $v_product['unit_price'],
'discontinued' => 1,
'wp_product_id' => $v_id,
'assigned_user_id' => '19x1',
);
//echo "<pre>";print_r($v_product_data);exit;
$is_new = $this->IsNewProduct($v_id);
if ($is_new != 0) {
$v_product_data['id'] = $is_new;
$product_wsid = $this->UpdateProductInCRM($v_product_data);
$productid = $is_new;
$sync_log_data = array(
'product_relation' => $product_wsid,
'wp_product_id' => $v_id,
'sync_type' => 'WPTOCRM',
'action_type' => 'Update',
'sync_status' => 'Success',
'description' => '',
'assigned_user_id' => '19x1',
);
$this->CreateProductSyncLogInCRM($sync_log_data);
} else {
$product_wsid = $this->CreateProductInCRM($v_product_data);
$sync_log_data = array(
'product_relation' => $product_wsid,
'wp_product_id' => $v_id,
'sync_type' => 'WPTOCRM',
'action_type' => 'Create',
'sync_status' => 'Success',
'description' => '',
'assigned_user_id' => '19x1',
);
$this->CreateProductSyncLogInCRM($sync_log_data);
$productid = explode('x', $product_wsid);
$productid = $productid[1];
}
}
}
function insertProductImage($id, $module, $base64_image, $imageExtension) {
global $adb, $current_user;
global $upload_badext;
$date_var = date("Y-m-d H:i:s");
//$current_id = $id + 1;
if($imageExtension == 'webp'){
$webpImage = imagecreatefromwebp($base64_image);
// Get the dimensions of the .webp image
$width = imagesx($webpImage);
$height = imagesy($webpImage);
// Create a blank canvas for the .png image
$pngImage = imagecreatetruecolor($width, $height);
// Preserve transparency if the .webp image has an alpha channel
imagesavealpha($pngImage, true);
$transparent = imagecolorallocatealpha($pngImage, 0, 0, 0, 127);
imagefill($pngImage, 0, 0, $transparent);
// Copy the .webp image onto the blank canvas
imagecopy($pngImage, $webpImage, 0, 0, 0, 0, $width, $height);
}else{
$base64_string = $base64_image;
$img = base64_decode($base64_image);
}//end of else
$current_id = $adb->getUniqueID("vtiger_crmentity");
$upload_file_path = decideFilePath();
$image_name = $current_id . "_wp_img.png";
$binFile = sanitizeUploadFileName($image_name, $upload_badext);
$encryptFileName = Vtiger_Util_Helper::getEncryptedFileName($binFile);
$upload_file_name = $current_id .'_'."$encryptFileName";
if($imageExtension == 'webp'){
$pngFile = $upload_file_path."/" .$upload_file_name;
imagepng($pngImage, $pngFile);
imagedestroy($webpImage);
imagedestroy($pngImage);
}else{
if ($file = fopen("$upload_file_path/" . $upload_file_name, 'wb')) {
fwrite($file, $img);
fclose($file);
}
}//end of else
$sql0 = "DELETE FROM vtiger_seattachmentsrel where crmid=?";
$params0 = array($id);
$adb->pquery($sql0, $params0);
$sql1 = "INSERT INTO vtiger_crmentity (crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime) VALUES (?, ?, ?, ?, ?, ?, ?)";
$params1 = array($current_id, $current_user->id, $current_user->id, $module . ' Image', '', $adb->formatDate($date_var, true), $adb->formatDate($date_var, true));
$adb->pquery($sql1, $params1);
//Add entry to attachments
$sql2 = "INSERT INTO vtiger_attachments(attachmentsid, name, description, type, path,storedname) values(?, ?, ?, ?, ?,?)";
$params2 = array($current_id, 'wp_img.png','', 'image/png', $upload_file_path,$encryptFileName);
$adb->pquery($sql2, $params2);
//Add relation
$sql3 = 'INSERT INTO vtiger_seattachmentsrel VALUES(?,?)';
$params3 = array($id, $current_id);
$adb->pquery($sql3, $params3);
return $current_id;
}
function convertImage2base64($imagepath) {
$img = file_get_contents($imagepath);
$data = base64_encode($img);
return $data;
}
function getScannedRecords(){
global $adb;
$sql = "SELECT COUNT(*) as scannedproducts FROM `vtiger_products` as a JOIN vtiger_crmentity as b ON (b.crmid=a.productid) WHERE b.deleted=0 AND wp_product_id != ''";
$result = $adb->pquery($sql,array());
$scannedproducts_count = $adb->query_result($result,0,'scannedproducts');
return $scannedproducts_count;
}
function IsNewProduct($wp_product_id) {
global $adb;
$sql = "SELECT productid FROM `vtiger_products` as a join vtiger_crmentity as b on (b.crmid=a.productid) where b.deleted=0 and a.wp_product_id=?";
$result = $adb->pquery($sql, array($wp_product_id));
$no_of_rows = $adb->num_rows($result);
if ($no_of_rows > 0) {
$isNew_status = $adb->query_result($result, 0, 'productid');
} else {
$isNew_status = 0;
}
return $isNew_status;
}
function PageEntryStatus(){
global $adb;
$sql = "SELECT 1 FROM `wc_product_sync`";
$result = $adb->pquery($sql,array());
$no_of_rows = $adb->num_rows($result);
if($no_of_rows > 0){
$status = 1;
}else{
$status = 0;
}
return $status;
}
public function InsertSyncInformation($page_no,$page_limit,$start_datetime,$status){
global $adb;
$sql = "INSERT INTO `wc_product_sync`(`page_no`, `page_limit`,`sync_start_time`, `status`) VALUES (?,?,?,?)";
$result = $adb->pquery($sql,array($page_no,$page_limit,$start_datetime,$status));
}
function UpdateNextTriggerInformation($next_page_no,$next_page_limit,$total_records,$scanned_records,$trigger_status,$sino){
global $adb;
$datetime = date('Y-m-d H:i:s');
$sql = "UPDATE `wc_product_sync` SET `page_no` = ?, `page_limit` =?,`total_records`=?,`scanned_records`=?,`status`=?,`sync_end_time` = ?, `last_sync_time` =? WHERE `sino`=?";
$result = $adb->pquery($sql,array($next_page_no,$next_page_limit,$total_records,$scanned_records,$trigger_status,$datetime,$datetime,$sino));
}
function CreateProductSyncLogInCRM($data) {
include_once 'include/Webservices/Utils.php';
include_once 'modules/Users/Users.php';
include_once 'include/Webservices/Create.php';
include_once 'includes/main/WebUI.php';
try {
$user = new Users();
$current_user = $user->retrieveCurrentUserInfoFromFile(Users::getActiveAdminId());
$product = vtws_create('CTProductSyncLog', $data, $current_user);
$sync_wsid = $product['id'];
} catch (WebServiceException $ex) {
echo $ex->getMessage();
}
return $sync_wsid;
}
function CreateProductInCRM($data) {
include_once 'include/Webservices/Utils.php';
include_once 'modules/Users/Users.php';
include_once 'include/Webservices/Create.php';
include_once 'includes/main/WebUI.php';
try {
$user = new Users();
$current_user = $user->retrieveCurrentUserInfoFromFile(Users::getActiveAdminId());
$product = vtws_create('Products', $data, $current_user);
$product_wsid = $product['id'];
$productid = explode('x', $product['id']);
$productid = $productid[1];
} catch (WebServiceException $ex) {
echo $ex->getMessage();
}
return $product_wsid;
}
function UpdateProductInCRM($data) {
include_once 'include/Webservices/Utils.php';
include_once 'modules/Users/Users.php';
include_once 'include/Webservices/Revise.php';
include_once 'includes/main/WebUI.php';
try {
$user = new Users();
$current_user = $user->retrieveCurrentUserInfoFromFile(Users::getActiveAdminId());
$productid = $data['id'];
$wsid = vtws_getWebserviceEntityId('Products', $productid); // Module_Webservice_ID x CRM_ID
$data['id'] = $wsid;
$product = vtws_revise($data, $current_user);
} catch (WebServiceException $ex) {
echo $ex->getMessage();
}
return $wsid;
}
function getPageSINO(){
global $adb;
$sql = "SELECT * FROM wc_product_sync WHERE status = ?";
$result = $adb->pquery($sql,array('Processed'));
$sino = $adb->query_result($result,0,'sino');
return $sino;
}
function getPageDetails(){
global $adb;
$date = date('Y-m-d');
$sql = "SELECT * FROM wc_product_sync WHERE status != ?";
$result = $adb->pquery($sql,array('Completed'));
//echo $adb->convert2sql($sql,array('Completed'))."<br>";
$no_of_rows = $adb->num_rows($result);
$pageInfo = array();
if($no_of_rows > 0){
$pageInfo['sino'] = $adb->query_result($result,0,'sino');
$pageInfo['page_no'] = $adb->query_result($result,0,'page_no');
$pageInfo['page_limit'] = $adb->query_result($result,0,'page_limit');
}else{
$pageInfo['sino'] = '';
$pageInfo['page_no'] = 1;
$pageInfo['page_limit'] = 10;
//date_default_timezone_set("Europe/Madrid");
//date_default_timezone_set("Asia/Kolkata");
$start_datetime = date('Y-m-d H:i:s');
$this->InsertSyncInformation(1,1,$start_datetime,'Processed');
}
return $pageInfo;
}
function getProductList($curlUrl, $method, $data){
$headers = array(
"Content-Type: application/json",
);
if($data != ""){
$data = json_encode($data);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($method != 'GET'){
curl_setopt($ch, CURLOPT_POSTFIELDS, ($data));
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// converting
$response = curl_exec($ch);
return $response;
curl_close($ch);
}
}
$handler = new SyncWCProductsHandler();
$handler->process();
?>