> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arcuserp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Set opening on-hand for a (product, variant, location) -- REPLACE semantics, migration-only

> NEW-GAP-MIGRATION-PHASE-4-NOT-IDEMPOTENT-P0 (2026-05-18). Canonical
REPLACE-NOT-ADD opening-balance writer for one-shot migration loads.

Routes to `utils/inventory-helpers.mjs::setOpeningStock`, which sets
`inventory_balances.on_hand = target_on_hand` directly (NOT add a
delta). Re-running with the same input is a no-op (idempotent);
re-running with a different target replaces the prior opening row
+ FIFO layer + balance. Re-running after the layer has been sold
/ transferred refuses with 409 `opening_stock_already_consumed`.

Scope: **migration:write only**. Live inventory edits MUST use
`POST /v1/inventory/adjustments` (delta semantics).

GL: NO journal entry posted. Opening Inventory Asset balance lands
via `gl_accounts.opening_balance` column write in Phase 5B of the
WSS migration. Posting a JE here would double-count.

Idempotency: `Idempotency-Key` header honored for double-click
protection. Payload-equality short-circuit inside setOpeningStock:
a re-run with the same `target_on_hand` + `unit_cost` returns
HTTP 200 + `{ idempotent: true, action: 'noop' }` without any
DB write. A re-run with a different target returns HTTP 201 +
`{ idempotent: false, action: 'replaced' }`.




## OpenAPI

````yaml /openapi.yaml post /inventory/opening-stock
openapi: 3.1.0
info:
  title: Arcus ERP Public API
  version: 1.0.0
  description: >
    Arcus ERP public REST API. Designed for external integrations and data
    migration.


    **Authentication.** Bearer token (API key) via the `Authorization` header.

    Format: `Authorization: Bearer ark_live_ent_<code>_<random>` (or
    `ark_test_*` for sandbox).

    API keys are issued per-entity in **Settings > Developers > API Keys**.


    **Entity scoping.** The entity is encoded in the API key prefix; routes are
    flat

    (e.g. `/v1/accounts`, `/v1/orders`, `/v1/products`). A small set of platform
    endpoints

    (migration, reconciliation, events, webhook endpoints, API keys) use the

    `/v1/entities/{entity_id}/...` form -- those are noted in their tags.


    **Key capabilities.**
      - Related-resource hydration via `?expand[]=` (see `x-arcus-expand` on each resource).
      - Cursor-based pagination (`starting_after` / `ending_before` / `limit`).
      - Idempotency via the `Idempotency-Key` header.
      - Webhook events for asynchronous notification.
      - Conditional requests / ETag for cache validation.
servers:
  - url: https://api.arcuserp.com/v1
    description: Arcus ERP API (accepts both live `ark_live_*` and test `ark_test_*` keys)
  - url: https://dev-api.arcuserp.com/v1
    description: >-
      Dev sandbox API (test-only data, accepts `ark_test_*` keys against dev
      RDS)
