def pretty_print(request_list):
    """
    send the same list of requests as you would to qb_request without the key or model name.  The requests will be formatted to qbxml and saved to files in the worker directory where they can be tested using the qbxml validator from intuit
    ex: 
    pretty_print.delay([
        (item_key, model_name, ('ItemReceiptAddRq', receipt_instance.quickbooks_request_tuple)),
        (item_key, model_name, ('ItemReceiptAddRq', receipt_instance.quickbooks_request_tuple))
    ])

    """
    qb = QuickBooks(**QB_LOOKUP)

    for entry in request_list:
        surrogate_key, model_name, request_body = entry
        request_type, request_dict = request_body
        qb.format_request(request_type, request_dictionary=request_dict, save_xml=True)
def quickbooks_query(query_type, query_params):
    """
    args are query type string and any query_params which should be a dict
    query types include 
    purchase_order, item, check
    """
    try:
        qb = QuickBooks(**QB_LOOKUP)
        qb.begin_session()
        results = qb.quickbooks_query(query_type, query_params)
        celery_app.send_task(
            'quickbooks.tasks.process_quickbooks_entities',
            queue='quickbooks', args=[query_type, list(results)], expires=1800
        )
    finally:
        del(qb)
def qb_requests(request_list=None, app='quickbooks'):
    """
    Always send a list of requests so we aren't opening and closing file more than necessary
    ex: 
    qb_requests.delay([
        (item_key, model_name, ('ItemReceiptAddRq', receipt_instance.quickbooks_request_tuple)),
        (item_key, model_name, ('ItemReceiptAddRq', receipt_instance.quickbooks_request_tuple))
    ])
    """
    try:
        qb = QuickBooks(**QB_LOOKUP)
        qb.begin_session()

        # process request list if it exists or just get open purchase orders
        if request_list:
            for entry in request_list:
                try:
                    surrogate_key, model_name, request_body = entry
                    request_type, request_dict = request_body
                    response = qb.call(request_type, request_dictionary=request_dict)
                    if surrogate_key and request_dict:
                        celery_app.send_task(
                            'quickbooks.tasks.process_response',
                            queue='quickbooks',
                            args=[surrogate_key, model_name, response, app], expires=1800
                        )
                except Exception as e:
                    logger.error(e)

        celery_app.send_task(
            'quickbooks.tasks.process_preferences',
            queue='quickbooks',
            args=[qb.get_preferences()], expires=1800
        )
    finally:
        # making sure to end session and close file
        del(qb)
        'filingDate': '20000101',
        'serial': 'ZZZ',
        'statusID': 40,
        'typeID': 50,
        'countryID': 60,
    }]
    patentTypes = [{
        'id': 50,
        'name': 'MMM',
    }]
    lawFirms = [{
        'id': 1000,
        'name': 'Hoffmann & Baron',
    }]

    qb = QuickBooks(applicationName=QUICKBOOKS_APPLICATION_NAME)
    qbr = HoffmannAndBaron(technologies, patents, patentTypes, lawFirms)
    lawFirmExpenses = qbr.load_expenses('a176102-parsed.csv')

    print '--- Technologies ---'
    qb.synchronize(technologies, 'Customer', dict(
        equal=qbr.equal_technology,
        parse_result=qbr.parse_customer,
        update_result=qbr.format_customer,
        format_result=qbr.format_customer,
        # expand_results=
        # collapse_packs=
        prompt_update=lambda pack, oldPack: True,
        prompt_save=lambda newPacks, newResults: True,
        # show_parse_error=
        # show_format_error=
def get_amount(expense):
    return expense['Amount']


def get_linkPack(expense):
    text = expense['CustomerRef']['FullName']
    lawFirmReference = re.search('Firm ID: ([^(]*)', text)


    linkTable = ''
    linkID = 0
    return linkTable, linkID


# Connect
qb = QuickBooks('Extract financial records')
# Load
bills = qb.call('BillQueryRq', {'IncludeLineItems': 1})


sqlalchemyURL = 'mssql+pyodbc://inteumCSdb'
Base = declarative_base()
engine = create_engine(sqlalchemyURL)
Base.metadata.reflect(engine)
tables = Base.metadata.tables
class Company(Base):
    __table__ = tables['COMPANY']
class Patent(Base):
    __table__ = tables['PATENTS']
class Technology(Base):
    __table__ = tables['TECHNOL']