<?php
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
$type = $data['type'];

if ($type == '') {
    $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went wrong try again !");
} 
else if ($type >= 0) {

    $collection_report_details = array();
    $all_collection_report = array();

    $search_value = $data['search_value'];
    $current_date = $data['current_date'];
    $start_date = $data['start_date'];
    $end_date = $data['end_date'];

    if($search_value=="chit") {
        $u_id = $data['u_id'];
        $select_query = "SELECT * from customers_chit WHERE (STR_TO_DATE(bid_date, '%d-%m-%Y') BETWEEN '$start_date' AND '$end_date') ORDER by STR_TO_DATE(bid_date, '%d-%m-%Y') ASC";
        $collection_report = $con->query($select_query);
        
        while ($collection_report_row = $collection_report->fetch_assoc()) {
            $cus_id = $collection_report_row['id'];
            $collection_report_details['id'] = $collection_report_row['id'];
            $collection_report_details['cus_name'] = $collection_report_row['cus_name'];
            $collection_report_details['cus_id'] = $collection_report_row['cus_id'];
            $collection_report_details['bid_date'] = $collection_report_row['bid_date'];
            $collection_report_details['bid_amount'] = $collection_report_row['bid_amount'];
            $collection_report_details['installment_number'] = $collection_report_row['installment_number'];
            $collection_report_details['auction_amount'] = $collection_report_row['auction_amount'];
            
            $sum_prev_paid_amount = "select sum(current_installment_amount) as prev_amount from transaction_chit where customer_id = '$cus_id'";
    		$sum_prev_paid_amount = $con->query($sum_prev_paid_amount)->fetch_assoc()['prev_amount'];
    		$sum_prev_paid_amount = $sum_prev_paid_amount == null ? 0 : $sum_prev_paid_amount;
    		$collection_report_details['previous_paid_amount'] = $sum_prev_paid_amount + $collection_report_row['previous_amount'];
    		
            $all_collection_report[] = $collection_report_details;
    }

    $returnArr = array(
        "ResponseCode" => "200",
        "Result" => "true",
        "ResponseMsg" => "Success",
        "customer_chit_list" => $all_collection_report
    );
    }

}
echo json_encode($returnArr);
mysqli_close($con);
?>