Option to ignore old CDRs, RT#81480
[freeside.git] / httemplate / elements / tr-select-reason.html
index 71997c2..64648ba 100755 (executable)
+<%doc>
+
+Example:
+
+  include( '/elements/tr-select-reason.html',
+
+    #required 
+    '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
+    '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)
+    'curr_value'     => $curr_value,
+    'curr_value'     => {
+                          'typenum' => $typenum,
+                          'reason'  => $reason,
+                        },
+
+  )
+
+</%doc>
+
+% # note style improvements.
+% # - no more conditionally included code here
+% # - callers are not expected to pass javascript fragments
+% # - 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' ],
+  skip_empty => 1,
+&>
 <SCRIPT TYPE="text/javascript">
-  function sh_add<% $func_suffix %>()
-  {
+  function <% $id %>_changed() {
+    var select_reason = document.getElementById('<% $id %>');
 
-    if (document.getElementById('<% $id %>').selectedIndex == 0){
-      <% $controlledbutton ? $controlledbutton.'.disabled = true;' : ';' %>
-    }else{
-      <% $controlledbutton ? $controlledbutton.'.disabled = false;' : ';' %>
-    }
+    get_hint(select_reason.value, function(stuff) {
+      document.getElementById('<% $id %>_hint').innerHTML = stuff || '';
+    });
 
-%if ($curuser->access_right($add_access_right)){
+    // toggle submit button state
+    var submit_button = document.getElementById(<% $opt{control_button} |js_string %>);
+    if (submit_button) {
+      submit_button.disabled = ( select_reason.value == 0 );
+    }
 
-    if (document.getElementById('<% $id %>').selectedIndex == 
-         (document.getElementById('<% $id %>').length - 1)) {
-      document.getElementById('new<% $id %>').disabled = false;
-      document.getElementById('new<% $id %>').style.display = 'inline';
-      document.getElementById('new<% $id %>Label').style.display = 'inline';
-      document.getElementById('new<% $id %>T').disabled = false;
-      document.getElementById('new<% $id %>T').style.display = 'inline';
-      document.getElementById('new<% $id %>TLabel').style.display = 'inline';
+    // toggle visibility of 'new reason' fields
+    var new_fields = document.getElementById('<% $id %>_new_fields');
+    if ( select_reason.value == -1 ) {
+      new_fields.disabled = false;
+      new_fields.style.display = '';
     } else {
-      document.getElementById('new<% $id %>').disabled = true;
-      document.getElementById('new<% $id %>').style.display = 'none';
-      document.getElementById('new<% $id %>Label').style.display = 'none';
-      document.getElementById('new<% $id %>T').disabled = true;
-      document.getElementById('new<% $id %>T').style.display = 'none';
-      document.getElementById('new<% $id %>TLabel').style.display = 'none';
+      new_fields.disabled = true;
+      new_fields.style.display = 'none';
     }
 
-%}
-
   }
+  <&| onload.js &> <% $id %>_changed(); </&>
 </SCRIPT>
 
+%# sadly can't just use add_inline here, as we have non-text fields
+
+<& tr-select-table.html,
+  'label'           => $label,
+  'field'           => $name,
+  'id'              => $id,
+  'table'           => 'reason',
+  'records'         => \@reasons,
+  'label_callback'  => sub { my $reason = shift;
+                             $reason->type . ' : ' .  $reason->reason },
+  'disable_empty'   => 1,
+  '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) && !$hide_addnew ) {
+<TR id="<% $id %>_new_fields">
+  <TD COLSPAN=2>
+    <TABLE CLASS="inv" STYLE="text-align: left">
+
+      <& tr-input-text.html,
+        label => 'New reason',
+        field => $id.'_new_reason'
+      &>
+
+%   my @types = qsearch( 'reason_type', { 'class' => $class } );
+%   if (scalar(@types) < 1) {  # we should never reach this
+      <TR>
+        <TD ALIGN="right">
+          <P><% mt('No reason types. Please add some.') |h %></P>
+        </TD>
+      </TR>
+%   } elsif (scalar(@types) == 1) {
+      <& tr-fixed.html,
+        label => 'Reason type',
+        field => $id.'_new_reason_type',
+        curr_value => $types[0]->typenum,
+        formatted_value => $types[0]->type,
+      &>
+%   } else { # more than one type, the normal case
+      <& tr-select-table.html,
+        label         => 'Reason type',
+        field         => $id.'_new_reason_type',
+        table         => 'reason_type',
+        name_col      => 'type',
+        hashref       => { 'class' => $class },
+        disable_empty => 1,
+      &>
+%   } # 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-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     => '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 (hints themselves come from xmlhttp-reason-hint)
 <TR>
-  <TD ALIGN="right">Reason</TD>
-  <TD>
-    <SELECT id="<% $id %>" name="<% $name %>" onFocus="sh_add<% $func_suffix %>()" onChange="sh_add<% $func_suffix %>()">
-%    my @reasons = qsearch( { table =>'reason', 
-%                             hashref => {},
-%                             extra_sql => $extra_sql,
-%                             addl_from => 'LEFT JOIN reason_type ON reason_type.typenum = reason.reason_type',
-%                           });
-      <OPTION VALUE="" <% ($init_reason eq "") ? 'SELECTED' : '' %>>Select Reason...</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>
-%    }
-%
-    </SELECT>
-  </TD>
-</TR>
-
-<TR>
-  <TD ALIGN="right">
-    <P id="new<% $id %>TLabel" style="display:<% $display %>">Reason Type</P>
-  </TD>
-  <TD>
-    <SELECT id="new<% $id %>T" name="new<% $name %>T" "<% $disabled %>" style="display:<% $display %>">
-%     for my $type (qsearch( 'reason_type', { 'class' => $class } )){
-        <OPTION VALUE="<% $type->typenum %>" <% ($init_type == $type->typenum) ? 'SELECTED' : '' %>><% $type->type %></OPTION>
-%     }
-    </SELECT>
-  </TD>
-</TR>
-
-<TR>
-  <TD ALIGN="right">
-    <P id="new<% $id %>Label" style="display:<% $display %>">New Reason</P>
+  <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint" style="font-size:small">
   </TD>
-  <TD><INPUT id="new<% $id %>" name="new<% $name %>" type="text" value="<% $init_newreason %>" "<% $disabled %>" style="display:<% $display %>"></TD>
 </TR>
 
 <%init>
 
+my $curuser = $FS::CurrentUser::CurrentUser;
 my %opt = @_;
 
 my $name = $opt{'field'};
 my $class = $opt{'reason_class'};
-my $init_reason = $opt{'curr_value'};
+my $label = $opt{'label'} ? $opt{'label'} : 'Reason';
+my $hide_addnew = $opt{'hide_addnew'} ? $opt{'hide_addnew'} : '';
 
-my $controlledbutton = $opt{'control_button'};
+my $init_reason;
+if ( $opt{'cgi'} ) {
+  $init_reason = $opt{'cgi'}->param($name);
+}
+$init_reason ||= $opt{'curr_value'};
 
-( my $func_suffix = $name ) =~ s/\./_/g;
+my $id = $opt{'id'} || $name;
+$id =~ s/\./_/g; # for edit/part_event
 
-my $id = $opt{'id'} || $func_suffix;
+my $label_id = $opt{'label_id'} || '';
 
-my( $add_access_right, $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';
-} else {
-  die "illegal class: $class";
-}
+my $add_access_right = $FS::reason_type::class_add_access_right{$class}
+  or die "unknown class: $class";
 
-my( $display, $disabled ) = ( 'none', 'DISABLED' );
-my( $init_type, $init_newreason ) = ( '', '' );
-if ($init_reason == -1 || ref($init_reason) ) {
+my @reasons = qsearch({
+  'table'           => 'reason',
+  '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',
+});
 
-  $display = 'inline';
-  $disabled = '';
 
-  if ( ref($init_reason) ) {
-    $init_type      = $init_reason->{'typenum'};
-    $init_newreason = $init_reason->{'reason'};
-    $init_reason = -1;
-  }
+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) && !$hide_addnew ) {
+  @post_options = ( -1 => 'Add new reason' );
 }
 
-my $extra_sql = "WHERE class = '$class' ORDER BY reason_type";
-my $curuser = $FS::CurrentUser::CurrentUser;
-
 </%init>