big commit, still breaking things...
[wip/koha-chris_n.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)
15     = get_template_and_user({template_name => "acqui/acqui-home.tmpl",
16                              query => $query,
17                              type => "intranet",
18                              authnotrequired => 0,
19                              flagsrequired => {acquisition => 1},
20                              debug => 1,
21                              });
22
23 # budget
24 my $dbh = C4::Context->dbh;
25 my $sthtemp = $dbh->prepare("Select flags, branchcode from borrowers where borrowernumber = ?");
26 $sthtemp->execute($loggedinuser);
27 my ($flags, $homebranch)=$sthtemp->fetchrow;
28
29 my ($count,@results)=bookfunds($homebranch);
30 my $classlist='';
31 my $total=0;
32 my $totspent=0;
33 my $totcomtd=0;
34 my $totavail=0;
35 my @loop_budget = ();
36 for (my $i=0;$i<$count;$i++){
37         my ($spent,$comtd)=bookfundbreakdown($results[$i]->{'bookfundid'});
38         my $avail=$results[$i]->{'budgetamount'}-($spent+$comtd);
39         my %line;
40         $line{bookfundname} = $results[$i]->{'bookfundname'};
41         $line{budgetamount} = $results[$i]->{'budgetamount'};
42         $line{spent} = sprintf  ("%.2f", $spent);
43         $line{comtd} = sprintf  ("%.2f",$comtd);
44         $line{avail}  = sprintf  ("%.2f",$avail);
45         push @loop_budget, \%line;
46         $total+=$results[$i]->{'budgetamount'};
47         $totspent+=$spent;
48         $totcomtd+=$comtd;
49         $totavail+=$avail;
50 }
51 #currencies
52 my $rates;
53 ($count,$rates)=getcurrencies();
54 my @loop_currency = ();
55 for (my $i=0;$i<$count;$i++){
56         my %line;
57         $line{currency} = $rates->[$i]->{'currency'};
58         $line{rate} = $rates->[$i]->{'rate'};
59         push @loop_currency, \%line;
60 }
61
62 # suggestions ?
63 my $suggestion = countsuggestion("ASKED");
64 $template->param(classlist => $classlist,
65                                                 type => 'intranet',
66                                                 loop_budget => \@loop_budget,
67                                                 loop_currency => \@loop_currency,
68                                                 total => sprintf("%.2f",$total),
69                                                 suggestion => $suggestion,
70                                                 totspent => sprintf("%.2f",$totspent),
71                                                 totcomtd => sprintf("%.2f",$totcomtd),
72                                                 totavail => sprintf("%.2f",$totavail),
73                                                 nobudget => $#results==-1?1:0);
74
75 output_html_with_http_headers $query, $cookie, $template->output;