From 51959dcbc1206e24488a144da17e43f89977a1e2 Mon Sep 17 00:00:00 2001 From: Kevin Khao Date: Mon, 15 Mar 2021 16:16:47 +0100 Subject: [PATCH] [IMP] Add UoM functionality, simplify some code --- .../models/product_product.py | 44 ++++++++++++++++--- .../readme/CONTRIBUTORS.rst | 1 + 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/base_product_mass_addition/models/product_product.py b/base_product_mass_addition/models/product_product.py index 73b34569bf7..7158bec801c 100644 --- a/base_product_mass_addition/models/product_product.py +++ b/base_product_mass_addition/models/product_product.py @@ -13,16 +13,37 @@ class ProductProduct(models.Model): qty_to_process = fields.Float( compute="_compute_process_qty", - inverse="_inverse_set_process_qty", + inverse="_inverse_quick_vals", help="Set this quantity to create a new line " "for this product or update the existing one.", ) + quick_uom_category_id = fields.Many2one( + "uom.category", compute="_compute_quick_uom_category_id" + ) + quick_uom_id = fields.Many2one( + "uom.uom", + domain="[('category_id', '=', quick_uom_category_id)]", + compute="_compute_purchase_quick_uom_id", + inverse="_inverse_quick_vals", + ) + + def _inverse_quick_vals(self): + self._sync_quick_lines() - def _inverse_set_process_qty(self): + @property + def pma_parent(self): + # shorthand for product_mass_addition parent parent_model = self.env.context.get("parent_model") parent_id = self.env.context.get("parent_id") - if parent_model: - parent = self.env[parent_model].browse(parent_id) + if parent_model and parent_id: + return self.env[parent_model].browse(parent_id) + + def _compute_quick_uom_category_id(self): + raise NotImplementedError + + def _sync_quick_lines(self): + parent = self.pma_parent + if parent: for product in self: quick_line = parent._get_quick_line(product) if quick_line: @@ -30,8 +51,21 @@ def _inverse_set_process_qty(self): else: parent._add_quick_line(product, quick_line._name) + def _default_quick_uom_id(self): + raise NotImplementedError + + def _compute_purchase_quick_uom_id(self): + parent = self.pma_parent + if parent: + for rec in self: + quick_line = parent._get_quick_line(rec) + if quick_line: + rec.quick_uom_id = quick_line.product_uom + else: + rec.quick_uom_id = rec._default_quick_uom_id() + def _compute_process_qty(self): - if not self.env.context.get("parent_id"): + if not self.pma_parent: return def button_quick_open_product(self): diff --git a/base_product_mass_addition/readme/CONTRIBUTORS.rst b/base_product_mass_addition/readme/CONTRIBUTORS.rst index 51a402da1ff..c5d975bed46 100644 --- a/base_product_mass_addition/readme/CONTRIBUTORS.rst +++ b/base_product_mass_addition/readme/CONTRIBUTORS.rst @@ -4,3 +4,4 @@ Akretion * Mourad EL HADJ MIMOUNE * Pierrick Brun * David Béal +* Kevin Khao