    <?php

session_start();

include_once("../../../admin/db/saucerer.php");

//include ('../../../db/config.php');

include('../../../admin/css/libs/phpqrcode/qrlib.php'); 

//IP Address Protocol
$ip_address = $_SERVER["REMOTE_ADDR"];

// Check if the "mobile" word exists in User-Agent 
$isMob = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "mobile")); 
  
// Check if the "tablet" word exists in User-Agent 
$isTab = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "tablet")); 
 

// Platform check  
$isWin = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "windows")); 
$isAndroid = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "android")); 
$isIPhone = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "iphone")); 
$isIPad = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "ipad")); 
$isIOS = $isIPhone || $isIPad; 
  
    if($isTab){ 
        if ($isIPad) {
        $device = 'iPad';
    } else {
        $device = 'Tablet'; 
    }
  } elseif($isMob){ 
    if ($isIPhone) {
        $device = 'iPhone';
    } else {
        $device = 'Phone'; 
    }

  } else{ 
        $device = 'Desktop'; 
} 
 
if($isIOS){ 
    $system = 'iOS'; 
}elseif($isAndroid){ 
    $system = 'ANDROID'; 
}elseif($isWin){ 
    $system = 'WINDOWS'; 
}

    //Client Handler

    if(isset($_POST['submit'])){


        date_default_timezone_set('Africa/Kampala');
        $today = date("His-dmY",time());

    $a=" ";
    $b="";
    $phone_space = str_replace($a, $b, $_POST['phone']);

    $x="+256";
    $y="0";
    $phone = str_replace($x, $y, $phone_space);


        $sql = "SELECT * FROM clients WHERE phone = '$phone' OR phone1 = '$phone'";
        $result = $conn->query($sql);  

                while($row = mysqli_fetch_array($result)) { 
                
                $gender = $row['gender'];

                if (empty($row['reward'])) {
                    $reward1 = 0;
                } else {
                    $reward1 = $row['reward'];
                }
                $total_reward = $reward1+$_POST['total_reward'];
                }
            $query = $conn->query($sql);
            if($query->num_rows > 0){

                $client_id = $row['client_id'];

               $sql = "UPDATE clients SET reward='$total_reward' WHERE client_id = '".$_POST['client_id']."'";

            }

            else {

    $client_id = date("HisdmY",time());
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $location = $_POST['location'];
    $reward = $_POST['total_reward'];
    $handler = $_POST['handler'];
    if ($_POST['handler']=='User') {
        $author = ''.$fname.' '.$lname.'';
    } else {
        $author = $_SESSION['name'];
    }
    

    $a=" ";
    $b="";
    $phone_space = str_replace($a, $b, $_POST['phone']);

    $x="+256";
    $y="0";
    $phone = str_replace($x, $y, $phone_space);

        $sql = "INSERT INTO clients (client_id, fname, lname, phone, email, address, location, handler, author, reward) VALUES ('$client_id','$fname','$lname','$phone','$email','$address','$location','$handler','$author','$reward')";

    }
        $conn->query($sql);

}

?>


<?php 

