From 230edf260aa1c583f904118376e6035bb0bed60d Mon Sep 17 00:00:00 2001 From: "DESKTOP-IQ6LM3K\\Jose Antonio" Date: Mon, 20 May 2024 20:35:33 -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 | 37 +++++++++++++++++------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/hantec_api_ecommerce/controllers/main.py b/hantec_api_ecommerce/controllers/main.py index 2c2b9d2..50c6941 100644 --- a/hantec_api_ecommerce/controllers/main.py +++ b/hantec_api_ecommerce/controllers/main.py @@ -725,16 +725,18 @@ 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. + """Retrieves the inventory details for all products with a SKU or a specific product based on its SKU from a given location. This function searches for all products in the Odoo database that have a SKU (default_code not null) - or searches for a product using the provided SKU and returns their inventory details in a JSON response. + 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 (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. JSON response: - message (str): A message indicating the action performed. @@ -747,19 +749,32 @@ def get_inventory(self): Returns: dict: A dictionary with a message and the inventory details of the products. """ - if request.httprequest.method == 'POST': - sku = request.jsonrequest.get("sku") - products = request.env["product.product"].search([("default_code", "=", sku)]) + 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 sku: + domain = [("default_code", "=", sku)] else: - products = request.env["product.product"].search([("default_code", "!=", False)]) + domain = [("default_code", "!=", False)] + + products = request.env["product.product"].search(domain) - product_fields = ["name", "default_code", "qty_available", "virtual_available"] - inventory_list = products.read(product_fields) + inventory_data = [] + for product in products: + 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, + "qty_available": quant.quantity, + "virtual_available": product.virtual_available, + }) return { "message": "Inventory data retrieved", - "inventory_data": inventory_list, - } + "inventory_data": inventory_data, + } @route('/get_states/', methods=["GET"], type="json", auth="user") def get_states(self, country):