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