Enhance invoice line formatting and content handling.

Standardize line formatting by prefixing notes and references with '#'. Refactor handling of registration numbers and discounts for improved clarity. Ensure empty lines are added for better visual separation in invoices.
This commit is contained in:
Jepp9350
2025-02-05 13:56:47 +01:00
parent d022982b77
commit 6404ba65b2
@@ -252,22 +252,42 @@ class economicInvoiceRoute
// This is a note for the invoice
//
// (?) = Optional
$line = 'Truck Wash - ' . $department_name . ', ' . $order->created_at->value();
$economic_invoice_draft->addLineTEXT("[ " . $order->created_at->value() . ' ' . $department_name . ' #' . $order->id . " ]");
// If there's a reference, add it to the invoice
if ($order->reference->value() !== '')
$line .= ', Ref: ' . $order->reference->value();
if ($order->reference->value() !== '') {
$economic_invoice_draft->addLineTEXT('Reference:');
// Add "# " in the beginning of each line (If there's more than one line, otherwise we just add the prefix once)
if (str_contains($order->reference->value(), "\n")) {
foreach ( explode("\n", $order->reference->value()) as $line ) {
$economic_invoice_draft->addLineTEXT('# ' . $line);
}
} else {
$economic_invoice_draft->addLineTEXT('# ' . $order->reference->value());
}
}
// Add the registration numbers (if any)
if ($order->reg_1->value() !== '')
$line .= ', Reg 1: ' . strtoupper($order->reg_1->value());
$line_reg = '';
if ($order->reg_1->value() !== '') {
$line_reg .= 'Reg 1: ' . strtoupper($order->reg_1->value());
}
if ($order->reg_2->value() !== '')
$line .= ', Reg 2: ' . strtoupper($order->reg_2->value());
$line_reg .= ', Reg 2: ' . strtoupper($order->reg_2->value());
if ($order->reg_3->value() !== '')
$line .= ', Reg 3: ' . strtoupper($order->reg_3->value());
$line_reg .= ', Reg 3: ' . strtoupper($order->reg_3->value());
// Add the line to the invoice
$economic_invoice_draft->addLineTEXT($line);
$economic_invoice_draft->addLineTEXT($line_reg);
// If there's a note, add it to the invoice
if ($order->notes->value() !== '')
$economic_invoice_draft->addLineTEXT((string)$order->notes->value());
if ($order->notes->value() !== '') {
$economic_invoice_draft->addLineTEXT('Notat:');
// Add "# " in the beginning of each line (If there's more than one line, otherwise we just add the prefix once)
if (str_contains($order->notes->value(), "\n")) {
foreach ( explode("\n", $order->notes->value()) as $line ) {
$economic_invoice_draft->addLineTEXT('# ' . $line);
}
} else {
$economic_invoice_draft->addLineTEXT('# ' . $order->notes->value());
}
}
}
/**
@@ -287,50 +307,80 @@ class economicInvoiceRoute
// (!?) = Should be added if the customer requires it
// Example:
// Tankcleaning 4 spulehoveder, Ref: 123456, Reg 1: ABC123, Reg 2: DEF456, Reg 3: GHI789, Note: This is a note, Discount: -100 DKK (20%)
$line = $order_item['product']['name'];
// If there's a reference, add it to the line
if ($order_item['reference'] !== '')
$line .= ', Ref: ' . $order_item['reference'];
// Add the registration numbers (if they exist, and the customer requires it)
if ($customer->doesUserHaveAttribute('requiresRegistrationNumbersInvoice')) {
$line .= ', Reg 1: ' . strtoupper($order->reg_1->value());
$line .= ', Reg 2: ' . strtoupper($order->reg_2->value());
$line .= ', Reg 3: ' . strtoupper($order->reg_3->value());
}
// If there's a note, add it to the line
if ($order_item['notes'] !== '')
$line .= ', Note: ' . $order_item['notes'];
// Calculate the discount percentage
$discountPercentage = round((($order_item['product']['price'] - $order_item['price']) / $order_item['product']['price']) * 100, 2);
// If the price is different from the product price, add it to the line
if ($order_item['price'] !== $order_item['product']['price'])
$line .= ', Discount: ' . ($order_item['price'] - $order_item['product']['price']) . ' DKK (~' . $discountPercentage . '%)';
// Get the department
$department = $order->getDepartmentByOrderId($order->id);
$economic_department_id = $department['economic_department_id'];
$economic_dimension_id = $department['economic_dimension_id'];
// Add an empty line to the invoice
$economic_invoice_draft->addLineTEXT('');
// Add the line to the invoice
$economic_invoice_draft->addLine(
(string)$order_item['product']['economic_product_id'],
(string)$line,
(string)$order_item['product']['name'] . ' #' . $order_item['id'],
(int)$quantity,
(int)$order_item['price'],
0, // Since we can't be specific about the discount, we set it to 0. (The API has a limit of 2 decimals, and that's not enough for our needs)
(int)$economic_department_id ?? 0,
(int)$economic_dimension_id ?? 0 // If the department is not set, we'll set it to 0
);
// Calculate the discount percentage
$discountPercentage = round((($order_item['product']['price'] - $order_item['price']) / $order_item['product']['price']) * 100, 0);
// If the price is different from the product price, add it to the line
if ($order_item['price'] !== $order_item['product']['price'])
$economic_invoice_draft->addLineTEXT('Rabat: ' . ($order_item['price'] - $order_item['product']['price']) . ' DKK (' . $discountPercentage . '%)');
// If there's a reference, add it to the line
if ($order_item['reference'] !== '') {
$economic_invoice_draft->addLineTEXT('Reference:');
// Add "# " in the beginning of each line (If there's more than one line, otherwise we just add the prefix once)
if (str_contains($order_item['reference'], "\n")) {
foreach ( explode("\n", $order_item['reference']) as $line ) {
$economic_invoice_draft->addLineTEXT('# ' . $line);
}
} else {
$economic_invoice_draft->addLineTEXT('# ' . $order_item['reference']);
}
}
// Add the registration numbers (if they exist, and the customer requires it)
if ($customer->doesUserHaveAttribute('requiresRegistrationNumbersInvoice')) {
$line_reg = '';
if ($order->reg_1->value() !== '') {
$line_reg .= 'Reg 1: ' . strtoupper($order->reg_1->value());
}
if ($order->reg_2->value() !== '') {
$line_reg .= ', Reg 2: ' . strtoupper($order->reg_2->value());
}
if ($order->reg_3->value() !== '') {
$line_reg .= ', Reg 3: ' . strtoupper($order->reg_3->value());
}
$economic_invoice_draft->addLineTEXT($line_reg);
}
// If there's a note, add it to the line
if ($order_item['notes'] !== '') {
$economic_invoice_draft->addLineTEXT('Notat:');
// Add "# " in the beginning of each line (If there's more than one line, otherwise we just add the prefix once)
if (str_contains($order_item['notes'], "\n")) {
foreach ( explode("\n", $order_item['notes']) as $line ) {
$economic_invoice_draft->addLineTEXT('# ' . $line);
}
} else {
$economic_invoice_draft->addLineTEXT('# ' . $order_item['notes']);
}
}
}
function addOrderToInvoiceDraft(int $economic_invoice_draft_id, orders_o $order, users_o $customer, array $order_items): object
{
$economic_invoice_draft = (new economic_invoice_draft_mo());
// Add two empty lines to the invoice, to separate the orders
$economic_invoice_draft->addLineTEXT('');
$economic_invoice_draft->addLineTEXT('');
// Add the department, date, reference
$this->addTheDepartmentDateReference($economic_invoice_draft, $order->getDepartmentByOrderId($order->id)['name'], $order);
// Add the lines to the invoice