Bug 12844: Use Koha::Number::Price where it can be useful
[koha.git] / acqui / acqui-home.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008 - 2009 BibLibre SARL
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 =head1 NAME
20
21 acqui-home.pl
22
23 =head1 DESCRIPTION
24
25 this script is the main page for acqui
26
27 =cut
28
29 use strict;
30 use warnings;
31 use Number::Format;
32
33 use CGI;
34 use C4::Auth;
35 use C4::Output;
36 use C4::Acquisition;
37 use C4::Budgets;
38 use C4::Members;
39 use C4::Branch;
40 use C4::Debug;
41 use C4::Suggestions;
42
43 my $query = CGI->new;
44 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
45     {   template_name   => 'acqui/acqui-home.tt',
46         query           => $query,
47         type            => 'intranet',
48         authnotrequired => 0,
49         flagsrequired   => { acquisition => '*' },
50         debug           => 1,
51     }
52 );
53
54 my $user = GetMember( 'borrowernumber' => $loggedinuser );
55 my $branchname = GetBranchName($user->{branchcode});
56
57 my $status           = $query->param('status') || "ASKED";
58 my $suggestions_count       = CountSuggestion($status);
59
60 my $budget_arr = GetBudgetHierarchy;
61
62 my $total      = 0;
63 my $totspent   = 0;
64 my $totordered = 0;
65 my $totcomtd   = 0;
66 my $totavail   = 0;
67
68 my $total_active        = 0;
69 my $totspent_active     = 0;
70 my $totordered_active   = 0;
71 my $totavail_active     = 0;
72
73 my @budget_loop;
74 foreach my $budget ( @{$budget_arr} ) {
75     next unless (CanUserUseBudget($loggedinuser, $budget, $userflags));
76
77     $budget->{budget_code_indent} =~ s/\ /\&nbsp\;/g;
78
79     $budget->{'budget_branchname'} =
80       GetBranchName( $budget->{'budget_branchcode'} );
81
82     my $member = GetMember( borrowernumber => $budget->{budget_owner_id} );
83     if ($member) {
84         $budget->{budget_owner_firstname} = $member->{'firstname'};
85         $budget->{budget_owner_surname} = $member->{'surname'};
86         $budget->{budget_owner_borrowernumber} = $member->{'borrowernumber'};
87     }
88
89     if ( !defined $budget->{budget_amount} ) {
90         $budget->{budget_amount} = 0;
91     }
92
93     $budget->{'budget_ordered'} = GetBudgetOrdered( $budget->{'budget_id'} );
94     $budget->{'budget_spent'}   = GetBudgetSpent( $budget->{'budget_id'} );
95     if ( !defined $budget->{budget_spent} ) {
96         $budget->{budget_spent} = 0;
97     }
98     if ( !defined $budget->{budget_ordered} ) {
99         $budget->{budget_ordered} = 0;
100     }
101     $budget->{'budget_avail'} =
102       $budget->{'budget_amount'} - ( $budget->{'budget_spent'} + $budget->{'budget_ordered'} );
103
104     $total      += $budget->{'budget_amount'};
105     $totspent   += $budget->{'budget_spent'};
106     $totordered += $budget->{'budget_ordered'};
107     $totavail   += $budget->{'budget_avail'};
108
109     if ($budget->{budget_period_active}){
110         $total_active      += $budget->{'budget_amount'};
111         $totspent_active   += $budget->{'budget_spent'};
112         $totordered_active += $budget->{'budget_ordered'};
113         $totavail_active   += $budget->{'budget_avail'};    
114     }
115
116     push @budget_loop, $budget;
117 }
118
119 $template->param(
120     type          => 'intranet',
121     loop_budget   => \@budget_loop,
122     branchname    => $branchname,
123     total         => $total,
124     totspent      => $totspent,
125     totordered    => $totordered,
126     totcomtd      => $totcomtd,
127     totavail      => $totavail,
128     total_active  => $total_active,
129     totspent_active     => $totspent_active,
130     totordered_active   => $totordered_active,
131     totavail_active     => $totavail_active,
132     suggestions_count   => $suggestions_count,
133 );
134
135 output_html_with_http_headers $query, $cookie, $template->output;