private void cmdAddBond_Click(object sender, EventArgs e)
        {
            try
            {
                if (manager.My_db.ClosedPeriods.FirstOrDefault(x => x.year == dtpIssuingDate.Value.Year && x.fund_id == manager.Selected) == null)
                {
                    if (doValidations())
                    {
                        BondsTFAM bond = new BondsTFAM();
                        bond.number                    = txtNumber.Text;
                        bond.issued                    = Convert.ToDateTime(dtpIssuingDate.Text);
                        bond.expired                   = Convert.ToDateTime(dtpExpirationDate.Text);
                        bond.FK_Bonds_Funds            = manager.Selected;
                        bond.amount                    = Math.Round(Convert.ToDecimal(txtAmount.Text), 2);
                        bond.interest_on_bond          = Convert.ToInt32(txtBondInterest.Text);
                        bond.interest_tff_contribution = Convert.ToInt32(txtTFFInterest.Text);
                        bond.currency_id               = int.Parse(cbCurrency.SelectedValue.ToString());

                        bond.active = 0;

                        manager.My_db.BondsTFAMs.Add(bond);

                        Resource _resource = manager.My_db.Resources.FirstOrDefault(x => x.Name == KeyDefinitions.BONDTFAM_CONSECUTIVE_KEY && x.FundId == manager.Selected);

                        fBondConsecutive++;

                        _resource.Value = fBondConsecutive.ToString();

                        manager.My_db.SaveChanges();

                        txtNumber.Text       = "Bond " + Conversions.toRomanNumeral(fBondConsecutive);
                        txtAmount.Text       = "0";
                        txtBondInterest.Text = "10";
                        txtTFFInterest.Text  = "1";
                        txtAmount.ReadOnly   = false;

                        MessageBox.Show("Bond has been saved", "");
                    }
                    else
                    {
                        MessageBox.Show("Data verification error.", "Error");
                    }
                }
                else
                {
                    ErrorMessage.showErrorMessage(new Exception("No movement allowed in closed period."));
                }
            }
            catch (Exception _ex)
            {
                ErrorMessage.showErrorMessage(_ex);
            }
        }
        private void generateInterest(bool forAll)
        {
            bool   errorsDetected = false;
            string errorMessages  = "";

            cmdGenerateInterest.Enabled    = false;
            cmdGenerateAllInterest.Enabled = false;

            Account account840 = manager.My_db.Accounts.FirstOrDefault(x => x.number == "840" && x.FK_Accounts_Funds == manager.Selected);
            Account account540 = manager.My_db.Accounts.FirstOrDefault(x => x.number == "540" && x.FK_Accounts_Funds == manager.Selected);

            if (account840 != null && account540 != null)
            {
                Subaccount subacct840 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account840.Id && x.name == "Interest on Bonds");
                Subaccount subacct540 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account540.Id && x.name == "Interest Bond");

                if (subacct840 != null && subacct540 != null)
                {
                    try
                    {
                        if (manager.My_db.ClosedPeriods.FirstOrDefault(x => x.year == dtpDate.Value.Year && x.fund_id == manager.Selected) == null)
                        {
                            DateTime _date = Convert.ToDateTime(dtpDate.Text);

                            List <BondsTFAM> _bondsToGenerateInterest = new List <BondsTFAM>();

                            if (!forAll)
                            {
                                foreach (ListViewItem _item in lvData.SelectedItems)
                                {
                                    int bondId = 0;

                                    if (int.TryParse(_item.Text, out bondId))
                                    {
                                        BondsTFAM toGenerate = manager.My_db.BondsTFAMs.FirstOrDefault(x => x.Id == bondId);

                                        if (toGenerate != null)
                                        {
                                            _bondsToGenerateInterest.Add(toGenerate);
                                        }

                                        if (toGenerate.expired <= toGenerate.issued)
                                        {
                                            errorsDetected = true;
                                            errorMessages += "\rBond " + toGenerate.number + ": expire <= issued date.";
                                        }

                                        if (toGenerate.amount == 0)
                                        {
                                            errorsDetected = true;
                                            errorMessages += "\rBond " + toGenerate.number + ": amount = 0.";
                                        }
                                    }
                                }
                            }
                            else
                            {
                                foreach (ListViewItem _item in lvData.Items)
                                {
                                    int bondId = 0;

                                    if (int.TryParse(_item.Text, out bondId))
                                    {
                                        BondsTFAM toGenerate = manager.My_db.BondsTFAMs.FirstOrDefault(x => x.Id == bondId);

                                        if (toGenerate != null)
                                        {
                                            _bondsToGenerateInterest.Add(toGenerate);
                                        }

                                        if (toGenerate.expired <= toGenerate.issued)
                                        {
                                            errorsDetected = true;
                                            errorMessages += "\rBond " + toGenerate.number + ": expire <= issued date.";
                                        }

                                        if (toGenerate.amount == 0)
                                        {
                                            errorsDetected = true;
                                            errorMessages += "\rBond " + toGenerate.number + ": amount = 0.";
                                        }
                                    }
                                }
                            }

                            if (errorsDetected)
                            {
                                if (MessageBox.Show("Do you want continue?\r\rThese bonds will be ignored in interest generation." + errorMessages, "Warning", MessageBoxButtons.OKCancel) != DialogResult.OK)
                                {
                                    cmdGenerateInterest.Enabled    = true;
                                    cmdGenerateAllInterest.Enabled = true;
                                    return;
                                }
                            }

                            if (_bondsToGenerateInterest.Count > 0)
                            {
                                BondsTFAMGeneratedInterest _interest = new BondsTFAMGeneratedInterest();
                                _interest.GeneratedDate = DateTime.Now;

                                bool interestCreated = false;

                                foreach (BondsTFAM _bondToGenerate in _bondsToGenerateInterest)
                                {
                                    BondsTFAMGeneratedInterestDetail existingInterest = manager.My_db.BondsTFAMGeneratedInterestDetails.FirstOrDefault(x => x.bond_id == _bondToGenerate.Id && x.generated_interest_date.Year == _date.Year && x.generated_interest_date.Month == _date.Month);

                                    if (existingInterest == null)
                                    {
                                        DateTime?fromDate = getLastGeneratedInterestDateFor(_bondToGenerate.Id);

                                        if (fromDate == null)
                                        {
                                            fromDate = _bondToGenerate.issued;
                                        }

                                        bool canContinueGeneratingInterest = true;

                                        decimal _interestOnBond = 0;
                                        decimal _interestOnTFF  = 0;

                                        DateTime toDate = dtpDate.Value <= _bondToGenerate.expired ? dtpDate.Value : _bondToGenerate.expired;

                                        int days = (toDate.Date - fromDate.Value.Date).Days;

                                        if (days > 0)
                                        {
                                            _interestOnBond = Math.Round(_bondToGenerate.amount * (decimal)_bondToGenerate.interest_on_bond * days / 360 / 100, 2);
                                            _interestOnTFF  = Math.Round(_bondToGenerate.amount * (decimal)_bondToGenerate.interest_tff_contribution * days / 360 / 100, 2);
                                        }

                                        canContinueGeneratingInterest = toDate < _bondToGenerate.expired;

                                        if (_interestOnBond > 0 && _interestOnTFF > 0)
                                        {
                                            if (!interestCreated)
                                            {
                                                manager.My_db.BondsTFAMGeneratedInterests.Add(_interest);
                                                interestCreated = true;
                                            }

                                            BondsTFAMGeneratedInterestDetail _interestDetail = new BondsTFAMGeneratedInterestDetail();
                                            _interestDetail.BondsTFAMGeneratedInterest = _interest;

                                            _interestDetail.bond_id = _bondToGenerate.Id;
                                            _interestDetail.generated_bond_interest = Math.Round(_interestOnBond, 2);
                                            _interestDetail.generated_tff_interest  = Math.Round(_interestOnTFF, 2);
                                            _interestDetail.generated_interest_date = toDate;

                                            manager.My_db.BondsTFAMGeneratedInterestDetails.Add(_interestDetail);

                                            AccountingMovement _accountingMovement = new AccountingMovement();

                                            _accountingMovement.FK_AccountingMovements_Funds = manager.Selected;
                                            _accountingMovement.description = _bondToGenerate.number + " Interest";
                                            _accountingMovement.date        = dtpDate.Value;
                                            _accountingMovement.reference   = KeyDefinitions.NextAccountMovementReference(dtpDate.Value.Year);
                                            _accountingMovement.FK_AccountingMovements_Currencies = _bondToGenerate.currency_id;
                                            _accountingMovement.original_reference = "";
                                            _accountingMovement.contract           = "";

                                            manager.My_db.AccountingMovements.Add(_accountingMovement);

                                            _interestDetail.AccountingMovement = _accountingMovement;

                                            Movements_Accounts _maccount840 = new Movements_Accounts();

                                            _maccount840.AccountingMovement             = _accountingMovement;
                                            _maccount840.FK_Movements_Accounts_Funds    = manager.Selected;
                                            _maccount840.FK_Movements_Accounts_Accounts = account840.Id;
                                            if (subacct840 != null)
                                            {
                                                _maccount840.FK_Movements_Accounts_Subaccounts = subacct840.Id;
                                            }
                                            _maccount840.subaccount_type = 9;
                                            _maccount840.subaccount      = _bondToGenerate.Id;
                                            _maccount840.debit           = Math.Round(_interestOnBond + _interestOnTFF, 2);
                                            _maccount840.credit          = 0;

                                            manager.My_db.Movements_Accounts.Add(_maccount840);

                                            Movements_Accounts _maccount540 = new Movements_Accounts();

                                            _maccount540.AccountingMovement             = _accountingMovement;
                                            _maccount540.FK_Movements_Accounts_Funds    = manager.Selected;
                                            _maccount540.FK_Movements_Accounts_Accounts = account540.Id;
                                            if (subacct540 != null)
                                            {
                                                _maccount540.FK_Movements_Accounts_Subaccounts = subacct540.Id;
                                            }
                                            _maccount540.subaccount_type = 9;
                                            _maccount540.subaccount      = _bondToGenerate.Id;
                                            _maccount540.debit           = 0;
                                            _maccount540.credit          = Math.Round(_interestOnBond + _interestOnTFF, 2);

                                            manager.My_db.Movements_Accounts.Add(_maccount540);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("Attempt to duplicate bond interest generation.");
                                    }
                                }

                                manager.My_db.SaveChanges();

                                BondGeneratedInterestForm generated_interest_form = new BondGeneratedInterestForm();
                                generated_interest_form.generated_interest_id = _interest.Id;
                                generated_interest_form.Show();
                            }
                            else
                            {
                                MessageBox.Show("No bonds found for interest generation.");
                            }
                        }
                        else
                        {
                            ErrorMessage.showErrorMessage(new Exception("No movement allowed in closed period."));
                        }
                    }
                    catch (Exception _ex)
                    {
                        ErrorMessage.showErrorMessage(_ex);
                    }
                }
                else
                {
                    ErrorMessage.showErrorMessage(new Exception("No subaccount for 840 or 540 were found."));
                }

                cmdGenerateInterest.Enabled    = true;
                cmdGenerateAllInterest.Enabled = true;
            }
            else
            {
                ErrorMessage.showErrorMessage(new Exception("No account 840 or 540 were found."));
            }
        }
