update the tax class editor to enable taxclass adding, RT#2929
[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   my $color = '#333399';
89   qq!overlib( OLiframeContent('${p}edit/cust_main_county.html?$taxnum', 540, 420, 'edit_cust_main_county_popup' ), CAPTION, 'Edit tax rate', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK, BGCOLOR, '$color', CGCOLOR, '$color' ); return false;!;
90 };
91
92 sub expand_link {
93   my( $row, $desc ) = @_;
94   my $taxnum = $row->taxnum;
95   my $url = "${p}edit/cust_main_county-expand.cgi?$taxnum";
96   my $color = '#333399';
97
98   qq!<FONT SIZE="-1"><A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('$url', 540, 420, 'edit_cust_main_county_popup' ), CAPTION, '$desc', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK, BGCOLOR, '$color', CGCOLOR, '$color' ); return false;">!;
99 }
100
101 sub separate_taxclasses_link {
102   my( $row ) = @_;
103   my $taxnum = $row->taxnum;
104   my $url = "${p}edit/process/cust_main_county-expand.cgi?taxclass=1;taxnum=$taxnum";
105
106   qq!<FONT SIZE="-1"><A HREF="$url">!;
107 }
108
109 </%once>
110 <%init>
111
112 die "access denied"
113   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
114
115 #my $conf = new FS::Conf;
116 #my $money_char = $conf->config('money_char') || '$';
117 my $enable_taxclasses = $conf->exists('enable_taxclasses');
118
119 my @menubar;
120
121 my $html_init =
122   "Click on <u>add states</u> to specify a country's tax rates by state or province.
123    <BR>Click on <u>add counties</u> to specify a state's tax rates by county.";
124 $html_init .= "<BR>Click on <u>separate taxclasses</u> to specify taxes per taxclass."
125   if $enable_taxclasses;
126 $html_init .= '<BR><BR>';
127
128 $html_init .= qq(
129   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws.js"></SCRIPT>
130   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_iframe.js"></SCRIPT>
131   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_draggable.js"></SCRIPT>
132   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/iframecontentmws.js"></SCRIPT>
133 );
134
135 my $title = '';
136
137 my $country = '';
138 if ( $cgi->param('country') =~ /^(\w\w)$/ ) {
139   $country = $1;
140   $title = $country;
141 }
142 $cgi->delete('country');
143
144 my $state = '';
145 if ( $cgi->param('state') =~ /^([\w \-\'\[\]]+)$/ ) {
146   $state = $1;
147   $title = "$state, $title";
148 }
149 $cgi->delete('state');
150
151 my $county = '';
152 if ( $cgi->param('county') =~ /^([\w \-\'\[\]]+)$/ ) {
153   $county = $1;
154   $title = "$county county, $title";
155 }
156 $cgi->delete('county');
157
158 $title = " for $title" if $title;
159
160 my $taxclass = '';
161 if ( $cgi->param('taxclass') =~ /^([\w \-]+)$/ ) {
162   $taxclass = $1;
163   $title .= " for $taxclass tax class";
164 }
165 $cgi->delete('taxclass');
166
167 if ( $country || $taxclass ) {
168   push @menubar, 'View all tax rates' => $p.'browse/cust_main_county.cgi';
169 }
170
171 $cgi->param('dummy', 1);
172
173 my $country_filter_change =
174   "window.location = '".
175   $cgi->self_url. ";country=' + this.options[this.selectedIndex].value;";
176
177 #restore this so pagination works
178 $cgi->param('country',  $country) if $country;
179 $cgi->param('state',    $state  ) if $state;
180 $cgi->param('county',   $county ) if $county;
181 $cgi->param('taxclass', $county ) if $taxclass;
182
183 my $html_posttotal =
184   '(show country: '.
185   qq(<SELECT NAME="country" onChange="$country_filter_change">).
186   qq(<OPTION VALUE="">(all)\n).
187   join("\n", map qq[<OPTION VALUE="$_"].
188                    ( $_ eq $country ? 'SELECTED' : '' ).
189                    '>'. code2country($_). " ($_)",
190                  @all_countries
191       ).
192   '</SELECT>)';
193
194 my $hashref = {};
195 my $count_query = 'SELECT COUNT(*) FROM cust_main_county';
196 if ( $country ) {
197   $hashref->{'country'} = $country;
198   $count_query .= ' WHERE country = '. dbh->quote($country);
199 }
200 if ( $state ) {
201   $hashref->{'state'} = $state;
202   $count_query .= '   AND state   = '. dbh->quote($state);
203 }
204 if ( $county ) {
205   $hashref->{'country'} = $country;
206   $count_query .= '   AND county  = '. dbh->quote($county);
207 }
208 if ( $taxclass ) {
209   $hashref->{'taxclass'} = $taxclass;
210   $count_query .= ( $count_query =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
211                   ' taxclass  = '. dbh->quote($taxclass);
212 }
213
214
215 $cell_style = '';
216
217 my @header        = ( 'Country', 'State/Province', 'County',);
218 my @header2       = ( '', '', '', );
219 my @links         = ( '', '', '', );
220 my @link_onclicks = ( '', '', '', );
221 my $align = 'lll';
222
223 my @fields = (
224   sub { my $country = shift->country;
225         code2country($country). " ($country)";
226       },
227   sub { state_label($_[0]->state, $_[0]->country).
228         ( $_[0]->state
229             ? ''
230             : '&nbsp'. expand_link($_[0], 'Add States').
231                        'add&nbsp;states</A></FONT>'
232         )
233       },
234   sub { $_[0]->county || '(all)&nbsp'.
235                          expand_link($_[0], 'Add Counties').
236                          'add&nbsp;counties</A></FONT>'
237       },
238 );
239
240 my @color = (
241   '000000',
242   sub { shift->state  ? '000000' : '999999' },
243   sub { shift->county ? '000000' : '999999' },
244 );
245
246 if ( $conf->exists('enable_taxclasses') ) {
247   push @header, qq!Tax class (<A HREF="${p}edit/part_pkg_taxclass.html">add new</A>)!;
248   push @header2, '(per-package classification)';
249   push @fields, sub { $_[0]->taxclass || '(all)&nbsp'.
250                        separate_taxclasses_link($_[0], 'Separate Taxclasses').
251                        'separate&nbsp;taxclasses</A></FONT>'
252                     };
253   push @color, sub { shift->taxclass ? '000000' : '999999' };
254   push @links, '';
255   push @link_onclicks, '';
256   $align .= 'l';
257 }
258
259 push @header, 'Tax name',
260               'Rate', #'Tax',
261               'Exemptions',
262               ;
263
264 push @header2, '(printed on invoices)',
265                '',
266                '',
267                ;
268
269 push @fields, 
270   sub { shift->taxname || 'Tax' },
271   sub { shift->tax. '%&nbsp;<FONT SIZE="-1">(edit)</FONT>' },
272   $exempt_sub,
273 ;
274
275 push @color,
276   sub { shift->taxname ? '000000' : '666666' },
277   sub { shift->tax     ? '000000' : '666666' },
278   '000000',
279 ;
280
281 $align .= 'lrl';
282
283 my @cell_style = map $cell_style_sub, (1..scalar(@header));
284
285 push @links,         '', $edit_link,    '';
286 push @link_onclicks, '', $edit_onclick, '';
287
288 </%init>