Files
arcodangeandClaude Fable 5 fb13bdcc4f feat(write-skill): GED attach op — upload the source document onto its invoice (erp#43)
document-attach.sh uploads a source piece (the supplier's own PDF) onto an
invoice's GED via POST /documents/upload — idempotent by (object, filename,
sha256): before any POST the object's GED is listed and a same-named entry is
downloaded back and sha256-compared. Identical → deduped no-op; different
content → ABORT (refuse-never-repair, overwriteifexists always 0, never
Dolibarr's overwrite flag). Read-back after upload: re-list + download +
sha256-verify. Module-relative download paths are derived from the listing's
fullname (supplier invoices carry an id-derived get_exdir prefix like
9/2/FAF2026013/…, so reconstruction would be wrong).

Promote integration: new `attach` op in promote-plan/promote-apply (OP_SCRIPT),
object_id resolvable via @ref and #supplierinvoice lookups; a relative `file`
resolves against the manifest's directory (replay packs carry pdfs/ beside the
manifest, gitignored — README documents the books@ re-fetch message ids).
promote-plan prints each file's sha256 (or a loud MISSING) at review time.
CLI: `arcodange sandbox attach`.

Proof: offline case 12 in tests/run-tests.sh (upload body, dedupe, conflict
abort, field refusal, manifest-relative resolution via stubbed /documents);
live: manifest-C-ged-attach.json applied twice on the sandbox — run 1 four
created, run 2 four deduped, one GED file per FAF2026010-013, stored sha256s
equal to the re-fetched sources; tests/replay-idempotency.sh extended with an
attach op (4 created → 4 deduped, ged_files count unchanged) and a live
same-name/different-bytes abort verified.

Closes erp#43

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VRShc4QhLLU73FLHx9vskh
2026-07-19 00:11:59 +02:00

77 lines
4.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Render a promote manifest as a human-readable change-set for review.
#
# A manifest is a JSON array of operations using SYMBOLIC refs (@name) instead of
# ids, so the SAME manifest replays on sandbox or prod:
# [ {"op":"thirdparty","ref":"tp1","input":{"name":"OVH","role":"supplier"}},
# {"op":"invoice","ref":"inv1","input":{"socid":"@tp1","kind":"supplier",
# "ref_supplier":"X","validate":true,"lines":[{"desc":"Hosting","qty":1,"price_ht":80,"tva":20,"type":"service"}]}},
# {"op":"payment","input":{"invoice_id":"@inv1","mode":"VIR","account_id":1}} ]
set -euo pipefail
MANIFEST="${1:?usage: promote-plan.sh <manifest.json>}"
python3 - "$MANIFEST" <<'PY'
import hashlib, json, os, sys
ops = json.load(open(sys.argv[1]))
print("Promote plan — %d operation(s) (symbolic refs resolve at apply time):\n" % len(ops))
for i, op in enumerate(ops, 1):
t = op["op"]; inp = op.get("input", {}); ref = op.get("ref")
print(" %d. %s%s" % (i, t, (" => @%s" % ref) if ref else ""))
if t == "thirdparty":
print(" name=%r role=%s%s (idempotent: dedupe by exact name; ambiguous aborts)"
% (inp.get("name"), inp.get("role", "client"),
(" tva=%s" % inp["tva_intra"]) if inp.get("tva_intra") else ""))
elif t == "invoice":
supplier = str(inp.get("kind", "customer")).lower() in ("supplier", "fournisseur")
dk = ("socid+ref_supplier" if supplier and inp.get("ref_supplier")
else "socid+date+total+lines")
print(" socid=%s kind=%s%s validate=%s (idempotent: dedupe by %s)"
% (inp.get("socid"), inp.get("kind", "customer"),
(" ref_supplier=%s" % inp["ref_supplier"]) if inp.get("ref_supplier") else "",
bool(inp.get("validate")), dk))
for ln in inp.get("lines", []):
print(" - %r qty=%s pu_ht=%s tva=%s%% [%s]" % (ln.get("desc", ""), ln.get("qty", 1),
ln.get("price_ht", ln.get("subprice")), ln.get("tva", ln.get("tva_tx", 20)), ln.get("type", "service")))
elif t == "creditnote":
print(" socid=%s source_invoice=%s validate=%s" % (inp.get("socid"), inp.get("source_invoice"), bool(inp.get("validate"))))
for ln in inp.get("lines", []):
print(" - %r qty=%s pu_ht=%s tva=%s%%" % (ln.get("desc", ""), ln.get("qty", 1),
ln.get("price_ht", ln.get("subprice")), ln.get("tva", ln.get("tva_tx", 20))))
elif t == "payment":
txid = inp.get("transaction_id") or inp.get("num")
print(" invoice=%s mode=%s account=%s %s%s" % (inp.get("invoice_id"), inp.get("mode", "VIR"),
inp.get("account_id"), ("amount=%s" % inp["amount"]) if inp.get("amount") else "(full)",
(" tx=%s (idempotent: dedupe by invoice+amount+tx)" % txid) if txid
else " tx=MISSING (no dedupe key — a replay WILL double-pay)"))
elif t == "thirdparty_update":
flds = inp.get("fields") or {}
print(" socid=%s update %d dossier field(s): %s" % (inp.get("socid"), len(flds),
", ".join(sorted(flds)) if flds else "NONE (will be refused)"))
elif t == "attach":
obj = inp.get("object_id") or inp.get("ref")
f = inp.get("file")
# A relative file resolves against the MANIFEST (same rule as apply).
resolved = None
if isinstance(f, str):
resolved = f if os.path.isabs(f) else os.path.normpath(
os.path.join(os.path.dirname(os.path.abspath(sys.argv[1])), f))
print(" object=%s modulepart=%s file=%s" % (obj, inp.get("modulepart"), f))
if resolved and os.path.isfile(resolved):
data = open(resolved, "rb").read()
print(" sha256=%s (%d bytes) (idempotent: dedupe by object+filename+sha256; "
"same name + different content aborts)"
% (hashlib.sha256(data).hexdigest(), len(data)))
else:
print(" !! FILE MISSING at plan time: %s — apply WILL fail; "
"re-fetch the source first" % resolved)
elif t == "contact":
name = " ".join(x for x in (inp.get("firstname"), inp.get("lastname")) if x) or "?"
print(" socid=%s contact %s%s%s (idempotent: dedupe by email, then lastname+firstname)"
% (inp.get("socid"), name,
(" — %s" % inp["poste"]) if inp.get("poste") else "",
(" <%s>" % inp["email"]) if inp.get("email") else ""))
print("\nNext:")
print(" promote-apply.sh <manifest> --target sandbox # rehearse the replay (safe)")
print(" promote-apply.sh <manifest> --target prod # WRITES PROD — needs DOLIBARR_PROD_WRITE_KEY")
print(" # + ARCO_PROMOTE_CONFIRM=I-UNDERSTAND-THIS-WRITES-PROD")
PY