/** * @param Account $account * @param Security $security * @return LotCollection */ public function findLotsByAccountAndSecurity(Account $account, Security $security) { $portfolio = $account->getClient()->getPortfolio(); $positions = $this->getPositionsByPortfolio($portfolio, $account, $security); $lotCollection = new LotCollection(); foreach ($positions as $position) { $lots = $this->getLastPositionLots($portfolio, $position['security_id'], $position['client_system_account_id']); foreach ($lots as $lot) { $lotCollection->add($lot); } if ($position['muni_id']) { $muniLots = $this->getLastPositionLots($portfolio, $position['muni_id'], $position['client_system_account_id'], true); foreach ($muniLots as $muniLot) { $lotCollection->add($muniLot); } } } return $lotCollection; }
public function testBuyRequiredCash() { $securityCollection1 = new SecurityCollection(); $securityCollection2 = new SecurityCollection(); $subclass1 = $this->getMockSubclass(array('id' => 1, 'current_allocation' => 5, 'target_allocation' => 20)); $security1 = $this->getMockSecurity(array('id' => 1, 'amount' => 1000, 'qty' => 100, 'name' => 'Security1', 'symbol' => 'Sec1')); $subclass2 = $this->getMockSubclass(array('id' => 2, 'current_allocation' => 25, 'target_allocation' => 45)); $security2 = $this->getMockSecurity(array('id' => 2, 'amount' => 5000, 'qty' => 10, 'name' => 'Security2', 'symbol' => 'Sec2')); $subclass3 = $this->getMockSubclass(array('id' => 3, 'current_allocation' => 70, 'target_allocation' => 35)); $security3 = $this->getMockSecurity(array('id' => 5, 'amount' => 14000, 'qty' => 20, 'name' => 'Security3', 'symbol' => 'Sec3')); $security1->setSubclass($subclass1); $subclass1->setSecurity($security1); $security2->setSubclass($subclass2); $subclass2->setSecurity($security2); $security3->setSubclass($subclass3); $subclass3->setSecurity($security3); $securityCollection1->add($security1); $securityCollection1->add($security2); $securityCollection2->add($security3); $lotsData = array(array('id' => 1, 'is_muni' => false, 'quantity' => 100, 'amount' => 1000, 'status' => Lot::LOT_IS_OPEN, 'security' => $security1, 'date' => '2012-01-01', 'initial' => array('id' => 1, 'date' => '2012-01-01')), array('id' => 2, 'is_muni' => false, 'quantity' => 10, 'amount' => 5000, 'status' => Lot::LOT_IS_OPEN, 'security' => $security2, 'date' => '2012-01-01', 'initial' => array('id' => 1, 'date' => '2012-01-01')), array('id' => 3, 'is_muni' => false, 'quantity' => 20, 'amount' => 14000, 'status' => Lot::LOT_IS_OPEN, 'security' => $security3, 'date' => '2012-01-01', 'initial' => array('id' => 1, 'date' => '2012-01-01'))); $lots = []; foreach ($lotsData as $key => $data) { /** @var Lot $lot */ $lot = $this->getMock('Model\\WealthbotRebalancer\\Lot', null); $lot->loadFromArray($data); $lots[$key + 1] = $lot; } $accountsData = array(array('id' => 1, 'is_ready_to_rebalance' => true, 'total_cash' => 100000, 'cash_buffer' => 2000, 'sas_cash' => 1000, 'billing_cash' => 500, 'scheduled_distribution' => 2300, 'one_time_distribution' => 0, 'client' => array('portfolio' => array('id' => 1), 'ria' => array('ria_company_information' => array('use_transaction_fees' => true, 'transaction_min_amount' => 400, 'transaction_min_amount_percent' => 1.5)))), array('id' => 2, 'is_ready_to_rebalance' => true, 'total_cash' => 150000, 'cash_buffer' => 1000, 'sas_cash' => 300, 'billing_cash' => 100, 'scheduled_distribution' => 0, 'one_time_distribution' => 2400, 'client' => array('portfolio' => array('id' => 1), 'ria' => array('ria_company_information' => array('use_transaction_fees' => true, 'transaction_min_amount' => 400, 'transaction_min_amount_percent' => 1.5))))); $accountCollection = new AccountCollection(); foreach ($accountsData as $key => $data) { /** @var Account $account */ $account = $this->getMock('Model\\WealthbotRebalancer\\Account', null); $account->loadFromArray($data); $securityCollectionVar = 'securityCollection' . ($key + 1); $account->setSecurities(${$securityCollectionVar}); $accountCollection->add($account); } $client = $this->getMockClient(array('portfolio' => array('id' => 1))); $client->setAccounts($accountCollection); $lotRepository = $this->getMockLotRepository(array('findOrderedLots')); $lotRepository->expects($this->any())->method('findOrderedLots')->will($this->returnCallback(function (Security $security) use($lots) { $lotCollection = new LotCollection(); $lotCollection->add($lots[$security->getSubclass()->getId()]); return $lotCollection; })); $securityTransactionRepository = $this->getMockSecurityTransactionRepository(array('findOneByPortfolioAndSecurity')); /** @var WealthbotRebalancer $rebalancer */ $rebalancer = $this->getMockWealthbotRebalancer(array('getRepository', 'getJob', 'getClient', 'checkShortTermRedemption', 'checkTransaction', 'getRebalancerAction'), false); $rebalancer->expects($this->any())->method('getRepository')->will($this->returnCallback(function ($repositoryName) use($lotRepository, $securityTransactionRepository) { if ($repositoryName === 'Lot') { return $lotRepository; } elseif ($repositoryName === 'SecurityTransaction') { return $securityTransactionRepository; } return null; })); $rebalancer->expects($this->any())->method('getJob')->will($this->returnValue($this->getMockJob(array('id' => 1)))); $rebalancer->expects($this->any())->method('getClient')->will($this->returnValue($client)); $rebalancer->expects($this->any())->method('checkShortTermRedemption')->will($this->returnValue(true)); $rebalancer->expects($this->any())->method('getRebalancerAction')->will($this->returnValue($this->getMockRebalancerAction(array('id' => 2)))); $rebalancer->buyRequiredCash(); $queue = $rebalancer->getRebalancerQueue(); $this->assertCount(3, $queue); $item1 = $queue[0]; $this->assertEquals('Sec1', $item1->getSecurity()->getSymbol()); $this->assertEquals(100, $item1->getQuantity()); $this->assertEquals(0, $item1->getSecurity()->getQty()); $item2 = $queue[1]; $this->assertEquals('Sec2', $item2->getSecurity()->getSymbol()); $this->assertEquals(8, $item2->getQuantity()); $this->assertEquals(2, $item2->getSecurity()->getQty()); $item3 = $queue[2]; $this->assertEquals('Sec3', $item3->getSecurity()->getSymbol()); $this->assertEquals(4, $item3->getQuantity()); $this->assertEquals(16, $item3->getSecurity()->getQty()); $this->assertEquals(6000, $accountCollection->get(1)->getTotalCash()); $this->assertEquals(4300, $accountCollection->get(2)->getTotalCash()); }
public function testLotOrderForMuni() { $positions = array(array('client_system_account_id' => 222, 'muni_id' => 78, 'security_id' => 22222), array('client_system_account_id' => 333, 'muni_id' => null, 'security_id' => 33333)); /** @var LotRepository $mockRepository */ $mockRepository = $this->getMockBuilder('Model\\WealthbotRebalancer\\Repository\\LotRepository')->disableOriginalConstructor()->setMethods(array('getPositionsByPortfolio', 'getLastPositionLots'))->getMock(); $mockRepository->expects($this->any())->method('getPositionsByPortfolio')->will($this->returnValue($positions)); $mockRepository->expects($this->any())->method('getLastPositionLots')->will($this->returnCallback(function (Portfolio $portfolio, $securityId, $clientSystemAccountId, $isMuni = false) { $lotCollection = new LotCollection(); $lot = new Lot(); $lot->setIsMuni($isMuni); $lotCollection->add($lot); return $lotCollection; })); $mockRepository->expects($this->any())->method('findLotsByAccountAndSecurity')->will($this->returnCallback(function (Portfolio $portfolio, $securityId, $clientSystemAccountId, $isMuni = false) { $lotCollection = new LotCollection(); $lot = new Lot(); $lot->setIsMuni($isMuni); $lotCollection->add($lot); return $lotCollection; })); $lotCollection = $mockRepository->findLotsBySubclass(new Portfolio(), new Subclass(), new Account()); $lot1 = $lotCollection->first(); $this->assertFalse($lot1->getIsMuni()); $lot2 = $lotCollection->next(); $this->assertTrue($lot2->getIsMuni()); $lot3 = $lotCollection->next(); $this->assertFalse($lot3->getIsMuni()); //-------------------------------------------------------------------------------------------------------------/ $account = new Account(); $client = new Client(); $client->setPortfolio(new Portfolio()); $account->setClient($client); $lotCollection = $mockRepository->findLotsByAccountAndSecurity($account, new Security()); $lot1 = $lotCollection->first(); $this->assertFalse($lot1->getIsMuni()); $lot2 = $lotCollection->next(); $this->assertTrue($lot2->getIsMuni()); $lot3 = $lotCollection->next(); $this->assertFalse($lot3->getIsMuni()); }