#!/usr/bin/env bash # Create a client and/or supplier thirdparty (fiche tiers) in the SANDBOX. # # Input: a JSON object on stdin (or a file path in $1). Fields: # name (required) # role "client" | "supplier" | "both" (default "client") # country_id numeric, default 1 (France) # client_code / supplier_code default "-1" = auto-generate via the code mask # siret, tva_intra, address, zip, town, email, phone, idprof1 (optional) # # Emits the new thirdparty id on stdout. All writes go through dol-write.sh, # which refuses any host that is not the sandbox. # # Examples: # echo '{"name":"KissMetrics","role":"client","tva_intra":"US.."}' | thirdparty-create.sh # thirdparty-create.sh fournisseur.json set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" W="${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 d = json.loads(sys.stdin.read()) if not d.get("name"): sys.exit("thirdparty-create.sh: 'name' is required") role = d.get("role", "client").lower() is_client = role in ("client", "both") is_supp = role in ("supplier", "fournisseur", "both") body = { "name": d["name"], "client": "1" if is_client else "0", "fournisseur": "1" if is_supp else "0", "country_id": str(d.get("country_id", 1)), # "-1" => Dolibarr auto-assigns the next code from the mask # (COMPANY_ELEPHANT_MASK_CUSTOMER / _SUPPLIER); "0" when that role is off. "code_client": (d.get("client_code", "-1") if is_client else "0"), "code_fournisseur": (d.get("supplier_code", "-1") if is_supp else "0"), } for k in ("siret", "tva_intra", "address", "zip", "town", "email", "phone", "idprof1"): if d.get(k): body[k] = d[k] print(json.dumps(body)) PY BODY="$(printf '%s' "${INPUT}" | python3 "${PYF}")" "${W}" POST /thirdparties "${BODY}"