class Quickbooks: def __init__(self, qb_file=None, version=6.0, stop_on_error=True): # Absolute path to your quickbooks self.qb_file = qb_file self.app_name = "Quickbooks Python" self.ticket = None self.rc = RequestCreator() self.version = version self.xml_prefix = '''<?xml version="1.0" encoding="utf-8"?><?qbxml version="2.0"?>''' # This are important for me. using upto version 6 of the SDK. # You can check the version suported very easy by doing: # self.verion_suported() it will return the value. self.__queries = [ ("Account", 6.0), ("Bill", 6.0), ("Check", 6.0), ("Class", 6.0), ("Customer", 6.0), ("Host", 6.0), ("Invoice", 6.0), ("Item", 6.0), ("ItemReceipt", 6.0), ("ItemSalesTax", 6.0), ("ReceivePayment", 6.0), ("SalesOrder", 6.0), ("SalesReceipt", 6.0), ("StandardTerms", 6.0), ("Template", 6.0), ("Terms", 6.0), ("ToDo", 6.0), ("Vendor", 6.0), ('BillPaymentCheck', 6.0), ('BuildAssembly', 6.0), ('Charge', 6.0), ('Check', 6.0), ('Credit', 6.0), ('CreditCardCharge', 6.0), ('CreditCardCredit', 6.0), ('Deposits', 6.0), ('Employee', 6.0), ('Estimate', 6.0), ('Estimates', 6.0), ('Invoices', 6.0), ('ItemPayment', 6.0), ('ItemReceipt', 6.0), ('JournalEntry', 6.0), ('PriceLevel', 6.0), ('Purchase', 6.0), ('ReceivePayment', 6.0), ('Sales', 6.0), ('SalesReceipt', 6.0), ('Statement', 6.0), ('TimeTracking', 6.0), ('VendorCredit', 6.0), ] self.queries = [query[0] for query in self.__queries] def version_suported(self): """ iter over the SupportedQBXMLVersion and return the last value. """ for ver in ET.fromstring(self.get('Host')).iter("SupportedQBXMLVersion"): return ver.text def __create_name(self, object_name, operation, method): ''' Acording to chapter 3 page # 32: Naming are sliced in 3 parts object, operation + Rq ''' return "%s%s%s" % (object_name, operation.title(), method.title()) def __create_object(self, mode=2): """ It creates and returns a Session Object. """ import win32com.client session = win32com.client.Dispatch("QBXMLRP2.RequestProcessor") session.OpenConnection('', self.app_name) # This need to be mode, 3 to gues wich self.ticket = session.BeginSession(self.qb_file, 3) return session def __close_connection(self, session, ticket): session.EndSession(ticket) session.CloseConnection() def make_qbxml(self, query=None, payload=None): """ Outputs a valid QBXML if there is a payload it will be included in the output :param query is the full name of the object like CustomerAddRq :param payload is the optional payload , it is required when adding items. """ if payload: qb_request = payload else: qb_request = None qbxml_query = { 'QBXML': { 'QBXMLMsgsRq': { '@onError': "stopOnError", query: qb_request } } } data_xml = self.xml_soap(xmltodict.unparse(qbxml_query, full_document=False)) data_xml = xmltodict.unparse(qbxml_query, full_document=False) res = self.xml_prefix + data_xml return res def make_request(self, query='CustomerQuery', payload=None): session = self.__create_object() resp = None try: q = self.rc.create_name(object_name=query, operation='', method='') resp = session.ProcessRequest(self.ticket, self.make_qbxml(query=q, payload=payload)) except Exception, e: logging.error(e) finally:
class Quickbooks: def __init__(self, qb_file=None, stop_on_error=True): # Absolute path to your quickbooks self.qb_file = qb_file self.app_name = "Quickbooks Python" self.ticket = None self.rc = RequestCreator() # This are important for me. using upto version 6 of the SDK. # You can check the version suported very easy by doing: # self.verion_suported() it will return the value. self.queries = [ "Customer", "Account", "Invoice", "Bill", "Check", "Host", "Invoice", "ReceivePayment", "SalesReceipt", "Vendor", "ToDo", "Terms", "Template", "StandardTerms", "SalesOrder", "ItemSalesTax", "ItemReceipt", "Item", "Estimate", "Class", ] def version_suported(self): """ iter over the SupportedQBXMLVersion and return the last value. """ for ver in ET.fromstring(self.get('Host')).iter("SupportedQBXMLVersion"): version = ver.text return version def __create_name(self, object_name, operation, method): ''' Acording to chapter 3 page # 32: Naming are sliced in 3 parts object, operation + Rq ''' return "%s%s%s" %(object_name, operation.title(), method.title()) def __create_object(self, mode=2): """ It creates and returns a Session Object. """ session = win32com.client.Dispatch("QBXMLRP2.RequestProcessor") session.OpenConnection('', self.app_name) self.ticket = session.BeginSession(self.qb_file, 2) return session def __close_connection(self, session, ticket): session.EndSession(ticket) session.CloseConnection() def make_request(self, query='CustomerQuery'): session = self.__create_object() resp = None try: q = self.rc.create_name(object_name=query, operation='', method='') qbxml_query = """ <?qbxml version="6.0"?> <QBXML> <QBXMLMsgsRq onError="stopOnError"> <%s></%s> </QBXMLMsgsRq> </QBXML> """ %(q, q) resp = session.ProcessRequest(self.ticket, qbxml_query) except Exception, e: print e finally: