~pedro-q/account-payment/7.0-account_payment_extension-IMP-payment-order-manual-moves

« back to all changes in this revision

Viewing changes to account_payment_extension/account_invoice.py

  • Committer: Raphaël Valyi
  • Author(s): renatonlima
  • Date: 2013-09-24 15:15:21 UTC
  • mfrom: (100.1.1 account-payment)
  • Revision ID: rvalyi@gmail.com-20130924151521-bjjao9i9uc0jw3s1
[MERGE] merged lp:~renatonlima/account-payment/account-payment-extension-7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- encoding: utf-8 -*-
2
2
##############################################################################
3
3
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (c) 2008 Zikzakmedia S.L. (http://zikzakmedia.com) All Rights Reserved.
6
 
#                       Jordi Esteve <jesteve@zikzakmedia.com>
7
 
#    AvanzOSC, Avanzed Open Source Consulting
8
 
#    Copyright (C) 2011-2012 Iker Coranti (www.avanzosc.com). All Rights Reserved
9
 
#    $Id$
10
 
#
11
 
#    This program is free software: you can redistribute it and/or modify
12
 
#    it under the terms of the GNU Affero General Public License as published by
13
 
#    the Free Software Foundation, either version 3 of the License, or
14
 
#    (at your option) any later version.
15
 
#
16
 
#    This program is distributed in the hope that it will be useful,
17
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 
#    GNU Affero General Public License for more details.
20
 
#
21
 
#    You should have received a copy of the GNU Affero General Public License
22
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
4
# OpenERP, Open Source Management Solution
 
5
# Copyright (c) 2008 Zikzakmedia S.L. (http://zikzakmedia.com)
 
6
#                    All Rights Reserved.Jordi Esteve <jesteve@zikzakmedia.com>
 
7
# AvanzOSC, Avanzed Open Source Consulting
 
8
# Copyright (C) 2011-2012 Iker Coranti (www.avanzosc.com). All Rights Reserved
 
9
# Copyright (C) 2013 Akretion Ltda ME (www.akretion.com) All Rights Reserved
 
10
# Renato Lima <renato.lima@akretion.com.br>
 
11
# $Id$
 
12
#
 
13
# This program is free software: you can redistribute it and/or modify
 
14
# it under the terms of the GNU Affero General Public License as published by
 
15
# the Free Software Foundation, either version 3 of the License, or
 
16
# (at your option) any later version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU Affero General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU Affero General Public License
 
24
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
25
#
24
26
##############################################################################
25
27
 
26
 
import netsvc
27
 
from osv import fields, osv
28
 
 
29
 
class account_invoice(osv.osv):
30
 
    _inherit='account.invoice'
31
 
    _columns={
 
28
from openerp.osv import fields, orm
 
29
 
 
30
 
 
31
class account_invoice(orm.Model):
 
32
    _inherit = 'account.invoice'
 
33
    _columns = {
32
34
        'payment_type': fields.many2one('payment.type', 'Payment type'),
33
35
    }
34
36
 
35
 
    def onchange_partner_id(self, cr, uid, ids, type, partner_id, date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
36
 
        # Copy partner data to invoice, also the new field payment_type
37
 
        result = super(account_invoice, self).onchange_partner_id(cr, uid, ids, type, partner_id, date_invoice, payment_term, partner_bank_id, company_id)
 
37
    def onchange_partner_id(self, cr, uid, ids, type, partner_id,
 
38
                            date_invoice=False, payment_term=False,
 
39
                            partner_bank_id=False, company_id=False):
 
40
 
 
41
        result = super(account_invoice, self).onchange_partner_id(
 
42
            cr, uid, ids, type, partner_id, date_invoice, payment_term,
 
43
            partner_bank_id, company_id)
38
44
        payment_type = False
39
45
        if partner_id:
40
 
            partner_line = self.pool.get('res.partner').browse(cr, uid, partner_id)
41
 
            if partner_line:
42
 
                if type=='in_invoice' or type=='in_refund':
43
 
                    payment_type = partner_line.payment_type_supplier.id
44
 
                else:
45
 
                    payment_type = partner_line.payment_type_customer.id
46
 
            if payment_type:
47
 
                result['value']['payment_type'] = payment_type
48
 
        return self.onchange_payment_type(cr, uid, ids, payment_type, partner_id, result)
 
46
            partner = self.pool.get('res.partner').browse(cr, uid, partner_id)
 
47
            if type in ('in_invoice', 'in_refund'):
 
48
                payment_type = partner.payment_type_supplier.id
 
49
            else:
 
50
                payment_type = partner.payment_type_customer.id
 
51
            result['value']['payment_type'] = payment_type
 
52
        return self.onchange_payment_type(
 
53
            cr, uid, ids, payment_type, partner_id, result)
49
54
 
50
 
    def onchange_payment_type(self, cr, uid, ids, payment_type, partner_id, result = None):
51
 
        if result is None:
 
55
    def onchange_payment_type(self, cr, uid, ids, payment_type,
 
56
                            partner_id, result=None):
 
57
        if not result:
52
58
            result = {'value': {}}
 
59
 
53
60
        if payment_type and partner_id:
54
 
            bank_types = self.pool.get('payment.type').browse(cr, uid, payment_type).suitable_bank_types
55
 
            if bank_types: # If the payment type is related with a bank account
 
61
            bank_types = self.pool.get('payment.type').browse(
 
62
                cr, uid, payment_type).suitable_bank_types
 
63
            # If the payment type is related with a bank account
 
64
            if bank_types:
56
65
                bank_types = [bt.code for bt in bank_types]
57
66
                partner_bank_obj = self.pool.get('res.partner.bank')
58
 
                args = [('partner_id', '=', partner_id), ('default_bank', '=', 1), ('state', 'in', bank_types)]
 
67
                args = [('partner_id', '=', partner_id),
 
68
                    ('default_bank', '=', 1),
 
69
                    ('state', 'in', bank_types)]
59
70
                bank_account_id = partner_bank_obj.search(cr, uid, args)
60
71
                if bank_account_id:
61
72
                    result['value']['partner_bank_id'] = bank_account_id[0]
63
74
        result['value']['partner_bank_id'] = False
64
75
        return result
65
76
 
66
 
    def action_move_create(self, cr, uid, ids, *args):
67
 
        ret = super(account_invoice, self).action_move_create(cr, uid, ids, *args)
68
 
        if ret:
69
 
            for inv in self.browse(cr, uid, ids):
 
77
    def action_move_create(self, cr, uid, ids, context=None):
 
78
        result = super(account_invoice, self).action_move_create(
 
79
            cr, uid, ids, context)
 
80
        if result:
 
81
            for inv in self.browse(cr, uid, ids, context):
70
82
                move_line_ids = []
71
83
                for move_line in inv.move_id.line_id:
72
 
                    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:
 
84
                    if (move_line.account_id.type == 'receivable' or
 
85
                    move_line.account_id.type == 'payable') and \
 
86
                    move_line.state != 'reconciled' and \
 
87
                    not move_line.reconcile_id.id:
73
88
                        move_line_ids.append(move_line.id)
74
89
                if len(move_line_ids) and inv.partner_bank_id:
75
90
                    aml_obj = self.pool.get("account.move.line")
76
 
                    aml_obj.write(cr, uid, move_line_ids, {'partner_bank_id': inv.partner_bank_id.id})
77
 
        return ret
78
 
 
79
 
account_invoice()
 
91
                    aml_obj.write(cr, uid, move_line_ids,
 
92
                        {'partner_bank_id': inv.partner_bank_id.id})
 
93
        return result