Пример #1
0
 /// <summary>
 /// Method to add a TrancheEvent into database. We use the NullableTypes to make the correspondance between
 /// nullable int, decimal and double types in database and our own objects
 /// </summary>
 /// <param name="trancheEvent">TrancheEvent Object</param>
 /// <returns>The id of the Tranche Event which has been added</returns>
 public int Add(TrancheEvent trancheEvent)
 {
     const string sqlText = @"
         INSERT INTO [TrancheEvents]
                    ( [id]
                     ,[interest_rate]
                     ,[amount]
                     ,[maturity]
                     ,[start_date]
                     ,[applied_new_interest])
                     VALUES
                     (@Id,
                      @InterestRate,
                      @Amount,
                      @Maturity,
                      @StartDate,
                      @applied_new_interest)
         SELECT SCOPE_IDENTITY()";
     using (SqlConnection conn = GetConnection())
     using (var cmd = new OpenCbsCommand(sqlText, conn))
     {
         SetTrancheEvent(cmd, trancheEvent);
         return int.Parse(cmd.ExecuteScalar().ToString());
     }
 }
Пример #2
0
 public void Update(TrancheEvent trancheEvent)
 {
     string sqlText = @"
                     UPDATE [TrancheEvents] SET
                     [interest_rate] = @InterestRate,
                     [amount] = @Amount,
                     [maturity] = @Maturity,
                     [start_date] = @StartDate,
                     [applied_new_interest] = @applied_new_interest
                     WHERE id = @Id";
     using (SqlConnection conn = GetConnection())
     using (var cmd = new OpenCbsCommand(sqlText, conn))
     {
         SetTrancheEvent(cmd, trancheEvent);
         cmd.ExecuteNonQuery();
     }
 }
Пример #3
0
 private void TrancheEventOrigination(TrancheEvent trancheEvent, Loan pContract, SqlTransaction sqlTransac)
 {
     _eventManagement.AddLoanEvent(trancheEvent, pContract.Id, sqlTransac);
 }
