Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use CGI qw ( -utf8 );
20 use Encode;
21
22 use C4::Auth;
23 use C4::Acquisition;
24 use C4::Output;
25 use C4::Context;
26
27 my $input = new CGI;
28 my ($template, $loggedinuser, $cookie) = get_template_and_user({
29     template_name => "acqui/csv/lateorders.tt",
30     query => $input,
31     type => "intranet",
32     authnotrequired => 0,
33     flagsrequired => {acquisition => 'order_receive'},
34 });
35 my @ordernumbers = $input->multi_param('ordernumber');
36
37 my $csv_profile_id = $input->param('csv_profile');
38
39 unless ( $csv_profile_id ) {
40     my @orders;
41     for my $ordernumber ( @ordernumbers ) {
42         my $order = GetOrder $ordernumber;
43         my $order_object = Koha::Acquisition::Orders->find($ordernumber);
44         my $claims = $order_object->claims;
45         push @orders, {
46                 orderdate => $order->{orderdate},
47                 latesince => $order->{latesince},
48                 estimateddeliverydate => $order->{estimateddeliverydate},
49                 supplier => $order->{supplier},
50                 supplierid => $order->{supplierid},
51                 title => $order->{title},
52                 author => $order->{author},
53                 publisher => $order->{publisher},
54                 unitpricesupplier => $order->{unitpricesupplier},
55                 quantity_to_receive => $order->{quantity_to_receive},
56                 subtotal => $order->{subtotal},
57                 budget => $order->{budget},
58                 basketname => $order->{basketname},
59                 basketno => $order->{basketno},
60                 claims_count => $claims->count,
61                 claimed_date => $claims->count ? $claims->last->claimed_on : undef,
62                 internalnote => $order->{order_internalnote},
63                 vendornote   => $order->{order_vendornote},
64                 isbn => $order->{isbn},
65             }
66         ;
67     }
68
69     # We want to export using the default profile, using the template acqui/csv/lateorders.tt
70     print $input->header(
71         -type       => 'text/csv',
72         -attachment => 'lateorders.csv',
73     );
74     $template->param( orders => \@orders );
75     for my $line ( split '\n', $template->output ) {
76         print "$line\n" unless $line =~ m|^\s*$|;
77     }
78     exit;
79 } else {
80     my $csv_profile = Koha::CsvProfiles->find($csv_profile_id);
81     my $content = '[% SET separator = "'.$csv_profile->csv_separator.'" ~%]' . $csv_profile->content;
82
83     my $csv = C4::Letters::_process_tt({
84         content => $content,
85         loops => { aqorders => \@ordernumbers },
86     });
87
88     print $input->header(
89         -type       => 'text/csv',
90         -attachment => 'lateorders.csv',
91         -charset    => 'UTF-8',
92     );
93     print Encode::encode_utf8($csv);
94     exit;
95 }