Bug 11003: fix JS error on the staff cart page
[koha.git] / serials / lateissues-excel.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 use C4::Csv qw( GetCsvProfile );
26
27 use Text::CSV_XS;
28
29 my $query = new CGI;
30 my $supplierid = $query->param('supplierid');
31 my @serialids = $query->param('serialid');
32 my $op = $query->param('op') || q{};
33
34 my $csv_profile_id = $query->param('csv_profile');
35 my $csv_profile = C4::Csv::GetCsvProfile( $csv_profile_id );
36 die "There is no valid csv profile given" unless $csv_profile;
37
38 my $csv = Text::CSV_XS->new({
39     'quote_char'  => '"',
40     'escape_char' => '"',
41     'sep_char'    => $csv_profile->{csv_separator},
42     'binary'      => 1
43 });
44
45 my $content = $csv_profile->{content};
46 my ( @headers, @fields );
47 while ( $content =~ /
48     ([^=]+) # header
49     =
50     ([^\|]+) # fieldname (table.row or row)
51     \|? /gxms
52 ) {
53     push @headers, $1;
54     my $field = $2;
55     $field =~ s/[^\.]*\.?//; # Remove the table name if exists.
56     push @fields, $field;
57 }
58
59 my @rows;
60 for my $serialid ( @serialids ) {
61     my @missingissues = GetLateOrMissingIssues($supplierid, $serialid);
62     my $issue = $missingissues[0];
63     my @row;
64     for my $field ( @fields ) {
65         push @row, $issue->{$field};
66     }
67     push @rows, \@row;
68
69     # update claim date to let one know they have looked at this missing item
70     updateClaim($serialid);
71 }
72
73 print $query->header(
74     -type       => 'plain/text',
75     -attachment => "serials-claims.csv",
76 );
77
78 print join( $csv_profile->{csv_separator}, @headers ) . "\n";
79
80 for my $row ( @rows ) {
81     $csv->combine(@$row);
82     my $string = $csv->string;
83     print $string, "\n";
84 }