~p-chiara/openobject-italia/fix_remove_ddt_btn_if_invoiced

« back to all changes in this revision

Viewing changes to l10n_it_ri_ba/account_move_line.py

  • Committer: eLBati
  • Date: 2012-01-03 09:02:46 UTC
  • mfrom: (108.1.19 riba)
  • Revision ID: lorenzo.battistini@agilebg.com-20120103090246-x5ctx1gre82irdgu
[MERGE] l10n_it_ri_ba

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#    
 
4
#    Copyright (C) 2011 Associazione OpenERP Italia
 
5
#    (<http://www.openerp-italia.org>).
 
6
#    All Rights Reserved 
 
7
#    Thanks to Cecchi s.r.l http://www.cecchi.com/
 
8
#
 
9
#    This program is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU Affero General Public License as published by
 
11
#    the Free Software Foundation, either version 3 of the License, or
 
12
#    (at your option) any later version.
 
13
#
 
14
#    This program is distributed in the hope that it will be useful,
 
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#    GNU General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU Affero General Public License
 
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
#
 
22
##############################################################################
 
23
 
 
24
from operator import itemgetter
 
25
from osv import fields, osv
 
26
from tools.translate import _
 
27
 
 
28
class account_move_line(osv.osv):
 
29
    _inherit = "account.move.line"
 
30
 
 
31
    def riba_amount_to_pay(self, cr, uid, ids, name, arg={}, context=None):
 
32
        """ Return the amount still to pay regarding all the riba orders
 
33
        (excepting cancelled orders)"""
 
34
        if not ids:
 
35
            return {}
 
36
        cr.execute("""SELECT ml.id,
 
37
                    CASE WHEN ml.amount_currency < 0
 
38
                        THEN - ml.amount_currency
 
39
                        ELSE ml.debit
 
40
                    END -
 
41
                    (SELECT coalesce(sum(amount_currency),0)
 
42
                        FROM riba_line rl
 
43
                            INNER JOIN riba_order ro
 
44
                                ON (rl.order_id = ro.id)
 
45
                        WHERE move_line_id = ml.id
 
46
                        AND ro.state != 'cancel') AS amount
 
47
                    FROM account_move_line ml
 
48
                    WHERE id IN %s""", (tuple(ids),))
 
49
        r = dict(cr.fetchall())
 
50
        return r
 
51
 
 
52
    def riba_to_pay_search(self, cr, uid, obj, name, args, context=None):
 
53
        if not args:
 
54
            return []
 
55
        line_obj = self.pool.get('account.move.line')
 
56
        query = line_obj._query_get(cr, uid, context={})
 
57
        where = ' and '.join(map(lambda x: '''(SELECT
 
58
        CASE WHEN l.amount_currency < 0
 
59
            THEN - l.amount_currency
 
60
            ELSE l.debit
 
61
        END - coalesce(sum(rl.amount_currency), 0)
 
62
        FROM riba_line rl
 
63
        INNER JOIN riba_order ro ON (rl.order_id = ro.id)
 
64
        WHERE move_line_id = l.id
 
65
        AND ro.state != 'cancel'
 
66
        ) %(operator)s %%s ''' % {'operator': x[1]}, args))
 
67
        sql_args = tuple(map(itemgetter(2), args))
 
68
 
 
69
        cr.execute(('''SELECT id
 
70
            FROM account_move_line l
 
71
            WHERE account_id IN (select id
 
72
                FROM account_account
 
73
                WHERE type=%s AND active)
 
74
            AND reconcile_id IS NOT NULL
 
75
            AND debit > 0
 
76
            AND ''' + where + ' and ' + query), ('receivable',)+sql_args )
 
77
        res = cr.fetchall()
 
78
        if not res:
 
79
            return [('id', '=', '0')]
 
80
        return [('id', 'in', map(lambda x:x[0], res))]
 
81
 
 
82
    def line_2_bank(self, cr, uid, ids, payment_type=None, context=None):
 
83
        """
 
84
        Try to return for each Ledger Posting line a corresponding bank
 
85
        account according to the payment type.  This work using one of
 
86
        the bank of the partner defined on the invoice eventually
 
87
        associated to the line.
 
88
        Return the last suitable bank for the corresponding partner.
 
89
        """
 
90
        riba_line_obj = self.pool.get('riba.line')
 
91
        line2bank = {}
 
92
        if not ids:
 
93
            return {}
 
94
        bank_type = riba_line_obj.s_bank_types(cr, uid, payment_type,
 
95
                context=context)
 
96
        for line in self.browse(cr, uid, ids, context=context):
 
97
            line2bank[line.id] = False
 
98
            if line.invoice and line.invoice.partner_bank_id:
 
99
                line2bank[line.id] = line.invoice.partner_bank_id.id
 
100
            elif line.partner_id:
 
101
                if not line.partner_id.bank_ids:
 
102
                    line2bank[line.id] = False
 
103
                else:
 
104
                    for bank in line.partner_id.bank_ids:
 
105
                        if bank.state in bank_type:
 
106
                            line2bank[line.id] = bank.id
 
107
                            break
 
108
                if not line2bank[line.id] and line.partner_id.bank_ids:
 
109
                    line2bank[line.id] = line.partner_id.bank_ids[-1].id
 
110
            else:
 
111
                raise osv.except_osv(_('Error !'), _('No partner defined on entry line'))
 
112
        return line2bank
 
113
 
 
114
    def line_2_iban(self, cr, uid, ids, payment_type=None, context=None):
 
115
        """
 
116
        Try to return for each Ledger Posting line a corresponding code
 
117
        iban to the payment type.  This work using one of
 
118
        the bank of the partner defined on the invoice eventually
 
119
        associated to the line.
 
120
        Return the last suitable bank for the corresponding partner.
 
121
        """
 
122
        riba_line_obj = self.pool.get('riba.line')
 
123
        line2iban = {}
 
124
        if not ids:
 
125
            return {}
 
126
        bank_type = riba_line_obj.s_bank_types(cr, uid, payment_type,
 
127
                context=context)
 
128
        for line in self.browse(cr, uid, ids, context=context):
 
129
            line2iban[line.id] = False
 
130
            if line.invoice and line.invoice.partner_bank_id:
 
131
                line2iban[line.id] = line.invoice.partner_bank_id.iban
 
132
            elif line.partner_id:
 
133
                if not line.partner_id.bank_ids:
 
134
                    line2iban[line.id] = False
 
135
                else:
 
136
                    for bank in line.partner_id.bank_ids:
 
137
                        if bank.state in bank_type:
 
138
                            line2iban[line.id] = bank.iban
 
139
                            break
 
140
                if not line2iban[line.id] and line.partner_id.bank_ids:
 
141
                    line2iban[line.id] = line.partner_id.bank_ids[-1].iban
 
142
            else:
 
143
                raise osv.except_osv(_('Error !'), _('No partner defined on entry line'))
 
144
        return line2iban
 
145
 
 
146
    _columns = {
 
147
        'riba_amount_to_pay': fields.function(riba_amount_to_pay, method=True,
 
148
            type='float', string='Amount to pay', fnct_search= riba_to_pay_search),       
 
149
    }
 
150
 
 
151
account_move_line()
 
152
 
 
153
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: