/** * @param Ticket $expenseTicket * @return bool|void * @throws \Exception */ public function save(Ticket $expenseTicket) { /** * @var Transactor\Expense $transaction */ if (!$this->finance instanceof Finance) { throw new \Exception('Finance not defined for expense transaction.'); } $transactionData = $this->getData(); $transaction = new Transactor\Expense(GeneralTransaction::ACCOUNT_MONEY_ACCOUNT, GeneralTransaction::getAccountTypeById($transactionData['accountTo']['type'])); $transaction->setServiceLocator($expenseTicket->getServiceLocator()); $transaction->setMode($this->getMode()); switch ($this->getMode()) { case self::MODE_ADD: $transaction->setAccountIdentity($transactionData['accountFrom']['id'], $transactionData['accountTo']['id']); $transaction->setIsRefund(isset($transactionData['isRefund']) ? $transactionData['isRefund'] : 0); $transaction->setTransactionAccountId($transactionData['accountTo']['transactionAccountId']); $transaction->setTransactionDate(isset($transactionData['date']) ? $transactionData['date'] : date('Y-m-d H:i:s')); $transaction->setDescription("Expense Transaction #{$expenseTicket->getExpenseId()}"); $transaction->setExpenseId($expenseTicket->getExpenseId()); $transaction->setAmount($transactionData['amount']); // If transaction is virtual it shouldn't have money_transaction_id if ($this->getMoneyTransactionId() !== false) { $transaction->setIsVirtual(); $transaction->setMoneyTransactionId($this->getMoneyTransactionId()); } $transaction->prepare(); break; case self::MODE_EDIT: $isVerified = $transactionData['isVerified']; $isVoided = $transactionData['isVoided']; if (!$isVerified && !$isVoided) { throw new \RuntimeException('Impossible to modify transaction.'); } $transaction->setTransactionId($this->getId()); if ($isVerified) { $transaction->setIsVerified(); $transaction->setVerifierId($expenseTicket->getCreatorId()); } if ($isVoided) { $transaction->setIsVoid(); $transaction->setVerifierId($expenseTicket->getCreatorId()); } $transaction->prepare(); break; case self::MODE_DELETE: throw new \RuntimeException('Impossible to delete transaction.'); break; } return $transaction->process(); }
/** * @param Ticket $expenseTicket * @throws NotFoundException */ public function save(Ticket $expenseTicket) { if (!$this->getServiceLocator() instanceof ServiceLocatorInterface) { throw new NotFoundException('Service locator not defined for expense item.'); } $itemDao = $this->getItemDao(); $costDao = $this->getCostDao(); // Normalize values $this->prepare(); $data = $this->getData(); $itemAmount = null; $costCenters = []; $itemData = []; if ($this->getMode() == self::MODE_ADD) { $costCenters = $data['costCenters']; $itemAmount = $data['amount']; $itemData = ['transaction_id' => $data['transactionId'], 'account_id' => $data['accountId'], 'account_reference' => $data['accountReference'], 'sub_category_id' => $data['subCategoryId'], 'currency_id' => $data['currencyId'], 'amount' => Helper::formatAmount($itemAmount), 'is_startup' => $data['isStartup'], 'is_deposit' => $data['isDeposit'], 'is_refund' => $data['isRefund'], 'comment' => $data['accountComment'], 'type' => $data['type'], 'status' => $data['status']]; if ($data['period']['from']) { $itemData['period_from'] = $data['period']['from']; $itemData['period_to'] = $data['period']['to']; } } switch ($this->getMode()) { case self::MODE_ADD: $itemData['expense_id'] = $expenseTicket->getExpenseId(); $itemData['creator_id'] = $expenseTicket->getCreatorId(); $itemData['date_created'] = date('Y-m-d H:i:s'); if (!$data['period']['from']) { $itemData['period_from'] = $itemData['period_to'] = date('Y-m-d'); } $expenseItemId = $itemDao->save($itemData); $this->setId($expenseItemId); if (count($costCenters)) { $costCenterAmount = $itemAmount / count($costCenters); foreach ($costCenters as $costCenter) { $costDao->save(['expense_item_id' => $expenseItemId, 'cost_center_id' => $costCenter['id'], 'cost_center_type' => $this->translateCCT($costCenter['type']), 'amount' => $this->calculateCostAmount($costCenterAmount, $data['currencyId'], $costCenter['currencyId'], $data['isRefund'])]); } } break; case self::MODE_DELETE: $costDao->delete(['expense_item_id' => $this->getId()]); $itemDao->delete(['id' => $this->getId()]); break; } }