| 1 |
|
| 2 |
|
| 3 |
from datetime import datetime |
| 4 |
|
| 5 |
from odoo.http import Controller, request, route |
| 6 |
|
| 7 |
|
| 8 |
class SaleProductConfiguratorController(Controller): |
| 9 |
|
| 10 |
@route(route='/sale/product_configurator/get_values', type='jsonrpc', auth='user', readonly=True) |
| 11 |
def sale_product_configurator_get_values( |
| 12 |
self, |
| 13 |
product_template_id, |
| 14 |
quantity, |
| 15 |
currency_id, |
| 16 |
so_date, |
| 17 |
product_uom_id=None, |
| 18 |
company_id=None, |
| 19 |
pricelist_id=None, |
| 20 |
ptav_ids=None, |
| 21 |
only_main_product=False, |
| 22 |
**kwargs, |
| 23 |
): |
| 24 |
"""Return all product information needed for the product configurator. |
| 25 |
|
| 26 |
:param int product_template_id: The product for which to seek information, as a |
| 27 |
`product.template` id. |
| 28 |
:param int quantity: The quantity of the product. |
| 29 |
:param int currency_id: The currency of the transaction, as a `res.currency` id. |
| 30 |
:param str so_date: The date of the `sale.order`, to compute the price at the right rate. |
| 31 |
:param int|None product_uom_id: The unit of measure of the product, as a `uom.uom` id. |
| 32 |
:param int|None company_id: The company to use, as a `res.company` id. |
| 33 |
:param int|None pricelist_id: The pricelist to use, as a `product.pricelist` id. |
| 34 |
:param list(int)|None ptav_ids: The combination of the product, as a list of |
| 35 |
`product.template.attribute.value` ids. |
| 36 |
:param bool only_main_product: Whether the optional products should be included or not. |
| 37 |
:param dict kwargs: Locally unused data passed to `_get_product_information`. |
| 38 |
:rtype: dict |
| 39 |
:return: A dict containing a list of products and a list of optional products information, |
| 40 |
generated by :meth:`_get_product_information`. |
| 41 |
""" |
| 42 |
if company_id: |
| 43 |
request.update_context(allowed_company_ids=[company_id]) |
| 44 |
product_template = self._get_product_template(product_template_id) |
| 45 |
|
| 46 |
combination = request.env['product.template.attribute.value'] |
| 47 |
if ptav_ids: |
| 48 |
combination = request.env['product.template.attribute.value'].browse(ptav_ids).filtered( |
| 49 |
lambda ptav: ptav.product_tmpl_id.id == product_template_id |
| 50 |
) |
| 51 |
|
| 52 |
unconfigured_ptals = ( |
| 53 |
product_template.attribute_line_ids - combination.attribute_line_id).filtered( |
| 54 |
lambda ptal: ptal.attribute_id.display_type != 'multi') |
| 55 |
combination += unconfigured_ptals.mapped( |
| 56 |
lambda ptal: ptal.product_template_value_ids._only_active()[:1] |
| 57 |
) |
| 58 |
if not combination: |
| 59 |
combination = product_template._get_first_possible_combination() |
| 60 |
currency = request.env['res.currency'].browse(currency_id) |
| 61 |
pricelist = request.env['product.pricelist'].browse(pricelist_id) |
| 62 |
so_date = datetime.fromisoformat(so_date) |
| 63 |
|
| 64 |
return { |
| 65 |
'products': [ |
| 66 |
dict( |
| 67 |
**self._get_product_information( |
| 68 |
product_template, |
| 69 |
combination, |
| 70 |
currency, |
| 71 |
pricelist, |
| 72 |
so_date, |
| 73 |
quantity=quantity, |
| 74 |
product_uom_id=product_uom_id, |
| 75 |
**kwargs, |
| 76 |
), |
| 77 |
) |
| 78 |
], |
| 79 |
'optional_products': [ |
| 80 |
dict( |
| 81 |
**self._get_product_information( |
| 82 |
optional_product_template, |
| 83 |
optional_product_template._get_first_possible_combination( |
| 84 |
parent_combination=combination |
| 85 |
), |
| 86 |
currency, |
| 87 |
pricelist, |
| 88 |
so_date, |
| 89 |
|
| 90 |
parent_combination=product_template.attribute_line_ids.\ |
| 91 |
product_template_value_ids, |
| 92 |
**kwargs, |
| 93 |
), |
| 94 |
parent_product_tmpl_id=product_template.id, |
| 95 |
) for optional_product_template in product_template.optional_product_ids if |
| 96 |
self._should_show_product(optional_product_template, combination) |
| 97 |
] if not only_main_product else [], |
| 98 |
'currency_id': currency_id, |
| 99 |
} |
| 100 |
|
| 101 |
@route( |
| 102 |
route='/sale/product_configurator/create_product', |
| 103 |
type='jsonrpc', |
| 104 |
auth='user', |
| 105 |
methods=['POST'], |
| 106 |
) |
| 107 |
def sale_product_configurator_create_product(self, product_template_id, ptav_ids): |
| 108 |
"""Create the product when there is a dynamic attribute in the combination. |
| 109 |
|
| 110 |
:param int product_template_id: The product for which to seek information, as a |
| 111 |
`product.template` id. |
| 112 |
:param list(int) ptav_ids: The combination of the product, as a list of |
| 113 |
`product.template.attribute.value` ids. |
| 114 |
:rtype: int |
| 115 |
:return: The product created, as a `product.product` id. |
| 116 |
""" |
| 117 |
product_template = self._get_product_template(product_template_id) |
| 118 |
combination = request.env['product.template.attribute.value'].browse(ptav_ids) |
| 119 |
product = product_template._create_product_variant(combination) |
| 120 |
return product.id |
| 121 |
|
| 122 |
@route( |
| 123 |
route='/sale/product_configurator/update_combination', |
| 124 |
type='jsonrpc', |
| 125 |
auth='user', |
| 126 |
methods=['POST'], |
| 127 |
readonly=True, |
| 128 |
) |
| 129 |
def sale_product_configurator_update_combination( |
| 130 |
self, |
| 131 |
product_template_id, |
| 132 |
ptav_ids, |
| 133 |
currency_id, |
| 134 |
so_date, |
| 135 |
quantity, |
| 136 |
product_uom_id=None, |
| 137 |
company_id=None, |
| 138 |
pricelist_id=None, |
| 139 |
**kwargs, |
| 140 |
): |
| 141 |
"""Return the updated combination information. |
| 142 |
|
| 143 |
:param int product_template_id: The product for which to seek information, as a |
| 144 |
`product.template` id. |
| 145 |
:param list(int) ptav_ids: The combination of the product, as a list of |
| 146 |
`product.template.attribute.value` ids. |
| 147 |
:param int currency_id: The currency of the transaction, as a `res.currency` id. |
| 148 |
:param str so_date: The date of the `sale.order`, to compute the price at the right rate. |
| 149 |
:param int quantity: The quantity of the product. |
| 150 |
:param int|None product_uom_id: The unit of measure of the product, as a `uom.uom` id. |
| 151 |
:param int|None company_id: The company to use, as a `res.company` id. |
| 152 |
:param int|None pricelist_id: The pricelist to use, as a `product.pricelist` id. |
| 153 |
:param dict kwargs: Locally unused data passed to `_get_basic_product_information`. |
| 154 |
:rtype: dict |
| 155 |
:return: Basic informations about a product, generated by |
| 156 |
:meth:`_get_basic_product_information`. |
| 157 |
""" |
| 158 |
if company_id: |
| 159 |
request.update_context(allowed_company_ids=[company_id]) |
| 160 |
product_template = self._get_product_template(product_template_id) |
| 161 |
pricelist = request.env['product.pricelist'].browse(pricelist_id) |
| 162 |
product_uom = request.env['uom.uom'].browse(product_uom_id) |
| 163 |
currency = request.env['res.currency'].browse(currency_id) |
| 164 |
combination = request.env['product.template.attribute.value'].browse(ptav_ids) |
| 165 |
product = product_template._get_variant_for_combination(combination) |
| 166 |
|
| 167 |
values = self._get_basic_product_information( |
| 168 |
product or product_template, |
| 169 |
pricelist, |
| 170 |
combination, |
| 171 |
quantity=quantity or 0.0, |
| 172 |
uom=product_uom, |
| 173 |
currency=currency, |
| 174 |
date=datetime.fromisoformat(so_date), |
| 175 |
**kwargs, |
| 176 |
) |
| 177 |
|
| 178 |
values.pop('pricelist_rule_id', None) |
| 179 |
return values |
| 180 |
|
| 181 |
@route( |
| 182 |
route='/sale/product_configurator/get_optional_products', |
| 183 |
type='jsonrpc', |
| 184 |
auth='user', |
| 185 |
readonly=True, |
| 186 |
) |
| 187 |
def sale_product_configurator_get_optional_products( |
| 188 |
self, |
| 189 |
product_template_id, |
| 190 |
ptav_ids, |
| 191 |
parent_ptav_ids, |
| 192 |
currency_id, |
| 193 |
so_date, |
| 194 |
company_id=None, |
| 195 |
pricelist_id=None, |
| 196 |
**kwargs, |
| 197 |
): |
| 198 |
"""Return information about optional products for the given `product.template`. |
| 199 |
|
| 200 |
:param int product_template_id: The product for which to seek information, as a |
| 201 |
`product.template` id. |
| 202 |
:param list(int) ptav_ids: The combination of the product, as a list of |
| 203 |
`product.template.attribute.value` ids. |
| 204 |
:param list(int) parent_ptav_ids: The combination of the parent product, as a list of |
| 205 |
`product.template.attribute.value` ids. |
| 206 |
:param int currency_id: The currency of the transaction, as a `res.currency` id. |
| 207 |
:param str so_date: The date of the `sale.order`, to compute the price at the right rate. |
| 208 |
:param int|None company_id: The company to use, as a `res.company` id. |
| 209 |
:param int|None pricelist_id: The pricelist to use, as a `product.pricelist` id. |
| 210 |
:param dict kwargs: Locally unused data passed to `_get_product_information`. |
| 211 |
:rtype: [dict] |
| 212 |
:return: A list of optional products information, generated by |
| 213 |
:meth:`_get_product_information`. |
| 214 |
""" |
| 215 |
if company_id: |
| 216 |
request.update_context(allowed_company_ids=[company_id]) |
| 217 |
product_template = self._get_product_template(product_template_id) |
| 218 |
parent_combination = request.env['product.template.attribute.value'].browse( |
| 219 |
parent_ptav_ids + ptav_ids |
| 220 |
) |
| 221 |
currency = request.env['res.currency'].browse(currency_id) |
| 222 |
pricelist = request.env['product.pricelist'].browse(pricelist_id) |
| 223 |
return [ |
| 224 |
dict( |
| 225 |
**self._get_product_information( |
| 226 |
optional_product_template, |
| 227 |
optional_product_template._get_first_possible_combination( |
| 228 |
parent_combination=parent_combination |
| 229 |
), |
| 230 |
currency, |
| 231 |
pricelist, |
| 232 |
datetime.fromisoformat(so_date), |
| 233 |
parent_combination=parent_combination, |
| 234 |
**kwargs, |
| 235 |
), |
| 236 |
parent_product_tmpl_id=product_template.id, |
| 237 |
) for optional_product_template in product_template.optional_product_ids if |
| 238 |
self._should_show_product(optional_product_template, parent_combination) |
| 239 |
] |
| 240 |
|
| 241 |
def _get_product_template(self, product_template_id): |
| 242 |
return request.env['product.template'].browse(product_template_id) |
| 243 |
|
| 244 |
def _get_product_information( |
| 245 |
self, |
| 246 |
product_template, |
| 247 |
combination, |
| 248 |
currency, |
| 249 |
pricelist, |
| 250 |
so_date, |
| 251 |
quantity=1, |
| 252 |
product_uom_id=None, |
| 253 |
parent_combination=None, |
| 254 |
show_packaging=True, |
| 255 |
**kwargs, |
| 256 |
): |
| 257 |
"""Return complete information about a product. |
| 258 |
|
| 259 |
:param product.template product_template: The product for which to seek information. |
| 260 |
:param product.template.attribute.value combination: The combination of the product. |
| 261 |
:param res.currency currency: The currency of the transaction. |
| 262 |
:param product.pricelist pricelist: The pricelist to use. |
| 263 |
:param datetime so_date: The date of the `sale.order`, to compute the price at the right |
| 264 |
rate. |
| 265 |
:param int quantity: The quantity of the product. |
| 266 |
:param int|None product_uom_id: The unit of measure of the product, as a `uom.uom` id. |
| 267 |
:param product.template.attribute.value|None parent_combination: The combination of the |
| 268 |
parent product. |
| 269 |
:param dict kwargs: Locally unused data passed to `_get_basic_product_information`. |
| 270 |
:rtype: dict |
| 271 |
:return: A dict with the following structure: |
| 272 |
{ |
| 273 |
'product_tmpl_id': int, |
| 274 |
'id': int, |
| 275 |
'description_sale': str|False, |
| 276 |
'display_name': str, |
| 277 |
'price': float, |
| 278 |
'quantity': int |
| 279 |
'attribute_line': [{ |
| 280 |
'id': int |
| 281 |
'attribute': { |
| 282 |
'id': int |
| 283 |
'name': str |
| 284 |
'display_type': str |
| 285 |
}, |
| 286 |
'attribute_value': [{ |
| 287 |
'id': int, |
| 288 |
'name': str, |
| 289 |
'price_extra': float, |
| 290 |
'html_color': str|False, |
| 291 |
'image': str|False, |
| 292 |
'is_custom': bool |
| 293 |
}], |
| 294 |
'selected_attribute_id': int, |
| 295 |
}], |
| 296 |
'exclusions': dict, |
| 297 |
'archived_combination': dict, |
| 298 |
'parent_exclusions': dict, |
| 299 |
'available_uoms': dict (optional), |
| 300 |
} |
| 301 |
""" |
| 302 |
uom = ( |
| 303 |
(product_uom_id and request.env['uom.uom'].browse(product_uom_id)) |
| 304 |
or product_template.uom_id |
| 305 |
) |
| 306 |
product = product_template._get_variant_for_combination(combination) |
| 307 |
attribute_exclusions = product_template._get_attribute_exclusions( |
| 308 |
parent_combination=parent_combination, |
| 309 |
combination_ids=combination.ids, |
| 310 |
) |
| 311 |
product_or_template = product or product_template |
| 312 |
ptals = product_template.attribute_line_ids |
| 313 |
attrs_map = { |
| 314 |
attr_data['id']: attr_data |
| 315 |
for attr_data in ptals.attribute_id.read(['id', 'name', 'display_type']) |
| 316 |
} |
| 317 |
ptavs = ptals.product_template_value_ids.filtered(lambda p: p.ptav_active or combination and p.id in combination.ids) |
| 318 |
ptavs_map = dict(zip(ptavs.ids, ptavs.read(['name', 'html_color', 'image', 'is_custom']))) |
| 319 |
|
| 320 |
values = dict( |
| 321 |
product_tmpl_id=product_template.id, |
| 322 |
**self._get_basic_product_information( |
| 323 |
product_or_template, |
| 324 |
pricelist, |
| 325 |
combination, |
| 326 |
quantity=quantity, |
| 327 |
uom=uom, |
| 328 |
currency=currency, |
| 329 |
date=so_date, |
| 330 |
**kwargs, |
| 331 |
), |
| 332 |
quantity=quantity, |
| 333 |
uom=uom.read(['id', 'display_name'])[0], |
| 334 |
attribute_lines=[{ |
| 335 |
'id': ptal.id, |
| 336 |
'attribute': dict(**attrs_map[ptal.attribute_id.id]), |
| 337 |
'attribute_values': [ |
| 338 |
dict( |
| 339 |
**ptavs_map[ptav.id], |
| 340 |
price_extra=self._get_ptav_price_extra( |
| 341 |
ptav, currency, so_date, product_or_template |
| 342 |
), |
| 343 |
) for ptav in ptal.product_template_value_ids |
| 344 |
if ptav.ptav_active or (combination and ptav.id in combination.ids) |
| 345 |
], |
| 346 |
'selected_attribute_value_ids': combination.filtered( |
| 347 |
lambda c: ptal in c.attribute_line_id |
| 348 |
).ids, |
| 349 |
'create_variant': ptal.attribute_id.create_variant, |
| 350 |
} for ptal in product_template.attribute_line_ids], |
| 351 |
exclusions=attribute_exclusions['exclusions'], |
| 352 |
archived_combinations=attribute_exclusions['archived_combinations'], |
| 353 |
parent_exclusions=attribute_exclusions['parent_exclusions'], |
| 354 |
) |
| 355 |
if show_packaging and product_template._has_multiple_uoms(): |
| 356 |
values['available_uoms'] = product_template._get_available_uoms().read( |
| 357 |
['id', 'display_name'] |
| 358 |
) |
| 359 |
|
| 360 |
values.pop('pricelist_rule_id', None) |
| 361 |
return values |
| 362 |
|
| 363 |
def _get_basic_product_information(self, product_or_template, pricelist, combination, **kwargs): |
| 364 |
"""Return basic information about a product. |
| 365 |
|
| 366 |
:param product.product|product.template product_or_template: The product for which to seek |
| 367 |
information. |
| 368 |
:param product.pricelist pricelist: The pricelist to use. |
| 369 |
:param product.template.attribute.value combination: The combination of the product. |
| 370 |
:param dict kwargs: Locally unused data passed to `_get_product_price`. |
| 371 |
:rtype: dict |
| 372 |
:return: A dict with the following structure: |
| 373 |
{ |
| 374 |
'id': int, # if product_or_template is a record of `product.product`. |
| 375 |
'description_sale': str|False, |
| 376 |
'display_name': str, |
| 377 |
'price': float, |
| 378 |
} |
| 379 |
""" |
| 380 |
basic_information = dict( |
| 381 |
**product_or_template.read(['description_sale', 'display_name'])[0] |
| 382 |
) |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
if not product_or_template.is_product_variant: |
| 387 |
basic_information['id'] = False |
| 388 |
combination_name = combination._get_combination_name() |
| 389 |
if combination_name: |
| 390 |
basic_information.update( |
| 391 |
display_name=f"{basic_information['display_name']} ({combination_name})" |
| 392 |
) |
| 393 |
price, pricelist_rule_id = request.env['product.template']._get_configurator_display_price( |
| 394 |
product_or_template.with_context( |
| 395 |
**product_or_template._get_product_price_context(combination) |
| 396 |
), |
| 397 |
pricelist=pricelist, |
| 398 |
**kwargs, |
| 399 |
) |
| 400 |
return dict( |
| 401 |
**basic_information, |
| 402 |
price=price, |
| 403 |
pricelist_rule_id=pricelist_rule_id, |
| 404 |
**request.env['product.template']._get_additional_configurator_data( |
| 405 |
product_or_template, pricelist=pricelist, **kwargs |
| 406 |
), |
| 407 |
) |
| 408 |
|
| 409 |
def _get_ptav_price_extra(self, ptav, currency, date, product_or_template): |
| 410 |
"""Return the extra price for a product template attribute value. |
| 411 |
|
| 412 |
:param product.template.attribute.value ptav: The product template attribute value for which |
| 413 |
to compute the extra price. |
| 414 |
:param res.currency currency: The currency to compute the extra price in. |
| 415 |
:param datetime date: The date to compute the extra price at. |
| 416 |
:param product.product|product.template product_or_template: The product on which the |
| 417 |
product template attribute value applies. |
| 418 |
:rtype: float |
| 419 |
:return: The extra price for the product template attribute value. |
| 420 |
""" |
| 421 |
return ptav.currency_id._convert( |
| 422 |
ptav.price_extra, |
| 423 |
currency, |
| 424 |
request.env.company, |
| 425 |
date.date(), |
| 426 |
) |
| 427 |
|
| 428 |
def _should_show_product(self, product_template, parent_combination): |
| 429 |
"""Decide whether a product should be shown in the configurator. |
| 430 |
|
| 431 |
:param product.template product_template: The product being checked. |
| 432 |
:param product.template.attribute.value parent_combination: The combination of the parent |
| 433 |
product. |
| 434 |
:rtype: bool |
| 435 |
:return: Whether the product should be shown in the configurator. |
| 436 |
""" |
| 437 |
return True |