From 0e591afe34ee27eac62ebdb2e45eea6917237222 Mon Sep 17 00:00:00 2001 From: Pichinov-Jose Date: Mon, 18 May 2026 01:16:01 +0200 Subject: [PATCH] Fix: allow free-text invoice/order/proposal lines when no product matches (#38298) ToolCrudObjects::processAddLine() uses the line's 'description' as a fallback product identifier when 'product' is not explicitly provided. It then calls findProduct() to look up an existing Dolibarr product by ref/barcode/label. When no product matches (the common case for AI-generated lines that describe a one-off service or a custom item), findProduct() returns an error array and the function aborts WITHOUT ever calling addline(). The resulting document gets its header created but ends up with zero lines and a 0.00 total. This is contrary to standard Dolibarr behavior: free-text line items (no product link, just a description and a price) are a first-class feature of invoices, orders, and proposals. This patch distinguishes between the two cases: - Caller explicitly passed 'product': they wanted a specific product -> still abort with the original error if not found (unchanged). - Caller only passed 'description': it's a free-text line -> silently fall through with $prod = null. Dolibarr then creates a free-text line with the user-provided description, qty, unit_price and vat_rate. Tested with: create_other_document object_type=proposal containing 11 line items, none of which match existing product references -> all 11 lines are now created with their correct totals (previously the document was created with 0 lines and a 0.00 total). --- htdocs/ai/tools/crud_objects.class.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/htdocs/ai/tools/crud_objects.class.php b/htdocs/ai/tools/crud_objects.class.php index c8634bd3735..9b5f25e1fda 100644 --- a/htdocs/ai/tools/crud_objects.class.php +++ b/htdocs/ai/tools/crud_objects.class.php @@ -552,8 +552,17 @@ If user says 'order' without any qualifier, they mean a SALES ORDER - use this t if ($productIdentifier !== '') { $findResult = $this->findProduct($productIdentifier); if (is_array($findResult) && isset($findResult['error'])) { - // Ensure the return array matches the expected shape by adding 'success' => false - return array_merge(['success' => false], $findResult); + // Only abort if the caller EXPLICITLY asked for a product (via the 'product' + // argument). If they only provided a free-text 'description', we silently + // fall through with $prod = null so Dolibarr creates a free-text line item, + // which is a perfectly valid Dolibarr feature. + // Previous behaviour aborted ALL line creations whose description didn't + // match an existing product reference, which broke AI-driven creation of + // invoices/orders/proposals from one-off line descriptions. + if (isset($args['product'])) { + return array_merge(['success' => false], $findResult); + } + // fall through: $prod stays null } if (is_object($findResult)) { $prod = $findResult;