1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
26 from mx import DateTime
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):
33 use_new_cursor: False or the dbname
39 cr = pooler.get_db(use_new_cursor).cursor()
40 wf_service = netsvc.LocalService("workflow")
42 procurement_obj = self.pool.get('mrp.procurement')
44 ids=procurement_obj.search(cr,uid,[], order="date_planned")
46 wf_service.trg_validate(uid, 'mrp.procurement', id, 'button_restart', cr)
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')
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)
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))
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)
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,))
101 end_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
103 request = self.pool.get('res.request')
104 summary = '''Here is the procurement scheduling report.
106 Computation Started; %s
107 Computation Finished; %s
109 Total procurement: %d
110 Exception procurement: %d
111 Not run now procurement: %d
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.",
127 def create_automatic_op(self,cr, uid, context=None):
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")
135 warehouse_ids = warehouse_obj.search(cr, uid, [], context=context)
137 cr.execute('select id from product_product')
138 products_id = [x for x, in cr.fetchall()]
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:
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
153 proc_id = proc_obj.create(cr, uid, {
154 'name': 'PROC:Automatic OP for product:%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',
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)
166 def _procure_orderpoint_confirm(self, cr, uid, automatic=False,\
167 use_new_cursor=False, context=None, user_id=False):
169 use_new_cursor: False or the dbname
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")
184 self.create_automatic_op(cr, uid, context=context)
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'):
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
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
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,
214 'product_uom': op.product_uom.id,
215 'location_id': op.warehouse_id.lot_input_id.id,
216 'procure_method': 'make_to_order',
219 wf_service.trg_validate(uid, 'mrp.procurement', proc_id,
220 'button_confirm', cr)
221 wf_service.trg_validate(uid, 'mrp.procurement', proc_id,
223 orderpoint_obj.write(cr, uid, [op.id],
224 {'procurement_id': proc_id})
228 if user_id and report:
229 request_obj.create(cr, uid, {
230 'name': 'Orderpoint report.',
233 'body': '\n'.join(report)