~sebastien.beau/e-commerce-addons/e-commerce-addons-invoice-payment-method

« back to all changes in this revision

Viewing changes to sale_quick_payment/sale.py

  • Committer: sebastien beau
  • Date: 2012-04-22 12:48:34 UTC
  • mto: This revision was merged to the branch mainline in revision 193.
  • Revision ID: sebastien.beau@akretion.com.br-20120422124834-9a0crl5bj9rb4nnw
[ADD] add sale_quick_payment, a module to generate a payment from the sale order

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
###############################################################################
 
3
#                                                                             #
 
4
#   sale_quick_payment for OpenERP                                  #
 
5
#   Copyright (C) 2011 Akretion Sébastien BEAU <sebastien.beau@akretion.com>   #
 
6
#                                                                             #
 
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.                           #
 
11
#                                                                             #
 
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.                       #
 
16
#                                                                             #
 
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/>.     #
 
19
#                                                                             #
 
20
###############################################################################
 
21
 
 
22
from osv import osv, fields
 
23
import netsvc
 
24
from collections import Iterable
 
25
 
 
26
 
 
27
class sale_order(osv.osv):
 
28
    _inherit = "sale.order"
 
29
 
 
30
    _columns = {
 
31
        'payment_id': fields.many2one('account.voucher', 'Payment'),
 
32
        'payment_method_id': fields.many2one('payment.method', 'Payment Method'),
 
33
    }
 
34
 
 
35
    def copy(self, cr, uid, id, default=None, context=None):
 
36
        if not default:
 
37
            default = {}
 
38
        default.update({
 
39
            'payment_id': False,
 
40
        })
 
41
        return super(sale_order, self).copy(cr, uid, id, default, context=context)
 
42
 
 
43
 
 
44
    def pay_sale_order(self, cr, uid, sale_id, journal_id, amount, date, context=None):
 
45
        """
 
46
        Generate a voucher for the payment
 
47
 
 
48
        It will try to match with the invoice of the order by
 
49
        matching the payment ref and the invoice origin.
 
50
 
 
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.
 
55
 
 
56
        """
 
57
        if isinstance(sale_id, Iterable):
 
58
            sale_id = sale_id[0]
 
59
 
 
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)
 
64
 
 
65
        journal = self.pool.get('account.journal').browse(
 
66
            cr, uid, journal_id, context=context)
 
67
 
 
68
        voucher_vals = {'reference': sale.name,
 
69
                        'journal_id': journal_id,
 
70
                        'amount': amount,
 
71
                        'date': date,
 
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,
 
76
                        'type': 'receipt', }
 
77
        voucher_id = voucher_obj.create(cr, uid, voucher_vals, context=context)
 
78
 
 
79
        # call on change to search the invoice lines
 
80
        onchange_voucher = voucher_obj.onchange_partner_id(
 
81
            cr, uid, [],
 
82
            partner_id=sale.partner_id.id,
 
83
            journal_id=journal.id,
 
84
            amount=amount,
 
85
            currency_id=journal.company_id.currency_id.id,
 
86
            ttype='receipt',
 
87
            date=date,
 
88
            context=context)['value']
 
89
 
 
90
        # keep in the voucher only the move line of the
 
91
        # invoice (eventually) created for this order
 
92
        matching_line = {}
 
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
 
101
                              in voucher_lines
 
102
                              if line['move_line_id'] in matching_ids]
 
103
            if matching_lines:
 
104
                matching_line = matching_lines[0]
 
105
                matching_line.update({
 
106
                    'amount': amount,
 
107
                    'voucher_id': voucher_id,
 
108
                })
 
109
 
 
110
        if matching_line:
 
111
            voucher_line_obj.create(cr, uid, matching_line, context=context)
 
112
 
 
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)
 
117
        return True