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