didi-website-erp-crm/erp_crm/seed-data.sh

152 lines
5.2 KiB
Bash
Executable file

#!/usr/bin/env bash
# Seed date de test pentru smoke testing
# Creeaza: 1 Customer, 3 Sales Invoices (paid), 1 Payment Log, 1 Service Agreement, 1 Lead
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
source .env 2>/dev/null || true
ERP_URL="${ERP_URL:-http://localhost:${ERPNEXT_PORT:-8080}}"
ADMIN_PWD="${ADMIN_PASSWORD:-admin}"
log() { printf '\033[1;36m[seed]\033[0m %s\n' "$*"; }
# Login si get cookies
COOKIES=$(mktemp)
trap "rm -f $COOKIES" EXIT
curl -sf -c "$COOKIES" -X POST "$ERP_URL/api/method/login" \
-d "usr=Administrator&pwd=$ADMIN_PWD" >/dev/null || {
echo "Login esuat. Verifica ca ERPNext ruleaza la $ERP_URL si parola e $ADMIN_PWD"
exit 1
}
# Helper
erp() {
local method="$1" path="$2" body="${3:-}"
if [ -n "$body" ]; then
curl -sf -b "$COOKIES" -X "$method" "$ERP_URL$path" \
-H "Content-Type: application/json" -d "$body"
else
curl -sf -b "$COOKIES" -X "$method" "$ERP_URL$path"
fi
}
KEYCLOAK_ID="${KEYCLOAK_ID:-14142351-ad1e-466b-ac5d-4a7a0ff562bf}"
# ── 1. Customer Test ────────────────────────────────────────
log "1/5 Creez Customer 'Test User'..."
if erp GET "/api/resource/Customer/Test%20User" >/dev/null 2>&1; then
log " Test User exista deja, skip"
else
erp POST "/api/resource/Customer" "$(cat <<JSON
{
"customer_name": "Test User",
"customer_type": "Individual",
"didi_user_id": "$KEYCLOAK_ID",
"email_id": "test@didi.local",
"iam_role": "paid_tier",
"active_plan": "paid"
}
JSON
)" > /dev/null && log " OK"
fi
# ── 2. Sales Invoices (3 bucati) ────────────────────────────
log "2/5 Creez 3 Sales Invoices..."
for i in 1 2 3; do
INV_JSON=$(erp POST "/api/resource/Sales%20Invoice" "$(cat <<JSON
{
"customer": "Test User",
"company": "TOP CLOSSERS SRL",
"naming_series": "DIDI-INV-.YYYY.-.#####",
"currency": "RON",
"conversion_rate": 1,
"items": [{"item_code": "DIDI-TECHNIQUES-TEXT", "qty": 1, "rate": 50}],
"taxes": [{
"charge_type": "On Net Total",
"account_head": "4427 - 4427 - TVA colectata - TC",
"description": "TVA 21% (inclus)",
"rate": 21,
"included_in_print_rate": 1
}]
}
JSON
)" 2>/dev/null) || { log " Esec creare factura #$i"; continue; }
INV_NAME=$(echo "$INV_JSON" | python3 -c "import sys,json; print(json.load(sys.stdin)['data']['name'])")
# Submit
FULL=$(erp GET "/api/resource/Sales%20Invoice/$INV_NAME")
erp POST "/api/method/frappe.client.submit" "$(echo "$FULL" | python3 -c "
import sys, json
d = json.load(sys.stdin)['data']
print(json.dumps({'doc': d}))
")" > /dev/null
log " ${INV_NAME} - Paid"
done
# ── 3. Mark all invoices as paid (cheat for demo) ───────────
log "3/5 Marchez ca platite..."
DB_NAME=$(docker exec didi-erpnext python3 -c "import json; print(json.load(open('/home/frappe/frappe-bench/sites/didi-erp/site_config.json'))['db_name'])" 2>/dev/null)
docker exec didi-mariadb mariadb -uroot -p"${MYSQL_ROOT_PASSWORD:?seteaza MYSQL_ROOT_PASSWORD in .env}" "$DB_NAME" -e "
UPDATE \`tabSales Invoice\` SET status='Paid', outstanding_amount=0 WHERE customer='Test User';
" 2>/dev/null
# ── 4. Payment Log (Stripe simulat) ─────────────────────────
log "4/5 Creez Payment Log..."
erp POST "/api/resource/Payment%20Log" "$(cat <<JSON
{
"event_type": "checkout.session.completed",
"status": "Succeeded",
"amount": 60.50,
"currency": "RON",
"stripe_session_id": "cs_test_seed_$(date +%s)",
"stripe_payment_intent_id": "pi_test_seed_$(date +%s)",
"customer": "Test User",
"raw_webhook_data": "{\"seed\": true, \"created\": \"$(date -Iseconds)\"}"
}
JSON
)" > /dev/null && log " OK"
# ── 5. Service Agreement ────────────────────────────────────
log "5/5 Creez Service Agreement..."
erp POST "/api/resource/Service%20Agreement" "$(cat <<JSON
{
"customer": "Test User",
"plan": "DIDI-TECHNIQUES-TEXT",
"status": "Accepted",
"acceptance_date": "$(date -u +%Y-%m-%d\ %H:%M:%S)",
"terms_version": "2026-v1",
"agreement_html": "<p>Acord servicii test - generat de seed-data.sh</p>"
}
JSON
)" > /dev/null && log " OK"
# ── 6. Lead (din formularul de contact) ─────────────────────
log "Bonus: Creez 1 Lead..."
erp POST "/api/resource/Lead" "$(cat <<JSON
{
"lead_name": "Prospect Demo",
"email_id": "prospect@example.com",
"company_name": "Demo Company SRL",
"source": "Website - Contact Form",
"source_form": "contact",
"page_origin": "/contact",
"status": "Lead"
}
JSON
)" > /dev/null && log " OK"
# ── Summary ─────────────────────────────────────────────────
log "============================================="
log "Date seedate:"
log " 1 Customer (Test User, paid_tier)"
log " 3 Sales Invoices (DIDI-INV-2026-xxxxx, Paid)"
log " 1 Payment Log (Stripe simulat)"
log " 1 Service Agreement"
log " 1 Lead (Prospect Demo)"
log ""
log "Verificare: http://localhost:${ERPNEXT_PORT:-8080}/app/customer"
log "============================================="