Conditionally display excluding / including tax?

O

Olav Tollefsen

I'm creating an ASP.NET e-commerce web site and I have to conditionally
(depending on site / user settings) display prices either excluding or
including tax. Prices are typically read from a database (where also the tax
rates are stored) various places in the code and typically (but now always)
the values are returned in DataSets which are bound to controls and
displayed to the user.

What would be the most elegant way to implement the logic to add tax to the
values presented to the user depending on some kind of setting?

Olav
 
K

Karl Seguin

Olav:
This might be overkill, but my personal preference would be to create a
custom server control which inherits from Label (assuming these are just
text), or TextBox (if the user can enter information), something as simple
as:

this code is just from the top of my head, but something like it ought to
work.

public class PriceDisplay
Inherits Label

private _price as decimal
public property Price() as Decimal
get
return _price
end get
set (ByVal value as Decimal)
_price = value
end set
end property

Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
If SHOULD_CALCULATE_TAX Then
_price = _price * (1 + GetTaxAmount())
End If
MyBase.Text = _price.ToString()
End Sub
end class

not that you can use a format string inside ToString() to get it to render
just like you want...

anyways, that's my $0.02...realize that I obviously left the the
SHOULD_CALCULATE_TAX logic as well as the GetTaxAmount() up to you to do...

Karl
 
O

Olav Tollefsen

Thanks for your suggestion!

Right now I lean towards doing the tax calculations closer to the database
layer. The main reason for this is that tax calculation could be quite
complex and depend heavily on knowing various business rules and exactly
what objects you are calculating tax on. It might not always be a flat rate
applied to all product prices.

This is an example on one way to do this (with a flat rate):

DataSet products = Product.GetProducts(catalogId, categoryId);

// Add the Price column (computed)
DataColumn column = new DataColumn("Price");
column.DataType = typeof(System.Double);
if (taxIncluded)
column.Expression = "(Listprice * (1 + TaxRate))";
else
column.Expression = "(Listprice)";
products.Tables["Products"].Columns.Add(column);

Now I can bind this dataset to a control and display the Price column.

Any other ideas / suggestions?

Olav
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top