~openerp-community/openobject-addons/mgmtsystem

« back to all changes in this revision

Viewing changes to account_payment_extension/wizard/wizard_payment_order.py

  • Committer: Jordi Esteve
  • Date: 2008-12-15 23:07:43 UTC
  • mto: (3378.1.27 trunk-extra-addons)
  • mto: This revision was merged to the branch mainline in revision 3379.
  • Revision ID: jesteve@alba-20081215230743-ef33s62lfwqyl3au
New module account_payment_extension: extends the account_payment module with a lot of features

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) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
import wizard
 
23
import pooler
 
24
from tools.misc import UpdateableStr
 
25
import time
 
26
 
 
27
 
 
28
FORM = UpdateableStr()
 
29
 
 
30
FIELDS = {
 
31
    'entries': {'string':'Entries', 'type':'many2many', 'relation':'account.move.line',},
 
32
    'communication2': {'string':'Communication 2', 'type':'char', 'size': 64, 'help':'The successor message of payment communication.'},
 
33
}
 
34
 
 
35
field_duedate={
 
36
    'duedate': {'string':'Due Date', 'type':'date','required':True, 'default': lambda *a: time.strftime('%Y-%m-%d'),},
 
37
    }
 
38
arch_duedate='''<?xml version="1.0"?>
 
39
<form string="Search Payment lines">
 
40
    <field name="duedate" />
 
41
</form>'''
 
42
 
 
43
 
 
44
def search_entries(self, cr, uid, data, context):
 
45
    search_due_date = data['form']['duedate']
 
46
 
 
47
    pool = pooler.get_pool(cr.dbname)
 
48
    order_obj = pool.get('payment.order')
 
49
    line_obj = pool.get('account.move.line')
 
50
 
 
51
    payment = order_obj.browse(cr, uid, data['id'],
 
52
            context=context)
 
53
    ctx = ''
 
54
    if payment.mode:
 
55
        ctx = '''context="{'journal_id': %d}"''' % payment.mode.journal.id
 
56
 
 
57
    # Search for move line to pay:
 
58
    domain = [('reconcile_id', '=', False),('account_id.type', '=', payment.type),('amount_to_pay', '<>', 0)]
 
59
    domain = domain + ['|',('date_maturity','<',search_due_date),('date_maturity','=',False)]
 
60
    if payment.mode:
 
61
        domain = [('payment_type','=',payment.mode.type.name)] + domain
 
62
    line_ids = line_obj.search(cr, uid, domain, context=context)
 
63
    FORM.string = '''<?xml version="1.0"?>
 
64
<form string="Populate Payment:">
 
65
    <field name="entries" colspan="4" height="300" width="800" nolabel="1"
 
66
        domain="[('id', 'in', [%s])]" %s/>
 
67
    <separator string="Extra message of payment communication" colspan="4"/>
 
68
    <field name="communication2" colspan="4"/>
 
69
</form>''' % (','.join([str(x) for x in line_ids]), ctx)
 
70
    return {}
 
71
 
 
72
 
 
73
def create_payment(self, cr, uid, data, context):
 
74
    line_ids= data['form']['entries'][0][2]
 
75
    if not line_ids: return {}
 
76
 
 
77
    pool= pooler.get_pool(cr.dbname)
 
78
    order_obj = pool.get('payment.order')
 
79
    line_obj = pool.get('account.move.line')
 
80
 
 
81
    payment = order_obj.browse(cr, uid, data['id'],
 
82
            context=context)
 
83
    t = payment.mode and payment.mode.type.id or None
 
84
    line2bank = pool.get('account.move.line').line2bank(cr, uid,
 
85
            line_ids, t, context)
 
86
 
 
87
    ## Finally populate the current payment with new lines:
 
88
    for line in line_obj.browse(cr, uid, line_ids, context=context):
 
89
        if payment.date_prefered == "now":
 
90
            #no payment date => immediate payment
 
91
            date_to_pay = False
 
92
        elif payment.date_prefered == 'due':
 
93
            date_to_pay = line.date_maturity
 
94
        elif payment.date_prefered == 'fixed':
 
95
            date_to_pay = payment.date_planned
 
96
        pool.get('payment.line').create(cr,uid,{
 
97
            'move_line_id': line.id,
 
98
            'amount_currency': line.amount_to_pay,
 
99
            'bank_id': line2bank.get(line.id),
 
100
            'order_id': payment.id,
 
101
            'partner_id': line.partner_id and line.partner_id.id or False,
 
102
            'communication': (line.ref and line.name!='/' and line.ref+'. '+line.name) or line.ref or line.name or '/',
 
103
            'communication2': data['form']['communication2'],
 
104
            'date': date_to_pay,
 
105
            }, context=context)
 
106
    return {}
 
107
 
 
108
 
 
109
class wizard_payment_order(wizard.interface):
 
110
    """
 
111
    Create a payment object with lines corresponding to the account move line
 
112
    to pay according to the date provided by the user and the mode-type payment of the order.
 
113
    Hypothesis:
 
114
    - Small number of non-reconcilied move line , payment mode and bank account type,
 
115
    - Big number of partner and bank account.
 
116
 
 
117
    If a type is given, unsuitable account move lines are ignored.
 
118
    """
 
119
    states = {
 
120
 
 
121
        'init': {
 
122
            'actions': [],
 
123
            'result': {
 
124
                'type': 'form',
 
125
                'arch': arch_duedate,
 
126
                'fields':field_duedate,
 
127
                'state': [
 
128
                    ('end','_Cancel'),
 
129
                    ('search','_Search', '', True)
 
130
                ]
 
131
            },
 
132
         },
 
133
 
 
134
        'search': {
 
135
            'actions': [search_entries],
 
136
            'result': {
 
137
                'type': 'form',
 
138
                'arch': FORM,
 
139
                'fields': FIELDS,
 
140
                'state': [
 
141
                    ('end','_Cancel'),
 
142
                    ('create','_Add to payment order', '', True)
 
143
                ]
 
144
            },
 
145
         },
 
146
        'create': {
 
147
            'actions': [],
 
148
            'result': {
 
149
                'type': 'action',
 
150
                'action': create_payment,
 
151
                'state': 'end'}
 
152
            },
 
153
        }
 
154
 
 
155
wizard_payment_order('populate_payment_ext')
 
156
 
 
157
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: