discounts, RT#6679
authorivan <ivan>
Fri, 5 Feb 2010 02:39:31 +0000 (02:39 +0000)
committerivan <ivan>
Fri, 5 Feb 2010 02:39:31 +0000 (02:39 +0000)
12 files changed:
FS/FS/cust_pkg.pm
FS/FS/cust_pkg_discount.pm
FS/FS/discount.pm
FS/FS/part_pkg/flat.pm
httemplate/edit/cust_pkg_discount.html
httemplate/edit/discount.html
httemplate/edit/process/discount.html
httemplate/edit/process/quick-cust_pkg.cgi
httemplate/elements/tr-select-discount.html
httemplate/misc/order_pkg.html
httemplate/view/cust_main/order_pkg_link.html
httemplate/view/cust_main/packages/status.html

index 5cdca09..acc73df 100644 (file)
@@ -277,15 +277,7 @@ sub insert {
                     );
 
   if ( $self->discountnum ) {
-    #XXX new/custom discount case
-    my $cust_pkg_discount = new FS::cust_pkg_discount {
-      'pkgnum'      => $self->pkgnum,
-      'discountnum' => $self->discountnum,
-      'months_used' => 0,
-      'end_date'    => '', #XXX
-      'otaker'      => $self->otaker,
-    };
-    my $error = $cust_pkg_discount->insert;
+    my $error = $self->insert_discount();
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return $error;
@@ -2303,6 +2295,44 @@ sub insert_reason {
   $cust_pkg_reason->insert;
 }
 
+=item insert_discount
+
+Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
+inserting a new discount on the fly (see L<FS::discount>).
+
+Available options are:
+
+=over 4
+
+=item discountnum
+
+=back
+
+If there is an error, returns the error, otherwise returns false.
+
+=cut
+
+sub insert_discount {
+  #my ($self, %options) = @_;
+  my $self = shift;
+
+  my $cust_pkg_discount = new FS::cust_pkg_discount {
+    'pkgnum'      => $self->pkgnum,
+    'discountnum' => $self->discountnum,
+    'months_used' => 0,
+    'end_date'    => '', #XXX
+    'otaker'      => $self->otaker,
+    #for the create a new discount case
+    '_type'       => $self->discountnum__type,
+    'amount'      => $self->discountnum_amount,
+    'percent'     => $self->discountnum_percent,
+    'months'      => $self->discountnum_months,
+    #'disabled'    => $self->discountnum_disabled,
+  };
+
+  $cust_pkg_discount->insert;
+}
+
 =item set_usage USAGE_VALUE_HASHREF 
 
 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
index 9fc618c..8dd00de 100644 (file)
@@ -2,7 +2,7 @@ package FS::cust_pkg_discount;
 
 use strict;
 use base qw( FS::Record );
-use FS::Record qw( qsearchs ); # qsearch );
+use FS::Record qw( dbh qsearchs ); # qsearch );
 use FS::cust_pkg;
 use FS::discount;
 
@@ -85,7 +85,47 @@ otherwise returns false.
 
 =cut
 
-# the insert method can be inherited from FS::Record
+sub insert {
+  #my( $self, %options ) = @_;
+  my $self = shift;
+
+  local $SIG{HUP} = 'IGNORE';
+  local $SIG{INT} = 'IGNORE';
+  local $SIG{QUIT} = 'IGNORE';
+  local $SIG{TERM} = 'IGNORE';
+  local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
+
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  if ( $self->discountnum == -1 ) {
+    my $discount = new FS::discount {
+      '_type'    => $self->_type,
+      'amount'   => $self->amount,
+      'percent'  => $self->percent,
+      'months'   => $self->months,
+      'disabled' => 'Y',
+    };
+    my $error = $discount->insert;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+    $self->discountnum($discount->discountnum);
+  }
+
+  my $error = $self->SUPER::insert; #(@_); #(%options);
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  '';
+
+}
 
 =item delete
 
index 6771510..8afeb2e 100644 (file)
@@ -113,6 +113,16 @@ and replace methods.
 sub check {
   my $self = shift;
 
+  if ( $self->_type eq 'Select discount type' ) {
+    return 'Please select a discount type';
+  } elsif ( $self->_type eq 'Amount' ) {
+    $self->percent('0');
+    return 'Amount must be greater than 0' unless $self->amount > 0;
+  } elsif ( $self->_type eq 'Percentage' ) {
+    $self->amount('0.00');
+    return 'Percentage must be greater than 0' unless $self->percent > 0;
+  }
+
   my $error = 
     $self->ut_numbern('discountnum')
     || $self->ut_textn('name')
index 402730d..e5fc089 100644 (file)
@@ -188,15 +188,17 @@ sub calc_discount {
      die "error discounting: $error" if $error;
 
      $amount *= $months;
+     $amount = sprintf('%.2f', $amount);
 
      #add details on discount to invoice
      my $conf = new FS::Conf;
      my $money_char = $conf->config('money_char') || '$';  
+     $months = sprintf('%.2f', $months) if $months =~ /\./;
 
      my $d = 'Includes ';
      $d .= $discount->name. ' ' if $discount->name;
      $d .= 'discount of '. $discount->description_short;
-     $d .= " for $months month". ( $months>1 ? 's' : '' );
+     $d .= " for $months month". ( $months!=1 ? 's' : '' );
      $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
      push @$details, $d;
 
index 22d8c63..0bb84b8 100755 (executable)
@@ -28,7 +28,8 @@
 
 <% include('/elements/tr-select-discount.html',
              'empty_label' => ( $pkgdiscountnum ? '' : 'Select discount' ),
-             'onchange'    => 'enable_discount_pkg',
+             'onchange'    => 'enable_discount_pkg()',
+             'cgi'         => $cgi,
           )
 %>
 
index 4f440b2..6e0d8e1 100644 (file)
@@ -29,7 +29,7 @@
                                '_type'       => 'Type&nbsp;',
                                'amount'      => 'Amount&nbsp;',
                                'percent'     => 'Percentage&nbsp;',
-                               'months'      => '# of Months',
+                               'months'      => 'Duration (months)',
                              },
                  'viewall_dir' => 'browse',
                  'new_callback' => $new_callback,
index 54307b7..80dc0f5 100644 (file)
@@ -1,7 +1,6 @@
 <% include( 'elements/process.html',
                'table'       => 'discount',
                'viewall_dir' => 'browse',
-               'precheck_callback' => $precheck_callback,
            )
 %>
 <%init>
@@ -9,20 +8,4 @@
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
 
-my $precheck_callback = sub {
-  my( $cgi ) = @_;
-
-  if ( $cgi->param('_type') eq 'Select discount type' ) {
-    return 'Please select a discount type';
-  } elsif ( $cgi->param('_type') eq 'Amount' ) {
-    $cgi->param('percent', '0');
-    return 'Amount must be greater than 0' unless $cgi->param('amount') > 0;
-  } elsif ( $cgi->param('_type') eq 'Percentage' ) {
-    $cgi->param('amount', '0.00');
-    return 'Percentage must be greater than 0' unless $cgi->param('percent') > 0;
-  }
-
-  '';
-};
-
 </%init>
index c601567..a095892 100644 (file)
@@ -45,17 +45,27 @@ my $refnum = $1;
 $cgi->param('locationnum') =~ /^(\-?\d*)$/
   or die 'illegal locationnum '. $cgi->param('locationnum');
 my $locationnum = $1;
+$cgi->param('discountnum') =~ /^(\-?\d*)$/
+  or die 'illegal discountnum '. $cgi->param('discountnum');
+my $discountnum = $1;
+
 
 my $cust_pkg = new FS::cust_pkg {
-  'custnum'     => $custnum,
-  'pkgpart'     => $pkgpart,
-  'start_date'  => ( scalar($cgi->param('start_date'))
-                       ? str2time($cgi->param('start_date'))
-                       : ''
-                   ),
-  'discountnum' => scalar($cgi->param('discountnum')),
-  'refnum'      => $refnum,
-  'locationnum' => $locationnum,
+  'custnum'              => $custnum,
+  'pkgpart'              => $pkgpart,
+  'start_date'           => ( scalar($cgi->param('start_date'))
+                                ? str2time($cgi->param('start_date'))
+                                : ''
+                            ),
+  'refnum'               => $refnum,
+  'locationnum'          => $locationnum,
+  'discountnum'          => $discountnum,
+  #for the create a new discount case
+  'discountnum__type'    => scalar($cgi->param('discountnum__type')),
+  'discountnum_amount'   => scalar($cgi->param('discountnum_amount')),
+  'discountnum_percent'  => scalar($cgi->param('discountnum_percent')),
+  'discountnum_months'   => scalar($cgi->param('discountnum_months')),
+  #'discountnum_disabled' => scalar($cgi->param('discountnum_disabled')),
 };
 
 my %opt = ( 'cust_pkg' => $cust_pkg );
index df221da..258eeb3 100644 (file)
@@ -6,11 +6,11 @@
 % } else { 
 
   <TR>
-    <TD ALIGN="right"><% $opt{'label'} || '<B>Discount</B>' %></TD>
+    <TD ALIGN="right" WIDTH="176"><% $opt{'label'} || '<B>Discount</B>' %></TD>
     <TD <% $colspan %>>
       <% include( '/elements/select-discount.html',
                     'curr_value' => $discountnum,
-                    'onchange'   => $name.'_changed',
+                    'onchange'   => $onchange,
                     %opt,
                 )
       %>
@@ -19,7 +19,7 @@
 
 % # a weird kind of false laziness w/edit/discount.html
 
-  <INPUT TYPE="hidden" NAME="<% $name %>_disabled" VALUE="Y">
+% #  <INPUT TYPE="hidden" NAME="<% $name %>_disabled" VALUE="Y">
 
 
   <% include( '/elements/tr-select.html',
                 'field'      => $name. '__type',
                 'id'         => $name. '__type',
                 'options'    => \@_type_options,
-                #XXX 'curr_value' => 
-                'onchange'   => '_type_changed',
+                'curr_value' => scalar($cgi->param($name.'__type')),
+                'onchange'   => $name.'__type_changed',
                 'colspan'    => $opt{'colspan'},
             )
   %>
 
   <% include( '/elements/tr-input-money.html',
-                'label'      => '<B>Discount Amount</B>',
+                'label'      => '<B>Discount Amount&nbsp;</B>',
                 'field'      => $name. '_amount',
                 'id'         => $name. '_amount',
                 'default'    => '0.00',
-                #XXX 'curr_value' =>
+                'curr_value' => scalar($cgi->param($name.'_amount')),
                 'colspan'    => $opt{'colspan'},
             )
   %>
                 'field'      => $name. '_percent',
                 'id'         => $name. '_percent',
                 'default'    => '0',
-                #XXX 'curr_value' =>
+                'curr_value' => scalar($cgi->param($name.'_percent')),
                 'colspan'    => $opt{'colspan'},
             )
   %>
 
   <% include( '/elements/tr-input-text.html',
-                'label'   => '<B>Discount # of Months</B>',
+                'label'   => '<B>Discount duration (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' =>
+                'curr_value' => scalar($cgi->param($name.'_months')),
                 'colspan'    => $opt{'colspan'},
            )
   %>
 %       #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 %>_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 %>_percent_input0').style.display = 'none';
         <% $ge %>('<% $name %>_percent_input0').style.visibility = 'hidden';
 
     }
 
     function <% $name %>__type_changed(what) {
-      var _type = what.options[what.selectedIndex].value;
+      var <% $name %>__type = what.options[what.selectedIndex].value;
 
       if ( <% $name %>__type == '<% $select %>' ) {
         <% $ge %>('<% $name %>_amount_label0').style.display = 'none';
       } 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 %>_amount_input0').style.display = '';
+        <% $ge %>('<% $name %>_amount_input0').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';
+        <% $ge %>('<% $name %>_percent_input0').style.display = 'none';
+        <% $ge %>('<% $name %>_percent_input0').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 %>_amount_input0').style.display = 'none';
+        <% $ge %>('<% $name %>_amount_input0').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 = '';
+        <% $ge %>('<% $name %>_percent_input0').style.display = '';
+        <% $ge %>('<% $name %>_percent_input0').style.visibility = '';
      }
 
     }
 
