Virtual field merge
[freeside.git] / FS / FS / cust_main_county.pm
1 package FS::cust_main_county;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $conf
5              @cust_main_county %cust_main_county $countyflag );
6 use Exporter;
7 use FS::Record qw( qsearch );
8
9 @ISA = qw( FS::Record );
10 @EXPORT_OK = qw( regionselector );
11
12 @cust_main_county = ();
13 $countyflag = '';
14
15 #ask FS::UID to run this stuff for us later
16 $FS::UID::callback{'FS::cust_main_county'} = sub { 
17   $conf = new FS::Conf;
18 };
19
20 =head1 NAME
21
22 FS::cust_main_county - Object methods for cust_main_county objects
23
24 =head1 SYNOPSIS
25
26   use FS::cust_main_county;
27
28   $record = new FS::cust_main_county \%hash;
29   $record = new FS::cust_main_county { 'column' => 'value' };
30
31   $error = $record->insert;
32
33   $error = $new_record->replace($old_record);
34
35   $error = $record->delete;
36
37   $error = $record->check;
38
39   ($county_html, $state_html, $country_html) =
40     FS::cust_main_county::regionselector( $county, $state, $country );
41
42 =head1 DESCRIPTION
43
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
46 currently supported:
47
48 =over 4
49
50 =item taxnum - primary key (assigned automatically for new tax rates)
51
52 =item state
53
54 =item county
55
56 =item country
57
58 =item tax - percentage
59
60 =item taxclass
61
62 =item exempt_amount
63
64 =item taxname - if defined, printed on invoices instead of "Tax"
65
66 =back
67
68 =head1 METHODS
69
70 =over 4
71
72 =item new HASHREF
73
74 Creates a new tax rate.  To add the tax rate to the database, see L<"insert">.
75
76 =cut
77
78 sub table { 'cust_main_county'; }
79
80 =item insert
81
82 Adds this tax rate to the database.  If there is an error, returns the error,
83 otherwise returns false.
84
85 =item delete
86
87 Deletes this tax rate from the database.  If there is an error, returns the
88 error, otherwise returns false.
89
90 =item replace OLD_RECORD
91
92 Replaces the OLD_RECORD with this one in the database.  If there is an error,
93 returns the error, otherwise returns false.
94
95 =item check
96
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
99 methods.
100
101 =cut
102
103 sub check {
104   my $self = shift;
105
106   $self->exempt_amount(0) unless $self->exempt_amount;
107
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')
116     || $self->SUPER::check
117     ;
118
119
120 }
121
122 =back
123
124 =head1 SUBROUTINES
125
126 =over 4
127
128 =item regionselector [ COUNTY STATE COUNTRY [ PREFIX [ ONCHANGE ] ] ]
129
130 =cut
131
132 sub regionselector {
133   my ( $selected_county, $selected_state, $selected_country,
134        $prefix, $onchange ) = @_;
135
136   $prefix = '' unless defined $prefix;
137
138   $countyflag = 0;
139
140 #  unless ( @cust_main_county ) { #cache 
141     @cust_main_county = qsearch('cust_main_county', {} );
142     foreach my $c ( @cust_main_county ) {
143       $countyflag=1 if $c->county;
144       #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
145       $cust_main_county{$c->country}{$c->state}{$c->county} = 1;
146     }
147 #  }
148   $countyflag=1 if $selected_county;
149
150   my $script_html = <<END;
151     <SCRIPT>
152     function opt(what,value,text) {
153       var optionName = new Option(text, value, false, false);
154       var length = what.length;
155       what.options[length] = optionName;
156     }
157     function ${prefix}country_changed(what) {
158       country = what.options[what.selectedIndex].text;
159       for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
160           what.form.${prefix}state.options[i] = null;
161 END
162       #what.form.${prefix}state.options[0] = new Option('', '', false, true);
163
164   foreach my $country ( sort keys %cust_main_county ) {
165     $script_html .= "\nif ( country == \"$country\" ) {\n";
166     foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
167       my $text = $state || '(n/a)';
168       $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
169     }
170     $script_html .= "}\n";
171   }
172
173   $script_html .= <<END;
174     }
175     function ${prefix}state_changed(what) {
176 END
177
178   if ( $countyflag ) {
179     $script_html .= <<END;
180       state = what.options[what.selectedIndex].text;
181       country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
182       for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
183           what.form.${prefix}county.options[i] = null;
184 END
185
186     foreach my $country ( sort keys %cust_main_county ) {
187       $script_html .= "\nif ( country == \"$country\" ) {\n";
188       foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
189         $script_html .= "\nif ( state == \"$state\" ) {\n";
190           #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
191           foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
192             my $text = $county || '(n/a)';
193             $script_html .=
194               qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
195           }
196         $script_html .= "}\n";
197       }
198       $script_html .= "}\n";
199     }
200   }
201
202   $script_html .= <<END;
203     }
204     </SCRIPT>
205 END
206
207   my $county_html = $script_html;
208   if ( $countyflag ) {
209     $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
210     $county_html .= '</SELECT>';
211   } else {
212     $county_html .=
213       qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
214   }
215
216   my $state_html = qq!<SELECT NAME="${prefix}state" !.
217                    qq!onChange="${prefix}state_changed(this); $onchange">!;
218   foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
219     my $text = $state || '(n/a)';
220     my $selected = $state eq $selected_state ? 'SELECTED' : '';
221     $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
222   }
223   $state_html .= '</SELECT>';
224
225   $state_html .= '</SELECT>';
226
227   my $country_html = qq!<SELECT NAME="${prefix}country" !.
228                      qq!onChange="${prefix}country_changed(this); $onchange">!;
229   my $countrydefault = $conf->config('countrydefault') || 'US';
230   foreach my $country (
231     sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
232       keys %cust_main_county
233   ) {
234     my $selected = $country eq $selected_country ? ' SELECTED' : '';
235     $country_html .= "\n<OPTION$selected>$country</OPTION>"
236   }
237   $country_html .= '</SELECT>';
238
239   ($county_html, $state_html, $country_html);
240
241 }
242
243 =back
244
245 =head1 BUGS
246
247 regionselector?  putting web ui components in here?  they should probably live
248 somewhere else...
249
250 =head1 SEE ALSO
251
252 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill>, schema.html from the base
253 documentation.
254
255 =cut
256
257 1;
258