master will be 4.0
[freeside.git] / httemplate / edit / elements / ApplicationCommon.html
1 <%doc>
2
3 Examples:
4
5   #cust_bill_pay
6   include('elements/ApplicationCommon.html',
7     'form_action' => 'process/cust_bill_pay.cgi', 
8     'src_table'   => 'cust_pay',
9     'src_thing'   => 'payment',
10     'dst_table'   => 'cust_bill',
11     'dst_thing'   => 'invoice',
12   )
13
14   #cust_credit_bill
15   include('elements/ApplicationCommon.html',
16     'form_action' => 'process/cust_credit_bill.cgi',
17     'src_table'   => 'cust_credit',
18     'src_thing'   => 'credit',
19     'dst_table'   => 'cust_bill',
20     'dst_thing'   => 'invoice',
21   )
22
23   #cust_pay_refund
24   include('elements/ApplicationCommon.html',
25     'form_action' => 'process/cust_pay_refund.cgi',
26     'src_table'   => 'cust_pay',
27     'src_thing'   => 'payment',
28     'dst_table'   => 'cust_refund',
29     'dst_thing'   => 'refund',
30   )
31
32   #cust_credit_refund
33   include('elements/ApplicationCommon.html',
34     'form_action' => 'process/cust_credit_refund.cgi',
35     'src_table'   => 'cust_credit',
36     'src_thing'   => 'credit',
37     'dst_table'   => 'cust_refund',
38     'dst_thing'   => 'refund',
39   )
40
41 </%doc>
42
43 <% include('/elements/header-popup.html', "Apply $src_thing$to", '', 'onLoad="myOnLoadFunction();"') %>
44
45 <% include('/elements/error.html') %>
46
47 <P ID="ErrorMessage"></P>
48 <FORM ACTION="<% $p1. $opt{'form_action'} %>" NAME="ApplicationForm" ID="ApplicationForm" METHOD=POST>
49
50 <% $src_thing %> #<B><% $src_pkeyvalue %></B><BR>
51 <INPUT TYPE="hidden" NAME="<% $src_pkey %>" VALUE="<% $src_pkeyvalue %>">
52
53 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
54
55 <TR>
56   <TD ALIGN="right">Date: </TD>
57   <TD><B><% time2str($date_format, $src->_date) %></B></TD>
58 </TR>
59
60 <TR>
61   <TD ALIGN="right">Amount: </TD>
62   <TD ID="original_amount"><B><% $money_char %><% $src_amount %></B>
63   </TD>
64   <TD>
65 % if ($use_sub_dst_thing && $can_change_credit) {
66     <INPUT TYPE="hidden" NAME="src_amount" VALUE="<% $src_amount %>" >
67     <BUTTON TYPE="button" NAME="expand_button" ID="expand_button" onClick="do_change_amount(this);">Change</BUTTON>
68 % }
69   </TD>
70
71 </TR>
72
73 <TR>
74   <TD ALIGN="right">Unapplied amount: </TD>
75   <TD ID="unapplied_amount"><B><% $money_char %><% $unapplied %></B></TD>
76 </TR>
77
78 % if ( $src_table eq 'cust_credit' ) {
79     <TR>
80       <TD ALIGN="right">Reason: </TD>
81       <TD COLSPAN=2><B><% $src->reason %></B></TD>
82     </TR>
83 % }
84
85 </TABLE>
86 <BR>
87
88 <SCRIPT TYPE="text/javascript">
89 function clear_amounts() {
90   var rownum=0
91   var table = document.getElementById('ApplicationTable');
92   for (var row = 2; table.rows[row]; row++)
93   {
94     var inputs = table.rows[row].getElementsByTagName('input');
95     if ( !inputs.length ) {
96       break;
97     }
98     inputs.item(0).value = ''; // amount
99   }
100
101 }
102
103 function changed(what) {
104   dst = what.options[what.selectedIndex].value;
105
106   if ( dst == '' ) {
107     what.form.submit.disabled=true;
108 %if ($use_sub_dst_thing && $src_pkey eq 'crednum') {
109     what.form.tax_button.disabled=true;
110     what.form.clear_button.disabled=true;
111 %}
112     return true;
113   }
114
115   what.form.submit.disabled=false;
116 %if ($use_sub_dst_thing && $src_pkey eq 'crednum') {
117   what.form.tax_button.disabled=false;
118   what.form.clear_button.disabled=false;
119 %}
120
121 % foreach my $dst ( @dst ) {
122
123     if ( dst == <% $dst->$dst_pkey %> ) {
124       what.form.amount.value = "<% min($dst->$dst_unapplied, $unapplied) %>";
125 %     if ($use_sub_dst_thing) {
126         what.form.display_amount.value = "<% min($dst->$dst_unapplied, $unapplied) %>";
127
128         var rownum=0
129         var table = document.getElementById('ApplicationTable');
130         while(table.rows[2]) {
131           table.deleteRow(2);
132         }
133 %       my $app_class = "FS::$link_table";
134 %       my $temp_app = $app_class->new(
135 %         { $src_pkey => $src_pkeyvalue,
136 %           $dst_pkey => $dst->$dst_pkey,
137 %           'amount'  => min($dst->$dst_unapplied, $unapplied),
138 %         }
139 %       );
140 %       my %apphash = ();
141 %       my $listref_or_error = $temp_app->calculate_applications;
142 %       %apphash = map { &{$key_generator}($_), $_ } @$listref_or_error
143 %         if ref($listref_or_error);
144 %       foreach my $cbp ( $dst->open_cust_bill_pkg ) {
145 %         my $desc = $cbp->desc;
146 %         my $total_owed = $cbp->owed_setup + $cbp->owed_recur;
147 %         my $key = &{$key_generator}([ $cbp, 0, {} ]);
148 %         my $amount = exists($apphash{ $key }) ? $apphash{ $key }->[1] : 0;
149 %         unless ( $cbp->pkgnum ) {
150 %           foreach my $taxX ( $cbp->cust_bill_pkg_tax_Xlocation ) {
151 %             my $pkey = $taxX->primary_key;
152 %             my $owed = $taxX->owed;
153 %             my $key = &{$key_generator}([ $cbp, 0, { $pkey => $taxX->$pkey } ]);
154 %             my $toapp = exists($apphash{ $key }) ? $apphash{ $key }->[1] : 0;
155               <% &{$row_generator}( $key, $cbp, $taxX->desc, $owed, $toapp, $taxX->$pkey ) %>
156 %             $total_owed -= $owed;
157 %             $amount -= $toapp;
158 %           }
159 %           $desc .= ' (default)';
160 %         }
161 %         if ( $total_owed > 0 ) {
162             <% &{$row_generator}($key, $cbp, $desc, $total_owed, $amount, '') %>
163 %         }
164 %       }
165 %     }
166     }
167
168 % } 
169
170 }
171
172 function sub_changed(what) {
173
174   var amount = 0;
175   var table = document.getElementById('ApplicationTable');
176   var i = table.rows.length;
177   while(i-- > 2) {
178     var inputs = table.rows[i].getElementsByTagName('input');
179     if (! inputs.length) {
180       continue;
181     }
182     amount += parseFloat( inputs.item(0).value ) || 0; 
183   }
184   what.form.amount.value = parseFloat(amount).toFixed(2);
185   what.form.display_amount.value = parseFloat(amount).toFixed(2);
186   set_amount_color(what);
187
188 }
189
190 function set_amount_color(what) {
191   if (what.form.src_amount.value < what.form.amount.value) {
192     what.form.display_amount.style.color = '#ff0000';
193   } else {
194     what.form.display_amount.style.color = '#00ff00';
195   }
196 }
197
198 </SCRIPT>
199
200 Apply to:
201
202 % if ($use_sub_dst_thing && $src_pkey eq 'crednum') {
203 <CENTER>
204   <TABLE>
205     <TR>
206       <TD>
207         <BUTTON TYPE="button" NAME="tax_button" ID="tax_button" onClick="do_calculate_tax(this);" DISABLED>Calculate Tax</BUTTON>
208       </TD>
209       <TD>
210         <BUTTON TYPE="button" NAME="clear_button" ID="clear_button" onClick="clear_amounts(this);" DISABLED>Clear Amounts</BUTTON>
211       </TD>
212     </TR>
213   </TABLE>
214 </CENTER>
215 <% include( '/elements/xmlhttp.html',
216             'url' =>  $p.'misc/xmlhttp-calculate_taxes.html',
217             'subs' => [ 'calculate_taxes' ],
218           )
219  %>
220 <SCRIPT TYPE="text/javascript">
221
222 function show_taxes(arg) {
223   var argsHash = eval('(' + arg + ')');
224
225   var button = document.getElementById('tax_button');
226   button.disabled = false;
227   button.innerHTML = 'Calculate Tax';
228   button = document.getElementById('clear_button');
229   button.disabled = false;
230
231   var error = argsHash['error'];
232
233   var paragraph = document.getElementById('ErrorMessage');
234   if (error) {
235     paragraph.innerHTML = 'Error: ' + error;
236     paragraph.style.color = '#ff0000';
237   } else {
238     paragraph.innerHTML = '';
239   }
240   var taxlines = argsHash['taxlines'];
241
242   var table = document.getElementById('ApplicationTable');
243
244   var aFoundRow = 0;
245   for (i = 0; taxlines[i]; i++) {
246     var itemdesc = taxlines[i][0];
247     var locnum   = taxlines[i][2];
248     if (taxlines[i][3]) {
249       locnum  = taxlines[i][3];
250     }
251
252     var found = 0;
253     for (var row = 2; table.rows[row]; row++) {
254       var inputs = table.rows[row].getElementsByTagName('input');
255       if (! inputs.length) {
256         while ( table.rows[row] ) {
257            table.deleteRow(row);
258         }
259         break;
260       }
261       if ( inputs.item(4).value == itemdesc && inputs.item(2).value == locnum )
262       {
263         inputs.item(0).value = taxlines[i][1];
264         aFoundRow = found = row;
265         break;
266       }
267     }
268     if (! found) {
269       var row = table.insertRow(table.rows.length);
270       var warning_cell = document.createElement('TD');
271       warning_cell.style.color = '#ff0000';
272       warning_cell.colSpan = 2;
273       warning_cell.innerHTML = 'Calculated Tax - ' + itemdesc + ' - ' +
274                                taxlines[i][1] + ' will not be applied';
275       row.appendChild(warning_cell);
276     }
277   }
278
279   if (aFoundRow) {
280     sub_changed(table.rows[aFoundRow].getElementsByTagName('input').item(0));
281   }
282     
283 }
284
285 function do_calculate_tax (what) {
286   what.innerHTML = 'Calculating....';
287   what.disabled = true;
288   var button = document.getElementById('clear_button');
289   button.disabled = true;
290   var taxed_items = new Array();
291   var table = document.getElementById('ApplicationTable');
292   for (var row = 2; table.rows[row]; row++)
293   {
294     var inputs = table.rows[row].getElementsByTagName('input');
295     if ( !inputs.length ) {
296       break;
297     }
298     var taxed_item = new Array(
299       inputs.item(1).value, // billpkgnum
300       inputs.item(3).value, // s_or_r
301       inputs.item(0).value || 0  // amount
302     );
303     taxed_items.push(taxed_item);
304   }
305
306   var args = new Array(
307     'crednum', '<% $src_pkeyvalue %>',
308     'items', taxed_items
309   );
310   calculate_taxes ( args, show_taxes );
311 }
312
313 function do_change_amount (what) {
314   var amount_cell = document.getElementById('original_amount');
315   var inputs = amount_cell.getElementsByTagName('input');
316   if (inputs.length) {
317     src_amount_changed();
318     amount_cell.innerHTML = '<B><% $money_char %></B>' + inputs.item(0).value;
319   } else {
320     amount_cell.innerHTML = '<% $money_char %>';
321     var amount_input = document.createElement('INPUT');
322     amount_input.setAttribute('name', 'entered_amount');
323     amount_input.setAttribute('id',   'entered_amount');
324     amount_input.style.textAlign = 'right';
325     amount_input.setAttribute('size', 8);
326     amount_input.setAttribute('maxlength', 8);
327     amount_input.setAttribute('value', what.form.src_amount.value);
328     amount_input.setAttribute('onChange', "src_amount_changed(this);");
329     amount_cell.appendChild(amount_input);
330   }
331 }
332
333 function src_amount_changed () {
334   //alert('src_amount_changed called');
335   var entered_amount = document.getElementById('entered_amount');
336   if ( entered_amount ) {
337     entered_amount.form.src_amount.value = entered_amount.value;
338     var unapplied_cell = document.getElementById('unapplied_amount');
339     unapplied_cell.innerHTML = '<B><% $money_char %>' + entered_amount.value + '</B>';
340     set_amount_color(entered_amount);
341   }
342   return true;
343 }
344
345 </SCRIPT>
346
347 %}
348
349 <TABLE ID="ApplicationTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
350
351 <TR>
352   <TD ALIGN="right"><% $dst_thing %>: </TD>
353   <TD><SELECT NAME="<% $dst_pkey %>" SIZE=1 onChange="changed(this)">
354 <OPTION VALUE="">Select <% $dst_thing %>
355
356 % foreach my $dst ( @dst ) { 
357   <OPTION<% $dst->$dst_pkey eq $dst_pkeyvalue ? ' SELECTED' : '' %> VALUE="<% $dst->$dst_pkey %>">#<% $dst->$dst_pkey %> - <% time2str($date_format, $dst->_date) %> - $<% $dst->$dst_unapplied %>
358 % } 
359
360 </SELECT>
361   </TD>
362 </TR>
363
364 <TR>
365   <TD ALIGN="right">Amount: </TD>
366   <TD><% $money_char %><INPUT TYPE="text" NAME="<% $use_sub_dst_thing ? 'display_' : '' %>amount" VALUE="<% $amount %>" SIZE=8 MAXLENGTH=8 <% $use_sub_dst_thing ? 'DISABLED' : '' %> STYLE="text-align:right;"></TD>
367 % if ($use_sub_dst_thing) {
368     <INPUT TYPE="hidden" NAME="amount" VALUE="<% $amount %>" >
369 % }
370 </TR>
371
372 </TABLE>
373
374 <BR>
375 <CENTER><INPUT TYPE="submit"
376                VALUE="Apply"
377                NAME="submit"
378                ID="submit"
379 % if ($use_sub_dst_thing && $can_change_credit) {
380                onClick="src_amount_changed()"
381 % }
382                DISABLED
383 ></CENTER>
384
385 </FORM>
386
387 <SCRIPT TYPE="text/javascript">
388
389 function myOnLoadFunction () {
390   <% $onload %>
391 }
392
393 </SCRIPT>
394
395 <% include('/elements/footer.html') %>
396
397 <%init>
398
399 my %opt = @_;
400
401 my $conf = new FS::Conf;
402 my $money_char  = $conf->config('money_char')  || '$';
403 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
404
405 my $src_thing = ucfirst($opt{'src_thing'});
406 my $src_table = $opt{'src_table'};
407 my $src_pkey = dbdef->table($src_table)->primary_key;
408
409 my $dst_thing = ucfirst($opt{'dst_thing'});
410 my $dst_table = $opt{'dst_table'};
411 my $dst_pkey = dbdef->table($dst_table)->primary_key;
412 my $dst_unapplied = $dst_table eq 'cust_bill' ? 'owed' : 'unapplied';
413
414 $opt{form_action} =~ /^process\/(.*)\./ or die "bad form action";
415 my $link_table = $1;
416
417 my $use_sub_dst_thing = 0;
418 $use_sub_dst_thing = 1
419   if ( $dst_table eq 'cust_bill' && $conf->exists("${link_table}_pkg-manual") );
420
421 my $can_change_credit = 0;
422 $can_change_credit = 1
423   if ( $src_table eq 'cust_credit' && 
424        $FS::CurrentUser::CurrentUser->access_right('Post credit') &&
425        $FS::CurrentUser::CurrentUser->access_right('Delete credit')
426      );
427
428 my $to = $dst_table eq 'cust_refund' ? ' to Refund' : '';
429
430 my($src_pkeyvalue, $amount, $dst_pkeyvalue, $src_amount);
431 if ( $cgi->param('error') ) {
432   $src_pkeyvalue = $cgi->param($src_pkey);
433   $amount    = $cgi->param('amount');
434   $dst_pkeyvalue    = $cgi->param($dst_pkey);
435   $src_amount = $cgi->param('src_amount');
436 } else {
437   my($query) = $cgi->keywords;
438   $query =~ /^(\d+)$/;
439   $src_pkeyvalue = $1;
440   $amount = '';
441   $dst_pkeyvalue = '';
442 }
443
444 my $otaker = getotaker;
445
446 my $p1 = popurl(1);
447
448 my $src = qsearchs($src_table, { $src_pkey => $src_pkeyvalue } );
449 die "$src_thing $src_pkeyvalue not found!" unless $src;
450
451 $src_amount = $src->amount unless $cgi->param('error');
452
453 my $unapplied = $src->unapplied;
454
455 my @dst = sort {    $a->_date     <=> $b->_date
456                  or $a->$dst_pkey <=> $b->$dst_pkey
457                }
458           grep { $_->$dst_unapplied != 0 }
459           qsearch($dst_table, { 'custnum' => $src->custnum } );
460
461 my $row_generator = sub {
462   my ($key, $cust_bill_pkg, $desc, $owed, $amount, $taxXnum) = @_;
463   my ($num, $s_or_r, $taxlinenum) = split(':', $key);
464   my $id = $cust_bill_pkg->pkgnum || 'Tax';
465   my $billpkgnum = $cust_bill_pkg->billpkgnum;
466   my $s_or_r = $cust_bill_pkg->setup > 0 ? 'setup' : 'recur';
467
468   $amount = sprintf("%.2f", $amount);
469   qq!
470       var tablebody = document.getElementsByTagName('tbody').item(0);
471       var row = table.insertRow(rownum+2);
472       var pkg_cell = document.createElement('TD');
473       pkg_cell.style.textAlign = 'right';
474       pkg_cell.innerHTML = "$id - $desc - $owed:";
475       var amount_cell = document.createElement('TD');
476       amount_cell.innerHTML = "$money_char";
477       var amount_input = document.createElement('INPUT');
478       amount_input.setAttribute('name', 'subamount'+rownum);
479       amount_input.setAttribute('id',   'subamount'+rownum);
480       amount_input.style.textAlign = 'right';
481       amount_input.setAttribute('size', 8);
482       amount_input.setAttribute('maxlength', 8);
483       amount_input.setAttribute('rownum', rownum);
484       amount_input.setAttribute('value', "$amount");
485       amount_input.setAttribute('onChange', "sub_changed(this);");
486       amount_cell.appendChild(amount_input);
487       var subnum_input = document.createElement('INPUT');
488       subnum_input.setAttribute('name', 'subnum'+rownum);
489       subnum_input.setAttribute('id',   'subnum'+rownum);
490       subnum_input.setAttribute('type', 'hidden');
491       subnum_input.setAttribute('rownum', rownum);
492       subnum_input.setAttribute('value', "$billpkgnum");
493       amount_cell.appendChild(subnum_input);
494       var taxnum_input = document.createElement('INPUT');
495       taxnum_input.setAttribute('name', 'taxXlocationnum'+rownum);
496       taxnum_input.setAttribute('id',   'taxXlocationnum'+rownum);
497       taxnum_input.setAttribute('type', 'hidden');
498       taxnum_input.setAttribute('rownum', rownum);
499       taxnum_input.setAttribute('value', "$taxXnum");
500       amount_cell.appendChild(taxnum_input);
501       var s_or_r_input = document.createElement('INPUT');
502       s_or_r_input.setAttribute('name', 's_or_r'+rownum);
503       s_or_r_input.setAttribute('id',   's_or_r'+rownum);
504       s_or_r_input.setAttribute('type', 'hidden');
505       s_or_r_input.setAttribute('rownum', rownum);
506       s_or_r_input.setAttribute('value', "$s_or_r");
507       amount_cell.appendChild(s_or_r_input);
508       var itemdesc_input = document.createElement('INPUT');
509       itemdesc_input.setAttribute('name', 'itemdesc'+rownum);
510       itemdesc_input.setAttribute('id',   'itemdesc'+rownum);
511       itemdesc_input.setAttribute('type', 'hidden');
512       itemdesc_input.setAttribute('rownum', rownum);
513       itemdesc_input.setAttribute('value', "$desc");
514       amount_cell.appendChild(itemdesc_input);
515       row.appendChild(pkg_cell);
516       row.appendChild(amount_cell);
517       rownum++;
518     !;
519 };
520
521 my $key_generator = sub {
522   my $listref = shift;
523   my ($cust_bill_pkg, $amount, $hashref) = @$listref;
524   my $setup_or_recur = $cust_bill_pkg->setup > 0 ? 'setup' : 'recur';
525   my $taxlinenum = $hashref->{billpkgtaxlocationnum} ||
526                    $hashref->{billpkgtaxratelocationnum} ||
527                    '';
528                    
529   join(':', $cust_bill_pkg->billpkgnum, $setup_or_recur, $taxlinenum);
530 };
531
532 my $onload = 'return true;';
533
534 if ($cgi->param('error')) {
535
536   my $set_sub_amounts =
537     join(';', map { "myform.subamount$_.value = ". $cgi->param("subamount$_") }
538                grep { /.+/ }
539                map { /^subnum(\d+)$/ ? $1 : '' }
540                $cgi->param
541   );
542   $set_sub_amounts &&= "$set_sub_amounts;sub_changed(myform.subamount0)";
543
544   $onload = qq!
545     var myform = document.getElementById('ApplicationForm');
546     changed(myform.elements['$dst_pkey']);
547     $set_sub_amounts;
548     return true;
549   !;
550 }
551
552 </%init>