fix safari weirdness with caching iframes and auto-submitting them (in tax editor...
[freeside.git] / httemplate / browse / cust_main_county.cgi
1 <% include( 'elements/browse.html',
2      'title'          => "Tax Rates $title",
3      'name_singular'  => 'tax rate',
4      'menubar'        => \@menubar,
5      'html_init'      => $html_init,
6      'html_posttotal' => $html_posttotal,
7      'query'          => {
8                            'table'    => 'cust_main_county',
9                            'hashref'  => $hashref,
10                            'order_by' =>
11                              'ORDER BY country, state, county, taxclass',
12                          },
13      'count_query'    => $count_query,
14      'header'         => \@header,
15      'header2'        => \@header2,
16      'fields'         => \@fields,
17      'align'          => $align,
18      'color'          => \@color,
19      'cell_style'     => \@cell_style,
20      'links'          => \@links,
21      'link_onclicks'  => \@link_onclicks,
22   )
23 %>
24 %
25 % #         <FONT SIZE=-1><A HREF="<% $p %>edit/process/cust_main_county-collapse.cgi?<% $hashref->{taxnum} %>">collapse state</A></FONT>
26 % # % } 
27 %
28 <%once>
29
30 my $conf = new FS::Conf;
31 my $money_char = $conf->config('money_char') || '$';
32
33 my @manual_countries = ( 'US', 'CA', 'AU', 'NZ', 'GB' ); #some manual ordering
34 my @all_countries = ( @manual_countries, 
35                       grep { my $c = $_; ! grep { $c eq $_ } @manual_countries }
36                       map { $_->country } 
37                           qsearch({
38                                     'select'    => 'country',
39                                     'table'     => 'cust_main_county',
40                                     'hashref'   => {},
41                                     'extra_sql' => 'GROUP BY country',
42                                  })
43                     );
44
45 my $exempt_sub = sub {
46   my $cust_main_county = shift;
47
48   my @exempt = ();
49   push @exempt,
50        sprintf("$money_char%.2f&nbsp;per&nbsp;month", $cust_main_county->exempt_amount )
51     if $cust_main_county->exempt_amount > 0;
52
53   push @exempt, 'Setup&nbsp;fee'
54     if $cust_main_county->setuptax =~ /^Y$/i;
55
56   push @exempt, 'Recurring&nbsp;fee'
57     if $cust_main_county->recurtax =~ /^Y$/i;
58
59   [ map [ {'data'=>$_} ], @exempt ];
60 };
61
62 my $oldrow;
63 my $cell_style;
64 my $cell_style_sub = sub {
65   my $row = shift;
66   if ( $oldrow ne $row ) {
67     if ( $oldrow ) {
68       if ( $oldrow->country ne $row->country ) {
69         $cell_style = 'border-top:1px solid #000000';
70       } elsif ( $oldrow->state ne $row->state ) {
71         $cell_style = 'border-top:1px solid #cccccc'; #default?
72       } elsif ( $oldrow->state eq $row->state ) {
73         #$cell_style = 'border-top:dashed 1px dark gray';
74         $cell_style = 'border-top:1px dashed #cccccc';
75       }
76     }
77     $oldrow = $row;
78   }
79   return $cell_style;
80 };
81
82 #my $edit_link = [ "${p}edit/cust_main_county.html", 'taxnum' ];
83 my $edit_link = [ 'javascript:void(0);', sub { ''; } ];
84
85 my $edit_onclick = sub {
86   my $row = shift;
87   my $taxnum = $row->taxnum;
88   include( '/elements/popup_link_onclick.html',
89              'action'      => "${p}edit/cust_main_county.html?$taxnum",
90              'actionlabel' => 'Edit tax rate',
91              'height'      => 420,
92              #default# 'width'  => 540,
93              #default# 'color' => '#333399',
94          );
95 };
96
97 sub expand_link {
98   my %param = @_;
99
100   my $taxnum = $param{'row'}->taxnum;
101   my $url = "${p}edit/cust_main_county-expand.cgi?$taxnum";
102
103   '<FONT SIZE="-1">'.
104     include( '/elements/popup_link.html',
105                'label'       => $param{'label'},
106                'action'      => $url,
107                'actionlabel' => $param{'desc'},
108                'height'      => 420,
109                #default# 'width'  => 540,
110                #default# 'color' => '#333399',
111            ).
112   '</FONT>';
113 }
114
115 sub separate_taxclasses_link {
116   my( $row ) = @_;
117   my $taxnum = $row->taxnum;
118   my $url = "${p}edit/process/cust_main_county-expand.cgi?taxclass=1;taxnum=$taxnum";
119
120   qq!<FONT SIZE="-1"><A HREF="$url">!;
121 }
122
123 </%once>
124 <%init>
125
126 die "access denied"
127   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
128
129 #my $conf = new FS::Conf;
130 #my $money_char = $conf->config('money_char') || '$';
131 my $enable_taxclasses = $conf->exists('enable_taxclasses');
132
133 my @menubar;
134
135 my $html_init =
136   "Click on <u>add states</u> to specify a country's tax rates by state or province.
137    <BR>Click on <u>add counties</u> to specify a state's tax rates by county.";
138 $html_init .= "<BR>Click on <u>separate taxclasses</u> to specify taxes per taxclass."
139   if $enable_taxclasses;
140 $html_init .= '<BR><BR>';
141
142 $html_init .= qq(
143   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws.js"></SCRIPT>
144   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_iframe.js"></SCRIPT>
145   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_draggable.js"></SCRIPT>
146   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/iframecontentmws.js"></SCRIPT>
147 );
148
149 my $title = '';
150
151 my $country = '';
152 if ( $cgi->param('country') =~ /^(\w\w)$/ ) {
153   $country = $1;
154   $title = $country;
155 }
156 $cgi->delete('country');
157
158 my $state = '';
159 if ( $cgi->param('state') =~ /^([\w \-\'\[\]]+)$/ ) {
160   $state = $1;
161   $title = "$state, $title";
162 }
163 $cgi->delete('state');
164
165 my $county = '';
166 if ( $cgi->param('county') =~ /^([\w \-\'\[\]]+)$/ ) {
167   $county = $1;
168   $title = "$county county, $title";
169 }
170 $cgi->delete('county');
171
172 $title = " for $title" if $title;
173
174 my $taxclass = '';
175 if ( $cgi->param('taxclass') =~ /^([\w \-]+)$/ ) {
176   $taxclass = $1;
177   $title .= " for $taxclass tax class";
178 }
179 $cgi->delete('taxclass');
180
181 if ( $country || $taxclass ) {
182   push @menubar, 'View all tax rates' => $p.'browse/cust_main_county.cgi';
183 }
184
185 $cgi->param('dummy', 1);
186
187 my $country_filter_change =
188   "window.location = '".
189   $cgi->self_url. ";country=' + this.options[this.selectedIndex].value;";
190
191 #restore this so pagination works
192 $cgi->param('country',  $country) if $country;
193 $cgi->param('state',    $state  ) if $state;
194 $cgi->param('county',   $county ) if $county;
195 $cgi->param('taxclass', $county ) if $taxclass;
196
197 my $html_posttotal =
198   '(show country: '.
199   qq(<SELECT NAME="country" onChange="$country_filter_change">).
200   qq(<OPTION VALUE="">(all)\n).
201   join("\n", map qq[<OPTION VALUE="$_"].
202                    ( $_ eq $country ? 'SELECTED' : '' ).
203                    '>'. code2country($_). " ($_)",
204                  @all_countries
205       ).
206   '</SELECT>)';
207
208 my $hashref = {};
209 my $count_query = 'SELECT COUNT(*) FROM cust_main_county';
210 if ( $country ) {
211   $hashref->{'country'} = $country;
212   $count_query .= ' WHERE country = '. dbh->quote($country);
213 }
214 if ( $state ) {
215   $hashref->{'state'} = $state;
216   $count_query .= '   AND state   = '. dbh->quote($state);
217 }
218 if ( $county ) {
219   $hashref->{'country'} = $country;
220   $count_query .= '   AND county  = '. dbh->quote($county);
221 }
222 if ( $taxclass ) {
223   $hashref->{'taxclass'} = $taxclass;
224   $count_query .= ( $count_query =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
225                   ' taxclass  = '. dbh->quote($taxclass);
226 }
227
228
229 $cell_style = '';
230
231 my @header        = ( 'Country', 'State/Province', 'County',);
232 my @header2       = ( '', '', '', );
233 my @links         = ( '', '', '', );
234 my @link_onclicks = ( '', '', '', );
235 my $align = 'lll';
236
237 my @fields = (
238   sub { my $country = shift->country;
239         code2country($country). " ($country)";
240       },
241   sub { state_label($_[0]->state, $_[0]->country).
242         ( $_[0]->state
243             ? ''
244             : '&nbsp'. expand_link( desc  => 'Add States',
245                                     row   => $_[0],
246                                     label => 'add&nbsp;states',
247                                   )
248         )
249       },
250   sub { $_[0]->county || '(all)&nbsp'.
251                          expand_link( desc  => 'Add Counties',
252                                       row   => $_[0],
253                                       label => 'add&nbsp;counties',
254                                     )
255       },
256 );
257
258 my @color = (
259   '000000',
260   sub { shift->state  ? '000000' : '999999' },
261   sub { shift->county ? '000000' : '999999' },
262 );
263
264 if ( $conf->exists('enable_taxclasses') ) {
265   push @header, qq!Tax class (<A HREF="${p}edit/part_pkg_taxclass.html">add new</A>)!;
266   push @header2, '(per-package classification)';
267   push @fields, sub { $_[0]->taxclass || '(all)&nbsp'.
268                        separate_taxclasses_link($_[0], 'Separate Taxclasses').
269                        'separate&nbsp;taxclasses</A></FONT>'
270                     };
271   push @color, sub { shift->taxclass ? '000000' : '999999' };
272   push @links, '';
273   push @link_onclicks, '';
274   $align .= 'l';
275 }
276
277 push @header, 'Tax name',
278               'Rate', #'Tax',
279               'Exemptions',
280               ;
281
282 push @header2, '(printed on invoices)',
283                '',
284                '',
285                ;
286
287 push @fields, 
288   sub { shift->taxname || 'Tax' },
289   sub { shift->tax. '%&nbsp;<FONT SIZE="-1">(edit)</FONT>' },
290   $exempt_sub,
291 ;
292
293 push @color,
294   sub { shift->taxname ? '000000' : '666666' },
295   sub { shift->tax     ? '000000' : '666666' },
296   '000000',
297 ;
298
299 $align .= 'lrl';
300
301 my @cell_style = map $cell_style_sub, (1..scalar(@header));
302
303 push @links,         '', $edit_link,    '';
304 push @link_onclicks, '', $edit_onclick, '';
305
306 </%init>