-
Notifications
You must be signed in to change notification settings - Fork 21
/
ZipCodeTaxPart.cs
54 lines (44 loc) · 1.96 KB
/
ZipCodeTaxPart.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using Nwazet.Commerce.Services;
using Orchard.ContentManagement;
using Orchard.Environment.Extensions;
using System.ComponentModel.DataAnnotations;
namespace Nwazet.Commerce.Models {
[OrchardFeature("Nwazet.Taxes")]
public class ZipCodeTaxPart : ContentPart, ITax {
public string Name {
get { return this.Retrieve(r => r.Name); }
set { this.Store(r => r.Name, value); }
}
public int Priority {
get { return this.Retrieve(r => r.Priority); }
set { this.Store(r => r.Priority, value); }
}
[RegularExpression(@"^(\d{5}(,\s?|\t)0?\.\d*\r*\n*)*$",
ErrorMessage="Invalid rate format. Requires comma or tab seperated zip and rate values, one per line.")]
public string Rates {
get { return this.Retrieve(r => r.Rates); }
set { this.Store(r => r.Rates, value); }
}
public Dictionary<string, decimal> GetRates() {
var rates = new Dictionary<string, decimal>();
string[] rateLines = Rates.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach(var rateLine in rateLines) {
var rateSplit = rateLine.Split(new string[] { ",", "\t" }, 2, StringSplitOptions.None);
rates.Add(rateSplit[0], Convert.ToDecimal(rateSplit[1]));
}
return rates;
}
public decimal ComputeTax(IEnumerable<ShoppingCartQuantityProduct> productQuantities, decimal subtotal,
decimal shippingCost, string country, string zipCode) {
var rates = GetRates();
if (country == Country.UnitedStates && rates.ContainsKey(zipCode)) {
var rate = rates[zipCode];
var tax = (subtotal + shippingCost) * rate;
return tax;
}
return 0;
}
}
}