Skip to content

Commit

Permalink
[IMP] api: Filter inventory by location
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. This enhancement improves
the ability to retrieve inventory details specific to a stock location, providing more targeted and relevant data.

This change ensures that users can query inventory data specific to a particular stock location, 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 107c3ae commit 3620051
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions hantec_api_ecommerce/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,13 +728,15 @@ def send_message_sale_order(self, order=False):

@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.
Expand All @@ -747,19 +749,33 @@ def get_inventory(self):
Returns:
dict: A dictionary with a message and the inventory details of the products.
"""
location_id = request.jsonrequest.get("location_id")

location = request.env["stock.location"].browse(location_id).exists()

if request.httprequest.method == 'POST':
sku = request.jsonrequest.get("sku")
products = request.env["product.product"].search([("default_code", "=", 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:
quant = request.env["stock.quant"].search([("product_id", "=", product.id), ("location_id", "=", location_id)])
if quant:
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/<model("res.country"):country>', methods=["GET"], type="json", auth="user")
def get_states(self, country):
Expand Down

0 comments on commit 3620051

Please sign in to comment.