HUGE COMMIT : code cleaning circulation.
[koha.git] / circ / currenttransfers.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use CGI;
24 use C4::Context;
25 use C4::Output;
26 use C4::Branch;
27 use C4::Auth;
28 use C4::Date;
29 use C4::Circulation;
30 use C4::Interface::CGI::Output;
31 use Date::Calc qw(
32   Today
33   Add_Delta_Days
34   Date_to_Days
35 );
36
37 use C4::Koha;
38 use C4::Reserves2;
39
40 my $input = new CGI;
41
42 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
43 my $itemnumber = $input->param('itemnumber');
44 my $todaysdate = join "-", &Today;
45
46 # if we have a resturn of the form to delete the transfer, we launch the subrroutine
47 if ($itemnumber) {
48     C4::Circulation::Circ2::DeleteTransfer($itemnumber);
49 }
50
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52     {
53         template_name   => "circ/currenttransfers.tmpl",
54         query           => $input,
55         type            => "intranet",
56         authnotrequired => 0,
57         flagsrequired   => { circulate => 1 },
58         debug           => 1,
59     }
60 );
61
62 # set the userenv branch
63 my $default = C4::Context->userenv->{'branch'};
64
65 # get the all the branches for reference
66 my $branches = GetBranches();
67 my @branchesloop;
68 foreach my $br ( keys %$branches ) {
69     my @transferloop;
70     my %branchloop;
71     $branchloop{'branchname'} = $branches->{$br}->{'branchname'};
72     $branchloop{'branchcode'} = $branches->{$br}->{'branchcode'};
73     my @gettransfers =
74       GetTransfersFromTo( $branches->{$br}->{'branchcode'}, $default );
75
76     if (@gettransfers) {
77         foreach my $num (@gettransfers) {
78             my %getransf;
79             my %env;
80
81             my ( $sent_year, $sent_month, $sent_day ) = split "-",
82               $num->{'datesent'};
83             $sent_day = ( split " ", $sent_day )[0];
84             ( $sent_year, $sent_month, $sent_day ) =
85               Add_Delta_Days( $sent_year, $sent_month, $sent_day,
86                 C4::Context->preference('TransfersMaxDaysWarning'));
87             my $calcDate = Date_to_Days( $sent_year, $sent_month, $sent_day );
88             my $today    = Date_to_Days(&Today);
89             my $warning  = ( $today > $calcDate );
90
91             if ( $warning > 0 ) {
92                 $getransf{'messcompa'} = 1;
93             }
94             my $gettitle     = GetBiblioFromItemNumber( $num->{'itemnumber'} );
95             my $itemtypeinfo = getitemtypeinfo( $gettitle->{'itemtype'} );
96
97             $getransf{'title'}        = $gettitle->{'title'};
98             $getransf{'datetransfer'} = format_date( $num->{'datesent'} );
99             $getransf{'biblionumber'} = $gettitle->{'biblionumber'};
100             $getransf{'itemnumber'}   = $gettitle->{'itemnumber'};
101             $getransf{'barcode'}      = $gettitle->{'barcode'};
102             $getransf{'itemtype'}       = $itemtypeinfo->{'description'};
103             $getransf{'homebranch'}     = $gettitle->{'homebranch'};
104             $getransf{'holdingbranch'}  = $gettitle->{'holdingbranch'};
105             $getransf{'itemcallnumber'} = $gettitle->{'itemcallnumber'};
106
107             #                           we check if we have a reserv for this transfer
108             my @checkreserv = GetReservations( $num->{'itemnumber'} );
109             if ( $checkreserv[0] ) {
110                 my $getborrower =
111                   GetMemberDetails( $checkreserv[1] );
112                 $getransf{'borrowernum'}  = $getborrower->{'borrowernumber'};
113                 $getransf{'borrowername'} = $getborrower->{'surname'};
114                 $getransf{'borrowerfirstname'} = $getborrower->{'firstname'};
115                 if ( $getborrower->{'emailaddress'} ) {
116                     $getransf{'borrowermail'} = $getborrower->{'emailaddress'};
117                 }
118                 $getransf{'borrowerphone'} = $getborrower->{'phone'};
119
120             }
121             push( @transferloop, \%getransf );
122         }
123
124       #                 If we have a return of reservloop we put it in the branchloop sequence
125         $branchloop{'reserv'} = \@transferloop;
126     }
127     else {
128
129 #       if we don't have a retrun from reservestobranch we unset branchname and branchcode
130         $branchloop{'branchname'} = 0;
131         $branchloop{'branchcode'} = 0;
132     }
133     push( @branchesloop, \%branchloop );
134 }
135
136 $template->param(
137     branchesloop => \@branchesloop,
138     show_date    => format_date($todaysdate),
139 );
140
141 output_html_with_http_headers $input, $cookie, $template->output;
142