Merge branch 'bug_8945' into 3.12-master
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23 use CGI;
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
51 my $cancel;
52
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54     {
55         template_name   => "circ/waitingreserves.tmpl",
56         query           => $input,
57         type            => "intranet",
58         authnotrequired => 0,
59         flagsrequired   => { circulate => "circulate_remaining_permissions" },
60         debug           => 1,
61     }
62 );
63
64 my $default = C4::Context->userenv->{'branch'};
65
66 my $transfer_when_cancel_all = C4::Context->preference('TransferWhenCancelAllWaitingHolds');
67 $template->param( TransferWhenCancelAllWaitingHolds => 1 ) if $transfer_when_cancel_all;
68
69 my @cancel_result;
70 # if we have a return from the form we launch the subroutine CancelReserve
71 if ($item) {
72     my $res = cancel( $item, $borrowernumber, $fbr, $tbr );
73     push @cancel_result, $res if $res;
74 }
75
76 if ( C4::Context->preference('IndependantBranches') ) {
77     undef $all_branches;
78 } else {
79     $template->param( all_branches_link => $input->url . '?allbranches=1' )
80       unless $all_branches;
81 }
82 $template->param( all_branches => 1 ) if $all_branches;
83
84 my (@reservloop, @overloop);
85 my ($reservcount, $overcount);
86 my @getreserves = $all_branches ? GetReservesForBranch() : GetReservesForBranch($default);
87 # get reserves for the branch we are logged into, or for all branches
88
89 my $today = Date_to_Days(&Today);
90 foreach my $num (@getreserves) {
91     next unless ($num->{'waitingdate'} && $num->{'waitingdate'} ne '0000-00-00');
92
93     my $itemnumber = $num->{'itemnumber'};
94     my $gettitle     = GetBiblioFromItemNumber( $itemnumber );
95     my $borrowernum = $num->{'borrowernumber'};
96     my $holdingbranch = $gettitle->{'holdingbranch'};
97     my $homebranch = $gettitle->{'homebranch'};
98
99     my %getreserv = (
100         itemnumber => $itemnumber,
101         borrowernum => $borrowernum,
102     );
103
104     # fix up item type for display
105     $gettitle->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $gettitle->{'itype'} : $gettitle->{'itemtype'};
106     my $getborrower = GetMember(borrowernumber => $num->{'borrowernumber'});
107     my $itemtypeinfo = getitemtypeinfo( $gettitle->{'itemtype'} );  # using the fixed up itype/itemtype
108     $getreserv{'waitingdate'} = format_date( $num->{'waitingdate'} );
109     my ( $waiting_year, $waiting_month, $waiting_day ) = split (/-/, $num->{'waitingdate'});
110     ( $waiting_year, $waiting_month, $waiting_day ) =
111       Add_Delta_Days( $waiting_year, $waiting_month, $waiting_day,
112         C4::Context->preference('ReservesMaxPickUpDelay'));
113     my $calcDate = Date_to_Days( $waiting_year, $waiting_month, $waiting_day );
114
115     $getreserv{'itemtype'}       = $itemtypeinfo->{'description'};
116     $getreserv{'title'}          = $gettitle->{'title'};
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     if ( $getborrower->{'emailaddress'} ) {
132         $getreserv{'borrowermail'}  = $getborrower->{'emailaddress'};
133     }
134  
135     if ($today > $calcDate) {
136         if ($cancelall) {
137             my $res = cancel( $itemnumber, $borrowernum, $holdingbranch, $homebranch, !$transfer_when_cancel_all );
138             push @cancel_result, $res if $res;
139             next;
140         } else {
141             push @overloop,   \%getreserv;
142             $overcount++;
143         }
144     }else{
145         push @reservloop, \%getreserv;
146         $reservcount++;
147     }
148     
149 }
150
151 $template->param(cancel_result => \@cancel_result) if @cancel_result;
152 $template->param(
153     reserveloop => \@reservloop,
154     reservecount => $reservcount,
155     overloop    => \@overloop,
156     overcount   => $overcount,
157     show_date   => format_date(C4::Dates->today('iso')),
158     dateformat  => C4::Context->preference("dateformat"),
159     ReservesMaxPickUpDelay => C4::Context->preference('ReservesMaxPickUpDelay')
160 );
161
162 if ($cancelall) {
163     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl");
164 } else {
165     output_html_with_http_headers $input, $cookie, $template->output;
166 }
167
168 exit;
169
170 sub cancel {
171     my ($item, $borrowernumber, $fbr, $tbr, $skip_transfers ) = @_;
172
173     my $transfer = $fbr ne $tbr; # XXX && !$nextreservinfo;
174
175     return if $transfer && $skip_transfers;
176
177     my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
178
179 #       if the document is not in his homebranch location and there is not reservation after, we transfer it
180     if ($transfer && !$nextreservinfo) {
181         ModItemTransfer( $item, $fbr, $tbr );
182     }
183     # if we have a result
184     if ($nextreservinfo) {
185         my %res;
186         my $borrowerinfo = GetMemberDetails( $nextreservinfo );
187         my $iteminfo = GetBiblioFromItemNumber($item);
188         if ( $messages->{'transfert'} ) {
189             $res{messagetransfert} = $messages->{'transfert'};
190             $res{branchname}       = GetBranchName($messages->{'transfert'});
191         }
192
193         $res{message}             = 1;
194         $res{nextreservnumber}    = $nextreservinfo;
195         $res{nextreservsurname}   = $borrowerinfo->{'surname'};
196         $res{nextreservfirstname} = $borrowerinfo->{'firstname'};
197         $res{nextreservitem}      = $item;
198         $res{nextreservtitle}     = $iteminfo->{'title'};
199         $res{waiting}             = $messages->{'waiting'} ? 1 : 0;
200
201         return \%res;
202     }
203
204     return;
205 }