~camptocamp/account-invoicing/correct_payment_term_balance_mdh

« back to all changes in this revision

Viewing changes to account_invoice_zero/account_invoice.py

  • Committer: Yannick Vaucher
  • Author(s): guewen.baconnier at camptocamp
  • Date: 2014-03-14 10:00:13 UTC
  • mfrom: (40.1.2 7.0-account_invoice_zero)
  • Revision ID: yannick.vaucher@camptocamp.com-20140314100013-gdib2fjpl1b0o82i
[ADD] account_invoice_zero module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Author: Guewen Baconnier
 
5
#    Copyright 2014 Camptocamp SA
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
from functools import partial
 
23
 
 
24
from openerp.osv import orm
 
25
from openerp.tools.float_utils import float_is_zero
 
26
 
 
27
 
 
28
class account_invoice(orm.Model):
 
29
    _inherit = 'account.invoice'
 
30
 
 
31
    def invoice_validate(self, cr, uid, ids, context=None):
 
32
        result = super(account_invoice, self).invoice_validate(
 
33
            cr, uid, ids, context=context)
 
34
        dp_obj = self.pool['decimal.precision']
 
35
        precision = dp_obj.precision_get(cr, uid, 'Account')
 
36
        is_zero = partial(float_is_zero, precision_digits=precision)
 
37
        for invoice in self.browse(cr, uid, ids, context=context):
 
38
            if is_zero(invoice.amount_total):
 
39
                account = invoice.account_id.id
 
40
                # search the payable / receivable lines
 
41
                lines = [line for line in invoice.move_id.line_id
 
42
                         if line.account_id.id == account]
 
43
                # reconcile the lines with a zero balance
 
44
                if is_zero(sum(line.debit - line.credit for line in lines)):
 
45
                    move_line_obj = self.pool['account.move.line']
 
46
                    move_line_obj.reconcile(cr, uid,
 
47
                                            [line.id for line in lines],
 
48
                                            context=context)
 
49
        return result
 
50