ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / serials / lateissues-export.pl
index a75b21c..e36e734 100755 (executable)
@@ -22,37 +22,43 @@ use C4::Serials;
 use C4::Acquisition;
 use C4::Output;
 use C4::Context;
-use C4::Csv qw( GetCsvProfile );
+
+use Koha::CsvProfiles;
 
 use Text::CSV_XS;
 
 my $query = new CGI;
 my $supplierid = $query->param('supplierid');
-my @serialids = $query->param('serialid');
+my @serialids = $query->multi_param('serialid');
 my $op = $query->param('op') || q{};
 
 my $csv_profile_id = $query->param('csv_profile');
-my $csv_profile = C4::Csv::GetCsvProfile( $csv_profile_id );
+my $csv_profile = Koha::CsvProfiles->find( $csv_profile_id );
 die "There is no valid csv profile given" unless $csv_profile;
 
 my $csv = Text::CSV_XS->new({
     'quote_char'  => '"',
     'escape_char' => '"',
-    'sep_char'    => $csv_profile->{csv_separator},
+    'sep_char'    => $csv_profile->csv_separator,
     'binary'      => 1
 });
 
-my $content = $csv_profile->{content};
+my $content = $csv_profile->content;
 my ( @headers, @fields );
 while ( $content =~ /
-    ([^=]+) # header
-    =
-    ([^\|]+) # fieldname (table.row or row)
+    ([^=\|]+) # header
+    =?
+    ([^\|]*) # fieldname (table.row or row)
     \|? /gxms
 ) {
-    push @headers, $1;
-    my $field = $2;
-    $field =~ s/[^\.]*\.?//; # Remove the table name if exists.
+    my $header = $1;
+    my $field = ($2 eq '') ? $1 : $2;
+
+    $header =~ s/^\s+|\s+$//g; # Trim whitespaces
+    push @headers, $header;
+
+    $field =~ s/[^.]+\.//; # Remove the table name if exists.
+    $field =~ s/^\s+|\s+$//g; # Trim whitespaces
     push @fields, $field;
 }
 
@@ -75,7 +81,7 @@ print $query->header(
     -attachment => "serials-claims.csv",
 );
 
-print join( $csv_profile->{csv_separator}, @headers ) . "\n";
+print join( $csv_profile->csv_separator, @headers ) . "\n";
 
 for my $row ( @rows ) {
     $csv->combine(@$row);