~zaber/openobject-addons/zaber-custom

« back to all changes in this revision

Viewing changes to purchase_payment/purchase_payment.py

  • Committer: Aki Mimoto
  • Date: 2013-08-27 12:25:06 UTC
  • Revision ID: aki+launchpad@zaber.com-20130827122506-e4hildop20zysli2
[IMP] Add modules from extras repo: account_payment_extension/ purchase_payment/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (c) 2010 Pexego S.L. (http://www.pexego.es) All Rights Reserved.
 
6
#
 
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.
 
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 General Public License for more details.
 
16
#
 
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/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
"""
 
23
Extension of the purchase orders to add payment info.
 
24
 
 
25
Based on the sale_payment module.
 
26
"""
 
27
__author__ = "Borja López Soilán (Pexego) <borjals@pexego.es>"
 
28
 
 
29
import netsvc
 
30
from osv import fields, osv
 
31
 
 
32
class purchase_order(osv.osv):
 
33
    _inherit = 'purchase.order'
 
34
    _columns = {
 
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.'),
 
38
    }
 
39
 
 
40
    def onchange_partner_id(self, cr, uid, ids, partner_id):
 
41
        """
 
42
        Extends the onchange to set the payment info of the partner.
 
43
        """
 
44
        result = super(purchase_order, self).onchange_partner_id(cr, uid, ids, partner_id)
 
45
        paytype_id = False
 
46
        if 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
 
52
 
 
53
        return self.onchange_paytype_id(cr, uid, ids, paytype_id, partner_id, result)
 
54
 
 
55
    def onchange_paytype_id(self, cr, uid, ids, paytype_id, partner_id, result = {'value': {}}):
 
56
        """
 
57
        Detect changes of the payment type and set the bank account accordingly.
 
58
        """
 
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)
 
66
                if bank_account_id:
 
67
                    result['value']['partner_bank'] = bank_account_id[0]
 
68
                    return result
 
69
        result['value']['partner_bank'] = False
 
70
        return result
 
71
 
 
72
    def action_invoice_create(self, cr, uid, ids, context=None):
 
73
        """
 
74
        Extend the invoice creation action to preset the payment options.
 
75
        """
 
76
        # Create the invoice as usual.
 
77
        invoice_id = super(purchase_order, self).action_invoice_create(
 
78
                                                        cr, uid,
 
79
                                                        ids, context=context)
 
80
        
 
81
        #
 
82
        # Check if the order has payment info.
 
83
        #
 
84
        vals = {}
 
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
 
92
        if vals:
 
93
            # Write the payment info into the invoice.
 
94
            self.pool.get('account.invoice').write(cr, uid, [invoice_id], vals)
 
95
        return invoice_id
 
96
 
 
97
purchase_order()
 
98
 
 
99
 
 
100
class stock_picking(osv.osv):
 
101
    _inherit = 'stock.picking'
 
102
 
 
103
    def action_invoice_create(self, cr, uid, ids, journal_id=False,
 
104
                                group=False, type='out_invoice', context=None):
 
105
        """
 
106
        Extend the invoice creation action to set the price type if needed.
 
107
        """
 
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)
 
111
 
 
112
        for picking_id, invoice_id in res.items():
 
113
            picking = self.browse(cr, uid, picking_id, context=context)
 
114
 
 
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
 
119
                vals = {}
 
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
 
126
                if vals:
 
127
                    # Write the payment info into the invoice.
 
128
                    self.pool.get('account.invoice').write(cr, uid, [invoice_id], vals, context=context)
 
129
                    
 
130
        return res
 
131
 
 
132
stock_picking()
 
133
 
 
134
 
 
135
class res_partner(osv.osv):
 
136
    """
 
137
    Extends the partners to add a payment terms for purchases option.
 
138
    """
 
139
    _inherit = 'res.partner'
 
140
 
 
141
    _columns = {
 
142
        'property_payment_term_supplier': fields.property(
 
143
            'account.payment.term',
 
144
            type='many2one',
 
145
            relation='account.payment.term',
 
146
            string ='Payment Term',
 
147
            method=True,
 
148
            view_load=True,
 
149
            help="This payment term will be used instead of the default one for the current partner on purchases"),
 
150
    }
 
151
    
 
152
res_partner()
 
153
 
 
154
 
 
155
class account_invoice(osv.osv):
 
156
    """
 
157
    Extend the invoices to autoselect the payment terms,
 
158
    using either the supplier payment terms or the customer payment terms.
 
159
    """
 
160
    _inherit = 'account.invoice'
 
161
 
 
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):
 
164
        """
 
165
        Extend the onchange to use the supplier payment terms if this is
 
166
        a purchase invoice.
 
167
        """
 
168
 
 
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)
 
171
 
 
172
        #
 
173
        # Set the correct payment term
 
174
        #
 
175
        partner_payment_term_id = None
 
176
        if partner_id:
 
177
            partner = self.pool.get('res.partner').browse(cr, uid, partner_id)
 
178
            if type in ('in_invoice', 'in_refund'):
 
179
                # Purchase invoice
 
180
                partner_payment_term_id = partner.property_payment_term_supplier and partner.property_payment_term_supplier.id or False
 
181
            else:
 
182
                # Sale invoice
 
183
                partner_payment_term_id = partner.property_payment_term and partner.property_payment_term.id or False
 
184
 
 
185
        result['value']['payment_term'] = partner_payment_term_id
 
186
 
 
187
        #
 
188
        # Recalculate the due date if needed
 
189
        #
 
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'])
 
194
            else:
 
195
                result['value']['date_due'] = False
 
196
 
 
197
        return result
 
198
 
 
199
account_invoice()