# Product Stock

### ProductStock Data

```json
{
    "pk": 1,
    "product": 2250,
    "stock": 46,
    "stock_list": 1,
    "unit_type": "qty",
    "extra_field":{},
    "sold_quantity_unreported": 0,
    "modified_date": "2017-01-23T13:37:31.947171Z"
}
```

In the data found in ProductStock:

* The **product** section contains the primary key (pk) of the product in Omnitrone.
* The **stock** section shows the available stock quantity.
* The **stock\_list** section contains the ID information of the stock list in Akinon.
* The **unit\_type** section indicates the unit of measure for the quantity.
* The **sold\_quantity\_unreported** section indicates the reserved stock quantity in Akinon.
* The **modified\_date** indicates the last updated date.

Tasks running under `channel_app_template.app.tasks` related to product integration.

### 1. Insert Stocks

This task retrieves the ProductStocks that need to be inserted into Akinon's sales side for the first time and sends this data to the command in `channel.commands.product_stocks.SendInsertedStocks`. This command creates the product's stock information for the sales channel.

If desired, the stock data can be enriched with the parameter values listed below.

The parameters and descriptions for the `insert_product_stocks` service in the StockService are as follows:

{% hint style="warning" %}
Parameters for the `insert_product_stocks` service in the Stock Service:

