Skip to content

Commit

Permalink
[IMP] Some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
maq-adhoc committed Nov 27, 2024
1 parent 67a37d8 commit e107792
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 40 deletions.
1 change: 1 addition & 0 deletions stock_currency_valuation/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
'depends': [
'stock_account',
'stock_landed_costs',
'product_replenishment_cost',
],
'data': [
Expand Down
2 changes: 2 additions & 0 deletions stock_currency_valuation/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions stock_currency_valuation/models/account_move.py
Original file line number Diff line number Diff line change
@@ -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
50 changes: 21 additions & 29 deletions stock_currency_valuation/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions stock_currency_valuation/models/stock_landed_cost.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion stock_currency_valuation/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
22 changes: 16 additions & 6 deletions stock_currency_valuation/models/stock_valuation_layer.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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,
Expand All @@ -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
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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()

0 comments on commit e107792

Please sign in to comment.