Started reworking main.py. Broke loadMenu out into LoadClient to get just client names, and ClientConfig, which will load xml for only the selected client.
439 lines
17 KiB
Python
439 lines
17 KiB
Python
import requests
|
|
from bs4 import BeautifulSoup as bs
|
|
from openpyxl import workbook
|
|
from openpyxl.utils import get_column_letter
|
|
from WriteReport import *
|
|
from ClientSpecific import *
|
|
|
|
|
|
|
|
|
|
def Obtain_Ticket(URL, HOST):
|
|
GetTicket = """<?xml version="1.0" encoding="utf-8"?>
|
|
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
|
|
<soap12:Body>
|
|
<ObtainUserTicket xmlns="http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111">
|
|
<username>ddembinski</username>
|
|
<password>mond@y</password>
|
|
<ident>string</ident>
|
|
</ObtainUserTicket>
|
|
</soap12:Body>
|
|
</soap12:Envelope>"""
|
|
|
|
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('<ObtainUserTicketResult>')
|
|
temp2 = temp1[1].split('</ObtainUserTicketResult>')
|
|
ticket = temp2[0]
|
|
print("got ticket: " + ticket)
|
|
return ticket
|
|
|
|
def Release_Ticket(ticket, URL, HOST):
|
|
ReleaseTicket = """<?xml version="1.0" encoding="utf-8"?>
|
|
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
|
|
<soap12:Body>
|
|
<ReleaseTicket xmlns="http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111">
|
|
<ticket>"""+ticket+""""</ticket>
|
|
</ReleaseTicket>
|
|
</soap12:Body>
|
|
</soap12:Envelope>"""
|
|
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 Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS, summary_columnFormat, summary_SSheader, detail_columnFormat, detail_SSheader):
|
|
|
|
ExternalID = []
|
|
id = []
|
|
shippingcost = []
|
|
products = set()
|
|
|
|
|
|
############# Order Summary tab #############
|
|
report_filter = """ <filterNameOrXml><?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></filterNameOrXml>
|
|
"""
|
|
|
|
send = """<?xml version="1.0" encoding="utf-8"?>
|
|
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
|
|
<soap12:Body>
|
|
<GetReport xmlns="http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111">
|
|
<token>"""+ticket+"""</token>
|
|
<reportName>Orders</reportName>
|
|
<archived>false</archived>
|
|
"""+report_filter+""""
|
|
<columnNames>"""+REPORT_FIELDS+"""</columnNames>
|
|
</GetReport>
|
|
</soap12:Body>
|
|
</soap12:Envelope>"""
|
|
|
|
|
|
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('</GetReportResult>')
|
|
temp2 = temp1[1].split('<error />')
|
|
report = (temp2[0])
|
|
print("working")
|
|
|
|
y = bs(report, "lxml")
|
|
|
|
# Opens report file and writes each row
|
|
sheet = ws
|
|
Write_Report(y, summary_columnFormat, summary_SSheader, sheet)
|
|
|
|
# Grabs all the Order IDs from Column A
|
|
for cell in ws['A']:
|
|
ExternalID.append(cell.value)
|
|
ExternalID.pop(0)
|
|
# weird = len(ExternalID)
|
|
# ExternalID.pop(weird-1)
|
|
# print(ExternalID)
|
|
# exit()
|
|
|
|
# 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 = """<?xml version="1.0" encoding="utf-8"?>
|
|
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
|
|
<soap12:Body>
|
|
<FindOrderID xmlns="http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111">
|
|
<token>""" + ticket + """</token>
|
|
<externalid>"""+x+"""</externalid>
|
|
</FindOrderID>
|
|
</soap12:Body>
|
|
</soap12:Envelope>"""
|
|
|
|
# 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 = """<?xml version="1.0" encoding="utf-8"?>
|
|
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
|
|
<soap12:Body>
|
|
<GetValue xmlns="http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111">
|
|
<t>"""+ticket+"""</t>
|
|
<fname>ShippingCharge</fname>
|
|
<ftype>OrderProperty</ftype>
|
|
<objid>"""+z+"""</objid>
|
|
</GetValue>
|
|
</soap12:Body>
|
|
</soap12:Envelope>
|
|
"""
|
|
|
|
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 = 1
|
|
for x in shippingcost:
|
|
ws.cell(column=6, row=count+1, value=x)
|
|
count = count + 1
|
|
|
|
# Production and Adjustment Charges
|
|
|
|
ItemPrice = 0
|
|
for x in detail_SSheader:
|
|
if x != "Item Price":
|
|
ItemPrice += 1
|
|
else:
|
|
break
|
|
ItemPrice += 1
|
|
ItemPriceColumn = get_column_letter(ItemPrice)
|
|
|
|
count = 1
|
|
for row in ws:
|
|
if count != 1:
|
|
|
|
value = "=SUMIF('item Detail'!A:A,A" + str(count) + ",'Item Detail'!" + ItemPriceColumn + ":" + ItemPriceColumn + ")"
|
|
ws.cell(column=5, row=count, value=value)
|
|
value = "=H" + str(count) + "-E" + str(count) + "-F" + str(count)
|
|
ws.cell(column=7, row=count, value = value)
|
|
if count < ws.max_row:
|
|
count += 1
|
|
|
|
|
|
############# Order Details tab #############
|
|
|
|
print("Getting Item Detail")
|
|
|
|
for z in ExternalID:
|
|
# report_filter = """<filterNameOrXml><?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></filterNameOrXml>"""
|
|
report_filter = """<filterNameOrXml><?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:Step publicFieldName="Item Status" query="ExactEquals" minValue=\"Shipped" maxValue="" /> </PFWebFilter:UserFilter></filterNameOrXml>"""
|
|
|
|
send = """<?xml version="1.0" encoding="utf-8"?>
|
|
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
|
|
<soap12:Body>
|
|
<GetReport xmlns="http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111">
|
|
<token>"""+ticket+"""</token>
|
|
<reportName>Items</reportName>
|
|
<archived>false</archived>
|
|
"""+report_filter+""""
|
|
<columnNames>"""+DETAIL_REPORT_FIELDS+"""</columnNames>
|
|
</GetReport>
|
|
</soap12:Body>
|
|
</soap12:Envelope>"""
|
|
|
|
|
|
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('</GetReportResult>')
|
|
temp2 = temp1[1].split('<error />')
|
|
report = (temp2[0])
|
|
|
|
y = bs(report, "lxml")
|
|
|
|
# Opens report file and writes each row
|
|
Write_Report(y, detail_columnFormat, detail_SSheader, ws2)
|
|
|
|
|
|
# Product Summary Tab
|
|
ws3 = wb.create_sheet("Product Summary")
|
|
|
|
# Load in all proudct names and dedupe
|
|
count = 1
|
|
for x in ws2.rows:
|
|
if count != 1:
|
|
value = ws2.cell(column = 6, row = count).value
|
|
products.add(value)
|
|
count += 1
|
|
|
|
count = 2
|
|
for x in products:
|
|
ws3.cell(column = 1, row = count, value = x)
|
|
count += 1
|
|
|
|
# Add Quantity and Item Price columns. Populate both based using SUMIF off of the Item Detail Tab
|
|
|
|
ws3['A1'] = "Product Name"
|
|
ws3['B1'] = "Quantity"
|
|
ws3['C1'] = "Item Price"
|
|
count = 1
|
|
for x in ws3:
|
|
value = "=SUMIF('Item Detail'!F:F,A"+str(count)+",'Item Detail'!G:G)"
|
|
if count != 1:
|
|
ws3.cell(column = 2, row = count, value = value)
|
|
count += 1
|
|
|
|
count = 1
|
|
for x in ws3:
|
|
value = "=SUMIF('Item Detail'!F:F,A"+str(count)+",'Item Detail'!H:H)"
|
|
if count != 1:
|
|
ws3.cell(column = 3, row = count, value = value)
|
|
count += 1
|
|
|
|
|
|
# Finalize Spreadsheet and save
|
|
Format_Report(ws, summary_SSheader, summary_columnFormat)
|
|
Format_Report(ws2, detail_SSheader, detail_columnFormat)
|
|
Save_Report(wb, ws, ws2, ws3, summary_SSheader, detail_SSheader, summary_columnFormat)
|
|
|
|
def loadClients():
|
|
|
|
ConfigFile = "../StorefrontUtilitiesConfig.xml"
|
|
ClientName = []
|
|
|
|
# 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)
|
|
|
|
return ClientName
|
|
|
|
def GetClientConfig(Client):
|
|
|
|
SF_URL = []
|
|
CLIENT_SUMMARY_TAB = []
|
|
client_headers = []
|
|
SUMMARY_TAB = []
|
|
headers =[]
|
|
CLIENT_COLUMN_FORMAT = []
|
|
COLUMN_FORMAT = []
|
|
DETAIL_TAB = []
|
|
detail_headers =[]
|
|
CLIENT_DETAIL_TAB = []
|
|
client_detail_headers = []
|
|
REPORT_FIELDS= []
|
|
DETAIL_REPORT_FIELDS = []
|
|
summary_columnformat = []
|
|
detail_columnformat = []
|
|
|
|
# Loads config file and reads in XML
|
|
ConfigFile = "../StorefrontUtilitiesConfig.xml"
|
|
|
|
# Open the config file
|
|
with open(ConfigFile) as f:
|
|
r = f.read()
|
|
|
|
y = bs(r, "lxml")
|
|
|
|
|
|
# Get Storefront API URl
|
|
for w in y.find_all('client'):
|
|
for x in w.find_all('name'):
|
|
if x.text == Client:
|
|
SF_URL = x.find_all('storefrontapiurl').text
|
|
|
|
# Get Storefront Host URL
|
|
temp1 = SF_URL.split('://')
|
|
temp2 = temp1[1].split('/')
|
|
SF_HOST = (temp2[0])
|
|
|
|
# Get Monthly Report Summary Fields
|
|
for w in y.find_all('client'):
|
|
for x in w.find_all('name'):
|
|
if x.text == 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('name'):
|
|
if x.text == 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('name'):
|
|
if x.text == 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 = []
|
|
|
|
# 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))
|
|
DETAIL_REPORT_FIELDS = '\n'.join(map(str, DETAIL_TAB))
|
|
|
|
# Get specific Summary Column Types for chosen client
|
|
for w in y.find_all('client'):
|
|
for x in w.find_all('name'):
|
|
if x.text == 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)
|
|
|
|
summary_columnformat = dict.fromkeys(headers)
|
|
# set Summary Column Types Header Dictionary
|
|
count = 0
|
|
for key in summary_columnformat:
|
|
summary_columnformat[key] = CLIENT_COLUMN_FORMAT[count]
|
|
count = count + 1
|
|
|
|
CLIENT_COLUMN_FORMAT = []
|
|
# Get specific Detail Column Types for chosen client
|
|
for w in y.find_all('client'):
|
|
for x in w.find_all('name'):
|
|
if x.text == Client:
|
|
for x in w.find_all('monthlyreport'):
|
|
for g in x.find_all('detailtab'):
|
|
for z in g.find_all('columnformat'):
|
|
CLIENT_COLUMN_FORMAT.append(z.text)
|
|
|
|
|
|
|
|
detail_columnformat = dict.fromkeys(detail_headers)
|
|
# set Detail Column Types Header Dictionary
|
|
count = 0
|
|
for key in detail_columnformat:
|
|
detail_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)
|
|
|
|
ws2 = wb.create_sheet("Item Detail")
|
|
ws2.append(detail_headers)
|
|
|
|
ClientConfig = [SF_URL, SF_HOST, REPORT_FIELDS, DETAIL_REPORT_FIELDS, summary_columnformat, headers, detail_columnformat, detail_headers, wb, ws, ws2]
|
|
|
|
return ClientConfig
|
|
|
|
|
|
# def loadMenu():
|
|
#
|
|
# Since I forgot what this is...It passes ClientName over to ClientSpecific.py to see if they have any custom tabs.
|
|
# client_Specific_Tabs(ClientName[option])
|
|
# URL_HOST = [SF_URL[option], SF_HOST[option], REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS, summary_columnformat, headers[option], detail_columnformat, detail_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]
|
|
# summary_columnFormat = URL_HOST[7]
|
|
#
|
|
# summary_SSheader = URL_HOST[8]
|
|
# detail_columnFormat = URL_HOST[9]
|
|
# detail_SSheader = URL_HOST[10]
|
|
#
|
|
# ticket = Obtain_Ticket(URL, HOST)
|
|
# Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS, summary_columnFormat, summary_SSheader, detail_columnFormat, detail_SSheader)
|
|
# Release_Ticket(ticket, URL, HOST)
|
|
#
|
|
# main() |