include '../../../includes/classes/core.php';
$start = $_REQUEST['start'];
$end = $_REQUEST['end'];
$action = $_REQUEST['act'];
$name = $_REQUEST['name'];
$error = '';
$data = '';
function GetSum($start, $end)
{
    $result = mysql_fetch_assoc(mysql_query("SELECT \tIFNULL(ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2),0) AS `sum`\r\n\t\t\t\t\t\t\t\t\t\t   FROM     sale_transaction\r\n\t\t\t\t\t\t\t\t\t\t   JOIN     sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id\r\n\t\t\t\t\t\t\t\t\t\t  WHERE     DATE(`sale_transaction`.`end_date`) >= '{$start}' AND DATE(`sale_transaction`.`end_date`) <= '{$end}' AND sale_transaction.status=0"));
    return $result['sum'];
}
function GetProductSum($name, $start, $end)
{
    $result = mysql_fetch_assoc(mysql_query("SELECT \tIFNULL(ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2),0) AS `sum`\r\n\t\t\t\t\t\t\t\t\t\t\t   FROM     sale_transaction\r\n\t\t\t\t\t\t\t\t\t\t\t   JOIN     sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id\r\n\t\t\t\t\t\t\t\t\t\t\t   JOIN     production ON production.id = sale_transaction_detail.production_id \r\n\t\t\t\t\t\t\t\t\t\t\t  WHERE     production.`name` = '{$name}' AND DATE(`sale_transaction`.`end_date`) >= '{$start}' AND DATE(`sale_transaction`.`end_date`) <= '{$end}' AND sale_transaction.`status` =0"));
    return $result['sum'];
}
switch ($action) {
    case 'get_sale_sum':
        $data = array('page' => GetSum($start, $end));
        break;
    case 'get_products_sum':
        $page = GetProductSum($name, $start, $end);
        $data = array('page' => $page);
        break;
    default:
        echo "Action is null!";
        break;
}
$data['error'] = $error;
echo json_encode($data);
}
function GetQuantity($start, $end)
{
    $result = mysql_fetch_assoc(mysql_query("SELECT  IFNULL(SUM(sale_transaction_detail.product_quantity),0) AS `sum`\r\n\t\t\t\t\t\t\t\t\t\t\t   FROM  sale_transaction\r\n\t\t\t\t\t\t\t\t\t\t\t   JOIN  sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id\r\n\t\t\t\t\t\t\t\t\t\t\t  WHERE  DATE(`sale_transaction`.`end_date`) >= '{$start}' AND DATE(`sale_transaction`.`end_date`) <= '{$end}' AND sale_transaction.status=0"));
    return $result['sum'];
}
function GetProductSum($name, $start, $end)
{
    $result = mysql_fetch_assoc(mysql_query("SELECT \t\tROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2) AS `sum`\r\n\t\t\tFROM sale_transaction\r\n\t\t\tJOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id\r\n\t\t\tJOIN production ON production.id = sale_transaction_detail.production_id\r\n\t\t\tJOIN production_category ON production.pos_menu_category = production_category.id\r\n\t\t\tWHERE production_category.`name` = '{$name}' AND production_category.`name` IS NOT NULL AND DATE(`sale_transaction`.`end_date`) >= '{$start}' AND DATE(`sale_transaction`.`end_date`) <= '{$end}' AND sale_transaction.`status` =0"));
    return $result['sum'];
}
function GetProductQuantity($name, $start, $end)
{
    $result = mysql_fetch_assoc(mysql_query("SELECT SUM(sale_transaction_detail.product_quantity) AS `sum`\r\n\t\t\tFROM sale_transaction\r\n\t\t\tJOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id\r\n\t\t\tJOIN production ON production.id = sale_transaction_detail.production_id\r\n\t\t\tJOIN production_category ON production.pos_menu_category = production_category.id\r\n\t\t\tWHERE production_category.`name` = '{$name}' AND production_category.`name` IS NOT NULL AND DATE(`sale_transaction`.`end_date`) >= '{$start}' AND DATE(`sale_transaction`.`end_date`) <= '{$end}' AND sale_transaction.`status` =0"));
    return $result['sum'];
}
switch ($action) {
    case 'get_category_sum':
        $data = array('page' => array(GetSum($start, $end), GetQuantity($start, $end)));
        break;
    case 'get_products_sum':
        $sum = GetProductSum($name, $start, $end);
        $qnt = GetProductQuantity($name, $start, $end);
        $data = array('page' => array($sum, $qnt));
        break;
    default:
        echo "Action is null!";
        break;
}
$data['error'] = $error;
echo json_encode($data);