Files
medicalalert-web-reloaded/wp/wp-content/plugins/iper-medical/iper-cron.php
Tony Volpe 4eb982d7a8 Merged in feature/from-pantheon (pull request #16)
code from pantheon

* code from pantheon
2024-01-10 17:03:02 +00:00

742 lines
29 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: linus
* Date: 03/02/16
* Time: 17:15
*/
$type_sync = "".$_GET['t'];
function createMessage($stato,$type,$Message){
$obj = array(
"time"=>time(),
"stato"=>$stato,
"type"=> $type,
"message"=>$Message
);
return $obj;
}
/************* chiamata request ****************/
$log = "";
$crypto = new SecuredContent();
if($type_sync != "c"){
/*$api_uri = get_option(id_api_salesforce);*/
/*$api_uri = 'https://lilsand2-connectamerica.cs23.force.com/RestServices/';*/ // this variable should be configurable
$api_rest="services/apexrest/GetProductCatalog";
/* $api_uri = get_option(id_api_salesforce);*/
$api_uri = get_option("id_api_salesforce"); // this variable should be configurable
$api_rest="GetProductCatalog";
$api_param="?brand=";
$api_brand= get_option("id_brand");
//$log_Array = array();
//array_push($log_Array,createMessage(1,"info","inizio sync"));
$is_post = false;//$_SERVER['REQUEST_METHOD'] === 'POST';
$url = $api_uri .$api_rest . $api_param . $api_brand; //$api_uri . (($is_post) ? $_POST['meta']['uri'] : $_GET['meta']['uri']);
echo $url;
$request = curl_init($url);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, $is_post ? 'POST' : 'GET');
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if ($is_post) {
$data = json_encode($crypto->encode_content(json_encode($_POST['content'])));
curl_setopt($request, CURLOPT_POSTFIELDS, $data);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($data)));
}
else {
$auth_header = json_encode($crypto->encode_content(urldecode($url)));
echo $auth_header;
curl_setopt($request, CURLOPT_HTTPHEADER, array('CA-Authorization: ' . $auth_header));
}
//array_push($log_Array,createMessage(1,"info","chiamata"));
global $wpdb;
$chiamta1=$wpdb->prepare("CALL IPER_MA_REQUEST_SET(%d,%s,%s,%s,%s,%d)",
0,
$url,
'',
'',
'',
-1
);
//array_push($log_Array,createMessage(1,"Chiamata",$chiamta1));
$rs=callDBStored($chiamta1,true);
array_push($log_Array,createMessage(1,"Risultato",$rs));
$result = curl_exec($request);
echo "---result--".$result."---result--";
curl_close($request);
// $result = json_decode($result);
// $result = json_decode($result);
$result = json_decode($result);
$result = (OBJECT)json_decode($crypto->decode_content($result));
$chiamta1=$wpdb->prepare("CALL IPER_MA_REQUEST_SET(%d,%s,%s,%s,%s,%d)",
$rs->identity,
$url,
'',
$result->ResponseHeader->ResponseId,
json_encode($result),
-1
);
//array_push($log_Array,createMessage(1,"Chiamata",$chiamta1));
$rs=callDBStored($chiamta1,true);
//array_push($log_Array,createMessage(1,"Risultato",$rs));
$RequestID = $rs->identity;
$ResponseId = $result->ResponseHeader->ResponseId;
$Brand = $result->ResponseBody->BrandOfferings;
if (!empty($Brand) && count($Brand)==1){
$BrandID = $result->ResponseBody->BrandOfferings[0]->BrandID;
$product = $result->ResponseBody->BrandOfferings[0]->Products;
$log = $log."INIZIO INSERIMENTO PRODOTTI".PHP_EOL;
if (!empty($product)){
//array_push($log_Array,createMessage(1,"info","Inizio Prodotti". count($product)));
$log = $log."PRODOTTI " . count($product) . PHP_EOL;
foreach ($product as $single){
/************* inserimento prodotto ****************/
$ProductName=sanitize_text_field($single->ProductName);
$ProductID=sanitize_text_field($single->ProductID);
$ProductCode=sanitize_text_field($single->ProductCode);
$IsAvailable=sanitize_text_field($single->IsAvailable)?1:0;
$log = $log."prodotto:".$ProductID .PHP_EOL;
$call=$wpdb->prepare("CALL IPER_MA_PRODUCT_SET(%d,%s,%s,%s,%s,%s,%d,%d)",
0,
$BrandID,
$ProductName,
$ProductID,
$ProductCode,
$IsAvailable,
0,
$RequestID
);
$log = $log."chiamata:".$call .PHP_EOL;
$rs=callDBStored($call,true);
$idProduct = $rs->identity;
if($rs->status == 2){
$typepost = array(
'post_title' => $ProductName,
'post_status' => 'publish', //Default status draft
'post_author' => 1,
'post_type' => 'product'
);
//creating post with arguments above and assign post id to $def_post_id
$def_post_id = wp_insert_post($typepost);
$call=$wpdb->prepare("CALL IPER_MA_PRODUCT_SET(%d,%s,%s,%s,%s,%s,%d,%d)",
$idProduct,
$BrandID,
$ProductName,
$ProductID,
$ProductCode,
$IsAvailable,
$def_post_id,
$RequestID
);
$rs=callDBStored($call,true);
$log = $log."Inserimento prodotto:".$ProductID. " id:".$idProduct.PHP_EOL;
}elseif ($rs->status == 1){
$log = $log."Modifica prodotto:".$ProductID. " id:".$idProduct.PHP_EOL;
}else{
$log = $log."Error prodotto:".$ProductID. PHP_EOL;
}
/************* inserimento RatePlans ****************/
$rateplan = $single->RatePlans;
if (!empty($rateplan)){
$log = $log."RATEPLAN " . count($rateplan) . PHP_EOL;
foreach ($rateplan as $rate) {
$Term=sanitize_text_field($rate->Term);
$RatePlanID=sanitize_text_field($rate->RatePlanID);
$Price=sanitize_text_field($rate->Price);
$OrderType=sanitize_text_field($rate->OrderType);
$MonthlyPrice = sanitize_text_field($rate->MonthlyPrice);
$log = $log."rateplan:".$RatePlanID .PHP_EOL;
$call=$wpdb->prepare("CALL IPER_MA_RATEPLAN_SET(%d,%d,%s,%s,%s,%s,%s,%d,%d)",
0,
$idProduct,
$Term,
$RatePlanID,
$Price,
$OrderType,
$MonthlyPrice,
0,
$RequestID
);
$log = $log."chiamata:".$call .PHP_EOL;
$rs=callDBStored($call,true);
$idRateplan = $rs->identity;
if($rs->status == 2){
$typepost = array(
'post_title' => $Term,
'post_status' => 'publish', //Default status draft
'post_author' => 1,
'post_type' => 'rateplan'
);
//creating post with arguments above and assign post id to $def_post_id
$def_post_id = wp_insert_post($typepost);
$call=$wpdb->prepare("CALL IPER_MA_RATEPLAN_SET(%d,%d,%s,%s,%s,%s,%s,%d,%d)",
$idRateplan,
$idProduct,
$Term,
$RatePlanID,
$Price,
$OrderType,
$MonthlyPrice,
$def_post_id,
$RequestID
);
$rs=callDBStored($call,true);
$log = $log."Inserimento rateplan:".$RatePlanID. " id:".$idRateplan.PHP_EOL;
}elseif ($rs->status == 1){
$log = $log."Modifica rateplan:".$RatePlanID. " id:".$idRateplan.PHP_EOL;
}else{
$log = $log."Error rateplan:".$RatePlanID. PHP_EOL;
}
/************* inserimento Upsell ****************/
$UpsellOptions = $rate->UpsellOptions; // da chiedere la struttura
if(!empty($UpsellOptions)) {
$log = $log."UpsellOptions " . count($UpsellOptions) . PHP_EOL;
foreach($UpsellOptions as $upsell){
$ShowBeforeAccessories = sanitize_text_field($upsell->ShowBeforeAccessories)?1:0;
$ProductName= sanitize_text_field($upsell->ProductName);
$ProductID= sanitize_text_field($upsell->ProductID);
$Price= sanitize_text_field($upsell->Price);
$log = $log."UpsellOptions:".$ProductID .PHP_EOL;
$call=$wpdb->prepare("CALL IPER_MA_UPSELL_SET(%d,%d,%s,%s,%s,%s,%d,%d)",
0,
$idRateplan,
$ProductName,
$ProductID,
$Price,
$ShowBeforeAccessories,
0,
$RequestID
);
$log = $log."chiamata:".$call .PHP_EOL;
$rs=callDBStored($call,true);
$idUpsell = $rs->identity;
if($rs->status == 2){
$typepost = array(
'post_title' => $ProductName,
'post_status' => 'publish', //Default status draft
'post_author' => 1,
'post_type' => 'upsell'
);
//creating post with arguments above and assign post id to $def_post_id
$def_post_id = wp_insert_post($typepost);
$call=$wpdb->prepare("CALL IPER_MA_UPSELL_SET(%d,%d,%s,%s,%s,%s,%d,%d)",
$idUpsell,
$idRateplan,
$ProductName,
$ProductID,
$Price,
$ShowBeforeAccessories,
$def_post_id,
$RequestID
);
$rs=callDBStored($call,true);
$log = $log."Inserimento UpsellOptions:".$idRateplan. " id:".$ProductID.PHP_EOL;
}elseif ($rs->status == 1){
$log = $log."Modifica UpsellOptions:".$idRateplan. " id:".$ProductID.PHP_EOL;
}else{
$log = $log."Error UpsellOptions:".$ProductID. PHP_EOL;
}
/********** INSERIMENTO UPSELL PROMOTION ************/
$log = $log."INSERIMENTO UPSELL PROMOTION <br>";
$UpsellPromotion = $upsell->Promotion;
$log = $log."INSERIMENTO UPSELL PROMOTION ".count($UpsellPromotion).PHP_EOL;
if(!empty($UpsellPromotion)){
$upsellPromotionID = sanitize_text_field($UpsellPromotion->PromotionID);
$upsellPrice = sanitize_text_field($UpsellPromotion->Price);
$upsellName = sanitize_text_field($UpsellPromotion->Name);
$upsellMonthlyPrice = sanitize_text_field($UpsellPromotion->MonthlyPrice);
$call = $wpdb->prepare("CALL IPER_MA_UPSELL_PROMOTION_SET(%d,%d,%s,%s,%s,%s,%d,%d)",
0,
$idUpsell,
$upsellPromotionID,
$upsellPrice,
$upsellName,
$upsellMonthlyPrice,
0,
$RequestID
);
$log = $log . "chiamata:" . $call . PHP_EOL;
$rs = callDBStored($call, true);
$idUpsellPromotions = $rs->identity;
$log = $log . "Inserimento Upsell Promotion:" . $upsellPromotionID . " id:" . $idUpsellPromotions . PHP_EOL;
}//UPSELL PROMOTION IF
}
}
/************* inserimento AccessoryGroups ****************/
$AccessoryGroups = $rate->AccessoryGroups;
if(!empty($AccessoryGroups)){
$log = $log."AccessoryGroups " . count($AccessoryGroups) . PHP_EOL;
foreach ($AccessoryGroups as $group){
$LimitRestriction=sanitize_text_field($group->LimitRestriction)?1:0;
$IncludedWithOrder=sanitize_text_field($group->IncludedWithOrder)?1:0;
$Description=sanitize_text_field($group->Description);
$AccessoryGroupID=sanitize_text_field($group->AccessoryGroupID);
$log = $log."AccessoryGroups:".$AccessoryGroupID .PHP_EOL;
$call=$wpdb->prepare("CALL IPER_MA_ACCESSORY_GROUP_SET(%d,%d,%s,%s,%s,%s,%d,%d)",
0,
$idRateplan,
$LimitRestriction,
$IncludedWithOrder,
$Description,
$AccessoryGroupID,
0,
$RequestID
);
$log = $log."chiamata:".$call .PHP_EOL;
$rs=callDBStored($call,true);
$idAccessoryGroup = $rs->identity;
$log = $log."Inserimento AccessoryGroups:".$AccessoryGroupID. " id:".$idAccessoryGroup.PHP_EOL;
/************* inserimento Accessories ****************/
$Accessory = $group->Accessories;
if(!empty($Accessory)){
$log = $log."Accessory " . count($Accessory) . PHP_EOL;
foreach ($Accessory as $single_Accessory){
$Quantity = sanitize_text_field($single_Accessory->Quantity);
$Price= sanitize_text_field($single_Accessory->Price);
$IsAvailable= sanitize_text_field($single_Accessory->IsAvailable)?1:0;
$AccessoryName= sanitize_text_field($single_Accessory->AccessoryName);
$AccessoryID= sanitize_text_field($single_Accessory->AccessoryID);
$AccessoryCode= sanitize_text_field($single_Accessory->AccessoryCode);
$log = $log."Accessories:".AccessoryID .PHP_EOL;
$call=$wpdb->prepare("CALL IPER_MA_ACCESSORY_SET(%d,%d,%s,%s,%s,%s,%s,%s,%d,%d)",
$idAccessoryGroup,
0,
$Quantity,
$Price,
$IsAvailable,
$AccessoryName,
$AccessoryID,
$AccessoryCode,
0,
$RequestID
);
$log = $log."chiamata:".$call .PHP_EOL;
$rs=callDBStored($call,true);
$idAccessory = $rs->identity;
$log = $log."Inserimento Accessories:".$AccessoryID. " id:".$idAccessory." Status".$rs->status.PHP_EOL;
if($rs->status == 2){
$typepost = array(
'post_title' => $AccessoryName,
'post_status' => 'publish', //Default status draft
'post_author' => 1,
'post_type' => 'accessory' //controllare
);
//creating post with arguments above and assign post id to $def_post_id
$def_post_id = wp_insert_post($typepost);
$call=$wpdb->prepare("CALL IPER_MA_ACCESSORY_SET(%d,%d,%s,%s,%s,%s,%s,%s,%d,%d)",
$idAccessoryGroup,
$idAccessory,
$Quantity,
$Price,
$IsAvailable,
$AccessoryName,
$AccessoryID,
$AccessoryCode,
$def_post_id,
$RequestID
);
$rs=callDBStored($call,true);
$log = $log."Inserimento Accessories:".$AccessoryID. " id:".$idAccessory.PHP_EOL;
}elseif ($rs->status == 1){
$log = $log."Modifica Accessories:".$AccessoryID. " id:".$idAccessory.PHP_EOL;
}else{
$log = $log."Error Accessories:".$ProductID. PHP_EOL;
}
}//ACCESSORY FOR
}//ACCESSORY IF
}//GROUP ACC FOR
}//GROUP ACC IF
/************* inserimento AvailablePromotions ****************/
$log = $log."INSERIMENTO PROMOTION <br>";
$AvailablePromotions = $rate->AvailablePromotions;
$log = $log."INSERIMENTO PROMOTION ".count($AvailablePromotions).PHP_EOL;
if(!empty($AvailablePromotions)){
foreach ($AvailablePromotions as $promotion) {
$PromotionID = "".sanitize_text_field($promotion->PromotionID);
$Price = "".sanitize_text_field($promotion->Price);
$Name = "".sanitize_text_field($promotion->Name);
$MonthlyPrice = "".sanitize_text_field($promotion->MonthlyPrice);
echo"idRate".$idRateplan;
print_r($promotion);
echo "CALL IPER_MA_PROMOTION_SET(%d,%d,%s,%s,%s,%s,%d,%d)";
$call = $wpdb->prepare("CALL IPER_MA_PROMOTION_SET(%d,%d,%s,%s,%s,%s,%d,%d)",
0,
$idRateplan,
$PromotionID,
$Price,
$Name,
$MonthlyPrice,
0,
$RequestID
);
$log = $log . "chiamata promotion:" . $call . PHP_EOL;
$rs = callDBStored($call, true);
$idAvailablePromotions = $rs->identity;
$log = $log . "Inserimento AvailablePromotions:" . $PromotionID . " id:" . $idAvailablePromotions . PHP_EOL;
}
}//PROMOTION IF
}//RATEPLAN FOR
}//RATEPLAN IF
$Bundles = $single->Bundles;
if(!empty($Bundles)){
$BundleProducts = $Bundles->BundleProducts;
if (!empty($BundleProducts)){
foreach ($BundleProducts as $bundle){
$Name = sanitize_text_field($bundle->Name);
$ID = sanitize_text_field($bundle->ID);
$call=$wpdb->prepare("CALL IPER_MA_BUNDLE_SET(%d,%d,%s,%s,%d,%d)",
0,
$idProduct,
$ID,
$Name,
0,
$RequestID
);
$log = $log."chiamata:".$call .PHP_EOL;
$rs=callDBStored($call,true);
$idBundleProducts = sanitize_text_field($rs->identity);
$log = $log."Inserimento BundleProducts:".$ID. " id:".$idBundleProducts.PHP_EOL;
}//BundleProducts for
}//BundleProductsif
}//bundles if
}//PRODUCT FOR
}//PRODUCT IF
/************* inserimento Shippings ****************/
$Shippings = $result->ResponseBody->BrandOfferings[0]->Shippings;
$log = $log."INIZIO INSERIMENTO SHIPPING <br>";
if(!empty($Shippings)){
$log = $log."SHIPPING " . count($Shippings) . PHP_EOL;
foreach ($Shippings as $single){
$ShippingMethod = sanitize_text_field($single->ShippingMethod);
$ShippingID = sanitize_text_field($single->ShippingID);
$ShippingCarrier = sanitize_text_field($single->ShippingCarrier);
$Price = sanitize_text_field($single->Price);
$call=$wpdb->prepare("CALL IPER_MA_SHIPPING_SET(%d,%s,%s,%s,%s,%s,%d,%d)",
0,
$BrandID,
$ShippingMethod,
$ShippingID,
$ShippingCarrier,
$Price,
0,
$RequestID
);
$log = $log."chiamata:".$call .PHP_EOL;
$rs=callDBStored($call,true);
$idShipping = sanitize_text_field($rs->identity);
if($rs->status == 2){
$typepost = array(
'post_title' => $ShippingMethod,
'post_status' => 'publish', //Default status draft
'post_author' => 1,
'post_type' => 'shipping' //controllare
);
//creating post with arguments above and assign post id to $def_post_id
$def_post_id = wp_insert_post($typepost);
$call=$wpdb->prepare("CALL IPER_MA_SHIPPING_SET(%d,%s,%s,%s,%s,%s,%d,%d)",
$idShipping,
$BrandID,
$ShippingMethod,
$ShippingID,
$ShippingCarrier,
$Price,
$def_post_id,
$RequestID
);
$log = $log."chiamata:".$call .PHP_EOL;
$rs=callDBStored($call,true);
$log = $log."Inserimento Shipping:".$ShippingID. " id:".$idShipping.PHP_EOL;
}elseif ($rs->status == 1){
$log = $log."Modifica Shipping:".$ShippingID. " id:".$idShipping.PHP_EOL;
}else{
$log = $log."Error Shipping:".$ShippingID. PHP_EOL;
}
}//Shipping for
}//shipping if
$call="CALL IPER_MA_CLEAN_WP_POST";
$rs=callDBStored($call,true);
$log = $log."Pulizia POST";
$call=$wpdb->prepare("CALL IPER_MA_REQUEST_SET_LOG(%d,%s)",
$RequestID,
$log
);
$rs=callDBStored($call,true);
$log = $log."Inserimento LOG id:".$RequestID.PHP_EOL;
}//brand
echo $log;
}else{
//CAMPAIGN SYNC
echo "\n\n***** CAMPAIGN SYNC *****\n";
$api_uri = get_option("id_api_salesforce"); // this variable should be configurable
$api_rest=IperCampaignController::kREST_ACTION;
$api_param="?Brand=";
$api_brand= get_option("id_brand");
$url = $api_uri .$api_rest . $api_param . $api_brand; //$api_uri . (($is_post) ? $_POST['meta']['uri'] : $_GET['meta']['uri']);
echo "URL: ".$url;
$request_campaign = curl_init($url);
curl_setopt($request_campaign, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt($request_campaign, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request_campaign, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($request_campaign, CURLOPT_COOKIE, 'debug_logs=debug_logs,domain=.force.com');
$auth_header = json_encode($crypto->encode_content(urldecode($url)));
curl_setopt($request_campaign, CURLOPT_HTTPHEADER, array('CA-Authorization: ' . $auth_header));
$result = curl_exec($request_campaign);
curl_close($request_campaign);
echo $result;
$data = stripslashes($result);
if(!empty($data)){
$data = substr($data,1);
$data = substr($data,0,-1);
$data = (OBJECT)json_decode($crypto->decode_content($data));
if(!empty($data) && !empty($data->ResponseBody)){
echo "\nCampaign Updates:";
$campaignController = IperCampaignController::instance();
$campaignController->cronJob($data);
}else{
echo "\nNo data";
}
}else{
echo "\n** FAILED **\n";
}
echo "\n\n***** END CAMPAIGN SYNC *****";
}
?>