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