new tax rating engine
[freeside.git] / httemplate / browse / part_pkg_taxproduct.cgi
1 <% include( 'elements/browse.html',
2      'title'          => "Tax Products $title",
3      'name_singular'  => 'tax product',
4      'menubar'        => \@menubar,
5      'html_init'      => $html_init,
6      'query'          => {
7                            'table'     => 'part_pkg_taxproduct',
8                            'hashref'   => $hashref,
9                            'order_by'  => 'ORDER BY description',
10                            'extra_sql' => $extra_sql,
11                          },
12      'count_query'    => $count_query,
13      'header'         => \@header,
14      'fields'         => \@fields,
15      'align'          => $align,
16      'links'          => \@links,
17      'link_onclicks'  => \@link_onclicks,
18   )
19 %>
20 <%once>
21
22 my $conf = new FS::Conf;
23
24 my $select_link = [ 'javascript:void(0);', sub { ''; } ];
25
26 </%once>
27 <%init>
28
29 die "access denied"
30   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
31
32 my @menubar;
33 my $title = '';
34
35 my $data_vendor = '';
36 if ( $cgi->param('data_vendor') =~ /^(\w+)$/ ) {
37   $data_vendor = $1;
38   $title = "$data_vendor";
39 }
40 $cgi->delete('data_vendor');
41
42 $title = " for $title" if $title;
43
44 my $taxproductnum = $1
45   if ( $cgi->param('taxproductnum') =~ /^(\d+)$/ );
46 my $tax_group = $1
47   if ( $cgi->param('tax_group') =~ /^([- \w\(\).\/]+)$/ );
48 my $tax_item = $1
49   if ( $cgi->param('tax_item') =~ /^([- \w\(\).\/&%]+)$/ );
50 my $tax_provider = $1
51   if ( $cgi->param('tax_provider') =~ /^([ \w]+)$/ );
52 my $tax_customer = $1
53   if ( $cgi->param('tax_customer') =~ /^([ \w]+)$/ );
54 my $id = $1
55   if ( $cgi->param('id') =~ /^([ \w]+)$/ );
56
57 my $select_onclick = sub {
58   my $row = shift;
59   my $taxnum = $row->taxproductnum;
60   my $desc = $row->description;
61   "parent.document.getElementById('$id').value = $taxnum;".
62   "parent.document.getElementById('${id}_description').value = '$desc';".
63   "parent.cClick();";
64 }
65   if $id;
66
67 my $selected_part_pkg_taxproduct;
68 if ($taxproductnum) {
69   $selected_part_pkg_taxproduct =
70     qsearchs('part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum });
71 }
72
73 my $hashref = {};
74 my $extra_sql = '';
75 if ( $data_vendor ) {
76   $extra_sql .= ' WHERE data_vendor = '. dbh->quote($data_vendor);
77 }
78
79 if ($tax_group || $tax_item || $tax_customer || $tax_provider) {
80   my $compare = "LIKE '". ( $tax_group || "%" ). " : ". ( $tax_item || "%" ). " : ".
81                 ( $tax_provider || "%" ). " : ". ( $tax_customer || "%" ). "'";
82   $compare = "= '$tax_group:$tax_item:$tax_provider:$tax_customer'"
83     if ($tax_group && $tax_item && $tax_provider && $tax_customer);
84
85   $extra_sql .= ($extra_sql =~ /WHERE/ ? ' AND ' : ' WHERE ').
86                 "description $compare";
87
88 }
89 $cgi->delete('tax_group');
90 $cgi->delete('tax_item');
91 $cgi->delete('tax_provider');
92 $cgi->delete('tax_customer');
93
94
95 if ( $tax_group || $tax_item || $tax_provider || $tax_customer ) {
96   push @menubar, 'View all tax products' => $p.'browse/part_pkg_taxproduct.cgi';
97 }
98
99 $cgi->param('dummy', 1);
100
101 #restore this so pagination works
102 $cgi->param('data_vendor',  $data_vendor) if $data_vendor;
103 $cgi->param('tax_group',  $tax_group) if $tax_group;
104 $cgi->param('tax_item', $tax_item ) if $tax_item;
105 $cgi->param('tax_provider', $tax_provider ) if $tax_provider;
106 $cgi->param('tax_customer', $tax_customer ) if $tax_customer;
107
108 my $count_query = "SELECT COUNT(*) FROM part_pkg_taxproduct $extra_sql";
109
110 my @header        = ( 'Data Vendor', 'Group', 'Item', 'Provider', 'Customer' );
111 my @links         = ( $select_link,
112                       $select_link,
113                       $select_link,
114                       $select_link,
115                       $select_link,
116                     );
117 my @link_onclicks = ( $select_onclick,
118                       $select_onclick,
119                       $select_onclick,
120                       $select_onclick,
121                       $select_onclick,
122                     );
123 my $align = 'lllll';
124
125 my @fields = (
126   'data_vendor',
127   sub { shift->description =~ /^(.*):.*:.*:.*$/; $1;},
128   sub { shift->description =~ /^.*:(.*):.*:.*$/; $1;},
129   sub { shift->description =~ /^.*:.*:(.*):.*$/; $1;},
130   sub { shift->description =~ /^.*:.*:.*:(.*)$/; $1;},
131 );
132
133 my $html_init = '';
134
135 $html_init = '<TABLE><TR><TD>Current tax product: </TD><TD>'.
136                 $selected_part_pkg_taxproduct->description.
137                 '</TD></TR></TABLE><BR><BR>'
138   if $selected_part_pkg_taxproduct;
139
140 my $type = $cgi->param('_type');
141 $html_init .= qq(
142   <FORM>
143     <INPUT NAME="_type" TYPE="hidden" VALUE="$type">
144     <INPUT NAME="taxproductnum" TYPE="hidden" VALUE="$taxproductnum">
145     <INPUT NAME="id" TYPE="hidden" VALUE="$id">
146     <TABLE>
147       <TR>
148         <TD><SELECT NAME="data_vendor" onChange="this.form.submit()">
149 );
150
151 my $sql = "SELECT DISTINCT data_vendor FROM part_pkg_taxproduct ORDER BY data_vendor";
152 my $dbh = dbh;
153 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
154 $sth->execute or die $sth->errstr;
155 for (['(choose data vendor)'], @{$sth->fetchall_arrayref}) {
156   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
157                 ($_->[0] eq $data_vendor ? " SELECTED" : "").
158                 '">'.  $_->[0];
159 }
160 $html_init .= qq(
161         </SELECT>
162
163 <!-- cch specific -->
164         <TD><SELECT NAME="tax_group" onChange="this.form.submit()">
165 );
166
167 $sql = "SELECT DISTINCT ".
168        qq!substring(description from '#"%#" : % : % : %' for '#'),!.
169        qq!substring(description from '#"%#" : % : % : %' for '#')!.
170        "FROM part_pkg_taxproduct ORDER BY 1";
171
172 $sth = $dbh->prepare($sql) or die $dbh->errstr;
173 $sth->execute or die $sth->errstr;
174 for (['', '(choose group)'], @{$sth->fetchall_arrayref}) {
175   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
176                  ($_->[0] eq $tax_group ? " SELECTED" : "").
177                  '">'. $_->[1];
178 }
179
180 $html_init .= qq(
181         </SELECT>
182
183         <TD><SELECT NAME="tax_item" onChange="this.form.submit()">
184 );
185
186 $sql = "SELECT DISTINCT ".
187        qq!substring(description from '% : #"%#" : %: %' for '#'),!.
188        qq!substring(description from '% : #"%#" : %: %' for '#')!.
189        "FROM part_pkg_taxproduct ORDER BY 1";
190
191 $sth = $dbh->prepare($sql) or die $dbh->errstr;
192 $sth->execute or die $sth->errstr;
193 for (@{$sth->fetchall_arrayref}) {
194   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
195                  ($_->[0] eq $tax_item ? " SELECTED" : "").
196                  '">'.  ($_->[0] ? $_->[1] : '(choose item)');
197 }
198
199 $html_init .= qq(
200         </SELECT>
201
202         <TD><SELECT NAME="tax_provider" onChange="this.form.submit()">
203 );
204
205 $sql = "SELECT DISTINCT ".
206        qq!substring(description from '% : % : #"%#" : %' for '#'),!.
207        qq!substring(description from '% : % : #"%#" : %' for '#')!.
208        "FROM part_pkg_taxproduct ORDER BY 1";
209
210 $sth = $dbh->prepare($sql) or die $dbh->errstr;
211 $sth->execute or die $sth->errstr;
212 for (@{$sth->fetchall_arrayref}) {
213   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
214                  ($_->[0] eq $tax_provider ? " SELECTED" : "").
215                  '">'.  ($_->[0] ? $_->[1] : '(choose provider type)');
216 }
217
218 $html_init .= qq(
219         </SELECT>
220
221         <TD><SELECT NAME="tax_customer" onChange="this.form.submit()">
222 );
223
224 $sql = "SELECT DISTINCT ".
225        qq!substring(description from '% : % : % : #"%#"' for '#'),!.
226        qq!substring(description from '% : % : % : #"%#"' for '#')!.
227        "FROM part_pkg_taxproduct ORDER BY 1";
228
229 $sth = $dbh->prepare($sql) or die $dbh->errstr;
230 $sth->execute or die $sth->errstr;
231 for (@{$sth->fetchall_arrayref}) {
232   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
233                  ($_->[0] eq $tax_customer ? " SELECTED" : "").
234                  '">'.  ($_->[0] ? $_->[1] : '(choose customer type)');
235 }
236
237 $html_init .= qq(
238         </SELECT>
239
240       </TR>
241     </TABLE>
242   </FORM>
243
244 );
245
246 </%init>