replace tabs with 4 spaces
[odoo/odoo.git] / addons / account_payment / account_move_line.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
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
12 # Service Company
13 #
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.
18 #
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.
23 #
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.
27 #
28 ##############################################################################
29
30 from osv import fields, osv
31
32
33 class account_move_line(osv.osv):
34     _inherit = "account.move.line"
35
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)"""
39         if not ids:
40             return {}
41         cr.execute("""SELECT ml.id,
42                     CASE WHEN ml.amount_currency < 0
43                         THEN - ml.amount_currency
44                         ELSE ml.credit
45                     END -
46                     (SELECT coalesce(sum(amount_currency),0)
47                         FROM payment_line pl
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))))
54         r=dict(cr.fetchall())
55         return r
56
57     def _to_pay_search(self, cr, uid, obj, name, args):
58         if not len(args):
59             return []
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
65             ELSE l.credit
66         END - coalesce(sum(pl.amount_currency), 0)
67         FROM payment_line pl
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))
71
72         cr.execute(('''select id
73             from account_move_line l
74             where account_id in (select id
75                 from account_account
76                 where type=%s and active)
77             and reconcile_id is null
78             and credit > 0
79             and ''' + where + ' and ' + query), ('payable',) )
80
81         res = cr.fetchall()
82         if not len(res):
83             return [('id','=','0')]
84         return [('id','in',map(lambda x:x[0], res))]
85
86     def line2bank(self, cr, uid, ids, payment_type='manual', context=None):
87         """
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.
93         """
94         payment_mode_obj = self.pool.get('payment.mode')
95         line2bank = {}
96         if not ids:
97             return {}
98         bank_type = payment_mode_obj.suitable_bank_types(cr, uid, payment_type,
99                 context=context)
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
108                         break
109                 if line.id not in line2bank and line.partner_id.bank_ids:
110                     line2bank[line.id] = line.partner_id.bank_ids[0].id
111         return line2bank
112
113     _columns = {
114         'amount_to_pay' : fields.function(amount_to_pay, method=True,
115             type='float', string='Amount to pay', fnct_search=_to_pay_search),
116     }
117
118 account_move_line()