Bug 28606: Remove $DEBUG and $ENV{DEBUG}
[koha.git] / circ / waitingreserves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # parts copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Circulation;
27 use C4::Members;
28 use C4::Biblio;
29 use C4::Items;
30 use Date::Calc qw(
31   Today
32   Add_Delta_Days
33   Date_to_Days
34 );
35 use C4::Reserves;
36 use C4::Koha;
37 use Koha::DateUtils;
38 use Koha::BiblioFrameworks;
39 use Koha::Items;
40 use Koha::ItemTypes;
41 use Koha::Patrons;
42
43 my $input = CGI->new;
44
45 my $item           = $input->param('itemnumber');
46 my $borrowernumber = $input->param('borrowernumber');
47 my $fbr            = $input->param('fbr') || '';
48 my $tbr            = $input->param('tbr') || '';
49 my $all_branches   = $input->param('allbranches') || '';
50 my $cancelall      = $input->param('cancelall');
51 my $tab            = $input->param('tab');
52
53 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
54     {
55         template_name   => "circ/waitingreserves.tt",
56         query           => $input,
57         type            => "intranet",
58         flagsrequired   => { circulate => "circulate_remaining_permissions" },
59     }
60 );
61
62 my $default = C4::Context->userenv->{'branch'};
63
64 my $transfer_when_cancel_all = C4::Context->preference('TransferWhenCancelAllWaitingHolds');
65 $template->param( TransferWhenCancelAllWaitingHolds => 1 ) if $transfer_when_cancel_all;
66
67 my @cancel_result;
68 # if we have a return from the form we cancel the holds
69 if ($item) {
70     my $res = cancel( $item, $borrowernumber, $fbr, $tbr );
71     push @cancel_result, $res if $res;
72 }
73
74 if ( C4::Context->preference('IndependentBranches') ) {
75     undef $all_branches;
76 } else {
77     $template->param( all_branches_link => '/cgi-bin/koha/circ/waitingreserves.pl' . '?allbranches=1' )
78       unless $all_branches;
79 }
80 $template->param( all_branches => 1 ) if $all_branches;
81
82 my (@reserve_loop, @over_loop);
83 # FIXME - Is priority => 0 useful? If yes it must be moved to waiting, otherwise we need to remove it from here.
84 my $holds = Koha::Holds->waiting->search({ priority => 0, ( $all_branches ? () : ( branchcode => $default ) ) }, { order_by => ['waitingdate'] });
85
86 # get reserves for the branch we are logged into, or for all branches
87
88 my $today = Date_to_Days(&Today);
89
90 while ( my $hold = $holds->next ) {
91     next unless $hold->waitingdate;
92
93     my ( $expire_year, $expire_month, $expire_day ) = split (/-/, $hold->expirationdate);
94     my $calcDate = Date_to_Days( $expire_year, $expire_month, $expire_day );
95
96     if ($today > $calcDate) {
97         if ($cancelall) {
98             my $res = cancel( $hold->item->itemnumber, $hold->borrowernumber, $hold->item->holdingbranch, $hold->item->homebranch, !$transfer_when_cancel_all );
99             push @cancel_result, $res if $res;
100             next;
101         } else {
102             push @over_loop, $hold;
103         }
104     }else{
105         push @reserve_loop, $hold;
106     }
107     
108 }
109
110 $template->param(cancel_result => \@cancel_result) if @cancel_result;
111
112 $template->param(
113     reserveloop => \@reserve_loop,
114     reservecount => scalar @reserve_loop,
115     overloop    => \@over_loop,
116     overcount   => scalar @over_loop,
117     show_date   => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
118     tab => $tab,
119 );
120
121 # Checking if there is a Fast Cataloging Framework
122 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
123
124 if ($item && $tab eq 'holdsover' && !@cancel_result) {
125     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl#holdsover");
126 } elsif ($cancelall) {
127     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl");
128 } else {
129     output_html_with_http_headers $input, $cookie, $template->output;
130 }
131
132 exit;
133
134 sub cancel {
135     my ($item, $borrowernumber, $fbr, $tbr, $skip_transfers ) = @_;
136
137     my $transfer = $fbr ne $tbr; # XXX && !$nextreservinfo;
138
139     return if $transfer && $skip_transfers;
140
141     my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
142
143 #       if the document is not in his homebranch location and there is not reservation after, we transfer it
144     if ($transfer && !$nextreservinfo) {
145         ModItemTransfer( $item, $fbr, $tbr, 'CancelReserve' );
146     }
147     # if we have a result
148     if ($nextreservinfo) {
149         my %res;
150         my $patron = Koha::Patrons->find( $nextreservinfo );
151         my $title = Koha::Items->find( $item )->biblio->title;
152         if ( $messages->{'transfert'} ) {
153             $res{messagetransfert} = $messages->{'transfert'};
154             $res{branchcode}       = $messages->{'transfert'};
155         }
156
157         $res{message}             = 1;
158         $res{nextreservnumber}    = $nextreservinfo;
159         $res{nextreservsurname}   = $patron->surname;
160         $res{nextreservfirstname} = $patron->firstname;
161         $res{nextreservitem}      = $item;
162         $res{nextreservtitle}     = $title;
163         $res{waiting}             = $messages->{'waiting'} ? 1 : 0;
164
165         return \%res;
166     }
167
168     return;
169 }