Handle cancelled (back-order) lineitems in EDI invoice
authorBill Erickson <berick@esilibrary.com>
Thu, 13 Dec 2012 14:44:55 +0000 (09:44 -0500)
committerDan Scott <dscott@laurentian.ca>
Wed, 23 Jan 2013 05:47:57 +0000 (00:47 -0500)
Receiving an invoice for a cancelled lineitem mean the lineitem is no
longer cancelled.  Identify such lineitems and uncancel them along with
the requested number of not-yet-invoiced copies.

This work flow is common for back-order items.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/EDI.pm

index 7a6b4f4..d898a8c 100644 (file)
@@ -761,6 +761,7 @@ sub create_acq_invoice_from_edi {
     }
 
     my @eg_inv_entries;
+    my @eg_inv_cancel_lis;
 
     $message->purchase_order($invoice->{purchase_order});
 
@@ -817,6 +818,9 @@ sub create_acq_invoice_from_edi {
         $eg_inv_entry->amount_paid($lineitem_price);
 
         push @eg_inv_entries, $eg_inv_entry;
+        push @eg_inv_cancel_lis, 
+            {lineitem => $li, quantity => $quantity} 
+            if $li->cancel_reason;
     }
 
     my @eg_inv_items;
@@ -898,6 +902,68 @@ sub create_acq_invoice_from_edi {
         }
     }
 
+    # if an invoiced lineitem is marked as cancelled 
+    # (e.g. back-order), invoicing the lineitem implies 
+    # we need to un-cancel it
+    for my $li_chunk (@eg_inv_cancel_lis) {
+        my $li = $li_chunk->{lineitem};
+        my $quantity = $li_chunk->{quantity};
+
+        $logger->info($log_prefix . 
+            "un-cancelling invoiced lineitem ". $li->id);
+         
+        # collect the LIDs, starting with those that are
+        # not cancelled (should not happen), followed by
+        # those that have keep-debits cancel_reasons, 
+        # followed by non-keep-debit cancel reasons.
+
+        my $lid_ids = $e->json_query({
+            select => {acqlid => ['id']},
+            from => {
+                acqlid => {
+                    acqcr => {type => 'left'},
+                    acqfdeb => {type => 'left'}
+                }
+            },
+            where => {
+                '+acqlid' => {lineitem => $li->id},
+                # not-yet invoiced copies
+                '+acqfdeb' => {encumbrance => 't'}
+            },
+            order_by => [{
+                class => 'acqcr',
+                field => 'keep_debits',
+                direction => 'desc'
+            }],
+            limit => $quantity
+        });
+
+        for my $lid_id (map {$_->{id}} @$lid_ids) {
+            my $lid = $e->retrieve_acq_lineitem_detail($lid_id);
+            next unless $lid->cancel_reason;
+
+            $lid->clear_cancel_reason;
+            unless ($e->update_acq_lineitem_detail($lid)) {
+                $logger->error($log_prefix . 
+                    "couldn't clear lid cancel reason: ". $e->die_event
+                );
+                return 0;
+            }
+        }
+
+        $li->clear_cancel_reason;
+        $li->state("on-order");
+        $li->edit_time('now'); 
+
+        unless ($e->update_acq_lineitem($li)) {
+            $logger->error($log_prefix . 
+                "couldn't clear li cancel reason: ". $e->die_event
+            );
+            return 0;
+        }
+    }
+
+
     $e->xact_commit;
     return 1;
 }