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