3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use constant PULL_INTERVAL => 2;
23 use List::MoreUtils qw( uniq );
27 use C4::Output qw( output_html_with_http_headers );
29 use C4::Auth qw( get_template_and_user );
31 use C4::Reserves qw( ModReserveCancelAll );
33 use Koha::DateUtils qw( dt_from_string );
35 use DateTime::Duration;
38 my $startdate = $input->param('from');
39 my $enddate = $input->param('to');
40 my $theme = $input->param('theme'); # only used if allowthemeoverride is set
41 my $op = $input->param('op') || '';
42 my $borrowernumber = $input->param('borrowernumber');
43 my $reserve_id = $input->param('reserve_id');
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47 template_name => "circ/pendingreserves.tt",
50 flagsrequired => { circulate => "circulate_remaining_permissions" },
55 if ( $op eq 'cud-cancel_reserve' and $reserve_id ) {
56 my $hold = Koha::Holds->find( $reserve_id );
58 my $cancellation_reason = $input->param('cancellation-reason');
59 $hold->cancel({ cancellation_reason => $cancellation_reason });
60 push @messages, { type => 'message', code => 'hold_cancelled' };
62 } elsif ( $op =~ m|^mark_as_lost| ) {
63 my $hold = Koha::Holds->find( $reserve_id );
64 die "wrong reserve_id" unless $hold; # This is a bit rude, but we are not supposed to get a wrong reserve_id
65 my $item = $hold->item;
66 if ( $item and C4::Context->preference('CanMarkHoldsToPullAsLost') =~ m|^allow| ) {
67 my $patron = $hold->borrower;
68 C4::Circulation::LostItem( $item->itemnumber, "pendingreserves" );
69 if ( $op eq 'cud-mark_as_lost_and_notify' and C4::Context->preference('CanMarkHoldsToPullAsLost') eq 'allow_and_notify' ) {
70 my $library = $hold->branch;
71 my $letter = C4::Letters::GetPreparedLetter(
73 letter_code => 'CANCEL_HOLD_ON_LOST',
74 branchcode => $patron->branchcode,
75 lang => $patron->lang,
77 branches => $library->branchcode,
78 borrowers => $patron->borrowernumber,
79 items => $item->itemnumber,
80 biblio => $hold->biblionumber,
81 biblioitems => $hold->biblionumber,
82 reserves => $hold->unblessed,
86 my $from_address = $library->from_email_address;
88 C4::Letters::EnqueueLetter(
90 borrowernumber => $patron->borrowernumber,
91 message_transport_type => 'email',
92 from_address => $from_address,
95 unless ( $patron->notice_email_address ) {
96 push @messages, {type => 'alert', code => 'no_email_address', };
98 push @messages, { type => 'message', code => 'letter_enqueued' };
100 push @messages, { type => 'error', code => 'no_template_notice' };
104 if ( $item->homebranch ne $item->holdingbranch ) {
105 C4::Items::ModItemTransfer( $item->itemnumber, $item->holdingbranch, $item->homebranch, 'LostReserve' );
108 my $assignments = C4::Context->yaml_preference('UpdateItemWhenLostFromHoldList');
109 if ( $assignments ) {
111 while ( my ( $f, $v ) = each( %$assignments ) ) {
116 warn "Unable to modify item itemnumber=" . $item->itemnumber . ": $@" if $@;
119 } elsif ( not $item ) {
120 push @messages, { type => 'alert', code => 'hold_placed_at_biblio_level'};
121 } # else the url parameters have been modified and the user is not allowed to continue
125 my $today = dt_from_string;
128 $startdate =~ s/^\s+//;
129 $startdate =~ s/\s+$//;
130 $startdate = eval{dt_from_string( $startdate )};
132 unless ( $startdate ){
133 # changed from delivered range of 10 years-yesterday to 2 days ago-today
134 # Find two days ago for the default shelf pull start date, unless HoldsToPullStartDate sys pref is set.
135 $startdate = $today - DateTime::Duration->new( days => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL );
139 $enddate =~ s/^\s+//;
140 $enddate =~ s/\s+$//;
141 $enddate = eval{dt_from_string( $enddate )};
143 unless ( $enddate ) {
144 #similarly: calculate end date with ConfirmFutureHolds (days)
145 $enddate = $today + DateTime::Duration->new( days => C4::Context->preference('ConfirmFutureHolds') || 0 );
148 # building query parameters
151 'me.priority' => { '!=' => 0 },
153 'itembib.itemlost' => 0,
154 'itembib.withdrawn' => 0,
155 'itembib.notforloan' => 0,
156 'itembib.itemnumber' => { -not_in => \'SELECT itemnumber FROM branchtransfers WHERE datearrived IS NULL AND datecancelled IS NULL' }
160 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
161 my $startdate_iso = $dtf->format_date($startdate);
162 my $enddate_iso = $dtf->format_date($enddate);
163 if ( $startdate_iso && $enddate_iso ){
164 $where{'me.reservedate'} = [ -and => { '>=', $startdate_iso }, { '<=', $enddate_iso } ];
165 } elsif ( $startdate_iso ){
166 $where{'me.reservedate'} = { '>=', $startdate_iso };
167 } elsif ( $enddate_iso ){
168 $where{'me.reservedate'} = { '<=', $enddate_iso };
172 if ( !C4::Context->preference('AllowHoldsOnDamagedItems') ){
173 $where{'itembib.damaged'} = 0;
176 if ( C4::Context->only_my_library() ){
177 $where{'me.branchcode'} = C4::Context->userenv->{'branch'};
180 # get all distinct unfulfilled reserves
181 my $holds = Koha::Holds->search(
183 { join => 'itembib', distinct => 1, columns => qw[me.biblionumber] }
186 my @biblionumbers = $holds->get_column('biblionumber');
189 if ( $holds->count ) {
190 foreach my $item ( $holds->get_items_that_can_fill->as_list ) {
191 push @{ $all_items->{ $item->biblionumber } }, $item;
195 # patrons count per biblio
196 my $patrons_count = {
197 map { $_->{biblionumber} => $_->{patrons_count} } @{ Koha::Holds->search(
200 select => [ 'biblionumber', { count => { distinct => 'borrowernumber' } } ],
201 as => [qw( biblionumber patrons_count )],
202 group_by => [qw( biblionumber )]
208 my $holds_biblios_map = {
209 map { $_->{biblionumber} => $_->{reserve_id} } @{ Koha::Holds->search(
212 join => ['itembib', 'biblio'],
213 select => ['me.biblionumber', 'me.reserve_id'],
214 order_by => { -desc => 'priority' }
221 map { $_->biblionumber => $_ } @{ Koha::Holds->search(
222 { reserve_id => [ values %$holds_biblios_map ]},
224 prefetch => [ 'borrowernumber', 'itembib', 'biblio', 'item_group' ],
230 # make final holds_info array and fill with info
233 foreach my $bibnum ( @biblionumbers ){
234 # Skip this record if it's already been handled
235 next if $seen->{$bibnum};
236 $seen->{$bibnum} = 1;
239 my $items = $all_items->{$bibnum};
240 my $items_count = defined $items ? scalar @$items : 0;
241 my $pull_count = $items_count <= $patrons_count->{$bibnum} ? $items_count : $patrons_count->{$bibnum};
242 if ( $pull_count == 0 ) {
246 # get available item types for each biblio
248 if ( C4::Context->preference('item-level_itypes') ){
249 @res_itemtypes = uniq map { defined $_->itype ? $_->itype : () } @$items;
251 @res_itemtypes = Koha::Biblioitems->search(
252 { biblionumber => $bibnum, itemtype => { '!=', undef } },
253 { columns => 'itemtype',
256 )->get_column('itemtype');
258 $hold_info->{itemtypes} = \@res_itemtypes;
260 my $res_info = $all_holds->{$bibnum};
262 # get available values for each biblio
264 collections => 'ccode',
265 locations => 'location',
266 callnumbers => 'itemcallnumber',
267 enumchrons => 'enumchron',
268 copynumbers => 'copynumber',
269 barcodes => 'barcode',
270 holdingbranches => 'holdingbranch'
274 my ( $key, $field ) = each %$fields )
277 [ uniq map { defined $_->$field ? $_->$field : () } @$items ];
280 if ( $res_info->item_group ) {
281 $hold_info->{barcodes} = [ uniq map { defined $_->barcode && $_->item_group && ( $_->item_group->id == $res_info->item_group_id ) ? $_->barcode : () } @$items ];
285 $hold_info->{items_count} = $items_count;
288 $hold_info->{patrons_count} = $patrons_count->{$bibnum};
290 # number of items to pull
291 $hold_info->{pull_count} = $pull_count;
293 # get other relevant information
294 $hold_info->{patron} = $res_info->patron;
295 $hold_info->{item} = $res_info->item;
296 $hold_info->{biblio} = $res_info->biblio;
297 $hold_info->{hold} = $res_info;
298 $hold_info->{item_group} = $res_info->item_group;
300 push @holds_info, $hold_info;
304 todaysdate => $today,
307 holds_info => \@holds_info,
308 HoldsToPullStartDate => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL,
309 HoldsToPullEndDate => C4::Context->preference('ConfirmFutureHolds') || 0,
310 messages => \@messages,
313 output_html_with_http_headers $input, $cookie, $template->output;