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


$datas = findQuery('SELECT * FROM tbl_challenge WHERE status=0 AND CURDATE() between challenge_startdate and challenge_enddate');











if ($datas) {
    $id = $datas[0]['id'];



    $data = findQuery("SELECT sum(tbl_transactiondetails.Totqty) Received,tbl_panchayat.panchayat_name,tbl_panchayat.id FROM `tbl_transaction`
    inner join tbl_transactiondetails on tbl_transactiondetails.hdrid=tbl_transaction.id
    inner join tbl_customer on tbl_customer.id=tbl_transaction.customerid
    inner join tbl_panchayat on tbl_panchayat.id=tbl_customer.Panchayat
    where tbl_transaction.challengeid=$id and tbl_transaction.sponsorship=0  group by tbl_panchayat.panchayat_name,tbl_panchayat.id having Received>0 order by Received desc");

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



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


];
        }
    }
} else {
    $data = findQuery('SELECT sum(tbl_transaction.Received) Received,tbl_panchayat.panchayat_name,tbl_panchayat.id FROM `tbl_transaction` inner join tbl_customer on tbl_customer.id=tbl_transaction.customerid inner join tbl_panchayat on tbl_panchayat.id=tbl_customer.Panchayat
    where tbl_transaction.donation=1 and Received>0 group by tbl_panchayat.panchayat_name,tbl_panchayat.id order by Received desc');

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



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


];
        }
    }
}

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

return;

?>

