Merge remote-tracking branch 'origin/new/bug_6634'
[koha.git] / acqui / spent.pl
1 #!/usr/bin/perl
2
3 # script to show a breakdown of committed and spent budgets
4
5 # Copyright 2002-2009 Katipo Communications Limited
6 # Copyright 2010,2011 Catalyst IT Limited
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 =head1 NAME
23
24  spent.pl
25
26 =head1 DESCRIPTION
27
28 this script is designed to show the spent amount in budgets
29
30 =cut
31
32 use C4::Context;
33 use C4::Auth;
34 use C4::Output;
35 use strict;
36 use warnings;
37 use CGI;
38
39 my $dbh      = C4::Context->dbh;
40 my $input    = new CGI;
41 my $bookfund = $input->param('fund');
42 my $fund_code = $input->param('fund_code');
43
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45     {
46         template_name   => "acqui/spent.tt",
47         query           => $input,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { acquisition => '*' },
51         debug           => 1,
52     }
53 );
54
55 my $query = <<EOQ;
56 SELECT
57     aqorders.basketno, aqorders.ordernumber,
58     quantity-quantityreceived AS tleft,
59     ecost, budgetdate, entrydate,
60     aqbasket.booksellerid,
61     itype,
62     title,
63     aqorders.booksellerinvoicenumber,
64     quantityreceived,
65     unitprice,
66     freight,
67     datereceived,
68     aqorders.biblionumber
69 FROM (aqorders, aqbasket)
70 LEFT JOIN items ON
71     items.biblioitemnumber=aqorders.biblioitemnumber
72 LEFT JOIN biblio ON
73     biblio.biblionumber=aqorders.biblionumber
74 LEFT JOIN aqorders_items ON
75     aqorders.ordernumber=aqorders_items.ordernumber
76 WHERE
77     aqorders.basketno=aqbasket.basketno AND
78     budget_id=? AND
79     (datecancellationprinted IS NULL OR
80         datecancellationprinted='0000-00-00')
81     GROUP BY aqorders.ordernumber
82 EOQ
83 my $sth = $dbh->prepare($query);
84 $sth->execute($bookfund);
85 if ( $sth->err ) {
86     die "An error occurred fetching records: " . $sth->errstr;
87 }
88 my $total = 0;
89 my $toggle;
90 my @spent;
91 while ( my $data = $sth->fetchrow_hashref ) {
92     my $recv = $data->{'quantityreceived'};
93     if ( $recv > 0 ) {
94         my $subtotal = $recv * ( $data->{'unitprice'} + $data->{'freight'} );
95         $data->{'subtotal'}  = sprintf( "%.2f", $subtotal );
96         $data->{'freight'}   = sprintf( "%.2f", $data->{'freight'} );
97         $data->{'unitprice'} = sprintf( "%.2f", $data->{'unitprice'} );
98         $total += $subtotal;
99         push @spent, $data;
100     }
101
102 }
103 $total = sprintf( "%.2f", $total );
104
105 $template->{VARS}->{'fund'}  = $bookfund;
106 $template->{VARS}->{'spent'} = \@spent;
107 $template->{VARS}->{'total'} = $total;
108 $template->{VARS}->{'fund_code'} = $fund_code;
109 $sth->finish;
110
111 output_html_with_http_headers $input, $cookie, $template->output;