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