1 package FS::cust_main_county;
4 use vars qw( @ISA @EXPORT_OK $conf
5 @cust_main_county %cust_main_county $countyflag );
7 use FS::Record qw( qsearch );
9 @ISA = qw( FS::Record );
10 @EXPORT_OK = qw( regionselector );
12 @cust_main_county = ();
15 #ask FS::UID to run this stuff for us later
16 $FS::UID::callback{'FS::cust_main_county'} = sub {
22 FS::cust_main_county - Object methods for cust_main_county objects
26 use FS::cust_main_county;
28 $record = new FS::cust_main_county \%hash;
29 $record = new FS::cust_main_county { 'column' => 'value' };
31 $error = $record->insert;
33 $error = $new_record->replace($old_record);
35 $error = $record->delete;
37 $error = $record->check;
39 ($county_html, $state_html, $country_html) =
40 FS::cust_main_county::regionselector( $county, $state, $country );
44 An FS::cust_main_county object represents a tax rate, defined by locale.
45 FS::cust_main_county inherits from FS::Record. The following fields are
50 =item taxnum - primary key (assigned automatically for new tax rates)
58 =item tax - percentage
64 =item setuptax - if 'Y', this tax does not apply to setup fees
66 =item recurtax - if 'Y', this tax does not apply to recurring fees
76 Creates a new tax rate. To add the tax rate to the database, see L<"insert">.
80 sub table { 'cust_main_county'; }
84 Adds this tax rate to the database. If there is an error, returns the error,
85 otherwise returns false.
89 Deletes this tax rate from the database. If there is an error, returns the
90 error, otherwise returns false.
92 =item replace OLD_RECORD
94 Replaces the OLD_RECORD with this one in the database. If there is an error,
95 returns the error, otherwise returns false.
99 Checks all fields to make sure this is a valid tax rate. If there is an error,
100 returns the error, otherwise returns false. Called by the insert and replace
108 $self->exempt_amount(0) unless $self->exempt_amount;
110 $self->ut_numbern('taxnum')
111 || $self->ut_anything('state')
112 || $self->ut_textn('county')
113 || $self->ut_text('country')
114 || $self->ut_float('tax')
115 || $self->ut_textn('taxclass') # ...
116 || $self->ut_money('exempt_amount')
117 || $self->ut_textn('taxname')
118 || $self->ut_enum('setuptax', [ '', 'Y' ] )
119 || $self->ut_enum('recurtax', [ '', 'Y' ] )
126 if ( $self->dbdef_table->column('taxname') ) {
127 return $self->setfield('taxname', $_[0]) if @_;
128 return $self->getfield('taxname');
135 if ( $self->dbdef_table->column('setuptax') ) {
136 return $self->setfield('setuptax', $_[0]) if @_;
137 return $self->getfield('setuptax');
144 if ( $self->dbdef_table->column('recurtax') ) {
145 return $self->setfield('recurtax', $_[0]) if @_;
146 return $self->getfield('recurtax');
157 =item regionselector [ COUNTY STATE COUNTRY [ PREFIX [ ONCHANGE ] ] ]
162 my ( $selected_county, $selected_state, $selected_country,
163 $prefix, $onchange ) = @_;
165 $prefix = '' unless defined $prefix;
169 # unless ( @cust_main_county ) { #cache
170 @cust_main_county = qsearch('cust_main_county', {} );
171 foreach my $c ( @cust_main_county ) {
172 $countyflag=1 if $c->county;
173 #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
174 $cust_main_county{$c->country}{$c->state}{$c->county} = 1;
177 $countyflag=1 if $selected_county;
179 my $script_html = <<END;
181 function opt(what,value,text) {
182 var optionName = new Option(text, value, false, false);
183 var length = what.length;
184 what.options[length] = optionName;
186 function ${prefix}country_changed(what) {
187 country = what.options[what.selectedIndex].text;
188 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
189 what.form.${prefix}state.options[i] = null;
191 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
193 foreach my $country ( sort keys %cust_main_county ) {
194 $script_html .= "\nif ( country == \"$country\" ) {\n";
195 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
196 my $text = $state || '(n/a)';
197 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
199 $script_html .= "}\n";
202 $script_html .= <<END;
204 function ${prefix}state_changed(what) {
208 $script_html .= <<END;
209 state = what.options[what.selectedIndex].text;
210 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
211 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
212 what.form.${prefix}county.options[i] = null;
215 foreach my $country ( sort keys %cust_main_county ) {
216 $script_html .= "\nif ( country == \"$country\" ) {\n";
217 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
218 $script_html .= "\nif ( state == \"$state\" ) {\n";
219 #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
220 foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
221 my $text = $county || '(n/a)';
223 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
225 $script_html .= "}\n";
227 $script_html .= "}\n";
231 $script_html .= <<END;
236 my $county_html = $script_html;
238 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
239 $county_html .= '</SELECT>';
242 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
245 my $state_html = qq!<SELECT NAME="${prefix}state" !.
246 qq!onChange="${prefix}state_changed(this); $onchange">!;
247 foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
248 my $text = $state || '(n/a)';
249 my $selected = $state eq $selected_state ? 'SELECTED' : '';
250 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
252 $state_html .= '</SELECT>';
254 $state_html .= '</SELECT>';
256 my $country_html = qq!<SELECT NAME="${prefix}country" !.
257 qq!onChange="${prefix}country_changed(this); $onchange">!;
258 my $countrydefault = $conf->config('countrydefault') || 'US';
259 foreach my $country (
260 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
261 keys %cust_main_county
263 my $selected = $country eq $selected_country ? ' SELECTED' : '';
264 $country_html .= "\n<OPTION$selected>$country</OPTION>"
266 $country_html .= '</SELECT>';
268 ($county_html, $state_html, $country_html);
276 regionselector? putting web ui components in here? they should probably live
281 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill>, schema.html from the base