From e107792bd5914fb371794e9f32fb7e57523e0492 Mon Sep 17 00:00:00 2001 From: Martin Quinteros Date: Tue, 26 Nov 2024 07:37:41 -0300 Subject: [PATCH] [IMP] Some changes --- stock_currency_valuation/__manifest__.py | 1 + stock_currency_valuation/models/__init__.py | 2 + .../models/account_move.py | 28 +++++++++++ .../models/product_template.py | 50 ++++++++----------- .../models/stock_landed_cost.py | 26 ++++++++++ stock_currency_valuation/models/stock_move.py | 3 +- .../models/stock_valuation_layer.py | 22 +++++--- .../wizard/{__init__,py => __init__.py} | 0 .../stock_valuation_layer_revaluation.py | 14 ++++-- 9 files changed, 106 insertions(+), 40 deletions(-) create mode 100644 stock_currency_valuation/models/account_move.py create mode 100644 stock_currency_valuation/models/stock_landed_cost.py rename stock_currency_valuation/wizard/{__init__,py => __init__.py} (100%) diff --git a/stock_currency_valuation/__manifest__.py b/stock_currency_valuation/__manifest__.py index 5efadb631..875043aa5 100644 --- a/stock_currency_valuation/__manifest__.py +++ b/stock_currency_valuation/__manifest__.py @@ -10,6 +10,7 @@ ], 'depends': [ 'stock_account', + 'stock_landed_costs', 'product_replenishment_cost', ], 'data': [ diff --git a/stock_currency_valuation/models/__init__.py b/stock_currency_valuation/models/__init__.py index 1226f4470..3aeac297f 100644 --- a/stock_currency_valuation/models/__init__.py +++ b/stock_currency_valuation/models/__init__.py @@ -3,3 +3,5 @@ from . import product_product from . import product_template from . import stock_move +from . import stock_landed_cost +from . import account_move diff --git a/stock_currency_valuation/models/account_move.py b/stock_currency_valuation/models/account_move.py new file mode 100644 index 000000000..12ff27089 --- /dev/null +++ b/stock_currency_valuation/models/account_move.py @@ -0,0 +1,28 @@ +from collections import defaultdict + +from odoo import models, api, fields +from odoo.tools.float_utils import float_compare, float_is_zero + + +class AccountMove(models.Model): + _inherit = "account.move" + + @api.model_create_multi + def create(self, vals_list): + if self.env.context.get('revaluation_force_currency'): + vals_list = self.alter_vals_for_revaluation_in_currency(vals_list) + res_ids = super(AccountMove, self).create(vals_list) + return res_ids + + def alter_vals_for_revaluation_in_currency(self, vals_list): + valuation_currency_id = self.env.context.get('revaluation_force_currency').id + value_in_currency = self.env.context.get('revaluation_value_in_currency') + vals_list[0]['line_ids'][0][2].update(({ + 'currency_id': valuation_currency_id, + 'amount_currency': abs(value_in_currency) + })) + vals_list[0]['line_ids'][1][2].update(({ + 'currency_id': valuation_currency_id, + 'amount_currency': abs(value_in_currency) * -1 + })) + return vals_list diff --git a/stock_currency_valuation/models/product_template.py b/stock_currency_valuation/models/product_template.py index af8661dd1..d07063688 100644 --- a/stock_currency_valuation/models/product_template.py +++ b/stock_currency_valuation/models/product_template.py @@ -19,46 +19,38 @@ class productTemplate(models.Model): ondelete={'average_in_currency': 'set default'} ) - @api.depends() + @api.depends('standard_price_in_currency') def _compute_replenishment_cost(self): use_average_in_currency = self.filtered(lambda x: x.replenishment_cost_type == "average_in_currency") super(productTemplate, self - use_average_in_currency)._compute_replenishment_cost() - if use_average_in_currency: - company_id = self.env.company - # domain = [ - # ('product_tmpl_id', 'in', use_average_in_currency.ids), - # ('company_id', '=', company_id.id), - # ] - # groups = self.env['stock.valuation.layer']._read_group(domain, ['value_in_currency:sum', 'quantity:sum'], - # ['product_tmpl_id']) - # products_avg_cost = {group['product_tmpl_id'][0]: group['value_in_currency'] / group['quantity'] - # if group['quantity'] else 0 for group in groups} - for rec in use_average_in_currency: - product_currency = rec.currency_id - #product_cost = products_avg_cost.get(rec.id) or 0 - price_unit = rec.valuation_currency_id._convert( - from_amount=rec.standard_price_in_currency, - to_currency=product_currency, - company=company_id, - date=fields.date.today(), - ) - rec.update({ - 'replenishment_base_cost_currency_id': rec.valuation_currency_id.id, - 'replenishment_base_cost_on_currency': price_unit, - 'replenishment_cost': price_unit, - 'replenishment_base_cost': rec.standard_price_in_currency, - }) + company_id = self.env.company + for rec in use_average_in_currency: + product_currency = rec.currency_id + replenishment_cost_rule = rec.replenishment_cost_rule_id + replenishment_cost = rec.standard_price_in_currency + if replenishment_cost_rule: + replenishment_cost = replenishment_cost_rule.compute_rule(replenishment_cost, rec) + replenishment_base_cost_on_currency = rec.valuation_currency_id._convert( + from_amount=replenishment_cost, + to_currency=product_currency, + company=company_id, + date=fields.date.today(), + ) + rec.update({ + 'replenishment_base_cost_on_currency': replenishment_base_cost_on_currency, + 'replenishment_cost': replenishment_cost, + }) @api.depends_context('company') @api.depends('product_variant_ids', 'product_variant_ids.standard_price') def _compute_standard_price_in_currency(self): - # Depends on force_company context because standard_price_in_currency is company_dependent - # on the product_product + # Por ahora hacemos esto porque replishment cost no es compatible al 100% con variantes + # obtenemos el precio del primer producto unique_variants = self.filtered(lambda template: len(template.product_variant_ids) == 1) for template in unique_variants: template.standard_price_in_currency = template.product_variant_ids.standard_price_in_currency for template in (self - unique_variants): - template.standard_price_in_currency = 0.0 + template.standard_price_in_currency = template.product_variant_ids[:1].standard_price_in_currency def _set_standard_price_in_currency(self): for template in self: diff --git a/stock_currency_valuation/models/stock_landed_cost.py b/stock_currency_valuation/models/stock_landed_cost.py new file mode 100644 index 000000000..f6345ae32 --- /dev/null +++ b/stock_currency_valuation/models/stock_landed_cost.py @@ -0,0 +1,26 @@ +from odoo import models + + +class AdjustmentLines(models.Model): + + _inherit = 'stock.valuation.adjustment.lines' + + def _create_accounting_entries(self, move, qty_out): + AccountMoveLine = super()._create_accounting_entries(move, qty_out) + if self.product_id.categ_id.valuation_currency_id: + amount = AccountMoveLine[0][2]['debit'] or AccountMoveLine[0][2]['credit'] * -1 + value_in_currency = self.cost_id.currency_id._convert( + from_amount=amount, + to_currency=self.product_id.categ_id.valuation_currency_id, + company=self.cost_id.company_id, + date=self.create_date, + ) + AccountMoveLine[0][2].update(({ + 'currency_id': self.product_id.categ_id.valuation_currency_id.id, + 'amount_currency': value_in_currency + })) + AccountMoveLine[1][2].update(({ + 'currency_id': self.product_id.categ_id.valuation_currency_id.id, + 'amount_currency': value_in_currency * -1 + })) + return AccountMoveLine diff --git a/stock_currency_valuation/models/stock_move.py b/stock_currency_valuation/models/stock_move.py index 9e5068350..ee16c8886 100644 --- a/stock_currency_valuation/models/stock_move.py +++ b/stock_currency_valuation/models/stock_move.py @@ -1,12 +1,13 @@ from collections import defaultdict -from odoo import models, fields +from odoo import models, api, fields from odoo.tools.float_utils import float_compare, float_is_zero class StockMove(models.Model): _inherit = "stock.move" + def _account_entry_move(self, qty, description, svl_id, cost): am_vals_list = super()._account_entry_move(qty, description, svl_id, cost) layer = self.env['stock.valuation.layer'].browse(svl_id) diff --git a/stock_currency_valuation/models/stock_valuation_layer.py b/stock_currency_valuation/models/stock_valuation_layer.py index b575e0ad5..7abad486c 100644 --- a/stock_currency_valuation/models/stock_valuation_layer.py +++ b/stock_currency_valuation/models/stock_valuation_layer.py @@ -1,7 +1,5 @@ -from odoo import models, fields, api, _ -from odoo.tools.safe_eval import safe_eval -from odoo.exceptions import ValidationError -from odoo.tools import float_compare +from odoo import models, fields, api +from odoo.tools.float_utils import float_is_zero class StockValuationLayer(models.Model): @@ -17,8 +15,7 @@ class StockValuationLayer(models.Model): @api.depends('categ_id', 'value', 'bypass_currency_valuation', 'stock_valuation_layer_id') def _compute_other_currency_values(self): for rec in self: - ##rec.stock_valuation_layer_id - + # rec.stock_valuation_layer_id if not rec.bypass_currency_valuation and rec.valuation_currency_id: rec.value_in_currency = rec.currency_id._convert( from_amount=rec.value, @@ -33,3 +30,16 @@ def _compute_other_currency_values(self): else: rec.value_in_currency = False rec.unit_cost_in_currency = False + + @api.model_create_multi + def create(self, vals_list): + res = super().create(vals_list) + res._update_currency_standard_price() + return res + + def _update_currency_standard_price(self): + for layer_id in self.filtered(lambda x: x.stock_landed_cost_id and x.product_id.cost_method == 'average' and x.value_in_currency): + # batch standard price computation avoid recompute quantity_svl at each iteration + product = layer_id.product_id.with_company(layer_id.company_id) + if not float_is_zero(product.quantity_svl, precision_rounding=product.uom_id.rounding): + product.sudo().with_context(disable_auto_svl=True).standard_price_in_currency += layer_id.value_in_currency / product.quantity_svl diff --git a/stock_currency_valuation/wizard/__init__,py b/stock_currency_valuation/wizard/__init__.py similarity index 100% rename from stock_currency_valuation/wizard/__init__,py rename to stock_currency_valuation/wizard/__init__.py diff --git a/stock_currency_valuation/wizard/stock_valuation_layer_revaluation.py b/stock_currency_valuation/wizard/stock_valuation_layer_revaluation.py index 57dc7e598..eef1b970f 100644 --- a/stock_currency_valuation/wizard/stock_valuation_layer_revaluation.py +++ b/stock_currency_valuation/wizard/stock_valuation_layer_revaluation.py @@ -1,14 +1,15 @@ -from odoo import models +from odoo import models, fields, _ +from odoo.exceptions import UserError +from odoo.tools import float_compare, float_is_zero class StockValuationLayerRevaluation(models.TransientModel): _inherit = 'stock.valuation.layer.revaluation' def action_validate_revaluation(self): - res = super().action_validate_revaluation() - # Update the stardard price in currency in case of AVCO product_id = self.product_id.with_company(self.company_id) if product_id.categ_id.property_cost_method in ('average', 'fifo') and product_id.categ_id.valuation_currency_id: + # Update the stardard price in currency in case of AVCO # Para actualizar el costo en currency vuelvo a calcular el valor en moneda # Si bien hago dos veces el calculo (en el layer y aqui) esto es mas # sencillo que obtener el ultimo layer y agregar el valor. @@ -19,4 +20,9 @@ def action_validate_revaluation(self): date=self.create_date, ) product_id.with_context(disable_auto_svl=True).standard_price_in_currency += value_in_currency / self.current_quantity_svl - return res + res = super(StockValuationLayerRevaluation, self.with_context( + revaluation_force_currency=product_id.categ_id.valuation_currency_id, + revaluation_value_in_currency=value_in_currency + )).action_validate_revaluation() + return res + return super().action_validate_revaluation()