(BUG #4806) circ/circulation.tmpl: Add "reservenumber" in the reserved table.
[wip/koha-chris_n.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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 my $all_branches   = $input->param('allbranches') || '';
48
49 my $cancel;
50
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52     {
53         template_name   => "circ/waitingreserves.tmpl",
54         query           => $input,
55         type            => "intranet",
56         authnotrequired => 0,
57         flagsrequired   => { circulate => "circulate_remaining_permissions" },
58         debug           => 1,
59     }
60 );
61
62 my $default = C4::Context->userenv->{'branch'};
63
64 # if we have a return from the form we launch the subroutine CancelReserve
65 if ($item) {
66     my ( $messages, $nextreservinfo ) = ModReserveCancelAll( $item, $borrowernumber );
67     # if we have a result
68     if ($nextreservinfo) {
69         my $borrowerinfo = GetMemberDetails( $nextreservinfo );
70         my $iteminfo = GetBiblioFromItemNumber($item);
71         if ( $messages->{'transfert'} ) {
72             $template->param(
73                 messagetransfert => $messages->{'transfert'},
74                 branchname       => GetBranchName($messages->{'transfert'}),
75             );
76         }
77
78         $template->param(
79             message             => 1,
80             nextreservnumber    => $nextreservinfo,
81             nextreservsurname   => $borrowerinfo->{'surname'},
82             nextreservfirstname => $borrowerinfo->{'firstname'},
83             nextreservitem      => $item,
84             nextreservtitle     => $iteminfo->{'title'},
85             waiting             => ($messages->{'waiting'}) ? 1 : 0,
86         );
87     }
88
89 #       if the document is not in his homebranch location and there is not reservation after, we transfer it
90     if ($fbr ne $tbr  and not $nextreservinfo) {
91         ModItemTransfer( $item, $fbr, $tbr );
92     }
93 }
94 if ( C4::Context->preference('IndependantBranches') ) {
95     undef $all_branches;
96 } else {
97     $template->param( all_branches_link => $input->url . '?allbranches=1&' . $input->query_string )
98       unless $all_branches;
99 }
100
101 my (@reservloop, @overloop);
102 my ($reservcount, $overcount);
103 my @getreserves = $all_branches ? GetReservesForBranch() : GetReservesForBranch($default);
104 # get reserves for the branch we are logged into, or for all branches
105
106 my $today = Date_to_Days(&Today);
107 foreach my $num (@getreserves) {
108     next unless ($num->{'waitingdate'} && $num->{'waitingdate'} ne '0000-00-00');
109     my %getreserv;
110     my $gettitle     = GetBiblioFromItemNumber( $num->{'itemnumber'} );
111     # fix up item type for display
112     $gettitle->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $gettitle->{'itype'} : $gettitle->{'itemtype'};
113     my $getborrower  = GetMemberDetails( $num->{'borrowernumber'} );
114     my $itemtypeinfo = getitemtypeinfo( $gettitle->{'itemtype'} );  # using the fixed up itype/itemtype
115     $getreserv{'waitingdate'} = format_date( $num->{'waitingdate'} );
116
117     my ( $waiting_year, $waiting_month, $waiting_day ) = split (/-/, $num->{'waitingdate'});
118     ( $waiting_year, $waiting_month, $waiting_day ) =
119       Add_Delta_Days( $waiting_year, $waiting_month, $waiting_day,
120         C4::Context->preference('ReservesMaxPickUpDelay'));
121     my $calcDate = Date_to_Days( $waiting_year, $waiting_month, $waiting_day );
122
123     $getreserv{'itemtype'}       = $itemtypeinfo->{'description'};
124     $getreserv{'title'}          = $gettitle->{'title'};
125     $getreserv{'itemnumber'}     = $gettitle->{'itemnumber'};
126     $getreserv{'biblionumber'}   = $gettitle->{'biblionumber'};
127     $getreserv{'barcode'}        = $gettitle->{'barcode'};
128     $getreserv{'homebranch'}     = GetBranchName($gettitle->{'homebranch'});
129     $getreserv{'holdingbranch'}  = $gettitle->{'holdingbranch'};
130     $getreserv{'itemcallnumber'} = $gettitle->{'itemcallnumber'};
131     if ( $gettitle->{'homebranch'} ne $gettitle->{'holdingbranch'} ) {
132         $getreserv{'dotransfer'} = 1;
133     }
134     $getreserv{'borrowernum'}       = $getborrower->{'borrowernumber'};
135     $getreserv{'borrowername'}      = $getborrower->{'surname'};
136     $getreserv{'borrowerfirstname'} = $getborrower->{'firstname'};
137     $getreserv{'borrowerphone'}     = $getborrower->{'phone'};
138     if ( $getborrower->{'emailaddress'} ) {
139         $getreserv{'borrowermail'}  = $getborrower->{'emailaddress'};
140     }
141  
142     if ($today > $calcDate) {
143         push @overloop,   \%getreserv;
144         $overcount++;
145     }else{
146         push @reservloop, \%getreserv;
147         $reservcount++;
148     }
149     
150 }
151
152 $template->param(
153     reserveloop => \@reservloop,
154     reservecount => $reservcount,
155     overloop    => \@overloop,
156     overcount   => $overcount,
157     show_date   => format_date(C4::Dates->today('iso')),
158         dateformat  => C4::Context->preference("dateformat"),
159 );
160
161 output_html_with_http_headers $input, $cookie, $template->output;