have the UI use full country names, and state names outside the US...
authorivan <ivan>
Mon, 3 Apr 2006 09:46:57 +0000 (09:46 +0000)
committerivan <ivan>
Mon, 3 Apr 2006 09:46:57 +0000 (09:46 +0000)
FS/FS/Misc.pm
htetc/handler.pl
httemplate/edit/cust_main/contact.html
httemplate/edit/cust_main/select-country.html
httemplate/edit/cust_main/select-state.html
httemplate/misc/states.cgi
httemplate/view/cust_main/contacts.html

index 2e383d5..101a2d4 100644 (file)
@@ -7,7 +7,7 @@ use Carp;
 use Data::Dumper;
 
 @ISA = qw( Exporter );
-@EXPORT_OK = qw( send_email send_fax );
+@EXPORT_OK = qw( send_email send_fax states_hash state_label );
 
 $DEBUG = 0;
 
@@ -185,6 +185,80 @@ sub send_email {
 
 }
 
+#this kludges a "mysmtpsend" method into Mail::Internet for send_email above
+package Mail::Internet;
+
+use Mail::Address;
+use Net::SMTP;
+
+sub Mail::Internet::mysmtpsend {
+    my $src  = shift;
+    my %opt = @_;
+    my $host = $opt{Host};
+    my $envelope = $opt{MailFrom};
+    my $noquit = 0;
+    my $smtp;
+    my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
+
+    push(@hello, 'Port', $opt{'Port'})
+       if exists $opt{'Port'};
+
+    push(@hello, 'Debug', $opt{'Debug'})
+       if exists $opt{'Debug'};
+
+    if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
+       $smtp = $host;
+       $noquit = 1;
+    }
+    else {
+       #local $SIG{__DIE__};
+       #$smtp = eval { Net::SMTP->new($host, @hello) };
+       $smtp = new Net::SMTP $host, @hello;
+    }
+
+    unless ( defined($smtp) ) {
+      my $err = $!;
+      $err =~ s/Invalid argument/Unknown host/;
+      return "can't connect to $host: $err"
+    }
+
+    my $hdr = $src->head->dup;
+
+    _prephdr($hdr);
+
+    # Who is it to
+
+    my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
+    @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
+       unless @rcpt;
+    my @addr = map($_->address, Mail::Address->parse(@rcpt));
+
+    return 'No valid destination addresses found!'
+       unless(@addr);
+
+    $hdr->delete('Bcc'); # Remove blind Cc's
+
+    # Send it
+
+    #warn "Headers: \n" . join('',@{$hdr->header});
+    #warn "Body: \n" . join('',@{$src->body});
+
+    my $ok = $smtp->mail( $envelope ) &&
+               $smtp->to(@addr) &&
+               $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
+
+    if ( $ok ) {
+      $smtp->quit
+          unless $noquit;
+      return '';
+    } else {
+      return $smtp->code. ' '. $smtp->message;
+    }
+
+}
+package FS::Misc;
+#eokludge
+
 =item send_fax OPTION => VALUE ...
 
 Options:
@@ -268,77 +342,66 @@ sub send_fax {
 
 }
 
-package Mail::Internet;
-
-use Mail::Address;
-use Net::SMTP;
-
-sub Mail::Internet::mysmtpsend {
-    my $src  = shift;
-    my %opt = @_;
-    my $host = $opt{Host};
-    my $envelope = $opt{MailFrom};
-    my $noquit = 0;
-    my $smtp;
-    my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
-
-    push(@hello, 'Port', $opt{'Port'})
-       if exists $opt{'Port'};
+=item states_hash COUNTRY
 
-    push(@hello, 'Debug', $opt{'Debug'})
-       if exists $opt{'Debug'};
+Returns a list of key/value pairs containing state (or other sub-country
+division) abbriviations and names.
 
-    if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
-       $smtp = $host;
-       $noquit = 1;
-    }
-    else {
-       #local $SIG{__DIE__};
-       #$smtp = eval { Net::SMTP->new($host, @hello) };
-       $smtp = new Net::SMTP $host, @hello;
-    }
-
-    unless ( defined($smtp) ) {
-      my $err = $!;
-      $err =~ s/Invalid argument/Unknown host/;
-      return "can't connect to $host: $err"
-    }
+=cut
 
