fix date parsing when using international dates (package date edit), RT#8027
authorivan <ivan>
Fri, 9 Apr 2010 08:18:24 +0000 (08:18 +0000)
committerivan <ivan>
Fri, 9 Apr 2010 08:18:24 +0000 (08:18 +0000)
18 files changed:
FS/FS/Mason.pm
FS/FS/Misc/DateTime.pm [new file with mode: 0644]
FS/FS/UI/Web.pm
FS/FS/cust_main/Import.pm
FS/MANIFEST
httemplate/edit/REAL_cust_pkg.cgi
httemplate/edit/cust_pay.cgi
httemplate/edit/process/REAL_cust_pkg.cgi
httemplate/edit/process/cust_main.cgi
httemplate/edit/process/cust_pay.cgi
httemplate/edit/process/quick-charge.cgi
httemplate/edit/process/quick-cust_pkg.cgi
httemplate/misc/process/cancel_pkg.html
httemplate/misc/process/delay_susp_pkg.html
httemplate/search/elements/cust_main_dayranges.html
httemplate/search/pay_batch.cgi
httemplate/search/report_prepaid_income.cgi
httemplate/search/sqlradius.cgi

index 82e64f2..f65d972 100644 (file)
@@ -57,12 +57,12 @@ if ( -e $addl_handler_use_file ) {
   use List::Util qw( max min );
   use Data::Dumper;
   use Date::Format;
   use List::Util qw( max min );
   use Data::Dumper;
   use Date::Format;
-  use Date::Parse;
   use Time::Local;
   use Time::HiRes;
   use Time::Duration;
   use DateTime;
   use DateTime::Format::Strptime;
   use Time::Local;
   use Time::HiRes;
   use Time::Duration;
   use DateTime;
   use DateTime::Format::Strptime;
+  use FS::Misc::DateTime qw( parse_datetime );
   use Lingua::EN::Inflect qw(PL);
   use Tie::IxHash;
   use URI;
   use Lingua::EN::Inflect qw(PL);
   use Tie::IxHash;
   use URI;
diff --git a/FS/FS/Misc/DateTime.pm b/FS/FS/Misc/DateTime.pm
new file mode 100644 (file)
index 0000000..a32c15a
--- /dev/null
@@ -0,0 +1,64 @@
+package FS::Misc::DateTime;
+
+use base qw( Exporter );
+use vars qw( @EXPORT_OK );
+use Carp;
+use Date::Parse;
+use DateTime::Format::Natural;
+use FS::Conf;
+
+@EXPORT_OK = qw( parse_datetime );
+
+=head1 NAME
+
+FS::Misc::DateTime - Date and time subroutines
+
+=head1 SYNOPSIS
+
+use FS::Misc::DateTime qw( parse_datetime );
+
+=head1 SUBROUTINES
+
+=over 4
+
+=item parse_datetime STRING
+
+Parses a date (and possibly time) from the supplied string and returns
+the date as an integer UNIX timestamp.
+
+=cut
+
+sub parse_datetime {
+  my $string = shift;
+  return '' unless $string =~ /\S/;
+
+  my $conf = new FS::Conf;
+  my $format = $conf->config('date_format') || '%m/%d/%Y';
+
+  if ( $format eq '%d/%m/%Y' ) { #  =~ /\%d.*\%m/ ) {
+    #$format =~ s/\%//g;
+    my $parser = DateTime::Format::Natural->new( 'time_zone' => 'local',
+                                                 #'format'=>'d/m/y',#lc($format)
+                                               );
+    $dt = $parser->parse_datetime($string);
+    if ( $parser->success ) {
+      return $dt->epoch;
+    } else {
+      #carp "WARNING: can't parse date: ". $parser->error;
+      #return '';
+      #huh, very common, we still need the "partially" (fully enough for our purposes) parsed date.
+      $dt->epoch;
+    }
+  } else {
+    return str2time($string);
+  }
+  
+}
+
+=back
+
+=head1 BUGS
+
+=cut
+
+1;
index c9264a0..821b191 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use vars qw($DEBUG @ISA @EXPORT_OK $me);
 use Exporter;
 use FS::Conf;
 use vars qw($DEBUG @ISA @EXPORT_OK $me);
 use Exporter;
 use FS::Conf;
+use FS::Misc::DateTime qw( parse_datetime );
 use FS::Record qw(dbdef);
 use FS::cust_main;  # are sql_balance and sql_date_balance in the right module?
 
 use FS::Record qw(dbdef);
 use FS::cust_main;  # are sql_balance and sql_date_balance in the right module?
 
@@ -30,7 +31,7 @@ sub parse_beginning_ending {
   if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
     $beginning = $1;
   } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
   if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
     $beginning = $1;
   } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
-    $beginning = str2time($1) || 0;
+    $beginning = parse_datetime($1) || 0;
   }
 
   my $ending = 4294967295; #2^32-1
   }
 
   my $ending = 4294967295; #2^32-1
@@ -38,7 +39,7 @@ sub parse_beginning_ending {
     $ending = $1 - 1;
   } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
     #probably need an option to turn off the + 86399
     $ending = $1 - 1;
   } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
     #probably need an option to turn off the + 86399
-    $ending = str2time($1) + 86399;
+    $ending = parse_datetime($1) + 86399;
   }
 
   ( $beginning, $ending );
   }
 
   ( $beginning, $ending );
index f477323..901ff18 100644 (file)
@@ -5,8 +5,8 @@ use vars qw( $DEBUG $conf );
 use Storable qw(thaw);
 use Data::Dumper;
 use MIME::Base64;
 use Storable qw(thaw);
 use Data::Dumper;
 use MIME::Base64;
-use Date::Parse;
 use File::Slurp qw( slurp );
 use File::Slurp qw( slurp );
+use FS::Misc::DateTime qw( parse_datetime );
 use FS::UID qw( dbh );
 use FS::Record qw( qsearchs );
 use FS::cust_main;
 use FS::UID qw( dbh );
 use FS::Record qw( qsearchs );
 use FS::cust_main;
@@ -264,13 +264,13 @@ sub batch_import {
 
       if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
 
 
       if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
 
-        #$cust_pkg{$1} = str2time( shift @$columns );
+        #$cust_pkg{$1} = parse_datetime( shift @$columns );
         if ( $1 eq 'pkgpart' ) {
           $cust_pkg{$1} = shift @columns;
         } elsif ( $1 eq 'setup' ) {
         if ( $1 eq 'pkgpart' ) {
           $cust_pkg{$1} = shift @columns;
         } elsif ( $1 eq 'setup' ) {
-          $billtime = str2time(shift @columns);
+          $billtime = parse_datetime(shift @columns);
         } else {
         } else {
-          $cust_pkg{$1} = str2time( shift @columns );
+          $cust_pkg{$1} = parse_datetime( shift @columns );
         } 
 
       } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
         } 
 
       } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
index b341517..900d3a1 100644 (file)
@@ -503,3 +503,4 @@ FS/part_event/Action/pkg_agent_credit.pm
 FS/part_event/Action/pkg_agent_credit_pkg.pm
 FS/part_event/Action/pkg_employee_credit.pm
 FS/part_event/Action/pkg_employee_credit_pkg.pm
 FS/part_event/Action/pkg_agent_credit_pkg.pm
 FS/part_event/Action/pkg_employee_credit.pm
 FS/part_event/Action/pkg_employee_credit_pkg.pm
+FS/Misc/DateTime.pm
index 29c8ca6..77ab1fe 100755 (executable)
@@ -187,9 +187,9 @@ if ( $cgi->param('error') ) {
   $cust_pkg = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
   die "No package!" unless $cust_pkg;
 
   $cust_pkg = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
   die "No package!" unless $cust_pkg;
 
-  foreach my $col (qw( setup last_bill bill adjourn expire )) {
+  foreach my $col (qw( start_date setup last_bill bill adjourn expire )) {
     my $value = $cgi->param($col);
     my $value = $cgi->param($col);
-    $cust_pkg->set( $col, $value ? str2time($value) : '' );
+    $cust_pkg->set( $col, $value ? parse_datetime($value) : '' );
   }
 
 } else {
   }
 
 } else {
index a6b73b1..cc4ec60 100755 (executable)
@@ -113,7 +113,7 @@ if ( $cgi->param('error') ) {
   $paid     = $cgi->param('paid');
   $payby    = $cgi->param('payby');
   $payinfo  = $cgi->param('payinfo');
   $paid     = $cgi->param('paid');
   $payby    = $cgi->param('payby');
   $payinfo  = $cgi->param('payinfo');
-  $_date    = $cgi->param('_date') ? str2time($cgi->param('_date')) : time;
+  $_date    = $cgi->param('_date') ? parse_datetime($cgi->param('_date')) : time;
 } elsif ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
   $link     = $cgi->param('popup') ? 'popup' : 'custnum';
   $linknum  = $1;
 } elsif ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
   $link     = $cgi->param('popup') ? 'popup' : 'custnum';
   $linknum  = $1;
index b5796eb..570f0e0 100755 (executable)
@@ -19,13 +19,8 @@ die "access denied"
 my $pkgnum = $cgi->param('pkgnum') or die;
 my $old = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
 my %hash = $old->hash;
 my $pkgnum = $cgi->param('pkgnum') or die;
 my $old = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
 my %hash = $old->hash;
-$hash{'start_date'} = $cgi->param('start_date') ? str2time($cgi->param('start_date')) : '';
-$hash{'setup'} = $cgi->param('setup') ? str2time($cgi->param('setup')) : '';
-$hash{'bill'} = $cgi->param('bill') ? str2time($cgi->param('bill')) : '';
-$hash{'last_bill'} =
-  $cgi->param('last_bill') ? str2time($cgi->param('last_bill')) : '';
-$hash{'adjourn'} = $cgi->param('adjourn') ? str2time($cgi->param('adjourn')) : '';
-$hash{'expire'} = $cgi->param('expire') ? str2time($cgi->param('expire')) : '';
+$hash{$_}= $cgi->param($_) ? parse_datetime($cgi->param($_)) : ''
+  foreach qw( start_date setup bill last_bill adjourn expire );
 
 my @errors = ();
 
 
 my @errors = ();
 
@@ -47,7 +42,6 @@ my $error;
 if ( @errors ) {
   $error = join(',', @errors);
 } else {
 if ( @errors ) {
   $error = join(',', @errors);
 } else {
-  warn join(',',%hash);
   $new = new FS::cust_pkg \%hash;
   $error = $new->replace($old);
 }
   $new = new FS::cust_pkg \%hash;
   $error = $new->replace($old);
 }
index f72ca0a..1311ba4 100755 (executable)
@@ -98,7 +98,7 @@ foreach my $dfield (qw( birthdate signupdate )) {
 
     } else {
 
 
     } else {
 
-      $parsed = str2time($value)
+      $parsed = parse_datetime($value)
         or $error ||= "Invalid $dfield: $value";
 
     }
         or $error ||= "Invalid $dfield: $value";
 
     }
index a310c53..df506c6 100755 (executable)
@@ -39,7 +39,7 @@ $cgi->param('link') =~ /^(custnum|invnum|popup)$/
 my $field = my $link = $1;
 $field = 'custnum' if $field eq 'popup';
 
 my $field = my $link = $1;
 $field = 'custnum' if $field eq 'popup';
 
-my $_date = str2time($cgi->param('_date'));
+my $_date = parse_datetime($cgi->param('_date'));
 
 my $new = new FS::cust_pay ( {
   $field => $linknum,
 
 my $new = new FS::cust_pay ( {
   $field => $linknum,
index 27921b8..484f6fc 100644 (file)
@@ -58,7 +58,7 @@ unless ( $error ) {
     'bill_now'      => scalar($cgi->param('bill_now')),
     'invoice_terms' => scalar($cgi->param('invoice_terms')),
     'start_date'    => ( scalar($cgi->param('start_date'))
     'bill_now'      => scalar($cgi->param('bill_now')),
     'invoice_terms' => scalar($cgi->param('invoice_terms')),
     'start_date'    => ( scalar($cgi->param('start_date'))
-                           ? str2time($cgi->param('start_date'))
+                           ? parse_datetime($cgi->param('start_date'))
                            : ''
                        ),
     'no_auto'       => scalar($cgi->param('no_auto')),
                            : ''
                        ),
     'no_auto'       => scalar($cgi->param('no_auto')),
index 202d0a3..2fde17f 100644 (file)
@@ -54,7 +54,7 @@ my $cust_pkg = new FS::cust_pkg {
   'custnum'              => $custnum,
   'pkgpart'              => $pkgpart,
   'start_date'           => ( scalar($cgi->param('start_date'))
   'custnum'              => $custnum,
   'pkgpart'              => $pkgpart,
   'start_date'           => ( scalar($cgi->param('start_date'))
-                                ? str2time($cgi->param('start_date'))
+                                ? parse_datetime($cgi->param('start_date'))
                                 : ''
                             ),
   'no_auto'              => scalar($cgi->param('no_auto')),
                                 : ''
                             ),
   'no_auto'              => scalar($cgi->param('no_auto')),
index 669af9c..e17872c 100755 (executable)
@@ -44,7 +44,7 @@ my $date = time;
 if ($method eq 'expire' || $method eq 'adjourn'){
   #untaint date
   $date = $cgi->param('date');
 if ($method eq 'expire' || $method eq 'adjourn'){
   #untaint date
   $date = $cgi->param('date');
-  str2time($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date";
+  parse_datetime($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date";
   $date = $1;
   $method = ($method eq 'expire') ? 'cancel' : 'suspend';
 }
   $date = $1;
   $method = ($method eq 'expire') ? 'cancel' : 'suspend';
 }
index c7cc7de..8649cc2 100755 (executable)
@@ -21,7 +21,7 @@ $cgi->param('pkgnum') =~ /^(\d+)$/ or die "Illegal pkgnum";
 $pkgnum = $1;
 
 #untaint date
 $pkgnum = $1;
 
 #untaint date
-str2time($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date";
+parse_datetime($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date";
 my $date = $1;
 
 $cust_pkg = qsearchs( 'cust_pkg', {'pkgnum'=>$pkgnum} );
 my $date = $1;
 
 $cust_pkg = qsearchs( 'cust_pkg', {'pkgnum'=>$pkgnum} );
index 9e4e86f..b3f1a2e 100644 (file)
@@ -110,7 +110,7 @@ my $range_sub = delete($opt{'range_sub'}); #or die
 
 my $offset = 0;
 if($cgi->param('as_of')) {
 
 my $offset = 0;
 if($cgi->param('as_of')) {
-  $offset = int((time - str2time($cgi->param('as_of'))) / 86400);
+  $offset = int((time - parse_datetime($cgi->param('as_of'))) / 86400);
   $opt{'title'} .= ' ('.$cgi->param('as_of').')' if $offset > 0;
 }
 
   $opt{'title'} .= ' ('.$cgi->param('as_of').')' if $offset > 0;
 }
 
index fb45287..ebd323e 100755 (executable)
@@ -97,12 +97,12 @@ my($begin, $end) = ( '', '' );
 my @where;
 if ( $cgi->param('beginning')
      && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
 my @where;
 if ( $cgi->param('beginning')
      && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
-  $begin = str2time($1);
+  $begin = parse_datetime($1);
   push @where, "download >= $begin";
 }
 if ( $cgi->param('ending')
       && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
   push @where, "download >= $begin";
 }
 if ( $cgi->param('ending')
       && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
-  $end = str2time($1) + 86399;
+  $end = parse_datetime($1) + 86399;
   push @where, "download < $end";
 }
 
   push @where, "download < $end";
 }
 
index c0e2b80..bfb699b 100644 (file)
@@ -84,7 +84,7 @@ my $actual_label = $legacy ? 'Actual ' : '';
 
 my $time = time;
 
 
 my $time = time;
 
-my $now = $cgi->param('date') && str2time($cgi->param('date')) || $time;
+my $now = $cgi->param('date') && parse_datetime($cgi->param('date')) || $time;
 $now =~ /^(\d+)$/ or die "unparsable date?";
 $now = $1;
 
 $now =~ /^(\d+)$/ or die "unparsable date?";
 $now = $1;
 
index 29f3602..cca1211 100644 (file)
@@ -100,11 +100,11 @@ die "access denied"
 my( $beginning, $ending ) = ( '', '' );
 if ( $cgi->param('stoptime_beginning')
      && $cgi->param('stoptime_beginning') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) {
 my( $beginning, $ending ) = ( '', '' );
 if ( $cgi->param('stoptime_beginning')
      && $cgi->param('stoptime_beginning') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) {
-  $beginning = str2time($1);
+  $beginning = parse_datetime($1);
 }
 if ( $cgi->param('stoptime_ending')
      && $cgi->param('stoptime_ending') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) {
 }
 if ( $cgi->param('stoptime_ending')
      && $cgi->param('stoptime_ending') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) {
-  $ending = str2time($1); # + 86399;
+  $ending = parse_datetime($1); # + 86399;
 }
 if ( $cgi->param('begin') && $cgi->param('begin') =~ /^(\d+)$/ ) {
   $beginning = $1;
 }
 if ( $cgi->param('begin') && $cgi->param('begin') =~ /^(\d+)$/ ) {
   $beginning = $1;
@@ -121,11 +121,11 @@ if ( $cgi->param('open_sessions') =~ /^(\d*)$/ ) {
 my( $starttime_beginning, $starttime_ending ) = ( '', '' );
 if ( $cgi->param('starttime_beginning')
      && $cgi->param('starttime_beginning') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) {
 my( $starttime_beginning, $starttime_ending ) = ( '', '' );
 if ( $cgi->param('starttime_beginning')
      && $cgi->param('starttime_beginning') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) {
-  $starttime_beginning = str2time($1);
+  $starttime_beginning = parse_datetime($1);
 }
 if ( $cgi->param('starttime_ending')
      && $cgi->param('starttime_ending') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) {
 }
 if ( $cgi->param('starttime_ending')
      && $cgi->param('starttime_ending') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) {
-  $starttime_ending = str2time($1); # + 86399;
+  $starttime_ending = parse_datetime($1); # + 86399;
 }
 
 my $cgi_svc_acct = '';
 }
 
 my $cgi_svc_acct = '';