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