# Routing Import Guide

## Overview

`admin/import_routing.php` provides two independent workflows:
1. **Create missing product revisions** — clone an existing rev to create a missing one
2. **Import routing CSV** — bulk-import routing operations per product

Run workflow 1 first if your CSV contains refs that do not yet exist in Dolibarr.

---

## Workflow 1 — Create missing product revisions

### When to use
Your CSV has e.g. `SF1116-01` but Dolibarr only has `SF1116-00`. Instead of skipping those rows, clone the existing revision and rename it.

### Steps

1. Go to **Production Rules → Admin → Routing CSV Import**
2. Scroll to the **"Create Missing Product Revisions"** section at the bottom
3. Upload the routing CSV and click **"Scan for missing products"**
4. Review the results table:
   - **CSV ref (missing)** — ref from CSV not found in DB
   - **Clone source** — existing revision that will be used as source
   - **Warning block** — refs with no source revision at all (must be created manually in Dolibarr)
5. Click the red **"Create N missing revision(s)"** button to confirm
6. Check the **Creation Summary** table for results

### What gets copied from the source product

| Category | Fields |
|---|---|
| Product attributes | type, label, description, prices, VAT, tax codes, dimensions, weights, units, accountancy codes, default workstation |
| Extrafields | `material_type`, `qty_per_hour_form`, `qty_per_hour_laser`, `qty_per_hour_cnc`, `qty_per_sheet`, `form_tool`, `trim_tool`, `customer`, `drawing` |

**Not copied:** barcode (must be unique), stock quantities (PMP/FIFO/stock), `fk_default_bom`, `part_issue`, `deviation_from_drawing`, batch/supplier references.

### Notes
- Operation is safe to re-run — already existing refs are skipped automatically
- Each product is created in its own DB transaction (partial failures do not roll back successful creates)
- After creation, run Workflow 2 — refs will now resolve exactly (no fallback needed)

---

## Workflow 2 — Import routing CSV

### CSV format

```
part_no,op_no,op_description,routing_instructions,inspection_notes,sfdc_op_notes,wc,sub_contract,subcon_code
SF1090-00,10,TOOL CALL-OFF,,,,,,
SF1090-00,20,MATERIAL CALL-OFF,,,,,,
SF1090-00,60,FORM - FM4,,,,FM4,,
FG1090-00,10,ISSUE MATERIAL,,,,,,
FG1090-00,20,FINAL INSPECTION,,,,INSPECTION,,
```

| Column | Required | Notes |
|---|---|---|
| `part_no` | Yes | Exact product ref (after Workflow 1, all refs should exist) |
| `op_no` | Yes | Integer, e.g. 10, 20, 30 |
| `op_description` | Yes | Operation label |
| `routing_instructions` | No | Free text |
| `inspection_notes` | No | Free text |
| `sfdc_op_notes` | No | Free text |
| `wc` | No | Workstation ref(s), pipe-separated: `1405\|1406` |
| `sub_contract` | No | `1` to force subcontract flag |
| `subcon_code` | No | Subcontractor product ref; auto-sets subcontract flag |

### Steps

1. Go to **Production Rules → Admin → Routing CSV Import**
2. Upload the CSV and click **Import**
3. Check the **Import Summary** table:
   - **Imported rows** — new routing rows inserted
   - **Skipped rows** — rows that failed (see Warnings log)
   - **Missing products** — refs not found even after fallback
   - **Missing workstations** — WC refs not in `llx_workstation_workstation`
   - **Ref fallbacks** — refs resolved via lower-revision fallback (run Workflow 1 to eliminate these)

### Deduplication
Rows are deduplicated by `(fk_product, op_no, label)`. Re-importing the same CSV is safe — existing rows are skipped but their workstation links are still processed (new links added, none removed).

---

## CSV file location

The source CSV is kept at:
```
custom/productionrules/routing_import.csv
```
