Beispiel #1
0
 public function isFirstBid()
 {
     $lender = $this->getLender();
     if (BidQuery::create()->filterByLender($lender)->findOne()) {
         return false;
     }
     return true;
 }
Beispiel #2
0
 protected function verifyBid($con, Loan $loan, Lender $lender, $data)
 {
     $method = new ReflectionMethod($this->loanService, 'createBid');
     $method->setAccessible(true);
     $method->invoke($this->loanService, $con, $loan, $lender, $data);
     $bid = \Zidisha\Loan\BidQuery::create()->filterByLoan($loan)->filterByLender($lender)->orderById('desc')->findOne();
     $newBidTransaction = \Zidisha\Balance\TransactionQuery::create()->filterByLoan($loan)->filterByUserId($lender->getId())->orderById('desc')->findOne();
     verify($bid)->notEmpty();
     verify($bid->getInterestRate())->equals($data['interestRate']);
     verify($bid->getBidAmount())->equals(Money::create($data['amount']));
     verify($newBidTransaction)->notEmpty();
     verify($newBidTransaction->getAmount())->equals(Money::create($data['acceptedAmount'])->multiply(-1));
 }
Beispiel #3
0
 public function postEditBid()
 {
     $data = \Input::all();
     $lender = \Auth::user()->getLender();
     $loan = LoanQuery::create()->filterById($data['loanId'])->findOne();
     $totalLoanAmount = $loan->getAmount();
     $amount = Money::valueOf($data['amount'], Currency::valueOf('USD'));
     $oldBid = BidQuery::create()->filterByLender($lender)->filterByLoan($loan)->findOne();
     if ($oldBid->getBidAmount()->compare($amount) != -1) {
         \Flash::error(\Lang::get('Loan.edit-bid.amount'));
         return Redirect::back()->withInput();
     }
     if ($totalLoanAmount->getAmount() <= $data['amount']) {
         \Flash::error(\Lang::get('Loan.edit-bid.interest-rate'));
         return Redirect::back()->withInput();
     }
     if ($data['interestRate'] <= $oldBid->getInterestRate()) {
         \Flash::error(\Lang::get('Loan.edit-bid.bid-amount-exceded'));
         return Redirect::back()->withInput();
     }
     $bid = BidQuery::create()->filterByLoan($loan)->filterByLender($lender)->findOne();
     $this->loanService->editBid($bid, $data);
     Flash::success(\Lang::get('Loan.edit-bid.success') . $data['amount']);
     return Redirect::route('loan:index', $data['loanId']);
 }
Beispiel #4
0
 public function expireLoan(Loan $loan)
 {
     PropelDB::transaction(function ($con) use($loan) {
         $loan->setStatus(Loan::EXPIRED)->setExpiredDate(new \DateTime());
         $loan->save($con);
         $loan->getBorrower()->setActiveLoan(null)->setLoanStatus(Loan::NO_LOAN);
         $loan->save($con);
         $this->changeLoanStage($con, $loan, Loan::OPEN, Loan::EXPIRED);
         $refunds = $this->refundLenders($con, $loan, Loan::EXPIRED);
         if ($loan->getStatus() == Loan::FUNDED) {
             BidQuery::create()->filterByLoan($loan)->update(['active' => 0, 'accepted_amount' => null], $con);
         }
     });
     // Todo: send emails to notify lenders use $refunds
     return true;
 }
Beispiel #5
0
 public function addRepayment(Loan $loan, \Datetime $date, Money $amount, BorrowerPayment $borrowerPayment = null)
 {
     // Divide the payment in the lenders and the web site fee
     // 1. Get the web site fee %
     // 2. Get who all lended and how much
     // 3. substract he website fee out of this installment
     // 4. remaining money should be divided in lenders according to their proportion and added
     // 5. If the loan gets completed with this payment set the loan status to complete
     $calculator = $this->getRepaymentCalculator($loan, $amount);
     if ($calculator->unpaidAmount()->isNegative()) {
         throw new \Exception('Unpaid amount is negative');
     }
     $con = Propel::getWriteConnection(TransactionTableMap::DATABASE_NAME);
     $con->beginTransaction();
     $refundThreshold = $this->currencyService->convertFromUSD(Money::create(1), $loan->getCurrency(), $date);
     $refundAmount = $calculator->refundAmount($refundThreshold);
     if ($refundAmount->isPositive()) {
         $this->addBorrowerRefund($con, $loan, $refundAmount);
         $amount = $amount->subtract($refundAmount);
         $calculator->setRepaymentAmount($amount);
     }
     $this->transactionService->addInstallmentTransaction($con, $amount, $loan, $date);
     $nativeFeeAmount = $calculator->installmentServiceFee();
     $feeAmount = $this->currencyService->convertToUSD($nativeFeeAmount, $date);
     $this->transactionService->addInstallmentFeeTransaction($con, $loan, $feeAmount, $date);
     $bids = BidQuery::create()->filterByLoan($loan)->filterByActive(true)->find();
     $loanRepayments = $calculator->loanRepayments($bids);
     /** @var $loanRepayment LoanRepayment */
     foreach ($loanRepayments as $loanRepayment) {
         $lender = $loanRepayment->getLender();
         $nativeLenderAmount = $loanRepayment->getAmount();
         $nativeLenderInviteCredit = $loanRepayment->getLenderInviteCredit();
         if ($nativeLenderAmount->isPositive()) {
             $lenderAmount = $this->currencyService->convertToUSD($nativeLenderAmount, $date);
             $this->transactionService->addRepaymentTransaction($con, $lenderAmount, $loan, $lender, $date);
         }
         if ($nativeLenderInviteCredit->isPositive()) {
             $lenderInviteCredit = $this->currencyService->convertToUSD($nativeLenderInviteCredit, $date);
             $this->transactionService->addLenderInviteCreditRepaymentTransaction($con, $lenderInviteCredit, $loan, $date);
         }
     }
     $updatedInstallments = $this->updateInstallmentSchedule($con, $loan, $amount, $date);
     // TODO
     // $database->setOntimeRepayCredit($rest4, $borrowerid, $amount);
     // TODO
     // $database->loanpaidback($borrowerid,$loanid);
     // TODO emails/sms
 }