<?php
session_start();
include '../db_connect.php';
include '../db_table_select.php';

$totamount=0;
$tdsamount = 0;
if($_POST[vchtype]=='PURCHASE'||$_POST[vchtype]=='RECEIPT')
if($_POST['tds']>0){
$tdsamount=round(($_POST['amount']-$_POST['sortpayment'])*$_POST['tds']/100);
}
else{
	$tdsamount=round($_POST['tdsamt']);
}
$totamount=round(($_POST['amount']-$_POST['sortpayment'])-$tdsamount);

$tdsVchNo = $_POST[vchno];





if ($_POST[vchtype]=='RECEIPT'&&$_POST[bookvchtype]=='YES'){
	
	$sqlQry = "select vchno,bill_no from vchreg where bill_no = '$_POST[bill_no]'";
	if($sqlRes = mysqli_query($con,$sqlQry))
	{
		if(mysqli_num_rows($sqlRes)>0)
		{
			$sqlRow = mysqli_fetch_assoc($sqlRes);
			$_GET['mode'] = "update";
			$vchno = $sqlRow['vchno'];
			$bill_no = $sqlRow['bill_no'];
		}
		
	}
	
}

if ($_POST[vchtype]=='PAYMENT'&&$_POST[bookvchtype]=='YES'){
	
	$sqlQry = "select vchno,bill_no from vchreg where bill_no = '$_POST[bill_no]'";
	if($sqlRes = mysqli_query($con,$sqlQry))
	{
		if(mysqli_num_rows($sqlRes)>0)
		{
			$sqlRow = mysqli_fetch_assoc($sqlRes);
			$_GET['mode'] = "update";
			$vchno = $sqlRow['vchno'];
			$bill_no = $sqlRow['bill_no'];
		}
		
	}
	
}

if ($_POST[vchtype]=='PURCHASE'&&isset($_GET['tmode'])&&$_GET['tmode']=='purchasebill'){
	$sqlQry = "select vchno,bill_no from purcreg where bill_no = '$_POST[bill_no]'";
	if($sqlRes = mysqli_query($con,$sqlQry))
	{
		if(mysqli_num_rows($sqlRes)>0)
		{
			$sqlRow = mysqli_fetch_assoc($sqlRes);
			$_GET['mode'] = "update";
			$vchno = $sqlRow['vchno'];
			$bill_no = $sqlRow['bill_no'];
		}
		
	}
}
else 
{
	$vchno = $_POST['vchno'];
}

