<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$response = array();
$page_start = $utilities->replaceZero($_POST['page_start']);
$order_date = $utilities->replaceToday($_POST['order_date']);
$flag_category = $utilities->replaceZero($_POST['flag_category']);
try {
    $query = "SELECT `order`.basket_code,\n                        `user`.user_id,\n                        CONCAT(`user`.f_name, ' ', `user`.l_name) AS user_name,\n                        CONCAT(`user`.country_code, `user`.phone_number) AS user_phone_number,\n                        `user`.image_url AS user_image_url,\n                        `order`.item_quantity_price_json,\n                        `order`.date,\n                        `order`.flag_category,\n                        address.street_address,\n                        address.landmark,\n                        address.city,\n                        address.state,\n                        address.country,\n                        address.pincode,\n                        CONCAT(address.country_code, address.phone_number) AS delivery_address_phone_number,\n                        time_slot.time_slot_name,\n                        time_slot.time_slot_start,\n                        time_slot.time_slot_end,\n                        `order`.log_datetime AS order_placed_datetime,\n                        `status`.status_id,\n                        `status`.`status`\n                FROM `order` INNER JOIN `user` ON `order`.user_id = `user`.user_id\n                         INNER JOIN address ON `order`.address_id = address.address_id\n                         INNER JOIN time_slot ON `order`.time_slot_id = time_slot.time_slot_id\n                         INNER JOIN `status` ON `order`.`status` = `status`.status_id\n                WHERE `order`.date = '{$order_date}'";
    $query .= $flag_category == 0 ? "" : " AND flag_category = '{$flag_category}' ";
    $query .= "ORDER BY order_placed_datetime DESC\n                LIMIT {$page_start}, 25";
    $statement = $dbConnection->prepare($query);
    try {
        if ($statement->execute($bindParams)) {
            $data = $statement->fetchAll(PDO::FETCH_ASSOC);
            $statement->closeCursor();
            $response = array('status' => 0, 'data' => $data, 'desc' => 'success');
        } else {
            $errorCode = -99;
            $dbError = $statement->errorInfo();
            $statement->closeCursor();
            $response = array('status' => $errorCode, 'data' => array(), 'desc' => 'DB error occured' . $dbError[2]);
        }
    } catch (PDOExecption $e) {
        $errorCode = -7;
        $statement->closeCursor();
        $error = "Exception: " . $e->getMessage();
        $response = array('status' => $errorCode, 'data' => array(), 'desc' => 'PDO exception occured' . $error);
$response = array();
$basket_code = uniqid("BSKT_");
/*
 * Json array with id, qunatity in this format.
 * [
 *  {'item':1,'name': 'xyz','quantity':2,'price':200},
 *  {'item':2,'name': 'abc','quantity':1,'price':150},
 *  {'item':3,'name': 'qwert','quantity':1,'price':20},
 *  {'item':4,'name': 'poiuyt','quantity':1,'price':30}
 * ]
 */
$item_quantity_price_json = $utilities->clean($_POST['item_quantity_price_json']);
$user_id = $utilities->replaceOne($_POST['user_id']);
$time_slot_id = $utilities->replaceOne($_POST['time_slot_id']);
$status = $utilities->replaceOne($_POST['status']);
$date = $utilities->replaceToday($_POST['date']);
$address_id = $utilities->replaceZero($_POST['address_id']);
$order_type_id = $utilities->replaceZero($_POST['order_type_id']);
$log_datetime = $utilities->replaceNow();
$order_id = -1;
try {
    $insertQuery = "INSERT INTO " . " `order` (basket_code, user_id, item_quantity_price_json, time_slot_id, status, date, address_id, log_datetime) " . " VALUES ('{$basket_code}', '{$user_id}', '{$item_quantity_price_json}', '{$time_slot_id}', '{$status}', '{$date}', '{$address_id}', '{$log_datetime}')";
    $statement = $dbConnection->prepare($insertQuery);
    try {
        $dbConnection->beginTransaction();
        if ($statement->execute()) {
            $order_id = $dbConnection->lastInsertId();
            $dbConnection->commit();
            $response = array('status' => $order_id, 'basket_code' => $basket_code, 'desc' => 'Success');
        } else {
            $status = -99;
<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$response = array();
$bill_code = uniqid("BILL_");
$bill_pay_through = $utilities->clean($_POST['bill_pay_through']);
$bill_date = $utilities->replaceToday($_POST['bill_date']);
$bill_discount = $utilities->clean($_POST['bill_discount']);
$bill_total_amount = $utilities->clean($_POST['bill_total_amount']);
$user_name = $utilities->clean($_POST['user_name']);
$user_email = $utilities->clean($_POST['user_email']);
$user_phone_number = $utilities->clean($_POST['user_phone_number']);
$use_delivery_address = $utilities->clean($_POST['use_delivery_address']);
$basket_code = $utilities->clean($_POST['basket_code']);
$item_quantity_price_json = $utilities->clean($_POST['item_quantity_price_json']);
$is_bill_payed = $utilities->replaceZero($_POST['is_bill_payed']);
$log_datetime = $utilities->replaceNow();
//Calculating bill amount...
$basketQunatityArray = array();
$totalAmount = 0;
$bill_item_count = 0;
$orderJSONArray = json_decode($item_quantity_price_json);
foreach ($orderJSONArray as $foodItem) {
    $subTotalPrice = $foodItem['qunatity'] * $foodItem['price'];
    $bill_item_count++;
    array_push($basketQunatityArray, array('item_name' => $foodItem['name'], 'qunatity' => $foodItem['qunatity'], 'unit_price' => $foodItem['price'], 'sub_total_price' => $subTotalPrice));
    $totalAmount += $subTotalPrice;
}