fix(invoices): Stornorechnung save failed on negative totals

Customer log: /do/inv/dsave failed with "Error converting data type float to
numeric" followed by "Cannot insert the value NULL into column 'InvId'".

- Bump OCORE: SQL_Float/SQL_Decimal string overloads parsed negatives via the
  de-DE server culture ("-19.95" -> -1995); float noise overflowed
  numeric(10,3), so fds__setInvoice never ran.
- InvoiceService: the header save batch now stops with a clear THROW when
  fds__createInvoice/fds__setInvoice yields no @Id, instead of failing again
  in fds__createInvoice_Details and burying the real error. Batch SQL
  extracted to BuildRegisterInvoiceSql for testing.
- Tests: signed/culture parsing under de-DE, Storno param build against the
  proc numeric limits, and the batch guard ordering.
- hlp: read-only script to find invoice/reminder amounts stored scaled up
  by the bug.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Stefan
2026-10-01 10:07:13 +02:00
co-authored by Claude Opus 4.8
parent c741535aec
commit 0a8384ab33
5 changed files with 224 additions and 18 deletions
@@ -0,0 +1,29 @@
-- Read-only check for amounts mangled by the OCORE SQL_Float sign bug (fixed 2026-10-01).
-- Before the fix, a negative amount posted as "-19.95" was parsed with the server's de-DE culture
-- ("." = thousands separator) and stored scaled up, e.g. -19.95 -> -1995. Positive amounts were fine.
-- Affected: fds__invoices header totals (Stornos / credits) and fds__invoice_details service totals
-- written through fds__setInvoice / fds__createInvoice / fds__createInvoice_Details, plus reminder
-- amounts written by ReminderService. Line items (fds__invoice_items) were NOT affected.
--
-- Signal: the header net total disagrees with the sum of the invoice's own line items.
-- 1) Invoices with a negative header total, compared against their persisted line items.
SELECT i.[Id], i.[InvoiceId], i.[InvoiceType], i.[IsFinal], i.[DateCreated], i.[DateModified],
i.[InvoiceBalance], i.[InvoiceBalance_net], i.[InvoiceVAT_net1],
d.[InvoiceService_net], d.[InvoiceService_VAT],
items_net = it.[items_net],
suspicious = CASE WHEN it.[items_net] IS NOT NULL
AND ABS(i.[InvoiceBalance_net] - it.[items_net]) > 0.05 THEN 1 ELSE 0 END
FROM [dbo].[fds__invoices] i
LEFT JOIN [dbo].[fds__invoice_details] d ON d.[InvId] = i.[Id]
OUTER APPLY (SELECT [items_net] = SUM(x.[value_total])
FROM [dbo].[fds__invoice_items] x
WHERE x.[InvId] = i.[Id]) it
WHERE i.[InvoiceBalance] < 0 OR i.[InvoiceBalance_net] < 0
OR d.[InvoiceService_net] < 0 OR d.[InvoiceService_VAT] < 0
ORDER BY suspicious DESC, i.[DateModified] DESC;
-- 2) Reminders with a negative amount (same helper).
SELECT *
FROM [dbo].[fds__reminder]
WHERE [amount] < 0 OR [amount_payed] < 0;