<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
require_once('../lib/dbclass.php');





$ward = $_REQUEST['ward'];


$where = 'tbl_transaction.id>0';







if ($ward > 0) {
    $where .= " and tbl_ward.id=$ward ";
}






$data = findQuery("SELECT 
    tbl_ward.ward_name AS Ward,
    
    tbl_ward.id AS id,
   
    IFNULL(SUM(tbl_transaction.Received), 0) AS Received
FROM tbl_transaction

INNER JOIN tbl_customer ON tbl_customer.id = tbl_transaction.customerid
INNER JOIN tbl_ward ON tbl_ward.id = tbl_customer.Ward


  where tbl_transaction.donation = 1
  
  AND tbl_transaction.Received > 0
  AND tbl_transaction.status = 0
  AND tbl_transaction.Amount > 0
 AND $where
GROUP BY 
    tbl_ward.ward_name,
   
    tbl_ward.id
   
ORDER BY Received DESC
");




$datasum = findQuery("Select 
   ifnull(sum(Received),0) Received
    from tbl_transaction
    INNER JOIN tbl_customer ON tbl_customer.id = tbl_transaction.customerid
INNER JOIN tbl_ward ON tbl_ward.id = tbl_customer.Ward

WHERE tbl_transaction.donation = 1
  
  AND tbl_transaction.Received > 0
  AND tbl_transaction.status = 0
  AND tbl_transaction.Amount > 0
 AND $where ");
$totalReceived = 0;
$totalReceived = $datasum[0]['Received'];







$Totqty = 0;





if ($data) {
    $message = 'true';
    for ($i = 0;$i < count($data);$i++) {
        $data2[] = [



'ward' => $data[$i]['Ward'],

'wardid' => $data[$i]['id'],

'amount' => $data[$i]['Received'],



];
    }
} else {
    $message = 'false';
    $data2[] = ['error' => 'Not Found'];
}



echo json_encode(['Status' => $message, 'data' => $data2,'datareceived' => $totalReceived,'dataqty' => 0]);

return;

?>

