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





$query = "   SELECT * FROM ( 
                              SELECT * FROM (
							     SELECT a.*,b.id bid,b.invnumber binvnumber,b.date bdate,b.enddate benddate,b.memo bmemo,
							     b.voucher voucher,b.description bdescription ,b.transtype,
                                         b.transaction,b.transmode,c.name cname , d.code dcode,e.name ename,
										f.name ffname,g.name gname,h.name hname     FROM  acctransactionsdetails a
                                        LEFT JOIN acctransactions b ON a.transid=b.id
                                        LEFT JOIN bppartner c ON a.partner=c.id
                                        LEFT JOIN acccurrency d ON  a.currency=d.id
                                        LEFT JOIN admbranch e on a.branch=e.id
                                        LEFT JOIN aduom f ON a.itemuom=f.id
                                        LEFT JOIN item g ON a.item = g.id
                                        LEFT JOIN adtransactiontype h ON b.transtype=h.id
                                        WHERE a.transmove=1 

										) x WHERE x.transtype=18 OR  x.transtype=10

          )h  where h.branch= '$storeID' 
          
          order by  h.bdate desc
          ";


$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>';
   $edit = ' <form method="post" action="#" >  <button type="submit" style="background:none;border:none;padding:0px; outline:none; " name="edit" value="' . $row["bid"] . '"   "> <i class="fa fa-eye" style="color:green;" ></i>  </button></form> ' ; 
   $print = ' <form method="post" action="printinvoice"  target="_blank_">  <button type="submit" style="background:none;border:none;padding:0px; font-color:green; outline:none; " name="printinvocie" value="' . $row["bid"] . '"   "> <i class="fa fa-print" style="color:green;" ></i>  </button></form> ' ; 
   $delete = ' <form method="post" action="" >  <button type="submit" style="background:none;border:none;padding:0px; font-color:red; outline:none; " name="deleteinv" value="' . $row["bid"] . '"   "> <i class="fa fa-trash" style="color:red;" ></i>  </button></form> ' ; 
     

   $sub_array = array();
   $sub_array[] =  $actionButton ;
   $sub_array[] =  $edit ;
//    $sub_array[] =  $print ;
//    $sub_array[] =  $delete ;
   $sub_array[] =  $row['ename'];
   $sub_array[] =  $row['hname'];
   $sub_array[] =  $row['bdate'];
   $sub_array[] =  $row['binvnumber'];
   $sub_array[] =  $row['bmemo'];
   $sub_array[] =  $row['cname'];
   $sub_array[] =  $row['gname'];
   $sub_array[] =  $row['ffname'];
   $sub_array[] =  number_format($row['itemout'], 2, '.', ','); 
   $sub_array[] =  number_format($row['itemprice'], 2, '.', ','); 
   $sub_array[] =  number_format($row['itemtotal'], 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);

?>