Пример #4
0
        /// <summary>
        /// Cancels last event from given contract, restores associated installment status.
        /// and restores Client(individual, corporate) status 
        /// </summary>
        /// <param name="contract">Contract</param>
        /// <param name="pClient"></param>
        /// <param name="comment"> </param>
        /// <returns>Cancelled event</returns>
        public Event CancelLastEvent(Loan contract, IClient pClient, string comment)
        {
            using (SqlConnection conn = _loanManager.GetConnection())
            using (SqlTransaction sqlTransaction = conn.BeginTransaction())
            {
                Event cancelledEvent;
                try
                {
                    Event evnt = contract.GetLastNonDeletedEvent();

                    if (null == evnt)
                        throw new OpenCbsContractSaveException(OpenCbsContractSaveExceptionEnum.EventIsNull);

                    if (!evnt.Cancelable)
                        throw new OpenCbsContractSaveException(OpenCbsContractSaveExceptionEnum.EventNotCancelable);

                    if (string.IsNullOrEmpty(comment))
                        throw new OpenCbsContractSaveException(OpenCbsContractSaveExceptionEnum.EventCommentIsEmpty);

                    if (pClient is Person)
                        evnt.ClientType = OClientTypes.Person;
                    else if (pClient is Group)
                        evnt.ClientType = OClientTypes.Group;
                    else if (pClient is Corporate)
                        evnt.ClientType = OClientTypes.Corporate;
                    else if (pClient is Village)
                        evnt.ClientType = OClientTypes.Village;

                    var evntCopy = evnt.Copy();
                    evntCopy.Id = evnt.ParentId ?? evnt.Id;
                    CallInterceptor(new Dictionary<string, object>
                    {
                        {"Loan", contract},
                        {"Event", evntCopy},
                        {"Deleted", true},
                        {"SqlTransaction", sqlTransaction}
                    });
                    if (ApplicationSettings.GetInstance(User.CurrentUser.Md5).UseMandatorySavingAccount)
                    {
                        var id = 0;
                        if (int.TryParse(evnt.Comment, out id))
                            ServicesProvider.GetInstance()
                                            .GetSavingServices()
                                            .DeleteEvent(new SavingWithdrawEvent()
                                                {
                                                    Id = id,
                                                    CancelDate = TimeProvider.Today
                                                });
                    }
                    evnt.Comment = comment;
                    evnt.CancelDate = TimeProvider.Now;

                    // if event is loan close event, we delete it first
                    if (evnt is LoanCloseEvent)
                    {
                        _ePs.CancelFireEvent(evnt, sqlTransaction, contract, contract.Product.Currency.Id);
                        evnt.Deleted = true;
                        evnt = contract.GetLastNonDeletedEvent();
                        if (pClient is Person)
                            evnt.ClientType = OClientTypes.Person;
                        else if (pClient is Group)
                            evnt.ClientType = OClientTypes.Group;
                        else if (pClient is Corporate)
                            evnt.ClientType = OClientTypes.Corporate;
                        else if (pClient is Village)
                            evnt.ClientType = OClientTypes.Village;
                        evnt.Comment = comment;
                        evnt.CancelDate = TimeProvider.Now;
                    }
                    _ePs.CancelFireEvent(evnt, sqlTransaction, contract, contract.Product.Currency.Id);
                    _ePs.UpdateCommentForLoanEvent(evnt, sqlTransaction);

                    evnt.Deleted = true;
                    //in case total repayment there could be several rep events
                    foreach (RepaymentEvent evt in contract.Events.GetRepaymentEvents())
                    {
                        if ((evt.ParentId == evnt.ParentId && evnt.ParentId != null) || (evt.Id == evnt.ParentId))
                        {
                            evt.Deleted = true;
                            evt.Comment = evnt.Comment;
                            _ePs.UpdateCommentForLoanEvent(evt, sqlTransaction);
                        }
                    }

                    if (evnt.Code == "ATR" || evnt.Code == "RBLE")
                    {
                        foreach (Event cie in contract.Events)
                        {
                            if (cie is CreditInsuranceEvent)
                                if (cie.Deleted == false && cie.Code == "LCIP")
                                {
                                    _ePs.CancelFireEvent(cie, sqlTransaction, contract, contract.Product.Currency.Id);
                                    cie.Deleted = true;
                                }
                        }
                    }

                    cancelledEvent = evnt;
                    // Restore the installment status.
                    UnarchiveInstallments(contract, cancelledEvent, sqlTransaction);
                    contract.InstallmentList = _instalmentManager.SelectInstallments(contract.Id, sqlTransaction);
                    contract.GivenTranches = _loanManager.SelectTranches(contract.Id, sqlTransaction);
                    contract.NbOfInstallments = contract.InstallmentList.Count;

                    //return interest rate after delete event
                    contract.InterestRate = GetPreviousRate(contract);

                    if (evnt is LoanDisbursmentEvent)
                    {
                        contract.Disbursed = false;
                    }
                    else if (evnt is RescheduleLoanEvent)
                    {
                        contract.Rescheduled = false;
                    }
                    else if (cancelledEvent is TrancheEvent)
                    {
                        contract.Amount = contract.Amount - (cancelledEvent as TrancheEvent).Amount;

                        TrancheEvent trancheEventToDelete = new TrancheEvent();

                        foreach (var trancheEvent in contract.GivenTranches)
                        {
                            if (trancheEvent.Id == cancelledEvent.Id)
                            {
                                trancheEventToDelete = trancheEvent;
                            }
                        }

                        contract.GivenTranches.Remove(trancheEventToDelete);

                        if (contract.AllInstallmentsRepaid)
                        {
                            contract.ContractStatus = OContractStatus.Closed;
                            contract.Closed = true;
                            //Restore interest rate
                            contract.InterestRate =
                                contract.GivenTranches[contract.GivenTranches.Count - 1].InterestRate.Value;
                        }
                    }
                    else if (cancelledEvent is RepaymentEvent)
                    {
                        //restor a person of the corporate
                        _clientManager.RestorMemberOfGroupByEventId(cancelledEvent.Id, contract, sqlTransaction);
                        contract.EscapedMember = null;
                        if (cancelledEvent.RepaymentType == OPaymentType.TotalPayment)
                        {
                            if (contract.HasCompulsoryAmount())
                            {
                                SavingEvent savingUnblockEvent =
                                    contract.CompulsorySavings.Events.FirstOrDefault(
                                        e => e.LoanEventId == cancelledEvent.Id);
                                if (savingUnblockEvent != null)
                                {
                                    _savingEventManager.DeleteSavingsEventByLoanEventId(
                                        cancelledEvent.ParentId ?? cancelledEvent.Id, sqlTransaction);
                                    savingUnblockEvent.Deleted = true;
                                }
                            }
                        }
                    }

                    if (!contract.WrittenOff && !contract.AllInstallmentsRepaid)
                    {
                        contract.Closed = false;

                        if (evnt is LoanDisbursmentEvent)
                            contract.ContractStatus = OContractStatus.Validated;
                        else
                        {
                            contract.ContractStatus = evnt is LoanValidationEvent
                                                          ? OContractStatus.Pending
                                                          : OContractStatus.Active;
                        }

                    }
                    //come back after write off
                    if (evnt is WriteOffEvent)
                    {
                        contract.WrittenOff = false;
                        contract.ContractStatus = OContractStatus.Active;
                        CreditInsuranceEvent lciw = contract.GetNotDeletedInsuranceWriteOff();
                        if (lciw != null)
                        {
                            _ePs.CancelFireEvent(lciw, sqlTransaction, contract, contract.Product.Currency.Id);
                            lciw.Deleted = true;
                        }

                    }

                    _loanManager.UpdateLoan(contract, sqlTransaction);

                    FundingLineEvent flFundingLineEvent;

                    if (cancelledEvent is LoanDisbursmentEvent)
                    {
                        if (contract.HasCompulsoryAmount())
                        {
                            _savingEventManager.DeleteSavingsEventByLoanEventId(
                                cancelledEvent.ParentId ?? cancelledEvent.Id, sqlTransaction);
                            SavingBlockCompulsarySavingsEvent savingBlockEvent =
                                contract.CompulsorySavings.GetBlockCompulsorySavingEvent();
                            savingBlockEvent.Deleted = true;
                        }

                        LoanDisbursmentEvent temp = (LoanDisbursmentEvent) cancelledEvent;
                        flFundingLineEvent = new FundingLineEvent
                            {
                                Code = String.Concat("DE_", contract.Code),
                                Type = OFundingLineEventTypes.Disbursment,
                                Amount = temp.Amount,
                                Movement = OBookingDirections.Debit,
                                CreationDate = TimeProvider.Now,
                                FundingLine = contract.FundingLine
                            };
                        DeleteFundingLineEvent(ref contract, flFundingLineEvent, sqlTransaction);
                        _clientManager.DecrementLoanCycleByContractId(contract.Id, sqlTransaction);
                        // delete entry fee events
                        foreach (Event contractEvent in contract.Events)
                        {
                            if (contractEvent.Deleted)
                                continue;
                            if (contractEvent is LoanEntryFeeEvent)
                            {
                                _ePs.CancelFireEvent(contractEvent, sqlTransaction, contract,
                                                     contract.Product.Currency.Id);
                                contractEvent.Deleted = true;
                                CallInterceptor(new Dictionary<string, object>
                                {
                                    {"Loan", contract},
                                    {"Event", contractEvent},
                                    {"Deleted", true},
                                    {"SqlTransaction", sqlTransaction}
                                });
                            }
                            if (contractEvent is CreditInsuranceEvent)
                            {
                                _ePs.CancelFireEvent(contractEvent, sqlTransaction, contract,
                                                     contract.Product.Currency.Id);
                                contractEvent.Deleted = true;
                            }
                        }

                        if (evnt.ClientType == OClientTypes.Person)
                        {
                            if (_econimcActivityServices.EconomicActivityLoanHistoryExists(contract.Id, pClient.Id,
                                                                                           sqlTransaction))
                                _econimcActivityServices.UpdateDeletedEconomicActivityLoanHistory(contract.Id,
                                                                                                  pClient.Id,
                                                                                                  ((Person) pClient)
                                                                                                      .Activity.Id,
                                                                                                  sqlTransaction, true);
                        }
                        else if (evnt.ClientType == OClientTypes.Group)
                        {
                            foreach (Member member in ((Group) pClient).Members)
                            {
                                if (_econimcActivityServices.EconomicActivityLoanHistoryExists(contract.Id,
                                                                                               member.Tiers.Id,
                                                                                               sqlTransaction))
                                    _econimcActivityServices.UpdateDeletedEconomicActivityLoanHistory(contract.Id,
                                                                                                      member.Tiers.Id,
                                                                                                      ((Person)
                                                                                                       member.Tiers)
                                                                                                          .Activity.Id,
                                                                                                      sqlTransaction,
                                                                                                      true);
                            }
                        }
                        else if (evnt.ClientType == OClientTypes.Corporate)
                        {
                            if (_econimcActivityServices.EconomicActivityLoanHistoryExists(contract.Id, pClient.Id,
                                                                                           sqlTransaction))
                                _econimcActivityServices.UpdateDeletedEconomicActivityLoanHistory(contract.Id,
                                                                                                  pClient.Id,
                                                                                                  ((Corporate) pClient)
                                                                                                      .Activity.Id,
                                                                                                  sqlTransaction, true);
                        }

                    }
                    else if (cancelledEvent is RepaymentEvent)
                    {
                        RepaymentEvent temp = (RepaymentEvent) cancelledEvent;
                        decimal amountCalc = (temp.Principal.HasValue ? temp.Principal.Value : 0) +
                                             (ApplicationSettings.GetInstance(_user != null ? _user.Md5 : "").
                                                                  InterestsCreditedInFL
                                                  ? ((temp.Interests.HasValue ? temp.Interests.Value : 0)
                                                     + (temp.Penalties.HasValue ? temp.Penalties.Value : 0))
                                                  : 0);

                        if (amountCalc > 0 ||
                            ApplicationSettings.GetInstance(_user != null ? _user.Md5 : "").InterestsCreditedInFL)
                        {
                            flFundingLineEvent = new FundingLineEvent
                                {
                                    Code = String.Concat("RE_", contract.Code, "_INS_", temp.InstallmentNumber),
                                    Type = OFundingLineEventTypes.Repay,
                                    Amount = amountCalc,
                                    CreationDate = TimeProvider.Now,
                                    FundingLine =
                                        _fundingLineServices.SelectFundingLineById(contract.FundingLine.Id,
                                                                                   sqlTransaction)
                                };

                            //temporary line to check whether funding line has enough amount to debit repayment event
                            flFundingLineEvent.Movement = OBookingDirections.Debit;
                            _fundingLineServices.ApplyRulesAmountEventFundingLine(flFundingLineEvent);
                            flFundingLineEvent.Movement = OBookingDirections.Credit;
                            DeleteFundingLineEvent(ref contract, flFundingLineEvent, sqlTransaction);
                        }
                    }

                    CancelSavingsEvent(cancelledEvent, sqlTransaction);
                    sqlTransaction.Commit();
                    sqlTransaction.Dispose();
                    SetClientStatus(contract, pClient);
                }
                catch (Exception)
                {
                    sqlTransaction.Rollback();
                    throw;
                }

                return cancelledEvent;
            }
        }
