Bug 30477: Add new UNIMARC installer translation files
[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 qw( output_html_with_http_headers );
25 use C4::Auth qw( get_template_and_user );
26 use C4::Items qw( ModItemTransfer );
27 use Date::Calc qw( Date_to_Days Today );
28 use C4::Reserves qw( ModReserve ModReserveCancelAll );
29 use Koha::DateUtils qw( dt_from_string output_pref );
30 use Koha::BiblioFrameworks;
31 use Koha::Items;
32 use Koha::ItemTypes;
33 use Koha::Patrons;
34 use Koha::BackgroundJob::BatchCancelHold;
35
36 my $input = CGI->new;
37
38 my $item           = $input->param('itemnumber');
39 my $borrowernumber = $input->param('borrowernumber');
40 my $fbr            = $input->param('fbr') || '';
41 my $tbr            = $input->param('tbr') || '';
42 my $all_branches   = $input->param('allbranches') || '';
43 my $cancelall      = $input->param('cancelall');
44 my $tab            = $input->param('tab');
45 my $cancelBulk     = $input->param('cancelBulk');
46
47 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
48     {
49         template_name   => "circ/waitingreserves.tt",
50         query           => $input,
51         type            => "intranet",
52         flagsrequired   => { circulate => "circulate_remaining_permissions" },
53     }
54 );
55
56 my $default = C4::Context->userenv->{'branch'};
57
58 my $transfer_when_cancel_all = C4::Context->preference('TransferWhenCancelAllWaitingHolds');
59 $template->param( TransferWhenCancelAllWaitingHolds => 1 ) if $transfer_when_cancel_all;
60
61 my @cancel_result;
62 # if we have a return from the form we cancel the holds
63 if ($item) {
64     my $res = cancel( $item, $borrowernumber, $fbr, $tbr );
65     push @cancel_result, $res if $res;
66 }
67
68 if ( C4::Context->preference('IndependentBranches') ) {
69     undef $all_branches;
70 } else {
71     $template->param( all_branches_link => '/cgi-bin/koha/circ/waitingreserves.pl' . '?allbranches=1' )
72       unless $all_branches;
73 }
74 $template->param( all_branches => 1 ) if $all_branches;
75
76 if ($cancelBulk) {
77     my $reason   = $input->param("cancellation-reason");
78     my @hold_ids = split( ',', scalar $input->param("ids") );
79     my $params   = {
80         reason   => $reason,
81         hold_ids => \@hold_ids,
82     };
83     my $job_id = Koha::BackgroundJob::BatchCancelHold->new->enqueue($params);
84
85     $template->param(
86         enqueued => 1,
87         job_id   => $job_id
88     );
89 }
90
91 my (@reserve_loop, @over_loop);
92 # FIXME - Is priority => 0 useful? If yes it must be moved to waiting, otherwise we need to remove it from here.
93 my $holds = Koha::Holds->waiting->search({ priority => 0, ( $all_branches ? () : ( branchcode => $default ) ) }, { order_by => ['waitingdate'] });
94
95 # get reserves for the branch we are logged into, or for all branches
96
97 my $today = Date_to_Days(&Today);
98
99 while ( my $hold = $holds->next ) {
100     next unless $hold->waitingdate;
101
102     my ( $expire_year, $expire_month, $expire_day ) = split (/-/, $hold->expirationdate);
103     my $calcDate = Date_to_Days( $expire_year, $expire_month, $expire_day );
104
105     if ($today > $calcDate) {
106         if ($cancelall) {
107             my $res = cancel( $hold->item->itemnumber, $hold->borrowernumber, $hold->item->holdingbranch, $hold->item->homebranch, !$transfer_when_cancel_all );
108             push @cancel_result, $res if $res;
109             next;
110         } else {
111             push @over_loop, $hold;
112         }
113     }else{
114         push @reserve_loop, $hold;
115     }
116     
117 }
118
119 $template->param(cancel_result => \@cancel_result) if @cancel_result;
120
121 $template->param(
122     reserveloop => \@reserve_loop,
123     reservecount => scalar @reserve_loop,
124     overloop    => \@over_loop,
125     overcount   => scalar @over_loop,
126     show_date   => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
127     tab => $tab,
128 );
129
130 # Checking if there is a Fast Cataloging Framework
131 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
132
133 if ($item && $tab eq 'holdsover' && !@cancel_result) {
134     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl#holdsover");
135 } elsif ($cancelall) {
136     print $input->redirect("/cgi-bin/koha/circ/waitingreserves.pl");
137 } else {
138     output_html_with_http_headers $input, $cookie, $template->output;
139 }
140
141 exit;
142
143 sub cancel {
144     my ($item, $borrowernumber, $fbr, $tbr, $skip_transfers ) = @_;
145
146     my $transfer = $fbr ne $tbr; # XXX && !$nextreservinfo;
147
148     return if $transfer && $skip_transfers;
149
150     my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
151
152 #       if the document is not in his homebranch location and there is not reservation after, we transfer it
153     if ($transfer && !$nextreservinfo) {
154         ModItemTransfer( $item, $fbr, $tbr, 'CancelReserve' );
155     }
156     # if we have a result
157     if ($nextreservinfo) {
158         my %res;
159         my $patron = Koha::Patrons->find( $nextreservinfo );
160         my $title = Koha::Items->find( $item )->biblio->title;
161         if ( $messages->{'transfert'} ) {
162             $res{messagetransfert} = $messages->{'transfert'};
163             $res{branchcode}       = $messages->{'transfert'};
164         }
165
166         $res{message}             = 1;
167         $res{nextreservnumber}    = $nextreservinfo;
168         $res{nextreservsurname}   = $patron->surname;
169         $res{nextreservfirstname} = $patron->firstname;
170         $res{nextreservitem}      = $item;
171         $res{nextreservtitle}     = $title;
172         $res{waiting}             = $messages->{'waiting'} ? 1 : 0;
173
174         return \%res;
175     }
176
177     return;
178 }