Ejemplo n.º 1
0
 def loadOperationPlans(self):
   print('Importing operationplans...')
   cnt = 0
   starttime = time()
   self.cursor.execute('''
     SELECT
       operation_id, id, quantity, startdate, enddate, locked, source
     FROM operationplan
     WHERE owner_id IS NULL and quantity >= 0 %s
     ORDER BY id ASC
     ''' % self.filter_and)
   for i in self.cursor.fetchall():
     cnt += 1
     frepple.operationplan(
       operation=frepple.operation(name=i[0]),
       id=i[1], quantity=i[2], start=i[3], end=i[4],
       locked=i[5], source=i[6]
       ).quantity = i[2]
   self.cursor.execute('''
     SELECT
       operation_id, id, quantity, startdate, enddate, locked, owner_id, source
     FROM operationplan
     WHERE owner_id IS NOT NULL and quantity >= 0 %s
     ORDER BY id ASC
     ''' % self.filter_and)
   for i in self.cursor.fetchall():
     cnt += 1
     frepple.operationplan(
       operation=frepple.operation(name=i[0]),
       id=i[1], start=i[3], end=i[4], quantity=i[2],
       locked=i[5], source=i[7],
       owner=frepple.operationplan(id=i[6])
       ).quantity = i[2]
   print('Loaded %d operationplans in %.2f seconds' % (cnt, time() - starttime))
Ejemplo n.º 2
0
def loadOperationPlans(cursor):
    print('Importing operationplans...')
    cnt = 0
    starttime = time()
    cursor.execute(
        '''SELECT operation_id, id, quantity, startdate, enddate, locked
     FROM operationplan
     where owner_id is null
     order by id asc''')
    for i, j, k, l, m, n in cursor.fetchall():
        cnt += 1
        frepple.operationplan(operation=frepple.operation(name=i),
                              id=j,
                              quantity=k,
                              start=l,
                              end=m).locked = n
    cursor.execute(
        '''SELECT operation_id, id, quantity, startdate, enddate, locked, owner_id
     FROM operationplan
     where owner_id is not null
     order by id asc''')
    for i, j, k, l, m, n, o in cursor.fetchall():
        cnt += 1
        frepple.operationplan(operation=frepple.operation(name=i),
                              id=j,
                              quantity=k,
                              start=l,
                              end=m,
                              owner=frepple.operationplan(id=o)).locked = n
    print('Loaded %d operationplans in %.2f seconds' %
          (cnt, time() - starttime))
Ejemplo n.º 3
0
 def loadOperationPlans(self):
     print('Importing operationplans...')
     cnt = 0
     starttime = time()
     self.cursor.execute('''
   SELECT
     operation_id, id, quantity, startdate, enddate, status, source
   FROM operationplan
   WHERE owner_id IS NULL and quantity >= 0 and status <> 'closed' %s
   ORDER BY id ASC
   ''' % self.filter_and)
     for i in self.cursor.fetchall():
         cnt += 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])
         if opplan.start <= frepple.settings.current:
             # We assume that locked operationplans with a start date in the past
             # have already consumed all materials.
             # TODO Specifying this explicitly may be more appropriate
             opplan.consume_material = False
     self.cursor.execute('''
   SELECT
     operation_id, id, quantity, startdate, enddate, status, owner_id, source
   FROM operationplan
   WHERE owner_id IS NOT NULL and quantity >= 0 and status <> 'closed' %s
   ORDER BY id ASC
   ''' % self.filter_and)
     for i in self.cursor.fetchall():
         cnt += 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 opplan.start <= frepple.settings.current:
             # We assume that locked operationplans with a start date in the past
             # have already consumed all materials.
             # TODO Specifying this explicitly may be more appropriate
             opplan.consume_material = False
     print('Loaded %d operationplans in %.2f seconds' %
           (cnt, time() - starttime))
Ejemplo n.º 4
0
  def run(cls, database=DEFAULT_DB_ALIAS, **kwargs):
    import frepple

    with connections[database].chunked_cursor() as cursor:
      cnt = 0
      starttime = time()
      cursor.execute('''
        select
          quantity, flowdate, operationplan_id, item_id, location_id, status
        from operationplanmaterial
        where status <> 'proposed'
      ''')
      for i in cursor:
        cnt += 1
        try:
          opplan = frepple.operationplan(id=i[2])
          if opplan.status not in ("confirmed", "approved"):
            pass
          for fl in opplan.flowplans:
            if fl.buffer.item and fl.buffer.item.name == i[3] \
              and fl.buffer.location and fl.buffer.location.name == i[4]:
                fl.status = "confirmed"
                if i[1]:
                  fl.date = i[1]
                fl.quantity = i[0]
                break
        except Exception as e:
          logger.error("**** %s ****" % e)
      logger.info('Loaded %d operationplanmaterials in %.2f seconds' % (cnt, time() - starttime))