security: []
paths:
  /inventory/opening-stock:
    post:
      tags:
        - Inventory
        - Migration
      summary: >-
        Set opening on-hand for a (product, variant, location) -- REPLACE
        semantics, migration-only
      description: |
        NEW-GAP-MIGRATION-PHASE-4-NOT-IDEMPOTENT-P0 (2026-05-18). Canonical
        REPLACE-NOT-ADD opening-balance writer for one-shot migration loads.

        Routes to `utils/inventory-helpers.mjs::setOpeningStock`, which sets
        `inventory_balances.on_hand = target_on_hand` directly (NOT add a
        delta). Re-running with the same input is a no-op (idempotent);
        re-running with a different target replaces the prior opening row
        + FIFO layer + balance. Re-running after the layer has been sold
        / transferred refuses with 409 `opening_stock_already_consumed`.

        Scope: **migration:write only**. Live inventory edits MUST use
        `POST /v1/inventory/adjustments` (delta semantics).

        GL: NO journal entry posted. Opening Inventory Asset balance lands
        via `gl_accounts.opening_balance` column write in Phase 5B of the
        WSS migration. Posting a JE here would double-count.

        Idempotency: `Idempotency-Key` header honored for double-click
        protection. Payload-equality short-circuit inside setOpeningStock:
        a re-run with the same `target_on_hand` + `unit_cost` returns
        HTTP 200 + `{ idempotent: true, action: 'noop' }` without any
        DB write. A re-run with a different target returns HTTP 201 +
        `{ idempotent: false, action: 'replaced' }`.
      operationId: postInventoryOpeningStock
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - product_id
                - location_id
                - target_on_hand
              properties:
                product_id:
                  type: string
                  format: uuid
                  description: Arcus product UUID.
                location_id:
                  type: string
                  format: uuid
                  description: Arcus location UUID.
                variant_id:
                  type: string
                  format: uuid
                  nullable: true
                  description: >-
                    Optional product_variants UUID; null for non-variant
                    products.
                target_on_hand:
                  type: integer
                  minimum: 0
                  description: >
                    The TARGET on-hand value (not a delta). setOpeningStock
                    writes

                    `inventory_balances.on_hand = target_on_hand` directly.
                unit_cost:
                  type: number
                  format: float
                  minimum: 0.0001
                  description: >
                    Per-unit cost. Required when target_on_hand > 0; ignored
                    when

                    target_on_hand = 0. setOpeningStock refuses positive opening

                    qty without a positive unit_cost (would corrupt FIFO + GL

                    opening Inventory Asset balance).
                external_id:
                  type: string
                  maxLength: 80
                  description: >
                    Optional source-system row identifier (e.g., Versa snapshot
                    id).

                    Stored on `inventory_transactions.reference_number` for
                    audit

                    traceability. Idempotency keys on (entity, product,
                    location,

                    reference_type='opening_balance'), NOT on external_id;
                    external_id

                    is informational only.
                notes:
                  type: string
                  description: >-
                    Optional free-text source memo. Stored on
                    `inventory_transactions.notes`.
            examples:
              fresh_opening:
                summary: Fresh opening stock (no prior opening row)
                value:
                  product_id: 7f6f3e14-eaad-4c45-b922-209f6dc6d140
                  location_id: 91aa7588-4dc7-4edd-b172-7de91f5e1f78
                  target_on_hand: 144
                  unit_cost: 44
                  external_id: VERSA-SNAPSHOT-99127
                  notes: Opening stock from Versa snapshot (Dayton facility)
              idempotent_rerun:
                summary: Re-run with same target (idempotent no-op)
                description: |
                  Identical payload to a prior call. Returns HTTP 200 +
                  { idempotent: true, action: 'noop' }. No DB writes.
                value:
                  product_id: 7f6f3e14-eaad-4c45-b922-209f6dc6d140
                  location_id: 91aa7588-4dc7-4edd-b172-7de91f5e1f78
                  target_on_hand: 144
                  unit_cost: 44
                  external_id: VERSA-SNAPSHOT-99127
      responses:
        '200':
          description: |
            Idempotent no-op. A prior opening row exists at this
            (entity, product, variant, location) with the same target_on_hand
            and unit_cost. No DB write occurred. Returns the existing balance
            + transaction.
          content:
            application/json:
              schema:
                type: object
                properties:
                  object:
                    type: string
                    enum:
                      - inventory_opening_stock_result
                  action:
                    type: string
                    enum:
                      - noop
                  idempotent:
                    type: boolean
                  balance:
                    type: object
                  transaction:
                    type: object
                  fifo_layer:
                    type: object
                    nullable: true
        '201':
          description: |
            Created (no prior opening row) OR replaced (prior opening row
            with different target). The opening txn + FIFO layer +
            balance now reflect target_on_hand exactly.
          content:
            application/json:
              schema:
                type: object
                properties:
                  object:
                    type: string
                    enum:
                      - inventory_opening_stock_result
                  action:
                    type: string
                    enum:
                      - created
                      - replaced
                  idempotent:
                    type: boolean
                  balance:
                    type: object
                  transaction:
                    type: object
                  fifo_layer:
                    type: object
                    nullable: true
        '400':
          $ref: '#/components/responses/Error'
        '401':
          $ref: '#/components/responses/Error'
        '403':
          $ref: '#/components/responses/Error'
        '409':
          description: |
            Opening stock at this (entity, product, variant, location) has
            already been consumed (sold / transferred). setOpeningStock
            refuses to mutate a layer that's no longer the live opening.
            Reconcile via POST /v1/inventory/adjustments deltas instead.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    enum:
                      - opening_stock_already_consumed
                  code:
                    type: string
                    enum:
                      - opening_stock_already_consumed
                  hint:
                    type: string
                  consumed_qty:
                    type: integer
                  opening_qty:
                    type: integer
      security:
        - ApiKeyAuth: []
components:
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      description: |
        Client-generated unique key for idempotent POST/PATCH/DELETE operations.
        Alias for the Idempotency parameter. Max 255 chars. On retry with the
        same key, the original response is returned without re-executing the
        operation. Keys expire after 24 hours.
      schema:
        type: string
        maxLength: 255
  responses:
    Error:
      description: Error response (400/401/403/404/409/422/429/500)
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorEnvelope'
  schemas:
    ErrorEnvelope:
      type: object
      description: |
        Canonical error response envelope. All API errors use this shape.
      required:
        - error
        - code
      properties:
        error:
          type: string
          description: Machine-readable error key
          example: not_found
        code:
          type: string
          description: Machine-readable error code (often same as error)
          example: not_found
        type:
          type: string
          enum:
            - validation_error
            - permission_error
            - not_found
            - conflict
            - rate_limit
            - internal
            - expand_error
            - not_implemented
          example: not_found
        hint:
          type: string
          description: Human-readable one-sentence explanation (English)
          example: >-
            The requested order does not exist or does not belong to this
            entity.
        param:
          type: string
          description: The parameter that caused the error, if applicable
          example: expand[0]
        required:
          type: string
          description: The scope required (only on insufficient_scope errors)
          example: accounts:read
        request_id:
          type: string
          description: >-
            Unique request ID for support tracing (maps to CloudWatch log
            stream)
          example: req_abc123
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      description: |
        API key issued per entity via Settings > Developers > API Keys.
        Each key carries scopes (e.g. orders:read, products:write).
        Bearer token format: Authorization: Bearer ark_live_ent_<code>_<random>
        Test keys use ark_test_ent_<code>_<random>. Both are issued per entity
        via Settings > Developers > API Keys.

````