add back remove ("collapse") links again. on each line this time. RT#2973
[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      'html_form'      => '<FORM NAME="taxesForm">',
8      'html_foot'      => $html_foot,
9      'query'          => {
10                            'table'    => 'cust_main_county',
11                            'hashref'  => $hashref,
12                            'order_by' =>
13                              'ORDER BY country, state, county, taxclass',
14                          },
15      'count_query'    => $count_query,
16      'header'         => \@header,
17      'header2'        => \@header2,
18      'fields'         => \@fields,
19      'align'          => $align,
20      'color'          => \@color,
21      'cell_style'     => \@cell_style,
22      'links'          => \@links,
23      'link_onclicks'  => \@link_onclicks,
24   )
25 %>
26 <%once>
27
28 my $conf = new FS::Conf;
29 my $money_char = $conf->config('money_char') || '$';
30
31 my $exempt_sub = sub {
32   my $cust_main_county = shift;
33
34   my @exempt = ();
35   push @exempt,
36        sprintf("$money_char%.2f&nbsp;per&nbsp;month", $cust_main_county->exempt_amount )
37     if $cust_main_county->exempt_amount > 0;
38
39   push @exempt, 'Setup&nbsp;fee'
40     if $cust_main_county->setuptax =~ /^Y$/i;
41
42   push @exempt, 'Recurring&nbsp;fee'
43     if $cust_main_county->recurtax =~ /^Y$/i;
44
45   [ map [ {'data'=>$_} ], @exempt ];
46 };
47
48 my $oldrow;
49 my $cell_style;
50 my $cell_style_sub = sub {
51   my $row = shift;
52   if ( $oldrow ne $row ) {
53     if ( $oldrow ) {
54       if ( $oldrow->country ne $row->country ) {
55         $cell_style = 'border-top:1px solid #000000';
56       } elsif ( $oldrow->state ne $row->state ) {
57         $cell_style = 'border-top:1px solid #cccccc'; #default?
58       } elsif ( $oldrow->state eq $row->state ) {
59         #$cell_style = 'border-top:dashed 1px dark gray';
60         $cell_style = 'border-top:1px dashed #cccccc';
61       }
62     }
63     $oldrow = $row;
64   }
65   return $cell_style;
66 };
67
68 #my $edit_link = [ "${p}edit/cust_main_county.html", 'taxnum' ];
69 my $edit_link = [ 'javascript:void(0);', sub { ''; } ];
70
71 my $edit_onclick = sub {
72   my $row = shift;
73   my $taxnum = $row->taxnum;
74   include( '/elements/popup_link_onclick.html',
75              'action'      => "${p}edit/cust_main_county.html?$taxnum",
76              'actionlabel' => 'Edit tax rate',
77              'height'      => 420,
78              #default# 'width'  => 540,
79              #default# 'color' => '#333399',
80          );
81 };
82
83 sub expand_link {
84   my %param = @_;
85
86   my $taxnum = $param{'row'}->taxnum;
87   my $url = "${p}edit/cust_main_county-expand.cgi?$taxnum";
88
89   '<FONT SIZE="-1">'.
90     include( '/elements/popup_link.html',
91                'label'       => $param{'label'},
92                'action'      => $url,
93                'actionlabel' => $param{'desc'},
94                'height'      => 420,
95                #default# 'width'  => 540,
96                #default# 'color' => '#333399',
97            ).
98   '</FONT>';
99 }
100
101 sub collapse_link {
102   my %param = @_;
103
104   my $taxnum = $param{'row'}->taxnum;
105   my $url = "${p}edit/process/cust_main_county-collapse.cgi?$taxnum";
106   $url = "javascript:collapse_areyousure('$url')";
107
108   qq(<FONT SIZE="-1"><A HREF="$url">$param{'label'}</A></FONT>);
109 }
110
111
112 sub separate_taxclasses_link {
113   my( $row ) = @_;
114   my $taxnum = $row->taxnum;
115   my $url = "${p}edit/process/cust_main_county-expand.cgi?taxclass=1;taxnum=$taxnum";
116
117   qq!<FONT SIZE="-1"><A HREF="$url">!;
118 }
119
120 #un-separate taxclasses too
121
122 </%once>
123 <%init>
124
125 die "access denied"
126   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
127
128 #my $conf = new FS::Conf;
129 #my $money_char = $conf->config('money_char') || '$';
130 my $enable_taxclasses = $conf->exists('enable_taxclasses');
131
132 my @menubar;
133
134 my $html_init = <<END;
135   <SCRIPT>
136     function collapse_areyousure(href) {
137      if (confirm("Are you sure you want to remove all county tax rates for this state?") == true)
138        window.location.href = href;
139     }
140   </SCRIPT>
141
142   Click on <u>add states</u> to specify a country's tax rates by state or province.
143   <BR>Click on <u>add counties</u> to specify a state's tax rates by county, or <u>remove counties</u> to remove per-county tax rates.
144 END
145
146 $html_init .= "<BR>Click on <u>separate taxclasses</u> to specify taxes per taxclass."
147   if $enable_taxclasses;
148 $html_init .= '<BR><BR>';
149
150 $html_init .= include('/elements/init_overlib.html');
151
152 my $title = '';
153
154 my $country = '';
155 if ( $cgi->param('country') =~ /^(\w\w)$/ ) {
156   $country = $1;
157   $title = $country;
158 }
159 $cgi->delete('country');
160
161 my $state = '';
162 if ( $country && $cgi->param('state') =~ /^([\w \-\'\[\]]+)$/ ) {
163   $state = $1;
164   $title = "$state, $title";
165 }
166 $cgi->delete('state');
167
168 my $county = '';
169 if ( $country && $state &&
170      $cgi->param('county') =~
171        /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)$/
172    )
173 {
174   $county = $1;
175   if ( $county eq '__NONE__' ) {
176     $title = "No county, $title";
177   } else {
178     $title = "$county county, $title";
179   }
180 }
181 $cgi->delete('county');
182
183 $title = " for $title" if $title;
184
185 my $taxclass = '';
186 if ( $cgi->param('taxclass') =~ /^([\w \-]+)$/ ) {
187   $taxclass = $1;
188   $title .= " for $taxclass tax class";
189 }
190 $cgi->delete('taxclass');
191
192 if ( $country || $taxclass ) {
193   push @menubar, 'View all tax rates' => $p.'browse/cust_main_county.cgi';
194 }
195
196 $cgi->param('dummy', 1);
197
198 my $filter_change =
199   "window.location = '". $cgi->self_url.
200   ";country=' + encodeURIComponent( document.getElementById('country').options[document.getElementById('country').selectedIndex].value ) + ".
201   "';state='   + encodeURIComponent( document.getElementById('state').options[document.getElementById('state').selectedIndex].value ) +".
202   "';county='  + encodeURIComponent( document.getElementById('county').options[document.getElementById('county').selectedIndex].value );";
203
204 #restore this so pagination works
205 $cgi->param('country',  $country) if $country;
206 $cgi->param('state',    $state  ) if $state;
207 $cgi->param('county',   $county ) if $county;
208 $cgi->param('taxclass', $county ) if $taxclass;
209
210 my $html_posttotal =
211   '<BR>( show country: '.
212   include('/elements/select-country.html',
213             'country'             => $country,
214             'onchange'            => $filter_change,
215             'empty_label'         => '(all)',
216             'disable_empty'       => 0,
217             'disable_stateupdate' => 1,
218          );
219
220 my %states_hash = $country ? states_hash($country) : ();
221 if ( scalar(keys(%states_hash)) > 1 ) {
222   $html_posttotal .=
223     ' show state: '.
224     include('/elements/select-state.html',
225               'country'              => $country,
226               'state'                => $state,
227               'onchange'             => $filter_change,
228               'empty_label'          => '(all)',
229               'disable_empty'        => 0,
230               'disable_countyupdate' => 1,
231            );
232 } else {
233   $html_posttotal .=
234     '<SELECT NAME="state" ID="state" STYLE="display:none">'.
235     '  <OPTION VALUE="" SELECTED>'.
236     '</SELECT>';
237 }
238
239 my @counties = ( $country && $state ) ? counties($state, $country) : ();
240 if ( scalar(@counties) > 1 ) {
241   $html_posttotal .=
242     ' show county: '.
243     include('/elements/select-county.html',
244               'country'              => $country,
245               'state'                => $state,
246               'county'               => $county,
247               'onchange'             => $filter_change,
248               'empty_label'          => '(all)',
249               'empty_data_label'     => '(none)',
250               'empty_data_value'     => '__NONE__',
251               'disable_empty'        => 0,
252               'disable_countyupdate' => 1,
253            );
254 } else {
255   $html_posttotal .=
256     '<SELECT NAME="county" ID="county" STYLE="display:none">'.
257     '  <OPTION VALUE="" SELECTED>'.
258     '</SELECT>';
259 }
260
261 $html_posttotal .= ' )';
262
263 my $bulk_popup_link = 
264   include( '/elements/popup_link_onclick.html',
265              'action'      => "${p}edit/bulk-cust_main_county.html?MAGIC_taxnum_MAGIC",
266              'actionlabel' => 'Bulk add new tax',
267              'nofalse'     => 1,
268              'height'      => 420,
269              #default# 'width'  => 540,
270              #default# 'color' => '#333399',
271          );
272
273 my $html_foot = <<END;
274 <SCRIPT TYPE="text/javascript">
275
276   function setAll(setTo) {
277     theForm = document.taxesForm;
278     for (i=0,n=theForm.elements.length;i<n;i++) {
279       if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
280         theForm.elements[i].checked = setTo;
281       }
282     }
283   }
284
285   function toggleAll() {
286     theForm = document.taxesForm;
287     for (i=0,n=theForm.elements.length;i<n;i++) {
288       if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
289         if ( theForm.elements[i].checked == true ) {
290           theForm.elements[i].checked = false;
291         } else {
292           theForm.elements[i].checked = true;
293         }
294       }
295     }
296   }
297
298   function bulkPopup() {
299     var bulk_popup_link = "$bulk_popup_link";
300     var bulkstring = '';
301     theForm = document.taxesForm;
302     for (i=0,n=theForm.elements.length;i<n;i++) {
303       if (    theForm.elements[i].name.indexOf("cust_main_county") != -1
304            && theForm.elements[i].checked == true
305          ) {
306         var name = theForm.elements[i].name;
307         var taxnum = name.replace(/cust_main_county/, '');
308         if ( bulkstring != '' ) {
309           bulkstring = bulkstring + ',';
310         }
311         bulkstring = bulkstring + taxnum;
312        
313       }
314     }
315     if ( bulk_popup_link.length > 1920 ) { // IE 2083 URL limit
316       alert('Too many selections'); // should do some session thing...
317       return false;
318     }
319     bulk_popup_link = bulk_popup_link.replace(/MAGIC_taxnum_MAGIC/, bulkstring);
320     eval(bulk_popup_link);
321   }
322
323 </SCRIPT>
324
325 <BR>
326 <A HREF="javascript:setAll(true)">select all</A> |
327 <A HREF="javascript:setAll(false)">unselect all</A> |
328 <A HREF="javascript:toggleAll()">toggle all</A>
329 <BR><BR>
330 <A HREF="javascript:void(0);" onClick="bulkPopup();">Add new tax to selected</A>
331
332 END
333
334 my $hashref = {};
335 my $count_query = 'SELECT COUNT(*) FROM cust_main_county';
336 if ( $country ) {
337   $hashref->{'country'} = $country;
338   $count_query .= ' WHERE country = '. dbh->quote($country);
339 }
340 if ( $state ) {
341   $hashref->{'state'} = $state;
342   $count_query .= ' AND state   = '. dbh->quote($state);
343 }
344 if ( $county ) {
345   if ( $county eq '__NONE__' ) {
346     $hashref->{'county'} = '';
347     $count_query .= " AND ( county = '' OR county IS NULL ) ";
348   } else {
349     $hashref->{'county'} = $county;
350     $count_query .= ' AND county  = '. dbh->quote($county);
351   }
352 }
353 if ( $taxclass ) {
354   $hashref->{'taxclass'} = $taxclass;
355   $count_query .= ( $count_query =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
356                   ' taxclass  = '. dbh->quote($taxclass);
357 }
358
359
360 $cell_style = '';
361
362 my @header        = ( 'Country', 'State/Province', 'County',);
363 my @header2       = ( '', '', '', );
364 my @links         = ( '', '', '', );
365 my @link_onclicks = ( '', '', '', );
366 my $align = 'lll';
367
368 my @fields = (
369   sub { my $country = shift->country;
370         code2country($country). " ($country)";
371       },
372   sub { state_label($_[0]->state, $_[0]->country).
373         ( $_[0]->state
374             ? ''
375             : '&nbsp'. expand_link( desc  => 'Add States',
376                                     row   => $_[0],
377                                     label => 'add&nbsp;states',
378                                   )
379         )
380       },
381   sub { $_[0]->county
382           ? $_[0]->county. '&nbsp'.
383               collapse_link( label=> 'remove&nbsp;counties',
384                              row  => $_[0],
385                            )
386           : '(all)&nbsp'.
387               expand_link(   desc  => 'Add Counties',
388                              row   => $_[0],
389                              label => 'add&nbsp;counties',
390                          );
391       },
392 );
393
394 my @color = (
395   '000000',
396   sub { shift->state  ? '000000' : '999999' },
397   sub { shift->county ? '000000' : '999999' },
398 );
399
400 if ( $conf->exists('enable_taxclasses') ) {
401   push @header, qq!Tax class (<A HREF="${p}edit/part_pkg_taxclass.html">add new</A>)!;
402   push @header2, '(per-package classification)';
403   push @fields, sub { $_[0]->taxclass || '(all)&nbsp'.
404                        separate_taxclasses_link($_[0], 'Separate Taxclasses').
405                        'separate&nbsp;taxclasses</A></FONT>'
406                     };
407   push @color, sub { shift->taxclass ? '000000' : '999999' };
408   push @links, '';
409   push @link_onclicks, '';
410   $align .= 'l';
411 }
412
413 push @header,
414               '', #checkbox column
415               'Tax name',
416               'Rate', #'Tax',
417               'Exemptions',
418               ;
419
420 push @header2,
421                '',
422                '(printed on invoices)',
423                '',
424                '',
425                ;
426
427 my $newregion = 1;
428 my $cb_oldrow = '';
429 my $cb_sub = sub {
430   my $cust_main_county = shift;
431
432   if ( $cb_oldrow ) {
433     if (    $cb_oldrow->country  ne $cust_main_county->country 
434          || $cb_oldrow->state    ne $cust_main_county->state  
435          || $cb_oldrow->county   ne $cust_main_county->county  
436          || $cb_oldrow->taxclass ne $cust_main_county->taxclass )
437     {
438       $newregion = 1;
439     } else {
440       $newregion = 0;
441     }  
442     
443   } else {
444     $newregion = 1;
445   }
446   $cb_oldrow = $cust_main_county;
447
448   if ( $newregion ) {
449     my $taxnum = $cust_main_county->taxnum;
450     qq!<INPUT NAME="cust_main_county$taxnum" TYPE="checkbox" VALUE="1">!;
451   } else {
452     '';
453   }
454 };
455
456 push @fields, 
457   $cb_sub,
458   sub { shift->taxname || 'Tax' },
459   sub { shift->tax. '%&nbsp;<FONT SIZE="-1">(edit)</FONT>' },
460   $exempt_sub,
461 ;
462
463 push @color,
464   '000000',
465   sub { shift->taxname ? '000000' : '666666' },
466   sub { shift->tax     ? '000000' : '666666' },
467   '000000',
468 ;
469
470 $align .= 'clrl';
471
472 my @cell_style = map $cell_style_sub, (1..scalar(@header));
473
474 push @links,         '', '', $edit_link,    '';
475 push @link_onclicks, '', '', $edit_onclick, '';
476
477 </%init>