1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Copyright (C) 2011 Associazione OpenERP Italia
5
# (<http://www.openerp-italia.org>).
7
# Thanks to Cecchi s.r.l http://www.cecchi.com/
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.
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.
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/>.
22
##############################################################################
24
from operator import itemgetter
25
from osv import fields, osv
26
from tools.translate import _
28
class account_move_line(osv.osv):
29
_inherit = "account.move.line"
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)"""
36
cr.execute("""SELECT ml.id,
37
CASE WHEN ml.amount_currency < 0
38
THEN - ml.amount_currency
41
(SELECT coalesce(sum(amount_currency),0)
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())
52
def riba_to_pay_search(self, cr, uid, obj, name, args, context=None):
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
61
END - coalesce(sum(rl.amount_currency), 0)
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))
69
cr.execute(('''SELECT id
70
FROM account_move_line l
71
WHERE account_id IN (select id
73
WHERE type=%s AND active)
74
AND reconcile_id IS NOT NULL
76
AND ''' + where + ' and ' + query), ('receivable',)+sql_args )
79
return [('id', '=', '0')]
80
return [('id', 'in', map(lambda x:x[0], res))]
82
def line_2_bank(self, cr, uid, ids, payment_type=None, context=None):
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.
90
riba_line_obj = self.pool.get('riba.line')
94
bank_type = riba_line_obj.s_bank_types(cr, uid, payment_type,
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
104
for bank in line.partner_id.bank_ids:
105
if bank.state in bank_type:
106
line2bank[line.id] = bank.id
108
if not line2bank[line.id] and line.partner_id.bank_ids:
109
line2bank[line.id] = line.partner_id.bank_ids[-1].id
111
raise osv.except_osv(_('Error !'), _('No partner defined on entry line'))
114
def line_2_iban(self, cr, uid, ids, payment_type=None, context=None):
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.
122
riba_line_obj = self.pool.get('riba.line')
126
bank_type = riba_line_obj.s_bank_types(cr, uid, payment_type,
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
136
for bank in line.partner_id.bank_ids:
137
if bank.state in bank_type:
138
line2iban[line.id] = bank.iban
140
if not line2iban[line.id] and line.partner_id.bank_ids:
141
line2iban[line.id] = line.partner_id.bank_ids[-1].iban
143
raise osv.except_osv(_('Error !'), _('No partner defined on entry line'))
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),
153
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: