[IMP] : Added context=None on methods used for _constraints and replaced context...
[odoo/odoo.git] / addons / account_payment / account_move_line.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from operator import itemgetter
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=None):
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""", (tuple(ids),))
47         r = dict(cr.fetchall())
48         return r
49
50     def _to_pay_search(self, cr, uid, obj, name, args, context=None):
51         if not args:
52             return []
53         if not context: context = {}
54         line_obj = self.pool.get('account.move.line')
55         query = line_obj._query_get(cr, uid, context={})
56         where = ' and '.join(map(lambda x: '''(SELECT
57         CASE WHEN l.amount_currency < 0
58             THEN - l.amount_currency
59             ELSE l.credit
60         END - coalesce(sum(pl.amount_currency), 0)
61         FROM payment_line pl
62         INNER JOIN payment_order po ON (pl.order_id = po.id)
63         WHERE move_line_id = l.id
64         AND po.state != 'cancel'
65         ) %(operator)s %%s ''' % {'operator': x[1]}, args))
66         sql_args = tuple(map(itemgetter(2), args))
67
68         cr.execute(('''SELECT id
69             FROM account_move_line l
70             WHERE account_id IN (select id
71                 FROM account_account
72                 WHERE type=%s AND active)
73             AND reconcile_id IS null
74             AND credit > 0
75             AND ''' + where + ' and ' + query), ('payable',)+sql_args )
76
77         res = cr.fetchall()
78         if not res:
79             return [('id', '=', '0')]
80         return [('id', 'in', map(lambda x:x[0], res))]
81
82     def line2bank(self, cr, uid, ids, payment_type=None, context=None):
83         """
84         Try to return for each Ledger Posting line a corresponding bank
85         account according to the payment type.  This work using one of
86         the bank of the partner defined on the invoice eventually
87         associated to the line.
88         Return the first suitable bank for the corresponding partner.
89         """
90         payment_mode_obj = self.pool.get('payment.mode')
91         line2bank = {}
92         if not context: context = {}
93         if not ids:
94             return {}
95         bank_type = payment_mode_obj.suitable_bank_types(cr, uid, payment_type,
96                 context=context)
97         for line in self.browse(cr, uid, ids, context=context):
98             line2bank[line.id] = False
99             if line.invoice and line.invoice.partner_bank_id:
100                 line2bank[line.id] = line.invoice.partner_bank_id.id
101             elif line.partner_id:
102                 if not line.partner_id.bank_ids:
103                     line2bank[line.id] = False
104                 else:
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             else:
112                 raise osv.except_osv(_('Error !'), _('No partner defined on entry line'))
113         return line2bank
114
115     _columns = {
116         'amount_to_pay': fields.function(amount_to_pay, method=True,
117             type='float', string='Amount to pay', fnct_search=_to_pay_search),
118     }
119
120 account_move_line()
121
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: