4.x style
[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   disable_waive_setup => 0,
11   carry_value_setup   => $carry_setup_discountnum,
12   carry_value_recur   => $carry_recur_discountnum,
13 &>
14
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
20   amount/percentage.
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
27
28 "disable_setup" locks the setup discount, but will still show a static
29 description if curr_value_setup is set. Likewise "disable_recur".
30
31 </%doc>
32 % # SETUP DISCOUNT
33
34 % # select-discount knows about the "custom discount" ACL
35 % if ( $curuser->access_right('Discount customer package')
36 %      and !$opt{disable_setup} )
37 % {
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') &>
43   <td>
44     <& select-discount.html,
45       field       => 'setup_discountnum',
46       id          => 'setup_discountnum',
47       hashref     =>  { disabled => '',
48                         setup    => 'Y'
49                       },
50       extra_sql   =>  ' AND (percent > 0 OR months = 1)',
51       curr_value  => $opt{'curr_value_setup'},
52       disable_empty => 1,
53       pre_options => $pre_options,
54       also_show_option => $opt{carry_value_setup},
55       label_callback   => $opt{carry_value_setup} ? $carry_setup_sub : undef,
56     &>
57   </td>
58 </tr>
59 % # custom discount
60 <tr class="setup_discount_custom">
61   <td></td>
62   <td>Amount <% $money_char %>
63     <& input-text.html,
64       field       => 'setup_discountnum_amount',
65       curr_value  => ($cgi->param('setup_discountnum_amount') || ''),
66       size        => 5,
67     &>
68   or percentage
69     <& input-text.html,
70       field       => 'setup_discountnum_percent',
71       curr_value  => ($cgi->param('setup_discountnum_percent') || ''),
72       size        => 5,
73     &> %
74   </td>
75 </tr>
76
77 % } elsif ( $curuser->access_right('Waive setup fee')
78 %             && !$opt{disable_waive_setup}
79 %             && !$opt{disable_setup}
80 %         )
81 % {
82
83 <& tr-td-label.html, label => emt('Waive setup fee') &>
84   <td>
85   <& checkbox.html,
86       field       => 'setup_discountnum',
87       id          => 'setup_discountnum',
88       value       => '-2',
89       curr_value  => $opt{'curr_value_setup'},
90   &>
91   </td>
92 </tr>
93
94 % } elsif ( $opt{'curr_value_setup'} ) { # user can't do anything
95 %
96 %   my $discount = FS::discount->by_key($opt{'curr_value_setup'});
97
98   <INPUT TYPE="hidden" NAME="setup_discountnum" VALUE="<% $opt{curr_value_setup} %>">
99
100   <% $discount->description_short %>
101
102 % }
103
104 % # RECUR DISCOUNT
105
106 % if ( $curuser->access_right('Discount customer package')
107 %      and !$opt{disable_recur} ) {
108
109 <& tr-td-label.html, label => $opt{recur_label} || emt('Recurring fee') &>
110   <td>
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,
118     &>
119
120   </td>
121 </tr>
122 % # custom discount
123 <tr class="recur_discount_custom">
124   <td></td>
125   <td>Amount <% $money_char %>
126     <& input-text.html,
127       field       => 'recur_discountnum_amount',
128       curr_value  => ($cgi->param('recur_discountnum_amount') || ''),
129       size        => 5,
130     &>
131   or percentage
132     <& input-text.html,
133       field       => 'recur_discountnum_percent',
134       curr_value  => ($cgi->param('recur_discountnum_percent') || ''),
135       size        => 5,
136     &> %
137   </td>
138 </tr>
139 <tr class="recur_discount_custom">
140   <td></td>
141   <td>Expires after
142     <& /elements/select-months.html,
143       field       => 'recur_discountnum_months',
144       curr_value  => ($cgi->param('recur_discountnum_months') || ''),
145     &>
146   </td>
147 </tr>
148
149 % } elsif ( $opt{'curr_value_recur'} ) {
150 %
151 %   my $discount = FS::discount->by_key($opt{'curr_value_recur'});
152
153   <INPUT TYPE="hidden" NAME="recur_discountnum" VALUE="<% $opt{curr_value_recur} %>">
154
155   <% $discount->description %>
156
157 % }
158
159 <SCRIPT TYPE="text/javascript">
160 $(document).ready(function() {
161   ['setup', 'recur'].forEach(function(x) {
162     var discountnum = $('#'+x+'_discountnum');
163
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');
168       if ( val == -1 ) {
169         custom.show();
170       } else {
171         custom.hide();
172       }
173     };
174
175     discountnum.on('change', discountnum_changed);
176     discountnum.trigger('change');
177
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;
183       } else {
184         other.disabled = false;
185       }
186     };
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);
191
192     amount.trigger('keyup');
193     percent.trigger('keyup');
194   });
195 });
196 </script>
197 <%init>
198
199 my %opt = (
200   'curr_value_setup' => ($cgi->param('setup_discountnum') || ''),
201   'curr_value_recur' => ($cgi->param('recur_discountnum') || ''),
202   @_
203 );
204 my $curuser = $FS::CurrentUser::CurrentUser;
205 my $money_char = FS::Conf->new->config('money_char') || '$';
206
207 # Change the label for the select-option when it matches the carryover
208 # discountnum
209 my $carry_recur_sub =  sub {
210   my ($rec) = @_;
211   $rec->discountnum eq $opt{carry_value_recur}
212     ? $rec->description() . ' [Continues existing discount]'
213     : $rec->description();
214 };
215 my $carry_setup_sub =  sub {
216   my ($rec) = @_;
217   $rec->discountnum eq $opt{carry_value_setup}
218     ? $rec->description() . ' [Continues existing discount]'
219     : $rec->description();
220 };
221 </%init>