summaryrefslogtreecommitdiff
path: root/httemplate/elements/tr-select-discount_term.html
blob: 171c1037a3a7aa32a9dd2bb41aaeafad4aa42710 (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
% if ( scalar(@discount_term) ) {
  <TR>
    <TD ALIGN="right"><% emt('Prepayment for') %></TD>
%  if ( $amount_id ) {
    <SCRIPT type="text/javascript">
var discounted_total = <% encode_json \%discounted_total %>;
function change_discount_term(what) {
  var new_term = what.value;
  var amount_field = document.getElementById('<% $amount_id %>');
  if(new_term == "") {
    amount_field.readOnly = false;
    amount_field.value = '';
  }
  else {
    amount_field.value = discounted_total[new_term];
    amount_field.readOnly = true;
  }
}
</SCRIPT>
% }
    <TD COLSPAN=2>
      <& select.html,
        field   => 'discount_term',
        id      => 'discount_term',
        options => [ '', @discount_term ],
        labels  => { '' => mt('1 month'), 
                     map { $_ => mt('[_1] months', sprintf('%.0f', $_)) }
                      @discount_term
                   },
        curr_value => '',
        onchange => $amount_id ? 'change_discount_term(this)' : '',
      &>
    </TD>
  </TR>

% }
<%init>

my %opt = @_;

my $custnum = $opt{'custnum'};

my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
  or die "unknown custnum $custnum\n";

my @discount_term = ();
my %discounted_total = ();

#this is inefficient for many years worth of invoices
my $last_bill = ($cust_main->cust_bill)[-1];

if ( $last_bill ) { # if not, there are no discounts possible
  my %plans = $last_bill->discount_plans;
  @discount_term = sort { $a <=> $b } keys %plans;
  %discounted_total = map { $_, $plans{$_}->discounted_total } @discount_term;
}

# the DOM id of an input to be disabled/populated with the amount due
my $amount_id = $opt{'amount_id'};

</%init>