~sebastien.beau/openobject-addons/on-change-support-extra-addons

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2004-2011
#        Pexego Sistemas Informáticos. (http://pexego.es) All Rights Reserved
#        Zikzakmedia S.L. (http://zikzakmedia.com) All Rights Reserved.
#
#    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/>.
#
##############################################################################

__author__ =  "Jordi Esteve (Zikzakmedia)"


from osv import fields, osv
import decimal_precision as dp 


class account_invoice(osv.osv):
    """
    Inheritance of account invoice to add company currency amounts
    """
    _inherit = "account.invoice"
    

    def _get_invoice_line2(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('account.invoice.line').browse(cr, uid, ids, context=context):
            result[line.invoice_id.id] = True
        return result.keys()


    def _get_invoice_tax2(self, cr, uid, ids, context=None):
        result = {}
        for tax in self.pool.get('account.invoice.tax').browse(cr, uid, ids, context=context):
            result[tax.invoice_id.id] = True
        return result.keys()
    

    def _cc_amount_all(self, cr, uid, ids, name, args, context=None):
        res = {}
        for invoice in self.browse(cr,uid,ids, context=context):
            if invoice.company_id.currency_id == invoice.currency_id:
                res[invoice.id] = {
                    'cc_amount_untaxed': invoice.amount_untaxed,
                    'cc_amount_tax': invoice.amount_tax,
                    'cc_amount_total': invoice.amount_total,
                }
            else:
                res[invoice.id] = {
                    'cc_amount_untaxed': 0.0,
                    'cc_amount_tax': 0.0,
                    'cc_amount_total': 0.0,
                }

                ##
                ## It could be computed only in open or paid invoices with a generated account move
                if invoice.move_id:
                    ##
                    ## Accounts to compute amount_untaxed
                    line_account = []
                    for line in invoice.invoice_line:
                        if line.account_id.id not in line_account:
                            line_account.append(line.account_id.id)

                    ##
                    ## Accounts to compute amount_tax
                    tax_account = []
                    for line in invoice.tax_line:
                        if line.account_id.id not in tax_account and line.amount != 0:
                            tax_account.append(line.account_id.id)

                    ##
                    ## The company currency amounts are the debit-credit amounts in the account moves
                    for line in invoice.move_id.line_id:
                        if line.account_id.id in line_account:
                            res[invoice.id]['cc_amount_untaxed'] += line.debit - line.credit
                        if line.account_id.id in tax_account:
                            res[invoice.id]['cc_amount_tax'] += line.debit - line.credit
                    if invoice.type in ('out_invoice','in_refund'):
                        res[invoice.id]['cc_amount_untaxed'] = -res[invoice.id]['cc_amount_untaxed']
                        res[invoice.id]['cc_amount_tax'] = -res[invoice.id]['cc_amount_tax']
                    res[invoice.id]['cc_amount_total'] = res[invoice.id]['cc_amount_tax'] + res[invoice.id]['cc_amount_untaxed']
        return res

    
    _columns = {
        'cc_amount_untaxed': fields.function(_cc_amount_all, method=True, digits_compute=dp.get_precision('Account'),
            string='Company Cur. Untaxed',
            help='Invoice untaxed amount in the company currency (useful when invoice currency is different from company currency).',
            store={
                'account.invoice': (lambda self, cr, uid, ids, c={}: ids, ['invoice_line','currency_id','move_id'], 20),
                'account.invoice.tax': (_get_invoice_tax2, None, 20),
                'account.invoice.line': (_get_invoice_line2, ['price_unit','invoice_line_tax_id','quantity','discount'], 20),
            },
            multi='cc_all'),
        'cc_amount_tax': fields.function(_cc_amount_all, method=True, digits_compute=dp.get_precision('Account'),
            string='Company Cur. Tax',
            help='Invoice tax amount in the company currency (useful when invoice currency is different from company currency).',
            store={
                'account.invoice': (lambda self, cr, uid, ids, c={}: ids, ['invoice_line','currency_id','move_id'], 20),
                'account.invoice.tax': (_get_invoice_tax2, None, 20),
                'account.invoice.line': (_get_invoice_line2, ['price_unit','invoice_line_tax_id','quantity','discount'], 20),
            },
            multi='cc_all'),
        'cc_amount_total': fields.function(_cc_amount_all, method=True, digits_compute=dp.get_precision('Account'),
            string='Company Cur. Total',
            help='Invoice total amount in the company currency (useful when invoice currency is different from company currency).',
            store={
                'account.invoice': (lambda self, cr, uid, ids, c={}: ids, ['invoice_line','currency_id','move_id'], 20),
                'account.invoice.tax': (_get_invoice_tax2, None, 20),
                'account.invoice.line': (_get_invoice_line2, ['price_unit','invoice_line_tax_id','quantity','discount'], 20),
            },
            multi='cc_all'),
    }
account_invoice()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: