Bug 15005: Remove CGI->url calls from pl scripts
[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 strict;
22 use warnings;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output;
26 use C4::Branch; # GetBranchName
27 use C4::Auth;
28 use C4::Circulation;
29 use C4::Members;
30 use C4::Biblio;
31 use C4::Items;
32 use Koha::DateUtils;
33 use Date::Calc qw(
34   Today
35   Add_Delta_Days
36   Date_to_Days
37 );
38 use C4::Reserves;
39 use C4::Koha;
40
41 my $input = new CGI;
42
43 my $item           = $input->param('itemnumber');
44 my $borrowernumber = $input->param('borrowernumber');
45 my $fbr            = $input->param('fbr') || '';
46 my $tbr            = $input->param('tbr') || '';
47 my $all_branches   = $input->param('allbranches') || '';
48 my $cancelall      = $input->param('cancelall');
49
50 my $cancel;
51
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53     {
54         template_name   => "circ/waitingreserves.tt",
55         query           => $input,
56         type            => "intranet",
57         authnotrequired => 0,
58         flagsrequired   => { circulate => "circulate_remaining_permissions" },
59         debug           => 1,
60     }
61 );
62
63 my $default = C4::Context->userenv->{'branch'};
64
65 my $transfer_when_cancel_all = C4::Context->preference('TransferWhenCancelAllWaitingHolds');
66 $template->param( TransferWhenCancelAllWaitingHolds => 1 ) if $transfer_when_cancel_all;
67
68 my @cancel_result;
69 # if we have a return from the form we launch the subroutine CancelReserve
70 if ($item) {
71     my $res = cancel( $item, $borrowernumber, $fbr, $tbr );
72     push @cancel_result, $res if $res;
73 }
74
75 if ( C4::Context->preference('IndependentBranches') ) {
76     undef $all_branches;
77 } else {
78     $template->param( all_branches_link => '/cgi-bin/koha/circ/waitingreserves.pl' . '?allbranches=1' )
79       unless $all_branches;
80 }
81 $template->param( all_branches => 1 ) if $all_branches;
82
83 my (@reservloop, @overloop);
84 my ($reservcount, $overcount);
85 my @getreserves = $all_branches ? GetReservesForBranch() : GetReservesForBranch($default);
86 # get reserves for the branch we are logged into, or for all branches
87
88 my $today = Date_to_Days(&Today);
89 foreach my $num (@getreserves) {
90     next unless ($num->{'waitingdate'} && $num->{'waitingdate'} ne '0000-00-00');
91
92     my $itemnumber = $num->{'itemnumber'};
93     my $gettitle     = GetBiblioFromItemNumber( $itemnumber );
94     my $borrowernum = $num->{'borrowernumber'};
95     my $holdingbranch = $gettitle->{'holdingbranch'};
96     my $homebranch = $gettitle->{'homebranch'};
97
98     my %getreserv = (
99         itemnumber => $itemnumber,
100         borrowernum => $borrowernum,
101     );
102
103     # fix up item type for display
104     $gettitle->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $gettitle->{'itype'} : $gettitle->{'itemtype'};
105     my $getborrower = GetMember(borrowernumber => $num->{'borrowernumber'});
106     my $itemtypeinfo = getitemtypeinfo( $gettitle->{'itemtype'} );  # using the fixed up itype/itemtype
107     $getreserv{'waitingdate'} = $num->{'waitingdate'};
108     my ( $waiting_year, $waiting_month, $waiting_day ) = split (/-/, $num->{'waitingdate'});
109     ( $waiting_year, $waiting_month, $waiting_day ) =
110       Add_Delta_Days( $waiting_year, $waiting_month, $waiting_day,
111         C4::Context->preference('ReservesMaxPickUpDelay'));
112     my $calcDate = Date_to_Days( $waiting_year, $waiting_month, $waiting_day );
113
114     $getreserv{'itemtype'}       = $itemtypeinfo->{'description'};
115     $getreserv{'title'}          = $gettitle->{'title'};
116     $getreserv{'subtitle'}       = GetRecordValue('subtitle', GetMarcBiblio($gettitle->{'biblionumber'}), GetFrameworkCode($gettitle->{'biblionumber'}));
117     $getreserv{'biblionumber'}   = $gettitle->{'biblionumber'};
118     $getreserv{'barcode'}        = $gettitle->{'barcode'};
119     $getreserv{'branchname'}     = GetBranchName($gettitle->{'homebranch'});
120     $getreserv{'homebranch'}     = $gettitle->{'homebranch'};
121     $getreserv{'holdingbranch'}  = $gettitle->{'holdingbranch'};
122     $getreserv{'itemcallnumber'} = $gettitle->{'itemcallnumber'};
123     $getreserv{'enumchron'}      = $gettitle->{'enumchron'};
124     $getreserv{'copynumber'}     = $gettitle->{'copynumber'};
125     if ( $homebranch ne $holdingbranch ) {
126         $getreserv{'dotransfer'} = 1;
127     }
128     $getreserv{'borrowername'}      = $getborrower->{'surname'};
129     $getreserv{'borrowerfirstname'} = $getborrower->{'firstname'};
130     $getreserv{'borrowerphone'}     = $getborrower->{'phone'};
131
132     my $borEmail = GetFirstValidEmailAddress( $borrowernum );
133
134     if ( $borEmail ) {
135         $getreserv{'borrowermail'}  = $borEmail;
136     }
137
138     if ($today > $calcDate) {
139         if ($cancelall) {
140             my $res = cancel( $itemnumber, $borrowernum, $holdingbranch, $homebranch, !$transfer_when_cancel_all );
141             push @cancel_result, $res if $res;
142             next;
143         } else {
144             push @overloop,   \%getreserv;
145             $overcount++;
146         }
147     }else{
148         push @reservloop, \%getreserv;
149         $reservcount++;
150     }
151     
152 }
153
154 $template->param(cancel_result => \@cancel_result) if @cancel_result;
155 $template->param(
156     reserveloop => \@reservloop,
157     reservecount => $reservcount,
158     overloop    => \@overloop,
159     overcount   => $overcount,
160     show_date   => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
161     ReservesMaxPickUpDelay => C4::Context->preference('ReservesMaxPickUpDelay')
162 );
163
164 if ($cancelall) {
165     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl");
166 } else {
167     output_html_with_http_headers $input, $cookie, $template->output;
168 }
169
170 exit;
171
172 sub cancel {
173     my ($item, $borrowernumber, $fbr, $tbr, $skip_transfers ) = @_;
174
175     my $transfer = $fbr ne $tbr; # XXX && !$nextreservinfo;
176
177     return if $transfer && $skip_transfers;
178
179     my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
180
181 #       if the document is not in his homebranch location and there is not reservation after, we transfer it
182     if ($transfer && !$nextreservinfo) {
183         ModItemTransfer( $item, $fbr, $tbr );
184     }
185     # if we have a result
186     if ($nextreservinfo) {
187         my %res;
188         my $borrowerinfo = GetMemberDetails( $nextreservinfo );
189         my $iteminfo = GetBiblioFromItemNumber($item);
190         if ( $messages->{'transfert'} ) {
191             $res{messagetransfert} = $messages->{'transfert'};
192             $res{branchname}       = GetBranchName($messages->{'transfert'});
193         }
194
195         $res{message}             = 1;
196         $res{nextreservnumber}    = $nextreservinfo;
197         $res{nextreservsurname}   = $borrowerinfo->{'surname'};
198         $res{nextreservfirstname} = $borrowerinfo->{'firstname'};
199         $res{nextreservitem}      = $item;
200         $res{nextreservtitle}     = $iteminfo->{'title'};
201         $res{waiting}             = $messages->{'waiting'} ? 1 : 0;
202
203         return \%res;
204     }
205
206     return;
207 }