<?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'];
$club = $_REQUEST['club'];


$volunteer = (int) $_REQUEST['volunteer'];


if ($volunteer > 0) {
    $where = "tbl_transaction.id>0 and tbl_transaction.donation=1 and tbl_transaction.volunteer=$volunteer and tbl_transaction.Received>0";
} else {
    $where = 'tbl_transaction.id>0 and tbl_transaction.donation=1 and tbl_transaction.Received>0';
}


$Totrecev = 0;


if ($ward > 0) {
    $where .= " and tbl_customer.Ward=$ward";
}

if ($club > 0) {
    $where .= " and tbl_transaction.clubid=$club";
}





$data = findQuery("SELECT tbl_transaction.hideidendity,tbl_customer.name,tbl_ward.ward_name,tbl_panchayat.panchayat_name,tbl_assembly.name assembly,tbl_district.district_name,tbl_transaction.Received,tbl_transaction.dateadded,tbl_ward.id wardid,tbl_transaction.clubid from tbl_transaction
    inner join tbl_customer on tbl_customer.id=tbl_transaction.customerid left join tbl_ward on tbl_ward.id=tbl_customer.Ward left join tbl_panchayat on tbl_panchayat.id=tbl_customer.Panchayat
    left join tbl_assembly on tbl_assembly.id=tbl_customer.Assembly 
    left join tbl_district on tbl_district.id=tbl_customer.District where $where order by tbl_transaction.id desc ");

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

        if ($data[$i]['hideidendity'] == 1) {
            $data[$i]['name'] = 'xxxxxxxxxx';
        }

        if ($data[$i]['wardid'] == 0) {
            $clubid                = $data[$i]['clubid'];
            $club                  = findQuery("SELECT * from tbl_club where id=$clubid");
            $data[$i]['ward_name'] = $club[0]['club_name'];
        }


        $data2[] = [



'name'      => $data[$i]['name'],
'ward'      => $data[$i]['ward_name'] ? $data[$i]['ward_name'] : 'NA',
'panchayat' => $data[$i]['panchayat_name'] ? $data[$i]['panchayat_name'] : 'NA',
'assembly'  => $data[$i]['assembly'] ? $data[$i]['assembly'] : 'NA',
'district'  => $data[$i]['district_name'] ? $data[$i]['district_name'] : 'NA',
'amount'    => $data[$i]['Received'],
'time'      => $data[$i]['dateadded'],

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



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

return;

?>

