Merge branch 'patch-19' of https://github.com/gjones2/Freeside
[freeside.git] / httemplate / edit / quick-charge.html
1 <& /elements/header-popup.html, mt('One-time charge'), '',
2             ( $cgi->param('error') ? '' : 'onload="addRow()"' ),
3 &>
4
5 <LINK REL="stylesheet" TYPE="text/css" HREF="<%$fsurl%>elements/calendar-win2k-2.css" TITLE="win2k-2">
6 <SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/calendar_stripped.js"></SCRIPT>
7 <SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/calendar-en.js"></SCRIPT>
8 <SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/calendar-setup.js"></SCRIPT>
9
10 <& /elements/error.html &>
11
12 <SCRIPT TYPE="text/javascript">
13
14 function enable_quick_charge (e) {
15
16   if (    document.QuickChargeForm.amount.value
17        && document.QuickChargeForm.pkg.value    ) {
18     document.QuickChargeForm.submit.disabled = false;
19   } else {
20     document.QuickChargeForm.submit.disabled = true;
21   }
22
23 % if ( $curuser->option('disable_enter_submit_onetimecharge') ) {
24
25     var key;
26     if (window.event)
27           key = window.event.keyCode; //IE
28     else
29
30           key = e.which; //firefox, others
31
32     return (key != 13);
33
34 % } else {
35     return true;
36 % }
37
38 }
39
40 function validate_quick_charge () {
41   var pkg = document.QuickChargeForm.pkg.value;
42   var pkg_regex = /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/ ;
43   var amount = document.QuickChargeForm.amount.value;
44   var amount_regex = /^\s*\$?\s*(\d*(\.?\d{1,2}))\s*$/ ;
45   var rval = true;
46
47   if ( ! amount_regex.test(amount) ) {
48     alert('Illegal amount - enter an amount to charge, for example, "5" or "43" or "21.46".');
49     return false;
50   }
51   if ( String(pkg).length < 1 ) {
52     rval = false;
53   }
54   if ( ! pkg_regex.test(pkg) ) {
55     rval = false;
56   }
57   var i=0;
58   for (i=0; i < rownum; i++) {
59     if (! eval('pkg_regex.test(document.QuickChargeForm.description' + i + '.value)')){
60       rval = false;
61       break;
62     }
63   }
64   if (rval == true) {
65     return true;
66   }
67
68   if ( ! pkg ) {
69     alert('Enter a description for the one-time charge');
70     return false;
71   }
72
73   alert('Illegal description - spaces, letters, numbers, and the following punctuation characters are allowed: . , ! ? @ # $ % & ( ) - + ; : ' + "'" + ' " = [ ]' );
74   return false;
75 }
76
77 function bill_now_changed (what) {
78   var form = what.form;
79   if ( what.checked ) {
80     form.start_date_text.disabled = true;
81     form.start_date.style.backgroundColor = '#dddddd';
82     form.start_date_button.style.display = 'none';
83     form.start_date_button_disabled.style.display = '';
84     form.invoice_terms.disabled = false;
85   } else {
86     form.start_date_text.disabled = false;
87     form.start_date.style.backgroundColor = '#ffffff';
88     form.start_date_button.style.display = '';
89     form.start_date_button_disabled.style.display = 'none';
90     form.invoice_terms.disabled = true;
91   }
92 }
93
94 </SCRIPT>
95
96 <FORM ACTION   = "process/quick-charge.cgi"
97       NAME     = "QuickChargeForm"
98       ID       = "QuickChargeForm"
99       METHOD   = "POST"
100       onSubmit = "document.QuickChargeForm.submit.disabled=true; return validate_quick_charge();"
101 >
102
103 <INPUT TYPE="hidden" NAME="custnum" VALUE="<% $custnum %>">
104
105 <TABLE ID="QuickChargeTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0 STYLE="background-color: #cccccc">
106
107 <TR>
108   <TD ALIGN="right"><% mt('Amount') |h %> </TD>
109   <TD>
110     <% $money_char %><INPUT TYPE       = "text"
111                             NAME       = "amount"
112                             SIZE       = 6
113                             VALUE      = "<% $amount %>"
114                             onChange   = "return enable_quick_charge(event)"
115                             onKeyPress = "return enable_quick_charge(event)"
116                      >
117   </TD>
118 </TR>
119
120 % if ( $conf->exists('invoice-unitprice') ) {
121     <TR>
122       <TD ALIGN="right"><% mt('Quantity') |h %> </TD>
123       <TD>
124         <INPUT TYPE       = "text"
125                NAME       = "quantity"
126                SIZE       = 4
127                VALUE      = "<% $quantity %>"
128                onKeyPress = "return enable_quick_charge(event)">
129       </TD>
130     </TR>
131 % }
132
133 <& /elements/tr-select-pkg_class.html, 'curr_value' => $cgi->param('classnum')  &>
134
135 <TR>
136   <TD ALIGN="right"><% mt('Invoice now') |h %></TD>
137   <TD>
138     <INPUT TYPE  = "checkbox"
139            NAME  = "bill_now"
140            VALUE = "1"
141            <% $cgi->param('bill_now') ? 'CHECKED' : '' %>
142            onClick  = "bill_now_changed(this);"
143            onChange = "bill_now_changed(this);"
144     >
145     <% mt('with terms') |h %> 
146     <& /elements/select-terms.html,
147                  'curr_value'  => scalar($cgi->param('invoice_terms')),
148                  'disabled'    => ( $cgi->param('bill_now') ? 0 : 1 ),
149     &>
150   </TD>
151 </TR>
152
153 %# false laziness w/misc/order_pkg.html
154 <TR>
155   <TD ALIGN="right"><% mt('Charge date') |h %> </TD>
156   <TD>
157     <INPUT TYPE  = "text"
158            NAME  = "start_date"
159            SIZE  = 32
160            ID    = "start_date_text"
161            VALUE = "<% $start_date %>"
162            onKeyPress="return enable_quick_charge(event)"
163            <% $cgi->param('bill_now')
164                 ? 'STYLE = "background-color:#dddddd" DISABLED'
165                 : ''
166            %>
167     >
168     <IMG SRC   = "<%$fsurl%>images/calendar.png"
169          ID    = "start_date_button"
170          TITLE = "<% mt('Select date') |h %>"
171          STYLE = "cursor:pointer<% $cgi->param('bill_now') ? ';display:none' : '' %>"
172     >
173     <IMG SRC   = "<%$fsurl%>images/calendar-disabled.png"
174          ID    = "start_date_button_disabled"
175          <% $cgi->param('bill_now') ? '' : 'STYLE="display:none"' %>
176     >
177     <FONT SIZE=-1>(<% mt('leave blank to charge immediately') |h %>)</FONT>
178   </TD>
179 </TR>
180
181 <SCRIPT TYPE="text/javascript">
182   Calendar.setup({
183     inputField: "start_date_text",
184     ifFormat:   "<% $date_format %>",
185     button:     "start_date_button",
186     align:      "BR"
187   });
188 </SCRIPT>
189
190 % if ( $cust_main->payby =~ /^(CARD|CHEK)$/ ) {
191 %   my $what = lc(FS::payby->shortname($cust_main->payby));
192     <TR>
193       <TD ALIGN="right"><% mt("Disable automatic $what charge") |h %> </TD>
194       <TD COLSPAN=6><INPUT TYPE="checkbox" NAME="no_auto" VALUE="Y"></TD>
195     </TR>
196 % }
197
198 <TR>
199   <TD ALIGN="right"><% mt('Tax exempt') |h %> </TD>
200   <TD><INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <% $cgi->param('setuptax') ? 'CHECKED' : '' %>></TD>
201 </TR>
202
203 <& /elements/tr-select-taxclass.html, 'curr_value' => $cgi->param('taxclass')  &>
204
205 <& /elements/tr-select-taxproduct.html, 'label' => emt('Tax product'), 'onclick' => 'parent.taxproductmagic(this);', 'curr_value' => $cgi->param('taxproductnum')  &>
206
207 <& /elements/tr-select-taxoverride.html, 'onclick' => 'parent.taxoverridemagic(this);', 'curr_value' => $cgi->param('tax_override')  &>
208
209 <TR>
210   <TD ALIGN="right"><% mt('Description') |h %> </TD>
211   <TD>
212     <INPUT TYPE       = "text"
213            NAME       = "pkg"
214            SIZE       = "50"
215            MAXLENGTH  = "50"
216            VALUE      = "<% $pkg %>"
217            onChange   = "return enable_quick_charge(event)"
218            onKeyPress = "return enable_quick_charge(event)"
219     >
220   </TD>
221 </TR>
222
223 <TR>
224   <TD></TD>
225   <TD><FONT SIZE="-1"><% mt('Optional additional description (also printed on invoice):') |h %> </FONT></TD>
226 </TR>
227
228 % my $row = 0;
229 %   if ( $cgi->param('error') || $cgi->param('magic') ) {
230 %     my $param = $cgi->Vars;
231 %
232 % for ( $row = 0; exists($param->{"description$row"}); $row++ ) { 
233
234     <TR>
235       <TD></TD>
236       <TD>
237         <INPUT TYPE       = "text"
238                NAME       = "description<% $row %>"
239                SIZE       = "60"
240                MAXLENGTH  = "65"
241                VALUE      = "<% $param->{"description$row"} |h %>"
242                rownum     = "<% $row %>"
243                onKeyPress = "return enable_quick_charge(event)"
244                onKeyUp    = "return possiblyAddRow(event)"
245         >
246       </TD>
247     </TR>
248 % } 
249 % } 
250
251
252 </TABLE>
253
254 <BR>
255 <INPUT TYPE="submit" ID="submit" NAME="submit" VALUE="<% mt('Add one-time charge') |h %>" <% $cgi->param('error') ? '' :' DISABLED' %>>
256
257 </FORM>
258
259
260 <SCRIPT TYPE="text/javascript">
261
262   var rownum = <% $row %>;
263
264   function possiblyAddRow(e) {
265
266     if ( ( rownum - this.getAttribute('rownum') ) == 1 ) {
267       addRow();
268     }
269
270 %   if ( $curuser->option('disable_enter_submit_onetimecharge') ) {
271
272       var key;
273       if (window.event)
274             key = window.event.keyCode; //IE
275       else
276             key = e.which; //firefox, others
277
278       return (key != 13);
279
280 %   } else {
281       return true;
282 %   }
283
284   }
285
286   function addRow() {
287
288     var table = document.getElementById('QuickChargeTable');
289     var tablebody = table.getElementsByTagName('tbody').item(0);
290
291     var row = document.createElement('TR');
292
293     var empty_cell = document.createElement('TD');
294     row.appendChild(empty_cell);
295
296     var description_cell = document.createElement('TD');
297
298       //var description_input = document.createElement('INPUT');
299       var di = document.createElement('INPUT');
300       di.setAttribute('name', 'description'+rownum);
301       di.setAttribute('id',   'description'+rownum);
302       di.setAttribute('size', 60);
303       di.setAttribute('maxLength', 65);
304       di.setAttribute('rownum',   rownum);
305       di.onkeyup = possiblyAddRow;
306       di.onkeypress = enable_quick_charge;
307       description_cell.appendChild(di);
308
309     row.appendChild(description_cell);
310
311     tablebody.appendChild(row);
312
313     rownum++;
314
315   }
316
317 </SCRIPT>
318
319 </BODY>
320 </HTML>
321 <%init>
322
323 my $curuser = $FS::CurrentUser::CurrentUser;
324
325 die "access denied"
326   unless $curuser->access_right('One-time charge');
327
328 my $conf = new FS::Conf;
329 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
330 my $money_char = $conf->config('money_char') || '$';
331
332 $cgi->param('custnum') =~ /^(\d+)$/ or die 'illegal custnum';
333 my $custnum = $1;
334 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ); #XXX agent-virt
335
336 my $format = "%m/%d/%Y %T %z (%Z)"; #false laziness w/REAL_cust_pkg.cgi?
337 my $start_date = $cust_main->next_bill_date;
338 $start_date = $start_date ? time2str($format, $start_date) : '';
339
340 my $amount = '';
341 if ( $cgi->param('amount') =~ /^\s*\$?\s*(\d+(\.\d{1,2})?)\s*$/ ) {
342   $amount = $1;
343 }
344
345 my $quantity = 1;
346 if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
347   $quantity = $1;
348 }
349
350 $cgi->param('pkg') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/ 
351   or die 'illegal description';
352 my $pkg = $1;
353
354 my $default_terms;
355 if ( $cust_main->invoice_terms ) {
356   $default_terms = emt("Customer default ([_1])", $cust_main->invoice_terms);
357 } else {
358   $default_terms = emt("Default ([_1])",
359             ($conf->config('invoice_default_terms') || emt('Payable upon receipt'))
360                       );
361 }
362
363 </%init>