Invoices with mixed status line item details can leave hanging encumbrances

Bug #1933859 reported by Tiffany Little
20
This bug affects 4 people
Affects Status Importance Assigned to Milestone
Evergreen
Confirmed
Undecided
Unassigned

Bug Description

Tested on 3.7ish master and first found in 3.6.2 production.

Scenario with some real-ish info: A line item is ordered with 9 copies. The EDI invoice is pulled in, and bills for 8 copies. At some point afterward, the items are delivered to the library. 8 copies are marked received, with 1 copy marked as canceled by vendor. The library checks the invoice--all good, 8 copies received, 8 copies billed for--and saves and closes the invoice. However, the PO still shows an amount as being encumbered.

When I tested this on a master server I found that: when the invoice entry is created and links up to the line item details (fund debits), it picks the first X number of line item detail ids, starting with the lowest.

To replicate and test this:

1. Create a purchase order
2. Create a lineitem with multiple copies and activate.
3. Create an invoice with that line item, and bill for 1 or more copies *less* than were ordered.
4. Go back and mark all the copies received except for 1 or more (however many you deducted from the invoice), starting from the bottom
5. Mark those unreceived copies as cancelled
6. Save and close the invoice.

You can check in the database that there are received copies that were *not* marked encumbrance=f even though the number of copies received matches the number of copies billed for. Here's a quick query for that:

select acqlid.*, acqfd.encumbrance from acq.lineitem_detail acqlid
left join acq.fund_debit acqfd on acqfd.id=acqlid.fund_debit
where lineitem=X order by acqlid.id;

I also tested this with just picking a copy in the middle to mark canceled, and got the same result.

This bug is problematic because the user has no way to know which fund debits are linked up and which ones aren't. Additionally, often multi-item line items are intended for multiple branches. So all line item details aren't necessarily equal; even if they knew "if you need to cancel one, cancel the bottom one," that isn't always a viable workaround because they might need to cancel a specific branch's copy.

My proposed fix would be that invoice entries aren't linked up to fund debits until an invoice is closed. The only way you can turn a fund debit into encumbrance=f *is* by closing an invoice, so until then there's no need for them to be linked up, and it can obviously cause some crossed wires in the system.

Having said all that, I haven't tested every iteration I could think of to narrow this down even further. Questions I might have that I haven't tested: Does it behave correctly if the copy you choose to cancel is the bottom one (the highest id)? Should the invoice linking be between line item detail based on whether a cancel reason is present, or against fund debit directly? What should happen if you bill for 8 copies but only 7 of them have fund debits? What if I marked the copies as received/cancelled *before* the invoice came in?*

*Even if that did work, this would still be a bug because often vendors will include the invoice in the box (meaning it's been generated), and thus send the EDI invoice before the library even receives the box. So EDI invoices linking up to lineitems can and regularly does happen before receiving.

description: updated
Revision history for this message
Tiffany Little (tslittle) wrote :

If anyone stumbles across this bug and needs a workaround. If you detach the line item from the invoice, save and close, and then add the line item to the invoice again, then save and close, the fund debits will shuffle themselves to be correct.

tags: added: acq-invoice acq-lineitem
Changed in evergreen:
status: New → Confirmed
Revision history for this message
Josh Stompro (u-launchpad-stompro-org) wrote :

I just spent 6 hours trying to figure this bug out. I don't think it has ever hit us before.

Here are my notes from tracing two EDI invoices being processed.

It seems to come down to not making use of the acq.lineitem_detail(fund_debit) link when searching for unlinked fund debits.

Search for acqfdeb acq.fund_debit and grabbed 313217 (Invoice.pm update_entry_debits -> find_entry_debits -> find_non_linked_debits line 607)

First Invoice 2023-01-24 7am
  Created a new acq.invoice_entry of 88197 (Invoice.pm build_invoice_impl line 89)
  Load lineitem 125361 (Invoice.pm uncancel_copies_as_needed line 429 )
  Picked lineitem detail 107886 (Invoice.pm uncancel_copies_as_needed line 445 )
  Uncancelled 107886 (Invoice.pm uncancel_copies_as_needed line 469 )
  Updated lineitem 125361 (Invoice.pm uncancel_copies_as_needed line 481 )
     Note, lineitem Detail 107886 has a link to acq.fund_debit 313216, but that isn't what gets picked later.
  Search for acqfdeb acq.fund_debit and grabbed 313217 (Invoice.pm update_entry_debits -> find_entry_debits -> find_non_linked_debits line 607)
     This seems to be the wrong acq.fund_debit, 313216 is the fund_debit in lineitem_detail 107886
  Update acq.fund_debit 313217 with invoice 88197 (update_entry_debits line 411)
  Search for acq.lineitem_detail with a fund debit of 313217 and flesh the eg_copy_id.
  Update asset.copy.update 3565799 to set the correct price (Invoice.pm update_copy_cost line 493)
    Note, this is not the copy associated with 107886

Second Invoice EDI 2023-02-13 7am
  Created a new acq.invoice_entry of 88654 (Invoice.pm build_invoice_impl line 89)
  Load lineitem 125361 (Invoice.pm uncancel_copies_as_needed line 429 )
  Picked lineitem detail 107886 (Invoice.pm uncancel_copies_as_needed line 445 )
    It picked the same lineitem_detail again, because the wrong acq.fund_debit invoice_entry was set last time.
  107886 isn't canceled, so this is skipped (Invoice.pm uncancel_copies_as_needed line 469 )
  Updated lineitem 125361 (Invoice.pm uncancel_copies_as_needed line 481 )
     Note, lineitem Detail 107886 has a link to acq.fund_debit 313216,
  Search for acqfdeb acq.fund_debit and grabbed 313216 (update_entry_debits -> find_entry_debits -> find_non_linked_debits line 607)
     This is the correct one.
  Update acq.fund_debit 313216 with invoice 88654 (update_entry_debits line 411)
  Search for acq.lineitem_detail with a fund debit of 313216 and flesh the eg_copy_id.
  Update asset.copy.update 3565798 to set the correct price (Invoice.pm update_copy_cost line 493)
    Note, this is now the copy associated with 107886

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.