1
# -*- encoding: utf-8 -*-
2
###############################################################################
4
# sale_quick_payment for OpenERP #
5
# Copyright (C) 2011 Akretion Sébastien BEAU <sebastien.beau@akretion.com> #
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. #
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. #
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/>. #
20
###############################################################################
22
from osv import osv, fields
24
from collections import Iterable
27
class sale_order(osv.osv):
28
_inherit = "sale.order"
31
'payment_id': fields.many2one('account.voucher', 'Payment'),
32
'payment_method_id': fields.many2one('payment.method', 'Payment Method'),
35
def copy(self, cr, uid, id, default=None, context=None):
41
return super(sale_order, self).copy(cr, uid, id, default, context=context)
44
def pay_sale_order(self, cr, uid, sale_id, journal_id, amount, date, context=None):
46
Generate a voucher for the payment
48
It will try to match with the invoice of the order by
49
matching the payment ref and the invoice origin.
51
The invoice does not necessarily exists at this point, so if yes,
52
it will be matched in the voucher, otherwise, the voucher won't
53
have any invoice lines and the payment lines will be reconciled
54
later with "auto-reconcile" if the option is used.
57
if isinstance(sale_id, Iterable):
60
voucher_obj = self.pool.get('account.voucher')
61
voucher_line_obj = self.pool.get('account.voucher.line')
62
move_line_obj = self.pool.get('account.move.line')
63
sale = self.browse(cr, uid, sale_id, context=context)
65
journal = self.pool.get('account.journal').browse(
66
cr, uid, journal_id, context=context)
68
voucher_vals = {'reference': sale.name,
69
'journal_id': journal_id,
72
'partner_id': sale.partner_id.id,
73
'account_id': journal.default_credit_account_id.id,
74
'currency_id': journal.company_id.currency_id.id,
75
'company_id': journal.company_id.id,
77
voucher_id = voucher_obj.create(cr, uid, voucher_vals, context=context)
79
# call on change to search the invoice lines
80
onchange_voucher = voucher_obj.onchange_partner_id(
82
partner_id=sale.partner_id.id,
83
journal_id=journal.id,
85
currency_id=journal.company_id.currency_id.id,
88
context=context)['value']
90
# keep in the voucher only the move line of the
91
# invoice (eventually) created for this order
93
if onchange_voucher.get('line_cr_ids'):
94
voucher_lines = onchange_voucher['line_cr_ids']
95
line_ids = [line['move_line_id'] for line in voucher_lines]
96
matching_ids = [line.id for line
97
in move_line_obj.browse(
98
cr, uid, line_ids, context=context)
99
if line.ref == sale.name]
100
matching_lines = [line for line
102
if line['move_line_id'] in matching_ids]
104
matching_line = matching_lines[0]
105
matching_line.update({
107
'voucher_id': voucher_id,
111
voucher_line_obj.create(cr, uid, matching_line, context=context)
113
wf_service = netsvc.LocalService("workflow")
114
wf_service.trg_validate(
115
uid, 'account.voucher', voucher_id, 'proforma_voucher', cr)
116
sale.write({'payment_id': voucher_id}, context=context)