diff --git a/nutrition_program.py b/nutrition_program.py index 1138127..163be8e 100644 --- a/nutrition_program.py +++ b/nutrition_program.py @@ -17,6 +17,7 @@ class NutritionProgram(ModelSQL, ModelView): start_weight = fields.Float('Start Weight') end_weight = fields.Float('End Weight') product = fields.Many2One('product.product', 'Product', required=True) + animal_product = fields.Many2One('product.product', 'Animal Product') bom = fields.Function(fields.Many2One('production.bom', 'BOM', domain=[ ('output_products', '=', Eval('product', 0)), @@ -48,16 +49,19 @@ class StockLot: if not animal: return - domain = [('product', '=', self.product)] + domain = [('animal_product', '=', self.product)] + order = [('end_weight', 'DESC')] + weight_domain = [] if animal.current_weight: weight = animal.current_weight.weight - domain.extend([ + weight_domain = [ ('start_weight', '<=', weight), ('end_weight', '>=', weight), - ], ) - programs = Program.search(domain, order=[ - ('end_weight', 'DESC'), - ], limit=1) + ] + programs = Program.search(domain + weight_domain, order=order, limit=1) + if len(programs) > 0: + return programs[0].id + programs = Program.search(weight_domain, order=order, limit=1) if len(programs) > 0: return programs[0].id diff --git a/tests/scenario_nutrition_program.rst b/tests/scenario_nutrition_program.rst index c019595..971a19e 100644 --- a/tests/scenario_nutrition_program.rst +++ b/tests/scenario_nutrition_program.rst @@ -85,6 +85,15 @@ Create products:: >>> group_template.save() >>> group_product = Product(template=group_template) >>> group_product.save() + >>> grain_template = ProductTemplate( + ... name='Graing', + ... default_uom=unit, + ... type='goods', + ... list_price=Decimal('40'), + ... cost_price=Decimal('25')) + >>> grain_template.save() + >>> grain_product = Product(template=grain_template) + >>> grain_product.save() Create sequence:: @@ -177,7 +186,8 @@ Create nutrition program:: >>> nutrition_program = NutritionProgram( ... start_weight=10.0, ... end_weight=30.0, - ... product=individual_product) + ... product=grain_product) + ... animal_product=individual_product) >>> nutrition_program.save() >>> individual.lot.nutrition_program == None True @@ -202,7 +212,8 @@ Create another nutrition program:: >>> nutrition_program2 = NutritionProgram( ... start_weight=50.0, ... end_weight=70.0, - ... product=individual_product) + ... product=grain_product) + ... animal_product=individual_product) >>> nutrition_program2.save() >>> AnimalWeight = Model.get('farm.animal.weight') >>> kg, = ProductUom.find([('name', '=', 'Kilogram')]) diff --git a/view/nutrition_program_form.xml b/view/nutrition_program_form.xml index 07a4d90..0e1d463 100644 --- a/view/nutrition_program_form.xml +++ b/view/nutrition_program_form.xml @@ -10,4 +10,6 @@