# stockSources query


`stockSources` is a GraphQL query of the Chameleoon MAX API. Send it as `POST https://max-api.chameleoon.sk/api/v1/graphql` with your API key, see [Chameleoon MAX](/chameleoon-max).

Works out where needed goods could be taken from: each need is split over the free stock in the
bins `input.from` reaches, in the order to take it. Use it with [`stockDemand`](/chameleoon-max/query/stock-demand) to plan a
transfer advice. Nothing is moved or reserved.

- Requires the `stockCard.read` and `position.read` permissions.
- A line's `lot` also requires the `lot.read` permission.
- A warehouse in `from.warehouseIds` outside your API key's warehouse, legal-entity or country
  scope is left out, as in [`stockCards`](/chameleoon-max/query/stock-cards). `forWarehouseId` outside it, or unknown,
  returns `NOT_FOUND`.

#### How the split is worked out

- Only bins of kind `STOCK` or `RECEIVING` that are in use are taken from, narrowed by
  `from`.
- Free stock: for each row of [`stockQuants`](/chameleoon-max/query/stock-quants) whose lot is released, and not expired
  where the row's warehouse blocks expired stock, its quantity less what is reserved on that bin
  and lot. With `forWarehouseId`, the lot must also not be expired where that warehouse blocks
  expired stock.
- With `forWarehouseId`, every other warehouse first keeps what its own confirmed orders still
  miss: of a variant's free stock that `from` reaches there, the [`stockDemand`](/chameleoon-max/query/stock-demand)
  `missing` of that variant and warehouse (`waiting` less `incoming`, never below 0) is not
  offered, kept from the rows that come last in the order. So a move between warehouses never
  takes what the source warehouse's own orders wait for. The warehouse `forWarehouseId` itself
  keeps nothing back.
- The order: first expiry first (an expired lot last where the row's warehouse, or
  `forWarehouseId`, only warns), then the larger free quantity first, then bin code, bin id and
  lot. Needs name distinct variants, so no
  row is promised twice.
- At most 20 lines per need. What they do not cover is in `uncovered`, with `LINE_CAP` when
  more free stock was left and `NO_STOCK` when none was.

`input.exceptAdviceIds` names OPEN transfer advices that nobody has started, at most 20. What
they hold is free again. The rules are those of [`binRefills`](/chameleoon-max/query/bin-refills).

Each need is priced as 20 lines at 3 points each, whatever you select.

Costs 2 points plus 60 per need.

## Arguments

- `input`: [`StockSourcesInput!`](/chameleoon-max/type/stock-sources-input) · required

## Returns

[`StockSourcesResult!`](/chameleoon-max/type/stock-sources-result)

## Example

**Query**

```graphql
query stockSources($input: StockSourcesInput!) {
  stockSources(input: $input) {
    lines {
      variantId
      warehouseId
      lotId
      quantity
    }
    uncovered {
      variantId
      quantity
      reason
    }
  }
}
```

**Variables**

```json
{
  "input": {
    "needs": [
      {
        "variantId": "00000000-0000-4000-8000-000000000001",
        "quantity": "10.00"
      }
    ],
    "from": {
      "warehouseIds": [
        "00000000-0000-4000-8000-000000000001"
      ]
    }
  }
}
```

**Response**

```json
{
  "data": {
    "stockSources": {
      "lines": [
        {
          "variantId": "00000000-0000-4000-8000-000000000001",
          "warehouseId": "00000000-0000-4000-8000-000000000001",
          "lotId": "00000000-0000-4000-8000-000000000001",
          "quantity": "10.00"
        }
      ],
      "uncovered": [
        {
          "variantId": "00000000-0000-4000-8000-000000000001",
          "quantity": "10.00",
          "reason": "NO_STOCK"
        }
      ]
    }
  }
}
```

Values are illustrative: replace IDs and codes with your own before you send the call.
