<?php


require_once('config.php');





if(($response["error"] == true))
{
    echo(json_encode($response));
}


if((!isset($_SESSION["GOLDFAYRE"])))
{
    $response["error"]          =   true;
    $response["error_msg"]      =   "An Error Occurred. You are requested to login first ";
}


if(($response["error"] == false))
{
    if((isset($_POST["request"])) && ($_POST["request"] == "place_order"))
    {
        
        $cart               =   array();
        $payable            =   0;
        $firstname          =   "";
        $lastname           =   "";
        $email              =   "";
        $country            =   "";
        $state              =   "";
        $city               =   "";
        $address            =   "";
        $payment_way        =   "";









        if((!isset($_SESSION["GOLDFAYRE"])))
        {
            $response["error"]      =   true;
            $response["error_msg"]  =   "An Error Occurred. You are requested to login first.";
        }

        if(($response["error"] == false))
        {
            if((!isset($_SESSION["GOLDFAYRE"]["CART"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. The Cart is empty.";
            }

        }


        if(($response["error"] == false))
        {
            if((!isset($_POST["firstname"])) || (empty($_POST["firstname"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. First Name is required.";
            }
            else
            {
                $firstname  =   $_POST["firstname"];
            }
        }

        if(($response["error"] == false))
        {
            if((!isset($_POST["lastname"])) || (empty($_POST["lastname"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. Last Name is required.";
            }
            else
            {
                $lastname  =   $_POST["lastname"];
            }
        }
        if(($response["error"] == false))
        {
            if((!isset($_POST["email"])) || (empty($_POST["email"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. Email is required.";
            }
            else
            {
                $email  =   $_POST["email"];
            }
        }

        if(($response["error"] == false))
        {
            if((!isset($_POST["country"])) || (empty($_POST["country"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. Country is required.";
            }
            else
            {
                $country  =   $_POST["country"];
            }
        }

        if(($response["error"] == false))
        {
            if((!isset($_POST["state"])) || (empty($_POST["state"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. State is required.";
            }
            else
            {
                $state  =   $_POST["state"];
            }
        }

        if(($response["error"] == false))
        {
            if((!isset($_POST["city"])) || (empty($_POST["city"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. City is required.";
            }
            else
            {
                $city  =   $_POST["city"];
            }
        }

        if(($response["error"] == false))
        {
            if((!isset($_POST["address"])) || (empty($_POST["address"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. Address is required.";
            }
            else
            {
                $address  =   $_POST["address"];
            }
        }


        if(($response["error"] == false))
        {
            if((!isset($_POST["payment_way"])) || (empty($_POST["payment_way"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. Payment Method is required.";
            }
            else
            {
                $payment_way  =   $_POST["payment_way"];
            }
        }

        if(($response["error"] == false))
        {
            if((isset($_SESSION["GOLDFAYRE"]["CART"])))
            {
                $cart   =   $_SESSION["GOLDFAYRE"]["CART"];
                
                foreach($cart as $k => $v)
                {
                    $item_price         =   0;
                    $quantity           =   0;
                    $item_total         =   0;
                    $item_price         =   $v["PRICE"];
                    $quantity           =   $v["QTY"];
                    $item_total         =   ($quantity * $item_price);
                    $payable            =   $payable + $item_total;
                    $v["SUB_TOTAL"]     =   $item_total;
                    $cart[$k]           =   $v;
                }

            }

        }


        if(($response["error"] == false))
        {
            $sql    =   "SELECT (COALESCE(MAX(M.id), 0)  + 1) ID FROM orders_mst M";
            $stmt = $db->prepare($sql); 
            $stmt->execute();
            $result     =   $stmt->get_result();
            $row        =   mysqli_fetch_assoc($result);
            if(($row))
            {
                $logs["ID"]         =   $row["ID"];
                $logs["ORDER_NO"]   =   date("Y").$logs["ID"];
            }
            else
            {
                $response["error"]      =   true;
                $response["error_msg"]  =   "An Error Occurred while creating the order. You are requested to try again later.";
            }
        }


        if(($response["error"] == false))
        {
            $status     =   "P";
            $sql        =   "   INSERT INTO orders_mst(order_no, firstname, lastname, email, country, state, city, address, 
                                payment_way, user_id, status, payable) 
                                VALUES (? , ?, ?, ? ,? ,? ,?, ?, ?, ?, ?, ?)";
            $stmt = $db->prepare($sql); 
            $stmt->bind_param("ssssssssssss", $logs["ORDER_NO"], $firstname, $lastname, $email, $country, $state, $city, $address, $payment_way, $_SESSION["GOLDFAYRE"]["USER_ID"], $status, $payable);
            $result     =   $stmt->execute();
            if((!$result))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred while creating the order. You are requested to try again later.";
            }
            else
            {
                $logs["ORDER_MST_ID"]   =   $db->insert_id;
            }
        }


        if(($response["error"] == false))
        {
            foreach($cart as $k => $v)
            {
                $sql        =   "INSERT INTO order_det(order_mst_id, product_id, amount, quantity) VALUES (?, ?, ?, ?)";
                $stmt       =   $db->prepare($sql); 
                $stmt->bind_param("ssss", $logs["ORDER_MST_ID"], $v["PID"], $v["SUB_TOTAL"], $v["QTY"]);
                $result     =   $stmt->execute();
                if((!$result))
                {
                    $response["error"]          =   true;
                    $response["error_msg"]      =   "An Error Occurred while creating the order. You are requested to try again later.";
                }
            }
        }


        if(($response["error"] == false))
        {
            $response["message"]    =   "Your Order has been placed successfully. Your Order # ".$logs["ORDER_NO"];
        }

        echo(json_encode($response));
    }


}












































?>