Пример #5
0
        public Loan AddTranche(Loan loan, IClient client, ITrancheConfiguration trancheConfiguration, IList<LoanEntryFee> entryFees, PaymentMethod paymentMethod)
        {
            using (var connection = _loanManager.GetConnection())
            using (var transaction = connection.BeginTransaction())
            {
                try
                {
                    CheckTranche(trancheConfiguration.StartDate, loan, trancheConfiguration.Amount);

                    var copyOfLoan = SimulateTranche(loan, trancheConfiguration);
                    var startInstallment =
                        copyOfLoan.InstallmentList
                                  .FindAll(i => i.ExpectedDate <= trancheConfiguration.StartDate)
                                  .LastOrDefault();
                    var trancheEvent = new TrancheEvent
                        {
                            Amount = trancheConfiguration.Amount,
                            ApplyNewInterest = trancheConfiguration.ApplyNewInterestRateToOlb,
                            Maturity = trancheConfiguration.NumberOfInstallments,
                            StartDate = trancheConfiguration.StartDate,
                            Date = trancheConfiguration.StartDate,
                            InterestRate = trancheConfiguration.InterestRate/100,
                            Number = copyOfLoan.GivenTranches.Count,
                            FirstRepaymentDate = trancheConfiguration.PreferredFirstInstallmentDate,
                            GracePeriod = trancheConfiguration.GracePeriod,
                            StartedFromInstallment = startInstallment == null ? 0 : startInstallment.Number,
                            User = _user,
                            PaymentMethod = paymentMethod
                        };

                    trancheEvent.User = _user;

                    //insert into table TrancheEvent
                    _ePs.FireEvent(trancheEvent, copyOfLoan, transaction);
                    copyOfLoan.Events.Add(trancheEvent);

                    CallInterceptor(new Dictionary<string, object>
                    {
                        {"Loan", copyOfLoan},
                        {"Event", trancheEvent},
                        {"SqlTransaction", transaction}
                    });

                    // Add entry fee events
                    foreach (var entryFee in entryFees)
                    {
                        if (entryFee.FeeValue == 0) continue;
                        var entryFeeEvent = new LoanEntryFeeEvent
                        {
                            Fee = entryFee.FeeValue,
                            Code = "LEE" + entryFee.ProductEntryFee.Index,
                            DisbursementEventId = trancheEvent.Id,
                            Cancelable = true,
                            User = User.CurrentUser,
                            Date = trancheEvent.Date
                        };
                        _ePs.FireEvent(entryFeeEvent, copyOfLoan, transaction);
                        copyOfLoan.Events.Add(entryFeeEvent);
                    }

                    var trancheEntryFeeEvent =
                        copyOfLoan.Events.OfType<LoanEntryFeeEvent>()
                                  .First(i => i.DisbursementEventId == trancheEvent.Id);
                    if (trancheEntryFeeEvent != null)
                        CallInterceptor(new Dictionary<string, object>
                        {
                            {"Loan", copyOfLoan},
                            {
                                "Event", new LoanEntryFeeEvent
                                    {
                                        Id = trancheEntryFeeEvent.Id,
                                        Fee = entryFees.Sum(i => i.FeeValue),
                                        Code = "LEE0"
                                    }
                            },
                            {"SqlTransaction", transaction}
                        });

                    ArchiveInstallments(loan, trancheEvent, transaction);

                    //delete all the old installments of the table Installments
                    _instalmentManager.DeleteInstallments(loan.Id, transaction);

                    //insert all the new installments in the table Installments
                    _instalmentManager.AddInstallments(copyOfLoan.InstallmentList, copyOfLoan.Id, transaction);

                    //Activate the contract if it's closed because of new tranch
                    if (copyOfLoan.Closed)
                    {
                        copyOfLoan.ContractStatus = OContractStatus.Active;
                        copyOfLoan.Closed = false;
                        _loanManager.UpdateLoan(copyOfLoan, transaction);
                    }
                    //in the feature might be combine UpdateLoan + UpdateLoanWithinTranche
                    _loanManager.UpdateLoanWithinTranche(
                        trancheConfiguration.InterestRate/100,
                        copyOfLoan.NbOfInstallments,
                        copyOfLoan,
                        transaction);
                    copyOfLoan.GivenTranches.Add(trancheEvent);
                    transaction.Commit();

                    SetClientStatus(copyOfLoan, client);
                    return copyOfLoan;
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
Пример #6
0
 public TrancheEvent AddTranche(TrancheEvent pTe)
 {
     GivenTranches.Add(pTe);
     return pTe;
 }
Пример #7
0
        /// <summary>
        /// Method to Disburse money for the contract
        /// </summary>
        /// <param name="pDisburseDate">Date of disbursment</param>
        /// <param name="pDisableFees">when true, entry commission paid for this contract are set to 0</param>
        /// <param name="pAlignInstallmentsDatesOnRealDisbursmentDate"></param>
        /// <returns>A LoanDisburment event if correct date of disbursment. /!\Becarful, loan must be non debursed </returns>
        public LoanDisbursmentEvent Disburse(DateTime pDisburseDate, bool pAlignInstallmentsDatesOnRealDisbursmentDate, bool pDisableFees)
        {
            LoanDisbursmentEvent e = _generalSettings.AccountingProcesses == OAccountingProcesses.Cash
                                         ? GenerateEvents.Cash.GenerateLoanDisbursmentEvent(this, _generalSettings,
                                                                                            pDisburseDate,
                                                                                            pAlignInstallmentsDatesOnRealDisbursmentDate,
                                                                                            pDisableFees, _user)
                                         : GenerateEvents.Accrual.GenerateLoanDisbursmentEvent(this, _generalSettings,
                                                                                               pDisburseDate,
                                                                                               pAlignInstallmentsDatesOnRealDisbursmentDate,
                                                                                               pDisableFees, _user);
            e.Interest = GetTotalInterestDue();
            if (!pDisableFees)
            {
                List<OCurrency> entryFees = GetEntryFees();
                if (entryFees != null)
                {
                    e.Commissions = new List<LoanEntryFeeEvent>();
                    for (int i=0; i<entryFees.Count; i++)
                    {
                        LoanEntryFeeEvent loanEntryFeeEvent = new LoanEntryFeeEvent
                                                                  {
                                                                      Fee =
                                                                          Product.Currency.UseCents
                                                                              ? Math.Round(entryFees[i].Value, 2)
                                                                              : Math.Round(entryFees[i].Value,
                                                                                           MidpointRounding.AwayFromZero),
                                                                      Code = "LEE" + LoanEntryFeesList[i].ProductEntryFee.Index,
                                                                      DisbursementEventId = e.Id,
                                                                      Cancelable = true,
                                                                      User = User.CurrentUser,
                                                                      Date = e.Date
                                                                  };
                        e.Commissions.Add(loanEntryFeeEvent);

                        if (Teller.CurrentTeller != null && Teller.CurrentTeller.Id != 0)
                            loanEntryFeeEvent.TellerId = Teller.CurrentTeller.Id;

                        if (loanEntryFeeEvent.Fee > 0)
                        {
                            Events.Add(loanEntryFeeEvent);
                        }
                    }
                }
            }

            if (HasCompulsoryAmount())
            {
                SavingBlockCompulsarySavingsEvent savingBlockEvent = new SavingBlockCompulsarySavingsEvent();
                Debug.Assert(CompulsorySavingsPercentage != null,
                             string.Format(
                                 "Loan with code {0}, should be cheked for compulsory existance, before adding saving block event",
                                 Code)
                    );
                OCurrency csAmount = (Amount.Value * CompulsorySavingsPercentage.Value) / 100m;
                int contracId = CompulsorySavings.Id;
                savingBlockEvent.ContracId = contracId;
                savingBlockEvent.User = _user;
                savingBlockEvent.Amount = csAmount;
                savingBlockEvent.Date = StartDate;
                savingBlockEvent.EntryDate = TimeProvider.Now;
                savingBlockEvent.Cancelable = true;
                CompulsorySavings.Events.Add(savingBlockEvent);
            }

            CreditInsuranceEvent cie = GetCreditInsuranceEvent(e);
            if (cie.Commission > 0)
                Events.Add(cie);

            GivenTranches.Clear();
            TrancheEvent trancheEvent = new TrancheEvent
            {
                StartDate = pDisburseDate,
                Maturity = NbOfInstallments,
                Amount = Amount,
                InterestRate = (decimal)InterestRate,
                ApplyNewInterest = false,
                Number = 0
            };

            GivenTranches.Add(trancheEvent);
            return e;
        }
Пример #8
0
        public void AddLoanEvent(TrancheEvent trancheEvent, int contractId, SqlTransaction sqlTransac)
        {
            trancheEvent.Id = AddLoanEventHead(trancheEvent, contractId, sqlTransac);

            const string q = @"INSERT INTO [TrancheEvents]
                                     ([id],[interest_rate],[amount],[maturity],[start_date], [applied_new_interest], [started_from_installment])
                                     VALUES(@id, @interest_rate, @amount, @maturity, @start_date, @applied_new_interest, @started_from_installment)";

            using (OpenCbsCommand c = new OpenCbsCommand(q, sqlTransac.Connection, sqlTransac))
            {
                SetLoanTrancheEvent(trancheEvent, c);
                c.ExecuteNonQuery();
            }
        }
Пример #9
0
 private static void SetLoanTrancheEvent(TrancheEvent pEvent, OpenCbsCommand c)
 {
     c.AddParam("@id", pEvent.Id);
     c.AddParam("@interest_rate", pEvent.InterestRate);
     c.AddParam("@amount", pEvent.Amount);
     c.AddParam("@maturity", pEvent.Maturity);
     c.AddParam("@start_date", pEvent.StartDate);
     c.AddParam("@applied_new_interest", pEvent.ApplyNewInterest);
     c.AddParam("@started_from_installment", pEvent.StartedFromInstallment);
 }
Пример #10
0
        private static OCurrency GetValue(TrancheEvent eventItem, ContractAccountingRule rule)
        {
            OCurrency amount = 0;

            if (rule.EventAttribute.Name.ToLower() == "amount")
            {
                amount = eventItem.Amount;
            }

            return amount;
        }
Пример #11
0
 private static void SetTrancheEvent(OpenCbsCommand cmd, TrancheEvent trancheEvent)
 {
     cmd.AddParam("@Id",  trancheEvent.Id);
     cmd.AddParam("@InterestRate", trancheEvent.InterestRate);
     cmd.AddParam("@Amount", trancheEvent.Amount);
     cmd.AddParam("@Maturity", trancheEvent.Maturity);
     cmd.AddParam("@StartDate", trancheEvent.StartDate);
     cmd.AddParam("@applied_new_interest", trancheEvent.StartDate);
 }
Пример #12
0
        public void AddLoanEvent(TrancheEvent trancheEvent, int contractId, SqlTransaction transaction)
        {
            trancheEvent.Id = AddLoanEventHead(trancheEvent, contractId, transaction);

            const string query = @"
                INSERT INTO dbo.TrancheEvents
                (
                    [id],
                    [interest_rate],
                    [amount],
                    [maturity],
                    [start_date],
                    [applied_new_interest],
                    [started_from_installment],
                    first_repayment_date,
                    grace_period
                )
                VALUES
                (
                    @id,
                    @interest_rate,
                    @amount,
                    @maturity,
                    @start_date,
                    @applied_new_interest,
                    @started_from_installment,
                    @first_repayment_date,
                    @grace_period
                )
            ";

            using (var command = new OpenCbsCommand(query, transaction.Connection, transaction))
            {
                SetLoanTrancheEvent(trancheEvent, command);
                command.ExecuteNonQuery();
            }
        }
Пример #13
0
 private static void SetLoanTrancheEvent(TrancheEvent trancheEvent, OpenCbsCommand command)
 {
     command.AddParam("@id", trancheEvent.Id);
     command.AddParam("@interest_rate", trancheEvent.InterestRate);
     command.AddParam("@amount", trancheEvent.Amount);
     command.AddParam("@maturity", trancheEvent.Maturity);
     command.AddParam("@start_date", trancheEvent.StartDate);
     command.AddParam("@applied_new_interest", trancheEvent.ApplyNewInterest);
     command.AddParam("@started_from_installment", trancheEvent.StartedFromInstallment);
     command.AddParam("@first_repayment_date", trancheEvent.FirstRepaymentDate);
     command.AddParam("@grace_period", trancheEvent.GracePeriod);
 }
Пример #14
0
        public Loan AddTranche(Loan loan, IClient client, ITrancheConfiguration trancheConfiguration)
        {
            using (var connection = _loanManager.GetConnection())
            using (var transaction = connection.BeginTransaction())
            {
                try
                {
                    CheckTranche(trancheConfiguration.StartDate, loan, trancheConfiguration.Amount);

                    var copyOfLoan = SimulateTranche(loan, trancheConfiguration);
                    var startInstallment =
                        copyOfLoan.InstallmentList
                        .FindAll(i => i.ExpectedDate <= trancheConfiguration.StartDate)
                        .LastOrDefault();
                    var trancheEvent = new TrancheEvent
                    {
                        Amount = trancheConfiguration.Amount,
                        ApplyNewInterest = trancheConfiguration.ApplyNewInterestRateToOlb,
                        Maturity = trancheConfiguration.NumberOfInstallments,
                        StartDate = trancheConfiguration.StartDate,
                        Date = trancheConfiguration.StartDate,
                        InterestRate = trancheConfiguration.InterestRate / 100,
                        Number = copyOfLoan.GivenTranches.Count,
                        FirstRepaymentDate = trancheConfiguration.PreferredFirstInstallmentDate,
                        GracePeriod = trancheConfiguration.GracePeriod,
                        StartedFromInstallment = startInstallment == null ? 0 : startInstallment.Number,
                        User = _user,
                    };

                    trancheEvent.User = _user;

                    //insert into table TrancheEvent
                    _ePs.FireEvent(trancheEvent, copyOfLoan, transaction);

                    ArchiveInstallments(loan, trancheEvent, transaction);

                    //delete all the old installments of the table Installments
                    _instalmentManager.DeleteInstallments(loan.Id, transaction);

                    //insert all the new installments in the table Installments
                    _instalmentManager.AddInstallments(copyOfLoan.InstallmentList, copyOfLoan.Id, transaction);

                    //Activate the contract if it's closed because of new tranch
                    if (copyOfLoan.Closed)
                    {
                        copyOfLoan.ContractStatus = OContractStatus.Active;
                        copyOfLoan.Closed = false;
                        _loanManager.UpdateLoan(copyOfLoan, transaction);
                    }
                    //in the feature might be combine UpdateLoan + UpdateLoanWithinTranche
                    _loanManager.UpdateLoanWithinTranche(
                        trancheConfiguration.InterestRate / 100,
                        copyOfLoan.NbOfInstallments,
                        copyOfLoan,
                        transaction);
                    copyOfLoan.Events.Add(trancheEvent);
                    copyOfLoan.GivenTranches.Add(trancheEvent);
                    transaction.Commit();

                    SetClientStatus(copyOfLoan, client);
                    return copyOfLoan;
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
Пример #15
0
        public TrancheEvent AddTranche(TrancheOptions trancheOptions)
        {
            _trancheEvent = new TrancheEvent
                                {
                                    Date = trancheOptions.TrancheDate,
                                    InterestRate = trancheOptions.InterestRate,
                                    Amount = trancheOptions.TrancheAmount,
                                    Maturity = trancheOptions.CountOfNewInstallments,
                                    StartDate = trancheOptions.TrancheDate,
                                    Number = _currentLoan.GivenTranches.Count
                                };

            trancheOptions.Number = _trancheEvent.Number;

            switch (_currentLoan.Product.LoanType)
            {
                case OLoanTypes.Flat:
                    {
                        AddFlatTranche(trancheOptions);
                    }
                    break;

                case OLoanTypes.DecliningFixedPrincipal:
                    {
                        AddFixedPrincipalTranche(trancheOptions);
                    }
                    break;

                case OLoanTypes.DecliningFixedInstallments:
                    {
                        AddFixedInstallmentTranche(trancheOptions);
                    }
                    break;
            }

            _currentLoan.CalculateStartDates();
            return _trancheEvent;
        }