summaryrefslogtreecommitdiff
path: root/httemplate/elements/tr-select-pkg-discount.html
blob: 810e462303728946038fb59386aa095be4bc093c (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
217
218
219
220
221
<%doc>

In order_pkg.html or similar:

<& /elements/tr-select-pkg-discount.html,
  curr_value_setup    => ($cgi->param('setup_discountnum') || ''),
  curr_value_recur    => ($cgi->param('recur_discountnum') || ''),
  disable_setup       => 0,
  disable_recur       => 0,
  disable_waive_setup => 0,
  carry_value_setup   => $carry_setup_discountnum,
  carry_value_recur   => $carry_recur_discountnum,
&>

This provides the following:
- If the user can waive setup fees or apply a discount, they get a 
  select box for the setup discount, with "Waive setup fee" as an option.
- If they can custom discount, they will also get "Custom discount" as an
  option. If selected, this will show fields to enter the custom discount
  amount/percentage.
- If they can waive setup fees but NOT apply a discount, they only get a
  checkbox to waive setup fee.
- Same for recurring fee, but without the "waive setup fee" stuff, obviously.
- Custom recurring discounts also have an option for a duration in months.
- When changing a package, the original package's discount option is
  labeled in the select options

"disable_setup" locks the setup discount, but will still show a static
description if curr_value_setup is set. Likewise "disable_recur".

</%doc>
% # SETUP DISCOUNT

% # select-discount knows about the "custom discount" ACL
% if ( $curuser->access_right('Discount customer package')
%      and !$opt{disable_setup} )
% {
%   my $pre_options = [ '' => '(none)' ];
%   push @$pre_options, -2 => 'Waive setup fee'
%     if $curuser->access_right('Waive setup fee')
%     && ! $opt{'disable_waive_setup'};
<& tr-td-label.html, label => $opt{setup_label} || emt('Setup fee') &>
  <td>
    <& select-discount.html,
      field       => 'setup_discountnum',
      id          => 'setup_discountnum',
      hashref     =>  { disabled => '',
                        setup    => 'Y'
                      },
      extra_sql   =>  ' AND (percent > 0 OR months = 1)',
      curr_value  => $opt{'curr_value_setup'},
      disable_empty => 1,
      pre_options => $pre_options,
      also_show_option => $opt{carry_value_setup},
      label_callback   => $opt{carry_value_setup} ? $carry_setup_sub : undef,
    &>
  </td>
</tr>
% # custom discount
<tr class="setup_discount_custom">
  <td></td>
  <td>Amount <% $money_char %>
    <& input-text.html,
      field       => 'setup_discountnum_amount',
      curr_value  => ($cgi->param('setup_discountnum_amount') || ''),
      size        => 5,
    &>
  or percentage
    <& input-text.html,
      field       => 'setup_discountnum_percent',
      curr_value  => ($cgi->param('setup_discountnum_percent') || ''),
      size        => 5,
    &> %
  </td>
</tr>

% } elsif ( $curuser->access_right('Waive setup fee')
%             && !$opt{disable_waive_setup}
%             && !$opt{disable_setup}
%         )
% {

<& tr-td-label.html, label => emt('Waive setup fee') &>
  <td>
  <& checkbox.html,
      field       => 'setup_discountnum',
      id          => 'setup_discountnum',
      value       => '-2',
      curr_value  => $opt{'curr_value_setup'},
  &>
  </td>
</tr>

% } elsif ( $opt{'curr_value_setup'} ) { # user can't do anything
%
%   my $discount = FS::discount->by_key($opt{'curr_value_setup'});

  <INPUT TYPE="hidden" NAME="setup_discountnum" VALUE="<% $opt{curr_value_setup} %>">

  <% $discount->description_short %>

% }

% # RECUR DISCOUNT

% if ( $curuser->access_right('Discount customer package')
%      and !$opt{disable_recur} ) {

<& tr-td-label.html, label => $opt{recur_label} || emt('Recurring fee') &>
  <td>
    <& select-discount.html,
      field       => 'recur_discountnum',
      id          => 'recur_discountnum',
      hashref     =>  { disabled => '' },
      curr_value  => $opt{'curr_value_recur'},
      also_show_option => $opt{carry_value_recur},
      label_callback   => $opt{carry_value_recur} ? $carry_recur_sub : undef,
    &>

  </td>
</tr>
% # custom discount
<tr class="recur_discount_custom">
  <td></td>
  <td>Amount <% $money_char %>
    <& input-text.html,
      field       => 'recur_discountnum_amount',
      curr_value  => ($cgi->param('recur_discountnum_amount') || ''),
      size        => 5,
    &>
  or percentage
    <& input-text.html,
      field       => 'recur_discountnum_percent',
      curr_value  => ($cgi->param('recur_discountnum_percent') || ''),
      size        => 5,
    &> %
  </td>
</tr>
<tr class="recur_discount_custom">
  <td></td>
  <td>Expires after
    <& /elements/select-months.html,
      field       => 'recur_discountnum_months',
      curr_value  => ($cgi->param('recur_discountnum_months') || ''),
    &>
  </td>
</tr>

% } elsif ( $opt{'curr_value_recur'} ) {
%
%   my $discount = FS::discount->by_key($opt{'curr_value_recur'});

  <INPUT TYPE="hidden" NAME="recur_discountnum" VALUE="<% $opt{curr_value_recur} %>">

  <% $discount->description %>

% }

<SCRIPT TYPE="text/javascript">
$(document).ready(function() {
  ['setup', 'recur'].forEach(function(x) {
    var discountnum = $('#'+x+'_discountnum');

    // if it's been set to a custom discount, show custom discount inputs
    var discountnum_changed = function() {
      var val = this.value;
      var custom = $('.'+x+'_discount_custom');
      if ( val == -1 ) {
        custom.show();
      } else {
        custom.hide();
      }
    };

    discountnum.on('change', discountnum_changed);
    discountnum.trigger('change');

    // if amount contains a value, disable percent, and vice versa
    var amount_percent_keyup = function(event) {
      var other = event.data;
      if (this.value.length > 0) {
        other.disabled = true;
      } else {
        other.disabled = false;
      }
    };
    var amount = $('#'+x+'_discountnum_amount');
    var percent = $('#'+x+'_discountnum_percent');
    amount.on('keyup', percent, amount_percent_keyup);
    percent.on('keyup', amount, amount_percent_keyup);

    amount.trigger('keyup');
    percent.trigger('keyup');
  });
});
</script>
<%init>

my %opt = (
  'curr_value_setup' => ($cgi->param('setup_discountnum') || ''),
  'curr_value_recur' => ($cgi->param('recur_discountnum') || ''),
  @_
);
my $curuser = $FS::CurrentUser::CurrentUser;
my $money_char = FS::Conf->new->config('money_char') || '$';

# Change the label for the select-option when it matches the carryover
# discountnum
my $carry_recur_sub =  sub {
  my ($rec) = @_;
  $rec->discountnum eq $opt{carry_value_recur}
    ? $rec->description() . ' [Continues existing discount]'
    : $rec->description();
};
my $carry_setup_sub =  sub {
  my ($rec) = @_;
  $rec->discountnum eq $opt{carry_value_setup}
    ? $rec->description() . ' [Continues existing discount]'
    : $rec->description();
};
</%init>