summaryrefslogtreecommitdiff
path: root/httemplate/elements/tr-select-discount.html
blob: ee862519f7705b7505c8b988c51d0e3723fe8b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
% if ( scalar(@{ $opt{'discount'} }) == 0
%      && ! $curuser->access_right('Custom discount customer package') ) { 

  <INPUT TYPE="hidden" NAME="<% $name %>" VALUE="<% $discountnum %>">

% } else { 

  <TR>
    <TD ALIGN="right" WIDTH="275"><% $opt{'label'} || '<B>'.emt('Discount').'</B>' %></TD>
    <TD <% $colspan %>>
      <% include( '/elements/select-discount.html',
                    'curr_value' => $discountnum,
                    'onchange'   => $onchange,
                    %opt,
                )
      %>
    </TD>
  </TR>

% # a weird kind of false laziness w/edit/discount.html

% #  <INPUT TYPE="hidden" NAME="<% $name %>_disabled" VALUE="Y">


  <% include( '/elements/tr-select.html',
                'label'      => '<B>Discount Type</B>',
                'field'      => $name. '__type',
                'id'         => $name. '__type',
                'options'    => \@_type_options,
                'curr_value' => scalar($cgi->param($name.'__type')),
                'onchange'   => $name.'__type_changed',
                'colspan'    => $opt{'colspan'},
            )
  %>

  <% include( '/elements/tr-input-money.html',
                'label'      => '<B>Discount Amount&nbsp;</B>',
                'field'      => $name. '_amount',
                'id'         => $name. '_amount',
                'default'    => '0.00',
                'curr_value' => scalar($cgi->param($name.'_amount')),
                'colspan'    => $opt{'colspan'},
            )
  %>

  <% include( '/elements/tr-input-percentage.html',
                'label'      => '<B>Discount Percentage</B>',
                'field'      => $name. '_percent',
                'id'         => $name. '_percent',
                'default'    => '0',
                'curr_value' => scalar($cgi->param($name.'_percent')),
                'colspan'    => $opt{'colspan'},
            )
  %>

  <% include( '/elements/tr-input-text.html',
                'label'   => '<B>Discount duration (months)</B>',
                'field'   => $name. '_months',
                'id'      => $name. '_months',
                'size'    => 2,
                'postfix' => qq(<FONT SIZE="-1" ID="${name}_months_postfix"><I>(blank for non-expiring discount)</I></FONT>),
                'curr_value' => scalar($cgi->param($name.'_months')),
                'colspan'    => $opt{'colspan'},
           )
  %>

  <% include( '/elements/tr-checkbox.html',
                'label'     => '<B>Apply discount to setup fee</B>',
                'field'     => $name.'_setup',
                'id'        => $name.'_setup',
                'curr_value' => scalar($cgi->param($name.'_setup')),
                'value'     => 'Y',
                'colspan'    => $opt{'colspan'},
            )
  %>

%#  <% include( '/elements/tr-checkbox.html',
%#                'label'     => '<B>Apply discount to add-on packages</B>',
%#                'field'     => $name.'_linked',
%#                'id'        => $name.'_linked',
%#                'curr_value' => scalar($cgi->param($name.'_linked')),
%#                'value'     => 'Y',
%#                'colspan'    => $opt{'colspan'},
%#            )
%#  %>

  <SCRIPT TYPE="text/javascript">

%   my $ge = 'document.getElementById';

    function <% $name %>_changed(what) {
      var <% $name %> = what.options[what.selectedIndex].value;

      if ( <% $name %> == '-1' && ! what.disabled ) {
        <% $ge %>('<% $name %>__type_label0').style.display = '';
        <% $ge %>('<% $name %>__type_label0').style.visibility = '';
        <% $ge %>('<% $name %>__type').style.display = '';
        <% $ge %>('<% $name %>__type').style.visibility = '';

        <% $name %>__type_changed( <% $ge %>('<% $name %>__type') );

        <% $ge %>('<% $name %>_months_label0').style.display = '';
        <% $ge %>('<% $name %>_months_label0').style.visibility = '';
        <% $ge %>('<% $name %>_months').style.display = '';
        <% $ge %>('<% $name %>_months').style.visibility = '';
        <% $ge %>('<% $name %>_months_postfix').style.display = '';
        <% $ge %>('<% $name %>_months_postfix').style.visibility = '';
        <% $ge %>('<% $name %>_setup_label0').style.display = '';
        <% $ge %>('<% $name %>_setup_label0').style.visibility = '';
        <% $ge %>('<% $name %>_setup').style.display = '';
        <% $ge %>('<% $name %>_setup').style.visibility = '';
      } else {

        <% $ge %>('<% $name %>__type_label0').style.display = 'none';
        <% $ge %>('<% $name %>__type_label0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>__type').style.display = 'none';
        <% $ge %>('<% $name %>__type').style.visibility = 'hidden';

        <% $name %>__type_changed( <% $ge %>('<% $name %>__type') );

        <% $ge %>('<% $name %>_months_label0').style.display = 'none';
        <% $ge %>('<% $name %>_months_label0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_months').style.display = 'none';
        <% $ge %>('<% $name %>_months').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_months_postfix').style.display = 'none';
        <% $ge %>('<% $name %>_months_postfix').style.visibility = 'hidden';
        
        <% $ge %>('<% $name %>_setup_label0').style.display = 'none';
        <% $ge %>('<% $name %>_setup_label0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_setup').style.display = 'none';
        <% $ge %>('<% $name %>_setup').style.visibility = 'hidden';

      }

    }

    function <% $name %>__type_changed(what) {
      var <% $name %>__type = what.options[what.selectedIndex].value;

      if ( <% $name %>__type == '<% $select %>' || what.style.display == 'none' ) {
        <% $ge %>('<% $name %>_amount_label0').style.display = 'none';
        <% $ge %>('<% $name %>_amount_label0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_amount_input0').style.display = 'none';
        <% $ge %>('<% $name %>_amount_input0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_percent_label0').style.display = 'none';
        <% $ge %>('<% $name %>_percent_label0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_percent_input0').style.display = 'none';
        <% $ge %>('<% $name %>_percent_input0').style.visibility = 'hidden';
//        <% $ge %>('<% $name %>_linked_label0').style.display = 'none';
//        <% $ge %>('<% $name %>_linked_label0').style.visibility = 'hidden';
//        <% $ge %>('<% $name %>_linked').style.display = 'none';
//        <% $ge %>('<% $name %>_linked').style.visibility = 'hidden';
      } else if ( <% $name %>__type == 'Amount' ) {
        <% $ge %>('<% $name %>_amount_label0').style.display = '';
        <% $ge %>('<% $name %>_amount_label0').style.visibility = '';
        <% $ge %>('<% $name %>_amount_input0').style.display = '';
        <% $ge %>('<% $name %>_amount_input0').style.visibility = '';
        <% $ge %>('<% $name %>_percent_label0').style.display = 'none';
        <% $ge %>('<% $name %>_percent_label0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_percent_input0').style.display = 'none';
        <% $ge %>('<% $name %>_percent_input0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_percent_input0').style.visibility = 'hidden';
//        <% $ge %>('<% $name %>_linked_label0').style.display = 'none';
//        <% $ge %>('<% $name %>_linked_label0').style.visibility = 'hidden';
//        <% $ge %>('<% $name %>_linked').style.display = 'none';
//        <% $ge %>('<% $name %>_linked').style.visibility = 'hidden';
      } else if ( <% $name %>__type == 'Percentage' ) {
        <% $ge %>('<% $name %>_amount_label0').style.display = 'none';
        <% $ge %>('<% $name %>_amount_label0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_amount_input0').style.display = 'none';
        <% $ge %>('<% $name %>_amount_input0').style.visibility = 'hidden';
        <% $ge %>('<% $name %>_percent_label0').style.display = '';
        <% $ge %>('<% $name %>_percent_label0').style.visibility = '';
        <% $ge %>('<% $name %>_percent_input0').style.display = '';
        <% $ge %>('<% $name %>_percent_input0').style.visibility = '';
        <% $ge %>('<% $name %>_percent_input0').style.visibility = '';
//        <% $ge %>('<% $name %>_linked_label0').style.display = '';
//        <% $ge %>('<% $name %>_linked_label0').style.visibility = '';
//        <% $ge %>('<% $name %>_linked').style.display = '';
//        <% $ge %>('<% $name %>_linked').style.visibility = '';
     }

    }

    <% $name %>_changed(<% $ge %>('<% $name %>'));

  </SCRIPT>

% } 
<%init>

my %opt = @_;
my $cgi = $opt{'cgi'};
my $discountnum = $opt{'curr_value'} || $opt{'value'};

$opt{'discount'} ||= [ qsearch( 'discount', { disabled=>'' } ) ];

my $curuser = $FS::CurrentUser::CurrentUser;

my $name = $opt{'element_name'} || $opt{'field'} || 'discountnum';

my $select = 'Select discount type';

my @_type_options = ( 'Amount', 'Percentage' );
unshift @_type_options, $select;

my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';

my $onchange = ( $opt{'onchange'} ? delete($opt{'onchange'}).';' : '' ).
               $name.'_changed(this);';

</%init>