RT# 77470 - Fixed error with select reason drop down box, not working when no pre_opt...
[freeside.git] / httemplate / elements / tr-select-reason.html
index 52dc956..f25171f 100755 (executable)
@@ -5,17 +5,22 @@ Example:
   include( '/elements/tr-select-reason.html',
 
     #required 
-    'field'         => 'reasonnum',
-    'reason_class'  => 'C', # currently 'C', 'R',  or 'S'
-                           # for cancel, credit, or suspend
+    'field'          => 'reasonnum',     # field name
+    'reason_class'   => 'C',             # one of those in %FS::reason_type::class_name
+    'label'          => 'Your Label',    # field display label
 
     #recommended
     'cgi' => $cgi, #easiest way for things to be properly "sticky" on errors
 
     #optional
-    'control_button' => 'element_name', #button to be enabled when a reason is
-                                        #selected
+    'control_button' => 'element_name',  #button to be enabled when a reason is
+                                         #selected
     'id'             => 'element_id',
+    'hide_add'       => '1',             # setting this will hide the add new reason link,
+                                         # even if the user has access to add a new reason.
+    'hide_onload'    => '1',             # setting this will hide reason select box on page load,
+                                         # allowing for it do be displayed later.
+    'pre_options'    => [ 0 => 'all'],   # an array of pre options.  Defaults to 0 => 'select reason...'
 
     #deprecated ways to keep things "sticky" on errors
     # (requires duplicate code in each using file to parse cgi params)
@@ -35,13 +40,17 @@ Example:
 % # - no redundant checking of ACLs or parameters
 % # - form fields are grouped for easy management
 % # - use the standard select-table widget instead of ad hoc crap
+<& /elements/xmlhttp.html,
+  url => $p . 'misc/xmlhttp-reason-hint.html',
+  subs => [ 'get_hint' ],
+&>
 <SCRIPT TYPE="text/javascript">
   function <% $id %>_changed() {
-    var hints = <% encode_json(\%all_hints) %>;
     var select_reason = document.getElementById('<% $id %>');
 
-    document.getElementById('<% $id %>_hint').innerHTML =
-      hints[select_reason.value] || '';
+    get_hint(select_reason.value, function(stuff) {
+      document.getElementById('<% $id %>_hint').innerHTML = stuff || '';
+    });
 
     // toggle submit button state
     var submit_button = document.getElementById(<% $opt{control_button} |js_string %>);
@@ -64,24 +73,28 @@ Example:
 </SCRIPT>
 
 %# sadly can't just use add_inline here, as we have non-text fields
+
 <& tr-select-table.html,
-  'label'           => 'Reason',
+  'label'           => $label,
   'field'           => $name,
   'id'              => $id,
   'table'           => 'reason',
   'records'         => \@reasons,
+  'label_callback'  => sub { my $reason = shift;
+                             $reason->type . ' : ' .  $reason->reason },
   'name_col'        => 'label',
   'disable_empty'   => 1,
-  'pre_options'     => [ 0 => 'Select reason...' ],
+  'pre_options'     => \@pre_options,
   'post_options'    => \@post_options,
   'curr_value'      => $init_reason,
   'onchange'        => $id.'_changed()',
+  'hide_onload'     => $opt{'hide_onload'},
 &>
 
 % # "add new reason" fields
 % # should be a <fieldset>, but that doesn't fit well into the table
 
-% if ( $curuser->access_right($add_access_right) ) {
+% if ( $curuser->access_right($add_access_right) && !$hide_addnew ) {
 <TR id="<% $id %>_new_fields">
   <TD COLSPAN=2>
     <TABLE CLASS="inv" STYLE="text-align: left">
@@ -116,30 +129,58 @@ Example:
       &>
 %   } # scalar(@types)
 
+%   if ( $class eq 'C' ) {
+      <& tr-checkbox.html,
+        label => 'Credit the unused portion of service when canceling',
+        field => $id.'_new_unused_credit',
+        value => 'Y'
+      &>
+%   }
 %   if ( $class eq 'S' ) {
       <& tr-checkbox.html,
         label => 'Credit the unused portion of service when suspending',
         field => $id.'_new_unused_credit',
         value => 'Y'
       &>
-      <& tr-select-part_pkg.html,
-        label   => 'Charge this fee when unsuspending',
-        field   => $id.'_new_unsuspend_pkgpart',
-        hashref => { disabled => '', freq => '0' },
+      <& tr-select-table.html,
+        label     => 'Charge a suspension fee',
+        field     => $id.'_new_feepart',
+        table     => 'part_fee',
+        hashref   => { disabled => '' },
+        name_col  => 'itemdesc',
+        value_col => 'feepart',
         empty_label => 'none',
       &>
+      <& tr-select.html,
+        label     => 'When this package is',
+        field     => $id.'_new_fee_on_unsuspend',
+        options   => [ '', 'Y' ],
+        labels    => { '' => 'suspended', 'Y' => 'unsuspended' },
+      &>
       <& tr-checkbox.html,
-        label => 'Hold unsuspension fee until the next bill',
-        field => $id.'_new_unsuspend_hold',
-        value => 'Y',
+        label     => 'Delay fee until the next bill',
+        field     => $id.'_new_fee_hold',
+        value     => 'Y',
       &>
+%# deprecated, but still accessible through the "Suspend Reasons" UI
+%#      <& tr-select-part_pkg.html,
+%#        label   => 'Charge this fee when unsuspending',
+%#        field   => $id.'_new_unsuspend_pkgpart',
+%#        hashref => { disabled => '', freq => '0' },
+%#        empty_label => 'none',
+%#      &>
+%#      <& tr-checkbox.html,
+%#        label => 'Hold unsuspension fee until the next bill',
+%#        field => $id.'_new_unsuspend_hold',
+%#        value => 'Y',
+%#      &>
 %   }
     </table>
   </td>
 </tr>
 % } # if the current user can add a reason
 
-% # container for hints
+% # container for hints (hints themselves come from xmlhttp-reason-hint)
 <TR>
   <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint" style="font-size:small">
   </TD>
@@ -152,6 +193,8 @@ my %opt = @_;
 
 my $name = $opt{'field'};
 my $class = $opt{'reason_class'};
+my $label = $opt{'label'} ? $opt{'label'} : 'Reason';
+my $hide_addnew = $opt{'hide_addnew'} ? $opt{'hide_addnew'} : '';
 
 my $init_reason;
 if ( $opt{'cgi'} ) {
@@ -163,6 +206,8 @@ if ( $opt{'cgi'} ) {
 my $id = $opt{'id'} || $name;
 $id =~ s/\./_/g; # for edit/part_event
 
+my $label_id = $opt{'label_id'} || '';
+
 my $add_access_right;
 if ($class eq 'C') {
   $add_access_right = 'Add on-the-fly cancel reason';
@@ -170,58 +215,34 @@ if ($class eq 'C') {
   $add_access_right = 'Add on-the-fly suspend reason';
 } elsif ($class eq 'R') {
   $add_access_right = 'Add on-the-fly credit reason';
+} elsif ($class eq 'X') {
+  $add_access_right = 'Add on-the-fly void credit reason';
+} elsif ($class eq 'F') {
+  $add_access_right = 'Add on-the-fly refund reason';
 } else {
   die "illegal class: $class";
 }
 
+my $select = join(',',
+  'reason.*',
+  FS::Record::concat_sql([ 'type', "' : '", 'reason' ]) . ' AS label',
+);
+
 my @reasons = qsearch({
   'table'           => 'reason',
-  'select'          => "reason.*, (type || ' : ' || reason) AS label",
+  'select'          => $select,
   'addl_from'       => ' LEFT JOIN reason_type'.
                        ' ON (reason.reason_type = reason_type.typenum)',
   'hashref'         => { disabled => '' },
   'extra_sql'       => " AND reason_type.class = '$class'",
+  'order_by'        => ' ORDER BY type, reason',
 });
 
-my %all_hints;
-if ( $class eq 'S' ) {
-  my $conf = FS::Conf->new;
-  %all_hints = ( 0 => '', -1 => '' );
-  foreach my $reason (@reasons) {
-    my @hints;
-    if ( $reason->unsuspend_pkgpart ) {
-      my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart);
-      if ( $part_pkg ) {
-        if ( $part_pkg->option('setup_fee',1) > 0 and 
-             $part_pkg->option('recur_fee',1) == 0 ) {
-          # the usual case
-          push @hints,
-            mt('A [_1] unsuspension fee will apply.', 
-               ($conf->config('money_char') || '$') .
-               sprintf('%.2f', $part_pkg->option('setup_fee'))
-               );
-        } else {
-          # oddball cases--not really supported
-          push @hints,
-            mt('An unsuspension package will apply: [_1]',
-              $part_pkg->price_info
-              );
-        }
-      } else { #no $part_pkg
-        push @hints,
-          '<FONT COLOR="#ff0000">Unsuspend pkg #'.$reason->unsuspend_pkgpart.
-          ' not found.</FONT>';
-      }
-    }
-    if ( $reason->unused_credit ) {
-      push @hints, mt('The customer will be credited for unused time.');
-    }
-    $all_hints{ $reason->reasonnum } = join('<BR>', @hints);
-  }
-}
+my @pre_options = ( 0 => 'Select reason...' );
+@pre_options = @{ $opt{'pre_options'} } if $opt{'pre_options'};
 
 my @post_options;
-if ( $curuser->access_right($add_access_right) ) {
+if ( $curuser->access_right($add_access_right) && !$hide_addnew ) {
   @post_options = ( -1 => 'Add new reason' );
 }