summaryrefslogtreecommitdiff
path: root/httemplate/edit/discount.html
blob: c2853bd47a557bf32f8c6435fd49de70faeb09d8 (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
<% include( 'elements/edit.html',
                 'name'   => 'Discount',
                 'table'  => 'discount',
                 'fields' => [
                               'name',
                               { field => 'classnum', type => 'select-discount_class' },
                               { field => 'disabled', type => 'checkbox', value=>'Y', },
                               # a weird kind of false laziness
                               # w/elements/tr-select-discount.html
                               { field => '_type',    type => 'select',
                                 options => \@_type_options,
                                 onchange => '_type_changed',
                               },
                               { field => 'amount',   type => 'money',
                                 default => '0.00',
                                 #cell_style => $amount_style,
                               },
                               { field => 'percent',  type => 'percentage',
                                 default => 0,
                                 #cell_style => $percent_style,
                               },
                               { field => 'months', type => 'text', size => 2,
                                 postfix => '<BR><FONT SIZE="-1"><I>(blank for non-expiring discount)</I></FONT>',
                               },
                               { field => 'setup', type => 'checkbox', value=>'Y', },
                               #{ field => 'linked', type => 'checkbox', value=>'Y', },
                             ],
                 'labels' => { 
                               'discountnum' => 'Discount #',
                               'name'        => 'Name&nbsp;',
                               'classnum'    => 'Class',
                               'disabled'    => 'Disabled&nbsp;',
                               '_type'       => 'Type&nbsp;',
                               'amount'      => 'Amount&nbsp;',
                               'percent'     => 'Percentage&nbsp;',
                               'months'      => 'Duration (months)',
                               'setup'       => 'Apply to setup fees',
                               #'linked'      => 'Apply to add-on packages',
                             },
                 'viewall_dir' => 'browse',
                 'new_callback' => $new_callback,
                 'edit_callback' => $edit_callback,
                 'error_callback' => $error_callback,
                 'html_init' => $javascript,
                 'body_etc' => 'onLoad="_type_changed(document.edit_topform._type)"',
           )
%>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');

my @_type_options = ( 'Amount', 'Percentage' );

#my $amount_style = '';
#my $percent_style = '';

#my $hide = 'display:none;visibility:hidden';
my $select = 'Select discount type';

my $new_callback = sub {
  #my( $cgi, $object, $fields_listref, $opt_hashref ) = @_;

  #$amount_style  = $hide;
  #$percent_style = $hide;
  unshift @_type_options, $select;
};

my $edit_callback = sub {
  #my( $cgi, $object, $fields_listref, $opt_hashref ) = @_;
  my( $cgi, $object ) = @_;

  if ( $object->amount > 0 && $object->percent == 0 ) {
    $object->set('_type', 'Amount');
    #$percent_style = $hide;
  } elsif ( $object->amount == 0 && $object->percent > 0 ) {
    $object->set('_type', 'Percentage');
    #$amount_style = $hide;
  } elsif ( $object->amount == 0 && $object->percent == 0 ) {
    #$amount_style  = $hide;
    #$percent_style = $hide;
    unshift @_type_options, $select;
  } else {
    die "discount.amount and discount.percent not yet handled by web UI";
  }

};

my $error_callback = sub {
  #my( $cgi, $object, $fields_listref, $opt_hashref ) = @_;
  my( $cgi, $object ) = @_;

  if ( $cgi->param('_type') eq 'Amount' ) {
    $object->set('_type', 'Amount');
    #A$percent_style = $hide;
  } elsif ( $cgi->param('_type') eq 'Percentage' ) {
    $object->set('_type', 'Percentage');
    #$amount_style = $hide;
  } else {
    #$amount_style  = $hide;
    #$percent_style = $hide;
    unshift @_type_options, $select;
  }

};

my $javascript = <<END;
  <SCRIPT TYPE="text/javascript">
    function _type_changed(what) {
      var _type = what.options[what.selectedIndex].value;

      if ( _type == '$select' ) {
        document.getElementById('amount_label').style.display = 'none';
        document.getElementById('amount_label').style.visibility = 'hidden';
        document.getElementById('amount_input0').style.display = 'none';
        document.getElementById('amount_input0').style.visibility = 'hidden';
        document.getElementById('percent_label').style.display = 'none';
        document.getElementById('percent_label').style.visibility = 'hidden';
        document.getElementById('percent_input0').style.display = 'none';
        document.getElementById('percent_input0').style.visibility = 'hidden';
//        document.getElementById('linked_label').style.display = 'none';
//        document.getElementById('linked_label').style.visibility = 'hidden';
//        document.getElementById('linked').style.display = 'none';
//        document.getElementById('linked').style.visibility = 'hidden';
      } else if ( _type == 'Amount' ) {
        document.getElementById('amount_label').style.display = '';
        document.getElementById('amount_label').style.visibility = '';
        document.getElementById('amount_input0').style.display = '';
        document.getElementById('amount_input0').style.visibility = '';
        document.getElementById('percent_label').style.display = 'none';
        document.getElementById('percent_label').style.visibility = 'hidden';
        document.getElementById('percent_input0').style.display = 'none';
        document.getElementById('percent_input0').style.visibility = 'hidden';
//        document.getElementById('linked_label').style.display = 'none';
//        document.getElementById('linked_label').style.visibility = 'hidden';
//        document.getElementById('linked').style.display = 'none';
//        document.getElementById('linked').style.visibility = 'hidden';
      } else if ( _type == 'Percentage' ) {
        document.getElementById('amount_label').style.display = 'none';
        document.getElementById('amount_label').style.visibility = 'hidden';
        document.getElementById('amount_input0').style.display = 'none';
        document.getElementById('amount_input0').style.visibility = 'hidden';
        document.getElementById('percent_label').style.display = '';
        document.getElementById('percent_label').style.visibility = '';
        document.getElementById('percent_input0').style.display = '';
        document.getElementById('percent_input0').style.visibility = '';
//        document.getElementById('linked_label').style.display = '';
//        document.getElementById('linked_label').style.visibility = '';
//        document.getElementById('linked').style.display = '';
//        document.getElementById('linked').style.visibility = '';
     }

    }
  </SCRIPT>
END

</%init>