-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
sale_order.py
71 lines (59 loc) · 2.63 KB
/
sale_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# © 2019 Eficent Business and IT Consulting Services S.L.
# - Jordi Ballester Alomar
# © 2019 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class SaleOrder(models.Model):
_inherit = 'sale.order'
@api.model
def _default_operating_unit(self):
team = self.env['crm.team']._get_default_team_id()
if team.operating_unit_id:
return team.operating_unit_id
return self.env.user.default_operating_unit_id
operating_unit_id = fields.Many2one(
comodel_name='operating.unit',
string='Operating Unit',
default=_default_operating_unit,
readonly=True,
states={'draft': [('readonly', False)],
'sent': [('readonly', False)]}
)
@api.onchange('team_id')
def onchange_team_id(self):
if self.team_id:
self.operating_unit_id = self.team_id.operating_unit_id
@api.onchange('operating_unit_id')
def onchange_operating_unit_id(self):
if self.team_id and self.team_id.operating_unit_id != \
self.operating_unit_id:
self.team_id = False
@api.multi
@api.constrains('team_id', 'operating_unit_id')
def _check_team_operating_unit(self):
for rec in self:
if (rec.team_id and
rec.team_id.operating_unit_id != rec.operating_unit_id):
raise ValidationError(_('Configuration error. The Operating '
'Unit of the sales team must match '
'with that of the quote/sales order.'))
@api.multi
@api.constrains('operating_unit_id', 'company_id')
def _check_company_operating_unit(self):
for rec in self:
if (rec.company_id and rec.operating_unit_id and
rec.company_id != rec.operating_unit_id.company_id):
raise ValidationError(_('Configuration error. The Company in '
'the Sales Order and in the Operating '
'Unit must be the same.'))
@api.multi
def _prepare_invoice(self):
self.ensure_one()
invoice_vals = super(SaleOrder, self)._prepare_invoice()
invoice_vals['operating_unit_id'] = self.operating_unit_id.id
return invoice_vals
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
operating_unit_id = fields.Many2one(related='order_id.operating_unit_id',
string='Operating Unit')