Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / edit / quick-charge.html
1 <& /elements/header-popup.html, mt('One-time charge'), '',
2             ( $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
174 %               unless ($billed) {
175 <TR>
176   <TD ALIGN="right"><% mt('Tax exempt') |h %> </TD>
177   <TD><INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <% $cgi->param('setuptax') ? 'CHECKED' : '' %>></TD>
178 </TR>
179
180 <& /elements/tr-select-taxclass.html, 'curr_value' => $part_pkg->get('taxclass')  &>
181
182 <& /elements/tr-select-taxproduct.html, 'label' => emt('Tax product'), 'onclick' => 'parent.taxproductmagic(this);', 'curr_value' => $part_pkg->get('taxproductnum')  &>
183 %               }
184
185 % } else { # new one-time charge
186
187     <TR>
188       <TD ALIGN="right"><% mt('Amount to charge') |h %> </TD>
189       <TD>
190         <% $money_char %><INPUT TYPE       = "text"
191                                 NAME       = "amount"
192                                 SIZE       = 8
193                                 VALUE      = "<% $amount %>"
194                                 onChange   = "return enable_quick_charge(event)"
195                                 onKeyPress = "return enable_quick_charge(event)"
196                          >
197       </TD>
198     </TR>
199
200 %   if ( $curuser->access_right('Edit package definition costs') ) {
201       <& /elements/tr-input-text.html,
202            label  => mt('Cost'),
203            field  => 'setup_cost',
204            value  => $setup_cost,
205            size   => 8,
206            prefix => $money_char,
207       &> 
208 %   }
209
210 %   if ( $conf->exists('invoice-unitprice') ) {
211     <TR>
212       <TD ALIGN="right"><% mt('Quantity') |h %> </TD>
213       <TD>
214         <INPUT TYPE       = "text"
215                NAME       = "quantity"
216                SIZE       = 4
217                VALUE      = "<% $quantity %>"
218                onKeyPress = "return enable_quick_charge(event)">
219       </TD>
220     </TR>
221 %   }
222
223 <& /elements/tr-select-pkg_class.html, 'curr_value' => $classnum  &>
224
225 % unless ( $quotationnum ) {
226
227     <TR>
228       <TD ALIGN="right"><% mt('Invoice now') |h %></TD>
229       <TD>
230         <INPUT TYPE  = "checkbox"
231                NAME  = "bill_now"
232                VALUE = "1"
233                <% $cgi->param('bill_now') ? 'CHECKED' : '' %>
234                onClick  = "bill_now_changed(this);"
235                onChange = "bill_now_changed(this);"
236         >
237         <% mt('with terms') |h %> 
238         <& /elements/select-terms.html,
239              'curr_value' => scalar($cgi->param('invoice_terms')),
240              'disabled'   => ( $cgi->param('bill_now') ? 0 : 1 ),
241              'agentnum'   => $cust_or_prospect->agentnum,
242         &>
243       </TD>
244     </TR>
245
246 %   # false laziness w/misc/order_pkg.html
247     <TR>
248       <TD ALIGN="right"><% mt('Charge date') |h %> </TD>
249       <TD>
250         <INPUT TYPE  = "text"
251                NAME  = "start_date"
252                SIZE  = 32
253                ID    = "start_date_text"
254                VALUE = "<% $start_date %>"
255                onKeyPress="return enable_quick_charge(event)"
256                <% $cgi->param('bill_now')
257                     ? 'STYLE = "background-color:#dddddd" DISABLED'
258                     : ''
259                %>
260         >
261         <IMG SRC   = "<%$fsurl%>images/calendar.png"
262              ID    = "start_date_button"
263              TITLE = "<% mt('Select date') |h %>"
264              STYLE = "cursor:pointer<% $cgi->param('bill_now') ? ';display:none' : '' %>"
265         >
266         <IMG SRC   = "<%$fsurl%>images/calendar-disabled.png"
267              ID    = "start_date_button_disabled"
268              <% $cgi->param('bill_now') ? '' : 'STYLE="display:none"' %>
269         >
270         <FONT SIZE=-1>(<% mt('leave blank to charge immediately') |h %>)</FONT>
271       </TD>
272     </TR>
273
274     <SCRIPT TYPE="text/javascript">
275       Calendar.setup({
276         inputField: "start_date_text",
277         ifFormat:   "<% $date_format %>",
278         button:     "start_date_button",
279         align:      "BR"
280       });
281     </SCRIPT>
282
283 % }
284
285 % if ( ! $quotationnum && $cust_main->payby =~ /^(CARD|CHEK)$/ ) {
286 %   my $what = lc(FS::payby->shortname($cust_main->payby));
287     <TR>
288       <TD ALIGN="right"><% mt("Disable automatic $what charge") |h %> </TD>
289       <TD COLSPAN=6><INPUT TYPE="checkbox" NAME="no_auto" VALUE="Y"></TD>
290     </TR>
291 % }
292
293 <TR>
294   <TD ALIGN="right"><% mt('Tax exempt') |h %> </TD>
295   <TD><INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <% $cgi->param('setuptax') ? 'CHECKED' : '' %>></TD>
296 </TR>
297
298 <& /elements/tr-select-taxclass.html, 'curr_value' => $cgi->param('taxclass')  &>
299
300 <& /elements/tr-select-taxproduct.html, 'label' => emt('Tax product'), 'onclick' => 'parent.taxproductmagic(this);', 'curr_value' => $cgi->param('taxproductnum')  &>
301
302 <& /elements/tr-select-taxoverride.html, 'onclick' => 'parent.taxoverridemagic(this);', 'curr_value' => $cgi->param('tax_override')  &>
303
304 % } # if !$cust_pkg
305
306 <TR>
307   <TD ALIGN="right"><% mt('Description') |h %> </TD>
308   <TD>
309     <INPUT TYPE       = "text"
310            NAME       = "pkg"
311            SIZE       = "50"
312            MAXLENGTH  = "50"
313            VALUE      = "<% $pkg %>"
314            onChange   = "return enable_quick_charge(event)"
315            onKeyPress = "return enable_quick_charge(event)"
316     >
317   </TD>
318 </TR>
319
320 <TR>
321   <TD></TD>
322   <TD><FONT SIZE="-1"><% mt('Optional additional description (also printed on invoice):') |h %> </FONT></TD>
323 </TR>
324
325 % my $row = 0;
326 % foreach (@description) {
327     <TR>
328       <TD></TD>
329       <TD>
330         <INPUT TYPE       = "text"
331                NAME       = "description<% $row %>"
332                SIZE       = "60"
333                MAXLENGTH  = "65"
334                VALUE      = "<% $_ |h %>"
335                rownum     = "<% $row %>"
336                onKeyPress = "return enable_quick_charge(event)"
337                onKeyUp    = "return possiblyAddRow(event)"
338         >
339       </TD>
340     </TR>
341 % $row++;
342 % } 
343
344
345 </TABLE>
346
347 <BR>
348 % my $label = $cust_pkg
349 %             ? emt('Modify one-time charge')
350 %             : emt('Add one-time charge');
351 <INPUT TYPE="submit" ID="submit" NAME="submit" VALUE="<% $label %>" \
352 <% ($cgi->param('error') || $cust_pkg) ? '' :' DISABLED' %>>
353
354 </FORM>
355
356
357 <SCRIPT TYPE="text/javascript">
358
359   var rownum = <% $row %>;
360
361   function possiblyAddRow(e) {
362
363     if ( ( rownum - this.getAttribute('rownum') ) == 1 ) {
364       addRow();
365     }
366
367 %   if ( $curuser->option('disable_enter_submit_onetimecharge') ) {
368
369       var key;
370       if (window.event)
371             key = window.event.keyCode; //IE
372       else
373             key = e.which; //firefox, others
374
375       return (key != 13);
376
377 %   } else {
378       return true;
379 %   }
380
381   }
382
383   function addRow() {
384
385     var table = document.getElementById('QuickChargeTable');
386     var tablebody = table.getElementsByTagName('tbody').item(0);
387
388     var row = document.createElement('TR');
389
390     var empty_cell = document.createElement('TD');
391     row.appendChild(empty_cell);
392
393     var description_cell = document.createElement('TD');
394
395       //var description_input = document.createElement('INPUT');
396       var di = document.createElement('INPUT');
397       di.setAttribute('name', 'description'+rownum);
398       di.setAttribute('id',   'description'+rownum);
399       di.setAttribute('size', 60);
400       di.setAttribute('maxLength', 65);
401       di.setAttribute('rownum',   rownum);
402       di.onkeyup = possiblyAddRow;
403       di.onkeypress = enable_quick_charge;
404       description_cell.appendChild(di);
405
406     row.appendChild(description_cell);
407
408     tablebody.appendChild(row);
409
410     rownum++;
411
412   }
413
414 </SCRIPT>
415
416 </BODY>
417 </HTML>
418 <%init>
419
420 my $curuser = $FS::CurrentUser::CurrentUser;
421
422 die "access denied"
423   unless $curuser->access_right('One-time charge');
424
425 my $conf = new FS::Conf;
426 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
427 my $money_char = $conf->config('money_char') || '$';
428
429 my( $cust_main, $cust_pkg, $prospect_main, $quotationnum ) = ( '', '', '', '' );
430 if ( $cgi->param('change_pkgnum') ) {
431   # change an existing one-time charge
432   die "access denied"
433     unless $curuser->access_right('Modify one-time charge');
434
435   $cgi->param('change_pkgnum') =~ /^(\d+)$/ or die "illegal pkgnum";
436   $cust_pkg = FS::cust_pkg->by_key($1) or die "pkgnum $1 not found";
437   $cust_main = $cust_pkg->cust_main;
438 } else {
439   if ( $cgi->param('quotationnum') =~ /^(\d+)$/ ) {
440     $quotationnum = $1;
441   }
442   if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
443     $cust_main = FS::cust_main->by_key($1) or die "custnum $1 not found";
444   }
445   if ( $cgi->param('prospectnum') =~ /^(\d+)$/ ) {
446     $prospect_main = FS::prospect_main->by_key($1) or die "prospectnum $1 not found";
447   }
448   die "custnum or prospectnum must be specified"
449     unless $cust_main || $prospect_main;
450 }
451
452 my $cust_or_prospect = $cust_main || $prospect_main;
453
454 if ( $cust_main ) {
455   my $custnum = $cust_main->custnum;
456   # agent-virt
457   if (!exists($curuser->agentnums_href->{$cust_main->agentnum})) {
458     die "custnum $custnum not found";
459   }
460 } elsif ( $prospect_main ) {
461   my $prospectnum = $prospect_main->prospectnum;
462   # agent-virt
463   if (!exists($curuser->agentnums_href->{$prospect_main->agentnum})) {
464     die "prospectnum $prospectnum not found";
465   }
466 }
467
468 my $format = "%m/%d/%Y %T %z (%Z)"; #false laziness w/REAL_cust_pkg.cgi?
469 my $start_date = $cust_main ? $cust_main->next_bill_date : '';
470 $start_date = $start_date ? time2str($format, $start_date) : '';
471
472 my $amount = '';
473 if ( $cgi->param('amount') =~ /^\s*\$?\s*(\d+(\.\d{1,2})?)\s*$/ ) {
474   $amount = $1;
475 }
476
477 my $setup_cost = '';
478 if ( $cgi->param('setup_cost') =~ /^\s*\$?\s*(\d+(\.\d{1,2})?)\s*$/ ) {
479   $setup_cost = $1;
480 }
481
482 my $quantity = 1;
483 if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
484   $quantity = $1;
485 }
486
487 $cgi->param('pkg') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/ 
488   or die 'illegal description';
489 my $pkg = $1;
490
491 my $default_terms;
492 if ( $cust_main && $cust_main->invoice_terms ) {
493   $default_terms = emt("Customer default ([_1])", $cust_main->invoice_terms);
494 } else {
495   $default_terms =
496     emt( "Default ([_1])",
497          ( $conf->config('invoice_default_terms', $cust_or_prospect->agentnum)
498              || emt('Payable upon receipt')
499          )
500        );
501 }
502
503 my @description;
504 my %param = $cgi->Vars;
505 for (my $i = 0; exists($param{"description$i"}); $i++) {
506   push @description, $param{"description$i"};
507 }
508
509 my $classnum;
510 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
511   $classnum = $1;
512 }
513
514 my $part_pkg;
515 my $billed = 0;
516
517 if ( $cust_pkg ) { # set defaults
518   $part_pkg = $cust_pkg->part_pkg;
519   $pkg ||= $part_pkg->pkg;
520   $classnum ||= $part_pkg->classnum;
521   if (!@description) {
522     for (my $i = 0; $i < ($part_pkg->option('additional_count',1) || 0); $i++) 
523     {
524       push @description, $part_pkg->option("additional_info$i",1);
525     }
526   }
527   $billed = $cust_pkg->get('setup') ? 1 : 0;
528 }
529
530 </%init>