def _set_location_in_invoices(self): """ Change Location value in invoice to Sales contained in client's suffix field TODO: make it scriptable to run into a cron job """ invoices = Invoice.filter(max_results=1000, order_by="DocNumber DESC", qb=self.client) for i in invoices: if i.DepartmentRef is None: c = Customer.get(i.CustomerRef.value, qb=self.client) if c.Suffix != "": location = self._get_location(sales_name=c.Suffix) if location is not None: # print(f"INVOICE {i.DocNumber} ASSIGNED TO {c.Suffix} LOCATION ID {location.Id}") i.DepartmentRef = location.to_ref() i.save(qb=self.client) else: print( f"[ERROR] Location IS NONE FOR {c.Suffix} OF {c}") else: print(f"[ERROR] c.Suffix IS NONE FOR {c}") print(f"set_location_in_invoices ended")
def test_delete(self): # First create an invoice invoice = Invoice() line = SalesItemLine() 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) # Then delete invoice_id = invoice.Id invoice.delete(qb=self.qb_client) query_invoice = Invoice.filter(Id=invoice_id, qb=self.qb_client) self.assertEqual([], query_invoice)
def verify_invoice(doc_number="", email=""): # checks QBO to ensure invoice number matches email provided # if match, returns QBO customer object attached to invoice # if mismatch, returns None refresh_stored_tokens() qb = fetch('qbclient') invoice_list = Invoice.filter(DocNumber=doc_number, qb=qb) if invoice_list: customers = Customer.filter(id=invoice_list[0].CustomerRef.value, qb=qb) else: return None if customers: if customers[0].PrimaryEmailAddr.Address.lower() == email.lower(): return customers[0] else: return None else: return None
def post_payment(doc_number="", amount=0, btcp_id=''): # post payment to QBO ''' doc_number: QBO invoice number amount: payment amount btcp_id: BTCPay invoice number ''' refresh_stored_tokens() qb = fetch('qbclient') # check if BTCPay is already in QBO as a pmt method pmt_method_list = PaymentMethod.filter(Name="BTCPay", qb=qb) try: # if BTCPay is already in QBO, set it as pmt method pmt_method = pmt_method_list[0] except IndexError: # if BTCPay is not in QBO, create it as pmt method new_pmt_method = PaymentMethod() new_pmt_method.Name = "BTCPay" new_pmt_method.save(qb=qb) # set newly created BTCPay pmt method as pmt method pmt_method_list = PaymentMethod.filter(Name="BTCPay", qb=qb) pmt_method = pmt_method_list[0] # check if QBO has asset acct for Bitcoin-BTCPay deposit_acct_list = Account.filter(Name="Bitcoin-BTCPay", qb=qb) try: # if Bitcoin-BTCPay is in QBO, set as deposit acct deposit_acct = deposit_acct_list[0] except IndexError: # if Bitcoin-BTCPay is not in QBO, create it as deposit acct new_acct = Account() new_acct.Name = "Bitcoin-BTCPay" new_acct.AccountSubType = "OtherCurrentAssets" new_acct.save(qb=qb) # set newly created Bitcoin-BTCPay acct as deposit acct deposit_acct_list = Account.filter(Name="Bitcoin-BTCPay", qb=qb) deposit_acct = deposit_acct_list[0] # pull list of invoice objects matching invoice number from QBO invoice_list = Invoice.filter(DocNumber=doc_number, qb=qb) try: # only one invoice can match the inv #, so pull it from list invoice = invoice_list[0] except IndexError: app.logger.warning(f'No such invoice exists: {doc_number}') return None else: # convert invoice object to linked invoice object linked_invoice = invoice.to_linked_txn() description = 'BTCPay: ' + btcp_id payment_line = PaymentLine() payment_line.Amount = amount payment_line.Description = description # attach linked invoice object to payment line object payment_line.LinkedTxn.append(linked_invoice) payment = Payment() payment.TotalAmt = amount payment.CustomerRef = invoice.CustomerRef # create deposit acct reference object from deposit acct object deposit_account_ref = Ref() deposit_account_ref.value = deposit_acct.Id # create pmt method reference object from pmt method object pmt_method_ref = Ref() pmt_method_ref.name = pmt_method.Name # attach pmt method ref, dep acct ref, and pmt line obj to pmt obj payment.PaymentMethodRef = pmt_method_ref payment.DepositToAccountRef = deposit_account_ref payment.Line.append(payment_line) payment.save(qb=qb) # save payment to temp redis store to fliter duplicates app.redis.set(btcp_id, 'payment', ex=21600) return "Payment Made: " + str(payment)