<?php
	include '../db/db.php';
	session_start();
	$log=htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
	if (empty($log)) {
	     
	  $redirectUrl = "index.php";
	  echo "<script type=\"text/javascript\">";
	  echo "window.location.href = '$redirectUrl'";
	  echo "</script>";
		 
	}
	
	if (array_key_exists('sales_report_load', $_POST)) {//data table

		$date = htmlspecialchars(trim($_POST['date']), ENT_QUOTES, 'UTF-8');

		$date1=substr($date, 0, 10);
		$date2=substr($date, 13, 23);

		$sql = "SELECT  * FROM order_details WHERE date BETWEEN '$date1' AND '$date2'" ;

	    $data = array();
	    $result = mysqli_query($conn,$sql);
	    while ($row = mysqli_fetch_assoc($result)) {
	        $data[] = $row;
	    }
	    echo json_encode($data);
	}

	if (array_key_exists('profit_per_order', $_POST)) {//data table

		$inv_id = htmlspecialchars(trim($_POST['inv_id']), ENT_QUOTES, 'UTF-8');

		$sql = "SELECT
cart_details.Qty,
cart_details.price,
cart_details.Item_Code,
stock_cost.avg_cost
FROM
stock_cost
Inner Join cart_details ON cart_details.Item_Code = stock_cost.item
WHERE
cart_details.inv_id =  '$inv_id'
" ;

	    $data = array();
	    $result = mysqli_query($conn,$sql);
	    while ($row = mysqli_fetch_assoc($result)) {
	        $data[] = $row;
	    }
	    echo json_encode($data);
	}


	if (array_key_exists('order_load', $_POST)) {//data table

		$inv_id = htmlspecialchars(trim($_POST['inv_id']), ENT_QUOTES, 'UTF-8');

		//$sql = "SELECT  * FROM cart_details WHERE inv_id = '$inv_id'" ;
		$sql = "SELECT
stock_cost.avg_cost,
cart_details.Item,
cart_details.Qty,
cart_details.price
FROM
cart_details
Inner Join stock_cost ON cart_details.Item_Code = stock_cost.item
WHERE
cart_details.inv_id =  '$inv_id'
";

	    $data = array();
	    $result = mysqli_query($conn,$sql);
	    while ($row = mysqli_fetch_assoc($result)) {
	        $data[] = $row;
	    }
	    echo json_encode($data);
	}
	
?>