import NP and *LK* from shadow file as * (no password)
[freeside.git] / FS / FS / cust_pkg.pm
index eb207d6..a3297ab 100644 (file)
@@ -1,8 +1,7 @@
 package FS::cust_pkg;
 
 use strict;
-use vars qw(@ISA);
-use vars qw( $quiet );
+use vars qw(@ISA $disable_agentcheck $DEBUG);
 use FS::UID qw( getotaker dbh );
 use FS::Record qw( qsearch qsearchs );
 use FS::cust_svc;
@@ -30,6 +29,10 @@ use Mail::Header;
 
 @ISA = qw( FS::Record );
 
+$DEBUG = 0;
+
+$disable_agentcheck = 0;
+
 sub _cache {
   my $self = shift;
   my ( $hashref, $cache ) = @_;
@@ -149,12 +152,15 @@ sub insert {
   return $error if $error;
 
   my $cust_main = $self->cust_main;
-  return "Unknown customer ". $self->custnum unless $cust_main;
-
-  my $agent = qsearchs( 'agent', { 'agentnum' => $cust_main->agentnum } );
-  my $pkgpart_href = $agent->pkgpart_hashref;
-  return "agent ". $agent->agentnum. " can't purchase pkgpart ". $self->pkgpart
-    unless $pkgpart_href->{ $self->pkgpart };
+  return "Unknown custnum: ". $self->custnum unless $cust_main;
+
+  unless ( $disable_agentcheck ) {
+    my $agent = qsearchs( 'agent', { 'agentnum' => $cust_main->agentnum } );
+    my $pkgpart_href = $agent->pkgpart_hashref;
+    return "agent ". $agent->agentnum.
+           " can't purchase pkgpart ". $self->pkgpart
+      unless $pkgpart_href->{ $self->pkgpart };
+  }
 
   $self->SUPER::insert;
 
@@ -242,25 +248,31 @@ sub check {
   $self->otaker($1);
 
   if ( $self->dbdef_table->column('manual_flag') ) {
-    $self->manual_flag =~ /^([01]?)$/ or return "Illegal manual_flag";
+    $self->manual_flag('') if $self->manual_flag eq ' ';
+    $self->manual_flag =~ /^([01]?)$/
+      or return "Illegal manual_flag ". $self->manual_flag;
     $self->manual_flag($1);
   }
 
   ''; #no error
 }
 
-=item cancel
+=item cancel [ OPTION => VALUE ... ]
 
 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
 in this package, then cancels the package itself (sets the cancel field to
 now).
 
+Available options are: I<quiet>
+
+I<quiet> can be set true to supress email cancellation notices.
+
 If there is an error, returns the error, otherwise returns false.
 
 =cut
 
 sub cancel {
-  my $self = shift;
+  my( $self, %options ) = @_;
   my $error;
 
   local $SIG{HUP} = 'IGNORE';
@@ -301,7 +313,7 @@ sub cancel {
 
   my $conf = new FS::Conf;
 
-  if ( !$quiet && $conf->exists('emailcancel')
+  if ( !$options{'quiet'} && $conf->exists('emailcancel')
        && grep { $_ ne 'POST' } $self->cust_main->invoicing_list) {
   
       my @invoicing_list = $self->cust_main->invoicing_list;
@@ -450,7 +462,10 @@ sub unsuspend {
 
   unless ( ! $self->getfield('susp') ) {
     my %hash = $self->hash;
+    my $inactive = time - $hash{'susp'};
     $hash{'susp'} = '';
+    $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive
+      if $inactive > 0 && ( $hash{'bill'} || $hash{'setup'} );
     my $new = new FS::cust_pkg ( \%hash );
     $error = $new->replace($self);
     if ( $error ) {
@@ -473,6 +488,10 @@ Useful for billing metered services.
 
 sub last_bill {
   my $self = shift;
+  if ( $self->dbdef_table->column('last_bill') ) {
+    return $self->setfield('last_bill', $_[1]) if @_;
+    return $self->getfield('last_bill') if $self->getfield('last_bill');
+  }    
   my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
                                                   'edate'  => $self->bill,  } );
   $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
@@ -619,6 +638,44 @@ sub attribute_since_sqlradacct {
 
 }
 
+=item reexport
+
+This method is deprecated.  See the I<depend_jobnum> option to the insert and
+order_pkgs methods in FS::cust_main for a better way to defer provisioning.
+
+=cut
+
+sub reexport {
+  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;
+
+  foreach my $cust_svc ( $self->cust_svc ) {
+    #false laziness w/svc_Common::insert
+    my $svc_x = $cust_svc->svc_x;
+    foreach my $part_export ( $cust_svc->part_svc->part_export ) {
+      my $error = $part_export->export_insert($svc_x);
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+    }
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  '';
+
+}
+
 =back
 
 =head1 SUBROUTINES
@@ -669,6 +726,12 @@ sub order {
       push @{ $svcnum{$cust_svc->getfield('svcpart')} }, $cust_svc;
     }
   }
+  if ( $DEBUG ) {
+    foreach my $svcpart ( keys %svcnum ) {
+      warn "initial svcpart $svcpart: existing svcnums ".
+           join(', ', map { $_->svcnum } @{$svcnum{$svcpart}} ). "\n";
+    }
+  }
   
   my @cust_svc;
   #generate @cust_svc
@@ -682,13 +745,29 @@ sub order {
     }
     push @cust_svc, [
       map {
-        ( $svcnum{$_} && @{ $svcnum{$_} } ) ? shift @{ $svcnum{$_} } : ();
-      } map { $_->svcpart }
+        my $svcnum = $svcnum{$_->{svcpart}};
+        if ( $svcnum && @$svcnum ) {
+          my $num = ( $_->{quantity} < scalar(@$svcnum) )
+                      ? $_->{quantity}
+                      : scalar(@$svcnum);
+          splice @$svcnum, 0, $num;
+        } else {
+          ();
+        }
+      } map { { 'svcpart'  => $_->svcpart,
+                'quantity' => $_->quantity } }
           qsearch('pkg_svc', { pkgpart  => $pkgpart,
                                quantity => { op=>'>', value=>'0', } } )
     ];
   }
 
+  if ( $DEBUG ) {
+    foreach my $svcpart ( keys %svcnum ) {
+      warn "after regular move svcpart $svcpart: existing svcnums ".
+           join(', ', map { $_->svcnum } @{$svcnum{$svcpart}} ). "\n";
+    }
+  }
+
   #special-case until this can be handled better
   # move services to new svcparts - even if the svcparts don't match (svcdb
   # needs to...)
@@ -725,7 +804,15 @@ sub order {
     }
 
   }
-  
+
+  if ( $DEBUG ) {
+    foreach my $svcpart ( keys %svcnum ) {
+      warn "after special-case move svcpart $svcpart: existing svcnums ".
+           join(', ', map { $_->svcnum } @{$svcnum{$svcpart}} ). "\n";
+    }
+  }
+
+
   #check for leftover services
   foreach (keys %svcnum) {
     next unless @{ $svcnum{$_} };