<?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) {
    $datas = findQuery('SELECT * FROM tbl_challenge WHERE status=0 order by id desc limit 1');
}
$name      = $_REQUEST['name'];
$club      = $_REQUEST['club'];
$ward      = $_REQUEST['ward'] ? $_REQUEST['ward'] : 0;
$district  = $_REQUEST['district'] ? $_REQUEST['district'] : 0;
$assembly  = $_REQUEST['assembly'] ? $_REQUEST['assembly'] : 0;
$panchayat = $_REQUEST['panchayat'] ? $_REQUEST['panchayat'] : 0;

$checking = findQuery('SELECT * FROM `settings`');

if ($checking) {
    $version = $checking[0]['version'];

    if ($version > 5) {
        $where = 'tbl_transaction.id>0 and tbl_transaction.sponsorship=1 and tbl_transaction.Received>0';
    } else {
        $where = "tbl_transaction.id>0 and tbl_transaction.sponsorship=1 and tbl_transaction.Received>0 and DATE_FORMAT(dateadded, '%Y-%m-%d')=CURDATE()";
    }
}


if ($name != '') {
    $where .= " and (tbl_customer.name like '%$name%' or tbl_customer.Mobile like '%$name%') ";
}

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

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

if ($district > 0) {
    $where .= " and tbl_customer.District=$district";
}

if ($assembly > 0) {
    $where .= " and tbl_customer.Assembly=$assembly";
}

if ($panchayat > 0) {
    $where .= " and tbl_customer.Panchayat=$panchayat";
}

$Totrecev = 0;
$Totpen   = 0;
$Totqty   = 0;






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



    $data = findQuery("SELECT tbl_transaction.id,tbl_transaction.hideidendity,tbl_transaction.Amount,tbl_customer.name,tbl_customer.Photo,tbl_ward.ward_name,tbl_panchayat.panchayat_name,tbl_assembly.name assembly,tbl_district.district_name,
    tbl_transaction.Received,DATE_FORMAT(tbl_transaction.dateadded,'%d-%m-%y %H:%i:%s')orderedtime 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 tbl_transaction.challengeid=$id and $where order by tbl_transaction.id desc ");

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

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

            $id       = $data[$i]['id'];
            $dataprod = findQuery("SELECT tbl_transactiondetails.qty
													FROM `tbl_transaction` inner join tbl_transactiondetails on tbl_transaction.id=tbl_transactiondetails.hdrid 
													where tbl_transactiondetails.hdrid=$id");
            $quantity = 0;

            if ($dataprod) {
                $quantity = $dataprod[0]['qty'];
            }
            $Totqty += $quantity;
            $image = $data[$i]['Photo'];

            if ($image != '') {
                $image = 'https://kozhikodechcentre.in/API/images/' . $data[$i]['Photo'];
            } else {
                $image = '';
            }


            $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'],
'image'     => $image,
'count'     => $quantity . ' KG',
'date'      => $data[$i]['orderedtime'],
];
        }
    }
} else {
    $message = 'false';
    $data2[] = ['error' => 'Not Found'];
}



if ($assembly == 0 && $panchayat == 0 && $ward == 0 && $district == 0) {
    $Totrecev = 0;
    $Totpen   = 0;
    $Totqty   = 0;
} elseif ($district > 0 && ($assembly == 0 && $panchayat == 0 && $ward == 0)) {
    $Totrecev = 0;
    $Totpen   = 0;
    $Totqty   = 0;
} else {
}

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

return;

?>

