1
1
# -*- encoding: utf-8 -*-
2
2
##############################################################################
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
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.
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.
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>
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.
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.
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/>.
24
26
##############################################################################
27
from osv import fields, osv
29
class account_invoice(osv.osv):
30
_inherit='account.invoice'
28
from openerp.osv import fields, orm
31
class account_invoice(orm.Model):
32
_inherit = 'account.invoice'
32
34
'payment_type': fields.many2one('payment.type', 'Payment type'),
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):
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
40
partner_line = self.pool.get('res.partner').browse(cr, uid, partner_id)
42
if type=='in_invoice' or type=='in_refund':
43
payment_type = partner_line.payment_type_supplier.id
45
payment_type = partner_line.payment_type_customer.id
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
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)
50
def onchange_payment_type(self, cr, uid, ids, payment_type, partner_id, result = None):
55
def onchange_payment_type(self, cr, uid, ids, payment_type,
56
partner_id, result=None):
52
58
result = {'value': {}}
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
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
66
def action_move_create(self, cr, uid, ids, *args):
67
ret = super(account_invoice, self).action_move_create(cr, uid, ids, *args)
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)
81
for inv in self.browse(cr, uid, ids, context):
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})
91
aml_obj.write(cr, uid, move_line_ids,
92
{'partner_bank_id': inv.partner_bank_id.id})