use GROUP BY instead of DISTINCT ON in the state and country queries for better cross...
authorivan <ivan>
Fri, 25 Aug 2006 19:30:52 +0000 (19:30 +0000)
committerivan <ivan>
Fri, 25 Aug 2006 19:30:52 +0000 (19:30 +0000)
FS/FS/Misc.pm
FS/FS/Record.pm
httemplate/edit/cust_main/select-country.html
httemplate/edit/cust_main/select-county.html
httemplate/edit/cust_main/select-state.html

index 101a2d4..06fa51f 100644 (file)
@@ -359,10 +359,12 @@ sub states_hash {
 #     sort
      map { s/[\n\r]//g; $_; }
      map { $_->state; }
-     qsearch( 'cust_main_county',
-              { 'country' => $country },
-              'DISTINCT ON ( state ) *',
-            )
+     qsearch({ 
+               'select'    => 'state',
+               'table'     => 'cust_main_county',
+               'hashref'   => { 'country' => $country },
+               'extra_sql' => 'GROUP BY state',
+            })
   ;
 
   #it could throw a fatal "Invalid country code" error (for example "AX")
index a551bb8..cc7f551 100644 (file)
@@ -388,7 +388,7 @@ sub qsearch {
   my %result;
   tie %result, "Tie::IxHash";
   my @stuff = @{ $sth->fetchall_arrayref( {} ) };
-  if($pkey) {
+  if ( $pkey && $stuff[0]->{$pkey} ) {
     %result = map { $_->{$pkey}, $_ } @stuff;
   } else {
     @result{@stuff} = @stuff;
index 5467f26..137f619 100644 (file)
@@ -1,15 +1,3 @@
-%
-%
-%  my %opt = @_;
-%  foreach my $opt (qw( county state country prefix onchange disabled )) {
-%    $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
-%  }
-%
-%  my $conf = new FS::Conf;
-%  my $countrydefault = $conf->config('countrydefault') || 'US';
-%
-%
-
 
 <% include('/elements/xmlhttp.html',
               'url'  => $p.'misc/states.cgi',
 </SCRIPT>
 
 <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 code2country($a) cmp code2country($b) }
 %     map { $_->country }
-%     qsearch( 'cust_main_county',{}, 'DISTINCT ON ( country ) *', )
-%   ) {
-%
-
+%     qsearch({
+%               'select'    => 'country',
+%               'table'     => 'cust_main_county',
+%               'hashref'   => {},
+%               'extra_sql' => 'GROUP BY country',
+%            })
+% ) {
 
   <OPTION VALUE="<% $country %>"<% $country eq $opt{'country'} ? ' SELECTED' : '' %>><% code2country($country). " ($country)" %>
-% } 
 
+% } 
 
 </SELECT>
 
+<%init>
+my %opt = @_;
+foreach my $opt (qw( county state country prefix onchange disabled )) {
+  $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
+}
+
+my $conf = new FS::Conf;
+my $countrydefault = $conf->config('countrydefault') || 'US';
+</%init>
+
index 70939fc..c9b64bf 100644 (file)
@@ -1,25 +1,10 @@
-%
-%
-%  my %opt = @_;
-%  foreach my $opt (qw( county state country prefix onchange disabled )) {
-%    $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
-%  }
-%
-%  my $sql = "SELECT COUNT(*) FROM cust_main_county".
-%            " WHERE county IS NOT NULL AND county != ''";
-%  my $sth = dbh->prepare($sql) or die dbh->errstr;
-%  $sth->execute or die $sth->errstr;
-%  my $countyflag = $sth->fetchrow_arrayref->[0];
-%
-%
 % if ( $countyflag ) { 
 
-
   <% include('/elements/xmlhttp.html',
                 'url'  => $p.'misc/counties.cgi',
                 'subs' => [ $opt{'prefix'}. 'get_counties' ],
              )
-%>
+  %>
   
   <SCRIPT TYPE="text/javascript">
   
 %                                     'country' => $opt{'country'},
 %                                   }
 %              )
-%     ) {
-%  
-
+% ) {
 
     <OPTION VALUE="<% $county %>"<% $county eq $opt{'county'} ? ' SELECTED' : '' %>><% $county %>
-% } 
 
+% } 
 
   </SELECT>
+
 % } else { 
 
 
   </SCRIPT>
 
   <INPUT TYPE="hidden" NAME="<% $opt{'prefix'} %>county" VALUE="<% $opt{'county'} %>">
+
 % } 
 
+<%init>
+my %opt = @_;
+foreach my $opt (qw( county state country prefix onchange disabled )) {
+  $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
+}
+
+my $sql = "SELECT COUNT(*) FROM cust_main_county".
+          " WHERE county IS NOT NULL AND county != ''";
+my $sth = dbh->prepare($sql) or die dbh->errstr;
+$sth->execute or die $sth->errstr;
+my $countyflag = $sth->fetchrow_arrayref->[0];
+</%init>
index a7e56e7..87546e5 100644 (file)
@@ -1,21 +1,20 @@
-%
-%
-%  my %opt = @_;
-%  foreach my $opt (qw( county state country prefix onchange disabled )) {
-%    $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
-%  }
-%
-%
-
-
 <SELECT NAME="<% $opt{'prefix'} %>state" onChange="<% $opt{'prefix'} %>state_changed(this); <% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
-% tie my %states, 'Tie::IxHash', states_hash( $opt{'country'} ); 
-% foreach my $state ( keys %states ) { 
 
+% foreach my $state ( keys %states ) { 
 
   <OPTION VALUE="<% $state %>"<% $state eq $opt{'state'} ? ' SELECTED' : '' %>><% $states{$state} || '(n/a)' %>
+
 % } 
 
 
 </SELECT>
 
+<%init>
+my %opt = @_;
+foreach my $opt (qw( county state country prefix onchange disabled )) {
+  $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
+}
+
+tie my %states, 'Tie::IxHash', states_hash( $opt{'country'} ); 
+</%init>
+