# Work-order imports

Import production tickets with actual inputs, outputs, costs, and historical records.

Canonical HTML: https://developer.shelfcycle.com/guides/work-order-imports

Import production tickets as confirmed or completed work orders, with actual materials, lots, quantities, and costs kept together.

`POST /work-orders/batch` accepts 1–50 independent tickets in a body of up to 2 MiB. Each item has a unique `clientItemId` and a `payload.externalId` identifying its source ticket. A validation failure affects that ticket; other valid tickets can still save. Missing identities or repeated `clientItemId` values reject the complete request.

## Access and preparation

Use `work-orders:write` with current Work Order create **and** update permissions, with Work Orders enabled for the organization and acting person. Full-access keys include the new scope; older restricted keys need it explicitly. Formula or Repack template write access alone does not grant work-order import.

Check `GET /me`: `effectiveCapabilities.workOrders.import` reports scope and permissions, `workOrders.workOrderV2Eligible` includes availability, and `writeReadiness.workOrders.import` includes that availability check. The import checks access again, including previews. The organization needs its accounting cutover date and timezone configured before importing new tickets.

Resolve existing product, packaging, warehouse, supplier, lot, and template IDs. Each input lot must belong to that row's product. Quantities use each product's canonical quantity unit; keep packaging and capacity with the row when needed.

## Choose the work-order type

| `type` | Template |
| --- | --- |
| `FROM_SCRATCH` | Default when omitted. Omit `ruleConfigTemplateId`; the ticket uses its supplied rows without a Formula or Repack rule. |
| `REPACK` | An optional `ruleConfigTemplateId` must identify a Repack template. |
| `FORMULA` | Requires the `ruleConfigTemplateId` of a Formula template. |

Create templates separately through [Formula imports](/guides/formulas) or [Repack template imports](/guides/repack-templates). A selected template supplies its rules and process steps; the ticket supplies the actual inputs, outputs, and manual costs. Template rows and charges are not added a second time.

## Preview a ticket

This example is a confirmed ticket from scratch. Replace the fictional UUIDs with the selected records, and use the source ticket's stable external ID.

```bash
curl "$SHELFCYCLE_API_BASE_URL/work-orders/batch?dryRun=true" \
  -X POST \
  -H "Authorization: Bearer $SHELFCYCLE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "items": [{
      "clientItemId": "ticket-8101",
      "payload": {
        "externalId": "8101",
        "type": "FROM_SCRATCH",
        "status": "CONFIRMED",
        "transactionCurrencyCode": "USD",
        "warehouseId": "11111111-1111-4111-8111-111111111111",
        "inputs": [{
          "productId": "22222222-2222-4222-8222-222222222222",
          "lotId": "33333333-3333-4333-8333-333333333333",
          "quantityUOM": "100"
        }],
        "outputs": [{
          "productId": "44444444-4444-4444-8444-444444444444",
          "lotNumber": "BATCH-8101",
          "quantityUOM": "100"
        }],
        "costs": []
      }
    }]
  }'
```

Preview returns ordered `data.results` with `status: "ready"` or `"failed"` for each `clientItemId`, and saves nothing. It does not require `Idempotency-Key`. Inspect each failure and its field guidance before execution.

For execution, remove `dryRun=true` and add a stable `Idempotency-Key`, such as `source:work-orders:import-8101`. Successful results have `status: "succeeded"` and `data` containing `id`, `orderNo`, `status`, `historical`, and `resultAction: "created"` or `"existing"`. Keep returned IDs and outcomes with the source tickets. `X-RateLimit-Cost` reports the submitted item count.

## Dates, quantities, and costs

Send `status` as `CONFIRMED` or `COMPLETED`. A completed ticket requires an ISO timestamp in `completedAt`. ShelfCycle compares that timestamp's organization-calendar day with the accounting cutover day. A completed ticket strictly before cutover is historical; a ticket on or after cutover follows the normal work-order lifecycle.

Send `transactionCurrencyCode` explicitly on the header and on every cost row. Input and output rows inherit the header currency and accept no currency field. Use exact decimal strings for quantities and amounts. Cost `amount` is in cents and each cost requires `ledgerAccountId`, `description`, `amount`, and `transactionCurrencyCode`; see the live reference for allocation fields. Foreign currency on normal tickets requires multi-currency availability.

Normal tickets use ShelfCycle's inventory and output costing: omit `costUOM` on their rows, and omit `lotId` on normal outputs. Confirmed Formula tickets follow Formula quantity checks. Completed Formula actuals retain product-membership and unit-conversion checks while accepting the recorded quantities. Normal Repack tickets follow the usual Repack execution checks.

## Historical production

For a historical ticket, every input and output needs an existing `lotId` matching its `productId`, canonical `quantityUOM`, and source unit cost in `costUOM` (cents per canonical unit). Each lot needs a recorded load receipt strictly later than the ticket date. Historical imports cannot serve as that load evidence. Header and cost currencies must equal the organization's base currency.

Output unit costs must already include ticket costs allocated by the source system; those costs are not added again. Historical Repack tickets preserve recorded actuals. The saved work order is read-only and contributes dated production history while leaving current stock, inventory value, and accounting balances intact. Its costs are not repriced or matched to bills.

Lot history retains its existing reporting basis: early historical movements can produce a negative running balance, identified with the returned history guidance. The older quantity CSV can clamp such balances or omit a lot from a ticket-only window without a later receipt in that window. Use the selected report's returned population and dates when reconciling history.

## Repeat and resume safely

Keep the exact body, item order, `clientItemId` values, and `Idempotency-Key` on an uncertain execution or `503 work_orders_batch_interrupted`. An identical retry returns recorded outcomes and continues pending tickets; a completed retry reports `idempotencyStatus: "replayed"`. A changed body with the same key returns a conflict. Corrected failed tickets belong in a new request with a new key.

The source ticket's `externalId` also identifies an imported ticket across different request keys in the same organization. A repeated ticket returns the existing work order unchanged, even when the later payload requests another status or template. Complete a previously confirmed work order through its normal workflow; re-importing does not transition it.
