Bug 7298: add option to export late orders as CSV
[koha.git] / acqui / lateorders-export.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19 use CGI;
20 use C4::Auth;
21 use C4::Serials;
22 use C4::Acquisition;
23 use C4::Output;
24 use C4::Context;
25
26 use Text::CSV::Encoded;
27 use open qw/ :std :utf8 /;
28
29 my $csv = Text::CSV::Encoded->new ({
30         encoding    => undef,
31         quote_char  => '"',
32         escape_char => '"',
33         sep_char    => ',',
34         binary      => 1,
35     });
36
37 my $query        = new CGI;
38 my @ordernumbers = $query->param('ordernumber');
39
40 print $query->header(
41     -type       => 'text/csv',
42     -attachment => "lateorders.csv",
43 );
44
45 print "LATE ORDERS\n\n";
46 print "ORDER DATE,ESTIMATED DELIVERY DATE,VENDOR,INFORMATION,TOTAL COST,BASKET,CLAIMS COUNT,CLAIMED DATE\n";
47
48 for my $ordernumber ( @ordernumbers ) {
49     my $order = GetOrder $ordernumber;
50     $csv->combine(
51         "(" . $order->{supplierid} . ") " . $order->{orderdate} . " (" . $order->{latesince} . " days)",
52         $order->{estimateddeliverydate},
53         $order->{supplier},
54         $order->{title} . ( $order->{author} ? " Author: $order->{author}" : "" ) . ( $order->{publisher} ? " Published by: $order->{publisher}" : "" ),
55         $order->{unitpricesupplier} . "x" . $order->{quantity_to_receive} . " = " . $order->{subtotal} . " (" . $order->{budget} . ")",
56         $order->{basketname} . " (" . $order->{basketno} . ")",
57         $order->{claims_count},
58         $order->{claimed_date}
59     );
60     my $string = $csv->string;
61     print $string, "\n";
62 }
63
64 print ",,Total Number Late, " . scalar @ordernumbers . "\n";