diff --git a/stock_orderpoint_generator/models/orderpoint_template.py b/stock_orderpoint_generator/models/orderpoint_template.py index d50047d12..893f1a542 100644 --- a/stock_orderpoint_generator/models/orderpoint_template.py +++ b/stock_orderpoint_generator/models/orderpoint_template.py @@ -46,6 +46,7 @@ class OrderpointTemplate(models.Model): ("median", "Most frequent"), ("avg", "Average"), ("min", "Minimum"), + ("delivered", "Delivered"), ], default="max", help="Select a criteria to auto compute the minimum", @@ -60,6 +61,7 @@ class OrderpointTemplate(models.Model): ("median", "Most frequent"), ("avg", "Average"), ("min", "Minimum"), + ("delivered", "Delivered"), ], help="Select a criteria to auto compute the maximum", ) @@ -118,7 +120,13 @@ class OrderpointTemplate(models.Model): self, products, location_id, from_date, to_date, criteria ): """Returns a dict with product ids as keys and the resulting - calculation of historic moves according to criteria""" + calculation of historic moves according to criteria. If the + creteria is delivered we just search how many items were + delivered in the given period of time""" + if criteria == "delivered": + return products._get_delivered_to_customer_dict( + location_id, from_date, to_date + ) stock_qty_history = products._compute_historic_quantities_dict( location_id=location_id, from_date=from_date, to_date=to_date ) diff --git a/stock_orderpoint_generator/models/product.py b/stock_orderpoint_generator/models/product.py index 8403f457f..f76ea12e5 100644 --- a/stock_orderpoint_generator/models/product.py +++ b/stock_orderpoint_generator/models/product.py @@ -101,6 +101,28 @@ class ProductProduct(models.Model): ] return product_moves_dict + def _get_delivered_to_customer_dict( + self, location=False, from_date=False, to_date=False + ): + """Returns a dict of products with their delivered qtys for the + given dates and locations + """ + domain = [ + ("product_id", "in", self.ids), + ("state", "=", "done"), + ("location_dest_id.usage", "=", "customer"), + ] + if location: + domain += [("location_id", "child_of", location.id)] + if from_date: + domain += [("date", ">=", from_date)] + if to_date: + domain += [("date", "<=", to_date)] + move_lines = self.env["stock.move.line"].read_group( + domain, ["product_id", "qty_done"], ["product_id"] + ) + return {p["product_id"][0]: p["qty_done"] for p in move_lines} + def _compute_historic_quantities_dict( self, location_id=False, from_date=False, to_date=False ): diff --git a/stock_orderpoint_generator/tests/test_orderpoint_generator.py b/stock_orderpoint_generator/tests/test_orderpoint_generator.py index b506efe85..672ab6c2c 100644 --- a/stock_orderpoint_generator/tests/test_orderpoint_generator.py +++ b/stock_orderpoint_generator/tests/test_orderpoint_generator.py @@ -378,6 +378,13 @@ class TestOrderpointGenerator(SavepointCase): wizard.action_configure() orderpoint_auto_dict.update({"product_min_qty": 55, "product_max_qty": 50}) self.check_orderpoint(self.p1, self.template, orderpoint_auto_dict) + # Check delivered + self.template.auto_min_qty_criteria = "delivered" + self.template.auto_max_qty_criteria = "delivered" + wizard = self.wizard_over_products(self.p1, self.template) + wizard.action_configure() + orderpoint_auto_dict.update({"product_min_qty": 3, "product_max_qty": 5}) + self.check_orderpoint(self.p1, self.template, orderpoint_auto_dict) def test_auto_qty_multi_products(self): """Each product has a different history"""