<body background="">
<?php include("../../include/dbconnect.php"); 

$sdate = $today;
$tddt = us($today);

	$result = mysqli_query($conn,"select account_year,start_date,end_date from branches ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$account_year = $row[0];
	$startdt = $row[1];
	$enddt = $row[2];

	
	if($tddt < $startdt or $tddt > $enddt)
	{
	?>
		<script language="JavaScript">
		alert("Selected date does not belong to current accounting year. Please recheck the date");
		</script>	
	<?
	exit;
	}

$result = mysqli_query($conn,"select max(trans_id) from tranid where account_year = '$account_year' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$trans_id = $row[0]+1;

$totamt=0;
$podt = substr($purdt,6,4).'-'.substr($purdt,3,2).'-'.substr($purdt,0,2);


$qtyresult = mysqli_query($conn,"select distinct bcode,compcode from purch ");
$prow = mysqli_fetch_row($qtyresult);
$bcode = $prow[0];
$compcode = $prow[1];


	
if($cnf=='Yes')
{
	$qtyresult = mysqli_query($conn,"select * from purch order by prodcode");
	while($prow = mysqli_fetch_row($qtyresult))
	{	
			$compcode = $prow[0];
			$bcode = $prow[1];
			$prodcode = $prow[2];
			$qty = $prow[3];
			$pmode = $prow[4];			
			$remarks = $prow[7];

			$result = mysqli_query($conn,"select prodname from  product where prodcode = '$prodcode' ") or die(mysqli_error());
			$row = mysqli_fetch_row($result);
			$prodname = trim($row[0]);						

			if(!empty($remarks))
			{
				$nar=$remarks;
			}
			else
			{
				$nar=$prodname;			
			}
			
			$batch_no = $bill_no.'/'.british($tddt);
			if($pmode == 1)
			{
				$purmode = 'I';
				if($remarks =='')
				$remarks = 'Issued for Production';
			}
			if($pmode == 2)
			{
				$purmode = 'R';
				if($remarks =='')
				$remarks = 'Receipt from Production';
			}
			if($pmode ==3)
			{
				$purmode = 'C';
				$pmode = 1;
				if($remarks =='')
				$remarks = 'Sent for Conversion';
			}
			if($pmode == 4)
			{
				$purmode = 'D';
				$pmode = 1;
				if($remarks =='')
				$remarks = 'Damage';
			}
			
			
			if($pmode == 1)
			{
				$query="insert into stktrans values ('$prodcode', '$bill_no', '$tddt', '','$qty', '$trans_id', '$taxtype', '$remarks', '$po_no', '$podt', '$taxamt','', '$netamt', '$compcode','$bcode','$purmode','', '$account_year','$batch_no','', NULL)";
				mysqli_query($conn,$query) or die (mysqli_error());			
			}
			elseif($pmode ==2)
			{
				$query="insert into stktrans values ('$prodcode', '$bill_no', '$tddt','$qty','',  '$trans_id', '$taxtype', '$remarks', '$po_no', '$podt', '$taxamt','', '$netamt', '$compcode','$bcode','$purmode', '','$account_year','$batch_no','', NULL)";
				mysqli_query($conn,$query) or die (mysqli_error());			
			
			}			
		}
}
		mysqli_query($conn,"insert into tranid values('$trans_id','$account_year')") or die(mysqli_error());
		mysqli_query($conn,"truncate purch ") or die(mysqli_error());
?>

<meta http-equiv="refresh" content="0 url=input.php">