# Stock takings

Stock taking (represented by [`StockTaking`](https://developers.portos.sk/data-models#stocktaking) class) is a method to adjust stock quantities for various stock items (`Plu` of type `StockItem`). These adjustments can be made multiple times until the stock taking is marked as *closed* by providing values for the `ClosedBy` and `ClosedAt` fields. Once a stock taking is closed, it becomes read-only and cannot be reopened. To close a stock taking, `ActualQuantity` must be provided for each item element (represented by [`StockTakingItem`](https://developers.portos.sk/data-models#stocktakingitem) class).

After a stock taking is closed, the API server automatically generates a new stock transfer (represented by [`StockTransfer`](https://developers.portos.sk/data-models#stocktransfer) class) with the type `correction`. This stock transfer is used to execute the necessary stock quantity corrections for the items involved in the stock taking process.

## API methods

## Get stock takings

<mark style="color:blue;">`GET`</mark> `http://{server-address}/stocktakings`

Returns all stocks that matches query parameters. Result is of type [`QueryResult<StockTaking>`](https://developers.portos.sk/data-models#queryresult).

#### Query Parameters

| Name              | Type      | Description                                                                                                                   |
| ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------- |
| id                | string    | Supports NData syntax.                                                                                                        |
| $skip             | int       | Pagination property. Describing number of records to skip from beginning of result collection.                                |
| $take             | int       | Pagination property. Maximum count of items to be returned.                                                                   |
| $sort             | string\[] | <p>Comma separated list of property names. Please see NData syntax.<br></p>                                                   |
| $count            | bool      | If set to `true`, response will not contain `items` collection. Use to get resource count, not the resources themselves.      |
| createdAt         | DateTime  | Supports NData syntax.                                                                                                        |
| createdByUserName | string    | Only stock takings containing item with matching StockTaking.CreatedBy.UserName property are returned. Supports NData syntax. |
| closedAt          | DateTime  | Supports NData syntax.                                                                                                        |
| closedByUserName  | string    | Only stock takings containing item with matching StockTaking.ClosedBy.UserName property are returned. Supports NData syntax.  |
| currencyLabel     | string    | Supports NData syntax.                                                                                                        |
| itemPluStockName  | string    | Only stock takings containing item with matching StockTakingItem.Plu.StockName property are returned. Supports NData syntax.  |
| itemPluCode       | int       | Only stock takings containing item with matching StockTakingItem.Plu.Code property are returned. Supports NData syntax.       |

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

{% tabs %}
{% tab title="200: OK Successful response" %}

```javascript
{
    "items":
    [
        {
            "id": "64cfa2153a6484c761208b8c",
            "createdAt": "2023-08-06T13:37:25.728Z",
            "closedAt": null,
            "createdBy":
            {
                "name": "Majiteľ",
                "userName": "999",
                "featureName": null
            },
            "closedBy": null,
            "currencyLabel": "EUR",
            "items":
            [
                {
                    "expectedQuantity":
                    {
                        "amount": 15.0,
                        "unit": "ks"
                    },
                    "actualQuantity":
                    {
                        "amount": 12.0,
                        "unit": "ks"
                    },
                    "plu":
                    {
                        "name": "Coca Cola 0,33l",
                        "code": 5,
                        "codes":
                        [],
                        "stockName": "S01",
                        "articleCategoryLabel": "DRI",
                        "vat":
                        {
                            "category": 1,
                            "rate": 20.0
                        },
                        "unitPurchasePrice":
                        {
                            "amount": 0.0,
                            "currencyLabel": "EUR"
                        },
                        "unitRetailPrice":
                        {
                            "amount": 3.491667,
                            "currencyLabel": "EUR"
                        }
                    }
                },
                {
                    "expectedQuantity":
                    {
                        "amount": 6.0,
                        "unit": "ks"
                    },
                    "actualQuantity":
                    {
                        "amount": 5.0,
                        "unit": "ks"
                    },
                    "plu":
                    {
                        "name": "Fanta 0.33l",
                        "code": 6,
                        "codes":
                        [],
                        "stockName": "S01",
                        "articleCategoryLabel": "DRI",
                        "vat":
                        {
                            "category": 1,
                            "rate": 20.0
                        },
                        "unitPurchasePrice":
                        {
                            "amount": 0.0,
                            "currencyLabel": "EUR"
                        },
                        "unitRetailPrice":
                        {
                            "amount": 3.491667,
                            "currencyLabel": "EUR"
                        }
                    }
                },
                {
                    "expectedQuantity":
                    {
                        "amount": 11.0,
                        "unit": "ks"
                    },
                    "actualQuantity": null,
                    "plu":
                    {
                        "name": "Sprite 0.33l",
                        "code": 7,
                        "codes":
                        [],
                        "stockName": "S01",
                        "articleCategoryLabel": "DRI",
                        "vat":
                        {
                            "category": 1,
                            "rate": 20.0
                        },
                        "unitPurchasePrice":
                        {
                            "amount": 0.0,
                            "currencyLabel": "EUR"
                        },
                        "unitRetailPrice":
                        {
                            "amount": 3.491667,
                            "currencyLabel": "EUR"
                        }
                    }
                }
            ],
            "_v": 1
        }
    ],
    "count": 1,
    "totalCount": 1
}
```

{% endtab %}
{% endtabs %}

## Get stock taking

<mark style="color:blue;">`GET`</mark> `http://{server-address}/stocktakings/{id}`

Result is of type [StockTaking](https://developers.portos.sk/data-models#stocktaking).

#### Path Parameters

| Name                                 | Type   | Description        |
| ------------------------------------ | ------ | ------------------ |
| id<mark style="color:red;">\*</mark> | string | Unique identifier. |

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

{% tabs %}
{% tab title="200: OK Resource found." %}

```javascript
{
    "id": "64cfa2153a6484c761208b8c",
    "createdAt": "2023-08-06T13:37:25.728Z",
    "closedAt": null,
    "createdBy":
    {
        "name": "Majiteľ",
        "userName": "999",
        "featureName": null
    },
    "closedBy": null,
    "currencyLabel": "EUR",
    "items":
    [
        {
            "expectedQuantity":
            {
                "amount": 15.0,
                "unit": "ks"
            },
            "actualQuantity":
            {
                "amount": 12.0,
                "unit": "ks"
            },
            "plu":
            {
                "name": "Coca Cola 0,33l",
                "code": 5,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        },
        {
            "expectedQuantity":
            {
                "amount": 6.0,
                "unit": "ks"
            },
            "actualQuantity":
            {
                "amount": 5.0,
                "unit": "ks"
            },
            "plu":
            {
                "name": "Fanta 0.33l",
                "code": 6,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        },
        {
            "expectedQuantity":
            {
                "amount": 11.0,
                "unit": "ks"
            },
            "actualQuantity": null,
            "plu":
            {
                "name": "Sprite 0.33l",
                "code": 7,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        }
    ],
    "_v": 1
}
```

{% endtab %}

{% tab title="404: Not Found Resource not found." %}

```javascript
{
    "title": "Zdroj nebol nájdený",
    "status": 404
}
```

{% endtab %}
{% endtabs %}

## Create stock taking

<mark style="color:green;">`POST`</mark> `http://{server-address}/stocktakings`

Creates new stock taking. Result is of type [StockTaking](https://developers.portos.sk/data-models#stocktaking).

**Required rights**

Authorized user must have `StockTakingCreate` [right ](https://developers.portos.sk/user-rights)assigned. If stock taking is marked as closed, `StockTakingClose` [right ](https://developers.portos.sk/user-rights)is required as well.

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

#### Request Body

| Name                                     | Type        | Description     |
| ---------------------------------------- | ----------- | --------------- |
| (body)<mark style="color:red;">\*</mark> | StockTaking | Model to create |

{% tabs %}
{% tab title="201: Created Successfuly created." %}

```javascript
{
    "id": "64cfa2153a6484c761208b8c",
    "createdAt": "2023-08-06T13:37:25.728Z",
    "closedAt": null,
    "createdBy":
    {
        "name": "Majiteľ",
        "userName": "999",
        "featureName": null
    },
    "closedBy": null,
    "currencyLabel": "EUR",
    "items":
    [
        {
            "expectedQuantity":
            {
                "amount": 15.0,
                "unit": "ks"
            },
            "actualQuantity":
            {
                "amount": 12.0,
                "unit": "ks"
            },
            "plu":
            {
                "name": "Coca Cola 0,33l",
                "code": 5,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        },
        {
            "expectedQuantity":
            {
                "amount": 6.0,
                "unit": "ks"
            },
            "actualQuantity":
            {
                "amount": 5.0,
                "unit": "ks"
            },
            "plu":
            {
                "name": "Fanta 0.33l",
                "code": 6,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        },
        {
            "expectedQuantity":
            {
                "amount": 11.0,
                "unit": "ks"
            },
            "actualQuantity": null,
            "plu":
            {
                "name": "Sprite 0.33l",
                "code": 7,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        }
    ],
    "_v": 1
}
```

{% endtab %}
{% endtabs %}

## Create or update stock taking

<mark style="color:orange;">`PUT`</mark> `http://{server-address}/stocktakings/{id}`

Updates existing or creates new stock taking. Result is of type [StockTaking](https://developers.portos.sk/data-models#stocktaking).

**Required rights**

Authorized user must have assigned `StockTakingCreate` or `StockTakingUpdate` [right](https://developers.portos.sk/user-rights).  If stock taking is marked as closed, `StockTakingClose` [right ](https://developers.portos.sk/user-rights)is required as well.

Only stock takings not marked as closed can be updated.

#### Path Parameters

| Name                                 | Type   | Description        |
| ------------------------------------ | ------ | ------------------ |
| id<mark style="color:red;">\*</mark> | string | Unique identifier. |

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

#### Request Body

| Name                                     | Type        | Description                |
| ---------------------------------------- | ----------- | -------------------------- |
| (body)<mark style="color:red;">\*</mark> | StockTaking | Model to create or update. |

{% tabs %}
{% tab title="200: OK Successfuly created or updated." %}

```javascript
{
    "id": "64cfa2153a6484c761208b8c",
    "createdAt": "2023-08-06T13:37:25.728Z",
    "closedAt": null,
    "createdBy":
    {
        "name": "Majiteľ",
        "userName": "999",
        "featureName": null
    },
    "closedBy": null,
    "currencyLabel": "EUR",
    "items":
    [
        {
            "expectedQuantity":
            {
                "amount": 15.0,
                "unit": "ks"
            },
            "actualQuantity":
            {
                "amount": 12.0,
                "unit": "ks"
            },
            "plu":
            {
                "name": "Coca Cola 0,33l",
                "code": 5,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        },
        {
            "expectedQuantity":
            {
                "amount": 6.0,
                "unit": "ks"
            },
            "actualQuantity":
            {
                "amount": 5.0,
                "unit": "ks"
            },
            "plu":
            {
                "name": "Fanta 0.33l",
                "code": 6,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        },
        {
            "expectedQuantity":
            {
                "amount": 11.0,
                "unit": "ks"
            },
            "actualQuantity": null,
            "plu":
            {
                "name": "Sprite 0.33l",
                "code": 7,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        }
    ],
    "_v": 1
}
```

{% endtab %}
{% endtabs %}

## Delete stock taking

<mark style="color:red;">`DELETE`</mark> `http://{server-address}/stocktakings/{id}`

**Required rights**

Authorized user must have `StockTakingDelete` [right ](https://developers.portos.sk/user-rights)assigned.

Only stock takings not marked as closed can be deleted.

#### Path Parameters

| Name                                 | Type   | Description        |
| ------------------------------------ | ------ | ------------------ |
| id<mark style="color:red;">\*</mark> | string | Unique identifier. |

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

{% tabs %}
{% tab title="200: OK Successfuly deleted." %}

```javascript
{
    "id": "64cfa2153a6484c761208b8c",
    "createdAt": "2023-08-06T13:37:25.728Z",
    "closedAt": null,
    "createdBy":
    {
        "name": "Majiteľ",
        "userName": "999",
        "featureName": null
    },
    "closedBy": null,
    "currencyLabel": "EUR",
    "items":
    [
        {
            "expectedQuantity":
            {
                "amount": 15.0,
                "unit": "ks"
            },
            "actualQuantity":
            {
                "amount": 12.0,
                "unit": "ks"
            },
            "plu":
            {
                "name": "Coca Cola 0,33l",
                "code": 5,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        },
        {
            "expectedQuantity":
            {
                "amount": 6.0,
                "unit": "ks"
            },
            "actualQuantity":
            {
                "amount": 5.0,
                "unit": "ks"
            },
            "plu":
            {
                "name": "Fanta 0.33l",
                "code": 6,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        },
        {
            "expectedQuantity":
            {
                "amount": 11.0,
                "unit": "ks"
            },
            "actualQuantity": null,
            "plu":
            {
                "name": "Sprite 0.33l",
                "code": 7,
                "codes":
                [],
                "stockName": "S01",
                "articleCategoryLabel": "DRI",
                "vat":
                {
                    "category": 1,
                    "rate": 20.0
                },
                "unitPurchasePrice":
                {
                    "amount": 0.0,
                    "currencyLabel": "EUR"
                },
                "unitRetailPrice":
                {
                    "amount": 3.491667,
                    "currencyLabel": "EUR"
                }
            }
        }
    ],
    "_v": 1
}
```

{% endtab %}

{% tab title="404: Not Found Given resource was not found." %}

```javascript
{
    "title": "Zdroj nebol nájdený",
    "status": 404
}
```

{% endtab %}
{% endtabs %}
