invoice_sections_with_taxes per-agent, RT#79636
[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 &>
47 <SCRIPT TYPE="text/javascript">
48   function <% $id %>_changed() {
49     var select_reason = document.getElementById('<% $id %>');
50
51     get_hint(select_reason.value, function(stuff) {
52       document.getElementById('<% $id %>_hint').innerHTML = stuff || '';
53     });
54
55     // toggle submit button state
56     var submit_button = document.getElementById(<% $opt{control_button} |js_string %>);
57     if (submit_button) {
58       submit_button.disabled = ( select_reason.value == 0 );
59     }
60
61     // toggle visibility of 'new reason' fields
62     var new_fields = document.getElementById('<% $id %>_new_fields');
63     if ( select_reason.value == -1 ) {
64       new_fields.disabled = false;
65       new_fields.style.display = '';
66     } else {
67       new_fields.disabled = true;
68       new_fields.style.display = 'none';
69     }
70
71   }
72   <&| onload.js &> <% $id %>_changed(); </&>
73 </SCRIPT>
74
75 %# sadly can't just use add_inline here, as we have non-text fields
76
77 <& tr-select-table.html,
78   'label'           => $label,
79   'field'           => $name,
80   'id'              => $id,
81   'table'           => 'reason',
82   'records'         => \@reasons,
83   'label_callback'  => sub { my $reason = shift;
84                              $reason->type . ' : ' .  $reason->reason },
85   'disable_empty'   => 1,
86   'pre_options'     => \@pre_options,
87   'post_options'    => \@post_options,
88   'curr_value'      => $init_reason,
89   'onchange'        => $id.'_changed()',
90   'hide_onload'     => $opt{'hide_onload'},
91 &>
92
93 % # "add new reason" fields
94 % # should be a <fieldset>, but that doesn't fit well into the table
95
96 % if ( $curuser->access_right($add_access_right) && !$hide_addnew ) {
97 <TR id="<% $id %>_new_fields">
98   <TD COLSPAN=2>
99     <TABLE CLASS="inv" STYLE="text-align: left">
100
101       <& tr-input-text.html,
102         label => 'New reason',
103         field => $id.'_new_reason'
104       &>
105
106 %   my @types = qsearch( 'reason_type', { 'class' => $class } );
107 %   if (scalar(@types) < 1) {  # we should never reach this
108       <TR>
109         <TD ALIGN="right">
110           <P><% mt('No reason types. Please add some.') |h %></P>
111         </TD>
112       </TR>
113 %   } elsif (scalar(@types) == 1) {
114       <& tr-fixed.html,
115         label => 'Reason type',
116         field => $id.'_new_reason_type',
117         curr_value => $types[0]->typenum,
118         formatted_value => $types[0]->type,
119       &>
120 %   } else { # more than one type, the normal case
121       <& tr-select-table.html,
122         label         => 'Reason type',
123         field         => $id.'_new_reason_type',
124         table         => 'reason_type',
125         name_col      => 'type',
126         hashref       => { 'class' => $class },
127         disable_empty => 1,
128       &>
129 %   } # scalar(@types)
130
131 %   if ( $class eq 'C' ) {
132       <& tr-checkbox.html,
133         label => 'Credit the unused portion of service when canceling',
134         field => $id.'_new_unused_credit',
135         value => 'Y'
136       &>
137 %   }
138 %   if ( $class eq 'S' ) {
139       <& tr-checkbox.html,
140         label => 'Credit the unused portion of service when suspending',
141         field => $id.'_new_unused_credit',
142         value => 'Y'
143       &>
144       <& tr-select-table.html,
145         label     => 'Charge a suspension fee',
146         field     => $id.'_new_feepart',
147         table     => 'part_fee',
148         hashref   => { disabled => '' },
149         name_col  => 'itemdesc',
150         value_col => 'feepart',
151         empty_label => 'none',
152       &>
153       <& tr-select.html,
154         label     => 'When this package is',
155         field     => $id.'_new_fee_on_unsuspend',
156         options   => [ '', 'Y' ],
157         labels    => { '' => 'suspended', 'Y' => 'unsuspended' },
158       &>
159       <& tr-checkbox.html,
160         label     => 'Delay fee until the next bill',
161         field     => $id.'_new_fee_hold',
162         value     => 'Y',
163       &>
164 %# deprecated, but still accessible through the "Suspend Reasons" UI
165 %#      <& tr-select-part_pkg.html,
166 %#        label   => 'Charge this fee when unsuspending',
167 %#        field   => $id.'_new_unsuspend_pkgpart',
168 %#        hashref => { disabled => '', freq => '0' },
169 %#        empty_label => 'none',
170 %#      &>
171 %#      <& tr-checkbox.html,
172 %#        label => 'Hold unsuspension fee until the next bill',
173 %#        field => $id.'_new_unsuspend_hold',
174 %#        value => 'Y',
175 %#      &>
176 %   }
177     </table>
178   </td>
179 </tr>
180 % } # if the current user can add a reason
181
182 % # container for hints (hints themselves come from xmlhttp-reason-hint)
183 <TR>
184   <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint" style="font-size:small">
185   </TD>
186 </TR>
187
188 <%init>
189
190 my $curuser = $FS::CurrentUser::CurrentUser;
191 my %opt = @_;
192
193 my $name = $opt{'field'};
194 my $class = $opt{'reason_class'};
195 my $label = $opt{'label'} ? $opt{'label'} : 'Reason';
196 my $hide_addnew = $opt{'hide_addnew'} ? $opt{'hide_addnew'} : '';
197
198 my $init_reason;
199 if ( $opt{'cgi'} ) {
200   $init_reason = $opt{'cgi'}->param($name);
201 }
202 $init_reason ||= $opt{'curr_value'};
203
204 my $id = $opt{'id'} || $name;
205 $id =~ s/\./_/g; # for edit/part_event
206
207 my $label_id = $opt{'label_id'} || '';
208
209 my $add_access_right = $FS::reason_type::class_add_access_right{$class}
210   or die "unknown class: $class";
211
212 my @reasons = qsearch({
213   'table'           => 'reason',
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   'order_by'        => ' ORDER BY type, reason',
219 });
220
221
222 my @pre_options = ( 0 => 'Select reason...' );
223 @pre_options = @{ $opt{'pre_options'} } if $opt{'pre_options'};
224
225 my @post_options;
226 if ( $curuser->access_right($add_access_right) && !$hide_addnew ) {
227   @post_options = ( -1 => 'Add new reason' );
228 }
229
230 </%init>