def updateInvoice(data, is_sparse=False, retry_count=3): invoice_id = data['Id'] invoice = readInvoice(invoice_id) if invoice == None: if retry_count >= 0: updateInvoice(data, is_sparse, retry_count - 1) else: print('log unable to fetch invoice') return access_token = get_access_token() url = _get_url() headers = _get_headers(access_token) data['sparse'] = is_sparse data['SyncToken'] = invoice['Invoice']['SyncToken'] r = requests.post(url=url, json=data, headers=headers) if r.status_code == 200: return r.json() elif r.status_code == 5010: print(r.content) if retry_count > 0: print('failed update invoice due to sync token', data, retry_count) return updateInvoice(data, is_sparse, retry_count - 1) else: print('failed update invoice due to sync token', data, retry_count) else: print('failed update invoice', data, r.json(), r.status_code) pass
def createItem(item_name): data = { 'Name': item_name, 'Type': 'NonInventory', 'Taxable': True, "ExpenseAccountRef": { # "name": "Cost of Goods Sold", "value": "47" }, "IncomeAccountRef": { # "name": "Sales of Product Income", "value": "1" }, # "AssetAccountRef": { # "name": "Inventory Asset", # "value": "81" # } } access_token = get_access_token() url = _get_url() headers = _get_headers(access_token) r = requests.post(url=url, json=data, headers=headers) if r.status_code == 200: return r.json() #log invoice created with id else: print('invoice item creation failed') pass
def deleteBillInQB(bill_id): bill = readBillFromQB(bill_id) if not bill: logger.error( "log unable to fetch bill in deleteBillInQB for {0}".format( bill_id)) # print('log unable to fetch bill in deleteBillInQB ', bill_id) return access_token = get_access_token() url = _get_url() + '/' + bill_id + '?operation=delete' data = { 'Id': bill_id, 'SyncToken': bill.get('Bill').get('SyncToken'), } headers = _get_headers(access_token) resp = requests.post(url=url, json=data, headers=headers) if resp.status_code == 200: # confirm logic return else: logger.error("log delete bill API failed | {0} | {1} | {2}".format( bill_id, resp.json(), resp.status_code)) # print('log delete bill API failed', bill_id, resp.json(), resp.status_code) # confirm logic return {'error': "Not found"}
def createInvoice(data): access_token = get_access_token() url = _get_url() headers = _get_headers(access_token) r = requests.post(url=url, json=data, headers=headers) if r.status_code == 200: return r.json() else: print('failed invoice creation', data, r.json(), r.status_code) pass
def readInvoice(invoice_id): access_token = get_access_token() url = _get_url() + '/' + invoice_id + '?minorversion=51' headers = _get_headers(access_token) r = requests.get(url=url, headers=headers) if r.status_code == 200: return r.json() else: print('log read invoice API failed', invoice_id) return {'error': "Not found"}
def readBillFromQB(bill_id): url = _get_read_url(bill_id) headers = _get_headers(get_access_token()) resp = requests.get(url=url, headers=headers) if resp.status_code == 200: return resp.json() else: logger.warn( "log read bills API failed in readBillInQB() {0}".format(bill_id)) # print('log read bills API failed in readBillInQB()', bill_id) return None
def readBillPayment(payment_id): access_token = get_access_token() url = _get_url() + '/' + payment_id + '?minorversion=51' headers = _get_headers(access_token) r = requests.get(url=url, headers=headers) if r.status_code == 200: return r.json() else: logger.error('log read BillPayment API failed {0} | {1} | {2}'.format( payment_id, r.json(), r.status_code)) return {'error': "Not found"}
def createBillInQB(data): access_token = get_access_token() url = _get_url() headers = _get_headers(access_token) resp = requests.post(url=url, json=data, headers=headers) if resp.status_code == 200: return resp.json() else: logger.error( 'failed expense creation, data={0}, resp={1}, status_code={2}', data, resp.json(), resp.status_code) pass
def queryTaxCode(tax_code_name): access_token = get_access_token() sql = "select * from TaxCode where Name='{0}' and Active=true".format(tax_code_name) parsed_sql = urllib.parse.quote(sql) url = '{0}/v3/company/{1}/query?query={2}&minorversion=51'.format(settings.QBO_BASE_URL, settings.QBO_COMPANY_ID, parsed_sql) headers = _get_headers(access_token) r = requests.get(url = url, headers = headers) if r.status_code == 200: result = r.json() if len(result['QueryResponse'].keys()) == 0: return {'error': 'NO ITEM FOUND'} return {'TaxCode': result['QueryResponse']['TaxCode'][0]} else: print('Error: queryCustomer', tax_code_name)
def deleteInvoice(invoice_id): invoice = readInvoice(invoice_id) if invoice == None: print('log unable to fetch invoice', invoice_id) return access_token = get_access_token() url = _get_url() + '/' + invoice_id + '?operation=delete' data = {'Id': invoice_id, 'SyncToken': invoice['Invoice']['SyncToken']} headers = _get_headers(access_token) r = requests.post(url=url, json=data, headers=headers) if r.status_code == 200: return else: print('log delete invoice API failed', invoice_id, r.json(), r.status_code) return {'error': "Not found"}
def updateBillInQB(data, is_sparse=False, retry_count=3): bill_id = data.get('Id') bill_dict = readBillFromQB(bill_id) if not bill_dict: if retry_count >= 0: updateBillInQB(data, is_sparse, retry_count - 1) return else: # print('updateBillInQB unable to fetch bill') logger.error( "updateBillInQB unable to fetch bill for {0}".format(bill_id)) return access_token = get_access_token() url = _get_url() headers = _get_headers(access_token) data['sparse'] = is_sparse data['SyncToken'] = bill_dict.get('Bill').get('SyncToken') resp = requests.post(url=url, json=data, headers=headers) if resp.status_code == 200: return resp.json() elif resp.status_code == 501: print(resp.content) if retry_count > 0: logger.error( "updateBillInQB failed update bill due to sync token | {0} | {1}" .format(data, retry_count)) # print('updateBillInQB failed update bill due to sync token', data, retry_count) return updateBillInQB(data, is_sparse, retry_count - 1) else: logger.error( "updateBillInQB failed update bill due to sync token | {0} | {1}" .format(data, retry_count)) # print('updateBillInQB failed update bill due to sync token', data, retry_count) else: logger.error( "updateBillInQB failed update bill | {0} | {1} | {2}".format( data, resp.json(), resp.status_code)) # print('updateBillInQB failed update bill', data, resp.json(), resp.status_code) pass
def getVendor(name): access_token = get_access_token() sql = "select * from Vendor where DisplayName='{0}' and Active=true".format(name) parsed_sql = urllib.parse.quote(sql) url = '{0}/v3/company/{1}/query?query={2}&minorversion=52'.format( settings.QBO_BASE_URL, settings.QBO_COMPANY_ID, parsed_sql) headers = _get_headers(access_token) response = requests.get( url=url, headers=headers ) if response.status_code == 200: result = response.json() if result.get('QueryResponse'): return { 'Vendor': result.get('QueryResponse').get('Vendor')[0] } else: logger.error("getVendor error for {0}".format(name)) return {'error': 'NO Vendor FOUND'} else: logger.error('Error: Vendor {0}'.format(name))
def attachNoteToEntity(note_text, entity_id, entity_name): request_body = { "Note": note_text, "AttachableRef": [{ "IncludeOnSend": "false", "EntityRef": { "type": entity_name, "value": entity_id } }] } url = _get_note_url() access_token = get_access_token() headers = _get_headers(access_token) response = requests.post(url=url, json=request_body, headers=headers) if response.status_code == 200: logger.info("attachNoteToEntity | note added | {0} | {1} | {2}".format( note_text, entity_id, entity_name)) else: logger.error( "attachNoteToEntity | note failed | {0} | {1} | {2}".format( note_text, entity_id, entity_name))