Skip to content

Commit

Permalink
[11.0][FIX] product_pricelist_supplierinfo: Add template compatibility (
Browse files Browse the repository at this point in the history
OCA#436)

Previously, only worked fine with product.product.
  • Loading branch information
carlosdauden authored and mdurepos committed Jun 26, 2024
1 parent e2ac643 commit eac3ddb
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 64 deletions.
56 changes: 53 additions & 3 deletions product_pricelist_supplierinfo/i18n/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ msgstr ""
"Plural-Forms: \n"

#. module: product_pricelist_supplierinfo
#: model:ir.ui.view,arch_db:product_pricelist_supplierinfo.product_pricelist_supplier_item_form_view
#: model_terms:ir.ui.view,arch_db:product_pricelist_supplierinfo.product_pricelist_supplier_item_form_view
msgid ""
"<span attrs=\"{'invisible':[('base', '!=', 'supplierinfo')]}\">Supplier "
"price - </span>"
Expand All @@ -26,21 +26,71 @@ msgstr ""
"proveedor - </span>"

#. module: product_pricelist_supplierinfo
#: model:ir.model.fields,field_description:product_pricelist_supplierinfo.field_product_pricelist_item_no_supplierinfo_min_quantity
#: model:ir.model.fields,help:product_pricelist_supplierinfo.field_product_pricelist_item__base
msgid ""
"Base price for computation.\n"
"Public Price: The base price will be the Sale/public Price.\n"
"Cost Price : The base price will be the cost price.\n"
"Other Pricelist : Computation of the base price based on another Pricelist."
msgstr ""

#. module: product_pricelist_supplierinfo
#: model:ir.model.fields,field_description:product_pricelist_supplierinfo.field_product_pricelist_item__base
msgid "Based on"
msgstr ""

#. module: product_pricelist_supplierinfo
#: selection:product.pricelist.item,base:0
msgid "Cost"
msgstr ""

#. module: product_pricelist_supplierinfo
#: model:ir.model.fields,field_description:product_pricelist_supplierinfo.field_product_pricelist_item__no_supplierinfo_min_quantity
msgid "Ignore Supplier Info Min. Quantity"
msgstr "Ignorar cantidad mínima de la info de proveedor"

#. module: product_pricelist_supplierinfo
#: selection:product.pricelist.item,base:0
#, fuzzy
#| msgid "Pricelist"
msgid "Other Pricelist"
msgstr "Lista de precios"

#. module: product_pricelist_supplierinfo
#: selection:product.pricelist.item,base:0
msgid "Partner Prices on the product form"
msgstr ""

#. module: product_pricelist_supplierinfo
#: model:ir.model,name:product_pricelist_supplierinfo.model_product_pricelist
msgid "Pricelist"
msgstr "Lista de precios"

#. module: product_pricelist_supplierinfo
#: model:ir.model,name:product_pricelist_supplierinfo.model_product_pricelist_item
msgid "Pricelist item"
#, fuzzy
#| msgid "Pricelist item"
msgid "Pricelist Item"
msgstr "Item de Lista de precios"

#. module: product_pricelist_supplierinfo
#: selection:product.pricelist.item,base:0
msgid "Prices based on supplier info"
msgstr ""

#. module: product_pricelist_supplierinfo
#: model:ir.model,name:product_pricelist_supplierinfo.model_product_product
msgid "Product"
msgstr "Producto"

#. module: product_pricelist_supplierinfo
#: model:ir.model,name:product_pricelist_supplierinfo.model_product_template
#, fuzzy
#| msgid "Product"
msgid "Product Template"
msgstr "Producto"

#. module: product_pricelist_supplierinfo
#: selection:product.pricelist.item,base:0
msgid "Public Price"
msgstr ""
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ msgstr ""
msgid "Product"
msgstr ""

#. module: product_pricelist_supplierinfo
#: model:ir.model,name:product_pricelist_supplierinfo.model_product_template
msgid "Product Template"
msgstr ""

#. module: product_pricelist_supplierinfo
#: selection:product.pricelist.item,base:0
msgid "Public Price"
Expand Down
3 changes: 2 additions & 1 deletion product_pricelist_supplierinfo/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import product_product
from . import product_pricelist
from . import product_product
from . import product_template
67 changes: 7 additions & 60 deletions product_pricelist_supplierinfo/models/product_product.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,19 @@
# Copyright 2018 Tecnativa - Vicent Cubells
# Copyright 2018 Tecnativa - Pedro M. Baeza
# Copyright 2019 Tecnativa - Carlos Dauden
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, models, tools
from odoo import models


class ProductProduct(models.Model):
_inherit = 'product.product'

def _get_supplierinfo_pricelist_price(self, rule, date=None,
quantity=None):
"""Method for getting the price from supplier info."""
self.ensure_one()
domain = [
'|',
('product_id', '=', self.id),
('product_tmpl_id', '=', self.product_tmpl_id.id),
]
if not rule.no_supplierinfo_min_quantity and quantity:
domain += [
'|',
('min_qty', '=', False),
('min_qty', '<=', quantity),
]
if date:
domain += [
'|',
('date_start', '=', False),
('date_start', '<=', date),
'|',
('date_end', '=', False),
('date_end', '>=', date),
]
# We use a different default order because we are interested in getting
# the price for lowest minimum quantity if no_supplierinfo_min_quantity
supplierinfos = self.env['product.supplierinfo'].search(
domain, order='min_qty,sequence,price',
)
if rule.no_supplierinfo_min_quantity:
price = supplierinfos[:1].price
else:
price = supplierinfos[-1:].price
if price:
# We have to replicate this logic in this method as pricelist
# method are atomic and we can't hack inside.
# Verbatim copy of part of product.pricelist._compute_price_rule.
qty_uom_id = self._context.get('uom') or self.uom_id.id
price_uom = self.env['uom.uom'].browse([qty_uom_id])
convert_to_price_uom = (
lambda price: self.uom_id._compute_price(
price, price_uom))
price_limit = price
price = (price - (price * (rule.price_discount / 100))) or 0.0
if rule.price_round:
price = tools.float_round(
price, precision_rounding=rule.price_round)
if rule.price_surcharge:
price_surcharge = convert_to_price_uom(rule.price_surcharge)
price += price_surcharge
if rule.price_min_margin:
price_min_margin = convert_to_price_uom(rule.price_min_margin)
price = max(price, price_limit + price_min_margin)
if rule.price_max_margin:
price_max_margin = convert_to_price_uom(rule.price_max_margin)
price = min(price, price_limit + price_max_margin)
return price
def _get_supplierinfo_pricelist_price(
self, rule, date=None, quantity=None):
return self.product_tmpl_id._get_supplierinfo_pricelist_price(
rule, date=date, quantity=quantity, product_id=self.id)

@api.multi
def price_compute(self, price_type, uom=False, currency=False,
company=False):
"""Return dummy not falsy prices when computation is done from supplier
Expand All @@ -75,5 +22,5 @@ def price_compute(self, price_type, uom=False, currency=False,
"""
if price_type == 'supplierinfo':
return dict.fromkeys(self.ids, 1.0)
return super(ProductProduct, self).price_compute(
return super().price_compute(
price_type, uom=uom, currency=currency, company=company)
84 changes: 84 additions & 0 deletions product_pricelist_supplierinfo/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2018 Tecnativa - Vicent Cubells
# Copyright 2018 Tecnativa - Pedro M. Baeza
# Copyright 2019 Tecnativa - Carlos Dauden
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import models, tools


class ProductTemplate(models.Model):
_inherit = 'product.template'

def _get_supplierinfo_pricelist_price(
self, rule, date=None, quantity=None, product_id=None):
"""Method for getting the price from supplier info."""
self.ensure_one()
if product_id:
domain = [
'|',
('product_id', '=', product_id),
('product_tmpl_id', '=', self.id),
]
else:
domain = [
('product_tmpl_id', '=', self.id),
]
if not rule.no_supplierinfo_min_quantity and quantity:
domain += [
'|',
('min_qty', '=', False),
('min_qty', '<=', quantity),
]
if date:
domain += [
'|',
('date_start', '=', False),
('date_start', '<=', date),
'|',
('date_end', '=', False),
('date_end', '>=', date),
]
# We use a different default order because we are interested in getting
# the price for lowest minimum quantity if no_supplierinfo_min_quantity
supplierinfos = self.env['product.supplierinfo'].search(
domain, order='min_qty,sequence,price',
)
if rule.no_supplierinfo_min_quantity:
price = supplierinfos[:1].price
else:
price = supplierinfos[-1:].price
if price:
# We have to replicate this logic in this method as pricelist
# method are atomic and we can't hack inside.
# Verbatim copy of part of product.pricelist._compute_price_rule.
qty_uom_id = self._context.get('uom') or self.uom_id.id
price_uom = self.env['uom.uom'].browse([qty_uom_id])
convert_to_price_uom = (
lambda price: self.uom_id._compute_price(
price, price_uom))
price_limit = price
price = (price - (price * (rule.price_discount / 100))) or 0.0
if rule.price_round:
price = tools.float_round(
price, precision_rounding=rule.price_round)
if rule.price_surcharge:
price_surcharge = convert_to_price_uom(rule.price_surcharge)
price += price_surcharge
if rule.price_min_margin:
price_min_margin = convert_to_price_uom(rule.price_min_margin)
price = max(price, price_limit + price_min_margin)
if rule.price_max_margin:
price_max_margin = convert_to_price_uom(rule.price_max_margin)
price = min(price, price_limit + price_max_margin)
return price

def price_compute(self, price_type, uom=False, currency=False,
company=False):
"""Return dummy not falsy prices when computation is done from supplier
info for avoiding error on super method. We will later fill these with
correct values.
"""
if price_type == 'supplierinfo':
return dict.fromkeys(self.ids, 1.0)
return super().price_compute(
price_type, uom=uom, currency=currency, company=company)
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def test_pricelist_based_on_product(self):
self.assertAlmostEqual(
self.pricelist.get_product_price(self.product, 1, False), 10.0,
)
self.assertAlmostEqual(
self.product.product_tmpl_id.with_context(
pricelist=self.pricelist.id).price, 10.0,
)

def test_pricelist_based_on_product_variant(self):
self.pricelist.item_ids[0].write({
Expand All @@ -87,6 +91,9 @@ def test_pricelist_based_on_product_variant(self):
self.assertAlmostEqual(
self.pricelist.get_product_price(self.product, 1, False), 12.5,
)
self.assertAlmostEqual(
self.product.with_context(pricelist=self.pricelist.id).price, 12.5,
)

def test_pricelist_min_quantity(self):
self.assertAlmostEqual(
Expand Down

0 comments on commit eac3ddb

Please sign in to comment.