Ejemplo n.º 5
0
 def loadOperationPlanMaterials(self):
     print('Importing operationplanmaterials...')
     cnt = 0
     starttime = time()
     self.cursor.execute('''
   select quantity, flowdate, operationplan_id, buffer, status
   from operationplanmaterial
   where status <> 'proposed'
 ''')
     for i in self.cursor.fetchall():
         cnt += 1
         try:
             opplan = frepple.operationplan(id=i[2])
             if opplan.status not in ("confirmed", "approved"):
                 pass
             for fl in opplan.flowplans:
                 if fl.buffer.name == i[3]:
                     fl.status = "confirmed"
                     if i[1]:
                         fl.date = i[1]
                     fl.quantity = i[0]
         except Exception as e:
             print("Error:", e)
     print('Loaded %d operationplanmaterials in %.2f seconds' %
           (cnt, time() - starttime))
Ejemplo n.º 6
0
 def loadOperationPlans(self):
   print('Importing operationplans...')
   cnt = 0
   starttime = time()
   self.cursor.execute('''
     SELECT
       operation_id, id, quantity, startdate, enddate, status, source
     FROM operationplan
     WHERE owner_id IS NULL and quantity >= 0 and status <> 'closed' %s
     ORDER BY id ASC
     ''' % self.filter_and)
   for i in self.cursor.fetchall():
     cnt += 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]
       )
     if opplan.start <= frepple.settings.current:
       # We assume that locked operationplans with a start date in the past
       # have already consumed all materials.
       # TODO Specifying this explicitly may be more appropriate
       opplan.consume_material = False
   self.cursor.execute('''
     SELECT
       operation_id, id, quantity, startdate, enddate, status, owner_id, source
     FROM operationplan
     WHERE owner_id IS NOT NULL and quantity >= 0 and status <> 'closed' %s
     ORDER BY id ASC
     ''' % self.filter_and)
   for i in self.cursor.fetchall():
     cnt += 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 opplan.start <= frepple.settings.current:
       # We assume that locked operationplans with a start date in the past
       # have already consumed all materials.
       # TODO Specifying this explicitly may be more appropriate
       opplan.consume_material=False
   print('Loaded %d operationplans in %.2f seconds' % (cnt, time() - starttime))
Ejemplo n.º 7
0
 def loadOperationPlans(self):
   print('Importing operationplans...')
   cnt = 0
   starttime = time()
   self.cursor.execute('''
     SELECT
       operation_id, id, quantity, startdate, enddate, status, source
     FROM operationplan
     WHERE owner_id IS NULL and quantity >= 0 and status <> 'closed' %s
     ORDER BY id ASC
     ''' % self.filter_and)
   for i in self.cursor.fetchall():
     cnt += 1
     opplan = frepple.operationplan(
       operation=frepple.operation(name=i[0]),
       id=i[1], quantity=i[2], source=i[6]
       )
     if i[3]:
       opplan.start = i[3]
     if i[4]:
       opplan.end = i[4]
     opplan.status = i[5]
   self.cursor.execute('''
     SELECT
       operation_id, id, quantity, startdate, enddate, status, owner_id, source
     FROM operationplan
     WHERE owner_id IS NOT NULL and quantity >= 0 and status <> 'closed' %s
     ORDER BY id ASC
     ''' % self.filter_and)
   for i in self.cursor.fetchall():
     cnt += 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])
       )
     if i[3]:
       opplan.start = i[3]
     if i[4]:
       opplan.end = i[4]
     opplan.status = i[5]
   print('Loaded %d operationplans in %.2f seconds' % (cnt, time() - starttime))
