<?php
include "../library.php";

$obj=new Library();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

$row_id=@$_POST['row_id'];


}else{

$row_id=@$_GET['row_id'];

}
$array = json_decode($row_id, true ); 
$row_id=$array['row_id'];
$c_id=$array['c_id'];
$code="  ";
if($c_id>0){
$code=" AND A.contractor_id = $c_id ";
}



 $sqln3="select * from (
SELECT 
A.type,
 CASE 
WHEN A.type=0 THEN '5'
WHEN A.type=1 THEN '1'
WHEN A.type=3 THEN '3'
end As Typeorder
, A.credit Amount
,  IFNULL(B.c_name ,D.lab_name) as name
, DATE_FORMAT(A.t_date,'%d-%m-%Y') as day
,R.c_name contrct_name
FROM payment_masters  A
inner JOIN contrct_or_sup_masters R on R.c_id=A.contractor_id
left join contrct_or_sup_masters B ON B.c_id = A.party_id AND A.type in (0,1)
left join labour_masters D ON D.lab_id = A.party_id AND A.type in (3)
WHERE  A.status=0 AND A.type in (0,1,3) AND A.trans_type=1 AND A.po_id='$row_id' $code

UNION ALL
SELECT 
B.type,
 CASE 
WHEN B.type=0 THEN '6'
WHEN B.type=1 THEN '2'
WHEN B.type=3 THEN '4'
end As Typeorder
,
 SUM(B.credit) Amount,
CASE 
WHEN B.type=0 THEN 'Total Contractor Expense'
WHEN B.type=1 THEN 'Total Supplier Expense'
WHEN B.type=3 THEN 'Total Labour Expense'
end  as name,
"" as day,
' ' contrct_name
FROM payment_masters  B
inner JOIN contrct_or_sup_masters R on R.c_id=B.contractor_id
WHERE  B.status=0 AND B.trans_type=1 AND B.po_id='$row_id'  AND B.type in(0,1,3)  $code
group by B.type
    
)X order by X.Typeorder,X.day ASC


;";

$ret=$obj->generalquery($sqln3);
$data=mysqli_fetch_all($ret,MYSQLI_ASSOC);







echo json_encode(array('edit'=>$data,'sq'=>$sqln3)); 
?>

