<?php
	require('../config/dbcon.php');
	$data = [];	
	for($i=0; $i < count($_POST['mem_id']); $i++){
	    if (!empty($_POST['ch_amount'][$i])) {
	        $receipt_number = $_POST['receipt_number'];
	        $srno = $_POST['srno']; //OK
	        $srnoupd = $_POST['srno'] + 1;
	        $receipt_date = $_POST['receipt_date'];
	        
	        $orgDate = $_POST['receipt_date'];  
    		$newDate = date("Y-m-d H:i:s", strtotime($orgDate));
    		$receipt_date = $newDate;
	        
	        
	        $customer_code = $_POST['customer_code'];
	        $customer_name = $_POST['customer_name'];
	        $cust_place = $_POST['cust_place'];
	        $acc_type = $_POST['acc_type'];
	        $bankname = $_POST['bankname'];
	        $bankaccno = $_POST['bankaccno'];
	        
	        $cheque_amount = $_POST['cheque_amount'];
	        $ch_number = $_POST['ch_number'];
	        $ch_date = $_POST['ch_date'];
	        $bank_name = $_POST['bank_name'];
	        $bankaccno = $_POST['bankaccno'];
	        $remarks = $_POST['remarks'];
	        
    		$data[$_POST['mem_id'][$i]]['inv_number'] = $_POST['inv_number'][$i];
    		$data[$_POST['mem_id'][$i]]['inv_date'] = $_POST['inv_date'][$i];
    		$data[$_POST['mem_id'][$i]]['total_inv_val'] = $_POST['total_inv_val'][$i];
    		$data[$_POST['mem_id'][$i]]['rec_amount'] = $_POST['rec_amount'][$i];
    		$data[$_POST['mem_id'][$i]]['ch_amount'] = $_POST['ch_amount'][$i];
    		
    		$data[$_POST['mem_id'][$i]]['balance_amount'] = $_POST['balance_amount'][$i];
    		$recamt =  $_POST['rec_amount'][$i] + $_POST['ch_amount'][$i];
    		$data[$_POST['mem_id'][$i]]['rec_amount'] = $_POST['rec_amount'][$i] + $_POST['ch_amount'][$i];
    		$balamt = $_POST['rec_amount'][$i] + $_POST['ch_amount'][$i];
    		$data[$_POST['mem_id'][$i]]['balance_amount'] = $_POST['total_inv_val'][$i] - $balamt;
    		echo($_POST['remarks']);
    		$status1 = "CLEARED";
    	}
	}	
	//================UPDATE INVOICE BEGINS
	foreach($data as $key => $value){
		mysqli_query($connect, "update invtotal_mst set rec_amount = '$value[rec_amount]', balance_amount = '$value[balance_amount]' where inv_number = '$value[inv_number]' and customer_code = '$customer_code' ");

        mysqli_query($connect,"insert into receipt_mst(receipt_number,srno,receipt_date,customer_code,customer_name,cust_place,ch_number,ch_date,cheque_amount,inv_number,inv_date,total_inv_val,rec_amount,balance_amount,ch_amount,status) values ('$receipt_number', '$srno', '$receipt_date', '$customer_code', '$customer_name', '$cust_place', '$ch_number', '$ch_date', '$cheque_amount', '$value[inv_number]', '$value[inv_date]', '$value[total_inv_val]', '$value[rec_amount]', '$value[balance_amount]', '$value[ch_amount]', '$status1')");		
        
        $update="update control_mst set srno = '".$srnoupd."' where description='RECEIPTS'";
		mysqli_query($connect, $update) or die(mysqli_error());
	}
	
	$query = "select inv_number,balance_amount,status from invtotal_mst where  customer_code = '$customer_code'";
	$resultstk = mysqli_query($connect,$query);    
	while($rows = mysqli_fetch_array($resultstk) ){
		$balance_amount = $rows["balance_amount"];
		$inv_number =$rows["inv_number"];
		if($balance_amount <= '0'){
		    $status1 = "CLEARED";
		}
		else{
		    $status1 = "PENDING";
		}
		$updatestatus="update invtotal_mst set status = '".$status1."' where inv_number='" . $inv_number . "'";
		mysqli_query($connect, $updatestatus) or die(mysqli_last_error());
	}
	//================UPDATE INVOICE ENDS
	
	//================UPDATE CUSTOMER_MST BEGINS
	$query = "SELECT customer_code,customer_name,opening,debit,credit,clbalance from customer_mst where customer_code = '" . $customer_code . "'";
	$resultstk = mysqli_query($connect,$query);    
	while($rows = mysqli_fetch_array($resultstk)){
		$opening = $rows["opening"];
		$debit =$rows["debit"];
		$credit =$rows["credit"];
		$clbalance =$rows["clbalance"];
		
		$curruntcredit = $cheque_amount;
		
		$finalcredit = $credit + $curruntcredit;
	    $finalbal = ($opening  + $debit) - $finalcredit;
		$updatestk="update customer_mst set credit = '".$finalcredit."',clbalance = '".$finalbal."' where customer_code='". $customer_code . "'";
		mysqli_query($connect, $updatestk) or die(mysqli_last_error());
	}
	//================UPDATE CUSTOMER_MST ENDS
	
	//================UPDATE BANK BEGINS
	$query = "SELECT customer_code,customer_name,opening,debit,credit,clbalance from customer_mst where bankaccno = '" . $bankaccno . "'";
	$resultstk = mysqli_query($connect,$query);    
	while($rows = mysqli_fetch_array($resultstk)){				
		$opening = $rows["opening"];
		$debit =$rows["debit"];
		$credit =$rows["credit"];
		$clbalance =$rows["clbalance"];
		
		$curruntcredit = $cheque_amount;
		
		$finalcredit = $credit + $curruntcredit;
	    $finalbal = ($opening  - $debit) + $finalcredit;
		$updatestk="update customer_mst set credit = '".$finalcredit."',clbalance = '".$finalbal."' where bankaccno='". $bankaccno . "'";
		mysqli_query($connect, $updatestk) or die(mysqli_last_error());
	}
	//================UPDATE BANK ENDS
	
	header('location: index');
?>