disable auto-billing of specific customer packages, RT#6378
[freeside.git] / FS / FS / part_pkg.pm
index 1ea941f..08c9b87 100644 (file)
@@ -1,7 +1,7 @@
 package FS::part_pkg;
 
 use strict;
-use vars qw( @ISA %plans $DEBUG $setup_hack );
+use vars qw( @ISA %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
 use Carp qw(carp cluck confess);
 use Scalar::Util qw( blessed );
 use Time::Local qw( timelocal_nocheck );
@@ -23,6 +23,7 @@ use FS::part_pkg_link;
 @ISA = qw( FS::m2m_Common FS::option_Common );
 $DEBUG = 0;
 $setup_hack = 0;
+$skip_pkg_svc_hack = 0;
 
 =head1 NAME
 
@@ -204,7 +205,11 @@ sub insert {
   warn "  inserting part_pkg_taxoverride records" if $DEBUG;
   my %overrides = %{ $options{'tax_overrides'} || {} };
   foreach my $usage_class ( keys %overrides ) {
-    my @overrides = (grep "$_", split (',', $overrides{$usage_class}) );
+    my $override =
+      ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
+        ? $overrides{$usage_class}
+        : '';
+    my @overrides = (grep "$_", split(',', $override) );
     my $error = $self->process_m2m (
                   'link_table'   => 'part_pkg_taxoverride',
                   'target_table' => 'tax_class',
@@ -217,26 +222,30 @@ sub insert {
     }
   }
 
-  warn "  inserting pkg_svc records" if $DEBUG;
-  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'} && $options{'primary_svc'}==$part_svc->svcpart )
-        ? 'Y'
-        : '';
-
-    my $pkg_svc = new FS::pkg_svc( {
-      'pkgpart'     => $self->pkgpart,
-      'svcpart'     => $part_svc->svcpart,
-      'quantity'    => $quantity, 
-      'primary_svc' => $primary_svc,
-    } );
-    my $error = $pkg_svc->insert;
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return $error;
+  unless ( $skip_pkg_svc_hack ) {
+
+    warn "  inserting pkg_svc records" if $DEBUG;
+    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'} && $options{'primary_svc'}==$part_svc->svcpart )
+          ? 'Y'
+          : '';
+
+      my $pkg_svc = new FS::pkg_svc( {
+        'pkgpart'     => $self->pkgpart,
+        'svcpart'     => $part_svc->svcpart,
+        'quantity'    => $quantity, 
+        'primary_svc' => $primary_svc,
+      } );
+      my $error = $pkg_svc->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
     }
+
   }
 
   if ( $options{'cust_pkg'} ) {
@@ -453,6 +462,7 @@ sub check {
     || $self->ut_textn('taxclass')
     || $self->ut_enum('disabled', [ '', 'Y' ] )
     || $self->ut_enum('custom', [ '', 'Y' ] )
+    || $self->ut_enum('no_auto', [ '', 'Y' ])
     #|| $self->ut_moneyn('setup_cost')
     #|| $self->ut_moneyn('recur_cost')
     || $self->ut_floatn('setup_cost')
@@ -460,6 +470,8 @@ sub check {
     || $self->ut_floatn('pay_weight')
     || $self->ut_floatn('credit_weight')
     || $self->ut_numbern('taxproductnum')
+    || $self->ut_foreign_keyn('classnum',       'pkg_class', 'classnum')
+    || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
     || $self->ut_foreign_keyn('taxproductnum',
                               'part_pkg_taxproduct',
                               'taxproductnum'
@@ -472,13 +484,6 @@ 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});
 
@@ -531,6 +536,22 @@ sub pkg_class {
   }
 }
 
+=item addon_pkg_class
+
+Returns the add-on package class, as an FS::pkg_class object, or the empty
+string if there is no add-on package class.
+
+=cut
+
+sub addon_pkg_class {
+  my $self = shift;
+  if ( $self->addon_classnum ) {
+    qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
+  } else {
+    return '';
+  }
+}
+
 =item categoryname 
 
 Returns the package category name, or the empty string if there is no package
@@ -561,6 +582,21 @@ sub classname {
     : '';
 }
 
+=item addon_classname 
+
+Returns the add-on package class name, or the empty string if there is no
+add-on package class.
+
+=cut
+
+sub addon_classname {
+  my $self = shift;
+  my $pkg_class = $self->addon_pkg_class;
+  $pkg_class
+    ? $pkg_class->classname
+    : '';
+}
+
 =item agent 
 
 Returns the associated agent for this event, if any, as an FS::agent object.
@@ -732,6 +768,7 @@ sub is_free {
   }
 }
 
+sub can_discount { 0; }
 
 sub freqs_href {
   #method, class method or sub? #my $self = shift;
@@ -872,9 +909,11 @@ sub options {
   map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
 }
 
-=item option OPTIONNAME
+=item option OPTIONNAME [ QUIET ]
 
-Returns the option value for the given name, or the empty string.
+Returns the option value for the given name, or the empty string.  If a true
+value is passed as the second argument, warnings about missing the option
+will be suppressed.
 
 =cut
 
@@ -917,10 +956,12 @@ sub svc_part_pkg_link {
 
 sub _part_pkg_link {
   my( $self, $type ) = @_;
-  qsearch('part_pkg_link', { 'src_pkgpart' => $self->pkgpart,
-                             'link_type'   => $type,
-                           }
-         );
+  qsearch({ table    => 'part_pkg_link',
+            hashref  => { 'src_pkgpart' => $self->pkgpart,
+                          'link_type'   => $type,
+                        },
+            order_by => "ORDER BY hidden",
+         });
 }
 
 sub self_and_bill_linked {
@@ -928,12 +969,18 @@ sub self_and_bill_linked {
 }
 
 sub _self_and_linked {
-  my( $self, $type ) = @_;
+  my( $self, $type, $hidden ) = @_;
+  $hidden ||= '';
+
+  my @result = ();
+  foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
+                     $self->_part_pkg_link($type) ) )
+  {
+    $_->hidden($hidden) if $hidden;
+    push @result, $_;
+  }
 
-  ( $self,
-    map { $_->dst_pkg->_self_and_linked($type) }
-        $self->_part_pkg_link($type)
-  );
+  (@result);
 }
 
 =item part_pkg_taxoverride [ CLASS ]
@@ -1281,6 +1328,7 @@ sub _upgrade_data { # class method
     $new->custom('Y');
     my $comment = $part_pkg->comment;
     $comment =~ s/^\(CUSTOM\) //;
+    $comment = '(none)' unless $comment =~ /\S/;
     $new->comment($comment);
 
     my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
@@ -1334,15 +1382,16 @@ sub _pkgs_sql {
 
   "
     (
-      agentnum IS NOT NULL
-      OR
-      0 < ( SELECT COUNT(*)
-              FROM type_pkgs
-                LEFT JOIN agent_type USING ( typenum )
-                LEFT JOIN agent AS typeagent USING ( typenum )
-              WHERE type_pkgs.pkgpart = part_pkg.pkgpart
-                AND typeagent.agentnum IN ($agentnums)
-          )
+      ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
+      OR ( agentnum IS NULL
+           AND EXISTS ( SELECT 1
+                          FROM type_pkgs
+                            LEFT JOIN agent_type USING ( typenum )
+                            LEFT JOIN agent AS typeagent USING ( typenum )
+                          WHERE type_pkgs.pkgpart = part_pkg.pkgpart
+                            AND typeagent.agentnum IN ($agentnums)
+                      )
+         )
     )
   ";