Moved base report designer in extra_addons
[odoo/odoo.git] / addons / account_payment / account_move_line.py
1 ##############################################################################
2 #
3 # Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
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
10 # Service Company
11 #
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.
16 #
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.
21 #
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.
25 #
26 ##############################################################################
27
28 from osv import fields, osv
29
30
31 class account_move_line(osv.osv):
32         _inherit = "account.move.line"
33
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)"""
37                 if not ids:
38                         return {}
39                 cr.execute("""SELECT ml.id,
40                                         CASE WHEN ml.amount_currency < 0
41                                                 THEN - ml.amount_currency
42                                                 ELSE ml.credit
43                                         END -
44                                         (SELECT coalesce(sum(amount_currency),0)
45                                                 FROM payment_line pl
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))))
52                 r=dict(cr.fetchall())
53                 return r
54
55         def _to_pay_search(self, cr, uid, obj, name, args):
56                 if not len(args):
57                         return []
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
63                         ELSE l.credit
64                 END - coalesce(sum(pl.amount_currency), 0)
65                 FROM payment_line pl
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))
69
70                 cr.execute(('''select id
71                         from account_move_line l
72                         where account_id in (select id
73                                 from account_account
74                                 where type=%s and active)
75                         and reconcile_id is null
76                         and credit > 0
77                         and ''' + where + ' and ' + query), ('payable',) )
78
79                 res = cr.fetchall()
80                 if not len(res):
81                         return [('id','=','0')]
82                 return [('id','in',map(lambda x:x[0], res))]
83
84         def line2bank(self, cr, uid, ids, payment_type='manual', context=None):
85                 """
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.
91                 """
92                 payment_mode_obj = self.pool.get('payment.mode')
93                 line2bank = {}
94                 if not ids:
95                         return {}
96                 bank_type = payment_mode_obj.suitable_bank_types(cr, uid, payment_type,
97                                 context=context)
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
106                                                 break
107                                 if line.id not in line2bank and line.partner_id.bank_ids:
108                                         line2bank[line.id] = line.partner_id.bank_ids[0].id
109                 return line2bank
110
111         _columns = {
112                 'amount_to_pay' : fields.function(amount_to_pay, method=True,
113                         type='float', string='Amount to pay', fnct_search=_to_pay_search),
114         }
115
116 account_move_line()