Beispiel #3
0
        private void loadMovementsInListView()
        {
            listView1.Items.Clear();

            total_credit = 0;
            total_debit  = 0;

            foreach (Movement _movement in movements)
            {
                _movement.SubAccountBalance = 0;
                _movement.AccountBalance    = 0;
            }

            foreach (Movement _movement in movements)
            {
                total_credit += _movement.Credit;
                total_debit  += _movement.Debit;

                Account    _account    = manager.My_db.Accounts.FirstOrDefault(x => x.Id == _movement.Account);
                Subaccount _subAccount = manager.My_db.Subaccounts.FirstOrDefault(x => x.Id == _movement.Subaccount);

                String detailText = "";

                switch (_movement.Detail_type)
                {
                case 1:
                    Client client = manager.My_db.Clients.FirstOrDefault(x => x.Id == _movement.Detail);
                    if (client != null)
                    {
                        detailText = client.name;
                    }
                    break;

                case 2:
                    BankingAccount baccount = manager.My_db.BankingAccounts.FirstOrDefault(x => x.Id == _movement.Detail);
                    if (baccount != null)
                    {
                        detailText = baccount.name;
                    }
                    break;

                case 3:
                    Employee employee = manager.My_db.Employees.FirstOrDefault(x => x.Id == _movement.Detail);
                    if (employee != null)
                    {
                        detailText = employee.name;
                    }
                    break;

                case 4:
                    Creditor creditor = manager.My_db.Creditors.FirstOrDefault(x => x.Id == _movement.Detail);
                    if (creditor != null)
                    {
                        detailText = creditor.name;
                    }
                    break;

                case 5:
                    OtherDetail detail = manager.My_db.OtherDetails.FirstOrDefault(x => x.Id == _movement.Detail);
                    if (detail != null)
                    {
                        detailText = detail.name;
                    }
                    break;

                case 6:
                    Shareholder holder = manager.My_db.Shareholders.FirstOrDefault(x => x.Id == _movement.Detail);
                    if (holder != null)
                    {
                        detailText = holder.name;
                    }
                    break;

                case 7:
                    ServiceSupplier supplier = manager.My_db.ServiceSuppliers.FirstOrDefault(x => x.Id == _movement.Detail);
                    if (supplier != null)
                    {
                        detailText = supplier.name;
                    }
                    break;

                //case 8:
                //    Creditor lender = manager.My_db.Creditors.FirstOrDefault(x => x.Id == _movement.Detail);
                //    if (lender != null)
                //    {
                //        detailText = lender.name;
                //    }
                //    break;
                case 9:
                    BondsTFAM bond = manager.My_db.BondsTFAMs.FirstOrDefault(x => x.Id == _movement.Detail);
                    if (bond != null)
                    {
                        detailText = bond.number;
                    }
                    break;
                }

                int _creditFactor = 1;
                int _debitFactor  = -1;

                if (Account.leftAccountingIncrement(_account.type))
                {
                    _creditFactor = -1;
                    _debitFactor  = 1;
                }

                decimal _amountShift = _debitFactor * _movement.Debit + _creditFactor * _movement.Credit;

                string[]     row     = { _account.name, _subAccount != null ? _subAccount.name : "", detailText, String.Format("{0:n}", _movement.Debit), String.Format("{0:n}", _movement.Credit) };
                ListViewItem my_item = new ListViewItem(row);

                listView1.Items.Add(my_item);
            }

            if (total_credit > 0 || total_debit > 0)
            {
                string[] totales           = { "", "", "Total", String.Format("{0:n}", total_debit), String.Format("{0:n}", total_credit) };
                var      listViewItemTotal = new ListViewItem(totales);

                if (total_credit == total_debit || AvoidAccountBalanceValidation)
                {
                    _color          = Color.FromName("Green");
                    button2.Enabled = true;
                }
                else
                {
                    _color          = Color.FromName("Red");
                    button2.Enabled = false;
                }

                listViewItemTotal.ForeColor = _color;
                listView1.Items.Add(listViewItemTotal);
            }

            checkForContractVisibility();
        }
        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 += "\rBond " + reference + " has too much repayment value.";
                                }
                            }
                            else
                            {
                                errors += "\r  Bond " + 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)
                        {
                            BondsTFAMRepayment bondRepayment = new BondsTFAMRepayment();
                            bondRepayment.repayment_date = dtpDate.Value;

                            Account account540 = manager.My_db.Accounts.FirstOrDefault(x => x.number == "540" && x.FK_Accounts_Funds == manager.Selected);

                            if (account540 != null)
                            {
                                Subaccount subAcct01 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account540.Id && x.name == "Principal Bonds");
                                Subaccount subAcct02 = manager.My_db.Subaccounts.FirstOrDefault(x => x.FK_Subaccounts_Accounts == account540.Id && x.name == "Interest Bond");

                                if (subAcct01 != null && subAcct02 != null)
                                {
                                    manager.My_db.BondsTFAMRepayments.Add(bondRepayment);

                                    AccountingMovement accountingMovement = new AccountingMovement();

                                    accountingMovement.FK_AccountingMovements_Funds = manager.Selected;
                                    accountingMovement.description        = "Bond Repayment";
                                    accountingMovement.date               = dtpDate.Value;
                                    accountingMovement.reference          = KeyDefinitions.NextAccountMovementReference(dtpDate.Value.Year);
                                    accountingMovement.original_reference = "";
                                    accountingMovement.contract           = "";
                                    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 bondId = int.Parse(rowId);

                                        BondsTFAM bond = manager.My_db.BondsTFAMs.FirstOrDefault(x => x.Id == bondId);

                                        if (bond != null)
                                        {
                                            Currency currency = manager.My_db.Currencies.FirstOrDefault(x => x.Id == bond.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);

                                                    bondRepayment.AccountingMovement = accountingMovement;
                                                }

                                                BondsTFAMRepaymentDetail repaymentDetail = new BondsTFAMRepaymentDetail();

                                                repaymentDetail.BondsTFAMRepayment = bondRepayment;
                                                repaymentDetail.bond_id            = bondId;
                                                repaymentDetail.principal_repaid   = Math.Round(principalToBeCollected, 2);
                                                repaymentDetail.interest_repaid    = Math.Round(interestToBeCollected, 2);

                                                if (bond.can_generate_interest == 0 && principal - principalToBeCollected <= 0 && interestAccrued - interestToBeCollected <= 0)
                                                {
                                                    bond.paid = 1;
                                                }

                                                manager.My_db.BondsTFAMRepaymentDetails.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 = account540.Id;
                                                    if (subAcct01 != null)
                                                    {
                                                        _maccount01.FK_Movements_Accounts_Subaccounts = subAcct01.Id;
                                                    }
                                                    _maccount01.subaccount      = bond.Id;
                                                    _maccount01.subaccount_type = 9;
                                                    _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 = account540.Id;
                                                    if (subAcct02 != null)
                                                    {
                                                        _maccount02.FK_Movements_Accounts_Subaccounts = subAcct02.Id;
                                                    }
                                                    _maccount02.subaccount      = bond.Id;
                                                    _maccount02.subaccount_type = 9;
                                                    _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 bond with Id=\"" + bondId + "\".";
                                            }
                                        }
                                        else
                                        {
                                            msg += "\rBond with Id=\"" + bondId.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 540 is missing."));
                            }
                        }
                        else
                        {
                            ErrorMessage.showErrorMessage(new Exception("No bond found."));
                        }
                    }

                    this.bondsTFAMToBeRepaidTableAdapter.FillByFund(this.fundsDBDataSet.BondsTFAMToBeRepaid, manager.Selected);
                }
                else
                {
                    ErrorMessage.showErrorMessage(new Exception("No movement allowed in closed period."));
                }
            }
            catch (Exception _ex)
            {
                ErrorMessage.showErrorMessage(_ex, false);
            }
        }