add customer fields option with agent, display_custnum, status and name, RT#73721
[freeside.git] / FS / FS / discount.pm
index 558d0f8..13146a9 100644 (file)
@@ -1,8 +1,7 @@
 package FS::discount;
+use base qw( FS::Record );
 
 use strict;
-use base qw( FS::Record );
-use FS::Record qw( qsearch qsearchs );
 
 =head1 NAME
 
@@ -54,6 +53,11 @@ months
 
 disabled
 
+=item setup - apply discount to setup fee (not just to recurring fee)
+
+If the discount is based on a percentage, then the % will be applied to the
+setup and recurring portions. 
+
 =back
 
 =head1 METHODS
@@ -113,41 +117,104 @@ and replace methods.
 sub check {
   my $self = shift;
 
+  if ( $self->_type eq 'Select discount type' ) {
+    return 'Please select a discount type';
+  } elsif ( $self->amount > 0 ) {
+    $self->set('percent', '0');
+  } elsif ( $self->percent > 0 ) {
+    $self->set('amount', '0.00');
+  } else {
+    return "Discount amount or percentage must be > 0";
+  }
+
   my $error = 
     $self->ut_numbern('discountnum')
+    || $self->ut_foreign_keyn('classnum', 'discount_class', 'classnum')
     || $self->ut_textn('name')
     || $self->ut_money('amount')
     || $self->ut_float('percent') #actually decimal, but this will do
     || $self->ut_floatn('months') #actually decimal, but this will do
     || $self->ut_enum('disabled', [ '', 'Y' ])
+    || $self->ut_enum('setup', [ '', 'Y' ])
+    #|| $self->ut_enum('linked', [ '', 'Y' ])
   ;
   return $error if $error;
 
+#causes "months must be integers greater than 1" errors when you go back and
+# try to edit an existing discount (because the months format as NN.000)
+#not worth whatever reason it came in with "prepayment discounts rt#5318" for
+#  #discourage non-integer months for package discounts
+#  if ($self->discountnum) {
+#    my $sql =
+#      "SELECT count(*) FROM part_pkg_discount WHERE part_pkg_discount.discountnum = ".
+#      $self->discountnum;
+#
+#    my $count = $self->scalar_sql($sql); 
+#    return "months must be integers greater than 1"
+#      if ( $count && ($self->ut_number('months') || $self->months < 2) );
+#  }
+    
   $self->SUPER::check;
 }
 
+=item description_short
+
 =item description
 
 Returns a text description incorporating the amount, percent and months fields.
 
+description_short omits term information
+
 =cut
 
-sub description {
+sub description_short {
   my $self = shift;
 
   my $conf = new FS::Conf;
   my $money_char = $conf->config('money_char') || '$';  
 
-  my $desc = '';
-  $desc .= $money_char. sprintf('%.2f/month ', $self->amount)
+  my $desc;
+  if ( $self->name ) {
+    $desc = $self->name . ': ';
+  } else {
+    $desc = 'Discount of ';
+  }
+  $desc .= $money_char. sprintf('%.2f/month', $self->amount)
     if $self->amount > 0;
-  $desc .= $self->percent. '% '
+
+  ( my $percent = $self->percent ) =~ s/\.0+$//;
+  $percent =~ s/(\.\d*[1-9])0+$/$1/;
+  $desc .= $percent. '%'
     if $self->percent > 0;
-  $desc .= 'for '. $self->months. ' months' if $self->months;
 
   $desc;
 }
 
+sub description {
+  my $self = shift;
+  my $desc = $self->description_short;
+
+  ( my $months = $self->months ) =~ s/\.0+$//;
+  $months =~ s/(\.\d*[1-9])0+$/$1/;
+  if ($months) {
+    if ($months == 1) {
+      $desc .= " for 1 month";
+    } else {
+      $desc .= " for $months months";
+    }
+  }
+
+  $desc .= ', applies to setup' if $self->setup;
+
+  $desc;
+}
+
+sub classname {
+  my $self = shift;
+  my $discount_class = $self->discount_class;
+  $discount_class ? $discount_class->classname : '(none)';
+}
+
 =back
 
 =head1 BUGS