don't require a leading 0 in the quick charge amount
[freeside.git] / httemplate / edit / quick-charge.html
1 <% include("/elements/header-popup.html", 'One-time charge', '',
2             ( $cgi->param('error') ? '' : 'onload="addRow()"' ),
3           )
4 %>
5
6 <% include('/elements/error.html') %>
7
8 <SCRIPT TYPE="text/javascript">
9
10 function enable_quick_charge () {
11   if (    document.QuickChargeForm.amount.value
12        && document.QuickChargeForm.pkg.value    ) {
13     document.QuickChargeForm.submit.disabled = false;
14   } else {
15     document.QuickChargeForm.submit.disabled = true;
16   }
17 }
18
19 function validate_quick_charge () {
20   var pkg = document.QuickChargeForm.pkg.value;
21   var pkg_regex = /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/ ;
22   var amount = document.QuickChargeForm.amount.value;
23   var amount_regex = /^\s*\$?\s*(\d*(\.?\d{1,2}))\s*$/ ;
24   var rval = true;
25
26   if ( ! amount_regex.test(amount) ) {
27     alert('Illegal amount - enter an amount to charge, for example, "5" or "43" or "21.46".');
28     return false;
29   }
30   if ( String(pkg).length < 1 ) {
31     rval = false;
32   }
33   if ( ! pkg_regex.test(pkg) ) {
34     rval = false;
35   }
36   var i=0;
37   for (i=0; i < rownum; i++) {
38     if (! eval('pkg_regex.test(document.QuickChargeForm.description' + i + '.value)')){
39       rval = false;
40       break;
41     }
42   }
43   if (rval == true) {
44     return true;
45   }
46
47   if ( ! pkg ) {
48     alert('Enter a description for the one-time charge');
49     return false;
50   }
51
52   alert('Illegal description - spaces, letters, numbers, and the following punctuation characters are allowed: . , ! ? @ # $ % & ( ) - + ; : ' + "'" + ' " = [ ]' );
53   return false;
54 }
55
56 </SCRIPT>
57
58 <FORM ACTION="process/quick-charge.cgi" NAME="QuickChargeForm" ID="QuickChargeForm" METHOD="POST" onsubmit="document.QuickChargeForm.submit.disabled=true;return validate_quick_charge();">
59
60 <INPUT TYPE="hidden" NAME="custnum" VALUE="<% $custnum %>">
61
62 <TABLE ID="QuickChargeTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0 STYLE="background-color: #cccccc">
63
64 <TR>
65   <TD ALIGN="right">Amount </TD>
66   <TD>
67     $<INPUT TYPE="text" NAME="amount" SIZE=6 VALUE="<% $amount %>" onChange="enable_quick_charge()" onKeyPress="enable_quick_charge()">
68   </TD>
69 </TR>
70
71 % if ( $conf->exists('invoice-unitprice') ) {
72     <TR>
73       <TD ALIGN="right">Quantity </TD>
74       <TD>
75         <INPUT TYPE="text" NAME="quantity" SIZE=4 VALUE="<% $quantity %>">
76       </TD>
77     </TR>
78 % }
79
80 <% include('/elements/tr-select-pkg_class.html', 'curr_value' => $cgi->param('classnum') ) %>
81
82
83 <TR>
84   <TD ALIGN="right">Tax exempt </TD>
85   <TD><INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <% $cgi->param('setuptax') ? 'CHECKED' : '' %>></TD>
86 </TR>
87
88 <% include('/elements/tr-select-taxclass.html', 'curr_value' => $cgi->param('taxclass') ) %>
89
90 <% include('/elements/tr-select-taxproduct.html', 'label' => 'Tax product', 'onclick' => 'parent.taxproductmagic(this);', 'curr_value' => $cgi->param('taxproductnum') ) %>
91
92 <% include('/elements/tr-select-taxoverride.html', 'onclick' => 'parent.taxoverridemagic(this);', 'curr_value' => $cgi->param('tax_override') ) %>
93
94 <TR>
95   <TD ALIGN="right">Description </TD>
96   <TD>
97     <INPUT TYPE="text" NAME="pkg" SIZE="50" MAXLENGTH="50" VALUE="<% $pkg %>" onChange="enable_quick_charge()" onKeyPress="enable_quick_charge()">
98   </TD>
99 </TR>
100
101 <TR>
102   <TD></TD>
103   <TD><FONT SIZE="-1">Optional additional description (also printed on invoice): </FONT></TD>
104 </TR>
105
106 % my $row = 0;
107 %   if ( $cgi->param('error') || $cgi->param('magic') ) {
108 %     my $param = $cgi->Vars;
109 %
110 % for ( $row = 0; exists($param->{"description$row"}); $row++ ) { 
111
112     <TR>
113       <TD></TD>
114       <TD>
115         <INPUT TYPE="text" NAME="description<% $row %>" SIZE="60" MAXLENGTH="65" VALUE="<% $param->{"description$row"} |h %>" rownum="<% $row %>" onkeyup = "possiblyAddRow;" >
116       </TD>
117     </TR>
118 % } 
119 % } 
120
121
122 </TABLE>
123
124 <BR>
125 <INPUT TYPE="submit" ID="submit" NAME="submit" VALUE="Add one-time charge" <% $cgi->param('error') ? '' :' DISABLED' %>>
126
127 </FORM>
128
129
130 <SCRIPT TYPE="text/javascript">
131
132   var rownum = <% $row %>;
133
134   function possiblyAddRow() {
135     if ( ( rownum - this.getAttribute('rownum') ) == 1 ) {
136       addRow();
137     }
138   }
139
140   function addRow() {
141
142     var table = document.getElementById('QuickChargeTable');
143     var tablebody = table.getElementsByTagName('tbody').item(0);
144
145     var row = document.createElement('TR');
146
147     var empty_cell = document.createElement('TD');
148     row.appendChild(empty_cell);
149
150     var description_cell = document.createElement('TD');
151
152       var description_input = document.createElement('INPUT');
153       description_input.setAttribute('name', 'description'+rownum);
154       description_input.setAttribute('id',   'description'+rownum);
155       description_input.setAttribute('size', 60);
156       description_input.setAttribute('maxLength', 65);
157       description_input.setAttribute('rownum',   rownum);
158       description_input.onkeyup = possiblyAddRow;
159       description_cell.appendChild(description_input);
160
161     row.appendChild(description_cell);
162
163     tablebody.appendChild(row);
164
165     rownum++;
166
167   }
168
169 </SCRIPT>
170
171 </BODY>
172 </HTML>
173 <%init>
174
175 die "access denied"
176   unless $FS::CurrentUser::CurrentUser->access_right('One-time charge');
177
178 my $conf = new FS::Conf;
179
180 $cgi->param('custnum') =~ /^(\d+)$/ or die 'illegal custnum';
181 my $custnum = $1;
182
183 my $amount = '';
184 if ( $cgi->param('amount') =~ /^\s*\$?\s*(\d+(\.\d{1,2})?)\s*$/ ) {
185   $amount = $1;
186 }
187
188 my $quantity = 1;
189 if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
190   $quantity = $1;
191 }
192
193 $cgi->param('pkg') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/ 
194   or die 'illegal description';
195 my $pkg = $1;
196
197 </%init>