202 lines
11 KiB
PHP
202 lines
11 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use classes\response;
|
|
use classes\router;
|
|
use economic_invoice_draft_mo;
|
|
use objects\departments_o;
|
|
use objects\economic_module_orders;
|
|
use objects\logs_o;
|
|
use objects\orders_o;
|
|
use objects\tokens_o;
|
|
use traits\route_t;
|
|
|
|
class economicInvoiceRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
global /** @var response $response */
|
|
/** @var router $router */
|
|
$router, $response;
|
|
|
|
$this->post('/economic/invoice/draft/export', function () {
|
|
global $response;
|
|
$this->requirePermission('economic_invoice_draft_export');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
$order_id = $response->getRequestParameter('order_id');
|
|
if (!isset($order_id)) {
|
|
$response->error('Order ID is required', 400);
|
|
}
|
|
$order = (new orders_o())->getOrderById($order_id);
|
|
// Check if the order exists
|
|
if (!$order->exists()) {
|
|
$response->error('Order not found', 404);
|
|
}
|
|
// Get the order items
|
|
$order_items = (new orders_o())->getOrderItems($order_id);
|
|
// Get the customer
|
|
$customer = (new orders_o())->getCustomerByOrderId($order_id);
|
|
// Check if the customer exists
|
|
if (!$customer->exists()) {
|
|
$response->error('Customer not found', 404);
|
|
}
|
|
// Get the customer economic number
|
|
$customer_economic = $customer->getCustomerEcocomicData()->economic_customer;
|
|
$economic_invoice_draft = (new economic_invoice_draft_mo());
|
|
// Set the customer number
|
|
$economic_invoice_draft->setCustomerNumber((int)$customer_economic->customer_number);
|
|
// Set the recipient
|
|
$economic_invoice_draft->setRecipient(
|
|
$customer_economic->name ?? 'Ukendt',
|
|
$customer_economic->address ?? 'Ukendt',
|
|
$customer_economic->zip ?? 'Ukendt',
|
|
$customer_economic->city ?? 'Ukendt'
|
|
);
|
|
// Make sure there are order items
|
|
if (count($order_items) === 0) {
|
|
$response->error('No order items found', 404);
|
|
}
|
|
// Get the department
|
|
$department = (new departments_o())->getDepartmentById($order->department_id->value());
|
|
// Add the department, date, reference
|
|
$economic_invoice_draft->addLineTEXT('Afdeling: ' . $department['name']);
|
|
$economic_invoice_draft->addLineTEXT('Dato: ' . $order->created_at->value());
|
|
$economic_invoice_draft->addLineTEXT('Reference: ' . $order->reference->value());
|
|
// Add the lines to the invoice
|
|
foreach ($order_items as $order_item) {
|
|
// If the customer requires any prefix or suffix to the product name, add it here
|
|
if ($customer->doesUserHaveAttribute('requiresRegistrationNumbersInvoice')) {
|
|
$order_item['product']['name'] = $order->reg_1->value() . ' ' . $order_item['product']['name'];
|
|
}
|
|
$economic_invoice_draft->addLine((string)$order_item['product']['economic_product_id'], (string)$order_item['product']['name'], 1, (int)$order_item['price'], 0);
|
|
}
|
|
// Create the invoice draft
|
|
$result = $economic_invoice_draft->createInvoiceDraftExample();
|
|
// Check if the invoice draft was created
|
|
if (!isset($result->draftInvoiceNumber)) {
|
|
// Log the error
|
|
(new logs_o())->add('economic_invoice_draft', 'global', 3, $user->id, 'ECONOMIC_INVOICE_DRAFT_EXPORT', 'Failed to create economic invoice draft');
|
|
// Check if we can get the errors from the response
|
|
if (isset($result->errors)) {
|
|
$response->add_meta('economic_errors', $result->errors);
|
|
}
|
|
// Try to parse the error message
|
|
$response->error($result->message ?? 'Failed to create economic invoice draft', 500);
|
|
}
|
|
// Add the economic invoice draft to the order
|
|
$economic_module_orders = (new economic_module_orders())->getByOrderId($order_id);
|
|
// Remove the existing invoice draft (if any)
|
|
if ($economic_module_orders->economic_invoice_draft_id->value() > 0) {
|
|
$economic_invoice_draft->deleteInvoiceDraft($economic_module_orders->economic_invoice_draft_id->value());
|
|
}
|
|
$economic_module_orders->economic_invoice_draft_id->set($result->draftInvoiceNumber);
|
|
// Return the response
|
|
(new logs_o())->add('economic_invoice_draft', 'global', 1, $user->id, 'ECONOMIC_INVOICE_DRAFT_EXPORT', 'Successfully exported an economic invoice draft');
|
|
$response->success($economic_module_orders->getArray());
|
|
} else {
|
|
(new logs_o())->add('economic_invoice_draft', 'global', 1, 0, 'ECONOMIC_INVOICE_DRAFT_EXPORT', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->delete('/economic/invoice/draft/delete', function () {
|
|
|
|
global /** @var response $response */
|
|
$response;
|
|
$this->requirePermission('economic_invoice_draft_delete');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
$order_id = $response->getRequestParameter('order_id');
|
|
if (!isset($order_id)) {
|
|
$response->error('Order ID is required', 400);
|
|
}
|
|
$order = (new orders_o())->getOrderById($order_id);
|
|
// Check if the order exists
|
|
if (!$order->exists()) {
|
|
$response->error('Order not found', 404);
|
|
}
|
|
// Make sure the order has an economic invoice draft
|
|
$economic_module_orders = (new economic_module_orders())->getByOrderId($order_id);
|
|
if ($economic_module_orders->economic_invoice_draft_id->value() === 0) {
|
|
$response->error('No economic invoice draft found', 404);
|
|
}
|
|
$economic_invoice_draft = (new economic_invoice_draft_mo());
|
|
// Get the invoice draft number
|
|
$invoiceDraftId = $economic_module_orders->economic_invoice_draft_id->value();
|
|
// Delete the invoice draft
|
|
$economic_invoice_draft->deleteInvoiceDraft($invoiceDraftId);
|
|
// Remove the economic invoice draft from the order
|
|
$economic_module_orders->economic_invoice_draft_id->set(null);
|
|
// Return the response
|
|
(new logs_o())->add('economic_invoice_draft', 'global', 1, $user->id, 'ECONOMIC_INVOICE_DRAFT_DELETE', 'Successfully deleted an economic invoice draft');
|
|
$response->success($economic_module_orders->getArray());
|
|
} else {
|
|
(new logs_o())->add('economic_invoice_draft', 'global', 1, 0, 'ECONOMIC_INVOICE_DRAFT_DELETE', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->post('/economic/invoice/export', function () {
|
|
global $response;
|
|
$this->requirePermission('economic_invoice_export');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
$order_id = $response->getRequestParameter('order_id');
|
|
if (!isset($order_id)) {
|
|
$response->error('Order ID is required', 400);
|
|
}
|
|
$order = (new orders_o())->getOrderById($order_id);
|
|
// Check if the order exists
|
|
if (!$order->exists()) {
|
|
$response->error('Order not found', 404);
|
|
}
|
|
// Get the customer
|
|
$customer = (new orders_o())->getCustomerByOrderId($order_id);
|
|
// Check if the customer exists
|
|
if (!$customer->exists()) {
|
|
$response->error('Customer not found', 404);
|
|
}
|
|
// Make sure the order has an economic invoice draft
|
|
$economic_module_orders = (new economic_module_orders())->getByOrderId($order_id);
|
|
if ($economic_module_orders->economic_invoice_draft_id->value() === 0) {
|
|
$response->error('No economic invoice draft found', 404);
|
|
}
|
|
$economic_invoice_draft = (new economic_invoice_draft_mo());
|
|
// Get the invoice draft number
|
|
$invoiceDraftId = $economic_module_orders->economic_invoice_draft_id->value();
|
|
// Make sure there's not already an invoice created
|
|
$invoiceId = $economic_module_orders->economic_invoice_id->value();
|
|
if ($invoiceId > 0) {
|
|
$response->error('An invoice has already been created, invoice ID: ' . $invoiceId, 400);
|
|
}
|
|
// Publish the invoice draft
|
|
$result = $economic_invoice_draft->publishInvoiceDraft((int)$invoiceDraftId);
|
|
// Check if the invoice was created
|
|
if (!isset($result->bookedInvoiceNumber)) {
|
|
// Log the error
|
|
(new logs_o())->add('economic_invoice', 'global', 3, $user->id, 'ECONOMIC_INVOICE_EXPORT', 'Failed to create economic invoice from draft: ' . $invoiceDraftId);
|
|
// Check if we can get the errors from the response
|
|
if (isset($result->errors)) {
|
|
$response->add_meta('economic_errors', $result->errors);
|
|
}
|
|
// Try to parse the error message
|
|
$response->error($result->message ?? 'Failed to create economic invoice', 500);
|
|
}
|
|
// Add the economic invoice to the order
|
|
$economic_module_orders = (new economic_module_orders())->getByOrderId($order_id);
|
|
$economic_module_orders->economic_invoice_id->set($result->bookedInvoiceNumber);
|
|
// Return the response
|
|
(new logs_o())->add('economic_invoice', 'global', 1, $user->id, 'ECONOMIC_INVOICE_EXPORT', 'Successfully exported an economic invoice');
|
|
$response->success($economic_module_orders->getArray());
|
|
} else {
|
|
(new logs_o())->add('economic_invoice', 'global', 1, 0, 'ECONOMIC_INVOICE_EXPORT', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
}
|
|
} |