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

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

    $statement_details = array();
    $all_statement = array();

    if ($cus_type == "chit") {
        $view_statement_query = $con->query("select customers_chit.cus_nick_name as c_name,customers_chit.cus_id as c_id,customers_chit.cus_type as c_t,
		                                            customers_chit.current_chit_amount as c_chit_amount,customers_chit.scheme as c_scheme,
                                                    customers_chit.auction_amount as c_auc_amount,customers_chit.installment_number as c_cur_ins,
                                                    customers_chit.multiply_number as c_m_n,
                                                    app_users.user_name as u_name,
                                                    installment_entry.installment_amount as original_installment_amount,
                                                    transaction_chit.current_installment_number c_i_n,transaction_chit.current_installment_amount as c_i_a,transaction_chit.status as c_i_s,
                                                    transaction_chit.updated_time as g_d,transaction_chit.id as t_id,transaction_chit.receipt_date as r_d
                                                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
                                                    INNER JOIN installment_entry 
                                                        on installment_entry.installment_number=transaction_chit.current_installment_number
                                                        and installment_entry.scheme=customers_chit.scheme
                                            where app_users.status='1' and customers_chit.id='$cus_id'
                                                    ORDER by transaction_chit.updated_time ASC");
        $result = $view_statement_query->num_rows;

    } 
    else if ($cus_type == "finance") {
        $view_statement_query = $con->query("select customers_finance.cus_nick_name as c_name,customers_finance.cus_id as c_id,customers_finance.cus_type as c_t,
                                                    customers_finance.payment_amount as c_payment_amount,customers_finance.days as c_days,
                                                    customers_finance.settlement_amount as c_set_amount,
		                                            app_users.user_name as u_name,
                                                    transaction_finance.today_paid_amount p_a,transaction_finance.receipt_date as r_d,
                                                    transaction_finance.updated_time as g_d,transaction_finance.id as t_id
		                                        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 customers_finance.id='$cus_id'
                                                GROUP BY transaction_finance.receipt_number
                                                ORDER by transaction_finance.updated_time ASC ");
        $result = $view_statement_query->num_rows;

    } else if ($cus_type == "interest") {

        $view_statement_query = $con->query("select customers_interest.cus_nick_name as c_name,customers_interest.cus_id as c_id,customers_interest.cus_type as c_t,
                                                    customers_interest.investment_amount as c_invest_amount,customers_interest.roi as c_roi,
                                                    customers_interest.closing_date as c_closing_date,customers_interest.interest_amount as c_int_amount,
		                                            app_users.user_name as u_name,
                                                    transaction_interest.today_paid_amount p_a,transaction_interest.receipt_date as r_d,
                                                    transaction_interest.updated_time as g_d,transaction_interest.id as t_id
		                                        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 customers_interest.id='$cus_id'
                                                GROUP BY transaction_interest.receipt_number
                                                ORDER by transaction_interest.updated_time ASC ");
        $result = $view_statement_query->num_rows;


    }

    $get_once = 1;
    $customer_name = "";
    $customer_id = "";
    $customer_type = "";

    $chit_amount = 0;
    $scheme = 0;
    $auction_amount = 0;
    $multiply_number = 0;

    $installment_number = 0;
    $current_total = 0;
    $count = 0;

    $prev_installment_number = 0;
    $payment_amount = 0;
    $days = 0;
    $settlement_amount = 0;


    $investment_amount = 0;
    $roi = 0;
    $interest_amount = 0;
    $closing_date = "";

    $total_paid_amount = 0;
    $no_of_installment = 0;

    $individual_installment_details = array();
    $all_individual_installment = array();

    while ($result!=0 &&  $statement_row = $view_statement_query->fetch_assoc()) {
        if ($get_once == 1) {
            $customer_name = $statement_row['c_name'];
            $customer_id = $statement_row['c_id'];
            $customer_type = $statement_row['c_t'];

            if ($cus_type == "chit") {
                $chit_amount = $statement_row['c_chit_amount'];
                $scheme = $statement_row['c_scheme'];
                $auction_amount = $statement_row['c_auc_amount'];
                $multiply_number =$statement_row['c_m_n'];
                $installment_number = $statement_row['c_cur_ins'];
                $prev_installment_number = $statement_row['c_i_n'];

            } else if ($cus_type == "finance") {
                $payment_amount = $statement_row['c_payment_amount'];
                $days = $statement_row['c_days'];
                $settlement_amount = $statement_row['c_set_amount'];

            } else {
                $investment_amount = $statement_row['c_invest_amount'];
                $roi = $statement_row['c_roi'];
                $interest_amount = $statement_row['c_int_amount'];
                $closing_date = date('d-m-Y', strtotime($statement_row['c_closing_date']));
            }

            $get_once = 0;
        }

        if ($cus_type == "chit") {
            $count += 1;
            $current_installment_number = $statement_row['c_i_n'];
            $current_installment_amount = $statement_row['c_i_a'];
            $current_status = $statement_row['c_i_s'];
            $original_installment_amount = $statement_row['original_installment_amount']* $multiply_number;


            if ($prev_installment_number != $current_installment_number) {
                
                $individual_installment_details['installment_number'] = $prev_installment_number;
                $individual_installment_details['installment_details'] = $all_individual_installment_statement;
                // $all_individual_installment[]=$individual_installment_details;
                $all_statement[] = $individual_installment_details;

                $individual_installment_statement = array();
                $all_individual_installment_statement = array();
                $individual_installment_details = array();
                $all_individual_installment = array();

                $current_total = 0;

            }

            $current_total += $current_installment_amount;
            $total_paid_amount += $current_installment_amount;
            // $individual_installment_statement['transaction_date'] = date('d-m-Y', strtotime($statement_row['g_d']));
            $individual_installment_statement['transaction_date'] = $statement_row['r_d'];

            // $individual_installment_statement['installment_number'] = $current_installment_number;
            $individual_installment_statement['transaction_paid_amount'] = $current_installment_amount;
            $individual_installment_statement['transaction_balance_amount'] = $current_total;
            $individual_installment_statement['status'] = $current_status;
            $individual_installment_statement['transaction_created_by'] = $statement_row['u_name'];

            $prev_installment_number = $current_installment_number;

            $all_individual_installment_statement[] = $individual_installment_statement;
            $individual_installment_details['installment_balance'] = $original_installment_amount - $current_total;


            if($count==$result){
                // $statement_details[$prev_installment_number] = $all_individual_installment;
                // $all_statement[] = $statement_details;
                // $individual_installment_details[$prev_installment_number] = $all_individual_installment_statement;
                // $all_individual_installment[]=$individual_installment_details;
                // $all_statement[] = $all_individual_installment;
                $individual_installment_details['installment_number'] = $prev_installment_number;
                // $individual_installment_details['installment_balance'] = $original_installment_amount ;

                $individual_installment_details['installment_details'] = $all_individual_installment_statement;
                // $all_individual_installment[]=$individual_installment_details;
                $all_statement[] = $individual_installment_details;
            }
            
        }
        else{
            // $statement_details['transaction_date'] = date('d-m-Y', strtotime($statement_row['g_d']));
            $statement_details['transaction_date'] = $statement_row['r_d'];

            $statement_details['transaction_paid_amount'] = $statement_row['p_a'];
            // if ($cus_type == "chit") {
            //     $statement_details['transaction_balance_amount'] = $statement_row['n_p'];
            // }
            $statement_details['transaction_created_by'] = $statement_row['u_name'];
    
            $no_of_installment += 1;
            $total_paid_amount += $statement_row['p_a'];
    
            $all_statement[] = $statement_details;
        }

    }


    if ($cus_type == "chit") {
        $returnArr = array(
            "ResponseCode" => "200",
            "Result" => "true",
            "ResponseMsg" => "Success",
            "chit_data" => $all_statement,
            "customer_name" => $customer_name,
            "customer_id" => $customer_id,
            "customer_type" => $customer_type,

            "header_one" => $chit_amount,
            "header_two" => $scheme,
            "header_three" => $auction_amount,
            "current_installment" => $installment_number,
            "total_paid_amount" => $total_paid_amount,
            "balance_amount" => $auction_amount*$multiply_number - $total_paid_amount,
        );

    } else if ($cus_type == "finance") {
        $returnArr = array(
            "ResponseCode" => "200",
            "Result" => "true",
            "ResponseMsg" => "Success",
            "data" => $all_statement,
            "customer_name" => $customer_name,
            "customer_id" => $customer_id,
            "customer_type" => $customer_type,

            "header_one" => $payment_amount,
            "header_two" => $days,
            "header_three" => $settlement_amount,

            "no_of_installment" => $no_of_installment,
            "total_paid_amount" => $total_paid_amount,
        );

    } else {
        $returnArr = array(
            "ResponseCode" => "200",
            "Result" => "true",
            "ResponseMsg" => "Success",
            "data" => $all_statement,
            "customer_name" => $customer_name,
            "customer_id" => $customer_id,
            "customer_type" => $customer_type,

            "header_one" => $investment_amount,
            "header_two" => $roi,
            "header_three" => $interest_amount,
            "header_four" => $closing_date,

            "no_of_installment" => $no_of_installment,
            "total_paid_amount" => $total_paid_amount,
        );

    }

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