<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
include("opendb.php");
date_default_timezone_set('Asia/Kolkata');
$dt=date("Y-m-d H:i:s");
if(strlen($brcode)<=0)
{
	print "<script> alert('Invalid User'); parent.document.location='index.html' </script>";
	 return;
}
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
    $ip = $_SERVER['REMOTE_ADDR'];
}
$cust = $_REQUEST['cust'];
$amount= $_REQUEST['amount'];
$recptid=$_REQUEST['recptid'];
if(isset($_POST['update']))
{
	$i=0;
	$invno =$_REQUEST['invno'];
	foreach($invno as $a)
	{
 		$invno[$i]=$a; 
 		$i++;		
	}
	$i=0;
	$invdate=$_REQUEST['invdate'];
	foreach($invdate as $a)
	{
 		$invdate[$i]=$a; 
 		$i++;
	}
	$i=0;
	$invamount=$_REQUEST['invamount'];
	foreach($invamount as $a)
	{
 		$invamount[$i]=$a; 
 		$i++;
	}
	$i=0;
	$remark=$_REQUEST['remark'];
	foreach($remark as $a)
	{
 		$remark[$i]=$a; 
 		$i++;
	}
	$i=0;
	$amt=$_REQUEST['amt'];
	foreach($amt as $a)
	{
 		$amt[$i]=$a; 
 		$i++;
	}
	$brres12=mysqli_query($db,"select max(receiptno) from paymentreceipt where brcode='$brcode'");
	$brrow12=mysqli_fetch_array($brres12);
	$receiptno = $brrow12[0]+1;
	for($ctr=0;$ctr<=$i;$ctr++)
	{
		if($amt[$ctr]>0)
		{
			$sql = "insert into paymentreceipt (invno,customer,receiptno,invdate,invamount,amtreceived,rec_date,remarks,brcode,user,entrydate,paymenttype) values ('$invno[$ctr]','$cust','$recptid','$invdate[$ctr]','$invamount[$ctr]','$amt[$ctr]','$dt','$remark[$ctr]','$brcode','$user','".date("Y-m-d")."','BR')";
			print $sql;			
			mysqli_query($db,$sql);
		}
	}
	print "<script> alert('Updated !!!!');window.close();</script>";
}
else
{
	$sql32 = "select invno from paymentreceipt where brcode='$brcode' and customer = '$cust' and (jvno <= 0 or jvno IS NULL)";
	$res32=mysqli_query($db,$sql32);
	$brrow32=mysqli_fetch_array($res32);
	if(strlen($brrow32[0])>0)
	{
		print "<script> alert('Found old data. It will be deleted'); </script>";
		mysqli_query($db,"delete from paymentreceipt where brcode='$brcode' and customer = '$cust' and (jvno <= 0 or jvno IS NULL)");
	}
}

$cust1=substr($cust,0,strpos($cust,'(0')-1);
$sql = "SELECT invno,entrydate,sum(tot),customer FROM spareinvoice WHERE (customer = '$cust' or customer LIKE '%$cust1%') and brcode = '$brcode' group by invno order by invno asc";
$res=mysqli_query($db,$sql);
print "<form name='f1' action='' method=post>";
print "<table><tr><th>Open Paid Invoices of $cust</th></table>	";
print "<table >";

print "<tr><th>Customer Name</th><th>Invoice No.</th><th width='90'>Date</th><th>Amount</th><th>Payment Received</th><th>Remarks</th>";
$i=1;
$mval = $amount;
while($row=mysqli_fetch_array($res))
{
	$brres22=mysqli_query($db,"select sum(amtreceived) from paymentreceipt where brcode='$brcode' and customer = '$cust' and invno = '$row[invno]' and jvno > 0");
	//$ss="select sum(amtreceived) from paymentreceipt where brcode='$brcode' and customer = '$cust' and invno = '$row[invno]' and jvno > 0";
	//echo $ss;
	$brrow22=mysqli_fetch_array($brres22);
	$mamt = $row[2]-$brrow22[0];
	if($mamt > 0)
	{
		$df=implode('-', array_reverse(explode('-', $row[entrydate])));
		print "<tr><td>".$row[3]."</td><td>".$row[invno]."</td><td>$df</td><td>".$mamt."</td>";
		print "<input type=hidden name='invno[]' id='invno$i' value='$row[invno]'>";
		print "<input type=hidden name='invdate[]' id='invdate$i' value='$row[entrydate]'>";
		print "<input type=hidden name='invamount[]' id='invamount$i' value='$mamt'>";
		if($mval>$mamt)
			$mval1 = $mamt;
		else
			$mval1 = $mval;
		print "<td><input type=text name='amt[]' id='amt$i' value = '$mval1' size=8 align='right'></td><td><input type=text name='remark[]' id='remark$i' size=12></td>";
		$mval = $mval - $mamt;
		if($mval <0) $mval = 0;
			print "</tr>";
		$i++;
	}
}
?>
</table>
<table><tr><td colspan='5' align='right'><input type='submit' name='update' value='UPDATE' ></td></tr></table>
</form>



<style type="text/css">
table {
  width: 100%;
  font-size: 0.923em;
  margin: 0 0 10px;
  border: 1px solid #bebfb9;
  font-family: calibri;
}
table td,
table th {
  vertical-align: middle;
  padding: 8px 10px;
  border: 0;
  color: #000;
}
tr.even,
tr.odd {
  border-width: 0 1px 0 1px;
  border-style: solid;
  border-color: #bebfb9;
  background: #f3f4ee;
}
tr.odd {
  background: #fff;
}
tr.drag {
  background: #fe7;
}
tr.drag-previous {
  background: #ffb;
}
table th {
  text-transform: uppercase;
  background: #e1e2dc;
  font-weight: bold;
  font-family: calibri;
  border-width: 1px;
  border-style: solid;
  border-color: #bebfb9;
  padding: 3px 10px;
}
table th.active {
  background: #bdbeb9;
}
table th a {
  display: block;
  position: relative;
}
table th.active a {
  padding: 0 25px 0 0; /* LTR */
}
table th.active img {
  position: absolute;
  top: 3px;
  right: 3px;
}
table td.active {
  background: #e9e9dd;
}
table tr.odd td.active {
  background: #f3f4ee;
}
table tr.selected td.active,
table tr.selected td {
  background: #ffc;
  border-color: #eeb;
}
table.system-status-report tr {
  border-bottom: 1px solid #ccc;
}
table.system-status-report tr.ok {
  color: #255b1e;
  background-color: #e5ffe2;
}
table.system-status-report tr.info {
  color: #040f37;
  background-color: #bdf;
}
table.system-status-report tr.warning {
  color: #840;
  background-color: #fffce5;
}
table.system-status-report tr.error {
  color: #8c2e0b;
  background-color: #fef5f1;
}
</style>
<script>
function valid()
{
	if(confirm('Do you want to save?'))
		return true;
	else
		return false;
}
</script>