From 1346aae17202b690c230b590b8117d791bfcd662 Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Wed, 23 Nov 2011 08:58:32 -0500 Subject: [PATCH] helpers.get_most_populous_location 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 --- .../lib/OpenILS/Application/Trigger/Reactor.pm | 34 ++++++++++++++++++++++ .../sql/Pg/upgrade/XXXX.schema.sms_carriers.sql | 1 + 2 files changed, 35 insertions(+) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm index 5cf5488df8..a56a2cd9fe 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm @@ -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) = @_; diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.sms_carriers.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.sms_carriers.sql index 04871fc538..1b7b259297 100644 --- a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.sms_carriers.sql +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.sms_carriers.sql @@ -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 %] ' ); -- 2.11.0