dasaproject/PriceLevelQuery.py
2023-09-27 15:49:36 +07:00

147 lines
4.9 KiB
Python

#!usr/bin/python
import win32com.client
import xml.etree.ElementTree as ET
import json
import xmltodict
# Connect to Quickbooks
sessionManager = win32com.client.Dispatch("QBXMLRP2.RequestProcessor")
sessionManager.OpenConnection('', 'Test qbXML Request')
ticket = sessionManager.BeginSession("C:\Quickbooks Bogor\qb test\distrindo bakti wutama.qbw", 0)
# Send query and receive response
qbxml_query = """
<?qbxml version="13.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<ItemInventoryQueryRq metaData="MetaDataAndResponseData">
<NameFilter>
<MatchCriterion>Contains</MatchCriterion>
<Name>TS-I502</Name>
</NameFilter>
<IncludeRetElement>QuantityOnHand</IncludeRetElement>
</ItemInventoryQueryRq>
</QBXMLMsgsRq>
</QBXML>
"""
qbxml_query = """
<?qbxml version="13.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<PriceLevelQueryRq metaData="MetaDataAndResponseData">
<FullName>A</FullName>
<FullName>B</FullName>
</PriceLevelQueryRq>
</QBXMLMsgsRq>
</QBXML>
"""
qbxml_query = """<?qbxml version="13.0"?>
<QBXML><QBXMLMsgsRq onError="stopOnError"><PriceLevelQueryRq metaData="MetaDataAndResponseData"><FullName>A</FullName><FullName>B</FullName></PriceLevelQueryRq></QBXMLMsgsRq></QBXML>"""
qbxml_query1 = """
<?qbxml version="13.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<ItemInventoryQueryRq metaData="MetaDataAndResponseData">
<IncludeRetElement>QuantityOnHand</IncludeRetElement>
<IncludeRetElement>QuantityOnOrder</IncludeRetElement>
<IncludeRetElement>QuantityOnSalesOrder</IncludeRetElement>
<IncludeRetElement>FullName</IncludeRetElement>
<IncludeRetElement>Name</IncludeRetElement>
</ItemInventoryQueryRq>
</QBXMLMsgsRq>
</QBXML>
"""
root = ET.Element("QBXML")
root.tail = "\n "
root.text = "\n "
QBXMLMsgsRq = ET.SubElement(root, "QBXMLMsgsRq")
QBXMLMsgsRq.set("onError", "stopOnError")
QBXMLMsgsRq.tail = "\n"
QBXMLMsgsRq.text = "\n "
PriceLevelQueryRq = ET.SubElement(QBXMLMsgsRq, "PriceLevelQueryRq")
#PriceLevelQueryRq.set("metaData", "MetaDataAndResponseData")
PriceLevelQueryRq.set("metaData", "NoMetaData")
PriceLevelQueryRq.tail = "\n "
PriceLevelQueryRq.text = "\n "
FullName = ET.SubElement(PriceLevelQueryRq, "FullName")
FullName.text = "A"
FullName.tail = "\n "
FullName = ET.SubElement(PriceLevelQueryRq, "FullName")
FullName.text = "C"
FullName.tail = "\n "
#mydata = ET.tostring(root, encoding = "unicode", method = "xml")
mydata = ET.tostring(root, encoding = "unicode")
#mydata = ET.tostring(root).decode()
print (mydata,type(mydata))
qbxml_query1 = """<?xml version="1.0" encoding="utf-8"?>"""
qbxml_query1 = qbxml_query1 + """<?qbxml version="11.0"?>"""
qbxml_query1 = qbxml_query1 + "\n" + mydata
print("")
print(qbxml_query1, type(qbxml_query1))
print("")
#print(qbxml_query, type(qbxml_query))
tree = ET.ElementTree(root)
print("test",tree)
#response_string = sessionManager.ProcessRequest(ticket, qbxml_query1)
# Disconnect from Quickbooks
sessionManager.EndSession(ticket) # Close the company file
sessionManager.CloseConnection() # Close the connection
#print (response_string)
# Parse the response into an Element Tree and peel away the layers of response
#QBXML = xml.etree.ElementTree.fromstring(response_string)
QBXML = ET.fromstring(response_string)
print("")
#print(QBXML)
#print(QBXML)
datadict=xmltodict.parse(response_string)
#print(datadict)
jsondata=json.dumps(datadict)
#print(jsondata)
ddict=json.loads(jsondata)
#print(ddict)
QBXMLMsgsRs = QBXML.find('QBXMLMsgsRs')
#InventoryAdjustmentQueryRs = QBXMLMsgsRs.getiterator("InventoryAdjustmentRet")
#InventoryAdjustmentQueryRs = QBXMLMsgsRs.iter("ItemInventoryRet")
PriceLevelQueryRs = QBXMLMsgsRs.iter("PriceLevelRet")
#print (PriceLevelQueryRs)
PriceLevelRet = ddict["QBXML"]["QBXMLMsgsRs"]["PriceLevelQueryRs"]["PriceLevelRet"]
#print (priceLevelPerItemRet)
for priceLevelPerItemRetList in PriceLevelRet:
#print(priceLevelPerItemRetList)
#priceLevelPerItemRet = priceLevelPerItemRet["PriceLevelPerItemRet"]
for PriceLevelPerItemRet in priceLevelPerItemRetList["PriceLevelPerItemRet"]:
pass
print(priceLevelPerItemRetList["Name"],PriceLevelPerItemRet["ItemRef"]["FullName"], PriceLevelPerItemRet["CustomPrice"])
# for PriceLevelRet in PriceLevelQueryRs:
# print(PriceLevelRet)
# # try:
# PLT = PriceLevelRet.find('PriceLevelType').text
# IsActive = PriceLevelRet.find('IsActive').text
# #txnid = PriceLevelRet.find('ListID').text
# name = PriceLevelRet.find('Name').text
# PriceLevelPerItemRet = PriceLevelRet.iter("PriceLevelPerItemRet")
# for ItemRef in PriceLevelPerItemRet:
# print(ItemRef)
# FullName = ItemRef.find('FullName').text
# CustomPrice = PriceLevelRet.find('CustomPrice').text
# print(FullName, name, PLT, CustomPrice)
# # except AttributeError:
# # print("attrib err")
# # except:
# # print("err")