if($_GET['mode']=="update")
{
	if ($_POST[vchtype]=='PURCHASE'){
	$sqlupdate = "UPDATE `purcreg` SET `refno` = '$_POST[refno]', `comcode` = '$_POST[comcode]', `vchtype` = '$_POST[vchtype]',`account` = '$_POST[account]',`narration` = '$_POST[narration]', `vchdate`=STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'), `dramount`='$_POST[amount]' , `cramount`='$_POST[tds]' ,`tdsamt`='$tdsamount',`chargeable`='$_POST[chargeable]',  `sortpayment`='$_POST[sortpayment]', `totamount`='$totamount', `agnstref`='$_POST[agnstref]' ,`basic_amt`='$_POST[basic_amt]',`toll_park`='$_POST[toll_park]',`ser_tax`='$_POST[ser_tax]',`bill_no` = '$_POST[bill_no]',`educess` = '$_POST[vhecess]',`seducess`='$_POST[vsurchg]',`ser_per`='$_POST[ser_per]',`misamt`='$_POST[misamt]', `invdate`=STR_TO_DATE('$_POST[invdate]','%d/%m/%Y'),`subtotal`='$_POST[subtotal]' ,`timestamp` = 'NOW()' WHERE `purcreg`.`vchno` = '$vchno';";
	}

elseif ($_POST[vchtype]=='SALES'){
	$sqlupdate = "UPDATE `purcreg` SET `refno` = '$_POST[refno]', `comcode` = '$_POST[comcode]', `vchtype` = '$_POST[vchtype]',`account` = '$_POST[account]',`narration` = '$_POST[narration]', `vchdate`=STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'), `dramount`='$_POST[amount]' , `cramount`='$_POST[tds]' ,`tdsamt`='$tdsamount',`chargeable`='$_POST[chargeable]',  `sortpayment`='$_POST[sortpayment]', `totamount`='$totamount', `agnstref`='$_POST[agnstref]' ,`basic_amt`='$_POST[basic_amt]',`toll_park`='$_POST[toll_park]',`ser_tax`='$_POST[ser_tax]',`bill_no`='$_POST[bill_no]',`educess`='$_POST[vhecess]',`seducess`='$_POST[vsurchg]',`ser_per`='$_POST[ser_per]',`misamt`='$_POST[misamt]', `invdate`=STR_TO_DATE('$_POST[invdate]','%d/%m/%Y'),`subtotal`='$_POST[subtotal]',`timestamp` = 'NOW()' WHERE `purcreg`.`vchno`='$_POST[vchno]';";
}
elseif ($_POST[vchtype]=='RECEIPT'&&$_POST[bookvchtype]=='YES'){
	
	$sqlQry = "select * from vchreg where bill_no = '$_POST[bill_no]'";
	if($sqlRes = mysqli_query($con,$sqlQry))
	{
		if(mysqli_num_rows($sqlRes)>0)
		{
			$sqlRow = mysqli_fetch_assoc($sqlRes);
				$sqlupdate = "update vchreg set  dramount = '$_POST[amount]' , subtotal =  '$_POST[amount]' where bill_no = '$bill_no' and vchno = '$vchno'";
		}
	}
	
	
		
	
	}
	elseif ($_POST[vchtype]=='PAYMENT'&&$_POST[bookvchtype]=='YES'){
	
	$sqlQry = "select * from vchreg where bill_no = '$_POST[bill_no]'";
	if($sqlRes = mysqli_query($con,$sqlQry))
	{
		if(mysqli_num_rows($sqlRes)>0)
		{
			$sqlRow = mysqli_fetch_assoc($sqlRes);
				$sqlupdate = "update vchreg set  dramount = '$_POST[amount]' , subtotal =  '$_POST[amount]' where bill_no = '$bill_no' and vchno = '$vchno'";
		}
	}
	
	
		
	
	}	
else{
	$sqlupdate = "UPDATE `vchreg` SET `refno` = '$_POST[refno]', `comcode` = '$_POST[comcode]', `vchtype` = '$_POST[vchtype]',`account` = '$_POST[account]',`narration` = '$_POST[narration]', `vchdate`=STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'),`bankname` = '$_POST[bankname]',`details` = '$_POST[details]',  `dramount`='$_POST[amount]', `cramount`='$_POST[tds]' ,`tdsamt`='$tdsamount',`chargeable`='$_POST[chargeable]',  `sortpayment`='$_POST[sortpayment]', `totamount`='$totamount', `agnstref`='$_POST[agnstref]' ,`basic_amt`='$_POST[basic_amt]',`toll_park`='$_POST[toll_park]',`ser_tax`='$_POST[ser_tax]',`bill_no` = '$_POST[bill_no]', `invdate`=STR_TO_DATE('$_POST[invdate]','%d/%m/%Y'),`subtotal`='$_POST[subtotal]',`timestamp` = 'NOW()' WHERE `vchreg`.`vchno` = '$_POST[vchno]';";
	}
if(mysqli_query($con,$sqlupdate))

{
	
	if ($_POST[vchtype]=='RECEIPT'&&$_POST[bookvchtype]=='YES'){
	$sql = "update vchdetails set  amount = '$_POST[amount]' where vchno = '$vchno';";
	mysqli_query($con,$sql);
	}
	
	if ($_POST[vchtype]=='PAYMENT'&&$_POST[bookvchtype]=='YES'){
	$sql = "update vchdetails set  amount = '$_POST[amount]' where vchno = '$vchno';";
	mysqli_query($con,$sql);
	}
	//Update JV for TDS
	
	if($_POST[vchtype]=='PURCHASE'&&$tdsamount>0)
	{
		//$refno = getReferenceNo($con,'JOURNAL');
	
		if($_POST['tds']=='1')
		{
			$account="TDS DEDUCTED NON COM. SUPPLIER";
		}
		else
		{
			$account="TDS DEDUCTED COM. SUPPLIER";
		}
		if(mysqli_query($con,"UPDATE `vchreg` set `vchdate` = STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'),`bankname` = '$_POST[bankname]',`details` = '$_POST[details]', `refno` = '$refno', `comcode` = '$_POST[comcode]', `dramount` ='$tdsamount',`cramount` = '', `account` = '$account', `narration` = '', `expst` = '$_POST[expst]',`tdsamt` = '',`chargeable` = '',`sortpayment` = '',`totamount` = '$tdsamount',`agnstref` = '$_POST[agnstref]',`basic_amt` = '',`toll_park` = '',`ser_tax` = '',invdate = STR_TO_DATE('$_POST[invdate]','%d/%m/%Y'),`timestamp` = 'NOW()' where`bill_no` = $tdsVchNo and `vchtype` ='JOURNAL';"))
		{
			//updateReferenceNo($con,$refno,'JOURNAL');
		}
	}
	elseif ($_POST[vchtype]=='RECEIPT'&&$tdsamount>0)
	{
			
		//$refno = getReferenceNo($con,'JOURNAL');
		$account="TDS DEDUCTED BY CLIENTS";
		if(mysqli_query($con,"UPDATE `vchreg` set `vchdate` = STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'),`bankname` = '$_POST[bankname]',`details` = '$_POST[details]', `refno` = '$refno', `comcode` = '$_POST[comcode]', `dramount` ='$tdsamount',`cramount` = '', `account` = '$account', `narration` = '', `expst` = '$_POST[expst]',`tdsamt` = '',`chargeable` = '',`sortpayment` = '',`totamount` = '$tdsamount',`agnstref` = '$_POST[agnstref]',`basic_amt` = '',`toll_park` = '',`ser_tax` = '',invdate = STR_TO_DATE('$_POST[invdate]','%d/%m/%Y') ,`timestamp` = 'NOW()' where`bill_no` = $tdsVchNo and `vchtype` ='JOURNAL';"))
		{
			//updateReferenceNo($con,$refno,'JOURNAL');
		}
	}
	
//log table updation


	  
mysqli_query($con,"INSERT INTO `logtable` (`id`,`entrydate`, `det1`, `det2`, `det3`, `det4`, `det5`, `det6`, `det7`, `det8`, `det9`, `det10`, `det11`, `det12`) VALUES (NULL,CURDATE(), '$_SESSION[SESS_FIRST_NAME]', '$_SERVER[REMOTE_ADDR]','$_POST[refno]','$_POST[vchno]', '$_POST[comcode]', '$_POST[vchtype]','$_POST[account]',STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'), '$_POST[amount]', '$totamount','$_SESSION[SESS_MEMBER_ID]', '".date("d/m/Y")."/".date("H:i")."');");	  
	  $to = "$_SESSION[SESS_MAILTO]";    
$subject = "Record Intimation for $bookid-$_POST[billedto]-$_POST[usedby]-$_POST[dutydate]";
$message = 
				"This message was sent from:\n" .
				"$_SESSION[SESS_FIRST_NAME]/$_SERVER[REMOTE_ADDR]/" .gethostbyaddr($_SERVER['REMOTE_ADDR'])."\n".
				"-----------------------------DETAILS--------------------------\n" .
				"Refno        	: $_POST[refno]\n" .
				"Vchno			: $_POST[vchno]\n".
				"Comcode        : $_POST[comcode]\n" .
				"vchtype 		: $_POST[vchtype]\n" .
				"Account	    : $_POST[account]\n" .
				"VchDate.     	: STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y')\n" .
				"Amount.	    : $_POST[amount]\n" .
				"Total       	: $totamount\n" .
				"User Id        : $_SESSION[SESS_MEMBER_ID]\n" .
				"Date/time      :".date("d/m/Y")."/".date("H:i")."\n".
				
				"-----------------------------DETAILS--------------------------\n" ;
				
$from = "$_SESSION[SESS_MAILFROM]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
	  
	  

	
	
	
	
 // log table update end
 if ($_POST[vchtype]=='PURCHASE'&&isset($_GET['tmode'])&&$_GET['tmode']=='purchasebill'){
	 echo "$vchno###massdet###Record Inserted Susscessfully!!!";
 }
 else
echo "Record Updated Susscessfully!!!";
}

else
echo "Error:".mysqli_error($con);
}

