# Migration Guide - Supplier Invoice Enhancements (v2.0.0)

## 🎯 Hvad migreres:

### Database Changes:
- ✅ `supplier_invoice_lines`: Nye kolonner (contra_account, line_purpose, resale_customer_id, resale_order_number)
- ✅ `economic_accounts`: Ny tabel til e-conomic kontoplan cache

### Backend Changes:
- ✅ e-conomic accounts API integration
- ✅ Line item update endpoint med modkonto support

### Frontend Changes:
- ✅ 3 nye faneblade (Til Betaling, Klar til Bogføring, Varelinjer)
- ✅ Inline redigering af modkonto og formål
- ✅ Backup version på /billing/supplier-invoices2

---

## 📋 Pre-Migration Checklist:

- [ ] Commit alle ændringer til git
- [ ] Test på lokal udvikling fungerer
- [ ] Backup af production database
- [ ] Tag ny version (v2.0.0)
- [ ] Push til Gitea

---

## 🚀 Migration Steps:

### Step 1: Commit og Tag Release

```bash
cd /Users/christianthomas/DEV/bmc_hub_dev

# Commit ændringer
git add .
git commit -m "Supplier invoice enhancements v2.0.0

- Added modkonto (contra_account) support per line
- Added line_purpose tracking (resale, internal, project, stock)
- Added e-conomic accounts API integration
- Redesigned frontend with 3 tabs: Payment, Ready for Booking, Line Items
- Database migration 1000 included
- Backup version available at /billing/supplier-invoices2"

# Opdater VERSION fil
echo "2.0.0" > VERSION

git add VERSION
git commit -m "Bump version to 2.0.0"

# Tag release
git tag v2.0.0
git push origin main
git push origin v2.0.0
```

### Step 2: Backup Production Database

```bash
# SSH til production
ssh bmcadmin@172.16.31.183

# Backup database
cd /srv/podman/bmc_hub_v1.0
podman exec bmc-hub-postgres-prod pg_dump -U bmc_hub bmc_hub > backup_pre_v2.0.0_$(date +%Y%m%d_%H%M%S).sql

# Verificer backup
ls -lh backup_pre_v2.0.0_*.sql
```

### Step 3: Deploy ny Version

Fra lokal Mac:

```bash
cd /Users/christianthomas/DEV/bmc_hub_dev

# Kør deployment script
./deploy_to_prod.sh v2.0.0
```

Dette script:
1. Opdaterer RELEASE_VERSION i .env
2. Stopper containers
3. Bygger nyt image fra Gitea tag v2.0.0
4. Starter containers igen

### Step 4: Kør Migration på Production

```bash
# SSH til production
ssh bmcadmin@172.16.31.183
cd /srv/podman/bmc_hub_v1.0

# Kør migration SQL
podman exec -i bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub < migrations/1000_supplier_invoice_enhancements.sql

# ELLER hvis migrationen ikke er mounted:
# Kopier migration til container først:
podman cp migrations/1000_supplier_invoice_enhancements.sql bmc-hub-postgres-prod:/tmp/migration.sql
podman exec bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub -f /tmp/migration.sql
```

### Step 5: Sync e-conomic Accounts

```bash
# Trigger initial sync af kontoplan
curl -X GET "http://172.16.31.183:8001/api/v1/supplier-invoices/economic/accounts?refresh=true"

# Verificer at konti er cached
curl -s "http://172.16.31.183:8001/api/v1/supplier-invoices/economic/accounts" | jq '.accounts | length'
# Skal returnere antal konti (fx 20)
```

### Step 6: Verificer Migration

