fix A/R report
[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', # one of those in %FS::reason_type::class_name
10
11     #recommended
12     'cgi' => $cgi, #easiest way for things to be properly "sticky" on errors
13
14     #optional
15     'control_button' => 'element_name', #button to be enabled when a reason is
16                                         #selected
17     'id'             => 'element_id',
18
19     #deprecated ways to keep things "sticky" on errors
20     # (requires duplicate code in each using file to parse cgi params)
21     'curr_value'     => $curr_value,
22     'curr_value'     => {
23                           'typenum' => $typenum,
24                           'reason'  => $reason,
25                         },
26
27   )
28
29 </%doc>
30
31 % # note style improvements.
32 % # - no more conditionally included code here
33 % # - callers are not expected to pass javascript fragments
34 % # - no redundant checking of ACLs or parameters
35 % # - form fields are grouped for easy management
36 % # - use the standard select-table widget instead of ad hoc crap
37 <& /elements/xmlhttp.html,
38   url => $p . 'misc/xmlhttp-reason-hint.html',
39   subs => [ 'get_hint' ],
40 &>
41 <SCRIPT TYPE="text/javascript">
42   function <% $id %>_changed() {
43     var select_reason = document.getElementById('<% $id %>');
44
45     get_hint(select_reason.value, function(stuff) {
46       document.getElementById('<% $id %>_hint').innerHTML = stuff || '';
47     });
48
49     // toggle submit button state
50     var submit_button = document.getElementById(<% $opt{control_button} |js_string %>);
51     if (submit_button) {
52       submit_button.disabled = ( select_reason.value == 0 );
53     }
54
55     // toggle visibility of 'new reason' fields
56     var new_fields = document.getElementById('<% $id %>_new_fields');
57     if ( select_reason.value == -1 ) {
58       new_fields.disabled = false;
59       new_fields.style.display = '';
60     } else {
61       new_fields.disabled = true;
62       new_fields.style.display = 'none';
63     }
64
65   }
66   <&| onload.js &> <% $id %>_changed(); </&>
67 </SCRIPT>
68
69 %# sadly can't just use add_inline here, as we have non-text fields
70 <& tr-select-table.html,
71   'label'           => 'Reason',
72   'field'           => $name,
73   'id'              => $id,
74   'table'           => 'reason',
75   'records'         => \@reasons,
76   'label_callback'  => sub { my $reason = shift;
77                              $reason->type . ' : ' .  $reason->reason },
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 'C' ) {
124       <& tr-checkbox.html,
125         label => 'Credit the unused portion of service when canceling',
126         field => $id.'_new_unused_credit',
127         value => 'Y'
128       &>
129 %   }
130 %   if ( $class eq 'S' ) {
131       <& tr-checkbox.html,
132         label => 'Credit the unused portion of service when suspending',
133         field => $id.'_new_unused_credit',
134         value => 'Y'
135       &>
136       <& tr-select-table.html,
137         label     => 'Charge a suspension fee',
138         field     => $id.'_new_feepart',
139         table     => 'part_fee',
140         hashref   => { disabled => '' },
141         name_col  => 'itemdesc',
142         value_col => 'feepart',
143         empty_label => 'none',
144       &>
145       <& tr-select.html,
146         label     => 'When this package is',
147         field     => $id.'_new_fee_on_unsuspend',
148         options   => [ '', 'Y' ],
149         labels    => { '' => 'suspended', 'Y' => 'unsuspended' },
150       &>
151       <& tr-checkbox.html,
152         label     => 'Delay fee until the next bill',
153         field     => $id.'_new_fee_hold',
154         value     => 'Y',
155       &>
156 %# deprecated, but still accessible through the "Suspend Reasons" UI
157 %#      <& tr-select-part_pkg.html,
158 %#        label   => 'Charge this fee when unsuspending',
159 %#        field   => $id.'_new_unsuspend_pkgpart',
160 %#        hashref => { disabled => '', freq => '0' },
161 %#        empty_label => 'none',
162 %#      &>
163 %#      <& tr-checkbox.html,
164 %#        label => 'Hold unsuspension fee until the next bill',
165 %#        field => $id.'_new_unsuspend_hold',
166 %#        value => 'Y',
167 %#      &>
168 %   }
169     </table>
170   </td>
171 </tr>
172 % } # if the current user can add a reason
173
174 % # container for hints (hints themselves come from xmlhttp-reason-hint)
175 <TR>
176   <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint" style="font-size:small">
177   </TD>
178 </TR>
179
180 <%init>
181
182 my $curuser = $FS::CurrentUser::CurrentUser;
183 my %opt = @_;
184
185 my $name = $opt{'field'};
186 my $class = $opt{'reason_class'};
187
188 my $init_reason;
189 if ( $opt{'cgi'} ) {
190   $init_reason = $opt{'cgi'}->param($name);
191 }
192 $init_reason ||= $opt{'curr_value'};
193
194 my $id = $opt{'id'} || $name;
195 $id =~ s/\./_/g; # for edit/part_event
196
197 my $add_access_right = $FS::reason_type::class_add_access_right{$class}
198   or die "unknown class: $class";
199
200 my @reasons = qsearch({
201   'table'           => 'reason',
202   'addl_from'       => ' LEFT JOIN reason_type'.
203                        ' ON (reason.reason_type = reason_type.typenum)',
204   'hashref'         => { disabled => '' },
205   'extra_sql'       => " AND reason_type.class = '$class'",
206   'order_by'        => ' ORDER BY type, reason',
207 });
208
209 my @post_options;
210 if ( $curuser->access_right($add_access_right) ) {
211   @post_options = ( -1 => 'Add new reason' );
212 }
213
214 </%init>