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