From 55a1e666d3f03d0ebb7d00632cc53dbf3ad0280e Mon Sep 17 00:00:00 2001 From: JasminSForgeFlow Date: Tue, 17 Dec 2024 17:18:15 +0530 Subject: [PATCH] [MIG] stock_account_product_run_fifo_hook: Migration to 18.0 --- .../README.rst | 10 +- .../__manifest__.py | 2 +- stock_account_product_run_fifo_hook/hooks.py | 98 +++++++++++++------ .../static/description/index.html | 6 +- 4 files changed, 75 insertions(+), 41 deletions(-) diff --git a/stock_account_product_run_fifo_hook/README.rst b/stock_account_product_run_fifo_hook/README.rst index 0f7407ab160c..e185738b2fb0 100644 --- a/stock_account_product_run_fifo_hook/README.rst +++ b/stock_account_product_run_fifo_hook/README.rst @@ -17,13 +17,13 @@ Stock Account Product Run FIFO Hook :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html :alt: License: LGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github - :target: https://github.com/OCA/stock-logistics-workflow/tree/17.0/stock_account_product_run_fifo_hook + :target: https://github.com/OCA/stock-logistics-workflow/tree/18.0/stock_account_product_run_fifo_hook :alt: OCA/stock-logistics-workflow .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/stock-logistics-workflow-17-0/stock-logistics-workflow-17-0-stock_account_product_run_fifo_hook + :target: https://translation.odoo-community.org/projects/stock-logistics-workflow-18-0/stock-logistics-workflow-18-0-stock_account_product_run_fifo_hook :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png - :target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&target_branch=17.0 + :target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&target_branch=18.0 :alt: Try me on Runboat |badge1| |badge2| |badge3| |badge4| |badge5| @@ -51,7 +51,7 @@ Bug Tracker Bugs are tracked on `GitHub Issues `_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -83,6 +83,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -This module is part of the `OCA/stock-logistics-workflow `_ project on GitHub. +This module is part of the `OCA/stock-logistics-workflow `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/stock_account_product_run_fifo_hook/__manifest__.py b/stock_account_product_run_fifo_hook/__manifest__.py index 29431dbe46c9..d2e460e5b54d 100644 --- a/stock_account_product_run_fifo_hook/__manifest__.py +++ b/stock_account_product_run_fifo_hook/__manifest__.py @@ -5,7 +5,7 @@ "name": "Stock Account Product Run FIFO Hook", "summary": "Add more flexibility in the run fifo method.", "author": "ForgeFlow, Odoo Community Association (OCA)", - "version": "17.0.1.0.1", + "version": "18.0.1.0.1", "category": "Warehouse Management", "website": "https://github.com/OCA/stock-logistics-workflow", "license": "LGPL-3", diff --git a/stock_account_product_run_fifo_hook/hooks.py b/stock_account_product_run_fifo_hook/hooks.py index 60346c7f5082..0e125573cfc4 100644 --- a/stock_account_product_run_fifo_hook/hooks.py +++ b/stock_account_product_run_fifo_hook/hooks.py @@ -12,14 +12,14 @@ # flake8: noqa: C901 def post_load_hook(): - def _run_fifo_new(self, quantity, company): + def _run_fifo_new(self, quantity, company, lot=False): if not hasattr(self, "_run_fifo_prepare_candidate_update"): - return self._run_fifo_original(quantity, company) + return self._run_fifo_original(quantity, company, lot) - # Find back incoming stock valuation layers (called candidates here) - # to value `quantity`. + # Find back incoming stock valuation layers + # (called candidates here) to value `quantity`. qty_to_take_on_candidates = quantity - candidates = self._get_fifo_candidates(company) + candidates = self._get_fifo_candidates(company, lot=lot) new_standard_price = 0 tmp_value = 0 # to accumulate the value taken on the candidates taken_data = {} @@ -40,6 +40,7 @@ def _run_fifo_new(self, quantity, company): } ) new_remaining_value = candidate.remaining_value - value_taken_on_candidate + candidate_vals = { "remaining_qty": candidate.remaining_qty - qty_taken_on_candidate, "remaining_value": new_remaining_value, @@ -56,6 +57,7 @@ def _run_fifo_new(self, quantity, company): qty_to_take_on_candidates -= qty_taken_on_candidate tmp_value += value_taken_on_candidate + if float_is_zero( qty_to_take_on_candidates, precision_rounding=self.uom_id.rounding ): @@ -155,6 +157,7 @@ def _run_fifo_vacuum_new(self, company=None): ) min_create_date = min(min_create_date, group[2]) all_candidates_by_product = defaultdict(lambda: ValuationLayer) + lot_to_update = [] domain = [ ("product_id", "in", self.ids), ("remaining_qty", ">", 0), @@ -179,14 +182,19 @@ def _run_fifo_vacuum_new(self, company=None): all_candidates = all_candidates_by_product[product.id] current_real_time_svls = ValuationLayer for svl_to_vacuum in svls_to_vacuum_by_product[product.id]: - # We don't use search to avoid executing _flush_search - # and to decrease interaction with DB + # We don't use search to avoid executing + # _flush_search and to decrease interaction with DB candidates = all_candidates.filtered( lambda r, svl_to_vacuum=svl_to_vacuum: r.create_date > svl_to_vacuum.create_date or r.create_date == svl_to_vacuum.create_date and r.id > svl_to_vacuum.id ) + if product.lot_valuated: + candidates = candidates.filtered( + lambda r, svl_to_vacuum=svl_to_vacuum: r.lot_id + == svl_to_vacuum.lot_id + ) if self.env.context.get("use_past_svl", False): candidates = all_candidates if not candidates: @@ -263,8 +271,8 @@ def _run_fifo_vacuum_new(self, company=None): } ) - # Don't create a layer or an accounting - # entry if the corrected value is zero. + # Don't create a layer or an accounting entry + # if the corrected value is zero. if svl_to_vacuum.currency_id.is_zero(corrected_value): continue @@ -288,8 +296,10 @@ def _run_fifo_vacuum_new(self, company=None): "description": "Revaluation of %s (negative inventory)" % (move.picking_id.name or move.name), "stock_valuation_layer_id": svl_to_vacuum.id, + "lot_id": svl_to_vacuum.lot_id.id, } ) + lot_to_update.append(svl_to_vacuum.lot_id) if product.valuation == "real_time": current_real_time_svls |= svl_to_vacuum real_time_svls_to_vacuum |= current_real_time_svls @@ -301,12 +311,22 @@ def _run_fifo_vacuum_new(self, company=None): product = product.with_company(company.id) if not svls_to_vacuum_by_product[product.id]: continue - if product.cost_method in ["average", "fifo"] and not float_is_zero( + if product.cost_method not in ["average", "fifo"] or float_is_zero( product.quantity_svl, precision_rounding=product.uom_id.rounding ): - product.sudo().with_context(disable_auto_svl=True).write( - {"standard_price": product.value_svl / product.quantity_svl} - ) + continue + if product.lot_valuated: + for lot in lot_to_update: + if float_is_zero( + lot.quantity_svl, precision_rounding=product.uom_id.rounding + ): + continue + lot.sudo().with_context(disable_auto_svl=True).write( + {"standard_price": lot.value_svl / lot.quantity_svl} + ) + product.sudo().with_context(disable_auto_svl=True).write( + {"standard_price": product.value_svl / product.quantity_svl} + ) vacuum_svls._validate_accounting_entries() self._create_fifo_vacuum_anglo_saxon_expense_entries( @@ -321,29 +341,43 @@ def _get_out_svl_vals_new(self, forced_quantity): svl_vals_list = [] for move in self: move = move.with_company(move.company_id) - valued_move_lines = move._get_out_move_lines() - valued_quantity = 0 - for valued_move_line in valued_move_lines: - valued_quantity += valued_move_line.product_uom_id._compute_quantity( - valued_move_line.quantity, move.product_id.uom_id - ) + lines = move._get_out_move_lines() + quantities = defaultdict(float) + if forced_quantity: + quantities[forced_quantity[0]] += forced_quantity[1] + else: + for line in lines: + quantities[line.lot_id] += line.product_uom_id._compute_quantity( + line.quantity, move.product_id.uom_id + ) if float_is_zero( - forced_quantity or valued_quantity, + sum(quantities.values()), precision_rounding=move.product_id.uom_id.rounding, ): continue - svl_vals = move.product_id.with_context( - move_requestor=move.id - )._prepare_out_svl_vals(forced_quantity or valued_quantity, move.company_id) - svl_vals.update(move._prepare_common_svl_vals()) - if forced_quantity: - svl_vals["description"] = ( - _("Correction of %s (modification of past move)") - % move.picking_id.name - or move.name - ) - svl_vals["description"] += svl_vals.pop("rounding_adjustment", "") - svl_vals_list.append(svl_vals) + + if move.product_id.lot_valuated: + vals = [] + for lot_id, qty in quantities.items(): + out_vals = move.product_id.with_context( + move_requestor=move.id + )._prepare_out_svl_vals(qty, move.company_id, lot=lot_id) + vals.append(out_vals) + else: + vals = [ + move.product_id.with_context( + move_requestor=move.id + )._prepare_out_svl_vals(sum(quantities.values()), move.company_id) + ] + for val in vals: + val.update(move._prepare_common_svl_vals()) + if forced_quantity: + val["description"] = _( + "Correction of %s (modification of past move)", + move.picking_id.name or move.name, + ) + val["description"] += val.pop("rounding_adjustment", "") + svl_vals_list += vals return svl_vals_list if not hasattr(StockMove, "_get_out_svl_vals_original"): diff --git a/stock_account_product_run_fifo_hook/static/description/index.html b/stock_account_product_run_fifo_hook/static/description/index.html index 6ef5104d2834..83a22f254ba9 100644 --- a/stock_account_product_run_fifo_hook/static/description/index.html +++ b/stock_account_product_run_fifo_hook/static/description/index.html @@ -369,7 +369,7 @@

Stock Account Product Run FIFO Hook

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! source digest: sha256:16ff2bf6dff55b6e2490baba84bf78dccb176514585090dd54df0f46c5f7ddcb !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> -

Beta License: LGPL-3 OCA/stock-logistics-workflow Translate me on Weblate Try me on Runboat

+

Beta License: LGPL-3 OCA/stock-logistics-workflow Translate me on Weblate Try me on Runboat

This module adds hook points in product_product._run_fifo in order to add more flexibility in the information that is stored in the fifo candidates.

@@ -400,7 +400,7 @@

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed -feedback.

+feedback.

Do not contact contributors directly about support or help with technical issues.

@@ -432,7 +432,7 @@

Maintainers

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

-

This module is part of the OCA/stock-logistics-workflow project on GitHub.

+

This module is part of the OCA/stock-logistics-workflow project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.