b62f16d985f96f4652af25ab0be2f8ae8a1dafcc
[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 distinct quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived
35     as qrev,subscription,title,itype as itemtype,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     inner join aqorderbreakdown on aqorderbreakdown.ordernumber = aqorders.ordernumber
41     inner join aqbasket on aqbasket.basketno = aqorders.basketno
42     left join items on  items.biblionumber=aqorders.biblionumber
43     where bookfundid=? 
44    and (datereceived >= ? and datereceived < ?)
45     and (datecancellationprinted is NULL or
46            datecancellationprinted='0000-00-00')
47
48
49   ";
50 my $sth = $dbh->prepare($query);
51 $sth->execute( $bookfund, $start, $end );
52
53 my $total = 0;
54 my $toggle;
55 my @spent_loop;
56 while ( my $data = $sth->fetchrow_hashref ) {
57     my $recv = $data->{'qrev'};
58     if ( $recv > 0 ) {
59         my $subtotal = $recv * $data->{'unitprice'};
60         $data->{'subtotal'} = $subtotal;
61         $data->{'unitprice'} += 0;
62         $total               += $subtotal;
63         if ($toggle) {
64             $toggle = 0;
65         }
66         else {
67             $toggle = 1;
68         }
69         $data->{'toggle'} = $toggle;
70         push @spent_loop, $data;
71     }
72
73 }
74
75 $template->param(
76     SPENTLOOP => \@spent_loop,
77     total     => $total
78 );
79 $sth->finish;
80
81 $dbh->disconnect;
82 output_html_with_http_headers $input, $cookie, $template->output;