RT# 81706 Remove redundant loads of jquery.js
[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>
20 var category_labels = <% encode_json(\%category_labels) %>;
21 $().ready(function() {
22   var new_taxproduct = $('#new_taxproduct');
23   var new_category_desc = $('#new_category_desc');
24   var new_taxproduct_desc = $('#new_taxproduct_desc');
25   var new_taxproduct_submit = $('#new_taxproduct_submit');
26
27   new_taxproduct.on('keyup', function() {
28     var curr_value = this.value || '';
29     var a = curr_value.match(/^\d{2}/);
30     var f = this.form;
31     if (a) { // there is a category code in the box
32       var category = a[0];
33       if (category_labels[category]) { // it matches an existing category
34         new_category_desc.val(category_labels[category]);
35         new_category_desc.prop('disabled', true);
36       } else {
37         new_category_desc.val('');
38         new_category_desc.prop('disabled', false);
39       }
40     } else {
41       new_category_desc.prop('disabled', true);
42     }
43     if (curr_value.match(/^\d{6}$/)) {
44       new_taxproduct_submit.prop('disabled', false);
45     } else {
46       new_taxproduct_submit.prop('disabled', true);
47     }
48   });
49
50   new_taxproduct_submit.on('click', function() {
51     select_taxproduct( -1,
52                        new_taxproduct.val()
53                           + ' '
54                           + new_category_desc.val()
55                           + ':'
56                           + new_taxproduct_desc.val()
57                      );
58   });
59 });
60 // post the values back to the parent form
61 function select_taxproduct(taxproductnum, description) {
62   parent.document.getElementById('<% $id %>').value = taxproductnum;
63   parent.document.getElementById('<% $id %>_description').value = description;
64   parent.cClick();
65 }
66   
67 </script>  
68 <BR>
69 <FORM NAME="myform">
70   <FONT SIZE="+1"><B><% emt('Add tax product') %></B></FONT>
71   <% ntable('#cccccc', 2) %>
72     <& /elements/tr-input-text.html,
73       'label'     => emt('Product code'),
74       'field'     => 'new_taxproduct',
75       'id'        => 'new_taxproduct',
76       'size'      => 6,
77       'maxlength' => 6,
78     &>
79     <& /elements/tr-input-text.html,
80       'label'     => emt('Category'),
81       'field'     => 'new_category_desc',
82       'id'        => 'new_category_desc',
83       'disabled'  => 1
84     &>
85     <& /elements/tr-input-text.html,
86       'label'     => emt('Product'),
87       'field'     => 'new_taxproduct_desc',
88       'id'        => 'new_taxproduct_desc',
89     &>
90   </table>
91   <input type="button" id="new_taxproduct_submit" disabled=1 value="Add">
92 </FORM>
93 <& /elements/footer-popup.html &>
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>