private void movementToolStripMenuItem_Click(object sender, EventArgs e) { this.IsMdiContainer = true; GeneralLedgerForm form = new GeneralLedgerForm(); form.MdiParent = this; form.Show(); }
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { String reference = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); AccountingMovement accMov = manager.My_db.AccountingMovements.FirstOrDefault(x => x.reference == reference && x.FK_AccountingMovements_Funds == manager.Selected); if (accMov != null) { GeneralLedgerForm ledger = new GeneralLedgerForm(); ledger.FormInEditAccountingMovement = true; ledger.IdOfAccountingMovementToEdit = accMov.Id; ledger.StartPosition = FormStartPosition.CenterScreen; ledger.ShowDialog(); loadData(); } }
private void cmdPay_Click(object sender, EventArgs e) { decimal totalPaid = 0; int investmentId = 0; Account account125 = manager.My_db.Accounts.FirstOrDefault(x => x.number == "125" && x.FK_Accounts_Funds == manager.Selected); AccountingMovement _accountingMovement = new AccountingMovement(); try { if (manager.My_db.ClosedPeriods.FirstOrDefault(x => x.year == dtpPayDate.Value.Year && x.fund_id == manager.Selected) == null) { if (account125 != null) { if (cbContract.SelectedValue != null && int.TryParse(cbContract.SelectedValue.ToString(), out investmentId)) { if (lvDisbursements.SelectedItems.Count > 0) { DisbursementPayment dPayment = new DisbursementPayment(); dPayment.investment_id = investmentId; dPayment.payment_date = Convert.ToDateTime(dtpPayDate.Text); manager.My_db.DisbursementPayments.Add(dPayment); foreach (ListViewItem _item in lvDisbursements.SelectedItems) { int disbursementId = 0; if (int.TryParse(_item.Text, out disbursementId)) { Disbursement toPay = manager.My_db.Disbursements.FirstOrDefault(x => x.Id == disbursementId); if (toPay != null) { toPay.pay_date = dPayment.payment_date; toPay.can_generate_interest = true; dPayment.Disbursements.Add(toPay); if (_accountingMovement.Id == 0) { _accountingMovement.FK_AccountingMovements_Funds = manager.Selected; _accountingMovement.description = ""; _accountingMovement.date = dPayment.payment_date; _accountingMovement.reference = KeyDefinitions.NextAccountMovementReference(dtpPayDate.Value.Year); _accountingMovement.FK_AccountingMovements_Currencies = toPay.currency_id; _accountingMovement.original_reference = cbContract.SelectedIndex > 0 ? cbContract.Text : ""; _accountingMovement.contract = cbContract.SelectedText; manager.My_db.AccountingMovements.Add(_accountingMovement); dPayment.AccountingMovement = _accountingMovement; } Currency currency = manager.My_db.Currencies.FirstOrDefault(x => x.Id == toPay.currency_id && x.FK_Currencies_Funds == manager.Selected); Subaccount subacct125 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account125.Id && x.name == "INV " + currency.symbol); Movements_Accounts _maccount125 = new Movements_Accounts(); _maccount125.AccountingMovement = _accountingMovement; _maccount125.FK_Movements_Accounts_Funds = manager.Selected; _maccount125.FK_Movements_Accounts_Accounts = account125.Id; if (subacct125 != null) { _maccount125.FK_Movements_Accounts_Subaccounts = subacct125.Id; } _maccount125.subaccount = toPay.client_id; _maccount125.subaccount_type = 1; _maccount125.debit = Math.Round(toPay.amount, 2); _maccount125.credit = 0; totalPaid += toPay.amount; manager.My_db.Movements_Accounts.Add(_maccount125); } } } lvDisbursements.SelectedIndices.Clear(); GeneralLedgerForm gledger = new GeneralLedgerForm(); gledger.StartPosition = FormStartPosition.CenterScreen; gledger.FromExternalOperation = true; gledger.ExternalAccountMovemet = _accountingMovement; gledger.ExternalCredit = totalPaid; gledger.ShowDialog(); if (!gledger.OperationCompleted) { throw new Exception("Ledger window has been closed. The operation has been rolled back."); } DisbursementPaymentForm disbursement_payments = new DisbursementPaymentForm(); disbursement_payments.paymentId = dPayment.id; disbursement_payments.Show(); } else { ErrorMessage.showErrorMessage(new Exception("No disbursement to pay was selected.")); } } else { ErrorMessage.showErrorMessage(new Exception("No contract selected.")); } } else { ErrorMessage.showErrorMessage(new Exception("Account 125 not found.")); } } else { ErrorMessage.showErrorMessage(new Exception("No movement allowed in closed period.")); } } catch (Exception _ex) { Console.WriteLine("Error in DisbursementsForm.cmdPay_Click: " + _ex.Message); ErrorMessage.showErrorMessage(_ex, false); } loadDisbursements(); }
private void cmdCollect_Click(object sender, EventArgs e) { try { if (manager.My_db.ClosedPeriods.FirstOrDefault(x => x.year == dtpDate.Value.Year && x.fund_id == manager.Selected) == null) { List <string> rowIds = new List <string>(); List <decimal> amounts = new List <decimal>(); List <decimal> profitShares = new List <decimal>(); List <decimal> delayInterests = new List <decimal>(); List <decimal> overdues = new List <decimal>(); List <decimal> collectPrincipal = new List <decimal>(); List <decimal> collectProfitShare = new List <decimal>(); List <decimal> collectDelayInterest = new List <decimal>(); List <decimal> collectOverdues = new List <decimal>(); List <bool> isBookings = new List <bool>(); String errors = ""; foreach (DataGridViewRow row in dataGridView1.Rows) { string rowId = row.Cells[IdIndex].Value.ToString(); string number = row.Cells[NumberIndex].Value.ToString(); decimal amount = decimal.Parse(row.Cells[AmountIndex].Value.ToString()); decimal profitShare = decimal.Parse(row.Cells[ProfitShareIndex].Value.ToString()); decimal delayInterest = decimal.Parse(row.Cells[DelayInterestIndex].Value.ToString()); decimal overdue = decimal.Parse(row.Cells[OverdueIndex].Value.ToString()); string amountToBeCollectedPrincipalStr = row.Cells[CollectToPrincipalIndex].Value != null ? row.Cells[CollectToPrincipalIndex].Value.ToString() : "0"; string amountToBeCollectedProfitShareStr = row.Cells[CollectToProfitShareIndex].Value != null ? row.Cells[CollectToProfitShareIndex].Value.ToString() : "0"; string amountToBeCollectedDelayInterestStr = row.Cells[CollectToDelayInterestIndex].Value != null ? row.Cells[CollectToDelayInterestIndex].Value.ToString() : "0"; string amountToBeCollectedOverdueStr = row.Cells[CollectToOverdueIndex].Value != null ? row.Cells[CollectToOverdueIndex].Value.ToString() : "0"; bool isBooking = row.Cells[IsBookingIndex].Value != null?bool.Parse(row.Cells[IsBookingIndex].Value.ToString()) : false; if (amountToBeCollectedPrincipalStr != "0" || amountToBeCollectedProfitShareStr != "0" || amountToBeCollectedDelayInterestStr != "0" || amountToBeCollectedOverdueStr != "0") { decimal amountToBeCollectedPrincipal = 0; decimal amountToBeCollectedProfitShare = 0; decimal amountToBeCollectedDelayInterest = 0; decimal amountToBeCollectedOverdue = 0; if (decimal.TryParse(amountToBeCollectedPrincipalStr, out amountToBeCollectedPrincipal) && decimal.TryParse(amountToBeCollectedProfitShareStr, out amountToBeCollectedProfitShare) && decimal.TryParse(amountToBeCollectedDelayInterestStr, out amountToBeCollectedDelayInterest) && decimal.TryParse(amountToBeCollectedOverdueStr, out amountToBeCollectedOverdue)) { if (amount - amountToBeCollectedPrincipal >= 0 && profitShare - amountToBeCollectedProfitShare >= 0 && delayInterest - amountToBeCollectedDelayInterest >= 0 && overdue - amountToBeCollectedOverdue >= 0) { rowIds.Add(rowId); amounts.Add(amount); profitShares.Add(profitShare); delayInterests.Add(delayInterest); overdues.Add(overdue); collectPrincipal.Add(amountToBeCollectedPrincipal); collectProfitShare.Add(amountToBeCollectedProfitShare); collectDelayInterest.Add(amountToBeCollectedDelayInterest); collectOverdues.Add(amountToBeCollectedOverdue); isBookings.Add(isBooking); } else { errors += "\r\tDisbursement " + number + " has too much collection value."; } } else { errors += "\r Disbursement " + number + " has wrong collection value."; } } } if (errors != "") { string msg = "Please, fix these errors:" + errors; ErrorMessage.showErrorMessage(new Exception(msg)); return; } else { string msg = ""; if (rowIds.Count > 0) { DisbursementCollection collection = new DisbursementCollection(); collection.collection_date = dtpDate.Value; collection.investment_id = cbContract.SelectedValue != null?int.Parse(cbContract.SelectedValue.ToString()) : 0; Account account125 = manager.My_db.Accounts.FirstOrDefault(x => x.number == "125" && x.FK_Accounts_Funds == manager.Selected); Account account128 = manager.My_db.Accounts.FirstOrDefault(x => x.number == "128" && x.FK_Accounts_Funds == manager.Selected); Account account130 = manager.My_db.Accounts.FirstOrDefault(x => x.number == "130" && x.FK_Accounts_Funds == manager.Selected); if (collection.investment_id > 0) { if (account125 != null && account128 != null && account130 != null) { manager.My_db.DisbursementCollections.Add(collection); AccountingMovement accountingMovement = new AccountingMovement(); accountingMovement.FK_AccountingMovements_Funds = manager.Selected; accountingMovement.description = "Disbursement Collection"; accountingMovement.date = dtpDate.Value; accountingMovement.reference = KeyDefinitions.NextAccountMovementReference(dtpDate.Value.Year); accountingMovement.original_reference = cbContract.SelectedIndex > 0 ? cbContract.Text : ""; accountingMovement.contract = cbContract.Text; accountingMovement.FK_AccountingMovements_Currencies = 0; bool showGL = false; decimal totalPaid = 0; for (int i = 0; i < rowIds.Count; i++) { string rowId = rowIds[i]; decimal amount = amounts[i]; decimal profitShare = profitShares[i]; decimal delayInterest = delayInterests[i]; decimal overdue = overdues[i]; decimal toBeCollectedPrincipal = collectPrincipal[i]; decimal toBeCollectedProfitShare = collectProfitShare[i]; decimal toBeCollectedDelayInterest = collectDelayInterest[i]; decimal toBeCollectedOverdue = collectOverdues[i]; bool isBooking = isBookings[i]; int disbursementId = int.Parse(rowId); Disbursement disbursement = manager.My_db.Disbursements.FirstOrDefault(x => x.Id == disbursementId); if (disbursement != null) { int currencyId = disbursement.currency_id; Currency currency = manager.My_db.Currencies.FirstOrDefault(x => x.Id == currencyId && x.FK_Currencies_Funds == manager.Selected); Subaccount subacct125 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account125.Id && x.name == "INV " + currency.symbol); Subaccount subacct128 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account128.Id && x.name == "INV " + currency.symbol); Subaccount subacct130 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account130.Id && x.name == "INV " + currency.symbol); if (currency != null) { if (subacct125 != null && subacct128 != null && subacct130 != null) { if (accountingMovement.FK_AccountingMovements_Currencies == 0) { accountingMovement.FK_AccountingMovements_Currencies = currency.Id; manager.My_db.AccountingMovements.Add(accountingMovement); collection.AccountingMovement = accountingMovement; } DisbursementCollectionsDetail disbursementCollectionDetail = new DisbursementCollectionsDetail(); disbursementCollectionDetail.DisbursementCollection = collection; disbursementCollectionDetail.disbursement_id = disbursementId; disbursementCollectionDetail.amount_to_be_collected = amount + profitShare + delayInterest + overdue; disbursementCollectionDetail.amount_collected = Math.Round(toBeCollectedPrincipal + toBeCollectedProfitShare + toBeCollectedDelayInterest + toBeCollectedOverdue, 2); disbursementCollectionDetail.principal_to_be_collected = amount; disbursementCollectionDetail.principal_collected = Math.Round(toBeCollectedPrincipal, 2); disbursementCollectionDetail.profit_share_to_be_collected = profitShare; disbursementCollectionDetail.profit_share_collected = Math.Round(toBeCollectedProfitShare, 2); disbursementCollectionDetail.delay_interest_be_collected = delayInterest; disbursementCollectionDetail.delay_interest_collected = Math.Round(toBeCollectedDelayInterest, 2); disbursementCollectionDetail.overdue_to_be_collected = overdue; disbursementCollectionDetail.overdue_collected = Math.Round(toBeCollectedOverdue, 2); if (amount - toBeCollectedPrincipal <= 0 && profitShare - toBeCollectedProfitShare <= 0 && delayInterest - toBeCollectedDelayInterest <= 0 && overdue - toBeCollectedOverdue <= 0 && !disbursement.can_generate_interest) { setCollected(disbursement); } manager.My_db.DisbursementCollectionsDetails.Add(disbursementCollectionDetail); if (toBeCollectedPrincipal > 0) { Movements_Accounts _maccount125 = new Movements_Accounts(); _maccount125.AccountingMovement = accountingMovement; _maccount125.FK_Movements_Accounts_Funds = manager.Selected; _maccount125.FK_Movements_Accounts_Accounts = account125.Id; if (subacct125 != null) { _maccount125.FK_Movements_Accounts_Subaccounts = subacct125.Id; } _maccount125.subaccount = disbursement.client_id; _maccount125.subaccount_type = 1; _maccount125.debit = 0; _maccount125.credit = Math.Round(toBeCollectedPrincipal, 2); manager.My_db.Movements_Accounts.Add(_maccount125); disbursementCollectionDetail.Movements_Accounts = _maccount125; } if (toBeCollectedProfitShare > 0) { Movements_Accounts _maccount128 = new Movements_Accounts(); _maccount128.AccountingMovement = accountingMovement; _maccount128.FK_Movements_Accounts_Funds = manager.Selected; _maccount128.FK_Movements_Accounts_Accounts = account128.Id; if (subacct128 != null) { _maccount128.FK_Movements_Accounts_Subaccounts = subacct128.Id; } _maccount128.subaccount = disbursement.client_id; _maccount128.subaccount_type = 1; _maccount128.debit = 0; _maccount128.credit = Math.Round(toBeCollectedProfitShare, 2); manager.My_db.Movements_Accounts.Add(_maccount128); disbursementCollectionDetail.Movements_Accounts1 = _maccount128; } if (toBeCollectedDelayInterest > 0 | toBeCollectedOverdue > 0) { Movements_Accounts _maccount130 = new Movements_Accounts(); _maccount130.AccountingMovement = accountingMovement; _maccount130.FK_Movements_Accounts_Funds = manager.Selected; _maccount130.FK_Movements_Accounts_Accounts = account130.Id; if (subacct130 != null) { _maccount130.FK_Movements_Accounts_Subaccounts = subacct130.Id; } _maccount130.subaccount = disbursement.client_id; _maccount130.subaccount_type = 1; _maccount130.debit = 0; _maccount130.credit = Math.Round(toBeCollectedDelayInterest + toBeCollectedOverdue, 2); manager.My_db.Movements_Accounts.Add(_maccount130); disbursementCollectionDetail.Movements_Accounts2 = _maccount130; } totalPaid += toBeCollectedPrincipal + toBeCollectedProfitShare + toBeCollectedDelayInterest + toBeCollectedOverdue; showGL = true; } else { msg += "\rSome sub account is missing. No collection has been generated for disbursement with Id=\"" + disbursementId + "\"."; } } else { msg += "\rCurrency is missing. No collection has been generated for disbursement with Id=\"" + disbursementId + "\"."; } } else { msg += "\rDisbursement with Id=\"" + disbursementId.ToString() + "\" not found."; } } if (msg != "") { MessageBox.Show("Warning:\r\r" + msg); } if (showGL) { GeneralLedgerForm gledger = new GeneralLedgerForm(); gledger.StartPosition = FormStartPosition.CenterScreen; gledger.FromExternalOperation = true; gledger.ExternalAccountMovemet = accountingMovement; gledger.ExternalDebit = totalPaid; gledger.ShowDialog(); if (!gledger.OperationCompleted) { throw new Exception("Ledger window has been closed. The operation has been rolled back."); } } } else { ErrorMessage.showErrorMessage(new Exception("Account 125, 128 or 130 is missing.")); } } else { ErrorMessage.showErrorMessage(new Exception("No investment found for this contract.")); } } else { ErrorMessage.showErrorMessage(new Exception("No disbursement found.")); } } updateDisbursementList(); } else { ErrorMessage.showErrorMessage(new Exception("No movement allowed in closed period.")); } } catch (Exception _ex) { ErrorMessage.showErrorMessage(_ex, false); } }
private void addLoan() { try { if (manager.My_db.ClosedPeriods.FirstOrDefault(x => x.year == dtpFrom.Value.Year && x.fund_id == manager.Selected) == null) { int lenderId = Convert.ToInt32(cbLender.SelectedValue); int currencyId = Convert.ToInt32(cbCurrency.SelectedValue); if (!fEditMode) { Loan _validation = manager.My_db.Loans.FirstOrDefault(x => x.reference == txtReference.Text && x.fund_id == manager.Selected); if (_validation != null) { MessageBox.Show("Duplicated reference."); return; } Loan _loan = new Loan(); _loan.fund_id = manager.Selected; _loan.lender_id = lenderId; _loan.reference = txtReference.Text; _loan.interest = Math.Round(decimal.Parse(txtInterest.Text), 2); _loan.amount = Math.Round(decimal.Parse(txtAmount.Text), 2); _loan.start = dtpFrom.Value; _loan.end = dtpTo.Value; _loan.currency_id = currencyId; _loan.renegotiated = false; _loan.interest_base = rb360.Checked ? 360 : 365; _loan.paid = 0; _loan.can_generate_interest = 1; AccountingMovement acctMov = generateAccountingMovement(_loan); if (acctMov != null) { _loan.AccountingMovement = acctMov; manager.My_db.Loans.Add(_loan); GeneralLedgerForm gledger = new GeneralLedgerForm(); gledger.StartPosition = FormStartPosition.CenterScreen; gledger.FromExternalOperation = true; gledger.ExternalAccountMovemet = acctMov; gledger.ExternalDebit = _loan.amount; gledger.ShowDialog(); if (!gledger.OperationCompleted) { throw new Exception("Ledger window has been closed. The operation has been rolled back."); } } } //else //{ // //TODO: no esta claro como proceder en la modificacion pues hay mov de cuentas involucrados // int _id = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()); // Loan _validation = manager.My_db.Loans.FirstOrDefault(x => x.Id != _id && x.reference == txtReference.Text && x.fund_id == manager.Selected); // if (_validation != null) // { // MessageBox.Show("Duplicated reference."); // return; // } // Loan _selected = manager.My_db.Loans.FirstOrDefault(x => x.Id == _id); // if (_selected != null) // { // _selected.fund_id = manager.Selected; // _selected.lender_id = lenderId; // _selected.reference = txtReference.Text; // _selected.interest = Math.Round(decimal.Parse(txtInterest.Text), 2); // _selected.amount = Math.Round(decimal.Parse(txtAmount.Text), 2); // _selected.start = dtpFrom.Value; // _selected.end = dtpTo.Value; // _selected.currency_id = currencyId; // _selected.interest_base = rb360.Checked ? 360 : 365; // manager.My_db.SaveChanges(); // } //} loadLoansData(); cmdCancel_Click(null, null); } else { ErrorMessage.showErrorMessage(new Exception("No movement allowed in closed period.")); } } catch (Exception _ex) { ErrorMessage.showErrorMessage(_ex, false); } }
private void cmdActivate_Click(object sender, EventArgs e) { try { if (manager.My_db.ClosedPeriods.FirstOrDefault(x => x.year == dtpActivationDate.Value.Year && x.fund_id == manager.Selected) == null) { if (cbBond.SelectedItem != null) { int bondId = int.Parse(cbBond.SelectedValue.ToString()); BondsTFAM bond = manager.My_db.BondsTFAMs.FirstOrDefault(x => x.Id == bondId); if (bond != null) { Account account540 = manager.My_db.Accounts.FirstOrDefault(x => x.number == "540" && x.FK_Accounts_Funds == manager.Selected); if (account540 != null) { AccountingMovement _accountingMovement = new AccountingMovement(); _accountingMovement.FK_AccountingMovements_Funds = manager.Selected; _accountingMovement.description = bond.number + " Activation"; _accountingMovement.date = dtpActivationDate.Value; _accountingMovement.reference = KeyDefinitions.NextAccountMovementReference(dtpActivationDate.Value.Year); _accountingMovement.FK_AccountingMovements_Currencies = bond.currency_id; _accountingMovement.original_reference = ""; _accountingMovement.contract = ""; manager.My_db.AccountingMovements.Add(_accountingMovement); bond.AccountingMovement = _accountingMovement; bond.active = 1; bond.can_generate_interest = 1; Subaccount subacct = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account540.Id && x.name == "Principal Bonds"); Movements_Accounts _maccount = new Movements_Accounts(); _maccount.AccountingMovement = _accountingMovement; _maccount.FK_Movements_Accounts_Funds = manager.Selected; _maccount.FK_Movements_Accounts_Accounts = account540.Id; if (subacct != null) { _maccount.FK_Movements_Accounts_Subaccounts = subacct.Id; } _maccount.subaccount = bond.Id; _maccount.subaccount_type = 9; _maccount.debit = 0; _maccount.credit = Math.Round(bond.amount, 2); manager.My_db.Movements_Accounts.Add(_maccount); GeneralLedgerForm gledger = new GeneralLedgerForm(); gledger.StartPosition = FormStartPosition.CenterScreen; gledger.FromExternalOperation = true; gledger.ExternalAccountMovemet = _accountingMovement; gledger.ExternalDebit = bond.amount; gledger.ShowDialog(); if (!gledger.OperationCompleted) { throw new Exception("Ledger window has been closed. The operation has been rolled back."); } } else { ErrorMessage.showErrorMessage(new Exception("Account 540 not found.")); } } } this.bondsTFAMTableAdapter.FillByActivation(this.fundsDBDataSet.BondsTFAM, manager.Selected); } else { ErrorMessage.showErrorMessage(new Exception("No movement allowed in closed period.")); } } catch (Exception _ex) { Console.WriteLine("Error in BondsTFAMActivation.cmdActivate_Click: " + _ex.Message); ErrorMessage.showErrorMessage(_ex, false); } }
private void cmdRepay_Click(object sender, EventArgs e) { try { if (manager.My_db.ClosedPeriods.FirstOrDefault(x => x.year == dtpDate.Value.Year && x.fund_id == manager.Selected) == null) { List <string> rowIds = new List <string>(); List <string> references = new List <string>(); List <decimal> principals = new List <decimal>(); List <decimal> interestAccrueds = new List <decimal>(); List <decimal> principalCollections = new List <decimal>(); List <decimal> interestCollections = new List <decimal>(); String errors = ""; foreach (DataGridViewRow row in dataGridView1.Rows) { string rowId = row.Cells[idIndex].Value.ToString(); string reference = row.Cells[referenceIndex].Value.ToString(); decimal principal = decimal.Parse(row.Cells[principalIndex].Value.ToString()); decimal interestAccrued = decimal.Parse(row.Cells[interestAccruedIndex].Value.ToString()); string principalToBeCollectedStr = row.Cells[principalCollectionIndex].Value != null ? row.Cells[principalCollectionIndex].Value.ToString() : "0"; string interestToBeCollectedStr = row.Cells[interestCollectionIndex].Value != null ? row.Cells[interestCollectionIndex].Value.ToString() : "0"; decimal principalToBeCollected = 0; decimal interestToBeCollected = 0; if (principalToBeCollectedStr != "0" || interestToBeCollectedStr != "0") { if (decimal.TryParse(principalToBeCollectedStr, out principalToBeCollected) && decimal.TryParse(interestToBeCollectedStr, out interestToBeCollected)) { if (principal - principalToBeCollected >= 0 && interestAccrued - interestToBeCollected >= 0) { rowIds.Add(rowId); references.Add(reference); principals.Add(principal); interestAccrueds.Add(interestAccrued); principalCollections.Add(principalToBeCollected); interestCollections.Add(interestToBeCollected); } else { errors += "\rLoan " + reference + " has too much repayment value."; } } else { errors += "\r Loan " + reference + " has wrong repayment value."; } } } if (errors != "") { string msg = "Please, fix these errors:" + errors; ErrorMessage.showErrorMessage(new Exception(msg)); return; } else { string msg = ""; if (rowIds.Count > 0) { Loan_Repayments loanRepayment = new Loan_Repayments(); loanRepayment.repayment_date = dtpDate.Value; Account account470 = manager.My_db.Accounts.FirstOrDefault(x => x.number == "470" && x.FK_Accounts_Funds == manager.Selected); if (account470 != null) { Subaccount subAcct01 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account470.Id && x.number == "01"); Subaccount subAcct02 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account470.Id && x.number == "02"); if (subAcct01 != null && subAcct02 != null) { manager.My_db.Loan_Repayments.Add(loanRepayment); AccountingMovement accountingMovement = new AccountingMovement(); accountingMovement.FK_AccountingMovements_Funds = manager.Selected; accountingMovement.description = "Loan Repayment"; accountingMovement.date = dtpDate.Value; accountingMovement.reference = KeyDefinitions.NextAccountMovementReference(dtpDate.Value.Year); accountingMovement.original_reference = cbContract.SelectedIndex > 0 ? cbContract.Text : ""; accountingMovement.contract = cbContract.Text; accountingMovement.FK_AccountingMovements_Currencies = 0; bool showGL = false; decimal totalPaid = 0; for (int i = 0; i < rowIds.Count; i++) { string rowId = rowIds[i]; decimal principal = principals[i]; decimal interestAccrued = interestAccrueds[i]; decimal principalToBeCollected = principalCollections[i]; decimal interestToBeCollected = interestCollections[i]; int loandId = int.Parse(rowId); Loan loan = manager.My_db.Loans.FirstOrDefault(x => x.Id == loandId); if (loan != null) { Currency currency = manager.My_db.Currencies.FirstOrDefault(x => x.Id == loan.currency_id && x.FK_Currencies_Funds == manager.Selected); if (currency != null) { if (accountingMovement.FK_AccountingMovements_Currencies == 0) { accountingMovement.FK_AccountingMovements_Currencies = currency.Id; manager.My_db.AccountingMovements.Add(accountingMovement); loanRepayment.AccountingMovement = accountingMovement; } LoanRepaymentDetail repaymentDetail = new LoanRepaymentDetail(); repaymentDetail.Loan_Repayments = loanRepayment; repaymentDetail.loan_id = loandId; repaymentDetail.principal_repaid = Math.Round(principalToBeCollected, 2); repaymentDetail.interest_repaid = Math.Round(interestToBeCollected, 2); if (principal - principalToBeCollected <= 0) { loan.can_generate_interest = 0; } if (loan.can_generate_interest == 0 && principal - principalToBeCollected <= 0 && interestAccrued - interestToBeCollected <= 0) { loan.can_generate_interest = 0; loan.paid = 1; } manager.My_db.LoanRepaymentDetails.Add(repaymentDetail); if (principalToBeCollected > 0) { Movements_Accounts _maccount01 = new Movements_Accounts(); _maccount01.AccountingMovement = accountingMovement; _maccount01.FK_Movements_Accounts_Funds = manager.Selected; _maccount01.FK_Movements_Accounts_Accounts = account470.Id; if (subAcct01 != null) { _maccount01.FK_Movements_Accounts_Subaccounts = subAcct01.Id; } _maccount01.subaccount = loan.lender_id; _maccount01.subaccount_type = 4; _maccount01.debit = Math.Round(principalToBeCollected, 2); _maccount01.credit = 0; manager.My_db.Movements_Accounts.Add(_maccount01); repaymentDetail.Movements_Accounts = _maccount01; } if (interestToBeCollected > 0) { Movements_Accounts _maccount02 = new Movements_Accounts(); _maccount02.AccountingMovement = accountingMovement; _maccount02.FK_Movements_Accounts_Funds = manager.Selected; _maccount02.FK_Movements_Accounts_Accounts = account470.Id; if (subAcct02 != null) { _maccount02.FK_Movements_Accounts_Subaccounts = subAcct02.Id; } _maccount02.subaccount = loan.lender_id; _maccount02.subaccount_type = 4; _maccount02.debit = Math.Round(interestToBeCollected, 2); _maccount02.credit = 0; manager.My_db.Movements_Accounts.Add(_maccount02); repaymentDetail.Movements_Accounts1 = _maccount02; } totalPaid += principalToBeCollected + interestToBeCollected; showGL = true; } else { msg += "\rCurrency is missing. No repayment has been generated for loan with Id=\"" + loandId + "\"."; } } else { msg += "\rLoan with Id=\"" + loandId.ToString() + "\" not found."; } } if (msg != "") { MessageBox.Show("Warning:\r\r" + msg); } if (showGL) { GeneralLedgerForm gledger = new GeneralLedgerForm(); gledger.StartPosition = FormStartPosition.CenterScreen; gledger.FromExternalOperation = true; gledger.ExternalAccountMovemet = accountingMovement; gledger.ExternalCredit = totalPaid; gledger.ShowDialog(); if (!gledger.OperationCompleted) { throw new Exception("Ledger window has been closed. The operation has been rolled back."); } } } else { ErrorMessage.showErrorMessage(new Exception("Sub Account 01 or 02 are missing.")); } } else { ErrorMessage.showErrorMessage(new Exception("Account 470 is missing.")); } } else { ErrorMessage.showErrorMessage(new Exception("No loan found.")); } } cbContract_SelectedIndexChanged(null, null); } else { ErrorMessage.showErrorMessage(new Exception("No movement allowed in closed period.")); } } catch (Exception _ex) { ErrorMessage.showErrorMessage(_ex, false); } }