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