private void lvContracts_SubItemEndEditing(object sender, UserControl.SubItemEndEditingEventArgs e) { if (4 == e.SubItem && e.Item.Tag != null) { e.Item.SubItems[e.SubItem].Tag = (OCurrency)decimal.Parse(e.DisplayText); e.Item.SubItems[e.SubItem].Text = e.DisplayText; } if (5 == e.SubItem && e.Item.Tag != null) { e.Item.SubItems[e.SubItem].Tag = (OCurrency)decimal.Parse(e.DisplayText); e.Item.SubItems[e.SubItem].Text = e.DisplayText; } var loan = new Loan(); if (4 == e.SubItem || 5 == e.SubItem || 9 == e.SubItem && e.Item.Tag != null) { loan = e.Item.Tag as Loan; } if (loan != null && loan.Id != 0) { Installment i = loan.GetFirstUnpaidInstallment(); bool disableFees = false; var result = new KeyValuePair<Loan, RepaymentEvent>(); OCurrency penalties = (OCurrency)e.Item.SubItems[4].Tag; OCurrency principal = result.Value == null ? i.PrincipalHasToPay : result.Value.Principal; OCurrency interest = result.Value == null ? i.InterestHasToPay : result.Value.InterestPrepayment; OCurrency total; if (penalties != loan.CalculateDuePenaltiesForInstallment(i.Number, TimeProvider.Today)) disableFees = true; total = (OCurrency)e.Item.SubItems[5].Tag; if (e.SubItem == 5) { if (total < 0) throw new ArithmeticException("Total cannot be negative."); if (total > penalties) { OCurrency remainder = total - penalties; if (remainder > interest) { remainder -= interest; principal = remainder > principal ? principal : remainder; remainder -= principal; total = principal + interest + penalties + remainder; } else { interest = remainder; principal = 0; } } else { penalties = total; principal = 0; interest = 0; } } e.Item.SubItems[2].Text = principal.GetFormatedValue(loan.UseCents); e.Item.SubItems[2].Tag = principal; e.Item.SubItems[3].Text = interest.GetFormatedValue(loan.UseCents); e.Item.SubItems[3].Tag = interest; e.Item.SubItems[4].Text = penalties.GetFormatedValue(loan.UseCents); e.Item.SubItems[4].Tag = penalties; e.Item.SubItems[5].Text = total.GetFormatedValue(loan.UseCents); e.Item.SubItems[5].Tag = total; if (e.Item.SubItems.Count > 9) e.Item.SubItems[9].Tag = cbItem.SelectedItem; if (5 == e.SubItem) e.DisplayText = total.GetFormatedValue(loan.UseCents); if (9 == e.SubItem) e.DisplayText = cbItem.SelectedItem.ToString(); if (10 == e.SubItem) e.Item.SubItems[10].Text = e.DisplayText; int paymentOption = cbItem.SelectedIndex + 1; KeyValuePair<Loan, RepaymentEvent> keyValuePair = CalculatePrincipalAndInterest(loan, total.Value, disableFees, penalties.Value, paymentOption); if (keyValuePair.Value != null) { e.Item.SubItems[2].Text = keyValuePair.Value.Principal.GetFormatedValue(loan.Product.UseCents); e.Item.SubItems[2].Tag = keyValuePair.Value.Principal; e.Item.SubItems[3].Text = keyValuePair.Value.Interests.GetFormatedValue(loan.Product.UseCents); e.Item.SubItems[3].Tag = keyValuePair.Value.Interests; e.Item.SubItems[4].Text = keyValuePair.Value.Penalties.GetFormatedValue(loan.Product.UseCents); e.Item.SubItems[4].Tag = keyValuePair.Value.Penalties; } } UpdateTotal(); }