<?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 =561;
$priceArr = array();
$result12 = $conn->query("select * from users where  id = 561 ");
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 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['qty'];
    	$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['quantity'];
        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 quantityflag = 1 where user_id = ". $user_id." and  id = ".$row['id']);
		}
	}
}

$result = $conn->query("select * from product_variants where shopifyvariantid != '' and quantityflag = 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'];
        $shopifyinventoryid = $row['shopifyinventoryid'];
        $shopifylocationid = $row['shopifylocationid'];
        if($shopifylocationid == ''){
            $shopifylocationid = $location_id;
        }
        $priceflag = $row['priceflag'];
        $quantityflag = $row['quantityflag'];
        $sku = $row['sku'];					
        $quantity = $row['quantity'];
        $price = $row['price'];
        $res = updateShopifyInventory($rowid, $user_id, $token, $shopurl, $shopifyinventoryid, $shopifylocationid, $quantity);
        $conn->query("update product_variants set quantityflag = 0 where id = ".$rowid);
        echo "updated";
    }

}
    }
}
function updateShopifyInventory($rowid, $user_id, $token, $shopurl, $inventory_item_id, $location_id, $quantity){
    global $conn;
    echo "token".$token;
    $data = array("location_id" => $location_id, "inventory_item_id" => $inventory_item_id, "available" => $quantity);
    print_r($data);
   
    $url = "https://".$shopurl."/admin/api/2023-04/inventory_levels/set.json";
    print_r($url);
    $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, "POST");
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec ($curl);
    curl_close ($curl);
    //$response_arr = explode("\n", $response);
    print_r($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);
}
    return false;
}
?>