Ejemplo n.º 1
0
 public function disburseLoan(Loan $loan, \DateTime $disbursedDate, Money $nativeAmount)
 {
     $isDisbursed = TransactionQuery::create()->filterByLoan($loan)->filterDisbursement()->count();
     if ($isDisbursed) {
         // TODO
         return;
     }
     PropelDB::transaction(function ($con) use($loan, $disbursedDate, $nativeAmount) {
         $this->transactionService->addDisbursementTransaction($con, $nativeAmount, $loan);
         $loans = LoanQuery::create()->filterByBorrower($loan->getBorrower())->count();
         if ($loans == 1) {
             $this->transactionService->addFeeTransaction($con, $nativeAmount, $loan);
         }
         //TODO service fee rate
         $loan->setStatus(Loan::ACTIVE)->setNativeDisbursedAmount($nativeAmount)->setDisbursedDate($disbursedDate)->calculateExtraDays($disbursedDate)->setServiceFeeRate(2.5);
         $loan->save($con);
         $this->changeLoanStage($con, $loan, Loan::FUNDED, Loan::ACTIVE);
         $installments = $this->generateLoanInstallments($loan);
         foreach ($installments as $installment) {
             $installment->save($con);
         }
     });
     //TODO Add repayment schedule
     //TODO Send email / sift sience event
 }
Ejemplo n.º 2
0
 public function uploadFunds(Payment $payment)
 {
     $con = Propel::getWriteConnection(TransactionTableMap::DATABASE_NAME);
     $con->beginTransaction();
     try {
         if ($payment->getTotalAmount()->isPositive()) {
             $this->transactionService->addUploadFundTransaction($con, $payment);
         }
         if ($payment->getDonationAmount()->isPositive()) {
             $this->transactionService->addDonation($con, $payment);
         }
     } catch (\Exception $e) {
         $con->rollback();
         throw $e;
     }
     $con->commit();
 }
Ejemplo n.º 3
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
 }