LP#1612752 - Add cancel_time to action.transit_copy and friends.
authorChris Sharp <csharp@georgialibraries.org>
Fri, 12 Aug 2016 17:41:25 +0000 (13:41 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Fri, 9 Jun 2017 15:27:08 +0000 (11:27 -0400)
It is useful to have keep action.transit_copy rows intact when
doing forensics on what happens after a staff member cancels a
transit.

Signed-off-by: Chris Sharp <csharp@georgialibraries.org>
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>

13 files changed:
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Booking.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/Circulate.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/Holds.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/Transit.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/CDBI/action.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/action.pm
Open-ILS/src/perlmods/lib/OpenILS/SIP/Item.pm
Open-ILS/src/sql/Pg/090.schema.action.sql
Open-ILS/src/sql/Pg/upgrade/XXXX-create-transit-cancel-time-column.sql [new file with mode: 0644]

index b1d287e..eaaa780 100644 (file)
@@ -2362,6 +2362,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
                        <field reporter:label="Prev Destination" name="prev_dest" reporter:datatype="org_unit"/>
                        <field reporter:label="Send Date/Time" name="source_send_time" reporter:datatype="timestamp"/>
                        <field reporter:label="Transited Copy" name="target_copy" reporter:datatype="link"/>
+                       <field reporter:label="Cancel Date/Time" name="cancel_time" reporter:datatype="timestamp"/>
                        <field reporter:label="Hold Transit" name="hold_transit_copy" oils_persist:virtual="true" reporter:datatype="link"/>
                </fields>
                <links>
@@ -7397,6 +7398,7 @@ SELECT  usr,
                        <field reporter:label="Transited Copy" name="target_copy" reporter:datatype="link"/>
                        <field reporter:label="Base Transit" name="transit_copy" oils_persist:virtual="true" reporter:datatype="link"/>
                        <field reporter:label="Prev Destination Library" name="prev_dest" reporter:datatype="org_unit"/>
+                       <field reporter:label="Cancel Date/Time" name="cancel_time" reporter:datatype="timestamp"/>
                </fields>
                <links>
                        <link field="transit_copy" reltype="might_have" key="id" map="" class="atc"/>
@@ -7432,6 +7434,7 @@ SELECT  usr,
                        <field reporter:label="Transited Copy" name="target_copy" reporter:datatype="link"/>
                        <field reporter:label="Base Transit" name="transit_copy" oils_persist:virtual="true" reporter:datatype="link"/>
                        <field reporter:label="Prev Destination Library" name="prev_dest" reporter:datatype="org_unit"/>
+                       <field reporter:label="Cancel Date/Time" name="cancel_time" reporter:datatype="timestamp"/>
                </fields>
                <links>
                        <link field="transit_copy" reltype="might_have" key="id" map="" class="atc"/>
@@ -11133,6 +11136,7 @@ SELECT  usr,
                        <field reporter:label="Send Date/Time" name="source_send_time" reporter:datatype="timestamp"/>
                        <field reporter:label="Transited Copy" name="target_copy" reporter:datatype="link"/>
                        <field reporter:label="Hold Transit" name="hold_transit_copy" oils_persist:virtual="true" reporter:datatype="link"/>
+                       <field reporter:label="Cancel Date/Time" name="cancel_time" reporter:datatype="timestamp"/>
                </fields>
                <links>
                        <link field="hold_transit_copy" reltype="might_have" key="id" map="" class="ahtc"/>
index b24a0db..bcfc81b 100644 (file)
@@ -411,7 +411,7 @@ sub fetch_hold_transit_by_hold {
 
     $transit = $self->simplereq(
         'open-ils.cstore',
-        'open-ils.cstore.direct.action.hold_transit_copy.search', { hold => $holdid } );
+        'open-ils.cstore.direct.action.hold_transit_copy.search', { hold => $holdid, cancel_time => undef } );
 
     $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', holdid => $holdid) unless $transit;
 
@@ -978,7 +978,7 @@ sub fetch_open_transit_by_copy {
     my($transit, $evt);
     $transit = $self->cstorereq(
         'open-ils.cstore.direct.action.transit_copy.search',
-        { target_copy => $copyid, dest_recv_time => undef });
+        { target_copy => $copyid, dest_recv_time => undef, cancel_time => undef });
     $evt = OpenILS::Event->new('ACTION_TRANSIT_COPY_NOT_FOUND') unless $transit;
     return ($transit, $evt);
 }
