RT# 79284 Option to set discount at Change Package
[freeside.git] / httemplate / elements / select-discount.html
1 <% include( '/elements/select-table.html',
2                  'table'        => 'discount',
3                  'name_col'     => 'description',
4                  'order_by'     => 'ORDER BY discountnum', #XXX weight
5                  'value'        => $discountnum,
6                  'empty_label'  => '(none)',
7                  'hashref'      => $hashref,
8                  'post_options' => $post_options,
9                  'label_callback' => $label_callback,
10                  %opt,
11              )
12 %>
13 <%init>
14
15 my %opt = @_;
16 my $discountnum = $opt{'curr_value'} || $opt{'value'};
17
18 $opt{'records'} = delete $opt{'discount'}
19   if $opt{'discount'};
20
21 my $curuser = $FS::CurrentUser::CurrentUser;
22
23 my $hashref = $opt{hashref} || { 'disabled' => '' };
24
25 my $post_options = [];
26
27 # If a 'custom' discount is applied to existing cust_pkg,
28 # also add that option to the selectbox
29 my $pkgnum = $cgi ? $cgi->param('pkgnum') : undef;
30 if ($pkgnum) {
31
32   # Does pkgnum have a custom discount?
33   my $cust_pkg_discount = qsearchs(cust_pkg_discount => {
34     disabled => '',
35     pkgnum   => $pkgnum,
36   });
37
38   if ($cust_pkg_discount) {
39     my $discount = qsearchs(discount => {
40       discountnum => $cust_pkg_discount->discountnum,
41       disabled    => 'Y',
42     });
43     if ($discount) {
44       my $descr = $opt{carry_value} eq $discount->discountnum
45         ? $discount->description.'  [Continue existing discount]'
46         : $discount->description;
47       push @$post_options, $discount->discountnum, $descr;
48     }
49   }
50 }
51
52 push @$post_options,  -1 => 'Custom discount'
53   if $curuser->access_right('Custom discount customer package')
54   && ! $opt{disable_custom_discount};
55
56 my $label_callback = sub {
57   my $rec = shift;
58   if ( $opt{carry_value} eq $rec->discountnum ) {
59     return $rec->description.'  [Continue existing discount]';
60   }
61   return $rec->description;
62 };
63
64 </%init>