main.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import http
  4. from odoo.http import request
  5. class ProductConfiguratorController(http.Controller):
  6. @http.route(['/sale_product_configurator/configure'], type='json', auth="user", methods=['POST'])
  7. def configure(self, product_template_id, pricelist_id, **kw):
  8. add_qty = float(kw.get('add_qty', 1))
  9. product_template = request.env['product.template'].browse(int(product_template_id))
  10. pricelist = self._get_pricelist(pricelist_id)
  11. product_combination = False
  12. attribute_value_ids = set(kw.get('product_template_attribute_value_ids', []))
  13. attribute_value_ids |= set(kw.get('product_no_variant_attribute_value_ids', []))
  14. if attribute_value_ids:
  15. product_combination = request.env['product.template.attribute.value'].browse(
  16. attribute_value_ids
  17. ).filtered(
  18. lambda ptav: ptav.product_tmpl_id == product_template
  19. ) # Filter out ptavs not belonging to the given template
  20. # It happens when you change the template on an already configured line
  21. # receiving the configured attributes data from the previous template configuration.
  22. if pricelist:
  23. product_template = product_template.with_context(pricelist=pricelist.id, partner=request.env.user.partner_id)
  24. return request.env['ir.ui.view']._render_template(
  25. "sale_product_configurator.configure",
  26. {
  27. 'product': product_template,
  28. 'pricelist': pricelist,
  29. 'add_qty': add_qty,
  30. 'product_combination': product_combination
  31. },
  32. )
  33. @http.route(['/sale_product_configurator/show_advanced_configurator'], type='json', auth="user", methods=['POST'])
  34. def show_advanced_configurator(self, product_id, variant_values, pricelist_id, **kw):
  35. pricelist = self._get_pricelist(pricelist_id)
  36. return self._show_advanced_configurator(product_id, variant_values, pricelist, False, **kw)
  37. @http.route(['/sale_product_configurator/optional_product_items'], type='json', auth="user", methods=['POST'])
  38. def optional_product_items(self, product_id, pricelist_id, **kw):
  39. pricelist = self._get_pricelist(pricelist_id)
  40. return self._optional_product_items(product_id, pricelist, **kw)
  41. def _optional_product_items(self, product_id, pricelist, **kw):
  42. add_qty = float(kw.get('add_qty', 1))
  43. product = request.env['product.product'].browse(int(product_id))
  44. parent_combination = product.product_template_attribute_value_ids
  45. if product.env.context.get('no_variant_attribute_values'):
  46. # Add "no_variant" attribute values' exclusions
  47. # They are kept in the context since they are not linked to this product variant
  48. parent_combination |= product.env.context.get('no_variant_attribute_values')
  49. return request.env['ir.ui.view']._render_template("sale_product_configurator.optional_product_items", {
  50. 'product': product,
  51. 'parent_name': product.name,
  52. 'parent_combination': parent_combination,
  53. 'pricelist': pricelist,
  54. 'add_qty': add_qty,
  55. })
  56. def _show_advanced_configurator(self, product_id, variant_values, pricelist, handle_stock, **kw):
  57. product = request.env['product.product'].browse(int(product_id))
  58. combination = request.env['product.template.attribute.value'].browse(variant_values)
  59. add_qty = float(kw.get('add_qty', 1))
  60. no_variant_attribute_values = combination.filtered(
  61. lambda product_template_attribute_value: product_template_attribute_value.attribute_id.create_variant == 'no_variant'
  62. )
  63. if no_variant_attribute_values:
  64. product = product.with_context(no_variant_attribute_values=no_variant_attribute_values)
  65. return request.env['ir.ui.view']._render_template("sale_product_configurator.optional_products_modal", {
  66. 'product': product,
  67. 'combination': combination,
  68. 'add_qty': add_qty,
  69. 'parent_name': product.name,
  70. 'variant_values': variant_values,
  71. 'pricelist': pricelist,
  72. 'handle_stock': handle_stock,
  73. 'already_configured': kw.get("already_configured", False),
  74. 'mode': kw.get('mode', 'add'),
  75. 'product_custom_attribute_values': kw.get('product_custom_attribute_values', None),
  76. 'no_attribute': kw.get('no_attribute', False),
  77. 'custom_attribute': kw.get('custom_attribute', False)
  78. })
  79. def _get_pricelist(self, pricelist_id, pricelist_fallback=False):
  80. return request.env['product.pricelist'].browse(int(pricelist_id or 0))