def __init__(self): """ Init function Create a client for the API and load all the clients to speed up the retrival process. TODO now it loads only 1000 clients, extend to all """ super(PythonQuickBooks, self).__init__() self.client = self._create_client() self.customers = Customer.where("Active=True", order_by='DisplayName', max_results=1000, qb=self.client) self.trovati = 0 self.quanti = len(self.customers)
def create_order(sr): #Get the sales receipt sales_receipt = SalesReceipt() sr_body = { "domain": "QBO", "Balance": 0, "CustomerRef": { "name": "", "value": "6" }, "CustomerMemo": { "value": "" }, "sparse": "false", "Line": [{ #"Description": "Custom Design", "DetailType": "SalesItemLineDetail", #required "SalesItemLineDetail": { "Qty": 1, #"UnitPrice": 75, "ItemRef": { #required "value": "44" #black mulch (1-9) } }, "LineNum": 1, "Amount": 0, }], "CustomField": [{ "DefinitionId": "1", "Name": "Scout Credit", "Type": "StringType", "StringValue": "" }], "PaymentMethodRef": { "value": sr.payment_method_ref }, "DepositToAccountRef": { "value": sr.deposit_account_ref }, "CustomerMemo": { "value": sr.memo }, } #amys = Customer.filter(start_position=1, max_results=25, Active=True, FamilyName="Smith", qb=qb_client) #amys = Customer.query("SELECT * from Customers where FamilyName='Smith'", qb=qb_client) #amys = qb_client.query("select count(*) from Customer Where Active=true and DisplayName LIKE '%Smith'") #customer_street_number = sr.customer_street.split(' ')[0] query = "Active=true and DisplayName = '" + sr.customer_name.lower() + "'" try: customers_count = Customer.count(query, qb=qb_client) except ValidationException as ve: print(ve.detail) if customers_count == 0: # create a new customer? if AUTO_CREATE_CUSTOMERS: answer = yesno( "Customer [{}] not found residing on [{}]. Create the customer?" .format(sr.customer_name, sr.customer_street)) if answer: logging.info( "Creating the customer [{}] in quickbooks.".format( sr.customer_name)) customer = create_customer(sr) if customer is not None: customers_count = 1 else: logging.warning( "Customer [{}] not found. Not creating customer due to settings." .format(sr.customer_name)) if customers_count == 1: #we have found a customer customers = Customer.where("Active=true and DisplayName LIKE '%" + sr.customer_name.lower() + "'", qb=qb_client) customer_id = customers[0].Id customer_name = customers[0].DisplayName logging.debug("Customer id: {}".format(customer_id)) if customer_id is not None: check_and_update_customer_information(sr, customer_id) sr_body['CustomerRef']['value'] = customer_id sr_body['CustomerRef']['name'] = customer_name sr_body['Line'][0]['Amount'] = sr.total_price product_id = lookup_product(sr.product_name) sr_body['Line'][0]['SalesItemLineDetail']['ItemRef'][ 'value'] = product_id sr_body['Line'][0]['SalesItemLineDetail']['Qty'] = sr.product_qty sr_body['Line'][0]['SalesItemLineDetail'][ 'UnitPrice'] = sr.product_price logging.debug("Revised Customer: {}".format(sr_body)) #print("SR Body: {}".format(sr_body)) #post a new one sales_receipt = sales_receipt.from_json(sr_body) sales_receipt.TxnDate = sr.date #check for duplicates #get all customer sales receipts duplicate = False srs = SalesReceipt.filter(CustomerRef=customer_id, qb=qb_client) for asr in srs: #get item ref info item = Item.get( asr.Line[0].SalesItemLineDetail['ItemRef']['value'], qb=qb_client) #print(asr.Line[0].SalesItemLineDetail['ItemRef']['name']) asr_date = str(parse(asr.TxnDate).date()) sr_date = str(parse(sr.date).date()) if item.Name == sr.product_name \ and asr_date == sr_date \ and asr.Line[0].SalesItemLineDetail['Qty'] == sr.product_qty \ and float(asr.TotalAmt) == float(sr.total_price): logging.warning( "found a duplicate for this customer: {} on {} for item: {}, qty: {}, total: {}. skipping..." .format(sr.customer_name, sr.date, sr.product_name, sr.product_qty, sr.total_price)) duplicate = True #add the item if not duplicate: try: sales_receipt.save(qb_client) logging.debug("SentBody: {}".format(json.dumps(sr_body))) logging.info( "Successful entry of SalesReceipt: [{}] into quickbooks. OrderId:[{}], Item:[{}], Qty:[{}], Total:[{}]" .format(sr.customer_last, sales_receipt.Id, sr.product_name, sr.product_qty, sr.total_price)) except QuickbooksException as e: logging.error("An error saving the sales_receipt: {}".format( e.detail)) elif customers_count > 1: logging.warning( "More than one customer matches name: [{}]. Cannot process record. Skipping." .format(sr.customer_last)) else: print("no customer found")