<?php 
 
// array for JSON response
$response = array();
 
 require_once __DIR__ . '/catchexception.php';
// include db connect class
require_once __DIR__ . '/db_config.php';
  $json = file_get_contents('php://input');
 $obj = json_decode($json,true);

$deviceid = $obj['deviceid'];
$clientcode=$obj['clientcode'];
$fromdate=$obj['fromdate'];
$todate=$obj['todate'];
$sortby=$obj['sortby'];
$sortorder=$obj['sortorder'];
$branchcode=$obj['branchcode'];

// $paraimeino ='f4f8ffbef470be87'; 
try{
if ($sortby =="value")
{
$sortby=" round(cast(sum(amount) as numeric),2) ";
}
else
{
  $sortby=" round(cast(sum(qty) as numeric),2) ";
}
if($branchcode == '0')
{
   $filterBranch = "1=1";
}
else{
 $filterBranch = "a.branchcode=$branchcode";
}
if ($deviceid !="" ) {
    
     

    $paraquery="
    select sum(qty) as qty, round(sum(cast(amount as numeric)),2) as salesvalue,b.itemgroupcode, c.itemgroupname  
    from tblsalesitemdetails as a  inner join tblitemmaster as b on a.itemcode=b.itemcode inner join 
      tblitemgroupmaster as c on b.itemgroupcode=c.itemgroupcode   where a.transactiondate between $1 and $2 and a.clientcode=$3 and ". $filterBranch."
            group by b.itemgroupcode,c.itemgroupname    order by ". $sortby."  ". $sortorder ;
 
  $resultmax=  pg_prepare($con,"paraquery",$paraquery);
      $data = [$fromdate,$todate,$clientcode];
      $result = pg_execute($con,"paraquery",$data);
      
    if (!empty($result)) {
        // check for empty result
        if (pg_num_rows($result) > 0) {
      // user node
            $response["itemgroupdetails"] = array();
 
            while ($row = pg_fetch_assoc($result)) {        
              $report = array();
                               
                $report["itemgroupname"] = $row["itemgroupname"];
                $report["salesvalue"] = $row["salesvalue"];
                $report["itemgroupcode"] = $row["itemgroupcode"];
			          $report["qty"] = $row["qty"];
                           
              // push single product into final response array
              array_push($response["itemgroupdetails"], $report);
            }
            // success
            $response["success"] = 1;
 
            // echoing JSON response
            echo  json_encode($response,JSON_UNESCAPED_UNICODE);


        } else {
            // no user found
            $response["success"] = 0;
            $response["message"] = "No Report found";
 
            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no user found
        $response["success"] = 0;
        $response["message"] = "No found";
 
        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
     echo  json_encode($response,JSON_UNESCAPED_UNICODE);
  }
  mch_db_release($con);
}
//catch exception
catch(Exception $e) {
mch_db_release($con);
echo 'Message: ' .$e->getMessage();
}
?>
