discounts, RT#6679
authorivan <ivan>
Thu, 4 Feb 2010 20:39:56 +0000 (20:39 +0000)
committerivan <ivan>
Thu, 4 Feb 2010 20:39:56 +0000 (20:39 +0000)
FS/FS/discount.pm
httemplate/edit/discount.html
httemplate/elements/select-discount.html
httemplate/elements/select-table.html
httemplate/elements/tr-input-text.html
httemplate/elements/tr-select-discount.html
httemplate/elements/tr-select.html

index 457c9d6..6771510 100644 (file)
@@ -142,7 +142,7 @@ sub description_short {
   my $conf = new FS::Conf;
   my $money_char = $conf->config('money_char') || '$';  
 
-  my $desc = '';
+  my $desc = $self->name ? $self->name.': ' : '';
   $desc .= $money_char. sprintf('%.2f/month ', $self->amount)
     if $self->amount > 0;
   $desc .= $self->percent. '% '
index 24d8fa5..4f440b2 100644 (file)
@@ -4,6 +4,8 @@
                  'fields' => [
                                'name',
                                { field => 'disabled', type => 'checkbox', value=>'Y', },
+                               # a weird kind of false laziness
+                               # w/elements/tr-select-discount.html
                                { field => '_type',    type => 'select',
                                  options => \@_type_options,
                                  onchange => '_type_changed',
index e0b6e6c..b7f1fa5 100644 (file)
@@ -1,10 +1,11 @@
 <% include( '/elements/select-table.html',
-                 'table'       => 'discount',
-                 'name_col'    => 'description',
-                 'order_by'    => 'ORDER BY discountnum', #XXX weight
-                 'value'       => $discountnum,
-                 'empty_label' => '(none)',
-                 'hashref'     => { 'disabled' => '' },
+                 'table'        => 'discount',
+                 'name_col'     => 'description',
+                 'order_by'     => 'ORDER BY discountnum', #XXX weight
+                 'value'        => $discountnum,
+                 'empty_label'  => '(none)',
+                 'hashref'      => { 'disabled' => '' },
+                 'post_options' => $post_options,
                  %opt,
              )
 %>
@@ -16,5 +17,12 @@ my $discountnum = $opt{'curr_value'} || $opt{'value'};
 $opt{'records'} = delete $opt{'discount'}
   if $opt{'discount'};
 
+my $curuser = $FS::CurrentUser::CurrentUser;
+
+#make an opt if we need to turn this off
+my $post_options = $curuser->access_right('Custom discount customer package')
+  ? [ -1 => 'Custom discount' ]
+  : [];
+
 </%init>
 
index 4d8d9a9..3e25c9f 100644 (file)
@@ -31,6 +31,7 @@ Example:
 
     #basic params controlling the resulting <SELECT>
     'pre_options'    => [ 'value' => 'option' ], #before normal options
+    'post_options'   => [ 'value' => 'option' ], #after normal options
     'empty_label'    => '', #better specify it though, the default might change
     'multiple'       => 0, # bool
     'disable_empty'  => 0, # bool (implied by multiple)
@@ -103,6 +104,16 @@ Example:
      %>
 % } 
 
+% while ( @post_options ) { 
+%   my $post_opt   = shift(@post_options);
+%   my $post_label = shift(@post_options);
+%   my $selected =    ( ref($value) && $value->{$post_opt} )
+%                  || ( $value eq $post_opt );
+    <OPTION VALUE="<% $post_opt %>"
+            <% $selected ? 'SELECTED' : '' %>
+    ><% $post_label %>
+% } 
+
 </SELECT>
 
 %}
@@ -180,6 +191,7 @@ if ( ref( $value ) eq 'ARRAY' ) {
   $value = { map { $_ => 1 } @$value };
 }
 
-my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();
+my @pre_options  = $opt{pre_options}  ? @{ $opt{pre_options} } : ();
+my @post_options = $opt{post_options} ? @{ $opt{post_options} } : ();
 
 </%init>
index 81efa1f..2127933 100644 (file)
@@ -1,6 +1,6 @@
 <% include('tr-td-label.html', @_ ) %>
 
-  <TD <% $cell_style %> ID="<% $opt{input_id} || $opt{id}.'_input0' %>"><% include('input-text.html', @_ ) %></TD>
+  <TD <% $colspan %> <% $cell_style %> ID="<% $opt{input_id} || $opt{id}.'_input0' %>"><% include('input-text.html', @_ ) %></TD>
 
 </TR>
 
@@ -10,4 +10,6 @@ my %opt = @_;
 
 my $cell_style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : '';
 
+my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
+
 </%init>
index 4ff8d13..df221da 100644 (file)
-% if ( scalar(@{ $opt{'discount'} }) == 0 ) { 
+% if ( scalar(@{ $opt{'discount'} }) == 0
+%      && ! $curuser->access_right('Custom discount customer package') ) { 
 
-  <INPUT TYPE="hidden" NAME="<% $opt{'element_name'} || $opt{'field'} || 'discountnum' %>" VALUE="">
+  <INPUT TYPE="hidden" NAME="<% $name %>" VALUE="<% $discountnum %>">
 
 % } else { 
 
   <TR>
     <TD ALIGN="right"><% $opt{'label'} || '<B>Discount</B>' %></TD>
-    <TD>
+    <TD <% $colspan %>>
       <% include( '/elements/select-discount.html',
                     'curr_value' => $discountnum,
+                    'onchange'   => $name.'_changed',
                     %opt,
                 )
       %>
     </TD>
   </TR>
 
+% # a weird kind of false laziness w/edit/discount.html
+
+  <INPUT TYPE="hidden" NAME="<% $name %>_disabled" VALUE="Y">
+
+
+  <% include( '/elements/tr-select.html',
+                'label'      => '<B>Discount Type</B>',
+                'field'      => $name. '__type',
+                'id'         => $name. '__type',
+                'options'    => \@_type_options,
+                #XXX 'curr_value' => 
+                'onchange'   => '_type_changed',
+                'colspan'    => $opt{'colspan'},
+            )
+  %>
+
+  <% include( '/elements/tr-input-money.html',
+                'label'      => '<B>Discount Amount</B>',
+                'field'      => $name. '_amount',
+                'id'         => $name. '_amount',
+                'default'    => '0.00',
+                #XXX 'curr_value' =>
+                'colspan'    => $opt{'colspan'},
+            )
+  %>
+
+  <% include( '/elements/tr-input-percentage.html',
+                'label'      => '<B>Discount Percentage</B>',
+                'field'      => $name. '_percent',
+                'id'         => $name. '_percent',
+                'default'    => '0',
+                #XXX 'curr_value' =>
+                'colspan'    => $opt{'colspan'},
+            )
+  %>
+
+  <% include( '/elements/tr-input-text.html',
+                'label'   => '<B>Discount # of Months</B>',
+                'field'   => $name. '_months',
+                'id'      => $name. '_months',
+                'size'    => 2,
+                'postfix' => qq(<FONT SIZE="-1" ID="${name}_months_postfix"><I>(blank for non-expiring discount)</I></FONT>),
+                #XXX 'curr_value' =>
+                'colspan'    => $opt{'colspan'},
+           )
+  %>
+
+  <SCRIPT TYPE="text/javascript">
+
+%   my $ge = 'document.getElementById';
+
+    function <% $name %>_changed(what) {
+      var <% $name %> = what.options[what.selectedIndex].value;
+
+      if ( <% $name %> == '-1' ) {
+        <% $ge %>('<% $name %>__type_label0').style.display = '';
+        <% $ge %>('<% $name %>__type_label0').style.visibility = '';
+        <% $ge %>('<% $name %>__type').style.display = '';
+        <% $ge %>('<% $name %>__type').style.visibility = '';
+%       #XXX retrieve previous visibility for amount, percent :/
+        <% $ge %>('<% $name %>_months_label0').style.display = '';
+        <% $ge %>('<% $name %>_months_label0').style.visibility = '';
+        <% $ge %>('<% $name %>_months').style.display = '';
+        <% $ge %>('<% $name %>_months').style.visibility = '';
+        <% $ge %>('<% $name %>_months_postfix').style.display = '';
+        <% $ge %>('<% $name %>_months_postfix').style.visibility = '';
+      } else {
+
+        <% $ge %>('<% $name %>__type_label0').style.display = 'none';
+        <% $ge %>('<% $name %>__type_label0').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>__type').style.display = 'none';
+        <% $ge %>('<% $name %>__type').style.visibility = 'hidden';
+
+%       #XXX save visibility settings for amount, percent :/
+        <% $ge %>('<% $name %>_amount_label0').style.display = 'none';
+        <% $ge %>('<% $name %>_amount_label0').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_amount').style.display = 'none';
+        <% $ge %>('<% $name %>_amount').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_amount_input0').style.display = 'none';
+        <% $ge %>('<% $name %>_amount_input0').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_percent_label0').style.display = 'none';
+        <% $ge %>('<% $name %>_percent_label0').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_percent').style.display = 'none';
+        <% $ge %>('<% $name %>_percent').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_percent_input0').style.display = 'none';
+        <% $ge %>('<% $name %>_percent_input0').style.visibility = 'hidden';
+
+        <% $ge %>('<% $name %>_months_label0').style.display = 'none';
+        <% $ge %>('<% $name %>_months_label0').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_months').style.display = 'none';
+        <% $ge %>('<% $name %>_months').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_months_postfix').style.display = 'none';
+        <% $ge %>('<% $name %>_months_postfix').style.visibility = 'hidden';
+
+      }
+
+    }
+
+    function <% $name %>__type_changed(what) {
+      var _type = what.options[what.selectedIndex].value;
+
+      if ( <% $name %>__type == '<% $select %>' ) {
+        <% $ge %>('<% $name %>_amount_label0').style.display = 'none';
+        <% $ge %>('<% $name %>_amount_label0').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_amount').style.display = 'none';
+        <% $ge %>('<% $name %>_amount').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_percent_label0').style.display = 'none';
+        <% $ge %>('<% $name %>_percent_label0').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_percent').style.display = 'none';
+        <% $ge %>('<% $name %>_percent').style.visibility = 'hidden';
+      } else if ( <% $name %>__type == 'Amount' ) {
+        <% $ge %>('<% $name %>_amount_label0').style.display = '';
+        <% $ge %>('<% $name %>_amount_label0').style.visibility = '';
+        <% $ge %>('<% $name %>_amount').style.display = '';
+        <% $ge %>('<% $name %>_amount').style.visibility = '';
+        <% $ge %>('<% $name %>_percent_label0').style.display = 'none';
+        <% $ge %>('<% $name %>_percent_label0').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_percent').style.display = 'none';
+        <% $ge %>('<% $name %>_percent').style.visibility = 'hidden';
+      } else if ( <% $name %>__type == 'Percentage' ) {
+        <% $ge %>('<% $name %>_amount_label0').style.display = 'none';
+        <% $ge %>('<% $name %>_amount_label0').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_amount').style.display = 'none';
+        <% $ge %>('<% $name %>_amount').style.visibility = 'hidden';
+        <% $ge %>('<% $name %>_percent_label0').style.display = '';
+        <% $ge %>('<% $name %>_percent_label0').style.visibility = '';
+        <% $ge %>('<% $name %>_percent').style.display = '';
+        <% $ge %>('<% $name %>_percent').style.visibility = '';
+     }
+
+    }
+
+  </SCRIPT>
+
 % } 
 <%init>
 
@@ -23,5 +159,15 @@ my $discountnum = $opt{'curr_value'} || $opt{'value'};
 
 $opt{'discount'} ||= [ qsearch( 'discount', { disabled=>'' } ) ];
 
-</%init>
+my $curuser = $FS::CurrentUser::CurrentUser;
+
+my $name = $opt{'element_name'} || $opt{'field'} || 'discountnum';
+
+my $select = 'Select discount type';
 
+my @_type_options = ( 'Amount', 'Percentage' );
+unshift @_type_options, $select;
+
+my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
+
+</%init>
index 07b0a01..f9d54f1 100644 (file)
@@ -1,6 +1,6 @@
 <% include('tr-td-label.html', @_ ) %>
 
-  <TD <% $style %>>
+  <TD <% $colspan %> <% $style %>>
 
     <SELECT NAME          = "<% $opt{field} %>"
             ID            = "<% $opt{id} %>"
@@ -58,4 +58,6 @@ my $style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : '';
 
 my $curr_value = $opt{'curr_value'};
 
+my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
+
 </%init>