5 include( '/elements/tr-select-reason.html',
8 'field' => 'reasonnum',
9 'reason_class' => 'C', # currently 'C', 'R', 'F', 'S' or 'X'
10 # for cancel, credit, refund, suspend or void credit
13 'cgi' => $cgi, #easiest way for things to be properly "sticky" on errors
16 'control_button' => 'element_name', #button to be enabled when a reason is
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,
24 'typenum' => $typenum,
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' ],
42 <SCRIPT TYPE="text/javascript">
43 function <% $id %>_changed() {
44 var select_reason = document.getElementById('<% $id %>');
46 get_hint(select_reason.value, function(stuff) {
47 document.getElementById('<% $id %>_hint').innerHTML = stuff || '';
50 // toggle submit button state
51 var submit_button = document.getElementById(<% $opt{control_button} |js_string %>);
53 submit_button.disabled = ( select_reason.value == 0 );
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 = '';
62 new_fields.disabled = true;
63 new_fields.style.display = 'none';
67 <&| onload.js &> <% $id %>_changed(); </&>
70 %# sadly can't just use add_inline here, as we have non-text fields
71 <& tr-select-table.html,
76 'records' => \@reasons,
77 'label_callback' => sub { my $reason = shift;
78 $reason->type . ' : ' . $reason->reason },
80 'pre_options' => [ 0 => 'Select reason...' ],
81 'post_options' => \@post_options,
82 'curr_value' => $init_reason,
83 'onchange' => $id.'_changed()',
86 % # "add new reason" fields
87 % # should be a <fieldset>, but that doesn't fit well into the table
89 % if ( $curuser->access_right($add_access_right) ) {
90 <TR id="<% $id %>_new_fields">
92 <TABLE CLASS="inv" STYLE="text-align: left">
94 <& tr-input-text.html,
95 label => 'New reason',
96 field => $id.'_new_reason'
99 % my @types = qsearch( 'reason_type', { 'class' => $class } );
100 % if (scalar(@types) < 1) { # we should never reach this
103 <P><% mt('No reason types. Please add some.') |h %></P>
106 % } elsif (scalar(@types) == 1) {
108 label => 'Reason type',
109 field => $id.'_new_reason_type',
110 curr_value => $types[0]->typenum,
111 formatted_value => $types[0]->type,
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',
119 hashref => { 'class' => $class },
124 % if ( $class eq 'S' ) {
126 label => 'Credit the unused portion of service when suspending',
127 field => $id.'_new_unused_credit',
130 <& tr-select-table.html,
131 label => 'Charge a suspension fee',
132 field => $id.'_new_feepart',
134 hashref => { disabled => '' },
135 name_col => 'itemdesc',
136 value_col => 'feepart',
137 empty_label => 'none',
140 label => 'When this package is',
141 field => $id.'_new_fee_on_unsuspend',
142 options => [ '', 'Y' ],
143 labels => { '' => 'suspended', 'Y' => 'unsuspended' },
146 label => 'Delay fee until the next bill',
147 field => $id.'_new_fee_hold',
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',
157 %# <& tr-checkbox.html,
158 %# label => 'Hold unsuspension fee until the next bill',
159 %# field => $id.'_new_unsuspend_hold',
166 % } # if the current user can add a reason
168 % # container for hints (hints themselves come from xmlhttp-reason-hint)
170 <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint" style="font-size:small">
176 my $curuser = $FS::CurrentUser::CurrentUser;
179 my $name = $opt{'field'};
180 my $class = $opt{'reason_class'};
184 $init_reason = $opt{'cgi'}->param($name);
186 $init_reason = $opt{'curr_value'};
189 my $id = $opt{'id'} || $name;
190 $id =~ s/\./_/g; # for edit/part_event
192 my $add_access_right;
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';
204 die "illegal class: $class";
207 my @reasons = qsearch({
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',
217 if ( $curuser->access_right($add_access_right) ) {
218 @post_options = ( -1 => 'Add new reason' );