fix(txid): normalize bank tx ids to fit Dolibarr's num_payment varchar(50)

Qonto transaction ids run ~67 chars (<org>-<n>-<n>-transaction-<uuid>) but
Dolibarr stores num_payment in varchar(50) (llx_paiement.num_paiement,
llx_paiementfourn.num_paiement) — POSTing a payment with the raw id fails
HTTP 400 "value too long for type character varying(50)". Parade proven live
on the sandbox (2026-07-11): store the UUID suffix (globally unique, ~37
chars). Wise ids (short numerics) are unaffected.

Writer side — payment-record.sh strips everything through "transaction-"
before POST, announces the normalization on stderr, REFUSES (never truncates)
ids still >50 chars after normalization, and emits the normalized num in the
output JSON.

Reader side — bank-match.sh PASS 0 (exact tx-id, erp#28) now compares BOTH
sides in raw AND canonical short form: Qonto feed ids are carried long+short,
payment nums are normalized on compare — so nums stored short (the varchar(50)
form) and historical long-form nums both keep matching. Wise ids untouched.

Proven offline (no credentials, no network, no sandbox/prod writes):
- arcodange-bank-reco/tests/run-tests.sh — new bank-match --fixtures offline
  mode: long feed id ↔ short num, long ↔ long (back-compat), Wise numeric,
  each Δ+19d outside the ±7d window so only PASS 0 can pair them (exit 0,
  3×[tx-id]); plus the empty-num negative (exit 1, 0 matched).
- dolibarr-sandbox-write/tests/run-tests.sh — payment-record via a stubbed
  dol-write.sh (DOL_WRITE hook): long→short in POST body + output JSON, Wise
  untouched, >50-after-normalization refused BEFORE any POST, citing
  varchar(50).

Both SKILL.md document the canonical short form + the varchar(50) constraint.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
2026-07-11 17:48:35 +02:00
co-authored by Claude Fable 5
parent 3045d50626
commit f9d83037b6
21 changed files with 349 additions and 41 deletions
@@ -12,10 +12,18 @@
# tx id from the feed). Stored on the payment's bank line
# (llx_bank.num_chq) so the règlement reconciles to the feed by id.
# `num` is a back-compat alias for the same field.
# NORMALIZED before POST: Dolibarr stores num_payment in varchar(50)
# (llx_paiement.num_paiement / llx_paiementfourn.num_paiement) while
# Qonto ids run ~67 chars (<org>-<n>-<n>-transaction-<uuid>), so
# everything through "transaction-" is stripped and the UUID suffix
# (globally unique, ~37 chars) is the canonical stored form. Wise ids
# (short numerics) pass through unchanged. An id still >50 chars after
# normalization is REFUSED with an error — never truncated silently.
# comment (optional)
#
# The invoice must be VALIDATED first (invoice-create.sh ... "validate":true).
# Emits {id, bank_transaction_id, transaction_id} on stdout. `bank_transaction_id`
# Emits {id, bank_transaction_id, transaction_id} on stdout — `transaction_id` is
# the NORMALIZED num actually stored (what bank-match keys on). `bank_transaction_id`
# is the Dolibarr bank line (llx_bank.fk_bank_line) the payment created — the id
# bank reconciliation (arcodange-bank-reco) keys on to link this règlement to a
# statement line. Recording without a transaction_id warns (it won't auto-reconcile).
@@ -29,7 +37,7 @@ if [[ -n "${SRC}" && "${SRC}" != "-" ]]; then INPUT="$(cat "${SRC}")"; else INPU
PYF="$(mktemp -t dolpy.XXXXXX)"; PYF2="$(mktemp -t dolpy2.XXXXXX)"
trap 'rm -f "${PYF}" "${PYF2}"' EXIT
cat > "${PYF}" <<'PY'
import json, sys, datetime
import json, sys, datetime, re
d = json.loads(sys.stdin.read())
if not d.get("invoice_id"):
sys.exit("payment-record.sh: 'invoice_id' is required")
@@ -46,7 +54,23 @@ epoch = int((datetime.datetime.strptime(ds, "%Y-%m-%d") if ds
else datetime.datetime.now()).timestamp())
inv = d["invoice_id"]
# transaction_id is the first-class bank-feed tx id; num is the back-compat alias.
tx = str(d.get("transaction_id") or d.get("num") or "")
raw_tx = str(d.get("transaction_id") or d.get("num") or "")
# Normalize to the canonical short form. Dolibarr stores num_payment in
# varchar(50) (llx_paiement.num_paiement / llx_paiementfourn.num_paiement) and
# a Qonto id (~67 chars, <org>-<n>-<n>-transaction-<uuid>) blows past it —
# HTTP 400 "value too long for type character varying(50)". Strip everything
# through "transaction-" and store the UUID suffix (globally unique). Wise ids
# (short numerics, no "transaction-") pass through unchanged. bank-match.sh
# normalizes feed ids the same way, so the short form still reconciles by id.
tx = re.sub(r'^.*transaction-', '', raw_tx)
if tx != raw_tx:
sys.stderr.write("payment-record.sh: transaction_id normalized to %r "
"(Qonto prefix stripped — num_payment is varchar(50))\n" % tx)
if len(tx) > 50:
sys.exit("payment-record.sh: transaction_id %r is %d chars even after "
"normalization — Dolibarr's num_payment is varchar(50) and silent "
"truncation would break bank reconciliation; pass a shorter id"
% (tx, len(tx)))
if not tx:
sys.stderr.write("payment-record.sh: WARNING — no transaction_id given; this "
"règlement won't auto-reconcile to the bank feed\n")
@@ -83,9 +107,11 @@ if tx:
if pick is None and rows:
pick = max(rows, key=lambda r: r.get("date", ""))
btx = (pick or {}).get("fk_bank_line")
# transaction_id: the num as stored (normalized) — fall back to the sent TX so
# the JSON always reports the canonical short form even on the recency fallback.
print(json.dumps({"id": pid,
"bank_transaction_id": int(btx) if btx and str(btx).isdigit() else btx,
"transaction_id": (pick or {}).get("num", "")}))
"transaction_id": (pick or {}).get("num", "") or tx}))
PY
MAPPED="$(printf '%s' "${INPUT}" | python3 "${PYF}")"