Bug 14997: Remove C4::Dates from tools (import / export)
authorMarc Véron <veron@veron.ch>
Sat, 10 Oct 2015 08:03:51 +0000 (10:03 +0200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 27 Oct 2015 13:10:18 +0000 (10:10 -0300)
This patch removes C4::Dates from:

- tools/export.pl
- tools/import_borrowers.pl

Note: For testing, both need preparation without patch, see below.

To test export:
- Without patch applied, go to
  Home > Tools > Export data > Export bibliographic records
- Define Start date / End date for Accession dates
- Export bibliographic records as 'without-patch.mrc'

- Do the same with patch, export as 'with-patch.mrc
- Compare the files, they should be the same

To test Import patrons:
- Without patch
- Go to Home > Tools > Import patrons
- Create a patron category like 'TEST' (useful for filtering...)
- Prepare a file with some patrons with category TEST to import.
  Fill date of birth, enrolment date, expiry date with values
  formatted in syspref format, in iso format and garbage
- Import
- Review the imported patrons (search for category TEST)

- With patch: Change cardnumber and names in import file
- Import
- Review again and compare with results from previous import.

Signed-off-by: Mirko Tietgen <mirko@abunchofthings.net>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

tools/export.pl
tools/import_borrowers.pl

index 80a15b4..a674248 100755 (executable)
@@ -29,6 +29,7 @@ use C4::Csv;
 use C4::Koha;               # GetItemTypes
 use C4::Output;
 use C4::Record;
+use Koha::DateUtils;
 
 my $query = new CGI;
 
@@ -198,15 +199,18 @@ if ( $op eq "export" ) {
         my $itemtype             = $query->param("itemtype");
         my $start_callnumber     = $query->param("start_callnumber");
         my $end_callnumber       = $query->param("end_callnumber");
-        $timestamp = ($timestamp) ? C4::Dates->new($timestamp) : ''
-          if ($commandline);
+        if ( $commandline ) {
+            $timestamp = eval { output_pref( { dt => dt_from_string( $timestamp ), dateonly => 1 }); };
+            $timestamp = '' unless ( $timestamp );
+        }
+
         my $start_accession =
           ( $query->param("start_accession") )
-          ? C4::Dates->new( $query->param("start_accession") )
+          ? eval { output_pref( { dt => dt_from_string( $query->param("start_accession") ), dateonly => 1, dateformat => 'iso' } ); }
           : '';
         my $end_accession =
           ( $query->param("end_accession") )
-          ? C4::Dates->new( $query->param("end_accession") )
+          ? eval { output_pref( { dt => dt_from_string( $query->param("end_accession") ), dateonly => 1, dateformat => 'iso' } ); }
           : '';
         $dont_export_items = $query->param("dont_export_item")
           unless ($commandline);
@@ -565,7 +569,7 @@ sub construct_query {
                 WHERE $biblioitemstable.timestamp >= ?
                   OR deleteditems.timestamp >= ?
             ) ";
-            my $ts = $timestamp->output('iso');
+            my $ts = eval { output_pref( { dt => dt_from_string( $timestamp ), dateonly => 1, dateformat => 'iso' }); };
             @sql_params = ( $ts, $ts, $ts, $ts );
         }
         else {
@@ -618,12 +622,12 @@ sub construct_query {
             }
             if ($start_accession) {
                 $sql_query .= " AND dateaccessioned >= ? ";
-                push @sql_params, $start_accession->output('iso');
+                push @sql_params, $start_accession;
             }
 
             if ($end_accession) {
                 $sql_query .= " AND dateaccessioned <= ? ";
-                push @sql_params, $end_accession->output('iso');
+                push @sql_params, $end_accession;
             }
 
             if ($itemtype) {
index e29a980..1876c88 100755 (executable)
@@ -39,7 +39,6 @@ use warnings;
 
 use C4::Auth;
 use C4::Output;
-use C4::Dates qw(format_date_in_iso);
 use C4::Context;
 use C4::Branch qw/GetBranchesLoop GetBranchName/;
 use C4::Members;
@@ -49,6 +48,7 @@ use C4::Members::Messaging;
 use C4::Reports::Guided;
 use C4::Templates;
 use Koha::Borrower::Debarments;
+use Koha::DateUtils;
 
 use Text::CSV;
 # Text::CSV::Unicode, even in binary mode, fails to parse lines with these diacriticals:
@@ -141,11 +141,9 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
     }
 
     push @feedback, {feedback=>1, name=>'headerrow', value=>join(', ', @csvcolumns)};
-    my $today_iso = C4::Dates->new()->output('iso');
+    my $today_iso = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
     my @criticals = qw(surname branchcode categorycode);    # there probably should be others
     my @bad_dates;  # I've had a few.
-    my $date_re = C4::Dates->new->regexp('syspref');
-    my  $iso_re = C4::Dates->new->regexp('iso');
     LINE: while ( my $borrowerline = <$handle> ) {
         my %borrower;
         my @missing_criticals;
@@ -211,9 +209,8 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
        # Popular spreadsheet applications make it difficult to force date outputs to be zero-padded, but we require it.
         foreach (qw(dateofbirth dateenrolled dateexpiry)) {
             my $tempdate = $borrower{$_} or next;
-            if ($tempdate =~ /$date_re/) {
-                $borrower{$_} = format_date_in_iso($tempdate);
-            } elsif ($tempdate =~ /$iso_re/) {
+            $tempdate = eval { output_pref( { dt => dt_from_string( $tempdate ), dateonly => 1, dateformat => 'iso' } ); };
+            if ($tempdate) {
                 $borrower{$_} = $tempdate;
             } else {
                 $borrower{$_} = '';