<body background="">

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

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



if($taxtype =='')
{
	?>
    <script language="javascript">
	  alert("Tax Type is not selected. Trading Invoice won't be saved. Please go back and refresh the screen and try submit");
	</script>
    <?
	exit;
}

if($bcode =='')
{
	?>
    <script language="javascript">
	  alert("Customer is not selected. Trading Invoice won't be saved. Please go back and refresh the screen and try submit");
	</script>
    <?
	exit;
}

$aresult = mysqli_query($conn,"select distinct account_year from company where compcode = '$compcode' ") or die(mysqli_error());
$arow = mysqli_fetch_row($aresult);
$account_year = $arow[0];



$abresult = mysqli_query($conn,"select invno,invdate,bcode from trading where compcode = '$compcode' and account_year = '$account_year' and invno = '$bill_no' ") or die(mysqli_error());
$abrow = mysqli_fetch_row($abresult);
if(is_array($abrow))
{

	$result = mysqli_query($conn,"select name from  buy where bcode = '$bcode' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$buyname = trim($row[0]);
		
	echo("<h2>Sorry this Trading Invoice No is already prepared. Can't prepare Trading Invoice with same No</h2><br>");
	echo('INV No: '.$bill_no.'&nbsp;dated &nbsp;'.british($abrow[1]).'&nbsp;&nbsp;for Customer&nbsp;&nbsp;'.$buyname.'<br>');
	
	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;

$result = mysqli_query($conn,"select distinct bcode from spurch ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$bcode = $row[0];
$selacctcode = $row[0];

$result = mysqli_query($conn,"select reserved_word from salestax where taxtype = '$taxtype' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$taxname = $row[0];

		$stresult = mysqli_query($conn,"select tax_acctcode,cess_acctcode,reserved_word from salestax where taxtype = '$taxtype' ") or die(mysqli_error());
		$srow = mysqli_fetch_row($stresult);
		$taxcode = $srow[0];
		$vatcess_taxcode = $srow[1];
		$taxname = $srow[2];
/*

if($taxname == 'VAT') 
{
	$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'OUTPUT TAX' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$taxcode = $row[0];

	$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'OUTPUT CESS' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$vatcess_taxcode = $row[0];

}
elseif($taxname == 'CST')
{
	$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'CST COLLECTION' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$taxcode = $row[0];
}
*/
$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'CASH' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$cashcode = $row[0];

$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'PLA' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$placode = $row[0];

$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'PLAEDCESS' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$plaedcesscode = $row[0];

$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'PLAHSCESS' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$plahscode = $row[0];

$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'EXCISE DUTY' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$excisedutycode = $row[0];


$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'EDCESS' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$edcesscode = $row[0];

$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'HSCESS' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$hscesscode = $row[0];

$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'DISCOUNT' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$discode = $row[0];

$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'ROUNDOFF' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$roundcode = $row[0];

if($taxname == 'CST')
{
	$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'CSTSALES' ") or die(mysqli_error());
}

elseif($taxname == 'VAT')
{
	$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'LOCALSALES' ") or die(mysqli_error());
}
$row = mysqli_fetch_row($result);
$salescode = $row[0];



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

$cresult = mysqli_query($conn,"select bed,cess,hcess from excise where tariff = '$cenv' ") or die(mysqli_error());
$crow = mysqli_fetch_row($cresult);
$bed = $crow[0];
$edcess = $crow[1];
$hscess = $crow[2];



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

$assesstotal =0;	
$valuetotal = 0;
$bedtotal = 0;
$edcesstotal =0;
$hscesstotal =0;
$gtotal = 0;
$gdiscount = 0;
if($cnf=='Yes')
{
	$qtyresult = mysqli_query($conn,"select * from spurch order by prodcode");
	while($prow = mysqli_fetch_row($qtyresult))
	{	
		$compcode = $prow[0];
		$bcode = $prow[1];
		$prodcode = $prow[2];
		$qty = $prow[3];
		$capacity = $prow[4];
		$rate = $prow[5];
		$discount = $prow[6];
		$remarks = $prow[7];
		$record = $prow[8];
		if($capacity >0)
		{
			$actualvalue = $capacity * $rate * $qty;
			$disamt = $actualvalue * $discount /100;
			$value = $actualvalue - $disamt;
		}
		else
		{
			$actualvalue = $rate * $qty;
			$disamt = $actualvalue * $discount /100;
			$value = $actualvalue - $disamt;
		
		}

		
		if($bed >0)
		{
			$val1=$bed;
		}
		if($edcess>0)
		{
			$val2 = $bed*$edcess/100;
		}
		if($hscess>0)
		{
			$val3 = $bed*$hscess/100;
		}
		$divider = 100+$val1+$val2+$val3;	
			
		if($divider >0)
		{	
			$assess_value = $value *100/$divider;
		}
		else
		{
			$assess_value = $value;
		}

		$gtotal = $gtotal + $value;
		$gdiscount = $gdiscount + $disamt;
		$assesstotal = $assesstotal + $assess_value;
			

		
		$dutyamt = $assess_value * $bed /100;
		$edcessamt = $dutyamt * $edcess /100;
		$hcessamt = $dutyamt *$hscess /100;
		$bedtotal = $bedtotal + $dutyamt;
		$edcesstotal = $edcesstotal + $edcessamt;
		$hscesstotal = $hscesstotal + $hcessamt;

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

		$result = mysqli_query($conn,"select name from  buy where bcode = '$bcode' ") or die(mysqli_error());
		$row = mysqli_fetch_row($result);
		$buyname = trim($row[0]);						


			$query="insert into stktrans values ('$prodcode', '$bill_no', '$tddt',  '', '$qty','$trans_id', '$taxtype', '$nar', '$po_no', '$podt', '$taxamt','', '$netamt', '$compcode','$bcode','S','$tddt', '$account_year', NULL)";
			mysqli_query($conn,$query) or die (mysqli_error());			

		$val = $value +$disamt;
		
		$query="insert into trading values ('$bill_no','$tddt','$compcode','$prodcode','$bcode','$qty','$capacity','$rate','$discount','$disamt','$val','$assess_value','$bed','$dutyamt','$edcess','$edcessamt','$hscess','$hcessamt','','','','','','','','','F','','','','','','','','','','','','$account_year','$remarks','$trans_id',NULL)";
		mysqli_query($conn,$query) or die (mysqli_error());
		
			
	}

	$gdiscount = round($gdiscount,2);
	$tresult = mysqli_query($conn,"select tax,cess from salestax where taxtype = '$taxtype' ") or die(mysqli_error());
	$trow = mysqli_fetch_row($tresult);
	$stax = $trow[0];
	$stcess = $trow[1];
	
	$cresult = mysqli_query($conn,"select bed,cess,hcess from excise where tariff = '$cenv' ") or die(mysqli_error());
	$crow = mysqli_fetch_row($cresult);
	$bed = $crow[0];
	$edcess = $crow[1];
	$hscess = $crow[2];
	

	$subtotal = round($assesstotal + $bedtotal + $edcesstotal + $hscesstotal,2);

	$stamt = round($subtotal * $stax /100,2);
	$stcessamt = round($stamt * $stcess/100,2);
	$netamt = $subtotal + $stamt + $stcessamt;
	$narr = 'As per bill no  T&nbsp;'.$bill_no;	
	if($taxname == 'CST')
	{		
		$narr = 'As per CST bill no T&nbsp;'.$bill_no;
	}
	elseif($taxname == 'VAT')
	{
		$narr = 'As per VAT bill no T&nbsp;'.$bill_no;
	}
	$gtotal = round($gtotal,2);
	
	$round_net=round($netamt,0);
	$diff= round($round_net-$netamt,2);	

	if($diff > 0)
	{
		$query="insert into ledger values ('$roundcode','$tddt','$nar','','$diff','CR','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		mysqli_query($conn,$query) or die (mysqli_error());
	}
	elseif($diff < 0)
	{
		$diff = $diff*-1;
		$query="insert into ledger values ('$roundcode','$tddt','$nar','$diff','','DR','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		mysqli_query($conn,$query) or die (mysqli_error());
	}

	
	if($gtotal > 0)
	{
	
			$nar = $buyname.' '.$narr;
			$gtotal = $gtotal+$gdiscount;
		//  LOCAL SALES CREDIT ENTRY
		$query="insert into ledger values ('$salescode','$tddt','$nar','','$gtotal','CR','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "1)LOCAL SALES CREDIT $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
	}
	if($gdiscount >0)
	{
		$nar = $buyname.' '.$narr;
		$query="insert into ledger values ('$discode','$tddt','$nar','$gdiscount','','DR','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "1)DISCOUNT DEBIT :$query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
	
	}

	if($netamt > 0)
	{

			$nar = $narr;
	
		//  PARTY ACCOUNT DEBIT ENTRY
		$query="insert into ledger values ('$selacctcode','$tddt','$nar','$round_net','','DB','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "2) PARTY ACCOUNT DEBIT $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
		if($taxname == 'CST')
		{
			$query="insert into cform values ('$selacctcode','$bill_no','$tddt','$nar','$round_net','$stamt','F','CF','$trans_id','','','','','$pur_no','$pur_date','$compcode','$account_year',NULL)";
			mysqli_query($conn,$query) or die (mysqli_error());		
		}
	}

	
	if($stamt > 0)
	{
	
				$nar = $narr;
	
		//  OUTPUT  TAX (CST / VAT CESS )CREDIT ENTRY
		$query="insert into ledger values ('$taxcode','$tddt','$nar','','$stamt','CR','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "3)OUTPUT TAX CREDIT $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
	}
	if($stcessamt>0)
	{
	
				$nar = $narr;
		// CESS CREDIT ENTRY
		$query="insert into ledger values ('$vatcess_taxcode','$tddt','$nar','','$stcessamt','CR','$bill_no','$compcode','$taxtype', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "4)CESS CREDIT ENTRY $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
	}			
			
	if($bedtotal>0)
	{
	
				$nar = 'Central Excise Duty paid a/c: '.$bill_no;
		// PLA CENVAT  CREDIT ENTRY
		$query="insert into ledger values ('$placode','$tddt','$nar','','$bedtotal','CR','$bill_no','$compcode','$cenv', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "5) CENVAT PLA CREDIT $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
	
				$nar = 'Primary ED Cess Paid a/c:'.$bill_no;	
		// PLA EDCESS  CREDIT ENTRY
		$query="insert into ledger values ('$plaedcesscode','$tddt','$nar','','$edcesstotal','CR','$bill_no','$compcode','$cenv', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "6)PLA EDCESS CREDIT $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
		
		
						$nar = ' S&H ED Cess Paid a/c:'.$bill_no;
		// PLA HS CESS  CREDIT ENTRY
		$query="insert into ledger values ('$plahscode','$tddt','$nar','','$hscesstotal','CR','$bill_no','$compcode','$cenv', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "7)PLA HS CESS CREDIT $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
	
					$nar = 'Personnel Ledger Account Inv:'.$bill_no;
		// CENVAT  DEBIT ENTRY
		$query="insert into ledger values ('$excisedutycode','$tddt','$nar','$bedtotal','','DB','$bill_no','$compcode','$cenv', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "8)CENVAT DEBIT ENTRY $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
		
		
						$nar = 'Primary ED Cess PLA a/c:'.$bill_no;
		// PLA EDCESS  CREDIT ENTRY
		$query="insert into ledger values ('$edcesscode','$tddt','$nar','$edcesstotal','','DB','$bill_no','$compcode','$cenv', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "9) EDCESS CREDIT ENTRY $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
		
						$nar = 'H&S ED Cess PLA  a/c:'.$bill_no;
		// PLA HS CESS  CREDIT ENTRY
		$query="insert into ledger values ('$hscesscode','$tddt','$nar','$hscesstotal','','DB','$bill_no','$compcode','$cenv', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "10)HS CESS CREDIT ENTRY $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());	
		
		
	}
	if($salemode == 'Cash')
	{
	
		$nar = $buyname.' Inv'.$bill_no;
		$query="insert into ledger values ('$cashcode','$tddt','$nar','$round_net','','DB','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "11)CASH CREDIT  $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());	
		
		$nar = 'Trading Sales Inv'.$bill_no;
		$query="insert into ledger values ('$bcode','$tddt','$nar','','$round_net','CR','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "12)CASH CREDIT $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
					
	}

	$prepdate = us($prepdate);
	$remdate = us($remdate);

	$query="insert into trading values ('$bill_no','$tddt','$compcode','TOTAL','$bcode','','','','','','$gtotal','$assesstotal','$bed','$bedtotal','$edcess','$edcesstotal','$hscess','$hscesstotal','$stax','$stamt','$stcess','$stcessamt','','$netamt','$diff','$round_net','F','$salemode','','$taxtype','$cenv','$prepdate','$remdate','$address','$vehicle_no','','$preptime','$remtime','$account_year','','$trans_id',NULL)";
	mysqli_query($conn,$query) or die (mysqli_error());
	echo("SALES REGISTER TOTAL ENTRY $query<br>");

	mysqli_query($conn,"insert into tranid values('$trans_id','$account_year')") or die(mysqli_error());
	mysqli_query($conn,"truncate spurch ") or die(mysqli_error());
	
}

if($sms == 'Yes')
{
	$bresult = mysqli_query($conn,"select fax,name from buy where bcode = '$bcode' ") or die(mysqli_error());
	$brow = mysqli_fetch_row($bresult);
	$mobile = $brow[0];
	$name = $brow[1];
	if($mobile > 0)
	{
		
		$message = 'Dear Customer, Thank you for your valuable order. It has been invoiced under inv'.$bill_no.'  dated  '.british($prepdate).' for an Amount Rs.'.$round_net;
		$message=addslashes($message);
		$hallo = sendsms($mobile,$message);
	}

}
//<meta http-equiv="refresh" content="0 url=index.html">
/*
?>
<br>
<script language="JavaScript">
close();
</script>
*/
?>

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

