JBAS-2024 Alt bib hold counts by rhrr
authorBill Erickson <berickxx@gmail.com>
Mon, 16 Apr 2018 21:47:32 +0000 (17:47 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Recover stock hold counts code to reduce merge confusion and add a new
handler for calculating hold counts per bib record using the new
materialized reporter.hold_request_record table.  This gives us copy,
etc. -level hold counts back.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/Holds.pm

index 43f4bbd..1a4318f 100644 (file)
@@ -4262,6 +4262,10 @@ sub rec_hold_count {
     my($self, $conn, $target_id, $args) = @_;
     $args ||= {};
 
+    # KCLS JBAS-2024
+    return rec_hold_count_via_rhrr($target_id, $args) 
+        unless $self->api_name =~ /mmr/;
+
     my $mmr_join = {
         mmrsm => {
             field => 'source',
@@ -4298,51 +4302,47 @@ sub rec_hold_count {
             '+ahr' => {
                 cancel_time => undef,
                 fulfillment_time => undef,
-# KCLS JBAS-826 -- make the record hold counts query faster by only
-# counting bib-level holds (which represent >99% of KCLS' holds).
-                hold_type => 'T',
-                target => $target_id
-#                '-or' => [
-#                    {
-#                        '-and' => {
-#                            hold_type => [qw/C F R/],
-#                            target => {
-#                                in => {
-#                                    select => {acp => ['id']},
-#                                    from => { acp => $cn_join }
-#                                }
-#                            }
-#                        }
-#                    },
-#                    {
-#                        '-and' => {
-#                            hold_type => 'V',
-#                            target => {
-#                                in => {
-#                                    select => {acn => ['id']},
-#                                    from => {acn => $bre_join}
-#                                }
-#                            }
-#                        }
-#                    },
-#                    {
-#                        '-and' => {
-#                            hold_type => 'P',
-#                            target => {
-#                                in => {
-#                                    select => {bmp => ['id']},
-#                                    from => {bmp => $bre_join}
-#                                }
-#                            }
-#                        }
-#                    },
-#                    {
-#                        '-and' => {
-#                            hold_type => 'T',
-#                            target => $target_id
-#                        }
-#                    }
-#                ]
+                '-or' => [
+                    {
+                        '-and' => {
+                            hold_type => [qw/C F R/],
+                            target => {
+                                in => {
+                                    select => {acp => ['id']},
+                                    from => { acp => $cn_join }
+                                }
+                            }
+                        }
+                    },
+                    {
+                        '-and' => {
+                            hold_type => 'V',
+                            target => {
+                                in => {
+                                    select => {acn => ['id']},
+                                    from => {acn => $bre_join}
+                                }
+                            }
+                        }
+                    },
+                    {
+                        '-and' => {
+                            hold_type => 'P',
+                            target => {
+                                in => {
+                                    select => {bmp => ['id']},
+                                    from => {bmp => $bre_join}
+                                }
+                            }
+                        }
+                    },
+                    {
+                        '-and' => {
+                            hold_type => 'T',
+                            target => $target_id
+                        }
+                    }
+                ]
             }
         }
     };
@@ -4392,6 +4392,52 @@ sub rec_hold_count {
     return new_editor()->json_query($query)->[0]->{count};
 }
 
+# KCLS JBAS-2024
+sub rec_hold_count_via_rhrr {
+    my ($target_id, $args) = @_;
+    $args ||= {};
+
+    # TODO: add metarecord holds count support by counting holds
+    # in rhrr that link to the master record.
+    # TODO: post to LP
+
+    my $query = {
+        select => {rhrr => [
+            {column => 'id', transform => 'count', alias => 'count'}
+        ]},
+        from => {rhrr => 'ahr'},
+        where => {
+            '+rhrr' => {bib_record => $target_id},
+            '+ahr' => {
+                cancel_time => undef,
+                fulfillment_time => undef
+            }
+        }
+    };
+
+    if (my $pld = $args->{pickup_lib_descendant}) {
+
+        my $top_ou = 
+            new_editor()->search_actor_org_unit(
+                {parent_ou => undef})->[0];
+
+        $query->{where}->{'+ahr'}->{pickup_lib} = {
+            in => {
+                select  => {aou => [{ 
+                    column => 'id', 
+                    transform => 'actor.org_unit_descendants', 
+                    result_field => 'id' 
+                }]},
+                from    => 'aou',
+                where   => {id => $pld}
+            }
+        } if ($pld != $top_ou->id);
+    }
+
+    return new_editor()->json_query($query)->[0]->{count};
+}
+
+
 # A helper function to calculate a hold's expiration time at a given
 # org_unit. Takes the org_unit as an argument and returns either the
 # hold expire time as an ISO8601 string or undef if there is no hold