OOPS, adding back Joshua's changes.
[koha.git] / acqui / acqui-home.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use CGI;
5 use C4::Auth;
6 use C4::Output;
7 use C4::Interface::CGI::Output;
8 use C4::Database;
9 use C4::Suggestions;
10 use HTML::Template;
11 use C4::Acquisition;
12
13 my $query = new CGI;
14 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
15     {
16         template_name   => "acqui/acqui-home.tmpl",
17         query           => $query,
18         type            => "intranet",
19         authnotrequired => 0,
20         flagsrequired   => { acquisition => 1 },
21         debug           => 1,
22     }
23 );
24
25 # budget
26 my $dbh     = C4::Context->dbh;
27 my $sthtemp =
28   $dbh->prepare(
29     "Select flags, branchcode from borrowers where borrowernumber = ?");
30 $sthtemp->execute($loggedinuser);
31 my ( $flags, $homebranch ) = $sthtemp->fetchrow;
32
33 my ( $count, @results ) = bookfunds($homebranch);
34 my $classlist   = '';
35 my $total       = 0;
36 my $totspent    = 0;
37 my $totcomtd    = 0;
38 my $totavail    = 0;
39 my @loop_budget = ();
40 for ( my $i = 0 ; $i < $count ; $i++ ) {
41
42     if ( $toggle eq 0 ) {
43         $toggle = 1;
44     }
45     else {
46         $toggle = 0;
47     }
48     my ( $spent, $comtd ) = bookfundbreakdown( $results[$i]->{'bookfundid'} );
49
50     my $avail = $results[$i]->{'budgetamount'} - ( $spent + $comtd );
51     my %line;
52     $line{bookfundname} = $results[$i]->{'bookfundname'};
53     $line{budgetamount} = $results[$i]->{'budgetamount'};
54     $line{bookfundid}   = $results[$i]->{'bookfundid'};
55     $line{sdate}        = $results[$i]->{'startdate'};
56     $line{edate}        = $results[$i]->{'enddate'};
57     $line{aqbudgetid}   = $results[$i]->{'aqbudgetid'};
58     $line{spent}        = sprintf( "%.2f", $spent );
59     $line{comtd}        = sprintf( "%.2f", $comtd );
60     $line{avail}        = sprintf( "%.2f", $avail );
61     $line{'toggle'}     = $toggle;
62     push @loop_budget, \%line;
63     $total    += $results[$i]->{'budgetamount'};
64     $totspent += $spent;
65     $totcomtd += $comtd;
66     $totavail += $avail;
67 }
68
69 #currencies
70 my $rates;
71 ( $count, $rates ) = getcurrencies();
72 my @loop_currency = ();
73 for ( my $i = 0 ; $i < $count ; $i++ ) {
74     my %line;
75     $line{currency} = $rates->[$i]->{'currency'};
76     $line{rate}     = $rates->[$i]->{'rate'};
77     push @loop_currency, \%line;
78 }
79
80 # suggestions ?
81 my $status           = $query->param('status') || "ASKED";
82 my $suggestion       = countsuggestion($status);
83 my $suggestions_loop = &searchsuggestion( '', '', '', '', $status, '' );
84
85 $template->param(
86     classlist        => $classlist,
87     type             => 'intranet',
88     loop_budget      => \@loop_budget,
89     loop_currency    => \@loop_currency,
90     total            => sprintf( "%.2f", $total ),
91     suggestion       => $suggestion,
92     suggestions_loop => $suggestions_loop,
93     totspent         => sprintf( "%.2f", $totspent ),
94     totcomtd         => sprintf( "%.2f", $totcomtd ),
95     totavail         => sprintf( "%.2f", $totavail ),
96     nobudget         => $#results == -1 ? 1 : 0
97 );
98
99 output_html_with_http_headers $query, $cookie, $template->output;