3e67b1122e6a7177cdbee3f2f0cc490c34510fdb
[freeside.git] / httemplate / browse / cust_main_county.cgi
1 <% include( 'elements/browse.html',
2      'title'          => 'Tax Rates',
3      'name_singular'  => 'tax rate',
4      'html_init'      => $html_init,
5      'html_posttotal' => $html_posttotal,
6      'query'          => {
7                            'table'    => 'cust_main_county',
8                            'hashref'  => $hashref,
9                            'order_by' =>
10                              'ORDER BY country, state, county, taxclass',
11                          },
12      'count_query'    => $count_query,
13      'header'         => \@header,
14      'header2'        => \@header2,
15      'fields'         => \@fields,
16      'align'          => $align,
17      'color'          => \@color,
18      'cell_style'     => \@cell_style,
19      'links'          => \@links,
20      'link_onclicks'  => \@link_onclicks,
21   )
22 %>
23 %
24 % #         <FONT SIZE=-1><A HREF="<% $p %>edit/process/cust_main_county-collapse.cgi?<% $hashref->{taxnum} %>">collapse state</A></FONT>
25 % # % } 
26 %
27 <%once>
28
29 my $conf = new FS::Conf;
30 my $money_char = $conf->config('money_char') || '$';
31
32 my @manual_countries = ( 'US', 'CA', 'AU', 'NZ', 'GB' ); #some manual ordering
33 my @all_countries = ( @manual_countries, 
34                       grep { my $c = $_; ! grep { $c eq $_ } @manual_countries }
35                       map { $_->country } 
36                           qsearch({
37                                     'select'    => 'country',
38                                     'table'     => 'cust_main_county',
39                                     'hashref'   => {},
40                                     'extra_sql' => 'GROUP BY country',
41                                  })
42                     );
43
44 my $exempt_sub = sub {
45   my $cust_main_county = shift;
46
47   my @exempt = ();
48   push @exempt,
49        sprintf("$money_char%.2f&nbsp;per&nbsp;month", $cust_main_county->exempt_amount )
50     if $cust_main_county->exempt_amount > 0;
51
52   push @exempt, 'Setup&nbsp;fee'
53     if $cust_main_county->setuptax =~ /^Y$/i;
54
55   push @exempt, 'Recurring&nbsp;fee'
56     if $cust_main_county->recurtax =~ /^Y$/i;
57
58   [ map [ {'data'=>$_} ], @exempt ];
59 };
60
61 my $oldrow;
62 my $cell_style;
63 my $cell_style_sub = sub {
64   my $row = shift;
65   if ( $oldrow ne $row ) {
66     if ( $oldrow ) {
67       if ( $oldrow->country ne $row->country ) {
68         $cell_style = 'border-top:1px solid #000000';
69       } elsif ( $oldrow->state ne $row->state ) {
70         $cell_style = 'border-top:1px solid #cccccc'; #default?
71       } elsif ( $oldrow->state eq $row->state ) {
72         #$cell_style = 'border-top:dashed 1px dark gray';
73         $cell_style = 'border-top:1px dashed #cccccc';
74       }
75     }
76     $oldrow = $row;
77   }
78   return $cell_style;
79 };
80
81 #my $edit_link = [ "${p}edit/cust_main_county.html", 'taxnum' ];
82 my $edit_link = [ 'javascript:void(0);', sub { ''; } ];
83
84 my $edit_onclick = sub {
85   my $row = shift;
86   my $taxnum = $row->taxnum;
87   my $color = '#333399';
88   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;!;
89 };
90
91 sub expand_link {
92   my( $row, $desc, %opt ) = @_;
93   my $taxnum = $row->taxnum;
94   $taxnum = "taxclass$taxnum" if $opt{'taxclass'};
95   my $color = '#333399';
96   qq!<FONT SIZE="-1"><A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}edit/cust_main_county-expand.cgi?$taxnum', 540, 420, 'edit_cust_main_county_popup' ), CAPTION, '$desc', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK, BGCOLOR, '$color', CGCOLOR, '$color' ); return false;">!;
97 }
98
99 </%once>
100 <%init>
101
102 die "access denied"
103   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
104
105 #my $conf = new FS::Conf;
106 #my $money_char = $conf->config('money_char') || '$';
107 my $enable_taxclasses = $conf->exists('enable_taxclasses');
108
109 my $html_init =
110   "Click on <u>add states</u> to specify a country's tax rates by state or province.
111    <BR>Click on <u>add counties</u> to specify a state's tax rates by county.";
112 $html_init .= "<BR>Click on <u>add taxclasses</u> to specify tax classes."
113   if $enable_taxclasses;
114 $html_init .= '<BR><BR>';
115
116 $html_init .= qq(
117   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws.js"></SCRIPT>
118   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_iframe.js"></SCRIPT>
119   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_draggable.js"></SCRIPT>
120   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/iframecontentmws.js"></SCRIPT>
121 );
122
123 my $filter_country = '';
124 if ( $cgi->param('filter_country') =~ /^(\w\w)$/ ) {
125   $filter_country = $1;
126 }
127 $cgi->delete('filter_country');
128 $cgi->param('dummy', 1);
129
130 my $country_filter_change =
131   "window.location = '".
132   $cgi->self_url. ";filter_country=' + this.options[this.selectedIndex].value;";
133
134 my $html_posttotal =
135   '(show country: '.
136   qq(<SELECT NAME="filter_country" onChange="$country_filter_change">).
137   qq(<OPTION VALUE="">(all)\n).
138   join("\n", map qq[<OPTION VALUE="$_"].
139                    ( $_ eq $filter_country ? 'SELECTED' : '' ).
140                    '>'. code2country($_). " ($_)",
141                  @all_countries
142       ).
143   '</SELECT>)';
144
145 my $hashref = {};
146 my $count_query = 'SELECT COUNT(*) FROM cust_main_county';
147 if ( $filter_country ) {
148   $hashref->{'country'} = $filter_country;
149   $count_query .= " WHERE country = '$filter_country'";
150 }
151
152 $cell_style = '';
153
154 my @header        = ( 'Country', 'State/Province', 'County',);
155 my @header2       = ( '', '', '', );
156 my @links         = ( '', '', '', );
157 my @link_onclicks = ( '', '', '', );
158 my $align = 'lll';
159
160 my @fields = (
161   sub { my $country = shift->country;
162         code2country($country). " ($country)";
163       },
164   sub { state_label($_[0]->state, $_[0]->country).
165         ( $_[0]->state
166             ? ''
167             : '&nbsp'. expand_link($_[0], 'Add States').
168                        'add&nbsp;states</A></FONT>'
169         )
170       },
171   sub { $_[0]->county || '(all)&nbsp'.
172                          expand_link($_[0], 'Add Counties').
173                          'add&nbsp;counties</A></FONT>'
174       },
175 );
176
177 my @color = (
178   '000000',
179   sub { shift->state  ? '000000' : '999999' },
180   sub { shift->county ? '000000' : '999999' },
181 );
182
183 if ( $conf->exists('enable_taxclasses') ) {
184   push @header,  'Tax class';
185   push @header2, '(per-package classification)';
186   push @fields,  sub { $_[0]->taxclass || '(all)&nbsp'.
187                          expand_link($_[0], 'Add Taxclasses', 'taxclass'=>1).
188                          'add&nbsp;taxclasses</A></FONT>'
189                      };
190   push @color,   sub { shift->taxclass ? '000000' : '999999' };
191   push @links,   '';
192   push @link_onclicks, '';
193   $align .= 'l';
194 }
195
196 push @header, 'Tax name',
197               'Rate', #'Tax',
198               'Exemptions',
199               ;
200
201 push @header2, '(printed on invoices)',
202                '',
203                '',
204                ;
205
206 push @fields, 
207   sub { shift->taxname || 'Tax' },
208   sub { shift->tax. '%&nbsp;<FONT SIZE="-1">(edit)</FONT>' },
209   $exempt_sub,
210 ;
211
212 push @color,
213   sub { shift->taxname ? '000000' : '666666' },
214   sub { shift->tax     ? '000000' : '666666' },
215   '000000',
216 ;
217
218 $align .= 'lrl';
219
220 my @cell_style = map $cell_style_sub, (1..scalar(@header));
221
222 push @links,         '', $edit_link,    '';
223 push @link_onclicks, '', $edit_onclick, '';
224
225 </%init>