Bug 18276: Remove GetBiblioFromItemNumber - Easy ones
[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::Auth;
27 use C4::Circulation;
28 use C4::Members;
29 use C4::Biblio;
30 use C4::Items;
31 use Date::Calc qw(
32   Today
33   Add_Delta_Days
34   Date_to_Days
35 );
36 use C4::Reserves;
37 use C4::Koha;
38 use Koha::DateUtils;
39 use Koha::BiblioFrameworks;
40 use Koha::Items;
41 use Koha::ItemTypes;
42
43 my $input = new CGI;
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         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('IndependentBranches') ) {
77     undef $all_branches;
78 } else {
79     $template->param( all_branches_link => '/cgi-bin/koha/circ/waitingreserves.pl' . '?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 my $max_pickup_delay = C4::Context->preference('ReservesMaxPickUpDelay');
91
92 foreach my $num (@getreserves) {
93     next unless ($num->{'waitingdate'} && $num->{'waitingdate'} ne '0000-00-00');
94
95     my $itemnumber = $num->{'itemnumber'};
96     my $item = Koha::Items->find( $itemnumber );
97     my $biblio = $item->biblio;
98     my $borrowernum = $num->{'borrowernumber'};
99     my $holdingbranch = $item->holdingbranch;
100     my $homebranch = $item->homebranch;
101
102     my %getreserv = (
103         itemnumber => $itemnumber,
104         borrowernum => $borrowernum,
105     );
106
107     my $getborrower = GetMember(borrowernumber => $num->{'borrowernumber'});
108     my $itemtype = Koha::ItemTypes->find( $item->effective_itemtype );
109     $getreserv{'waitingdate'} = $num->{'waitingdate'};
110     my ( $expire_year, $expire_month, $expire_day ) = split (/-/, $num->{'expirationdate'});
111     my $calcDate = Date_to_Days( $expire_year, $expire_month, $expire_day );
112
113     $getreserv{'itemtype'}       = $itemtype->description; # FIXME Should not it be translated_description?
114     $getreserv{'title'}          = $biblio->title;
115     $getreserv{'subtitle'}       = GetRecordValue('subtitle', GetMarcBiblio($biblio->biblionumber), $biblio->frameworkcode);
116     $getreserv{'biblionumber'}   = $biblio->biblionumber;
117     $getreserv{'barcode'}        = $item->barcode;
118     $getreserv{'homebranch'}     = $homebranch;
119     $getreserv{'holdingbranch'}  = $item->holdingbranch;
120     $getreserv{'itemcallnumber'} = $item->itemcallnumber;
121     $getreserv{'enumchron'}      = $item->enumchron;
122     $getreserv{'copynumber'}     = $item->copynumber;
123     if ( $homebranch ne $holdingbranch ) {
124         $getreserv{'dotransfer'} = 1;
125     }
126     $getreserv{'borrowername'}      = $getborrower->{'surname'};
127     $getreserv{'borrowerfirstname'} = $getborrower->{'firstname'};
128     $getreserv{'borrowerphone'}     = $getborrower->{'phone'};
129
130     my $borEmail = GetFirstValidEmailAddress( $borrowernum );
131
132     if ( $borEmail ) {
133         $getreserv{'borrowermail'}  = $borEmail;
134     }
135
136     if ($today > $calcDate) {
137         if ($cancelall) {
138             my $res = cancel( $itemnumber, $borrowernum, $holdingbranch, $homebranch, !$transfer_when_cancel_all );
139             push @cancel_result, $res if $res;
140             next;
141         } else {
142             push @overloop,   \%getreserv;
143             $overcount++;
144         }
145     }else{
146         push @reservloop, \%getreserv;
147         $reservcount++;
148     }
149     
150 }
151
152 $template->param(cancel_result => \@cancel_result) if @cancel_result;
153 $template->param(
154     reserveloop => \@reservloop,
155     reservecount => $reservcount,
156     overloop    => \@overloop,
157     overcount   => $overcount,
158     show_date   => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
159     ReservesMaxPickUpDelay => $max_pickup_delay,
160     tab => $tab,
161 );
162
163 # Checking if there is a Fast Cataloging Framework
164 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
165
166 if ($item && $tab eq 'holdsover' && !@cancel_result) {
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 $title = Koha::Items->find( $item )->biblio->title;
194         if ( $messages->{'transfert'} ) {
195             $res{messagetransfert} = $messages->{'transfert'};
196             $res{branchcode}       = $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}     = $title;
205         $res{waiting}             = $messages->{'waiting'} ? 1 : 0;
206
207         return \%res;
208     }
209
210     return;
211 }