Bug 10854: add ability to export serial claims using CSV profile.
authorJonathan Druart <jonathan.druart@biblibre.com>
Tue, 10 Sep 2013 13:24:07 +0000 (15:24 +0200)
committerGalen Charlton <gmc@esilibrary.com>
Fri, 11 Oct 2013 03:33:30 +0000 (03:33 +0000)
Test plan:
- Create a CSV profile (or use the default one) with a type 'sql'.
- Go to serials/claims.pl, select the wanted CSV profile and click on
  the "Export selected items data".
- Verify the CSV file is correctly generated.

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Work as described. No koha-qa errors

On top of 10853 (solving merge conflict)

Need to do homework to test. Add subscription and serial claim
notice.

1) Create a new CSV profile, type SQL, copy sample fields
2) Go to claims, select vendor
3) Go to Export selected items with created profile
4) CSV (in my case I use | as separator) download successfully
5) All fields present, file correct

Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Passes all tests and QA script.
Works as advertised, keeps existing format by porting it to
a SQL profile.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
installer/data/mysql/en/optional/csv_profiles.sql [new file with mode: 0644]
installer/data/mysql/en/optional/csv_profiles.txt [new file with mode: 0644]
installer/data/mysql/updatedatabase.pl
koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt
serials/claims.pl
serials/lateissues-excel.pl

diff --git a/installer/data/mysql/en/optional/csv_profiles.sql b/installer/data/mysql/en/optional/csv_profiles.sql
new file mode 100644 (file)
index 0000000..ac42760
--- /dev/null
@@ -0,0 +1 @@
+INSERT IGNORE INTO export_format( profile, description, content, csv_separator, type ) VALUES ( "issues to claim", "default csv export for issues claims", "SUPPLIER=aqbooksellers.name|TITLE=subscription.title|ISSUE NUMBER=serial.serialseq|LATE SINCE=serial.planneddate", ",", "sql" );
diff --git a/installer/data/mysql/en/optional/csv_profiles.txt b/installer/data/mysql/en/optional/csv_profiles.txt
new file mode 100644 (file)
index 0000000..af07c86
--- /dev/null
@@ -0,0 +1,3 @@
+csv profiles
+
+issues to claim: Bug 10854 allows to choose a csv profile for exporting claims. This profile is the default one.
index 566715e..4f3d173 100755 (executable)
@@ -7204,6 +7204,30 @@ if ( CheckVersion($DBversion) ) {
     SetVersion($DBversion);
 }
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+$DBversion = "3.13.00.XXX";
+if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
+    $dbh->do(q{
+        INSERT IGNORE INTO export_format( profile, description, content, csv_separator, type ) VALUES ( "issues to claim", "default csv export for issues claims", "SUPPLIER=aqbooksellers.name|TITLE=subscription.title|ISSUE NUMBER=serial.serialseq|LATE SINCE=serial.planneddate", ",", "sql" )
+    });
+    print "Upgrade to $DBversion done (Bug 10854: Add the default csv profile for claiming issues)\n";
+    SetVersion($DBversion);
+}
+
+
+
 =head1 FUNCTIONS
 
 =head2 TableExists($table)
index 1346b57..b2b5c1a 100644 (file)
@@ -14,7 +14,7 @@
          var sTable = $("#claimst").dataTable($.extend(true, {}, dataTablesDefaults, {
             "sDom": 't',
             "aoColumnDefs": [
-                { "aTargets": [ 0,1,-1 ], "bSortable": false, "bSearchable": false }
+                { "aTargets": [ 0,1 ], "bSortable": false, "bSearchable": false }
             ],
             "bPaginate": false
         }));
@@ -25,7 +25,6 @@
 
            // Checkboxes : Select All / None
            $("span.checkall").html("<input type=\"checkbox\" name=\"CheckAll\"> "+_("Check All")+"</input>");
-           $("span.exportSelected").html("<a id=\"ExportSelected\" href=\"/cgi-bin/koha/serials/claims.pl\"> "+_("Export selected items data") +"<\/a>");
 
         $("#CheckAll").click(function() {
             $("#claimst tr:visible :checkbox").attr('checked', $("#CheckAll").is(':checked'));
@@ -46,6 +45,7 @@
                for (var i = 0; i < selected.length; i++) {
                    url += '&amp;serialid=' + selected[i].value;
                }
+        url += '&amp;csv_profile=' + $("#csv_profile_for_export option:selected").val();
                // And redirecting to the CSV page
                location.href = url;
                return false;
                     <th>Status</th>
                     <th>Since</th>
                     <th>Claim date</th>
-                <th>Begin claim</th>
                 </tr></thead>
                 <tbody>[% FOREACH missingissue IN missingissues %]
                     <tr>
                         <td>
                         [% missingissue.claimdate %]
                         </td>
-                        <td>
-                            <a href="/cgi-bin/koha/serials/lateissues-excel.pl?supplierid=[% missingissue.supplieri %]&amp;serialid=[% missingissue.serialid %]&amp;op=claims">Export item data</a>
-                        </td>
                     </tr>
                 [% END %]</tbody>
             </table>
-            <p><span class="exportSelected"></span></p>
+
+        [% IF csv_profiles %]
+          <p>
+            <span class="exportSelected"><a id="ExportSelected" href="/cgi-bin/koha/serials/claims.pl">Export selected items data</a></span>
+            using the following csv profile:
+            <select id="csv_profile_for_export">
+              [% FOR csv IN csv_profiles %]
+                <option value="[% csv.export_format_id %]">[% csv.profile %]</option>
+               [% END %]
+            </select>
+          </p>
+        [% END %]
 
 [% IF ( letter ) %]
         <fieldset class="action"> <label for="letter_code">Select notice:</label>
index 5434b2a..5377478 100755 (executable)
@@ -28,6 +28,7 @@ use C4::Bookseller qw( GetBookSeller );
 use C4::Context;
 use C4::Letters;
 use C4::Branch;    # GetBranches GetBranchesLoop
+use C4::Csv qw( GetCsvProfiles );
 
 my $input = CGI->new;
 
@@ -98,6 +99,7 @@ $template->param(
         claimletter => $claimletter,
         supplierloop => \@supplierinfo,
         branchloop   => $branchloop,
+        csv_profiles => C4::Csv::GetCsvProfiles( "sql" ),
         (uc(C4::Context->preference("marcflavour"))) => 1
         );
 output_html_with_http_headers $input, $cookie, $template->output;
index e5cc849..4103d3b 100755 (executable)
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-use strict;
-use warnings;
+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 @serialids = $query->param('serialid');
 my $op = $query->param('op') || q{};
-my $serialidcount = @serialid;
 
-my @loop1;
-my @lateissues;
-if($op ne 'claims'){
-    @lateissues = GetLateIssues($supplierid);
-    for my $issue (@lateissues){
-        push @loop1,
-      [ $issue->{'name'}, $issue->{'title'}, $issue->{'serialseq'}, $issue->{'planneddate'},];
-    }
+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 @missingissues;
-for (my $k=0;$k<@serialid;$k++){
-    @missingissues = GetLateOrMissingIssues($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 += scalar @missingissues;
-    # 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";
+    -type       => 'plain/text',
+    -attachment => "serials-claims.csv",
+);
 
-    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";
-
-for my $row ( @loop2 ) {
-
-        $csv->combine(@$row);
-        my $string = $csv->string;
-        print $string, "\n";
-    }
-
-print ",,,,,,,\n";
-print ",,,,,,,\n";
-if($op ne 'claims'){
-    my $count = scalar @lateissues;
-    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";
 }