/** * @param int $ticketId * @return bool */ public function deleteTicket($ticketId) { $expenseDao = new Expenses($this->getServiceLocator(), '\\ArrayObject'); $expenseItemDao = new ExpenseItem($this->getServiceLocator(), '\\ArrayObject'); $costCenterDao = new ExpenseCost($this->getServiceLocator(), '\\ArrayObject'); $expenseAttachmentDao = new ExpenseAttachments($this->getServiceLocator(), '\\ArrayObject'); $expenseItemAttachmentDao = $this->getServiceLocator()->get('dao_finance_expense_expense_item_attachments'); try { $expenseDao->beginTransaction(); $expense = $expenseDao->fetchOne(['id' => $ticketId]); if (!$expense) { throw new NotFoundException('Purchase order not found.'); } $expenseItems = $expenseItemDao->fetchAll(['expense_id' => $ticketId], ['id']); $expenseAttachments = $expenseAttachmentDao->fetchAll(['expense_id' => $ticketId], ['id']); $expenseItemAttachments = $expenseItemAttachmentDao->fetchAll(['expense_id' => $ticketId], ['item_id']); // Delete items if ($expenseItems->count()) { $itemIdList = []; foreach ($expenseItems as $expenseItem) { array_push($itemIdList, $expenseItem['id']); } $costCenterDao->deleteItemCosts($itemIdList); } $expenseItemDao->delete(['expense_id' => $ticketId]); $purchaseOrderTransactions = $this->getServiceLocator()->get('service_finance_transaction_po_transaction'); $purchaseOrderTransactions->removePurchaseOrderAllTransactions($ticketId, false); // Delete Ticket attachments if ($expenseAttachments->count()) { $attachmentIdList = []; foreach ($expenseAttachments as $expenseAttachment) { array_push($attachmentIdList, $expenseAttachment['id']); } $errorMessages = $this->removeFiles($attachmentIdList); if (count($errorMessages)) { /** * @todo: There are an error messages */ } } // Delete Item attachments if ($expenseItemAttachments->count()) { $attachmentItemIdList = []; foreach ($expenseItemAttachments as $expenseItemAttachment) { array_push($attachmentItemIdList, ['item_id' => $expenseItemAttachment->getItemId(), 'expense_id' => $ticketId]); } $this->removeItemFiles($attachmentItemIdList); } // Delete ticket $expenseDao->delete(['id' => $ticketId]); $expenseDao->commitTransaction(); return true; } catch (\Exception $ex) { $expenseDao->rollbackTransaction(); } return false; }