From f9cc58e368eda8c44f2c515f7ef38a13f98cd8ed Mon Sep 17 00:00:00 2001 From: Fabien Pinckaers Date: Tue, 9 Sep 2008 11:11:44 +0200 Subject: [PATCH] Improved Scheduler bzr revid: fp@tinyerp.com-20080909091144-9lj9kyziwc250hwr --- addons/mrp/__init__.py | 5 +- addons/mrp/__terp__.py | 1 + addons/mrp/mrp.py | 42 +--- addons/mrp/mrp_wizard.xml | 8 +- addons/mrp/wizard/schedulers.py | 226 ------------------ .../wizard/wizard_orderpoint_procurement.py | 4 +- addons/mrp/wizard/wizard_procurement.py | 25 +- addons/mrp/wizard/wizard_schedulers_all.py | 24 +- addons/sale/sale.py | 6 +- addons/stock/wizard/wizard_partial_picking.py | 10 +- 10 files changed, 29 insertions(+), 322 deletions(-) diff --git a/addons/mrp/__init__.py b/addons/mrp/__init__.py index 1f42fa8786f..0cca0e4c805 100644 --- a/addons/mrp/__init__.py +++ b/addons/mrp/__init__.py @@ -31,6 +31,5 @@ import mrp import wizard import report - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: - +import company +import schedulers diff --git a/addons/mrp/__terp__.py b/addons/mrp/__terp__.py index f3fe4989e05..0a5220eddaf 100644 --- a/addons/mrp/__terp__.py +++ b/addons/mrp/__terp__.py @@ -69,6 +69,7 @@ "mrp_view.xml", "mrp_wizard.xml", "mrp_report.xml", + "company_view.xml" ], "active": False, "installable": True diff --git a/addons/mrp/mrp.py b/addons/mrp/mrp.py index e9877ce1349..e6365079726 100644 --- a/addons/mrp/mrp.py +++ b/addons/mrp/mrp.py @@ -868,10 +868,12 @@ class mrp_procurement(osv.osv): def action_produce_assign_product(self, cr, uid, ids, context={}): produce_id = False + company = self.pool.get('res.users').browse(cr, uid, uid, context).company_id for procurement in self.browse(cr, uid, ids): res_id = procurement.move_id.id loc_id = procurement.location_id.id newdate = DateTime.strptime(procurement.date_planned, '%Y-%m-%d') - DateTime.RelativeDateTime(days=procurement.product_id.product_tmpl_id.produce_delay or 0.0) + newdate = newdate - DateTime.RelativeDateTime(days=company.manufacturing_lead) produce_id = self.pool.get('mrp.production').create(cr, uid, { 'origin': procurement.origin, 'product_id': procurement.product_id.id, @@ -892,8 +894,9 @@ class mrp_procurement(osv.osv): wf_service.trg_validate(uid, 'mrp.production', produce_id, 'button_confirm', cr) return produce_id - def action_po_assign(self, cr, uid, ids): + def action_po_assign(self, cr, uid, ids, context={}): purchase_id = False + company = self.pool.get('res.users').browse(cr, uid, uid, context).company_id for procurement in self.browse(cr, uid, ids): res_id = procurement.move_id.id partner = procurement.product_id.seller_ids[0].name @@ -910,6 +913,7 @@ class mrp_procurement(osv.osv): price = self.pool.get('product.pricelist').price_get(cr, uid, [pricelist_id], procurement.product_id.id, qty, False, {'uom': uom_id})[pricelist_id] newdate = DateTime.strptime(procurement.date_planned, '%Y-%m-%d') - DateTime.RelativeDateTime(days=procurement.product_id.product_tmpl_id.seller_delay or 0.0) + newdate = newdate - DateTime.RelativeDateTime(days=company.po_lead) line = { 'name': procurement.product_id.name, 'product_qty': qty, @@ -969,45 +973,15 @@ class mrp_procurement(osv.osv): for id in ids: wf_service.trg_trigger(uid, 'mrp.procurement', id, cr) return res - def run_scheduler(self, cr, uid, user_id=False, schedule_cycle=1.0,\ - po_cycle=1.0, po_lead=1.0, security_lead=50.0, picking_lead=1.0,\ - automatic=False, use_new_cursor=False, context=None): + def run_scheduler(self, cr, uid, automatic=False, use_new_cursor=False, context=None): ''' use_new_cursor: False or the dbname ''' if not context: context={} - self.run_procure_confirm(cr, uid, schedule_cycle=schedule_cycle,\ - po_cycle=po_cycle, po_lead=po_lead, security_lead=security_lead,\ - picking_lead=picking_lead, user_id=user_id,\ + self._procure_confirm(cr, uid, use_new_cursor=use_new_cursor, context=context) + self._procure_orderpoint_confirm(cr, uid, automatic=automatic,\ use_new_cursor=use_new_cursor, context=context) - self.run_orderpoint_confirm(cr, uid, automatic=automatic,\ - use_new_cursor=use_new_cursor, context=context, user_id=user_id) - - def run_procure_confirm(self, cr, uid, user_id=False, schedule_cycle=1.0,\ - po_cycle=1.0, po_lead=1.0, security_lead=50.0, picking_lead=1.0, \ - use_new_cursor=False, context=None): - ''' - use_new_cursor: False or the dbname - ''' - from wizard.schedulers import _procure_confirm - if not context: - context={} - _procure_confirm(self, cr, uid, schedule_cycle=schedule_cycle,\ - po_cycle=po_cycle, po_lead=po_lead, security_lead=security_lead,\ - picking_lead=picking_lead, user_id=user_id, use_new_cursor=use_new_cursor,\ - context=context) - - def run_orderpoint_confirm(self, cr, uid, automatic=False, use_new_cursor=False,\ - context=None, user_id=False): - ''' - use_new_cursor: False or the dbname - ''' - from wizard.schedulers import _procure_orderpoint_confirm - if not context: - context={} - _procure_orderpoint_confirm(self, cr, uid, automatic=automatic,\ - use_new_cursor=use_new_cursor, context=context, user_id=user_id) mrp_procurement() diff --git a/addons/mrp/mrp_wizard.xml b/addons/mrp/mrp_wizard.xml index 14a162c9551..6e02dbc55e6 100644 --- a/addons/mrp/mrp_wizard.xml +++ b/addons/mrp/mrp_wizard.xml @@ -2,19 +2,19 @@ - Compute all schedulers + Compute All Schedulers mrp.procurement.compute.all - Compute procurement + Compute Procurements Only mrp.procurement.compute - Compute stock minimum rules + Compute Stock Minimum Rules Only mrp.procurement.orderpoint.compute @@ -31,4 +31,4 @@ - \ No newline at end of file + diff --git a/addons/mrp/wizard/schedulers.py b/addons/mrp/wizard/schedulers.py index 98ff98111ea..0f88cbc8c01 100644 --- a/addons/mrp/wizard/schedulers.py +++ b/addons/mrp/wizard/schedulers.py @@ -34,231 +34,5 @@ from mx import DateTime import netsvc from osv import osv -def _procure_confirm(self, cr, uid, schedule_cycle=1.0, po_cycle=1.0,\ - po_lead=1.0, security_lead=50.0, picking_lead=1.0, user_id=False,\ - use_new_cursor=False, context=None): - ''' - use_new_cursor: False or the dbname - ''' - if not context: - context={} - - if use_new_cursor: - cr = pooler.get_db(use_new_cursor).cursor() - wf_service = netsvc.LocalService("workflow") - - procurement_obj = pooler.get_pool(cr.dbname).get('mrp.procurement') - ids=procurement_obj.search(cr,uid,[], order="date_planned") - for id in ids: - wf_service.trg_validate(uid, 'mrp.procurement', id, 'button_restart', cr) - if use_new_cursor: - cr.commit() - - po_time = po_cycle + po_lead - maxdate = DateTime.now() + DateTime.RelativeDateTime(days=schedule_cycle + security_lead) - start_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss') - offset = 0 - report = [] - report_total = 0 - report_except = 0 - report_later = 0 - ids = [1] - while len(ids): - cr.execute('select id from mrp_procurement where state=%s and procure_method=%s order by date_planned limit 500 offset %d', ('confirmed','make_to_order',offset)) - ids = map(lambda x:x[0], cr.fetchall()) - for proc in procurement_obj.browse(cr, uid, ids): - if proc.product_id.supply_method=='produce': - wf_service.trg_validate(uid, 'mrp.procurement', proc.id, 'button_check', cr) - else: - if (maxdate + DateTime.RelativeDateTime(days=(proc.product_id.seller_delay or 0 + po_time))).strftime('%Y-%m-%d')>=proc.date_planned: - wf_service.trg_validate(uid, 'mrp.procurement', proc.id, 'button_check', cr) - else: - offset+=1 - report_later += 1 - for proc in procurement_obj.browse(cr, uid, ids): - if proc.state == 'exception': - report.append('PROC %d: on order - %3.2f %-5s - %s' % \ - (proc.id, proc.product_qty, proc.product_uom.name, - proc.product_id.name)) - report_except += 1 - report_total += 1 - if use_new_cursor: - cr.commit() - - offset = 0 - ids = [1] - while len(ids): - report_ids = [] - ids = pooler.get_pool(cr.dbname).get('mrp.procurement').search(cr, uid, [('state','=','confirmed'),('procure_method','=','make_to_stock')], offset=offset) - for proc in procurement_obj.browse(cr, uid, ids): - if (maxdate + DateTime.RelativeDateTime(days=picking_lead)).strftime('%Y-%m-%d') >= proc.date_planned: - wf_service.trg_validate(uid, 'mrp.procurement', proc.id, 'button_check', cr) - report_ids.append(proc.id) - else: - report_later +=1 - report_total +=1 - for proc in procurement_obj.browse(cr, uid, report_ids): - if proc.state == 'exception': - report.append('PROC %d: from stock - %3.2f %-5s - %s' % \ - (proc.id, proc.product_qty, proc.product_uom.name, - proc.product_id.name,)) - report_except +=1 - if use_new_cursor: - cr.commit() - offset += len(ids) - end_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss') - if user_id: - request = pooler.get_pool(cr.dbname).get('res.request') - summary = '''Here is the procurement scheduling report. - -Computation Started; %s -Computation Finnished; %s - -Total procurement: %d -Exception procurement: %d -Not run now procurement: %d - -Exceptions; -'''% (start_date,end_date,report_total, report_except,report_later) - summary += '\n'.join(report) - request.create(cr, uid, - {'name' : "Procurement calculation report.", - 'act_from' : user_id, - 'act_to' : user_id, - 'body': summary, - }) - if use_new_cursor: - cr.commit() - cr.close() - return {} - -def create_automatic_op(cr, uid, context=None): - if not context: - context={} - product_obj = pooler.get_pool(cr.dbname).get('product.product') - proc_obj = pooler.get_pool(cr.dbname).get('mrp.procurement') - warehouse_obj = pooler.get_pool(cr.dbname).get('stock.warehouse') - wf_service = netsvc.LocalService("workflow") - - cr.execute('select id from stock_warehouse') - warehouses = [x for x, in cr.fetchall()] - cr.execute('select id from product_product') - products_id = [x for x, in cr.fetchall()] - products = dict(zip(products_id, product_obj.browse(cr, uid, products_id, context))) - for warehouse in warehouses: - v_stock = product_obj._product_virtual_available(cr, uid, products_id, None, {'warehouse': warehouse}) - cr.execute("select lot_stock_id from stock_warehouse") - location_ids_str = ','.join([str(id) for id, in cr.fetchall()]) - stock_locations = warehouse_obj.read(cr, uid, [warehouse], ['lot_input_id', 'lot_stock_id'])[0] - for prod_id, available in v_stock.items(): - if available >= 0: - continue - newdate = DateTime.now() + DateTime.RelativeDateTime(days=products[prod_id].seller_delay) - if products[prod_id].supply_method == 'buy': - location_id = stock_locations['lot_input_id'][0] - elif products[prod_id].supply_method == 'produce': - location_id = stock_locations['lot_stock_id'][0] - else: - continue - proc_id = proc_obj.create(cr, uid, { - 'name': 'PROC:Automatic OP for product:%s' % products[prod_id].name, - 'origin': 'SCHEDULER', - 'date_planned': newdate.strftime('%Y-%m-%d'), - 'product_id': prod_id, - 'product_qty': -available, - 'product_uom': products[prod_id].uom_id.id, - 'location_id': location_id, - 'procure_method': 'make_to_order', - }) - wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_confirm', cr) - wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_check', cr) - - -def _procure_orderpoint_confirm(self, cr, uid, automatic=False,\ - use_new_cursor=False, context=None, user_id=False): - ''' - use_new_cursor: False or the dbname - ''' - if not context: - context={} - if use_new_cursor: - cr = pooler.get_db(use_new_cursor).cursor() - pool = pooler.get_pool(cr.dbname) - orderpoint_obj = pool.get('stock.warehouse.orderpoint') - location_obj = pool.get('stock.location') - procurement_obj = pool.get('mrp.procurement') - request_obj = pool.get('res.request') - wf_service = netsvc.LocalService("workflow") - report = [] - offset = 0 - ids = [1] - if automatic: - create_automatic_op(cr, uid, context=context) - while ids: - ids=orderpoint_obj.search(cr,uid,[],offset=offset,limit=100) - for op in orderpoint_obj.browse(cr, uid, ids): - #if op.procurement_id and op.procurement_id.purchase_id: - # if (op.procurement_id.purchase_id.state=='confirmed'): - # # - # # TODO: Should write a request with this warning - # # - # continue - # elif op.procurement_id.purchase_id.state=='draft': - # wf_service = netsvc.LocalService("workflow") - # wf_service.trg_validate(uid, 'purchase.order', - # op.procurement_id.purchase_id.id, 'purchase_cancel', cr) - try: - prods = location_obj._product_virtual_get(cr, uid, - op.warehouse_id.lot_stock_id.id, [op.product_id.id], - {'uom': op.product_uom.id})[op.product_id.id] - if prods < op.product_min_qty: - # order the quantity necessary to get to max(min_qty, max_qty) - qty = max(op.product_min_qty, op.product_max_qty)-prods - reste = qty % op.qty_multiple - if reste>0: - qty += op.qty_multiple-reste - newdate = DateTime.now() + DateTime.RelativeDateTime( - days=op.product_id.seller_delay) - if op.product_id.supply_method == 'buy': - location_id = op.warehouse_id.lot_input_id - elif op.product_id.supply_method == 'produce': - location_id = op.warehouse_id.lot_stock_id - else: - continue - proc_id = procurement_obj.create(cr, uid, { - 'name': 'OP:'+str(op.id), - 'date_planned': newdate.strftime('%Y-%m-%d'), - 'product_id': op.product_id.id, - 'product_qty': qty, - 'product_uom': op.product_uom.id, - 'location_id': op.warehouse_id.lot_input_id.id, - 'procure_method': 'make_to_order', - 'origin': op.name - }) - wf_service.trg_validate(uid, 'mrp.procurement', proc_id, - 'button_confirm', cr) - wf_service.trg_validate(uid, 'mrp.procurement', proc_id, - 'button_check', cr) - orderpoint_obj.write(cr, uid, [op.id], - {'procurement_id': proc_id}) - except Exception, e: - report.append('OP %d:\n%s\n' % (op.id, str(e))) - offset += len(ids) - if use_new_cursor: - cr.commit() - if user_id and report: - request_obj.create(cr, uid, { - 'name': 'Orderpoint report.', - 'act_from': user_id, - 'act_to': user_id, - 'body': '\n'.join(report) - }) - if use_new_cursor: - cr.commit() - cr.close() - return {} - - - # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/mrp/wizard/wizard_orderpoint_procurement.py b/addons/mrp/wizard/wizard_orderpoint_procurement.py index ef64c2c070b..956b5ed111c 100644 --- a/addons/mrp/wizard/wizard_orderpoint_procurement.py +++ b/addons/mrp/wizard/wizard_orderpoint_procurement.py @@ -51,8 +51,8 @@ def _procure_calculation_orderpoint(self, db_name, uid, data, context): cr = db.cursor() proc_obj = pool.get('mrp.procurement') automatic = data['form']['automatic'] - proc_obj.run_orderpoint_confirm(cr, uid, automatic=automatic,\ - use_new_cursor=cr.dbname, context=context, user_id=uid) + proc_obj._procure_orderpoint_confirm(cr, uid, automatic=automatic,\ + use_new_cursor=cr.dbname, context=context) return {} def _procure_calculation(self, cr, uid, data, context): diff --git a/addons/mrp/wizard/wizard_procurement.py b/addons/mrp/wizard/wizard_procurement.py index afa5795cdb8..09dde8071aa 100644 --- a/addons/mrp/wizard/wizard_procurement.py +++ b/addons/mrp/wizard/wizard_procurement.py @@ -34,38 +34,17 @@ import pooler parameter_form = '''
- - - - - - - - +