private void LoanInterestAccrualOrigination(LoanInterestAccrualEvent loanInterestAccrualEvent, Loan loanContract, SqlTransaction sqlTransac) { _eventManagement.AddLoanEvent(loanInterestAccrualEvent, loanContract.Id, sqlTransac); }
public void LoanInterestAccrual() { const string q = @"WITH summary AS (SELECT al.id ,inst.interest ,inst.expected_date ,ROW_NUMBER() OVER(PARTITION BY al.id ORDER BY al.id) AS rk FROM Activeloans(@date,0) AS al LEFT JOIN InstallmentSnapshot(@date) AS inst ON inst.contract_id=al.id WHERE @date < inst.expected_date) SELECT s.id, s.interest, s.expected_date FROM summary s WHERE s.rk = 1"; const string q1 = @"SELECT TOP 1 date FROM ( SELECT MAX(inst.expected_date) AS date FROM Activeloans(@date, 0) AS al LEFT JOIN InstallmentSnapshot(@date) AS inst ON inst.contract_id=al.id WHERE @date >= inst.expected_date AND al.id=@contractId group by al.id UNION ALL SELECT start_date FROM Contracts WHERE id=@contractId) t ORDER by date desc"; var user = ServicesProvider.GetInstance().GetUserServices().Find(1); var em = new EventManager(user); var date = LastInterestAccrualEventDate(); while (date.Date < DateTime.Now.Date) { date = date.AddDays(1); using (var connection = _loanManager.GetConnection()) using (var transaction = connection.BeginTransaction()) try { var interestEventList = new List<LoanInterestAccrualEvent>(); using (var c = new OpenCbsCommand(q, connection, transaction)) { c.AddParam("@date", date); using (var r = c.ExecuteReader()) { while (r.Read()) { var loanId = r.GetInt("id"); var interest = r.GetDecimal("interest"); var expectedDate = r.GetDateTime("expected_date"); var interestEvent = new LoanInterestAccrualEvent { Date = date, User = user, Interest = interest, InstallmentExpectedDate = expectedDate, ContracId = loanId }; interestEventList.Add(interestEvent); } } } foreach (var interestEvent in interestEventList) { var previousInstallmentDate = new DateTime(); using (var c = new OpenCbsCommand(q1, connection, transaction)) { c.AddParam("@date", date); c.AddParam("@contractId", interestEvent.ContracId); using (var r = c.ExecuteReader()) { r.Read(); previousInstallmentDate = r.GetDateTime("date"); } } var passedPeriod = interestEvent.Date - previousInstallmentDate; if (passedPeriod.Days > 30) interestEvent.Interest = 0; else if (interestEvent.Date == interestEvent.InstallmentExpectedDate) interestEvent.Interest -= GetSumOfAccruedInterests(interestEvent.Date, previousInstallmentDate, interestEvent.ContracId); else interestEvent.Interest = interestEvent.Interest.Value/30; em.AddLoanEvent(interestEvent, interestEvent.ContracId, transaction); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }
public void LoanInterestAccrual(DateTime launchDate) { var listOfLoans = _loanManager.GetListOfLoansToAccrueInterest(launchDate); var em = new EventManager(User.CurrentUser); using (var connection = _loanManager.GetConnection()) foreach (var item in listOfLoans) { var date = item.Value; var loan = SelectLoan(item.Key, true, false, false); while (date < launchDate) { date = date.AddDays(1); var interest = GetDailyInterestForLoan(loan, date); if (interest <= 0) continue; var interestEvent = new LoanInterestAccrualEvent { Interest = interest, Date = date, User = User.CurrentUser, ContracId = item.Key }; using (var transaction = connection.BeginTransaction()) try { _ePs.FireEvent(interestEvent, loan, transaction); CallInterceptor(new Dictionary<string, object> { {"Loan", loan}, {"Event", interestEvent}, {"SqlTransaction", transaction} }); transaction.Commit(); } catch (Exception) { transaction.Rollback(); //throw; } } } }
public void AddLoanEvent(LoanInterestAccrualEvent pEvent, int contractId, SqlTransaction transaction) { pEvent.Id = AddLoanEventHead(pEvent, contractId, transaction); const string q = @"INSERT INTO [AccrualInterestLoanEvents]( [id], [interest]) VALUES(@id, @interest)"; using (var c = new OpenCbsCommand(q, transaction.Connection, transaction)) { SetLoanInterestAccrualEvent(c, pEvent, pEvent.Interest); c.ExecuteNonQuery(); } }