LP#1380709 Fund debit links to invoice entry
authorBill Erickson <berickxx@gmail.com>
Thu, 16 Oct 2014 20:17:16 +0000 (16:17 -0400)
committerKathy Lussier <klussier@masslnc.org>
Thu, 18 Feb 2016 16:47:32 +0000 (11:47 -0500)
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 <berickxx@gmail.com>
Signed-off-by: Jennifer Pringle <jennifer.pringle@bc.libraries.coop>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Invoice.pm
Open-ILS/src/sql/Pg/200.schema.acq.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fund_debit_invoice_links.sql [new file with mode: 0644]

index 421b967..85b485f 100644 (file)
@@ -7935,10 +7935,12 @@ SELECT  usr,
                        <field reporter:label="Encumbrance" name="encumbrance" reporter:datatype="bool" />
                        <field reporter:label="Debit Type" name="debit_type" reporter:datatype="text" />
                        <field reporter:label="Create Time" name="create_time" reporter:datatype="timestamp" />
+                       <field reporter:label="Invoice Entry" name="invoice_entry" reporter:datatype="link" />
                </fields>
                <links>
                        <link field="fund" reltype="has_a" key="id" map="" class="acqf"/>
                        <link field="origin_currency_type" reltype="has_a" key="code" map="" class="acqct"/>
+                       <link field="invoice_entry" reltype="has_a" key="id" map="" class="acqie"/>
                </links>
                <permacrud xmlns="http://open-ils.org/spec/opensrf/IDL/permacrud/v1">
                        <actions>
index 3980e62..95071bb 100644 (file)
@@ -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});
 }
 
 
index 0359ed7..ac780c5 100644 (file)
@@ -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 (file)
index 0000000..aca7918
--- /dev/null
@@ -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;