89a71e50dc94d42f60e8765e1a3b6dc5fa55c14c
[koha.git] / acqui / spent.pl
1 #!/usr/bin/perl
2
3 # script to show a breakdown of committed and spent budgets
4
5 # needs to be templated at some point
6
7 use C4::Context;
8 use C4::Auth;
9 use C4::Output;
10 use strict;
11 use CGI;
12
13 my $dbh      = C4::Context->dbh;
14 my $input    = new CGI;
15 my $bookfund = $input->param('bookfund');
16 my $start    = $input->param('start');
17 my $end      = $input->param('end');
18
19 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
20     {
21         template_name   => "acqui/spent.tmpl",
22         query           => $input,
23         type            => "intranet",
24         authnotrequired => 0,
25         flagsrequired   => { acquisition => 1 },
26         debug           => 1,
27     }
28 );
29
30 #James Winter 3/4/2009: Original query does not select spent rows
31 #       correctly due to missing joins between tables
32
33 my $query =
34 "SELECT quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived
35     as qrev,subscription,title,aqorders.biblionumber,aqorders.booksellerinvoicenumber,
36     quantity-quantityreceived as tleft,
37     aqorders.ordernumber
38     as ordnum,entrydate,budgetdate,aqbasket.booksellerid,aqbasket.basketno
39     FROM aqorders
40     LEFT JOIN aqorderbreakdown USING (ordernumber)
41     LEFT JOIN aqbasket USING (basketno)
42     LEFT JOIN aqbudget USING (bookfundid)
43     WHERE bookfundid=?
44     AND (datecancellationprinted IS NULL OR datecancellationprinted = '0000-00-00')
45     AND closedate BETWEEN startdate AND enddate 
46     AND creationdate > startdate
47     ORDER BY datereceived
48   ";
49 my $sth = $dbh->prepare($query);
50 $sth->execute( $bookfund);
51
52 my $total = 0;
53 my $toggle;
54 my @spent_loop;
55 while ( my $data = $sth->fetchrow_hashref ) {
56     my $recv = $data->{'qrev'};
57     if ( $recv > 0 ) {
58         my $subtotal = $recv * $data->{'unitprice'};
59         $data->{'subtotal'} = $subtotal;
60         $data->{'unitprice'} += 0;
61         $total               += $subtotal;
62         if ($toggle) {
63             $toggle = 0;
64         }
65         else {
66             $toggle = 1;
67         }
68         $data->{'toggle'} = $toggle;
69         push @spent_loop, $data;
70     }
71
72 }
73
74 $template->param(
75     SPENTLOOP => \@spent_loop,
76     total     => $total
77 );
78 $sth->finish;
79
80 $dbh->disconnect;
81 output_html_with_http_headers $input, $cookie, $template->output;