Commit 7a974453 authored by panos's avatar panos Committed by Georgios Dagkakis

Insert the required scripts for the implementation of the DB approach in CapacityStations model

parent 1eabf447
# ===========================================================================
# Copyright 2013 University of Limerick
#
# This file is part of DREAM.
#
# DREAM 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 3 of the License, or
# (at your option) any later version.
#
# DREAM 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 DREAM. If not, see <http://www.gnu.org/licenses/>.
# ===========================================================================
'''
Created on 20 March 2015
@author: Panos
'''
import Tkinter as tk
from Tkinter import *
import pyodbc
import tkMessageBox
from datetime import datetime
class Demo1(Frame):
def __init__(self):
tk.Frame.__init__(self)
self.pack()
self.master.title("CapacityStations Interface")
if not self.checkInsertedProject():
self.labelText = StringVar()
self.labelText.set('There is no new order inserted in the system, please insert one and try again')
label5 = Label(self, textvariable=self.labelText, height=5)
label5.pack()
else:
self.labelText = StringVar()
self.labelText.set('Please follow the instructions to update the system.')
label1 = Label(self, textvariable=self.labelText, height=2)
label1.pack()
self.labelText = StringVar()
self.labelText.set('1. Please select order')
label2 = Label(self, textvariable=self.labelText, height=2)
label2.pack()
self.OrderOption = StringVar()
self.OrderOption.set(None)
options = self.checkInsertedProject()
self.ProjectDropDown = OptionMenu(self, self.OrderOption, *options).pack()
self.labelText = StringVar()
self.labelText.set('2. Please select the operation')
label4 = Label(self, textvariable=self.labelText, height=2)
label4.pack()
self.operationOption = StringVar()
self.operationOption.set(None)
options = ['SMF', 'WELD', 'CNC', 'MCH', 'EEP', 'PPASB', 'PAINT', 'ASBTST']
self.operationDropDown = OptionMenu(self, self.operationOption, *options).pack()
self.labelText = StringVar()
self.labelText.set('3. Please click below only once, when you insert a WP_id. \n If you have already clicked, please go to next step no 4')
label6 = Label(self, textvariable=self.labelText, height=2)
label6.pack()
self.checkBoxVal1 = IntVar()
checkBox1 = Checkbutton(self, variable=self.checkBoxVal1, text='Click to record the START DATE', height=3, command=self.recordStartDate)
checkBox1.pack()
self.labelText = StringVar()
self.labelText.set('4. Please insert estimated capacity left, to be completed')
label6 = Label(self, textvariable=self.labelText, height=2)
label6.pack()
self.capleft = StringVar(None)
self.capacity = Entry(self, textvariable=self.capleft)
self.capacity.pack()
self.labelText = StringVar()
self.labelText.set('5. Please insert any comments')
label2 = Label(self, textvariable=self.labelText, height=2)
label2.pack()
self.remark = StringVar(None)
self.comments = Entry(self, textvariable=self.remark)
self.comments.pack()
self.labelText = StringVar()
self.labelText.set('6. Please click below only when the operation finished')
label6 = Label(self, textvariable=self.labelText, height=2)
label6.pack()
self.checkBoxVal2 = IntVar()
checkBox = Checkbutton(self, variable=self.checkBoxVal2, text='Click to record the END DATE', height=3, command=self.recordEndDate)
checkBox.pack()
self.labelText = StringVar()
self.labelText.set('7. Please update the system clicking the button below')
label6 = Label(self, textvariable=self.labelText, height=2)
label6.pack()
self.button4 = Button(self, text='Update the system', width=20, command=self.updateDatabase)
self.button4.pack()
def recordStartDate(self):
self.startdate = str(datetime.now())
print self.startdate
return
def recordEndDate(self):
self.endDate = str(datetime.now())
print self.endDate
return
def checkInsertedProject(self):
cnxn =pyodbc.connect("Driver={MySQL ODBC 3.51 Driver};SERVER=localhost; PORT=3306;DATABASE=bal_database;UID=root; PASSWORD=Pitheos10;")
cursor1 = cnxn.cursor()
cursor2 = cnxn.cursor()
a=cursor1.execute("""
select Order_id, ProjectName, Status
from orders
""")
availableProject=[]
for j in range(a.rowcount):
#get the next line
ind1=a.fetchone()
#and create a dictionary order
if ind1.Status == 'in progress' or ind1.Status =='accepted':
availableProject.append(ind1.Order_id)
return availableProject
def alreadyInsertedWP(self):
cnxn =pyodbc.connect("Driver={MySQL ODBC 3.51 Driver};SERVER=localhost; PORT=3306;DATABASE=bal_database;UID=root; PASSWORD=Pitheos10; ")
cursor = cnxn.cursor()
c=cursor.execute("""
select WP_id, END_DATE
from production_status
""")
listb=[]
for i in range(c.rowcount):
ind=c.fetchone()
if ind.WP_id:
listb.append(ind.WP_id)
else:
continue
return
def updateDatabase(self):
cnxn =pyodbc.connect("Driver={MySQL ODBC 3.51 Driver};SERVER=localhost; PORT=3306;DATABASE=bal_database;UID=root; PASSWORD=Pitheos10; ")
cursor = cnxn.cursor()
cursor1 = cnxn.cursor()
if self.checkBoxVal2.get():
update_order= ("INSERT INTO production_status(`status_id`, `WP_id`, `Operation_Name`, `START_DATE`, `Capacity_left`, `Remarks`,`END_DATE`) VALUES ( ?, ?, ?, ?, ?, ?, ?)")
cursor.execute("SELECT @@IDENTITY AS ID")
order = self.OrderOption.get()
a = cursor1.execute("""
select WP_id, Order_id
from sequence where Order_id=?
""", order)
for j in range(a.rowcount):
ind2=a.fetchone()
lastWP =ind2.WP_id
print lastWP
b = cursor1.execute("""
select sequence.WP_id, sequence.Order_id
from sequence where Operation_Name=?
""", self.operationOption.get())
ind4=b.fetchone()
status2 = 'finished'
row = cursor.fetchone()
WP=ind4[0]
order_ref = row.ID
status1 = 'in progress'
cursor.execute(update_order, (order_ref, WP, self.operationOption.get(), str(datetime.now()), self.capacity.get(), self.comments.get(),str(datetime.now()) ))
if WP == lastWP:
cursor.execute("UPDATE orders SET `Status`=? WHERE Order_id=? ", status2, order)
cursor.commit()
self.close_window()
else:
update_order= ("INSERT INTO production_status(`status_id`, `WP_id`, `Operation_Name`, `START_DATE`, `Capacity_left`, `Remarks`) VALUES ( ?, ?, ?, ?, ?, ?)")
cursor.execute("SELECT @@IDENTITY AS ID")
order = self.OrderOption.get()
a = cursor1.execute("""
select sequence.WP_id, sequence.Order_id, sequence.Operation_Name
from sequence where Order_id=? and Operation_Name=?
""", order, self.operationOption.get())
ind3=a.fetchone()
WP=ind3.WP_id
row = cursor.fetchone()
order_ref = row.ID
status1 = 'in progress'
cursor.execute(update_order, (order_ref, WP, self.operationOption.get(), str(datetime.now()), self.capacity.get(), self.comments.get()))
cursor.execute("UPDATE orders SET `Status`=? WHERE Order_id=? ", status1, order)
cursor.commit()
self.close_window()
return
def close_window(self):
self.destroy()
def main():
Demo1().mainloop()
if __name__ == '__main__':
main()
\ No newline at end of file
# ===========================================================================
# Copyright 2013 University of Limerick
#
# This file is part of DREAM.
#
# DREAM 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 3 of the License, or
# (at your option) any later version.
#
# DREAM 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 DREAM. If not, see <http://www.gnu.org/licenses/>.
# ===========================================================================
import pyodbc
import json
import ImportDatabase
cnxn=ImportDatabase.ConnectionData(seekName='ServerData', implicitExt='txt', number_of_cursors=6)
cursor=cnxn.getCursors()
#create a dictionary data
data={}
#create list inside data dictionary that holds generic order's attributes
data['orders']=[]
#create dictionary inside data dictionary that holds the WIP
data['WIP']={}
#create dictionary inside data dictionary that holds the available weekly operation capacity
data['operations']={}
#SQL query that extracts info form operations table
b=cursor[0].execute("""
select OperationName, description
from operations
""")
# for every operation of the operations table
for j in range(b.rowcount):
#create a dictionary order
order={}
#get the next line
ind1=b.fetchone()
process=ind1.OperationName
# status = ind0.Status
# if status == 'accepted' or status == 'in progress':
#create a dictionary to insert the process sequence
data['operations'][process]={}
#SQL query to extract the available capacity for each of the operations
c= cursor[1].execute("""
select SMF, WELD, CNC, MCH, EEP, PPASB, ASBTST, PAINT
from capacity
""")
#create a dictionary to insert the available capacity
dicta={}
#insert operation's name as key in the dictionary
dicta['name']=ind1.OperationName
dicta['intervalCapacity']=[]
for line in range(c.rowcount):
ind2=c.fetchone()
#operation's name is given by the SQL quesry
operation=ind1.OperationName
#Check the operation name and insert the interval capacity
if operation=='SMF':
dicta['intervalCapacity'].append(ind2.SMF)
elif operation=='WELD':
dicta['intervalCapacity'].append(ind2.WELD)
elif operation=='CNC':
dicta['intervalCapacity'].append(ind2.CNC)
elif operation=='MCH':
dicta['intervalCapacity'].append(ind2.MCH)
elif operation=='EEP':
dicta['intervalCapacity'].append(ind2.EEP)
elif operation=='PPASB':
dicta['intervalCapacity'].append(ind2.PPASB)
elif operation=='ASBTST':
dicta['intervalCapacity'].append(ind2.ASBTST)
else:
dicta['intervalCapacity'].append(ind2.PAINT)
data['operations'][process]=dicta
###### Find the capacity ratio between SMF and WELD ######
#SQL query in sequence table to extract the capacity required from each operation
f=cursor[2].execute("""
select WP_id, Operation_Name, CapacityRequirement, EarliestStart
from sequence
""")
#create a list called capacity
capacity=[]
#for every line in sequence table
for j in range(f.rowcount):
#get the next line
ind4=f.fetchone()
#insert in capacity list the capacity required by each operation
capacity.append(ind4.CapacityRequirement)
#create variable that holds the calculation of the capacity ratio between SMF and WELD
key= capacity[1]/float(capacity[0])
#SQL query in orders table to extract generic info referring to each order
a=cursor[3].execute("""
select Order_id, ProjectName, Customer, Order_date, Due_date, FloorSpaceRequired, Status
from orders
""")
#for every order in orders table
for i in range(a.rowcount):
#create order disctionary
order={}
ind3=a.fetchone()
status = ind3.Status
#check order's status and move on if it's either 'accepted or 'in progress'
if status == 'accepted' or status == 'in progress':
#insert the following keys in order dictionary, keys referring to generic order's information
order['orderName']=ind3.ProjectName
order['orderID']=ind3.Order_id
order['customer']=ind3.Customer
order['orderDate']=str(ind3.Order_date)
order['dueDate']=str(ind3.Due_date)
order['floorSpaceRequired']=ind3.FloorSpaceRequired
order['sequence']=[]
#if order's status is 'finished', then continue to the next one
elif status == 'finished':
continue
#append order dictionary in data['orders'] list
data['orders'].append(order)
#SQL query that extracts data from sequence table where order is ind3.Order_id - given by the last query
d=cursor[4].execute("""
select WP_id, Operation_Name, CapacityRequirement, EarliestStart
from sequence where Order_id=?
""", ind3.Order_id)
#for every line in sequence table
for j in range(d.rowcount):
#create step dictionary
step={}
ind4=d.fetchone()
process=ind4.Operation_Name
task=ind4.WP_id
#create another dictionary in step dictionary and insert the following attributes
step[process]={}
step[process]['task_id']=task
step[process]['operation']=ind4.Operation_Name
step[process]['requiredCapacity']=ind4.CapacityRequirement
step[process]['earliestStart']=str(ind4.EarliestStart)
order['sequence'].append(step)
#SQL query that extracts data from production_status table, joining the sequence and production_status table in WP_id attribute, in order to retrieve that WIP
e= cursor[5].execute("""
select production_status.WP_id, sequence.WP_id, sequence.Order_id, sequence.CapacityRequirement, production_status.Capacity_left, production_status.START_DATE, production_status.END_DATE, production_status.Operation_Name
from production_status
join sequence on sequence.WP_id = production_status.WP_id
""")
#create a list that holds the performed operations
appended=[]
#initiate the following indicators
weldfinishedCap=0
cncfinishedCap=0
mchfinishedCap=0
wipList= e.fetchall()
#for every line in production_status table
for x in range(e.rowcount):
ind5=wipList[x]
task=ind5.WP_id
orderID=ind5.Order_id
#create a dictionary inside data['WIP'] with keys the task id
data['WIP'][task]={}
#insert the following attributes extracted from the database
data['WIP'][task]['operation']=ind5.Operation_Name
data['WIP'][task]['Start date']=str(ind5.START_DATE)
data['WIP'][task]['Total Capacity required']=ind5.CapacityRequirement
data['WIP'][task]['Capacity required']=ind5.Capacity_left
data['WIP'][task]['order_id']=ind5.Order_id
data['WIP'][task]['End date']=str(ind5.END_DATE)
#check operation's name and if is one of the three in the list; create the actually non-existing dictionary that holds the WIP - buffered just before the PPASB assembly operation
if data['WIP'][task]['operation'] in ['CNC','WELD','MCH']:
data['WIP']['PPASB_id' + orderID]={}
#check operation's name and if is a not yet finished SMF; create another 'fake' dict that holds the WIP buffered just before WELD
if data['WIP'][task]['operation']=='SMF' and not ind5.END_DATE:
data['WIP']['WELD_id' + orderID]={}
#check capacity required for each task - if it's 0 then delete this task from WIP
if data['WIP'][task]['Capacity required'] == 0:
del data['WIP'][task]
#if it's SMF and capacity finished then delete the fake dictionary created to hold the WIP just before WELD
if ind5.Operation_Name == 'SMF':
del data['WIP']['WELD_id' + orderID]
if ind5.Operation_Name not in appended:
appended.append(ind5.Operation_Name)
#try syntax to check if the operation finished is one of the following three; if yes delete the fake dictionary created to hold the WIP just before PPASB assembly station
try:
if 'CNC' and 'WELD' and 'MCH' in appended:
del data['WIP']['PPASB_id' + orderID]
except KeyError:
continue
#for every line in production_status table
for x in range(e.rowcount):
ind5=wipList[x]
orderID=ind5.Order_id
task=ind5.WP_id
operation=ind5.Operation_Name
#check ii operation is a not finished SMF then using the calculate using the relationship ratio between SMF and WELD the available capacity can start in WELD
if operation=='SMF' and not ind5.END_DATE:
finishedCap= ind5.CapacityRequirement - ind5.Capacity_left
startWELD = float(key) * float(finishedCap)
try:
data['WIP']['WELD_id' + orderID]['operation']='WELD'
data['WIP']['WELD_id' + orderID]['buffered']=startWELD
data['WIP']['WELD_id' + orderID]['order_id']=orderID
except KeyError:
continue
#check and if operation is WELD calculate first the finished capacity and then based on the status of CNC and MCH operations, the capacity buffered before the assembly station PPASB
if operation=='WELD':
try:
weldfinishedCap= ind5.CapacityRequirement - ind5.Capacity_left
data['WIP']['PPASB_id' + orderID ]['operation']='PPASB'
data['WIP']['PPASB_id' + orderID ]['order_id']=orderID
if not ind5.END_DATE:
data['WIP'][task]['Capacity required']= startWELD - weldfinishedCap
if cncfinishedCap or mchfinishedCap:
FinishedCap= weldfinishedCap + cncfinishedCap + mchfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
elif not mchfinishedCap:
FinishedCap= cncfinishedCap + weldfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
elif not cncfinishedCap:
FinishedCap= weldfinishedCap + mchfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
else:
FinishedCap=weldfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
except KeyError:
continue
#check and if operation is CNC calculate first the finished capacity and then based on the status of WELD and MCH operations, the capacity buffered before the assembly station PPASB
if operation=='CNC':
try:
cncfinishedCap= ind5.CapacityRequirement - ind5.Capacity_left
data['WIP']['PPASB_id' + orderID ]['operation']='PPASB'
data['WIP']['PPASB_id' + orderID ]['order_id']=orderID
if weldfinishedCap or mchfinishedCap:
FinishedCap= weldfinishedCap + cncfinishedCap + mchfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
elif not mchfinishedCap:
FinishedCap= cncfinishedCap + weldfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
elif not weldfinishedCap:
FinishedCap= cncfinishedCap + mchfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
else:
FinishedCap=cncfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
except KeyError:
continue
#check and if operation is MCH calculate first the finished capacity and then based on the status of CNC and WELD operations, the capacity buffered before the assembly station PPASB
if operation=='MCH':
try:
mchfinishedCap= ind5.CapacityRequirement - ind5.Capacity_left
data['WIP']['PPASB_id' + orderID ]['operation']='PPASB'
data['WIP']['PPASB_id' + orderID ]['order_id']=orderID
if weldfinishedCap or cncfinishedCap:
FinishedCap= weldfinishedCap + cncfinishedCap + mchfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
elif not cncfinishedCap:
FinishedCap= mchfinishedCap + weldfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
elif not weldfinishedCap:
FinishedCap= mchfinishedCap + cncfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
else:
FinishedCap = mchfinishedCap
data['WIP']['PPASB_id' + orderID ]['buffered']=FinishedCap
except KeyError:
continue
# print data['WIP']
print data
# export a JSON file called DBExtraction with this data
outputJSONString=json.dumps(data, indent=5)
outputJSONFile=open('DBExtraction.json', mode='w')
outputJSONFile.write(outputJSONString)
......@@ -20,14 +20,19 @@
import json
from datetime import datetime
configuration=open("PilotCases\CapacityStations\sampleConfiguration.json", "r")
# configuration=open("sampleConfiguration.json", "r")
# gets the data and the station id and returns the buffer id of this station
def getBuffer(data,stationId):
for (edge_id, edge) in data['graph']['edge'].iteritems():
if edge['destination']==stationId:
return edge['source']
# configuration=open("PilotCases\CapacityStations\sampleConfiguration.json", "r")
configuration=open("sampleConfiguration.json", "r")
configurationData=configuration.read()
configurationJSON=json.loads(configurationData)
db=open("PilotCases\CapacityStations\sampleDBExtraction.json", "r")
# configuration=open("sampleConfiguration.json", "r")
# db=open("PilotCases\CapacityStations\sampleDBExtraction.json", "r")
db=open("sampleDBExtraction.json", "r")
dbData=db.read()
dbJSON=json.loads(dbData)
operations=dbJSON.get('operations',{})
......@@ -75,7 +80,27 @@ for order in orders:
"orderDate":orderDate
})
WIP=dbJSON.get('WIP',{})
if WIP:
node=configurationJSON['graph']['node']
#create an empty wip list in all CapacityStationBuffers
for (node_id,node_data) in node.iteritems():
if node_data['_class']=='dream.simulation.applications.CapacityStations.CapacityStationBuffer.CapacityStationBuffer':
node_data['wip']=[]
for taskid, wip in WIP.iteritems():
oper=wip.get('operation',[])
requiredCapacity=wip.get('Capacity required', 0)
buffered=wip.get('buffered', 0)
orderId=wip.get('order_id',[])
if requiredCapacity or buffered:
capacityBuffer=getBuffer(configurationJSON,oper)
configurationJSON['graph']['node'][capacityBuffer]['wip'].append({
"_class": "dream.simulation.applications.CapacityStations.CapacityEntity.CapacityEntity",
"requiredCapacity": max([requiredCapacity,buffered]),
"capacityProjectId": orderId,
"name": orderId+'_'+oper+'_'+str(requiredCapacity)
})
updatedModelJSONString=json.dumps(configurationJSON, indent=5)
updatedModel=open('UpdatedModel.json', mode='w')
updatedModel.write(updatedModelJSONString)
......
# ===========================================================================
# Copyright 2013 University of Limerick
#
# This file is part of DREAM.
#
# DREAM 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 3 of the License, or
# (at your option) any later version.
#
# DREAM 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 DREAM. If not, see <http://www.gnu.org/licenses/>.
# ===========================================================================
import xlrd
import pyodbc
import datetime
import os
import ImportDatabase
#Static variables that hold the column's number in Workplan (Excel document - template)
WPColumn=11
OrderColumn=0
ProjectColumn=2
FloorSpaceReq=5
DayColumn=0
#Method created to extract the required data from the spreadsheet
def dataExtraction(File):
# loop through all the sheets
for sheet in xls_workbook.sheets():
if sheet.name=='Workplan':
orderId=sheet.cell(4,0).value
customer=sheet.cell(4,1).value
projectName=sheet.cell(4,2).value
orderDate=datetime.datetime.utcfromtimestamp((sheet.cell(4,3).value - 25569) * 86400.0)
dueDate=datetime.datetime.utcfromtimestamp((sheet.cell(4,4).value - 25569) * 86400.0)
floorSpace=sheet.cell(4,5).value
#create a dictionary called workplan with key the order id
workplan[orderId]={}
workplan[orderId][projectName]=[]
workplan[orderId][projectName].insert(0, customer)
workplan[orderId][projectName].insert(1, orderDate)
workplan[orderId][projectName].insert(2, dueDate)
workplan[orderId][projectName].insert(3, floorSpace)
#loop in the rows of the spreadsheet
for i in range(4,sheet.nrows):
#check and continue the loop if there are WP_ids in column 11.
if sheet.cell(i,11).value:
wpId=sheet.cell(i,11).value
partId=sheet.cell(i,6).value
partType=sheet.cell(i,7).value
operation=sheet.cell(i,8).value
capacityRequired=sheet.cell(i,9).value
earliestStart=datetime.datetime.utcfromtimestamp((sheet.cell(i,10).value - 25569) * 86400.0)
workplan[orderId][wpId]=[]
workplan[orderId][wpId].insert(0, partId)
workplan[orderId][wpId].insert(1, partType)
workplan[orderId][wpId].insert(2, operation)
workplan[orderId][wpId].insert(3, capacityRequired)
workplan[orderId][wpId].insert(4, earliestStart)
#pop-up message that inform the user that he should insert the directory where he stores the workplans
folder_path=raw_input('insert the path to the folder containing the required files:')
print folder_path
os.chdir(folder_path)
#create a list that hold the already inserted orders
alreadyInserted=[]
#use of the KE tool object to connect to database
cnxn=ImportDatabase.ConnectionData(seekName='ServerData', implicitExt='txt', number_of_cursors=6)
cursor=cnxn.getCursors()
#loop that searches the files in the given directory
for fileName in os.listdir("."):
print fileName
#using the xlrd python library, we open the documents
xls_workbook = xlrd.open_workbook(fileName)
#define Main the first sheet - sheet with the name 'Workplan'
Main= xls_workbook.sheet_by_name('Workplan')
workplan={}
#SQL query to extract data from orders table
check_order= cursor[0].execute("""
select Order_id, ProjectName, Customer, Order_date, Due_date,FloorSpaceRequired,Status
from orders
""")
#check if rowcount is 0, if it is move on - otherwise go to the else part
if check_order.rowcount==0:
#call the method giving as argument the document
dataExtraction(fileName)
for i in range(4,Main.nrows):
orderId=Main.cell(4,OrderColumn).value
projectName=Main.cell(4,ProjectColumn).value
#Another SQL query to input data this time in the database in orders table
add_order= ("INSERT INTO orders(`id`, `Order_id`, `ProjectName`, `Customer`, `Order_date`, `Due_date`, `FloorSpaceRequired`, `Status`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
cursor[0].execute("SELECT @@IDENTITY AS ID")
row = cursor[0].fetchone()
order_ref = row.ID
status= 'accepted'
#the following data inserted to the the database based on the structure of the query above
cursor[0].execute(add_order, (order_ref, orderId, projectName, workplan[orderId][projectName][0], workplan[orderId][projectName][1], workplan[orderId][projectName][2], workplan[orderId][projectName][3], status))
cursor[0].commit()
#SQL query to input data in the database in sequence table
add_sequence= ("INSERT INTO sequence(`seq_id`, `WP_id`, `Order_id`, `PartCode`, `PartName`, `Operation_Name`, `CapacityRequirement`, `EarliestStart`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ")
cursor[1].execute("SELECT seq_id AS ID from sequence")
seq_ref = row.ID
#loop used to insert the required data based on the structure of the above query
for i in range(4,Main.nrows):
wpId=Main.cell(i,WPColumn).value
cursor[1].execute(add_sequence, (seq_ref, wpId, orderId, workplan[orderId][wpId][0], workplan[orderId][wpId][1], workplan[orderId][wpId][2], workplan[orderId][wpId][3], workplan[orderId][wpId][4]))
cursor[1].commit()
seq_ref+=1
else:
#follows the logic of the if part
check_order= cursor[3].execute("""
select Order_id, ProjectName, Customer, Order_date, Due_date,FloorSpaceRequired
from orders
""")
for j in range(check_order.rowcount):
#get the next line
ind1=check_order.fetchone()
dataExtraction(fileName)
for i in range(4,Main.nrows):
orderId=Main.cell(4,OrderColumn).value
projectName=Main.cell(4,ProjectColumn).value
#check the existence of orderId
if not orderId in ind1.Order_id:
add_order= ("INSERT INTO orders(`id`, `Order_id`, `ProjectName`, `Customer`, `Order_date`, `Due_date`, `FloorSpaceRequired`, `Status`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
cursor[3].execute("SELECT MAX(id)+1 AS ID from orders")
row = cursor[3].fetchone()
order_ref = row.ID
status= 'accepted'
cursor[3].execute(add_order, (order_ref, orderId, projectName, workplan[orderId][projectName][0], workplan[orderId][projectName][1], workplan[orderId][projectName][2], workplan[orderId][projectName][3], status))
cursor[3].commit()
add_sequence= ("INSERT INTO sequence(`seq_id`, `WP_id`, `Order_id`, `PartCode`, `PartName`, `Operation_Name`, `CapacityRequirement`, `EarliestStart`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ")
cursor[4].execute("SELECT MAX(seq_id)+1 AS ID from sequence")
row = cursor[4].fetchone()
seq_ref = row.ID
for i in range(4,Main.nrows):
wpId=Main.cell(i,WPColumn).value
cursor[4].execute(add_sequence, (seq_ref, wpId, orderId, workplan[orderId][wpId][0], workplan[orderId][wpId][1], workplan[orderId][wpId][2], workplan[orderId][wpId][3], workplan[orderId][wpId][4]))
cursor[4].commit()
seq_ref+=1
else:
continue
\ No newline at end of file
......@@ -171,21 +171,129 @@
"dueDate": "2014-08-25",
"orderName": "italy",
"orderDate": "2014-08-19"
},
{
"orderID": "Touto",
"customer": "t2",
"floorSpaceRequired": 300,
"sequence": [
{
"SMF": {
"operation": "SMF",
"earliestStart": "2014-08-23",
"task_id": "ID-009",
"requiredCapacity": 40
}
},
{
"WELD": {
"operation": "WELD",
"earliestStart": "2014-08-24",
"task_id": "ID-010",
"requiredCapacity": 25
}
},
{
"CNC": {
"operation": "CNC",
"earliestStart": "2014-08-25",
"task_id": "ID-011",
"requiredCapacity": 55
}
},
{
"MCH": {
"operation": "MCH",
"earliestStart": "2014-08-26",
"task_id": "ID-012",
"requiredCapacity": 35
}
},
{
"EEP": {
"operation": "EEP",
"earliestStart": "2014-08-27",
"task_id": "ID-013",
"requiredCapacity": 25
}
},
{
"PPASB": {
"operation": "PPASB",
"earliestStart": "2014-08-28",
"task_id": "ID-014",
"requiredCapacity": 35
}
},
{
"PAINT": {
"operation": "PAINT",
"earliestStart": "2014-08-29",
"task_id": "ID-015",
"requiredCapacity": 45
}
},
{
"ASBTST": {
"operation": "ASBTST",
"earliestStart": "2014-08-30",
"task_id": "ID-016",
"requiredCapacity": 55
}
}
],
"dueDate": "2014-08-31",
"orderName": "ecofil",
"orderDate": "2014-08-21"
}
],
"WIP": {
"WELD_idTade": {
"ID-002": {
"End date": "None",
"Total Capacity required": 15,
"Capacity required": 7.5,
"order_id": "Tade",
"buffered": 10.0,
"operation": "WELD"
"operation": "WELD",
"Start date": "2015-03-20"
},
"ID-003": {
"End date": "None",
"Total Capacity required": 45,
"Capacity required": 5,
"order_id": "Tade",
"operation": "CNC",
"Start date": "2015-03-20"
},
"ID-001": {
"End date": "None",
"Total Capacity required": 30,
"Capacity required": 10,
"Capacity required": 5,
"order_id": "Tade",
"operation": "SMF",
"Start date": "2015-03-13"
"Start date": "2015-03-17"
},
"WELD_idTouto": {
"order_id": "Touto",
"buffered": 17.5,
"operation": "WELD"
},
"ID-009": {
"End date": "None",
"Total Capacity required": 40,
"Capacity required": 5,
"order_id": "Touto",
"operation": "SMF",
"Start date": "2015-03-20"
},
"PPASB_idTade": {
"order_id": "Tade",
"buffered": 45,
"operation": "PPASB"
},
"WELD_idTade": {
"order_id": "Tade",
"buffered": 12.5,
"operation": "WELD"
}
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment