get 2884 backport in the loop
[freeside.git] / FS / FS / cust_main_county.pm
index 76c982a..9df4b55 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use vars qw( @ISA @EXPORT_OK $conf
              @cust_main_county %cust_main_county $countyflag );
 use Exporter;
-use FS::Record qw( qsearch );
+use FS::Record qw( qsearch dbh );
 
 @ISA = qw( FS::Record );
 @EXPORT_OK = qw( regionselector );
@@ -110,7 +110,7 @@ sub check {
   $self->exempt_amount(0) unless $self->exempt_amount;
 
   $self->ut_numbern('taxnum')
-    || $self->ut_textn('state')
+    || $self->ut_anything('state')
     || $self->ut_textn('county')
     || $self->ut_text('country')
     || $self->ut_float('tax')
@@ -151,19 +151,60 @@ sub recurtax {
   return '';
 }
 
+=item sql_taxclass_sameregion
+
+Returns an SQL WHERE fragment or the empty string to search for entries
+with different tax classes.
+
+=cut
+
+#hmm, description above could be better...
+
+sub sql_taxclass_sameregion {
+  my $self = shift;
+
+  my $same_query = 'SELECT taxclass FROM cust_main_county '.
+                   ' WHERE taxnum != ? AND country = ?';
+  my @same_param = ( 'taxnum', 'country' );
+  foreach my $opt_field (qw( state county )) {
+    if ( $self->$opt_field() ) {
+      $same_query .= " AND $opt_field = ?";
+      push @same_param, $opt_field;
+    } else {
+      $same_query .= " AND $opt_field IS NULL";
+    }
+  }
+
+  my @taxclasses = $self->_list_sql( \@same_param, $same_query );
+
+  return '' unless scalar(@taxclasses);
+
+  '( taxclass IS NULL OR ( '.  #only if !$self->taxclass ??
+     join(' AND ', map { 'taxclass != '.dbh->quote($_) } @taxclasses ). 
+  ' ) ) ';
+}
+
+sub _list_sql {
+  my( $self, $param, $sql ) = @_;
+  my $sth = dbh->prepare($sql) or die dbh->errstr;
+  $sth->execute( map $self->$_(), @$param )
+    or die "Unexpected error executing statement $sql: ". $sth->errstr;
+  map $_->[0], @{ $sth->fetchall_arrayref };
+}
+
 =back
 
 =head1 SUBROUTINES
 
 =over 4
 
-=item regionselector [ COUNTY STATE COUNTRY [ PREFIX [ ONCHANGE ] ] ]
+=item regionselector [ COUNTY STATE COUNTRY [ PREFIX [ ONCHANGE [ DISABLED ] ] ] ]
 
 =cut
 
 sub regionselector {
   my ( $selected_county, $selected_state, $selected_country,
-       $prefix, $onchange ) = @_;
+       $prefix, $onchange, $disabled ) = @_;
 
   $prefix = '' unless defined $prefix;
 
@@ -196,8 +237,9 @@ END
   foreach my $country ( sort keys %cust_main_county ) {
     $script_html .= "\nif ( country == \"$country\" ) {\n";
     foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
-      my $text = $state || '(n/a)';
-      $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
+      ( my $dstate = $state ) =~ s/[\n\r]//g;
+      my $text = $dstate || '(n/a)';
+      $script_html .= qq!opt(what.form.${prefix}state, "$dstate", "$text");\n!;
     }
     $script_html .= "}\n";
   }
@@ -238,7 +280,7 @@ END
 
   my $county_html = $script_html;
   if ( $countyflag ) {
-    $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
+    $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange" $disabled>!;
     $county_html .= '</SELECT>';
   } else {
     $county_html .=
@@ -246,25 +288,25 @@ END
   }
 
   my $state_html = qq!<SELECT NAME="${prefix}state" !.
-                   qq!onChange="${prefix}state_changed(this); $onchange">!;
+                   qq!onChange="${prefix}state_changed(this); $onchange" $disabled>!;
   foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
     my $text = $state || '(n/a)';
     my $selected = $state eq $selected_state ? 'SELECTED' : '';
-    $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
+    $state_html .= qq(\n<OPTION $selected VALUE="$state">$text</OPTION>);
   }
   $state_html .= '</SELECT>';
 
   $state_html .= '</SELECT>';
 
   my $country_html = qq!<SELECT NAME="${prefix}country" !.
-                     qq!onChange="${prefix}country_changed(this); $onchange">!;
+                     qq!onChange="${prefix}country_changed(this); $onchange" $disabled>!;
   my $countrydefault = $conf->config('countrydefault') || 'US';
   foreach my $country (
     sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
       keys %cust_main_county
   ) {
     my $selected = $country eq $selected_country ? ' SELECTED' : '';
-    $country_html .= "\n<OPTION$selected>$country</OPTION>"
+    $country_html .= qq(\n<OPTION$selected VALUE="$country">$country</OPTION>");
   }
   $country_html .= '</SELECT>';