Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion product_currency/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from . import models
from . import models, demo
4 changes: 1 addition & 3 deletions product_currency/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
"views/product_template_views.xml",
"security/product_currency_security.xml",
],
"demo": [
"demo/product_product_demo.xml",
],
"demo": ["demo/product_product_demo.xml"],
"installable": True,
"auto_install": False,
"application": False,
Expand Down
1 change: 1 addition & 0 deletions product_currency/demo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import product_currency_demo
92 changes: 92 additions & 0 deletions product_currency/demo/product_currency_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import logging

from odoo import api, models

_logger = logging.getLogger(__name__)


# pylint: disable=consider-merging-classes-inherited
# pylint: disable=R8180
class ProductTemplate(models.Model):
_inherit = "product.template"

@api.model
def _install_product_currency_demo(self, companies=None):
"""Create demo records.

Backwards-compatible: if `companies` is provided (old callers/tests), create
per-company using an env restricted to that company. Otherwise create using
the current env.
"""
if companies:
for company in companies:
_logger.info("Creating product_currency demo data for: %s", company.name)
env = self.with_context(allowed_company_ids=[company.id]).env
for xml_name, model_name, values in self._product_currency_demo_records(env):
self._create_demo_record(env, model_name, xml_name, "product_currency", values)
return

_logger.info("Creating product_currency demo data")
for xml_name, model_name, values in self._product_currency_demo_records(self.env):
self._create_demo_record(self.env, model_name, xml_name, "product_currency", values)

@api.model
def _product_currency_demo_records(self, env):
"""Retorna lista de (xml_name, model, values)."""
usd_id = env.ref("base.USD").id
eur_id = env.ref("base.EUR").id
return [
(
"demo_product_usd",
"product.template",
{
"name": "Product USD",
"type": "service",
"list_price": 100.0,
"currency_id": usd_id,
"force_currency_id": usd_id,
},
),
(
"product_with_forced_currency",
"product.template",
{
"name": "Product with forced currency (EUR)",
"categ_id": env.ref("product.product_category_goods").id,
"standard_price": 50.0,
"list_price": 100.0,
"currency_id": usd_id,
"force_currency_id": eur_id,
"type": "consu",
},
),
]

@api.model
def _create_demo_record(self, env, model_name, xml_name, module, values):
"""Crea o actualiza un registro demo con su XML ID. Idempotente y forzando valores."""
IrModelData = env["ir.model.data"]

existing = IrModelData.search(
[
("module", "=", module),
("name", "=", xml_name),
("model", "=", model_name),
],
limit=1,
)
if existing:
record = env[model_name].browse(existing.res_id)
record.sudo().write(values)
else:
record = env[model_name].sudo().create(values)
IrModelData.sudo().create(
{
"module": module,
"name": xml_name,
"model": model_name,
"res_id": record.id,
"noupdate": True,
}
)
return record
12 changes: 2 additions & 10 deletions product_currency/demo/product_product_demo.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">
<record id="product_with_forced_currency" model="product.product">
<field name="name">Product with forced currency (EUR)</field>
<field name="categ_id" ref="product.product_category_goods"/>
<field name="standard_price">50</field>
<field name="list_price">100</field>
<field name="currency_id" ref="base.USD"/>
<field name="force_currency_id" ref="base.EUR"/>
<field name="type">consu</field>
</record>
<odoo>
<function model="product.template" name="_install_product_currency_demo"/>
</odoo>
1 change: 1 addition & 0 deletions product_currency/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_product_currency_demo
25 changes: 25 additions & 0 deletions product_currency/tests/test_product_currency_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from odoo.tests import TransactionCase, tagged


@tagged("post_install", "-at_install")
class TestProductCurrencyDemo(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Ejecutar el hook de demo data explícitamente
cls.env["product.template"]._install_product_currency_demo([cls.env.company])
cls.demo_product = cls.env.ref("product_currency.demo_product_usd", raise_if_not_found=False)

def test_demo_product_exists(self):
"""El producto demo debe existir y tener force_currency_id seteado."""
self.assertTrue(self.demo_product, "No se encontró el producto demo")
self.assertTrue(self.demo_product.force_currency_id, "El producto demo debe tener force_currency_id")

def test_force_currency_applies(self):
"""currency_id debe ser igual a force_currency_id en el demo."""
self.assertEqual(
self.demo_product.force_currency_id.id,
self.demo_product.currency_id.id,
"currency_id debe ser igual a force_currency_id en el demo",
)
# Copiamos el producto y forzamos el campo force_currency_id
Loading