RT# 82137 - default payment amount now has processing fee in total if processing...
[freeside.git] / httemplate / elements / tr-amount_fee.html
1   <TR>
2     <TH ALIGN="right"><% mt('Payment amount') |h %></TH>
3     <TD COLSPAN=7>
4       <TABLE><TR><TD BGCOLOR="#ffffff">
5         <% $money_char %><INPUT NAME     = "amount"
6                                 ID       = "amount"
7                                 TYPE     = "text"
8                                 VALUE    = "<% $amount %>"
9                                 SIZE     = 8
10                                 STYLE    = "text-align:right;"
11 %                               if ( $fee || $processing_fee) {
12                                   onChange   = "amount_changed(this)"
13                                   onKeyDown  = "amount_changed(this)"
14                                   onKeyUp    = "amount_changed(this)"
15                                   onKeyPress = "amount_changed(this)"
16 %                               }
17                          >
18       </TD><TD BGCOLOR="#cccccc">
19 %        if ( $fee ) {
20            <INPUT TYPE="hidden" NAME="fee_pkgpart" VALUE="<% $fee_pkg->pkgpart %>">
21            <INPUT TYPE="hidden" NAME="fee" VALUE="<% $fee_display eq 'add' ? $fee : '' %>">
22            <B><FONT SIZE='+1'><% $fee_op %></FONT>
23               <% $money_char . $fee %>
24            </B>
25            <% $fee_pkg->pkg |h %>
26            <B><FONT SIZE='+1'>=</FONT></B>
27       </TD><TD ID="ajax_total_cell" BGCOLOR="#dddddd" STYLE="border:1px solid blue">
28            <FONT SIZE="+1"><% length($amount) ? $money_char. sprintf('%.2f', ($fee_display eq 'add') ? $amount + $fee : $amount - $fee ) : '' %> <% $fee_display eq 'add' ? 'TOTAL' : 'AVAILABLE' %></FONT>
29   
30 %        }
31       </TD></TR></TABLE>
32     </TD>
33   </TR>
34
35 %        if ( $processing_fee ) {
36       <TR>
37         <TH ALIGN="right"><% mt('Processing fee') |h %></TH>
38         <TD>
39           <TABLE><TR>
40             <TD BGCOLOR="#ffffff">
41              <INPUT TYPE="checkbox" NAME="processing_fee" ID="processing_fee" VALUE="<% $processing_fee %>" onclick="<% $opt{prefix} %>process_fee_changed()" checked>
42             </TD>
43             <TD>
44              Apply a processing fee of <% $processing_fee %> .</FONT>
45             </TD>
46           </TR></TABLE>
47         </TD>
48       </TR>
49 %        }
50
51 % if ($fee) {
52
53     <SCRIPT TYPE="text/javascript">
54
55       function amount_changed(what) {
56
57
58         var total = '';
59         if ( what.value.length ) {
60           total = parseFloat(what.value) <% $fee_op %> <% $fee %>;
61           /* total = Math.round(total*100)/100; */
62           total = '<% $money_char %>' + total.toFixed(2);
63         }
64
65         var total_cell = document.getElementById('ajax_total_cell');
66         total_cell.innerHTML = '<FONT SIZE="+1">' + total + ' <% $fee_display eq 'add' ? 'TOTAL' : 'AVAILABLE' %></FONT>';
67
68       }
69
70     </SCRIPT>
71
72 % }
73
74 % if ($processing_fee) {
75
76     <SCRIPT TYPE="text/javascript">
77
78       function <% $opt{prefix} %>process_fee_changed(what) {
79
80         if (document.getElementById('processing_fee').checked == true) {
81           var amount = +document.getElementById('amount').value + +document.getElementById('processing_fee').value;
82           $('#amount').val(amount.toFixed(2));
83         }
84         else {
85           var amount = +document.getElementById('amount').value - +document.getElementById('processing_fee').value;
86           $('#amount').val(amount.toFixed(2));
87         }
88
89       }
90
91     </SCRIPT>
92
93 % }
94
95 <%init>
96
97 my %opt = @_;
98
99 my $conf = new FS::Conf;
100 my $money_char = $conf->config('money_char') || '$';
101
102 my $fee = '';
103 my $fee_pkg = '';
104 my $fee_display = '';
105 my $fee_op = '';
106 my $processing_fee = 0;
107
108 if ( $opt{'process-pkgpart'}
109      and ! $opt{'process-skip_first'} || $opt{'num_payments'}
110    )
111 {
112
113   $fee_display = $opt{'process-display'} || 'add';
114   $fee_op = $fee_display eq 'add' ? '+' : '-';
115
116   $fee_pkg =
117     qsearchs('part_pkg', { pkgpart=>$opt{'process-pkgpart'} } );
118
119   #well ->unit_setup or ->calc_setup both call for a $cust_pkg
120   # (though ->unit_setup doesn't use it...)
121   $fee = $fee_pkg->option('setup_fee')
122     if $fee_pkg; #in case.. better than dying with a perl traceback
123
124 }
125
126 my $amount = $opt{'amount'};
127 if ( $amount > 0 ) {
128   $amount += $fee
129     if $fee && $fee_display eq 'subtract';
130
131   #&{ $opt{post_fee_callback} }( \$amount ) if $opt{post_fee_callback};
132   $amount += $amount * $opt{'surcharge_percentage'}/100
133     if $opt{'surcharge_percentage'} > 0;
134
135   $amount += $opt{'surcharge_flatfee'}
136     if $opt{'surcharge_flatfee'} > 0;
137
138   $processing_fee = $opt{'processing_fee'} if $opt{'processing_fee'} > 0;
139
140   $amount += $processing_fee; ## needed if processing fee is checked on default.
141
142   $amount = sprintf("%.2f", $amount);
143 }
144
145 </%init>