public function testDelete()
 {
     $securityRepository = new SecurityRepository();
     $security = $securityRepository->findOneBySymbol('VTI');
     $accountRepository = new AccountRepository();
     $account = $accountRepository->findOneByAccountNumber('916985328');
     $subclassRepo = new SubclassRepository();
     $subclass = $subclassRepo->findFirst();
     $riaRepo = new RiaRepository();
     $ria = $riaRepo->findOneBy(array('email' => '*****@*****.**'));
     $jobRepo = new JobRepository();
     $job = $jobRepo->findOneBy(array('user_id' => $ria->getId()));
     $rebalancerActionRepo = new RebalancerActionRepository();
     $rebalancerActions = $rebalancerActionRepo->bindForJob($job);
     $rebalancerAction = $rebalancerActions->first();
     $rebalancerQueue = new QueueItem();
     $rebalancerQueue->setSecurity($security);
     $rebalancerQueue->setAccount($account);
     $rebalancerQueue->setAmount(100.1);
     $rebalancerQueue->setRebalancerActionId($rebalancerAction->getId());
     $rebalancerQueue->setQuantity(10);
     $rebalancerQueue->setStatus(QueueItem::STATUS_BUY);
     $rebalancerQueue->setSubclass($subclass);
     $this->repository->save($rebalancerQueue);
     $last = $this->repository->findById($this->repository->getLastInsertId());
     $this->repository->delete($last);
     $deletedLast = $this->repository->findById($last->getId());
     $this->assertNull($deletedLast);
 }
Ejemplo n.º 2
0
 public function testSetAmount()
 {
     $this->queueItem->setAmount(21.1);
     $this->assertEquals(21.1, $this->queueItem->getAmount());
 }
Ejemplo n.º 3
0
 /**
  * Buy security
  *
  * @param Security $security
  * @param Account $account
  * @param $amount
  * @param bool $dryRun
  * @return float|int
  * @throws \Exception
  */
 public function buySecurity(Security $security, Account $account, $amount, $dryRun = false)
 {
     $price = $security->getPrice();
     $buyQuantity = $price > 0 ? floor($amount / $price) : 0;
     $buyAmount = $buyQuantity * $price;
     if (!$security->isCanBePurchased($buyQuantity, $buyAmount)) {
         $exception = new \Exception(sprintf('Buying security error: cannot buy security with id: %s, qty: %s, amount: %s.', $security->getId(), $buyQuantity, $buyAmount));
         $this->logger->logError($exception);
         throw $exception;
     }
     if (!$this->checkTransaction($account, $amount, $security, 'buy')) {
         $exception = new \Exception("Cannot buy: {$security->getId()} . Transaction check fails. (Amount:{$amount})");
         $this->logger->logError($exception);
         throw $exception;
     }
     if (!$dryRun) {
         $this->logger->logInfo("Buy security {$security->getId()} qty: {$buyQuantity}; amount: {$buyAmount}");
         $security->buy($buyQuantity, $buyAmount);
         $queueItem = new QueueItem();
         $queueItem->setRebalancerActionId($this->getRebalancerAction()->getId());
         $queueItem->setSecurity($security);
         $queueItem->setAccount($account);
         $queueItem->setQuantity($buyQuantity);
         $queueItem->setAmount($buyAmount);
         $queueItem->setStatus(QueueItem::STATUS_BUY);
         $this->getRebalancerQueue()->addItem($queueItem);
     }
     return $buyAmount;
 }