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






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










if ($data) {
    $message  = 'true';
    $dataqty  = findQuery('SELECT sum(Received) Received  FROM  tbl_transaction where  tbl_transaction.Received>0');
    $units    = 0;
    $dataqty1 = findQuery('SELECT *   FROM  tbl_transaction where tbl_transaction.Received>0');

    if ($dataqty[0]['Received'] > 0) {
        $units   = count($dataqty1);
        $data2[] = [



'Count'       => (string) $units,
'Description' => 'Donation',
'unit'        => 'Donors',
'Received'    => $dataqty[0]['Received'],
];
    } else {
        $units   = 0;
        $data2[] = [



'Count'       => (string) $units,
'Description' => 'Donation',
'unit'        => 'Donors',
'Received'    => 0,
];
    }
} else {
    $message = 'true';
    $dataqty = findQuery('SELECT sum(Received) Received  FROM  tbl_transaction where donation=1 and tbl_transaction.Received>0');
    $datadon = findQuery('SELECT *  FROM  tbl_transaction where donation=1 and tbl_transaction.Received>0');
    $units   = 0;

    if ($dataqty[0]['Received'] > 0) {
        $units   = count($datadon);
        $data2[] = [



'Count'       => (string) $units,
'Description' => 'Donation',
'unit'        => 'Donors',
'Received'    => $dataqty[0]['Received'],
];
    } else {
        $data = findQuery('SELECT * FROM tbl_challenge WHERE status=0 order by id desc limit 1');

        $message = 'true';
        $id      = $data[0]['id'];


        $units = 0;






        $dataqty1 = findQuery("SELECT ifnull(sum(Totqty),0) QTY FROM `tbl_transactiondetails` inner join tbl_transaction on tbl_transaction.id=tbl_transactiondetails.hdrid
    where tbl_transaction.challengeid=$id and tbl_transaction.sponsorship=0 and tbl_transaction.Received>0");

        $dataamt = findQuery("SELECT ifnull(sum(Received),0) Received FROM tbl_transaction
    where tbl_transaction.challengeid=$id and tbl_transaction.Received>0");




        $units = (string) ($dataqty1[0]['QTY']);






        $data2[] = [



'Count'       => $units,
'Description' => 'Challenge',
'unit'        => 'Packets',
'Received'    => $dataamt[0]['Received'],
];
    }
}

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

return;

?>

