scalar cgi param
[freeside.git] / httemplate / browse / part_pkg_taxproduct / suretax.html
1 <& /elements/header-popup.html, $title &>
2 <& /browse/elements/browse.html,
3   'name_singular'  => 'tax product',
4   'html_form'      => include('.form', $category_code),
5   'query'          => {
6                         'table'     => 'part_pkg_taxproduct',
7                         'hashref'   => $hashref,
8                         'order_by'  => 'ORDER BY taxproduct',
9                       },
10   'count_query'    => $count_query,
11   'header'         => \@header,
12   'fields'         => \@fields,
13   'align'          => $align,
14   'links'          => [],
15   'link_onclicks'  => \@link_onclicks,
16   'nohtmlheader'   => 1,
17   'disable_total'  => 1,
18 &>
19 <script src="<% $fsurl %>elements/jquery.js"></script>
20 <script>
21 var category_labels = <% encode_json(\%category_labels) %>;
22 $().ready(function() {
23   var new_taxproduct = $('#new_taxproduct');
24   var new_category_desc = $('#new_category_desc');
25   var new_taxproduct_desc = $('#new_taxproduct_desc');
26   var new_taxproduct_submit = $('#new_taxproduct_submit');
27
28   new_taxproduct.on('keyup', function() {
29     var curr_value = this.value || '';
30     var a = curr_value.match(/^\d{2}/);
31     var f = this.form;
32     if (a) { // there is a category code in the box
33       var category = a[0];
34       if (category_labels[category]) { // it matches an existing category
35         new_category_desc.val(category_labels[category]);
36         new_category_desc.prop('disabled', true);
37       } else {
38         new_category_desc.val('');
39         new_category_desc.prop('disabled', false);
40       }
41     } else {
42       new_category_desc.prop('disabled', true);
43     }
44     if (curr_value.match(/^\d{6}$/)) {
45       new_taxproduct_submit.prop('disabled', false);
46     } else {
47       new_taxproduct_submit.prop('disabled', true);
48     }
49   });
50
51   new_taxproduct_submit.on('click', function() {
52     select_taxproduct( -1,
53                        new_taxproduct.val()
54                           + ' '
55                           + new_category_desc.val()
56                           + ':'
57                           + new_taxproduct_desc.val()
58                      );
59   });
60 });
61 // post the values back to the parent form
62 function select_taxproduct(taxproductnum, description) {
63   parent.document.getElementById('<% $id %>').value = taxproductnum;
64   parent.document.getElementById('<% $id %>_description').value = description;
65   parent.cClick();
66 }
67   
68 </script>  
69 <BR>
70 <FORM NAME="myform">
71   <FONT SIZE="+1"><B><% emt('Add tax product') %></B></FONT>
72   <% ntable('#cccccc', 2) %>
73     <& /elements/tr-input-text.html,
74       'label'     => emt('Product code'),
75       'field'     => 'new_taxproduct',
76       'id'        => 'new_taxproduct',
77       'size'      => 6,
78       'maxlength' => 6,
79     &>
80     <& /elements/tr-input-text.html,
81       'label'     => emt('Category'),
82       'field'     => 'new_category_desc',
83       'id'        => 'new_category_desc',
84       'disabled'  => 1
85     &>
86     <& /elements/tr-input-text.html,
87       'label'     => emt('Product'),
88       'field'     => 'new_taxproduct_desc',
89       'id'        => 'new_taxproduct_desc',
90     &>
91   </table>
92   <input type="button" id="new_taxproduct_submit" disabled=1 value="Add">
93 </FORM>
94 <%shared>
95 # populate dropdown
96
97 # taxproduct is 6 digits: 2-digit category code + 4-digit detail code.
98 # Description is also two parts, corresponding to those codes, separated with
99 # a :.
100
101 my (@category_codes, @taxproduct_codes, %category_labels, %taxproduct_labels);
102 foreach my $row ( qsearch({
103   table   => 'part_pkg_taxproduct',
104   select  => 'DISTINCT substr(taxproduct, 1, 2) AS code, '.
105              "substring(description from '(.*):') AS label",
106   hashref => { data_vendor => 'suretax' },
107   }))
108 {
109   $category_labels{$row->get('code')} = $row->get('label');
110 }
111
112 @category_codes = sort {$a <=> $b} keys %category_labels;
113
114 </%shared>
115 <%def .form>
116 % my ($category_code) = @_;
117 <FORM ACTION="<% $cgi->url %>" METHOD="GET">
118 <& /elements/select.html,
119   field       => 'category_code',
120   options     => \@category_codes,
121   labels      => \%category_labels,
122   curr_value  => $category_code,
123   onchange    => 'this.form.submit()',
124 &>
125 <& /elements/hidden.html,
126   field       => 'id',
127   curr_value  => scalar($cgi->param('id')),
128 &>
129 </%def>
130 <%init>
131
132 die "access denied"
133   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
134
135 $cgi->param('id') =~ /^\w+$/ or die "missing id parameter";
136 my $id = $cgi->param('id');
137
138 my $select_onclick = sub {
139   my $row = shift;
140   my $taxnum = $row->taxproductnum;
141   my $desc = $row->taxproduct . ' ' . $row->description;
142   "select_taxproduct('$taxnum', '$desc')";
143 };
144
145 my @menubar;
146 my $title = 'Tax Products';
147
148 my $hashref = { data_vendor => 'suretax' };
149
150 my ($category_code, $taxproduct);
151 if ( $cgi->param('category_code') =~ /^(\d+)$/ ) {
152   $category_code = $1;
153   $taxproduct = $category_code . '%';
154 } else {
155   $taxproduct = '%';
156 }
157
158 $hashref->{taxproduct} = { op => 'LIKE', value => $taxproduct };
159
160 my $count_query = "SELECT COUNT(*) FROM part_pkg_taxproduct ".
161                   "WHERE data_vendor = 'suretax' AND ".
162                   "taxproduct LIKE '$taxproduct'";
163
164 my @fields = (
165   'taxproduct',
166   'description',
167   'note'
168 );
169
170 my @header = (
171   'Code',
172   'Description',
173   '',
174 );
175
176 my $align = 'lll';
177 my @link_onclicks = ( $select_onclick, $select_onclick );
178
179 </%init>