Bug 19522: Label creator: translate empty list message
[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 Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Circulation;
27 use C4::Members;
28 use C4::Biblio;
29 use C4::Items;
30 use Date::Calc qw(
31   Today
32   Add_Delta_Days
33   Date_to_Days
34 );
35 use C4::Reserves;
36 use C4::Koha;
37 use Koha::DateUtils;
38 use Koha::BiblioFrameworks;
39 use Koha::Items;
40 use Koha::ItemTypes;
41 use Koha::Patrons;
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 cancel the holds
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 # FIXME - Is priority => 0 useful? If yes it must be moved to waiting, otherwise we need to remove it from here.
87 my $holds = Koha::Holds->waiting->search({ priority => 0, ( $all_branches ? () : ( branchcode => $default ) ) }, { order_by => ['waitingdate'] });
88 # get reserves for the branch we are logged into, or for all branches
89
90 my $today = Date_to_Days(&Today);
91 my $max_pickup_delay = C4::Context->preference('ReservesMaxPickUpDelay');
92
93 while ( my $hold = $holds->next ) {
94     next unless ($hold->waitingdate && $hold->waitingdate ne '0000-00-00');
95
96     my $item = $hold->item;
97     my $patron = $hold->borrower;
98     my $biblio = $item->biblio;
99     my $holdingbranch = $item->holdingbranch;
100     my $homebranch = $item->homebranch;
101
102     my %getreserv = (
103         title             => $biblio->title,
104         itemnumber        => $item->itemnumber,
105         waitingdate       => $hold->waitingdate,
106         reservedate       => $hold->reservedate,
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     $getreserv{patron} = $patron;
134
135     if ($today > $calcDate) {
136         if ($cancelall) {
137             my $res = cancel( $item->itemnumber, $patron->borrowernumber, $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   => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
158     ReservesMaxPickUpDelay => $max_pickup_delay,
159     tab => $tab,
160 );
161
162 # Checking if there is a Fast Cataloging Framework
163 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
164
165 if ($item && $tab eq 'holdsover' && !@cancel_result) {
166     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl#holdsover");
167 } elsif ($cancelall) {
168     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl");
169 } else {
170     output_html_with_http_headers $input, $cookie, $template->output;
171 }
172
173 exit;
174
175 sub cancel {
176     my ($item, $borrowernumber, $fbr, $tbr, $skip_transfers ) = @_;
177
178     my $transfer = $fbr ne $tbr; # XXX && !$nextreservinfo;
179
180     return if $transfer && $skip_transfers;
181
182     my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
183
184 #       if the document is not in his homebranch location and there is not reservation after, we transfer it
185     if ($transfer && !$nextreservinfo) {
186         ModItemTransfer( $item, $fbr, $tbr );
187     }
188     # if we have a result
189     if ($nextreservinfo) {
190         my %res;
191         my $patron = Koha::Patrons->find( $nextreservinfo );
192         my $title = Koha::Items->find( $item )->biblio->title;
193         if ( $messages->{'transfert'} ) {
194             $res{messagetransfert} = $messages->{'transfert'};
195             $res{branchcode}       = $messages->{'transfert'};
196         }
197
198         $res{message}             = 1;
199         $res{nextreservnumber}    = $nextreservinfo;
200         $res{nextreservsurname}   = $patron->surname;
201         $res{nextreservfirstname} = $patron->firstname;
202         $res{nextreservitem}      = $item;
203         $res{nextreservtitle}     = $title;
204         $res{waiting}             = $messages->{'waiting'} ? 1 : 0;
205
206         return \%res;
207     }
208
209     return;
210 }