Bug 10572: DBrev 3.13.00.037
[koha.git] / serials / lateissues-excel.pl
index e07eb93..4103d3b 100755 (executable)
 #!/usr/bin/perl
 
-use strict;
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
 use CGI;
 use C4::Auth;
 use C4::Serials;
 use C4::Acquisition;
 use C4::Output;
 use C4::Context;
+use C4::Csv qw( GetCsvProfile );
 
-# use Date::Manip;
 use Text::CSV_XS;
 
-
-# &Date_Init("DateFormat=non-US"); # set non-USA date, eg:19/08/2005
-
-
-my $csv = Text::CSV_XS->new(
-        {
-            'quote_char'  => '"',
-            'escape_char' => '"',
-            'sep_char'    => ',',
-            'binary'      => 1
-        }
-    );
-
-
 my $query = new CGI;
 my $supplierid = $query->param('supplierid');
-my @serialid = $query->param('serialid');
-my $op = $query->param('op');
-my $serialidcount = @serialid;
-
-my %supplierlist = GetSuppliersWithLateIssues;
-my @select_supplier;
-
-my @loop1;
-my ($count, @lateissues);
-if($op ne 'claims'){
-    ($count, @lateissues) = GetLateIssues($supplierid);
-    for (my $i=0;$i<@lateissues;$i++){
-        my @rows1 = ($lateissues[$i]->{'name'},          # lets build up a row
-                    $lateissues[$i]->{'title'}, 
-                     $lateissues[$i]->{'serialseq'},
-                     $lateissues[$i]->{'planneddate'},
-                     );
-        push (@loop1, \@rows1);
-    }
+my @serialids = $query->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 );
+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},
+    'binary'      => 1
+});
+
+my $content = $csv_profile->{content};
+my ( @headers, @fields );
+while ( $content =~ /
+    ([^=]+) # header
+    =
+    ([^\|]+) # fieldname (table.row or row)
+    \|? /gxms
+) {
+    push @headers, $1;
+    my $field = $2;
+    $field =~ s/[^\.]*\.?//; # Remove the table name if exists.
+    push @fields, $field;
 }
-my $totalcount2 = 0;
-my @loop2;
-my ($count2, @missingissues);
-for (my $k=0;$k<@serialid;$k++){
-    ($count2, @missingissues) = GetMissingIssues($supplierid, $serialid[$k]);
 
-    for (my $j=0;$j<@missingissues;$j++){
-       my @rows2 = ($missingissues[$j]->{'name'},          # lets build up a row
-                    $missingissues[$j]->{'title'}, 
-                     $missingissues[$j]->{'serialseq'},
-                     $missingissues[$j]->{'planneddate'},
-                     );
-        push (@loop2, \@rows2);
+my @rows;
+for my $serialid ( @serialids ) {
+    my @missingissues = GetLateOrMissingIssues($supplierid, $serialid);
+    my $issue = $missingissues[0];
+    my @row;
+    for my $field ( @fields ) {
+        push @row, $issue->{$field};
     }
-    $totalcount2 = $totalcount2 + $count2;
-    # update claim date to let one know they have looked at this missing item
-    updateClaim($serialid[$k]);
-}
+    push @rows, \@row;
 
-my $heading ='';
-my $filename ='';
-if($supplierid){
-    if($missingissues[0]->{'name'}){ # if exists display supplier name in heading for neatness
-       # not necessarily needed as the name will appear in supplier column also
-        $heading = "FOR $missingissues[0]->{'name'}";
-       $filename = "_$missingissues[0]->{'name'}"; 
-    }
+    # update claim date to let one know they have looked at this missing item
+    updateClaim($serialid);
 }
 
 print $query->header(
-        -type       => 'application/vnd.ms-excel',
-        -attachment => "claims".$filename.".csv",
-    );
-
-if($op ne 'claims'){
-    print "LATE ISSUES ".$heading."\n\n";
-    print "SUPPLIER,TITLE,ISSUE NUMBER,LATE SINCE\n";
-
-    for my $row ( @loop1 ) {
-    
-        $csv->combine(@$row);
-        my $string = $csv->string;
-        print $string, "\n";
-    }
-
-    print ",,,,,,,\n\n";
-}
-if($serialidcount == 1){
-    print "MISSING ISSUE ".$heading."\n\n";
-} else {
-    print "MISSING ISSUES ".$heading."\n\n";
-}
-print "SUPPLIER,TITLE,ISSUE NUMBER,LATE SINCE\n";
+    -type       => 'plain/text',
+    -attachment => "serials-claims.csv",
+);
 
-for my $row ( @loop2 ) {
-    
-        $csv->combine(@$row);
-        my $string = $csv->string;
-        print $string, "\n";
-    }
-
-print ",,,,,,,\n";
-print ",,,,,,,\n";
-if($op ne 'claims'){
-    print ",,Total Number Late, $count\n";
-}
-if($serialidcount == 1){
+print join( $csv_profile->{csv_separator}, @headers ) . "\n";
 
-} else {
-    print ",,Total Number Missing, $totalcount2\n";
+for my $row ( @rows ) {
+    $csv->combine(@$row);
+    my $string = $csv->string;
+    print $string, "\n";
 }