index bb38731..3727011 100644 (file)
@@ -1044,7 +1044,7 @@ sub capture_reservation {
 
         # need to transit the item ... is it already in transit?
         my $transit = $e->search_action_reservation_transit_copy(
-            {"reservation" => $res_id, "dest_recv_time" => undef}
+            {"reservation" => $res_id, "dest_recv_time" => undef, cancel_time => undef}
         )->[0];
 
         if (!$transit) { # not yet in transit
index a07c9be..69e5b4b 100644 (file)
@@ -428,12 +428,13 @@ sub delete_copy {
     $copy->edit_date('now');
     $editor->update_asset_copy($copy) or return $editor->event;
 
-    # Delete any open transits for this copy
+    # Cancel any open transits for this copy
     my $transits = $editor->search_action_transit_copy(
-        { target_copy=>$copy->id, dest_recv_time => undef } );
+        { target_copy=>$copy->id, dest_recv_time => undef, cancel_time => undef } );
 
     for my $t (@$transits) {
-        $editor->delete_action_transit_copy($t)
+        $t->cancel_time('now');
+        $editor->update_action_transit_copy($t)
             or return $editor->event;
     }
 
index 4d9f881..8f439e5 100644 (file)
@@ -1078,7 +1078,7 @@ sub copy_details {
     OpenILS::Application::Circ::Holds::flesh_hold_transits([$hold]) if $hold;
 
     my $transit = $e->search_action_transit_copy(
-        { target_copy => $copy_id, dest_recv_time => undef } )->[0];
+        { target_copy => $copy_id, dest_recv_time => undef, cancel_time => undef } )->[0];
 
     # find the most recent circulation for the requested copy,
     # be it active, completed, or aged.
index e12828c..21b875d 100644 (file)
@@ -176,7 +176,7 @@ sub run_method {
             my $res_id_list = [ map { $_->id } @$resources ];
             my $transit = $circulator->editor->search_action_reservation_transit_copy(
                 [
-                    { target_copy => $res_id_list, dest => $circulator->circ_lib, dest_recv_time => undef },
+                    { target_copy => $res_id_list, dest => $circulator->circ_lib, dest_recv_time => undef, cancel_time => undef },
                     { order_by => { artc => 'source_send_time' }, limit => 1 }
                 ]
             )->[0]; # Any transit for this barcode?
@@ -2208,7 +2208,7 @@ sub check_transit_checkin_interval {
     # capture the transit so we don't have to fetch it again later during checkin
     $self->transit(
         $self->editor->search_action_transit_copy(
-            {target_copy => $self->copy->id, dest_recv_time => undef}
+            {target_copy => $self->copy->id, dest_recv_time => undef, cancel_time => undef}
         )->[0]
     ); 
 
@@ -2397,7 +2397,7 @@ sub do_checkin {
     if( $self->copy and !$self->transit ) {
         $self->transit(
             $self->editor->search_action_transit_copy(
-                { target_copy => $self->copy->id, dest_recv_time => undef }
+                { target_copy => $self->copy->id, dest_recv_time => undef, cancel_time => undef }
             )->[0]
         ); 
     }
index 024a0e6..9c1ca72 100644 (file)
@@ -620,7 +620,7 @@ sub retrieve_holds {
 
         $hold->transit(
             $e->search_action_hold_transit_copy([
-                {hold => $hold->id},
+                {hold => $hold->id, cancel_time => undef},
                 {order_by => {ahtc => 'source_send_time desc'}, limit => 1}])->[0]
         );
 
@@ -807,7 +807,7 @@ sub cancel_hold {
 
             my $hid = $hold->id;
             $logger->warn("! canceling hold [$hid] that is in transit");
-            my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id},{idlist=>1})->[0];
+            my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id,cancel_time=>undef},{idlist=>1})->[0];
 
             if( $transid ) {
                 my $trans = $e->retrieve_action_transit_copy($transid);
@@ -1011,7 +1011,7 @@ sub update_hold_impl {
 
             # update the transit to reflect the new pickup location
             my $transit = $e->search_action_hold_transit_copy(
-                {hold=>$hold->id, dest_recv_time => undef})->[0]
+                {hold=>$hold->id, cancel_time => undef, dest_recv_time => undef})->[0]
                 or return $e->die_event;
 
             $transit->prev_dest($transit->dest); # mark the previous destination on the transit
@@ -1293,6 +1293,7 @@ sub _hold_status {
         my $transit    = $e->search_action_hold_transit_copy({
                             hold           => $hold->id,
                             target_copy    => $copy->id,
+                            cancel_time     => undef,
                             dest_recv_time => {'!=' => undef},
                          })->[0];
         my $start_time = ($transit) ? $transit->dest_recv_time : $hold->capture_time;
@@ -1972,7 +1973,7 @@ sub _reset_hold {
             # We don't want the copy to remain "in transit"
             $copy->status(OILS_COPY_STATUS_RESHELVING);
             $logger->warn("! reseting hold [$hid] that is in transit");
-            my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id},{idlist=>1})->[0];
+            my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id,cancel_time=>undef},{idlist=>1})->[0];
 
             if( $transid ) {
                 my $trans = $e->retrieve_action_transit_copy($transid);
@@ -2049,7 +2050,7 @@ sub flesh_hold_transits {
             $apputils->simplereq(
                 'open-ils.cstore',
                 "open-ils.cstore.direct.action.hold_transit_copy.search.atomic",
-                { hold => $hold->id },
+                { hold => $hold->id, cancel_time => undef },
                 { order_by => { ahtc => 'id desc' }, limit => 1 }
             )->[0]
         );
index fad7923..f627b99 100644 (file)
@@ -204,7 +204,7 @@ sub abort_transit {
     } elsif( $copy ) {
 
         $transit = $e->search_action_transit_copy(
-            { target_copy => $copy->id, dest_recv_time => undef })->[0];
+            { target_copy => $copy->id, dest_recv_time => undef, cancel_time => undef })->[0];
         return $e->event unless $transit;
     }
 
@@ -241,7 +241,9 @@ sub __abort_transit {
 
     my $holdtransit = $e->retrieve_action_hold_transit_copy($transit->id);
 
-    return $e->die_event unless $e->delete_action_transit_copy($transit);
+    # rather than deleting the transit row, set the cancel_time
+    $transit->cancel_time('now');
+    return $e->die_event unless $e->update_action_transit_copy($transit);
 
     # Only change the copy status if the copy status is "In Transit."
     if ($copy->status == OILS_COPY_STATUS_IN_TRANSIT) {
@@ -297,7 +299,7 @@ sub get_open_copy_transit {
     return $e->event unless $e->checkauth;
     return $e->event unless $e->allowed('VIEW_USER'); # XXX rely on editor perms
     my $t = $e->search_action_transit_copy(
-        { target_copy => $copyid, dest_recv_time => undef });
+        { target_copy => $copyid, dest_recv_time => undef, cancel_time => undef });
     return $e->event unless @$t;
     return $$t[0];
 }
@@ -316,7 +318,8 @@ sub fetch_transit_by_copy {
     my $t = $e->search_action_transit_copy(
         {
             target_copy => $copyid,
-            dest_recv_time => undef
+            dest_recv_time => undef,
+            cancel_time => undef
         }
     )->[0];
     return $e->event unless $t;
@@ -340,7 +343,7 @@ sub transits_by_lib {
     return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # eh.. basically the same permission
 
     my $order_by = {order_by => { atc => 'source_send_time' }};
-    my $search = { dest_recv_time => undef };
+    my $search = { dest_recv_time => undef, cancel_time => undef };
 
     if($end_date) {
         if($start_date) {
index 8a33f4f..5a59393 100644 (file)
@@ -124,7 +124,7 @@ __PACKAGE__->table('action_hold_transit_copy');
 __PACKAGE__->columns(Primary => 'id');
 __PACKAGE__->columns(Essential => qw/source dest persistant_transfer target_copy
                      source_send_time dest_recv_time prev_hop prev_dest
-                     copy_status hold/);
+                     cancel_time copy_status hold/);
 
 #-------------------------------------------------------------------------------
 
@@ -134,7 +134,7 @@ __PACKAGE__->table('action_reservation_transit_copy');
 __PACKAGE__->columns(Primary => 'id');
 __PACKAGE__->columns(Essential => qw/source dest persistant_transfer target_copy
                      source_send_time dest_recv_time prev_hop prev_dest
-                     copy_status reservation/);
+                     cancel_time copy_status reservation/);
 
 #-------------------------------------------------------------------------------
 
@@ -144,7 +144,7 @@ __PACKAGE__->table('action_transit_copy');
 __PACKAGE__->columns(Primary => 'id');
 __PACKAGE__->columns(Essential => qw/source dest persistant_transfer target_copy
                      source_send_time dest_recv_time prev_hop prev_dest
-                     copy_status/);
+                     cancel_time copy_status/);
 
 #-------------------------------------------------------------------------------
 
index 6e2ae2c..c6118f2 100644 (file)
@@ -420,6 +420,7 @@ sub build_hold_sort_clause {
                 COALESCE(dest_recv_time, source_send_time) AS moment
             FROM action.transit_copy
             WHERE target_copy = %d
+            AND cancel_time IS NULL
             ORDER BY moment DESC LIMIT 1
         ) UNION (
             SELECT
@@ -504,7 +505,8 @@ sub build_hold_sort_clause {
         WHERE
             atc.target_copy = %d AND
             (atc.dest = %d OR atc.source = %d) AND
-            atc.dest_recv_time >= NOW() - (SELECT value FROM go_home_interval)
+            atc.dest_recv_time >= NOW() - (SELECT value FROM go_home_interval) AND
+            atc.cancel_time IS NULL
     ) AS result FROM copy_has_not_been_home
 ) !, $cp->id, $cp->circ_lib, $cp->circ_lib);
         $joins .= " JOIN copy_has_not_been_home_even_to_idle ON (true) ";
index 2a98825..b32fb01 100644 (file)
@@ -170,7 +170,8 @@ sub fetch_transit {
         my $transit = $e->search_action_transit_copy([
             {
                 target_copy    => $copy->id,    # NOT barcode ($self->id)
-                dest_recv_time => undef
+                dest_recv_time => undef,
+                cancel_time => undef
             },
             {
                 flesh => 1,
index bed82b7..11ee4c3 100644 (file)
@@ -494,7 +494,8 @@ CREATE TABLE action.transit_copy (
        prev_hop                INT                             REFERENCES action.transit_copy (id) DEFERRABLE INITIALLY DEFERRED,
        copy_status             INT                             NOT NULL REFERENCES config.copy_status (id) DEFERRABLE INITIALLY DEFERRED,
        persistant_transfer     BOOL                            NOT NULL DEFAULT FALSE,
-       prev_dest       INT                             REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED
+       prev_dest               INT                             REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
+       cancel_time             TIMESTAMP WITH TIME ZONE
 );
 CREATE INDEX active_transit_dest_idx ON "action".transit_copy (dest); 
 CREATE INDEX active_transit_source_idx ON "action".transit_copy (source);
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX-create-transit-cancel-time-column.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX-create-transit-cancel-time-column.sql
new file mode 100644 (file)
index 0000000..e5cb0b1
--- /dev/null
@@ -0,0 +1,8 @@
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
+
+ALTER TABLE action.transit_copy
+       ADD COLUMN cancel_time TIMESTAMPTZ;
+
+COMMIT;