* **add\_price:** Adds product price data to the product stocks.[Adding Price Data to Product Stock Data](https://github.com/akinon/docs/blob/main/technicalguides/channel-app-template/development-steps/encoding-the-sales-channel/broken-reference/README.md)
* **add\_product\_objects:** Adds product data to the product stocks.[Adding Product Data to Product Stock Data](https://github.com/akinon/docs/blob/main/technicalguides/channel-app-template/development-steps/encoding-the-sales-channel/broken-reference/README.md)
* **is\_sync:** Indicates whether the status is obtained immediately when the stock is sent to the sales channel. It specifies whether the process is Synchronous or Asynchronous.
  {% endhint %}

**classSendInsertedStocks(integration, objects=None, batch\_request=None, \*\*kwargs)**

**get\_data()**

This function prepares the data to be sent in a request to transfer the stock information of products from Omnitron to the sales channel. The response should return a list containing ProductStock.

**validated\_data(*****data*****)**

It takes the response returned by the get\_data function as a parameter. If a validation of the product stocks to be sent to the sales channel is required, it will be used for that purpose. If no validation is needed, the provided data should be returned.

**transform\_data(data)**

It takes the response returned by the validated\_data function as a parameter. It is used if changes need to be made to the data before sending it to the sales channel. It returns the final version of the data to be sent.

**send\_request(transformed\_data)**

It takes the response returned by the transform\_data function as a parameter. This function uses the received data as the endpoint where the request will be sent to the sales channel. It should return either the response or the data that comes with the response.

{% hint style="warning" %}
In this section, since the response to be returned will be sent to the normalize\_response function, it is important to pay attention to data types when returning the data.
{% endhint %}

**normalize\_response(data,validated\_data, transformed\_data, response)**

This function is where we gather and finalize the data used to send our product stocks to the sales channel in the insert\_stocks task. The response returned by this function will be directly used in the insert\_product\_stocks function.

If the process is asynchronous, the remote\_batch\_id returned from the sales channel should be processed in the batch\_request.

```
remote_batch_id = response.get("remote_batch_request_id")
self.batch_request.remote_batch_id = remote_batch_id
return "", report, data
```

{% hint style="warning" %}
This section of the response should consist of three parts:

1. **response\_data:** This is the processed version of the data returned from the sales channel. Its type can be either a string or a list. If there is no data to be used in the returned response, an empty string is sufficient. If the returned response is to be used, it must be of type list, with each element being of type BatchRequestResponseDto.
2. **report:** These are the error reports generated while processing the response from the sales channel.
3. **data:** This is the first parameter received by our function, which is the response obtained from the get\_data function.

**Example return:**

```
return response_data, report, data
```

{% endhint %}

### 2. Update Stocks

It retrieves the ProductStocks that need to be updated on the sales side from Akinon and sends this data to the command found in `channel.commands.product_stocks.SendUpdatedStocks`. This command updates the stock information of the product in the sales channel.

If desired, the stock data can be enriched with the parameter values listed below.

The parameters and their descriptions for the `update_product_stocks` service within the StockService are as follows:

{% hint style="warning" %}
Parameters for the `insert_product_stocks` service in the Stock Service:

* **add\_price:** Adds product price data to the product stocks.[Adding Price Data to Product Stock Data](https://github.com/akinon/docs/blob/main/technicalguides/channel-app-template/development-steps/encoding-the-sales-channel/broken-reference/README.md)
* **add\_product\_objects:** Adds product data to the product stocks.[Adding Product Data to Product Stock Data](https://github.com/akinon/docs/blob/main/technicalguides/channel-app-template/development-steps/encoding-the-sales-channel/broken-reference/README.md)
* **is\_sync:** Indicates whether the status is obtained immediately when the stock is sent to the sales channel. It specifies whether the process is Synchronous or Asynchronous.
  {% endhint %}

**classSendUpdatedStocks(integration, objects=None, batch\_request=None, \*\*kwargs)**

**get\_data()**

In this function, the data to be sent in a request to transfer the updated stock information of products from Omnitron to the sales channel is prepared. The response should return a list containing ProductStock.

**validated\_data(*****data*****)**

It takes the response returned by the get\_data function as a parameter. If validation is needed on the product stocks to be sent to the sales channel, it will be used for that purpose. If no validation is required, the provided data should be returned.

**transform\_data(*****data*****)**

It takes the response returned by the validated\_data function as a parameter. It is used if changes need to be made to the data before sending it to the sales channel. It returns the final version of the data to be sent.

**send\_request(*****transformed\_data*****)**

It takes the response returned by the transform\_data function as a parameter. This function uses the received data as the endpoint where the request will be sent to the sales channel. It should return either the response or the data that comes with the response.

{% hint style="warning" %}
In this section, since the response to be returned will be sent to the normalize\_response function, it is important to pay attention to data types when returning the data.
{% endhint %}

**normalize\_response(*****data*****,&#x20;*****validated\_data*****,&#x20;*****transformed\_data*****,&#x20;*****response*****)**

This function is where we gather and finalize the data used to send our product stocks to the sales channel in the update\_prices step. The response returned by this function will be directly used in the update\_product\_stocks function.

If the process is asynchronous, the remote\_batch\_id returned from the sales channel should be processed in the batch\_request.

```
remote_batch_id = response.get("remote_batch_request_id")
self.batch_request.remote_batch_id = remote_batch_id
return "", report, data
```

{% hint style="warning" %}
The response returned in this section should consist of three parts:

1. **response\_data**: This is the processed version of the data returned from the sales channel. Its type can be either a string or a list. If there is no data to be used in the returned response, an empty string is sufficient. If the returned response is to be used, it must be of type list, with each element being of type BatchRequestResponseDto.
2. **report**: These are the error reports generated while processing the response from the sales channel.
3. **data**: This is the first parameter received by our function, which is the response obtained from the get\_data function.

**Example return:**

```
return response_data, report, data
```

{% endhint %}

### 3. Check Stocks

It retrieves asynchronously updated or created BatchRequests from Akinon, the statuses of which are unknown, and sends this data to the command found in `channel.commands.product_stocks.CheckStocks`. This command allows for checking whether the stock information of the product in the sales channel has been created or updated.

The `get_stock_batch_requests` function within the StockService is used for this purpose.

**classCheckStocks(*****integration*****,&#x20;*****objects=None*****,&#x20;*****batch\_request=None*****, kwargs\*)**

**get\_data()**

In this function, the data to be sent in a request to check the status of the stock information that has been sent to the sales channel is prepared. The response should return a list containing BatchRequest.

**validated\_data(*****data*****)**

It takes the response returned by the get\_data function as a parameter. If validation is needed on the product stock data that has been sent to the sales channel, it will be used for that purpose. If no validation is required, the provided data should be returned.

**transform\_data(*****data*****)**

It takes the response returned by the validated\_data function as a parameter. It is used if changes need to be made to the data before sending it to the sales channel. It returns the final version of the data to be sent.

**send\_request(*****transformed\_data*****)**

It takes the response returned by the transform\_data function as a parameter. This function uses the received data as the endpoint where the request will be sent to the sales channel. It should return either the response or the data that comes with the response.

{% hint style="warning" %}
In this section, since the response to be returned will be sent to the normalize\_response function, it is important to pay attention to data types when returning the data.
{% endhint %}

**normalize\_response(*****data*****,&#x20;*****validated\_data*****,&#x20;*****transformed\_data*****,&#x20;*****response*****)**

This function is where we gather and finalize the data used to query the sales channel regarding the processing status of our product stocks in the check\_stocks task. The response returned by this function will be directly used in the get\_stock\_batch\_requests function.

{% hint style="warning" %}
The response returned in this section should consist of three parts:

1. **response\_data**: This is the processed version of the data returned from the sales channel. Its type can be either a string or a list. If there is no data to be used in the returned response, an empty string is sufficient. If the returned response is to be used, it must be of type list, with each element being of type BatchRequestResponseDto.
2. **report**: These are the error reports generated while processing the response from the sales channel.
3. **data**: This is the first parameter received by our function, which is the response obtained from the get\_data function.

**Example return:**

```
return response_data, report, data
```

{% endhint %}

### Adding Price Data to Product Stock Data

```json
{
    "pk": 1,
    "product": 2250,
    "stock": 46,
    "stock_list": 1,
    "unit_type": "qty",
    "extra_field":{},
    "sold_quantity_unreported": 0,
    "modified_date": "2017-01-23T13:37:31.947171Z",
    "productprice":    {
        "pk": 2,
        "product": 913,
        "price": "62.44",
        "price_list": 1,
        "currency_type": "try",
        "tax_rate": "8.00",
        "retail_price": "249.75",
        "extra_field": {},
        "discount_percentage": "75.00",
        "modified_date": "2017-01-23T18:29:23.716095Z"
    }
}
```

### Adding Product Data to Product Stock Data

```json
{
    "pk": 1,
    "product": 2250,
    "stock": 46,
    "stock_list": 1,
    "unit_type": "qty",
    "extra_field":{},
    "sold_quantity_unreported": 0,
    "modified_date": "2017-01-23T13:37:31.947171Z",
    "product": {
        "pk": 12227,
        "name": "Kırmızı Tişört",
        "base_code": "1KBATC0231",
        "sku": "1KBATC0231001",
        "product_type": "0",
        "is_active": true,
        "parent": null,
        "attributes": {
            "boyut": "34X34",
            "renk": "001",
            "uretim_yeri": "Türkiye",
            "materyal": "%100 POLYESTER",
        },
        "productimage_set": [
            {
                "pk": 20044,
                "status": "active",
                "image": "http://localhost:8001/media/products/2021/10/17/12227/1bfe74b4-175e-4c1a-80f2-b355feae498c.jpg"
            }
        ],
        "attribute_set": 2,
        "productization_date": "2017-01-23T16:40:58.578504Z"
    }
}
```

### Source Code

Source Code of the channel.commands.product\_stocks Item:

```
from typing import Tuple, Any, List

from channel_app.channel.commands.product_stocks import (
    SendUpdatedStocks as AppSendUpdatedStocks,
    SendInsertedStocks as AppSendInsertedStocks,
    CheckStocks as AppCheckStocks,
)
from channel_app.core.data import (
    ErrorReportDto, BatchRequestResponseDto
)
from omnisdk.omnitron.models import ProductStock, BatchRequest

class SendUpdatedStocks(AppSendUpdatedStocks):
    param_sync = True

    def get_data(self) -> List[ProductStock]:
        raise NotImplementedError

    def validated_data(self, data) -> object:
        raise NotImplementedError

    def transform_data(self, data) -> object:
        raise NotImplementedError

    def send_request(self, transformed_data) -> object:
        raise NotImplementedError

    def normalize_response(self, data, validated_data, transformed_data,
                           response) -> Tuple[List[BatchRequestResponseDto],
                                              ErrorReportDto, Any]:
        raise NotImplementedError

class SendInsertedStocks(AppSendInsertedStocks):
    param_sync = True

    def get_data(self) -> List[ProductStock]:
        raise NotImplementedError

    def validated_data(self, data) -> object:
        raise NotImplementedError

    def transform_data(self, data) -> object:
        raise NotImplementedError

    def send_request(self, transformed_data) -> object:
        raise NotImplementedError

    def normalize_response(self, data, validated_data, transformed_data,
                           response) -> Tuple[List[BatchRequestResponseDto],
                                              ErrorReportDto, Any]:
        raise NotImplementedError

class CheckStocks(AppCheckStocks):

    def get_data(self) -> BatchRequest:
        raise NotImplementedError

    def validated_data(self, data) -> object:
        raise NotImplementedError

    def transform_data(self, data) -> object:
        raise NotImplementedError

    def send_request(self, transformed_data) -> object:
        raise NotImplementedError

    def normalize_response(self, data, validated_data, transformed_data,
                           response) -> Tuple[List[BatchRequestResponseDto],
                                              ErrorReportDto, Any]:
        raise NotImplementedError
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.akinon.com/technical-guides/channel-app-template/development-steps/encoding-the-sales-channel/product-stock.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
