Пример #1
0
        private ViewModels.CreateLoanViewModel CalculateLoanParameters(long LoanAmount, int InstallmentsCount, int Percent = 0)
        {
            ViewModels.CreateLoanViewModel oCreateLoanViewModel = new ViewModels.CreateLoanViewModel();

            oCreateLoanViewModel.InstallmentsCount = InstallmentsCount;
            oCreateLoanViewModel.LoanAmount = LoanAmount;
            oCreateLoanViewModel.CommissionAmount = (Percent == 0) ? 0 : (long)System.Math.Round(((double)Percent / 100) * LoanAmount);
            oCreateLoanViewModel.RefundAmount = oCreateLoanViewModel.LoanAmount + oCreateLoanViewModel.CommissionAmount;

            LoanAmount += oCreateLoanViewModel.CommissionAmount;

            long InstallmentValue = 0;

            decimal temp = 0;

            if (LoanAmount % InstallmentsCount == 0)
            {
                temp = LoanAmount / InstallmentsCount;

                while (LoanAmount >= temp)
                {
                    oCreateLoanViewModel.Installments.Add((long)temp);

                    LoanAmount -= (long)temp;
                }
            }
            else
            {
                temp = LoanAmount / InstallmentsCount;

                InstallmentValue = (long)System.Math.Round((System.Math.Round(temp) / 10000)) * 10000;

                while (LoanAmount >= InstallmentValue)
                {
                    oCreateLoanViewModel.Installments.Add(InstallmentValue);

                    LoanAmount -= InstallmentValue;
                }

                oCreateLoanViewModel.Installments.Add(LoanAmount);
            }

            return oCreateLoanViewModel;
        }
Пример #2
0
        private void OBackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            DAL.UnitOfWork oUnitOfWork = null;

            oUnitOfWork = new DAL.UnitOfWork();

            Models.Member oMember = oUnitOfWork.MemberRepository
                .GetById(Utility.CurrentMember.Id);

            Models.Fund oFund = oUnitOfWork.FundRepository
                .GetById(Utility.CurrentFund.Id);

            Models.Loan oLoan = new Models.Loan();

            this.Dispatcher.Invoke(() =>
            {
                oLoan.LoanAmount = LoanAmountTextBox.Text.StringToMoney();
                oLoan.IsActive = true;
                oLoan.IsPayed = false;
                oLoan.MemberId = Utility.CurrentMember.Id;
                oLoan.InstallmentsCount = System.Convert.ToInt32(InstallmentsCountTextBox.Text.Trim());
                oLoan.RefundAmount = LoanAmountTextBox.Text.StringToMoney();
                oLoan.StartDate = LoanDateTimeDatePicker.SelectedDateTime;
                oLoan.EndDate = LoanDateTimeDatePicker.SelectedDateTime;
                oLoan.Description = DescriptionTextBox.Text.Trim();
            });

            oUnitOfWork.LoanRepository.Insert(oLoan);

            oFund.Balance -= oLoan.LoanAmount;

            oUnitOfWork.FundRepository.Update(oFund);

            oUnitOfWork.Save();

            Utility.CurrentFund = oFund;

            Models.Transaction oTransaction = new Models.Transaction();

            oTransaction.Amount = oLoan.LoanAmount;
            oTransaction.Balance = oFund.Balance;
            oTransaction.Date = System.DateTime.Now;
            oTransaction.Description = string.Format("پرداخت وام به مبلغ {0} ریال به {1}", oLoan.LoanAmount, Utility.CurrentMember.FullName);
            oTransaction.FundId = Utility.CurrentFund.Id;
            oTransaction.TransactionType = Models.TransactionType.Loan;
            oTransaction.MemberId = Utility.CurrentMember.Id;
            oTransaction.LoanId = oLoan.Id;

            oUnitOfWork.TransactionRepository.Insert(oTransaction);

            int percent = 0;

            this.Dispatcher.Invoke(() =>
            {
                percent = (CalculatePercentCheckBox.IsChecked == true) ? Utility.CurrentFund.Percent : 0;
            });

            ViewModels.CreateLoanViewModel oCreateLoanViewModel = CalculateLoanParameters(oLoan.LoanAmount, oLoan.InstallmentsCount, percent);

            for (int index = 0; index < oLoan.InstallmentsCount; index++)
            {
                int progressBarPercent = System.Convert.ToInt32(((double)(index + 1) / oLoan.InstallmentsCount) * 100);

                Models.Installment oInstallment = new Models.Installment();

                oInstallment.PaymentAmount = oCreateLoanViewModel.Installments.ElementAt(index);
                oInstallment.IsPayed = false;
                oInstallment.InstallmentDate = oLoan.StartDate.AddMonths(index + 1);
                oInstallment.PaymentDate = null;
                oInstallment.LoanId = oLoan.Id;

                oUnitOfWork.InstallmentRepository.Insert(oInstallment);

                Models.Reminder oReminder = new Models.Reminder();

                FarsiLibrary.Utils.PersianDate oPersianDate = new FarsiLibrary.Utils.PersianDate(oInstallment.InstallmentDate);

                oReminder.DateTime = oInstallment.InstallmentDate;
                oReminder.PersianDate.Year = oPersianDate.Year;
                oReminder.PersianDate.Month = oPersianDate.Month;
                oReminder.PersianDate.Day = oPersianDate.Day;
                oReminder.EventType = Models.Event.Installment;
                oReminder.Description = string.Format("قسط {0} وام {1} به مبلغ {2}",
                    FarsiLibrary.Utils.ToWords.ToString(index + 1), Utility.CurrentMember.FullName, oInstallment.PaymentAmount.ToRialStringFormat());
                oReminder.InstallmentId = oInstallment.Id;
                oReminder.FundId = Utility.CurrentFund.Id;

                oUnitOfWork.RemainderRepository.Insert(oReminder);

                if (index == oLoan.InstallmentsCount - 1)
                {
                    oLoan.EndDate = oInstallment.InstallmentDate;
                    oLoan.RefundAmount = oCreateLoanViewModel.RefundAmount;
                    oUnitOfWork.LoanRepository.Update(oLoan);
                }

                oUnitOfWork.Save();

                (sender as System.ComponentModel.BackgroundWorker).ReportProgress(progressBarPercent);

            }

            Utility.CurrentLoan = oLoan;

            oMember.Balance += oLoan.LoanAmount;

            oUnitOfWork.MemberRepository.Update(oMember);

            oUnitOfWork.Save();
        }