improve tax edit UI: add more counties & remove individual counties, RT#11144
[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 $cs_oldrow;
49 my $cell_style;
50 my $cell_style_sub = sub {
51   my $row = shift;
52   if ( $cs_oldrow ne $row ) {
53     if ( $cs_oldrow ) {
54       if ( $cs_oldrow->country ne $row->country ) {
55         $cell_style = 'border-top:2px solid #000000';
56       } elsif ( $cs_oldrow->state ne $row->state ) {
57         #$cell_style = 'border-top:1px solid #cccccc'; #default?
58         $cell_style = 'border-top:1px solid #bbbbbb';
59       } elsif ( $cs_oldrow->state eq $row->state ) {
60         #$cell_style = 'border-top:dashed 1px dark gray';
61         #$cell_style = 'border-top:1px dashed #cccccc';
62         $cell_style = '';
63       }
64     }
65     $cs_oldrow = $row;
66   }
67   return $cell_style;
68 };
69
70 #my $edit_link = [ "${p}edit/cust_main_county.html", 'taxnum' ];
71 my $edit_link = [ 'javascript:void(0);', sub { ''; } ];
72
73 my $edit_onclick = sub {
74   my $row = shift;
75   my $taxnum = $row->taxnum;
76   include( '/elements/popup_link_onclick.html',
77              'action'      => "${p}edit/cust_main_county.html?$taxnum",
78              'actionlabel' => 'Edit tax rate',
79              'height'      => 420,
80              #default# 'width'  => 540,
81              #default# 'color' => '#333399',
82          );
83 };
84
85 my $ex_oldrow;
86 sub expand_link {
87   my %param = @_;
88
89   if ( $ex_oldrow eq $param{'row'} ) {
90     return '';
91   } else {
92     $ex_oldrow = $param{'row'};
93   }
94
95   my $taxnum = $param{'row'}->taxnum;
96   my $url = "${p}edit/cust_main_county-expand.cgi?$taxnum";
97
98   '<FONT SIZE="-1">'.
99     include( '/elements/popup_link.html',
100                'label'       => $param{'label'},
101                'action'      => $url,
102                'actionlabel' => $param{'desc'},
103                'height'      => 420,
104                #default# 'width'  => 540,
105                #default# 'color' => '#333399',
106            ).
107   '</FONT>';
108 }
109
110 sub add_link {
111   my %param = @_;
112
113   #if ( $ex_oldrow eq $param{'row'} ) {
114   #  return '';
115   #} else {
116   #  $ex_oldrow = $param{'row'};
117   #}
118
119   my %below = ( 'county' => 'city',
120                 'state'  => 'county',
121               );
122   my $what = $below{ $param{'col' } };
123
124   my $taxnum = $param{'row'}->taxnum;
125   my $url = "${p}edit/cust_main_county-add.cgi?taxnum=$taxnum;what=$what";
126
127   '<FONT SIZE="-1">'.
128     include( '/elements/popup_link.html',
129                'label'       => $param{'label'},
130                'action'      => $url,
131                'actionlabel' => $param{'desc'},
132                'height'      => 420,
133                #default# 'width'  => 540,
134                #default# 'color' => '#333399',
135            ).
136   '</FONT>';
137 }
138
139 sub collapse_link {
140   my %param = @_;
141
142   my $row = $param{'row'};
143   my $col = $param{'col'};
144 #  return ''
145 #    if $col eq 'state' and $row->city
146 #                            || qsearch({
147 #                                 'table'   => 'cust_main_county',
148 #                                 'hashref' => {
149 #                                   'country' => $row->country,
150 #                                   'state'   => $row->state,
151 #                                   'city'    => { op=>'!=', value=>'' },
152 #                                 },
153 #                                 'order_by' => 'LIMIT 1',
154 #                               });
155
156   my %below = ( 'county' => 'city',
157                 'state'  => 'county',
158               );
159
160   #XXX can still show the link when you have some counties broken down into
161   #cities and others not :/
162
163   my $taxnum = $param{'row'}->taxnum;
164   my $url = "${p}edit/process/cust_main_county-collapse.cgi?taxnum=$taxnum;".
165               'country='. uri_escape($cgi->param('country')). ';'.
166               'state='.   uri_escape($cgi->param('state')).   ';'.
167               'county='.  uri_escape($cgi->param('county'));
168   $url = "javascript:collapse_areyousure('$url', '$below{$col}', '$col')";
169
170   qq(<FONT SIZE="-1"><A HREF="$url">$param{'label'}</A></FONT>);
171 }
172
173 sub remove_link {
174   my %param = @_;
175
176   my $row = $param{'row'};
177   my $col = $param{'col'};
178  
179   my $taxnum = $param{'row'}->taxnum;
180   my $url = "${p}edit/process/cust_main_county-remove.cgi?taxnum=$taxnum;".
181               'country='. uri_escape($cgi->param('country')). ';'.
182               'state='.   uri_escape($cgi->param('state')).   ';'.
183               'county='.  uri_escape($cgi->param('county'));
184   $url = "javascript:remove_areyousure('$url', '$col')";
185
186   qq(<FONT SIZE="-1"><A HREF="$url">$param{'label'}</A></FONT>);
187
188 }
189
190 sub separate_taxclasses_link {
191   my( $row ) = @_;
192   my $taxnum = $row->taxnum;
193   my $url = "${p}edit/process/cust_main_county-expand.cgi?taxclass=1;taxnum=$taxnum";
194
195   qq!<FONT SIZE="-1"><A HREF="$url">!;
196 }
197
198 #un-separate taxclasses too
199
200 </%once>
201 <%init>
202
203 die "access denied"
204   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
205
206 #my $conf = new FS::Conf;
207 #my $money_char = $conf->config('money_char') || '$';
208 my $enable_taxclasses = $conf->exists('enable_taxclasses');
209
210 my @menubar;
211
212 my $html_init = <<END;
213   <SCRIPT>
214     function collapse_areyousure(href,col,above) {
215      if (confirm('Are you sure you want to remove all ' + col + ' tax rates for this ' + above + '?') == true)
216        window.location.href = href;
217     }
218     function remove_areyousure(href,col) {
219      if (confirm('Are you sure you want to remove this ' + col + '?') == true)
220        window.location.href = href;
221     }
222   </SCRIPT>
223 END
224
225 $html_init .= "<BR>Click on <u>separate taxclasses</u> to specify taxes per taxclass."
226   if $enable_taxclasses;
227 $html_init .= '<BR><BR>';
228
229 $html_init .= include('/elements/init_overlib.html');
230
231 my $title = '';
232
233 my $country = '';
234 if ( $cgi->param('country') =~ /^(\w\w)$/ ) {
235   $country = $1;
236   $title = $country;
237 }
238 $cgi->delete('country');
239
240 my $state = '';
241 if ( $country && $cgi->param('state') =~ /^([\w \-\'\[\]]+)$/ ) {
242   $state = $1;
243   $title = "$state, $title";
244 }
245 $cgi->delete('state');
246
247 my $county = '';
248 if ( $country && $state &&
249      $cgi->param('county') =~
250        /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)$/
251    )
252 {
253   $county = $1;
254   if ( $county eq '__NONE__' ) {
255     $title = "No county, $title";
256   } else {
257     $title = "$county county, $title";
258   }
259 }
260 $cgi->delete('county');
261
262 $title = " for $title" if $title;
263
264 my $taxclass = '';
265 if ( $cgi->param('taxclass') =~ /^([\w \-]+)$/ ) {
266   $taxclass = $1;
267   $title .= " for $taxclass tax class";
268 }
269 $cgi->delete('taxclass');
270
271 if ( $country || $taxclass ) {
272   push @menubar, 'View all tax rates' => $p.'browse/cust_main_county.cgi';
273 }
274
275 $cgi->param('dummy', 1);
276
277 my $filter_change =
278   "window.location = '". $cgi->self_url.
279   ";country=' + encodeURIComponent( document.getElementById('country').options[document.getElementById('country').selectedIndex].value ) + ".
280   "';state='   + encodeURIComponent( document.getElementById('state').options[document.getElementById('state').selectedIndex].value ) +".
281   "';county='  + encodeURIComponent( document.getElementById('county').options[document.getElementById('county').selectedIndex].value );";
282
283 #restore this so pagination works
284 $cgi->param('country',  $country) if $country;
285 $cgi->param('state',    $state  ) if $state;
286 $cgi->param('county',   $county ) if $county;
287 $cgi->param('taxclass', $county ) if $taxclass;
288
289 my $html_posttotal =
290   '<BR>( show country: '.
291   include('/elements/select-country.html',
292             'country'             => $country,
293             'onchange'            => $filter_change,
294             'empty_label'         => '(all)',
295             'disable_empty'       => 0,
296             'disable_stateupdate' => 1,
297          );
298
299 my %states_hash = $country ? states_hash($country) : ();
300 if ( scalar(keys(%states_hash)) > 1 ) {
301   $html_posttotal .=
302     ' show state: '.
303     include('/elements/select-state.html',
304               'country'              => $country,
305               'state'                => $state,
306               'onchange'             => $filter_change,
307               'empty_label'          => '(all)',
308               'disable_empty'        => 0,
309               'disable_countyupdate' => 1,
310            );
311 } else {
312   $html_posttotal .=
313     '<SELECT NAME="state" ID="state" STYLE="display:none">'.
314     '  <OPTION VALUE="" SELECTED>'.
315     '</SELECT>';
316 }
317
318 my @counties = ( $country && $state ) ? counties($state, $country) : ();
319 if ( scalar(@counties) > 1 ) {
320   $html_posttotal .=
321     ' show county: '.
322     include('/elements/select-county.html',
323               'country'              => $country,
324               'state'                => $state,
325               'county'               => $county,
326               'onchange'             => $filter_change,
327               'empty_label'          => '(all)',
328               'empty_data_label'     => '(none)',
329               'empty_data_value'     => '__NONE__',
330               'disable_empty'        => 0,
331               'disable_cityupdate'   => 1,
332            );
333 } else {
334   $html_posttotal .=
335     '<SELECT NAME="county" ID="county" STYLE="display:none">'.
336     '  <OPTION VALUE="" SELECTED>'.
337     '</SELECT>';
338 }
339
340 $html_posttotal .= ' )';
341
342 my $bulk_popup_link = 
343   include( '/elements/popup_link_onclick.html',
344              'action'      => "${p}edit/bulk-cust_main_county.html?taxnum=MAGIC_taxnum_MAGIC",
345              'actionlabel' => 'Bulk add new tax',
346              'nofalse'     => 1,
347              'height'      => 420,
348              #default# 'width'  => 540,
349              #default# 'color' => '#333399',
350          );
351
352 my $html_foot = <<END;
353 <SCRIPT TYPE="text/javascript">
354
355   function setAll(setTo) {
356     theForm = document.taxesForm;
357     for (i=0,n=theForm.elements.length;i<n;i++) {
358       if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
359         theForm.elements[i].checked = setTo;
360       }
361     }
362   }
363
364   function toggleAll() {
365     theForm = document.taxesForm;
366     for (i=0,n=theForm.elements.length;i<n;i++) {
367       if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
368         if ( theForm.elements[i].checked == true ) {
369           theForm.elements[i].checked = false;
370         } else {
371           theForm.elements[i].checked = true;
372         }
373       }
374     }
375   }
376
377   function bulkPopup(action) {
378     var bulk_popup_link = "$bulk_popup_link";
379     var bulkstring = '';
380     theForm = document.taxesForm;
381     for (i=0,n=theForm.elements.length;i<n;i++) {
382       if (    theForm.elements[i].name.indexOf("cust_main_county") != -1
383            && theForm.elements[i].checked == true
384          ) {
385         var name = theForm.elements[i].name;
386         var taxnum = name.replace(/cust_main_county/, '');
387         if ( bulkstring != '' ) {
388           bulkstring = bulkstring + ',';
389         }
390         bulkstring = bulkstring + taxnum;
391        
392       }
393     }
394     bulkstring = bulkstring + ';action=' + action;
395     if ( bulk_popup_link.length > 1920 ) { // IE 2083 URL limit
396       alert('Too many selections'); // should do some session thing...
397       return false;
398     }
399     bulk_popup_link = bulk_popup_link.replace(/MAGIC_taxnum_MAGIC/, bulkstring);
400     eval(bulk_popup_link);
401   }
402
403 </SCRIPT>
404
405 <BR>
406 <A HREF="javascript:setAll(true)">select all</A> |
407 <A HREF="javascript:setAll(false)">unselect all</A> |
408 <A HREF="javascript:toggleAll()">toggle all</A>
409 <BR><BR>
410 <A HREF="javascript:void(0);" onClick="bulkPopup('add');">Add new tax to selected</A>
411 |
412 <A HREF="javascript:void(0);" onClick="bulkPopup('edit');">Bulk edit selected</A>
413
414 END
415
416 my $hashref = {};
417 my $count_query = 'SELECT COUNT(*) FROM cust_main_county';
418 if ( $country ) {
419   $hashref->{'country'} = $country;
420   $count_query .= ' WHERE country = '. dbh->quote($country);
421 }
422 if ( $state ) {
423   $hashref->{'state'} = $state;
424   $count_query .= ' AND state   = '. dbh->quote($state);
425 }
426 if ( $county ) {
427   if ( $county eq '__NONE__' ) {
428     $hashref->{'county'} = '';
429     $count_query .= " AND ( county = '' OR county IS NULL ) ";
430   } else {
431     $hashref->{'county'} = $county;
432     $count_query .= ' AND county  = '. dbh->quote($county);
433   }
434 }
435 if ( $taxclass ) {
436   $hashref->{'taxclass'} = $taxclass;
437   $count_query .= ( $count_query =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
438                   ' taxclass  = '. dbh->quote($taxclass);
439 }
440
441
442 $cell_style = '';
443
444 my @header        = ( 'Country', 'State/Province', 'County', 'City' );
445 my @header2       = ( '', '', '', '', );
446 my @links         = ( '', '', '', '', );
447 my @link_onclicks = ( '', '', '', '', );
448 my $align = 'llll';
449
450 my %seen_country = ();
451 my %seen_state = ();
452
453 my @fields = (
454   sub { my $country = shift->country;
455         return '' if $seen_country{$country}++;
456         code2country($country). "&nbsp;($country)";
457       },
458   sub { my $label = $seen_state{$_[0]->country}->{$_[0]->state}++
459                       ? '' : state_label($_[0]->state, $_[0]->country);
460         my $countylinks = ( $_[0]->county && $label )
461                              ? '&nbsp;'. add_link(
462                                  desc => 'Add more counties',
463                                  col  => 'state',
464                                  label=> 'add&nbsp;more&nbsp;counties',
465                                  row  => $_[0],
466                                  cgi  => $cgi,
467                                ).
468                                ' '. collapse_link(
469                                  col  => 'state',
470                                  label=> 'remove&nbsp;all&nbsp;counties',
471                                  row  => $_[0],
472                                  cgi  => $cgi,
473                                )
474                              : '';
475         my $addlink = 
476           ( $_[0]->state
477               ? ''
478               : '&nbsp;'. expand_link( desc  => 'Add States',
479                                        row   => $_[0],
480                                        label => 'add&nbsp;states',
481                                        cgi  => $cgi,
482                                      )
483           );
484         $label.$countylinks.$addlink;
485       },
486   sub { $_[0]->county
487           ? $_[0]->county. '&nbsp;'.
488               remove_link(   col  => 'county',
489                              label=> 'remove&nbsp;county',
490                              row  => $_[0],
491                              cgi  => $cgi,
492                            )
493           : '(all)&nbsp;'.
494               expand_link(   desc  => 'Add Counties',
495                              row   => $_[0],
496                              label => 'add&nbsp;counties',
497                              cgi  => $cgi,
498                          );
499       },
500   sub { $_[0]->city
501           ? $_[0]->city. '&nbsp;'.
502               collapse_link( col  => 'county',
503                              label=> 'remove&nbsp;cities',
504                              row  => $_[0],
505                              cgi  => $cgi,
506                            )
507           : '(all)&nbsp;'.
508               expand_link(   desc  => 'Add Cities',
509                              row   => $_[0],
510                              label => 'add&nbsp;cities',
511                              cgi  => $cgi,
512                          );
513       },
514 );
515
516 my @color = (
517   '000000',
518   sub { shift->state  ? '000000' : '999999' },
519   sub { shift->county ? '000000' : '999999' },
520   sub { shift->city   ? '000000' : '999999' },
521 );
522
523 if ( $conf->exists('enable_taxclasses') ) {
524   push @header, qq!Tax class (<A HREF="${p}edit/part_pkg_taxclass.html">add new</A>)!;
525   push @header2, '(per-package classification)';
526   push @fields, sub { $_[0]->taxclass || '(all)&nbsp;'.
527                        separate_taxclasses_link($_[0], 'Separate Taxclasses').
528                        'separate&nbsp;taxclasses</A></FONT>'
529                     };
530   push @color, sub { shift->taxclass ? '000000' : '999999' };
531   push @links, '';
532   push @link_onclicks, '';
533   $align .= 'l';
534 }
535
536 push @header,
537               '', #checkbox column
538               'Tax name',
539               'Rate', #'Tax',
540               'Exemptions',
541               ;
542
543 push @header2,
544                '',
545                '(printed on invoices)',
546                '',
547                '',
548                ;
549
550 my $newregion = 1;
551 my $cb_oldrow = '';
552 my $cb_sub = sub {
553   my $cust_main_county = shift;
554
555   if ( $cb_oldrow ) {
556     if (    $cb_oldrow->city     ne $cust_main_county->city 
557          || $cb_oldrow->county   ne $cust_main_county->county  
558          || $cb_oldrow->state    ne $cust_main_county->state  
559          || $cb_oldrow->country  ne $cust_main_county->country 
560          || $cb_oldrow->taxclass ne $cust_main_county->taxclass )
561     {
562       $newregion = 1;
563     } else {
564       $newregion = 0;
565     }  
566     
567   } else {
568     $newregion = 1;
569   }
570   $cb_oldrow = $cust_main_county;
571
572   if ( $newregion ) {
573     my $taxnum = $cust_main_county->taxnum;
574     qq!<INPUT NAME="cust_main_county$taxnum" TYPE="checkbox" VALUE="1">!;
575   } else {
576     '';
577   }
578 };
579
580 push @fields, 
581   $cb_sub,
582   sub { shift->taxname || 'Tax' },
583   sub { shift->tax. '%&nbsp;<FONT SIZE="-1">(edit)</FONT>' },
584   $exempt_sub,
585 ;
586
587 push @color,
588   '000000',
589   sub { shift->taxname ? '000000' : '666666' },
590   sub { shift->tax     ? '000000' : '666666' },
591   '000000',
592 ;
593
594 $align .= 'clrl';
595
596 my @cell_style = map $cell_style_sub, (1..scalar(@header));
597
598 push @links,         '', '', $edit_link,    '';
599 push @link_onclicks, '', '', $edit_onclick, '';
600
601 </%init>