[MERGE] lp881356
[odoo/odoo.git] / addons / procurement / schedulers.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import time
23 from datetime import datetime
24 from dateutil.relativedelta import relativedelta
25 from osv import osv
26 from tools.translate import _
27 from tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
28 import tools
29 import netsvc
30 import pooler
31
32 class procurement_order(osv.osv):
33     _inherit = 'procurement.order'
34
35     def run_scheduler(self, cr, uid, automatic=False, use_new_cursor=False, context=None):
36         ''' Runs through scheduler.
37         @param use_new_cursor: False or the dbname
38         '''
39         self._procure_confirm(cr, uid, use_new_cursor=use_new_cursor, context=context)
40         self._procure_orderpoint_confirm(cr, uid, automatic=automatic,\
41                 use_new_cursor=use_new_cursor, context=context)
42
43     def _procure_confirm(self, cr, uid, ids=None, use_new_cursor=False, context=None):
44         '''
45         Call the scheduler to check the procurement order
46
47         @param self: The object pointer
48         @param cr: The current row, from the database cursor,
49         @param uid: The current user ID for security checks
50         @param ids: List of selected IDs
51         @param use_new_cursor: False or the dbname
52         @param context: A standard dictionary for contextual values
53         @return:  Dictionary of values
54         '''
55         if context is None:
56             context = {}
57
58         try:
59             if use_new_cursor:
60                 cr = pooler.get_db(use_new_cursor).cursor()
61             wf_service = netsvc.LocalService("workflow")
62
63             procurement_obj = self.pool.get('procurement.order')
64             if not ids:
65                 ids = procurement_obj.search(cr, uid, [('state', '=', 'exception')], order="date_planned")
66             for id in ids:
67                 wf_service.trg_validate(uid, 'procurement.order', id, 'button_restart', cr)
68             if use_new_cursor:
69                 cr.commit()
70             company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
71             maxdate = (datetime.today() + relativedelta(days=company.schedule_range)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
72             start_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
73             offset = 0
74             report = []
75             report_total = 0
76             report_except = 0
77             report_later = 0
78             while True:
79                 ids = procurement_obj.search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_order')], offset=offset, limit=500, order='priority, date_planned', context=context)
80                 for proc in procurement_obj.browse(cr, uid, ids, context=context):
81                     if maxdate >= proc.date_planned:
82                         wf_service.trg_validate(uid, 'procurement.order', proc.id, 'button_check', cr)
83                     else:
84                         offset += 1
85                         report_later += 1
86
87                     if proc.state == 'exception':
88                         report.append(_('PROC %d: on order - %3.2f %-5s - %s') % \
89                                 (proc.id, proc.product_qty, proc.product_uom.name,
90                                     proc.product_id.name))
91                         report_except += 1
92                     report_total += 1
93                 if use_new_cursor:
94                     cr.commit()
95                 if not ids:
96                     break
97             offset = 0
98             ids = []
99             while True:
100                 report_ids = []
101                 ids = procurement_obj.search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_stock')], offset=offset)
102                 for proc in procurement_obj.browse(cr, uid, ids):
103                     if maxdate >= proc.date_planned:
104                         wf_service.trg_validate(uid, 'procurement.order', proc.id, 'button_check', cr)
105                         report_ids.append(proc.id)
106                     else:
107                         report_later += 1
108                     report_total += 1
109
110                     if proc.state == 'exception':
111                         report.append(_('PROC %d: from stock - %3.2f %-5s - %s') % \
112                                 (proc.id, proc.product_qty, proc.product_uom.name,
113                                     proc.product_id.name,))
114                         report_except += 1
115
116                 if use_new_cursor:
117                     cr.commit()
118                 offset += len(ids)
119                 if not ids: break
120             end_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
121             if uid:
122                 request = self.pool.get('res.request')
123                 summary = _("""Here is the procurement scheduling report.
124
125         Start Time: %s 
126         End Time: %s 
127         Total Procurements processed: %d 
128         Procurements with exceptions: %d 
129         Skipped Procurements (scheduled date outside of scheduler range) %d 
130
131         Exceptions:\n""") % (start_date, end_date, report_total, report_except, report_later)
132                 summary += '\n'.join(report)
133                 request.create(cr, uid,
134                     {'name': "Procurement Processing Report.",
135                         'act_from': uid,
136                         'act_to': uid,
137                         'body': summary,
138                     })
139
140             if use_new_cursor:
141                 cr.commit()
142         finally:
143             if use_new_cursor:
144                 try:
145                     cr.close()
146                 except Exception:
147                     pass
148         return {}
149
150     def _prepare_automatic_op_procurement(self, cr, uid, product, warehouse, location_id, context=None):
151         return {'name': _('Automatic OP: %s') % (product.name,),
152                 'origin': _('SCHEDULER'),
153                 'date_planned': datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
154                 'product_id': product.id,
155                 'product_qty': -product.virtual_available,
156                 'product_uom': product.uom_id.id,
157                 'location_id': location_id,
158                 'company_id': warehouse.company_id.id,
159                 'procure_method': 'make_to_order',}
160
161     def create_automatic_op(self, cr, uid, context=None):
162         """
163         Create procurement of  virtual stock < 0
164
165         @param self: The object pointer
166         @param cr: The current row, from the database cursor,
167         @param uid: The current user ID for security checks
168         @param context: A standard dictionary for contextual values
169         @return:  Dictionary of values
170         """
171         if context is None:
172             context = {}
173         product_obj = self.pool.get('product.product')
174         proc_obj = self.pool.get('procurement.order')
175         warehouse_obj = self.pool.get('stock.warehouse')
176         wf_service = netsvc.LocalService("workflow")
177
178         warehouse_ids = warehouse_obj.search(cr, uid, [], context=context)
179         products_ids = product_obj.search(cr, uid, [('purchase_ok', '=', True)], order='id', context=context)
180
181         for warehouse in warehouse_obj.browse(cr, uid, warehouse_ids, context=context):
182             context['warehouse'] = warehouse
183             # Here we check products availability.
184             # We use the method 'read' for performance reasons, because using the method 'browse' may crash the server.
185             for product_read in product_obj.read(cr, uid, products_ids, ['virtual_available'], context=context):
186                 if product_read['virtual_available'] >= 0.0:
187                     continue
188
189                 product = product_obj.browse(cr, uid, [product_read['id']], context=context)[0]
190                 if product.supply_method == 'buy':
191                     location_id = warehouse.lot_input_id.id
192                 elif product.supply_method == 'produce':
193                     location_id = warehouse.lot_stock_id.id
194                 else:
195                     continue
196                 proc_id = proc_obj.create(cr, uid,
197                             self._prepare_automatic_op_procurement(cr, uid, product, warehouse, location_id, context=context),
198                             context=context)
199                 wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
200                 wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)
201         return True
202
203     def _get_orderpoint_date_planned(self, cr, uid, orderpoint, start_date, context=None):
204         date_planned = start_date + \
205                        relativedelta(days=orderpoint.product_id.seller_delay or 0.0)
206         return date_planned.strftime(DEFAULT_SERVER_DATE_FORMAT)
207
208     def _prepare_orderpoint_procurement(self, cr, uid, orderpoint, product_qty, context=None):
209         return {'name': orderpoint.name,
210                 'date_planned': self._get_orderpoint_date_planned(cr, uid, orderpoint, datetime.today(), context=context),
211                 'product_id': orderpoint.product_id.id,
212                 'product_qty': product_qty,
213                 'company_id': orderpoint.company_id.id,
214                 'product_uom': orderpoint.product_uom.id,
215                 'location_id': orderpoint.location_id.id,
216                 'procure_method': 'make_to_order',
217                 'origin': orderpoint.name}
218
219     def _procure_orderpoint_confirm(self, cr, uid, automatic=False,\
220             use_new_cursor=False, context=None, user_id=False):
221         '''
222         Create procurement based on Orderpoint
223         use_new_cursor: False or the dbname
224
225         @param self: The object pointer
226         @param cr: The current row, from the database cursor,
227         @param user_id: The current user ID for security checks
228         @param context: A standard dictionary for contextual values
229         @param param: False or the dbname
230         @return:  Dictionary of values
231         """
232         '''
233         if context is None:
234             context = {}
235         if use_new_cursor:
236             cr = pooler.get_db(use_new_cursor).cursor()
237         orderpoint_obj = self.pool.get('stock.warehouse.orderpoint')
238         location_obj = self.pool.get('stock.location')
239         procurement_obj = self.pool.get('procurement.order')
240         request_obj = self.pool.get('res.request')
241         wf_service = netsvc.LocalService("workflow")
242         report = []
243         offset = 0
244         ids = [1]
245         if automatic:
246             self.create_automatic_op(cr, uid, context=context)
247         while ids:
248             ids = orderpoint_obj.search(cr, uid, [], offset=offset, limit=100)
249             for op in orderpoint_obj.browse(cr, uid, ids, context=context):
250                 if op.procurement_id.state != 'exception':
251                     if op.procurement_id and op.procurement_id.purchase_id and op.procurement_id.purchase_id.state in ('draft', 'confirmed'):
252                         continue
253                 prods = location_obj._product_virtual_get(cr, uid,
254                         op.location_id.id, [op.product_id.id],
255                         {'uom': op.product_uom.id})[op.product_id.id]
256
257                 if prods < op.product_min_qty:
258                     qty = max(op.product_min_qty, op.product_max_qty)-prods
259
260                     reste = qty % op.qty_multiple
261                     if reste > 0:
262                         qty += op.qty_multiple - reste
263
264                     if qty <= 0:
265                         continue
266                     if op.product_id.type not in ('consu'):
267                         if op.procurement_draft_ids:
268                         # Check draft procurement related to this order point
269                             pro_ids = [x.id for x in op.procurement_draft_ids]
270                             procure_datas = procurement_obj.read(cr, uid, pro_ids, ['id','product_qty'], context=context, order='product_qty desc')
271                             to_generate = qty
272                             for proc_data in procure_datas:
273                                 if to_generate >= proc_data['product_qty']:
274                                     wf_service.trg_validate(uid, 'procurement.order', proc_data['id'], 'button_confirm', cr)
275                                     procurement_obj.write(cr, uid, [proc_data['id']],  {'origin': op.name}, context=context)
276                                     to_generate -= proc_data['product_qty']
277                                 if not to_generate:
278                                     break
279                             qty = to_generate
280
281                     if qty:
282                         proc_id = procurement_obj.create(cr, uid,
283                                                          self._prepare_orderpoint_procurement(cr, uid, op, qty, context=context),
284                                                          context=context)
285                         wf_service.trg_validate(uid, 'procurement.order', proc_id,
286                                 'button_confirm', cr)
287                         wf_service.trg_validate(uid, 'procurement.order', proc_id,
288                                 'button_check', cr)
289                         orderpoint_obj.write(cr, uid, [op.id],
290                                 {'procurement_id': proc_id}, context=context)
291             offset += len(ids)
292             if use_new_cursor:
293                 cr.commit()
294         if user_id and report:
295             request_obj.create(cr, uid, {
296                 'name': 'Orderpoint report.',
297                 'act_from': user_id,
298                 'act_to': user_id,
299                 'body': '\n'.join(report)
300             })
301         if use_new_cursor:
302             cr.commit()
303             cr.close()
304         return {}
305
306 procurement_order()
307
308 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: