public StandardJsonResult CalculateTax(decimal subtotal, string countryCode, int?regionId, int?taxClassId) { var taxResult = new { taxRate = 0m, taxAmount = 0m, taxName = "Tax" }; TaxZone taxZone = taxZoneService.Find(countryCode, regionId); //TODO: Tax class should be calculated per product if (taxZone != null) { TaxRate taxRate = db.TaxRates.FirstOrDefault(r => r.TaxZoneId == taxZone.Id); if (taxRate != null) { TaxClassRate taxClassRate = db.TaxClassRates.FirstOrDefault(r => r.TaxRateId == taxRate.Id && r.TaxClassId == taxClassId); decimal rate = taxClassRate != null ? taxClassRate.Amount : taxRate.Amount; taxResult = new { taxRate = rate, taxAmount = (subtotal * rate) / 100m, taxName = taxRate.Name }; } } return(JsonSuccess(taxResult)); }
public decimal CalculateTax(int taxZoneID, int?taxClassId, decimal price) { decimal taxableAmount = price; decimal tax = 0m; List <IGrouping <int, TaxRate> > taxRateGrouped = db.TaxRates.Where(r => r.TaxZoneId == taxZoneID).GroupBy(r => r.Order).OrderBy(r => r.Key).ToList(); foreach (var rateGroup in taxRateGrouped) { decimal groupTax = 0m; foreach (TaxRate rate in rateGroup) { if (taxClassId.HasValue && rate.ClassRates.Any(cr => cr.TaxClassId == taxClassId)) { TaxClassRate classRate = rate.ClassRates.First(cr => cr.TaxClassId == taxClassId); groupTax += (taxableAmount * classRate.Amount) / 100m; } else { groupTax += (taxableAmount * rate.Amount) / 100m; } } tax += groupTax; taxableAmount += groupTax; } return(tax); }
public TaxRate AddOrUpdate(TaxRateEditViewModel model) { TaxRate taxRate; if (model.Id == 0) { taxRate = Mapper.Map <TaxRate>(model); db.TaxRates.Add(taxRate); } else { taxRate = Find(model.Id); taxRate = Mapper.Map(model, taxRate); var classRatesToDelete = taxRate.ClassRates.ToList().Where(r => !model.ClassRates.Any(cr => cr.TaxClassId == r.TaxClassId) || model.ClassRates.Any(cr => cr.TaxClassId == r.TaxClassId && cr.Amount == null)); foreach (var taxClassRate in classRatesToDelete) { db.TaxClassRates.Remove(taxClassRate); } } foreach (var classRateView in model.ClassRates) { if (classRateView.Amount == null) { continue; } TaxClassRate classRate = null; if (model.Id != 0) { classRate = taxRate.ClassRates.FirstOrDefault(r => r.TaxClassId == classRateView.TaxClassId); // If rate exists in db and is changed in view if (classRate != null) { classRate.Amount = classRateView.Amount.Value; } } if (classRate == null) { classRate = new TaxClassRate { TaxRateId = taxRate.Id, TaxClassId = classRateView.TaxClassId, Amount = classRateView.Amount.Value }; db.TaxClassRates.Add(classRate); } } db.SaveChanges(); return(taxRate); }