def loadDemand(self): print('Importing demands...') cnt = 0 starttime = time() self.cursor.execute(''' SELECT name, due, quantity, priority, item_id, operation_id, customer_id, owner_id, minshipment, maxlateness, category, subcategory, source, location_id, status FROM demand WHERE (status IS NULL OR status ='open' OR status = 'quote') %s ''' % self.filter_and) for i in self.cursor.fetchall(): cnt += 1 try: x = frepple.demand( name=i[0], due=i[1], quantity=i[2], priority=i[3], status=i[14], item=frepple.item(name=i[4]), category=i[10], subcategory=i[11], source=i[12] ) if i[5]: x.operation = frepple.operation(name=i[5]) if i[6]: x.customer = frepple.customer(name=i[6]) if i[7]: x.owner = frepple.demand(name=i[7]) if i[8]: x.minshipment = i[8] if i[9] is not None: x.maxlateness = i[9].total_seconds() if i[13]: x.location = frepple.location(name=i[13]) except Exception as e: print("Error:", e) print('Loaded %d demands in %.2f seconds' % (cnt, time() - starttime))
def loadDemand(self): print('Importing demands...') cnt = 0 starttime = time() self.cursor.execute(''' SELECT name, due, quantity, priority, item_id, operation_id, customer_id, owner_id, minshipment, maxlateness, category, subcategory, source, location_id, status FROM demand WHERE (status IS NULL OR status ='open' OR status = 'quote') %s ''' % self.filter_and) for i in self.cursor.fetchall(): cnt += 1 try: x = frepple.demand( name=i[0], due=i[1], quantity=i[2], priority=i[3], status=i[14], item=frepple.item(name=i[4]), category=i[10], subcategory=i[11], source=i[12] ) if i[5]: x.operation = frepple.operation(name=i[5]) if i[6]: x.customer = frepple.customer(name=i[6]) if i[7]: x.owner = frepple.demand(name=i[7]) if i[8]: x.minshipment = i[8] if i[9] is not None: x.maxlateness = i[9] if i[13]: x.location = frepple.location(name=i[13]) except Exception as e: print("Error:", e) print('Loaded %d demands in %.2f seconds' % (cnt, time() - starttime))
def loadDemand(cursor): print('Importing demands...') cnt = 0 starttime = time() cursor.execute('''SELECT name, due, quantity, priority, item_id, operation_id, customer_id, owner_id, minshipment, maxlateness, category, subcategory FROM demand where status is null or status ='open' or status = 'quote' ''') for i, j, k, l, m, n, o, p, q, r, s, t in cursor.fetchall(): cnt += 1 try: x = frepple.demand(name=i, due=j, quantity=k, priority=l, item=frepple.item(name=m), category=s, subcategory=t) if n: x.operation = frepple.operation(name=n) if o: x.customer = frepple.customer(name=o) if p: x.owner = frepple.demand(name=p) if q: x.minshipment = q if r != None: x.maxlateness = r except Exception as e: print("Error:", e) print('Loaded %d demands in %.2f seconds' % (cnt, time() - starttime))
def run(cls, database=DEFAULT_DB_ALIAS, **kwargs): import frepple if cls.filter: filter_and = "and %s " % cls.filter filter_where = "where %s " % cls.filter else: filter_and = "" filter_where = "" with connections[database].chunked_cursor() as cursor: cnt = 0 starttime = time() cursor.execute(''' SELECT name, due, quantity, priority, item_id, operation_id, customer_id, owner_id, minshipment, maxlateness, category, subcategory, source, location_id, status FROM demand WHERE (status IS NULL OR status ='open' OR status = 'quote') %s ''' % filter_and) for i in cursor: cnt += 1 try: x = frepple.demand( name=i[0], due=i[1], quantity=i[2], priority=i[3], status=i[14], item=frepple.item(name=i[4]), category=i[10], subcategory=i[11], source=i[12] ) if i[5]: x.operation = frepple.operation(name=i[5]) if i[6]: x.customer = frepple.customer(name=i[6]) if i[7]: x.owner = frepple.demand(name=i[7]) if i[8] is not None: x.minshipment = i[8] if i[9] is not None: x.maxlateness = i[9].total_seconds() if i[13]: x.location = frepple.location(name=i[13]) except Exception as e: logger.error("**** %s ****" % e) logger.info('Loaded %d demands in %.2f seconds' % (cnt, time() - starttime))
def loadDemand(cursor): print('Importing demands...') cnt = 0 starttime = time() cursor.execute('''SELECT name, due, quantity, priority, item_id, operation_id, customer_id, owner_id, minshipment, maxlateness, category, subcategory FROM demand where status is null or status ='open' ''') for i,j,k,l,m,n,o,p,q,r,s,t in cursor.fetchall(): cnt += 1 try: x = frepple.demand( name=i, due=j, quantity=k, priority=l, item=frepple.item(name=m),category=s,subcategory=t) if n: x.operation = frepple.operation(name=n) if o: x.customer = frepple.customer(name=o) if p: x.owner = frepple.demand(name=p) if q: x.minshipment = q if r != None: x.maxlateness = r except Exception as e: print("Error:", e) print('Loaded %d demands in %.2f seconds' % (cnt, time() - starttime))
def loadOperationPlans(self): # TODO if we are going to replan anyway, we can skip loading the proposed operationplans print('Importing operationplans...') if 'supply' in os.environ: confirmed_filter = " and operationplan.status = 'confirmed'" else: confirmed_filter = "" cnt_mo = 0 cnt_po = 0 cnt_do = 0 cnt_dlvr = 0 starttime = time() self.cursor.execute(''' SELECT operationplan.operation_id, operationplan.id, operationplan.quantity, operationplan.startdate, operationplan.enddate, operationplan.status, operationplan.source, operationplan.type, operationplan.origin_id, operationplan.destination_id, operationplan.supplier_id, operationplan.item_id, operationplan.location_id, reference, coalesce(dmd.name, null) FROM operationplan LEFT OUTER JOIN (select name from demand where demand.status = 'open' ) dmd on dmd.name = operationplan.demand_id WHERE operationplan.owner_id IS NULL and operationplan.quantity >= 0 and operationplan.status <> 'closed' %s%s and operationplan.type in ('PO', 'MO', 'DO', 'DLVR') ORDER BY operationplan.id ASC ''' % (self.filter_and, confirmed_filter)) for i in self.cursor.fetchall(): try: if i[7] == 'MO': cnt_mo += 1 opplan = frepple.operationplan( operation=frepple.operation(name=i[0]), id=i[1], quantity=i[2], source=i[6], start=i[3], end=i[4], status=i[5], reference=i[13] ) elif i[7] == 'PO': cnt_po += 1 opplan = frepple.operationplan( location=frepple.location(name=i[12]), ordertype=i[7], id=i[1], reference=i[12], item=frepple.item(name=i[11]) if i[11] else None, supplier=frepple.supplier(name=i[10]) if i[10] else None, quantity=i[2], start=i[3], end=i[4], status=i[5], source=i[6] ) elif i[7] == 'DO': cnt_do += 1 opplan = frepple.operationplan( location=frepple.location(name=i[9]) if i[9] else None, id=i[1], reference=i[12], ordertype=i[7], item=frepple.item(name=i[11]) if i[11] else None, origin=frepple.location(name=i[8]) if i[8] else None, quantity=i[2], start=i[3], end=i[4], status=i[5], source=i[6] ) elif i[7] == 'DLVR': cnt_dlvr += 1 opplan = frepple.operationplan( location=frepple.location(name=i[12]) if i[12] else None, id=i[1], reference=i[12], ordertype=i[7], item=frepple.item(name=i[11]) if i[11] else None, origin=frepple.location(name=i[8]) if i[8] else None, demand=frepple.demand(name=i[14]) if i[14] else None, quantity=i[2], start=i[3], end=i[4], status=i[5], source=i[6] ) opplan = None else: print("Warning: unhandled operationplan type '%s'" % i[7]) continue if i[14] and opplan: opplan.demand = frepple.demand(name=i[14]) except Exception as e: print("Error:", e) self.cursor.execute(''' SELECT operationplan.operation_id, operationplan.id, operationplan.quantity, operationplan.startdate, operationplan.enddate, operationplan.status, operationplan.owner_id, operationplan.source, coalesce(dmd.name, null) FROM operationplan INNER JOIN (select id from operationplan ) opplan_parent on operationplan.owner_id = opplan_parent.id LEFT OUTER JOIN (select name from demand where demand.status = 'open' ) dmd on dmd.name = operationplan.demand_id WHERE operationplan.quantity >= 0 and operationplan.status <> 'closed' %s%s and operationplan.type = 'MO' ORDER BY operationplan.id ASC ''' % (self.filter_and, confirmed_filter)) for i in self.cursor.fetchall(): cnt_mo += 1 opplan = frepple.operationplan( operation=frepple.operation(name=i[0]), id=i[1], quantity=i[2], source=i[7], owner=frepple.operationplan(id=i[6]), start=i[3], end=i[4], status=i[5] ) if i[8]: opplan.demand = frepple.demand(name=i[8]) print('Loaded %d manufacturing orders, %d purchase orders, %d distribution orders and %s deliveries in %.2f seconds' % (cnt_mo, cnt_po, cnt_do, cnt_dlvr, time() - starttime)) # Assure the operationplan ids will be unique. # We call this method only at the end, as calling it earlier gives a slower # performance to load operationplans self.cursor.execute(''' select coalesce(max(id),1) + 1 as max_id from operationplan ''') d = self.cursor.fetchone() frepple.settings.id = d[0]
except Exception as e: print("Catching exception %s: %s" % (e.__class__.__name__, e)) try: buf1.crazyfield = "doesn't exist" except Exception as e: print("Catching exception %s: %s" % (e.__class__.__name__, e)) try: buf1.owner = buf2 except Exception as e: print("Catching exception %s: %s" % (e.__class__.__name__, e)) ### print("\nCreating demands") order1 = frepple.demand(name="order 1", item=item, quantity=10, priority=1, \ due=datetime.datetime(2009,3,2,9), customer=mycustomer, maxlateness=0) order2 = frepple.demand(name="order 2", item=item, quantity=10, priority=2, \ due=datetime.datetime(2009,3,2,8,30,0), customer=mycustomer, maxlateness=0) order3 = frepple.demand(name="order 3", item=item, quantity=10, priority=3, \ due=datetime.datetime(2009,3,2,20,0,0), customer=mycustomer, maxlateness=0) ### print("\nCreating a solver and running it") frepple.solver_mrp(name="MRP", constraints=7, loglevel=0).solve() ### print("\nEchoing the model to a file") printModel("output.1.xml") ### print("\nSaving the model to an XML-file")
def run(cls, database=DEFAULT_DB_ALIAS, **kwargs): import frepple if cls.filter: filter_and = "and %s " % cls.filter filter_where = "where %s " % cls.filter else: filter_and = "" filter_where = "" with connections[database].chunked_cursor() as cursor: if 'supply' in os.environ: confirmed_filter = " and operationplan.status in ('confirmed', 'approved')" create_flag = True else: confirmed_filter = "" create_flag = False cnt_mo = 0 cnt_po = 0 cnt_do = 0 cnt_dlvr = 0 starttime = time() cursor.execute(''' SELECT operationplan.operation_id, operationplan.id, operationplan.quantity, operationplan.startdate, operationplan.enddate, operationplan.status, operationplan.source, operationplan.type, operationplan.origin_id, operationplan.destination_id, operationplan.supplier_id, operationplan.item_id, operationplan.location_id, reference, coalesce(dmd.name, null) FROM operationplan LEFT OUTER JOIN (select name from demand where demand.status = 'open' ) dmd on dmd.name = operationplan.demand_id WHERE operationplan.owner_id IS NULL and operationplan.quantity >= 0 and operationplan.status <> 'closed' %s%s and operationplan.type in ('PO', 'MO', 'DO', 'DLVR') ORDER BY operationplan.id ASC ''' % (filter_and, confirmed_filter)) for i in cursor: try: if i[7] == 'MO': cnt_mo += 1 opplan = frepple.operationplan( operation=frepple.operation(name=i[0]), id=i[1], quantity=i[2], source=i[6], start=i[3], end=i[4], status=i[5], reference=i[13], create=create_flag ) elif i[7] == 'PO': cnt_po += 1 opplan = frepple.operationplan( location=frepple.location(name=i[12]), ordertype=i[7], id=i[1], reference=i[13], item=frepple.item(name=i[11]) if i[11] else None, supplier=frepple.supplier(name=i[10]) if i[10] else None, quantity=i[2], start=i[3], end=i[4], status=i[5], source=i[6], create=create_flag ) elif i[7] == 'DO': cnt_do += 1 opplan = frepple.operationplan( location=frepple.location(name=i[9]) if i[9] else None, id=i[1], reference=i[13], ordertype=i[7], item=frepple.item(name=i[11]) if i[11] else None, origin=frepple.location(name=i[8]) if i[8] else None, quantity=i[2], start=i[3], end=i[4], status=i[5], source=i[6], create=create_flag ) elif i[7] == 'DLVR': cnt_dlvr += 1 opplan = frepple.operationplan( location=frepple.location(name=i[12]) if i[12] else None, id=i[1], reference=i[13], ordertype=i[7], item=frepple.item(name=i[11]) if i[11] else None, origin=frepple.location(name=i[8]) if i[8] else None, demand=frepple.demand(name=i[14]) if i[14] else None, quantity=i[2], start=i[3], end=i[4], status=i[5], source=i[6], create=create_flag ) opplan = None else: logger.warning("Warning: unhandled operationplan type '%s'" % i[7]) continue if i[14] and opplan: opplan.demand = frepple.demand(name=i[14]) except Exception as e: logger.error("**** %s ****" % e) with connections[database].chunked_cursor() as cursor: cursor.execute(''' SELECT operationplan.operation_id, operationplan.id, operationplan.quantity, operationplan.startdate, operationplan.enddate, operationplan.status, operationplan.owner_id, operationplan.source, operationplan.reference, coalesce(dmd.name, null) FROM operationplan INNER JOIN (select id from operationplan ) opplan_parent on operationplan.owner_id = opplan_parent.id LEFT OUTER JOIN (select name from demand where demand.status = 'open' ) dmd on dmd.name = operationplan.demand_id WHERE operationplan.quantity >= 0 and operationplan.status <> 'closed' %s%s and operationplan.type = 'MO' ORDER BY operationplan.id ASC ''' % (filter_and, confirmed_filter)) for i in cursor: cnt_mo += 1 opplan = frepple.operationplan( operation=frepple.operation(name=i[0]), id=i[1], quantity=i[2], source=i[7], start=i[3], end=i[4], status=i[5], reference=i[8] ) if i[6] and opplan: try: opplan.owner = frepple.operationplan(id=i[6]) except: pass if i[9] and opplan: opplan.demand = frepple.demand(name=i[9]) logger.info('Loaded %d manufacturing orders, %d purchase orders, %d distribution orders and %s deliveries in %.2f seconds' % (cnt_mo, cnt_po, cnt_do, cnt_dlvr, time() - starttime)) with connections[database].cursor() as cursor: # Assure the operationplan ids will be unique. # We call this method only at the end, as calling it earlier gives a slower # performance to load operationplans cursor.execute(''' select coalesce(max(id),1) + 1 as max_id from operationplan ''') d = cursor.fetchone() frepple.settings.id = d[0]
except Exception as e: print("Catching exception %s: %s" % (e.__class__.__name__, e)) try: buf1.myfield = "my custom field" except Exception as e: print("Catching exception %s: %s" % (e.__class__.__name__, e)) try: buf1.owner = buf2 except Exception as e: print("Catching exception %s: %s" % (e.__class__.__name__, e)) ### print("\nCreating demands") order1 = frepple.demand(name="order 1", item=item, quantity=10, priority=1, \ due=datetime.datetime(2009,3,2,9), customer=mycustomer, maxlateness=0) order2 = frepple.demand(name="order 2", item=item, quantity=10, priority=2, \ due=datetime.datetime(2009,3,2,8,30,0), customer=mycustomer, maxlateness=0) order3 = frepple.demand(name="order 3", item=item, quantity=10, priority=3, \ due=datetime.datetime(2009,3,2,20,0,0), customer=mycustomer, maxlateness=0) ### print("\nCreating a solver and running it") frepple.solver_mrp(name="MRP", constraints=7, loglevel=0).solve() ### print("\nEchoing the model to a file") printModel("output.1.xml") ### print("\nSaving the model to an XML-file")