start of package class web UI (add/edit package classes, package class selection...
[freeside.git] / FS / FS / part_pkg.pm
index faaf485..05dc599 100644 (file)
@@ -2,7 +2,7 @@ package FS::part_pkg;
 
 use strict;
 use vars qw( @ISA %freq %plans $DEBUG );
-use Carp qw(carp cluck);
+use Carp qw(carp cluck confess);
 use Tie::IxHash;
 use FS::Conf;
 use FS::Record qw( qsearch qsearchs dbh dbdef );
@@ -12,8 +12,11 @@ use FS::cust_pkg;
 use FS::agent_type;
 use FS::type_pkgs;
 use FS::part_pkg_option;
+use FS::pkg_class;
 
-@ISA = qw( FS::Record );
+@ISA = qw( FS::Record ); # FS::option_Common ); # this can use option_Common
+                                                # when all the plandata bs is
+                                                # gone
 
 $DEBUG = 0;
 
@@ -56,6 +59,8 @@ inherits from FS::Record.  The following fields are currently supported:
 
 =item comment - Text name of this package definition (non-customer-viewable)
 
+=item classnum - Optional package class (see L<FS::pkg_class>)
+
 =item promo_code - Promotional code
 
 =item setup - Setup fee expression (deprecated)
@@ -230,7 +235,10 @@ sub insert {
   my $pkg_svc = $options{'pkg_svc'} || {};
   foreach my $part_svc ( qsearch('part_svc', {} ) ) {
     my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
-    my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
+    my $primary_svc =
+      ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
+        ? 'Y'
+        : '';
 
     my $pkg_svc = new FS::pkg_svc( {
       'pkgpart'     => $self->pkgpart,
@@ -372,6 +380,7 @@ sub replace {
     next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
   
     my $new_pkg_svc = new FS::pkg_svc( {
+      'pkgsvcnum'   => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
       'pkgpart'     => $new->pkgpart,
       'svcpart'     => $part_svc->svcpart,
       'quantity'    => $quantity, 
@@ -414,7 +423,7 @@ sub check {
     my $error = $self->ut_number('freq');
     return $error if $error;
   } else {
-    $self->freq =~ /^(\d+[dw]?)$/
+    $self->freq =~ /^(\d+[hdw]?)$/
       or return "Illegal or empty freq: ". $self->freq;
     $self->freq($1);
   }
@@ -432,12 +441,54 @@ sub check {
   ;
   return $error if $error;
 
+  if ( $self->classnum !~ /^$/ ) {
+    my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum');
+    return $error if $error;
+  } else {
+    $self->classnum('');
+  }
+
   return 'Unknown plan '. $self->plan
     unless exists($plans{$self->plan});
 
+  my $conf = new FS::Conf;
+  return 'Taxclass is required'
+    if ! $self->taxclass && $conf->exists('require_taxclasses');
+
   '';
 }
 
+=item pkg_class
+
+Returns the package class, as an FS::pkg_class object, or the empty string
+if there is no package class.
+
+=cut
+
+sub pkg_class {
+  my $self = shift;
+  if ( $self->classnum ) {
+    qsearchs('pkg_class', { 'classnum' => $self->classnum } );
+  } else {
+    return '';
+  }
+}
+
+=item classname 
+
+Returns the package class name, or the empty string if there is no package
+class.
+
+=cut
+
+sub classname {
+  my $self = shift;
+  my $pkg_class = $self->pkg_class;
+  $pkg_class
+    ? $pkg_class->classname
+    : '';
+}
+
 =item pkg_svc
 
 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
@@ -529,6 +580,7 @@ Returns an english representation of the I<freq> field, such as "monthly",
 
 tie %freq, 'Tie::IxHash', 
   '0'  => '(no recurring fee)',
+  '1h' => 'hourly',
   '1d' => 'daily',
   '1w' => 'weekly',
   '2w' => 'biweekly (every 2 weeks)',
@@ -538,6 +590,10 @@ tie %freq, 'Tie::IxHash',
   '6'  => 'semiannually (every 6 months)',
   '12' => 'annually',
   '24' => 'biannually (every 2 years)',
+  '36' => 'triannually (every 3 years)',
+  '48' => '(every 4 years)',
+  '60' => '(every 5 years)',
+  '120' => '(every 10 years)',
 ;
 
 sub freq_pretty {
@@ -547,8 +603,8 @@ sub freq_pretty {
     $freq{$freq};
   } else {
     my $interval = 'month';
-    if ( $freq =~ /^(\d+)([dw])$/ ) {
-      my %interval = ( 'd'=>'day', 'w'=>'week' );
+    if ( $freq =~ /^(\d+)([hdw])$/ ) {
+      my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
       $interval = $interval{$2};
     }
     if ( $1 == 1 ) {
@@ -635,9 +691,16 @@ on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
 sub _rebless {
   my $self = shift;
   my $plan = $self->plan;
+  unless ( $plan ) {
+    confess "no price plan found for pkgpart ". $self->pkgpart. "\n"
+      if $DEBUG;
+    return $self;
+  }
+  return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
   my $class = ref($self). "::$plan";
+  warn "reblessing $self into $class" if $DEBUG;
   eval "use $class;";
-  #die $@ if $@;
+  die $@ if $@;
   bless($self, $class) unless $@;
   $self;
 }
@@ -689,6 +752,7 @@ sub calc_cancel { 0; }
 
 my %info;
 foreach my $INC ( @INC ) {
+  warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
   foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
     warn "attempting to load plan info from $file\n" if $DEBUG;
     $file =~ /\/(\w+)\.pm$/ or do {
@@ -729,15 +793,18 @@ sub plan_info {
 
 =head1 NEW PLAN CLASSES
 
-A module should be added in FS/FS/part_pkg/ (an example may be found in
-eg/plan_template.pm)
+A module should be added in FS/FS/part_pkg/  Eventually, an example may be
+found in eg/plan_template.pm.  Until then, it is suggested that you use the
+other modules in FS/FS/part_pkg/ as a guide.
 
 =head1 BUGS
 
 The delete method is unimplemented.
 
 setup and recur semantics are not yet defined (and are implemented in
-FS::cust_bill.  hmm.).
+FS::cust_bill.  hmm.).  now they're deprecated and need to go.
+
+plandata should go
 
 =head1 SEE ALSO