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

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

$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];
echo("select tax_acctcode,cess_acctcode,reserved_word from salestax where taxtype = '$taxtype' <br>");

$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 = $row[2];


$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($tax_description == 'CST')
{
	$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'CSTSALES' ") or die(mysqli_error());
}
elseif($tax_description == '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];

		$nar = trim($prodname);						
//echo "$nar";


$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;
}

	
if ( isset($submit) && $submit == "Update")
{
$result="delete from ledger where  invno='$bill_no' and trans_id='$trans_id' and account_year='$account_year'";
//echo "$result";
		mysqli_query($conn,$result) or die (mysqli_error());

$result="delete from cform where  invno='$bill_no' and trans_id='$trans_id' and account_year='$account_year'";
//echo "$result";
		mysqli_query($conn,$result) or die (mysqli_error());

		$result = mysqli_query($conn,"select prodname from  product where prodcode = '$prodcode' ") or die(mysqli_error());
		$row = mysqli_fetch_row($result);
		$nar=$prodname;			
// transis update with condition of transid and saletype			

	$round_net=round($netamt,0);
	$diff= $round_net-$netamt;	
	

		$myresult = mysqli_query($conn,"select invno,account_year,trans_id from trading where record = '$record' ") or die(mysqli_error());
		$myrow = mysqli_fetch_row($myresult);
		$invno = $myrow[0];
		$account_year = $myrow[1];
		$trans_id = $myrow[2];



		$query="update  trading set invdate = '$tddt',value='$gtotal',  bed_amt='$dutyamt',  edcess_amt='$edcess',  hscess_amt='$hcess',  vat_amount='$stamt',  vatcess_amt='$stcessamt', total='$netamt', round_off='$diff', gtotal='$round_net', address='$address', vehicle_no='$vehicle_no', prepare_date='$prepdate', removal_date='$remdate', prepare_time = '$preptime', remove_time = '$remtime' where record='$record' and trans_id='$trans_id' and account_year='$account_year'";
		echo "0)$query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());

		$query="update  stktrans set date = '$tddt' where  trans_id='$trans_id' and account_year='$account_year' and invno = '$invno' ";
		mysqli_query($conn,$query) or die (mysqli_error());

		$query="update  trading set invdate = '$tddt' where  trans_id='$trans_id' and account_year='$account_year' and invno = '$invno' ";
		mysqli_query($conn,$query) or die (mysqli_error());





	if($gtotal > 0)
	{
			$nar = buyname.' '.$narr;
			
	
		//  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)$query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
	}


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

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


	if($diff > 0)
	{
			$nar = buyname.' '.$narr;
			
	
		//  LOCAL SALES CREDIT ENTRY
		$query="insert into ledger values ('$roundcode','$tddt','$nar','','$diff','CR','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "1)$query<br>";
		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)";
		echo "1)$query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
		
	}
	
	if($stamt > 0)
	{
		$nar = 'Local Sales Invno:'.$bill_no;
	
		//  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)$query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
	}
	if($stcessamt>0)
	{
		$nar = 'Local Sales Invno:'.$bill_no;

		// CESS CREDIT ENTRY
		$query="insert into ledger values ('$vatcess_taxcode','$tddt','$nar','','$stcessamt','CR','$bill_no','$compcode','$tax_type', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "4)$query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
	}			
			
	if($dutyamt>0)
	{

		$nar = 'Central Excise Duty paid a/c: '.$bill_no;
	
		// PLA CENVAT  CREDIT ENTRY
		$query="insert into ledger values ('$placode','$tddt','$nar','','$dutyamt','CR','$bill_no','$compcode','$cenv_type', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "5)$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','','$edcess','CR','$bill_no','$compcode','$cenv_type', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "6)$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','','$hcess','CR','$bill_no','$compcode','$cenv_type', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "7)$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','$dutyamt','','DB','$bill_no','$compcode','$cenv_type', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "8)$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','$edcess','','DB','$bill_no','$compcode','$cenv_type', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "9)$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','$hcess','','DB','$bill_no','$compcode','$cenv_type', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "10)$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','','$netamt','CR','$bill_no','$compcode','', '$branch',  '$trans_id', '', '$account_year','','','J','','templedger','','','','','$bcode',NULL)";
		echo "11)$query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());		

		$nar = 'Local 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 DEBIT $query<br>";
		mysqli_query($conn,$query) or die (mysqli_error());
				
	}



}

elseif (isset($submit) && $submit == "Delete")

{
$result="delete from ledger where  invno='$bill_no' and trans_id='$trans_id' and account_year='$account_year'";
echo "$result";
		mysqli_query($conn,$result) or die (mysqli_error());				
$result="delete from stktrans where  invno='$bill_no' and 	transact_type='S' and trans_id='$trans_id' and account_year='$account_year'";
echo "$result";
		mysqli_query($conn,$result) or die (mysqli_error());				
$result="delete from trading where  invno='$bill_no' and trans_id='$trans_id' and account_year='$account_year'";
echo "$result";
		mysqli_query($conn,$result) or die (mysqli_error());				

}






?>
<script language="JavaScript">
close();
</script>
<meta http-equiv="refresh" content="0 url=view2.php">
