feat(payment): first-class transaction_id when recording a règlement

Make the originating bank transaction id a first-class input on payment-record.sh
so every règlement is tied to the real bank movement at write time.

- `transaction_id` is the canonical field (the Qonto/Wise feed tx id); `num` stays
  as a back-compat alias. It's stored on the payment's bank line (llx_bank.num_chq),
  the reconciliation key.
- Recording WITHOUT a transaction_id prints a stderr warning (still posts, but won't
  auto-reconcile) — nudges the agent to always carry it.
- Output normalises to {id, bank_transaction_id, transaction_id}.
- Promote: manifests' payment ops carry transaction_id; promote-plan shows it
  (tx=… or tx=MISSING).

Proven live: customer + supplier record with transaction_id; the `num` alias maps
to the same field; the no-tx warning fires; promote plan/apply carry it through.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-30 00:34:10 +02:00
parent d81bff0ed3
commit b945c8de47
5 changed files with 42 additions and 28 deletions

View File

@@ -8,14 +8,17 @@
# account_id (required) the bank account id receiving/paying
# date "YYYY-MM-DD" (default today)
# amount (REQUIRED for supplier; customer pays the full remaining)
# num (optional) the bank reference stored on the payment — put the
# Qonto/Wise transaction id here so the règlement reconciles to the feed
# transaction_id (recommended) the originating bank transaction id (the Qonto/Wise
# 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.
# comment (optional)
#
# The invoice must be VALIDATED first (invoice-create.sh ... "validate":true).
# Emits {id, bank_transaction_id, num} on stdout. `bank_transaction_id` is the
# Dolibarr bank line (llx_bank.fk_bank_line) the payment created — the id that bank
# reconciliation (bank-match) keys on to link this payment to a statement line.
# Emits {id, bank_transaction_id, transaction_id} on stdout. `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).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
W="${DOL_WRITE:-${SCRIPT_DIR}/dol-write.sh}"
@@ -42,35 +45,39 @@ ds = d.get("date")
epoch = int((datetime.datetime.strptime(ds, "%Y-%m-%d") if ds
else datetime.datetime.now()).timestamp())
inv = d["invoice_id"]
num = d.get("num", "")
# 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 "")
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")
if supplier:
if d.get("amount") is None:
sys.exit("payment-record.sh: supplier payments require an 'amount'")
endpoint = "/supplierinvoices/%s/payments" % inv
body = {"datepaye": epoch, "payment_mode_id": mode, "closepaidinvoices": "yes",
"accountid": d["account_id"],
"amount": str(d["amount"]), "num_payment": num,
"amount": str(d["amount"]), "num_payment": tx,
"comment": d.get("comment", "")}
else:
endpoint = "/invoices/%s/payments" % inv
body = {"datepaye": epoch, "paymentid": mode, "closepaidinvoices": "yes",
"accountid": d["account_id"], "num_payment": num,
"accountid": d["account_id"], "num_payment": tx,
"comment": d.get("comment", "")}
print(endpoint)
print(json.dumps(body))
print(num)
print(tx)
PY
# Correlate the created payment back to its bank transaction line. The payments
# list carries fk_bank_line but not the paiement rowid, so match on the provided
# num (the external bank ref), else fall back to the most recent line.
# transaction_id (the external bank ref), else fall back to the most recent line.
cat > "${PYF2}" <<'PY'
import json, sys, os
rows = json.load(sys.stdin); rows = rows if isinstance(rows, list) else []
num = os.environ.get("NUM", ""); pid = int(os.environ["PAYID"])
tx = os.environ.get("TX", ""); pid = int(os.environ["PAYID"])
pick = None
if num:
cand = [r for r in rows if str(r.get("num", "")) == num]
if tx:
cand = [r for r in rows if str(r.get("num", "")) == tx]
if cand:
pick = cand[-1]
if pick is None and rows:
@@ -78,13 +85,13 @@ if pick is None and rows:
btx = (pick or {}).get("fk_bank_line")
print(json.dumps({"id": pid,
"bank_transaction_id": int(btx) if btx and str(btx).isdigit() else btx,
"num": (pick or {}).get("num", "")}))
"transaction_id": (pick or {}).get("num", "")}))
PY
MAPPED="$(printf '%s' "${INPUT}" | python3 "${PYF}")"
ENDPOINT="$(sed -n 1p <<<"${MAPPED}")"
BODY="$(sed -n 2p <<<"${MAPPED}")"
NUM="$(sed -n 3p <<<"${MAPPED}")"
TX="$(sed -n 3p <<<"${MAPPED}")"
PAYID="$("${W}" POST "${ENDPOINT}" "${BODY}")"
if [[ ! "${PAYID}" =~ ^[0-9]+$ ]]; then
@@ -93,4 +100,4 @@ if [[ ! "${PAYID}" =~ ^[0-9]+$ ]]; then
fi
# Same path serves the GET list; resolve fk_bank_line and emit the enriched record.
"${W}" GET "${ENDPOINT}" | PAYID="${PAYID}" NUM="${NUM}" python3 "${PYF2}"
"${W}" GET "${ENDPOINT}" | PAYID="${PAYID}" TX="${TX}" python3 "${PYF2}"