Bug 4108 Catch cases when GetMember is generating errors
[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 warn("LOGGED=$loggedinuser");
71 my $borrower= GetMember('borrowernumber' => $loggedinuser);
72 my ( $flags, $homebranch )= ($borrower->{'flags'},$borrower->{'branchcode'});
73
74 my @results = GetBudgets($homebranch);
75 my $count = scalar @results;
76 my $branchname = GetBranchName($homebranch);
77
78 #my $count = scalar @results;
79 my $classlist   = '';
80 my $total       = 0;
81 my $totspent    = 0;
82 my $totordered  = 0;
83 my $totcomtd    = 0;
84 my $totavail    = 0;
85 my @loop_budget = ();
86
87 # ---------------------------------------------------
88 # currencies
89 my $cur;
90 my @rates = GetCurrencies();
91 $count = scalar @rates;
92
93 my $active_currency = GetCurrency;
94 my $num;
95
96 my $cur_format = C4::Context->preference("CurrencyFormat");
97 if ( $cur_format eq 'FR' ) {
98     $num = new Number::Format(
99         'decimal_fill'      => '2',
100         'decimal_point'     => ',',
101         'int_curr_symbol'   => '',
102         'mon_thousands_sep' => ' ',
103         'thousands_sep'     => ' ',
104         'mon_decimal_point' => ','
105     );
106 } else {  # US by default..
107     $num = new Number::Format(
108         'int_curr_symbol'   => '',
109         'mon_thousands_sep' => ',',
110         'mon_decimal_point' => '.'
111     );
112 }
113
114 my @loop_currency = ();
115 for ( my $i = 0 ; $i < $count ; $i++ ) {
116     my %line;
117     $line{currency}        = $rates[$i]->{'currency'} ;
118     $line{currency_symbol} = $rates[$i]->{'symbol'};
119     $line{rate}            = sprintf ( '%.2f',  $rates[$i]->{'rate'} );
120     push @loop_currency, \%line;
121 }
122
123 # suggestions
124 my $status           = $query->param('status') || "ASKED";
125 my $suggestion       = CountSuggestion($status);
126 my $suggestions_loop = &SearchSuggestion( {STATUS=> $status} );
127 # ---------------------------------------------------
128 # number format
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 @results           = @$moo;
134 my $period_total      = 0;
135 my $toggle            = 0;
136 my @loop;
137
138 foreach my $result (@results) {
139     # only get top-level budgets for display
140     #         warn  $result->{'budget_branchcode'};
141
142     $period_total += $result->{'budget_amount'};
143
144     my $a = $result->{'budget_code_indent'};
145     $a =~ s/\ /\&nbsp\;/g;
146     $result->{'budget_code_indent'} = $a;
147
148     my $r = GetBranchName( $result->{'budget_owner_id'} );
149     $result->{'budget_branchname'} = GetBranchName( $result->{'budget_branchcode'} );
150
151     my $member      = GetMember( borrowernumber => $result->{budget_owner_id} );
152     my $member_full = $member->{'firstname'} . ' ' . $member->{'surname'} if $member;
153
154     $result->{'budget_owner'}   = $member_full;
155     $result->{'budget_ordered'} = GetBudgetOrdered( $result->{'budget_id'} );
156     $result->{'budget_spent'}   = GetBudgetSpent( $result->{'budget_id'} );
157     $result->{'budget_avail'}   = $result->{'budget_amount'} - $result->{'budget_spent'} - $result->{'budget_ordered'};
158
159     $total      += $result->{'budget_amount'};
160     $totspent   += $result->{'budget_spent'};
161     $totordered += $result->{'budget_ordered'};
162     $totavail   += $result->{'budget_avail'};
163
164     $result->{'budget_amount'}  = $num->format_price( $result->{'budget_amount'} );
165     $result->{'budget_spent'}   = $num->format_price( $result->{'budget_spent'} );
166     $result->{'budget_ordered'} = $num->format_price( $result->{'budget_ordered'} );
167     $result->{'budget_avail'}   = $num->format_price( $result->{'budget_avail'} );
168
169     #        my $spent_percent = ( $result->{'budget_spent'} / $result->{'budget_amount'} ) * 100;
170     #        $result->{'budget_spent_percent'} = sprintf( "%00d", $spent_percent );
171
172     if ($member) {
173         $result->{budget_owner_name} = $member->{'firstname'} . ' ' . $member->{'surname'};
174     }
175
176     push( @loop_budget, { %{$result}, toggle => $toggle++ % 2, } );
177 }
178
179 $template->param(
180     classlist     => $classlist,
181     type          => 'intranet',
182     loop_budget   => \@loop_budget,
183     loop_currency => \@loop_currency,
184     active_symbol => $active_currency->{'symbol'},
185     branchname    => $branchname,
186     budget        => $period->{budget_name},
187     total         => $num->format_price(  $total ),
188     totspent      => $num->format_price( $totspent ),
189     totordered    => $num->format_price( $totordered ),
190     totcomtd      => $num->format_price( $totcomtd ),
191     totavail      => $num->format_price( $totavail ),
192     suggestion    => $suggestion,
193 );
194
195 output_html_with_http_headers $query, $cookie, $template->output;