Debian Codegraph @

addons/sale_management/models/sale_order_template.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import _, api, fields, models
4 from odoo.exceptions import ValidationError
5 from odoo.fields import Command
6
7
8 class SaleOrderTemplate(models.Model):
9 _name = 'sale.order.template'
10 _description = "Quotation Template"
11 _order = 'sequence, id'
12
13 active = fields.Boolean(
14 default=True,
15 help="If unchecked, it will allow you to hide the quotation template without removing it.")
16 company_id = fields.Many2one(comodel_name='res.company', default=lambda self: self.env.company)
17
18 name = fields.Char(string="Quotation Template", required=True)
19 note = fields.Html(string="Terms and conditions", translate=True)
20 sequence = fields.Integer(default=10)
21
22 mail_template_id = fields.Many2one(
23 comodel_name='mail.template',
24 string="Confirmation Mail",
25 domain=[('model', '=', 'sale.order')],
26 help="This e-mail template will be sent on confirmation. Leave empty to send nothing.")
27 number_of_days = fields.Integer(
28 string="Quotation Duration",
29 help="Number of days for the validity date computation of the quotation")
30
31 require_signature = fields.Boolean(
32 string="Online Signature",
33 compute='_compute_require_signature',
34 store=True, readonly=False,
35 help="Request a online signature to the customer in order to confirm orders automatically.")
36 require_payment = fields.Boolean(
37 string="Online Payment",
38 compute='_compute_require_payment',
39 store=True, readonly=False,
40 help="Request an online payment to the customer in order to confirm orders automatically.")
41 prepayment_percent = fields.Float(
42 string="Prepayment percentage",
43 compute="_compute_prepayment_percent",
44 store=True, readonly=False,
45 help="The percentage of the amount needed to be paid to confirm quotations.")
46
47 sale_order_template_line_ids = fields.One2many(
48 comodel_name='sale.order.template.line', inverse_name='sale_order_template_id',
49 string="Lines",
50 copy=True)
51 journal_id = fields.Many2one(
52 'account.journal', string="Invoicing Journal",
53 domain=[('type', '=', 'sale')], company_dependent=True, check_company=True,
54 help="If set, SO with this template will invoice in this journal; "
55 "otherwise the sales journal with the lowest sequence is used.")
56
57 #=== COMPUTE METHODS ===#
58
59 @api.depends('company_id')
60 def _compute_require_signature(self):
61 for order in self:
62 order.require_signature = (order.company_id or order.env.company).portal_confirmation_sign
63
64 @api.depends('company_id')
65 def _compute_require_payment(self):
66 for order in self:
67 order.require_payment = (order.company_id or order.env.company).portal_confirmation_pay
68
69 @api.depends('company_id', 'require_payment')
70 def _compute_prepayment_percent(self):
71 for template in self:
72 template.prepayment_percent = (
73 template.company_id or template.env.company
74 ).prepayment_percent
75
76 #=== ONCHANGE METHODS ===#
77
78 @api.onchange('prepayment_percent')
79 def _onchange_prepayment_percent(self):
80 for template in self:
81 if not template.prepayment_percent:
82 template.require_payment = False
83
84 #=== CONSTRAINT METHODS ===#
85
86 @api.constrains('company_id', 'sale_order_template_line_ids')
87 def _check_company_id(self):
88 for template in self:
89 restricted_products = template.sale_order_template_line_ids.product_id.filtered(
90 'company_id'
91 )
92 if not restricted_products:
93 continue
94
95 if not template.company_id:
96 raise ValidationError(_(
97 "Your template cannot contain products from specific companies if it's shared"
98 " between companies. Please restrict the template access, or remove those"
99 " products."
100 ))
101
102 authorized_products = restricted_products.filtered_domain(
103 self.env['product.product']._check_company_domain(template.company_id)
104 )
105 if unauthorized_products := restricted_products - authorized_products:
106 unaccessible_companies = unauthorized_products.company_id
107 if len(unaccessible_companies) > 1:
108 raise ValidationError(_(
109 "Your template belongs to company %(template_company)s but contains"
110 " products from other companies (%(product_company)s) that are not"
111 " accessible to %(template_company)s.\nPlease change the company of your"
112 " template or remove the products from other companies.",
113 product_company=', '.join(unaccessible_companies.mapped('display_name')),
114 template_company=template.company_id.display_name,
115 ))
116
117 raise ValidationError(_(
118 "Your template belongs to company %(template_company)s but contains"
119 " products from company (%(product_company)s) that are not"
120 " accessible to %(template_company)s.\nPlease change the company of your"
121 " template or remove the products from other companies.",
122 product_company=unaccessible_companies.display_name,
123 template_company=template.company_id.display_name,
124 ))
125
126 @api.constrains('prepayment_percent')
127 def _check_prepayment_percent(self):
128 for template in self:
129 if template.require_payment and not (0 < template.prepayment_percent <= 1.0):
130 raise ValidationError(_("Prepayment percentage must be a valid percentage."))
131
132 #=== CRUD METHODS ===#
133
134 @api.model_create_multi
135 def create(self, vals_list):
136 records = super().create(vals_list)
137 records._update_product_translations()
138 return records
139
140 def write(self, vals):
141 if 'active' in vals and not vals.get('active'):
142 companies = self.env['res.company'].sudo().search([('sale_order_template_id', 'in', self.ids)])
143 companies.sale_order_template_id = None
144 result = super().write(vals)
145 self._update_product_translations()
146 return result
147
148 def _update_product_translations(self):
149 languages = self.env['res.lang'].search([('active', '=', True)])
150 for lang in languages:
151 for line in self.sale_order_template_line_ids:
152 if line.name == line.product_id.get_product_multiline_description_sale():
153 line.with_context(lang=lang.code).name = line.product_id.with_context(lang=lang.code).get_product_multiline_description_sale()
154
155 @api.model
156 def _demo_configure_template(self):
157 demo_template = self.env.ref(
158 'sale_management.sale_order_template_1', raise_if_not_found=False
159 )
160 if not demo_template or demo_template.sale_order_template_line_ids:
161 # Skip if template not found, or already configured
162 return
163
164 acoustic_bloc_screen_product = self.env.ref(
165 'product.product_template_acoustic_bloc_screens'
166 ).product_variant_id
167 chair_protection_product = self.env.ref(
168 'sale.product_product_1_product_template'
169 ).product_variant_id
170 demo_template.sale_order_template_line_ids = [
171 Command.create({
172 'product_id': self.env.ref('product.consu_delivery_02').id,
173 }),
174 Command.create({
175 'product_id': self.env.ref('product.product_delivery_01').id,
176 'product_uom_qty': 8,
177 }),
178 Command.create({
179 'product_id': acoustic_bloc_screen_product.id,
180 }),
181 Command.create({
182 'product_id': chair_protection_product.id,
183 'product_uom_qty': 8,
184 }),
185 Command.create({
186 'name': self.env._("Optional Products Section"),
187 'display_type': 'line_section',
188 'is_optional': True,
189 'product_uom_qty': 0,
190 }),
191 Command.create({
192 'product_id': self.env.ref('product.product_product_16').id,
193 'product_uom_qty': 0,
194 }),
195 Command.create({
196 'product_id': self.env.ref('product.product_product_7').id,
197 'product_uom_qty': 0,
198 }),
199 Command.create({
200 'product_id': self.env.ref('product.product_product_12').id,
201 'product_uom_qty': 0,
202 }),
203 ]