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




$query = "  SELECT x.branch,x.item,SUM(x.qty) balance,y.name product  FROM (                                                 
																	 SELECT a.id,a.branch branch,a.item,a.itemuom,a.itemunit,a.itemin,'' itemout , a.itemin qty,a.transmove, b.transtype,b.transmode btransmode,b.date,b.memo
                                                    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.branchto branch,a.item,a.itemuom,a.itemunit,a.itemin,'' itemout,a.itemin qty,a.transmove, b.transtype,b.transmode btransmode,b.date,b.memo
                                                    FROM acctransactionsdetails a
                                                    LEFT JOIN acctransactions b ON a.transid=b.id
                                                    WHERE b.transtype=21
                                                    
                                     UNION 

                                                    SELECT a.id,a.branch branch,a.item,a.itemuom,a.itemunit,'' itemin, a.itemout,a.itemout *-1 qty,a.transmove, b.transtype,b.transmode btransmode,b.date,b.memo
                                                    FROM acctransactionsdetails a
                                                    LEFT JOIN acctransactions b ON a.transid=b.id
                                                    WHERE b.transtype=10 OR b.transtype=18
                                                    
                                    UNION
                                                        
                                                    SELECT a.id,a.branch branch,a.item,a.itemuom,a.itemunit,'' itemin,a.itemout,a.itemout *-1 qty,a.transmove, b.transtype,b.transmode btransmode,b.date,b.memo
                                                    FROM acctransactionsdetails a
                                                    LEFT JOIN acctransactions b ON a.transid=b.id
                                                    WHERE b.transtype=21
                  )x 
                     left join item y on x.item=y.id
                     WHERE  x.transmove=1 AND x.branch='$storeID'
                     GROUP BY x.branch, x.item;
                        

        ";




$statement = $conn->prepare($query);

$statement->execute();

$number_filter_row = $statement->rowCount();

$statement = $conn->prepare($query );

$statement->execute();

$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['product'];
     $sub_array[] =  $row['product'];
     $sub_array[] =  number_format($row['balance'], 2, '.', ','); 

     $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);

?>

