Taxes in email

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
joewkm
Ensign (ENS)
Ensign (ENS)
Posts: 8
Joined: Mon Jun 01, 2009 10:55 pm

Taxes in email

Post by joewkm » Wed Aug 05, 2009 10:34 pm

Hi,

I'm trying to display Canadian taxes on my email invoice. All 3 taxes, PST,GST & HST. Whoever buys from which province, that province's taxation rules applys.

I managed to do it on the screen and printable version because I modify OrderTotalSummary.ascx.cs to look lile this.

protected void Page_Init(object sender, EventArgs e)
{
_OrderId = AlwaysConvert.ToInt(Request.QueryString["OrderId"]);
_Order = OrderDataSource.Load(_OrderId);
LSDecimal subtotal = 0;
LSDecimal shipping = 0;
LSDecimal taxes = 0;

LSDecimal taxesPST = 0;
LSDecimal taxesGST = 0;
LSDecimal taxesHST = 0;
LSDecimal coupons = 0;
LSDecimal total = 0;
LSDecimal giftwrap = 0;
LSDecimal adjustments = 0;
foreach (OrderItem item in _Order.Items)
{
LSDecimal extendedPrice = item.ExtendedPrice;
switch (item.OrderItemType)
{
case OrderItemType.Shipping:
case OrderItemType.Handling:
shipping += extendedPrice;
break;
case OrderItemType.Tax:
// taxes += extendedPrice;
if (item.Name == "PST") {
taxesPST += extendedPrice;
}
if (item.Name == "GST") {
taxesGST += extendedPrice;
}
if (item.Name == "HST") {
taxesHST += extendedPrice;
}
break;
case OrderItemType.Coupon:
coupons += extendedPrice;
break;
case OrderItemType.GiftWrap:
giftwrap += extendedPrice;
break;
case OrderItemType.Charge:
case OrderItemType.Credit:
adjustments += extendedPrice;
break;
default:
subtotal += extendedPrice;
break;
}
total += item.ExtendedPrice;
}
Subtotal.Text = subtotal.ToString("ulc");
Shipping.Text = shipping.ToString("ulc");
// Taxes.Text = taxes.ToString("ulc");
TaxesPST.Text = taxesPST.ToString("ulc");
TaxesGST.Text = taxesGST.ToString("ulc");
TaxesHST.Text = taxesHST.ToString("ulc");
Total.Text = total.ToString("ulc");
if (giftwrap != 0)
{
trGiftWrap.Visible = true;
GiftWrap.Text = giftwrap.ToString("ulc");
}
else trGiftWrap.Visible = false;

if (coupons != 0)
{
trCoupon.Visible = true;
Coupons.Text = coupons.ToString("ulc");
}
else trCoupon.Visible = false;

if (adjustments != 0)
{
trAdjustments.Visible = true;
Adjustments.Text = adjustments.ToString("ulc");
}
else trAdjustments.Visible = false;
}

The resultant display on the screen invoice looks like this: (e.g. the billing address is from BC)

PAYMENT INFORMATION
Order Summary
Item Subtotal: CAD $40.00
Shipping: CAD $12.39
PST: CAD $2.80
GST: CAD $2.00
HST: CAD $0.00
--------------------------------------------------------------------------------
Total: CAD $57.19


However, I had a hard time doing the same with the email version because nvelocity dynamic variables cannot do decimal maths. I have to sum up in group all 3 different taxes applicable to one line item and there are more than one line items per invoice. nvelocity would not add decimal number.

Google on the net does not give me a satisfactory answer with nvelocity solution.

Does anybody has any idea how best to approach this problem? If there is a cs file I can modify like the above, which one can I change?

Many thanks,
Joe

kazirahul
Ensign (ENS)
Ensign (ENS)
Posts: 1
Joined: Thu Aug 06, 2009 1:18 am

Re: Taxes in email

Post by kazirahul » Thu Aug 06, 2009 2:07 am

managed to do it on the screen and printable version because I modify OrderTotalSummary.ascx.cs to look lile this.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Taxes in email

Post by mazhar » Thu Aug 06, 2009 3:22 am

Read following thread, it may help you
viewtopic.php?f=42&t=8717

joewkm
Ensign (ENS)
Ensign (ENS)
Posts: 8
Joined: Mon Jun 01, 2009 10:55 pm

Re: Taxes in email

Post by joewkm » Wed Aug 12, 2009 10:47 pm

Hi,

I have read the post on viewtopic.php?f=42&t=8717 looks like a tangible solution and alternative to the nvelocity problem. Can you tell me which file and source line exactly trigger the invoice email when then confirm payment button is clicked?

Would this be the right place to put the email nvelocity variables before calling the email template send. I'm not exactly good at reading source code. I search through all the source file and could not find it.

