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