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 'name_col' => 'label',
79 'pre_options' => [ 0 => 'Select reason...' ],
80 'post_options' => \@post_options,
81 'curr_value' => $init_reason,
82 'onchange' => $id.'_changed()',
85 % # "add new reason" fields
86 % # should be a <fieldset>, but that doesn't fit well into the table
88 % if ( $curuser->access_right($add_access_right) ) {
89 <TR id="<% $id %>_new_fields">
91 <TABLE CLASS="inv" STYLE="text-align: left">
93 <& tr-input-text.html,
94 label => 'New reason',
95 field => $id.'_new_reason'
98 % my @types = qsearch( 'reason_type', { 'class' => $class } );
99 % if (scalar(@types) < 1) { # we should never reach this
102 <P><% mt('No reason types. Please add some.') |h %></P>
105 % } elsif (scalar(@types) == 1) {
107 label => 'Reason type',
108 field => $id.'_new_reason_type',
109 curr_value => $types[0]->typenum,
110 formatted_value => $types[0]->type,
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',
118 hashref => { 'class' => $class },
123 % if ( $class eq 'C' ) {
125 label => 'Credit the unused portion of service when canceling',
126 field => $id.'_new_unused_credit',
130 % if ( $class eq 'S' ) {
132 label => 'Credit the unused portion of service when suspending',
133 field => $id.'_new_unused_credit',
136 <& tr-select-table.html,
137 label => 'Charge a suspension fee',
138 field => $id.'_new_feepart',
140 hashref => { disabled => '' },
141 name_col => 'itemdesc',
142 value_col => 'feepart',
143 empty_label => 'none',
146 label => 'When this package is',
147 field => $id.'_new_fee_on_unsuspend',
148 options => [ '', 'Y' ],
149 labels => { '' => 'suspended', 'Y' => 'unsuspended' },
152 label => 'Delay fee until the next bill',
153 field => $id.'_new_fee_hold',
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',
163 %# <& tr-checkbox.html,
164 %# label => 'Hold unsuspension fee until the next bill',
165 %# field => $id.'_new_unsuspend_hold',
172 % } # if the current user can add a reason
174 % # container for hints (hints themselves come from xmlhttp-reason-hint)
176 <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint" style="font-size:small">
182 my $curuser = $FS::CurrentUser::CurrentUser;
185 my $name = $opt{'field'};
186 my $class = $opt{'reason_class'};
190 $init_reason = $opt{'cgi'}->param($name);
192 $init_reason = $opt{'curr_value'};
195 my $id = $opt{'id'} || $name;
196 $id =~ s/\./_/g; # for edit/part_event
198 my $add_access_right;
200 $add_access_right = 'Add on-the-fly cancel reason';
201 } elsif ($class eq 'S') {
202 $add_access_right = 'Add on-the-fly suspend reason';
203 } elsif ($class eq 'R') {
204 $add_access_right = 'Add on-the-fly credit reason';
205 } elsif ($class eq 'X') {
206 $add_access_right = 'Add on-the-fly void credit reason';
207 } elsif ($class eq 'F') {
208 $add_access_right = 'Add on-the-fly refund reason';
210 die "illegal class: $class";
213 my $select = join(',',
215 FS::Record::concat_sql([ 'type', "' : '", 'reason' ]) . ' AS label',
218 my @reasons = qsearch({
221 'addl_from' => ' LEFT JOIN reason_type'.
222 ' ON (reason.reason_type = reason_type.typenum)',
223 'hashref' => { disabled => '' },
224 'extra_sql' => " AND reason_type.class = '$class'",
228 if ( $curuser->access_right($add_access_right) ) {
229 @post_options = ( -1 => 'Add new reason' );