default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / browse / tax_rate.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_form'          => $html_form,
7      'disableable'        => 1,
8      'disabled_statuspos' => 5,
9      'query'              => $query,
10      'count_query'        => $count_query,
11      'header'             => \@header,
12      'header2'            => \@header2,
13      'fields'             => \@fields,
14      'align'              => $align,
15      'color'              => \@color,
16      'cell_style'         => \@cell_style,
17      'links'              => \@links,
18      'link_onclicks'      => \@link_onclicks,
19   )
20 %>
21 <%once>
22
23 my $conf = new FS::Conf;
24 my $money_char = $conf->config('money_char') || '$';
25
26 my $rate_sub = sub {
27   my $tax_rate = shift;
28
29   my $units = $tax_rate->unittype_name;
30   $units =~ s/ /&nbsp;/g;
31
32   my @rate = ();
33   push @rate,
34       ($tax_rate->tax * 100). '%&nbsp;<FONT SIZE="-1">(edit)</FONT>'
35     if $tax_rate->tax > 0 || $tax_rate->taxbase > 0;
36   push @rate,
37       ($tax_rate->excessrate * 100). '%&nbsp;<FONT SIZE="-1">(edit)</FONT>'
38     if $tax_rate->excessrate > 0;
39   push @rate,
40       $money_char. $tax_rate->fee.
41       qq!&nbsp;per&nbsp;$units<FONT SIZE="-1">(edit)</FONT>!
42     if $tax_rate->fee > 0 || $tax_rate->feebase > 0;
43   push @rate,
44       $money_char. $tax_rate->excessfee.
45       qq!&nbsp;per&nbsp;$units<FONT SIZE="-1">(edit)</FONT>!
46     if $tax_rate->excessfee > 0;
47
48
49   [ map [ {'data'=>$_} ], @rate ];
50 };
51
52 my $limit_sub = sub {
53   my $tax_rate = shift;
54
55   my $maxtype = $tax_rate->maxtype_name;
56   $maxtype =~ s/ /&nbsp;/g;
57
58   my $units = $tax_rate->unittype_name;
59   $units =~ s/ /&nbsp;/g;
60
61   my @limit = ();
62   push @limit,
63        sprintf("$money_char%.2f&nbsp%s", $tax_rate->taxbase, $maxtype )
64     if $tax_rate->taxbase > 0;
65   push @limit,
66        sprintf("$money_char%.2f&nbsp;tax", $tax_rate->taxmax )
67     if $tax_rate->taxmax > 0;
68   push @limit,
69        $tax_rate->feebase. "&nbsp;$units". ($tax_rate->feebase == 1 ? '' : 's')
70     if $tax_rate->feebase > 0;
71   push @limit,
72        $tax_rate->feemax. "&nbsp;$units". ($tax_rate->feebase == 1 ? '' : 's')
73     if $tax_rate->feemax > 0;
74
75   push @limit, 'Excluding&nbsp;setup&nbsp;fee'
76     if $tax_rate->setuptax =~ /^Y$/i;
77
78   push @limit, 'Excluding&nbsp;recurring&nbsp;fee'
79     if $tax_rate->recurtax =~ /^Y$/i;
80
81   [ map [ {'data'=>$_} ], @limit ];
82 };
83
84 my $oldrow;
85 my $cell_style;
86 my $cell_style_sub = sub {
87   my $row = shift;
88   if ( $oldrow ne $row ) {
89     if ( $oldrow ) {
90       if ( $oldrow->country ne $row->country ) {
91         $cell_style = 'border-top:1px solid #000000';
92       } elsif ( $oldrow->state ne $row->state ) {
93         $cell_style = 'border-top:1px solid #cccccc'; #default?
94       } elsif ( $oldrow->state eq $row->state ) {
95         #$cell_style = 'border-top:dashed 1px dark gray';
96         $cell_style = 'border-top:1px dashed #cccccc';
97       }
98     }
99     $oldrow = $row;
100   }
101   return $cell_style;
102 };
103
104 my $select_link = [ 'javascript:void(0);', sub { ''; } ];
105
106 my $select_onclick = sub {
107   my $row = shift;
108   my $taxnum = $row->taxnum;
109   my $color = '#333399';
110   qq!overlib( OLiframeContent('${p}edit/tax_rate.html?$taxnum', 540, 620, 'edit_tax_rate_popup' ), CAPTION, 'Edit tax rate', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK, BGCOLOR, '$color', CGCOLOR, '$color' ); return false;!;
111 };
112
113 </%once>
114 <%init>
115
116 die "access denied"
117   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
118
119 my @menubar;
120 my $title = '';
121
122 my $data_vendor = '';
123 if ( $cgi->param('data_vendor') =~ /^(\w+)$/ ) {
124   $data_vendor = $1;
125   $title = "$data_vendor";
126 }
127 $cgi->delete('data_vendor');
128
129 my $geocode = '';
130 if ( $cgi->param('geocode') =~ /^(\w+)$/ ) {
131   $geocode = $1;
132   $title = " geocode $geocode";
133 }
134 $cgi->delete('geocode');
135
136 $title = " for $title" if $title;
137
138 my $taxclassnum = '';
139 if ( $cgi->param('taxclassnum') =~ /^(\d+)$/ ) {
140   $taxclassnum = $1;
141   my $tax_class = qsearchs('tax_class', {'taxclassnum' => $taxclassnum});
142   if ($tax_class) {
143     $title .= " for ". $tax_class->taxclass.
144               " (".  $tax_class->description. ") tax class";
145   }else{
146     $taxclassnum = '';
147   }
148 }
149 $cgi->delete('taxclassnum');
150
151 my $tax_type = $1
152   if ( $cgi->param('tax_type') =~ /^(\d+)$/ );
153 my $tax_cat = $1
154   if ( $cgi->param('tax_cat') =~ /^(\d+)$/ );
155
156 if ($tax_type || $tax_cat ) {
157   my $compare = "LIKE '". ( $tax_type || "%" ). ":". ( $tax_cat || "%" ). "'";
158   $compare = "= '$tax_type:$tax_cat'" if ($tax_type && $tax_cat);
159   my @tax_class =
160     qsearch({ 'table'     => 'tax_class',
161               'hashref'   => {},
162               'extra_sql' => "WHERE taxclass $compare",
163            });
164   if (@tax_class) {
165     $tax_class[0]->description =~ /^(.*):(.*)/;
166     $title .= " for";
167     $title .= " $tax_type ($1) tax type" if $tax_type;
168     $title .= " and" if ($tax_type && $tax_cat);
169     $title .= " $tax_cat ($2) tax category" if $tax_cat;
170   }else{
171     $tax_type = '';
172     $tax_cat = '';
173   }
174 }
175 $cgi->delete('tax_type');
176 $cgi->delete('tax_cat');
177
178 if ( $geocode || $taxclassnum ) {
179   push @menubar, 'View all tax rates' => $p.'browse/tax_rate.cgi';
180 }
181
182 $cgi->param('dummy', 1);
183
184 #restore this so pagination works
185 $cgi->param('data_vendor',  $data_vendor) if $data_vendor;
186 $cgi->param('geocode',  $geocode) if $geocode;
187 $cgi->param('taxclassnum', $taxclassnum ) if $taxclassnum;
188 $cgi->param('tax_type', $tax_type ) if $tax_type;
189 $cgi->param('tax_cat', $tax_cat ) if $tax_cat;
190
191 my $html_form = include('/elements/init_overlib.html'). '<BR><BR>'.
192   join(' ',
193     map {
194       include('/elements/popup_link.html',
195                {
196                  'action' => $p. "misc/enable_or_disable_tax.html?action=$_&".
197                              $cgi->query_string,
198                  'label' => ucfirst($_). ' all these taxes',
199                  'actionlabel' => ucfirst($_). ' taxes',
200                },
201              );
202     }
203     qw(disable enable)
204   );
205
206 my ($query, $count_query) = FS::tax_rate::browse_queries(scalar($cgi->Vars));
207
208 $cell_style = '';
209
210 my @header        = ( 'Location Code',  );
211 my @header2       = ( '', );
212 my @links         = ( '', );
213 my @link_onclicks = ( '', );
214 my $align = 'l';
215
216 my @fields = (
217   'geocode',
218 );
219
220 my @color = (
221   '000000',
222 );
223
224 push @header, qq!Tax class (<A HREF="${p}edit/tax_class.html">add new</A>)!;
225 push @header2, '(per-tax classification)';
226 push @fields, 'taxclass_description';
227 push @color, '000000';
228 push @links, '';
229 push @link_onclicks, '';
230 $align .= 'l';
231
232 push @header, 'Tax name',
233               'Rate', #'Tax',
234               'Limits',
235               ;
236
237 push @header2, '(printed on invoices)',
238                '',
239                '',
240                ;
241
242 push @fields, 
243   sub { shift->taxname || 'Tax' },
244   $rate_sub,
245   $limit_sub,
246 ;
247
248 push @color,
249   sub { shift->taxname ? '000000' : '666666' },
250   sub { shift->tax     ? '000000' : '666666' },
251   '000000',
252 ;
253
254 $align .= 'lrl';
255
256 my @cell_style = map $cell_style_sub, (1..scalar(@header));
257
258 push @links,         '', $select_link,    '';
259 push @link_onclicks, '', $select_onclick, '';
260
261 my $html_init = '';
262
263 $html_init .= qq(
264   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws.js"></SCRIPT>
265   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_iframe.js"></SCRIPT>
266   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_draggable.js"></SCRIPT>
267   <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/iframecontentmws.js"></SCRIPT>
268
269 );
270
271 $html_init .= qq(
272   <FORM>
273     <TABLE>
274       <TR>
275         <TD><SELECT NAME="data_vendor" onChange="this.form.submit()">
276 );
277
278 my $sql = "SELECT DISTINCT data_vendor FROM tax_rate ORDER BY data_vendor";
279 my $dbh = dbh;
280 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
281 $sth->execute or die $sth->errstr;
282 for (['(choose data vendor)'], @{$sth->fetchall_arrayref}) {
283   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
284                 ($_->[0] eq $data_vendor ? " SELECTED" : "").
285                 '">'.  $_->[0];
286 }
287 $html_init .= qq(
288         </SELECT>
289
290         <TD><INPUT NAME="geocode" TYPE="text" SIZE="12" VALUE="$geocode"></TD>
291
292 <!-- generic
293         <TD><INPUT NAME="taxclassnum" TYPE="text" SIZE="12" VALUE="$taxclassnum"></TD>
294         <TD><INPUT TYPE="submit" VALUE="Filter by tax_class"></TD>
295 -->
296
297 <!-- cch specific -->
298         <TD><SELECT NAME="tax_type" onChange="this.form.submit()">
299 );
300
301 $sql = "SELECT DISTINCT ".
302        "substring(taxclass from 1 for position(':' in taxclass)-1),".
303        "substring(description from 1 for position(':' in description)-1) ".
304        "FROM tax_class WHERE data_vendor='cch' ORDER BY 2";
305
306 $sth = $dbh->prepare($sql) or die $dbh->errstr;
307 $sth->execute or die $sth->errstr;
308 for (['', '(choose tax type)'], @{$sth->fetchall_arrayref}) {
309   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
310                  ($_->[0] eq $tax_type ? " SELECTED" : "").
311                  '">'. $_->[1];
312 }
313
314 $html_init .= qq(
315         </SELECT>
316
317         <TD><SELECT NAME="tax_cat" onChange="this.form.submit()">
318 );
319
320 $sql = "SELECT DISTINCT ".
321        "substring(taxclass from position(':' in taxclass)+1),".
322        "substring(description from position(':' in description)+1) ".
323        "from tax_class WHERE data_vendor='cch' ORDER BY 2";
324
325 $sth = $dbh->prepare($sql) or die $dbh->errstr;
326 $sth->execute or die $sth->errstr;
327 for (['', '(choose tax category)'], @{$sth->fetchall_arrayref}) {
328   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
329                  ($_->[0] eq $tax_cat ? " SELECTED" : "").
330                  '">'.  $_->[1];
331 }
332
333 $html_init .= qq(
334         </SELECT>
335
336       </TR>
337       <TR>
338         <TD></TD>
339         <TD><INPUT TYPE="submit" VALUE="Filter by geocode"></TD>
340         <TD></TD>
341         <TD></TD>
342       </TR>
343     </TABLE>
344   </FORM>
345
346 );
347
348 </%init>