|
| 1 | +from odoo import api, fields, models |
| 2 | + |
| 3 | + |
| 4 | +class ProductAttribute(models.Model): |
| 5 | + _inherit = "product.attribute" |
| 6 | + _order = "sequence" |
| 7 | + |
| 8 | + cpq_propagate_to_variant = fields.Boolean( |
| 9 | + "Propagate To Variant", |
| 10 | + default=True, |
| 11 | + ) |
| 12 | + |
| 13 | + def copy_data(self, default=None): |
| 14 | + default = dict(default or {}) |
| 15 | + vals_list = super().copy_data(default=default) |
| 16 | + if "name" not in default: |
| 17 | + for attribute, vals in zip(self, vals_list, strict=False): |
| 18 | + vals["name"] = self.env._("%s (copy)", attribute.name) |
| 19 | + return vals_list |
| 20 | + |
| 21 | + |
| 22 | +class ProductAttributeValue(models.Model): |
| 23 | + _inherit = "product.attribute.value" |
| 24 | + |
| 25 | + cpq_custom_type = fields.Selection( |
| 26 | + [ |
| 27 | + ("integer", "Integer"), |
| 28 | + ("float", "Float"), |
| 29 | + ("char", "Text"), |
| 30 | + ], |
| 31 | + "Configurable Custom Type", |
| 32 | + ) |
| 33 | + |
| 34 | + def copy_data(self, default=None): |
| 35 | + default = dict(default or {}) |
| 36 | + vals_list = super().copy_data(default=default) |
| 37 | + if "name" not in default: |
| 38 | + for value, vals in zip(self, vals_list, strict=False): |
| 39 | + vals["name"] = self.env._("%s (copy)", value.name) |
| 40 | + return vals_list |
| 41 | + |
| 42 | + def _cpq_cast_custom(self, value): |
| 43 | + """ |
| 44 | + Cast the stored custom_value into the real value. |
| 45 | + i.e. custom_value may store a int, which we need to cast into an Odoo |
| 46 | + record |
| 47 | + """ |
| 48 | + self.ensure_one() |
| 49 | + |
| 50 | + if not self.is_custom or not self.cpq_custom_type: |
| 51 | + return value |
| 52 | + |
| 53 | + method = f"_cpq_cast_custom_{self.cpq_custom_type}" |
| 54 | + return getattr(self, method)(value) |
| 55 | + |
| 56 | + def _cpq_cast_custom_integer(self, value): |
| 57 | + return self._cpq_sanitise_custom_integer(value) |
| 58 | + |
| 59 | + def _cpq_cast_custom_float(self, value): |
| 60 | + return self._cpq_sanitise_custom_float(value) |
| 61 | + |
| 62 | + def _cpq_cast_custom_char(self, value): |
| 63 | + return self._cpq_sanitise_custom_char(value) |
| 64 | + |
| 65 | + def _cpq_sanitise_custom(self, value): |
| 66 | + self.ensure_one() |
| 67 | + |
| 68 | + if not self.is_custom or not self.cpq_custom_type: |
| 69 | + return value |
| 70 | + |
| 71 | + method = f"_cpq_sanitise_custom_{self.cpq_custom_type}" |
| 72 | + return getattr(self, method)(value) |
| 73 | + |
| 74 | + @api.model |
| 75 | + def _cpq_sanitise_custom_integer(self, value): |
| 76 | + return int(value) |
| 77 | + |
| 78 | + @api.model |
| 79 | + def _cpq_sanitise_custom_float(self, value): |
| 80 | + return float(value) |
| 81 | + |
| 82 | + @api.model |
| 83 | + def _cpq_sanitise_custom_char(self, value): |
| 84 | + if not value: |
| 85 | + return "" |
| 86 | + return value.strip() |
| 87 | + |
| 88 | + def _cpq_validate_custom(self, value): |
| 89 | + self.ensure_one() |
| 90 | + |
| 91 | + if not self.is_custom or not self.cpq_custom_type: |
| 92 | + return True |
| 93 | + |
| 94 | + method = f"_cpq_validate_custom_{self.cpq_custom_type}" |
| 95 | + return getattr(self, method)(value) |
| 96 | + |
| 97 | + @api.model |
| 98 | + def _cpq_validate_custom_integer(self, value): |
| 99 | + if isinstance(value, bool): |
| 100 | + return False |
| 101 | + try: |
| 102 | + int(value) |
| 103 | + return True |
| 104 | + except (ValueError, TypeError): |
| 105 | + return False |
| 106 | + |
| 107 | + @api.model |
| 108 | + def _cpq_validate_custom_float(self, value): |
| 109 | + if isinstance(value, bool): |
| 110 | + return False |
| 111 | + try: |
| 112 | + float(value) |
| 113 | + return True |
| 114 | + except (ValueError, TypeError): |
| 115 | + return False |
| 116 | + |
| 117 | + @api.model |
| 118 | + def _cpq_validate_custom_char(self, value): |
| 119 | + return isinstance(value, str) and value.strip() |
| 120 | + |
| 121 | + |
| 122 | +class ProductTemplateAttributeLine(models.Model): |
| 123 | + _inherit = "product.template.attribute.line" |
| 124 | + |
| 125 | + cpq_propagate_to_variant = fields.Boolean( |
| 126 | + related="attribute_id.cpq_propagate_to_variant", store=True |
| 127 | + ) |
| 128 | + |
| 129 | + def _cpq_get_combination_info(self): |
| 130 | + self.ensure_one() |
| 131 | + i = self |
| 132 | + |
| 133 | + return { |
| 134 | + "id": i.id, |
| 135 | + "name": i.display_name, |
| 136 | + "display_type": i.attribute_id.display_type, |
| 137 | + "ptav_ids": [ |
| 138 | + ptav_id._cpq_get_combination_info() |
| 139 | + for ptav_id in i.product_template_value_ids.filtered( |
| 140 | + lambda attr_line: attr_line.ptav_active |
| 141 | + ) |
| 142 | + ], |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | +class ProductTemplateAttributeValue(models.Model): |
| 147 | + _inherit = "product.template.attribute.value" |
| 148 | + |
| 149 | + cpq_propagate_to_variant = fields.Boolean( |
| 150 | + related="attribute_id.cpq_propagate_to_variant" |
| 151 | + ) |
| 152 | + cpq_custom_type = fields.Selection( |
| 153 | + related="product_attribute_value_id.cpq_custom_type" |
| 154 | + ) |
| 155 | + |
| 156 | + def _cpq_get_combination_info(self): |
| 157 | + self.ensure_one() |
| 158 | + ptav_id = self |
| 159 | + |
| 160 | + return { |
| 161 | + "id": ptav_id.id, |
| 162 | + "name": ptav_id.name, |
| 163 | + "html_color": ptav_id.html_color, |
| 164 | + "is_custom": ptav_id.is_custom, |
| 165 | + "price_extra": 0.0, |
| 166 | + "excluded": False, |
| 167 | + "cpq_custom_type": ptav_id.cpq_custom_type, |
| 168 | + } |
0 commit comments