summaryrefslogtreecommitdiff
path: root/httemplate/browse/part_pkg_taxproduct/suretax.html
blob: 178062c4a1ee85053918a14c42954ce73ca7f055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<& /elements/header-popup.html, $title &>
<& /browse/elements/browse.html,
  'name_singular'  => 'tax product',
  'html_form'      => include('.form', $category_code),
  'query'          => {
                        'table'     => 'part_pkg_taxproduct',
                        'hashref'   => $hashref,
                        'order_by'  => 'ORDER BY taxproduct',
                      },
  'count_query'    => $count_query,
  'header'         => \@header,
  'fields'         => \@fields,
  'align'          => $align,
  'links'          => [],
  'link_onclicks'  => \@link_onclicks,
  'nohtmlheader'   => 1,
  'disable_total'  => 1,
&>
<script src="<% $fsurl %>elements/jquery.js"></script>
<script>
var category_labels = <% encode_json(\%category_labels) %>;
$().ready(function() {
  var new_taxproduct = $('#new_taxproduct');
  var new_category_desc = $('#new_category_desc');
  var new_taxproduct_desc = $('#new_taxproduct_desc');
  var new_taxproduct_submit = $('#new_taxproduct_submit');

  new_taxproduct.on('keyup', function() {
    var curr_value = this.value || '';
    var a = curr_value.match(/^\d{2}/);
    var f = this.form;
    if (a) { // there is a category code in the box
      var category = a[0];
      if (category_labels[category]) { // it matches an existing category
        new_category_desc.val(category_labels[category]);
        new_category_desc.prop('disabled', true);
      } else {
        new_category_desc.val('');
        new_category_desc.prop('disabled', false);
      }
    } else {
      new_category_desc.prop('disabled', true);
    }
    if (curr_value.match(/^\d{6}$/)) {
      new_taxproduct_submit.prop('disabled', false);
    } else {
      new_taxproduct_submit.prop('disabled', true);
    }
  });

  new_taxproduct_submit.on('click', function() {
    select_taxproduct( -1,
                       new_taxproduct.val()
                          + ' '
                          + new_category_desc.val()
                          + ':'
                          + new_taxproduct_desc.val()
                     );
  });
});
// post the values back to the parent form
function select_taxproduct(taxproductnum, description) {
  parent.document.getElementById('<% $id %>').value = taxproductnum;
  parent.document.getElementById('<% $id %>_description').value = description;
  parent.cClick();
}
  
</script>  
<BR>
<FORM NAME="myform">
  <FONT SIZE="+1"><B><% emt('Add tax product') %></B></FONT>
  <% ntable('#cccccc', 2) %>
    <& /elements/tr-input-text.html,
      'label'     => emt('Product code'),
      'field'     => 'new_taxproduct',
      'id'        => 'new_taxproduct',
      'size'      => 6,
      'maxlength' => 6,
    &>
    <& /elements/tr-input-text.html,
      'label'     => emt('Category'),
      'field'     => 'new_category_desc',
      'id'        => 'new_category_desc',
      'disabled'  => 1
    &>
    <& /elements/tr-input-text.html,
      'label'     => emt('Product'),
      'field'     => 'new_taxproduct_desc',
      'id'        => 'new_taxproduct_desc',
    &>
  </table>
  <input type="button" id="new_taxproduct_submit" disabled=1 value="Add">
</FORM>
<%shared>
# populate dropdown

# taxproduct is 6 digits: 2-digit category code + 4-digit detail code.
# Description is also two parts, corresponding to those codes, separated with
# a :.

my (@category_codes, @taxproduct_codes, %category_labels, %taxproduct_labels);
foreach my $row ( qsearch({
  table   => 'part_pkg_taxproduct',
  select  => 'DISTINCT substr(taxproduct, 1, 2) AS code, '.
             "substring(description from '(.*):') AS label",
  hashref => { data_vendor => 'suretax' },
  }))
{
  $category_labels{$row->get('code')} = $row->get('label');
}

@category_codes = sort {$a <=> $b} keys %category_labels;

</%shared>
<%def .form>
% my ($category_code) = @_;
<FORM ACTION="<% $cgi->url %>" METHOD="GET">
<& /elements/select.html,
  field       => 'category_code',
  options     => \@category_codes,
  labels      => \%category_labels,
  curr_value  => $category_code,
  onchange    => 'this.form.submit()',
&>
<& /elements/hidden.html,
  field       => 'id',
  curr_value  => scalar($cgi->param('id')),
&>
</%def>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');

$cgi->param('id') =~ /^\w+$/ or die "missing id parameter";
my $id = $cgi->param('id');

my $select_onclick = sub {
  my $row = shift;
  my $taxnum = $row->taxproductnum;
  my $desc = $row->taxproduct . ' ' . $row->description;
  "select_taxproduct('$taxnum', '$desc')";
};

my @menubar;
my $title = 'Tax Products';

my $hashref = { data_vendor => 'suretax' };

my ($category_code, $taxproduct);
if ( $cgi->param('category_code') =~ /^(\d+)$/ ) {
  $category_code = $1;
  $taxproduct = $category_code . '%';
} else {
  $taxproduct = '%';
}

$hashref->{taxproduct} = { op => 'LIKE', value => $taxproduct };

my $count_query = "SELECT COUNT(*) FROM part_pkg_taxproduct ".
                  "WHERE data_vendor = 'suretax' AND ".
                  "taxproduct LIKE '$taxproduct'";

my @fields = (
  'taxproduct',
  'description',
  'note'
);

my @header = (
  'Code',
  'Description',
  '',
);

my $align = 'lll';
my @link_onclicks = ( $select_onclick, $select_onclick );

</%init>