Files
StorefrontUtility/main.py

234 lines
8.5 KiB
Python

import requests
from bs4 import BeautifulSoup as bs
# import csv
from openpyxl import workbook
from openpyxl.styles import Font
bold = Font(bold=True)
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, ws, wb):
ExternalID = []
id = []
shippingcost = []
report_filter = """ <filterNameOrXml>&lt;?xml version="1.0"?&gt; &lt;PFWebFilter:UserFilter xmlns:PFWebFilter="http://www.pageflex.com/schemas/2004/Storefront/UserFilters/20040817" filterClass="Orders"&gt; &lt;PFWebFilter:Step publicFieldName="Balance Due" query="ExactUnequals" minValue="0.00" maxValue="" /&gt; &lt;PFWebFilter:Step publicFieldName="Order Status" query="ExactEquals" minValue="Completed" maxValue="" /&gt; &lt;/PFWebFilter:UserFilter&gt;</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>"""
# print(send)
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")
results = []
# Opens report file and writes each row
for child in y.report:
for childs in child:
results.append(childs.text)
ws.append(results)
results = []
# 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 = """<?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 Shipping Charge column. Set header and make bold
ws.insert_cols(4)
ws['D1'] = "Shipping Charges"
ws['D1'].font = bold
# Load Shipping Charges into the sheet
count = 2
for x in shippingcost:
ws.cell(column=4, row=count, value=x)
count = count + 1
wb.save('report.xlsx')
def loadMenu():
ClientName = []
SF_URL = []
SF_HOST = []
CLIENT_SUMMARY_TAB = []
SUMMARY_TAB = []
headers = []
client_headers = []
# Open the config file
with open("Config_ORIG.xml") 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 = []
choice = 0
for x in ClientName:
print(choice, x)
choice = choice + 1
option = int(input())
# create new report file and adds headers based on config file
wb = workbook.Workbook()
ws = wb.active
ws.title = "Order Summary"
ws.append(headers[option])
bold = Font(bold=True)
for cell in ws["1:1"]:
cell.font = bold
# cell.row_dimensions.width = 25
# 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]))
URL_HOST = [SF_URL[option], SF_HOST[option], REPORT_FIELDS, wb, ws]
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]
ticket = Obtain_Ticket(URL, HOST)
Run_Report(ticket, URL, HOST, REPORT_FIELDS, ws, wb)
Release_Ticket(ticket, URL, HOST)
main()