Thanks,
Joe

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Taxes in email

Post by mazhar » Thu Aug 13, 2009 4:25 am

Code to trigger confirmation Email is in back end, so you can't modify it to incorporate your custom info. There are two possibilities if you want to send processes information to your Email template. First is that every basket item contains a property called LineMessage. You can pass data in that property for some basket item and it will be available in Email template on order item. For example in OnePageCheckout.ascx.cs class's CheckingOut method you can process basket and find out calculated tax and then pass in that tax in LineMessage field of some basket item.

Second approach could be to disable the Order Confirmation Email trigger and to send confirmation Email manually. In this case you would need to put some code in CheckedOut method of OnePageCheckout.ascx.cs. Read following thread about manual Emails, for custom parameter you can just create another parameter in parameter list and access it in Email template just like the other ones
viewtopic.php?f=42&t=9243

joewkm
Ensign (ENS)
Ensign (ENS)
Posts: 8
Joined: Mon Jun 01, 2009 10:55 pm

Re: Taxes in email

Post by joewkm » Sat Aug 15, 2009 10:28 am

Thank Mazhar,

I've got my problem fixed. I used the second approach, disabled the default email trigger and trigger the email manually at the ReceiptPage.ascx.cs. I did the tax calculation there and added the additional emailTemplate parameters and the email now able to show customized tax info. As such I would like to post my solution here for all who is looking for similar solution.

Solution:
1. Disable default email trigger on order confirmation in your admin panel.
2. On the ReceiptPage.ascx.cs, add the following lines to BindOrder().

// sum up all GST,PST,HST
LSDecimal taxesPST = 0;
LSDecimal taxesGST = 0;
LSDecimal taxesHST = 0;
foreach (OrderItem item in _Order.Items)
{
LSDecimal extendedPrice = item.ExtendedPrice;
switch (item.OrderItemType)
{
case OrderItemType.Tax:
if (item.Name == "PST") {
taxesPST += extendedPrice;
}
if (item.Name == "GST") {
taxesGST += extendedPrice;
}
if (item.Name == "HST") {
taxesHST += extendedPrice;
}
break;
default:
break;
}
}

To trigger the email manually add the following after the above code:

EmailTemplate emailTemplate = EmailTemplateDataSource.Load(1);
if(emailTemplate != null) {
Order order = OrderDataSource.Load(_OrderId);
if (order != null) {
emailTemplate.Parameters.Add("store", Token.Instance.Store);
emailTemplate.Parameters.Add("order", order);
emailTemplate.Parameters.Add("payments", order.Payments);
emailTemplate.Parameters.Add("customer", order.User);
emailTemplate.Parameters.Add("user", order.User);
emailTemplate.Parameters.Add("PST", taxesPST.ToString("ulc"));
emailTemplate.Parameters.Add("GST", taxesGST.ToString("ulc"));
emailTemplate.Parameters.Add("HST", taxesHST.ToString("ulc"));
emailTemplate.Send();
}
}

Where "EmailTemplate emailTemplate = EmailTemplateDataSource.Load(1);" uses the default order confirmation email template. You should pass in the emailTemplateID in Load(x) whichever your order confirmation template ID is. For my case is "1".

I also notice that these codes posted in viewtopic.php?f=42&t=9243 does not work:

EmailTemplate emailTemplate = EmailTemplateDataSource.LoadForCriteria("Name = 'Customer Order Notification' ");
if(emailTemplate != null)
{
Order order = OrderDataSource.Load(orderId);
if (order != null)
{
Hashtable parameters = new Hashtable();
parameters.Add("store", Token.Instance.Store);
parameters.Add("order", order);
parameters.Add("payments", order.Payments);
parameters.Add("customer", order.User);
parameters.Add("user", order.User);
emailTemplate.Parameters = parameters; // this line does not work and will error out
emailTemplate.Send();
}
}

because "emailTemplate.Parameters" is read only. It does not contain the set method.

3. Next modify your order confirmation template to show the new parameters.

...

<table class="Email">

....

<tr>
<td width="40%" style="background:#cccccc; color:#00000; text-align: right;">
<strong>PST:</strong>
</td>
<td width="15%" class="Email" style="text-align: right;">
${PST}
</td>
</tr>
<tr>
<td width="40%" style="background:#cccccc; color:#00000; text-align: right;">
<strong>GST:</strong>
</td>
<td width="15%" class="Email" style="text-align: right;">
${GST}
</td>
</tr>
<tr>
<td width="40%" style="background:#cccccc; color:#00000; text-align: right;">
<strong>HST:</strong>
</td>
<td width="15%" class="Email" style="text-align: right;">
${HST}
</td>
</tr>

....

</table>


4. You're done!

Thanks Mazhar for pointing to the right direction.

Joe

Post Reply