summaryrefslogtreecommitdiff
path: root/httemplate/elements/tr-select-payment_options.html
blob: c5b84e7564eb4e5682074b70b236826bb2968a56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<%doc>

Example:

  include( '/elements/tr-select-payment_options.html',

    #opt - most get used in /elements/tr-amount-fee
    'cust_main'            => $cust_main,     # custmain needed for selecting invoices
    'prefix'               => 'pre', # prefix to fields and row ID's
    'amount'               => 1,     # payment amount optional, if no amount will grab balance due from cust_main
    'process-pkgpart'      => scalar($conf->config('manual_process-pkgpart', $cust_main->agentnum)),
    'process-display'      => scalar($conf->config('manual_process-display')),
    'process-skip_first'   => $conf->exists('manual_process-skip_first'),
    'num_payments'         => scalar($cust_main->cust_pay),
    'surcharge_percentage' =>
      ( $payby eq 'CARD'
          ? scalar($conf->config('credit-card-surcharge-percentage', $cust_main->agentnum))
          : 0
      ),
    'surcharge_flatfee' =>
      ( $payby eq 'CARD'
          ? scalar($conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum))
          : 0
      ),
    'processing_fee' => scalar($conf->config('processing-fee', $cust_main->agentnum)),
  )

</%doc>

  <TR ID="payment_option_row">
    <TH ALIGN="right"><% mt('What would you like to pay') |h %></TH>
    <TD>
     <SELECT
  	  ID       = "<% $opt{prefix} %>payment_option"
  	  NAME     = "<% $opt{prefix} %>payment_option"
  	  onChange = "<% $opt{prefix} %>payment_option_changed(this)"
  	  <% $opt{disabled} %>
	   >
      <OPTION VALUE="select">Select the amount you would like to pay</OPTION>
      <% ($amount > 0) ? '<OPTION VALUE="'.$amount.'">Pay full balance</OPTION>' : '' %>
      <% (@open_invoices) ? '<OPTION VALUE="invoice">Pay specific invoice</OPTION>' : '' %>
      <OPTION VALUE="specific">Pay specific amount</OPTION>
	   </SELECT>
    </TD>
  </TR>

  <& /elements/tr-select-invoice.html,
       'cust_main' => $cust_main,
       'status'    => 'open',
       'prefix'    => $opt{prefix},
  &>

  <& /elements/tr-amount_fee.html,
       'amount'     => $amount,
       'custnum'    => $custnum,
       %opt
  &>

  <SCRIPT TYPE="text/javascript">

      $('#payment_option_row').<% $payment_option_row %>();
      $('#payment_amount_row').<% $payment_amount_row %>();
      $('#ajax_processingfee_cell').hide();

      if($('#payment_amount_row').is(':visible')) {
        var surcharge;
        var amount = document.getElementById('amount').value;

        if ((document.getElementById('surcharge_percentage') || document.getElementById('surcharge_flatfee')) && amount > 0) {
          surcharge = (+amount * +document.getElementById('surcharge_percentage').value) + +document.getElementById('surcharge_flatfee').value;
        }
        else { surcharge = 0; }
        if (document.getElementById('ajax_surcharge_cell')) {
          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>';
        }
      }

      function <% $opt{prefix} %>payment_option_changed(what) {

        var surcharge;
        var processingFee = 0;
        var pfElement = document.getElementById('processing_fee');

        if (document.getElementById('surcharge_percentage') || document.getElementById('surcharge_flatfee')) {
          surcharge = (+what.value * +document.getElementById('surcharge_percentage').value) + +document.getElementById('surcharge_flatfee').value;
        }
        else { surcharge = 0; }

        if (pfElement != null) {
          if (pfElement.checked == true) {
           processingFee = +pfElement.value;
          }
        }

        var amount = +what.value + +surcharge + +processingFee;
        document.getElementById('amount').disabled = true;

        if ( what.value == 'select' ) {
          $('#payment_amount_row').hide();
          $('#invoice_row').hide();
          $("#<% $opt{prefix} %>invoice").val('select');
          $('#amount').val('');
        }
        else if ( what.value == 'invoice' ) {
          $('#payment_amount_row').hide();
          $('#invoice_row').show();
          $('#apply_box_row').hide();
          $('#apply_box').val('yes');
          $("#<% $opt{prefix} %>payment_option option[value='select']").remove();
          var selectExists = ($("#<% $opt{prefix} %>invoice option[value='select']").length > 0);
          if(!selectExists) {
            $("#<% $opt{prefix} %>invoice").prepend("<option value='select'>Select an invoice to pay</option>");
            $("#<% $opt{prefix} %>invoice").val($('option:first', "#<% $opt{prefix} %>invoice").val());
          }
          $('#amount').val('');
        }
        else if ( what.value == 'specific' ) {
          $('#payment_amount_row').show();
          $('#invoice_row').hide();
          $('#apply_box_row').show();
          $("#<% $opt{prefix} %>payment_option option[value='select']").remove();
          $('#amount').val('0.00');
          document.getElementById('amount').disabled = false;
          if (document.getElementById('ajax_surcharge_cell')) {
            document.getElementById('ajax_surcharge_cell').innerHTML = '<FONT SIZE="+1">A credit card surcharge of <% $money_char %>0.00 is included in this payment</FONT>';
          }
        }
        else {
          $('#payment_amount_row').show();
          $('#invoice_row').hide();
          $('#apply_box_row').hide();
          $('#apply_box').val('yes');
          $("#<% $opt{prefix} %>payment_option option[value='select']").remove();
          $('#amount').val(amount.toFixed(2));
          document.getElementById('amount').disabled = true;
          if (document.getElementById('ajax_surcharge_cell')) {
            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>';
          }
        }

      }

      function <% $opt{prefix} %>invoice_select_changed(what) {

        var surcharge;
        var processingFee = 0;
        var pfElement = document.getElementById('processing_fee');
        var invdue = document.getElementById("<% $opt{prefix} %>inv" + what.value);
        if (document.getElementById('surcharge_percentage') || document.getElementById('surcharge_flatfee')) {
          surcharge = (+invdue.value * +document.getElementById('surcharge_percentage').value) + +document.getElementById('surcharge_flatfee').value;
        }
        else { surcharge = 0; }

        if (pfElement != null) {
          if (pfElement.checked == true) {
           processingFee = +pfElement.value;
          }
        }

        var amount = +invdue.value + +surcharge + +processingFee;

        if ( what.value == 'select' ) {
          $('#payment_amount_row').hide();
          $('#amount').val('');
        }
        else {
          $('#payment_amount_row').show();
          $("#<% $opt{prefix} %>invoice option[value='select']").remove();
          $('#amount').val(amount.toFixed(2));
          document.getElementById('amount').disabled = true;
          if (document.getElementById('ajax_surcharge_cell')) {
            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>';
          }
        }

      }

      function <% $opt{prefix} %>process_fee_changed(what) {

        if (document.getElementById('processing_fee').checked == true) {
          var amount = +document.getElementById('amount').value + +document.getElementById('processing_fee').value;
          $('#amount').val(amount.toFixed(2));
          $('#ajax_processingfee_cell').show();
        }
        else {
          var amount = +document.getElementById('amount').value - +document.getElementById('processing_fee').value;
          $('#amount').val(amount.toFixed(2));
          $('#ajax_processingfee_cell').hide();
        }

      }

</SCRIPT>

<%init>

my %opt = @_;

my $cust_main = $opt{'cust_main'};
my $amount = $opt{'amount'} ? $opt{'amount'} : $cust_main->balance;
my $custnum = $cust_main->custnum;

my @open_invoices = $cust_main->open_cust_bill;

my $payment_option_row = "show";
my $payment_amount_row = "hide";

unless ($amount > 0 && @open_invoices) {
  $payment_option_row = "hide";
  $payment_amount_row = "show";
}

my $conf = new FS::Conf;
my $money_char = $conf->config('money_char') || '$';

</%init>