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

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

	if($mode=="3"){
		$result_customer_chit = getChitCustomerList($con, $mode, $data);
		$returnArr = array(
			"ResponseCode" => "200",
			"Result" => "true",
			"ResponseMsg" => "Success",
			"customer_chit_list" => $result_customer_chit
		);
	}
	else if($mode=="5"){ // delete group
	 $group_id = $data['group_id'];
	 $group_name = $data['group_name'];
	    if($type == '0') {
            $update_group_status = "update group_list set `status`='0'
                                                        where id = '$group_id' and group_name = '$group_name'";
            $con->query($update_group_status);
		$returnArr = array(
			"ResponseCode" => "200",
			"Result" => "true",
			"ResponseMsg" => "Delete Group Sucess"
		);
	    }
	}
	else{
		$result_customer_chit = getChitCustomerList($con, $mode, $data);
		$result_customer_finance = getFinanceCustomerList($con, $mode, $data);
		$result_customer_interest = getInterestCustomerList($con, $mode, $data);
	
		$all_last_receipt_number = array(
			getLastChitReceiptNumber($con),
			getLastFinanceReceiptNumber($con),
			getLastInterestReceiptNumber($con),
		);
	
		// $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Success", 
		// "data" => $q,"receipt_number"=>$receipt_number);
		$returnArr = array(
			"ResponseCode" => "200",
			"Result" => "true",
			"ResponseMsg" => "Success",
			"customer_chit_list" => $result_customer_chit,
			"customer_finance_list" => $result_customer_finance,
			"customer_interest_list" => $result_customer_interest,
	
			"receipt_number" => $all_last_receipt_number
		);
	
	}
	
}

