The human-gated path that carries a reviewed sandbox change to prod.
- promote-plan.sh: render a manifest (JSON array of write ops with symbolic @refs
instead of ids — portable sandbox->prod) as a human-readable change-set.
- promote-apply.sh <manifest> --target sandbox|prod: replay it, resolving each
@ref to the id actually created during the run (dependent ops wire up). sandbox
rehearses via dol-write.sh; prod via dol-prod-write.sh.
- dol-prod-write.sh: the ONLY prod-write path. Prod key read from the ENVIRONMENT
only (DOLIBARR_PROD_WRITE_KEY, never a stored .env); every write refused unless
ARCO_PROMOTE_CONFIRM=I-UNDERSTAND-THIS-WRITES-PROD.
- create scripts take a DOL_WRITE override so promote-apply reuses them per target.
- bin/arcodange: `promote {plan|apply}` group + example manifest.
- payment-record.sh: fixed supplier payments (payment_mode_id + closepaidinvoices).
Proven live: plan renders; apply --target sandbox replays a 3-op chain with refs
resolved (@tp1->id, invoice socid=@tp1, payment invoice=@inv1); --target prod
without the confirm flag is REFUSED before sending. Supplier payment now works
end-to-end via the script.
Limitation (documented): manifests reference entities they create (@ref);
pre-existing prod entities need business-key resolution (follow-up).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
65 lines
2.6 KiB
Bash
Executable File
65 lines
2.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Create a customer credit note (avoir) in the SANDBOX — a customer invoice of
|
|
# type 2 that references the original invoice. Amounts come out negative.
|
|
#
|
|
# Input: a JSON object on stdin (or a file path in $1):
|
|
# socid (required) the client (must match the source invoice's client)
|
|
# source_invoice (required) the original invoice id being credited
|
|
# date "YYYY-MM-DD" (default today)
|
|
# validate true|false (default false = leave draft)
|
|
# lines: [ { desc, qty, price_ht, tva, type: "product"|"service", product_id? } ]
|
|
# the lines being credited (positive amounts; Dolibarr nets them as a credit)
|
|
#
|
|
# Emits {id, ref, total_ht, total_ttc, fk_facture_source, statut} on stdout.
|
|
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)"; trap 'rm -f "${PYF}"' EXIT
|
|
cat > "${PYF}" <<'PY'
|
|
import json, sys, datetime
|
|
d = json.loads(sys.stdin.read())
|
|
for req in ("socid", "source_invoice"):
|
|
if not d.get(req):
|
|
sys.exit("creditnote-create.sh: '%s' is required" % req)
|
|
ds = d.get("date")
|
|
epoch = int((datetime.datetime.strptime(ds, "%Y-%m-%d") if ds
|
|
else datetime.datetime.now()).timestamp())
|
|
lines = []
|
|
for ln in d.get("lines", []):
|
|
is_product = ln.get("type", "service").lower() in ("product", "produit")
|
|
L = {
|
|
"desc": ln.get("desc", ""),
|
|
"subprice": str(ln.get("price_ht", ln.get("subprice", 0))),
|
|
"qty": str(ln.get("qty", 1)),
|
|
"tva_tx": str(ln.get("tva", ln.get("tva_tx", 20))),
|
|
"product_type": "0" if is_product else "1",
|
|
}
|
|
if ln.get("product_id"):
|
|
L["fk_product"] = str(ln["product_id"])
|
|
lines.append(L)
|
|
body = {"socid": d["socid"], "type": 2, "fk_facture_source": d["source_invoice"],
|
|
"date": epoch, "lines": lines}
|
|
print(json.dumps(body))
|
|
print("1" if d.get("validate") else "0")
|
|
PY
|
|
|
|
MAPPED="$(printf '%s' "${INPUT}" | python3 "${PYF}")"
|
|
BODY="$(sed -n 1p <<<"${MAPPED}")"
|
|
VALIDATE="$(sed -n 2p <<<"${MAPPED}")"
|
|
|
|
ID="$("${W}" POST /invoices "${BODY}")"
|
|
if [[ ! "${ID}" =~ ^[0-9]+$ ]]; then
|
|
echo "creditnote-create.sh: create did not return an id: ${ID}" >&2
|
|
exit 1
|
|
fi
|
|
if [[ "${VALIDATE}" == "1" ]]; then
|
|
"${W}" POST "/invoices/${ID}/validate" '{}' >/dev/null
|
|
fi
|
|
"${W}" GET "/invoices/${ID}" | python3 -c "import json,sys
|
|
d=json.load(sys.stdin)
|
|
print(json.dumps({k:d.get(k) for k in ('id','ref','total_ht','total_ttc','fk_facture_source','statut')}))"
|