3 In order_pkg.html or similar:
5 <& /elements/tr-select-pkg-discount.html,
6 curr_value_setup => ($cgi->param('setup_discountnum') || ''),
7 curr_value_recur => ($cgi->param('recur_discountnum') || ''),
10 disable_waive_setup => 0,
11 carry_value_setup => $carry_setup_discountnum,
12 carry_value_recur => $carry_recur_discountnum,
15 This provides the following:
16 - If the user can waive setup fees or apply a discount, they get a
17 select box for the setup discount, with "Waive setup fee" as an option.
18 - If they can custom discount, they will also get "Custom discount" as an
19 option. If selected, this will show fields to enter the custom discount
21 - If they can waive setup fees but NOT apply a discount, they only get a
22 checkbox to waive setup fee.
23 - Same for recurring fee, but without the "waive setup fee" stuff, obviously.
24 - Custom recurring discounts also have an option for a duration in months.
25 - When changing a package, the original package's discount option is
26 labeled in the select options
28 "disable_setup" locks the setup discount, but will still show a static
29 description if curr_value_setup is set. Likewise "disable_recur".
34 % # select-discount knows about the "custom discount" ACL
35 % if ( $curuser->access_right('Discount customer package')
36 % and !$opt{disable_setup} )
38 % my $pre_options = [ '' => '(none)' ];
39 % push @$pre_options, -2 => 'Waive setup fee'
40 % if $curuser->access_right('Waive setup fee')
41 % && ! $opt{'disable_waive_setup'};
42 <& tr-td-label.html, label => $opt{setup_label} || emt('Setup fee') &>
44 <& select-discount.html,
45 field => 'setup_discountnum',
46 id => 'setup_discountnum',
47 hashref => { disabled => '',
50 extra_sql => ' AND (percent > 0 OR months = 1)',
51 curr_value => $opt{'curr_value_setup'},
53 pre_options => $pre_options,
54 also_show_option => $opt{carry_value_setup},
55 label_callback => $opt{carry_value_setup} ? $carry_setup_sub : undef,
60 <tr class="setup_discount_custom">
62 <td>Amount <% $money_char %>
64 field => 'setup_discountnum_amount',
65 curr_value => ($cgi->param('setup_discountnum_amount') || ''),
70 field => 'setup_discountnum_percent',
71 curr_value => ($cgi->param('setup_discountnum_percent') || ''),
77 % } elsif ( $curuser->access_right('Waive setup fee')
78 % && !$opt{disable_waive_setup}
79 % && !$opt{disable_setup}
83 <& tr-td-label.html, label => emt('Waive setup fee') &>
86 field => 'setup_discountnum',
87 id => 'setup_discountnum',
89 curr_value => $opt{'curr_value_setup'},
94 % } elsif ( $opt{'curr_value_setup'} ) { # user can't do anything
96 % my $discount = FS::discount->by_key($opt{'curr_value_setup'});
98 <INPUT TYPE="hidden" NAME="setup_discountnum" VALUE="<% $opt{curr_value_setup} %>">
100 <% $discount->description_short %>
106 % if ( $curuser->access_right('Discount customer package')
107 % and !$opt{disable_recur} ) {
109 <& tr-td-label.html, label => $opt{recur_label} || emt('Recurring fee') &>
111 <& select-discount.html,
112 field => 'recur_discountnum',
113 id => 'recur_discountnum',
114 hashref => { disabled => '' },
115 curr_value => $opt{'curr_value_recur'},
116 also_show_option => $opt{carry_value_recur},
117 label_callback => $opt{carry_value_recur} ? $carry_recur_sub : undef,
123 <tr class="recur_discount_custom">
125 <td>Amount <% $money_char %>
127 field => 'recur_discountnum_amount',
128 curr_value => ($cgi->param('recur_discountnum_amount') || ''),
133 field => 'recur_discountnum_percent',
134 curr_value => ($cgi->param('recur_discountnum_percent') || ''),
139 <tr class="recur_discount_custom">
142 <& /elements/select-months.html,
143 field => 'recur_discountnum_months',
144 curr_value => ($cgi->param('recur_discountnum_months') || ''),
149 % } elsif ( $opt{'curr_value_recur'} ) {
151 % my $discount = FS::discount->by_key($opt{'curr_value_recur'});
153 <INPUT TYPE="hidden" NAME="recur_discountnum" VALUE="<% $opt{curr_value_recur} %>">
155 <% $discount->description %>
159 <SCRIPT TYPE="text/javascript">
160 $(document).ready(function() {
161 ['setup', 'recur'].forEach(function(x) {
162 var discountnum = $('#'+x+'_discountnum');
164 // if it's been set to a custom discount, show custom discount inputs
165 var discountnum_changed = function() {
166 var val = this.value;
167 var custom = $('.'+x+'_discount_custom');
175 discountnum.on('change', discountnum_changed);
176 discountnum.trigger('change');
178 // if amount contains a value, disable percent, and vice versa
179 var amount_percent_keyup = function(event) {
180 var other = event.data;
181 if (this.value.length > 0) {
182 other.disabled = true;
184 other.disabled = false;
187 var amount = $('#'+x+'_discountnum_amount');
188 var percent = $('#'+x+'_discountnum_percent');
189 amount.on('keyup', percent, amount_percent_keyup);
190 percent.on('keyup', amount, amount_percent_keyup);
192 amount.trigger('keyup');
193 percent.trigger('keyup');
200 'curr_value_setup' => ($cgi->param('setup_discountnum') || ''),
201 'curr_value_recur' => ($cgi->param('recur_discountnum') || ''),
204 my $curuser = $FS::CurrentUser::CurrentUser;
205 my $money_char = FS::Conf->new->config('money_char') || '$';
207 # Change the label for the select-option when it matches the carryover
209 my $carry_recur_sub = sub {
211 $rec->discountnum eq $opt{carry_value_recur}
212 ? $rec->description() . ' [Continues existing discount]'
213 : $rec->description();
215 my $carry_setup_sub = sub {
217 $rec->discountnum eq $opt{carry_value_setup}
218 ? $rec->description() . ' [Continues existing discount]'
219 : $rec->description();