German updates for 3.2
[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=? and
44     aqorders.ordernumber=aqorderbreakdown.ordernumber and
45     aqorders.basketno=aqbasket.basketno
46    and (
47         (datereceived >= ? and datereceived < ?))
48     and (datecancellationprinted is NULL or
49            datecancellationprinted='0000-00-00')
50
51
52   ";
53 my $sth = $dbh->prepare($query);
54 $sth->execute( $bookfund, $start, $end );
55
56 my $total = 0;
57 my $toggle;
58 my @spent_loop;
59 while ( my $data = $sth->fetchrow_hashref ) {
60     my $recv = $data->{'qrev'};
61     if ( $recv > 0 ) {
62         my $subtotal = $recv * $data->{'unitprice'};
63         $data->{'subtotal'} = $subtotal;
64         $data->{'unitprice'} += 0;
65         $total               += $subtotal;
66         if ($toggle) {
67             $toggle = 0;
68         }
69         else {
70             $toggle = 1;
71         }
72         $data->{'toggle'} = $toggle;
73         push @spent_loop, $data;
74     }
75
76 }
77
78 $template->param(
79     SPENTLOOP => \@spent_loop,
80     total     => $total
81 );
82 $sth->finish;
83
84 $dbh->disconnect;
85 output_html_with_http_headers $input, $cookie, $template->output;