#!/usr/bin/env bash # Record a payment (règlement) on a validated invoice in the SANDBOX. # # Input: a JSON object on stdin (or a file path in $1): # invoice_id (required) the invoice to pay # kind "customer" | "supplier" (default "customer") # mode "VIR" | "CB" | "CHQ" | "LIQ" (default "VIR") # account_id (required) the bank account id receiving/paying # date "YYYY-MM-DD" (default today) # amount (REQUIRED for supplier; customer pays the full remaining) # 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, 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}" SRC="${1:-}" if [[ -n "${SRC}" && "${SRC}" != "-" ]]; then INPUT="$(cat "${SRC}")"; else INPUT="$(cat)"; fi 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()) if not d.get("invoice_id"): sys.exit("payment-record.sh: 'invoice_id' is required") if not d.get("account_id"): sys.exit("payment-record.sh: 'account_id' is required") # Stable Dolibarr c_paiement ids (sandbox seeded from prod / standard defaults). MODE = {"VIR": 2, "CB": 6, "CHQ": 7, "LIQ": 4} mode = MODE.get(str(d.get("mode", "VIR")).upper()) if mode is None: sys.exit("payment-record.sh: unknown mode (use VIR|CB|CHQ|LIQ)") supplier = d.get("kind", "customer").lower() in ("supplier", "fournisseur") ds = d.get("date") 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 "") 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": tx, "comment": d.get("comment", "")} else: endpoint = "/invoices/%s/payments" % inv body = {"datepaye": epoch, "paymentid": mode, "closepaidinvoices": "yes", "accountid": d["account_id"], "num_payment": tx, "comment": d.get("comment", "")} print(endpoint) print(json.dumps(body)) 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 # 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 [] tx = os.environ.get("TX", ""); pid = int(os.environ["PAYID"]) pick = None 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: 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, "transaction_id": (pick or {}).get("num", "")})) PY MAPPED="$(printf '%s' "${INPUT}" | python3 "${PYF}")" ENDPOINT="$(sed -n 1p <<<"${MAPPED}")" BODY="$(sed -n 2p <<<"${MAPPED}")" TX="$(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}" TX="${TX}" python3 "${PYF2}"