<?php
require "global.ajax.php";

// POST VARIABLES

switch ($_GET["op"]) {

	case 'ajax_countOrderNotification':

		// GET NOTIFICATION COUNT
		$sqlNotification = "SELECT
		IFNULL(SUM( IF ( ord_det_status = 'Pending', 1, 0 ) ),0) AS total_pending,
		IFNULL(SUM( IF ( ord_det_status = 'Delivered', 1, 0 ) ),0) AS total_complete 
		FROM tb_orders_request
		WHERE ord_det_ent_id = {$session_user_ent_id}
		AND ord_det_pos_id = {$session_user_pos_id}
		AND ord_det_cancel_status = 'Enabled' 
		AND DATE_FORMAT( ord_created_at, '%Y-%m-%d %H:%i:%s' ) BETWEEN '" . $currentDayStart . "' AND '" . $currentDayEnd . "';";

		// exit($sqlNotification);
		$answerOrderNotification = executePDOFetchOne($sqlNotification, "");

		// GET PENDING ORDERS BY USER
		$wherePosAndUserId = "AND sale_pos_id = {$session_user_pos_id} AND sale_created_by = {$session_user_id}";
		$whereDate = "AND sale_date BETWEEN '{$currentDayStart}' AND '{$currentDayEnd}'";

		$sqlPendingOrderByUser = "SELECT
				COUNT(*) AS pendingSaleByUser
				FROM tb_sales
				WHERE IFNULL((SELECT IFNULL(SUM(sale_det_pro_price*sale_det_pro_qty),0) FROM tb_sales_details WHERE sale_det_sale_id = sale_id)+(IFNULL(sale_cost_delivery,0)+IFNULL(sale_tax_value,0)+IFNULL(sale_tax_law_value,0)+(IFNULL(sale_type_tax_value,0)))-IFNULL(sale_discount_value,0),0)-(SELECT IFNULL(SUM(pay_det_value),0) FROM tb_payments_details WHERE pay_det_sale_id = sale_id) > 0 AND sale_status = 'Enabled' AND sale_debt_status = 'On' $wherePosAndUserId $whereDate;";

		// exit($sqlPendingOrderByUser);
		$answerPendingOrderByUser = executePDOFetchOne($sqlPendingOrderByUser, "");

		$newArray = ["OrderNotification" => $answerOrderNotification, "PendingOrderByUser" => $answerPendingOrderByUser];
		// $newArray = [$answerOrderNotification, $answerPendingOrderByUser];
		echo json_encode($newArray);

		break;


	// case 'ajaxCountPendingOrderByUser':

	// 	$wherePosAndUserId = "AND sale_pos_id = {$session_user_pos_id} AND sale_created_by = {$session_user_id}";
	// 	$whereDate = "AND DATE_FORMAT(ord_created_at, '%Y-%m-%d %H:%i:%s') BETWEEN '{$currentDayStart}' AND '{$currentDayEnd}'";

	// 	$sql = "SELECT
	// 			COUNT(*) AS pendingSaleByUser
	// 			FROM tb_sales
	// 			LEFT JOIN tb_orders_request ON ord_det_sale_id = sale_id
	// 			WHERE IFNULL((SELECT IFNULL(SUM(sale_det_pro_price*sale_det_pro_qty),0) FROM tb_sales_details WHERE sale_det_sale_id = sale_id)+(IFNULL(sale_cost_delivery,0)+IFNULL(sale_tax_value,0)+IFNULL(sale_tax_law_value,0)+(IFNULL(sale_type_tax_value,0)))-IFNULL(sale_discount_value,0),0)-(SELECT IFNULL(SUM(pay_det_value),0) FROM tb_payments_details WHERE pay_det_sale_id = sale_id) > 0 AND sale_status = 'Enabled' AND sale_debt_status = 'On' $wherePosAndUserId $whereDate;";

	// 	// exit($sql);
	// 	$answer = executePDOFetchOne($sql, "");
	// 	echo json_encode($answer);

	// 	break;


	case 'ajax_pending_orders_list':

		$sql = "SELECT
		ord_det_id
		,ord_det_number
		,ord_det_code
		,ord_det_sale_id
		,ord_det_sale_code
		,ord_det_cus_id
		FROM tb_orders_request
		WHERE ord_det_ent_id = {$session_user_ent_id} AND ord_det_pos_id = {$session_user_pos_id} AND ord_det_status = 'Pending' AND ord_det_cancel_status = 'Enabled' AND DATE_FORMAT(ord_created_at, '%Y-%m-%d %H:%i:%s') BETWEEN '{$currentDayStart}' AND '{$currentDayEnd}' ORDER BY ord_det_sale_id,ord_det_number ASC;";
		// exit($sql);
		$answer = executePDOFetchAllAPI($sql);
		echo json_encode($answer);

		break;


	case 'ajax_complete_orders_list':

		$sql = "SELECT
		ord_det_id
		,ord_det_number
		,ord_det_code
		,ord_det_sale_id
		,ord_det_sale_code
		,ord_det_cus_id
		FROM tb_orders_request
		WHERE ord_det_ent_id = {$session_user_ent_id} AND ord_det_pos_id = {$session_user_pos_id} AND ord_det_cancel_status = 'Enabled' AND ord_det_status = 'Delivered' AND DATE_FORMAT(ord_created_at, '%Y-%m-%d %H:%i:%s') BETWEEN '{$currentDayStart}' AND '{$currentDayEnd}' ORDER BY ord_det_sale_id,ord_det_number ASC;";

		$answer = executePDOFetchAllAPI($sql);
		echo json_encode($answer);

		break;


	case 'ajax_complete_order':

		$ord_det_code = isset($_POST["ord_det_code"]) ? funCheckInput($_POST["ord_det_code"]) : "";

		$sql = "UPDATE tb_orders_request SET ord_det_status = 'Delivered' WHERE ord_det_code = '$ord_det_code';";
		$result = executeQueryPDO($sql, $translate["successfully_delivered"], "Error");
		echo json_encode($result);

		break;


	case 'ajax_complete_all_orders':

		$sql = "UPDATE tb_orders_request SET ord_det_status = 'Delivered' WHERE ord_det_pos_id = {$session_user_pos_id};";
		$result = executeQueryPDO($sql, $translate["successfully_delivered"], "Error");
		echo json_encode($result);

		break;


	case 'ajaxListPendingOrderByUser':

		$wherePosAndUserId = "AND sale_ent_id = {$session_user_ent_id} AND sale_created_by = {$session_user_id}";
		$whereDate = "AND DATE_FORMAT(ord_created_at, '%Y-%m-%d %H:%i:%s') BETWEEN '{$currentDayStart}' AND '{$currentDayEnd}'";

		$sql = "SELECT
				sale_id
				,(select cus_full_name FROM tb_customers WHERE cus_id = sale_cus_id) AS client_full_name
				,(select table_description FROM tb_tables WHERE table_id = sale_table_id) AS table_description
	
				,IFNULL((SELECT IFNULL(SUM(sale_det_pro_price*sale_det_pro_qty),0) FROM tb_sales_details WHERE sale_det_sale_id = sale_id)+(IFNULL(sale_cost_delivery,0)+IFNULL(sale_tax_value,0)+IFNULL(sale_tax_law_value,0)+(IFNULL(sale_type_tax_value,0)))-IFNULL(sale_discount_value,0),0)-(SELECT IFNULL(SUM(pay_det_value),0) FROM tb_payments_details WHERE pay_det_sale_id = sale_id) AS sale_debts

				FROM tb_sales
				LEFT JOIN tb_orders_request ON ord_det_sale_id = sale_id
				WHERE IFNULL((SELECT IFNULL(SUM(sale_det_pro_price*sale_det_pro_qty),0) FROM tb_sales_details WHERE sale_det_sale_id = sale_id)+(IFNULL(sale_cost_delivery,0)+IFNULL(sale_tax_value,0)+IFNULL(sale_tax_law_value,0)+(IFNULL(sale_type_tax_value,0)))-IFNULL(sale_discount_value,0),0)-(SELECT IFNULL(SUM(pay_det_value),0) FROM tb_payments_details WHERE pay_det_sale_id = sale_id) > 0 AND sale_status = 'Enabled' AND sale_debt_status = 'On' $wherePosAndUserId $whereDate
				GROUP BY sale_id ORDER BY sale_id DESC;";

		$answer = executePDOFetchAllAPI($sql);
		echo json_encode($answer);

		break;
}
