separate setup and recur discounts, #14092
[freeside.git] / httemplate / elements / tr-select-pkg-discount.html
1 <%doc>
2
3 In order_pkg.html or similar:
4
5 <& /elements/tr-select-pkg-discount.html,
6   curr_value_setup => ($cgi->param('setup_discountnum') || ''),
7   curr_value_recur => ($cgi->param('recur_discountnum') || ''),
8   disable_setup    => 0,
9   disable_recur    => 0,
10 &>
11
12 This provides the following:
13 - If the user can waive setup fees or apply a discount, they get a 
14   select box for the setup discount, with "Waive setup fee" as an option.
15 - If they can custom discount, they will also get "Custom discount" as an
16   option. If selected, this will show fields to enter the custom discount
17   amount/percentage.
18 - If they can waive setup fees but NOT apply a discount, they only get a
19   checkbox to waive setup fee.
20 - Same for recurring fee, but without the "waive setup fee" stuff, obviously.
21 - Custom recurring discounts also have an option for a duration in months.
22
23 "disable_setup" locks the setup discount, but will still show a static
24 description if curr_value_setup is set. Likewise "disable_recur".
25
26 </%doc>
27 % # SETUP DISCOUNT
28
29 % # select-discount knows about the "custom discount" ACL
30 % if ( $curuser->access_right('Discount customer package')
31 %      and !$opt{disable_setup} )
32 % {
33 %   my $pre_options = [ '' => '(none)' ];
34 %   if ( $curuser->access_right('Waive setup fee') ) {
35 %     push @$pre_options, -2 => 'Waive setup fee';
36 %   }
37 <& tr-td-label.html, label => emt('Setup fee') &>
38   <td>
39     <& select-discount.html,
40       field       => 'setup_discountnum',
41       id          => 'setup_discountnum',
42       hashref     =>  { disabled => '',
43                         setup    => 'Y'
44                       },
45       extra_sql   =>  ' AND (percent > 0 OR months = 1)',
46       curr_value  => $opt{'curr_value_setup'},
47       disable_empty => 1,
48       pre_options => $pre_options,
49     &>
50   </td>
51 </tr>
52 % # custom discount
53 <tr class="setup_discount_custom">
54   <td></td>
55   <td>Amount <% $money_char %>
56     <& input-text.html,
57       field       => 'setup_discountnum_amount',
58       curr_value  => ($cgi->param('setup_discountnum_amount') || ''),
59       size        => 5,
60     &>
61   or percentage
62     <& input-text.html,
63       field       => 'setup_discountnum_percent',
64       curr_value  => ($cgi->param('setup_discountnum_percent') || ''),
65       size        => 5,
66     &> %
67   </td>
68 </tr>
69
70 % } elsif ( $curuser->access_right('Waive setup fee')
71 %           and !$opt{disable_setup} )
72 % {
73
74 <& tr-td-label.html, label => emt('Waive setup fee') &>
75   <td>
76   <& checkbox.html,
77       field       => 'setup_discountnum',
78       id          => 'setup_discountnum',
79       value       => '-2',
80       curr_value  => $opt{'curr_value_setup'},
81   &>
82   </td>
83 </tr>
84
85 % } elsif ( $opt{'curr_value_setup'} ) { # user can't do anything
86 %
87 %   my $discount = FS::discount->by_key($opt{'curr_value_setup'});
88
89   <INPUT TYPE="hidden" NAME="setup_discountnum" VALUE="<% $opt{curr_value_setup} %>">
90
91   <% $discount->description_short %>
92
93 % }
94
95 % # RECUR DISCOUNT
96
97 % if ( $curuser->access_right('Discount customer package')
98 %      and !$opt{disable_recur} ) {
99
100 <& tr-td-label.html, label => emt('Recurring fee') &>
101   <td>
102     <& select-discount.html,
103       field       => 'recur_discountnum',
104       id          => 'recur_discountnum',
105       hashref     =>  { disabled => '' },
106       curr_value  => $opt{'curr_value_recur'},
107     &>
108
109   </td>
110 </tr>
111 % # custom discount
112 <tr class="recur_discount_custom">
113   <td></td>
114   <td>Amount <% $money_char %>
115     <& input-text.html,
116       field       => 'recur_discountnum_amount',
117       curr_value  => ($cgi->param('recur_discountnum_amount') || ''),
118       size        => 5,
119     &>
120   or percentage
121     <& input-text.html,
122       field       => 'recur_discountnum_percent',
123       curr_value  => ($cgi->param('recur_discountnum_percent') || ''),
124       size        => 5,
125     &> %
126   </td>
127 </tr>
128 <tr class="recur_discount_custom">
129   <td></td>
130   <td>Expires after
131     <& /elements/select-months.html,
132       field       => 'recur_discountnum_months',
133       curr_value  => ($cgi->param('recur_discountnum_months') || ''),
134     &>
135   </td>
136 </tr>
137
138 % } elsif ( $opt{'curr_value_recur'} ) {
139 %
140 %   my $discount = FS::discount->by_key($opt{'curr_value_recur'});
141
142   <INPUT TYPE="hidden" NAME="recur_discountnum" VALUE="<% $opt{curr_value_recur} %>">
143
144   <% $discount->description %>
145
146 % }
147
148 <SCRIPT TYPE="text/javascript">
149 $(document).ready(function() {
150   ['setup', 'recur'].forEach(function(x) {
151     var discountnum = $('#'+x+'_discountnum');
152
153     // if it's been set to a custom discount, show custom discount inputs
154     var discountnum_changed = function() {
155       var val = this.value;
156       var custom = $('.'+x+'_discount_custom');
157       if ( val == -1 ) {
158         custom.show();
159       } else {
160         custom.hide();
161       }
162     };
163
164     discountnum.on('change', discountnum_changed);
165     discountnum.trigger('change');
166
167     // if amount contains a value, disable percent, and vice versa
168     var amount_percent_keyup = function(event) {
169       var other = event.data;
170       if (this.value.length > 0) {
171         other.disabled = true;
172       } else {
173         other.disabled = false;
174       }
175     };
176     var amount = $('#'+x+'_discountnum_amount');
177     var percent = $('#'+x+'_discountnum_percent');
178     amount.on('keyup', percent, amount_percent_keyup);
179     percent.on('keyup', amount, amount_percent_keyup);
180
181     amount.trigger('keyup');
182     percent.trigger('keyup');
183   });
184 });
185 </script>
186 <%init>
187
188 my %opt = (
189   'curr_value_setup' => ($cgi->param('setup_discountnum') || ''),
190   'curr_value_recur' => ($cgi->param('recur_discountnum') || ''),
191   @_
192 );
193 my $curuser = $FS::CurrentUser::CurrentUser;
194 my $money_char = FS::Conf->new->config('money_char') || '$';
195
196 </%init>