| 1 |
|
| 2 |
|
| 3 |
from odoo import _, api, fields, models |
| 4 |
from odoo.exceptions import UserError |
| 5 |
|
| 6 |
|
| 7 |
class SaleOrderTemplateLine(models.Model): |
| 8 |
_name = 'sale.order.template.line' |
| 9 |
_description = "Quotation Template Line" |
| 10 |
_order = 'sale_order_template_id, sequence, id' |
| 11 |
|
| 12 |
_accountable_product_id_required = models.Constraint( |
| 13 |
'CHECK(display_type IS NOT NULL OR (product_id IS NOT NULL AND product_uom_id IS NOT NULL))', |
| 14 |
'Missing required product and UoM on accountable sale quote line.', |
| 15 |
) |
| 16 |
_non_accountable_fields_null = models.Constraint( |
| 17 |
'CHECK(display_type IS NULL OR (product_id IS NULL AND product_uom_qty = 0 AND product_uom_id IS NULL))', |
| 18 |
'Forbidden product, quantity and UoM on non-accountable sale quote line', |
| 19 |
) |
| 20 |
|
| 21 |
sale_order_template_id = fields.Many2one( |
| 22 |
comodel_name='sale.order.template', |
| 23 |
string='Quotation Template Reference', |
| 24 |
index=True, required=True, |
| 25 |
ondelete='cascade') |
| 26 |
sequence = fields.Integer( |
| 27 |
string="Sequence", |
| 28 |
help="Gives the sequence order when displaying a list of sale quote lines.", |
| 29 |
default=10) |
| 30 |
|
| 31 |
company_id = fields.Many2one( |
| 32 |
related='sale_order_template_id.company_id', store=True, index=True) |
| 33 |
|
| 34 |
product_id = fields.Many2one( |
| 35 |
comodel_name='product.product', |
| 36 |
check_company=True, |
| 37 |
domain=lambda self: self._product_id_domain()) |
| 38 |
|
| 39 |
name = fields.Text( |
| 40 |
string="Description", |
| 41 |
translate=True, |
| 42 |
) |
| 43 |
|
| 44 |
allowed_uom_ids = fields.Many2many('uom.uom', compute='_compute_allowed_uom_ids') |
| 45 |
product_uom_id = fields.Many2one( |
| 46 |
comodel_name='uom.uom', |
| 47 |
string="Unit", |
| 48 |
domain="[('id', 'in', allowed_uom_ids)]", |
| 49 |
compute='_compute_product_uom_id', |
| 50 |
store=True, readonly=False, precompute=True) |
| 51 |
product_uom_qty = fields.Float( |
| 52 |
string='Quantity', |
| 53 |
required=True, |
| 54 |
digits='Product Unit', |
| 55 |
default=1) |
| 56 |
|
| 57 |
display_type = fields.Selection([ |
| 58 |
('line_section', "Section"), |
| 59 |
('line_subsection', "Subsection"), |
| 60 |
('line_note', "Note")], default=False) |
| 61 |
|
| 62 |
|
| 63 |
parent_id = fields.Many2one( |
| 64 |
string="Parent Section Line", |
| 65 |
comodel_name='sale.order.template.line', |
| 66 |
compute='_compute_parent_id', |
| 67 |
) |
| 68 |
is_optional = fields.Boolean( |
| 69 |
string="Optional Line", |
| 70 |
copy=True, |
| 71 |
default=False, |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
@api.depends('product_id', 'product_id.uom_id', 'product_id.uom_ids') |
| 77 |
def _compute_allowed_uom_ids(self): |
| 78 |
for option in self: |
| 79 |
option.allowed_uom_ids = option.product_id.uom_id | option.product_id.uom_ids |
| 80 |
|
| 81 |
@api.depends('product_id') |
| 82 |
def _compute_product_uom_id(self): |
| 83 |
for option in self: |
| 84 |
option.product_uom_id = option.product_id.uom_id |
| 85 |
|
| 86 |
def _compute_parent_id(self): |
| 87 |
option_lines = set(self) |
| 88 |
for template, lines in self.grouped('sale_order_template_id').items(): |
| 89 |
if not template: |
| 90 |
lines.parent_id = False |
| 91 |
continue |
| 92 |
last_section = False |
| 93 |
last_sub = False |
| 94 |
for line in template.sale_order_template_line_ids.sorted('sequence'): |
| 95 |
if line.display_type == 'line_section': |
| 96 |
last_section = line |
| 97 |
if line in option_lines: |
| 98 |
line.parent_id = False |
| 99 |
last_sub = False |
| 100 |
elif line.display_type == 'line_subsection': |
| 101 |
if line in option_lines: |
| 102 |
line.parent_id = last_section |
| 103 |
last_sub = line |
| 104 |
elif line in option_lines: |
| 105 |
line.parent_id = last_sub or last_section |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
@api.model_create_multi |
| 110 |
def create(self, vals_list): |
| 111 |
for vals in vals_list: |
| 112 |
if vals.get('display_type', self.default_get(['display_type'])['display_type']): |
| 113 |
vals.update(product_id=False, product_uom_qty=0, product_uom_id=False) |
| 114 |
return super().create(vals_list) |
| 115 |
|
| 116 |
def write(self, vals): |
| 117 |
if 'display_type' in vals and self.filtered(lambda line: line.display_type != vals.get('display_type')): |
| 118 |
raise UserError(_("You cannot change the type of a sale quote line. Instead you should delete the current line and create a new line of the proper type.")) |
| 119 |
return super().write(vals) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
@api.model |
| 124 |
def _product_id_domain(self): |
| 125 |
""" Returns the domain of the products that can be added to the template. """ |
| 126 |
return [('sale_ok', '=', True), ('type', '!=', 'combo')] |
| 127 |
|
| 128 |
def _prepare_order_line_values(self): |
| 129 |
""" Give the values to create the corresponding order line. |
| 130 |
|
| 131 |
:return: `sale.order.line` create values |
| 132 |
:rtype: dict |
| 133 |
""" |
| 134 |
self.ensure_one() |
| 135 |
vals = { |
| 136 |
'display_type': self.display_type, |
| 137 |
'product_id': self.product_id.id, |
| 138 |
'product_uom_qty': self.product_uom_qty, |
| 139 |
'product_uom_id': self.product_uom_id.id, |
| 140 |
'is_optional': self.is_optional, |
| 141 |
'sequence': self.sequence, |
| 142 |
} |
| 143 |
if self.name: |
| 144 |
vals['name'] = self.name |
| 145 |
return vals |