From 4a0f3f43912b2b6ce310bceea6e2bd8ded657ddf Mon Sep 17 00:00:00 2001 From: augusto-weiss Date: Fri, 27 Oct 2023 10:21:23 -0300 Subject: [PATCH] [WIP][16.0] product pack new approach to get price --- product_pack/models/product_pack_line.py | 11 ++++ product_pack/models/product_product.py | 59 +++++++++---------- sale_product_pack/models/product_pack_line.py | 10 ++++ sale_product_pack/models/sale_order_line.py | 29 +++++++++ .../controllers/__init__.py | 1 + website_sale_product_pack/controllers/main.py | 8 +++ .../controllers/variant.py | 9 --- 7 files changed, 88 insertions(+), 39 deletions(-) create mode 100644 website_sale_product_pack/controllers/main.py diff --git a/product_pack/models/product_pack_line.py b/product_pack/models/product_pack_line.py index 5a1057770..7e346749f 100644 --- a/product_pack/models/product_pack_line.py +++ b/product_pack/models/product_pack_line.py @@ -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 diff --git a/product_pack/models/product_product.py b/product_pack/models/product_product.py index 251ff0385..5bf477757 100644 --- a/product_pack/models/product_product.py +++ b/product_pack/models/product_product.py @@ -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") @@ -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 diff --git a/sale_product_pack/models/product_pack_line.py b/sale_product_pack/models/product_pack_line.py index 522727695..4350cfabb 100644 --- a/sale_product_pack/models/product_pack_line.py +++ b/sale_product_pack/models/product_pack_line.py @@ -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 diff --git a/sale_product_pack/models/sale_order_line.py b/sale_product_pack/models/sale_order_line.py index 5c2d79c84..d64593ba6 100644 --- a/sale_product_pack/models/sale_order_line.py +++ b/sale_product_pack/models/sale_order_line.py @@ -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 diff --git a/website_sale_product_pack/controllers/__init__.py b/website_sale_product_pack/controllers/__init__.py index ca58e65bf..80469fe0e 100644 --- a/website_sale_product_pack/controllers/__init__.py +++ b/website_sale_product_pack/controllers/__init__.py @@ -1 +1,2 @@ from . import variant +from . import main diff --git a/website_sale_product_pack/controllers/main.py b/website_sale_product_pack/controllers/main.py new file mode 100644 index 000000000..731c082cf --- /dev/null +++ b/website_sale_product_pack/controllers/main.py @@ -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) diff --git a/website_sale_product_pack/controllers/variant.py b/website_sale_product_pack/controllers/variant.py index 7cb83264a..4d966e99e 100644 --- a/website_sale_product_pack/controllers/variant.py +++ b/website_sale_product_pack/controllers/variant.py @@ -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 ):