forked from OCA/operating-unit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
invoice.py
51 lines (42 loc) · 1.99 KB
/
invoice.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
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.translate import _
class AccountInvoice(models.Model):
_inherit = "account.invoice"
operating_unit_id = fields.Many2one('operating.unit', 'Operating Unit',
default=lambda self:
self.env['res.users'].
operating_unit_default_get(self._uid),
readonly=True,
states={'draft': [('readonly',
False)]})
@api.multi
def finalize_invoice_move_lines(self, move_lines):
move_lines = super(AccountInvoice,
self).finalize_invoice_move_lines(move_lines)
new_move_lines = []
for line_tuple in move_lines:
if self.operating_unit_id:
line_tuple[2]['operating_unit_id'] = \
self.operating_unit_id.id
new_move_lines.append(line_tuple)
return new_move_lines
@api.multi
@api.constrains('operating_unit_id', 'company_id')
def _check_company_operating_unit(self):
for pr in self:
if (
pr.company_id and
pr.operating_unit_id and
pr.company_id != pr.operating_unit_id.company_id
):
raise ValidationError(_('The Company in the Invoice and in '
'Operating Unit must be the same.'))
return True
class AccountInvoiceLine(models.Model):
_inherit = 'account.invoice.line'
operating_unit_id = fields.Many2one('operating.unit',
related='invoice_id.operating_unit_id',
string='Operating Unit', store=True,
readonly=True)