47acd854bd9c1efe5c20dcc9cce81cf0e058809c
[freeside.git] / httemplate / elements / tr-select-reason.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/tr-select-reason.html',
6
7     #required 
8     'field'         => 'reasonnum',
9     'reason_class'  => 'C', # currently 'C', 'R', 'F', 'S' or 'X'
10                            # for cancel, credit, refund, suspend or void credit
11
12     #recommended
13     'cgi' => $cgi, #easiest way for things to be properly "sticky" on errors
14
15     #optional
16     'control_button' => 'element_name', #button to be enabled when a reason is
17                                         #selected
18     'id'             => 'element_id',
19
20     #deprecated ways to keep things "sticky" on errors
21     # (requires duplicate code in each using file to parse cgi params)
22     'curr_value'     => $curr_value,
23     'curr_value'     => {
24                           'typenum' => $typenum,
25                           'reason'  => $reason,
26                         },
27
28   )
29
30 </%doc>
31
32 % # note style improvements.
33 % # - no more conditionally included code here
34 % # - callers are not expected to pass javascript fragments
35 % # - no redundant checking of ACLs or parameters
36 % # - form fields are grouped for easy management
37 % # - use the standard select-table widget instead of ad hoc crap
38 <& /elements/xmlhttp.html,
39   url => $p . 'misc/xmlhttp-reason-hint.html',
40   subs => [ 'get_hint' ],
41 &>
42 <SCRIPT TYPE="text/javascript">
43   function <% $id %>_changed() {
44     var select_reason = document.getElementById('<% $id %>');
45
46     get_hint(select_reason.value, function(stuff) {
47       document.getElementById('<% $id %>_hint').innerHTML = stuff || '';
48     });
49
50     // toggle submit button state
51     var submit_button = document.getElementById(<% $opt{control_button} |js_string %>);
52     if (submit_button) {
53       submit_button.disabled = ( select_reason.value == 0 );
54     }
55
56     // toggle visibility of 'new reason' fields
57     var new_fields = document.getElementById('<% $id %>_new_fields');
58     if ( select_reason.value == -1 ) {
59       new_fields.disabled = false;
60       new_fields.style.display = '';
61     } else {
62       new_fields.disabled = true;
63       new_fields.style.display = 'none';
64     }
65
66   }
67   <&| onload.js &> <% $id %>_changed(); </&>
68 </SCRIPT>
69
70 %# sadly can't just use add_inline here, as we have non-text fields
71 <& tr-select-table.html,
72   'label'           => 'Reason',
73   'field'           => $name,
74   'id'              => $id,
75   'table'           => 'reason',
76   'records'         => \@reasons,
77   'name_col'        => 'label',
78   'disable_empty'   => 1,
79   'pre_options'     => [ 0 => 'Select reason...' ],
80   'post_options'    => \@post_options,
81   'curr_value'      => $init_reason,
82   'onchange'        => $id.'_changed()',
83 &>
84
85 % # "add new reason" fields
86 % # should be a <fieldset>, but that doesn't fit well into the table
87
88 % if ( $curuser->access_right($add_access_right) ) {
89 <TR id="<% $id %>_new_fields">
90   <TD COLSPAN=2>
91     <TABLE CLASS="inv" STYLE="text-align: left">
92
93       <& tr-input-text.html,
94         label => 'New reason',
95         field => $id.'_new_reason'
96       &>
97
98 %   my @types = qsearch( 'reason_type', { 'class' => $class } );
99 %   if (scalar(@types) < 1) {  # we should never reach this
100       <TR>
101         <TD ALIGN="right">
102           <P><% mt('No reason types. Please add some.') |h %></P>
103         </TD>
104       </TR>
105 %   } elsif (scalar(@types) == 1) {
106       <& tr-fixed.html,
107         label => 'Reason type',
108         field => $id.'_new_reason_type',
109         curr_value => $types[0]->typenum,
110         formatted_value => $types[0]->type,
111       &>
112 %   } else { # more than one type, the normal case
113       <& tr-select-table.html,
114         label         => 'Reason type',
115         field         => $id.'_new_reason_type',
116         table         => 'reason_type',
117         name_col      => 'type',
118         hashref       => { 'class' => $class },
119         disable_empty => 1,
120       &>
121 %   } # scalar(@types)
122
123 %   if ( $class eq 'S' ) {
124       <& tr-checkbox.html,
125         label => 'Credit the unused portion of service when suspending',
126         field => $id.'_new_unused_credit',
127         value => 'Y'
128       &>
129       <& tr-select-table.html,
130         label     => 'Charge a suspension fee',
131         field     => $id.'_new_feepart',
132         table     => 'part_fee',
133         hashref   => { disabled => '' },
134         name_col  => 'itemdesc',
135         value_col => 'feepart',
136         empty_label => 'none',
137       &>
138       <& tr-select.html,
139         label     => 'When this package is',
140         field     => $id.'_new_fee_on_unsuspend',
141         options   => [ '', 'Y' ],
142         labels    => { '' => 'suspended', 'Y' => 'unsuspended' },
143       &>
144       <& tr-checkbox.html,
145         label     => 'Delay fee until the next bill',
146         field     => $id.'_new_fee_hold',
147         value     => 'Y',
148       &>
149 %# deprecated, but still accessible through the "Suspend Reasons" UI
150 %#      <& tr-select-part_pkg.html,
151 %#        label   => 'Charge this fee when unsuspending',
152 %#        field   => $id.'_new_unsuspend_pkgpart',
153 %#        hashref => { disabled => '', freq => '0' },
154 %#        empty_label => 'none',
155 %#      &>
156 %#      <& tr-checkbox.html,
157 %#        label => 'Hold unsuspension fee until the next bill',
158 %#        field => $id.'_new_unsuspend_hold',
159 %#        value => 'Y',
160 %#      &>
161 %   }
162     </table>
163   </td>
164 </tr>
165 % } # if the current user can add a reason
166
167 % # container for hints (hints themselves come from xmlhttp-reason-hint)
168 <TR>
169   <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint" style="font-size:small">
170   </TD>
171 </TR>
172
173 <%init>
174
175 my $curuser = $FS::CurrentUser::CurrentUser;
176 my %opt = @_;
177
178 my $name = $opt{'field'};
179 my $class = $opt{'reason_class'};
180
181 my $init_reason;
182 if ( $opt{'cgi'} ) {
183   $init_reason = $opt{'cgi'}->param($name);
184 } else {
185   $init_reason = $opt{'curr_value'};
186 }
187
188 my $id = $opt{'id'} || $name;
189 $id =~ s/\./_/g; # for edit/part_event
190
191 my $add_access_right;
192 if ($class eq 'C') {
193   $add_access_right = 'Add on-the-fly cancel reason';
194 } elsif ($class eq 'S') {
195   $add_access_right = 'Add on-the-fly suspend reason';
196 } elsif ($class eq 'R') {
197   $add_access_right = 'Add on-the-fly credit reason';
198 } elsif ($class eq 'X') {
199   $add_access_right = 'Add on-the-fly void credit reason';
200 } elsif ($class eq 'F') {
201   $add_access_right = 'Add on-the-fly refund reason';
202 } else {
203   die "illegal class: $class";
204 }
205
206 my $select = join(',',
207   'reason.*',
208   FS::Record::concat_sql([ 'type', "' : '", 'reason' ]) . ' AS label',
209 );
210
211 my @reasons = qsearch({
212   'table'           => 'reason',
213   'select'          => $select,
214   'addl_from'       => ' LEFT JOIN reason_type'.
215                        ' ON (reason.reason_type = reason_type.typenum)',
216   'hashref'         => { disabled => '' },
217   'extra_sql'       => " AND reason_type.class = '$class'",
218 });
219
220 my @post_options;
221 if ( $curuser->access_right($add_access_right) ) {
222   @post_options = ( -1 => 'Add new reason' );
223 }
224
225 </%init>