if($_GET['mode']=="insert")
{
	$refno = getReferenceNo($con,$_POST[vchtype]);
	$vchrefno = $refno ;
if ($_POST[vchtype]=='PURCHASE'){
	$sqlinsert = "INSERT INTO `purcreg` (`vchno`, `cmp`, `fny` , `vchdate`,`bankname`,`details`, `refno`, `comcode`, `vchtype`, `dramount`, `cramount`, `account`, `narration`, `expst`,`tdsamt`,`chargeable`,`sortpayment`,`totamount`,`agnstref`,`basic_amt`,`toll_park`,`ser_tax`, `bill_no`,`educess`,`seducess`,`ser_per`,`misamt`, `invdate` ,`subtotal`,`timestamp`) VALUES (NULL, '$_SESSION[SESS_CMPC]', '$_SESSION[SESS_FNY]',STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'),'$_POST[bankname]', '$_POST[details]', '$refno', '$_POST[comcode]', '$_POST[vchtype]', '$_POST[amount]', '$_POST[tds]', '$_POST[account]', '$_POST[narration]', '$_POST[expst]', '$tdsamount','$_POST[chargeable]', '$_POST[sortpayment]', '$totamount','$_POST[agnstref]','$_POST[basic_amt]','$_POST[toll_park]','$_POST[ser_tax]','$_POST[bill_no]','$_POST[vhecess]','$_POST[vsurchg]','$_POST[ser_per]','$_POST[misamt]',STR_TO_DATE('$_POST[invdate]','%d/%m/%Y'),'$_POST[subtotal]',  'NOW()');";
	}

elseif ($_POST[vchtype]=='SALES'){
	$sqlinsert = "INSERT INTO `purcreg` (`vchno`, `cmp`, `fny` , `vchdate`,`bankname`,`details`, `refno`, `comcode`, `vchtype`, `dramount`, `cramount`, `account`, `narration`, `expst`,`tdsamt`,`chargeable`,`sortpayment`,`totamount`,`agnstref`,`basic_amt`,`toll_park`,`ser_tax`,`bill_no`,`educess`,`seducess`,`ser_per`,`misamt`,`invdate`,`subtotal`,`timestamp`) VALUES (NULL, '$_SESSION[SESS_CMPC]', '$_SESSION[SESS_FNY]',STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'),'$_POST[bankname]', '$_POST[details]', '$refno', '$_POST[comcode]', '$_POST[vchtype]', '$_POST[amount]', '$_POST[cramount]', '$_POST[account]', '$_POST[narration]', '$_POST[expst]', '$tdsamount','$_POST[chargeable]', '$_POST[sortpayment]', '$totamount','$_POST[agnstref]','$_POST[basic_amt]','$_POST[toll_park]','$_POST[ser_tax]','$_POST[bill_no]','$_POST[vhecess]','$_POST[vsurchg]','$_POST[ser_per]','$_POST[misamt]',STR_TO_DATE('$_POST[invdate]','%d/%m/%Y'),'$_POST[subtotal]',  'NOW()');";
	}
elseif ($_POST[vchtype]=='RECEIPT'&&$_POST[bookvchtype]=='YES'){
		
	
	$sqlinsert = "INSERT INTO `vchreg` (`vchno`, `cmp`, `fny` , `vchdate`,`bankname`,`details`, `refno`, `comcode`, `vchtype`, `dramount`, `cramount`, `account`, `narration`, `expst`,`tdsamt`,`chargeable`,`sortpayment`,`totamount`,`agnstref`,`basic_amt`,`toll_park`,`ser_tax`, `bill_no`, `invdate`,`subtotal`,`timestamp`) VALUES (NULL, '$_SESSION[SESS_CMPC]', '$_SESSION[SESS_FNY]',CURDATE(), '$refno', '$_POST[comcode]', '$_POST[vchtype]','$_POST[bankname]', '$_POST[details]', '$_POST[amount]', '', '$_POST[account]', '$_POST[narration]', '', '','', '$_POST[sortpayment]', '$_POST[amount]','$_POST[agnstref]','$_POST[amount]','','','$_POST[bill_no]',STR_TO_DATE('$_POST[invdate]','%d/%m/%Y'),'$_POST[amount]',  'NOW()');";
	
	
	}
	
	elseif ($_POST[vchtype]=='PAYMENT'&&$_POST[bookvchtype]=='YES'){
		
	
	$sqlinsert = "INSERT INTO `vchreg` (`vchno`, `cmp`, `fny` , `vchdate`,`bankname`,`details`, `refno`, `comcode`, `vchtype`, `dramount`, `cramount`, `account`, `narration`, `expst`,`tdsamt`,`chargeable`,`sortpayment`,`totamount`,`agnstref`,`basic_amt`,`toll_park`,`ser_tax`, `bill_no`, `invdate`,`subtotal`,`timestamp`) VALUES (NULL, '$_SESSION[SESS_CMPC]', '$_SESSION[SESS_FNY]',CURDATE(),'$_POST[bankname]', '$_POST[details]', '$refno', '$_POST[comcode]', '$_POST[vchtype]', '$_POST[amount]', '', '$_POST[account]', '$_POST[narration]', '', '','', '$_POST[sortpayment]', '$_POST[amount]','$_POST[agnstref]','$_POST[amount]','','','$_POST[bill_no]',STR_TO_DATE('$_POST[invdate]','%d/%m/%Y'),'$_POST[amount]',  'NOW()');";
	
	
	}	
else{
	$sqlinsert = "INSERT INTO `vchreg` (`vchno`, `cmp`, `fny` , `vchdate`,`bankname`,`details`, `refno`, `comcode`, `vchtype`, `dramount`, `cramount`, `account`, `narration`, `expst`,`tdsamt`,`chargeable`,`sortpayment`,`totamount`,`agnstref`,`basic_amt`,`toll_park`,`ser_tax`, `bill_no`, `invdate`,`subtotal`,`vchsource`,`timestamp`) VALUES (NULL, '$_SESSION[SESS_CMPC]', '$_SESSION[SESS_FNY]',STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'),'$_POST[bankname]', '$_POST[details]', '$refno', '$_POST[comcode]', '$_POST[vchtype]', '$_POST[amount]', '$_POST[cramount]', '$_POST[account]', '$_POST[narration]', '$_POST[expst]', '$tdsamount','$_POST[chargeable]', '$_POST[sortpayment]', '$totamount','$_POST[agnstref]','$_POST[basic_amt]','$_POST[toll_park]','$_POST[ser_tax]','$_POST[bill_no]',STR_TO_DATE('$_POST[invdate]','%d/%m/%Y'),'$_POST[subtotal]','$_POST[vchsource]',  'NOW()');";
	}
	

if(mysqli_query($con,$sqlinsert))
{
	$vchno= mysqli_insert_id($con);
	updateReferenceNo($con,$refno,$_POST[vchtype]);
	
	
	if ($_POST[vchtype]=='RECEIPT'&&$_POST[bookvchtype]=='YES'){
	$sql = "INSERT INTO vchdetails (`vchno`,  `billno`, `amount` ,`crbillno`, `cramount` ,`paytype`, `vchtype`) VALUES ('$vchno', '$_POST[agnstref]',  '$_POST[amount]', '',  '','Advance','RECEIPT')";
	mysqli_query($con,$sql);
	}
	
	//$refno=$_POST['refno'];
	
	//Update JV for TDS
	
	if($_POST[vchtype]=='PURCHASE'&&$tdsamount>0)
	{
		$refno = getReferenceNo($con,'JOURNAL');
	
		if($_POST['tds']=='1')
		{
			$account="TDS DEDUCTED NON COM. SUPPLIER";
		}
		else
		{
			$account="TDS DEDUCTED COM. SUPPLIER";
		}
		if(mysqli_query($con,"INSERT INTO `vchreg` (`vchno`, `cmp`, `fny` , `vchdate`,`bankname`,`details`,  `refno`, `comcode`, `vchtype`, `dramount`, `cramount`, `account`, `narration`, `expst`,`tdsamt`,`chargeable`,`sortpayment`,`totamount`,`agnstref`,`basic_amt`,`toll_park`,`ser_tax`,`bill_no`,invdate,`timestamp`) VALUES (NULL, '$_SESSION[SESS_CMPC]', '$_SESSION[SESS_FNY]',STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'), '$_POST[bankname]', '$_POST[details]', '$refno', '$_POST[comcode]', 'JOURNAL', '', '$tdsamount', '$account', ' ', '$_POST[expst]', '','', '', '$tdsamount','$_POST[agnstref]','','','',$vchno,STR_TO_DATE('$_POST[invdate]','%d/%m/%Y')),  'NOW()';"))
		{
			updateReferenceNo($con,$refno,'JOURNAL');
		}
	}
	elseif ($_POST[vchtype]=='RECEIPT'&&$tdsamount>0)
	{
			
		$refno = getReferenceNo($con,'JOURNAL');
		$account="TDS DEDUCTED BY CLIENTS";
		if(mysqli_query($con,"INSERT INTO `vchreg` (`vchno`, `cmp`, `fny` , `vchdate`,`bankname`,`details`, `refno`, `comcode`, `vchtype`, `dramount`, `cramount`, `account`, `narration`, `expst`,`tdsamt`,`chargeable`,`sortpayment`,`totamount`,`agnstref`,`basic_amt`,`toll_park`,`ser_tax`,`bill_no`,invdate,`timestamp`) VALUES (NULL, '$_SESSION[SESS_CMPC]', '$_SESSION[SESS_FNY]',STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'), '$_POST[bankname]', '$_POST[details]', '$refno', '$_POST[comcode]', 'JOURNAL', '$tdsamount', ' ', '$account', ' ', '$_POST[expst]', '','', '', '$tdsamount','$_POST[agnstref]','','','',$vchno,STR_TO_DATE('$_POST[invdate]','%d/%m/%Y')),  'NOW()';"))
		{
			updateReferenceNo($con,$refno,'JOURNAL');
		}
	}
	elseif	($_POST[vchtype]=='JOURNAL')  // update bookingreg form driver advance
	{
		mysqli_query($con,"UPDATE  $bookingreg SET  `vchnoref` =  '$vchno' WHERE  $bookingreg.`bookid` = '$_POST[bill_no]';");
		
	}
	
	elseif	($_POST[vchtype]=='RECEIPT')  // update bookingreg form driver advance
	{
		mysqli_query($con,"UPDATE  $bookingreg SET  `vchreceipt` =  '$vchno' WHERE  $bookingreg.`bookid` = '$_POST[bill_no]';");
		
		$sql = "INSERT INTO vchdetails (`vchno`,  `billno`, `amount` ,`crbillno`, `cramount` ,`paytype`, `vchtype`) VALUES ('$vchno', '$vchno',  '$_POST[amount]', '',  '','Advance','RECEIPT')";
	mysqli_query($con,$sql);
		
	}
	
	elseif	($_POST[vchtype]=='PAYMENT')  // update bookingreg form driver advance
	{
		mysqli_query($con,"UPDATE  $bookingreg SET  `vchpayment` =  '$vchno' WHERE  $bookingreg.`bookid` = '$_POST[bill_no]';");
		
		
		
	}
	
	//log table updation
	  
mysqli_query($con,"INSERT INTO `logtable` (`id`,`entrydate`, `det1`, `det2`, `det3`, `det4`, `det5`, `det6`, `det7`, `det8`, `det9`, `det10`, `det11`, `det12`) VALUES (NULL,CURDATE(), '$_SESSION[SESS_FIRST_NAME]', '$_SERVER[REMOTE_ADDR]','$_POST[refno]','$vchno', '$_POST[comcode]', '$_POST[vchtype]','$_POST[account]',STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y'), '$_POST[amount]', '$totamount','$_SESSION[SESS_MEMBER_ID]', '".date("d/m/Y")."/".date("H:i")."');");	  
	  $to = "$_SESSION[SESS_MAILTO]";    
$subject = "Record Intimation for $bookid-$_POST[billedto]-$_POST[usedby]-$_POST[dutydate]";
$message = 
				"This message was sent from:\n" .
				"$_SESSION[SESS_FIRST_NAME]/$_SERVER[REMOTE_ADDR]/" .gethostbyaddr($_SERVER['REMOTE_ADDR'])."\n".
				"-----------------------------DETAILS--------------------------\n" .
				"Refno        	: $_POST[refno]\n" .
				"Vchno			: $vchno\n".
				"Comcode        : $_POST[comcode]\n" .
				"vchtype 		: $_POST[vchtype]\n" .
				"Account	    : $_POST[account]\n" .
				"VchDate.     	: STR_TO_DATE('$_POST[vchdate]','%d/%m/%Y')\n" .
				"Amount.	    : $_POST[amount]\n" .
				"Total       	: $totamount\n" .
				"User Id        : $_SESSION[SESS_MEMBER_ID]\n" .
				"Date/time      :".date("d/m/Y")."/".date("H:i")."\n".
				
				"-----------------------------DETAILS--------------------------\n" ;
				
$from = "$_SESSION[SESS_MAILFROM]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
	  
	  
	
	
	 // log table update end

	echo "$vchno###$vchrefno###Record Inserted Susscessfully!!!";

}


else
echo "Error:".mysqli_error($con);
}

function getReferenceNo($con,$vchType)
{
	if($SqlResult=mysqli_query($con,"SELECT refno FROM `getvchno` where cmp like '$_SESSION[SESS_CMPC]' and fny like '$_SESSION[SESS_FNY]' and `vchtype` like '$vchType'  "))
	{

		$row = mysqli_fetch_assoc($SqlResult);

		$refno=$row['refno'];
		
		return $refno;
		

	}
}

function updateReferenceNo($con,$refno,$vchType)
{
	//increment the reference# only after the Coucher is created
	(mysqli_query($con,"UPDATE `getvchno` SET `refno` = $refno+1,`timestamp` = 'NOW()' WHERE cmp like '$_SESSION[SESS_CMPC]' and fny like '$_SESSION[SESS_FNY]' and `vchtype` like '$vchType'"));
	
}



?>

