<?php
include_once('../dashboard_tapal.php');
require_once('../convert_marathi.php');
require_once('db.php');
 ############################  Database Fill To Fill Stamp Details To Dispatched Letters #######################
$id=$_GET['num'];
$total=$_POST['total'];
$remark=$_POST['remark'];
convert_marathi();
$str="";
$q=mysql_query("SELECT stamp_nm,value,stock from stamp_master");
while($r=mysql_fetch_array($q))
{
	$name=$r['value'];
	$tic=$_POST[$name];
	$stk=$r['stock'];
	if($tic > $stk)
	{
		$flag="true";
		break;
		
	}
	
	else
	{
		$abc=$name.":".$tic;
		/////update stock/////////
		$query=mysql_query("SELECT stock from stamp_master where value='$name' ");
		$rs=mysql_fetch_array($query);
		$stock=$rs['stock']-$tic;
		mysql_query("UPDATE stamp_master SET stock='$stock' where value='$name' ");
		
		if($str=="")
		{
			$str=$str."".$abc;
		}
		else
		{
			$str=$str.",".$abc;
		}
		$flag="false";
	}
}


if($flag=="true")
{
	echo "<script type='text/javascript'>alert('Insufficient stock is available!')</script>";
	echo '<script language="JavaScript"> window.location.href ="../allocate_stamp_form.php?id='.$id.'" </script>';	
}
else
{
	$curdate=date("Y-m-d");
	$qry1=("UPDATE dispatched_transaction SET tic_1='$str',total='$total',remark='$remark',tapal_dispatched_date='$curdate' WHERE dispatched_id=$id");	

	if(mysql_query($qry1))
	{ 
		echo "<script type='text/javascript'>alert('Updated Successfully!')</script>";
		echo '<script language="JavaScript"> window.location.href ="../allocatestamptable.php" </script>';	
	}
	else
	{
		echo '<script language="JavaScript"> window.location.href ="../allocatestamptable.php" </script>';
	}

}

?>
