BIG COMMIT : cleaning of Reserves.pm. See detail on koha-devel
[koha.git] / circ / transferstoreceive.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::Biblio;
30 use C4::Circulation;
31 use C4::Members;
32 use C4::Interface::CGI::Output;
33 use Date::Calc qw(
34   Today
35   Add_Delta_Days
36   Date_to_Days
37 );
38
39 use C4::Koha;
40 use C4::Reserves;
41
42 my $input = new CGI;
43
44 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
45 my $itemnumber = $input->param('itemnumber');
46 my $todaysdate = join "-", &Today;
47
48 # if we have a resturn of the form to delete the transfer, we launch the subrroutine
49 if ($itemnumber) {
50     C4::Circulation::Circ2::DeleteTransfer($itemnumber);
51 }
52
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54     {
55         template_name   => "circ/transferstoreceive.tmpl",
56         query           => $input,
57         type            => "intranet",
58         authnotrequired => 0,
59         flagsrequired   => { circulate => 1 },
60         debug           => 1,
61     }
62 );
63
64 # set the userenv branch
65 my $default = C4::Context->userenv->{'branch'};
66
67 # get the all the branches for reference
68 my $branches = GetBranches();
69 my @branchesloop;
70 foreach my $br ( keys %$branches ) {
71     my @transferloop;
72     my %branchloop;
73     $branchloop{'branchname'} = $branches->{$br}->{'branchname'};
74     $branchloop{'branchcode'} = $branches->{$br}->{'branchcode'};
75     my @gettransfers =
76       GetTransfersFromTo( $branches->{$br}->{'branchcode'}, $default );
77
78     if (@gettransfers) {
79         foreach my $num (@gettransfers) {
80             my %getransf;
81
82             my ( $sent_year, $sent_month, $sent_day ) = split "-",
83               $num->{'datesent'};
84             $sent_day = ( split " ", $sent_day )[0];
85             ( $sent_year, $sent_month, $sent_day ) =
86               Add_Delta_Days( $sent_year, $sent_month, $sent_day,
87                 C4::Context->preference('TransfersMaxDaysWarning'));
88             my $calcDate = Date_to_Days( $sent_year, $sent_month, $sent_day );
89             my $today    = Date_to_Days(&Today);
90             my $warning  = ( $today > $calcDate );
91
92             if ( $warning > 0 ) {
93                 $getransf{'messcompa'} = 1;
94             }
95             my $gettitle     = GetBiblioFromItemNumber( $num->{'itemnumber'} );
96             my $itemtypeinfo = getitemtypeinfo( $gettitle->{'itemtype'} );
97
98             $getransf{'title'}        = $gettitle->{'title'};
99             $getransf{'datetransfer'} = format_date( $num->{'datesent'} );
100             $getransf{'biblionumber'} = $gettitle->{'biblionumber'};
101             $getransf{'itemnumber'}   = $gettitle->{'itemnumber'};
102             $getransf{'barcode'}      = $gettitle->{'barcode'};
103             $getransf{'itemtype'}       = $itemtypeinfo->{'description'};
104             $getransf{'homebranch'}     = $gettitle->{'homebranch'};
105             $getransf{'holdingbranch'}  = $gettitle->{'holdingbranch'};
106             $getransf{'itemcallnumber'} = $gettitle->{'itemcallnumber'};
107
108             #                           we check if we have a reserv for this transfer
109             my @checkreserv = GetReservesFromItemnumber($num->{'itemnumber'} );
110             if ( $checkreserv[0] ) {
111                 my $getborrower =
112                   GetMemberDetails( $checkreserv[1] );
113                 $getransf{'borrowernum'}  = $getborrower->{'borrowernumber'};
114                 $getransf{'borrowername'} = $getborrower->{'surname'};
115                 $getransf{'borrowerfirstname'} = $getborrower->{'firstname'};
116                 if ( $getborrower->{'emailaddress'} ) {
117                     $getransf{'borrowermail'} = $getborrower->{'emailaddress'};
118                 }
119                 $getransf{'borrowerphone'} = $getborrower->{'phone'};
120
121             }
122             push( @transferloop, \%getransf );
123         }
124
125       #                 If we have a return of reservloop we put it in the branchloop sequence
126         $branchloop{'reserv'} = \@transferloop;
127     }
128     else {
129
130 #       if we don't have a retrun from reservestobranch we unset branchname and branchcode
131         $branchloop{'branchname'} = 0;
132         $branchloop{'branchcode'} = 0;
133     }
134     push( @branchesloop, \%branchloop );
135 }
136
137 $template->param(
138     branchesloop => \@branchesloop,
139     show_date    => format_date($todaysdate),
140 );
141
142 output_html_with_http_headers $input, $cookie, $template->output;
143