//Checkout Client 

    if(isset($_POST['submit']))  
    {  

    if ($_POST['client_id']) {
    $client_id = $_POST['client_id'];
    } elseif ($client_id) {
    $client_id = $client_id;
    } else {
    $client_id = date("HisdmY",time());
    }

    $order_code = $_POST['order_code'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $location = $_POST['location'];
    $handler = $_POST['handler'];
    $token = $_POST['token'];
    $image = $_POST['image'];
    $qr_image = $_POST['qr_image'];
    $delivery = $_POST['delivery'];
    $discount = $_POST['discount'];
    $notes = $_POST['notes'];
    $total=$_POST['total'];
    if ($_POST['handler']=='User') {
        $author = ''.$fname.' '.$lname.'';
    } else {
        $author = $_SESSION['name'];
    }
    $name1=$_POST['name'];  
    $name2="";  
    $cat1=$_POST['cat'];  
    $cat2="";  
    $quantity1=$_POST['quantity'];  
    $quantity2="";
    $price1=$_POST['price'];  
    $price2="";
    $subtotal1=$_POST['subtotal'];  
    $subtotal2="";
    $reward1=$_POST['reward'];  
    $reward2="";
    $image1=$_POST['image'];  
    $image2="";


    for($i=0;$i<count($qr_image);$i++){

         $qr_img_id = $qr_image[$i];

    $tempDir = '../../../admin/orders/qr/'; 
    $filename = $qr_img_id;
    $codeContents = 'https://irungikitchen.com/admin/clients/manager/?client='.$email.''; 
    QRcode::png($codeContents, $tempDir.''.$filename.'.png', QR_ECLEVEL_L, 5);

}


date_default_timezone_set('Africa/Kampala');
$day = date("D",time());
$month = date("M",time());
$month_one = date("m",time());
$year = date("Y",time());

    for($i=0;$i<count($name1);$i++){
        $name_id = $name1[$i];
        $cat_id = $cat1[$i];
        $quantity_id = $quantity1[$i];
        $price_id = $price1[$i];
        $subtotal_id = $subtotal1[$i];
        $reward_id = $reward1[$i];
        $image_id = $image1[$i];

    $in_ch=mysqli_query($con,"insert into checkout(order_code, client_id, fname, lname, email, phone, gender, address, location, handler, author, image, name, cat, price, quantity, subtotal, delivery, discount, total, day, month, month_one, year, ip_address, device_name, system_name, reward, notes) values ('$order_code', '$client_id', '$fname', '$lname', '$email', '$phone', '$gender', '$address', '$location', '$handler', '$author', '$image_id', '$name_id', '$cat_id', '$price_id', '$quantity_id', '$subtotal_id', '$delivery', '$discount', '$total', '$day', '$month', '$month_one', '$year', '$ip_address', '$device', '$system', '$reward_id', '$notes')");  

        }

    if($in_ch==1)  
       {  
        if ($handler=="Admin") {
        $_SESSION['success'] = 'Order placed successfully <a href="invoices/invoice/?i='.$order_code.'" class="text-italic text-underline text-white">View Invoice</a>.';
    } else {
          $_SESSION['success'] = 'Order placed successfully.';  
      }
    }  
    else  
       {  
          $_SESSION['error'] = "Failed to place order, try again!";  
       }  

    }  

    ?>


<?php 

//Checkout Admin

    if(isset($_POST['submit']))  
    {  

    $order_code = $_POST['order_code'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $location = $_POST['location'];
    $handler = $_POST['handler'];
    $token = $_POST['token'];
    $name = "".$fname." ".$lname."";
    $access = $_POST['access'];
    $image = $_POST['image'];
    $delivery = $_POST['delivery'];
    $discount = $_POST['discount'];
    $reward = $_POST['total_reward'];
    $notes = $_POST['notes'];
    $total=$_POST['total'];  
    if ($_POST['handler']=='User') {
        $author = ''.$fname.' '.$lname.'';
    } else {
        $author = $_SESSION['name'];
    }
    $name1=$_POST['name'];  
    $name2="";  
    $cat1=$_POST['cat'];  
    $cat2="";  
    $quantity1=$_POST['quantity'];  
    $quantity2="";
    $price1=$_POST['price'];  
    $price2="";
    $subtotal1=$_POST['subtotal'];  
    $subtotal2="";
    $image1=$_POST['image'];  
    $image2="";

    foreach($name1 as $name3)  
       {  
          $name2 .= $name3.",";  
        }

    foreach($cat1 as $cat3)  
       {  
          $cat2 .= $cat3.",";  
        }

        foreach($quantity1 as $quantity3)  
       {  
          $quantity2 .= $quantity3.","; 
        }

        foreach($price1 as $price3)  
       {  
          $price2 .= $price3.","; 
        }

        foreach($subtotal1 as $subtotal3)  
       {  
          $subtotal2 .= $subtotal3.","; 
        }

        foreach($image1 as $image3)  
       {  
          $image2 .= $image3.","; 
        }


$sql = "SELECT count(*) FROM checkout WHERE order_code = '$order_code' AND email='$email'"; 

$result = $conn->query($sql);  

while($row = mysqli_fetch_array($result)) { 

$count = $row['count(*)'];
   
   if ($count=='1') {
    $items = ''.$count.' item';
        } else {
    $items = ''.$count.' items';
        }

    }

    $in_ch=mysqli_query($con,"insert into checkout_admin(order_code, fname, lname, email, phone, gender, address, location, handler, author, image, name, cat, price, quantity, items, subtotal, delivery, discount, total, reward, day, month, month_one, year, ip_address, device_name, system_name, notes) values ('$order_code', '$fname', '$lname', '$email', '$phone', '$gender', '$address', '$location', '$handler', '$author', '$image2', '$name2', '$cat2', '$price2', '$quantity2', '$items', '$subtotal2', '$delivery', '$discount', '$total', '$reward', '$day', '$month', '$month_one', '$year', '$ip_address', '$device', '$system', '$notes')");  
    
    if($in_ch==1)  
       {  
        $result = "Success";
        $summary = "".$name." placed an order";
        $link = "invoices/invoice/?i=$order_code";
        $channel = "Notifications";
       }  
    else  
       {  
        $result = "Error";
        $summary = "".$name." failed to place an order";
        $link = "#!";
        $channel = "System";
       }


    if ($handler=="User") {
        header("location: order/?code=$order_code&email=$email");
        $filter = "Client";
        $user = "".$device." (".$system.")";
    } elseif ($handler=="Admin") {
        header("location: ../../../admin/sync/?action=empty&page=../");
        $filter = "Admin";
        $user = $_SESSION['name'];
    }


        $attempt = "Placing";
        $action = "Order";
        $category = "Product";

        $sql = "INSERT INTO secure (name, token, access, attempt, action, user, handler, result, filter, category, channel, summary, link, ip_address, device_name, system_name) 

                VALUES ('$name', '$token', '$access', '$attempt', '$action', '$user', 'Admin', '$result', '$filter', '$category', '$channel', '$summary', '$link', '$ip_address', '$device', '$system')";
                mysqli_query($mysqli, $sql);
      
    }  
    ?>


<?php 

//Rewards 

    if(isset($_POST['submit']))  
    {  

    $order_code = $_POST['order_code'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $reward = $_POST['reward'];
    $handler = $_POST['handler'];
    $author = ''.$fname.' '.$lname.'';
    $name1=$_POST['name'];  
    $name2="";  
    $quantity1=$_POST['quantity'];  
    $quantity2="";
    $price1=$_POST['price'];  
    $price2="";
    $image1=$_POST['image'];  
    $image2="";

    $names = "".$fname." ".$lname."";



    for($i=0;$i<count($name1);$i++){
        $name_id = $name1[$i];
        $quantity_id = $quantity1[$i];
        $price_id = $price1[$i];
        $image_id = $image1[$i];

    $in_ch=mysqli_query($con,"insert into rewards(names, email, phone, order_code, reward, handler, author) values ('$names', '$email', '$phone', '$order_code', '$reward', '$handler', '$author')");  

        }


    }  

    ?>


