From e6ba9a522955a76a196f02508aa4894b3244ec64 Mon Sep 17 00:00:00 2001
From: phasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Thu, 1 Apr 2010 05:14:47 +0000
Subject: [PATCH] Rough cut of 'open-ils.acq.lineitem.cancel', which "Cancels a
 lineitem, any of its detail entries and corresponding copies and call
 numbers, and potentially related holds (if the bib becomes empty)."  Need to
 smoke test it, add hold deletion if the bib becomes empty (and add an org
 unit setting for optionally deleting the bib as well), and check to see if
 volume deletion is being handled automatically by
 open-ils.cat.asset.copy.fleshed.batch.update

From my open-ils.acq.lineitem.cancel branch


git-svn-id: svn://svn.open-ils.org/ILS/trunk@16076 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 .../perlmods/OpenILS/Application/Acq/Lineitem.pm   | 61 ++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Lineitem.pm b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Lineitem.pm
index 92c00f22ce..1249c5a079 100644
--- a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Lineitem.pm
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Lineitem.pm
@@ -151,7 +151,68 @@ sub retrieve_lineitem_impl {
     return $li;
 }
 
+__PACKAGE__->register_method(
+	method => 'cancel_lineitem',
+	api_name	=> 'open-ils.acq.lineitem.cancel',
+	signature => {
+        desc => 'Cancels a lineitem, any of its detail entries and corresponding copies and call numbers, and potentially related holds (if the bib becomes empty).',
+        params => [
+            {desc => 'Authentication token', type => 'string'},
+            {desc => 'lineitem ID to cancel', type => 'number'},
+        ],
+        return => {desc => '1 on success, Event on error'}
+    }
+);
+
+sub cancel_lineitem {
+    my($self, $conn, $auth, $li_id, $cancel_reason) = @_;
+    my $e = new_editor(xact=>1, authtoken=>$auth);
+    return $e->die_event unless $e->checkauth;
+
+    $cancel_reason = $e->retrieve_acq_cancel_reason($cancel_reason);
+    if (!$cancel_reason) {
+        $e->rollback;
+        return new OpenILS::Event(
+            "BAD_PARAMS", 
+            "note" => "Provide cancel reason ID" # let client handle I18N for such events?
+        );
+    }
+
+    my $li = $e->retrieve_acq_lineitem($li_id)
+        or return $e->die_event;
+    $li->cancel_reason( $cancel_reason);
+
+    # cancel the attached lineitem_details and gather corresponding copies
+    my $lids = $e->search_acq_lineitem_detail({
+        lineitem => $li_id
+    }, {
+        flesh => 1,
+        flesh_fields => { acqlid => ['eg_copy_id'] }
+    });
+
+    my $copies = [];
+    for my $lid (@$lids) {
+        $lid->cancel_reason( $cancel_reason );
+        $e->update_acq_lineitem_detail( $lid );
+        $lid->eg_copy_id->isdeleted('t');
+        push @$copies, $lid->eg_copy_id;
+    }
+
+    # attempt to delete the gathered copies (will this may handle volume deletion and do hold retargeting for us?)
+    my $cat = OpenSRF::AppSession->create('open-ils.cat');
+    $cat->connect;
+    my $req = $cat->request('open-ils.cat.asset.copy.fleshed.batch.update', $auth, $copies);
+    my $result = $req->recv;
+    $cat->disconnect;
+    if ($result != 1) { # failed to delete copies
+        $e->rollback;
+        return $result;
+    }
 
+    $e->update_acq_lineitem($li) or return $e->die_event;
+    $e->commit;
+    return 0;
+}
 
 __PACKAGE__->register_method(
 	method => 'delete_lineitem',
-- 
2.11.0