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