| 1 |
|
| 2 |
|
| 3 |
from odoo.http import request, route |
| 4 |
from odoo.tools import float_is_zero |
| 5 |
|
| 6 |
from odoo.addons.sale.controllers.product_configurator import SaleProductConfiguratorController |
| 7 |
from odoo.addons.website_sale.controllers.main import WebsiteSale |
| 8 |
|
| 9 |
|
| 10 |
class WebsiteSaleProductConfiguratorController(SaleProductConfiguratorController, WebsiteSale): |
| 11 |
|
| 12 |
@route( |
| 13 |
route='/website_sale/should_show_product_configurator', |
| 14 |
type='jsonrpc', |
| 15 |
auth='public', |
| 16 |
website=True, |
| 17 |
readonly=True, |
| 18 |
) |
| 19 |
def website_sale_should_show_product_configurator( |
| 20 |
self, product_template_id, ptav_ids, is_product_configured |
| 21 |
): |
| 22 |
""" Return whether the product configurator dialog should be shown. |
| 23 |
|
| 24 |
:param int product_template_id: The product being checked, as a `product.template` id. |
| 25 |
:param list(int) ptav_ids: The combination of the product, as a list of |
| 26 |
`product.template.attribute.value` ids. |
| 27 |
:param bool is_product_configured: Whether the product is already configured. |
| 28 |
:rtype: bool |
| 29 |
:return: Whether the product configurator dialog should be shown. |
| 30 |
""" |
| 31 |
product_template = request.env['product.template'].browse(product_template_id) |
| 32 |
combination = request.env['product.template.attribute.value'].browse(ptav_ids) |
| 33 |
single_product_variant = product_template.get_single_product_variant() |
| 34 |
|
| 35 |
|
| 36 |
has_optional_products = bool(product_template.optional_product_ids.filtered( |
| 37 |
lambda op: self._should_show_product(op, combination) |
| 38 |
)) |
| 39 |
return ( |
| 40 |
has_optional_products |
| 41 |
or not (single_product_variant.get('product_id') or is_product_configured) |
| 42 |
) |
| 43 |
|
| 44 |
def _get_product_template(self, product_template_id): |
| 45 |
if request.is_frontend: |
| 46 |
combo_item = request.env['product.combo.item'].sudo().search([ |
| 47 |
('product_id.product_tmpl_id.id', '=', product_template_id), |
| 48 |
]) |
| 49 |
if combo_item and request.env['product.template'].sudo().search_count([ |
| 50 |
('combo_ids', 'in', combo_item.mapped('combo_id.id')), |
| 51 |
('website_published', '=', True), |
| 52 |
]): |
| 53 |
return request.env['product.template'].sudo().browse(product_template_id) |
| 54 |
return super()._get_product_template(product_template_id) |
| 55 |
|
| 56 |
@route( |
| 57 |
route='/website_sale/product_configurator/get_values', |
| 58 |
type='jsonrpc', |
| 59 |
auth='public', |
| 60 |
website=True, |
| 61 |
readonly=True, |
| 62 |
) |
| 63 |
def website_sale_product_configurator_get_values(self, *args, **kwargs): |
| 64 |
self._populate_currency_and_pricelist(kwargs) |
| 65 |
return super().sale_product_configurator_get_values(*args, **kwargs) |
| 66 |
|
| 67 |
@route( |
| 68 |
route='/website_sale/product_configurator/create_product', |
| 69 |
type='jsonrpc', |
| 70 |
auth='public', |
| 71 |
methods=['POST'], |
| 72 |
website=True, |
| 73 |
) |
| 74 |
def website_sale_product_configurator_create_product(self, *args, **kwargs): |
| 75 |
return super().sale_product_configurator_create_product(*args, **kwargs) |
| 76 |
|
| 77 |
@route( |
| 78 |
route='/website_sale/product_configurator/update_combination', |
| 79 |
type='jsonrpc', |
| 80 |
auth='public', |
| 81 |
methods=['POST'], |
| 82 |
website=True, |
| 83 |
readonly=True, |
| 84 |
) |
| 85 |
def website_sale_product_configurator_update_combination(self, *args, **kwargs): |
| 86 |
self._populate_currency_and_pricelist(kwargs) |
| 87 |
return super().sale_product_configurator_update_combination(*args, **kwargs) |
| 88 |
|
| 89 |
@route( |
| 90 |
route='/website_sale/product_configurator/get_optional_products', |
| 91 |
type='jsonrpc', |
| 92 |
auth='public', |
| 93 |
website=True, |
| 94 |
readonly=True, |
| 95 |
) |
| 96 |
def website_sale_product_configurator_get_optional_products(self, *args, **kwargs): |
| 97 |
self._populate_currency_and_pricelist(kwargs) |
| 98 |
return super().sale_product_configurator_get_optional_products(*args, **kwargs) |
| 99 |
|
| 100 |
def _get_basic_product_information( |
| 101 |
self, product_or_template, pricelist, combination, currency=None, date=None, **kwargs |
| 102 |
): |
| 103 |
""" Override of `sale` to append website data and apply taxes. |
| 104 |
|
| 105 |
:param product.product|product.template product_or_template: The product for which to seek |
| 106 |
information. |
| 107 |
:param product.pricelist pricelist: The pricelist to use. |
| 108 |
:param product.template.attribute.value combination: The combination of the product. |
| 109 |
:param res.currency|None currency: The currency of the transaction. |
| 110 |
:param datetime|None date: The date of the `sale.order`, to compute the price at the right |
| 111 |
rate. |
| 112 |
:param dict kwargs: Locally unused data passed to `super`. |
| 113 |
:rtype: dict |
| 114 |
:return: A dict with the following structure: |
| 115 |
{ |
| 116 |
... # fields from `super`. |
| 117 |
'price': float, |
| 118 |
'can_be_sold': bool, |
| 119 |
'category_name': str, |
| 120 |
'currency_name': str, |
| 121 |
'strikethrough_price': float, # if there's a strikethrough_price to display. |
| 122 |
} |
| 123 |
""" |
| 124 |
basic_product_information = super()._get_basic_product_information( |
| 125 |
product_or_template.with_context(display_default_code=not request.is_frontend), |
| 126 |
pricelist, |
| 127 |
combination, |
| 128 |
currency=currency, |
| 129 |
date=date, |
| 130 |
**kwargs, |
| 131 |
) |
| 132 |
|
| 133 |
if request.is_frontend: |
| 134 |
has_zero_price = float_is_zero( |
| 135 |
basic_product_information['price'], precision_rounding=currency.rounding |
| 136 |
) |
| 137 |
basic_product_information['can_be_sold'] = not ( |
| 138 |
request.website.prevent_zero_price_sale and has_zero_price |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
strikethrough_price = self._get_strikethrough_price( |
| 143 |
product_or_template.with_context( |
| 144 |
**product_or_template._get_product_price_context(combination) |
| 145 |
), |
| 146 |
currency, |
| 147 |
date, |
| 148 |
basic_product_information['price'], |
| 149 |
basic_product_information['pricelist_rule_id'], |
| 150 |
) if 'price_info' not in basic_product_information else None |
| 151 |
if strikethrough_price: |
| 152 |
basic_product_information['strikethrough_price'] = strikethrough_price |
| 153 |
return basic_product_information |
| 154 |
|
| 155 |
def _get_ptav_price_extra(self, ptav, currency, date, product_or_template): |
| 156 |
""" Override of `sale` to apply taxes. |
| 157 |
|
| 158 |
:param product.template.attribute.value ptav: The product template attribute value for which |
| 159 |
to compute the extra price. |
| 160 |
:param res.currency currency: The currency to compute the extra price in. |
| 161 |
:param datetime date: The date to compute the extra price at. |
| 162 |
:param product.product|product.template product_or_template: The product on which the |
| 163 |
product template attribute value applies. |
| 164 |
:rtype: float |
| 165 |
:return: The extra price for the product template attribute value. |
| 166 |
""" |
| 167 |
price_extra = super()._get_ptav_price_extra(ptav, currency, date, product_or_template) |
| 168 |
if request.is_frontend: |
| 169 |
return self._apply_taxes_to_price(price_extra, product_or_template, currency) |
| 170 |
return price_extra |
| 171 |
|
| 172 |
def _get_strikethrough_price(self, product_or_template, currency, date, price, pricelist_rule_id=None): |
| 173 |
""" Return the strikethrough price of the product, if there is one. |
| 174 |
|
| 175 |
:param product.product|product.template product_or_template: The product for which to |
| 176 |
compute the strikethrough price. |
| 177 |
:param res.currency currency: The currency to compute the strikethrough price in. |
| 178 |
:param datetime date: The date to compute the strikethrough price at. |
| 179 |
:param float price: The actual price of the product. |
| 180 |
:rtype: float|None |
| 181 |
:return: The strikethrough price of the product, if there is one. |
| 182 |
""" |
| 183 |
pricelist_rule = request.env['product.pricelist.item'].browse(pricelist_rule_id) |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if pricelist_rule._show_discount_on_shop(): |
| 188 |
pricelist_base_price = self._apply_taxes_to_price( |
| 189 |
pricelist_rule._compute_price_before_discount( |
| 190 |
product=product_or_template, |
| 191 |
quantity=1.0, |
| 192 |
uom=product_or_template.uom_id, |
| 193 |
date=date, |
| 194 |
currency=currency, |
| 195 |
), |
| 196 |
product_or_template, |
| 197 |
currency, |
| 198 |
) |
| 199 |
|
| 200 |
if currency.compare_amounts(pricelist_base_price, price) == 1: |
| 201 |
return pricelist_base_price |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
if ( |
| 206 |
request.env['res.groups']._is_feature_enabled('website_sale.group_product_price_comparison') |
| 207 |
and product_or_template.compare_list_price |
| 208 |
): |
| 209 |
compare_list_price = product_or_template.currency_id._convert( |
| 210 |
from_amount=product_or_template.compare_list_price, |
| 211 |
to_currency=currency, |
| 212 |
company=request.env.company, |
| 213 |
date=date, |
| 214 |
round=False, |
| 215 |
) |
| 216 |
|
| 217 |
if currency.compare_amounts(compare_list_price, price) == 1: |
| 218 |
return compare_list_price |
| 219 |
return None |
| 220 |
|
| 221 |
def _should_show_product(self, product_template, parent_combination): |
| 222 |
""" Override of `sale` to only show products that can be added to the cart. |
| 223 |
|
| 224 |
:param product.template product_template: The product being checked. |
| 225 |
:param product.template.attribute.value parent_combination: The combination of the parent |
| 226 |
product. |
| 227 |
:rtype: bool |
| 228 |
:return: Whether the product should be shown in the configurator. |
| 229 |
""" |
| 230 |
should_show_product = super()._should_show_product(product_template, parent_combination) |
| 231 |
if request.is_frontend: |
| 232 |
return ( |
| 233 |
should_show_product |
| 234 |
and product_template._is_add_to_cart_possible(parent_combination) |
| 235 |
and product_template.filtered_domain(request.website.website_domain()) |
| 236 |
) |
| 237 |
return should_show_product |
| 238 |
|
| 239 |
@staticmethod |
| 240 |
def _apply_taxes_to_price(price, product_or_template, currency): |
| 241 |
product_taxes = product_or_template.sudo().taxes_id._filter_taxes_by_company( |
| 242 |
request.env.company |
| 243 |
) |
| 244 |
if product_taxes: |
| 245 |
taxes = request.fiscal_position.map_tax(product_taxes) |
| 246 |
return request.env['product.template']._apply_taxes_to_price( |
| 247 |
price, currency, product_taxes, taxes, product_or_template, website=request.website |
| 248 |
) |
| 249 |
return price |