# Sandvik SAP cXML Integration — Phase 1 Status

## Phase 1 Scope: MINIMAL

**Orders IN, Confirmations OUT. Nothing else.**

| # | Direction | Document | Status |
|---|-----------|----------|--------|
| 1 | IN | Purchase Order (OrderRequest) | `order-sales-xml-convert.json` — fixed ControlKeys |
| 2 | OUT | Order Confirmation (ConfirmationRequest) | `order-confirmation-xml-convert.json` — done |

**NOT in phase 1**: ASN (our POs say `notAllowed`), Invoice (Sandvik WIP), Goods Receipt, OC Approval.

## Completed

### Convert files

| File | Lines | Purpose |
|------|-------|---------|
| `save/order-sales-xml-convert.json` | 1097 | OrderRequest IN — ControlKeys now mapped to fields |
| `save/order-confirmation-xml-convert.json` | 250 | ConfirmationRequest OUT — Sandvik format with ItemIn/ItemDetail, `from_static` for export |
| `save/order-ship_notice-xml-convert.json` | 160 | Phase 2 — ShipNoticeRequest, `from_static` for export |
| `save/order-invoice-xml-convert.json` | 312 | Phase 2 — InvoiceDetailRequest, `from_static` for export, SupplierPartID 4D override |

### Documentation

| File | Purpose |
|------|---------|
| `documentation/cxml.md` | Full reference: cXML structures, field mappings, Sandvik business rules, phase 1 scope |
| `documentation/cxml-file-move.md` | Transport: endpoint, Caddy, certificates, worker functions |
| `documentation/plan.md` | This file — status and scope |

### extra_fields added

**order.json** (12 fields):

- `json_data.confirm_id` (varchar 80)
- `json_data.confirm_type` (varchar 20)
- `json_data.confirm_operation` (varchar 20) — `new` or `update`
- `json_data.shipment_id` (varchar 80)
- `json_data.carrier_code` (varchar 40)
- `json_data.tracking_number` (varchar 80)
- `json_data.company_code` (varchar 20)
- `json_data.purchase_group` (varchar 20)
- `json_data.purchase_organization` (varchar 20)
- `json_data.customer_vat_id` (varchar 40)
- `json_data.supplier_vat_id` (varchar 40)

**order_row.json** (3 new fields):

- `json_data.confirmation_instruction` (varchar 20) — `allowed` / `notAllowed`
- `json_data.ship_notice_instruction` (varchar 20) — `allowed` / `notAllowed`
- `json_data.invoice_instruction` (varchar 20) — `isNotERS` / `isERS` / `notAllowed`

### Code fix: ControlKeys

`order-sales-xml-convert.json` changed from hardcoded to field mappings:

```text
Before: OCInstruction/@value = "allowed" (static)
After:  OCInstruction/@value → ordr.json_data.confirmation_instruction (dynamic)
        ASNInstruction/@value → ordr.json_data.ship_notice_instruction
        InvoiceInstruction/@value → ordr.json_data.invoice_instruction
```

Also fixed: `salesInstruction` tag renamed to `InvoiceInstruction` (correct cXML element name).

## Phase 1 Data Flow

```mermaid
graph LR
    S[Sandvik SAP S/4HANA] -->|OrderRequest| BN[SAP Business Network]
    BN -->|HTTPS POST| MG[Managed Gateway]
    MG -->|POST /rest/nc/upload/cxml| NC[NC cxml worker]
    NC -->|order-sales-xml-convert| ORD[NC Sales Order]
    ORD -->|order-confirmation-xml-convert| OC[ConfirmationRequest]
    OC -->|sendCxml + client cert| MG
    MG -->|forward| BN
    BN -->|cXML| S
```

## Old Files — Manual Cleanup

Delete these old-named files:

- `save/confirmation-sales-xml-convert.json`
- `save/ship-notice-sales-xml-convert.json`
- `save/invoice-sales-xml-convert.json`

## Real Test PO

PO 4500038285 from Sandvik Mining & Construction (26 Nov 2025):

- 2 lines: THREAD PIECE (5 EA) + LAMP BRACKET (1 EA)
- Control Keys: OC=allowed, ASN=notAllowed, Invoice=isNotERS
- Tax: NN 0%
- Payment: 75 days
- Transport: FCA
