export_file.py :  » ERP » frePPLe » frepple-0.8.0 » contrib » django » freppledb » execute » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » ERP » frePPLe 
frePPLe » frepple 0.8.0 » contrib » django » freppledb » execute » export_file.py
#
# Copyright (C) 2007 by Johan De Taeye
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#

# file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.8.0/contrib/django/freppledb/execute/export_file.py $
# revision : $LastChangedRevision: 1176 $  $LastChangedBy: jdetaeye $
# date : $LastChangedDate: 2010-02-14 22:27:13 +0100 (Sun, 14 Feb 2010) $

r'''
Exports frePPLe information to flat files.

The code in this file is executed NOT by Django, but by the embedded Python
interpreter from thefrePPLeengine. import 

The code iterates over all objects in the C++ core engine, and writes this
information to a set of text files.
'''


from time import time
import csv
from threading import Thread
import inspect


import frepple


def exportProblems():
  print "Exporting problems..."
  starttime = time()
  writer = csv.writer(open("problems.csv", "wb"), quoting=csv.QUOTE_ALL)
  writer.writerow(('#entity','name','description','start date','end date','weight'))
  for i in frepple.problems():
    writer.writerow(
      (i.entity, i.name, i.owner, i.description, i.start, i.end, i.weight)
      )
  print 'Exported problems in %.2f seconds' % (time() - starttime)


def exportOperationplans():
  print "Exporting operationplans..."
  starttime = time()
  writer = csv.writer(open("operations.csv", "wb"), quoting=csv.QUOTE_ALL)
  writer.writerow(('#id','operation','quantity','start date','end date','demand','locked'))
  for i in frepple.operationplans():
    writer.writerow(
     ( i.id, i.operation.name, i.quantity, i.start, i.end,
       i.demand and i.demand.name or '', i.locked, i.unavailable, 
       i.owner and i.owner.id or None)
     )
  print 'Exported operationplans in %.2f seconds' % (time() - starttime)


def exportFlowplans():
  print "Exporting flowplans..."
  starttime = time()
  writer = csv.writer(open("buffers.csv", "wb"), quoting=csv.QUOTE_ALL)
  writer.writerow(('#operationplan id','buffer','quantity','date','on hand'))
  for i in frepple.buffers():
    for j in i.flowplans:
      writer.writerow(
       (j.operationplan.id, j.buffer.name, j.quantity,
        j.date, j.onhand)
       )
  print 'Exported flowplans in %.2f seconds' % (time() - starttime)


def exportLoadplans():
  print "Exporting loadplans..."
  starttime = time()
  writer = csv.writer(open("resources.csv", "wb"), quoting=csv.QUOTE_ALL)
  writer.writerow(('#operationplan id','resource','quantity','start date','end date','setup'))
  for i in frepple.resources():
    for j in i.loadplans:
      writer.writerow(
       (j.operationplan.id, j.resource.name, j.quantity,
        j.startdate, j.enddate, j.setup)
       )
  print 'Exported loadplans in %.2f seconds' % (time() - starttime)


def exportDemand():

  def deliveries(d):
    cumplanned = 0
    n = d
    while n.hidden and n.owner: n = n.owner
    n = n and n.name or 'unspecified'
    # Loop over all delivery operationplans
    for i in d.operationplans:
      cumplanned += i.quantity
      cur = i.quantity
      if cumplanned > d.quantity:
        cur -= cumplanned - d.quantity
        if cur < 0: cur = 0
      yield (n, d.item.name, d.due, cur, i.end, i.quantity, i.id)
    # Extra record if planned short
    if cumplanned < d.quantity:
      yield (n, d.item.name, d.customer and d.customer.name or None, d.due, 
        d.quantity - cumplanned, None, None, None)

  print "Exporting demand plans..."
  starttime = time()
  writer = csv.writer(open("demands.csv", "wb"), quoting=csv.QUOTE_ALL)
  writer.writerow(('#demand','item','customer','due date','requested quantity',
    'plan date','plan quantity','operationplan id'))
  for i in frepple.demands():
    if i.quantity == 0: continue
    for j in deliveries(i):
      writer.writerow(j)
  print 'Exported demand plans in %.2f seconds' % (time() - starttime)


def exportPegging():
  print "Exporting pegging..."
  starttime = time()
  writer = csv.writer(open("demand_pegging.csv", "wb"), quoting=csv.QUOTE_ALL)
  writer.writerow(('#demand','level','consuming operationplan id','consuming date',
    'producing operationplan id','producing date','buffer','item','quantity demand',
    'quantity buffer','pegged'))
  for i in frepple.demands():
    # Find non-hidden demand owner
    n = i
    while n.hidden and n.owner: n = n.owner
    n = n and n.name or 'unspecified'
    # Export pegging
    for j in i.pegging:
      writer.writerow((
       n, j.level, j.consuming and j.consuming.id or '',
       j.consuming_date,
       j.producing and j.producing.id or '', j.producing_date,
       j.buffer and j.buffer.name or '',
       (j.buffer and j.buffer.item and j.buffer.item.name) or '',
       j.quantity_demand, j.quantity_buffer, j.pegged
       ))
  print 'Exported pegging in %.2f seconds' % (time() - starttime)


def exportForecast():
  # Detect whether the forecast module is available
  if not 'demand_forecastbucket' in [ a for a, b in inspect.getmembers(frepple) ]:
    return

  print "Exporting forecast plans..."
  starttime = time()
  writer = csv.writer(open("forecast.csv", "wb"), quoting=csv.QUOTE_ALL)
  writer.writerow(('#forecast','start date','end date','total quantity',
    'net quantity','consumed quantity'))
  for i in frepple.demands():
    if not isinstance(i, frepple.demand_forecastbucket) or i.total <= 0.0:
      continue
    writer.writerow((
      i.name, i.startdate, i.enddate, i.total, i.quantity, i.consumed
      ))
  print 'Exported forecast plans in %.2f seconds' % (time() - starttime)


def exportfrepple():
  exportProblems()
  exportOperationplans()
  exportFlowplans()
  exportLoadplans()
  exportDemand()
  exportPegging()
  exportForecast()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.