From c40774b575d0f3213ba8d590c49dab19ca76ba75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesu=CC=81s=20Alan=20Ramos=20Rodri=CC=81guez?= Date: Thu, 18 Apr 2024 13:53:07 -0600 Subject: [PATCH] [FIX] stock_picking_accounting_date: fix price unit based on accounting data and not today --- .../models/stock_move.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/stock_picking_accounting_date/models/stock_move.py b/stock_picking_accounting_date/models/stock_move.py index fa8829279501..e98bcbee6db6 100644 --- a/stock_picking_accounting_date/models/stock_move.py +++ b/stock_picking_accounting_date/models/stock_move.py @@ -3,6 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import models +from odoo.tools import float_round class StockMove(models.Model): @@ -30,3 +31,40 @@ def _prepare_account_move_vals( if self.picking_id.accounting_date: am_vals.update({"date": self.picking_id.accounting_date}) return am_vals + + def _get_price_unit(self): + """Returns the unit price for the move""" + self.ensure_one() + if ( + not self.origin_returned_move_id + and self.purchase_line_id + and self.product_id.id == self.purchase_line_id.product_id.id + ): + price_unit_prec = self.env["decimal.precision"].precision_get( + "Product Price" + ) + line = self.purchase_line_id + order = line.order_id + price_unit = line.price_unit + if line.taxes_id: + qty = line.product_qty or 1 + price_unit = line.taxes_id.with_context(round=False).compute_all( + price_unit, currency=line.order_id.currency_id, quantity=qty + )["total_void"] + price_unit = float_round( + price_unit / qty, precision_digits=price_unit_prec + ) + if line.product_uom.id != line.product_id.uom_id.id: + price_unit *= line.product_uom.factor / line.product_id.uom_id.factor + if ( + order.currency_id != order.company_id.currency_id + and self.picking_id.accounting_date + ): + return order.currency_id._convert( + price_unit, + order.company_id.currency_id, + order.company_id, + self.picking_id.accounting_date, + round=False, + ) + return super()._get_price_unit()