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
68 Creates a new tax rate. To add the tax rate to the database, see L<"insert">.
72 sub table { 'cust_main_county'; }
76 Adds this tax rate to the database. If there is an error, returns the error,
77 otherwise returns false.
81 Deletes this tax rate from the database. If there is an error, returns the
82 error, otherwise returns false.
84 =item replace OLD_RECORD
86 Replaces the OLD_RECORD with this one in the database. If there is an error,
87 returns the error, otherwise returns false.
91 Checks all fields to make sure this is a valid tax rate. If there is an error,
92 returns the error, otherwise returns false. Called by the insert and replace
100 $self->ut_numbern('taxnum')
101 || $self->ut_textn('state')
102 || $self->ut_textn('county')
103 || $self->ut_text('country')
104 || $self->ut_float('tax')
115 =item regionselector [ COUNTY STATE COUNTRY [ PREFIX [ ONCHANGE ] ] ]
120 my ( $selected_county, $selected_state, $selected_country,
121 $prefix, $onchange ) = @_;
122 $countyflag=1 if $selected_county;
124 unless ( @cust_main_county ) { #cache
125 @cust_main_county = qsearch('cust_main_county', {} );
126 foreach my $c ( @cust_main_county ) {
127 $countyflag=1 if $c->county;
128 push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
132 my $script_html = <<END;
134 function opt(what,value,text) {
135 var optionName = new Option(text, value, false, false);
136 var length = what.length;
137 what.options[length] = optionName;
139 function ${prefix}country_changed(what) {
140 country = what.options[what.selectedIndex].text;
141 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
142 what.form.${prefix}state.options[i] = null;
144 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
146 foreach my $country ( sort keys %cust_main_county ) {
147 $script_html .= "\nif ( country == \"$country\" ) {\n";
148 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
149 my $text = $state || '(n/a)';
150 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
152 $script_html .= "}\n";
155 $script_html .= <<END;
157 function ${prefix}state_changed(what) {
161 $script_html .= <<END;
162 state = what.options[what.selectedIndex].text;
163 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
164 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
165 what.form.${prefix}county.options[i] = null;
168 foreach my $country ( sort keys %cust_main_county ) {
169 $script_html .= "\nif ( country == \"$country\" ) {\n";
170 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
171 $script_html .= "\nif ( state == \"$state\" ) {\n";
172 foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
173 my $text = $county || '(n/a)';
175 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
177 $script_html .= "}\n";
179 $script_html .= "}\n";
183 $script_html .= <<END;
188 my $county_html = $script_html;
190 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
191 $county_html .= '</SELECT>';
194 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
197 my $state_html = qq!<SELECT NAME="${prefix}state" !.
198 qq!onChange="${prefix}state_changed(this); $onchange">!;
199 foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
200 my $text = $state || '(n/a)';
201 my $selected = $state eq $selected_state ? 'SELECTED' : '';
202 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
204 $state_html .= '</SELECT>';
206 $state_html .= '</SELECT>';
208 my $country_html = qq!<SELECT NAME="${prefix}country" !.
209 qq!onChange="${prefix}country_changed(this); $onchange">!;
210 my $countrydefault = $conf->config('countrydefault') || 'US';
211 foreach my $country (
212 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
213 keys %cust_main_county
215 my $selected = $country eq $selected_country ? ' SELECTED' : '';
216 $country_html .= "\n<OPTION$selected>$country</OPTION>"
218 $country_html .= '</SELECT>';
220 ($county_html, $state_html, $country_html);
228 regionseletor? putting web ui components in here? they should probably live
233 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill>, schema.html from the base