LP#978095 Moved line item cancel code to new sub
authorLiam Whalen <liam.whalen@bc.libraries.coop>
Fri, 6 Nov 2015 15:17:06 +0000 (07:17 -0800)
committerLiam Whalen <liam.whalen@bc.libraries.coop>
Fri, 6 Nov 2015 15:17:06 +0000 (07:17 -0800)
The line item cancel code when there are no line item details needs a
valid $e object.  However, there was no $e object in the sub where this
code currently exists.  So, I moved the code to its own sub and passed
in a valid $e object to the new sub.

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

index 2a85b18..051637f 100644 (file)
@@ -3257,6 +3257,8 @@ sub cancel_lineitem_detail_api {
     my $result = cancel_lineitem_detail($mgr, $lid_id, $cancel_reason) or
         return $e->die_event;
 
+    no_lid_cancel_lineitem($e, $lid_id);
+
     if (not_cancelable($result)) {
         $e->rollback;
         return $result;
@@ -3332,50 +3334,70 @@ 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" => { 
-                        "join" => { "acqcr" => {} }
-                    }
+    return {"lid" => {$lid_id => {"cancel_reason" => $cancel_reason}}};
+}
+
+sub no_lid_cancel_lineitem {
+    my ($e, $lid_id) = @_;
+    my $lid = $mgr->editor->retrieve_acq_lineitem_detail([
+        $lid_id, {
+            "flesh" => 2,
+            "flesh_fields" => {
+                "acqlid" => ["lineitem","cancel_reason"],
+                "jub" => ["purchase_order"]
+            }
+        }
+    ]) or return 0;
+
+    #Get a count of lineitem details that exist on the lineitem
+    #that are not cancelled, or lineitem details that are
+    #cancelled but having keep_debits = true.
+    my $query = { "select" => {
+        "acqlid" => [
+            "lineitem", {
+                "column" => "lineitem",
+                "transform" => "count",
+                "alias" => "lineitem_count",
+                "aggregate" => "true"
+            }]
+        },
+        "from" => {
+            "jub" => {
+                "acqlid" => {
+                    "join" => { "acqcr" => {} }
                 }
-            }, 
-            "where" => [{
-                    "+acqlid" => { 
-                        "lineitem" => $lid->lineitem->id, 
-                        "cancel_reason" => null 
-                    }
-                }, {
-                    "purchase_order" => $lid->lineitem->purchase_order
-                }, {
-                    "-or" => [{
-                            "+acqlid" => { 
-                                "lineitem" => $lid->lineitem->id, 
-                                "cancel_reason" => { "!=" => null }
-                            }
-                        }, {
-                            "+acqcr" => "keep_debits"
-                        }, {
-                            "purchase_order" => $lid->lineitem->purchase_order
-                        }]
+            }
+        },
+        "where" => [{
+                "+acqlid" => {
+                    "lineitem" => $lid->lineitem->id,
+                    "cancel_reason" => null
                 }
-            ]};
-    
-    my $lineitem_query = $e->json_query($query);
+            }, {
+                "purchase_order" => $lid->lineitem->purchase_order
+            }, {
+                "-or" => [{
+                        "+acqlid" => {
+                            "lineitem" => $lid->lineitem->id,
+                            "cancel_reason" => { "!=" => null }
+                        }
+                    }, {
+                        "+acqcr" => "keep_debits"
+                    }, {
+                        "purchase_order" => $lid->lineitem->purchase_order
+                    }]
+            }
+        ]
+    };
+
+    my $lineitem_query = $e->json_query($query)
+        or return 0;
 
     if ($lineitem_query->lineitem_count == 0) {
-        cancel_lineitem_api($lid->lineitem->id, "Canceled: No Copies");
+        return cancel_lineitem_api($lid->lineitem->id, "Canceled: No Copies");
     }
 
-    return {"lid" => {$lid_id => {"cancel_reason" => $cancel_reason}}};
+    return 0;
 }
 
 __PACKAGE__->register_method(