/** * @param int $ticketId * @return bool * @throws ExpenseCustomException * @throws \Exception */ public function revokeTicket($ticketId) { /** * @var BackofficeAuthenticationService $auth * @var Logger $logger * @var \DDD\Dao\Finance\Budget\Budget $budgetDao */ $expenseDao = new Expenses($this->getServiceLocator(), '\\ArrayObject'); $budgetDao = $this->getServiceLocator()->get('dao_finance_budget_budget'); $auth = $this->getServiceLocator()->get('library_backoffice_auth'); $logger = $this->getServiceLocator()->get('ActionLogger'); $currencyExchange = new \Library\Utility\Currency($this->getServiceLocator()->get('dao_currency_currency')); $expenseExist = $expenseDao->fetchOne(['id' => $ticketId, 'status' => Ticket::STATUS_GRANTED], ['id', 'budget_id', 'currency_id', 'manager_id', 'limit']); if (!$expenseExist) { throw new ExpenseCustomException('Impossible to handle ticket'); } else { if (!$auth->hasRole(Roles::ROLE_PO_APPROVER)) { throw new ExpenseCustomException('You have no premissions to revoke this purchase order.'); } try { $expenseDao->beginTransaction(); if ($expenseExist['budget_id']) { $budgetBalanceIncrease = $currencyExchange->convert($expenseExist['limit'], intval($expenseExist['currency_id']), CurrencyService::DEFAULT_CURRENCY); $budgetDao->save(['balance' => new Expression('balance + ' . $budgetBalanceIncrease)], ['id' => $expenseExist['budget_id']]); } $expenseDao->save(['status' => Ticket::STATUS_PENDING, 'finance_status' => Ticket::FIN_STATUS_NEW], ['id' => $ticketId]); $logger->save(Logger::MODULE_EXPENSE, $ticketId, Logger::ACTION_APPROVED, Logger::NEGATIVE); $expenseDao->commitTransaction(); } catch (\Exception $ex) { $expenseDao->rollbackTransaction(); throw new \Exception($ex); } } return true; }