calculate_qty (Formel): Input- und Formel-Feld auch als Dom-Id übergeben können
[kivitendo-erp.git] / templates / webpages / generic / calculate_qty.html
1 [%- USE T8 %]
2 [%- USE HTML %]
3 <h1>[% title %]</h1>
4
5  <form name="Form">
6
7   <input type="hidden" name="input_name" value="[% HTML.escape(input_name) %]">
8   <input type="hidden" name="input_id"   value="[% HTML.escape(input_id) %]">
9
10   <table width="100%">
11    <tr><td>[% 'Please insert object dimensions below.' | $T8 %]</td></tr>
12
13    <tr>
14     <td>
15
16      <table>
17       <tr class="listheading">
18        [% FOREACH col = HEADER %]
19         <th nowrap class="listheading">[% col.column_title %]</a></th>
20        [% END %]
21       </tr>
22
23       [% FOREACH row = VARIABLES %]
24        <tr class="listrow[% loop.count % 2 %]">
25         <td>[% HTML.escape(row.description) %]:</td><td><input id="[% row.name %]" name="[% row.name %]" value=""></td>
26         <td>[% HTML.escape(row.unit) %]</td>
27        </tr>
28       [% END %]
29      </table>
30
31     </td>
32    </tr>
33   </table>
34  <button type="button" onclick="calculate_qty()">[% 'Calculate' | $T8 %]</button>
35  </form>
36
37  <script type="text/javascript">
38    function calculate_qty() {
39 [%- FOREACH row = VARIABLES %]
40      var [% row.name %] = parse_amount('[% MYCONFIG.numberformat %]', document.getElementsByName("[% row.name %]")[0].value);
41 [%- END %]
42      var result = [% formel %];
43      result = number_format(result, 2, '[% MYCONFIG.numberformat %]');
44      if (document.Form.input_id.value) {
45        window.opener.document.getElementById(document.Form.input_id.value).value = result;
46      } else {
47        window.opener.document.getElementsByName(document.Form.input_name.value)[0].value = result;
48      }
49      self.close();
50    }
51
52    function parse_amount(numberformat, amount) {
53      if (numberformat == '1.000,00' || numberformat == '1000,00')
54        amount = amount.replace(/\./g, "").replace(/,/, ".");
55      if (numberformat == "1'000.00")
56        amount = amount.replace(/\'/g, '');
57      return amount.replace(/,/g, '');
58    }
59
60    function number_format(number, precision, numberformat) {
61      number = Math.round( number * Math.pow(10, precision) ) / Math.pow(10, precision);
62      var nf     = numberformat.replace(/\d/g, '').split('').reverse();
63      var sep    = nf[0];
64      var th_sep = nf[1];
65
66      str_number = number+"";
67      arr_int = str_number.split(".");
68      if(!arr_int[0]) arr_int[0] = "0";
69      if(!arr_int[1]) arr_int[1] = "";
70      if(arr_int[1].length < precision) {
71        nachkomma = arr_int[1];
72        for(i=arr_int[1].length+1; i <= precision; i++) {
73          nachkomma += "0";
74        }
75        arr_int[1] = nachkomma;
76      }
77      if(th_sep != "" && arr_int[0].length > 3) {
78        raw_arr_int = arr_int[0];
79        arr_int[0] = "";
80        for(j = 3; j < raw_arr_int.length ; j+=3) {
81          arr_int[0] = th_sep + raw_arr_int.slice(raw_arr_int.length - j, raw_arr_int.length - j + 3) +  arr_int[0] + "";
82        }
83        str_first = raw_arr_int.substr(0, (raw_arr_int.length % 3 == 0) ? 3 : (raw_arr_int.length % 3));
84        arr_int[0] = str_first + arr_int[0];
85      }
86      return arr_int[0] + sep + arr_int[1];
87    }
88  </script>