<?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();
 
    $mode = $data['mode'];
    $search_user = $data['search_user'];
    $search_value = $data['search_value'];
    $start_date = $data['start_date'];
    $end_date = $data['end_date'];
    $isReceiptDelete = false;
    // {"type":"0","search_value":"chit","search_user":"All","start_date":"2024-08-18","end_date":"2024-08-18","receipt_no":"REC-C-0000163","cus_id":"48","mode":4}
    if($mode == '4') {
        $receipt_no = $data['receipt_no'];
        $cus_id = $data['cus_id'];
        
        $cy = $con->query("select * from transaction_chit where `customer_id`= '$cus_id' and `receipt_number`= '$receipt_no' and status ='1'");
        if ($row = $cy->fetch_assoc()) {
            $isReceiptDelete = true;
            $created_by = $row['created_by'];
    		$current_installment_number = $row['current_installment_number'];
    		$receipt_date = $row['receipt_date'];
    		$current_installment_amount = $row['current_installment_amount'];
     		$today_paid_amount = $row['today_paid_amount'];
     		$balance_amount = $row['balance_amount'];
    		$prev_paid_amount = $row['prev_paid_amount'];
    		$prev_balance_amount = $row['prev_balance_amount'];
    		$pay_mode = $row['pay_mode'];
    		$status = $row['status'];
    		$created_time = $row['created_time'];
    		
    		$new_transaction = "insert into del_transaction_chit
                                                (`created_by`,`customer_id`,`receipt_date`,`receipt_number`,`current_installment_number`,`current_installment_amount`,
                                                `today_paid_amount`,`balance_amount`,`prev_paid_amount`,`prev_balance_amount`,`status`,`pay_mode`,
                                                `created_time`)values
                                                ('$created_by','$cus_id','$receipt_date','$receipt_no','$current_installment_number','$current_installment_amount',
                                                '$today_paid_amount','$balance_amount','$prev_paid_amount','$prev_balance_amount','$status','$pay_mode',
                                                '$created_time')";
            $result = $con->query($new_transaction);
            if($result) {
                $cy = $con->query("select * from last_installment_details where id = '$cus_id' and status = '1'");
                $insert = false;
                if ($roww = $cy->fetch_assoc()) {
                    $install_number = $roww['installment_number'];
	                $install_balance_amount= $roww['installment_balance_amount'];
	                
	                $update_sub_sql = "update customers_chit set `installment_number`='$install_number',
	                        `installment_balance_amount`='$install_balance_amount' where id = '$cus_id' and customer_status = '1'";
                    $result_sub = $con->query($update_sub_sql);
                }
                    
                $sql = "DELETE FROM `transaction_chit` WHERE `customer_id`= '$cus_id' and `receipt_number`= '$receipt_no'";
                $results = mysqli_query($con,$sql);
                if($results) {
                    
                    // $update_prev_transaction_status = "update transaction_chit set  `status`='1'
                    //                                             where customer_id = '$cus_id' order by id desc limit 1";
                    // $con->query($update_prev_transaction_status);
                } else {
                    $returnArr = array(
                        "ResponseCode" => "401",
                        "Result" => "false",
                        "ResponseMsg" => "Error",
                        "data" => $all_collection_report,
                        "total_paid_amount" => $total_paid_amount);
                }
            }
        }
        else {
            $isReceiptDelete = false;
            $returnArr = array(
                "ResponseCode" => "401",
                "Result" => "false",
                "ResponseMsg" => "Can't Delete this Receipt No",
                "data" => $all_collection_report,
                "total_paid_amount" => $total_paid_amount);
            echo json_encode($returnArr);
            return;
        }
    }
    else {
        $isReceiptDelete = true;
    }

    if($mode == '0')
    {
        $string_if_all_selected = "";
    }
    else
    {
        if($search_user == 'All')
        {
            $string_if_all_selected = "";
        }
        else
        {
            $string_if_all_selected = " app_users.user_name like '%$search_user%' and ";
        }
    }
    
    if($search_value=="chit"){
        if($type == "3") {
            $u_id = $data['u_id'];
            $select_query = "SELECT customers_chit.id as c_id,customers_chit.cus_nick_name as c_nick_name,customers_chit.installment_number as ins_no,
                                customers_chit.installment_balance_amount as ins_bal,
                                customers_chit.cus_id as c_cus_id,app_users.user_name as u_name,
                                transaction_chit.receipt_date as t_r_d,transaction_chit.receipt_number as t_r_n,transaction_chit.pay_mode as p_m,
                                transaction_chit.today_paid_amount as t_p_a,transaction_chit.balance_amount as b_a,transaction_chit.status as c_i_s
		                    FROM transaction_chit
				                INNER JOIN customers_chit ON
                			                customers_chit.id = transaction_chit.customer_id
                                INNER JOIN app_users on 
                			                app_users.id = transaction_chit.created_by
                            WHERE app_users.status = '1' and app_users.id = '$u_id' and " . $string_if_all_selected . " customers_chit.customer_status <> 0 AND
                                    (STR_TO_DATE(transaction_chit.receipt_date, '%d-%m-%Y') BETWEEN '$start_date' AND '$end_date')
                                    GROUP BY transaction_chit.receipt_number
                                    ORDER by STR_TO_DATE(transaction_chit.receipt_date, '%d-%m-%Y') DESC;";
        } else {
        $select_query = "SELECT customers_chit.id as c_id,customers_chit.cus_nick_name as c_nick_name,customers_chit.installment_number as ins_no,
                                customers_chit.installment_balance_amount as ins_bal,
                                customers_chit.cus_id as c_cus_id,app_users.user_name as u_name,
                                transaction_chit.receipt_date as t_r_d,transaction_chit.receipt_number as t_r_n,transaction_chit.pay_mode as p_m,
                                transaction_chit.today_paid_amount as t_p_a,transaction_chit.balance_amount as b_a,transaction_chit.status as c_i_s
		                    FROM transaction_chit
				                INNER JOIN customers_chit ON
                			                customers_chit.id = transaction_chit.customer_id
                                INNER JOIN app_users on 
                			                app_users.id = transaction_chit.created_by
                            WHERE app_users.status = '1' and " . $string_if_all_selected . " customers_chit.customer_status <> 0 AND
                                    (STR_TO_DATE(transaction_chit.receipt_date, '%d-%m-%Y') BETWEEN '$start_date' AND '$end_date')
                                    GROUP BY transaction_chit.receipt_number
                                    ORDER by STR_TO_DATE(transaction_chit.receipt_date, '%d-%m-%Y') DESC;";
        }

        $collection_report = $con->query($select_query);


    }
    else if($search_value == "finance"){
        $select_query = "SELECT customers_finance.id as c_id,customers_finance.cus_nick_name as c_nick_name,
                                customers_finance.cus_id as c_cus_id,
                                app_users.user_name as u_name,
                                transaction_finance.receipt_date as t_r_d,
                                transaction_finance.today_paid_amount as t_p_a
                            FROM transaction_finance
                                INNER JOIN customers_finance ON
                                            customers_finance.id = transaction_finance.customer_id
                                INNER JOIN app_users on 
                                            app_users.id = transaction_finance.created_by
                            WHERE app_users.status = '1' and " . $string_if_all_selected . " customers_finance.customer_status<>0 AND
                                    (STR_TO_DATE(transaction_finance.receipt_date, '%d-%m-%Y') BETWEEN '$start_date' AND '$end_date')
                                    ORDER by STR_TO_DATE(transaction_finance.receipt_date, '%d-%m-%Y') DESC;";

        $collection_report = $con->query($select_query);

    }
    else if($search_value == "interest"){
        $select_query = "SELECT customers_interest.id as c_id,customers_interest.cus_nick_name as c_nick_name,
                                customers_interest.cus_id as c_cus_id,
                                app_users.user_name as u_name,
                                transaction_interest.receipt_date as t_r_d,
                                transaction_interest.today_paid_amount as t_p_a
                            FROM transaction_interest
                                    INNER JOIN customers_interest ON
                                                customers_interest.id = transaction_interest.customer_id
                                    INNER JOIN app_users on 
                                                app_users.id = transaction_interest.created_by
                            WHERE app_users.status = '1' and " . $string_if_all_selected . " customers_interest.customer_status<>0 AND
                                    (STR_TO_DATE(transaction_interest.receipt_date, '%d-%m-%Y') BETWEEN '$start_date' AND '$end_date')
                                    ORDER by STR_TO_DATE(transaction_interest.receipt_date, '%d-%m-%Y') DESC;";

        $collection_report = $con->query($select_query);
    }
    $total_paid_amount = 0;
    while ($collection_report_row = $collection_report->fetch_assoc()) {
        $collection_report_details['customer_id'] = $collection_report_row['c_id'];
        $collection_report_details['customer_name'] = $collection_report_row['c_nick_name'];
        $collection_report_details['customer_cus_id'] = $collection_report_row['c_cus_id']; 
        $collection_report_details['installment_number'] = $collection_report_row['ins_no'];
        $collection_report_details['installment_balance_amount'] = $collection_report_row['ins_bal']; 
        $collection_report_details['transaction_date'] = $collection_report_row['t_r_d'];
        $collection_report_details['receipt_number'] = $collection_report_row['t_r_n'];
        $collection_report_details['receipt_date'] = $collection_report_row['t_r_d'];
        $collection_report_details['transaction_paid_amount'] = $collection_report_row['t_p_a'];
        $collection_report_details['transaction_balance_amount'] = $collection_report_row['b_a'];
        $collection_report_details['transaction_created_by'] = $collection_report_row['u_name'];
        $collection_report_details['pay_mode'] = $collection_report_row['p_m'];
        $collection_report_details['status'] = $collection_report_row['c_i_s'];
        $total_paid_amount += $collection_report_row['t_p_a'];

        $all_collection_report[] = $collection_report_details;
    }

    $returnArr = array(
        "ResponseCode" => "200",
        "Result" => $isReceiptDelete,
        "ResponseMsg" => $select_query,
        "data" => $all_collection_report,
        "total_paid_amount" => $total_paid_amount
    );

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