Debian Codegraph @

addons/sale_service/models/sale_order_line.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from itertools import groupby
5
6 from odoo import api, fields, models
7 from odoo.tools import format_amount
8 from odoo.tools.sql import column_exists, create_column
9
10
11 class SaleOrderLine(models.Model):
12 _inherit = "sale.order.line"
13
14 _name_search_services_index = models.Index("(order_id DESC, sequence, id) WHERE is_service IS TRUE")
15
16 # used to know if generate a task and/or a project, depending on the product settings
17 is_service = fields.Boolean("Is a Service", compute='_compute_is_service', store=True, compute_sudo=True, export_string_translation=False)
18
19 def _domain_sale_line_service(self, **kwargs):
20 """
21 Get the default generic services domain for sale.order.line.
22 You can filter out domain leafs by passing kwargs of the form 'check_<leaf_field>=False'.
23 Only 'is_service' cannot be disabled.
24
25 :param kwargs: boolean kwargs of the form 'check_<leaf_field>=False'
26 :return: a valid domain
27 """
28 domain = [('is_service', '=', True)]
29 if kwargs.get("check_is_expense", True):
30 domain.append(('is_expense', '=', False))
31 if kwargs.get("check_state", True):
32 domain.append(('state', '=', 'sale'))
33 return domain
34
35 @api.depends('product_id.type')
36 def _compute_is_service(self):
37 self.fetch(['is_service', 'product_id'])
38 self.product_id.fetch(['type'])
39 for so_line in self:
40 so_line.is_service = so_line.product_id.type == 'service'
41
42 def _auto_init(self):
43 """
44 Create column to stop ORM from computing it himself (too slow)
45 """
46 if not column_exists(self.env.cr, 'sale_order_line', 'is_service'):
47 create_column(self.env.cr, 'sale_order_line', 'is_service', 'bool')
48 self.env.cr.execute("""
49 UPDATE sale_order_line line
50 SET is_service = (pt.type = 'service')
51 FROM product_product pp
52 LEFT JOIN product_template pt ON pt.id = pp.product_tmpl_id
53 WHERE pp.id = line.product_id
54 """)
55 return super()._auto_init()
56
57 def _additional_name_per_id(self):
58 name_per_id = super()._additional_name_per_id() if not self.env.context.get('hide_partner_ref') else {}
59 if not self.env.context.get('with_price_unit'):
60 return name_per_id
61
62 sols_list = [list(sols) for dummy, sols in groupby(self, lambda sol: (sol.order_id, sol.product_id))]
63 for sols in sols_list:
64 if len(sols) <= 1 or not all(sol.is_service for sol in sols):
65 continue
66 for line in sols:
67 additional_name = name_per_id.get(line.id)
68 name = format_amount(self.env, line.price_unit, line.currency_id)
69 if additional_name:
70 name += f' {additional_name}'
71 name_per_id[line.id] = f'- {name}'
72
73 return name_per_id
74
75 @api.model
76 def name_search(self, name='', domain=None, operator='ilike', limit=100):
77 domain = domain or []
78 # optimization for a SOL services name_search, to avoid joining on sale_order with too many lines
79 if domain and ('is_service', '=', True) in domain and operator in ('like', 'ilike') and limit is not None:
80 sols = self.search_fetch(
81 domain, ['display_name'], limit=limit, order='order_id.id DESC, sequence, id',
82 )
83 return [(sol.id, sol.display_name) for sol in sols]
84 return super().name_search(name, domain, operator, limit)