Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / httemplate / elements / tr-select-reason.html
index 2639a9d..b7a715b 100755 (executable)
@@ -1,8 +1,46 @@
+<%doc>
+
+Example:
+
+  include( '/elements/tr-select-reason.html',
+
+    #required 
+    'field'         => 'reasonnum',
+    'reason_class'  => 'C', # currently 'C', 'R', 'F',  or 'S'
+                           # for cancel, credit, refund, or suspend
+
+    #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
+    'id'             => 'element_id',
+
+    #deprecated ways to keep things "sticky" on errors
+    # (requires duplicate code in each using file to parse cgi params)
+    'curr_value'     => $curr_value,
+    'curr_value'     => {
+                          'typenum' => $typenum,
+                          'reason'  => $reason,
+                        },
+
+  )
+
+</%doc>
+
 <SCRIPT TYPE="text/javascript">
   function sh_add<% $func_suffix %>()
   {
+    var hints = <% encode_json(\@hints) %>;
+    var select_reason = document.getElementById('<% $id %>');
 
-    if (document.getElementById('<% $id %>').selectedIndex == 0){
+% if ( $class eq 'S' ) {    
+    document.getElementById('<% $id %>_hint').innerHTML =
+      hints[select_reason.selectedIndex];
+% }
+
+    if (select_reason.selectedIndex == 0){
       <% $controlledbutton ? $controlledbutton.'.disabled = true;' : ';' %>
     }else{
       <% $controlledbutton ? $controlledbutton.'.disabled = false;' : ';' %>
@@ -10,8 +48,8 @@
 
 %if ($curuser->access_right($add_access_right)){
 
-    if (document.getElementById('<% $id %>').selectedIndex == 
-         (document.getElementById('<% $id %>').length - 1)) {
+    if (select_reason.selectedIndex == 
+         (select_reason.length - 1)) {
       document.getElementById('new<% $id %>').disabled = false;
       document.getElementById('new<% $id %>').style.display = 'inline';
       document.getElementById('new<% $id %>Label').style.display = 'inline';
 </SCRIPT>
 
 <TR>
-  <TD ALIGN="right">Reason</TD>
+  <TD ALIGN="right"><% mt('Reason') |h %></TD>
   <TD>
     <SELECT id="<% $id %>" name="<% $name %>" onFocus="sh_add<% $func_suffix %>()" onChange="sh_add<% $func_suffix %>()">
-      <OPTION VALUE="" <% ($init_reason eq "") ? 'SELECTED' : '' %>>Select Reason...</OPTION>
+      <OPTION VALUE="" <% ($init_reason eq '') ? 'SELECTED' : '' %>><% mt('Select Reason...') |h %></OPTION>
 %    foreach my $reason (@reasons) {
       <OPTION VALUE="<% $reason->reasonnum %>" <% ($init_reason == $reason->reasonnum) ? 'SELECTED' : '' %>><% $reason->reasontype->type %> : <% $reason->reason %></OPTION>
 %    }
 %    if ($curuser->access_right($add_access_right)) {
-      <OPTION VALUE="-1" <% ($init_reason == -1) ? 'SELECTED' : '' %>>Add new reason</OPTION>
+      <OPTION VALUE="-1" <% ($init_reason == -1) ? 'SELECTED' : '' %>><% mt('Add new reason') |h %></OPTION>
 %    }
 %
     </SELECT>
 %   if (scalar(@types) < 1) {  # we should never reach this
 <TR>
   <TD ALIGN="right">
-    <P>No reason types.  Go add some. </P>
+    <P><% mt('No reason types. Please add some.') |h %></P>
   </TD>
 </TR>
 %   }elsif (scalar(@types) == 1) {
 <TR>
   <TD ALIGN="right">
-    <P id="new<% $name %>TLabel" style="display:<% $display %>">Reason Type</P>
+    <P id="new<% $name %>TLabel" style="display:<% $display %>"><% mt('Reason Type') |h %></P>
   </TD>
   <TD>
     <P id="new<% $name %>T" disabled="<% $disabled %>" style="display:<% $display %>"><% $types[0]->type %>
 
 <TR>
   <TD ALIGN="right">
-    <P id="new<% $id %>TLabel" style="display:<% $display %>">Reason Type</P>
+    <P id="new<% $id %>TLabel" style="display:<% $display %>"><% mt('Reason Type') |h %></P>
   </TD>
   <TD>
     <SELECT id="new<% $id %>T" name="new<% $name %>T" "<% $disabled %>" style="display:<% $display %>">
 </TR>
 %   }
 
+% if ( $class eq 'S' ) {
+<TR>
+  <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint">
+  </TD>
+</TR>
+% }
+
 <TR>
   <TD ALIGN="right">
-    <P id="new<% $id %>Label" style="display:<% $display %>">New Reason</P>
+    <P id="new<% $id %>Label" style="display:<% $display %>"><% mt('New Reason') |h %></P>
   </TD>
-  <TD><INPUT id="new<% $id %>" name="new<% $name %>" type="text" value="<% $init_newreason %>" "<% $disabled %>" style="display:<% $display %>"></TD>
+  <TD><INPUT id="new<% $id %>" name="new<% $name %>" type="text" value="<% $init_newreason |h %>" "<% $disabled %>" style="display:<% $display %>"></TD>
 </TR>
 
 <%init>
@@ -95,7 +140,13 @@ my %opt = @_;
 
 my $name = $opt{'field'};
 my $class = $opt{'reason_class'};
-my $init_reason = $opt{'curr_value'};
+
+my $init_reason;
+if ( $opt{'cgi'} ) {
+  $init_reason = $opt{'cgi'}->param($name);
+} else {
+  $init_reason = $opt{'curr_value'};
+}
 
 my $controlledbutton = $opt{'control_button'};
 
@@ -103,16 +154,15 @@ my $controlledbutton = $opt{'control_button'};
 
 my $id = $opt{'id'} || $func_suffix;
 
-my( $add_access_right, $access_right ); 
+my $add_access_right;
 if ($class eq 'C') {
-  $access_right = 'Cancel customer';
   $add_access_right = 'Add on-the-fly cancel reason';
 } elsif ($class eq 'S') {
-  $access_right = 'Suspend customer package';
   $add_access_right = 'Add on-the-fly suspend reason';
 } elsif ($class eq 'R') {
-  $access_right = 'Post credit';
   $add_access_right = 'Add on-the-fly credit reason';
+} elsif ($class eq 'F') {
+  $add_access_right = 'Add on-the-fly refund reason';
 } else {
   die "illegal class: $class";
 }
@@ -128,6 +178,9 @@ if ($init_reason == -1 || ref($init_reason) ) {
     $init_type      = $init_reason->{'typenum'};
     $init_newreason = $init_reason->{'reason'};
     $init_reason = -1;
+  } elsif ( $opt{'cgi'} ) {
+    $init_type      = $opt{'cgi'}->param( "new${name}T" );
+    $init_newreason = $opt{'cgi'}->param( "new$name"    );
   }
 
 }
@@ -144,6 +197,43 @@ my @reasons = qsearch({
   order_by  => 'ORDER BY reason_type.type ASC, reason.reason ASC',
 });
 
+my @hints;
+if ( $class eq 'S' ) {
+  my $conf = FS::Conf->new;
+  @hints = ( '' );
+  foreach my $reason (@reasons) {
+    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>';
+      }
+    } else { #no unsuspend_pkgpart
+      push @hints, '';
+    }
+  }
+  push @hints, ''; # for the "new reason" case
+  @hints = map {'<FONT SIZE="-1">'.$_.'</FONT>'} @hints;
+}
+
+
 my $curuser = $FS::CurrentUser::CurrentUser;
 
 </%init>