<?php
session_start();

$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();
}
if ($_SERVER['REQUEST_METHOD'] != "POST") {
    header("location: index.php");
    die();
}

require "vendor/autoload.php";
$today = date("Y-m-d");        
$today_hour = date("Y-m-d H:i:s");



$qrcode = new QrReader($_FILES['qrimage']['tmp_name']);
$text = $qrcode->text();
 $logged_id = $_SESSION['getUserID'];
$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'];



?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Decoding QR codes</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    <style>
        html, body {
            height: 100%;
            width: 100%;
        }
        .bg {
            background-image: url("images/bg.jpg");
            height: 100%;
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
        }
    </style>
</head>
<body class="bg">
    <div class="container">
        <br><br><br>
        <div class="row">
            <div class="col-md-6 offset-md-3" style="background-color: white; padding: 20px; box-shadow: 10px 10px 5px #888;">
                <div class="panel-heading">
                    <h1>Decode QR codes</h1>
                </div>
                <hr>
                <p><strong>Data in QR-code:</strong></p>
                <p><?php echo $text." ".$logged_id ;
                $link ="../../../main_pos?code=$name";
               
                header("Location:  $link"); /* Redirect browser */

                ?></p>
                <hr>
                <a href="index.php">Decode Another</a>
            </div>
        </div>
    </div>
</body>
</html>