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