X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main_county.pm;h=9df4b552cb34edb21fd39dd3d5775d738c93461a;hb=6c7b2706ba9297e7b8f9774b2f0f98c524c6354b;hp=d8796e4518ef350fe9d35b93c331a54e76257cd9;hpb=8965012fa53fd05d851d2f5abed4e056ab758797;p=freeside.git diff --git a/FS/FS/cust_main_county.pm b/FS/FS/cust_main_county.pm index d8796e451..9df4b552c 100644 --- a/FS/FS/cust_main_county.pm +++ b/FS/FS/cust_main_county.pm @@ -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 ); @@ -63,6 +63,10 @@ currently supported: =item taxname - if defined, printed on invoices instead of "Tax" +=item setuptax - if 'Y', this tax does not apply to setup fees + +=item recurtax - if 'Y', this tax does not apply to recurring fees + =back =head1 METHODS @@ -106,15 +110,86 @@ 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') || $self->ut_textn('taxclass') # ... || $self->ut_money('exempt_amount') || $self->ut_textn('taxname') - ; + || $self->ut_enum('setuptax', [ '', 'Y' ] ) + || $self->ut_enum('recurtax', [ '', 'Y' ] ) + || $self->SUPER::check + ; + +} + +sub taxname { + my $self = shift; + if ( $self->dbdef_table->column('taxname') ) { + return $self->setfield('taxname', $_[0]) if @_; + return $self->getfield('taxname'); + } + return ''; +} + +sub setuptax { + my $self = shift; + if ( $self->dbdef_table->column('setuptax') ) { + return $self->setfield('setuptax', $_[0]) if @_; + return $self->getfield('setuptax'); + } + return ''; +} + +sub recurtax { + my $self = shift; + if ( $self->dbdef_table->column('recurtax') ) { + return $self->setfield('recurtax', $_[0]) if @_; + return $self->getfield('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 @@ -123,13 +198,13 @@ sub check { =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; @@ -162,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"; } @@ -204,7 +280,7 @@ END my $county_html = $script_html; if ( $countyflag ) { - $county_html .= qq!!; $county_html .= ''; } else { $county_html .= @@ -212,25 +288,25 @@ END } my $state_html = qq!'; $state_html .= ''; my $country_html = qq!';