]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/PhoneList/Overdues.pm
Add PhoneList.pm.
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / PhoneList / Overdues.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::Overdues;
16
17 use strict;
18 use warnings;
19
20 use OpenSRF::Utils::Logger qw/$logger/;
21 use OpenILS::Application::AppUtils;
22 use OpenILS::Utils::Fieldmapper;
23 use OpenILS::Utils::CStoreEditor qw/:funcs/;
24 use OpenILS::WWW::PhoneList::Base;
25
26 my $U = 'OpenILS::Application::AppUtils';
27
28 BEGIN {
29     our @ISA = ('OpenILS::WWW::PhoneList::Base');
30 }
31
32 my %fields = (
33               skipemail => 0,
34               days => 14,
35              );
36
37 sub new {
38     my $class = shift;
39     my $args = shift;
40     my $self = $class->SUPER::new($args);
41     foreach my $element (keys %fields) {
42         $self->{_permitted}->{$element} = $fields{$element};
43     }
44     @{$self}{keys %fields} = values %fields;
45     $self->perms(['VIEW_USER', 'VIEW_CIRCULATIONS']);
46     $self->skipemail($args->{skipemail}) if (defined($args->{skipemail}));
47     $self->days($args->{days});
48     my $columns = ['Name', 'Phone', 'Barcode', 'Titles'];
49     $self->columns($columns);
50
51     # Results in an array ref.
52     $self->{results} = [];
53
54     return $self;
55 }
56
57 sub query {
58     my $self = shift;
59     my $ou_id = $self->work_ou;
60
61     # Need a CStoreEditor to run some queries:
62     my $e = new_editor(authtoken => $self->{authtoken});
63
64     # Get org_unit and descendant ids for the main search:
65     my $query =
66         {
67          "select" =>
68          {
69           "aou"=>
70           [
71            {
72             "transform"=>"actor.org_unit_descendants",
73             "column"=>"id",
74             "result_field"=>"id",
75             "alias"=>"id"
76            }
77           ]
78          },
79          "from"=>"aou",
80          "where"=>{"id"=>$ou_id}
81         };
82
83     my $result = $e->json_query($query);
84     my $where = [];
85     if (defined($result) && ref($result) eq 'ARRAY') {
86         foreach my $r (@$result) {
87             push (@$where, $r->{id});
88         }
89     } else {
90         $where = $ou_id;
91     }
92
93     # Set the due date to $self->days() ago.
94     my $when = DateTime->now();
95     $when->subtract(days => $self->days());
96     # All due dates are set to 23:59:59 in Evergreen.
97     $when->set(hour => 23, minute => 59, second => 59);
98
99     # This is what we're here for, the main search call to get fleshed
100     # circulation information for items that were due $where $when
101     # days ago.
102     my $circs = $e->search_action_circulation(
103         [
104          {
105           circ_lib => $where,
106           checkin_time => undef,
107           due_date => $when->iso8601()
108          },
109          {
110           flesh => 4,
111           flesh_fields =>
112           {
113            circ => ['usr', 'target_copy'],
114            au => ['card'],
115            acp => ['call_number'],
116            acn => ['record'],
117            bre => ['simple_record']
118           }
119          }
120         ], {substream => 1});
121
122     # Add any results to our internal results array.
123     if (defined($circs) && ref($circs) eq 'ARRAY') {
124         my $stuff = {};
125         foreach my $circ (@$circs) {
126             next if ($self->skipemail() && $circ->usr->email());
127             next unless($circ->usr->day_phone());
128             my $barcode = $circ->usr->card->barcode();
129             my $title = $circ->target_copy->call_number->record->simple_record->
130                 title();
131             if (defined($stuff->{$barcode})) {
132                 $stuff->{$barcode}->{titles} .= ':' . $title;
133             } else {
134                 my $phone = $circ->usr->day_phone();
135                 my $name = $self->_get_usr_name($circ);
136                 $stuff->{$barcode}->{phone} = $phone;
137                 $stuff->{$barcode}->{name} = $name;
138                 $stuff->{$barcode}->{titles} = $title;
139             }
140         }
141         foreach my $key (keys %$stuff) {
142             push (@{$self->{results}},
143                         [ $stuff->{$key}->{name}, $stuff->{$key}->{phone},
144                           $key, $stuff->{$key}->{titles} ]);
145         }
146     }
147
148     # Clean up?
149     $e->finish;
150
151     return scalar @{$self->{results}};
152 }
153
154 sub next {
155     my $self = shift;
156     if (@{$self->{results}}) {
157         return shift @{$self->{results}};
158     }
159     else {
160         return [];
161     }
162 }
163
164 # some helper functions:
165 sub _get_usr_name {
166     my $self = shift;
167     my $circ = shift;
168     my $first_name = $circ->usr->first_given_name();
169     my $last_name = $circ->usr->family_name();
170     return ($first_name eq 'N/A' || $first_name eq '') ? $last_name
171         : $first_name . ' ' . $last_name;
172 }
173
174 1;