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).
This commit is contained in:
Pichinov-Jose 2026-05-18 01:16:01 +02:00 committed by GitHub
parent 6ee5130f24
commit 0e591afe34
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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;