multiple usage classes checkpoint
[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 $remove_onclick = <<EOS
58   parent.document.getElementById('$id').value = '';
59   parent.document.getElementById('${id}_description').value = '';
60   parent.cClick();
61 EOS
62   if $id;
63
64 my $select_onclick = sub {
65   my $row = shift;
66   my $taxnum = $row->taxproductnum;
67   my $desc = $row->description;
68   "parent.document.getElementById('$id').value = $taxnum;".
69   "parent.document.getElementById('${id}_description').value = '$desc';".
70   "parent.cClick();";
71 }
72   if $id;
73
74 my $selected_part_pkg_taxproduct;
75 if ($taxproductnum) {
76   $selected_part_pkg_taxproduct =
77     qsearchs('part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum });
78 }
79
80 my $hashref = {};
81 my $extra_sql = '';
82 if ( $data_vendor ) {
83   $extra_sql .= ' WHERE data_vendor = '. dbh->quote($data_vendor);
84 }
85
86 if ($tax_group || $tax_item || $tax_customer || $tax_provider) {
87   my $compare = "LIKE '". ( $tax_group || "%" ). " : ". ( $tax_item || "%" ). " : ".
88                 ( $tax_provider || "%" ). " : ". ( $tax_customer || "%" ). "'";
89   $compare = "= '$tax_group:$tax_item:$tax_provider:$tax_customer'"
90     if ($tax_group && $tax_item && $tax_provider && $tax_customer);
91
92   $extra_sql .= ($extra_sql =~ /WHERE/ ? ' AND ' : ' WHERE ').
93                 "description $compare";
94
95 }
96 $cgi->delete('tax_group');
97 $cgi->delete('tax_item');
98 $cgi->delete('tax_provider');
99 $cgi->delete('tax_customer');
100
101
102 if ( $tax_group || $tax_item || $tax_provider || $tax_customer ) {
103   push @menubar, 'View all tax products' => $p.'browse/part_pkg_taxproduct.cgi';
104 }
105
106 $cgi->param('dummy', 1);
107
108 #restore this so pagination works
109 $cgi->param('data_vendor',  $data_vendor) if $data_vendor;
110 $cgi->param('tax_group',  $tax_group) if $tax_group;
111 $cgi->param('tax_item', $tax_item ) if $tax_item;
112 $cgi->param('tax_provider', $tax_provider ) if $tax_provider;
113 $cgi->param('tax_customer', $tax_customer ) if $tax_customer;
114
115 my $count_query = "SELECT COUNT(*) FROM part_pkg_taxproduct $extra_sql";
116
117 my @header        = ( 'Data Vendor', 'Group', 'Item', 'Provider', 'Customer' );
118 my @links         = ( $select_link,
119                       $select_link,
120                       $select_link,
121                       $select_link,
122                       $select_link,
123                     );
124 my @link_onclicks = ( $select_onclick,
125                       $select_onclick,
126                       $select_onclick,
127                       $select_onclick,
128                       $select_onclick,
129                     );
130 my $align = 'lllll';
131
132 my @fields = (
133   'data_vendor',
134   sub { shift->description =~ /^(.*):.*:.*:.*$/; $1;},
135   sub { shift->description =~ /^.*:(.*):.*:.*$/; $1;},
136   sub { shift->description =~ /^.*:.*:(.*):.*$/; $1;},
137   sub { shift->description =~ /^.*:.*:.*:(.*)$/; $1;},
138 );
139
140 my $html_init = '';
141
142 my $select_link = [ 'javascript:void(0);', sub { ''; } ];
143 $html_init = '<TABLE><TR><TD><A HREF="javascript:void(0)" '.
144                 qq!onClick="$remove_onclick">(remove)</A>&nbsp;!.
145                 'Current tax product: </TD><TD>'.
146                 $selected_part_pkg_taxproduct->description.
147                 '</TD></TR></TABLE><BR><BR>'
148   if $selected_part_pkg_taxproduct;
149
150 my $type = $cgi->param('_type');
151 $html_init .= qq(
152   <FORM>
153     <INPUT NAME="_type" TYPE="hidden" VALUE="$type">
154     <INPUT NAME="taxproductnum" TYPE="hidden" VALUE="$taxproductnum">
155     <INPUT NAME="id" TYPE="hidden" VALUE="$id">
156     <TABLE>
157       <TR>
158         <TD><SELECT NAME="data_vendor" onChange="this.form.submit()">
159 );
160
161 my $sql = "SELECT DISTINCT data_vendor FROM part_pkg_taxproduct ORDER BY data_vendor";
162 my $dbh = dbh;
163 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
164 $sth->execute or die $sth->errstr;
165 for (['(choose data vendor)'], @{$sth->fetchall_arrayref}) {
166   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
167                 ($_->[0] eq $data_vendor ? " SELECTED" : "").
168                 '">'.  $_->[0];
169 }
170 $html_init .= qq(
171         </SELECT>
172
173 <!-- cch specific -->
174         <TD><SELECT NAME="tax_group" onChange="this.form.submit()">
175 );
176
177 $sql = "SELECT DISTINCT ".
178        qq!substring(description from '#"%#" : % : % : %' for '#'),!.
179        qq!substring(description from '#"%#" : % : % : %' for '#')!.
180        "FROM part_pkg_taxproduct ORDER BY 1";
181
182 $sth = $dbh->prepare($sql) or die $dbh->errstr;
183 $sth->execute or die $sth->errstr;
184 for (['', '(choose group)'], @{$sth->fetchall_arrayref}) {
185   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
186                  ($_->[0] eq $tax_group ? " SELECTED" : "").
187                  '">'. $_->[1];
188 }
189
190 $html_init .= qq(
191         </SELECT>
192
193         <TD><SELECT NAME="tax_item" onChange="this.form.submit()">
194 );
195
196 $sql = "SELECT DISTINCT ".
197        qq!substring(description from '% : #"%#" : %: %' for '#'),!.
198        qq!substring(description from '% : #"%#" : %: %' for '#')!.
199        "FROM part_pkg_taxproduct ORDER BY 1";
200
201 $sth = $dbh->prepare($sql) or die $dbh->errstr;
202 $sth->execute or die $sth->errstr;
203 for (@{$sth->fetchall_arrayref}) {
204   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
205                  ($_->[0] eq $tax_item ? " SELECTED" : "").
206                  '">'.  ($_->[0] ? $_->[1] : '(choose item)');
207 }
208
209 $html_init .= qq(
210         </SELECT>
211
212         <TD><SELECT NAME="tax_provider" onChange="this.form.submit()">
213 );
214
215 $sql = "SELECT DISTINCT ".
216        qq!substring(description from '% : % : #"%#" : %' for '#'),!.
217        qq!substring(description from '% : % : #"%#" : %' for '#')!.
218        "FROM part_pkg_taxproduct ORDER BY 1";
219
220 $sth = $dbh->prepare($sql) or die $dbh->errstr;
221 $sth->execute or die $sth->errstr;
222 for (@{$sth->fetchall_arrayref}) {
223   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
224                  ($_->[0] eq $tax_provider ? " SELECTED" : "").
225                  '">'.  ($_->[0] ? $_->[1] : '(choose provider type)');
226 }
227
228 $html_init .= qq(
229         </SELECT>
230
231         <TD><SELECT NAME="tax_customer" onChange="this.form.submit()">
232 );
233
234 $sql = "SELECT DISTINCT ".
235        qq!substring(description from '% : % : % : #"%#"' for '#'),!.
236        qq!substring(description from '% : % : % : #"%#"' for '#')!.
237        "FROM part_pkg_taxproduct ORDER BY 1";
238
239 $sth = $dbh->prepare($sql) or die $dbh->errstr;
240 $sth->execute or die $sth->errstr;
241 for (@{$sth->fetchall_arrayref}) {
242   $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
243                  ($_->[0] eq $tax_customer ? " SELECTED" : "").
244                  '">'.  ($_->[0] ? $_->[1] : '(choose customer type)');
245 }
246
247 $html_init .= qq(
248         </SELECT>
249
250       </TR>
251     </TABLE>
252   </FORM>
253
254 );
255
256 </%init>