[MERGE] Sync with trunk
[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 openerp.osv import osv
23 from openerp.tools.translate import _
24
25 class account_move_line(osv.osv):
26     _inherit = "account.move.line"
27
28     def line2bank(self, cr, uid, ids, payment_type=None, context=None):
29         """
30         Try to return for each Ledger Posting line a corresponding bank
31         account according to the payment type.  This work using one of
32         the bank of the partner defined on the invoice eventually
33         associated to the line.
34         Return the first suitable bank for the corresponding partner.
35         """
36         payment_mode_obj = self.pool.get('payment.mode')
37         line2bank = {}
38         if not ids:
39             return {}
40         bank_type = payment_mode_obj.suitable_bank_types(cr, uid, payment_type,
41                 context=context)
42         for line in self.browse(cr, uid, ids, context=context):
43             line2bank[line.id] = False
44             if line.invoice and line.invoice.partner_bank_id:
45                 line2bank[line.id] = line.invoice.partner_bank_id.id
46             elif line.partner_id:
47                 if not line.partner_id.bank_ids:
48                     line2bank[line.id] = False
49                 else:
50                     for bank in line.partner_id.bank_ids:
51                         if bank.state in bank_type:
52                             line2bank[line.id] = bank.id
53                             break
54                 if line.id not in line2bank and line.partner_id.bank_ids:
55                     line2bank[line.id] = line.partner_id.bank_ids[0].id
56             else:
57                 raise osv.except_osv(_('Error!'), _('There is no partner defined on the entry line.'))
58         return line2bank
59
60
61 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: