예제 #1
0
 /**
  * @param int $ticketId
  * @return bool
  * @throws ExpenseCustomException
  * @throws \Exception
  */
 public function settleTicket($ticketId)
 {
     /**
      * @var BackofficeAuthenticationService $auth
      * @var Logger $logger
      */
     $expenseDao = new Expenses($this->getServiceLocator(), '\\ArrayObject');
     $auth = $this->getServiceLocator()->get('library_backoffice_auth');
     $logger = $this->getServiceLocator()->get('ActionLogger');
     $expenseExist = $expenseDao->checkForSettle($ticketId);
     if (!$expenseExist) {
         throw new ExpenseCustomException('Impossible to settle ticket');
     } else {
         if (!$auth->hasRole(Roles::ROLE_PO_AND_TRANSFER_MANAGER_GLOBAL)) {
             throw new ExpenseCustomException('You have no premissions to settle this purchase order.');
         }
         try {
             $expenseDao->beginTransaction();
             $expenseDao->save(['finance_status' => Ticket::FIN_STATUS_SETTLED], ['id' => $ticketId]);
             $logger->save(Logger::MODULE_EXPENSE, $ticketId, Logger::ACTION_SETTLED);
             // increase budget if limit different item balance great zero
             if ($expenseExist['remaining'] > 0) {
                 $currencyExchange = new \Library\Utility\Currency($this->getServiceLocator()->get('dao_currency_currency'));
                 $budgetBalanceIncrease = $expenseExist['remaining'];
                 if ($expenseExist['currency_id'] != CurrencyService::DEFAULT_CURRENCY) {
                     $budgetBalanceIncrease = $currencyExchange->convert($budgetBalanceIncrease, intval($expenseExist['currency_id']), CurrencyService::DEFAULT_CURRENCY);
                 }
                 /** @var @var \DDD\Dao\Finance\Budget\Budget $budgetDao $budgetDao */
                 $budgetDao = $this->getServiceLocator()->get('dao_finance_budget_budget');
                 $budgetDao->save(['balance' => new Expression('balance + ' . $budgetBalanceIncrease)], ['id' => $expenseExist['budget_id']]);
             }
             $expenseDao->commitTransaction();
         } catch (\Exception $ex) {
             $expenseDao->rollbackTransaction();
             throw new \Exception($ex);
         }
     }
     return true;
 }