# Data Migration Guide — AARJAY GDP Portal

Use this when moving existing operational data into the portal.

## Recommended order

1. Users / staff
2. Suppliers
3. Supplier licences and supplier categories
4. Customers
5. Products
6. Existing stock batches
7. Sales orders / dispatch history
8. Purchase requests / goods receiving history
9. SOPs / documents / forms
10. QMS records: deviations, CAPA, complaints, recalls
11. Training records
12. Equipment/assets and calibration
13. Accounts/tax data

## Important rules

- Do not import live stock before products and suppliers exist.
- Do not import dispatch history before customers and batches exist.
- Preserve batch number and expiry date exactly.
- Preserve original document dates and approval dates where possible.
- Keep legacy evidence files in stable upload paths.
- Reconcile imported stock quantity against physical stock count.

## CSV import staging recommendation

Use a staging database first. Import a small sample, then verify:

- Foreign keys resolve
- Lists load without PHP errors
- Detail views work
- Reports and GDP Gap Analysis still load
- Dashboard KPI counts are reasonable

## Mapping examples

### Supplier data

| Legacy field | Portal field |
|---|---|
| Supplier name | `suppliers.company_name` |
| WDA number | `suppliers.wda_licence_number` |
| Licence expiry | `suppliers.wda_expiry_date` |
| RP contact | `suppliers.responsible_person_name/email/phone` |
| Risk | `suppliers.risk_level` |

### Customer data

| Legacy field | Portal field |
|---|---|
| Customer name | `customers.customer_name` |
| Licence number | `customers.licence_number` |
| Licence expiry | `customers.licence_expiry_date` |
| Recall contact | `customers.quality_recall_email` |
| Account status | `customers.account_status` |

### Batch data

| Legacy field | Portal field |
|---|---|
| Product | `stock_batches.product_id` |
| Batch number | `stock_batches.batch_number` |
| Expiry | `stock_batches.expiry_date` |
| Available quantity | `stock_batches.quantity_available` |
| Supplier | `stock_batches.supplier_id` |

## After migration

1. Run `/system-health`.
2. Run GDP Gap Analysis snapshot.
3. Reconcile stock reports.
4. Check low-stock and expiry alerts.
5. Have RP/QA approve migration sign-off.
