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