forked from OCA/account-invoicing
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccount_invoice.py
77 lines (71 loc) · 3.35 KB
/
account_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
#############################################################################
#
# Invoice Fiscal Position Update module for OpenERP
# Copyright (C) 2011-2014 Julius Network Solutions SARL <[email protected]>
# Copyright (C) 2014 Akretion (http://www.akretion.com)
# @author Mathieu Vatel <mathieu _at_ julius.fr>
# @author Alexis de Lattre <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models, api, _
class account_invoice(models.Model):
_inherit = "account.invoice"
@api.onchange('fiscal_position')
def fiscal_position_change(self):
"""Updates taxes and accounts on all invoice lines"""
self.ensure_one()
res = {}
lines_without_product = []
fp = self.fiscal_position
inv_type = self.type
for line in self.invoice_line:
if line.product_id:
product = line.product_id
if inv_type in ('out_invoice', 'out_refund'):
account = (
product.property_account_income or
product.categ_id.property_account_income_categ)
taxes = product.taxes_id
else:
account = (
product.property_account_expense or
product.categ_id.property_account_expense_categ)
taxes = product.supplier_taxes_id
taxes = taxes or account.tax_ids
if fp:
account = fp.map_account(account)
taxes = fp.map_tax(taxes)
line.invoice_line_tax_id = [(6, 0, taxes.ids)]
line.account_id = account.id
else:
lines_without_product.append(line.name)
if lines_without_product:
res['warning'] = {'title': _('Warning')}
if len(lines_without_product) == len(self.invoice_line):
res['warning']['message'] = _(
"The invoice lines were not updated to the new "
"Fiscal Position because they don't have products.\n"
"You should update the Account and the Taxes of each "
"invoice line manually.")
else:
res['warning']['message'] = _(
"The following invoice lines were not updated "
"to the new Fiscal Position because they don't have a "
"Product:\n- %s\nYou should update the Account and the "
"Taxes of these invoice lines manually."
) % ('\n- '.join(lines_without_product))
return res