def test_create(self): invoice = Invoice() line = SaleItemLine() line.LineNum = 1 line.Description = "description" line.Amount = 100 line.SalesItemLineDetail = SalesItemLineDetail() item = Item.all(max_results=1, qb=self.qb_client)[0] line.SalesItemLineDetail.ItemRef = item.to_ref() invoice.Line.append(line) customer = Customer.all(max_results=1, qb=self.qb_client)[0] invoice.CustomerRef = customer.to_ref() invoice.CustomerMemo = CustomerMemo() invoice.CustomerMemo.value = "Customer Memo" invoice.save(qb=self.qb_client) query_invoice = Invoice.get(invoice.Id, qb=self.qb_client) self.assertEquals(query_invoice.CustomerRef.name, customer.DisplayName) self.assertEquals(query_invoice.CustomerMemo.value, "Customer Memo") self.assertEquals(query_invoice.Line[0].Description, "description") self.assertEquals(query_invoice.Line[0].Amount, 100.0)
def handle(self, *args, **options): live = options.get("live", False) print(f"This is LIVE: ", live) Invoice.objects.all().delete() client = get_quickbooks_client() customers = Customer.all(qb=client, max_results=1000) for customer in customers: chapter_name = customer.CompanyName if not chapter_name or not hasattr(customer, "CustomerTypeRef"): continue customer_type = customer.CustomerTypeRef["value"] if customer_type == "7300000000000214210": # This is a chapter if "Chapter" in chapter_name: chapter_name = customer.CompanyName.split(" Chapter")[0] elif customer_type == "7300000000000214211": # This is a candidate chapter # Candidate Chapter is normally in the name pass elif customer_type == "7300000000000220483": # This is a natoff continue elif customer_type == "7300000000000247061": # This is other continue else: # Maybe not chapter/candidate chapter, but other? continue print(f"Syncing: ", chapter_name) try: chapter = Chapter.objects.get(name=chapter_name) except Chapter.DoesNotExist: print(f" Chapter matching {chapter_name} does not exist") continue balance = customer.Balance print(" New balance: ", balance) if live: chapter.balance = balance chapter.balance_date = timezone.now() chapter.save() # Total emails are limited to 100 characters, need to be strategic # [regent, scribe, vice, treasurer] council_emails = chapter.get_current_officers_council_specific() # [email_regent, email_scribe, email_vice_regent, email_treasurer, email_corresponding_secretary, email, generic_emails = chapter.get_generic_chapter_emails() emails = [ # Tresurer council_emails[3], generic_emails[3], # Generic generic_emails[5], # Regent council_emails[0], generic_emails[0], # Vice council_emails[2], generic_emails[2], # Scribe council_emails[1], generic_emails[1], # Corsec generic_emails[4], ] emails = [email for email in emails if email] if not emails: print(" NO EMAILS") email_str = "" for email in emails: if not isinstance(email, str): email = email.email if not email: continue if (len(email_str + email) + 1) < 100 and email not in email_str: email_str = email_str + email + "," else: break email_str = email_str[:-1] print(" Current Email: ", customer.PrimaryEmailAddr.Address) if customer.PrimaryEmailAddr.Address != email_str: print(" New Email: ", email_str) if live: customer.PrimaryEmailAddr.Address = email_str customer.save(qb=client) else: print(" No new emails") if not balance > 0: continue invoices = QBInvoice.query( select= f"select * from Invoice where balance > '0' AND CustomerRef = '{customer.Id}'", qb=client, ) for invoice_res in invoices: invoice = QBInvoice.get(invoice_res.Id, qb=client) Invoice( link=invoice.InvoiceLink, due_date=invoice.DueDate, central_id=invoice.DocNumber, description="<br>".join([ f"{line.Description}; Line Amount: {line.Amount} <br>" for line in invoice.Line if line.DetailType == "SalesItemLineDetail" ]), total=invoice.Balance, chapter=chapter, ).save()