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





$query = "  SELECT partner,SUM(amount) balance,c.name cname ,c.phone cphone,c.email cemail,c.type ctype,c.branch,d.name dname,e.name ename    FROM (
                                    SELECT a.id myid, b.date mydate  , a.partner partner,a.transmove,
                                    a.total amount 
                                    FROM acctransactionsdetails a
                                    LEFT JOIN acctransactions b ON a.transid=b.id
                                    WHERE  b.transtype=3 OR b.transtype=4 or b.transtype=6 OR  b.transtype=10 
                                                                                             OR b.transtype=7 OR b.transtype=16
                                                                        
                                    UNION 

                                    SELECT a.id myid, b.date mydate  , a.partner partner,a.transmove,
                                  a.total *-1  amount  
                                  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 
					LEFT JOIN bppartner c ON b.partner=c.id
					LEFT JOIN bppartnertype d ON c.type=d.id
					LEFT JOIN admbranch e ON c.branch =e.id
					WHERE b.transmove=5
					GROUP BY partner
";




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

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

$result = $statement->fetchAll();

$data = array();

// $mydr=0;
// $mycr=0;

foreach($result as $row)
{
  
 $actionButton = '<i class="fa fa-circle" style="color:green;" /> </i>';
 
 $sub_array = array();
 $sub_array[] =  $actionButton ;
 
 $sub_array[] = $row['dname'];
 $sub_array[] = $row['ename'];
 $sub_array[] = $row['cname'];
 $sub_array[] = $row['cphone'];
 $sub_array[] = $row['cemail'];
 $sub_array[] = $row['balance'];

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

?>




