# Westech R2 Deployment — 2026-05-19

## What we're deploying
- **Serial No warehousing pricing + grading** (High/Med/Low/Flagged)
- **CPU/RAM test gates** (Fail → auto Flagged + Dismantle)
- **eBay scraper fix** (post-2024 s-card HTML parser)
- **Schema Diagram** shortcut in Westech workspace
- **Intake button removal** from EIM dashboard

## Pre-deploy (do this first)

```bash
# SSH into production
ssh -o ProxyCommand="cloudflared access ssh --hostname ssh.advante.ch" vagrant@ssh.advante.ch

# Fresh backup (already automated but take one now)
sudo bash /opt/backups/backup-eim.sh
sudo -u frappe bash /opt/backups/backup-mariadb.sh

# Verify backups exist
ls -la /opt/backups/eim/eim_$(date +%Y%m%d)*.db.xz
ls -la /opt/backups/mariadb/erpnext_$(date +%Y%m%d)*.sql.xz
```

## Step 1: Pull code

```bash
cd /home/frappe/erpnext-bench/apps/westech_r2
sudo -u frappe git pull origin main

cd /opt/eim/app
sudo -u vagrant git pull origin main
```

## Step 2: Migrate ERPNext schema

```bash
cd /home/frappe/erpnext-bench
sudo -u frappe bash -c 'source env/bin/activate && python3 -c "
import frappe
frappe.init(site=\"erpnext.local\", sites_path=\"/home/frappe/erpnext-bench/sites\")
frappe.connect()
from frappe.migrate import migrate
migrate()
frappe.destroy()
"'
```

This creates the new fields: `grade`, `cpu_test`, `ram_test`, `assigned_price`, `pricing_status`, etc.

## Step 3: Run data migration

**CRITICAL — do this immediately after migrate finishes.**

Maps `Device Condition Report.cosmetic_grade` → new `Serial No.grade`:
```bash
cd /home/frappe/erpnext-bench
sudo -u frappe bash -c 'source env/bin/activate && python3 /home/frappe/erpnext-bench/apps/westech_r2/westech_r2/migrations/migrate_serial_grades.py'
```

Expected output:
```
Total Serial Nos: ~43,000
High: ~C5-C9 count
Med: ~C4 count
Low: ~C3 count
Flagged: everything else
```

## Step 4: Restart services

```bash
# Restart ERPNext
sudo supervisorctl restart frappe-bench-web: frappe-bench-worker:

# Restart EIM
cd /opt/eim/app && sudo systemctl restart eim.service
```

## Step 5: Verify

1. Open any Serial No in ERPNext
2. Confirm `Grade` dropdown shows: High / Med / Low / Flagged
3. Set grade to High → `Recommended Price` should populate from Item market data
4. Set `CPU Test` to Fail → grade should auto-lock to Flagged, status = Dismantle
5. Westech workspace should show "Schema Diagram" shortcut
6. EIM dashboard should NOT show "Intake Station" button

## Rollback (if needed)

```bash
# Stop services
sudo supervisorctl stop frappe-bench-web: frappe-bench-worker:
sudo systemctl stop eim.service

# Restore MariaDB
sudo -u frappe bash -c 'cd /opt/backups/mariadb && xz -dc erpnext_YYYYMMDD_HHMMSS.sql.xz | mysql -u root -p erpnext_db'

# Restore EIM DB
sudo sqlite3 /opt/eim/eim.db ".restore /opt/backups/eim/eim_YYYYMMDD_HHMMSS.db.xz"

# Revert code
cd /home/frappe/erpnext-bench/apps/westech_r2 && sudo -u frappe git reset --hard HEAD~7
cd /opt/eim/app && sudo -u vagrant git reset --hard HEAD~1

# Restart
sudo supervisorctl start frappe-bench-web: frappe-bench-worker:
sudo systemctl start eim.service
```

## If shit breaks

- **"Grade cannot be X" errors**: Migration script didn't run. Run it.
- **No prices showing**: eBay Pricing Settings missing Oxylabs creds. Check site_config.json.
- **Fields missing**: `bench migrate` failed. Check logs: `tail -f /home/frappe/erpnext-bench/logs/migrate.log`

## Contact
If anything goes wrong and you can't fix it: stop, do NOT touch production further, and get help.
