Bug 30477: Add new UNIMARC installer translation files
[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 qw( get_template_and_user );
34 use C4::Output qw( output_html_with_http_headers );
35 use Koha::Acquisition::Invoice::Adjustments;
36 use C4::Acquisition qw( get_rounded_price );
37
38 my $dbh     = C4::Context->dbh;
39 my $input   = CGI->new;
40 my $fund_id = $input->param('fund');
41 my $fund_code = $input->param('fund_code');
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name   => "acqui/ordered.tt",
46         query           => $input,
47         type            => "intranet",
48         flagsrequired   => { acquisition => '*' },
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     GROUP_CONCAT(DISTINCT itype SEPARATOR '|') AS itypes,
60     title
61 FROM aqorders
62 JOIN aqbasket USING (basketno)
63 LEFT JOIN biblio ON
64     biblio.biblionumber=aqorders.biblionumber
65 LEFT JOIN aqorders_items ON
66     aqorders.ordernumber=aqorders_items.ordernumber
67 LEFT JOIN items ON
68     items.itemnumber=aqorders_items.itemnumber
69 LEFT JOIN aqbooksellers ON
70     aqbasket.booksellerid = aqbooksellers.id
71 WHERE
72     budget_id=? AND
73     datecancellationprinted IS NULL AND
74     (quantity > quantityreceived OR quantityreceived IS NULL)
75     GROUP BY aqorders.biblionumber, aqorders.basketno, aqorders.ordernumber,
76              tleft,
77              ecost_tax_included, budgetdate, entrydate,
78              aqbasket.booksellerid,
79              aqbooksellers.name,
80              title
81 EOQ
82
83 my $sth = $dbh->prepare($query);
84
85 $sth->execute($fund_id);
86 if ( $sth->err ) {
87     die "Error occurred fetching records: " . $sth->errstr;
88 }
89 my @ordered;
90
91 my $total = 0;
92 while ( my $data = $sth->fetchrow_hashref ) {
93     $data->{'itemtypes'} = [split('\|', $data->{itypes})];
94     my $left = $data->{'tleft'};
95     if ( !$left || $left eq '' ) {
96         $left = $data->{'quantity'};
97     }
98     if ( $left && $left > 0 ) {
99         my $subtotal = $left * get_rounded_price( $data->{'ecost_tax_included'} );
100         $data->{subtotal} = sprintf( "%.2f", $subtotal );
101         $data->{'left'} = $left;
102         push @ordered, $data;
103         $total += $subtotal;
104     }
105 }
106
107 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $fund_id, closedate => undef, encumber_open => 1 }, { prefetch => 'invoiceid' } );
108 while ( my $adj = $adjustments->next ){
109     $total += $adj->adjustment;
110 }
111
112 $total = sprintf( "%.2f", $total );
113
114 $template->{VARS}->{'fund'}    = $fund_id;
115 $template->{VARS}->{'ordered'} = \@ordered;
116 $template->{VARS}->{'total'}   = $total;
117 $template->{VARS}->{'fund_code'} = $fund_code;
118 $template->{VARS}->{'adjustments'} = $adjustments;
119
120 $sth->finish;
121
122 output_html_with_http_headers $input, $cookie, $template->output;