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::Overdues;
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;
26 my $U = 'OpenILS::Application::AppUtils';
29 our @ISA = ('OpenILS::WWW::PhoneList::Base');
40 my $self = $class->SUPER::new($args);
41 foreach my $element (keys %fields) {
42 $self->{_permitted}->{$element} = $fields{$element};
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);
51 # Results in an array ref.
52 $self->{results} = [];
59 my $ou_id = $self->work_ou;
61 # Need a CStoreEditor to run some queries:
62 my $e = new_editor(authtoken => $self->{authtoken});
64 # Get org_unit and descendant ids for the main search:
72 "transform"=>"actor.org_unit_descendants",
80 "where"=>{"id"=>$ou_id}
83 my $result = $e->json_query($query);
85 if (defined($result) && ref($result) eq 'ARRAY') {
86 foreach my $r (@$result) {
87 push (@$where, $r->{id});
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);
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
102 my $circs = $e->search_action_circulation(
106 checkin_time => undef,
107 due_date => $when->iso8601()
113 circ => ['usr', 'target_copy'],
115 acp => ['call_number'],
117 bre => ['simple_record']
120 ], {substream => 1});
122 # Add any results to our internal results array.
123 if (defined($circs) && ref($circs) eq 'ARRAY') {
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->
131 if (defined($stuff->{$barcode})) {
132 $stuff->{$barcode}->{titles} .= ':' . $title;
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;
141 foreach my $key (keys %$stuff) {
142 push (@{$self->{results}},
143 [ $stuff->{$key}->{name}, $stuff->{$key}->{phone},
144 $key, $stuff->{$key}->{titles} ]);
151 return scalar @{$self->{results}};
156 if (@{$self->{results}}) {
157 return shift @{$self->{results}};
164 # some helper functions:
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;