private static void CheckConsistency <TR>(IPriced <TR> newRow) { if (newRow.GetAmountIncVat() - newRow.GetAmountExVat() * (100 + newRow.GetVatPercent()) / 100 != 0) { throw new SveaWebPayValidationException(string.Format("Orderrow amounts and vat is inconsistent for row. Ex vat: {0} Inc vat: {1} Vat: {2}", newRow.GetAmountExVat(), newRow.GetAmountIncVat(), newRow.GetVatPercent())); } }
private static void FillMissingVatAndAmount <TR>(IPriced <TR> orderRow, IPriced <TR> newRow) { if (orderRow.GetAmountExVat() != null && orderRow.GetAmountIncVat() != null && orderRow.GetVatPercent() != null) { newRow.SetAmountExVat(orderRow.GetAmountExVat().GetValueOrDefault()); newRow.SetAmountIncVat(orderRow.GetAmountIncVat().GetValueOrDefault()); newRow.SetVatPercent(orderRow.GetVatPercent().GetValueOrDefault()); } else if (orderRow.GetAmountIncVat() == 0 && orderRow.GetAmountExVat() > 0 || orderRow.GetAmountExVat() == 0 && orderRow.GetAmountIncVat() > 0) { throw new SveaWebPayValidationException( "Order is inconsistent. Amount excluding and including vat must either both be 0 or both be >0."); } else if (orderRow.GetAmountExVat() != null && orderRow.GetAmountIncVat() != null) { newRow.SetAmountExVat(orderRow.GetAmountExVat().GetValueOrDefault()); newRow.SetAmountIncVat(orderRow.GetAmountIncVat().GetValueOrDefault()); var vat = orderRow.GetAmountExVat() == 0 || orderRow.GetAmountIncVat() == 0 ? 0 : (orderRow.GetAmountIncVat() - orderRow.GetAmountExVat()) / orderRow.GetAmountExVat(); var vatPercent = MathUtil.BankersRound((vat * 100).GetValueOrDefault()); newRow.SetVatPercent(vatPercent); } else if (orderRow.GetVatPercent() != null && orderRow.GetAmountIncVat() != null) { newRow.SetAmountIncVat(orderRow.GetAmountIncVat().GetValueOrDefault()); newRow.SetVatPercent(orderRow.GetVatPercent().GetValueOrDefault()); var exVatAmount = orderRow.GetAmountIncVat().GetValueOrDefault() * 100 / (100 + orderRow.GetVatPercent().GetValueOrDefault()); newRow.SetAmountExVat(exVatAmount); } else if (orderRow.GetVatPercent() != null && orderRow.GetAmountExVat() != null) { newRow.SetAmountExVat(orderRow.GetAmountExVat().GetValueOrDefault()); newRow.SetVatPercent(orderRow.GetVatPercent().GetValueOrDefault()); var incVatAmount = orderRow.GetAmountExVat().GetValueOrDefault() * (100 + orderRow.GetVatPercent().GetValueOrDefault()) / 100; newRow.SetAmountIncVat(incVatAmount); } else { throw new SveaWebPayValidationException( "Order is inconsistent. You need to set at least two of SetAmountIncVat, SetAmountExVat and SetVatPercent. If you set all three, make sure their values are consistent."); } }