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 taxname - if defined, printed on invoices instead of "Tax"
66 =item setuptax - if 'Y', this tax does not apply to setup fees
68 =item recurtax - if 'Y', this tax does not apply to recurring fees
78 Creates a new tax rate. To add the tax rate to the database, see L<"insert">.
82 sub table { 'cust_main_county'; }
86 Adds this tax rate to the database. If there is an error, returns the error,
87 otherwise returns false.
91 Deletes this tax rate from the database. If there is an error, returns the
92 error, otherwise returns false.
94 =item replace OLD_RECORD
96 Replaces the OLD_RECORD with this one in the database. If there is an error,
97 returns the error, otherwise returns false.
101 Checks all fields to make sure this is a valid tax rate. If there is an error,
102 returns the error, otherwise returns false. Called by the insert and replace
110 $self->exempt_amount(0) unless $self->exempt_amount;
112 $self->ut_numbern('taxnum')
113 || $self->ut_anything('state')
114 || $self->ut_textn('county')
115 || $self->ut_text('country')
116 || $self->ut_float('tax')
117 || $self->ut_textn('taxclass') # ...
118 || $self->ut_money('exempt_amount')
119 || $self->ut_textn('taxname')
120 || $self->ut_enum('setuptax', [ '', 'Y' ] )
121 || $self->ut_enum('recurtax', [ '', 'Y' ] )
122 || $self->SUPER::check
129 if ( $self->dbdef_table->column('taxname') ) {
130 return $self->setfield('taxname', $_[0]) if @_;
131 return $self->getfield('taxname');
138 if ( $self->dbdef_table->column('setuptax') ) {
139 return $self->setfield('setuptax', $_[0]) if @_;
140 return $self->getfield('setuptax');
147 if ( $self->dbdef_table->column('recurtax') ) {
148 return $self->setfield('recurtax', $_[0]) if @_;
149 return $self->getfield('recurtax');
160 =item regionselector [ COUNTY STATE COUNTRY [ PREFIX [ ONCHANGE [ DISABLED ] ] ] ]
165 my ( $selected_county, $selected_state, $selected_country,
166 $prefix, $onchange, $disabled ) = @_;
168 $prefix = '' unless defined $prefix;
172 # unless ( @cust_main_county ) { #cache
173 @cust_main_county = qsearch('cust_main_county', {} );
174 foreach my $c ( @cust_main_county ) {
175 $countyflag=1 if $c->county;
176 #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
177 $cust_main_county{$c->country}{$c->state}{$c->county} = 1;
180 $countyflag=1 if $selected_county;
182 my $script_html = <<END;
184 function opt(what,value,text) {
185 var optionName = new Option(text, value, false, false);
186 var length = what.length;
187 what.options[length] = optionName;
189 function ${prefix}country_changed(what) {
190 country = what.options[what.selectedIndex].text;
191 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
192 what.form.${prefix}state.options[i] = null;
194 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
196 foreach my $country ( sort keys %cust_main_county ) {
197 $script_html .= "\nif ( country == \"$country\" ) {\n";
198 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
199 my $text = $state || '(n/a)';
200 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
202 $script_html .= "}\n";
205 $script_html .= <<END;
207 function ${prefix}state_changed(what) {
211 $script_html .= <<END;
212 state = what.options[what.selectedIndex].text;
213 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
214 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
215 what.form.${prefix}county.options[i] = null;
218 foreach my $country ( sort keys %cust_main_county ) {
219 $script_html .= "\nif ( country == \"$country\" ) {\n";
220 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
221 $script_html .= "\nif ( state == \"$state\" ) {\n";
222 #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
223 foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
224 my $text = $county || '(n/a)';
226 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
228 $script_html .= "}\n";
230 $script_html .= "}\n";
234 $script_html .= <<END;
239 my $county_html = $script_html;
241 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange" $disabled>!;
242 $county_html .= '</SELECT>';
245 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
248 my $state_html = qq!<SELECT NAME="${prefix}state" !.
249 qq!onChange="${prefix}state_changed(this); $onchange" $disabled>!;
250 foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
251 my $text = $state || '(n/a)';
252 my $selected = $state eq $selected_state ? 'SELECTED' : '';
253 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
255 $state_html .= '</SELECT>';
257 $state_html .= '</SELECT>';
259 my $country_html = qq!<SELECT NAME="${prefix}country" !.
260 qq!onChange="${prefix}country_changed(this); $onchange" $disabled>!;
261 my $countrydefault = $conf->config('countrydefault') || 'US';
262 foreach my $country (
263 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
264 keys %cust_main_county
266 my $selected = $country eq $selected_country ? ' SELECTED' : '';
267 $country_html .= "\n<OPTION$selected>$country</OPTION>"
269 $country_html .= '</SELECT>';
271 ($county_html, $state_html, $country_html);
279 regionselector? putting web ui components in here? they should probably live
284 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill>, schema.html from the base