ej2 / python-quickbooks

A Python library for accessing the Quickbooks API.
MIT License
402 stars 194 forks source link

How to create an Invoice with a selected TaxRate #172

Closed epalm closed 4 years ago

epalm commented 4 years ago

I've been going around in circles with TaxAgency, TaxCode, TaxRate, TaxService, and TxnTaxDetail. I don't really understand which to use when. My goal is simply to create an Invoice with a selected tax rate (see dropdown box in screenshot below).

2019-11-16 20_29_28-Window

Assuming this is on the invoice level (not the line level), the Invoice class has a TxnTaxDetail field, on which you can set a TotalTax, TxnTaxCodeRef and list of TaxLines, but I'm not sure how to use these to just set a tax rate to use for the invoice itself.

I've looked through the integration tests for an example but couldn't find one. Any pointers? I'm probably just missing something obvious, I just need to pick a tax for the invoice I'm creating.

epalm commented 4 years ago

Answering my own question, I just queried an invoice with no tax rate applied, then manually applied the tax in QBO, saved it, and re-queried the same invoice, dumped both to json and looked for differences. I really should have just done this in the first place.

All I needed to add to the Invoice class was the following

invoice.TxnTaxDetail = TxnTaxDetail()
invoice.TxnTaxDetail.TxnTaxCodeRef = Ref()
invoice.TxnTaxDetail.TxnTaxCodeRef.value = 4

where 4 is the Id of the TaxCode. You can look for the tax code you want via

tax_codes = TaxCode.filter(Name=tax_name, max_results=1, qb=qb_client)
if not len(tax_codes):
    raise Exception(f"Tax code {tax_name} not found")
tax_code = tax_codes[0]
jasonhoward64 commented 1 year ago

FYI for others who are dealing with this.

It seems that if there are multiple taxes on an invoice, you have to create them all under a TaxCode using TaxService and then when you assign this taxcode to invoice.TxnTaxDetail.TxnTaxCodeRef.value, the individual tax lines won't show up on the Invoice in Quickbooks, but rather, one line with the total tax rate and tax amount.