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 = """
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 Run_Report(ticket, URL, HOST, REPORT_FIELDS, ws, wb):
ExternalID = []
id = []
shippingcost = []
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+"""
"""
# 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('')
temp2 = temp1[1].split('')
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 = """
""" + 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 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()