[FIX] Schedule jobs even if their next time has passed.
[odoo/odoo.git] / addons / account_payment / wizard / wizard_payment_order.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 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',
32         'relation': 'account.move.line',},
33 }
34 field_duedate={
35     'duedate': {'string':'Due Date', 'type':'date','required':True, 'default': lambda *a: time.strftime('%Y-%m-%d'),},
36     }
37 arch_duedate='''<?xml version="1.0"?>
38 <form string="Search Payment lines">
39     <field name="duedate" />
40 </form>'''
41
42
43 def search_entries(self, cr, uid, data, context):
44     search_due_date=data['form']['duedate']
45
46     pool = pooler.get_pool(cr.dbname)
47     order_obj = pool.get('payment.order')
48     line_obj = pool.get('account.move.line')
49
50     payment = order_obj.browse(cr, uid, data['id'],
51             context=context)
52     ctx = ''
53     if payment.mode:
54         ctx = '''context="{'journal_id': %d}"''' % payment.mode.journal.id
55
56     # Search for move line to pay:
57     domain = [('reconcile_id', '=', False),('account_id.type', '=', 'payable'),('amount_to_pay', '>', 0)]
58     domain = domain + ['|',('date_maturity','<=',search_due_date),('date_maturity','=',False)]
59     line_ids = line_obj.search(cr, uid, domain, context=context)
60     FORM.string = '''<?xml version="1.0"?>
61 <form string="Populate Payment:">
62     <field name="entries" colspan="4" height="300" width="800" nolabel="1"
63         domain="[('id', 'in', [%s])]" %s/>
64 </form>''' % (','.join([str(x) for x in line_ids]), ctx)
65     return {}
66
67 def create_payment(self, cr, uid, data, context):
68     line_ids= data['form']['entries'][0][2]
69     if not line_ids: return {}
70
71     pool= pooler.get_pool(cr.dbname)
72     order_obj = pool.get('payment.order')
73     line_obj = pool.get('account.move.line')
74
75     payment = order_obj.browse(cr, uid, data['id'],
76             context=context)
77     t = payment.mode and payment.mode.type.id or None
78     line2bank = pool.get('account.move.line').line2bank(cr, uid,
79             line_ids, t, context)
80
81     ## Finally populate the current payment with new lines:
82     for line in line_obj.browse(cr, uid, line_ids, context=context):
83         if payment.date_prefered == "now":
84             #no payment date => immediate payment
85             date_to_pay = False
86         elif payment.date_prefered == 'due':
87             date_to_pay = line.date_maturity
88         elif payment.date_prefered == 'fixed':
89             date_to_pay = payment.date_planned
90         pool.get('payment.line').create(cr,uid,{
91             'move_line_id': line.id,
92             'amount_currency': line.amount_to_pay,
93             'bank_id': line2bank.get(line.id),
94             'order_id': payment.id,
95             'partner_id': line.partner_id and line.partner_id.id or False,
96             'communication': line.ref or '/',
97             'date': date_to_pay,
98             'currency': line.invoice and line.invoice.currency_id.id or False,
99             }, context=context)
100     return {}
101
102
103 class wizard_payment_order(wizard.interface):
104     """
105     Create a payment object with lines corresponding to the account move line
106     to pay according to the date and the mode provided by the user.
107     Hypothesis:
108     - Small number of non-reconcilied move line , payment mode and bank account type,
109     - Big number of partner and bank account.
110
111     If a type is given, unsuitable account move lines are ignored.
112     """
113     states = {
114
115         'init': {
116             'actions': [],
117             'result': {
118                 'type': 'form',
119                 'arch': arch_duedate,
120                 'fields':field_duedate,
121                 'state': [
122                     ('end','_Cancel'),
123                     ('search','_Search', '', True)
124                 ]
125             },
126          },
127
128         'search': {
129             'actions': [search_entries],
130             'result': {
131                 'type': 'form',
132                 'arch': FORM,
133                 'fields': FIELDS,
134                 'state': [
135                     ('end','_Cancel'),
136                     ('create','_Add to payment order', '', True)
137                 ]
138             },
139          },
140         'create': {
141             'actions': [],
142             'result': {
143                 'type': 'action',
144                 'action': create_payment,
145                 'state': 'end'}
146             },
147         }
148
149 wizard_payment_order('populate_payment')
150
151
152
153 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
154