From 82108878f429cb09cd99c6b9ca830ed512bb3564 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Fri, 9 Aug 2024 14:31:55 +0200 Subject: [PATCH] [UPD] change following code review --- account_stock_situation/README.rst | 1 + account_stock_situation/models/__init__.py | 1 - account_stock_situation/models/company.py | 63 +++++++------------ account_stock_situation/models/stock_quant.py | 7 --- .../readme/CONTRIBUTORS.rst | 1 + .../static/description/index.html | 1 + account_stock_situation/views/action.xml | 2 +- 7 files changed, 27 insertions(+), 49 deletions(-) delete mode 100644 account_stock_situation/models/stock_quant.py diff --git a/account_stock_situation/README.rst b/account_stock_situation/README.rst index a97ccd38..8e9ad56d 100644 --- a/account_stock_situation/README.rst +++ b/account_stock_situation/README.rst @@ -57,6 +57,7 @@ Contributors * Akretion * David BEAL + * Mathieu DELVA Maintainers ~~~~~~~~~~~ diff --git a/account_stock_situation/models/__init__.py b/account_stock_situation/models/__init__.py index 6f19864b..ff33dd7a 100644 --- a/account_stock_situation/models/__init__.py +++ b/account_stock_situation/models/__init__.py @@ -1,3 +1,2 @@ from . import company from . import config_settings -from . import stock_quant diff --git a/account_stock_situation/models/company.py b/account_stock_situation/models/company.py index 149d360f..1d64d2a6 100644 --- a/account_stock_situation/models/company.py +++ b/account_stock_situation/models/company.py @@ -22,7 +22,7 @@ class ResCompany(models.Model): def _set_account_stock_valuation(self, company_string_id): self = self.env.ref(company_string_id) - value, attach = self._get_stock_valuation_another() + value, attach = self._get_stock_valuation() for mfield in ( "account_stock_id", "account_purchase_stock_id", @@ -65,42 +65,34 @@ def _set_account_stock_valuation(self, company_string_id): ) attach.res_id = move.id - def _get_stock_valuation_another(self): + def _get_stock_valuation(self): self.ensure_one() - coef = self.cost_vs_purchase_threshold base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url") if tools.config.get("running_env") == "dev": base_url = "http://anothercorp.localhost/" location_ids = [x.lot_stock_id.id for x in self.valued_warehouse_ids] - # TODO conserver un group par emplacement : donc autant de colonnes - # de nombres de produits que d'entrepots dans l'excel - product_qties = self.env["stock.quant"].read_group( + + stock_quant_ids = self.env["stock.quant"].search( [("location_id", "child_of", location_ids)], - ["product_id", "warehouse_id", "quantity"], - ["product_id", "warehouse_id"], - lazy=False, ) - - product_ids = list({x["product_id"][0] for x in product_qties}) - - products = self.env["product.product"].browse(product_ids) - prices = { - x: x.variant_seller_ids and x.variant_seller_ids[0] or 0 for x in products - } + products = self.env["product.product"].browse(stock_quant_ids.product_id.ids) vals = defaultdict(list) product_dict = {} - for product in product_qties: - if not product["product_id"][0] in product_dict: - product_dict[product["product_id"][0]] = [ - product["warehouse_id"][0], - product["quantity"], + for stock_quant in stock_quant_ids: + if stock_quant.product_id.id not in product_dict: + product_dict[stock_quant.product_id.id] = [ + stock_quant.warehouse_id, + stock_quant.quantity, ] else: - product_dict[product["product_id"][0]] += [ - product["warehouse_id"][0], - product["quantity"], - ] + if stock_quant.warehouse_id in product_dict[stock_quant.product_id.id]: + product_dict[stock_quant.product_id.id][1] += stock_quant.quantity + else: + product_dict[stock_quant.product_id.id] += [ + stock_quant.warehouse_id, + stock_quant.quantity, + ] for product_id, warehouse_quantities in product_dict.items(): product = products.filtered(lambda s: s.id == product_id) @@ -111,33 +103,24 @@ def _get_stock_valuation_another(self): ) vals["code"].append(product.default_code) vals["designation"].append(product.name) + for i in range(0, len(warehouse_quantities), 2): warehouse_id = warehouse_quantities[i] quantity = warehouse_quantities[i + 1] - warehouse_id = self.env["stock.warehouse"].browse(warehouse_id) - vals[f"qté_{warehouse_id.name}"].append(round(quantity)) + vals[f"qty_{warehouse_id.code}"].append(round(quantity)) if len(warehouse_quantities) / 2 < len(self.valued_warehouse_ids): warehouse_without_qty = self.valued_warehouse_ids.filtered( lambda r: r.id not in [ - warehouse_quantities[i] + warehouse_quantities[i].id for i in range(0, len(warehouse_quantities), 2) ] ) for warehouse in warehouse_without_qty: - vals[f"qté_{warehouse.name}"].append(0) + vals[f"qty_{warehouse.code}"].append(0) + + vals["value"].append(round(product.standard_price)) - # TODO quand la valeur est < cost_vs_purchase_threshold % de ce seuil - # mettre une colonne 'check' à la valeur 1 - vals["valeur"].append( - round( - max( - product.standard_price, - prices[product] and prices[product].price or 0 * coef / 100, - ) - * product["quantity"] - ) - ) df = pl.from_dict(vals) mfile = io.BytesIO() df.write_excel(workbook=mfile) diff --git a/account_stock_situation/models/stock_quant.py b/account_stock_situation/models/stock_quant.py deleted file mode 100644 index b4c638bf..00000000 --- a/account_stock_situation/models/stock_quant.py +++ /dev/null @@ -1,7 +0,0 @@ -from odoo import fields, models - - -class StockQuant(models.Model): - _inherit = "stock.quant" - - warehouse_id = fields.Many2one(store=True, index=True) diff --git a/account_stock_situation/readme/CONTRIBUTORS.rst b/account_stock_situation/readme/CONTRIBUTORS.rst index 3237802e..51f1ed70 100644 --- a/account_stock_situation/readme/CONTRIBUTORS.rst +++ b/account_stock_situation/readme/CONTRIBUTORS.rst @@ -1,2 +1,3 @@ * Akretion * David BEAL + * Mathieu DELVA \ No newline at end of file diff --git a/account_stock_situation/static/description/index.html b/account_stock_situation/static/description/index.html index b5426d63..1b44c16e 100644 --- a/account_stock_situation/static/description/index.html +++ b/account_stock_situation/static/description/index.html @@ -411,6 +411,7 @@

Contributors

Akretion
diff --git a/account_stock_situation/views/action.xml b/account_stock_situation/views/action.xml index 2b7494d5..623adcf3 100644 --- a/account_stock_situation/views/action.xml +++ b/account_stock_situation/views/action.xml @@ -2,7 +2,7 @@ - Stock Valorisation + Stock Situation code