allow unicode letters in one-time charge description, #72175
[freeside.git] / httemplate / edit / quick-charge.html
1 <& /elements/header-popup.html, mt('One-time charge'), '',
2             ( ($quotationnum || $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 <SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/xregexp-all.js"></SCRIPT>
10
11 <& /elements/error.html &>
12
13 <SCRIPT TYPE="text/javascript">
14
15 function enable_quick_charge (e) {
16
17   if (    document.QuickChargeForm.amount.value
18        && document.QuickChargeForm.pkg.value    ) {
19     document.QuickChargeForm.submit.disabled = false;
20   } else {
21     document.QuickChargeForm.submit.disabled = true;
22   }
23
24 % if ( $curuser->option('disable_enter_submit_onetimecharge') ) {
25
26     var key;
27     if (window.event)
28           key = window.event.keyCode; //IE
29     else
30
31           key = e.which; //firefox, others
32
33     return (key != 13);
34
35 % } else {
36     return true;
37 % }
38
39 }
40
41 function validate_quick_charge () {
42   var pkg = document.QuickChargeForm.pkg.value;
43   var pkg_regex = XRegExp('^([\\p{L}\\p{N} \_\!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\\[\\]]*)$');
44   var amount = document.QuickChargeForm.amount.value;
45   var amount_regex = /^\s*\$?\s*(\d*(\.?\d{1,2}))\s*$/ ;
46   var rval = true;
47
48   if ( ! amount_regex.test(amount) ) {
49     alert('Illegal amount - enter an amount to charge, for example, "5" or "43" or "21.46".');
50     return false;
51   }
52   if ( String(pkg).length < 1 ) {
53     rval = false;
54   }
55   if ( ! pkg_regex.test(pkg) ) {
56     rval = false;
57   }
58   var i=0;
59   for (i=0; i < rownum; i++) {
60     if (! eval('pkg_regex.test(document.QuickChargeForm.description' + i + '.value)')){
61       rval = false;
62       break;
63     }
64   }
65   if (rval == true) {
66     return true;
67   }
68
69   if ( ! pkg ) {
70     alert('Enter a description for the one-time charge');
71     return false;
72   }
73
74   alert('Illegal description - spaces, letters, numbers, and the following punctuation characters are allowed: . , ! ? @ # $ % & ( ) - + ; : ' + "'" + ' " = [ ]' );
75   return false;
76 }
77
78 function bill_now_changed (what) {
79   var form = what.form;
80   if ( what.checked ) {
81     form.start_date_text.disabled = true;
82     form.start_date.style.backgroundColor = '#dddddd';
83     form.start_date_button.style.display = 'none';
84     form.start_date_button_disabled.style.display = '';
85     form.invoice_terms.disabled = false;
86   } else {
87     form.start_date_text.disabled = false;
88     form.start_date.style.backgroundColor = '#ffffff';
89     form.start_date_button.style.display = '';
90     form.start_date_button_disabled.style.display = 'none';
91     form.invoice_terms.disabled = true;
92   }
93 }
94
95 </SCRIPT>
96
97 <FORM ACTION   = "process/quick-charge.cgi"
98       NAME     = "QuickChargeForm"
99       ID       = "QuickChargeForm"
100       METHOD   = "POST"
101       onSubmit = "document.QuickChargeForm.submit.disabled=true; return validate_quick_charge();"
102 >
103
104 <INPUT TYPE="hidden" NAME="custnum"     VALUE="<% $cust_main ? $cust_main->custnum : '' %>">
105 <INPUT TYPE="hidden" NAME="prospectnum" VALUE="<% $prospect_main ? $prospect_main->prospectnum : '' %>">
106 <INPUT TYPE="hidden" NAME="quotationnum" VALUE="<% $quotationnum %>">
107
108 <TABLE ID="QuickChargeTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0 STYLE="background-color: #cccccc">
109
110 % if ( $cust_pkg ) { #modify one-time charge
111
112 <INPUT TYPE="hidden" NAME="pkgnum" VALUE="<% $cust_pkg->pkgnum %>">
113 % my $field = '/elements/tr-input-text.html';
114 % # don't allow changing these after the fact
115 % $field = '/elements/tr-fixed.html' if $billed;
116 <& $field,
117   label  => mt('Amount to charge'),
118   field  => 'amount',
119   value  => sprintf('%.2f',$part_pkg->option('setup_fee')),
120   size   => 8,
121   prefix => $money_char,
122 &> 
123
124 % if ( $curuser->access_right('Edit package definition costs') ) {
125   <& $field,
126     label  => mt('Cost'),
127     field  => 'setup_cost',
128     value  => sprintf('%.2f',$part_pkg->setup_cost),
129     size   => 8,
130     prefix => $money_char,
131   &> 
132 % }
133
134 %   if ( $conf->exists('invoice-unitprice') ) {
135 <& $field,
136   label => 'Quantity',
137   field => 'quantity',
138   value => $cust_pkg->quantity
139 &>
140 %   }
141
142 <& /elements/tr-select-pkg_class.html, 'curr_value' => $classnum  &>
143
144 % # crudely estimate whether any agent commission credits might exist
145 %   my @events = grep { $_->part_event->action =~ /credit/ }
146 %                $cust_pkg->cust_event;
147 %   if ( scalar @events ) {
148 <TR><TD></TD>
149   <TD><INPUT TYPE="checkbox" NAME="adjust_commission" VALUE="Y" CHECKED>
150 <% emt('Adjust commission credits if necessary') %>
151 </TD>
152 </TR>
153 %   }
154
155 % #display the future or past charge date, but don't allow changes
156 % # XXX we probably _could_ let as-yet unbilled charges be rescheduled, but
157 % # there's no compelling need yet
158 %   if ( $billed ) {
159       <& /elements/tr-fixed-date.html,
160         label => emt('Billed on'),
161         value => $cust_pkg->get('setup')
162       &>
163 %   } else {
164       <& /elements/tr-input-date-field.html,
165         {
166           name    => 'start_date',
167           label   => emt('Will be billed'),
168           value   => $cust_pkg->get('start_date'),
169           format  => $date_format,
170           noinit  => 1,
171         }
172       &>
173
174       <& /elements/tr-checkbox.html,
175         label => emt('Invoice this charge separately'),
176         field => 'separate_bill',
177         value => 'Y',
178         curr_value => $cust_pkg->get('separate_bill'),
179       &>
180       <TR>
181         <TD ALIGN="right"><% mt('Tax exempt') |h %> </TD>
182         <TD><INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <% $cgi->param('setuptax') ? 'CHECKED' : '' %>></TD>
183       </TR>
184
185       <& /elements/tr-select-taxclass.html, 'curr_value' => $part_pkg->get('taxclass')  &>
186
187       <& /elements/tr-select-taxproduct.html, 'label' => emt('Tax product'), 'onclick' => 'parent.taxproductmagic(this);', 'curr_value' => $part_pkg->get('taxproductnum')  &>
188 % }
189
190 % } else { # new one-time charge
191
192     <TR>
193       <TD ALIGN="right"><% mt('Amount to charge') |h %> </TD>
194       <TD>
195         <% $money_char %><INPUT TYPE       = "text"
196                                 NAME       = "amount"
197                                 SIZE       = 8
198                                 VALUE      = "<% $amount %>"
199                                 onChange   = "return enable_quick_charge(event)"
200                                 onKeyPress = "return enable_quick_charge(event)"
201                          >
202       </TD>
203     </TR>
204
205 %   if ( $curuser->access_right('Edit package definition costs') ) {
206       <& /elements/tr-input-text.html,
207            label  => mt('Cost'),
208            field  => 'setup_cost',
209            value  => $setup_cost,
210            size   => 8,
211            prefix => $money_char,
212       &> 
213 %   }
214
215 %   if ( $conf->exists('invoice-unitprice') ) {
216     <TR>
217       <TD ALIGN="right"><% mt('Quantity') |h %> </TD>
218       <TD>
219         <INPUT TYPE       = "text"
220                NAME       = "quantity"
221                SIZE       = 4
222                VALUE      = "<% $quantity %>"
223                onKeyPress = "return enable_quick_charge(event)">
224       </TD>
225     </TR>
226 %   }
227
228 <& /elements/tr-select-pkg_class.html, 'curr_value' => $classnum  &>
229
230 % unless ( $quotationnum ) {
231
232     <TR>
233       <TD ALIGN="right"><% mt('Invoice now') |h %></TD>
234       <TD>
235         <INPUT TYPE  = "checkbox"
236                NAME  = "bill_now"
237                VALUE = "1"
238                <% $cgi->param('bill_now') ? 'CHECKED' : '' %>
239                onClick  = "bill_now_changed(this);"
240                onChange = "bill_now_changed(this);"
241         >
242         <% mt('with terms') |h %> 
243         <& /elements/select-terms.html,
244              'curr_value' => scalar($cgi->param('invoice_terms')),
245              'disabled'   => ( $cgi->param('bill_now') ? 0 : 1 ),
246              'agentnum'   => $cust_or_prospect->agentnum,
247         &>
248       </TD>
249     </TR>
250
251 %   # false laziness w/misc/order_pkg.html
252     <TR>
253       <TD ALIGN="right"><% mt('Charge date') |h %> </TD>
254       <TD>
255         <INPUT TYPE  = "text"
256                NAME  = "start_date"
257                SIZE  = 32
258                ID    = "start_date_text"
259                VALUE = "<% $start_date %>"
260                onKeyPress="return enable_quick_charge(event)"
261                <% $cgi->param('bill_now')
262                     ? 'STYLE = "background-color:#dddddd" DISABLED'
263                     : ''
264                %>
265         >
266         <IMG SRC   = "<%$fsurl%>images/calendar.png"
267              ID    = "start_date_button"
268              TITLE = "<% mt('Select date') |h %>"
269              STYLE = "cursor:pointer<% $cgi->param('bill_now') ? ';display:none' : '' %>"
270         >
271         <IMG SRC   = "<%$fsurl%>images/calendar-disabled.png"
272              ID    = "start_date_button_disabled"
273              <% $cgi->param('bill_now') ? '' : 'STYLE="display:none"' %>
274         >
275         <FONT SIZE=-1>(<% mt('leave blank to charge immediately') |h %>)</FONT>
276       </TD>
277     </TR>
278
279     <SCRIPT TYPE="text/javascript">
280       Calendar.setup({
281         inputField: "start_date_text",
282         ifFormat:   "<% $date_format %>",
283         button:     "start_date_button",
284         align:      "BR"
285       });
286     </SCRIPT>
287
288 <& /elements/tr-checkbox.html,
289   label => emt('Invoice this charge separately'),
290   field => 'separate_bill',
291   value => 'Y'
292 &>
293
294 % }
295
296 % if ( ! $quotationnum && $cust_main->payby =~ /^(CARD|CHEK)$/ ) {
297 %   my $what = lc(FS::payby->shortname($cust_main->payby));
298     <TR>
299       <TD ALIGN="right"><% mt("Disable automatic $what charge") |h %> </TD>
300       <TD COLSPAN=6><INPUT TYPE="checkbox" NAME="no_auto" VALUE="Y"></TD>
301     </TR>
302 % }
303
304 <TR>
305   <TD ALIGN="right"><% mt('Tax exempt') |h %> </TD>
306   <TD><INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <% $cgi->param('setuptax') ? 'CHECKED' : '' %>></TD>
307 </TR>
308
309 <& /elements/tr-select-taxclass.html, 'curr_value' => $cgi->param('taxclass')  &>
310
311 <& /elements/tr-select-taxproduct.html, 'label' => emt('Tax product'), 'onclick' => 'parent.taxproductmagic(this);', 'curr_value' => $cgi->param('taxproductnum')  &>
312
313 <& /elements/tr-select-taxoverride.html, 'onclick' => 'parent.taxoverridemagic(this);', 'curr_value' => $cgi->param('tax_override')  &>
314
315 % } # if !$cust_pkg
316
317 <TR>
318   <TD ALIGN="right"><% mt('Description') |h %> </TD>
319   <TD>
320     <INPUT TYPE       = "text"
321            NAME       = "pkg"
322            SIZE       = "50"
323            MAXLENGTH  = "50"
324            VALUE      = "<% $pkg %>"
325            onChange   = "return enable_quick_charge(event)"
326            onKeyPress = "return enable_quick_charge(event)"
327     >
328   </TD>
329 </TR>
330
331 % my $row = 0;
332 % # quotation details are handled by quotation_pkg_detail records, added via link from view/quotation.html
333 % # the details below get attached to the part_pkg record, and there's no way to edit that from quotations
334 % unless ($quotationnum) {
335 <TR>
336   <TD></TD>
337   <TD><FONT SIZE="-1"><% mt('Optional additional description (also printed on invoice):') |h %> </FONT></TD>
338 </TR>
339
340 %   foreach (@description) {
341     <TR>
342       <TD></TD>
343       <TD>
344         <INPUT TYPE       = "text"
345                NAME       = "description<% $row %>"
346                SIZE       = "60"
347                MAXLENGTH  = "65"
348                VALUE      = "<% $_ |h %>"
349                rownum     = "<% $row %>"
350                onKeyPress = "return enable_quick_charge(event)"
351                onKeyUp    = "return possiblyAddRow(event)"
352         >
353       </TD>
354     </TR>
355 %     $row++;
356 %   }
357 % }
358
359
360 </TABLE>
361
362 <BR>
363 % my $label = $cust_pkg
364 %             ? emt('Modify one-time charge')
365 %             : emt('Add one-time charge');
366 <INPUT TYPE="submit" ID="submit" NAME="submit" VALUE="<% $label %>" \
367 <% ($cgi->param('error') || $cust_pkg) ? '' :' DISABLED' %>>
368
369 </FORM>
370
371
372 <SCRIPT TYPE="text/javascript">
373
374   var rownum = <% $row %>;
375
376   function possiblyAddRow(e) {
377
378     if ( ( rownum - this.getAttribute('rownum') ) == 1 ) {
379       addRow();
380     }
381
382 %   if ( $curuser->option('disable_enter_submit_onetimecharge') ) {
383
384       var key;
385       if (window.event)
386             key = window.event.keyCode; //IE
387       else
388             key = e.which; //firefox, others
389
390       return (key != 13);
391
392 %   } else {
393       return true;
394 %   }
395
396   }
397
398   function addRow() {
399
400     var table = document.getElementById('QuickChargeTable');
401     var tablebody = table.getElementsByTagName('tbody').item(0);
402
403     var row = document.createElement('TR');
404
405     var empty_cell = document.createElement('TD');
406     row.appendChild(empty_cell);
407
408     var description_cell = document.createElement('TD');
409
410       //var description_input = document.createElement('INPUT');
411       var di = document.createElement('INPUT');
412       di.setAttribute('name', 'description'+rownum);
413       di.setAttribute('id',   'description'+rownum);
414       di.setAttribute('size', 60);
415       di.setAttribute('maxLength', 65);
416       di.setAttribute('rownum',   rownum);
417       di.onkeyup = possiblyAddRow;
418       di.onkeypress = enable_quick_charge;
419       description_cell.appendChild(di);
420
421     row.appendChild(description_cell);
422
423     tablebody.appendChild(row);
424
425     rownum++;
426
427   }
428
429 </SCRIPT>
430
431 </BODY>
432 </HTML>
433 <%init>
434
435 my $curuser = $FS::CurrentUser::CurrentUser;
436
437 die "access denied"
438   unless $curuser->access_right('One-time charge');
439
440 my $conf = new FS::Conf;
441 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
442 my $money_char = $conf->config('money_char') || '$';
443
444 my( $cust_main, $cust_pkg, $prospect_main, $quotationnum ) = ( '', '', '', '' );
445 if ( $cgi->param('change_pkgnum') ) {
446   # change an existing one-time charge
447   die "access denied"
448     unless $curuser->access_right('Modify one-time charge');
449
450   $cgi->param('change_pkgnum') =~ /^(\d+)$/ or die "illegal pkgnum";
451   $cust_pkg = FS::cust_pkg->by_key($1) or die "pkgnum $1 not found";
452   $cust_main = $cust_pkg->cust_main;
453 } else {
454   if ( $cgi->param('quotationnum') =~ /^(\d+)$/ ) {
455     $quotationnum = $1;
456   }
457   if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
458     $cust_main = FS::cust_main->by_key($1) or die "custnum $1 not found";
459   }
460   if ( $cgi->param('prospectnum') =~ /^(\d+)$/ ) {
461     $prospect_main = FS::prospect_main->by_key($1) or die "prospectnum $1 not found";
462   }
463   die "custnum or prospectnum must be specified"
464     unless $cust_main || $prospect_main;
465 }
466
467 my $cust_or_prospect = $cust_main || $prospect_main;
468
469 if ( $cust_main ) {
470   my $custnum = $cust_main->custnum;
471   # agent-virt
472   if (!exists($curuser->agentnums_href->{$cust_main->agentnum})) {
473     die "custnum $custnum not found";
474   }
475 } elsif ( $prospect_main ) {
476   my $prospectnum = $prospect_main->prospectnum;
477   # agent-virt
478   if (!exists($curuser->agentnums_href->{$prospect_main->agentnum})) {
479     die "prospectnum $prospectnum not found";
480   }
481 }
482
483 my $format = "%m/%d/%Y %T %z (%Z)"; #false laziness w/REAL_cust_pkg.cgi?
484 my $start_date = $cust_main ? $cust_main->next_bill_date : '';
485 $start_date = $start_date ? time2str($format, $start_date) : '';
486
487 my $amount = '';
488 if ( $cgi->param('amount') =~ /^\s*\$?\s*(\d+(\.\d{1,2})?)\s*$/ ) {
489   $amount = $1;
490 }
491
492 my $setup_cost = '';
493 if ( $cgi->param('setup_cost') =~ /^\s*\$?\s*(\d+(\.\d{1,2})?)\s*$/ ) {
494   $setup_cost = $1;
495 }
496
497 my $quantity = 1;
498 if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
499   $quantity = $1;
500 }
501
502 $cgi->param('pkg') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/ 
503   or die 'illegal description';
504 my $pkg = $1;
505
506 my $default_terms;
507 if ( $cust_main && $cust_main->invoice_terms ) {
508   $default_terms = emt("Customer default ([_1])", $cust_main->invoice_terms);
509 } else {
510   $default_terms =
511     emt( "Default ([_1])",
512          ( $conf->config('invoice_default_terms', $cust_or_prospect->agentnum)
513              || emt('Payable upon receipt')
514          )
515        );
516 }
517
518 my @description;
519 my %param = $cgi->Vars;
520 for (my $i = 0; exists($param{"description$i"}); $i++) {
521   push @description, $param{"description$i"};
522 }
523
524 my $classnum;
525 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
526   $classnum = $1;
527 }
528
529 my $part_pkg;
530
531 if ( $cust_pkg ) { # set defaults
532   $part_pkg = $cust_pkg->part_pkg;
533   $pkg ||= $part_pkg->pkg;
534   $classnum ||= $part_pkg->classnum;
535   if (!@description) {
536     for (my $i = 0; $i < ($part_pkg->option('additional_count',1) || 0); $i++) 
537     {
538       push @description, $part_pkg->option("additional_info$i",1);
539     }
540   }
541 }
542
543 my $billed = ($cust_pkg and $cust_pkg->get('setup')) ? 1 : 0;
544
545 </%init>