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