Ejemplo n.º 8
0
def loadOperationPlans(cursor):
  print('Importing operationplans...')
  cnt = 0
  starttime = time()
  cursor.execute('''SELECT operation_id, id, quantity, startdate, enddate, locked
     FROM operationplan
     where owner_id is null
     order by id asc''')
  for i, j, k, l, m, n in cursor.fetchall():
    cnt += 1
    frepple.operationplan(operation=frepple.operation(name=i),
      id=j, quantity=k, start=l, end=m).locked = n
  cursor.execute('''SELECT operation_id, id, quantity, startdate, enddate, locked, owner_id
     FROM operationplan
     where owner_id is not null
     order by id asc''')
  for i, j, k, l, m, n, o in cursor.fetchall():
    cnt += 1
    frepple.operationplan(operation=frepple.operation(name=i),
      id=j, quantity=k, start=l, end=m, owner=frepple.operationplan(id=o)).locked = n
  print('Loaded %d operationplans in %.2f seconds' % (cnt, time() - starttime))
Ejemplo n.º 9
0
 def loadOperationPlanResources(self):
   print('Importing operationplanresources...')
   cnt = 0
   starttime = time()
   self.cursor.execute('''
     select resource, quantity, operationplan_id
     from operationplanresource
     where status <> 'proposed'
   ''')
   for i in self.cursor.fetchall():
     cnt += 1
     try:
       opplan = frepple.operationplan(id=i[2])
       if opplan.status not in ("confirmed","approved"):
         pass
       for lo in opplan.loadplans:
         if lo.resource.name == i[0]:
           lo.status = "confirmed"
           lo.quantity = i[1]
     except Exception as e:
       print("Error:", e)
   print('Loaded %d operationplanresources in %.2f seconds' % (cnt, time() - starttime))
Ejemplo n.º 10
0
  def run(cls, database=DEFAULT_DB_ALIAS, **kwargs):
    import frepple

    with connections[database].chunked_cursor() as cursor:
      cnt = 0
      starttime = time()
      cursor.execute('''
        select resource_id, quantity, operationplan_id
        from operationplanresource
        where status <> 'proposed'
      ''')
      for i in cursor:
        cnt += 1
        try:
          opplan = frepple.operationplan(id=i[2])
          if opplan.status not in ("confirmed", "approved"):
            pass
          for lo in opplan.loadplans:
            if lo.resource.name == i[0]:
              lo.status = "confirmed"
              lo.quantity = i[1]
        except Exception as e:
          logger.error("**** %s ****" % e)
      logger.info('Loaded %d operationplanresources in %.2f seconds' % (cnt, time() - starttime))
Ejemplo n.º 11
0
  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]
Ejemplo n.º 12
0
      <operation name="delivery end item" />
      <buffer name="end item" />
      <quantity>-1</quantity>
    </flow>
    <flow xsi:type="flow_end">
      <operation name="make or buy item" />
      <buffer name="end item" />
      <quantity>1</quantity>
    </flow>
  </flows>
</plan>
''')

###
print("\nCreating operationplans")
opplan = frepple.operationplan(operation="make item", quantity=9, end=datetime.datetime(2011,1,1))
opplan.locked = True

###
print("\nCreating items")
item = frepple.item(name="end item", operation=shipoper)
itemlist = [ frepple.item(name="item %d" % i) for i in range(10) ]

###
print("\nTesting the comparison operator")
print("makeoper < shipoper", makeoper < shipoper)
print("shipoper < makeoper", shipoper < makeoper)
print("shipoper != makeoper", shipoper != makeoper)
print("shipoper == makeoper", shipoper == makeoper)
print("shipoper == shipoper", shipoper == shipoper)
try:
Ejemplo n.º 13
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]
Ejemplo n.º 14
0
      <buffer name="end item" />
      <quantity>-1</quantity>
    </flow>
    <flow xsi:type="flow_end">
      <operation name="make or buy item" />
      <buffer name="end item" />
      <quantity>1</quantity>
    </flow>
  </flows>
</plan>
''')

###
print("\nCreating operationplans")
opplan = frepple.operationplan(operation="make item",
                               quantity=9,
                               end=datetime.datetime(2011, 1, 1))
opplan.locked = True

###
print("\nCreating items")
item = frepple.item(name="end item", operation=shipoper)
itemlist = [frepple.item(name="item %d" % i) for i in range(10)]

###
print("\nTesting the comparison operator")
print("makeoper < shipoper", makeoper < shipoper)
print("shipoper < makeoper", shipoper < makeoper)
print("shipoper != makeoper", shipoper != makeoper)
print("shipoper == makeoper", shipoper == makeoper)
print("shipoper == shipoper", shipoper == shipoper)
Ejemplo n.º 15
0
  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]