
<?php

$file = $_FILES['file_qr']["tmp_name"];
// Create a cURL handle
$ch = curl_init('https://api.qrserver.com/v1/read-qr-code/');

// Create a CURLFile object
$cfile = new CURLFile($file,'image/*','file');

// Assign POST data
$data = array('file' => $cfile , 'MAX_FILE_SIZE' => '1048576');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(false){
     echo 'Curl error: ' . curl_error($ch);
     echo $link ="../../../main_pos?code=Error try again";
//header("Location:  $link");
}else{
$response = curl_exec($ch);

// close the connection, release resources used
curl_close($ch);

// do anything you want with your response
$x=json_decode($response, true);


session_start();
 $logged_id = $_SESSION['getUserID'];

$con=mysqli_connect($_SESSION['DB_HOST'], $_SESSION['DB_USER'], $_SESSION['DB_PASS'], $_SESSION['DB_NAME']);
if(mysqli_connect_errno()){
echo "Connection Fail".mysqli_connect_error();
}

$today = date("Y-m-d");        
$today_hour = date("Y-m-d H:i:s");



$text = $x[0]['symbol'][0]['data'];
$sales_tracker = mysqli_query($con, "SELECT * FROM sales_tracker  WHERE supplier_id='$logged_id' and customers_ID='$text'");
$sales_tracker_row =mysqli_fetch_array($sales_tracker);
$sales_tracker_ID =$sales_tracker_row['ID'];
$tracker_group =$sales_tracker_row['tracker_group'];
 $daily =$sales_tracker_row['daily'];
 $start_date =$sales_tracker_row['start_date'];
 $end_date= date('Y-m-d', strtotime($start_date. " + $daily days"));

$sales_tracker_check = mysqli_query($con, "SELECT * FROM sales_tracker_check  WHERE supplier_id='$logged_id' and customers_ID='$text'");
$num_row = mysqli_num_rows($sales_tracker_check);

$no_havd_error = true;

if($start_date <= $today && $end_date >= $today ){
    $query = mysqli_query($con, "insert into  sales_tracker_check(supplier_id,customers_ID,todey_day,check_it,sales_tracker_ID,tracker_group,old_start_date) value('$logged_id','$text','$today_hour','yes','$sales_tracker_ID','$tracker_group','$start_date')");
    $end_date2= date('Y-m-d', strtotime($end_date. " + 1 days"));
    $query_update = mysqli_query($con, "UPDATE sales_tracker SET start_date='$end_date2' WHERE supplier_id='$logged_id' and customers_ID='$text' ");
}elseif ($start_date <= date('Y-m-d', strtotime($today. " + $daily days")) && $end_date >= date('Y-m-d', strtotime($today. " + $daily days")) ) {
    $no_havd_error = false;

}
else{
    
    $query = mysqli_query($con, "insert into  sales_tracker_check(supplier_id,customers_ID,todey_day,check_it,sales_tracker_ID,tracker_group,old_start_date) value('$logged_id','$text','$today_hour','no','$sales_tracker_ID','$tracker_group','$start_date')");
    $end_date2= date('Y-m-d', strtotime($end_date. " + 1 days"));
    $query_update = mysqli_query($con, "UPDATE sales_tracker SET start_date='$end_date2' WHERE supplier_id='$logged_id' and customers_ID='$text' ");
    $query = mysqli_query($con, "insert into  sales_tracker_check(supplier_id,customers_ID,todey_day,check_it,sales_tracker_ID,tracker_group,old_start_date) value('$logged_id','$text','$today_hour','yes','$sales_tracker_ID','$tracker_group','$start_date')");
}

$sales_tracker_group = mysqli_query($con, "SELECT * FROM sales_tracker_group  WHERE old_start_date='$start_date' and old_end_date='$end_date' and tracker_group='$tracker_group'");
$num_sales_tracker_group = mysqli_num_rows($sales_tracker_group);
if($num_sales_tracker_group <= 0 && $no_havd_error){
    $end_date2 =strtotime($end_date);
    
    $query = mysqli_query($con, "insert into  sales_tracker_group(old_start_date,old_end_date,tracker_group) value ('$start_date','$end_date','$tracker_group')");
}




$tbl_customers = mysqli_query($con, "SELECT * FROM tbl_customers  WHERE  Customer_ID='$text'");
$tbl_customers_row =mysqli_fetch_array($tbl_customers);
$name =$tbl_customers_row['Customer_Name'];
echo $link ="../../../main_pos?code=$text";
//header("Location:  $link");
    
}