helpers.get_most_populous_location
authorJason Etheridge <jason@esilibrary.com>
Wed, 23 Nov 2011 13:58:32 +0000 (08:58 -0500)
committerJason Etheridge <jason@esilibrary.com>
Thu, 5 Jan 2012 17:46:02 +0000 (12:46 -0500)
Given a call number id, returns a copy location object that has the most copies
for the given call number.  Put this with the SMS template for call numbers

Signed-off-by: Jason Etheridge <jason@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.sms_carriers.sql

index 5cf5488..a56a2cd 100644 (file)
@@ -125,6 +125,40 @@ $_TT_helpers = {
         }
     },
 
+    # given a call number, returns the copy location with the most copies
+    get_most_populous_location => sub {
+        my $acn_id = shift;
+
+        # FIXME - there's probably a more efficient way to do this with json_query/SQL
+        my $call_number = new_editor(xact=>1)->retrieve_asset_call_number([
+            $acn_id,
+            {
+                flesh => 1,
+                flesh_fields => {
+                    acn => ['copies']
+                }
+            }
+        ]);
+        my %location_count = (); my $winning_location; my $winning_total;
+        use Data::Dumper;
+        foreach my $copy (@{$call_number->copies()}) {
+            if (! defined $location_count{ $copy->location() }) {
+                $location_count{ $copy->location() } = 1;
+            } else {
+                $location_count{ $copy->location() } += 1;
+            }
+            if ($location_count{ $copy->location() } > $winning_total) {
+                $winning_total = $location_count{ $copy->location() };
+                $winning_location = $copy->location();
+            }
+        }
+
+        my $location = new_editor(xact=>1)->retrieve_asset_copy_location([
+            $winning_location, {}
+        ]);
+        return $location;
+    },
+
     # returns the org unit setting value
     get_org_setting => sub {
         my($org_id, $setting) = @_;
index 04871fc..1b7b259 100644 (file)
@@ -1402,6 +1402,7 @@ Title: [% title %]
 Author: [% author %]
 [%- END %]
 Call Number: [% target.label %]
+Location: [% helpers.get_most_populous_location( target.id ).name %]
 Library: [% target.owning_lib.name %]
 '
 );