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