[REF] purchase: search view of purchase order and form view of merge order wizard
[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 osv import fields, osv
23 from tools.translate import _
24
25 class account_move_line(osv.osv):
26     _inherit = "account.move.line"
27
28     def amount_to_pay(self, cr, uid, ids, name, arg={}, context={}):
29         """ Return the amount still to pay regarding all the payemnt orders
30         (excepting cancelled orders)"""
31         if not ids:
32             return {}
33         cr.execute("""SELECT ml.id,
34                     CASE WHEN ml.amount_currency < 0
35                         THEN - ml.amount_currency
36                         ELSE ml.credit
37                     END -
38                     (SELECT coalesce(sum(amount_currency),0)
39                         FROM payment_line pl
40                             INNER JOIN payment_order po
41                                 ON (pl.order_id = po.id)
42                         WHERE move_line_id = ml.id
43                         AND po.state != 'cancel') as amount
44                     FROM account_move_line ml
45                     WHERE id =ANY(%s)""" ,(ids,))
46         r=dict(cr.fetchall())
47         return r
48
49     def _to_pay_search(self, cr, uid, obj, name, args, context):
50         if not len(args):
51             return []
52         line_obj = self.pool.get('account.move.line')
53         query = line_obj._query_get(cr, uid, context={})
54         where = ' and '.join(map(lambda x: '''(SELECT
55         CASE WHEN l.amount_currency < 0
56             THEN - l.amount_currency
57             ELSE l.credit
58         END - coalesce(sum(pl.amount_currency), 0)
59         FROM payment_line pl
60         INNER JOIN payment_order po ON (pl.order_id = po.id)
61         WHERE move_line_id = l.id AND po.state != 'cancel')''' \
62         + x[1] + str(x[2])+' ',args))
63
64         cr.execute(('''select id
65             from account_move_line l
66             where account_id in (select id
67                 from account_account
68                 where type=%s and active)
69             and reconcile_id is null
70             and credit > 0
71             and ''' + where + ' and ' + query), ('payable',) )
72
73         res = cr.fetchall()
74         if not len(res):
75             return [('id','=','0')]
76         return [('id','in',map(lambda x:x[0], res))]
77
78     def line2bank(self, cr, uid, ids, payment_type=None, context=None):
79         """
80         Try to return for each Ledger Posting line a corresponding bank
81         account according to the payment type.  This work using one of
82         the bank of the partner defined on the invoice eventually
83         associated to the line.
84         Return the first suitable bank for the corresponding partner.
85         """
86         payment_mode_obj = self.pool.get('payment.mode')
87         line2bank = {}
88         if not ids:
89             return {}
90         bank_type = payment_mode_obj.suitable_bank_types(cr, uid, payment_type,
91                 context=context)
92         for line in self.browse(cr, uid, ids, context=context):
93             line2bank[line.id] = False
94             if line.invoice and line.invoice.partner_bank:
95                 line2bank[line.id] = line.invoice.partner_bank.id
96             elif line.partner_id:
97                 if not line.partner_id.bank_ids:
98                     #raise osv.except_osv(_('Error !'), _('Partner '+ line.partner_id.name+ ' has no bank account defined'))
99                     line2bank[line.id] = False
100                 else:
101                     for bank in line.partner_id.bank_ids:
102                         if bank.state in bank_type:
103                             line2bank[line.id] = bank.id
104                             break
105                 if line.id not in line2bank and line.partner_id.bank_ids:
106                     line2bank[line.id] = line.partner_id.bank_ids[0].id
107             else:
108                 raise osv.except_osv(_('Error !'), _('No partner defined on entry line'))
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()
117
118 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
119