feat(skills,cli): supplier avoirs + banque lire (bank-account discovery) #25

Merged
arcodange merged 1 commits from claude/dolibarr-last-bits into main 2026-06-30 00:08:08 +02:00
5 changed files with 52 additions and 17 deletions
Showing only changes of commit 64d2cb4237 - Show all commits

View File

@@ -102,20 +102,27 @@ echo '{"invoice_id":13,"kind":"supplier","mode":"VIR","account_id":1,"amount":96
```
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 (the
read-only `dolibarr-payments-state` skill lists them; `ai_agent_sandbox` does not
yet have `banque lire`, so pass the id). Emits the new payment id.
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.
### 4 · Credit note (avoir) — `scripts/creditnote-create.sh`
```sh
# customer avoir
echo '{"socid":42,"source_invoice":19,"validate":true,
"lines":[{"desc":"Avoir partiel conseil","qty":1,"price_ht":100,"tva":20,"type":"service"}]}' \
| scripts/creditnote-create.sh
# supplier avoir (avoir fournisseur)
echo '{"socid":12,"kind":"supplier","source_invoice":17,"ref_supplier":"AV-2026-77","validate":true,
"lines":[{"desc":"Avoir hosting","qty":1,"price_ht":120,"tva":20,"type":"service"}]}' \
| scripts/creditnote-create.sh
```
A customer invoice of `type=2` referencing `source_invoice` (`fk_facture_source`);
amounts come out negative (a credit). `validate:true` turns the draft into a
numbered `AVC…` avoir. Emits `{id, ref, total_ht, total_ttc, fk_facture_source, statut}`.
An invoice of `type=2` referencing `source_invoice` (`fk_facture_source`); amounts
come out negative. `kind:"supplier"` targets `/supplierinvoices` (carry
`ref_supplier`); default `customer` targets `/invoices`. `validate:true` numbers it
(`AVC…` for customer, `AVF…` for supplier). Emits `{id, ref, ref_supplier, total_ht,
total_ttc, fk_facture_source, statut}`.
## Promote to prod (rehearse → review → replay)
@@ -163,8 +170,9 @@ sandbox KissMetrics on `--target sandbox` and the prod one on `--target prod`.
REST create needs `code_client`/`code_fournisseur = "-1"` to trigger it — the
script does this; without it the API errors `ErrorCustomerCodeRequired`.
- **Dates** are sent as Unix epochs; pass `date:"YYYY-MM-DD"` or omit for today.
- **`banque lire`** isn't granted yet → `GET /bankaccounts` returns empty. Add it
to the provisioner's rights set if account discovery from this skill is needed.
- **`banque lire`** (rights id 111) is granted → `scripts/bank-accounts.sh` lists
accounts (id/label/bank) so a payment can pick its `account_id`. It's in the
provisioner's `WRITE_IDS`, so a fresh `provisionSandbox.ts` run includes it.
- **Avoirs (credit notes)** → `creditnote-create.sh` (customer invoice `type=2`
referencing `source_invoice`; amounts negative, ref `AVC…`). Supplier avoirs
are a follow-up.

View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# List the sandbox bank accounts (id + label) so a payment can pick its account_id.
# Read-only; needs the ai_agent_sandbox user to hold `banque lire` (rights id 111).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
W="${DOL_WRITE:-${SCRIPT_DIR}/dol-write.sh}"
"${W}" GET "/bankaccounts" | python3 -c "import json,sys
d = json.load(sys.stdin); rows = d if isinstance(d, list) else []
if not rows:
print('(no bank accounts defined)'); sys.exit(0)
print('%-4s %-34s %-10s %s' % ('id', 'label', 'bank', 'currency'))
for a in rows:
print('%-4s %-34s %-10s %s' % (a.get('id'), (a.get('label') or '')[:34],
a.get('bank') or '', a.get('currency_code') or ''))"

View File

@@ -1,16 +1,20 @@
#!/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.
# Create a credit note (avoir) in the SANDBOX — an invoice of type 2 that
# references the original invoice. Amounts come out negative. Customer avoir =
# /invoices; supplier avoir (avoir fournisseur) = /supplierinvoices.
#
# Input: a JSON object on stdin (or a file path in $1):
# socid (required) the client (must match the source invoice's client)
# socid (required) the third party (must match the source invoice's)
# source_invoice (required) the original invoice id being credited
# kind "customer" (default) | "supplier"
# ref_supplier (supplier only) the supplier's avoir reference (recommended —
# some supplier setups require it to validate)
# 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.
# Emits {id, ref, ref_supplier, total_ht, total_ttc, fk_facture_source, statut}.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
W="${DOL_WRITE:-${SCRIPT_DIR}/dol-write.sh}"
@@ -25,6 +29,7 @@ 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)
is_supplier = str(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())
@@ -43,22 +48,26 @@ for ln in d.get("lines", []):
lines.append(L)
body = {"socid": d["socid"], "type": 2, "fk_facture_source": d["source_invoice"],
"date": epoch, "lines": lines}
if is_supplier and d.get("ref_supplier"):
body["ref_supplier"] = d["ref_supplier"]
print(json.dumps(body))
print("1" if d.get("validate") else "0")
print("/supplierinvoices" if is_supplier else "/invoices")
PY
MAPPED="$(printf '%s' "${INPUT}" | python3 "${PYF}")"
BODY="$(sed -n 1p <<<"${MAPPED}")"
VALIDATE="$(sed -n 2p <<<"${MAPPED}")"
ENDPOINT="$(sed -n 3p <<<"${MAPPED}")"
ID="$("${W}" POST /invoices "${BODY}")"
ID="$("${W}" POST "${ENDPOINT}" "${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
"${W}" POST "${ENDPOINT}/${ID}/validate" '{}' >/dev/null
fi
"${W}" GET "/invoices/${ID}" | python3 -c "import json,sys
"${W}" GET "${ENDPOINT}/${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')}))"
print(json.dumps({k:d.get(k) for k in ('id','ref','ref_supplier','total_ht','total_ttc','fk_facture_source','statut')}))"

View File

@@ -86,7 +86,8 @@ COMMANDS
thirdparty Create a client/supplier fiche
invoice Customer/supplier invoice + product/service lines
payment Record a règlement on a validated invoice
creditnote Create an avoir (credit note) of a source invoice
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
promote Replay a reviewed change-set sandbox -> prod (ADR-0003)
@@ -285,6 +286,7 @@ EOF
invoice) exec "${SKILLS}/dolibarr-sandbox-write/scripts/invoice-create.sh" "$@" ;;
payment) exec "${SKILLS}/dolibarr-sandbox-write/scripts/payment-record.sh" "$@" ;;
creditnote) exec "${SKILLS}/dolibarr-sandbox-write/scripts/creditnote-create.sh" "$@" ;;
accounts) exec "${SKILLS}/dolibarr-sandbox-write/scripts/bank-accounts.sh" "$@" ;;
write) exec "${SKILLS}/dolibarr-sandbox-write/scripts/dol-write.sh" "$@" ;;
help|-h|--help)
cat <<'EOF'

View File

@@ -38,6 +38,7 @@ import userSetup from "./scripts/admin/userSetup.ts";
societe contact: lire=281, creer=282
fournisseur: lire=1181, facture lire=1231, facture creer=1232
produit: lire=31, creer=32
banque: lire=111
*/
const WRITE_IDS = [
11, // facture lire
@@ -54,6 +55,7 @@ const WRITE_IDS = [
1232, // fournisseur facture creer
31, // produit lire
32, // produit creer
111, // banque lire — list bank accounts (GET /bankaccounts) to pick account_id
];
const KEY_FILE = ".ai_agent_sandbox.key";