-    my $hdr = $src->head->dup;
+use FS::Record qw(qsearch);
+use Locale::SubCountry;
+
+sub states_hash {
+  my($country) = @_;
+
+  my @states = 
+#     sort
+     map { s/[\n\r]//g; $_; }
+     map { $_->state; }
+     qsearch( 'cust_main_county',
+              { 'country' => $country },
+              'DISTINCT ON ( state ) *',
+            )
+  ;
+
+  #it could throw a fatal "Invalid country code" error (for example "AX")
+  my $subcountry = eval { new Locale::SubCountry($country) }
+    or return ( '', '(n/a)' );
+
+  #"i see your schwartz is as big as mine!"
+  map  { ( $_->[0] => $_->[1] ) }
+  sort { $a->[1] cmp $b->[1] }
+  map  { [ $_ => state_label($_, $subcountry) ] }
+       @states;
+}
 
-    _prephdr($hdr);
+=item state_label STATE COUNTRY_OR_LOCALE_SUBCOUNRY_OBJECT
 
-    # Who is it to
+=cut
 
-    my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
-    @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
-       unless @rcpt;
-    my @addr = map($_->address, Mail::Address->parse(@rcpt));
+sub state_label {
+  my( $state, $country ) = @_;
 
-    return 'No valid destination addresses found!'
-       unless(@addr);
+  unless ( ref($country) ) {
+    $country = eval { new Locale::SubCountry($country) }
+      or return'(n/a)';
 
-    $hdr->delete('Bcc'); # Remove blind Cc's
-
-    # Send it
+  }
 
-    #warn "Headers: \n" . join('',@{$hdr->header});
-    #warn "Body: \n" . join('',@{$src->body});
+  # US kludge to avoid changing existing behaviour 
+  # also we actually *use* the abbriviations...
+  my $full_name = $country->country_code eq 'US'
+                    ? ''
+                    : $country->full_name($state);
 
-    my $ok = $smtp->mail( $envelope ) &&
-               $smtp->to(@addr) &&
-               $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
+  $full_name = '' if $full_name eq 'unknown';
+  $full_name =~ s/\(see also.*\)\s*$//;
+  $full_name .= " ($state)" if $full_name;
 
-    if ( $ok ) {
-      $smtp->quit
-          unless $noquit;
-      return '';
-    } else {
-      return $smtp->code. ' '. $smtp->message;
-    }
+  $full_name || $state || '(n/a)';
 
 }
-package FS::Misc;
 
 =back
 
index 9b808e6..008b0b5 100644 (file)
@@ -114,6 +114,7 @@ sub handler
       use String::Approx qw(amatch);
       use Chart::LinesPoints;
       use HTML::Widgets::SelectLayers 0.05;
+      use Locale::Country;
       use FS;
       use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
       use FS::Record qw(qsearch qsearchs fields dbdef);
@@ -122,7 +123,7 @@ sub handler
                      eidiot small_custview myexit http_header);
       use FS::UI::Web;
       use FS::Msgcat qw(gettext geterror);
-      use FS::Misc qw( send_email send_fax );
+      use FS::Misc qw( send_email send_fax states_hash state_label );
       use FS::Report::Table::Monthly;
       use FS::TicketSystem;
 
@@ -208,6 +209,7 @@ sub handler
         my( $self, $location ) = @_;
         use vars qw($m);
 
+        # false laziness w/below
         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
 
           my $page =
index e0cd06f..6e4f089 100644 (file)
@@ -96,7 +96,7 @@ my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
 
 <TR>
   <TH ALIGN="right"><%=$r%>Country</TH>
-  <TD><%= include('select-country.html', %select_hash ) %></TD>
+  <TD COLSPAN=5><%= include('select-country.html', %select_hash ) %></TD>
 </TR>
 
 <TR>
