1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 TeMPO Consulting, MSF
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.
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.
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/>.
20
##############################################################################
23
from osv import fields
27
class procurement_batch_cron(osv.osv):
28
_name = 'procurement.batch.cron'
32
'name': fields.char(size=64, string='Name', required=True),
33
'type': fields.selection([('standard', 'POs creation Batch (from orders)'), ('rules', 'POs creation Batch (replenishment rules)')], string='Type', required=True),
34
'request_ids': fields.one2many('res.request', 'batch_id', string='Associated Requests', readonly=True),
35
'cron_ids': fields.one2many('ir.cron', 'batch_id', string='Associated Cron tasks'),
36
'last_run_on': fields.datetime('Last run on', readonly=True),
42
def open_request_view(self, cr, uid, ids, context=None):
43
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'procurement_report', 'batch_requests_view')[1]
44
return {'type': 'ir.actions.act_window',
45
'res_model': 'procurement.batch.cron',
52
def _create_associated_cron(self, cr, uid, batch_id, vals, context=None):
54
Create cron according to type
56
cron_obj = self.pool.get('ir.cron')
58
data = {'active': vals.get('active', True),
60
'interval_number': vals.get('interval_number'),
61
'interval_type': vals.get('interval_type'),
62
'nextcall': vals.get('nextcall'),
65
'batch_id': int(batch_id),
66
'model': 'procurement.order',
67
'args': '(False, %s)' % batch_id}
69
if vals.get('type') == 'standard':
70
# Create a cron task for the standard batch
71
data.update({'function': 'procure_confirm',
72
'args': '(False, False, %s)' % batch_id,
73
'name': '%s - Run mrp scheduler' % vals.get('name', 'Run mrp scheduler')})
74
cron_obj.create(cr, uid, data, context=context)
76
# Create a cron for order cycle
77
data.update({'function': 'run_automatic_cycle',
78
'name': '%s - Run automatic cycle' % vals.get('name', 'Run automatic cycle')})
79
cron_obj.create(cr, uid, data, context=context)
81
# Create a cron for auto supply
82
data.update({'function': 'run_automatic_supply',
83
'name': '%s - Run automatic supply' % vals.get('name', 'Run automatic supply')})
84
cron_obj.create(cr, uid, data, context=context)
86
# Create a cron for threshold values
87
data.update({'function': 'run_threshold_value',
88
'name': '%s - Run threshold value' % vals.get('name', 'Run threshold value')})
89
cron_obj.create(cr, uid, data, context=context)
91
# Create a cron for min/max rules
92
data.update({'function': 'procure_orderpoint_confirm',
93
'args': '(False, False, %s)' % batch_id,
94
'name': '%s - Run Min/Max rules' % vals.get('name', 'Run Min/Max rules')})
95
cron_obj.create(cr, uid, data, context=context)
97
def create(self, cr, uid, vals, context=None):
99
Create the associated cron tasks according to parameters
101
vals.update({'args': '()'})
102
# Get the id of the new batch
103
batch_id = super(procurement_batch_cron, self).create(cr, uid, vals, context=context)
105
self._create_associated_cron(cr, uid, batch_id, vals, context=context)
110
def write(self, cr, uid, ids, vals, context=None):
112
Set batch modifications on associated cron tasks
114
cron_obj = self.pool.get('ir.cron')
116
if isinstance(ids, (int, long)):
119
for batch in self.browse(cr, uid, ids, context=context):
120
if vals.get('type') and vals.get('type') != batch.type:
121
for cron in batch.cron_ids:
122
cron_obj.unlink(cr, uid, cron.id, context=context)
123
self._create_associated_cron(cr, uid, batch.id, vals, context=context)
125
for cron in batch.cron_ids:
128
cron_obj.write(cr, uid, cron.id, vals, context=context)
130
return super(procurement_batch_cron, self).write(cr, uid, ids, vals, context=context)
133
def read(self, cr, uid, ids, fields_to_read=None, context=None, load='_classic_read'):
134
if not fields_to_read:
137
res = super(procurement_batch_cron, self).read(cr, uid, ids, fields_to_read, context=context)
139
if 'nextcall' in fields_to_read:
140
for data in ([res] if isinstance(res, dict) else res):
141
cron_ids = self.pool.get('ir.cron').search(cr, uid, [('batch_id', '=', data['id'])])
143
nextcall = self.pool.get('ir.cron').browse(cr, uid, cron_ids[0]).nextcall
144
data.update({'nextcall': nextcall})
148
procurement_batch_cron()
151
class procurement_order(osv.osv):
152
_name = 'procurement.order'
153
_inherit = 'procurement.order'
155
def procure_confirm(self, cr, uid, ids=None, use_new_cursor=False, batch_id=False, context=None):
159
ctx.update({'batch_id': batch_id})
160
return self._procure_confirm(cr, uid, ids, use_new_cursor, context=ctx)
162
def procure_orderpoint_confirm(self, cr, uid, automatic=False, use_new_cursor=False, \
163
batch_id=False, context=None, user_id=False):
167
ctx.update({'batch_id': batch_id})
168
return self._procure_orderpoint_confirm(cr, uid, automatic, use_new_cursor, context=ctx, user_id=user_id)
170
def _hook_request_vals(self, cr, uid, *args, **kwargs):
172
Add the batch_id in request
174
request_obj = self.pool.get('res.request')
175
res = super(procurement_order, self)._hook_request_vals(cr, uid, *args, **kwargs)
176
if 'context' in kwargs:
177
batch_id = kwargs['context'].get('batch_id', False)
178
res.update({'batch_id': batch_id})
180
# Remove the link between batch and old requests
182
self.pool.get('procurement.batch.cron').write(cr, uid, batch_id, {'last_run_on': time.strftime('%Y-%m-%d %H:%M:%S')})
183
old_request = request_obj.search(cr, uid, [('batch_id', '=', batch_id), ('name', '=', res.get('name', 'Procurement Processing Report.'))])
184
request_obj.write(cr, uid, old_request, {'batch_id': False})
187
def _hook_add_purchase_created(self, cr, uid, *args, **kwargs):
189
Returns the created docs in report
191
if not 'purchase_ids' in kwargs:
194
created_doc = '''################################
195
Created documents : \n'''
196
for proc in self.browse(cr, uid, kwargs['purchase_ids']):
197
created_doc += " * %s => %s \n" % (proc.name, proc.purchase_id.name)
205
class ir_cron(osv.osv):
210
'batch_id': fields.many2one('procurement.batch.cron', string='Batch', ondelete='cascade'),
216
class res_request(osv.osv):
217
_name = 'res.request'
218
_inherit = 'res.request'
221
'batch_id': fields.many2one('procurement.batch.cron', string='Batch', ondelete='cascade'),
226
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: