1 # ---------------------------------------------------------------
2 # Copyright (C) 2011 Merrimack Valley Library Consortium
3 # Jason Stephenson <jstephenson@mvlc.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
15 package OpenILS::WWW::PhoneList::Holds;
20 use OpenSRF::Utils::Logger qw/$logger/;
21 use OpenSRF::Utils::JSON;
22 use OpenILS::Application::AppUtils;
23 use OpenILS::Utils::Fieldmapper;
24 use OpenILS::Utils::CStoreEditor qw/:funcs/;
25 use OpenILS::WWW::PhoneList::Base;
27 my $U = 'OpenILS::Application::AppUtils';
30 our @ISA = ('OpenILS::WWW::PhoneList::Base');
41 my $self = $class->SUPER::new($args);
42 foreach my $element (keys %fields) {
43 $self->{_permitted}->{$element} = $fields{$element};
45 @{$self}{keys %fields} = values %fields;
46 $self->perms(['VIEW_USER', 'VIEW_HOLD', 'VIEW_HOLD_NOTIFICATION', 'CREATE_HOLD_NOTIFICATION']);
47 $self->addcount($args->{addcount}) if (defined($args->{addcount}));
48 $self->skipemail($args->{skipemail}) if (defined($args->{skipemail}));
49 my $columns = ['Name', 'Phone', 'Barcode'];
50 push(@{$columns}, 'Count') if ($self->addcount);
51 $self->columns($columns);
57 my $ou_id = $self->work_ou;
59 # Hold results in an array ref.
60 $self->{results} = [];
62 my $raw_query =<<" QUERY";
64 "select": { "au": [ "first_given_name", "family_name", "email" ],
66 "ahr": [ "phone_notify", "id", "email_notify" ] },
68 "from": { "au" : { "ac" : { "fkey":"card", "field":"id" },
69 "ahr": { "fkey":"id", "field":"usr" } } },
71 "where": { "+ahr": { "pickup_lib": { "in": {"select": {"aou":[{"transform":"actor.org_unit_descendants","column":"id","result_field":"id","alias":"id"}]},
73 "where":{"id":$ou_id}}},
75 "fulfillment_time":null,
76 "-and": [{"phone_notify": {"<>": null}}, {"phone_notify":{"<>":""}}],
77 "shelf_time":{"<>":null},
78 "capture_time":{"<>":null},
79 "current_copy":{"<>":null},
80 "id": { "not in": { "from":"ahn",
81 "select": { "ahn": [ "hold" ] },
82 "where": { "method":"phone" } } } } },
83 "order_by": { "ac": [ "barcode" ], "ahr": [ "phone_notify" ] }
87 my $q = OpenSRF::Utils::JSON->JSON2perl($raw_query);
88 my $e = new_editor(authtoken=>$self->authtoken);
89 my $info = $e->json_query($q);
90 if ($info && @$info) {
91 my ($bc, $pn,$count,$name, $skipme);
95 $skipme = 1; # Assume we skip until we have a notice w/out email.
96 foreach my $i (@$info) {
97 if ($i->{barcode} ne $bc || $i->{phone_notify} ne $pn) {
98 if ($count > 0 && $skipme == 0) {
101 my $out = [$name, $phone, $bc];
102 push(@$out, $count) if ($self->addcount);
103 push(@{$self->{results}}, $out);
106 if ($i->{first_given_name} eq 'N/A' || $i->{first_given_name} eq '') {
107 $name = $i->{family_name};
110 $name = $i->{first_given_name} . ' ' . $i->{family_name};
113 $pn = $i->{phone_notify};
114 $skipme = 1; # Assume we skip until we have a notice w/out email.
116 unless ($self->skipemail && $i->{email} && $i->{email_notify} eq 't') {
117 my $ahn = Fieldmapper::action::hold_notification->new;
118 $ahn->hold($i->{id});
119 $ahn->notify_staff($self->user);
120 $ahn->method('phone');
121 $ahn->note('PhoneList.pm');
122 $logger->activity("Attempting notification creation hold: " . $ahn->hold . " method: " . $ahn->method . " note: " . $ahn->note);
123 my $notification = $U->simplereq('open-ils.circ', 'open-ils.circ.hold_notification.create', $self->authtoken, $ahn);
124 if (ref($notification)) {
125 $logger->error("Error creating notification: " . $notification->{textcode});
128 $logger->activity("Created ahn: $notification");
130 #patron has at least 1 phone-only notice, so we print.
135 # Get that last one, since we only print when barcode and/or
137 if ($count > 0 && $skipme == 0) {
140 my $out = [$name, $phone, $bc];
141 push(@$out, $count) if ($self->addcount);
142 push(@{$self->{results}}, $out);
146 return scalar @{$self->{results}};
151 if (@{$self->{results}}) {
152 return shift @{$self->{results}};