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