Code cleaning : POD added, GPL header added, SQL queries moved on modules.
[koha.git] / acqui / acqui-home.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 # $Id$
19
20
21 =head1 NAME
22
23 acqui-home.pl
24
25 =head1 DESCRIPTION
26
27 this script is the main page for acqui/
28 It presents the budget's dashboard, another table about differents currency with 
29 their rates and the pending suggestions.
30
31 =head1 CGI PARAMETERS
32
33 =over 4
34
35 =item $status
36 C<$status> is the status a suggestion could has. Default value is 'ASKED'.
37 thus, it can be REJECTED, ACCEPTED, ORDERED, ASKED, AVAIBLE
38 =back
39
40 =cut
41
42 use strict;
43 use CGI;
44 use C4::Auth;
45 use C4::Output;
46 use C4::Interface::CGI::Output;
47 use C4::Database;
48 use C4::Suggestions;
49 use HTML::Template;
50 use C4::Acquisition;
51 use C4::Members;
52
53 my $query = new CGI;
54 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
55     {
56         template_name   => "acqui/acqui-home.tmpl",
57         query           => $query,
58         type            => "intranet",
59         authnotrequired => 0,
60         flagsrequired   => { acquisition => 1 },
61         debug           => 1,
62     }
63 );
64
65 # budget
66 my ( $flags, $homebranch ) = GetFlagsAndBranchFromBorrower($loggedinuser);
67
68 my @results = GetBookFunds($homebranch);
69 my $count = scalar @results;
70
71 my $classlist   = '';
72 my $total       = 0;
73 my $totspent    = 0;
74 my $totcomtd    = 0;
75 my $totavail    = 0;
76 my @loop_budget = ();
77
78 for (my $i=0; $i<$count; $i++){
79         my ($spent,$comtd)=GetBookFundBreakdown($results[$i]->{'bookfundid'});
80         my $avail=$results[$i]->{'budgetamount'}-($spent+$comtd);
81         my %line;
82         $line{bookfundname} = $results[$i]->{'bookfundname'};
83         $line{budgetamount} = $results[$i]->{'budgetamount'};
84         $line{spent} = sprintf  ("%.2f", $spent);
85         $line{comtd} = sprintf  ("%.2f",$comtd);
86         $line{avail}  = sprintf  ("%.2f",$avail);
87         push @loop_budget, \%line;
88         $total+=$results[$i]->{'budgetamount'};
89         $totspent+=$spent;
90         $totcomtd+=$comtd;
91         $totavail+=$avail;
92 }
93
94 # currencies
95 my @rates = GetCurrencies();
96 my $count = scalar @rates;
97
98 my @loop_currency = ();
99 for ( my $i = 0 ; $i < $count ; $i++ ) {
100     my %line;
101     $line{currency} = @rates[$i]->{'currency'};
102     $line{rate}     = @rates[$i]->{'rate'};
103     push @loop_currency, \%line;
104 }
105
106 # suggestions
107 my $status           = $query->param('status') || "ASKED";
108 my $suggestion       = CountSuggestion($status);
109 my $suggestions_loop = &SearchSuggestion( '', '', '', '', $status, '' );
110
111 $template->param(
112     classlist        => $classlist,
113     type             => 'intranet',
114     loop_budget      => \@loop_budget,
115     loop_currency    => \@loop_currency,
116     total            => sprintf( "%.2f", $total ),
117     suggestion       => $suggestion,
118     suggestions_loop => $suggestions_loop,
119     totspent         => sprintf( "%.2f", $totspent ),
120     totcomtd         => sprintf( "%.2f", $totcomtd ),
121     totavail         => sprintf( "%.2f", $totavail ),
122     nobudget         => $#results == -1 ? 1 : 0
123 );
124
125 output_html_with_http_headers $query, $cookie, $template->output;