1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (c) 2010 Pexego S.L. (http://www.pexego.es) All Rights Reserved.
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
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 General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
##############################################################################
23
Extension of the purchase orders to add payment info.
25
Based on the sale_payment module.
27
__author__ = "Borja López Soilán (Pexego) <borjals@pexego.es>"
30
from osv import fields, osv
32
class purchase_order(osv.osv):
33
_inherit = 'purchase.order'
35
'payment_term': fields.many2one('account.payment.term', 'Payment Term', help='The payment terms. They will be transferred to the invoice.'),
36
'payment_type': fields.many2one('payment.type', 'Payment type', help='The type of payment. It will be transferred to the invoice.'),
37
'partner_bank': fields.many2one('res.partner.bank','Bank Account', select=True, help='The bank account to pay to or to be paid from. It will be transferred to the invoice.'),
40
def onchange_partner_id(self, cr, uid, ids, partner_id):
42
Extends the onchange to set the payment info of the partner.
44
result = super(purchase_order, self).onchange_partner_id(cr, uid, ids, partner_id)
47
partner = self.pool.get('res.partner').browse(cr, uid, partner_id)
48
paytype_id = partner.payment_type_supplier.id
49
result['value']['payment_type'] = paytype_id
50
partner_payment_term_id = partner.property_payment_term_supplier and partner.property_payment_term_supplier.id or False
51
result['value']['payment_term'] = partner_payment_term_id
53
return self.onchange_paytype_id(cr, uid, ids, paytype_id, partner_id, result)
55
def onchange_paytype_id(self, cr, uid, ids, paytype_id, partner_id, result = {'value': {}}):
57
Detect changes of the payment type and set the bank account accordingly.
59
if paytype_id and partner_id:
60
paytype = self.pool.get('payment.type').browse(cr, uid, paytype_id)
61
if paytype.suitable_bank_types and paytype.active:
62
# if the payment type is related to a bank account
63
partner_bank_obj = self.pool.get('res.partner.bank')
64
args = [('partner_id', '=', partner_id), ('default_bank', '=', 1)]
65
bank_account_id = partner_bank_obj.search(cr, uid, args)
67
result['value']['partner_bank'] = bank_account_id[0]
69
result['value']['partner_bank'] = False
72
def action_invoice_create(self, cr, uid, ids, context=None):
74
Extend the invoice creation action to preset the payment options.
76
# Create the invoice as usual.
77
invoice_id = super(purchase_order, self).action_invoice_create(
82
# Check if the order has payment info.
85
for order in self.browse(cr, uid, ids):
86
if order.payment_type:
87
vals['payment_term'] = order.payment_term.id
88
if order.payment_type:
89
vals['payment_type'] = order.payment_type.id
90
if order.partner_bank:
91
vals['partner_bank_id'] = order.partner_bank.id
93
# Write the payment info into the invoice.
94
self.pool.get('account.invoice').write(cr, uid, [invoice_id], vals)
100
class stock_picking(osv.osv):
101
_inherit = 'stock.picking'
103
def action_invoice_create(self, cr, uid, ids, journal_id=False,
104
group=False, type='out_invoice', context=None):
106
Extend the invoice creation action to set the price type if needed.
108
# Create the invoices as usual-
109
res = super(stock_picking, self).action_invoice_create(cr, uid, ids,
110
journal_id=journal_id, group=group, type=type, context=context)
112
for picking_id, invoice_id in res.items():
113
picking = self.browse(cr, uid, picking_id, context=context)
115
# Check if the picking comes from a purchase
116
if picking.purchase_id:
117
# Use the payment options from the order
118
order = picking.purchase_id
120
if order.payment_term:
121
vals['payment_term'] = order.payment_term.id
122
if order.payment_type:
123
vals['payment_type'] = order.payment_type.id
124
if order.partner_bank:
125
vals['partner_bank_id'] = order.partner_bank.id
127
# Write the payment info into the invoice.
128
self.pool.get('account.invoice').write(cr, uid, [invoice_id], vals, context=context)
135
class res_partner(osv.osv):
137
Extends the partners to add a payment terms for purchases option.
139
_inherit = 'res.partner'
142
'property_payment_term_supplier': fields.property(
143
'account.payment.term',
145
relation='account.payment.term',
146
string ='Payment Term',
149
help="This payment term will be used instead of the default one for the current partner on purchases"),
155
class account_invoice(osv.osv):
157
Extend the invoices to autoselect the payment terms,
158
using either the supplier payment terms or the customer payment terms.
160
_inherit = 'account.invoice'
162
def onchange_partner_id(self, cr, uid, ids, type, partner_id,
163
date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
165
Extend the onchange to use the supplier payment terms if this is
169
result = super(account_invoice, self).onchange_partner_id(cr, uid, ids, type, partner_id,
170
date_invoice=date_invoice, payment_term=payment_term, partner_bank_id=partner_bank_id, company_id=company_id)
173
# Set the correct payment term
175
partner_payment_term_id = None
177
partner = self.pool.get('res.partner').browse(cr, uid, partner_id)
178
if type in ('in_invoice', 'in_refund'):
180
partner_payment_term_id = partner.property_payment_term_supplier and partner.property_payment_term_supplier.id or False
183
partner_payment_term_id = partner.property_payment_term and partner.property_payment_term.id or False
185
result['value']['payment_term'] = partner_payment_term_id
188
# Recalculate the due date if needed
190
if payment_term != partner_payment_term_id:
191
if partner_payment_term_id:
192
to_update = self.onchange_payment_term_date_invoice(cr, uid, ids, partner_payment_term_id, date_invoice)
193
result['value'].update(to_update['value'])
195
result['value']['date_due'] = False