+    <% $name %>_changed(<% $ge %>('<% $name %>'));
+
   </SCRIPT>
 
 % } 
 <%init>
 
 my %opt = @_;
+my $cgi = $opt{'cgi'};
 my $discountnum = $opt{'curr_value'} || $opt{'value'};
 
 $opt{'discount'} ||= [ qsearch( 'discount', { disabled=>'' } ) ];
@@ -170,4 +173,7 @@ unshift @_type_options, $select;
 
 my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
 
+my $onchange = ( $opt{'onchange'} ? delete($opt{'onchange'}).';' : '' ).
+               $name.'_changed(this);';
+
 </%init>
index e05205b..864e832 100644 (file)
@@ -69,6 +69,8 @@
 % if ( $curuser->access_right('Discount customer package') ) {
   <% include('/elements/tr-select-discount.html',
                'element_etc' => 'DISABLED',
+               'colspan'     => 7,
+               'cgi'         => $cgi,
             )
   %>
 % }
index 9a12019..0c53f2a 100644 (file)
@@ -6,7 +6,7 @@
               'cust_main'   => $cust_main,
               'closetext'   => 'Close',
               'width'       => 763,
-              'height'      => 408,
+              'height'      => 476,
           )
 %>
 <%init>
index c65eb70..7b1b53a 100644 (file)
@@ -300,9 +300,11 @@ sub pkg_status_row_discount {
     my $discount = $cust_pkg_discount->discount;
 
     my $label = '<B>Discount</B>: '. $discount->description;
-    $label .= ' ('. ( $discount->months - $cust_pkg_discount->months_used ).
-              ' months remaining)'
-      if $discount->months;
+    if ( $discount->months ) {
+      my $remaining = $discount->months - $cust_pkg_discount->months_used;
+      $remaining = sprintf('%.2f', $remaining) if $remaining =~ /\./;
+      $label .= " ($remaining months remaining)"
+    }
 
     $label .= ' <FONT SIZE="-1">('.
                 '<A HREF="../misc/delete-cust_pkg_discount.html?'.