replacing links to bookfunds into budgets
[koha.git] / acqui / acqui-home.pl
blobf94ecc9d8d492d42a2831f8b5ea306ad3ed92c56
1 #!/usr/bin/perl
3 # Copyright 2008 - 2009 BibLibre SARL
4 # This file is part of Koha.
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.
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.
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
21 =head1 NAME
23 acqui-home.pl
25 =head1 DESCRIPTION
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.
31 =head1 CGI PARAMETERS
33 =over 4
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
39 =back
41 =cut
43 use strict;
44 use Number::Format;
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;
56 my $query = new CGI;
57 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
59 template_name => "acqui/acqui-home.tmpl",
60 query => $query,
61 type => "intranet",
62 authnotrequired => 0,
63 flagsrequired => { acquisition => "*" },
64 debug => 1,
68 # budget
69 my $borrower= GetMember('borrowernumber' => $loggedinuser);
70 my ( $flags, $homebranch )= ($borrower->{'flags'},$borrower->{'branchcode'});
72 my @results = GetBookFunds($homebranch);
73 my $count = scalar @results;
74 my $branchname = GetBranchName($homebranch);
76 #my $count = scalar @results;
77 my $count;
78 my $classlist = '';
79 my $total = 0;
80 my $totspent = 0;
81 my $totcomtd = 0;
82 my $totavail = 0;
83 my @loop_budget = ();
85 # ---------------------------------------------------
86 # currencies
87 my $cur;
88 my @rates = GetCurrencies();
89 $count = scalar @rates;
91 my $active_currency = GetCurrency;
92 my $num = new Number::Format(-int_curr_symbol => '',
93 -decimal_digits => "2" );
94 my @loop_currency = ();
95 for ( my $i = 0 ; $i < $count ; $i++ ) {
96 my %line;
97 $line{currency} = $rates[$i]->{'currency'} ;
98 $line{currency_symbol} = $rates[$i]->{'symbol'};
99 $line{rate} = sprintf ( '%.2f', $rates[$i]->{'rate'} );
100 push @loop_currency, \%line;
103 # suggestions
104 my $status = $query->param('status') || "ASKED";
105 my $suggestion = CountSuggestion($status);
106 my $suggestions_loop = &SearchSuggestion( {STATUS=> $status} );
107 # ---------------------------------------------------
108 # number format
109 my $cur_format = C4::Context->preference("CurrencyFormat");
110 my $num;
112 if ( $cur_format eq 'FR' ) {
113 $num = new Number::Format(
114 'decimal_fill' => '2',
115 'decimal_point' => ',',
116 'int_curr_symbol' => '',
117 'mon_thousands_sep' => ' ',
118 'thousands_sep' => ' ',
119 'mon_decimal_point' => ','
121 } else { # US by default..
122 $num = new Number::Format(
123 'int_curr_symbol' => '',
124 'mon_thousands_sep' => ',',
125 'mon_decimal_point' => '.'
129 my $period = GetBudgetPeriod;
130 my $budget_period_id = $period->{budget_period_id};
131 my $budget_branchcode = $period->{budget_branchcode};
132 my $moo = GetBudgetHierarchy( $budget_period_id, $homebranch, $template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'} );
133 my @results = @$moo;
134 my $period_total = 0;
135 my $toggle = 0;
136 my @loop;
137 my ( $total, $totspent, $totcomtd, $totavail );
139 foreach my $result (@results) {
140 # only get top-level budgets for display
141 # warn $result->{'budget_branchcode'};
143 $period_total += $result->{'budget_amount'};
145 my $a = $result->{'budget_code_indent'};
146 $a =~ s/\ /\&nbsp\;/g;
147 $result->{'budget_code_indent'} = $a;
149 my $r = GetBranchName( $result->{'budget_owner_id'} );
150 $result->{'budget_branchname'} = GetBranchName( $result->{'budget_branchcode'} );
152 my $member = GetMember( $result->{'budget_owner_id'} );
153 my $member_full = $member->{'firstname'} . ' ' . $member->{'surname'};
155 $result->{'budget_owner'} = $member_full;
156 $result->{'budget_avail'} = $result->{'budget_amount'} - $result->{'budget_spent'};
157 $result->{'budget_spent'} = GetBudgetSpent( $result->{'budget_id'} );
159 $total += $result->{'budget_amount'};
160 $totspent += $result->{'budget_spent'};
161 $totavail += $result->{'budget_avail'};
163 $result->{'budget_amount'} = $num->format_price( $result->{'budget_amount'} );
164 $result->{'budget_spent'} = $num->format_price( $result->{'budget_spent'} );
165 $result->{'budget_avail'} = $num->format_price( $result->{'budget_avail'} );
167 # my $spent_percent = ( $result->{'budget_spent'} / $result->{'budget_amount'} ) * 100;
168 # $result->{'budget_spent_percent'} = sprintf( "%00d", $spent_percent );
170 my $borrower = &GetMember( $result->{budget_owner_id} );
171 $result->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
173 push( @loop_budget, { %{$result}, toggle => $toggle++ % 2, } );
177 # ---------------------------------------------------
179 =c FIXME
182 ### $cur
184 ## suggestions
186 my $dbh = C4::Context->dbh;
189 ## liste des domaines
191 my $sth=$dbh->prepare("
192 SELECT bookfundgroupnumber,bookfundgroupname
193 FROM `aq2bookfundgroups`
194 ORDER BY Bookfundgroupname
196 $sth->execute;
198 my @bookfundgroup_loop; ## liste des domaines
200 while (my $row=$sth->fetchrow_hashref) {
201 push @bookfundgroup_loop,$row;
203 $sth->finish;
206 ## liste des BFG ayant des suggestions à traiter
209 ## nowsuggestions = Number Of Waiting Suggestions
211 my $dbh = C4::Context->dbh;
213 my $sth=$dbh->prepare("
214 SELECT bookfundgroupnumber, count(*) AS nowsuggestions
215 FROM `aq2orders`
216 WHERE step=2
217 AND STATUS='ASKED'
218 GROUP BY bookfundgroupnumber
220 $sth->execute;
222 my @nowsuggestionsneq0_loop; ## liste des BFG ayant des suggestions à traiter
224 while (my $row=$sth->fetchrow_hashref) {
225 push @nowsuggestionsneq0_loop,$row;
227 $sth->finish;
230 ## liste des BFG avec l'effectif des suggestions à traiter (effectif éventuellement nul)
232 my @nowsuggestions_loop;
234 foreach my $data1 (@bookfundgroup_loop) {
235 $data1->{'nowsuggestions'}=0;
236 foreach my $data2 (@nowsuggestionsneq0_loop) {
237 if ($data1->{'bookfundgroupnumber'}==$data2->{'bookfundgroupnumber'}) {
238 $data1->{'nowsuggestions'}=$data2->{'nowsuggestions'};
243 =cut
246 $template->param(
247 classlist => $classlist,
248 type => 'intranet',
249 loop_budget => \@loop_budget,
250 loop_currency => \@loop_currency,
251 active_symbol => $active_currency->{'symbol'},
252 branchname => $branchname,
253 budget => $period->{budget_name},
254 total => $num->format_price( $total ),
255 totspent => $num->format_price($totspent ),
256 totcomtd => $num->format_price( $totcomtd ),
257 totavail => $num->format_price( $totavail ),
259 # nowsuggestions_loop => \@nowsuggestions_loop,
260 # bookfundgroup_loop => \@bookfundgroup_loop,
261 # numberofwaitingsuggestionsgpd => $numberofwaitingsuggestionsgpd,
262 # numberofwaitingsuggestionspd => $numberofwaitingsuggestionspd,
263 # suggestions_loop => $suggestions_loop,
267 output_html_with_http_headers $query, $cookie, $template->output;