*modified ml_inv_ref field from char to many2one(account.invoice)
[odoo/odoo.git] / addons / account_payment / account_move_line.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
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
13 # Service Company
14 #
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.
19 #
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.
24 #
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.
28 #
29 ##############################################################################
30
31 from osv import fields, osv
32
33
34 class account_move_line(osv.osv):
35     _inherit = "account.move.line"
36
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)"""
40         if not ids:
41             return {}
42         cr.execute("""SELECT ml.id,
43                     CASE WHEN ml.amount_currency < 0
44                         THEN - ml.amount_currency
45                         ELSE ml.credit
46                     END -
47                     (SELECT coalesce(sum(amount_currency),0)
48                         FROM payment_line pl
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))))
55         r=dict(cr.fetchall())
56         return r
57
58     def _to_pay_search(self, cr, uid, obj, name, args):
59         if not len(args):
60             return []
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
66             ELSE l.credit
67         END - coalesce(sum(pl.amount_currency), 0)
68         FROM payment_line pl
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))
72
73         cr.execute(('''select id
74             from account_move_line l
75             where account_id in (select id
76                 from account_account
77                 where type=%s and active)
78             and reconcile_id is null
79             and credit > 0
80             and ''' + where + ' and ' + query), ('payable',) )
81
82         res = cr.fetchall()
83         if not len(res):
84             return [('id','=','0')]
85         return [('id','in',map(lambda x:x[0], res))]
86
87     def line2bank(self, cr, uid, ids, payment_type=None, context=None):
88         """
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.
94         """
95         payment_mode_obj = self.pool.get('payment.mode')
96         line2bank = {}
97         if not ids:
98             return {}
99         bank_type = payment_mode_obj.suitable_bank_types(cr, uid, payment_type,
100                 context=context)
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
111                         break
112                 if line.id not in line2bank and line.partner_id.bank_ids:
113                     line2bank[line.id] = line.partner_id.bank_ids[0].id
114             else:
115                 raise osv.except_osv(_('Error !'), _('No partner defined on entry line'))
116         return line2bank
117
118     _columns = {
119         'amount_to_pay' : fields.function(amount_to_pay, method=True,
120             type='float', string='Amount to pay', fnct_search=_to_pay_search),
121     }
122
123 account_move_line()
124
125 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
126