import requests from bs4 import BeautifulSoup as bs # import csv from openpyxl import workbook from openpyxl.styles import Font from WriteReport import * bold = Font(bold=True) def Obtain_Ticket(URL, HOST): GetTicket = """ ddembinski mond@y string """ headers = {'Host': HOST, 'Content-Type': 'application/soap+xml; charset=utf-8', 'Content-Length': 'length'} response = requests.post(url=URL, data=GetTicket, headers=headers).text # print(response) temp1 = response.split('') temp2 = temp1[1].split('') ticket = temp2[0] print("got ticket: " + ticket) return ticket def Release_Ticket(ticket, URL, HOST): ReleaseTicket = """ """+ticket+"""" """ headers = {'Host': HOST, 'Content-Type': 'application/soap+xml; charset=utf-8', 'Content-Length': 'length'} response = requests.post(url=URL, data=ReleaseTicket, headers=headers).text print("released ticket "+ticket) def Westfield(): print("Westfield") exit() def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS, columnFormat, SSheader): ExternalID = [] id = [] shippingcost = [] ############# Order Summary tab ############# report_filter = """ <?xml version="1.0"?> <PFWebFilter:UserFilter xmlns:PFWebFilter="http://www.pageflex.com/schemas/2004/Storefront/UserFilters/20040817" filterClass="Orders"> <PFWebFilter:Step publicFieldName="Balance Due" query="ExactUnequals" minValue="0.00" maxValue="" /> <PFWebFilter:Step publicFieldName="Order Status" query="ExactEquals" minValue="Completed" maxValue="" /> </PFWebFilter:UserFilter> """ send = """ """+ticket+""" Orders false """+report_filter+"""" """+REPORT_FIELDS+""" """ headers = {'Host': HOST, 'Content-Type': 'application/soap+xml; charset=utf-8', 'Content-Length': 'length', 'SOAPAction': 'http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111/GetReport'} response = requests.post(url=URL, data=send, headers=headers).text temp1 = response.split('') temp2 = temp1[1].split('') report = (temp2[0]) print("working") y = bs(report, "lxml") # Opens report file and writes each row Write_Report(y, ws, columnFormat, SSheader) # Grabs all the Order IDs from Column A for cell in ws['A']: ExternalID.append(cell.value) ExternalID.pop(0) # Reset the header SOAPAction to FindOrderID. This is needed to get the OrderID needed for Shipping Charges headers = {'Host': HOST, 'Content-Type': 'application/soap+xml; charset=utf-8', 'Content-Length': 'length', 'SOAPAction': 'http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111/FindOrderID'} # Loop through ExternalIDs print("Getting Doc IDs") for x in ExternalID: ID_send = """ """ + ticket + """ """+x+""" """ # Parse returned for DocIDs getDocID = requests.post(url=URL, data=ID_send, headers=headers).text y = bs(getDocID, "lxml") for w in y.find_all('val'): id.append(w.text) # Reset headers for GerValue headers = {'Host': HOST, 'Content-Type': 'application/soap+xml; charset=utf-8', 'Content-Length': 'length','SOAPAction': 'http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111/GetValue'} print("Getting Shipping Costs") # Loop through the ExternalIDs and get the Shipping Charge for z in id: shipping_send = """ """+ticket+""" ShippingCharge OrderProperty """+z+""" """ getShipping = requests.post(url=URL, data=shipping_send, headers=headers).text y = bs(getShipping, "lxml") for w in y.find_all('fval'): shippingcost.append(float(w.text)) # Add columns to Summary Tab ws.insert_cols(2) ws['B1'] = "Match" ws.insert_cols(5) ws['E1'] = "Production Charges" ws.insert_cols(6) ws['F1'] = "Shipping Charges" ws.insert_cols(7) ws['G1'] = "Adjustments" # Load Shipping Charges into the sheet count = 2 for x in shippingcost: ws.cell(column=6, row=count, value=x) count = count + 1 # Compute Production Charges and write to spreadsheet count = 2 for row in ws: value = "=H"+str(count)+"-G"+str(count)+"-F"+str(count) ws.cell(column=5, row=count, value=value) count += 1 ############# Order Details tab ############# print("Getting Item Detail") for z in ExternalID: report_filter = """<?xml version="1.0"?> <PFWebFilter:UserFilter xmlns:PFWebFilter="http://www.pageflex.com/schemas/2004/Storefront/UserFilters/20040817" filterClass="Items"> <PFWebFilter:Step publicFieldName="Order ID" query="ExactEquals" minValue=\"""" + z + """\" maxValue="" /> </PFWebFilter:UserFilter>""" send = """ """+ticket+""" Items false """+report_filter+"""" """+DETAIL_REPORT_FIELDS+""" """ headers = {'Host': HOST, 'Content-Type': 'application/soap+xml; charset=utf-8', 'Content-Length': 'length', 'SOAPAction': 'http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111/GetReport'} response = requests.post(url=URL, data=send, headers=headers).text temp1 = response.split('') temp2 = temp1[1].split('') report = (temp2[0]) y = bs(report, "lxml") # Opens report file and writes each row Detail_Write_Report(y, ws) # Finalize Spreadsheet and save for cell in ws["1:1"]: cell.font = bold for cell in ws2["1:1"]: cell.font = bold print("Enter a filename for the report: ") filename = input() wb.save('report.xlsx') def loadMenu(): ConfigFile = "StorefrontUtilitiesConfig.xml" ClientName = [] SF_URL = [] SF_HOST = [] CLIENT_SUMMARY_TAB = [] SUMMARY_TAB = [] headers = [] client_headers = [] CLIENT_DETAIL_TAB = [] DETAIL_TAB = [] client_detail_headers = [] detail_headers = [] CLIENT_COLUMN_FORMAT = [] COLUMN_FORMAT = [] # Open the config file with open(ConfigFile) as f: r = f.read() y = bs(r, "lxml") # Get list of clients for x in y.find_all('name'): # print(x.text) ClientName.append(x.text) # Get Storefront API URls for x in y.find_all('storefrontapiurl'): # print(x) SF_URL.append(x.text) # Get Storefront Host URLs for x in SF_URL: temp1 = x.split('://') temp2 = temp1[1].split('/') HOST = (temp2[0]) SF_HOST.append(HOST) # Get Monthly Report Summary Fields for w in y.find_all('client'): for x in w.find_all('monthlyreport'): for g in x.find_all('summarytab'): for z in g.find_all('columnname'): z.name = "string" CLIENT_SUMMARY_TAB.append(z) client_headers.append(z.text) SUMMARY_TAB.append(CLIENT_SUMMARY_TAB) headers.append(client_headers) CLIENT_SUMMARY_TAB = [] client_headers = [] # Get Monthly Report Column Format for w in y.find_all('client'): for x in w.find_all('monthlyreport'): for g in x.find_all('summarytab'): for z in g.find_all('columnformat'): CLIENT_COLUMN_FORMAT.append(z.text) COLUMN_FORMAT.append(CLIENT_COLUMN_FORMAT) CLIENT_COLUMN_FORMAT = [] # Get Monthly Report Item Detail Fields for w in y.find_all('client'): for x in w.find_all('monthlyreport'): for g in x.find_all('detailtab'): for z in g.find_all('columnname'): z.name = "string" CLIENT_DETAIL_TAB.append(z) client_detail_headers.append(z.text) DETAIL_TAB.append(CLIENT_DETAIL_TAB) detail_headers.append(client_detail_headers) CLIENT_DETAIL_TAB = [] client_detail_headers = [] choice = 0 for x in ClientName: print(choice, x) choice = choice + 1 option = int(input()) if ClientName[option] == "Westfield": Westfield() columnformat = dict.fromkeys(headers[option]) # Get specific Column Types for chosen client for w in y.find_all('client'): for x in w.find_all('name'): if x.text == ClientName[option]: for x in w.find_all('monthlyreport'): for g in x.find_all('summarytab'): for z in g.find_all('columnformat'): CLIENT_COLUMN_FORMAT.append(z.text) # set Column Types Header Dictionary count = 0 for key in columnformat: columnformat[key] = CLIENT_COLUMN_FORMAT[count] count = count + 1 # create new report file and add headers based on config file wb = workbook.Workbook() ws = wb.active ws.title = "Order Summary" ws.append(headers[option]) ws2 = wb.create_sheet("Item Detail") ws2.append(detail_headers[option]) # converts summary_tab values to a string, adds everything to the URL_HOST array and returns it REPORT_FIELDS = '\n'.join(map(str, SUMMARY_TAB[option])) DETAIL_REPORT_FIELDS = '\n'.join(map(str, DETAIL_TAB[option])) URL_HOST = [SF_URL[option], SF_HOST[option], REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS, columnformat, headers[option]] return URL_HOST def main(): URL_HOST = loadMenu() URL = URL_HOST[0] HOST = URL_HOST[1] REPORT_FIELDS = URL_HOST[2] wb = URL_HOST[3] ws = URL_HOST[4] ws2 = URL_HOST[5] DETAIL_REPORT_FIELDS = URL_HOST[6] columnFormat = URL_HOST[7] SSheader = URL_HOST[8] ticket = Obtain_Ticket(URL, HOST) Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS, columnFormat, SSheader) Release_Ticket(ticket, URL, HOST) main()