5 include( '/elements/tr-select-reason.html',
8 'field' => 'reasonnum',
9 'reason_class' => 'C', # currently 'C', 'R', or 'S'
10 # for cancel, credit, or suspend
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 <SCRIPT TYPE="text/javascript">
39 function <% $id %>_changed() {
40 var hints = <% encode_json(\%all_hints) %>;
41 var select_reason = document.getElementById('<% $id %>');
43 document.getElementById('<% $id %>_hint').innerHTML =
44 hints[select_reason.value] || '';
46 // toggle submit button state
47 var submit_button = document.getElementById(<% $opt{control_button} |js_string %>);
49 submit_button.disabled = ( select_reason.value == 0 );
52 // toggle visibility of 'new reason' fields
53 var new_fields = document.getElementById('<% $id %>_new_fields');
54 if ( select_reason.value == -1 ) {
55 new_fields.disabled = false;
56 new_fields.style.display = '';
58 new_fields.disabled = true;
59 new_fields.style.display = 'none';
63 <&| onload.js &> <% $id %>_changed(); </&>
66 %# sadly can't just use add_inline here, as we have non-text fields
67 <& tr-select-table.html,
72 'records' => \@reasons,
73 'name_col' => 'label',
75 'pre_options' => [ 0 => 'Select reason...' ],
76 'post_options' => \@post_options,
77 'curr_value' => $init_reason,
78 'onchange' => $id.'_changed()',
81 % # "add new reason" fields
82 % # should be a <fieldset>, but that doesn't fit well into the table
84 % if ( $curuser->access_right($add_access_right) ) {
85 <TR id="<% $id %>_new_fields">
87 <TABLE CLASS="inv" STYLE="text-align: left">
89 <& tr-input-text.html,
90 label => 'New reason',
91 field => $id.'_new_reason'
94 % my @types = qsearch( 'reason_type', { 'class' => $class } );
95 % if (scalar(@types) < 1) { # we should never reach this
98 <P><% mt('No reason types. Please add some.') |h %></P>
101 % } elsif (scalar(@types) == 1) {
103 label => 'Reason type',
104 field => $id.'_new_reason_type',
105 curr_value => $types[0]->typenum,
106 formatted_value => $types[0]->type,
108 % } else { # more than one type, the normal case
109 <& tr-select-table.html,
110 label => 'Reason type',
111 field => $id.'_new_reason_type',
112 table => 'reason_type',
114 hashref => { 'class' => $class },
119 % if ( $class eq 'S' ) {
121 label => 'Credit the unused portion of service when suspending',
122 field => $id.'_new_unused_credit',
125 <& tr-select-part_pkg.html,
126 label => 'Charge this fee when unsuspending',
127 field => $id.'_new_unsuspend_pkgpart',
128 hashref => { disabled => '', freq => '0' },
129 empty_label => 'none',
132 label => 'Hold unsuspension fee until the next bill',
133 field => $id.'_new_unsuspend_hold',
140 % } # if the current user can add a reason
142 % # container for hints
144 <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint" style="font-size:small">
150 my $curuser = $FS::CurrentUser::CurrentUser;
153 my $name = $opt{'field'};
154 my $class = $opt{'reason_class'};
158 $init_reason = $opt{'cgi'}->param($name);
160 $init_reason = $opt{'curr_value'};
163 my $id = $opt{'id'} || $name;
164 $id =~ s/\./_/g; # for edit/part_event
166 my $add_access_right;
168 $add_access_right = 'Add on-the-fly cancel reason';
169 } elsif ($class eq 'S') {
170 $add_access_right = 'Add on-the-fly suspend reason';
171 } elsif ($class eq 'R') {
172 $add_access_right = 'Add on-the-fly credit reason';
174 die "illegal class: $class";
177 my @reasons = qsearch({
179 'select' => "reason.*, (type || ' : ' || reason) AS label",
180 'addl_from' => ' LEFT JOIN reason_type'.
181 ' ON (reason.reason_type = reason_type.typenum)',
182 'hashref' => { disabled => '' },
183 'extra_sql' => " AND reason_type.class = '$class'",
187 if ( $class eq 'S' ) {
188 my $conf = FS::Conf->new;
189 %all_hints = ( 0 => '', -1 => '' );
190 foreach my $reason (@reasons) {
192 if ( $reason->unsuspend_pkgpart ) {
193 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart);
195 if ( $part_pkg->option('setup_fee',1) > 0 and
196 $part_pkg->option('recur_fee',1) == 0 ) {
199 mt('A [_1] unsuspension fee will apply.',
200 ($conf->config('money_char') || '$') .
201 sprintf('%.2f', $part_pkg->option('setup_fee'))
204 # oddball cases--not really supported
206 mt('An unsuspension package will apply: [_1]',
207 $part_pkg->price_info
210 } else { #no $part_pkg
212 '<FONT COLOR="#ff0000">Unsuspend pkg #'.$reason->unsuspend_pkgpart.
213 ' not found.</FONT>';
216 if ( $reason->unused_credit ) {
217 push @hints, mt('The customer will be credited for unused time.');
219 $all_hints{ $reason->reasonnum } = join('<BR>', @hints);
224 if ( $curuser->access_right($add_access_right) ) {
225 @post_options = ( -1 => 'Add new reason' );