waynerobinson / xeroizer

Xero accounting system API library.
http://waynerobinson.github.com/xeroizer
Other
238 stars 311 forks source link

Line items are recreated instead of being updated #480

Open StaverDmitry opened 5 years ago

StaverDmitry commented 5 years ago

This is a bug report; Expected: invoice.save! should update line items; What happens: invoice.save! deletes and recreates line items with new ids.

Hi! I'm not sure what's going on because following documentation and the way the gem is working it should work properly. Documentation says that It is recommended that you include LineItemIDs on update requests. If LineItemIDs are not included with line items in an update request then the line items are deleted and recreated. However it seems even though LineItemID is automatically included into a invoice.save! request, their ids are still changed. Here's the LIneItems part of the request I got from invoice.to_xml (some params including LineItemID are modified for security). As you can see the LineItemID is included here and I verified that it's correct.

  <LineItems>
    <LineItem>
      <ItemCode>my-item-code</ItemCode>
      <Description>description</Description>
      <UnitAmount>1</UnitAmount>
      <TaxType>TAX004</TaxType>
      <TaxAmount>0.0</TaxAmount>
      <LineAmount>1.0</LineAmount>
      <AccountCode>100</AccountCode>
      <Tracking>
        <TrackingCategory>
          <Name>TC_name</Name>
          <Option>TC_option</Option>
          <TrackingCategoryID>TC_ID</TrackingCategoryID>
        </TrackingCategory>
      </Tracking>
      <Quantity>1.0</Quantity>
      <LineItemId>myawesomeid-f8d1-590c-8036-212eeb63a8hc</LineItemId>
    </LineItem>
  </LineItems>

Here I just updated the UnitAmount of a single line item. Though seems like line items are always recreated even if nothing was changed.

If I'm not mistaken, following the code invoice.save! should directly pass this xml to the http request to XERO. So the LineItemID is included and it should just update those line items, but right after invoice.save! method, line items in this same invoice object get new line_item_id's meaning they were recreated.

RORingBBK commented 5 years ago

Is this the expected behaviour?

toyhammered commented 3 years ago

The expected behavior according the the docs is that It will update the line item (when a line_item_id) is supplied. This is still a bug as of writing this comment.

rjaus commented 3 years ago

This gem was created before Lineitems had Id's. Lineitem Id's were introduced as part of "Linked Transactions", which is used for Billable Expenses.

Docs: https://developer.xero.com/documentation/api/accounting/linkedtransactions#overview Billable Expenses Demo: https://www.youtube.com/watch?v=9gwHEZudsNA

Prior to this Linked Transactions, lineitems were only relevant to the invoice they appears on (for calculating totals). After Linked Transactions a lineitem could be referenced by another document (AccRec for example).

In terms of the actual fix, you are correct.

A lineitem with LineItemId should be updated, not recreated.

So I'd classify this as a feature / improvement to the SDK. Tho, considering Billable Expenses was released in 2015 (6 years ago), I can see why it would also be classified as a bug at this stage.

If someone would like to fix this, please submit a PR, happy to merge & release

troubadournz commented 1 year ago

I just hit this problem with my app and after talking it through with Xero, it seems the problem is that Xero's XML is case-sensitive. The key should be LineItemID not LineItemId.

troubadournz commented 1 year ago

Looking through the code it looks like the problem is that LineItemId has been set as a string not as a guid - which has the fix for Id vs ID.