Bug Fixing : 1st Step Bookfund has become Budgets
[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 $count;
78 my $classlist   = '';
79 my $total       = 0;
80 my $totspent    = 0;
81 my $totcomtd    = 0;
82 my $totavail    = 0;
83 my @loop_budget = ();
84
85 # ---------------------------------------------------
86 # currencies
87 my $cur;
88 my @rates = GetCurrencies();
89 $count = scalar @rates;
90
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;
101 }
102
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;
111
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' => ','
120     );
121 } else {  # US by default..
122     $num = new Number::Format(
123         'int_curr_symbol'   => '',
124         'mon_thousands_sep' => ',',
125         'mon_decimal_point' => '.'
126     );
127 }
128
129 my $period            = GetBudgetPeriod;
130 my $budget_period_id  = $period->{budget_period_id};
131 my $budget_branchcode = $period->{budget_branchcode};
132 my $moo               = GetBudgetHierarchy('',$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 );
138
139 foreach my $result (@results) {
140     # only get top-level budgets for display
141     #         warn  $result->{'budget_branchcode'};
142
143     $period_total += $result->{'budget_amount'};
144
145     my $a = $result->{'budget_code_indent'};
146     $a =~ s/\ /\&nbsp\;/g;
147     $result->{'budget_code_indent'} = $a;
148
149     my $r = GetBranchName( $result->{'budget_owner_id'} );
150     $result->{'budget_branchname'} = GetBranchName( $result->{'budget_branchcode'} );
151
152     my $member      = GetMember( $result->{'budget_owner_id'} );
153     my $member_full = $member->{'firstname'} . ' ' . $member->{'surname'};
154
155     $result->{'budget_owner'} = $member_full;
156     $result->{'budget_avail'} = $result->{'budget_amount'} - $result->{'budget_spent'};
157     $result->{'budget_spent'} = GetBudgetSpent( $result->{'budget_id'} );
158
159     $total    += $result->{'budget_amount'};
160     $totspent += $result->{'budget_spent'};
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_avail'}  = $num->format_price( $result->{'budget_avail'} );
166
167     #        my $spent_percent = ( $result->{'budget_spent'} / $result->{'budget_amount'} ) * 100;
168     #        $result->{'budget_spent_percent'} = sprintf( "%00d", $spent_percent );
169
170     my $borrower = &GetMember( $result->{budget_owner_id} );
171     $result->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
172
173     push( @loop_budget, { %{$result}, toggle => $toggle++ % 2, } );
174 }
175
176
177 # ---------------------------------------------------
178
179 =c FIXME
180
181
182 ### $cur
183
184 ## suggestions
185
186 my $dbh = C4::Context->dbh;
187
188
189 ## liste des domaines
190
191 my $sth=$dbh->prepare("
192 SELECT bookfundgroupnumber,bookfundgroupname
193 FROM `aq2bookfundgroups`
194 ORDER BY Bookfundgroupname
195 ");
196 $sth->execute;
197
198 my @bookfundgroup_loop;  ## liste des domaines
199
200 while (my $row=$sth->fetchrow_hashref) {
201         push @bookfundgroup_loop,$row;
202 }
203 $sth->finish;
204
205
206 ## liste des BFG ayant des suggestions à traiter
207
208
209 ## nowsuggestions = Number Of Waiting Suggestions
210
211 my $dbh = C4::Context->dbh;
212
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
219 ");
220 $sth->execute;
221
222 my @nowsuggestionsneq0_loop;  ## liste des BFG ayant des suggestions à traiter
223
224 while (my $row=$sth->fetchrow_hashref) {
225         push @nowsuggestionsneq0_loop,$row;
226 }
227 $sth->finish;
228
229
230 ## liste des BFG avec l'effectif des suggestions à traiter (effectif éventuellement nul)
231
232 my @nowsuggestions_loop;
233
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'};
239         }
240     }
241 }
242
243 =cut
244
245
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 ),
258
259     #      nowsuggestions_loop           => \@nowsuggestions_loop,
260     #      bookfundgroup_loop            => \@bookfundgroup_loop,
261     #      numberofwaitingsuggestionsgpd => $numberofwaitingsuggestionsgpd,
262     #      numberofwaitingsuggestionspd  => $numberofwaitingsuggestionspd,
263     #      suggestions_loop              => $suggestions_loop,
264
265 );
266
267 output_html_with_http_headers $query, $cookie, $template->output;