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