#!usr/bin/python
import win32com.client
import xml.etree.ElementTree

# Connect to Quickbooks
sessionManager = win32com.client.Dispatch("QBXMLRP2.RequestProcessor")    
sessionManager.OpenConnection('', 'Test qbXML Request')
ticket = sessionManager.BeginSession("", 2)

# Send query and receive response
qbxml_query = """
<?qbxml version="6.0"?>
<QBXML>
   <QBXMLMsgsRq onError="stopOnError"> 
      <InventoryAdjustmentQueryRq metaData="MetaDataAndResponseData">
      </InventoryAdjustmentQueryRq>
   </QBXMLMsgsRq> 
</QBXML>
"""
qbxml_query1 = """

<?qbxml version="13.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
                <TransactionQueryRq metaData="ENUMTYPE" iterator="ENUMTYPE" iteratorID="UUIDTYPE">
<TxnTypeFilter >SalesOrder</TxnTypeFilter> <!-- required, may repeat -->
</TransactionQueryRq>
</QBXMLMsgsRq>
</QBXML>
"""

qbxml_query = """
<?qbxml version="6.0"?>
<QBXML>
        <QBXMLMsgsRq onError="stopOnError">
                <ItemInventoryQueryRq metaData="MetaDataAndResponseData">
				                                <ActiveStatus >ActiveOnly</ActiveStatus> <!-- optional -->

                        <IncludeRetElement >FullName</IncludeRetElement> <!-- optional, may repeat -->
						<IncludeRetElement >Name</IncludeRetElement> <!-- optional, may repeat -->
                </ItemInventoryQueryRq>
		</QBXMLMsgsRq>
</QBXML>

"""

response_string = sessionManager.ProcessRequest(ticket, qbxml_query)

# 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)
print("")
print(QBXML)
QBXMLMsgsRs = QBXML.find('QBXMLMsgsRs')
#InventoryAdjustmentQueryRs = QBXMLMsgsRs.getiterator("InventoryAdjustmentRet")
InventoryAdjustmentQueryRs = QBXMLMsgsRs.iter("InventoryAdjustmentRet")
for InvAdjRet in InventoryAdjustmentQueryRs:
    txnid = InvAdjRet.find('TxnID').text
    #memo = InvAdjRet.find('memo').text
   #  print(txnid)