Refactor invoice handling and add total net amount calculation

Introduce logic for validating booked and draft invoices in E-conomic. Add methods to calculate the total net amount for invoice collections and improve error handling for specific invoice states. Updated related workflows to ensure consistent validation and data integrity.
This commit is contained in:
Jepp9350
2025-03-07 16:22:14 +01:00
parent d160ecbb63
commit b3c4e9696f
3 changed files with 125 additions and 12 deletions
@@ -48,7 +48,7 @@ class orderInvoicesRoute
'closed_at' => $collected_order_invoice['closed_at'] ? (string)$collected_order_invoice['closed_at'] : null,
'updated_at' => (string)$collected_order_invoice['updated_at'],
'created_at' => (string)$collected_order_invoice['created_at'],
'orders' => ((new collected_order_invoices_o())->select((int)$collected_order_invoice['id']))->getOrders(true)
'orders' => ((new collected_order_invoices_o())->select((int)$collected_order_invoice['id']))->getOrders(true),
];
}
));
@@ -136,8 +136,23 @@ class orderInvoicesRoute
// Validate the ID against the database
$collected_order_invoices = (new collected_order_invoices_o())->select((int)self::getParameter('id'));
$collected_order_invoices->requireSelected();
// Add the collected order invoice to E-Conomic
$collected_order_invoices->addToEconomic();
// Check if the collected order invoice has an external ID
if ($collected_order_invoices->external_id->value() === null) {
// Add the collected order invoice to E-Conomic
$collected_order_invoices->addToEconomic();
$response->success($collected_order_invoices->asArray());
}
// Check if the invoice has been booked
if ($collected_order_invoices->booked_invoice_id->value() !== null) {
$response->error('Invoice has already been booked', 400);
}
// Check if the invoice draft exists in E-Conomic
if ($collected_order_invoices->isDraftExisting()) {
$response->error('Invoice draft already exists in E-Conomic', 400);
}
// Create the invoice in E-Conomic
$collected_order_invoices->addToEconomic(true);
// Return the collected order invoice
$response->success($collected_order_invoices->asArray());
} else {
(new logs_o())->add('orderInvoices', 'global', 0, 0, 'ADD_COLLECTED_INVOICE_ECONOMIC', 'User tried to add a collected order invoice to E-Conomic without a valid session');