function getLastChitReceiptNumber($con)
{
	$last_receipt_number = $con->query("SELECT receipt_number FROM transaction_chit ORDER BY id DESC LIMIT 1");
	$last_receipt_number = $last_receipt_number->fetch_assoc();
	$count = isset($last_receipt_number['receipt_number']);

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

function getLastFinanceReceiptNumber($con)
{
	$last_receipt_number = $con->query("SELECT receipt_number FROM transaction_finance ORDER BY id DESC LIMIT 1");
	$last_receipt_number = $last_receipt_number->fetch_assoc();
	$count = isset($last_receipt_number['receipt_number']);

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

function getLastInterestReceiptNumber($con)
{
	$last_receipt_number = $con->query("SELECT receipt_number FROM transaction_interest ORDER BY id DESC LIMIT 1");
	$last_receipt_number = $last_receipt_number->fetch_assoc();
	$count = isset($last_receipt_number['receipt_number']);

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

function getChitCustomerList($con, $mode, $data)
{
	$customer_chit_details = array();
	$all_customer_chit_details = array();

	//customer details in entry
	if ($mode == 0) {
		$cy = $con->query("select * from customers_chit where payment_status<>3 and customer_status=1 Order by id DESC");
	}
	//customer list in fragment
	else if ($mode == 1) {
		$cy = $con->query("select * from customers_chit where payment_status=3 and customer_status=1 Order by paid_time DESC ");
	}
	//single customer
	else if ($mode == 2) {
		$cus_id = $data['cus_id'];
		$cy = $con->query("select * from customers_chit where cus_id='$cus_id' and customer_status=1");
	}
	//group customer. list
	else if ($mode == 3) {
		$group_id = $data['group_id'];

		// $cy = $con->query("select * from customers_chit 
		// 								INNER JOIN group_list on customers_chit.group_name =group_list.group_name 
		// 								where group_list.id='$group_id' and customers_chit.customer_status=1;");
		$cy = $con->query("select * from customers_chit 
										where group_name='$group_id' and customer_status=1");
	}

	while ($row = $cy->fetch_assoc()) {
		$cus_id = $row['id'];
		$customer_chit_details['id'] = $row['id'];
		$customer_chit_details['cus_date'] = $row['cus_date'];
		$customer_chit_details['cus_nick_name'] = $row['cus_name'];
		$customer_chit_details['cus_name'] = $row['cus_nick_name'];
// 		$customer_chit_details['cus_nick_name'] = $row['cus_nick_name'];
// 		$customer_chit_details['cus_name'] = $row['cus_name'];
		$customer_chit_details['cus_id'] = $row['cus_id'];
		$customer_chit_details['cus_type'] = $row['cus_type'];
		$customer_chit_details['cus_mobile'] = $row['cus_mobile'];
		$customer_chit_details['address'] = $row['address'];
		$customer_chit_details['nominee_name'] = $row['nominee_name'];
		$customer_chit_details['nominee_number'] = $row['nominee_number'];

		$customer_chit_details['created_time'] =date('d-m-Y', strtotime($row['created_time'])); 

		$customer_chit_details['group_name'] = $row['group_name'];
		$customer_chit_details['commitment_month'] = $row['commitment_month'];
		$customer_chit_details['commitment_amount'] = $row['commitment_amount'];
		$customer_chit_details['previous_amount'] = $row['previous_amount'];
		$customer_chit_details['scheme'] = $row['scheme'];
		$customer_chit_details['installment_number'] = $row['installment_number'];
		$customer_chit_details['installment_balance_amount'] = $row['installment_balance_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;

		$customer_chit_details['previous_paid_amount'] = $sum_prev_paid_amount + $row['previous_amount'];

		$customer_chit_details['multiply_number'] = $row['multiply_number'];
		$customer_chit_details['current_chit_amount'] = $row['current_chit_amount'];
		$customer_chit_details['auction_amount'] = $row['auction_amount'];
		$customer_chit_details['final_chit_amount'] = $row['final_chit_amount'];

		$customer_chit_details['conversion_status'] = $row['conversion_status'];
		$customer_chit_details['payment_status'] = $row['payment_status'];
		$customer_chit_details['customer_status'] = $row['customer_status'];
		$customer_chit_details['bid_amount'] = $row['bid_amount'];
		$customer_chit_details['bid_date'] = $row['bid_date'];


		$all_customer_chit_details[] = $customer_chit_details;
	}

	return $all_customer_chit_details;
}

function getFinanceCustomerList($con, $mode, $data)
{
	$customer_finance_details = array();
	$all_customer_finance_details = array();

	//customer details in entry
	if ($mode == 0) {
		$cy = $con->query("select * from customers_finance where payment_status<>3 and customer_status=1 Order by id DESC");
	}
	//customer list in fragment
	else if ($mode == 1) {
		$cy = $con->query("select * from customers_finance where payment_status=3 and customer_status=1 Order by paid_time DESC ");
	}
	//single customer
	else if ($mode == 2) {
		$cus_id = $data['cus_id'];
		$cy = $con->query("select * from customers_finance where cus_id='$cus_id' and customer_status=1 ");
	}

	while ($row = $cy->fetch_assoc()) {
		$cus_id = $row['id'];
		$customer_finance_details['id'] = $row['id'];
		$customer_finance_details['cus_date'] = $row['cus_date'];
		$customer_finance_details['cus_nick_name'] = $row['cus_name'];
		$customer_finance_details['cus_name'] = $row['cus_nick_name'];
// 		$customer_finance_details['cus_nick_name'] = $row['cus_nick_name'];
// 		$customer_finance_details['cus_name'] = $row['cus_name'];
		$customer_finance_details['cus_id'] = $row['cus_id'];
		$customer_finance_details['cus_type'] = $row['cus_type'];
		$customer_finance_details['cus_mobile'] = $row['cus_mobile'];
		$customer_finance_details['address'] = $row['address'];
		$customer_finance_details['nominee_name'] = $row['nominee_name'];
		$customer_finance_details['nominee_number'] = $row['nominee_number'];

		$customer_finance_details['payment_amount'] = $row['payment_amount'];
		$customer_finance_details['days'] = $row['days'];
		$customer_finance_details['settlement_amount'] = $row['settlement_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;

		// $customer_chit_details['previous_paid_amount'] = $sum_prev_paid_amount + $row['previous_amount'];


		$customer_finance_details['payment_status'] = $row['payment_status'];
		$customer_finance_details['customer_status'] = $row['customer_status'];



		$all_customer_finance_details[] = $customer_finance_details;
	}

	return $all_customer_finance_details;
}

function getInterestCustomerList($con, $mode, $data)
{
	$customer_interest_details = array();
	$all_customer_interest_details = array();

	//customer details in entry
	if ($mode == 0) {
		$cy = $con->query("select * from customers_interest where payment_status<>3 and customer_status=1 Order by id DESC");
	}
	//customer list in fragment
	else if ($mode == 1) {
		$cy = $con->query("select * from customers_interest where payment_status=3 and customer_status=1 Order by paid_time DESC ");
	}
	//single customer
	else if ($mode == 2) {
		$cus_id = $data['cus_id'];
		$cy = $con->query("select * from customers_interest where cus_id='$cus_id' and customer_status=1");
	}

	while ($row = $cy->fetch_assoc()) {
		$cus_id = $row['id'];
		$customer_interest_details['id'] = $row['id'];
		$customer_interest_details['cus_date'] = $row['cus_date'];
		$customer_interest_details['cus_nick_name'] = $row['cus_name'];
		$customer_interest_details['cus_name'] = $row['cus_nick_name'];
// 		$customer_interest_details['cus_nick_name'] = $row['cus_nick_name'];
// 		$customer_interest_details['cus_name'] = $row['cus_name'];
		$customer_interest_details['cus_id'] = $row['cus_id'];
		$customer_interest_details['cus_type'] = $row['cus_type'];
		$customer_interest_details['cus_mobile'] = $row['cus_mobile'];
		$customer_interest_details['address'] = $row['address'];
		$customer_interest_details['nominee_name'] = $row['nominee_name'];
		$customer_interest_details['nominee_number'] = $row['nominee_number'];

		$customer_interest_details['investment_amount'] = $row['investment_amount'];
		$customer_interest_details['roi'] = $row['roi'];
		$customer_interest_details['interest_amount'] = $row['interest_amount'];
		$customer_interest_details['closing_date'] = date('d-m-Y', strtotime($row['closing_date']));

		
		// $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;

		// $customer_chit_details['previous_paid_amount'] = $sum_prev_paid_amount + $row['previous_amount'];


		$customer_interest_details['payment_status'] = $row['payment_status'];
		$customer_interest_details['customer_status'] = $row['customer_status'];



		$all_customer_interest_details[] = $customer_interest_details;
	}

	return $all_customer_interest_details;
}

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