<?php
require 'config_db.php';
// require 'customer_details.php';
$data = json_decode(file_get_contents('php://input'), true);
if ($data['uid'] == '' or $data['cus_name'] == '' or $data['cus_id'] == '') {
    $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went Wrong!");
} else {
    $uid = strip_tags(mysqli_real_escape_string($con, $data['uid']));
    $cus_date = strip_tags(mysqli_real_escape_string($con, $data['cus_date']));
    $cus_nick_name = strip_tags(mysqli_real_escape_string($con, $data['cus_nick_name']));
    $cus_name = strip_tags(mysqli_real_escape_string($con, $data['cus_name']));
    $cus_id = strip_tags(mysqli_real_escape_string($con, $data['cus_id']));
    $cus_type = strip_tags(mysqli_real_escape_string($con, $data['cus_type']));
    $cus_mobile = strip_tags(mysqli_real_escape_string($con, $data['cus_mobile']));
    $address = strip_tags(mysqli_real_escape_string($con, $data['address']));
    $nominee_name = strip_tags(mysqli_real_escape_string($con, $data['nominee_name']));
    $nominee_number = strip_tags(mysqli_real_escape_string($con, $data['nominee_number']));
    $current_time = strip_tags(mysqli_real_escape_string($con, $data['current_time']));
    $mode = strip_tags(mysqli_real_escape_string($con, $data['mode']));
    $group_name = strip_tags(mysqli_real_escape_string($con, $data['group_name']));
    //Chit Customer
    if ($cus_type == "chit") {

        //save chit customer
        if ($mode == 1) {
        
            $val = getCustomerIdCheck($con,$cus_id);
            if($val == 0) {
                $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Cus ID Already Present");
            } else {
                
                $commitment_month = strip_tags(mysqli_real_escape_string($con, $data['commitment_month']));
                $commitment_amount = strip_tags(mysqli_real_escape_string($con, $data['commitment_amount']));
                $previous_amount = strip_tags(mysqli_real_escape_string($con, $data['previous_amount']));
                $scheme = strip_tags(mysqli_real_escape_string($con, $data['scheme']));
                $multiply_number = strip_tags(mysqli_real_escape_string($con, $data['multiply_number']));
                $current_chit_amount = strip_tags(mysqli_real_escape_string($con, $data['current_chit_amount']));
                $auction_amount = strip_tags(mysqli_real_escape_string($con, $data['auction_amount']));
                $final_chit_amount = strip_tags(mysqli_real_escape_string($con, $data['final_chit_amount']));
    
                $commitment_month = empty($commitment_month) ? 0 : $commitment_month;
                $commitment_amount = empty($commitment_amount) ? 0.00 : $commitment_amount;
    
                $installment_number = 1;
                if ($scheme == 10) {
                    $installment_balance_amount = $multiply_number * 10000;
                } else {
                    $installment_balance_amount = $multiply_number * 5000;
                }
    
                // $cus_id = getChitCustomerId($con); 
                if ($previous_amount != "0.00") {
                    $installment_qry = "SELECT installment_number,installment_amount 
                                                FROM installment_entry 
                                                where scheme=$scheme ORDER BY id ASC";
                    $result_installment = $con->query($installment_qry);
    
                    $installment_number = 0;
                    $sum_installment_amount = 0;
                    while ($row = $result_installment->fetch_assoc()) {
                        $installment_number = $row['installment_number'];
                        $sum_installment_amount += $row['installment_amount'] * $multiply_number;
    
                        if ($sum_installment_amount > $previous_amount) {
                            $installment_balance_amount = $sum_installment_amount - $previous_amount;
                            break;
                        } else if ($sum_installment_amount == $previous_amount) {
                            $next_row = $result_installment->fetch_assoc();
                            $installment_number = $next_row['installment_number'];
                            $installment_balance_amount = $next_row['installment_amount'] * $multiply_number;
                            break;
                        }
    
                    }
    
                }
    
                $insert_sql = "insert into customers_chit(
                    `created_by`,`cus_date`,`cus_nick_name`, `cus_name`, `cus_id`, `cus_type`, `cus_mobile`,`address`,`nominee_name`,`nominee_number`,
                    `group_name`,`commitment_month`,`commitment_amount`,`previous_amount`,`scheme`,
                    `installment_number`,`installment_balance_amount`,
                    `multiply_number`,`current_chit_amount`,`auction_amount`,`final_chit_amount`,
                    `created_time`,`updated_time`)
                values(
                    '$uid','$cus_date','$cus_nick_name','$cus_name','$cus_id','$cus_type','$cus_mobile','$address','$nominee_name','$nominee_number',
                    '$group_name','$commitment_month','$commitment_amount','$previous_amount','$scheme',
                    '$installment_number','$installment_balance_amount',
                    '$multiply_number','$current_chit_amount','$auction_amount','$final_chit_amount',
                    '$current_time','$current_time')";
                $result = $con->query($insert_sql);
                
                $insert_sub_sql = "insert into last_installment_details(
                            `cus_name`, `cus_id`,`scheme`,`installment_number`,`installment_balance_amount`,`status`)
                        values(
                            '$cus_name','$cus_id','$scheme','$installment_number','$installment_balance_amount','1')";
                $result_sub = $con->query($insert_sub_sql);
    
                if ($result)
                    $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "New Customer Added");
                else
                    $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
            }
            
        }
        //edit chit customer
        else if ($mode == 2) {
            $cid = strip_tags(mysqli_real_escape_string($con, $data['cid']));
            
            // $val = getCustomerIdCheck($con,$cus_id);
            // if($val == 0) {
            //     $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Cus ID Already Present");
            // } else {
                $update_customer_sql = "update customers_chit set `cus_id`='$cus_id',
                                                `cus_name`='$cus_nick_name',`cus_date`='$cus_date',`cus_nick_name`='$cus_name',`cus_mobile`='$cus_mobile',
                                                `address`='$address',`group_name`='$group_name',
                                                `nominee_name`='$nominee_name',`nominee_number`='$nominee_number',`updated_time`='$current_time'
                                            where id = '$cid'";
    
                $result = $con->query($update_customer_sql);
    
                if ($result)
                    $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Customer Updated");
                else
                    $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");   
            // }
        }
        //convert to paid customer
        else if($mode == 3)
        {
            $cid = strip_tags(mysqli_real_escape_string($con, $data['cid']));

            $update_customer_sql = "update customers_chit set `payment_status`='3'
                                        where id = '$cid'";

            $result = $con->query($update_customer_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Customer Updated");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }
        //delete customer
        else if($mode == 4)
        {
            $cid = strip_tags(mysqli_real_escape_string($con, $data['cid']));

            $update_customer_sql = "update customers_chit set `customer_status`='0'
                                        where id = '$cid'";

            $result = $con->query($update_customer_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Customer Deleted");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }

    }
    //finance customer
    else if ($cus_type == "finance") {

        //save chit customer
        if ($mode == 1) {
            $cus_id = getFinanceCustomerId($con); 
            $payment_amount = strip_tags(mysqli_real_escape_string($con, $data['payment_amount']));
            $days = strip_tags(mysqli_real_escape_string($con, $data['days']));
            $settlement_amount = strip_tags(mysqli_real_escape_string($con, $data['settlement_amount']));

            $insert_sql = "insert into customers_finance(
                `created_by`,`cus_date`, `cus_nick_name`,`cus_name`, `cus_id`, `cus_type`, `cus_mobile`,`address`,`nominee_name`,`nominee_number`,
                `payment_amount`,`days`,`settlement_amount`,
                `created_time`,`updated_time`)
            values(
                '$uid','$cus_date','$cus_nick_name','$cus_name','$cus_id','$cus_type','$cus_mobile','$address','$nominee_name','$nominee_number',
                '$payment_amount','$days','$settlement_amount',
                '$current_time','$current_time')";
            $result = $con->query($insert_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "New Customer Added");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }
         //edit finance customer
         else if ($mode == 2) {
            $cid = strip_tags(mysqli_real_escape_string($con, $data['cid']));

            $update_customer_sql = "update customers_finance set  
                                            `cus_name`='$cus_name',`cus_date`='$cus_date',`cus_nick_name`='$cus_nick_name',`cus_mobile`='$cus_mobile',`address`='$address',
                                            `nominee_name`='$nominee_name',`nominee_number`='$nominee_number',`updated_time`='$current_time'
                                        where id = '$cid'";

            $result = $con->query($update_customer_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Customer Updated");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }
        //convert to paid customer
        else if($mode == 3)
        {
            $cid = strip_tags(mysqli_real_escape_string($con, $data['cid']));

            $update_customer_sql = "update customers_finance set `payment_status`='3'
                                        where id = '$cid'";

            $result = $con->query($update_customer_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Customer Updated");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }
        //delete customer
        else if($mode == 4)
        {
            $cid = strip_tags(mysqli_real_escape_string($con, $data['cid']));

            $update_customer_sql = "update customers_finance set `customer_status`='0'
                                        where id = '$cid'";

            $result = $con->query($update_customer_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Customer Deleted");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }

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

        //save interest customer
        if ($mode == 1) {
            $cus_id = getInterestCustomerId($con); 
            $investment_amount = strip_tags(mysqli_real_escape_string($con, $data['investment_amount']));
            $roi = strip_tags(mysqli_real_escape_string($con, $data['roi']));
            $interest_amount = strip_tags(mysqli_real_escape_string($con, $data['interest_amount']));
            $closing_date = strip_tags(mysqli_real_escape_string($con, $data['closing_date']));

            $insert_sql = "insert into customers_interest(
                `created_by`,`cus_date`,`cus_nick_name`, `cus_name`, `cus_id`, `cus_type`, `cus_mobile`,`address`,`nominee_name`,`nominee_number`,
                `investment_amount`,`roi`,`interest_amount`,`closing_date`,
                `created_time`,`updated_time`)
            values(
                '$uid','$cus_date','$cus_nick_name','$cus_name','$cus_id','$cus_type','$cus_mobile','$address','$nominee_name','$nominee_number',
                '$investment_amount','$roi','$interest_amount','$closing_date',
                '$current_time','$current_time')";
            $result = $con->query($insert_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "New Customer Added");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }
        //edit interest customer
        else if ($mode == 2) {
            $cid = strip_tags(mysqli_real_escape_string($con, $data['cid']));

            $update_customer_sql = "update customers_interest set  
                                            `cus_name`='$cus_name',`cus_date`='$cus_date',`cus_nick_name`='$cus_nick_name',`cus_mobile`='$cus_mobile',`address`='$address',
                                            `nominee_name`='$nominee_name',`nominee_number`='$nominee_number',`updated_time`='$current_time'
                                        where id = '$cid'";

            $result = $con->query($update_customer_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Customer Updated");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }
        //convert to paid customer
        else if($mode == 3)
        {
            $cid = strip_tags(mysqli_real_escape_string($con, $data['cid']));

            $update_customer_sql = "update customers_interest set `payment_status`='3'
                                        where id = '$cid'";

            $result = $con->query($update_customer_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Customer Updated");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }
        //delete customer
        else if($mode == 4)
        {
            $cid = strip_tags(mysqli_real_escape_string($con, $data['cid']));

            $update_customer_sql = "update customers_interest set `customer_status`='0'
                                        where id = '$cid'";

            $result = $con->query($update_customer_sql);

            if ($result)
                $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Customer Deleted");
            else
                $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
        }

    }


}

function getCustomerIdCheck($con,$cus_id)
{
    $user_access_query = $con->query("SELECT * FROM customers_chit where cus_id = '$cus_id' and customer_status='1'");
    $user_count = $user_access_query->num_rows;
    if($user_count == 0) {
        // $nn = getChitCustomerId($con);
        // return $nn;
        return 1;
    } else {
        return 0;
    }
}


function getChitCustomerId($con)
{
	$last_cus_id = $con->query("SELECT cus_id FROM customers_chit ORDER BY id DESC LIMIT 1");
	$last_cus_id = $last_cus_id->fetch_assoc();
	$count = isset($last_cus_id['cus_id']);

	if ($count) {
		$receipt_number = $last_cus_id['cus_id'];
		$length = 7;
		$rec_text_number = explode("-", $receipt_number);
		$no = $rec_text_number[2] + 1;
		$no = substr(str_repeat(0, $length) . $no, -$length);
		$rec_no = "CUS" . "-C-" . $no;
	} else {
		$rec_no = "CUS" . "-C-" . "0000001";
	}
	return $rec_no;
}

function getFinanceCustomerId($con)
{
	$last_cus_id = $con->query("SELECT cus_id FROM customers_finance ORDER BY id DESC LIMIT 1");
	$last_cus_id = $last_cus_id->fetch_assoc();
	$count = isset($last_cus_id['cus_id']);

	if ($count) {
		$receipt_number = $last_cus_id['cus_id'];
		$length = 7;
		$rec_text_number = explode("-", $receipt_number);
		$no = $rec_text_number[2] + 1;
		$no = substr(str_repeat(0, $length) . $no, -$length);
		$rec_no = "CUS" . "-F-" . $no;
	} else {
		$rec_no = "CUS" . "-F-" . "0000001";
	}
	return $rec_no;
}



function getInterestCustomerId($con)
{
	$last_cus_id = $con->query("SELECT cus_id FROM customers_interest ORDER BY id DESC LIMIT 1");
	$last_cus_id = $last_cus_id->fetch_assoc();
	$count = isset($last_cus_id['cus_id']);

	if ($count) {
		$receipt_number = $last_cus_id['cus_id'];
		$length = 7;
		$rec_text_number = explode("-", $receipt_number);
		$no = $rec_text_number[2] + 1;
		$no = substr(str_repeat(0, $length) . $no, -$length);
		$rec_no = "CUS" . "-I-" . $no;
	} else {
		$rec_no = "CUS" . "-I-" . "0000001";
	}
	return $rec_no;
}

echo json_encode($returnArr);
?>