Bug 10694: (follow-up) fix various issues
[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.tmpl',
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
58 my $num_formatter;
59
60 my $cur_format = C4::Context->preference("CurrencyFormat");
61 if ( $cur_format eq 'FR' ) {
62     $num_formatter = Number::Format->new(
63         'decimal_fill'      => '2',
64         'decimal_point'     => ',',
65         'int_curr_symbol'   => '',
66         'mon_thousands_sep' => ' ',
67         'thousands_sep'     => ' ',
68         'mon_decimal_point' => ','
69     );
70 } else {    # US by default..
71     $num_formatter = Number::Format->new(
72         'int_curr_symbol'   => '',
73         'mon_thousands_sep' => ',',
74         'mon_decimal_point' => '.'
75     );
76 }
77
78 my $status           = $query->param('status') || "ASKED";
79 my $suggestions_count       = CountSuggestion($status);
80
81 my $budget_arr = GetBudgetHierarchy;
82
83 my $total      = 0;
84 my $totspent   = 0;
85 my $totordered = 0;
86 my $totcomtd   = 0;
87 my $totavail   = 0;
88
89 my $total_active        = 0;
90 my $totspent_active     = 0;
91 my $totordered_active   = 0;
92 my $totavail_active     = 0;
93
94 my @budget_loop;
95 foreach my $budget ( @{$budget_arr} ) {
96     next unless (CanUserUseBudget($loggedinuser, $budget, $userflags));
97
98     $budget->{budget_code_indent} =~ s/\ /\&nbsp\;/g;
99
100     $budget->{'budget_branchname'} =
101       GetBranchName( $budget->{'budget_branchcode'} );
102
103     my $member = GetMember( borrowernumber => $budget->{budget_owner_id} );
104     if ($member) {
105         $budget->{budget_owner_firstname} = $member->{'firstname'};
106         $budget->{budget_owner_surname} = $member->{'surname'};
107         $budget->{budget_owner_borrowernumber} = $member->{'borrowernumber'};
108     }
109
110     if ( !defined $budget->{budget_amount} ) {
111         $budget->{budget_amount} = 0;
112     }
113
114     $budget->{'budget_ordered'} = GetBudgetOrdered( $budget->{'budget_id'} );
115     $budget->{'budget_spent'}   = GetBudgetSpent( $budget->{'budget_id'} );
116     if ( !defined $budget->{budget_spent} ) {
117         $budget->{budget_spent} = 0;
118     }
119     if ( !defined $budget->{budget_ordered} ) {
120         $budget->{budget_ordered} = 0;
121     }
122     $budget->{'budget_avail'} =
123       $budget->{'budget_amount'} - ( $budget->{'budget_spent'} + $budget->{'budget_ordered'} );
124
125     $total      += $budget->{'budget_amount'};
126     $totspent   += $budget->{'budget_spent'};
127     $totordered += $budget->{'budget_ordered'};
128     $totavail   += $budget->{'budget_avail'};
129
130     if ($budget->{budget_period_active}){
131         $total_active      += $budget->{'budget_amount'};
132         $totspent_active   += $budget->{'budget_spent'};
133         $totordered_active += $budget->{'budget_ordered'};
134         $totavail_active   += $budget->{'budget_avail'};    
135     }
136
137     for my $field (qw( budget_amount budget_spent budget_ordered budget_avail ) ) {
138         $budget->{"formatted_$field"} = $num_formatter->format_price( $budget->{$field} );
139     }
140
141     push @budget_loop, $budget;
142 }
143
144 $template->param(
145     type          => 'intranet',
146     loop_budget   => \@budget_loop,
147     branchname    => $branchname,
148     total         => $num_formatter->format_price($total),
149     totspent      => $num_formatter->format_price($totspent),
150     totordered    => $num_formatter->format_price($totordered),
151     totcomtd      => $num_formatter->format_price($totcomtd),
152     totavail      => $num_formatter->format_price($totavail),
153     total_active  => $num_formatter->format_price($total_active),
154     totspent_active     => $num_formatter->format_price($totspent_active),
155     totordered_active   => $num_formatter->format_price($totordered_active),
156     totavail_active     => $num_formatter->format_price($totavail_active),
157     suggestions_count   => $suggestions_count,
158 );
159
160 output_html_with_http_headers $query, $cookie, $template->output;