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