Bug 929 : Last follow up, implementing the last of Katrins suggestions
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 committed.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 strict;
32 use warnings;
33 use CGI;
34 use C4::Auth;
35 use C4::Output;
36 use C4::Dates;
37
38 my $dbh     = C4::Context->dbh;
39 my $input   = new CGI;
40 my $fund_id = $input->param('fund');
41 my $start   = $input->param('start');
42 my $end     = $input->param('end');
43
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45     {
46         template_name   => "acqui/ordered.tt",
47         query           => $input,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { acquisition => 1 },
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 FROM (aqorders, aqbasket)
64 LEFT JOIN biblio ON
65     biblio.biblionumber=aqorders.biblionumber
66 LEFT JOIN aqorders_items ON
67     aqorders.ordernumber=aqorders_items.ordernumber
68 LEFT JOIN items ON
69     items.itemnumber=aqorders_items.itemnumber
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'};
95         $data->{subtotal} = sprintf( "%.2f", $subtotal );
96         $data->{'left'} = $left;
97         push @ordered, $data;
98         $total += $subtotal;
99     }
100     my $entrydate = C4::Dates->new( $data->{'entrydate'}, 'iso' );
101     $data->{'entrydate'} = $entrydate->output("syspref");
102 }
103 $total = sprintf( "%.2f", $total );
104
105 $template->{VARS}->{'fund'}    = $fund_id;
106 $template->{VARS}->{'ordered'} = \@ordered;
107 $template->{VARS}->{'total'}   = $total;
108
109 $sth->finish;
110
111 output_html_with_http_headers $input, $cookie, $template->output;