<?php
include 'connect.php';

//$url1 = 'https://api.orderingplus.com/v400/en/tikram/business/100/products?offset=1&limit=10';
$url1 = 'http://api.orderingplus.com/v400/en/tikram/business/100/products?limit=10&offset=1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json',
  'Accept: application/json',
  'x-api-key: '.$user_key
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$adresesult = curl_exec($ch);
$usraddress = json_decode($adresesult);

echo "<pre>";
echo sizeof($usraddress->result);
print_r($usraddress);
exit;

$url1 = $site_api_url_fetch.'/users/331/addresses/339';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json',
  'Accept: application/json',
  'x-api-key: '.$user_key
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$adresesult = curl_exec($ch);
$usraddress = json_decode($adresesult);
if (is_array($usraddress->result)) {
    foreach ($usraddress->result as &$item) {
        unset($item->user);
        unset($item->metafields);
    }
    unset($item);
} elseif (is_object($usraddress->result)) {
    unset($usraddress->result->user);
    unset($usraddress->result->metafields);
}
echo "<pre>";
print_r($usraddress->result);
exit;


$business = json_decode(file_get_contents('php://input'));
$business_id = [34];
$business_id = json_encode($business_id);

$url1 = $site_api_url_fetch.'/orders?orderBy=-id&page=1&page_size=50&where={"conditions":[{"conector":"OR","conditions":[{"attribute":"products","conditions":[{"attribute":"type","value":{"condition":"=","value":"item"}}]},{"attribute":"cloned_order_id","value":{"condition":"!=","value":null}},{"attribute":"delivery_type","value":{"condition":"=","value":9}}]},{"attribute":"status","value":[0,13,1,11,15,7,8,4,9,3,14,18,19,20,21,22,23,24,25,26]},{"conector":"AND","conditions":[{"attribute":"business_id","value":'.$business_id.'}]}],"conector":"AND"}&mode=dashboard';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json',
  'Accept: application/json',
  'x-api-key: '.$user_key
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$busordresult = curl_exec($ch);
$busorders = json_decode($busordresult);

$newArray = [];

foreach($busorders->result as $bus){

    foreach ($bus->products as $product) {
        foreach ($product->options as $option) {

            // Collect suboptions for comparison
            $suboptionsArray = [];
            foreach ($option->suboptions as $subop) {
                $suboptionsArray[] = [
                    'suboption_id' => $subop->id,
                    'suboption_name' => $subop->name,
                ];
            }

            // Check if same product + option + suboptions already exist
            $exists = false;
            foreach ($newArray as $existing) {
                foreach ($existing->options as $existingOption) {
                    $existingSuboptionsArray = [];
                    foreach ($existingOption->suboptions as $exSub) {
                        $existingSuboptionsArray[] = [
                            'suboption_id' => $exSub->id,
                            'suboption_name' => $exSub->name,
                        ];
                    }

                    if (
                        $existing->id == $product->id &&
                        $existingOption->id == $option->id &&
                        $existingSuboptionsArray == $suboptionsArray
                    ) {
                        $exists = true;
                        break 2; // stop both loops
                    }
                }
            }

            if (!$exists) {
                // Push the full product object
                $newArray[] = $product;
                break; // stop checking other options of this product
            }
        }
    }
}
echo sizeof($newArray).'//';
echo "<pre>";
print_r($newArray);
?>
