3 # Copyright 2000-2002 Katipo Communications
4 # parts copyright 2010 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use C4::Output qw( output_html_with_http_headers );
25 use C4::Auth qw( get_template_and_user );
26 use C4::Items qw( ModItemTransfer );
27 use Date::Calc qw( Date_to_Days Today );
28 use C4::Reserves qw( ModReserve ModReserveCancelAll );
29 use Koha::DateUtils qw( dt_from_string );
30 use Koha::BiblioFrameworks;
34 use Koha::BackgroundJob::BatchCancelHold;
38 my $item = $input->param('itemnumber');
39 my $borrowernumber = $input->param('borrowernumber');
40 my $fbr = $input->param('fbr') || '';
41 my $tbr = $input->param('tbr') || '';
42 my $all_branches = $input->param('allbranches') || '';
43 my $cancelall = $input->param('cancelall');
44 my $tab = $input->param('tab');
45 my $cancelBulk = $input->param('cancelBulk');
47 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
49 template_name => "circ/waitingreserves.tt",
52 flagsrequired => { circulate => "circulate_remaining_permissions" },
56 my $default = C4::Context->userenv->{'branch'};
58 my $transfer_when_cancel_all = C4::Context->preference('TransferWhenCancelAllWaitingHolds');
59 $template->param( TransferWhenCancelAllWaitingHolds => 1 ) if $transfer_when_cancel_all;
62 # if we have a return from the form we cancel the holds
64 my $res = cancel( $item, $borrowernumber, $fbr, $tbr );
65 push @cancel_result, $res if $res;
68 if ( C4::Context->preference('IndependentBranches') ) {
71 $template->param( all_branches_link => '/cgi-bin/koha/circ/waitingreserves.pl' . '?allbranches=1' )
74 $template->param( all_branches => 1 ) if $all_branches;
77 my $reason = $input->param("cancellation-reason");
78 my @hold_ids = split( ',', scalar $input->param("ids") );
81 hold_ids => \@hold_ids,
83 my $job_id = Koha::BackgroundJob::BatchCancelHold->new->enqueue($params);
91 my (@reserve_loop, @over_loop);
92 # FIXME - Is priority => 0 useful? If yes it must be moved to waiting, otherwise we need to remove it from here.
93 my $holds = Koha::Holds->waiting->search({ priority => 0, ( $all_branches ? () : ( branchcode => $default ) ) }, { order_by => ['waitingdate'] });
95 # get reserves for the branch we are logged into, or for all branches
97 my $today = Date_to_Days(&Today);
99 while ( my $hold = $holds->next ) {
100 next unless $hold->waitingdate;
102 my ( $expire_year, $expire_month, $expire_day ) = split (/-/, $hold->expirationdate);
103 my $calcDate = Date_to_Days( $expire_year, $expire_month, $expire_day );
105 if ($today > $calcDate) {
107 my $res = cancel( $hold->item->itemnumber, $hold->borrowernumber, $hold->item->holdingbranch, $hold->item->homebranch, !$transfer_when_cancel_all );
108 push @cancel_result, $res if $res;
111 push @over_loop, $hold;
114 push @reserve_loop, $hold;
119 my $holds_with_cancellation_requests = Koha::Holds->waiting->filter_by_has_cancellation_requests;
121 $template->param(cancel_result => \@cancel_result) if @cancel_result;
124 reserveloop => \@reserve_loop,
125 reservecount => scalar @reserve_loop,
126 overloop => \@over_loop,
127 overcount => scalar @over_loop,
128 cancel_reqs_count => $holds_with_cancellation_requests->count,
129 cancel_reqs => $holds_with_cancellation_requests,
130 show_date => dt_from_string,
134 # Checking if there is a Fast Cataloging Framework
135 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
137 if ($item && $tab eq 'holdsover' && !@cancel_result) {
138 print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl#holdsover");
139 } elsif ($cancelall) {
140 print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl");
142 output_html_with_http_headers $input, $cookie, $template->output;
148 my ($item, $borrowernumber, $fbr, $tbr, $skip_transfers ) = @_;
150 my $transfer = $fbr ne $tbr; # XXX && !$nextreservinfo;
152 return if $transfer && $skip_transfers;
154 my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
156 # if the document is not in his homebranch location and there is not reservation after, we transfer it
157 if ($transfer && !$nextreservinfo) {
158 ModItemTransfer( $item, $fbr, $tbr, 'CancelReserve' );
160 # if we have a result
161 if ($nextreservinfo) {
163 my $patron = Koha::Patrons->find( $nextreservinfo );
164 my $title = Koha::Items->find( $item )->biblio->title;
165 if ( $messages->{'transfert'} ) {
166 $res{messagetransfert} = $messages->{'transfert'};
167 $res{branchcode} = $messages->{'transfert'};
171 $res{nextreservnumber} = $nextreservinfo;
172 $res{nextreservsurname} = $patron->surname;
173 $res{nextreservfirstname} = $patron->firstname;
174 $res{nextreservitem} = $item;
175 $res{nextreservtitle} = $title;
176 $res{waiting} = $messages->{'waiting'} ? 1 : 0;