[IMP]
[odoo/odoo.git] / addons / account_payment / account_move_line.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields, osv
24 from tools.translate import _
25
26 class account_move_line(osv.osv):
27     _inherit = "account.move.line"
28
29     def amount_to_pay(self, cr, uid, ids, name, arg={}, context={}):
30         """ Return the amount still to pay regarding all the payemnt orders
31         (excepting cancelled orders)"""
32         if not ids:
33             return {}
34         cr.execute("""SELECT ml.id,
35                     CASE WHEN ml.amount_currency < 0
36                         THEN - ml.amount_currency
37                         ELSE ml.credit
38                     END -
39                     (SELECT coalesce(sum(amount_currency),0)
40                         FROM payment_line pl
41                             INNER JOIN payment_order po
42                                 ON (pl.order_id = po.id)
43                         WHERE move_line_id = ml.id
44                         AND po.state != 'cancel') as amount
45                     FROM account_move_line ml
46                     WHERE id in (%s)""" % (",".join(map(str, ids))))
47         r=dict(cr.fetchall())
48         return r
49
50     def _to_pay_search(self, cr, uid, obj, name, args):
51         if not len(args):
52             return []
53         line_obj = self.pool.get('account.move.line')
54         query = line_obj._query_get(cr, uid, context={})
55         where = ' and '.join(map(lambda x: '''(SELECT
56         CASE WHEN l.amount_currency < 0
57             THEN - l.amount_currency
58             ELSE l.credit
59         END - coalesce(sum(pl.amount_currency), 0)
60         FROM payment_line pl
61         INNER JOIN payment_order po ON (pl.order_id = po.id)
62         WHERE move_line_id = l.id AND po.state != 'cancel')''' \
63         + x[1] + str(x[2])+' ',args))
64
65         cr.execute(('''select id
66             from account_move_line l
67             where account_id in (select id
68                 from account_account
69                 where type=%s and active)
70             and reconcile_id is null
71             and credit > 0
72             and ''' + where + ' and ' + query), ('payable',) )
73
74         res = cr.fetchall()
75         if not len(res):
76             return [('id','=','0')]
77         return [('id','in',map(lambda x:x[0], res))]
78
79     def line2bank(self, cr, uid, ids, payment_type=None, context=None):
80         """
81         Try to return for each account move line a corresponding bank
82         account according to the payment type.  This work using one of
83         the bank of the partner defined on the invoice eventually
84         associated to the line.
85         Return the first suitable bank for the corresponding partner.
86         """
87         payment_mode_obj = self.pool.get('payment.mode')
88         line2bank = {}
89         if not ids:
90             return {}
91         bank_type = payment_mode_obj.suitable_bank_types(cr, uid, payment_type,
92                 context=context)
93         for line in self.browse(cr, uid, ids, context=context):
94             line2bank[line.id] = False
95             if line.invoice and line.invoice.partner_bank:
96                 line2bank[line.id] = line.invoice.partner_bank.id
97             elif line.partner_id:
98                 if not line.partner_id.bank_ids:
99                     #raise osv.except_osv(_('Error !'), _('Partner '+ line.partner_id.name+ ' has no bank account defined'))
100                     line2bank[line.id] = False
101                 else:
102                     for bank in line.partner_id.bank_ids:
103                         if bank.state in bank_type:
104                             line2bank[line.id] = bank.id
105                             break
106                 if line.id not in line2bank and line.partner_id.bank_ids:
107                     line2bank[line.id] = line.partner_id.bank_ids[0].id
108             else:
109                 raise osv.except_osv(_('Error !'), _('No partner defined on entry line'))
110         return line2bank
111
112     _columns = {
113         'amount_to_pay' : fields.function(amount_to_pay, method=True,
114             type='float', string='Amount to pay', fnct_search=_to_pay_search),
115     }
116
117 account_move_line()
118
119 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
120