0ae6a0f74156a16bc958f0f6fe3218bd37de50c3
[freeside.git] / httemplate / elements / tr-amount_fee.html
1   <TR ID="payment_amount_row">
2     <TH ALIGN="right"><% mt('Payment amount') |h %></TH>
3     <TD>
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 || $surcharge_percentage || $surcharge_flatfee || $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="<% length($fee) ? '#cccccc' : '#ffffff' %>">
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 %        if ( $surcharge_percentage || $surcharge_flatfee ) {
32            <INPUT TYPE="hidden" NAME="surcharge_percentage" ID="surcharge_percentage" VALUE="<% $surcharge_percentage %>">
33            <INPUT TYPE="hidden" NAME="surcharge_flatfee" ID="surcharge_flatfee" VALUE="<% $surcharge_flatfee %>">
34       </TD><TD ID="ajax_surcharge_cell" BGCOLOR="#dddddd" STYLE="border:1px solid blue">
35            <FONT SIZE="+1">A credit card surcharge of <% $money_char. sprintf('%.2f', $surcharge) %> is included in this payment</FONT>
36 %        }
37       </TD></TR></TABLE>
38     </TD>
39   </TR>
40
41 %        if ( $processing_fee ) {
42       <TR>
43         <TH ALIGN="right"><% mt('Apply processing fee') |h %></TH>
44         <TD>
45           <TABLE><TR>
46             <TD BGCOLOR="#ffffff">
47              <INPUT TYPE="checkbox" NAME="processing_fee" ID="processing_fee" VALUE="<% $processing_fee %>" onclick="<% $opt{prefix} %>process_fee_changed()" checked>
48             </TD>
49             <TD ID="ajax_processingfee_cell" BGCOLOR="#dddddd" STYLE="border:1px solid blue">
50              <FONT SIZE="+1">A processing fee of <% $processing_fee %> is being applied to this transaction.</FONT>
51             </TD>
52           </TR></TABLE>
53         </TD>
54       </TR>
55 %        }
56
57 % if ($fee || $surcharge_percentage || $surcharge_flatfee || $processing_fee) {
58
59     <SCRIPT TYPE="text/javascript">
60
61       function amount_changed(what) {
62
63 % if ( $fee ) {
64         var total = '';
65         if ( what.value.length ) {
66           total = parseFloat(what.value) <% $fee_op %> <% $fee %>;
67           /* total = Math.round(total*100)/100; */
68           total = '<% $money_char %>' + total.toFixed(2);
69         }
70
71         var total_cell = document.getElementById('ajax_total_cell');
72         total_cell.innerHTML = '<FONT SIZE="+1">' + total + ' <% $fee_display eq 'add' ? 'TOTAL' : 'AVAILABLE' %></FONT>';
73 % }
74
75 % if ( $surcharge_percentage || $surcharge_flatfee ) {
76         var surcharge_cell = document.getElementById('ajax_surcharge_cell');
77         var amount = what.value;
78         if (document.getElementById('processing_fee').checked == true) {
79           amount = (what.value - <% $processing_fee %>);
80         }
81         var surcharge = ((what.value - <% $surcharge_flatfee %>) * <% $surcharge_percentage %>) + <% $surcharge_flatfee %>;
82         surcharge_cell.innerHTML = '<FONT SIZE="+1">A credit card surcharge of ' + surcharge.toFixed(2) + ' is included in this payment</FONT>';
83 % }
84
85       }
86
87     </SCRIPT>
88
89 % }
90
91 <%init>
92
93 my %opt = @_;
94
95 my $conf = new FS::Conf;
96 my $money_char = $conf->config('money_char') || '$';
97
98 my $fee = '';
99 my $fee_pkg = '';
100 my $fee_display = '';
101 my $fee_op = '';
102 my $surcharge = '';
103 my $surcharge_percentage = 0;
104 my $surcharge_flatfee = 0;
105 my $processing_fee = 0;
106
107 if ( $opt{'process-pkgpart'}
108      and ! $opt{'process-skip_first'} || $opt{'num_payments'}
109    )
110 {
111
112   $fee_display = $opt{'process-display'} || 'add';
113   $fee_op = $fee_display eq 'add' ? '+' : '-';
114
115   $fee_pkg =
116     qsearchs('part_pkg', { pkgpart=>$opt{'process-pkgpart'} } );
117
118   #well ->unit_setup or ->calc_setup both call for a $cust_pkg
119   # (though ->unit_setup doesn't use it...)
120   $fee = $fee_pkg->option('setup_fee')
121     if $fee_pkg; #in case.. better than dying with a perl traceback
122
123 }
124
125 my $amount = $opt{'amount'};
126 if ( $amount ) {
127   # probably should not happen, but will prevent surcharge being applied to negative due amounts
128   unless ($amount > 0) { $amount = 0; }
129
130   $amount += $fee
131     if $fee && $fee_display eq 'subtract';
132
133   #&{ $opt{post_fee_callback} }( \$amount ) if $opt{post_fee_callback};
134
135   $surcharge_percentage = $opt{'surcharge_percentage'}/100 if $opt{'surcharge_percentage'} > 0;
136   $surcharge_flatfee = $opt{'surcharge_flatfee'} if $opt{'surcharge_flatfee'} > 0;
137   $surcharge = $amount * $surcharge_percentage if $surcharge_percentage > 0;
138   $surcharge += $surcharge_flatfee if ( $surcharge_flatfee > 0 && $amount > 0 );
139   $processing_fee = $opt{'processing_fee'} if $opt{'processing_fee'} > 0;
140
141   $amount += $surcharge;
142
143   $amount = sprintf("%.2f", $amount);
144 }
145
146 </%init>