Skip to content

Commit

Permalink
[IMP] api: Filter inventory by location and SKU
Browse files Browse the repository at this point in the history
Modified the get_inventory endpoint to allow filtering of inventory based on a given location and optionally by SKU.
This enhancement improves the ability to retrieve inventory details specific to a stock location and a product SKU,
providing more targeted and relevant data.

This change ensures that users can query inventory data specific to a particular stock location and product SKU, enhancing
the accuracy and usefulness of the inventory information provided by the endpoint.

Closes #25
  • Loading branch information
JAntonioSalas committed May 21, 2024
1 parent 63ed3ec commit 439b116
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions hantec_api_ecommerce/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ def send_message_sale_order(self, order=False):
return {
"message": f"Message successfully posted in sale order with ID: {order.id}."
}

@route(["/get_inventory", "/get_inventory_by_sku"], methods=["POST", "GET"], type="json", auth="user")
def get_inventory(self):
"""Retrieves the inventory details for all products with a SKU or a specific product based on its SKU from a given location.
Expand All @@ -734,13 +734,10 @@ def get_inventory(self):
or searches for a product using the provided SKU, and returns their inventory details from a given location
in a JSON response.
JSON request body for POST:
JSON request body for POST (if applicable):
- sku (str, optional): The SKU of the product to search for.
- location_id (int): The ID of the stock location to filter inventory by.
URL parameters for GET:
- location_id (int): The ID of the stock location to filter inventory by.
JSON response:
- message (str): A message indicating the action performed.
- inventory_data (list of dict): A list of dictionaries containing the inventory details for each product, including:
Expand All @@ -752,12 +749,11 @@ def get_inventory(self):
Returns:
dict: A dictionary with a message and the inventory details of the products.
"""
location_id = request.params.get("location_id")

location = request.env["stock.location"].browse(location_id).exists()
location_id = request.jsonrequest.get("location_id") or request.params.get("location_id")
location = request.env["stock.location"].browse(int(location_id)).exists()
sku = request.jsonrequest.get("sku") if request.httprequest.method == 'POST' else request.params.get("sku")

if request.httprequest.method == 'POST':
sku = request.jsonrequest.get("sku")
if sku:
domain = [("default_code", "=", sku)]
else:
domain = [("default_code", "!=", False)]
Expand All @@ -766,8 +762,8 @@ def get_inventory(self):

inventory_data = []
for product in products:
quant = request.env["stock.quant"].search([("product_id", "=", product.id), ("location_id", "=", location_id)])
if quant:
quants = request.env["stock.quant"].search([("product_id", "=", product.id), ("location_id", "=", location_id)])
for quant in quants:
inventory_data.append({
"name": product.name,
"default_code": product.default_code,
Expand Down

0 comments on commit 439b116

Please sign in to comment.