feat(payment): return the bank transaction id on règlements (reconciliation link) #26

Merged
arcodange merged 1 commits from claude/dolibarr-payment-txn into main 2026-06-30 00:18:00 +02:00
3 changed files with 52 additions and 10 deletions
Showing only changes of commit e748efd8f0 - Show all commits

View File

@@ -96,15 +96,21 @@ to leave a draft. Emits `{id, ref, ref_supplier, total_ht, total_ttc, statut}`.
### 3 · Payment (règlement) — `scripts/payment-record.sh`
```sh
echo '{"invoice_id":19,"mode":"VIR","account_id":1}' | scripts/payment-record.sh
echo '{"invoice_id":13,"kind":"supplier","mode":"VIR","account_id":1,"amount":96}' \
echo '{"invoice_id":19,"mode":"VIR","account_id":1,"num":"QONTO-TX-1234"}' | scripts/payment-record.sh
echo '{"invoice_id":13,"kind":"supplier","mode":"VIR","account_id":1,"amount":96,"num":"WISE-TX-5678"}' \
| scripts/payment-record.sh
```
The invoice must be **validated** first. `mode`: `VIR|CB|CHQ|LIQ`. Customer
payments settle the full remaining amount and mark the invoice paid; **supplier**
payments require an explicit `amount`. `account_id` is the bank account id — list
them with `scripts/bank-accounts.sh` (`ai_agent_sandbox` now holds `banque lire`).
Emits the new payment id.
Emits **`{id, bank_transaction_id, num}`**. `bank_transaction_id` is the Dolibarr
bank line (`llx_bank`) the payment created — the id bank reconciliation
(`arcodange-bank-reco`) keys on to link a règlement to a statement line. Put the
**originating bank transaction id** (the Qonto/Wise tx id) in `num`: it is stored
on that bank line (`num_chq`), so the feed reconciles by id rather than by fuzzy
amount/date. Both ends of the reconciliation are therefore captured at write time.
### 4 · Credit note (avoir) — `scripts/creditnote-create.sh`

View File

@@ -8,10 +8,14 @@
# 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, comment (optional)
# num (optional) the bank reference stored on the payment — put the
# Qonto/Wise transaction id here so the règlement reconciles to the feed
# comment (optional)
#
# The invoice must be VALIDATED first (invoice-create.sh ... "validate":true).
# Emits the new payment id on stdout.
# 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.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
W="${DOL_WRITE:-${SCRIPT_DIR}/dol-write.sh}"
@@ -19,7 +23,8 @@ W="${DOL_WRITE:-${SCRIPT_DIR}/dol-write.sh}"
SRC="${1:-}"
if [[ -n "${SRC}" && "${SRC}" != "-" ]]; then INPUT="$(cat "${SRC}")"; else INPUT="$(cat)"; fi
PYF="$(mktemp -t dolpy.XXXXXX)"; trap 'rm -f "${PYF}"' EXIT
PYF="$(mktemp -t dolpy.XXXXXX)"; PYF2="$(mktemp -t dolpy2.XXXXXX)"
trap 'rm -f "${PYF}" "${PYF2}"' EXIT
cat > "${PYF}" <<'PY'
import json, sys, datetime
d = json.loads(sys.stdin.read())
@@ -37,24 +42,55 @@ 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", "")
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": d.get("num", ""),
"amount": str(d["amount"]), "num_payment": num,
"comment": d.get("comment", "")}
else:
endpoint = "/invoices/%s/payments" % inv
body = {"datepaye": epoch, "paymentid": mode, "closepaidinvoices": "yes",
"accountid": d["account_id"], "num_payment": d.get("num", ""),
"accountid": d["account_id"], "num_payment": num,
"comment": d.get("comment", "")}
print(endpoint)
print(json.dumps(body))
print(num)
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.
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"])
pick = None
if num:
cand = [r for r in rows if str(r.get("num", "")) == num]
if cand:
pick = cand[-1]
if pick is None and rows:
pick = max(rows, key=lambda r: r.get("date", ""))
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", "")}))
PY
MAPPED="$(printf '%s' "${INPUT}" | python3 "${PYF}")"
ENDPOINT="$(sed -n 1p <<<"${MAPPED}")"
BODY="$(sed -n 2p <<<"${MAPPED}")"
"${W}" POST "${ENDPOINT}" "${BODY}"
NUM="$(sed -n 3p <<<"${MAPPED}")"
PAYID="$("${W}" POST "${ENDPOINT}" "${BODY}")"
if [[ ! "${PAYID}" =~ ^[0-9]+$ ]]; then
echo "payment-record.sh: payment POST did not return an id: ${PAYID}" >&2
exit 1
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}"

View File

@@ -85,7 +85,7 @@ COMMANDS
sandbox WRITE ops on erp-sandbox ONLY (host-guarded; JSON on stdin)
thirdparty Create a client/supplier fiche
invoice Customer/supplier invoice + product/service lines
payment Record a règlement on a validated invoice
payment Record a règlement (num=bank tx id) → bank_transaction_id
creditnote Create an avoir — customer or supplier (kind)
accounts List bank accounts (id/label) to pick account_id
write <METHOD> <path> [body] Raw host-guarded write