<?php include("../../include/dbconnect.php"); 

$result = mysql_query("select account_year from company") or die(mysql_error());
$row = mysql_fetch_row($result);
$account_year = $row[0];

if ( isset($submit) && $submit == "Submit")
{
	for($ind = 0;$ind <=count($record);$ind++)
	{
			if($canc[$ind]=='Yes')
			{
			
				$result = mysql_query("select cust_record from salesreg where invno='$invnos[$ind]'  and drawing_no ='TOTAL' ") or die(mysql_error());
				$row = mysql_fetch_row($result);
				$cust_record=$row[0];

				$query=	"select order_record,remarks,quantity,record from salesreg where invno='$invnos[$ind]'  and account_year='$account_year' "  ;
				$salresult = mysql_query($query) or die(mysql_error());
				while($salrow = mysql_fetch_row($salresult))
				{	
						$order_record=$salrow[0];
						$s_remarks = $salrow[1];
						$quantity = $salrow[2];
						$record = $salrow[3];

						if(empty($row[1]))
						{
							$new_s_remarks=$remarks[$ind];
						}
						else
						{
							$new_s_remarks=$row[2].','.$remarks[$ind];
						
						}			
						
						$query = "update salesreg set remarks='$new_s_remarks',drawing_no ='CANCELLED',quantity='0',value='0',min_no='Cancelled',packing_amt=0,bed_amt=0,cess_amt=0,vat_amount=0,gtotal=0,po_no='Cancelled',freight=0,total=0 where record ='$record' ";
						mysql_query($query) or die(mysql_error());
						echo("Invoice Qty is cancelled and reversed to Order Book as pending to issue<br>");

					
					
						$result = mysql_query("select issued_qty,balance_qty,remarks from orders where record= '$order_record' ") or die(mysql_error());
						$row = mysql_fetch_row($result);
				
						$issued_qty=$row[0]- $quantity;
						$balance_qty=$row[1]+$quantity;
			
			

			
						$query="update orders set  issued_qty ='$issued_qty',balance_qty = '$balance_qty' where record = '$order_record' ";
						mysql_query($query) or die(mysql_error());
				}		
					$query =" delete from custledger where record = '$cust_record' ";
					 mysql_query($query) or die(mysql_error());
					
										
				echo(" Invoice amount removed from Customer Ledger also<br>");				
				
			}
	}
}
?>
<table>
<tr><td colspan=4 align=middle>

<h4><a href="view_sele.php">View Order Details  </a>
<h4><a href="reject_input_new.php">Enter Another Approval Entry  </a>

<br><br><font size=1><center><a href= "../../index.html">Return to index page</center></font></table>
