<?php
require_once("config/config.php");
////// pick unadjusted payment of each party
$res_pay = mysqli_query($link1,"SELECT doc_no , from_location , rec_amount , payment_date FROM payment_receive where adjust_flag = '' order by payment_date,from_location asc");
while($row_pay = mysqli_fetch_assoc($res_pay)){
	$adjusted_amt = $row_pay["rec_amount"];
	///// pick unadjusted invoice of that party and adjust
	$res_inv = mysqli_query($link1,"SELECT challan_no , total_cost , adjusted_payment_amount FROM billing_master where to_location='".$row_pay["from_location"]."' and status='Received' and (is_adjust='' or is_adjust='P')  order by sale_date asc");
	while($row_inv = mysqli_fetch_assoc($res_inv)){
		$inv_amt = $row_inv["total_cost"] - $row_inv["adjusted_payment_amount"];
		///// check invoice amount to payment received amount
		if($inv_amt == $adjusted_amt){  //////// complete payment adjust with complete invoice
			///// update payment against invoice
			mysqli_query($link1,"UPDATE billing_master set payment_ref = '".$row_pay["doc_no"]."', adjusted_payment_amount='".$adjusted_amt."', is_adjust='Y', payment_date='".$row_pay["payment_date"]."' where challan_no='".$row_inv["challan_no"]."'");
			////// insert in payment adjust history
			mysqli_query($link1,"INSERT INTO payment_adjust_history set payment_ref='".$row_pay["doc_no"]."', payment_amt='".$row_pay["rec_amount"]."', adjust_amt='".$adjusted_amt."', invoice_no='".$row_inv["challan_no"]."', invoice_amt='".$row_inv["total_cost"]."', update_by='".$_SESSION["userid"]."', update_date='".$today."', update_time='".$currtime."'");
			break;
		}else if($inv_amt < $adjusted_amt){  ///// partial payment adjust with complete invoice amount
			///// update payment against invoice
			mysqli_query($link1,"UPDATE billing_master set payment_ref = '".$row_pay["doc_no"]."', adjusted_payment_amount='".$inv_amt."', is_adjust='Y', payment_date='".$row_pay["payment_date"]."' where challan_no='".$row_inv["challan_no"]."'");
			////// insert in payment adjust history
			mysqli_query($link1,"INSERT INTO payment_adjust_history set payment_ref='".$row_pay["doc_no"]."', payment_amt='".$row_pay["rec_amount"]."', adjust_amt='".$inv_amt."', invoice_no='".$row_inv["challan_no"]."', invoice_amt='".$row_inv["total_cost"]."', update_by='".$_SESSION["userid"]."', update_date='".$today."', update_time='".$currtime."'");
			///// remain bal of payment
			$adjusted_amt = $adjusted_amt - $inv_amt;
		}else{  //// complete payment adjust with partial invoice amount
			///// update payment against invoice
			mysqli_query($link1,"UPDATE billing_master set payment_ref = '".$row_pay["doc_no"]."', adjusted_payment_amount='".$adjusted_amt."', is_adjust='P', payment_date='".$row_pay["payment_date"]."' where challan_no='".$row_inv["challan_no"]."'");
			////// insert in payment adjust history
			mysqli_query($link1,"INSERT INTO payment_adjust_history set payment_ref='".$row_pay["doc_no"]."', payment_amt='".$row_pay["rec_amount"]."', adjust_amt='".$adjusted_amt."', invoice_no='".$row_inv["challan_no"]."', invoice_amt='".$row_inv["total_cost"]."', update_by='".$_SESSION["userid"]."', update_date='".$today."', update_time='".$currtime."'");
		}
	}////close invoice while loop
}//// close payment while loop
?>