RT# 74693 - Added city select when using tax classes
[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_add'       => '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   'name_col'        => 'label',
87   'disable_empty'   => 1,
88   'pre_options'     => \@pre_options,
89   'post_options'    => \@post_options,
90   'curr_value'      => $init_reason,
91   'onchange'        => $id.'_changed()',
92   'hide_onload'     => $opt{'hide_onload'},
93 &>
94
95 % # "add new reason" fields
96 % # should be a <fieldset>, but that doesn't fit well into the table
97
98 % if ( $curuser->access_right($add_access_right) && !$hide_addnew ) {
99 <TR id="<% $id %>_new_fields">
100   <TD COLSPAN=2>
101     <TABLE CLASS="inv" STYLE="text-align: left">
102
103       <& tr-input-text.html,
104         label => 'New reason',
105         field => $id.'_new_reason'
106       &>
107
108 %   my @types = qsearch( 'reason_type', { 'class' => $class } );
109 %   if (scalar(@types) < 1) {  # we should never reach this
110       <TR>
111         <TD ALIGN="right">
112           <P><% mt('No reason types. Please add some.') |h %></P>
113         </TD>
114       </TR>
115 %   } elsif (scalar(@types) == 1) {
116       <& tr-fixed.html,
117         label => 'Reason type',
118         field => $id.'_new_reason_type',
119         curr_value => $types[0]->typenum,
120         formatted_value => $types[0]->type,
121       &>
122 %   } else { # more than one type, the normal case
123       <& tr-select-table.html,
124         label         => 'Reason type',
125         field         => $id.'_new_reason_type',
126         table         => 'reason_type',
127         name_col      => 'type',
128         hashref       => { 'class' => $class },
129         disable_empty => 1,
130       &>
131 %   } # scalar(@types)
132
133 %   if ( $class eq 'C' ) {
134       <& tr-checkbox.html,
135         label => 'Credit the unused portion of service when canceling',
136         field => $id.'_new_unused_credit',
137         value => 'Y'
138       &>
139 %   }
140 %   if ( $class eq 'S' ) {
141       <& tr-checkbox.html,
142         label => 'Credit the unused portion of service when suspending',
143         field => $id.'_new_unused_credit',
144         value => 'Y'
145       &>
146       <& tr-select-table.html,
147         label     => 'Charge a suspension fee',
148         field     => $id.'_new_feepart',
149         table     => 'part_fee',
150         hashref   => { disabled => '' },
151         name_col  => 'itemdesc',
152         value_col => 'feepart',
153         empty_label => 'none',
154       &>
155       <& tr-select.html,
156         label     => 'When this package is',
157         field     => $id.'_new_fee_on_unsuspend',
158         options   => [ '', 'Y' ],
159         labels    => { '' => 'suspended', 'Y' => 'unsuspended' },
160       &>
161       <& tr-checkbox.html,
162         label     => 'Delay fee until the next bill',
163         field     => $id.'_new_fee_hold',
164         value     => 'Y',
165       &>
166 %# deprecated, but still accessible through the "Suspend Reasons" UI
167 %#      <& tr-select-part_pkg.html,
168 %#        label   => 'Charge this fee when unsuspending',
169 %#        field   => $id.'_new_unsuspend_pkgpart',
170 %#        hashref => { disabled => '', freq => '0' },
171 %#        empty_label => 'none',
172 %#      &>
173 %#      <& tr-checkbox.html,
174 %#        label => 'Hold unsuspension fee until the next bill',
175 %#        field => $id.'_new_unsuspend_hold',
176 %#        value => 'Y',
177 %#      &>
178 %   }
179     </table>
180   </td>
181 </tr>
182 % } # if the current user can add a reason
183
184 % # container for hints (hints themselves come from xmlhttp-reason-hint)
185 <TR>
186   <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint" style="font-size:small">
187   </TD>
188 </TR>
189
190 <%init>
191
192 my $curuser = $FS::CurrentUser::CurrentUser;
193 my %opt = @_;
194
195 my $name = $opt{'field'};
196 my $class = $opt{'reason_class'};
197 my $label = $opt{'label'} ? $opt{'label'} : 'Reason';
198 my $hide_addnew = $opt{'hide_addnew'} ? $opt{'hide_addnew'} : '';
199
200 my $init_reason;
201 if ( $opt{'cgi'} ) {
202   $init_reason = $opt{'cgi'}->param($name);
203 } else {
204   $init_reason = $opt{'curr_value'};
205 }
206
207 my $id = $opt{'id'} || $name;
208 $id =~ s/\./_/g; # for edit/part_event
209
210 my $label_id = $opt{'label_id'} || '';
211
212 my $add_access_right;
213 if ($class eq 'C') {
214   $add_access_right = 'Add on-the-fly cancel reason';
215 } elsif ($class eq 'S') {
216   $add_access_right = 'Add on-the-fly suspend reason';
217 } elsif ($class eq 'R') {
218   $add_access_right = 'Add on-the-fly credit reason';
219 } elsif ($class eq 'X') {
220   $add_access_right = 'Add on-the-fly void credit reason';
221 } elsif ($class eq 'F') {
222   $add_access_right = 'Add on-the-fly refund reason';
223 } else {
224   die "illegal class: $class";
225 }
226
227 my $select = join(',',
228   'reason.*',
229   FS::Record::concat_sql([ 'type', "' : '", 'reason' ]) . ' AS label',
230 );
231
232 my @reasons = qsearch({
233   'table'           => 'reason',
234   'select'          => $select,
235   'addl_from'       => ' LEFT JOIN reason_type'.
236                        ' ON (reason.reason_type = reason_type.typenum)',
237   'hashref'         => { disabled => '' },
238   'extra_sql'       => " AND reason_type.class = '$class'",
239   'order_by'        => ' ORDER BY type, reason',
240 });
241
242 my @pre_options = ( 0 => 'Select reason...' );
243 @pre_options = @{ $opt{'pre_options'} } if $opt{'pre_options'};
244
245 my @post_options;
246 if ( $curuser->access_right($add_access_right) && !$hide_addnew ) {
247   @post_options = ( -1 => 'Add new reason' );
248 }
249
250 </%init>