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