From 9e461403736d49db1c8c63018d63b5434f7ea161 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Mon, 3 Mar 2025 16:55:09 +0100 Subject: [PATCH] Add support for handling economic invoice drafts/booked IDs Added methods to check and retrieve economic invoice draft and booked IDs, improving invoice tracking functionality. Introduced a new method in the economic endpoint to fetch booked invoice details using external IDs. Additionally, updated the object property class to handle integer values during SQL updates. --- .../nginx/app/classes/object_property.php | 7 +- .../economic_invoices_booked_endpoint.php | 24 +++++ .../objects/collected_order_invoices_o.php | 95 +++++++++++++++---- 3 files changed, 106 insertions(+), 20 deletions(-) diff --git a/services/nginx/app/classes/object_property.php b/services/nginx/app/classes/object_property.php index 0b2859ab..9b74b235 100644 --- a/services/nginx/app/classes/object_property.php +++ b/services/nginx/app/classes/object_property.php @@ -20,7 +20,7 @@ class object_property $this->required = $required; $this->default = $default; } - + public function __toString(): string { // Return the value of the field in the database table @@ -54,7 +54,10 @@ class object_property // If the value is null, set it to null if ($value === null) { $sql = "UPDATE $this->table SET $this->column = NULL WHERE id = $this->id"; - } // If the value is a string, escape it + } // If the value is an integer, set it to the integer value + elseif (is_int($value)) { + $sql = "UPDATE $this->table SET $this->column = $value WHERE id = $this->id"; + } // If the value is a string, escape it else { $value = $db->escape_string($value); $sql = "UPDATE $this->table SET $this->column = '$value' WHERE id = $this->id"; diff --git a/services/nginx/app/modules/economic/endpoints/invoices/economic_invoices_booked_endpoint.php b/services/nginx/app/modules/economic/endpoints/invoices/economic_invoices_booked_endpoint.php index 5e28a55b..de3cb410 100644 --- a/services/nginx/app/modules/economic/endpoints/invoices/economic_invoices_booked_endpoint.php +++ b/services/nginx/app/modules/economic/endpoints/invoices/economic_invoices_booked_endpoint.php @@ -2,6 +2,7 @@ namespace endpoints\orders; +use Exception; use traits\economic_endpoint_t; class economic_invoices_booked_endpoint @@ -42,4 +43,27 @@ class economic_invoices_booked_endpoint return $tmp_invoices; } + /** + * Get booked invoice from external id + * @param string $external_id External id to get + * @return int Invoice id + * @throws Exception If the request fails + * @throws Exception If the invoice is not found + */ + public function get_from_external_id(string $external_id): int + { + $response = $this->send_request( + '/invoices/booked?filter=references.other$eq:' . $external_id, + 'GET' + ); + // Return the response as an object + $response = json_decode($response); + //var_dump($response); + if (isset($response->collection[0])) { + return $response->collection[0]->bookedInvoiceNumber; + } else { + throw new Exception('Invoice booked with external id (' . $external_id . ') not found'); + } + } + } \ No newline at end of file diff --git a/services/nginx/app/objects/collected_order_invoices_o.php b/services/nginx/app/objects/collected_order_invoices_o.php index ea5aa524..40a74e7a 100644 --- a/services/nginx/app/objects/collected_order_invoices_o.php +++ b/services/nginx/app/objects/collected_order_invoices_o.php @@ -80,7 +80,9 @@ class collected_order_invoices_o extends db 'created_at' => (string)$this->created_at->value(), 'updated_at' => (string)$this->updated_at->value(), 'closed_at' => (string)$this->closed_at->value(), - 'orders' => $this->getOrders() + 'orders' => $this->getOrders(), + 'economic_invoice_draft_id' => self::isDraftExisting() ? self::getInvoiceDraftId() : null, + 'economic_invoice_booked_id' => self::isBooked() ? self::getInvoiceBookedId() : null, ]; } @@ -112,6 +114,78 @@ class collected_order_invoices_o extends db return $result; } + /** + * Check if the invoice draft is existing in E-conomic + * @returns bool If the invoice draft is existing + * @throws Exception If the request was not successful + * @throws Exception If the invoice collection is not set + */ + public function isDraftExisting(): bool + { + // Require the invoice collection to be selected + self::requireSelected(); + // Get the invoice draft id from the external id + try { + self::getInvoiceDraftId(); + return true; + } catch (Exception $e) { + return false; + } + } + + /** + * Get the invoice draft id from the external id + * @return int The invoice draft id + * @throws Exception If the request was not successful + * @throws Exception If the invoice draft was not found + */ + public function getInvoiceDraftId(): int + { + // Require the invoice collection to be selected + self::requireSelected(); + // Create an economic object + $economic = new economic(); + // Get the invoice draft id from the external id + $invoice_draft_id = $economic->invoices->draft->get_from_external_id($this->external_id->value()); + return (int)$invoice_draft_id; + } + + /** + * Check if the invoice is booked in E-conomic + * @returns bool If the invoice is booked + * @throws Exception If the request was not successful + * @throws Exception If the invoice collection is not set + */ + public function isBooked(): bool + { + // Require the invoice collection to be selected + self::requireSelected(); + // Get the invoice booked id from the external id + try { + self::getInvoiceBookedId(); + return true; + } catch (Exception $e) { + return false; + } + } + + /** + * Get the invoice booked id from the external id + * @return int The invoice booked id + * @throws Exception If the request was not successful + * @throws Exception If the invoice booked was not found + */ + public function getInvoiceBookedId(): int + { + // Require the invoice collection to be selected + self::requireSelected(); + // Create an economic object + $economic = new economic(); + // Get the invoice booked id from the external id + $invoice_booked_id = $economic->invoices->booked->get_from_external_id($this->external_id->value()); + return (int)$invoice_booked_id; + } + /** * Check if a customer has an open invoice collection * @param int $customer_number The E-conomic customer number @@ -250,6 +324,8 @@ class collected_order_invoices_o extends db if (empty($response->references->other)) { throw new Exception('Invoice collection was not created successfully'); } + // Set the processor + $this->processor->set(1); // Object changed self::objectChanged(); return $this; @@ -273,23 +349,6 @@ class collected_order_invoices_o extends db } } - /** - * Get the invoice draft id from the external id - * @return int The invoice draft id - * @throws Exception If the request was not successful - * @throws Exception If the invoice draft was not found - */ - public function getInvoiceDraftId(): int - { - // Require the invoice collection to be selected - self::requireSelected(); - // Create an economic object - $economic = new economic(); - // Get the invoice draft id from the external id - $invoice_draft_id = $economic->invoices->draft->get_from_external_id($this->external_id->value()); - return (int)$invoice_draft_id; - } - /** * Create a new collected order invoice * @param int $customer_number The E-conomic customer number