Skip to content

Commit

Permalink
Basic solution for this enhancement - needs refining and polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
Navilois committed Jun 25, 2020
1 parent 5a0ea66 commit d767231
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using System.Text.RegularExpressions;
using System.Web.WebPages.Scope;
using DotNetNuke.Security;
using DotNetNuke.Security.Membership;
Expand All @@ -38,6 +39,7 @@
using Hotcakes.Commerce.Contacts;
using Hotcakes.Commerce.Dnn;
using Hotcakes.Commerce.Extensions;
using Hotcakes.Commerce.Globalization;
using Hotcakes.Commerce.Membership;
using Hotcakes.Commerce.Metrics;
using Hotcakes.Commerce.Orders;
Expand Down Expand Up @@ -405,6 +407,79 @@ public ActionResult GetCustomerData()
return new PreJsonResult(Web.Json.ObjectToJson(result));
}

/// <summary>
/// Gets called after a the VAT number was changed.
/// </summary>
/// <returns></returns>
[HccHttpPost]
public ActionResult ApplyEUVatRules()
{
// https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s21.html - 23.06.2020
Regex EUVatRegex = new Regex(@"^(
(AT)?U[0-9]{8} | # Austria
(BE)?0[0-9]{9} | # Belgium
(BG)?[0-9]{9,10} | # Bulgaria
(CY)?[0-9]{8}L | # Cyprus
(CZ)?[0-9]{8,10} | # Czech Republic
(DE)?[0-9]{9} | # Germany
(DK)?[0-9]{8} | # Denmark
(EE)?[0-9]{9} | # Estonia
(EL|GR)?[0-9]{9} | # Greece
(ES)?[0-9A-Z][0-9]{7}[0-9A-Z] | # Spain
(FI)?[0-9]{8} | # Finland
(FR)?[0-9A-Z]{2}[0-9]{9} | # France
(GB)?([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{3}) | # United Kingdom
(HU)?[0-9]{8} | # Hungary
(IE)?[0-9]S[0-9]{5}L | # Ireland
(IT)?[0-9]{11} | # Italy
(LT)?([0-9]{9}|[0-9]{12}) | # Lithuania
(LU)?[0-9]{8} | # Luxembourg
(LV)?[0-9]{11} | # Latvia
(MT)?[0-9]{8} | # Malta
(NL)?[0-9]{9}B[0-9]{2} | # Netherlands
(PL)?[0-9]{10} | # Poland
(PT)?[0-9]{9} | # Portugal
(RO)?[0-9]{2,10} | # Romania
(SE)?[0-9]{12} | # Sweden
(SI)?[0-9]{8} | # Slovenia
(SK)?[0-9]{10} # Slovakia
)$", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);


var countryRepo = Factory.CreateRepo<CountryRepository>();
var storeIsoCode = countryRepo.Find(HccApp.ContactServices.Addresses.FindStoreContactAddress().CountryBvin).IsoCode;

string result = null;
string userVatNumber = security.InputFilter(Regex.Replace(Request.Form["UserVatNumber"], "[-. ]", ""), PortalSecurity.FilterFlag.NoMarkup);
bool foundMatch = EUVatRegex.IsMatch(userVatNumber);

var orderid = Request.Form["OrderId"] ?? string.Empty;
orderid = security.InputFilter(orderid.Trim(), PortalSecurity.FilterFlag.NoMarkup);
var order = HccApp.OrderServices.Orders.FindForCurrentStore(orderid);

// EU VAT: remove tax if VAT number is valid and customer is not in the same country as seller
if (foundMatch && storeIsoCode != userVatNumber.Substring(0, 2).ToUpper())
{
foreach (var i in order.Items)
{
i.IsTaxExempt = true;
}
HccApp.CalculateOrderAndSave(order);
result = "worx";
}
else if (!foundMatch)
{
foreach (var i in order.Items)
{
i.IsTaxExempt = false;
}
HccApp.CalculateOrderAndSave(order);
result = "invalid";
}

return new PreJsonResult(Web.Json.ObjectToJson(result));
}

#endregion

#region Implementation / Load model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
this.$shState = $('#shippingstate');
this.$shFirstname = $('#shippingfirstname');
this.$shLastname = $('#shippinglastname');
this.$shVatNumber = $('#shippingvatnumber');
this.$shAddress = $('#shippingaddress');
this.$shAddress2 = $('#shippingaddress2');
this.$shCity = $('#shippingcity');
Expand Down Expand Up @@ -269,6 +270,7 @@
$('#shippingtempregion').val($(this).val());
});
this.$shAll.change(function (e) { Addresses.shippingChanged(e); });
this.$shVatNumber.change(function () { ApplyEUVatRules(); });

this.$blAvailableAddresses.change(function (e) { Addresses.selectedAddressChanged(e); });
this.$blCountry.change(function () {
Expand Down Expand Up @@ -672,6 +674,22 @@
}
}

function ApplyEUVatRules() {
$.ajax({
type: "POST",
url: hcc.getServiceUrl("checkout/applyeuvatrules"),
data: {
UserVatNumber: $('#shippingvat').val(),
OrderId: $('#orderbvin').val()
},
dataType: "json",
success: function (data) {
$("#shippingaddress").change();
},
error: function () { }
});
}

// Order Summary ------------------------

var OrderSummary = {
Expand Down

0 comments on commit d767231

Please sign in to comment.