Exemplo n.º 1
0
        public void SetUp()
        {
            _session = CreateSession();
            _lhrepo  = new LoanHistoryRepository(_session);

            using (var tx = _session.BeginTransaction())
            {
                var type = new StandardLoanType()
                {
                    Id = 1, Name = "Standard"
                };
                _loan = new Loan()
                {
                    LoanType = type
                };
                _session.Save(type);
                _session.Save(_loan);
                tx.Commit();
            }
        }
Exemplo n.º 2
0
        }         // 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