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







//  if(isset($_POST['type'], $_POST['filter_country'],$_POST['partner'])  
//            && $_POST['type'] != '' && $_POST['filter_country'] != '' && $_POST['partner'] != '' )

//           {

         // $partner = $_POST['partner'] ;
          $item = 1 ; //$_POST['item'] ;

         
          $startdate = $_POST['sdate'] ;
          $startdate = date("Y-m-d", strtotime($startdate));

          $firstenddate = date("Y-m-d", strtotime("+1 day , $startdate"));

           //$enddate= '2025-08-25';
           $enddate= $_POST['edate'] ;
          $enddate = date("Y-m-d", strtotime($enddate));

          $query = "   SELECT  0 fid,'' dname ,'' date,'' itemin, '' itemout,'Opening Balance' description,SUM(balance) AS cumbalance
                         FROM (        
                                SELECT *,  sum(totitem) over (order BY ax.id ROWS UNBOUNDED PRECEDING )  as balance
                                FROM  ( 
                                                  SELECT a.id,a.branch branch,a.item,a.itemuom,a.itemunit,a.itemin,'' itemout,a.itemin totitem, 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 totitem, 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 totitem, 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 totitem, 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   

                              ) ax  WHERE  item='$item' and  date   between '2022-01-01' and '$startdate'
                         )a
         
                          UNION 
   
          
                         SELECT  0 fid,'' dname ,'' date,'' itemin,  itemout,'Opening Balance' description,SUM(balance) AS cumbalance
                         FROM (

                                   SELECT *, sum(totitem) over (order BY bx.id ROWS UNBOUNDED PRECEDING )  as balance
                                   FROM ( 
                                             SELECT a.id,a.branch branch,a.item,a.itemuom,a.itemunit,a.itemin,'' itemout,a.itemin totitem, 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 totitem, 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 totitem, 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 totitem, 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   

                                        ) bx  WHERE  item='$item' and  date   between '$firstenddate' and '$enddate'
                             )b
                    ";


$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="' . $row["id"] . '" /> </i></a>';
//   $explode = '<button btn-xs class="details-control" value="' . $row["id"] . '" > <i class="fa fa-plus"></i> </button> ';

 $sub_array = array();
//  $sub_array[] =  $actionButton  ;
//  $sub_array[] = $row['cdate'];
//  $sub_array[] = $row['dname'];
//  $sub_array[] = $row['loaded'];
 
//  $sub_array[] = $row['disburse'];
//  $sub_array[] = $row['balance'];
//  $sub_array[] = $row['memo'];
 $sub_array[] = $row['date'];
  $sub_array[] = $row['itemin'];
   $sub_array[] = $row['itemout'];

 $sub_array[] = $row['cumbalance'];

//  $sub_array[] = $explode ;

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



?>