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