<?php
set_time_limit(0);
ini_set('memory_limit', '-1');
require_once("includes/config.php");

require_once("includes/etsyv3/vendor/autoload.php");
use OAuth\Client;

$user_id =1552;
$priceArr = array();
$result12 = $conn->query("select * from users where  id = 1552 ");
if ($result12->num_rows > 0) {// 
	while($row12 = $result12->fetch_assoc()) {
$id = $row12['user_id'];
		$shopurl = $row12['shopurl'];
		$token = $row12['token'];		
		$shop_id = $row12['shop_id'];


$result = $conn->query("SELECT variantid, qty,price FROM shopifyproducts WHERE user_id = ".$user_id);
if($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
        $variantid = $row['variantid'];
    //	$price = $row['price'];
       $price = $row['price'];
    	$priceArr[$variantid] = $price;
    }
}
print_r($priceArr);
$result = $conn->query("SELECT id, shopifyvariantid, price, quantity FROM product_variants WHERE shopifyvariantid != '' and user_id = ".$user_id);
if($result->num_rows > 0){
	while($row = $result->fetch_assoc()){
		$shopifyvariantid = $row['shopifyvariantid'];
		// $price = $row['price'];
		// $price = number_format($price, 2, '.', '');
		// $orig_price = $row['orig_price'];
		// if($orig_price > 0){
		//     $price = $orig_price;
		// }
		$quantity = $row['price'];
        print_r($quantity);
                //  $quantity = $row['price'];
		if(array_key_exists($shopifyvariantid, $priceArr) && $priceArr[$shopifyvariantid] != $quantity){
		    echo $shopifyvariantid." - ".$priceArr[$shopifyvariantid]." <> ".$quantity."---<br />";
			$conn->query("update product_variants set priceflag = 1 where user_id = ". $user_id." and  id = ".$row['id']);
		}
	}
}

$result = $conn->query("select * from product_variants where shopifyvariantid != '' and priceflag = 1 and user_id = ".$user_id)or die("error in query");
if($result->num_rows > 0){
	while($row = $result->fetch_assoc()){
         
       
        $rowid = $row['id'];
        $shopifyproductid = $row['shopifyproductid'];
        $shopifyvariantid = $row['shopifyvariantid'];
      
        $priceflag = $row['priceflag'];
       
        $price = $row['price'];
        $data = array(
            "variant"=>array(
                "id" => $shopifyvariantid,		
                "price" => number_format($price, 2, '.', '')
            )
        );				
$res = updateShopifyVariant($token, $shopurl, $shopifyvariantid, $data, $rowid, $user_id);
if($res){
    $conn->query("update product_variants set priceflag = 0 where user_id = ".$user_id." and id = ".$rowid);
 
}
    }

}
    }
}
function updateShopifyVariant($token, $shopurl, $variant_id, $data, $rowid, $user_id){
	global $conn;
	$url = "https://".$shopurl."/admin/api/2023-04/variants/".$variant_id.".json";
	
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token:'.$token, 'Content-Type: application/json; charset=utf-8'));
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_VERBOSE, 0);
	curl_setopt($curl, CURLOPT_HEADER, 1);
	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
	curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	$response = curl_exec ($curl);
	curl_close ($curl);
    echo $response;
	$response_arr = explode("\n",$response);	
	$climit = -1;
	foreach($response_arr as $obj){
		if (strpos($obj, 'X-Shopify-Shop-Api-Call-Limit') !== false) {
			$tempArr = explode(":", $obj);
			$climit = substr(trim(end($tempArr)), 0, -3);
		}
	}	
	if(intval($climit) > 35){
		sleep(5);
	} else {
	    sleep(1);
	}
	if( (strstr(($response_arr[0]), "200")) || (strstr(($response_arr[1]), "200")) || (strstr(($response_arr[2]), "200")) ) {
		return true;
	} else if(strstr(($response_arr[0]), "404")){
		$conn->query("update product_variants set quantityflag = 0, priceflag = 0, deleted = 1 where user_id = ".$user_id." and id = ".$rowid);
	} else if(strstr(($response_arr[0]), "429")){
		sleep(1);
	}
	return false;
}
?>