From 439b116137cde932460e95bd3e8297f32f0d6366 Mon Sep 17 00:00:00 2001 From: "DESKTOP-IQ6LM3K\\Jose Antonio" Date: Mon, 20 May 2024 20:55:06 -0600 Subject: [PATCH] [IMP] api: Filter inventory by location and SKU 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 --- hantec_api_ecommerce/controllers/main.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/hantec_api_ecommerce/controllers/main.py b/hantec_api_ecommerce/controllers/main.py index 8b4610c..50c6941 100644 --- a/hantec_api_ecommerce/controllers/main.py +++ b/hantec_api_ecommerce/controllers/main.py @@ -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. @@ -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: @@ -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)] @@ -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,