def test_create(self): item = Item() item.Name = self.name item.Type = "Inventory" item.TrackQtyOnHand = True item.QtyOnHand = 10 item.InvStartDate = "2015-01-01" item.IncomeAccountRef = self.income_account.to_ref() item.ExpenseAccountRef = self.expense_account.to_ref() item.AssetAccountRef = self.asset_account.to_ref() item.save(qb=self.qb_client) query_item = Item.get(item.Id, qb=self.qb_client) self.assertEquals(query_item.Id, item.Id) self.assertEquals(query_item.Name, self.name) self.assertEquals(query_item.Type, "Inventory") self.assertEquals(query_item.TrackQtyOnHand, True) self.assertEquals(query_item.QtyOnHand, 10) self.assertEquals(query_item.IncomeAccountRef.value, self.income_account.Id) self.assertEquals(query_item.ExpenseAccountRef.value, self.expense_account.Id) self.assertEquals(query_item.AssetAccountRef.value, self.asset_account.Id)
def qbo_create_invoice(so, customer_id): client = create_qbc() customer_ref = Customer.get(customer_id, qb=client).to_ref() line_detail = SalesItemLineDetail() line_detail.UnitPrice = 100 # in dollars line_detail.Qty = 1 # quantity can be decimal item_ref = Item.get(35, qb=client).to_ref() line_detail.ItemRef = item_ref line = SalesItemLine() line.Amount = 100 # in dollars line.SalesItemLineDetail = line_detail line.DetailType = "SalesItemLineDetail" invoice = Invoice() invoice.CustomerRef = customer_ref invoice.Line = [line] invoice.save(qb=client)
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")
def process_data(): rows = [] subdivision_data = load_subdivision_data() sales_receipts = get_sales_receipts(PROCESSING_START_DATETIME, PROCESSING_END_DATETIME) for receipt in sales_receipts: for r in receipt.Line: if r.Id is not None: sr = MulchSalesReport() #customer c = Customer.get(receipt.CustomerRef.value, qb=qb_client) sr.customer_name = c.DisplayName sr.customer_first = c.GivenName sr.customer_last = c.FamilyName if c.ShipAddr is not None: sr.customer_street = c.ShipAddr.Line1 sr.customer_city = c.ShipAddr.City sr.customer_state = c.ShipAddr.CountrySubDivisionCode sr.customer_zip = c.ShipAddr.PostalCode raw_street_lookup = sr.customer_street.split(' ')[0:-1] street_lookup = ' '.join(raw_street_lookup).strip().upper() sr.subdivision = subdivision_data.get(street_lookup) if c.BillAddr is not None: sr.billing_street = c.BillAddr.Line1 sr.billing_city = c.BillAddr.City sr.billing_state = c.BillAddr.CountrySubDivisionCode sr.billing_zip = c.BillAddr.PostalCode sr.customer_phone = c.PrimaryPhone sr.customer_email = c.PrimaryEmailAddr #sales receipt sr.date = receipt.TxnDate sr.sr_record_id = receipt.DocNumber sr.date_modified = receipt.MetaData['LastUpdatedTime'] sr.sr_total_price = float(r.Amount) scout_credit = receipt.CustomField[0] if scout_credit.StringValue != '': credit = scout_credit.StringValue.split(':') if len(credit) > 1: sr.unit_sale = credit[0].upper() sr.scout_sale = credit[1].strip() elif len(credit) == 1: if re.findall(TROOP_KEYS, credit[0].lower()): sr.unit_sale = credit[0].upper() else: sr.scout_sale = credit[0].strip() else: sr.scout_sale = scout_credit.StringValue.strip() if receipt.CustomerMemo is not None: sr.sr_product_memo = receipt.CustomerMemo['value'] deposit_account = receipt.DepositToAccountRef if deposit_account is not None: sr.unit_income = lookup_payer_name(deposit_account.name) sr.sr_check_no = receipt.PaymentRefNum sr.payment_method_name = receipt.PaymentMethodRef #sales receipt line item qty = r.SalesItemLineDetail['Qty'] #sr.sr_product_qty = qty item = Item.get(r.SalesItemLineDetail['ItemRef']['value'], qb=qb_client) sr.sr_product_sku = item.Sku sr.sr_product_name = item.Name sr.sr_product_price = r.SalesItemLineDetail['UnitPrice'] #color item_name = sr.sr_product_name.lower() if re.findall(BROWN_KEYS, item_name): sr.brown_qty = qty sr.sr_product_color = 'Brown' elif re.findall(RED_KEYS,item_name): sr.red_qty = qty sr.sr_product_color = 'Red' elif re.findall(BLACK_KEYS,item_name): sr.black_qty = qty sr.sr_product_color = 'Black' elif re.findall(SPREAD_KEYS,item_name): sr.spread_qty = qty sr.spread_check_no = sr.sr_check_no sr.spread_sale_no = receipt.DocNumber sr.spread_total = sr.sr_total_price if receipt.CustomerMemo is not None: sr.spread_notes = receipt.CustomerMemo['value'] if re.findall('tbd', item.Name.lower()): sr.spread_notes = "{} : SPREAD DATE TBD".format(sr.spread_notes) else: sr.spread_date = lookup_spreading_date(item.Name) elif re.findall(DONATION_KEYS,item_name): sr.donate_total = sr.sr_total_price sr.sr_product_memo = '' sr.sr_bags_qty = sr.black_qty + sr.brown_qty + sr.red_qty #Reports #result = qb_client.get_report('TransactionListByTagType') #print(result) if len(rows)%20 == 0: print() print('.', end='') rows.append(sr) return rows