<?php
	require('../config/dbcon.php');
	$data = [];	
	for($i=0; $i < count($_POST['mem_id']); $i++){
	    if (!empty($_POST['ch_amount'][$i])) {
	        $payment_number = $_POST['payment_number'];
	        $srno = $_POST['srno']; //OK
	        $srnoupd = $_POST['srno'] + 1;
	        $payment_date = $_POST['payment_date'];
	        
	        $orgDate = $_POST['payment_date'];  
    		$newDate = date("Y-m-d H:i:s", strtotime($orgDate));
    		$payment_date = $newDate;
	        
	        
	        $supplier_code = $_POST['supplier_code'];
	        $supplier_name = $_POST['supplier_name'];
	        $supplier_place = $_POST['supplier_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];
    		$payamt =  $_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 pinvtotal_mst set rec_amount = '$value[rec_amount]', balance_amount = '$value[balance_amount]' where inv_number = '$value[inv_number]' and supplier_code = '$supplier_code' ");

        mysqli_query($connect,"insert into payment_mst(payment_number,srno,payment_date,supplier_code,supplier_name,supplier_place,ch_number,ch_date,cheque_amount,inv_number,inv_date,total_inv_val,rec_amount,balance_amount,ch_amount,status) values ('$payment_number', '$srno', '$payment_date', '$supplier_code', '$supplier_name', '$supplier_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='PAYMENTS'";
		mysqli_query($connect, $update) or die(mysqli_error());
	}
	
	$query = "select inv_number,balance_amount,status from pinvtotal_mst where  supplier_code = '$supplier_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 pinvtotal_mst set status = '".$status1."' where inv_number='" . $inv_number . "'";
		mysqli_query($connect, $updatestatus) or die(mysqli_last_error());
	}
	//================UPDATE INVOICE ENDS
	
	//================UPDATE supplier_mst BEGINS
	$query = "SELECT supplier_code,supplier_name,opening,debit,credit,clbalance from supplier_mst where supplier_code = '" . $supplier_code . "'";
	$resultstk = mysqli_query($connect,$query);    
	while($rows = mysqli_fetch_array($resultstk)){
		$opening = $rows["opening"];
		$debit =$rows["debit"];
		$credit =$rows["credit"];
		$clbalance =$rows["clbalance"];
		
		$curruntdebit = $cheque_amount;
		
		$finaldebit = $debit + $curruntdebit;
	    $finalbal = ($opening  + $credit) - $finaldebit;
		$updatestk="update supplier_mst set debit = '".$finaldebit."',clbalance = '".$finalbal."' where supplier_code='". $supplier_code . "'";
		mysqli_query($connect, $updatestk) or die(mysqli_last_error());
	}
	//================UPDATE supplier_mst ENDS
	
	//================UPDATE CASH BEGINS
	$query = "SELECT customer_code,customer_name,opening,debit,credit,clbalance from customer_mst where acc_type = '" . "CAS" . "'";
	$resultstk = mysqli_query($connect,$query);    
	while($rows = mysqli_fetch_array($resultstk)){				
		$opening = $rows["opening"];
		$debit =$rows["debit"];
		$credit =$rows["credit"];
		$clbalance =$rows["clbalance"];
		
		$curruntdebit = $cheque_amount;
		
		$finaldebit = $debit + $curruntdebit;
	    $finalbal = ($opening  + $credit) - $finaldebit;
	    $updatestk="update customer_mst set debit = '".$finaldebit."',clbalance = '".$finalbal."' where acc_type='". "CAS" . "'";
		mysqli_query($connect, $updatestk) or die(mysqli_last_error());
	}
	//================UPDATE CASH ENDS
	
	header('location: index');
?>