|
| 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 |
0 commit comments