public Loan AddTranche(Loan pContract, IClient pClient, DateTime pDate, int pNbOfMaturity, int pTrancheAmount, bool pApplyNewInterestOnOLB, decimal pNewInterestRate) { using (SqlConnection conn = _loanManager.GetConnection()) using (SqlTransaction sqlTransaction = conn.BeginTransaction()) { try { CheckTranche(pDate, pContract, pTrancheAmount); Loan copyOfLoan = pContract.Copy(); TrancheOptions to = new TrancheOptions { TrancheDate = pDate, CountOfNewInstallments = pNbOfMaturity, TrancheAmount = pTrancheAmount, InterestRate = pNewInterestRate, ApplyNewInterestOnOLB = pApplyNewInterestOnOLB }; TrancheEvent trancheEvent = pContract.CalculateTranche(to); trancheEvent.User = _user; //insert into table TrancheEvent _ePs.FireEvent(trancheEvent, pContract, sqlTransaction); ArchiveInstallments(copyOfLoan, trancheEvent, sqlTransaction); //delete all the old installments of the table Installments _instalmentManager.DeleteInstallments(pContract.Id, sqlTransaction); //insert all the new installments in the table Installments _instalmentManager.AddInstallments(pContract.InstallmentList, pContract.Id, sqlTransaction); //Activate the contract if it's closed because of new tranch if (pContract.Closed) { pContract.ContractStatus = OContractStatus.Active; pContract.Closed = false; _loanManager.UpdateLoan(pContract, sqlTransaction); } //in the feature might be combine UpdateLoan + UpdateLoanWithinTranche _loanManager.UpdateLoanWithinTranche(to.InterestRate, pContract.NbOfInstallments, pContract, sqlTransaction); pContract.Events.Add(trancheEvent); pContract.GivenTranches.Add(trancheEvent); sqlTransaction.Commit(); SetClientStatus(pContract, pClient); return pContract; } catch (Exception ex) { if (sqlTransaction != null) sqlTransaction.Rollback(); throw ex; } } }