Bug 16429 - Going to circulation from notice triggers may change logged in branch
[koha.git] / circ / waitingreserves.pl
blob0050ce3ac5bddf3d5fb63c82f48702e7db6af003
1 #!/usr/bin/perl
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>.
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
38 use C4::Reserves;
39 use C4::Koha;
41 my $input = new CGI;
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 my $tab = $input->param('tab');
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53 template_name => "circ/waitingreserves.tt",
54 query => $input,
55 type => "intranet",
56 authnotrequired => 0,
57 flagsrequired => { circulate => "circulate_remaining_permissions" },
58 debug => 1,
62 my $default = C4::Context->userenv->{'branch'};
64 my $transfer_when_cancel_all = C4::Context->preference('TransferWhenCancelAllWaitingHolds');
65 $template->param( TransferWhenCancelAllWaitingHolds => 1 ) if $transfer_when_cancel_all;
67 my @cancel_result;
68 # if we have a return from the form we launch the subroutine CancelReserve
69 if ($item) {
70 my $res = cancel( $item, $borrowernumber, $fbr, $tbr );
71 push @cancel_result, $res if $res;
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;
80 $template->param( all_branches => 1 ) if $all_branches;
82 my (@reservloop, @overloop);
83 my ($reservcount, $overcount);
84 my @getreserves = $all_branches ? GetReservesForBranch() : GetReservesForBranch($default);
85 # get reserves for the branch we are logged into, or for all branches
87 my $today = Date_to_Days(&Today);
88 my $max_pickup_delay = C4::Context->preference('ReservesMaxPickUpDelay');
89 $max_pickup_delay-- if C4::Context->preference('ExpireReservesMaxPickUpDelay');
91 foreach my $num (@getreserves) {
92 next unless ($num->{'waitingdate'} && $num->{'waitingdate'} ne '0000-00-00');
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'};
100 my %getreserv = (
101 itemnumber => $itemnumber,
102 borrowernum => $borrowernum,
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'});
112 ( $waiting_year, $waiting_month, $waiting_day ) =
113 Add_Delta_Days( $waiting_year, $waiting_month, $waiting_day,
114 $max_pickup_delay);
115 my $calcDate = Date_to_Days( $waiting_year, $waiting_month, $waiting_day );
117 $getreserv{'itemtype'} = $itemtypeinfo->{'description'};
118 $getreserv{'title'} = $gettitle->{'title'};
119 $getreserv{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($gettitle->{'biblionumber'}), GetFrameworkCode($gettitle->{'biblionumber'}));
120 $getreserv{'biblionumber'} = $gettitle->{'biblionumber'};
121 $getreserv{'barcode'} = $gettitle->{'barcode'};
122 $getreserv{'branchname'} = GetBranchName($gettitle->{'homebranch'});
123 $getreserv{'homebranch'} = $gettitle->{'homebranch'};
124 $getreserv{'holdingbranch'} = $gettitle->{'holdingbranch'};
125 $getreserv{'itemcallnumber'} = $gettitle->{'itemcallnumber'};
126 $getreserv{'enumchron'} = $gettitle->{'enumchron'};
127 $getreserv{'copynumber'} = $gettitle->{'copynumber'};
128 if ( $homebranch ne $holdingbranch ) {
129 $getreserv{'dotransfer'} = 1;
131 $getreserv{'borrowername'} = $getborrower->{'surname'};
132 $getreserv{'borrowerfirstname'} = $getborrower->{'firstname'};
133 $getreserv{'borrowerphone'} = $getborrower->{'phone'};
135 my $borEmail = GetFirstValidEmailAddress( $borrowernum );
137 if ( $borEmail ) {
138 $getreserv{'borrowermail'} = $borEmail;
141 if ($today > $calcDate) {
142 if ($cancelall) {
143 my $res = cancel( $itemnumber, $borrowernum, $holdingbranch, $homebranch, !$transfer_when_cancel_all );
144 push @cancel_result, $res if $res;
145 next;
146 } else {
147 push @overloop, \%getreserv;
148 $overcount++;
150 }else{
151 push @reservloop, \%getreserv;
152 $reservcount++;
157 $template->param(cancel_result => \@cancel_result) if @cancel_result;
158 $template->param(
159 reserveloop => \@reservloop,
160 reservecount => $reservcount,
161 overloop => \@overloop,
162 overcount => $overcount,
163 show_date => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
164 ReservesMaxPickUpDelay => $max_pickup_delay,
167 if ($item && $tab eq 'holdsover') {
168 print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl#holdsover");
169 } elsif ($cancelall) {
170 print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl");
171 } else {
172 output_html_with_http_headers $input, $cookie, $template->output;
175 exit;
177 sub cancel {
178 my ($item, $borrowernumber, $fbr, $tbr, $skip_transfers ) = @_;
180 my $transfer = $fbr ne $tbr; # XXX && !$nextreservinfo;
182 return if $transfer && $skip_transfers;
184 my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
186 # if the document is not in his homebranch location and there is not reservation after, we transfer it
187 if ($transfer && !$nextreservinfo) {
188 ModItemTransfer( $item, $fbr, $tbr );
190 # if we have a result
191 if ($nextreservinfo) {
192 my %res;
193 my $borrowerinfo = C4::Members::GetMember( borrowernumber => $nextreservinfo );
194 my $iteminfo = GetBiblioFromItemNumber($item);
195 if ( $messages->{'transfert'} ) {
196 $res{messagetransfert} = $messages->{'transfert'};
197 $res{branchname} = GetBranchName($messages->{'transfert'});
200 $res{message} = 1;
201 $res{nextreservnumber} = $nextreservinfo;
202 $res{nextreservsurname} = $borrowerinfo->{'surname'};
203 $res{nextreservfirstname} = $borrowerinfo->{'firstname'};
204 $res{nextreservitem} = $item;
205 $res{nextreservtitle} = $iteminfo->{'title'};
206 $res{waiting} = $messages->{'waiting'} ? 1 : 0;
208 return \%res;
211 return;