Access tax base, tax rate and total tax in ticket template

Getting Tax Rates dynamically from DB …

function test(standardTax, reducedTax) {
	
	dlg.ShowMessage("PARMS\r\nst:"+standardTax +" rt:"+reducedTax);
	
//	standardTax = Number(standardTax.replace(/,/g,''));
//	reducedTax  = Number(reducedTax.replace(/,/g,''));

//	standardTax = Number(standardTax);
//	reducedTax  = Number(reducedTax);
	
	standardTax = Helper.ToNumber(standardTax);
	reducedTax  = Helper.ToNumber(reducedTax);

	dlg.ShowMessage("ToNumber()\r\nst:"+standardTax +" rt:"+reducedTax);


	var T1name = 'T1';
	var T2name = 'T2';
	
	var SQL = "SELECT [Id],[Name],[Rate],[Rate],[Rounding],[AccountTransactionType_Id] FROM [TaxTemplates] WHERE [Name]='"+T1name+"'";
	var res = sql.Exec(SQL);
	var	taxdata = res[0].split(',');
		T1name = taxdata[1];
	var T1rate = Helper.ToNumber(taxdata[2]);
	
	var SQL = "SELECT [Id],[Name],[Rate],[Rounding],[AccountTransactionType_Id] FROM [TaxTemplates] WHERE [Name]='"+T2name+"'";
	var res = sql.Exec(SQL);
	var	taxdata = res[0].split(',');
		T2name = taxdata[1];
	var T2rate = Helper.ToNumber(taxdata[2]);
	
	dlg.ShowMessage("TAXDATA\r\n"+T1name + ":" + T1rate + "\r\n" + T2name + ":" + T2rate);
	
	var standardNet   = standardTax / T2rate * 100;
	var reducedNet    = reducedTax / T1rate * 100;
	var standardGross = standardNet + standardTax;
	var reducedGross  = reducedNet + reducedTax;
	
	var text = 
	"<J00>základ "+T1rate+"%:|" + reducedNet + "\n" +
	"<J00>základ "+T2rate+"%:|" + standardNet + "\n" +
	"<J00>celkem "+T1rate+"%:|" + reducedGross + "\n" +
	"<J00>celkem "+T2rate+"%:|" + standardGross + "\n";
	
	return text;
	
}

So I’m not sure what all this “reducedTax”, “reducedNet”, “reducedGross” jargon is about, however, this is what I would be interested in seeing…

taxName    taxRate  taxable   tax          total
T1           15.00    10.43  1.57          12.00
T2           18.00     2.54  0.46           3.00
-                -    12.98  2.02          15.00

Background:

  • Tax “T1” is 15%
  • Tax “T2” is 18%

My prices include the Tax already, so I need to “backout” the tax to get the Taxable Amount.


Test Data:

Sell 2 items:

  • $12 item which includes 15% tax, so the Taxable Amount is actually 10.43 and the applied Tax Amount is 1.57 (which is 15% of 10.43), which totals 12.00

  • $3 item which includes 18% tax, so the Taxable Amount is actually 2.54 and the applied Tax Amount is 0.46 (which is 18% of 2.54), which totals 3.00


Final Output:

taxName    taxRate  taxable   tax          total
T1           15.00    10.43  1.57          12.00
T2           18.00     2.54  0.46           3.00
-                -    12.98  2.02          15.00

Not sure if that is the same as what you want, but here it is:

Name: Taxes
Handler: taxes
Script:

