From: Bill Erickson Date: Thu, 16 Oct 2014 20:17:16 +0000 (-0400) Subject: LP#1380709 Fund debit links to invoice entry X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=94ec1e175985be989f64ecbb24bfa3f78e97a023;p=evergreen%2Fmasslnc.git LP#1380709 Fund debit links to invoice entry Fund debits created by lineitem_details (copies) now track which invoice entry last touched the fund debit. With this, it's possible to say with certainty which invoice a fund_debit "belongs" to, which is particularly useful for reporting fund summary information on an invoice. Note this has no affect fund_debit's linked to invoice_item's, since they already link to the fund debits. Signed-off-by: Bill Erickson Signed-off-by: Jennifer Pringle Signed-off-by: Kathy Lussier --- diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml index 421b9675cb..85b485f334 100644 --- a/Open-ILS/examples/fm_IDL.xml +++ b/Open-ILS/examples/fm_IDL.xml @@ -7935,10 +7935,12 @@ SELECT usr, + + diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Invoice.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Invoice.pm index 3980e6215c..95071bbef2 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Invoice.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Invoice.pm @@ -287,6 +287,10 @@ sub rollback_entry_debits { # revert to the original estimated amount re-encumber $debit->encumbrance('t'); $debit->amount($lineitem->estimated_unit_price()); + + # debit is no longer "invoiced"; detach it from the entry; + $debit->clear_invoice_entry; + $e->update_acq_fund_debit($debit) or return $e->die_event; update_copy_cost($e, $debit) or return $e->die_event; # clear the cost } @@ -312,6 +316,11 @@ sub update_entry_debits { my $amount = entry_amount_per_item($entry); $debit->amount($amount); $debit->encumbrance('f'); + + # debit always reports the invoice_entry responsible + # for its most recent modification. + $debit->invoice_entry($entry->id); + $e->update_acq_fund_debit($debit) or return $e->die_event; # TODO: this does not reflect ancillary charges, like taxes, etc. @@ -473,14 +482,30 @@ sub amounts_spent_per_fund { return \@totals; } -# there is no direct link between invoice_entry and fund debits. -# when we need to retrieve the related debits, we have to do some searching +# find fund debits related to an invoice entry. sub find_entry_debits { - my($e, $entry, $encumbrance, $amount) = @_; + my($e, $entry, $encumbrance, $amount, $fallback) = @_; my $query = { select => {acqfdeb => ['id']}, - from => { + # sort received items to the front + order_by => {'acqlid' => ['recv_time']} + }; + + if ($encumbrance eq 'f' and !$fallback) { # previously invoiced + + # Debits which have been invoiced (encumbrance = f) will have a + # link to the last entry which affected them + + $query->{from} = {acqfdeb => 'acqlid'}; + $query->{where} = {'+acqfdeb' => {invoice_entry => $entry->id}}; + + } else { + + # For un-invoiced (or $fallback) debits, search for those + # that are linked to the entry via the lineitem. + + $query->{from} = { acqfdeb => { acqlid => { join => { @@ -494,17 +519,34 @@ sub find_entry_debits { } } } - }, - where => {'+acqfdeb' => {encumbrance => $encumbrance}}, - order_by => {'acqlid' => ['recv_time']}, # un-received items will sort to the end - limit => $entry->phys_item_count - }; + }; + + $query->{limit} = $entry->phys_item_count; + $query->{where} = {'+acqfdeb' => {encumbrance => $encumbrance}}; + } $query->{where}->{'+acqfdeb'}->{amount} = $amount if $amount; my $debits = $e->json_query($query); my $debit_ids = [map { $_->{id} } @$debits]; - return (@$debit_ids) ? $e->search_acq_fund_debit({id => $debit_ids}) : []; + + if (!@$debit_ids) { # no debits found + + # if a lookup for previously invoiced debits (encumbrance=f) + # returns zero results, it may be becuase the debits were + # created before the presence of the acq.fund_debit.invoice_entry + # column. Attempt to use the old-style lookup for these debits + # using the "$fallback" flag. + if (!$fallback and $encumbrance eq 'f') { + $logger->info( + "invoice: using debit fallback lookup for entry ".$entry->id); + return find_entry_debits($e, $entry, $encumbrance, $amount, 1); + } + + return []; + } + + return $e->search_acq_fund_debit({id => $debit_ids}); } diff --git a/Open-ILS/src/sql/Pg/200.schema.acq.sql b/Open-ILS/src/sql/Pg/200.schema.acq.sql index 0359ed7ab4..ac780c526c 100644 --- a/Open-ILS/src/sql/Pg/200.schema.acq.sql +++ b/Open-ILS/src/sql/Pg/200.schema.acq.sql @@ -852,6 +852,11 @@ CREATE INDEX ie_inv_idx on acq.invoice_entry (invoice); CREATE INDEX ie_po_idx on acq.invoice_entry (purchase_order); CREATE INDEX ie_li_idx on acq.invoice_entry (lineitem); +ALTER TABLE acq.fund_debit + ADD COLUMN invoice_entry INTEGER + REFERENCES acq.invoice_entry (id) + ON DELETE SET NULL; + CREATE TABLE acq.invoice_item_type ( code TEXT PRIMARY KEY, name TEXT NOT NULL, -- i18n-ize diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fund_debit_invoice_links.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fund_debit_invoice_links.sql new file mode 100644 index 0000000000..aca7918dbb --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fund_debit_invoice_links.sql @@ -0,0 +1,10 @@ +BEGIN; + +-- SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version); + +ALTER TABLE acq.fund_debit + ADD COLUMN invoice_entry INTEGER + REFERENCES acq.invoice_entry (id) + ON DELETE SET NULL; + +COMMIT;