index 014effd..3941f2f 100644 (file)
@@ -26,7 +26,7 @@
 
   function <%= $opt{'prefix'} %>country_changed(what, callback) {
 
-    country = what.options[what.selectedIndex].text;
+    country = what.options[what.selectedIndex].value;
 
     function <%= $opt{'prefix'} %>update_states(states) {
 
@@ -36,8 +36,8 @@
 
       // add the new states
       var statesArray = eval('(' + states + ')' );
-      for ( var s = 0; s < statesArray.length; s++ ) {
-          var stateLabel = statesArray[s];
+      for ( var s = 0; s < statesArray.length; s=s+2 ) {
+          var stateLabel = statesArray[s+1];
           if ( stateLabel == "" )
               stateLabel = '(n/a)';
           opt(what.form.<%= $opt{'prefix'} %>state, statesArray[s], stateLabel);
 <SELECT NAME="<%= $opt{'prefix'} %>country" onChange="<%= $opt{'prefix'} %>country_changed(this); <%= $opt{'onchange'} %>" <%= $opt{'disabled'} %>>
 
 <% foreach my $country (
-     sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
+     sort {    ($b eq $countrydefault) <=> ($a eq $countrydefault)
+            or code2country($a) cmp code2country($b) }
      map { $_->country }
      qsearch( 'cust_main_county',{}, 'DISTINCT ON ( country ) *', )
    ) {
 %>
 
-  <OPTION VALUE="<%= $country %>"<%= $country eq $opt{'country'} ? ' SELECTED' : '' %>><%= $country %>
+  <OPTION VALUE="<%= $country %>"<%= $country eq $opt{'country'} ? ' SELECTED' : '' %>><%= code2country($country). " ($country)" %>
 
 <% } %>
 
index 98e685a..64da36b 100644 (file)
@@ -9,17 +9,10 @@
 
 <SELECT NAME="<%= $opt{'prefix'} %>state" onChange="<%= $opt{'prefix'} %>state_changed(this); <%= $opt{'onchange'} %>" <%= $opt{'disabled'} %>>
 
-<% foreach my $state (
-     sort
-     map { $_->state }
-     qsearch( 'cust_main_county',
-              { 'country' => $opt{'country'} },
-              'DISTINCT ON ( state ) *',
-            )
-   ) {
-%>
+<% tie my %states, 'Tie::IxHash', states_hash( $opt{'country'} ); %>
+<% foreach my $state ( keys %states ) { %>
 
-  <OPTION VALUE="<%= $state %>"<%= $state eq $opt{'state'} ? ' SELECTED' : '' %>><%= $state || '(n/a)' %>
+  <OPTION VALUE="<%= $state %>"<%= $state eq $opt{'state'} ? ' SELECTED' : '' %>><%= $states{$state} || '(n/a)' %>
 
 <% } %>
 
index cff2c97..63c494d 100644 (file)
@@ -1,16 +1,6 @@
 <%
 
   my $country = $cgi->param('arg');
+  my @output = states_hash($country);
 
-  my @states = 
-     sort
-     map { s/[\n\r]//g; $_; }
-     map { $_->state; }
-     qsearch( 'cust_main_county',
-              { 'country' => $country },
-              'DISTINCT ON ( state ) *',
-            )
-  ;
-
-
-%>[ <%= join(', ', map { qq("$_") } @states) %> ]
+%>[ <%= join(', ', map { qq("$_") } @output) %> ]
index 89926ea..7aba118 100644 (file)
@@ -33,13 +33,13 @@ Billing address
   <TD ALIGN="right">City</TD>
   <TD BGCOLOR="#ffffff"><%= $cust_main->city %></TD>
   <TD ALIGN="right">State</TD>
-  <TD BGCOLOR="#ffffff"><%= $cust_main->state %></TD>
+  <TD BGCOLOR="#ffffff"><%= state_label($cust_main->state, $cust_main->country) %></TD>
   <TD ALIGN="right">Zip</TD>
   <TD BGCOLOR="#ffffff"><%= $cust_main->zip %></TD>
 </TR>
 <TR>
   <TD ALIGN="right">Country</TD>
-  <TD BGCOLOR="#ffffff"><%= $cust_main->country %></TD>
+  <TD BGCOLOR="#ffffff"><%= code2country($cust_main->country) %></TD>
 </TR>
 <%
   my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/