~openerp-community/openobject-addons/mgmtsystem

« back to all changes in this revision

Viewing changes to account_payment_extension/account_invoice.py

  • Committer: Jordi Esteve
  • Date: 2008-12-15 23:07:43 UTC
  • mto: (3378.1.27 trunk-extra-addons)
  • mto: This revision was merged to the branch mainline in revision 3379.
  • Revision ID: jesteve@alba-20081215230743-ef33s62lfwqyl3au
New module account_payment_extension: extends the account_payment module with a lot of features

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
 
 
3
##############################################################################
 
4
#
 
5
# Copyright (c) 2009 Zikzakmedia S.L. (http://www.zikzakmedia.com) All Rights Reserved.
 
6
# Copyright (c) 2008 Pablo Rocandio (salbet@gmail.com) All Rights Reserved.
 
7
# Copyright (c) 2006 ACYSOS S.L. (http://acysos.com) All Rights Reserved.
 
8
#
 
9
# WARNING: This program as such is intended to be used by professional
 
10
# programmers who take the whole responsability of assessing all potential
 
11
# consequences resulting from its eventual inadequacies and bugs
 
12
# End users who are looking for a ready-to-use solution with commercial
 
13
# garantees and support are strongly adviced to contract a Free Software
 
14
# Service Company
 
15
#
 
16
# This program is Free Software; you can redistribute it and/or
 
17
# modify it under the terms of the GNU General Public License
 
18
# as published by the Free Software Foundation; either version 2
 
19
# of the License, or (at your option) any later version.
 
20
#
 
21
# This program is distributed in the hope that it will be useful,
 
22
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
23
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
24
# GNU General Public License for more details.
 
25
#
 
26
# You should have received a copy of the GNU General Public License
 
27
# along with this program; if not, write to the Free Software
 
28
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
29
#
 
30
##############################################################################
 
31
 
 
32
import netsvc
 
33
from osv import fields, osv
 
34
 
 
35
class account_invoice(osv.osv):
 
36
    _inherit='account.invoice'
 
37
    _columns={
 
38
        'payment_type': fields.many2one('payment.type', 'Payment type'),
 
39
    }
 
40
 
 
41
    def onchange_partner_id2(self, cr, uid, ids, type, partner_id, date_invoice=False, payment_term=False, partner_bank_id=False):
 
42
        # Copy partner data to invoice, also the new field payment_type
 
43
        result = self.onchange_partner_id(cr, uid, ids, type, partner_id, date_invoice, payment_term, partner_bank_id)
 
44
        payment_type = False
 
45
        if partner_id:
 
46
            partner_line = self.pool.get('res.partner').browse(cr, uid, partner_id)
 
47
            if partner_line:
 
48
                if type=='in_invoice' or type=='in_refund':
 
49
                    payment_type = partner_line.payment_type_supplier.id
 
50
                else:
 
51
                    payment_type = partner_line.payment_type_customer.id
 
52
            if payment_type:
 
53
                result['value']['payment_type'] = payment_type
 
54
        return self.onchange_payment_type(cr, uid, ids, payment_type, partner_id, result)
 
55
 
 
56
    def onchange_payment_type(self, cr, uid, ids, payment_type, partner_id, result = {'value': {}}):
 
57
        if payment_type and partner_id:
 
58
            bank_types = self.pool.get('payment.type').browse(cr, uid, payment_type).suitable_bank_types
 
59
            if bank_types: # If the payment type is related with a bank account
 
60
                bank_types = [bt.code for bt in bank_types]
 
61
                partner_bank_obj = self.pool.get('res.partner.bank')
 
62
                args = [('partner_id', '=', partner_id), ('default_bank', '=', 1), ('state', 'in', bank_types)]
 
63
                bank_account_id = partner_bank_obj.search(cr, uid, args)
 
64
                if bank_account_id:
 
65
                    result['value']['partner_bank'] = bank_account_id[0]
 
66
                    return result
 
67
        result['value']['partner_bank'] = False
 
68
        return result
 
69
 
 
70
    def action_move_create(self, cr, uid, ids, *args):
 
71
        ret = super(account_invoice, self).action_move_create(cr, uid, ids, *args)
 
72
        if ret:
 
73
            for inv in self.browse(cr, uid, ids):
 
74
                move_line_ids = []
 
75
                for move_line in inv.move_id.line_id:
 
76
                    if (move_line.account_id.type == 'receivable' or move_line.account_id.type == 'payable') and move_line.state != 'reconciled' and not move_line.reconcile_id.id:
 
77
                        move_line_ids.append(move_line.id)
 
78
                if len(move_line_ids) and inv.partner_bank:
 
79
                    aml_obj = self.pool.get("account.move.line")
 
80
                    aml_obj.write(cr, uid, move_line_ids, {'partner_bank': inv.partner_bank.id})
 
81
        return ret
 
82
 
 
83
account_invoice()