1 ##############################################################################
3 # Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsability of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # garantees and support are strongly adviced to contract a Free Software
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ##############################################################################
28 from osv import fields, osv
31 class account_move_line(osv.osv):
32 _inherit = "account.move.line"
34 def amount_to_pay(self, cr, uid, ids, name, arg={}, context={}):
35 """ Return the amount still to pay regarding all the payemnt orders
36 (excepting cancelled orders)"""
39 cr.execute("""SELECT ml.id,
40 CASE WHEN ml.amount_currency < 0
41 THEN - ml.amount_currency
44 (SELECT coalesce(sum(amount_currency),0)
46 INNER JOIN payment_order po
47 ON (pl.order_id = po.id)
48 WHERE move_line_id = ml.id
49 AND po.state != 'cancel') as amount
50 FROM account_move_line ml
51 WHERE id in (%s)""" % (",".join(map(str, ids))))
55 def _to_pay_search(self, cr, uid, obj, name, args):
58 line_obj = self.pool.get('account.move.line')
59 query = line_obj._query_get(cr, uid, context={})
60 where = ' and '.join(map(lambda x: '''(SELECT
61 CASE WHEN l.amount_currency < 0
62 THEN - l.amount_currency
64 END - coalesce(sum(pl.amount_currency), 0)
66 INNER JOIN payment_order po ON (pl.order_id = po.id)
67 WHERE move_line_id = l.id AND po.state != 'cancel')''' \
68 + x[1] + str(x[2])+' ',args))
70 cr.execute(('''select id
71 from account_move_line l
72 where account_id in (select id
74 where type=%s and active)
75 and reconcile_id is null
77 and ''' + where + ' and ' + query), ('payable',) )
81 return [('id','=','0')]
82 return [('id','in',map(lambda x:x[0], res))]
84 def line2bank(self, cr, uid, ids, payment_type='manual', context=None):
86 Try to return for each account move line a corresponding bank
87 account according to the payment type. This work using one of
88 the bank of the partner defined on the invoice eventually
89 associated to the line.
90 Return the first suitable bank for the corresponding partner.
92 payment_mode_obj = self.pool.get('payment.mode')
96 bank_type = payment_mode_obj.suitable_bank_types(cr, uid, payment_type,
98 for line in self.browse(cr, uid, ids, context=context):
99 line2bank[line.id] = False
100 if line.invoice and line.invoice.partner_bank:
101 line2bank[line.id] = line.invoice.partner_bank.id
102 elif line.partner_id:
103 for bank in line.partner_id.bank_ids:
104 if bank.state in bank_type:
105 line2bank[line.id] = bank.id
107 if line.id not in line2bank and line.partner_id.bank_ids:
108 line2bank[line.id] = line.partner_id.bank_ids[0].id
112 'amount_to_pay' : fields.function(amount_to_pay, method=True,
113 type='float', string='Amount to pay', fnct_search=_to_pay_search),