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, description, owner_id, category, subcategory, source FROM supplier %s ''' % filter_where) for i in cursor: cnt += 1 try: x = frepple.supplier(name=i[0], description=i[1], category=i[3], subcategory=i[4], source=i[5]) if i[2]: x.owner = frepple.supplier(name=i[2]) except Exception as e: logger.error("**** %s ****" % e) logger.info('Loaded %d suppliers in %.2f seconds' % (cnt, time() - starttime))
def loadPurchaseOrders(self): print('Importing purchase orders...') cnt = 0 starttime = time() self.cursor.execute(''' SELECT location_id, id, reference, item_id, supplier_id, quantity, startdate, enddate, status, source FROM purchase_order WHERE status <> 'closed' %s ORDER BY id ASC ''' % self.filter_and) for i in self.cursor.fetchall(): cnt += 1 try: frepple.operation_itemsupplier.createOrder( location=frepple.location(name=i[0]), id=i[1], reference=i[2], item=frepple.item(name=i[3]) if i[3] else None, supplier=frepple.supplier(name=i[4]) if i[4] else None, quantity=i[5], start=i[6], end=i[7], status=i[8], source=i[9] ) except Exception as e: print("Error:", e) print('Loaded %d purchase orders in %.2f seconds' % (cnt, time() - starttime))
def loadPurchaseOrders(self): print('Importing purchase orders...') cnt = 0 starttime = time() self.cursor.execute(''' SELECT location_id, id, reference, item_id, supplier_id, quantity, startdate, enddate, status, source FROM purchase_order WHERE status <> 'closed' %s ORDER BY id ASC ''' % self.filter_and) for i in self.cursor.fetchall(): cnt += 1 try: frepple.operation_itemsupplier.createOrder( location=frepple.location(name=i[0]), id=i[1], reference=i[2], item=frepple.item(name=i[3]) if i[3] else None, supplier=frepple.supplier(name=i[4]) if i[4] else None, quantity=i[5], start=i[6], end=i[7], status=i[8], source=i[9]) except Exception as e: print("Error:", e) print('Loaded %d purchase orders in %.2f seconds' % (cnt, time() - starttime))
def loadSuppliers(self): print('Importing suppliers...') cnt = 0 starttime = time() self.cursor.execute(''' SELECT name, description, owner_id, category, subcategory, source FROM supplier %s ''' % self.filter_where) for i in self.cursor.fetchall(): cnt += 1 try: x = frepple.supplier(name=i[0], description=i[1], category=i[3], subcategory=i[4], source=i[5]) if i[2]: x.owner = frepple.supplier(name=i[2]) except Exception as e: print("Error:", e) print('Loaded %d suppliers 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 supplier_id, item_id, location_id, sizeminimum, sizemultiple, cost, priority, effective_start, effective_end, source, leadtime, resource_id, resource_qty, fence FROM itemsupplier %s ORDER BY supplier_id, item_id, location_id, priority desc ''' % filter_where) cursuppliername = None curitemname = None for i in cursor: cnt += 1 try: if i[0] != cursuppliername: cursuppliername = i[0] cursupplier = frepple.supplier(name=cursuppliername) if i[1] != curitemname: curitemname = i[1] curitem = frepple.item(name=curitemname) curitemsupplier = frepple.itemsupplier( supplier=cursupplier, item=curitem, source=i[9], leadtime=i[10].total_seconds() if i[10] else 0, fence=i[13].total_seconds() if i[13] else 0, resource_qty=i[12] ) if i[2]: curitemsupplier.location = frepple.location(name=i[2]) if i[3]: curitemsupplier.size_minimum = i[3] if i[4]: curitemsupplier.size_multiple = i[4] if i[5]: curitemsupplier.cost = i[5] if i[6]: curitemsupplier.priority = i[6] if i[7]: curitemsupplier.effective_start = i[7] if i[8]: curitemsupplier.effective_end = i[8] if i[11]: curitemsupplier.resource = frepple.resource(name=i[11]) except Exception as e: logger.error("**** %s ****" % e) logger.info('Loaded %d item suppliers in %.2f seconds' % (cnt, time() - starttime))
def loadItemSuppliers(self): print('Importing item suppliers...') cnt = 0 starttime = time() self.cursor.execute(''' SELECT supplier_id, item_id, location_id, sizeminimum, sizemultiple, cost, priority, effective_start, effective_end, source, leadtime, resource_id, resource_qty, fence FROM itemsupplier %s ORDER BY supplier_id, item_id, location_id, priority desc ''' % self.filter_where) cursuppliername = None curitemname = None for i in self.cursor.fetchall(): cnt += 1 try: if i[0] != cursuppliername: cursuppliername = i[0] cursupplier = frepple.supplier(name=cursuppliername) if i[1] != curitemname: curitemname = i[1] curitem = frepple.item(name=curitemname) curitemsupplier = frepple.itemsupplier( supplier=cursupplier, item=curitem, source=i[9], leadtime=i[10].total_seconds() if i[10] else 0, fence=i[13].total_seconds() if i[13] else 0, resource_qty=i[12]) if i[2]: curitemsupplier.location = frepple.location(name=i[2]) if i[3]: curitemsupplier.size_minimum = i[3] if i[4]: curitemsupplier.size_multiple = i[4] if i[5]: curitemsupplier.cost = i[5] if i[6]: curitemsupplier.priority = i[6] if i[7]: curitemsupplier.effective_start = i[7] if i[8]: curitemsupplier.effective_end = i[8] if i[11]: curitemsupplier.resource = frepple.resource(name=i[11]) except Exception as e: print("Error:", e) print('Loaded %d item suppliers in %.2f seconds' % (cnt, time() - starttime))
def loadItemSuppliers(self): print('Importing item suppliers...') cnt = 0 starttime = time() self.cursor.execute(''' SELECT supplier_id, item_id, location_id, sizeminimum, sizemultiple, cost, priority, effective_start, effective_end, source, leadtime, resource_id, resource_qty, fence FROM itemsupplier %s ORDER BY supplier_id, item_id, location_id, priority desc ''' % self.filter_where) cursuppliername = None curitemname = None for i in self.cursor.fetchall(): cnt += 1 try: if i[0] != cursuppliername: cursuppliername = i[0] cursupplier = frepple.supplier(name=cursuppliername) if i[1] != curitemname: curitemname = i[1] curitem = frepple.item(name=curitemname) curitemsupplier = frepple.itemsupplier( supplier=cursupplier, item=curitem, source=i[9], leadtime=i[10].total_seconds() if i[10] else 0, fence=i[13].total_seconds() if i[13] else 0, resource_qty=i[12] ) if i[2]: curitemsupplier.location = frepple.location(name=i[2]) if i[3]: curitemsupplier.size_minimum = i[3] if i[4]: curitemsupplier.size_multiple = i[4] if i[5]: curitemsupplier.cost = i[5] if i[6]: curitemsupplier.priority = i[6] if i[7]: curitemsupplier.effective_start = i[7] if i[8]: curitemsupplier.effective_end = i[8] if i[11]: curitemsupplier.resource = frepple.resource(name=i[11]) except Exception as e: print("Error:", e) print('Loaded %d item suppliers 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]
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]