(bug #3825) improve waiting holds
[koha.git] / circ / waitingreserves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Branch; # GetBranchName
26 use C4::Auth;
27 use C4::Dates qw/format_date/;
28 use C4::Circulation;
29 use C4::Members;
30 use C4::Biblio;
31 use C4::Items;
32
33 use Date::Calc qw(
34   Today
35   Add_Delta_Days
36   Date_to_Days
37 );
38 use C4::Reserves;
39 use C4::Koha;
40
41 my $input = new CGI;
42
43 my $item           = $input->param('itemnumber');
44 my $borrowernumber = $input->param('borrowernumber');
45 my $fbr            = $input->param('fbr') || '';
46 my $tbr            = $input->param('tbr') || '';
47
48 my $cancel;
49
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
51     {
52         template_name   => "circ/waitingreserves.tmpl",
53         query           => $input,
54         type            => "intranet",
55         authnotrequired => 0,
56         flagsrequired   => { circulate => "circulate_remaining_permissions" },
57         debug           => 1,
58     }
59 );
60
61 my $default = C4::Context->userenv->{'branch'};
62
63 # if we have a return from the form we launch the subroutine CancelReserve
64 if ($item) {
65     my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
66     # if we have a result
67     if ($nextreservinfo) {
68         my $borrowerinfo = GetMemberDetails( $nextreservinfo );
69         my $iteminfo = GetBiblioFromItemNumber($item);
70         if ( $messages->{'transfert'} ) {
71             $template->param(
72                 messagetransfert => $messages->{'transfert'},
73                 branchname       => GetBranchName($messages->{'transfert'}),
74             );
75         }
76
77         $template->param(
78             message             => 1,
79             nextreservnumber    => $nextreservinfo,
80             nextreservsurname   => $borrowerinfo->{'surname'},
81             nextreservfirstname => $borrowerinfo->{'firstname'},
82             nextreservitem      => $item,
83             nextreservtitle     => $iteminfo->{'title'},
84             waiting             => ($messages->{'waiting'}) ? 1 : 0,
85         );
86     }
87
88 #       if the document is not in his homebranch location and there is not reservation after, we transfer it
89     if ($fbr ne $tbr  and not $nextreservinfo) {
90         ModItemTransfer( $item, $fbr, $tbr );
91     }
92 }
93
94 my (@reservloop, @overloop);
95 my ($reservcount, $overcount);
96 my @getreserves = $default ? GetReservesForBranch($default) : GetReservesForBranch();
97 # get reserves for the branch we are logged into, or for all branches
98
99 my $today = Date_to_Days(&Today);
100 foreach my $num (@getreserves) {
101     next unless ($num->{'waitingdate'} && $num->{'waitingdate'} ne '0000-00-00');
102     my %getreserv;
103     my $gettitle     = GetBiblioFromItemNumber( $num->{'itemnumber'} );
104     # fix up item type for display
105     $gettitle->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $gettitle->{'itype'} : $gettitle->{'itemtype'};
106     my $getborrower  = GetMemberDetails( $num->{'borrowernumber'} );
107     my $itemtypeinfo = getitemtypeinfo( $gettitle->{'itemtype'} );  # using the fixed up itype/itemtype
108     $getreserv{'waitingdate'} = format_date( $num->{'waitingdate'} );
109
110     my ( $waiting_year, $waiting_month, $waiting_day ) = split (/-/, $num->{'waitingdate'});
111     ( $waiting_year, $waiting_month, $waiting_day ) =
112       Add_Delta_Days( $waiting_year, $waiting_month, $waiting_day,
113         C4::Context->preference('ReservesMaxPickUpDelay'));
114     my $calcDate = Date_to_Days( $waiting_year, $waiting_month, $waiting_day );
115
116     $getreserv{'itemtype'}       = $itemtypeinfo->{'description'};
117     $getreserv{'title'}          = $gettitle->{'title'};
118     $getreserv{'itemnumber'}     = $gettitle->{'itemnumber'};
119     $getreserv{'biblionumber'}   = $gettitle->{'biblionumber'};
120     $getreserv{'barcode'}        = $gettitle->{'barcode'};
121     $getreserv{'homebranch'}     = GetBranchName($gettitle->{'homebranch'});
122     $getreserv{'holdingbranch'}  = $gettitle->{'holdingbranch'};
123     $getreserv{'itemcallnumber'} = $gettitle->{'itemcallnumber'};
124     if ( $gettitle->{'homebranch'} ne $gettitle->{'holdingbranch'} ) {
125         $getreserv{'dotransfer'} = 1;
126     }
127     $getreserv{'borrowernum'}       = $getborrower->{'borrowernumber'};
128     $getreserv{'borrowername'}      = $getborrower->{'surname'};
129     $getreserv{'borrowerfirstname'} = $getborrower->{'firstname'};
130     $getreserv{'borrowerphone'}     = $getborrower->{'phone'};
131     if ( $getborrower->{'emailaddress'} ) {
132         $getreserv{'borrowermail'}  = $getborrower->{'emailaddress'};
133     }
134  
135     if ($today > $calcDate) {
136         push @overloop,   \%getreserv;
137         $overcount++;
138     }else{
139         push @reservloop, \%getreserv;
140         $reservcount++;
141     }
142     
143 }
144
145 $template->param(
146     reserveloop => \@reservloop,
147     reservecount => $reservcount,
148     overloop    => \@overloop,
149     overcount   => $overcount,
150     show_date   => format_date(C4::Dates->today('iso')),
151         dateformat  => C4::Context->preference("dateformat"),
152 );
153
154 output_html_with_http_headers $input, $cookie, $template->output;