Skip to content

Commit

Permalink
[WIP][16.0] product pack new approach to get price
Browse files Browse the repository at this point in the history
  • Loading branch information
augusto-weiss committed Oct 31, 2023
1 parent 0d5653c commit 4a0f3f4
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 39 deletions.
11 changes: 11 additions & 0 deletions product_pack/models/product_pack_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ def _check_recursion(self):
def get_price(self):
self.ensure_one()
return self.product_id.lst_price * self.quantity

def price_compute(
self, price_type, uom=False, currency=False, company=False, date=False
):
pack_line_prices = self.product_id.price_compute(
price_type, uom, currency, company, date
)
for line in self:
pack_line_prices[line.product_id.id] *= line.quantity

return pack_line_prices
59 changes: 29 additions & 30 deletions product_pack/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,11 @@ def price_compute(
pack_price = 0.0
if product.product_tmpl_id.pack_component_price == "detailed":
pack_price = product.list_price
for pack_line in product.sudo().pack_line_ids:
pack_price += pack_line.get_price()
pricelist_id_or_name = self._context.get("pricelist")
# if there is a pricelist on the context the returned prices are on
# that currency but, if the pack product has a different currency
# it will be converted again by pp._compute_price_rule, so if
# that is the case we convert the amounts to the pack currency
if pricelist_id_or_name:
if isinstance(pricelist_id_or_name, list):
pricelist_id_or_name = pricelist_id_or_name[0]
if isinstance(pricelist_id_or_name, str):
pricelist_name_search = self.env["product.pricelist"].name_search(
pricelist_id_or_name, operator="=", limit=1
)
if pricelist_name_search:
pricelist = self.env["product.pricelist"].browse(
[pricelist_name_search[0][0]]
)
elif isinstance(pricelist_id_or_name, int):
pricelist = self.env["product.pricelist"].browse(
pricelist_id_or_name
)
if pricelist and pricelist.currency_id != product.currency_id:
pack_price = pricelist.currency_id._convert(
pack_price,
product.currency_id,
self.company_id or self.env.company,
fields.Date.today(),
)
prices[product.id] = pack_price
pack_line_prices = product.sudo().pack_line_ids.price_compute(
price_type, uom, currency, company, date
)
prices[product.id] = pack_price + sum(pack_line_prices.values())

return prices

@api.depends("list_price", "price_extra")
Expand All @@ -86,3 +61,27 @@ def _compute_product_lst_price(self):
list_price = product.uom_id._compute_price(list_price, to_uom)
product.lst_price = list_price + product.price_extra
return ret_val


class Pricelist(models.Model):
_inherit = "product.pricelist"

def _compute_price_rule(self, products, qty, uom=None, date=False, **kwargs):
self.ensure_one()

packs, no_packs = products.split_pack_products()
res = super()._compute_price_rule(no_packs, qty, uom=uom, date=date, **kwargs)

for pack in packs:
pack_price = 0.0
if pack.pack_component_price == "detailed":
pack_price = pack.list_price
pack_lines = pack.sudo().pack_line_ids
tmp_products = pack_lines.mapped("product_id")
tmp_res = super()._compute_price_rule(
tmp_products, qty, uom=uom, date=date, **kwargs
)
for line in pack_lines:
pack_price += tmp_res[line.product_id.id][0] * line.quantity
res |= dict({pack.id: (pack_price, 0)})
return res
10 changes: 10 additions & 0 deletions sale_product_pack/models/product_pack_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ def get_sale_order_line_vals(self, line, order):
def get_price(self):
self.ensure_one()
return super().get_price() * (1 - self.sale_discount / 100.0)

def price_compute(
self, price_type, uom=False, currency=False, company=False, date=False
):
pack_line_prices = super().price_compute(
price_type, uom, currency, company, date
)
for line in self:
pack_line_prices[line.product_id.id] *= 1 - line.sale_discount / 100.0
return pack_line_prices
29 changes: 29 additions & 0 deletions sale_product_pack/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,32 @@ def action_open_parent_pack_product_view(self):
"view_mode": "tree,form",
"domain": domain,
}

# def _get_pricelist_price(self):
# self.ensure_one()
# self.product_id.ensure_one()

# if not self.product_id.product_tmpl_id.pack_ok \
# or not self.product_id.pack_component_price == "totalized":
# return super()._get_pricelist_price()
# else:
# pack_price = 0.0
# pricelist_rule = self.pricelist_item_id
# pricelist_rule_obj = self.env["product.pricelist.item"]
# order_date = self.order_id.date_order or fields.Date.today()
# qty = self.product_uom_qty or 1.0
# uom = self.product_uom or self.product_id.uom_id
# for pack_line in self.product_id.pack_line_ids:
# product = pack_line.product_id.with_context(
# **self._get_product_price_context()
# )
# pricelist_rule = self.order_id.pricelist_id._get_product_rule(
# product, pack_line.quantity or 1.0, uom, order_date
# )
# pack_price += (
# pricelist_rule_obj.browse(pricelist_rule)._compute_price(
# product, qty, uom, order_date, currency=self.currency_id
# )
# * pack_line.quantity
# )
# return pack_price
1 change: 1 addition & 0 deletions website_sale_product_pack/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import variant
from . import main
8 changes: 8 additions & 0 deletions website_sale_product_pack/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo.http import request
from odoo.addons.website_sale.controllers.main import WebsiteSale

class WebsiteSale(WebsiteSale):

def shop(self, page=0, category=None, search='', min_price=0.0, max_price=0.0, ppg=False, **post):
request.update_context(whole_pack_price=True)
return super().shop(page=page, category=category, search=search, min_price=min_price, max_price=max_price, ppg=ppg, **post)
9 changes: 0 additions & 9 deletions website_sale_product_pack/controllers/variant.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
from odoo import http

from odoo.addons.website_sale.controllers.variant import WebsiteSaleVariantController


class WebsiteSaleVariantController(WebsiteSaleVariantController):
@http.route(
["/sale/get_combination_info_website"],
type="json",
auth="public",
methods=["POST"],
website=True,
)
def get_combination_info_website(
self, product_template_id, product_id, combination, add_qty, **kw
):
Expand Down

0 comments on commit 4a0f3f4

Please sign in to comment.