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