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