[MT2593] Fixed budget amount computation in acqui-home
[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 with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA  02111-1307 USA
18
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
39 =back
40
41 =cut
42
43 use strict;
44 use warnings;
45 use Number::Format;
46
47 use CGI;
48 use C4::Auth;
49 use C4::Output;
50 use C4::Suggestions;
51 use C4::Acquisition;
52 use C4::Budgets;
53 use C4::Members;
54 use C4::Branch;
55 use C4::Debug;
56
57 my $query = new CGI;
58 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
59     {
60         template_name   => "acqui/acqui-home.tmpl",
61         query           => $query,
62         type            => "intranet",
63         authnotrequired => 0,
64         flagsrequired   => { acquisition => "*" },
65         debug           => 1,
66     }
67 );
68
69 # budget
70 my $borrower= GetMember('borrowernumber' => $loggedinuser);
71 my ( $flags, $homebranch )= ($borrower->{'flags'},$borrower->{'branchcode'});
72
73 my @results = GetBudgets($homebranch);
74 my $count = scalar @results;
75 my $branchname = GetBranchName($homebranch);
76
77 #my $count = scalar @results;
78 my $classlist   = '';
79 my $total       = 0;
80 my $totspent    = 0;
81 my $totordered  = 0;
82 my $totcomtd    = 0;
83 my $totavail    = 0;
84 my @loop_budget = ();
85
86 # ---------------------------------------------------
87 # currencies
88 my $cur;
89 my @rates = GetCurrencies();
90 $count = scalar @rates;
91
92 my $active_currency = GetCurrency;
93 my $num;
94
95 my $cur_format = C4::Context->preference("CurrencyFormat");
96 if ( $cur_format eq 'FR' ) {
97     $num = new Number::Format(
98         'decimal_fill'      => '2',
99         'decimal_point'     => ',',
100         'int_curr_symbol'   => '',
101         'mon_thousands_sep' => ' ',
102         'thousands_sep'     => ' ',
103         'mon_decimal_point' => ','
104     );
105 } else {  # US by default..
106     $num = new Number::Format(
107         'int_curr_symbol'   => '',
108         'mon_thousands_sep' => ',',
109         'mon_decimal_point' => '.'
110     );
111 }
112
113 my @loop_currency = ();
114 for ( my $i = 0 ; $i < $count ; $i++ ) {
115     my %line;
116     $line{currency}        = $rates[$i]->{'currency'} ;
117     $line{currency_symbol} = $rates[$i]->{'symbol'};
118     $line{rate}            = sprintf ( '%.2f',  $rates[$i]->{'rate'} );
119     push @loop_currency, \%line;
120 }
121
122 # suggestions
123 my $status           = $query->param('status') || "ASKED";
124 my $suggestion       = CountSuggestion($status);
125 my $suggestions_loop = &SearchSuggestion( {STATUS=> $status} );
126 # ---------------------------------------------------
127 # number format
128 my $period            = GetBudgetPeriod;
129 my $budget_period_id  = $period->{budget_period_id};
130 my $budget_branchcode = $period->{budget_branchcode};
131 my $moo               = GetBudgetHierarchy('',$homebranch, $template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'} );
132 @results           = @$moo;
133 my $period_total      = 0;
134 my $toggle            = 0;
135 my @loop;
136
137 foreach my $result (@results) {
138     # only get top-level budgets for display
139     #         warn  $result->{'budget_branchcode'};
140
141     $period_total += $result->{'budget_amount'};
142
143     my $a = $result->{'budget_code_indent'};
144     $a =~ s/\ /\&nbsp\;/g;
145     $result->{'budget_code_indent'} = $a;
146
147     my $r = GetBranchName( $result->{'budget_owner_id'} );
148     $result->{'budget_branchname'} = GetBranchName( $result->{'budget_branchcode'} );
149
150     my $member      = GetMember( $result->{'budget_owner_id'} );
151     my $member_full = $member->{'firstname'} . ' ' . $member->{'surname'} if $member;
152
153     $result->{'budget_owner'}   = $member_full;
154     $result->{'budget_ordered'} = GetBudgetOrdered( $result->{'budget_id'} );
155     $result->{'budget_spent'}   = GetBudgetSpent( $result->{'budget_id'} );
156     $result->{'budget_avail'}   = $result->{'budget_amount'} - $result->{'budget_spent'} - $result->{'budget_ordered'};
157
158     $total      += $result->{'budget_amount'};
159     $totspent   += $result->{'budget_spent'};
160     $totordered += $result->{'budget_ordered'};
161     $totavail   += $result->{'budget_avail'};
162
163     $result->{'budget_amount'}  = $num->format_price( $result->{'budget_amount'} );
164     $result->{'budget_spent'}   = $num->format_price( $result->{'budget_spent'} );
165     $result->{'budget_ordered'} = $num->format_price( $result->{'budget_ordered'} );
166     $result->{'budget_avail'}   = $num->format_price( $result->{'budget_avail'} );
167
168     #        my $spent_percent = ( $result->{'budget_spent'} / $result->{'budget_amount'} ) * 100;
169     #        $result->{'budget_spent_percent'} = sprintf( "%00d", $spent_percent );
170
171     my $borrower = &GetMember( $result->{budget_owner_id} );
172     $result->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'} if $borrower;
173
174     push( @loop_budget, { %{$result}, toggle => $toggle++ % 2, } );
175 }
176
177 $template->param(
178     classlist     => $classlist,
179     type          => 'intranet',
180     loop_budget   => \@loop_budget,
181     loop_currency => \@loop_currency,
182     active_symbol => $active_currency->{'symbol'},
183     branchname    => $branchname,
184     budget        => $period->{budget_name},
185     total         => $num->format_price(  $total ),
186     totspent      => $num->format_price( $totspent ),
187     totordered    => $num->format_price( $totordered ),
188     totcomtd      => $num->format_price( $totcomtd ),
189     totavail      => $num->format_price( $totavail ),
190     suggestion    => $suggestion,
191 );
192
193 output_html_with_http_headers $query, $cookie, $template->output;