]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/PhoneList/Holds.pm
Strip out spaces as well as - from phone numbers in PhoneList::Holds.
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / PhoneList / Holds.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2011 Merrimack Valley Library Consortium
3 # Jason Stephenson <jstephenson@mvlc.org>
4
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.
9
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;
16
17 use strict;
18 use warnings;
19
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;
26
27 my $U = 'OpenILS::Application::AppUtils';
28
29 BEGIN {
30     our @ISA = ('OpenILS::WWW::PhoneList::Base');
31 }
32
33 my %fields = (
34               addcount => 0,
35               skipemail => 0,
36              );
37
38 sub new {
39     my $class = shift;
40     my $args = shift;
41     my $self = $class->SUPER::new($args);
42     foreach my $element (keys %fields) {
43         $self->{_permitted}->{$element} = $fields{$element};
44     }
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);
52     return $self;
53 }
54
55 sub query {
56     my $self = shift;
57     my $ou_id = $self->work_ou;
58
59     # Hold results in an array ref.
60     $self->{results} = [];
61
62     my $raw_query =<<"    QUERY";
63 {
64 "select": { "au": [ "first_given_name", "family_name", "email" ],
65             "ac": [ "barcode" ],
66             "ahr": [ "phone_notify", "id", "email_notify" ] },
67
68 "from": { "au" : { "ac" : { "fkey":"card", "field":"id" },
69                    "ahr": { "fkey":"id", "field":"usr" } } },
70
71 "where": { "+ahr": { "pickup_lib": { "in": {"select": {"aou":[{"transform":"actor.org_unit_descendants","column":"id","result_field":"id","alias":"id"}]},
72                                             "from":"aou",
73                                             "where":{"id":$ou_id}}},
74                      "cancel_time":null,
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" ] }
84 }
85     QUERY
86
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);
92         $bc = "";
93         $pn = "";
94         $count = 0;
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) {
99                     my $phone = $pn;
100                     $phone =~ s/[- ]//g;
101                     my $out = [$name, $phone, $bc];
102                     push(@$out, $count) if ($self->addcount);
103                     push(@{$self->{results}}, $out);
104                     $count = 0;
105                 }
106                 if ($i->{first_given_name} eq 'N/A' || $i->{first_given_name} eq '') {
107                     $name = $i->{family_name};
108                 }
109                 else {
110                     $name = $i->{first_given_name} . ' ' . $i->{family_name};
111                 }
112                 $bc = $i->{barcode};
113                 $pn = $i->{phone_notify};
114                 $skipme = 1; # Assume we skip until we have a notice w/out email.
115             }
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});
126                 }
127                 else {
128                     $logger->activity("Created ahn: $notification");
129                 }
130                 #patron has at least 1 phone-only notice, so we print.
131                 $skipme = 0;
132             }
133             $count++;
134         }
135         # Get that last one, since we only print when barcode and/or
136         # phone changes.
137         if ($count > 0 && $skipme == 0) {
138             my $phone = $pn;
139             $phone =~ s/-//g;
140             my $out = [$name, $phone, $bc];
141             push(@$out, $count) if ($self->addcount);
142             push(@{$self->{results}}, $out);
143             $count = 0;
144         }
145     }
146     return scalar @{$self->{results}};
147 }
148
149 sub next {
150     my $self = shift;
151     if (@{$self->{results}}) {
152         return shift @{$self->{results}};
153     }
154     else {
155         return [];
156     }
157 }
158
159 1;