Skip to content

Commit 552e487

Browse files
author
Cédric Pigeon
committed
[8.0][ADD] add new module to manage order of invoice lines
1 parent 60d3e51 commit 552e487

10 files changed

+455
-0
lines changed

account_invoice_line_sort/README.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Sort Customer Invoice Lines
2+
============================
3+
4+
This module was written to extend the functionality of customer invoice and allow you to manage sort of lines.
5+
6+
An attribute is added on the customer form to select the sort of invoice lines.
7+
When selecting the customer on an invoice, the sort options are automatically inherited.
8+
If needed, the user can always change it on the invoice without impacting the customer data.
9+
When saving the invoice, all lines are sorted given the sort options selected.
10+
11+
In any cases, the default sort is by sequence ascending.
12+
13+
Credits
14+
=======
15+
16+
Contributors
17+
------------
18+
19+
* Cédric Pigeon <[email protected]>
20+
21+
Maintainer
22+
----------
23+
24+
.. image:: http://odoo-community.org/logo.png
25+
:alt: Odoo Community Association
26+
:target: http://odoo-community.org
27+
28+
This module is maintained by the OCA.
29+
30+
OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.
31+
32+
To contribute to this module, please visit http://odoo-community.org.

account_invoice_line_sort/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# -*- coding: utf-8 -*-
2+
from . import models
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
4+
# This file is part of account_invoice_line_sort, an Odoo module.
5+
#
6+
# Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>)
7+
#
8+
# account_invoice_line_sort is free software: you can redistribute it
9+
# and/or modify it under the terms of the GNU Affero General Public License
10+
# as published by the Free Software Foundation, either version 3 of
11+
# the License, or (at your option) any later version.
12+
#
13+
# account_invoice_line_sort is distributed in the hope that it will
14+
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU Affero General Public License for more details.
17+
#
18+
# You should have received a copy of the
19+
# GNU Affero General Public License
20+
# along with account_invoice_line_sort.
21+
# If not, see <http://www.gnu.org/licenses/>.
22+
#
23+
##############################################################################
24+
{
25+
'name': "Sort Customer Invoice Lines",
26+
27+
'summary': """
28+
Manage sort of customer invoice lines by customers""",
29+
30+
'author': "ACSONE SA/NV",
31+
'website': "http://acsone.eu",
32+
33+
'category': 'Finance',
34+
'version': '0.1',
35+
'license': 'AGPL-3',
36+
37+
'depends': [
38+
'account',
39+
],
40+
41+
'data': [
42+
'views/account_invoice_view.xml',
43+
'views/res_partner_view.xml'
44+
],
45+
46+
'demo': [
47+
],
48+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- coding: utf-8 -*-
2+
from . import account_invoice
3+
from . import res_partner
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
4+
# This file is part of account_invoice_line_sort, an Odoo module.
5+
#
6+
# Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>)
7+
#
8+
# account_invoice_line_sort is free software: you can redistribute it
9+
# and/or modify it under the terms of the GNU Affero General Public License
10+
# as published by the Free Software Foundation, either version 3 of
11+
# the License, or (at your option) any later version.
12+
#
13+
# account_invoice_line_sort is distributed in the hope that it will
14+
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU Affero General Public License for more details.
17+
#
18+
# You should have received a copy of the
19+
# GNU Affero General Public License
20+
# along with account_invoice_line_sort.
21+
# If not, see <http://www.gnu.org/licenses/>.
22+
#
23+
##############################################################################
24+
25+
from openerp import models, fields, api
26+
27+
AVAILABLE_SORT_OPTIONS = [
28+
('sequence', 'Sequence'),
29+
('name', 'Description'),
30+
('price_unit', 'Unit Price'),
31+
('price_subtotal', 'Amount'),
32+
]
33+
AVAILABLE_ORDER_OPTIONS = [
34+
('asc', 'Ascending'),
35+
('desc', 'Descending')
36+
]
37+
38+
39+
class account_invoice(models.Model):
40+
_inherit = "account.invoice"
41+
_sort_trigger_fields = ('line_order',
42+
'line_order_direction')
43+
44+
line_order = fields.Selection(AVAILABLE_SORT_OPTIONS,
45+
"Sort Lines By",
46+
default='sequence')
47+
line_order_direction = fields.Selection(AVAILABLE_ORDER_OPTIONS,
48+
"Sort Direction",
49+
default='asc')
50+
51+
@api.model
52+
def get_partner_sort_options(self, partner_id):
53+
res = {}
54+
if partner_id:
55+
p = self.env['res.partner'].browse(partner_id)
56+
res['line_order'] = p.line_order
57+
res['line_order_direction'] = p.line_order_direction
58+
return res
59+
60+
@api.multi
61+
def onchange_partner_id(self, type, partner_id, date_invoice=False,
62+
payment_term=False, partner_bank_id=False,
63+
company_id=False):
64+
res = super(account_invoice,
65+
self).onchange_partner_id(type,
66+
partner_id,
67+
date_invoice=date_invoice,
68+
payment_term=payment_term,
69+
partner_bank_id=partner_bank_id,
70+
company_id=company_id)
71+
if partner_id:
72+
res['value'].update(self.get_partner_sort_options(partner_id))
73+
return res
74+
75+
@api.one
76+
def _sort_account_invoice_line(self):
77+
line_model = self.env['account.invoice.line']
78+
if self.invoice_line:
79+
order = "%s %s" % (self.line_order, self.line_order_direction)
80+
domain = [('id', 'in', self.invoice_line.ids)]
81+
sequence = 0
82+
for line in line_model.search(domain, order=order):
83+
sequence += 10
84+
line.sequence = sequence
85+
86+
@api.multi
87+
def write(self, vals):
88+
sort = False
89+
fields = [key for key in vals if key in self._sort_trigger_fields]
90+
if fields:
91+
if [key for key in fields if vals[key] != self[key]]:
92+
sort = True
93+
res = super(account_invoice, self).write(vals)
94+
if sort or 'invoice_line' in vals:
95+
self._sort_account_invoice_line()
96+
return res
97+
98+
@api.model
99+
@api.returns('self', lambda value: value.id)
100+
def create(self, vals):
101+
if not [key for key in vals if key in self._sort_trigger_fields]:
102+
partner_id = vals.get('partner_id', False)
103+
vals.update(self.get_partner_sort_options(partner_id))
104+
invoice = super(account_invoice, self).create(vals)
105+
invoice._sort_account_invoice_line()
106+
return invoice
107+
108+
109+
class account_invoice_line(models.Model):
110+
_inherit = "account.invoice.line"
111+
_sort_trigger_fields = ('name', 'quantity', 'price_unit', 'discount')
112+
113+
@api.multi
114+
def write(self, vals):
115+
sort = False
116+
fields = [key for key in vals if key in self._sort_trigger_fields]
117+
if fields:
118+
if [key for key in fields if vals[key] != self[key]]:
119+
sort = True
120+
res = super(account_invoice_line, self).write(vals)
121+
if sort:
122+
self.invoice_id._sort_account_invoice_line()
123+
return res
124+
125+
@api.model
126+
@api.returns('self', lambda value: value.id)
127+
def create(self, vals):
128+
line = super(account_invoice_line, self).create(vals)
129+
self.invoice_id._sort_account_invoice_line()
130+
return line
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
4+
# This file is part of account_invoice_line_sort, an Odoo module.
5+
#
6+
# Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>)
7+
#
8+
# account_invoice_line_sort is free software: you can redistribute it
9+
# and/or modify it under the terms of the GNU Affero General Public License
10+
# as published by the Free Software Foundation, either version 3 of
11+
# the License, or (at your option) any later version.
12+
#
13+
# account_invoice_line_sort is distributed in the hope that it will
14+
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU Affero General Public License for more details.
17+
#
18+
# You should have received a copy of the
19+
# GNU Affero General Public License
20+
# along with account_invoice_line_sort.
21+
# If not, see <http://www.gnu.org/licenses/>.
22+
#
23+
##############################################################################
24+
25+
from openerp import models, fields
26+
from . import account_invoice
27+
28+
29+
class res_partner(models.Model):
30+
_inherit = "res.partner"
31+
32+
line_order = fields.Selection(account_invoice.AVAILABLE_SORT_OPTIONS,
33+
"Sort Invoice Lines By",
34+
default='sequence')
35+
line_order_direction = fields.Selection(
36+
account_invoice.AVAILABLE_ORDER_OPTIONS,
37+
"Sort Direction",
38+
default='asc')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# -*- coding: utf-8 -*-
2+
from . import test_account_invoice_line_sort

0 commit comments

Comments
 (0)