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