Optimize "Customer has a referring customer" condition, RT#74452
[freeside.git] / httemplate / elements / tr-select-payment_options.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/tr-select-payment_options.html',
6
7     #opt - most get used in /elements/tr-amount-fee
8     'cust_main'            => $cust_main,     # custmain needed for selecting invoices
9     'prefix'               => 'pre', # prefix to fields and row ID's
10     'amount'               => 1,     # payment amount optional, if no amount will grab balance due from cust_main
11     'process-pkgpart'      => scalar($conf->config('manual_process-pkgpart', $cust_main->agentnum)),
12     'process-display'      => scalar($conf->config('manual_process-display')),
13     'process-skip_first'   => $conf->exists('manual_process-skip_first'),
14     'num_payments'         => scalar($cust_main->cust_pay),
15     'surcharge_percentage' =>
16       ( $payby eq 'CARD'
17           ? scalar($conf->config('credit-card-surcharge-percentage', $cust_main->agentnum))
18           : 0
19       ),
20     'surcharge_flatfee' =>
21       ( $payby eq 'CARD'
22           ? scalar($conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum))
23           : 0
24       ),
25   )
26
27 </%doc>
28
29   <TR ID="payment_option_row">
30     <TH ALIGN="right"><% mt('What would you like to pay') |h %></TH>
31     <TD>
32      <SELECT
33           ID       = "<% $opt{prefix} %>payment_option"
34           NAME     = "<% $opt{prefix} %>payment_option"
35           onChange = "<% $opt{prefix} %>payment_option_changed(this)"
36           <% $opt{disabled} %>
37            >
38       <OPTION VALUE="select">Select the amount you would like to pay</OPTION>
39       <% ($amount > 0) ? '<OPTION VALUE="'.$amount.'">Pay full balance</OPTION>' : '' %>
40       <% (@open_invoices) ? '<OPTION VALUE="invoice">Pay specific invoice</OPTION>' : '' %>
41       <OPTION VALUE="specific">Pay specific amount</OPTION>
42            </SELECT>
43     </TD>
44   </TR>
45
46   <& /elements/tr-select-invoice.html,
47        'cust_main' => $cust_main,
48        'status'    => 'open',
49        'prefix'    => $opt{prefix},
50   &>
51
52   <& /elements/tr-amount_fee.html,
53        'amount'     => $amount,
54        'custnum'    => $custnum,
55        %opt
56   &>
57
58   <SCRIPT TYPE="text/javascript">
59
60       $('#payment_option_row').<% $payment_option_row %>();
61       $('#payment_amount_row').<% $payment_amount_row %>();
62
63       if($('#payment_amount_row').is(':visible')) {
64         var surcharge;
65         var amount = document.getElementById('amount').value;
66
67         if ((document.getElementById('surcharge_percentage') || document.getElementById('surcharge_flatfee')) && amount > 0) {
68           surcharge = (+amount * +document.getElementById('surcharge_percentage').value) + +document.getElementById('surcharge_flatfee').value;
69         }
70         else { surcharge = 0; }
71         if (document.getElementById('ajax_surcharge_cell')) {
72           document.getElementById('ajax_surcharge_cell').innerHTML = '<FONT SIZE="+1">A credit card surcharge of <% $money_char %>' + surcharge.toFixed(2) + ' is included in this payment</FONT>';
73         }
74       }
75
76       function <% $opt{prefix} %>payment_option_changed(what) {
77
78         var surcharge;
79         if (document.getElementById('surcharge_percentage') || document.getElementById('surcharge_flatfee')) {
80           surcharge = (+what.value * +document.getElementById('surcharge_percentage').value) + +document.getElementById('surcharge_flatfee').value;
81         }
82         else { surcharge = 0; }
83         var amount = +what.value + +surcharge;
84         document.getElementById('amount').disabled = true;
85
86         if ( what.value == 'select' ) {
87           $('#payment_amount_row').hide();
88           $('#invoice_row').hide();
89           $("#<% $opt{prefix} %>invoice").val('select');
90           $('#amount').val('');
91         }
92         else if ( what.value == 'invoice' ) {
93           $('#payment_amount_row').hide();
94           $('#invoice_row').show();
95           $('#apply_box_row').hide();
96           $('#apply_box').val('yes');
97           $("#<% $opt{prefix} %>payment_option option[value='select']").remove();
98           var selectExists = ($("#<% $opt{prefix} %>invoice option[value='select']").length > 0);
99           if(!selectExists) {
100             $("#<% $opt{prefix} %>invoice").prepend("<option value='select'>Select an invoice to pay</option>");
101             $("#<% $opt{prefix} %>invoice").val($('option:first', "#<% $opt{prefix} %>invoice").val());
102           }
103           $('#amount').val('');
104         }
105         else if ( what.value == 'specific' ) {
106           $('#payment_amount_row').show();
107           $('#invoice_row').hide();
108           $('#apply_box_row').show();
109           $("#<% $opt{prefix} %>payment_option option[value='select']").remove();
110           $('#amount').val('0.00');
111           document.getElementById('amount').disabled = false;
112           if (document.getElementById('ajax_surcharge_cell')) {
113             document.getElementById('ajax_surcharge_cell').innerHTML = '<FONT SIZE="+1">A credit card surcharge of <% $money_char %>0.00 is included in this payment</FONT>';
114           }
115         }
116         else {
117           $('#payment_amount_row').show();
118           $('#invoice_row').hide();
119           $('#apply_box_row').hide();
120           $('#apply_box').val('yes');
121           $("#<% $opt{prefix} %>payment_option option[value='select']").remove();
122           $('#amount').val(amount.toFixed(2));
123           document.getElementById('amount').disabled = true;
124           if (document.getElementById('ajax_surcharge_cell')) {
125             document.getElementById('ajax_surcharge_cell').innerHTML = '<FONT SIZE="+1">A credit card surcharge of <% $money_char %>' + surcharge.toFixed(2) + ' is included in this payment</FONT>';
126           }
127         }
128
129       }
130
131       function <% $opt{prefix} %>invoice_select_changed(what) {
132
133         var surcharge;
134         var invdue = document.getElementById("<% $opt{prefix} %>inv" + what.value);
135         if (document.getElementById('surcharge_percentage') || document.getElementById('surcharge_flatfee')) {
136           surcharge = (+invdue.value * +document.getElementById('surcharge_percentage').value) + +document.getElementById('surcharge_flatfee').value;
137         }
138         else { surcharge = 0; }
139         var amount = +invdue.value + +surcharge;
140
141         if ( what.value == 'select' ) {
142           $('#payment_amount_row').hide();
143           $('#amount').val('');
144         }
145         else {
146           $('#payment_amount_row').show();
147           $("#<% $opt{prefix} %>invoice option[value='select']").remove();
148           $('#amount').val(amount.toFixed(2));
149           document.getElementById('amount').disabled = true;
150           if (document.getElementById('ajax_surcharge_cell')) {
151             document.getElementById('ajax_surcharge_cell').innerHTML = '<FONT SIZE="+1">A credit card surcharge of <% $money_char %>' + surcharge.toFixed(2) + ' is included in this payment</FONT>';
152           }
153         }
154
155       }
156
157 </SCRIPT>
158
159 <%init>
160
161 my %opt = @_;
162
163 my $cust_main = $opt{'cust_main'};
164 my $amount = $opt{'amount'} ? $opt{'amount'} : $cust_main->balance;
165 my $custnum = $cust_main->custnum;
166
167 my @open_invoices = $cust_main->open_cust_bill;
168
169 my $payment_option_row = "show";
170 my $payment_amount_row = "hide";
171
172 unless ($amount > 0 && @open_invoices) {
173   $payment_option_row = "hide";
174   $payment_amount_row = "show";
175 }
176
177 my $conf = new FS::Conf;
178 my $money_char = $conf->config('money_char') || '$';
179
180 </%init>