public RepaymentEvent SaveRepaymentEvent(RepaymentEvent repaymentEvent, IDbTransaction tx) { var query = @" insert into dbo.ContractEvents (event_type, contract_id, event_date, user_id, is_deleted, entry_date) values (@Code, @LoanId, @EventDate, @UserId, 0, getdate()) select cast(scope_identity() as int) "; repaymentEvent.Id = tx.Connection.Query<int>(query, repaymentEvent, tx).First(); query = @" insert into dbo.RepaymentEvents (id, past_due_days, principal, interests, installment_number, commissions, penalties, payment_method_id, bounce_fee) values (@Id, @LateDays, @Principal, @Interest, @InstallmentNumber, 0, 0, 1, 0) "; tx.Connection.Execute(query, repaymentEvent, tx); // Create a funding line event query = @" declare @code nvarchar(max) declare @funding_line_id int select @code = contract_code from dbo.Contracts where id = @LoanId select @funding_line_id = fundingLine_id from dbo.Credit where id = @LoanId declare @funding_line_code nvarchar(max) set @funding_line_code = 'RE_' + @code + '_INS_' + cast(@InstallmentNumber as nvarchar(max)) insert into dbo.FundingLineEvents (code, amount, direction, fundingline_id, deleted, creation_date, type, user_id, contract_event_id) values (@funding_line_code, @Principal, 1, @funding_line_id, 0, @EventDate, 2, @UserId, @Id) "; tx.Connection.Execute(query, new { repaymentEvent.LoanId, repaymentEvent.InstallmentNumber, repaymentEvent.Principal, repaymentEvent.EventDate, repaymentEvent.UserId, repaymentEvent.Id }, tx); return repaymentEvent; }
private static RepaymentEvent GetRepaymentEvent(Loan loan, Loan repaidLoan) { var firstUnpaidInstallment = loan.Schedule.Find(x => !x.Repaid); var lateDays = (TimeProvider.Today - firstUnpaidInstallment.ExpectedDate.Date).Days; lateDays = lateDays < 0 ? 0 : lateDays; var repaymentEvent = new RepaymentEvent(); repaymentEvent.LoanId = loan.Id; repaymentEvent.Code = lateDays > 0 ? "RBLE" : "RGLE"; repaymentEvent.InstallmentNumber = firstUnpaidInstallment.Number; repaymentEvent.EventDate = TimeProvider.Now; repaymentEvent.UserId = User.CurrentUser.Id; repaymentEvent.LateDays = lateDays; repaymentEvent.Principal = repaidLoan.Schedule.Sum(x => x.PaidPrincipal) - loan.Schedule.Sum(x => x.PaidPrincipal); repaymentEvent.Interest = repaidLoan.Schedule.Sum(x => x.PaidInterest) - loan.Schedule.Sum(x => x.PaidInterest); return repaymentEvent; }