Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Invoice with New Line Item Preapends Instead of Appends #111

Closed
hello-woof opened this issue Apr 15, 2023 · 1 comment
Closed

Update Invoice with New Line Item Preapends Instead of Appends #111

hello-woof opened this issue Apr 15, 2023 · 1 comment

Comments

@hello-woof
Copy link

hello-woof commented Apr 15, 2023

Hey all, I've been trying to add a new line item to the bottom of an existing invoice, but every time I update the invoice it instead prepends it to the top. Is there something else deciding the ordering of the line items?

Expectations: The order in which invoice line items appear should be based on the order of the list:
Actual: All new line items are prepended to the line_items list
Version: xero-python:1.23.0

def update_invoice(invoice: Invoice, xero_tenant_id: str):
  accounting_api = AccountingApi(api_client)
  new_line = LineItem(
      description=f"Hello world!"
  )
  invoice.line_items.append(new_line)
  print(invoice.line_items)
  invoices = Invoices(invoices=[invoice])
  response = accounting_api.update_invoice(xero_tenant_id, invoice.invoice_id, invoices)
  print(response.invoices[0].line_items)

I tried just rebuilding the entire invoice and flipping the order or the line_items, but it puts the "newest" item I want at the bottom instead at the top:

def update_invoice(invoice: Invoice, xero_tenant_id: str):
  accounting_api = AccountingApi(api_client)
  new_line = LineItem(
      description=f"Hello world!"
  )
  line_items = [new_line ]
  for item in invoice.line_items[::-1]:
      item.line_item_id = None
      line_items.append(item)

  invoice.line_items = line_items
  print(invoice.line_items)
  invoices = Invoices(invoices=[invoice])
  response = accounting_api.update_invoice(xero_tenant_id, invoice.invoice_id, invoices)
  print(response.invoices[0].line_items)
@hello-woof
Copy link
Author

For others having this issue, I found removing the line ID kept the order correct

# This allows the order of line items to be respected
        line_item: LineItem
        for line_item in self.data.line_items:
            line_item.line_item_id = ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant