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



$store =        $_POST['tstore'];  





$query = "         SELECT a.*,SUM(itemunit) tunit,SUM(itemunittotal) totalunit,b.name bname ,b.code bcode,c.name cname  FROM (  
							SELECT a.id,a.transid,a.branch,a.item,a.warehouse,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=1 OR   b.transtype=6 and b.transmode=1 OR b.transmode=2
                    
                    UNION  
                    
                     SELECT a.id,a.transid,a.branch,a.item,a.warehouse,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  AND a.transmove=1
               ) a 
               LEFT JOIN item b ON a.item=b.id
               LEFT JOIN admbranch c ON a.warehouse=c.id
                 LEFT JOIN aduom d ON b.uom=d.id
						 WHERE a.warehouse='$store'

						  GROUP BY a.item
						
            
            
                       

 ";




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

$result = $statement->fetchAll();

$data = array();



foreach($result as $row)
{
  

    $actionButton = '<i class="fa fa-circle" style="color:green;"> </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> ' ; 
    // $quantity = ' <form method="post" action="storequantity" >  <button type="submit" style="background:none;border:none;padding:0px; outline:none; " name="thisstore" value="' . $row["id"] . '"   ">  View Quantity  <i class="fa fa-arrow-right"></i>  </button></form> ' ; 
    // $movement = ' <form method="post" action="storemovement" >  <button type="submit" style="background:none;border:none;padding:0px; outline:none; " name="edit" value="' . $row["id"] . '"   ">  View Movement   <i class="fa fa-arrow-right"></i>  </button></form> ' ; 
    
    
     $sub_array = array();
     $sub_array[] =  $actionButton  ;
    //  $sub_array[] =  $edit  ;
     $sub_array[] = $row['cname'];
     $sub_array[] = $row['bname'];
     $sub_array[] = $row['bcode'];
     $sub_array[] =  number_format($row['itemunittotal'], 2) ;
     $sub_array[] = $row['itemunittotal'];
    

  
    //  $sub_array[] = $row['code'];
    //  $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);

?>

