Add form field options handling and invoice discount logic

Added support for defining and retrieving form field options, along with methods to manage field-specific configurations. Enhanced invoice draft generation to handle discounts, including the ability to calculate total discounts and display them as separate lines. These updates improve flexibility in forms and invoice processing.
This commit is contained in:
Jepp9350
2025-03-24 10:27:11 +01:00
parent 45328a6b72
commit 7976b89fff
3 changed files with 124 additions and 3 deletions
@@ -174,10 +174,19 @@ class economic_invoice_draft
$order_items = $order->applyDepartmentPrices($order_items, $order->department_id->value());
// Get the department
$department = $order->getDepartmentByOrderId($order->id);
// Define the total discount applied to the order
$total_discount = 0;
// Loop through the order items
foreach ( $order_items as $order_item ) {
// Add the order item to the draft invoice
self::addOrderItemLine($order_item, $department);
// Add the line discount to the total discount
$total_discount += $order_item['product']['price'] - $order_item['price'];
}
// If the total discount is greater than 0, add it to the invoice
if ($total_discount > 0) {
// Add the discount to the invoice
self::addProductDiscountLine($total_discount, $department['economic_department_id'], $department['economic_dimension_id'] ?? 0);
}
}
@@ -186,11 +195,12 @@ class economic_invoice_draft
* @note The lines won't be saved until the addLines() method is called.
* @param array $order_item The order item to add
* @param array $department The department to add
* @param boolean $show_discount Whether to show the discount or not
* @return void
* @throws Exception if the order item is not found
* @throws Exception if the order item is not valid
*/
public function addOrderItemLine(array $order_item, array $department): void
public function addOrderItemLine(array $order_item, array $department, bool $show_discount = false): void
{
// Check if the order item is valid
if (!isset($order_item['id'])) {
@@ -205,7 +215,7 @@ class economic_invoice_draft
(string)$order_item['product']['economic_product_id'],
(string)$order_item['product']['name'],
(int)$order_item['quantity'] ?? 1,
(int)$order_item['price'],
(int)$order_item['product']['price'],
$economic_department_id,
$economic_dimension_id
);
@@ -214,7 +224,7 @@ class economic_invoice_draft
$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'])
if ($order_item['price'] !== $order_item['product']['price'] && $show_discount)
self::addTextLine('Rabat: ' . ($order_item['price'] - $order_item['product']['price']) . ' DKK (' . $discountPercentage . '%)');
// If there's a reference, add it to the line
@@ -273,6 +283,37 @@ class economic_invoice_draft
$this->draft_lines[] = $line;
}
/**
* Add a discount line to the draft invoice
* @note The lines won't be saved until the addLines() method is called.
* @param float $discount The discount to add
* @param int $economic_department_id The department id to add
* @param int $economic_dimension_id The dimension id to add
* @return void
*/
public function addProductDiscountLine(float $discount, int $economic_department_id, int $economic_dimension_id): void
{
// Add a line to the invoice
$line = [
'product' => [
'productNumber' => 'TotDiscount',
],
'quantity' => 1,
'unitNetPrice' => -$discount,
'discountPercentage' => 0,
'description' => 'Rabat',
];
// If the department is set, add it to the line
if ($economic_department_id) {
$line['departmentalDistribution'] = [
'departmentalDistributionNumber' => $economic_department_id,
'DistributionType' => 'Department',
'dimension' => $economic_dimension_id
];
}
$this->draft_lines[] = $line;
}
/**
* Check if a draft invoice exists in the Economic system
* @throws Exception if the draft invoice does not exist
@@ -59,5 +59,7 @@ class book_wash_f extends form_helper_c
'wants_pickup' => self::className() . '::validateBoolean',
'notes' => self::className() . '::validateString',
]);
// Add the department field options
self::addInputFieldOptions('department_id', self::getDepartmentOptions());
}
}
+78
View File
@@ -5,6 +5,7 @@ namespace traits;
use classes\response;
use Exception;
use forms\book_wash_f;
use objects\departments_o;
use objects\form_submissions_o;
use routes\formRoute;
@@ -55,6 +56,12 @@ trait form_t
* @see validateInput()
*/
public array $form_fields_validations = [];
/**
* The form fields options
* @notation This should be used to store the form fields options (e.g. ['field1' => ['option1' => 'Option 1', 'option2' => 'Option 2']])
* @var array $form_fields_options The form fields options
*/
public array $form_fields_options = [];
/**
* The form raw data / unsanitized input values
@@ -286,6 +293,40 @@ trait form_t
$this->form_fields_validations = $fields;
}
/**
* Add options to the form input fields
* @param string $field The field to add options to
* @param array $input_fields_options The input fields options (e.g. ['option1' => 'Option 1', 'option2' => 'Option 2'])
* @throws Exception If the field is not valid
* @throws Exception If the input fields options are not valid
* @notation E.g. ['field1' => ['option1' => 'Option 1', 'option2' => 'Option 2']]
* @throws Exception
*/
public function addInputFieldOptions(string $field, array $input_fields_options): void
{
// Check if the field is valid
if (!isset($this->form_fields_validations[$field])) {
throw new Exception('The field is not valid: ' . $field);
}
// Check if the input fields options are valid
if (empty($input_fields_options)) {
throw new Exception('The input fields options are not valid: ' . $field);
}
// Check if the input fields options are valid
foreach ( $input_fields_options as $option => $value ) {
// Check if the option is valid
if (!is_string($value)) {
throw new Exception('The input fields options are not valid: ' . $field);
}
}
// Check if the field is valid
if (!isset($this->form_fields_validations[$field])) {
throw new Exception('The field is not valid: ' . $field);
}
// Set the form input fields options
$this->form_fields_options[$field] = $input_fields_options;
}
/**
* Validation method for emails
* @param string $email The email to validate
@@ -490,10 +531,30 @@ trait form_t
'id' => self::getFormFieldPrefix() . $field,
'validation' => removeMethodPrefix($validation_method),
];
// Check if the field has options
if (count(self::getFormFieldOptions($field)) > 0) {
$formatted_fields[$field]['options'] = self::getFormFieldOptions($field);
}
}
return $formatted_fields;
}
/**
* Get the form field options (if applicable)
* @notation This should be used to get the form field options (e.g. ['option1' => 'Option 1', 'option2' => 'Option 2'])
* @param string $field The field to get the options for
* @return array The form field options
*/
public function getFormFieldOptions(string $field): array
{
// Check if the field is valid
if (!isset($this->form_fields_options[$field])) {
return [];
}
// Return the form field options
return $this->form_fields_options[$field];
}
/**
* Set the sanitized data to the form
* @param array $data The sanitized data
@@ -508,4 +569,21 @@ trait form_t
// Set the form sanitized data
$this->form_sanitized_data = $data;
}
/**
* Get department options
* @return array The department options
* @notation This should be used to get the department options (e.g. [1 => 'Department 1', 2 => 'Department 2'])
* @see formRoute - This is used to get the department options, to dynamically generate the form.
*/
public function getDepartmentOptions(): array
{
$departments = (new departments_o())->list(false) ?? [];
$options = [];
/** @var array $department */
foreach ( $departments as $department ) {
$options[$department['id']] = $department['name'];
}
return $options;
}
}