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



// 




if( isset($_POST['enddate'], $_POST['startdate'],$_POST['partner'])  
           )
{
    $partner =    $_POST['partner'] ;
    $startdate =  '2024-01-01'  ; //$_POST['startdate'] ;
    $startdate = date("Y-m-d", strtotime($startdate));

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

    $enddate=  '2026-01-01'; //$_POST['enddate'] ;
    $enddate = date("Y-m-d", strtotime($enddate));

    $query = "  SELECT   fid id ,dname, date cdate, memo ,description,loaded ,disburse ,cumbalance , sum(cumbalance) over (order BY date,fid ROWS UNBOUNDED PRECEDING )   balance 
 FROM 
       (
         SELECT  0 fid,'' dname ,'' date,'' memo ,'' loaded, '' disburse ,'Opening Balance' description,SUM(totalamount) AS cumbalance
             FROM (
                 SELECT *,  sum(totalamount) over (order BY c.myid ROWS UNBOUNDED PRECEDING )  as balance
               FROM  (   
                          SELECT * FROM (
                                          SELECT a.id myid, b.date mydate  , a.partner coa,a.total totalamount,a.transmove  FROM acctransactionsdetails a
                                          LEFT JOIN acctransactions b ON a.transid=b.id
                                          WHERE b.transtype=1 OR b.transtype=3 OR b.transtype=4 or b.transtype=6 OR  b.transtype=10 
                                                               OR b.transtype=14 OR b.transtype=16
                                          
                                          UNION 

                                          SELECT a.id myid, b.date mydate  , a.partner coa,a.total*-1 totalamount,a.transmove   FROM acctransactionsdetails a
                                          LEFT JOIN acctransactions b ON a.transid=b.id
                                          WHERE b.transtype=11 OR b.transtype=12 OR b.transtype=13 OR b.transtype=14 OR  b.transtype=17
                                     ) b  WHERE b.transmove=5
                     ) c  WHERE  coa='$partner' and  mydate   between '2022-01-01' and '$startdate'   
               )d
             UNION  
         SELECT cid fid ,dname dname, cdate DATE ,memo memo ,loaded loaded ,disburse disburse, description description,amount AS cumbalance
             FROM (  SELECT c.myid cid ,c.mydate cdate,c.loaded as loaded, c.disburse  as disburse,c.memo memo ,c.description description, c.amount amount , d.id did ,d.name dname ,
                 sum(amount ) over (order BY c.myid ROWS UNBOUNDED PRECEDING ) as balance 
                 FROM
                     (    
                        SELECT * FROM (
                                    SELECT a.id myid, b.date mydate  , a.partner coa,a.total totalamount,a.transmove,
                                    a.total amount,a.total loaded, '' disburse, b.memo memo , b.description description  
                                    FROM acctransactionsdetails a
                                    LEFT JOIN acctransactions b ON a.transid=b.id
                                    WHERE b.transtype=1 OR b.transtype=3 OR b.transtype=4 or b.transtype=6 OR  b.transtype=10 
                                                                                             OR b.transtype=14 OR b.transtype=16
                                                                        
                                    UNION 

                                    SELECT a.id myid, b.date mydate  , a.partner coa,a.total*-1 totalamount,a.transmove,
                                  a.total *-1  amount, ''  loaded ,a.total disburse,b.memo memo , b.description description  
                                  FROM acctransactionsdetails a
                                    LEFT JOIN acctransactions b ON a.transid=b.id
                                    WHERE b.transtype=11 OR b.transtype=12 OR b.transtype=13 OR b.transtype=14 OR  b.transtype=17
                                    
                                    ) b  WHERE b.transmove=5
                    ) c join bppartner d on c.coa = d.id   WHERE coa='$partner' and c.mydate  between '$firstenddate' and '$enddate' 
             )e
       ) f


             ";

}

// if(isset($_POST['order']))
// {
//  $query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
// }
// else
// {
//  $query .= 'ORDER BY date,id asc   ';
// }

// 

// if($_POST["length"] != -1)
// {
//  $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
// }

$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="' . $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['description'];

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

?>