<?php
include("../public/impOpreation.php");
$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");
if (empty($do)) {  
    $smarty->display("header.html");
    $smarty->display("salesreturnsratingsreportview/show.html");
    $smarty->display("footer.html");
    
} else if ($do == "getdata") { 
    $start_date = filter_input(INPUT_POST, 'start_date');  
    $end_date = filter_input(INPUT_POST, 'end_date');  
    $categoryid =  filter_input(INPUT_POST, 'categoryid');  
    $clientQuery = '';
    $sellQuery = '';
    $returnQuery = '';
    if($start_date != '' && $end_date != ''){
        $sellQuery .=' and  sellbill.sellbilldate  >= "' . $start_date . ' 00-00-00" and sellbill.sellbilldate <= "' . $end_date . ' 23-59-55" ';
        $returnQuery .=' and  returnsellbill.returnsellbilldate  >= "' . $start_date . ' 00-00-00" and returnsellbill.returnsellbilldate <= "' . $end_date . ' 23-59-55" ';
    }

    
    
    if($categoryid){
        $productcat = R::getRow('SELECT * FROM productcat  where productCatId =  ' . $categoryid );
        $sellbill = R::getAll('SELECT * FROM sellbilldetail  JOIN sellbill ON sellbilldetail.sellbillid = sellbill.sellbillid
            where sellbill.conditions = 0 and sellbilldetail.sellbilldetailcatid  = ' . $categoryid . $sellQuery);
        $returnsellbill = R::getAll('SELECT *  FROM returnsellbilldetail  
                JOIN returnsellbill ON returnsellbilldetail.returnsellbillid = returnsellbill.returnsellbillid
                where returnsellbill.conditions = 0  and returnsellbilldetail.returnsellbilldetailcatid = ' . $categoryid . $returnQuery);
        $smarty->assign('productcat', $productcat);
        $smarty->assign('sellbill', $sellbill);
        $smarty->assign('returnsellbill', $returnsellbill);
        $smarty->display("salesreturnsratingsreportview/getcategory.html");  
    }else{
        $productcats = R::getAll('SELECT * FROM productcat where productcat.conditions = 0 ');
        $productcatsdatas =[];
        foreach ($productcats as $productcat) {
            $sellbill = R::getRow('SELECT sum(sellbilldetailtotalprice) as total ,  sum(sellbilldetailquantity) as qunt FROM sellbilldetail 
            JOIN sellbill ON sellbilldetail.sellbillid = sellbill.sellbillid
            where sellbill.conditions = 0 and sellbilldetail.sellbilldetailcatid =  ' . $productcat['productCatId'] . $sellQuery);
            $returnsellbill = R::getRow('SELECT sum(returnsellbilldetailtotalprice) as total ,  sum(returnsellbilldetailquantity) as qunt FROM returnsellbilldetail  
                JOIN returnsellbill ON returnsellbilldetail.returnsellbillid = returnsellbill.returnsellbillid
                where returnsellbill.conditions = 0  and returnsellbilldetail.returnsellbilldetailcatid = ' .  $productcat['productCatId'] . $returnQuery);
            $productcatsdatas[] =[
                'productCatName' => $productcat['productCatName'],
                'squnt' => $sellbill['qunt'],
                'stotal' => $sellbill['total'],
                'rqunt' => $returnsellbill['qunt'],
                'rtotal' => $returnsellbill['total'],
            ];
            
        }
        $smarty->assign('productcatsdatas', $productcatsdatas);
        $smarty->display("salesreturnsratingsreportview/getdata.html");  
        
            
      
       
        
    }

   
   
}else if ($do == "select2category") {
    select2category();
}
function select2category() {
    $name = $_POST['searchTerm'];
    $productsData = R::getAll("SELECT productCatId, productCatName
    FROM productcat WHERE conditions = 0 and productCatName LIKE '%" . $name . "%' limit 50");
    $return_arr = array();
    foreach ($productsData as $pro) {
        $row_array = array();
        $row_array['id'] = $pro['productCatId'];
        $row_array['text'] = $pro['productCatName'];
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
}
?>

