public void adding_transaction_to_loan_generates_refnumber() { var loan = new Loan() { RefNumber = "01141490002" }; var tran = new PaypointTransaction(); loan.AddTransaction(tran); Assert.That(tran.RefNumber.Length, Is.EqualTo(14)); Assert.That(tran.RefNumber, Is.EqualTo("01141490002" + "001")); }
public void adding_transaction_to_loan_sets_references() { var loan = new Loan() { Id = 1 }; var tran = new PaypointTransaction() { Id = 2 }; loan.AddTransaction(tran); Assert.That(tran.Loan.Id, Is.EqualTo(loan.Id)); Assert.That(loan.Transactions.Count, Is.EqualTo(1)); Assert.That(loan.Transactions.First().Id, Is.EqualTo(tran.Id)); }
} // constructor /// <summary> /// Заплатить за кредит. Платёж может быть произвольный. Early, On time, Late. /// Perform loan payment. Payment can be manual. Early, On time, Late. /// </summary> public virtual decimal PayLoan( Loan loan, string transId, decimal amount, string ip, DateTime?term = null, string description = "payment from customer", bool interestOnly = false, string sManualPaymentMethod = null, NL_Payments nlPayment = null) { int customerID = loan.Customer.Id; var paymentTime = term ?? DateTime.UtcNow; var oldLoan = loan.Clone(); const string Manual = "--- manual ---"; string otherMethod = transId == Manual ? "Manual" : "Auto"; var loanTransactionMethod = this.loanTransactionMethodRepository.FindOrDefault(sManualPaymentMethod, otherMethod); var transaction = this.session.BeginTransaction(); var transactionItem = new PaypointTransaction { Amount = amount, Description = description, PostDate = paymentTime, Status = LoanTransactionStatus.Done, PaypointId = transId, IP = ip, LoanRepayment = oldLoan.Principal - loan.Principal, Interest = loan.InterestPaid - oldLoan.InterestPaid, InterestOnly = interestOnly, LoanTransactionMethod = loanTransactionMethod }; try { loan.AddTransaction(transactionItem); List <InstallmentDelta> deltas = loan.Schedule.Select(inst => new InstallmentDelta(inst)) .ToList(); var calculator = new LoanRepaymentScheduleCalculator(loan, paymentTime, this.amountToChargeFrom); calculator.RecalculateSchedule(); if (this._historyRepository != null) { var historyRecord = new LoanHistory(loan, paymentTime); this._historyRepository.SaveOrUpdate(historyRecord); } // if loan.UpdateStatus(paymentTime); if (loan.Customer != null) { loan.Customer.UpdateCreditResultStatus(); } if (loan.Id > 0) { foreach (InstallmentDelta dlt in deltas) { dlt.SetEndValues(); if (dlt.IsZero) { continue; } loan.ScheduleTransactions.Add(new LoanScheduleTransaction { Date = DateTime.UtcNow, FeesDelta = dlt.Fees.EndValue - dlt.Fees.StartValue, InterestDelta = dlt.Interest.EndValue - dlt.Interest.StartValue, Loan = loan, PrincipalDelta = dlt.Principal.EndValue - dlt.Principal.StartValue, Schedule = dlt.Installment, StatusAfter = dlt.Status.EndValue, StatusBefore = dlt.Status.StartValue, Transaction = transactionItem }); } // for each delta } // if if (nlPayment != null) { Log.InfoFormat("PayLoan: oldLoanID: {0} customer: {1} nlpayment {2}", loan.Id, customerID, nlPayment); // override those for backword compatibility nlPayment.PaymentMethodID = loanTransactionMethod.Id; nlPayment.Notes = description; nlPayment.CreationTime = DateTime.UtcNow; nlPayment.PaymentTime = paymentTime; nlPayment.Amount = amount; nlPayment.PaymentStatusID = (int)NLPaymentStatuses.Active; Log.InfoFormat("PayLoan: overriden nlpayment {0}", nlPayment); long nlLoanId = serviceInstance.GetLoanByOldID(loan.Id, customerID); if (nlLoanId == 0) { Log.InfoFormat("Failed to find nl loan for oldLoanID {0}, customer {1}", loan.Id, customerID); } else { nlPayment.LoanID = nlLoanId; // use argument's nlPayment data: CreatedByUserID if (nlPayment.PaymentSystemType == NLPaymentSystemTypes.Paypoint) { // workaround - from MakeAutomaticPayment sent transactionid with timestamp concated var card = loan.Customer.PayPointCards.FirstOrDefault(x => transId.StartsWith(x.TransactionId)); if (card == null) { Log.InfoFormat("PayPointCard for customer {0}, transId={1}, oldLoanID={2}, nl loanID={3} not found. nl payment\n {4}{5}", customerID, transId, loan.Id, nlPayment.LoanID, AStringable.PrintHeadersLine(typeof(NL_Payments)), nlPayment.ToStringAsTable()); } else { nlPayment.PaypointTransactions.Clear(); nlPayment.PaypointTransactions.Add(new NL_PaypointTransactions() { TransactionTime = paymentTime, Amount = amount, Notes = description, PaypointTransactionStatusID = (int)NLPaypointTransactionStatuses.Done, PaypointUniqueID = transId, PaypointCardID = card.Id, IP = ip }); } } serviceInstance.AddPayment(customerID, nlPayment, nlPayment.CreatedByUserID); } } transaction.Commit(); } catch (Exception ex) { Log.ErrorFormat("Failed to pay {1} pounds for loan {0}, rollbacking \n {2}", loan.Id, amount, ex); transaction.Rollback(); } Log.InfoFormat("LinkPaymentToInvestor {0} {1} {2} {3} {4} begin", transactionItem.Id, loan.Id, loan.Customer.Id, amount, paymentTime); serviceInstance.LinkPaymentToInvestor(1, transactionItem.Id, loan.Id, loan.Customer.Id, amount, paymentTime); // modified by elinar at 9/02/2016 EZ-4678 bugfix return(amount); } // PayLoan
} // constructor /// <exception cref="Exception">PacnetSafeGuard stopped money transfer</exception> /// <exception cref="OverflowException"><paramref /> is less than <see cref="F:System.TimeSpan.MinValue" /> or greater than <see cref="F:System.TimeSpan.MaxValue" />.-or-<paramref /> is <see cref="F:System.Double.PositiveInfinity" />.-or-<paramref name="value" /> is <see cref="F:System.Double.NegativeInfinity" />. </exception> /// <exception cref="LoanDelayViolationException">Condition. </exception> public Loan CreateLoan(Customer cus, decimal loanAmount, PayPointCard card, DateTime now, Ezbob.Backend.ModelsWithDB.NewLoan.NL_Model nlModel = null) { ValidateCustomer(cus); // continue (customer's data/status, finish wizard, bank account data) ValidateAmount(loanAmount, cus); // continue (loanAmount > customer.CreditSum) ValidateOffer(cus); // check offer validity dates - in AddLoan strategy ValidateLoanDelay(cus, now, TimeSpan.FromMinutes(1)); // checks if last loan was taken a minute before "now" - ?? to prevent multiple clicking on "create loan" button? ValidateRepaymentPeriodAndInterestRate(cus); bool isFakeLoanCreate = (card == null); var cr = cus.LastCashRequest; Loan loan = this.loanBuilder.CreateLoan(cr, loanAmount, now); var transfered = loan.LoanAmount - loan.SetupFee; PacnetReturnData ret; if (PacnetSafeGuard(cus, transfered)) { if (!isFakeLoanCreate && !cus.IsAlibaba) { ret = SendMoney(cus, transfered); VerifyAvailableFunds(transfered); } else { log.Debug( "Not sending money via Pacnet. isFake: {0}, isAlibaba: {1}", isFakeLoanCreate, cus.IsAlibaba ); ret = new PacnetReturnData { Status = "Done", TrackingNumber = "fake" }; } // if } else { log.Error("PacnetSafeGuard stopped money transfer"); // ReSharper disable once ThrowingSystemException throw new Exception("PacnetSafeGuard stopped money transfer"); } // if cr.HasLoans = true; loan.Customer = cus; loan.Status = LoanStatus.Live; loan.CashRequest = cr; loan.LoanType = cr.LoanType; loan.GenerateRefNumber(cus.RefNumber, cus.Loans.Count); if (nlModel == null) { nlModel = new NL_Model(cus.Id); } nlModel.UserID = this.context.UserId; // NL fund transfer nlModel.FundTransfer = new NL_FundTransfers() { Amount = loanAmount, // logic transaction - full amount TransferTime = now, FundTransferStatusID = (int)NLFundTransferStatuses.Pending, // (int)NLPacnetTransactionStatuses.InProgress, LoanTransactionMethodID = (int)NLLoanTransactionMethods.Pacnet, PacnetTransactions = new List <NL_PacnetTransactions>() }; PacnetTransaction loanTransaction; if (!cus.IsAlibaba) { loanTransaction = new PacnetTransaction { Amount = loan.LoanAmount, Description = "Ezbob " + FormattingUtils.FormatDateToString(DateTime.Now), PostDate = now, Status = (isFakeLoanCreate) ? LoanTransactionStatus.Done : LoanTransactionStatus.InProgress, TrackingNumber = ret.TrackingNumber, PacnetStatus = ret.Status, Fees = loan.SetupFee, LoanTransactionMethod = this.tranMethodRepo.FindOrDefault("Pacnet"), }; // NL pacnet transaction nlModel.FundTransfer.PacnetTransactions.Add(new NL_PacnetTransactions() { TransactionTime = now, Amount = loanAmount, Notes = "Ezbob " + FormattingUtils.FormatDateToString(DateTime.Now) + " Status: " + ret.Status + " Err:" + ret.Error, StatusUpdatedTime = DateTime.UtcNow, TrackingNumber = ret.TrackingNumber, PacnetTransactionStatusID = (isFakeLoanCreate) ? (int)NLPacnetTransactionStatuses.Done : (int)NLPacnetTransactionStatuses.InProgress, }); } else { // alibaba loanTransaction = new PacnetTransaction { Amount = loan.LoanAmount, Description = "Ezbob " + FormattingUtils.FormatDateToString(DateTime.Now), PostDate = now, Status = LoanTransactionStatus.Done, TrackingNumber = "alibaba deal. CustomerID: " + cus.Id, // TODO save who got the money PacnetStatus = ret.Status, Fees = loan.SetupFee, LoanTransactionMethod = this.tranMethodRepo.FindOrDefault("Manual") }; log.Debug("Alibaba loan, adding manual pacnet transaction to loan schedule"); // NL: only logic transaction created in "alibaba" case; real money transfer will be done later, not transferred to customer (alibaba buyer) directly, but to seller (3rd party) nlModel.FundTransfer.LoanTransactionMethodID = (int)NLLoanTransactionMethods.Manual; } // if // This is the place where the funds transferred to customer saved to DB log.Info( "Save transferred funds to customer {0} amount {1}, isFake {2} , isAlibaba {3}", cus.Id, transfered, isFakeLoanCreate, cus.IsAlibaba ); loan.AddTransaction(loanTransaction); var aprCalc = new APRCalculator(); loan.APR = (decimal)aprCalc.Calculate(loanAmount, loan.Schedule, loan.SetupFee, now); cus.AddLoan(loan); cus.FirstLoanDate = cus.Loans.Min(x => x.Date); cus.LastLoanDate = cus.Loans.Max(x => x.Date); cus.LastLoanAmount = cus.Loans.First(x => x.Date == cus.LastLoanDate).LoanAmount; cus.AmountTaken = cus.Loans.Sum(x => x.LoanAmount); cus.CreditSum = cus.CreditSum - loanAmount; if (loan.SetupFee > 0) { cus.SetupFee = loan.SetupFee; } /** * 1. Build/ReBuild agreement model - private AgreementModel GenerateAgreementModel(Customer customer, Loan loan, DateTime now, double apr); in \App\PluginWeb\EzBob.Web\Code\AgreementsModelBuilder.cs * 2. RenderAgreements: loan.Agreements.Add * 3. RenderAgreements: SaveAgreement (file?) \backend\Strategies\Misc\Agreement.cs strategy */ // NL model - loan. Create history here for agreements processing nlModel.Loan = new NL_Loans(); nlModel.Loan.Histories.Clear(); nlModel.Loan.Histories.Add(new NL_LoanHistory() { Amount = loanAmount, EventTime = now }); // populate nlModel by agreements data also this.agreementsGenerator.RenderAgreements(loan, true, nlModel); var loanHistoryRepository = new LoanHistoryRepository(this.session); loanHistoryRepository.SaveOrUpdate(new LoanHistory(loan, now)); // This is the place where the loan is created and saved to DB log.Info( "Create loan for customer {0} cash request {1} amount {2}", cus.Id, loan.CashRequest.Id, loan.LoanAmount ); // actually this is the place where the loan saved to DB this.session.Flush(); Loan oldloan = cus.Loans.First(s => s.RefNumber.Equals(loan.RefNumber)); nlModel.Loan.OldLoanID = oldloan.Id; nlModel.Loan.Refnum = loan.RefNumber; // TODO generate another refnum with new algorithm in addloan strategy try { // copy newly created agreementtemplateID (for new templeates) //foreach (NL_LoanAgreements ag in nlModel.Loan.LastHistory().Agreements) { // if (ag.LoanAgreementTemplateID == 0) // ag.LoanAgreementTemplateID = oldloan.Agreements.FirstOrDefault(a => a.FilePath.Equals(ag.FilePath)).TemplateID; //} this.serviceClient.Instance.AddLoan(null, cus.Id, nlModel); // ReSharper disable once CatchAllClause } catch (Exception ex) { log.Debug("Failed to save new loan {0}", ex); } // try if (!isFakeLoanCreate) { this.serviceClient.Instance.CashTransferred(cus.Id, transfered, loan.RefNumber, cus.Loans.Count() == 1); } this.serviceClient.Instance.LinkLoanToInvestor(this.context.UserId, cus.Id, loan.Id); // verify see above line 45-48 // // ++++++++ // \App\PluginWeb\EzBob.Web\Code\LoanBuilder.cs , method public Loan CreateNewLoan(CashRequest cr, decimal amount, DateTime now, int term, int interestOnlyTerm = 0) // // calculate setupFee // calculate CalculateBrokerFee // var loanLegal = cr.LoanLegals.LastOrDefault(); // // --------------------- // file \Integration\PaymentServices\Calculators\LoanScheduleCalculator.cs, method // public IList<LoanScheduleItem> Calculate(decimal total, Loan loan = null, DateTime? startDate = null, int interestOnlyTerm = 0) // // calculate Schedules, but use it only for "fill in loan with total data" // GetDiscounts // loanType => GetBalances => fill in loan with total data: Interest, LoanAmount; Principal, Balance (loan.LoanAmount loan.Interest); InterestRate; startDate.Value // --------------------- // // LoanSource // brokerFee // +++++++++++++++++++++ // // line 85 // prepare pacnet transaction => add to loan => loan.arp => add loan to cutomer // agreement // save loan via new LoanHistory (139) // SF // flush this.serviceClient.Instance.SalesForceUpdateOpportunity( cus.Id, cus.Id, new ServiceClientProxy.EzServiceReference.OpportunityModel { Email = cus.Name, CloseDate = now, TookAmount = (int)loan.LoanAmount, ApprovedAmount = (int)(cus.CreditSum ?? 0) + (int)loanAmount, DealCloseType = OpportunityDealCloseReason.Won.ToString(), Origin = cus.CustomerOrigin.Name } ); this.serviceClient.Instance.SalesForceAddUpdateLeadAccount(cus.Id, cus.Name, cus.Id, false, false); //update account with new number of loans HandleSalesForceTopup(cus, now); //EZ-3908 return(loan); } // CreateLoan