~pedro.baeza/account-payment/6.1-payment-extension_context-handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    AvanzOSC, Avanzed Open Source Consulting
#    Copyright (C) 2011-2012 Iker Coranti (www.avanzosc.com). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from lxml import etree
from osv import osv, fields

import pooler



class payment_order_create(osv.osv_memory):
    """
    Create a payment object with lines corresponding to the account move line
    to pay according to the date provided by the user and the mode-type payment of the order.
    Hypothesis:
    - Small number of non-reconcilied move line , payment mode and bank account type,
    - Big number of partner and bank account.

    If a type is given, unsuitable account move lines are ignored.
    """
    _inherit = 'payment.order.create'
    _description = ''

    _columns={
        'communication2':fields.char ('Communication 2',size = 64, help ='The successor message of payment communication.'),
        'amount':fields.float ('Amount', help ='Next step will automatically select payments up to this amount as long as account moves have bank account if that is required by the selected payment mode.'),
        'show_refunds':fields.boolean('Show Refunds', help = 'Indicates if search should include refunds.'),
              }

    _defaults={
               'show_refunds': lambda *a: False,
               }

    def default_get(self, cr, uid, fields, context=None):
        """
        This function gets default values
        @param self: The object pointer
        @param cr: the current row, from the database cursor,
        @param uid: the current user’s ID for security checks,
        @param fields: List of fields for default value
        @param context: A standard dictionary for contextual values

        @return : default values of fields.
        """
        context = context or {}
        line_obj = self.pool.get('account.move.line')
        res = super(payment_order_create, self).default_get(cr, uid, fields, context=context)
        if 'entries' in fields:
            if context.get('line_ids'):
                res.update({'entries':  context['line_ids']})

        return res

    def search_entries(self, cr, uid, ids, context=None):
        context = context or {}
        pool = pooler.get_pool(cr.dbname)
        order_obj = self.pool.get('payment.order')
        line_obj = self.pool.get('account.move.line')
        mod_obj = self.pool.get('ir.model.data')
        data = self.browse(cr, uid, ids, context=context)[0]
        search_due_date = data.duedate
        show_refunds = data.show_refunds
        amount = data.amount
        payment = order_obj.browse(cr, uid, context.get('active_id'), context=context)
        # Search for move line to pay:
        domain = [('reconcile_id', '=', False), ('account_id.type', '=', payment.type), ('amount_to_pay', '<>', 0)]
        if payment.type =='payable' and not show_refunds:
            domain += [ ('credit','>',0) ]
        elif not show_refunds:
            domain += [('debit', '>', 0)]
        if payment.mode:
            domain += [('payment_type', '=', payment.mode.type.id)]
        domain += ['|',('date_maturity','<',search_due_date),('date_maturity','=',False)]
        line_ids = line_obj.search(cr, uid, domain, order='date_maturity', context=context)
        selected_ids = []
        if amount > 0.0:
            # If user specified an amount, search what moves match the criteria
            for line in pool.get('account.move.line').browse(cr, uid, line_ids, context):
                if abs(line.amount_to_pay) <= amount:
                    amount -= abs(line.amount_to_pay)
                    selected_ids.append( line.id )
        elif not amount:
            selected_ids = line_ids

        ctx = context.copy()
        ctx.update({'line_ids': selected_ids})
        model_data_ids = mod_obj.search(cr, uid,[('model', '=', 'ir.ui.view'), ('name', '=', 'view_create_payment_order_lines')], context=context)
        resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
        return {'name': ('Entrie Lines'),
                'context': ctx,
                'view_type': 'form',
                'view_mode': 'form',
                'res_model': 'payment.order.create',
                'views': [(resource_id,'form')],
                'type': 'ir.actions.act_window',
                'target': 'new',
        }

    def create_payment(self, cr, uid, ids, context=None):
        context = context or {}
        order_obj = self.pool.get('payment.order')
        line_obj = self.pool.get('account.move.line')
        payment_obj = self.pool.get('payment.line')
        data = self.browse(cr, uid, ids, context=context)[0]
        line_ids = [entry.id for entry in data.entries]
        if not line_ids:
            return {'type': 'ir.actions.act_window_close'}

        payment = order_obj.browse(cr, uid, context['active_id'], context=context)
        t = payment.mode and payment.mode.id or None
        line2bank = line_obj.line2bank(cr, uid, line_ids, t, context)
        ## Finally populate the current payment with new lines:
        for line in line_obj.browse(cr, uid, line_ids, context=context):
            if payment.date_prefered == "now":
                #no payment date => immediate payment
                date_to_pay = False
            elif payment.date_prefered == 'due':
                date_to_pay = line.date_maturity
            elif payment.date_prefered == 'fixed':
                date_to_pay = payment.date_scheduled
            if payment.type == 'payable':
                amount_to_pay = line.amount_to_pay
            else:
                amount_to_pay = -line.amount_to_pay

            payment_obj.create(cr, uid,{
                    'move_line_id': line.id,
                    'amount_currency': amount_to_pay,
                    'bank_id': line2bank.get(line.id),
                    'order_id': payment.id,
                    'partner_id': line.partner_id and line.partner_id.id or False,
                    'communication': (line.ref and line.name!='/' and line.ref+'. '+line.name) or line.ref or line.name or '/',
                    'communication2': data.communication2,
                    'date': date_to_pay,
                    'currency': line.invoice and line.invoice.currency_id.id or False,
                    'account_id': line.account_id.id,
                }, context=context)
        return {'type': 'ir.actions.act_window_close'}

payment_order_create()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: