Bug 36511: Some scripts missing a dependency following Bug 24879
[koha.git] / serials / lateissues-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 C4::Auth;
21 use C4::Serials qw( GetLateOrMissingIssues updateClaim );
22 use C4::Output;
23 use C4::Context;
24 use C4::Auth qw( check_cookie_auth );
25
26 use Koha::CsvProfiles;
27
28 use Text::CSV_XS;
29
30 my $query = CGI->new;
31 my ($auth_status) =
32     check_cookie_auth( $query->cookie('CGISESSID'), { catalogue => 1 } );
33 if ( $auth_status ne "ok" ) {
34     print $query->header( -type => 'text/plain', -status => '403 Forbidden' );
35     exit 0;
36 }
37
38 my $supplierid = $query->param('supplierid');
39 my @serialids = $query->multi_param('serialid');
40 my $op = $query->param('op') || q{};
41
42 my $csv_profile_id = $query->param('csv_profile');
43 my $csv_profile = Koha::CsvProfiles->find( $csv_profile_id );
44 die "There is no valid csv profile given" unless $csv_profile;
45
46 my $delimiter = $csv_profile->csv_separator;
47 $delimiter = "\t" if $delimiter eq "\\t";
48
49 my $csv = Text::CSV_XS->new({
50     'quote_char'  => '"',
51     'escape_char' => '"',
52     'sep_char'    => $delimiter,
53     'binary'      => 1
54 });
55
56 my $content = $csv_profile->content;
57 my ( @headers, @fields );
58 while ( $content =~ /
59     ([^=\|]+) # header
60     =?
61     ([^\|]*) # fieldname (table.row or row)
62     \|? /gxms
63 ) {
64     my $header = $1;
65     my $field = ($2 eq '') ? $1 : $2;
66
67     $header =~ s/^\s+|\s+$//g; # Trim whitespaces
68     push @headers, $header;
69
70     $field =~ s/[^.]+\.//; # Remove the table name if exists.
71     $field =~ s/^\s+|\s+$//g; # Trim whitespaces
72     push @fields, $field;
73 }
74
75 my @rows;
76 for my $serialid ( @serialids ) {
77     my @missingissues = GetLateOrMissingIssues($supplierid, $serialid);
78     my $issue = $missingissues[0];
79     my @row;
80     for my $field ( @fields ) {
81         push @row, $issue->{$field};
82     }
83     push @rows, \@row;
84
85     # update claim date to let one know they have looked at this missing item
86     updateClaim($serialid);
87 }
88
89 print $query->header(
90     -type       => 'plain/text',
91     -attachment => "serials-claims.csv",
92 );
93
94 print join( $delimiter, @headers ) . "\n";
95
96 for my $row ( @rows ) {
97     $csv->combine(@$row);
98     my $string = $csv->string;
99     print $string, "\n";
100 }