function taxTable(ta1,ta2) {
	// validate input parms
	ta1 = typeof(ta1)==='undefined' || ta1=='' ? '0' : ta1;
	ta2 = typeof(ta2)==='undefined' || ta2=='' ? '0' : ta2;

	dlg.ShowMessage("PARMS\r\nTA1:"+ta1 +"\r\nTA2:"+ta2);
	
	// convert parms from String to Number
	ta1 = Helper.ToNumber(ta1);
	ta2 = Helper.ToNumber(ta2);

	dlg.ShowMessage("ToNumber()\r\nTA1:"+ta1 +"\r\nTA2:"+ta2);
	
	// load parms Array
	var parms = [];
		parms.push(ta1);
		parms.push(ta2);

	// load taxes Array (rates will be updated later via SQL)
	var taxes = [];
		taxes.push({"name":"T1","rate":"1"});
		taxes.push({"name":"T2","rate":"1"});
		
	var taxmsg = 'TAXDATA';
	
	// use SQL to update Tax Rates in the taxes Array
	for (var t=0; t<taxes.length; t++) {
		var taxName = taxes[t].name;
		var SQL = "SELECT [Id],[Name],[Rate],[Rate],[Rounding],[AccountTransactionType_Id] FROM [TaxTemplates] WHERE [Name]='"+taxName+"'";
		var res = sql.Exec(SQL);
		var	taxdata = res[0].split(',');
			taxes[t].name = taxdata[1];
			taxes[t].rate = Helper.ToNumber(taxdata[2]);
		taxmsg += "\r\n"+taxes[t].name + ":" + taxes[t].rate;
	}

	dlg.ShowMessage(taxmsg);
	
	// load taxTable Array
	var taxTable = [];
	for (var t=0; t<taxes.length; t++) {
		var taxableAmount = parms[t] / (1 + (taxes[t].rate/100));
		var tax = parms[t] - taxableAmount;
		taxTable.push({"name":taxes[t].name,"rate":taxes[t].rate,"taxableAmount":taxableAmount,"tax":tax,"total":parms[t]});
	}

	// initialize total vars
	var taxableTotal = 0.00;
	var taxTotal = 0.00;
	var total = 0.00;
	
	// build output
	var text = "<J00>taxName|taxRate|taxable|tax|total" + "\n";
	for (var t=0; t<taxTable.length; t++) {
		text += "<J00>"+taxTable[t].name + "|";
		text += Helper.Format(taxTable[t].rate) + "|";
		text += Helper.Format(taxTable[t].taxableAmount) + "|";
		text += Helper.Format(taxTable[t].tax) + "|";
		text += Helper.Format(taxTable[t].total) + "\n";
		
		taxableTotal += taxTable[t].taxableAmount;
		taxTotal += taxTable[t].tax;
		total += taxTable[t].total;
	}
	text += "<J00>-|-|"+Helper.Format(taxableTotal)+"|"+Helper.Format(taxTotal)+"|"+Helper.Format(total);
	
	return text;
}

Sample Call:

{CALL:taxes.taxTable('{TICKET ORDER EXACT TOTAL EXP:O.Taxes.Contains("T1")}','{TICKET ORDER EXACT TOTAL EXP:O.Taxes.Contains("T2")}')}

That script uses Arrays and Objects so that I can reduce the code by using loops. All Arrays actually only contain 2 indices ([0] and [1]) because I have only 2 Taxes. If you have more (or less) than that, you would adjust the parameter list to accommodate, and the first pushes for parms[] and taxes[]. Then the rest of the script will just “auto-magically” work without the need to add more code or variables.

I’ve got no clue what I’m doing wrong:
[=TN(’{TICKET ORDER EXACT TOTAL EXP: O.Taxes.Contains(“DPH21”)}’)] stopped working suddenly no matter what I try:

EDIT: deleted part of my post as I’ve found a typo error :slight_smile:
Need to get some sleep now, will get back tomorrow. Thank you, GN.

LOL, we posted at the same time.

See my post just previous to yours.

Use a prefix such as <L00>


<L00> 
<L00>[=TN('{TICKET ORDER EXACT TOTAL EXP:O.Taxes.Contains("T1")}')]
<L00>[=TN('{TICKET ORDER EXACT TOTAL EXP:O.Taxes.Contains("T2")}')]
<L00> 
{CALL:taxes.taxTable('{TICKET ORDER EXACT TOTAL EXP:O.Taxes.Contains("T1")}','{TICKET ORDER EXACT TOTAL EXP:O.Taxes.Contains("T2")}')}
<L00> 

Oh, you’re right.

{CALL:eet.test2(’{TICKET ORDER EXACT TOTAL EXP:O.Taxes.Contains(“DPH21”)}’,’{TICKET ORDER EXACT TOTAL EXP:O.Taxes.Contains(“DPH15”)}’)}

function test2(Total21,Total15){
var Total21 = Helper.ToNumber(Total21);
var Total15 = Helper.ToNumber(Total15);
var Total = Total21 + Total15;
var Tax21 = Math.round(Total21 * 0.1736);
var Tax15 = Math.round(Total15 * 0.1304);
var Base21 = Total21 - Tax21;
var Base15 = Total15 - Tax15

works great! Thank you guys for all the effort.

The last thing I need is to get a sum of orders tagged with a specific tag. I have created an order tag group and added a single tag (togo). I need an expression that returns a sum of all orders tagged with “togo”. I searched the forums and tried modifying emre’s expression , e.g. {TICKET ORDER EXACT TOTAL EXP:O.Tags.Contains(“togo”)} with no success.

Try this:

<L00>[=TN('{TICKET ORDER EXACT TOTAL EXP:O.OrderTags.Contains("Pork")}')]

You can often look at the column names in the DB to find what you want: