<?php  include_once('../../config/db.php');  //include_once('../config/check.php'); 






$query = "        SELECT item,y.warehouse whouse ,SUM(itemunit) totalunit, SUM(itemunittotal) totalbal,
        z.name zname,z.code zcode, z.description zdescription,x.name xname 
		 FROM (
         		SELECT a.id,a.branch,a.warehouse,a.item,a.itemuom,a.itemunit,a.itemunittotal,a.transmove, b.transtype,b.transmode btransmode
               FROM acctransactionsdetails a
               LEFT JOIN acctransactions b ON a.transid=b.id
               WHERE b.transtype=6 OR b.transtype=1 
      
                UNION 
                    
                  SELECT a.id,a.branch,a.warehouse,a.item,a.itemuom,a.itemunit*-1,a.itemunittotal*-1,a.transmove, b.transtype,b.transmode btransmode
                  FROM acctransactionsdetails a
                  LEFT JOIN acctransactions b ON a.transid=b.id
                   WHERE b.transtype=10  OR b.transtype=18 OR b.transtype=19          
                    
             ) y   JOIN item z ON y.item=z.id
                   LEFT JOIN admbranch x ON y.warehouse=x.id
				      
				  GROUP BY item,whouse
        ";




$statement = $conn->prepare($query);
$statement->execute();
$number_filter_row = $statement->rowCount();

$result = $statement->fetchAll();

$data = array();



foreach($result as $row)
{
  

    $actionButton = '<input type="radio" name="myradio" class="myradio" value="" /> </i></a>';
    //$edit = ' <form method="post" action="invcategorye" >  <button type="submit" style="background:none;border:none;padding:0px; outline:none; " name="edit" value="' . $row["id"] . '"   "> <i class="fa fa-edit"></i>  </button></form> ' ; 
    
     $sub_array = array();
     $sub_array[] =  $actionButton  ;
     $sub_array[] = $row['xname'];
     $sub_array[] = $row['zname'];
     $sub_array[] = $row['zcode'];
     $sub_array[] = $row['zdescription'];
     $sub_array[] =  $row['totalbal']  ;
    //  $sub_array[] = $row['iut'];
     
    //  $sub_array[] = $row['description'];
     $data[] = $sub_array;
     
 

}

function count_all_data($conn,$query)
{
 $statement = $conn->prepare($query);
 $statement->execute();
 return $statement->rowCount();
}

$output = array(
                       "draw"       =>  $number_filter_row ,
                        "recordsTotal"   =>  count_all_data($conn,$query),
                        "recordsFiltered"  =>  $number_filter_row,
                        "data"       =>  $data
);

echo json_encode($output);

?>

