Bug 29408: Add JSDoc documentation for kohaTable function
[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
25 use Koha::CsvProfiles;
26
27 use Text::CSV_XS;
28
29 my $query = CGI->new;
30 my $supplierid = $query->param('supplierid');
31 my @serialids = $query->multi_param('serialid');
32 my $op = $query->param('op') || q{};
33
34 my $csv_profile_id = $query->param('csv_profile');
35 my $csv_profile = Koha::CsvProfiles->find( $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     my $header = $1;
54     my $field = ($2 eq '') ? $1 : $2;
55
56     $header =~ s/^\s+|\s+$//g; # Trim whitespaces
57     push @headers, $header;
58
59     $field =~ s/[^.]+\.//; # Remove the table name if exists.
60     $field =~ s/^\s+|\s+$//g; # Trim whitespaces
61     push @fields, $field;
62 }
63
64 my @rows;
65 for my $serialid ( @serialids ) {
66     my @missingissues = GetLateOrMissingIssues($supplierid, $serialid);
67     my $issue = $missingissues[0];
68     my @row;
69     for my $field ( @fields ) {
70         push @row, $issue->{$field};
71     }
72     push @rows, \@row;
73
74     # update claim date to let one know they have looked at this missing item
75     updateClaim($serialid);
76 }
77
78 print $query->header(
79     -type       => 'plain/text',
80     -attachment => "serials-claims.csv",
81 );
82
83 print join( $csv_profile->csv_separator, @headers ) . "\n";
84
85 for my $row ( @rows ) {
86     $csv->combine(@$row);
87     my $string = $csv->string;
88     print $string, "\n";
89 }