Esempio n. 1
0
        private void ShowLoanInListView(VillageMember member, Loan loan)
        {
            Person person = (Person)member.Tiers;
            ApplicationSettings dataParam = ApplicationSettings.GetInstance(string.Empty);
            int          decimalPlaces    = dataParam.InterestRateDecimalPlaces;
            ListViewItem item             = new ListViewItem(person.Name)
            {
                Tag = member
            };

            if (loan == null || _village.EstablishmentDate == null)
            {
                return;
            }
            if (loan.CreationDate.Date >= _village.EstablishmentDate.Value.Date && _village.Id == loan.NsgID)
            {
                item.SubItems.Add(loan.ProductName);
                item.SubItems.Add(loan.Code);
                item.SubItems.Add(MultiLanguageStrings.GetString(Ressource.ClientForm, loan.ContractStatus + ".Text"));
                item.SubItems.Add(loan.Amount.GetFormatedValue(loan.UseCents));
                item.SubItems.Add(
                    loan.CalculateActualOlbBasedOnRepayments().GetFormatedValue(loan.UseCents));
                item.SubItems.Add(loan.Product.Currency.Name);
                item.SubItems.Add(Math.Round(loan.InterestRate * 100m, decimalPlaces).ToString());
                item.SubItems.Add(loan.InstallmentType.Name);
                item.SubItems.Add(loan.NbOfInstallments.ToString());
                item.SubItems.Add(loan.AlignDisbursementDate.ToShortDateString());

                if (loan.GetLastNonDeletedEvent() != null)
                {
                    item.SubItems.Add(loan.GetLastNonDeletedEvent().Date.ToShortDateString());
                }
                else
                {
                    item.SubItems.Add("-");
                }

                if (loan.NextInstallment != null)
                {
                    item.SubItems.Add(loan.NextInstallment.ExpectedDate.ToShortDateString());
                    item.SubItems.Add(
                        ServicesHelper.ConvertDecimalToString(loan.NextInstallment.Amount.Value));
                }
                else
                {
                    item.SubItems.Add("-");
                    item.SubItems.Add("-");
                }
                item.SubItems.Add(loan.CloseDate.ToShortDateString());
                if (member.LeftDate != null)
                {
                    item.BackColor = Color.Red;
                }
                listViewLoans.Items.Add(item);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            bool isError = false;

            try
            {
                if (_notEnoughMoney)
                {
                    if (!MessageBox.Show(MultiLanguageStrings.GetString(Ressource.VillageForm, "MoneyNotEnoughForAll.Text"), "!",
                                         MessageBoxButtons.YesNo).Equals(DialogResult.Yes))
                    {
                        return;
                    }
                }
                foreach (ListViewItem item in lvMembers.Items)
                {
                    if (!item.Checked || item == _itemTotal)
                    {
                        continue;
                    }
                    var      loan = item.Tag as Loan;
                    DateTime date = Convert.ToDateTime(item.SubItems[IdxDDate].Text);

                    VillageMember activeMember = null;

                    int index = 0;
                    foreach (VillageMember member in _village.NonDisbursedMembers)
                    {
                        int tIndex = member.ActiveLoans.IndexOf(loan);
                        if (tIndex > -1)
                        {
                            activeMember = member;
                            index        = tIndex;
                        }
                    }

                    if (loan != null)
                    {
                        loan.CompulsorySavings = ServicesProvider.GetInstance().GetSavingServices().GetSavingForLoan(loan.Id, true);
                        if (loan.Product.UseCompulsorySavings)
                        {
                            if (loan.CompulsorySavings == null)
                            {
                                string text = string.Format("The loan with the code {0} requires a compulsory savings account to be disbursed!", loan.Code);
                                MessageBox.Show(text, @"No compulsory savings", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                            decimal totalAmountPercentage = 0;
                            decimal savingsBalance        = loan.CompulsorySavings.GetBalance().Value;

                            foreach (Loan assosiatedLoan in loan.CompulsorySavings.Loans)
                            {
                                if (assosiatedLoan.ContractStatus != OContractStatus.Closed &&
                                    assosiatedLoan.ContractStatus != OContractStatus.Abandoned &&
                                    assosiatedLoan.ContractStatus != OContractStatus.Postponed &&
                                    assosiatedLoan.CompulsorySavingsPercentage != null)
                                {
                                    totalAmountPercentage += (assosiatedLoan.Amount.Value * ((decimal)assosiatedLoan.CompulsorySavingsPercentage / 100));
                                }
                            }

                            if (totalAmountPercentage > savingsBalance)
                            {
                                MessageBox.Show(String.Format(
                                                    "The balance on savings account {2} of {0} is not enough to cover total loans amount percentage of {1}.\nClient name: {3}",
                                                    ServicesHelper.ConvertDecimalToString(savingsBalance),
                                                    ServicesHelper.ConvertDecimalToString(totalAmountPercentage),
                                                    loan.CompulsorySavings.Code,
                                                    item.SubItems[0].Text),
                                                @"Insufficient savings balance", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                        }
                    }

                    date = DateTime.Parse(item.SubItems[2].Text);

                    loan.StartDate = date;
                    if (loan.AlignDisbursementDate.Date != DateTime.Parse(item.SubItems[3].Text).Date)
                    {
                        loan.FirstInstallmentDate  = DateTime.Parse(item.SubItems[3].Text).Date;
                        loan.AlignDisbursementDate = loan.CalculateAlignDisbursementDate(DateTime.Parse(item.SubItems[3].Text).Date);
                        if (!loan.ScheduleChangedManually)
                        {
                            loan.InstallmentList = loan.CalculateInstallments(true);
                            loan.CalculateStartDates();
                        }
                    }

                    if (item.SubItems[IdxPaymentMethod].Tag != null && item.SubItems[IdxPaymentMethod].Tag.ToString() == OPaymentMethods.Savings.ToString())
                    {
                        if (loan.Product.UseCompulsorySavings && loan.CompulsorySavings != null)
                        {
                            if (loan.CompulsorySavings.Status == OSavingsStatus.Active)
                            {
                                ServicesProvider.GetInstance().GetSavingServices().LoanDisbursement(loan.CompulsorySavings, loan,
                                                                                                    loan.AlignDisbursementDate, "Disbursement of the loan: " + loan.Code, User.CurrentUser, false);
                            }
                            else
                            {
                                throw new OpenCbsSavingException(OpenCbsSavingExceptionEnum.CompulsorySavingsContractIsNotActive);
                            }
                        }
                        else
                        {
                            string text = string.Format(@"The loan of client '{0}' requires a compulsory savings account!", ((VillageMember)item.Tag).Tiers.Name);
                            MessageBox.Show(text, @"No compulsory savings", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    PaymentMethod method =
                        ServicesProvider.GetInstance().GetPaymentMethodServices().GetPaymentMethodByName(item.SubItems[IdxPaymentMethod].Text);
                    activeMember.ActiveLoans[index] = ServicesProvider.GetInstance().GetContractServices().Disburse(loan, date, true, false, method);
                }
            }
            catch (Exception ex)
            {
                isError = true;
                new frmShowError(CustomExceptionHandler.ShowExceptionText(ex)).ShowDialog();
            }

            if (!isError)
            {
                Close();
            }
        }