Bug 17600: Standardize our EXPORT_OK
[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
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 =head1 NAME
20
21 acqui-home.pl
22
23 =head1 DESCRIPTION
24
25 this script is the main page for acqui
26
27 =cut
28
29 use Modern::Perl;
30
31 use CGI qw ( -utf8 );
32 use C4::Auth qw( get_template_and_user );
33 use C4::Output qw( output_html_with_http_headers );
34 use C4::Budgets qw( GetBudgetHierarchy GetBudget CanUserUseBudget );
35 use C4::Members;
36 use Koha::Acquisition::Currencies;
37 use Koha::Patrons;
38 use Koha::Suggestions;
39
40 my $query = CGI->new;
41 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
42     {   template_name   => 'acqui/acqui-home.tt',
43         query           => $query,
44         type            => 'intranet',
45         flagsrequired   => { acquisition => '*' },
46     }
47 );
48
49 my $status           = $query->param('status') || "ASKED";
50 # Get current branch count and total viewable count, if they don't match then pass
51 # both to template
52 if( C4::Context->only_my_library ){
53     my $local_pendingsuggestions_count = Koha::Suggestions->search({ status => "ASKED", branchcode => C4::Context->userenv()->{'branch'} })->count();
54     $template->param( suggestions_count => $local_pendingsuggestions_count );
55 } else {
56     my $pendingsuggestions = Koha::Suggestions->search({ status => "ASKED" });
57     my $local_pendingsuggestions_count = $pendingsuggestions->search({ 'me.branchcode' => C4::Context->userenv()->{'branch'} })->count();
58     my $pendingsuggestions_count = $pendingsuggestions->count();
59     $template->param(
60         all_pendingsuggestions => $pendingsuggestions_count != $local_pendingsuggestions_count ? $pendingsuggestions_count : 0,
61         suggestions_count => $local_pendingsuggestions_count
62     );
63 }
64
65 my $budget_arr = GetBudgetHierarchy;
66
67 my $total      = 0;
68 my $totspent   = 0;
69 my $totordered = 0;
70 my $totcomtd   = 0;
71 my $totavail   = 0;
72
73 my $total_active        = 0;
74 my $totspent_active     = 0;
75 my $totordered_active   = 0;
76 my $totavail_active     = 0;
77
78 my @budget_loop;
79 foreach my $budget ( @{$budget_arr} ) {
80     next unless (CanUserUseBudget($loggedinuser, $budget, $userflags));
81
82     my $patron = Koha::Patrons->find( $budget->{budget_owner_id} );
83     if ( $patron ) {
84         $budget->{budget_owner} = $patron;
85     }
86
87     if ( !defined $budget->{budget_amount} ) {
88         $budget->{budget_amount} = 0;
89     }
90     if ( !defined $budget->{budget_spent} ) {
91         $budget->{budget_spent} = 0;
92     }
93     if ( !defined $budget->{budget_ordered} ) {
94         $budget->{budget_ordered} = 0;
95     }
96     $budget->{'budget_avail'} =
97       $budget->{'budget_amount'} - ( $budget->{'budget_spent'} + $budget->{'budget_ordered'} );
98
99     $total      += $budget->{'budget_amount'};
100     $totspent   += $budget->{'budget_spent'};
101     $totordered += $budget->{'budget_ordered'};
102     $totavail   += $budget->{'budget_avail'};
103
104     if ($budget->{budget_period_active}){
105         $total_active      += $budget->{'budget_amount'};
106         $totspent_active   += $budget->{'budget_spent'};
107         $totordered_active += $budget->{'budget_ordered'};
108         $totavail_active   += $budget->{'budget_avail'};    
109     }
110
111     push @budget_loop, $budget;
112 }
113
114 $template->param(
115     type          => 'intranet',
116     loop_budget   => \@budget_loop,
117     total         => $total,
118     totspent      => $totspent,
119     totordered    => $totordered,
120     totcomtd      => $totcomtd,
121     totavail      => $totavail,
122     total_active  => $total_active,
123     totspent_active     => $totspent_active,
124     totordered_active   => $totordered_active,
125     totavail_active     => $totavail_active,
126 );
127
128 my $cur = Koha::Acquisition::Currencies->get_active;
129 if ( $cur ) {
130     $template->param(
131         currency => $cur->currency,
132     );
133 }
134
135 output_html_with_http_headers $query, $cookie, $template->output;