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