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"
74 Creates a new tax rate. To add the tax rate to the database, see L<"insert">.
78 sub table { 'cust_main_county'; }
82 Adds this tax rate to the database. If there is an error, returns the error,
83 otherwise returns false.
87 Deletes this tax rate from the database. If there is an error, returns the
88 error, otherwise returns false.
90 =item replace OLD_RECORD
92 Replaces the OLD_RECORD with this one in the database. If there is an error,
93 returns the error, otherwise returns false.
97 Checks all fields to make sure this is a valid tax rate. If there is an error,
98 returns the error, otherwise returns false. Called by the insert and replace
106 $self->exempt_amount(0) unless $self->exempt_amount;
108 $self->ut_numbern('taxnum')
109 || $self->ut_textn('state')
110 || $self->ut_textn('county')
111 || $self->ut_text('country')
112 || $self->ut_float('tax')
113 || $self->ut_textn('taxclass') # ...
114 || $self->ut_money('exempt_amount')
115 || $self->ut_textn('taxname')
126 =item regionselector [ COUNTY STATE COUNTRY [ PREFIX [ ONCHANGE ] ] ]
131 my ( $selected_county, $selected_state, $selected_country,
132 $prefix, $onchange ) = @_;
134 $prefix = '' unless defined $prefix;
138 # unless ( @cust_main_county ) { #cache
139 @cust_main_county = qsearch('cust_main_county', {} );
140 foreach my $c ( @cust_main_county ) {
141 $countyflag=1 if $c->county;
142 #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
143 $cust_main_county{$c->country}{$c->state}{$c->county} = 1;
146 $countyflag=1 if $selected_county;
148 my $script_html = <<END;
150 function opt(what,value,text) {
151 var optionName = new Option(text, value, false, false);
152 var length = what.length;
153 what.options[length] = optionName;
155 function ${prefix}country_changed(what) {
156 country = what.options[what.selectedIndex].text;
157 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
158 what.form.${prefix}state.options[i] = null;
160 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
162 foreach my $country ( sort keys %cust_main_county ) {
163 $script_html .= "\nif ( country == \"$country\" ) {\n";
164 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
165 my $text = $state || '(n/a)';
166 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
168 $script_html .= "}\n";
171 $script_html .= <<END;
173 function ${prefix}state_changed(what) {
177 $script_html .= <<END;
178 state = what.options[what.selectedIndex].text;
179 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
180 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
181 what.form.${prefix}county.options[i] = null;
184 foreach my $country ( sort keys %cust_main_county ) {
185 $script_html .= "\nif ( country == \"$country\" ) {\n";
186 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
187 $script_html .= "\nif ( state == \"$state\" ) {\n";
188 #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
189 foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
190 my $text = $county || '(n/a)';
192 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
194 $script_html .= "}\n";
196 $script_html .= "}\n";
200 $script_html .= <<END;
205 my $county_html = $script_html;
207 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
208 $county_html .= '</SELECT>';
211 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
214 my $state_html = qq!<SELECT NAME="${prefix}state" !.
215 qq!onChange="${prefix}state_changed(this); $onchange">!;
216 foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
217 my $text = $state || '(n/a)';
218 my $selected = $state eq $selected_state ? 'SELECTED' : '';
219 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
221 $state_html .= '</SELECT>';
223 $state_html .= '</SELECT>';
225 my $country_html = qq!<SELECT NAME="${prefix}country" !.
226 qq!onChange="${prefix}country_changed(this); $onchange">!;
227 my $countrydefault = $conf->config('countrydefault') || 'US';
228 foreach my $country (
229 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
230 keys %cust_main_county
232 my $selected = $country eq $selected_country ? ' SELECTED' : '';
233 $country_html .= "\n<OPTION$selected>$country</OPTION>"
235 $country_html .= '</SELECT>';
237 ($county_html, $state_html, $country_html);
245 regionselector? putting web ui components in here? they should probably live
250 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill>, schema.html from the base