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