/** * @param array $data * @return bool * @throws \Exception * @throws \RuntimeException */ public function makeTransaction($data) { /** * @var BackofficeAuthenticationService $auth * @var MoneyAccount $moneyAccountDao * @var \DDD\Domain\Finance\Expense\Expenses $po * @var \DDD\Domain\MoneyAccount\MoneyAccount $moneyAccount * @var TransactionAccount $transactionAccountService */ $transactionAccountService = $this->getServiceLocator()->get('service_finance_transaction_account'); $moneyAccountDao = $this->getServiceLocator()->get('dao_money_account_money_account'); $expenseTransactionDao = new ExpenseTransactions($this->getServiceLocator()); $auth = $this->getServiceLocator()->get('library_backoffice_auth'); $transactionDao = new Transactions($this->getServiceLocator()); $expenseDao = new Expenses($this->getServiceLocator()); $expenseItemDao = new ExpenseItem($this->getServiceLocator()); $currencyService = new CurrencyVault(); $currencyService->setServiceLocator($this->getServiceLocator()); if (empty($data['poId']) || empty($data['itemId']) || empty($data['accountId']) || empty($data['moneyAccount']) || empty($data['transactionDate']) || empty($data['amount'])) { throw new \RuntimeException('Invalid data provided.'); } $isRefund = 0; if (isset($data['isRefund']) && $data['isRefund']) { $isRefund = 1; } $amount = $isRefund ? $data['amount'] : -1 * $data['amount']; $moneyAccount = $moneyAccountDao->fetchOne(['id' => $data['moneyAccount']], ['currency_id', 'balance']); $po = $expenseDao->fetchOne(['id' => $data['poId']], ['ticket_balance', 'transaction_balance', 'currency_id']); $accountId = $transactionAccountService->getTransactionAccountIdByIdentity($data['moneyAccount'], Account::TYPE_MONEY_ACCOUNT); if (!$moneyAccount || !$po || !$accountId) { throw new \RuntimeException('Problem with PO and Money Account.'); } $transactionDao->save(['account_id' => $accountId, 'currency_id' => $moneyAccount->getCurrencyId(), 'date' => $data['transactionDate'], 'description' => '', 'amount' => $amount]); $expenseTransactionDao->save(['money_transaction_id' => $transactionDao->lastInsertValue, 'expense_id' => $data['poId'], 'creator_id' => $auth->getIdentity()->id, 'money_account_id' => $data['moneyAccount'], 'account_to_id' => $data['accountId'], 'transaction_date' => $data['transactionDate'], 'creation_date' => date('Y-m-d H:i:s'), 'amount' => $amount, 'is_refund' => $isRefund]); $expenseItemDao->save(['transaction_id' => $expenseTransactionDao->lastInsertValue], ['id' => $data['itemId']]); $moneyAccountDao->save(['balance' => $moneyAccount->getBalance() + $amount], ['id' => $data['moneyAccount']]); if ($po->getCurrencyId() != $moneyAccount->getCurrencyId()) { $amount = $currencyService->convertCurrency($amount, (int) $moneyAccount->getCurrencyId(), (int) $po->getCurrencyId()); } $expenseDao->save(['ticket_balance' => $po->getTicketBalance() + $amount, 'transaction_balance' => $po->getTransactionBalance() + $amount], ['id' => $data['poId']]); }