LP#978095 Cancel lineitem if there are no copies
authorLiam Whalen <liam.whalen@bc.libraries.coop>
Thu, 5 Nov 2015 18:34:22 +0000 (10:34 -0800)
committerLiam Whalen <liam.whalen@bc.libraries.coop>
Thu, 5 Nov 2015 18:34:22 +0000 (10:34 -0800)
Currently, it is possible to cancel the last copy on a lineitem without
also cancelling the lineitem itself.  This code cancels the lineitem
after the last copy has been canceled.

Signed-off-by: Liam Whalen <liam.whalen@bc.libraries.coop>
Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm

index d3a106e..e779c69 100644 (file)
@@ -3332,6 +3332,27 @@ sub cancel_lineitem_detail {
     # update these on the LI when we alter an LID?
     $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
 
+    my $query = { "select" => { 
+                    "acqlid" => [ 
+                        "lineitem", { 
+                            "column" => "lineitem", 
+                            "transform" => "count", 
+                            "alias" => "lineitem_count", 
+                            "aggregate" => "true" 
+                        }] 
+                    }, 
+                    "from" => { "jub" => "acqlid" }, 
+                    "where" => [{
+                        "+acqlid" => { "lineitem" => $lid->lineitem->id, "cancel_reason" => null }
+                        }, 
+                        {"purchase_order" => $lid->lineitem->purchase_order} 
+                     ]};
+    my $lineitem_query = $e->json_query($query);
+
+    if ($lineitem_query->lineitem_count == 0) {
+        cancel_lineitem_api($lid->lineitem->id, "Canceled: No Copies");
+    }
+
     return {"lid" => {$lid_id => {"cancel_reason" => $cancel_reason}}};
 }