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