```bash
# Tjek database kolonner
podman exec bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub -c "\d supplier_invoice_lines"
# Skal vise: contra_account, line_purpose, resale_customer_id, resale_order_number

# Tjek economic_accounts tabel
podman exec bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub -c "SELECT COUNT(*) FROM economic_accounts;"
# Skal returnere antal accounts (fx 20)

# Test frontend
# Åbn: http://172.16.31.183:8001/billing/supplier-invoices
# Skal vise: Til Betaling, Klar til Bogføring, Varelinjer tabs

# Test backup version
# Åbn: http://172.16.31.183:8001/billing/supplier-invoices2
# Skal vise: Original version med Fakturaer, Mangler Behandling tabs
```

---

## 🔄 Rollback Plan (hvis noget går galt):

### Option 1: Rollback til forrige version

```bash
ssh bmcadmin@172.16.31.183
cd /srv/podman/bmc_hub_v1.0

# Opdater til forrige version (fx v1.3.123)
sed -i 's/^RELEASE_VERSION=.*/RELEASE_VERSION=v1.3.123/' .env

# Rebuild og restart
podman-compose down
podman-compose build --no-cache
podman-compose up -d
```

### Option 2: Restore database backup

```bash
ssh bmcadmin@172.16.31.183
cd /srv/podman/bmc_hub_v1.0

# Stop API for at undgå data ændringer
podman stop bmc-hub-api-prod

# Restore database
podman exec -i bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub < backup_pre_v2.0.0_XXXXXXXX.sql

# Restart API
podman start bmc-hub-api-prod
```

---

## 📊 Post-Migration Validation:

### Test Cases:

1. **Upload Invoice**
   - Upload PDF faktura
   - Verificer Quick Analysis virker
   - Tjek vendor auto-match

2. **Process Invoice**
   - Klik "Behandl" på uploaded fil
   - Verificer template extraction
   - Tjek at linjer oprettes

3. **Assign Modkonto**
   - Gå til "Varelinjer" tab
   - Vælg modkonto fra dropdown (skal vise 20 konti)
   - Vælg formål (Videresalg, Internt, osv.)
   - Gem og verificer

4. **Check Ready for Booking**
   - Gå til "Klar til Bogføring" tab
   - Skal kun vise fakturaer hvor ALLE linjer har modkonto
   - Test "Send til e-conomic" knap

5. **Payment View**
   - Gå til "Til Betaling" tab
   - Verificer sortering efter forfaldsdato
   - Test bulk selection

---

## 🎯 Success Criteria:

- ✅ Migration SQL kørt uden fejl
- ✅ 20+ e-conomic accounts cached i database
- ✅ Nye faneblade vises korrekt
- ✅ Modkonto dropdown virker
- ✅ Inline editing af linjer fungerer
- ✅ Backup version tilgængelig på /supplier-invoices2
- ✅ Send til e-conomic virker med nye modkonti

---

## ⚠️ Known Issues & Workarounds:

### Issue 1: Accounts endpoint timeout
**Symptom**: Første kald til accounts endpoint er langsomt (2-3 sek)
**Reason**: Første gang syncer fra e-conomic API
**Workaround**: Pre-trigger sync efter deployment (Step 5)

### Issue 2: Eksisterende fakturaer har ingen modkonto
**Symptom**: Gamle fakturaer vises ikke i "Klar til Bogføring"
**Expected**: Kun nye fakturaer (efter migration) vil have modkonti
**Solution**: Manuel assignment via "Varelinjer" tab for gamle fakturaer hvis nødvendigt

### Issue 3: Browser cache
**Symptom**: Gamle faneblade vises stadig
**Solution**: Ctrl+Shift+R (hard refresh) i browser

---

## 📞 Support:

Ved problemer, tjek:
1. Container logs: `podman logs bmc-hub-api-prod --tail 100`
2. Database logs: `podman logs bmc-hub-postgres-prod --tail 100`
3. Migration status: `podman exec bmc-hub-postgres-prod psql -U bmc_hub -d bmc_hub -c "SELECT * FROM economic_accounts LIMIT 5;"`

---

**Version**: 2.0.0
**Date**: 2026-01-07
**Migration File**: 1000_supplier_invoice_enhancements.sql
