<?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'];

$volunteer = $_REQUEST['volunteer'];

$where    = "tbl_transaction.id>0 and tbl_transaction.donation=1 and tbl_transaction.volunteer=$volunteer";
$where1   = "tbl_transaction.id>0 and tbl_transaction.donation=1 and tbl_transaction.volunteer=$volunteer";
$Totrecev = 0;
$Totpen   = 0;


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




//echo $where;
$data = findQuery("SELECT  tbl_transaction.volunteer,tbl_transaction.id,tbl_transaction.Amount,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 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 inner join tbl_panchayat on tbl_panchayat.id=tbl_customer.Panchayat
    inner join tbl_assembly on tbl_assembly.id=tbl_customer.Assembly 
    inner join tbl_district on tbl_district.id=tbl_customer.District where $where
    
");


function array_msort($array, $cols)
{
    $colarr = [];

    foreach ($cols as $col => $order) {
        $colarr[$col] = [];

        foreach ($array as $k => $row) {
            $colarr[$col]['_' . $k] = strtolower($row[$col]);
        }
    }
    $eval = 'array_multisort(';

    foreach ($cols as $col => $order) {
        $eval .= '$colarr[\'' . $col . '\'],' . $order . ',';
    }
    $eval = substr($eval, 0, -1) . ');';
    eval($eval);
    $ret = [];

    foreach ($colarr as $col => $arr) {
        foreach ($arr as $k => $v) {
            $k = substr($k, 1);

            if (!isset($ret[$k])) {
                $ret[$k] = $array[$k];
            }
            $ret[$k][$col] = $array[$k][$col];
        }
    }

    return $ret;
}
$data = array_msort($data, ['id' => SORT_DESC]);

$data = array_values($data);




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


        if ($volunteer > 0) {
            if ($data[$i]['volunteer'] == $volunteer) {
                $volunteerthrough = 1;
            }
        }



        $Totrecev += $data[$i]['Received'];
        $Totpen   += $data[$i]['Amount'];

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


'id'           => $data[$i]['id'],
'name'         => $data[$i]['name'],
'ward'         => $data[$i]['ward_name'],
'panchayat'    => $data[$i]['panchayat_name'],
'assembly'     => $data[$i]['assembly'],
'district'     => $data[$i]['district_name'],
'amount'       => $data[$i]['Received'],
'payable'      => $data[$i]['Amount'],
'caneditorder' => $volunteerthrough,


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



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

return;

?>

