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 output_pref );
30 use Koha::BiblioFrameworks;
37 my $item = $input->param('itemnumber');
38 my $borrowernumber = $input->param('borrowernumber');
39 my $fbr = $input->param('fbr') || '';
40 my $tbr = $input->param('tbr') || '';
41 my $all_branches = $input->param('allbranches') || '';
42 my $cancelall = $input->param('cancelall');
43 my $tab = $input->param('tab');
45 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
47 template_name => "circ/waitingreserves.tt",
50 flagsrequired => { circulate => "circulate_remaining_permissions" },
54 my $default = C4::Context->userenv->{'branch'};
56 my $transfer_when_cancel_all = C4::Context->preference('TransferWhenCancelAllWaitingHolds');
57 $template->param( TransferWhenCancelAllWaitingHolds => 1 ) if $transfer_when_cancel_all;
60 # if we have a return from the form we cancel the holds
62 my $res = cancel( $item, $borrowernumber, $fbr, $tbr );
63 push @cancel_result, $res if $res;
66 if ( C4::Context->preference('IndependentBranches') ) {
69 $template->param( all_branches_link => '/cgi-bin/koha/circ/waitingreserves.pl' . '?allbranches=1' )
72 $template->param( all_branches => 1 ) if $all_branches;
74 my (@reserve_loop, @over_loop);
75 # FIXME - Is priority => 0 useful? If yes it must be moved to waiting, otherwise we need to remove it from here.
76 my $holds = Koha::Holds->waiting->search({ priority => 0, ( $all_branches ? () : ( branchcode => $default ) ) }, { order_by => ['waitingdate'] });
78 # get reserves for the branch we are logged into, or for all branches
80 my $today = Date_to_Days(&Today);
82 while ( my $hold = $holds->next ) {
83 next unless $hold->waitingdate;
85 my ( $expire_year, $expire_month, $expire_day ) = split (/-/, $hold->expirationdate);
86 my $calcDate = Date_to_Days( $expire_year, $expire_month, $expire_day );
88 if ($today > $calcDate) {
90 my $res = cancel( $hold->item->itemnumber, $hold->borrowernumber, $hold->item->holdingbranch, $hold->item->homebranch, !$transfer_when_cancel_all );
91 push @cancel_result, $res if $res;
94 push @over_loop, $hold;
97 push @reserve_loop, $hold;
102 $template->param(cancel_result => \@cancel_result) if @cancel_result;
105 reserveloop => \@reserve_loop,
106 reservecount => scalar @reserve_loop,
107 overloop => \@over_loop,
108 overcount => scalar @over_loop,
109 show_date => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
113 # Checking if there is a Fast Cataloging Framework
114 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
116 if ($item && $tab eq 'holdsover' && !@cancel_result) {
117 print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl#holdsover");
118 } elsif ($cancelall) {
119 print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl");
121 output_html_with_http_headers $input, $cookie, $template->output;
127 my ($item, $borrowernumber, $fbr, $tbr, $skip_transfers ) = @_;
129 my $transfer = $fbr ne $tbr; # XXX && !$nextreservinfo;
131 return if $transfer && $skip_transfers;
133 my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
135 # if the document is not in his homebranch location and there is not reservation after, we transfer it
136 if ($transfer && !$nextreservinfo) {
137 ModItemTransfer( $item, $fbr, $tbr, 'CancelReserve' );
139 # if we have a result
140 if ($nextreservinfo) {
142 my $patron = Koha::Patrons->find( $nextreservinfo );
143 my $title = Koha::Items->find( $item )->biblio->title;
144 if ( $messages->{'transfert'} ) {
145 $res{messagetransfert} = $messages->{'transfert'};
146 $res{branchcode} = $messages->{'transfert'};
150 $res{nextreservnumber} = $nextreservinfo;
151 $res{nextreservsurname} = $patron->surname;
152 $res{nextreservfirstname} = $patron->firstname;
153 $res{nextreservitem} = $item;
154 $res{nextreservtitle} = $title;
155 $res{waiting} = $messages->{'waiting'} ? 1 : 0;