private void SubtractExistingTaxes(Order order, HccRequestContext hccContext) { var applyVATRules = hccContext.CurrentStore.Settings.ApplyVATRules; if (applyVATRules) { order.TotalShippingAfterDiscounts = 0; foreach (ITaxable item in order.Items) { if (item.IsTaxExempt) { continue; } var orderService = Factory.CreateService <OrderService>(hccContext); ITaxSchedule schedule = orderService.TaxSchedules.FindForThisStore(item.TaxSchedule); if (schedule == null) { continue; } var defaultRate = schedule.TaxScheduleDefaultRate() / 100; var defaultShippingRate = schedule.TaxScheduleDefaultShippingRate() / 100; if (defaultRate != 0) { //Subtract included tax var lineTotal = item.LineTotal; var lineTotalVAT = Money.RoundCurrency(lineTotal - lineTotal / (1 + defaultRate)); item.LineTotal = lineTotal - lineTotalVAT; } if (defaultShippingRate != 0) { //Subtract tax from shipping portion always since rates may differ var shippingPortion = item.ShippingPortion; var shippingPortionVAT = Money.RoundCurrency(shippingPortion - shippingPortion / (1 + defaultShippingRate)); item.ShippingPortion = shippingPortion - shippingPortionVAT; } item.AdjustedPricePerItem = Money.RoundCurrency(item.LineTotal / item.Quantity); } foreach (ITaxable item in order.Items) { // shipping portions have to be collected even if product is tax exempt or doesn't have tax schedule order.TotalShippingAfterDiscounts += item.ShippingPortion; } } }
private void TaxItems(List <ITaxable> items, IAddress billingAddress, IAddress shippingAddress, decimal totalOrderDiscounts, string userId) { var applyVATRules = _app.CurrentStore.Settings.ApplyVATRules; decimal discount = 0; decimal qty = 0; if (totalOrderDiscounts != 0) { foreach (var i in items) { if (i.IsTaxExempt == false && i.TaxSchedule != -1) { if (_app.OrderServices.TaxSchedules.FindForThisStore(i.TaxSchedule) != null) { qty += i.Quantity; } } } if (qty != 0) { discount = totalOrderDiscounts / qty; } } foreach (var item in items) { if (item.IsTaxExempt) { continue; } ITaxSchedule schedule = _app.OrderServices.TaxSchedules.FindForThisStore(item.TaxSchedule); if (schedule == null) { continue; } //Get best match by address var taxationAddress = item.IsNonShipping ? billingAddress : shippingAddress; var tax = _app.OrderServices.Taxes.FindByAdress(_app.CurrentStore.Id, schedule.TaxScheduleId(), taxationAddress); var defaultRate = schedule.TaxScheduleDefaultRate() / 100; var defaultShippingRate = schedule.TaxScheduleDefaultShippingRate() / 100; decimal rate = 0; decimal shippingRate = 0; if (applyVATRules) { rate = defaultRate; shippingRate = defaultShippingRate; } if (tax != null) { var user = _app.MembershipServices.Customers.Find(userId); var taxExemptUser = user != null ? user.TaxExempt : false; if (!taxExemptUser) { rate = tax.Rate / 100; if (tax.ApplyToShipping) { shippingRate = tax.ShippingRate / 100; } } } item.SetTaxRate(rate); item.SetShippingTaxRate(shippingRate); var lineItemTotalDiscount = item.Quantity * discount; if (tax != null) { if (applyVATRules) { if (rate != defaultRate) { //Subtract included tax var lineTotal = item.LineTotal; var lineTotalVAT = Money.RoundCurrency(lineTotal - lineTotal / (1 + defaultRate)); item.LineTotal = lineTotal - lineTotalVAT; //Add new tax value item.TaxPortion = Money.RoundCurrency(item.LineTotal * rate); item.LineTotal += item.TaxPortion; } else { var lineTotal = item.LineTotal; item.TaxPortion = Money.RoundCurrency(lineTotal - lineTotal / (1 + rate)); } if (shippingRate != defaultShippingRate) { //Subtract tax from shipping portion always since rates may differ var shippingPortion = item.ShippingPortion; var shippingPortionVAT = Money.RoundCurrency(shippingPortion - shippingPortion / (1 + defaultShippingRate)); item.ShippingPortion = shippingPortion - shippingPortionVAT; item.ShippingTaxPortion = Money.RoundCurrency(item.ShippingPortion * shippingRate); item.ShippingPortion += item.ShippingTaxPortion; } else { var shippingPortion = item.ShippingPortion; item.ShippingTaxPortion = Money.RoundCurrency(shippingPortion - shippingPortion / (1 + shippingRate)); } } else { var lineTotalTax = Money.RoundCurrency((item.LineTotal + lineItemTotalDiscount) * rate); item.TaxPortion = lineTotalTax; var shippingPortionTax = Money.RoundCurrency(item.ShippingPortion * shippingRate); item.ShippingTaxPortion = shippingPortionTax; } } item.AdjustedPricePerItem = Money.RoundCurrency(item.LineTotal / item.Quantity); } }