|
| 1 | +# Copyright 2024 Cetmix OÜ |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 3 | +from odoo import api, models |
| 4 | + |
| 5 | + |
| 6 | +class SaleOrderLine(models.Model): |
| 7 | + _inherit = "sale.order.line" |
| 8 | + |
| 9 | + @api.model_create_multi |
| 10 | + def create(self, vals_list): |
| 11 | + optional_quantity_enabled = self.env.user.has_group( |
| 12 | + "product_optional_product_quantity.group_product_optional_quantity" |
| 13 | + ) |
| 14 | + if not optional_quantity_enabled: |
| 15 | + return super().create(vals_list) |
| 16 | + res = super().create(vals_list) |
| 17 | + for line in res: |
| 18 | + for product_tmpl in line._get_optional_products(): |
| 19 | + product = self.env["product.product"].search( |
| 20 | + [("product_tmpl_id", "=", product_tmpl.id)] |
| 21 | + )[0] |
| 22 | + option = line.order_id._create_optional_line_if_not_exists( |
| 23 | + product_tmpl, |
| 24 | + product._get_tax_included_unit_price( |
| 25 | + line.company_id, |
| 26 | + line.order_id.currency_id, |
| 27 | + line.order_id.date_order, |
| 28 | + "sale", |
| 29 | + fiscal_position=line.order_id.fiscal_position_id, |
| 30 | + product_currency=line.currency_id, |
| 31 | + ), |
| 32 | + ) |
| 33 | + option._compute_quantity() |
| 34 | + if line._is_optional_product(): |
| 35 | + line.order_id._create_optional_line_if_not_exists( |
| 36 | + line.product_template_id, line.price_unit |
| 37 | + ) |
| 38 | + return res |
| 39 | + |
| 40 | + @api.depends("product_template_id", "order_id.order_line") |
| 41 | + def _compute_product_uom_qty(self): |
| 42 | + optional_quantity_enabled = self.env.user.has_group( |
| 43 | + "product_optional_product_quantity.group_product_optional_quantity" |
| 44 | + ) |
| 45 | + if not optional_quantity_enabled: |
| 46 | + return super()._compute_product_uom_qty() |
| 47 | + for line in self: |
| 48 | + order = line.order_id |
| 49 | + line_product_id = line.product_template_id.id |
| 50 | + order_lines = order.order_line.filtered( |
| 51 | + lambda x: line_product_id |
| 52 | + in (x.product_template_id.optional_product_ids.ids) |
| 53 | + ) |
| 54 | + if not order_lines: |
| 55 | + line.product_uom_qty = line.product_uom_qty |
| 56 | + continue |
| 57 | + |
| 58 | + multiplier = sum( |
| 59 | + order_lines.mapped("product_template_id") |
| 60 | + .mapped("product_optional_line_ids") |
| 61 | + .filtered(lambda x: x.optional_product_tmpl_id.id == line_product_id) |
| 62 | + .mapped("quantity") |
| 63 | + ) |
| 64 | + qty = sum(order_lines.mapped("product_uom_qty")) |
| 65 | + line.product_uom_qty = qty * multiplier |
| 66 | + |
| 67 | + def _is_optional_product(self): |
| 68 | + """ |
| 69 | + Check if product is optional. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + bool: True if product is optional, False otherwise |
| 73 | + """ |
| 74 | + self.ensure_one() |
| 75 | + return bool( |
| 76 | + self.env["product.template"].search_count( |
| 77 | + [("optional_product_ids", "in", self.product_template_id.id)] |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + def _get_optional_products(self): |
| 82 | + """ |
| 83 | + Get product.template recordset with product templates |
| 84 | + related to current line's product as optional products. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + product.template recordset: Optional products |
| 88 | + """ |
| 89 | + self.ensure_one() |
| 90 | + return ( |
| 91 | + self.env["product.optional.line"] |
| 92 | + .search([("product_tmpl_id", "=", self.product_template_id.id)]) |
| 93 | + .mapped("optional_product_tmpl_id") |
| 94 | + ) |
0 commit comments