/** * @param ArrayCollection $initialLots * @param LotModel $model * @return int|null */ protected function compareRtQuantity(ArrayCollection $initialLots, LotModel $model) { foreach ($initialLots as $initialLot) { if ($initialLot->getQuantity() > $model->getQuantity()) { // Close lot $model->setStatus(WealthbotLot::LOT_CLOSED); $model->setWasClosed(true); $model->setInitialLotId($initialLot->getId()); $model->setRealizedGain($model->getAmount() - $model->getQuantity() * $initialLot->calcPrice()); //$this->lotRepo->insert($model); $this->lotRepo->save($model); // Create new lot $newModel = new LotModel(); $newModel->setDate($model->getDate()); $newModel->setStatus(WealthbotLot::LOT_INITIAL); $newModel->setQuantity($initialLot->getQuantity() - $model->getQuantity()); $newModel->setAmount($newModel->getQuantity() * $initialLot->calcPrice()); $newModel->setWasClosed(false); $newModel->setCostBasis($model->getCostBasis()); $newModel->setSecurityId($model->getSecurityId()); $newModel->setRealizedGain(null); $newModel->setInitialLotId($initialLot->getId()); $newModel->setClientSystemAccountId($model->getClientSystemAccountId()); $newModel->setWasRebalancerDiff(false); // Divide initial lot $this->dividedLot($initialLot->getId()); // Save lot return $this->lotRepo->save($newModel); } } return null; }
/** * @param SystemClientAccountModel $account * @param SecurityModel $security * @param array $data * @return mixed */ public function process(SystemClientAccountModel $account, SecurityModel $security, array $data) { $model = new LotModel(); $model->setSymbol($data['symbol']); $model->setTransactionCode($data['transaction_code']); $model->setInitialLotId(null); $model->setQuantity($data['qty']); $model->setStatus(WealthbotLot::LOT_INITIAL); $model->setDate($data['tx_date']); $model->setWasClosed(false); $model->setAmount($data['net_amount']); $model->setCostBasis($data['gross_amount']); $model->setRealizedGain(null); $model->setSecurityId($security->getId()); $model->setClientSystemAccountId($account->getId()); $model->setWasRebalancerDiff(false); $class = Factory::make($model->isBuy() ? 'Buy' : 'Sell'); // Create new lot return $class->create($model); }