Bug 19299: Replace C4::Reserves::GetReservesForBranch with Koha::Holds->waiting
[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 use Koha::Patrons;
43
44 my $input = new CGI;
45
46 my $item           = $input->param('itemnumber');
47 my $borrowernumber = $input->param('borrowernumber');
48 my $fbr            = $input->param('fbr') || '';
49 my $tbr            = $input->param('tbr') || '';
50 my $all_branches   = $input->param('allbranches') || '';
51 my $cancelall      = $input->param('cancelall');
52 my $tab            = $input->param('tab');
53
54 my ( $template, $loggedinuser, $cookie, $flags ) = 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 cancel the holds
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 => '/cgi-bin/koha/circ/waitingreserves.pl' . '?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 # FIXME - Is priority => 0 useful? If yes it must be moved to waiting, otherwise we need to remove it from here.
88 my $holds = Koha::Holds->waiting->search({ priority => 0, ( $all_branches ? () : ( branchcode => $default ) ) }, { order_by => ['waitingdate'] });
89 # get reserves for the branch we are logged into, or for all branches
90
91 my $today = Date_to_Days(&Today);
92 my $max_pickup_delay = C4::Context->preference('ReservesMaxPickUpDelay');
93
94 while ( my $hold = $holds->next ) {
95     next unless ($hold->waitingdate && $hold->waitingdate ne '0000-00-00');
96
97     my $item = $hold->item;
98     my $patron = $hold->borrower;
99     my $biblio = $item->biblio;
100     my $holdingbranch = $item->holdingbranch;
101     my $homebranch = $item->homebranch;
102
103     my %getreserv = (
104         title             => $biblio->title,
105         itemnumber        => $item->itemnumber,
106         waitingdate       => $hold->waitingdate,
107         borrowernum       => $patron->borrowernumber,
108         biblionumber      => $biblio->biblionumber,
109         barcode           => $item->barcode,
110         homebranch        => $homebranch,
111         holdingbranch     => $item->holdingbranch,
112         itemcallnumber    => $item->itemcallnumber,
113         enumchron         => $item->enumchron,
114         copynumber        => $item->copynumber,
115         borrowername      => $patron->surname, # FIXME Let's send $patron to the template
116         borrowerfirstname => $patron->firstname,
117         borrowerphone     => $patron->phone,
118     );
119
120     my $itemtype = Koha::ItemTypes->find( $item->effective_itemtype );
121     my ( $expire_year, $expire_month, $expire_day ) = split (/-/, $hold->expirationdate);
122     my $calcDate = Date_to_Days( $expire_year, $expire_month, $expire_day );
123
124     $getreserv{'itemtype'}       = $itemtype->description; # FIXME Should not it be translated_description?
125     $getreserv{'subtitle'}       = GetRecordValue(
126         'subtitle',
127         GetMarcBiblio({ biblionumber => $biblio->biblionumber }),
128         $biblio->frameworkcode);
129     if ( $homebranch ne $holdingbranch ) {
130         $getreserv{'dotransfer'} = 1;
131     }
132
133     my $borEmail = $patron->first_valid_email_address;
134
135     if ( $borEmail ) {
136         $getreserv{'borrowermail'}  = $borEmail;
137     }
138
139     if ($today > $calcDate) {
140         if ($cancelall) {
141             my $res = cancel( $item->itemnumber, $patron->borrowernumber, $holdingbranch, $homebranch, !$transfer_when_cancel_all );
142             push @cancel_result, $res if $res;
143             next;
144         } else {
145             push @overloop,   \%getreserv;
146             $overcount++;
147         }
148     }else{
149         push @reservloop, \%getreserv;
150         $reservcount++;
151     }
152     
153 }
154
155 $template->param(cancel_result => \@cancel_result) if @cancel_result;
156 $template->param(
157     reserveloop => \@reservloop,
158     reservecount => $reservcount,
159     overloop    => \@overloop,
160     overcount   => $overcount,
161     show_date   => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
162     ReservesMaxPickUpDelay => $max_pickup_delay,
163     tab => $tab,
164 );
165
166 # Checking if there is a Fast Cataloging Framework
167 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
168
169 if ($item && $tab eq 'holdsover' && !@cancel_result) {
170     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl#holdsover");
171 } elsif ($cancelall) {
172     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl");
173 } else {
174     output_html_with_http_headers $input, $cookie, $template->output;
175 }
176
177 exit;
178
179 sub cancel {
180     my ($item, $borrowernumber, $fbr, $tbr, $skip_transfers ) = @_;
181
182     my $transfer = $fbr ne $tbr; # XXX && !$nextreservinfo;
183
184     return if $transfer && $skip_transfers;
185
186     my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
187
188 #       if the document is not in his homebranch location and there is not reservation after, we transfer it
189     if ($transfer && !$nextreservinfo) {
190         ModItemTransfer( $item, $fbr, $tbr );
191     }
192     # if we have a result
193     if ($nextreservinfo) {
194         my %res;
195         my $patron = Koha::Patrons->find( $nextreservinfo );
196         my $title = Koha::Items->find( $item )->biblio->title;
197         if ( $messages->{'transfert'} ) {
198             $res{messagetransfert} = $messages->{'transfert'};
199             $res{branchcode}       = $messages->{'transfert'};
200         }
201
202         $res{message}             = 1;
203         $res{nextreservnumber}    = $nextreservinfo;
204         $res{nextreservsurname}   = $patron->surname;
205         $res{nextreservfirstname} = $patron->firstname;
206         $res{nextreservitem}      = $item;
207         $res{nextreservtitle}     = $title;
208         $res{waiting}             = $messages->{'waiting'} ? 1 : 0;
209
210         return \%res;
211     }
212
213     return;
214 }