[FIX] Schedule jobs even if their next time has passed.
[odoo/odoo.git] / addons / mrp / schedulers.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
23 from osv import osv
24 import netsvc
25 import pooler
26 from mx import DateTime
27 import time
28
29
30 class mrp_procurement(osv.osv):
31     _inherit = 'mrp.procurement'
32
33     def _procure_confirm(self, cr, uid, ids=None, use_new_cursor=False, context=None):
34         '''
35         use_new_cursor: False or the dbname
36         '''
37         if not context:
38             context = {}
39
40         if use_new_cursor:
41             cr = pooler.get_db(use_new_cursor).cursor()
42         wf_service = netsvc.LocalService("workflow")
43
44         procurement_obj = self.pool.get('mrp.procurement')
45         if not ids:
46             ids = procurement_obj.search(cr, uid, [], order="date_planned")
47         for id in ids:
48             wf_service.trg_validate(uid, 'mrp.procurement', id, 'button_restart', cr)
49         if use_new_cursor:
50             cr.commit()
51
52         company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
53         maxdate = DateTime.now() + DateTime.RelativeDateTime(days=company.schedule_range)
54         start_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
55         offset = 0
56         report = []
57         report_total = 0
58         report_except = 0
59         report_later = 0
60         while True:
61             cr.execute('select id from mrp_procurement where state=%s and procure_method=%s order by date_planned limit 500 offset %s', ('confirmed', 'make_to_order', offset))
62             ids = map(lambda x: x[0], cr.fetchall())
63             for proc in procurement_obj.browse(cr, uid, ids):
64                 if (maxdate.strftime('%Y-%m-%d')>=proc.date_planned):
65                     wf_service.trg_validate(uid, 'mrp.procurement', proc.id, 'button_check', cr)
66                 else:
67                     offset += 1
68                     report_later += 1
69             for proc in procurement_obj.browse(cr, uid, ids):
70                 if proc.state == 'exception':
71                     report.append('PROC %d: on order - %3.2f %-5s - %s' % \
72                             (proc.id, proc.product_qty, proc.product_uom.name,
73                                 proc.product_id.name))
74                     report_except += 1
75                 report_total += 1
76             if use_new_cursor:
77                 cr.commit()
78             if not ids:
79                 break
80
81         offset = 0
82         ids = []
83         while True:
84             report_ids = []
85             ids = self.pool.get('mrp.procurement').search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_stock')], offset=offset)
86             for proc in procurement_obj.browse(cr, uid, ids):
87                 if ((maxdate).strftime('%Y-%m-%d') >= proc.date_planned) :
88                     wf_service.trg_validate(uid, 'mrp.procurement', proc.id, 'button_check', cr)
89                     report_ids.append(proc.id)
90                 else:
91                     report_later += 1
92                 report_total += 1
93             for proc in procurement_obj.browse(cr, uid, report_ids):
94                 if proc.state == 'exception':
95                     report.append('PROC %d: from stock - %3.2f %-5s - %s' % \
96                             (proc.id, proc.product_qty, proc.product_uom.name,
97                                 proc.product_id.name,))
98                     report_except += 1
99             if use_new_cursor:
100                 cr.commit()
101             offset += len(ids)
102             if not ids: break
103         end_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
104         if uid:
105             request = self.pool.get('res.request')
106             summary = '''Here is the procurement scheduling report.
107
108     Computation Started; %s
109     Computation Finished; %s
110
111     Total procurement: %d
112     Exception procurement: %d
113     Not run now procurement: %d
114
115     Exceptions;
116     '''% (start_date, end_date, report_total, report_except, report_later)
117             summary += '\n'.join(report)
118             request.create(cr, uid,
119                 {'name': "Procurement calculation report.",
120                     'act_from': uid,
121                     'act_to': uid,
122                     'body': summary,
123                 })
124         if use_new_cursor:
125             cr.commit()
126             cr.close()
127         return {}
128
129     def create_automatic_op(self, cr, uid, context=None):
130         if not context:
131             context = {}
132         product_obj = self.pool.get('product.product')
133         proc_obj = self.pool.get('mrp.procurement')
134         warehouse_obj = self.pool.get('stock.warehouse')
135         wf_service = netsvc.LocalService("workflow")
136
137         warehouse_ids = warehouse_obj.search(cr, uid, [], context=context)
138
139         cr.execute('select id from product_product')
140         products_id = [x for x, in cr.fetchall()]
141
142         for warehouse in warehouse_obj.browse(cr, uid, warehouse_ids, context=context):
143             context['warehouse'] = warehouse
144             for product in self.pool.get('product.product').browse(cr, uid, products_id, context=context):
145                 if product.virtual_available >= 0.0:
146                     continue
147
148                 newdate = DateTime.now()
149                 if product.supply_method == 'buy':
150                     location_id = warehouse.lot_input_id.id
151                 elif product.supply_method == 'produce':
152                     location_id = warehouse.lot_stock_id.id
153                 else:
154                     continue
155                 proc_id = proc_obj.create(cr, uid, {
156                     'name': 'Automatic OP: %s' % product.name,
157                     'origin': 'SCHEDULER',
158                     'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
159                     'product_id': product.id,
160                     'product_qty': -product.virtual_available,
161                     'product_uom': product.uom_id.id,
162                     'location_id': location_id,
163                     'procure_method': 'make_to_order',
164                     })
165                 wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_confirm', cr)
166                 wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_check', cr)
167
168     def _procure_orderpoint_confirm(self, cr, uid, automatic=False,\
169             use_new_cursor=False, context=None, user_id=False):
170         '''
171         use_new_cursor: False or the dbname
172         '''
173         if not context:
174             context = {}
175         if use_new_cursor:
176             cr = pooler.get_db(use_new_cursor).cursor()
177         orderpoint_obj = self.pool.get('stock.warehouse.orderpoint')
178         location_obj = self.pool.get('stock.location')
179         procurement_obj = self.pool.get('mrp.procurement')
180         request_obj = self.pool.get('res.request')
181         wf_service = netsvc.LocalService("workflow")
182         report = []
183         offset = 0
184         ids = [1]
185         if automatic:
186             self.create_automatic_op(cr, uid, context=context)
187         while ids:
188             ids = orderpoint_obj.search(cr, uid, [], offset=offset, limit=100)
189             for op in orderpoint_obj.browse(cr, uid, ids):
190                 if op.procurement_id and op.procurement_id.purchase_id and op.procurement_id.purchase_id.state in ('draft', 'confirmed'):
191                     continue
192                 prods = location_obj._product_virtual_get(cr, uid,
193                         op.location_id.id, [op.product_id.id],
194                         {'uom': op.product_uom.id})[op.product_id.id]
195                 if prods < op.product_min_qty:
196                     qty = max(op.product_min_qty, op.product_max_qty)-prods
197                     reste = qty % op.qty_multiple
198                     if reste > 0:
199                         qty += op.qty_multiple - reste
200                     newdate = DateTime.now() + DateTime.RelativeDateTime(
201                             days=int(op.product_id.seller_delay))
202                     if op.product_id.supply_method == 'buy':
203                         location_id = op.warehouse_id.lot_input_id
204                     elif op.product_id.supply_method == 'produce':
205                         location_id = op.warehouse_id.lot_stock_id
206                     else:
207                         continue
208                     if qty <= 0:
209                         continue
210                     if op.product_id.type not in ('consu'):
211                         proc_id = procurement_obj.create(cr, uid, {
212                             'name': 'OP:' + str(op.id),
213                             'date_planned': newdate.strftime('%Y-%m-%d'),
214                             'product_id': op.product_id.id,
215                             'product_qty': qty,
216                             'product_uom': op.product_uom.id,
217                             'location_id': op.warehouse_id.lot_input_id.id,
218                             'procure_method': 'make_to_order',
219                             'origin': op.name
220                         })
221                         wf_service.trg_validate(uid, 'mrp.procurement', proc_id,
222                                 'button_confirm', cr)
223                         wf_service.trg_validate(uid, 'mrp.procurement', proc_id,
224                                 'button_check', cr)
225                         orderpoint_obj.write(cr, uid, [op.id],
226                                 {'procurement_id': proc_id})
227             offset += len(ids)
228             if use_new_cursor:
229                 cr.commit()
230         if user_id and report:
231             request_obj.create(cr, uid, {
232                 'name': 'Orderpoint report.',
233                 'act_from': user_id,
234                 'act_to': user_id,
235                 'body': '\n'.join(report)
236                 })
237         if use_new_cursor:
238             cr.commit()
239             cr.close()
240         return {}
241 mrp_procurement()
242 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: