localize dates that will appear on invoices, #24850
authorMark Wells <mark@freeside.biz>
Tue, 28 Jan 2014 00:52:39 +0000 (16:52 -0800)
committerMark Wells <mark@freeside.biz>
Tue, 28 Jan 2014 00:52:39 +0000 (16:52 -0800)
FS/FS/TemplateItem_Mixin.pm
FS/FS/Template_Mixin.pm
FS/FS/cust_bill.pm
FS/FS/cust_bill_pkg.pm
FS/FS/cust_main_Mixin.pm
FS/FS/quotation_pkg.pm

index 7df75d4..6644329 100644 (file)
@@ -98,16 +98,19 @@ sub time_period_pretty {
 
   my $time_period;
   if ( defined($date_style) && $date_style eq 'month_of' ) {
-    $time_period = time2str('The month of %B', $self->sdate);
+    # (now watch, someone's going to make us do Chinese)
+    $time_period = $self->mt('The month of [_1]',
+                      $self->time2str_local('The month of %B', $self->sdate)
+                   );
   } elsif ( defined($date_style) && $date_style eq 'X_month' ) {
     my $desc = $conf->config( 'cust_bill-line_item-date_description',
                                $agentnum
                             );
     $desc .= ' ' unless $desc =~ /\s$/;
-    $time_period = $desc. time2str('%B', $self->sdate);
+    $time_period = $desc. $self->time2str_local('%B', $self->sdate);
   } else {
-    $time_period =      time2str($date_format, $self->sdate).
-                 " - ". time2str($date_format, $self->edate);
+    $time_period =      $self->time2str_local($date_format, $self->sdate).
+                 " - ". $self->time2str_local($date_format, $self->edate);
   }
 
   " ($time_period)";
index 990f31f..c8e5546 100644 (file)
@@ -498,9 +498,9 @@ sub print_generic {
     '_date'           => ( $params{'no_date'} ? '' : $self->_date ),
     'date'            => ( $params{'no_date'}
                              ? ''
-                             : time2str($date_format, $self->_date)
+                             : $self->time2str_local($date_format, $self->_date)
                          ),
-    'today'           => time2str($date_format_long, $today),
+    'today'           => $self->time2str_local($date_format_long, $today),
     'terms'           => $self->terms,
     'template'        => $template, #params{'template'},
     'notice_name'     => $notice_name, # escape?
@@ -545,15 +545,9 @@ sub print_generic {
   );
  
   #localization
-  my $lh = FS::L10N->get_handle( $locale );
   $invoice_data{'emt'} = sub { &$escape_function($self->mt(@_)) };
-  my %info = FS::Locales->locale_info($cust_main->locale || 'en_US');
-  # eval to avoid death for unimplemented languages
-  my $dh = eval { Date::Language->new($info{'name'}) } ||
-           Date::Language->new(); # fall back to English
   # prototype here to silence warnings
-  $invoice_data{'time2str'} = sub ($;$$) { $dh->time2str(@_) };
-  # eventually use this date handle everywhere in here, too
+  $invoice_data{'time2str'} = sub ($;$$) { $self->time2str_local(@_) };
 
   my $min_sdate = 999999999999;
   my $max_edate = 0;
@@ -566,8 +560,9 @@ sub print_generic {
   }
 
   $invoice_data{'bill_period'} = '';
-  $invoice_data{'bill_period'} = time2str('%e %h', $min_sdate) 
-    . " to " . time2str('%e %h', $max_edate)
+  $invoice_data{'bill_period'} = $self->time2str_local('%e %h', $min_sdate) 
+                                 . " to " .
+                                 $self->time2str_local('%e %h', $max_edate)
     if ($max_edate != 0 && $min_sdate != 999999999999);
 
   $invoice_data{finance_section} = '';
@@ -1721,7 +1716,7 @@ sub due_date {
 
 sub due_date2str {
   my $self = shift;
-  $self->due_date ? time2str(shift, $self->due_date) : '';
+  $self->due_date ? $self->time2str_local(shift, $self->due_date) : '';
 }
 
 sub balance_due_msg {
@@ -1743,7 +1738,7 @@ sub balance_due_date {
   my $duedate = '';
   if (    $conf->exists('invoice_default_terms') 
        && $conf->config('invoice_default_terms')=~ /^\s*Net\s*(\d+)\s*$/ ) {
-    $duedate = time2str($rdate_format, $self->_date + ($1*86400) );
+    $duedate = $self->time2str_local($rdate_format, $self->_date + ($1*86400) );
   }
   $duedate;
 }
@@ -1761,7 +1756,7 @@ Returns a string with the date, for example: "3/20/2008"
 
 sub _date_pretty {
   my $self = shift;
-  time2str($date_format, $self->_date);
+  $self->time2str_local($date_format, $self->_date);
 }
 
 =item _items_sections OPTIONS
@@ -2761,8 +2756,8 @@ sub _items_cust_bill_pkg {
         if ( $cust_bill_pkg->recur != 0 ) {
           push @b, {
             'description' => "$desc (".
-                             time2str($date_format, $cust_bill_pkg->sdate). ' - '.
-                             time2str($date_format, $cust_bill_pkg->edate). ')',
+                             $self->time2str_local($date_format, $cust_bill_pkg->sdate). ' - '.
+                             $self->time2str_local($date_format, $cust_bill_pkg->edate). ')',
             'amount'      => sprintf("%.2f", $cust_bill_pkg->recur),
           };
         }
index 63ae884..165bb1a 100644 (file)
@@ -1659,6 +1659,7 @@ sub send_csv {
   my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
   mkdir $spooldir, 0700 unless -d $spooldir;
 
+  # don't localize dates here, they're a defined format
   my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', time);
   my $file = "$spooldir/$tracctnum.csv";
   
@@ -2252,7 +2253,7 @@ sub print_csv {
             ? time2str("%x", $cust_bill_pkg->sdate)
             : '' ),
           ($cust_bill_pkg->edate 
-            ?time2str("%x", $cust_bill_pkg->edate)
+            ? time2str("%x", $cust_bill_pkg->edate)
             : '' ),
         );
   
@@ -2966,7 +2967,7 @@ sub _items_previous {
   foreach ( @pr_cust_bill ) {
     my $date = $conf->exists('invoice_show_prior_due_date')
                ? 'due '. $_->due_date2str($date_format)
-               : time2str($date_format, $_->_date);
+               : $self->time2str_local($date_format, $_->_date);
     push @b, {
       'description' => $self->mt('Previous Balance, Invoice #'). $_->invnum. " ($date)",
       #'pkgpart'     => 'N/A',
@@ -3030,7 +3031,7 @@ sub _items_credits {
       #                 " (". time2str("%x",$_->cust_credit->_date) .")".
       #                 $reason,
       'description' => $self->mt('Credit applied').' '.
-                       time2str($date_format,$obj->_date). $reason,
+                       $self->time2str_local($date_format,$obj->_date). $reason,
       'amount'      => sprintf("%.2f",$obj->amount),
     };
   }
@@ -3074,7 +3075,7 @@ sub _items_payments {
   foreach my $obj (@objects) {
     my $cust_pay = $obj->isa('FS::cust_pay') ? $obj : $obj->cust_pay;
     my $desc = $self->mt('Payment received').' '.
-               time2str($date_format, $cust_pay->_date );
+               $self->time2str_local($date_format, $cust_pay->_date );
     $desc .= $self->mt(' via ') .
              $cust_pay->payby_payinfo_pretty( $self->cust_main->locale )
       if $detailed;
index c71ebf4..b234d6f 100644 (file)
@@ -510,6 +510,21 @@ sub regularize_details {
 
 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
 
+=item cust_main
+
+Returns the customer (L<FS::cust_main> object) for this line item.
+
+=cut
+
+sub cust_main {
+  # required for cust_main_Mixin equivalence
+  # and use cust_bill instead of cust_pkg because this might not have a 
+  # cust_pkg
+  my $self = shift;
+  my $cust_bill = $self->cust_bill or return '';
+  $cust_bill->cust_main;
+}
+
 =item previous_cust_bill_pkg
 
 Returns the previous cust_bill_pkg for this package, if any.
index f584b41..92dea28 100644 (file)
@@ -582,6 +582,28 @@ sub mt {
   return $lh->maketext(@_);
 }
 
+=item time2str_local FORMAT, TIME
+
+Localizes a date (see L<Date::Language>) for the customer's locale.
+
+=cut
+
+sub time2str_local {
+  # renamed so that we don't have to change every single reference to 
+  # time2str everywhere
+  my $self = shift;
+  if (!exists($self->{_dh})) {
+    my $cust_main = $self->cust_main;
+    my $locale = $cust_main->locale  if $cust_main;
+    $locale ||= 'en_US';
+    my %info = FS::Locales->locale_info($locale);
+    my $dh = eval { Date::Language->new($info{'name'}) } ||
+             Date::Language->new(); # fall back to English
+    $self->{_dh} = $dh;
+  }
+  $self->{_dh}->time2str(@_);
+}
+
 =back
 
 =head1 BUGS
index c9724d1..c73f857 100644 (file)
@@ -192,6 +192,18 @@ sub cust_bill_pkg_display {
 
 }
 
+=item cust_main
+
+Returns the customer (L<FS::cust_main> object).
+
+=cut
+
+sub cust_main {
+  my $self = shift;
+  my $quotation = FS::quotation->by_key($self->quotationnum) or return '';
+  $quotation->cust_main;
+}
+
 =back
 
 =head1 BUGS