Fix FSF address in directory acqui/
[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
20
21 =head1 NAME
22
23 acqui-home.pl
24
25 =head1 DESCRIPTION
26
27 this script is the main page for acqui/
28
29 =head1 CGI PARAMETERS
30
31 =over 4
32
33 =item $status
34 C<$status> is the status a suggestion could has. Default value is 'ASKED'.
35 thus, it can be REJECTED, ACCEPTED, ORDERED, ASKED, AVAIBLE
36
37 =back
38
39 =cut
40
41 use strict;
42 use warnings;
43 use Number::Format;
44
45 use CGI;
46 use C4::Auth;
47 use C4::Output;
48 use C4::Suggestions;
49 use C4::Acquisition;
50 use C4::Budgets;
51 use C4::Members;
52 use C4::Branch;
53 use C4::Debug;
54
55 my $query = new CGI;
56 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
57     {
58         template_name   => "acqui/acqui-home.tmpl",
59         query           => $query,
60         type            => "intranet",
61         authnotrequired => 0,
62         flagsrequired   => { acquisition => "*" },
63         debug           => 1,
64     }
65 );
66
67 # budget
68 my $borrower= GetMember('borrowernumber' => $loggedinuser);
69 my ( $flags, $homebranch )= ($borrower->{'flags'},$borrower->{'branchcode'});
70
71 my @results = GetBudgets($homebranch);
72 my $count = scalar @results;
73 my $branchname = GetBranchName($homebranch);
74
75 my $classlist   = '';
76 my $total       = 0;
77 my $totspent    = 0;
78 my $totordered  = 0;
79 my $totcomtd    = 0;
80 my $totavail    = 0;
81 my @loop_budget = ();
82
83 # ---------------------------------------------------
84 # currencies
85 my $cur;
86
87 my $active_currency = GetCurrency;
88 my $num;
89
90 my $cur_format = C4::Context->preference("CurrencyFormat");
91 if ( $cur_format eq 'FR' ) {
92     $num = new Number::Format(
93         'decimal_fill'      => '2',
94         'decimal_point'     => ',',
95         'int_curr_symbol'   => '',
96         'mon_thousands_sep' => ' ',
97         'thousands_sep'     => ' ',
98         'mon_decimal_point' => ','
99     );
100 } else {  # US by default..
101     $num = new Number::Format(
102         'int_curr_symbol'   => '',
103         'mon_thousands_sep' => ',',
104         'mon_decimal_point' => '.'
105     );
106 }
107
108 # suggestions
109 my $status           = $query->param('status') || "ASKED";
110 my $suggestion       = CountSuggestion($status);
111 my $suggestions_loop = &SearchSuggestion( {STATUS=> $status} );
112 # ---------------------------------------------------
113 # number format
114 my $period            = GetBudgetPeriod;
115 my $budget_period_id  = $period->{budget_period_id};
116 my $budget_branchcode = $period->{budget_branchcode};
117 my $moo               = GetBudgetHierarchy('',$homebranch, $template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'} );
118 @results           = @$moo;
119 my $period_total      = 0;
120 my $toggle            = 0;
121 my @loop;
122
123 foreach my $result (@results) {
124     # only get top-level budgets for display
125     #         warn  $result->{'budget_branchcode'};
126
127     $period_total += $result->{'budget_amount'};
128
129     my $a = $result->{'budget_code_indent'};
130     $a =~ s/\ /\&nbsp\;/g;
131     $result->{'budget_code_indent'} = $a;
132
133     my $r = GetBranchName( $result->{'budget_owner_id'} );
134     $result->{'budget_branchname'} = GetBranchName( $result->{'budget_branchcode'} );
135
136     my $member      = GetMember( borrowernumber => $result->{budget_owner_id} );
137     my $member_full = $member->{'firstname'} . ' ' . $member->{'surname'} if $member;
138     # as we are going to treat these as numbers let's ensure they are not NULL
139     if (!defined $result->{budget_amount} ) {
140         $result->{budget_amount} = 0;
141     }
142     if (!defined $result->{budget_spent} ) {
143         $result->{budget_spent} = 0;
144     }
145     if (!defined $result->{budget_ordered} ) {
146         $result->{budget_ordered} = 0;
147     }
148
149     $result->{'budget_owner'}   = $member_full;
150     $result->{'budget_ordered'} = GetBudgetOrdered( $result->{'budget_id'} );
151     $result->{'budget_spent'}   = GetBudgetSpent( $result->{'budget_id'} );
152     $result->{'budget_avail'}   = $result->{'budget_amount'} - $result->{'budget_spent'} - $result->{'budget_ordered'};
153
154     $total      += $result->{'budget_amount'};
155     $totspent   += $result->{'budget_spent'};
156     $totordered += $result->{'budget_ordered'};
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_ordered'} = $num->format_price( $result->{'budget_ordered'} );
162     $result->{'budget_avail'}   = $num->format_price( $result->{'budget_avail'} );
163
164     #        my $spent_percent = ( $result->{'budget_spent'} / $result->{'budget_amount'} ) * 100;
165     #        $result->{'budget_spent_percent'} = sprintf( "%00d", $spent_percent );
166
167     if ($member) {
168         $result->{budget_owner_name} = $member->{'firstname'} . ' ' . $member->{'surname'};
169     }
170
171     push( @loop_budget, { %{$result}, toggle => $toggle++ % 2, } );
172 }
173
174 $template->param(
175     classlist     => $classlist,
176     type          => 'intranet',
177     loop_budget   => \@loop_budget,
178     active_symbol => $active_currency->{'symbol'},
179     branchname    => $branchname,
180     budget        => $period->{budget_name},
181     total         => $num->format_price(  $total ),
182     totspent      => $num->format_price( $totspent ),
183     totordered    => $num->format_price( $totordered ),
184     totcomtd      => $num->format_price( $totcomtd ),
185     totavail      => $num->format_price( $totavail ),
186     suggestion    => $suggestion,
187 );
188
189 output_html_with_http_headers $query, $cookie, $template->output;