remove svc_acct_sm
[freeside.git] / FS / FS / cust_pkg.pm
index f858a99..0c71435 100644 (file)
@@ -14,7 +14,6 @@ use FS::pkg_svc;
 # setup }
 # because they load configuraion by setting FS::UID::callback (see TODO)
 use FS::svc_acct;
-use FS::svc_acct_sm;
 use FS::svc_domain;
 use FS::svc_www;
 use FS::svc_forward;
@@ -139,7 +138,13 @@ sub insert {
   my $error = $self->ut_number('custnum');
   return $error if $error;
 
-  return "Unknown customer ". $self->custnum unless $self->cust_main;
+  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 };
 
   $self->SUPER::insert;
 
@@ -219,11 +224,11 @@ sub check {
     return "Unknown customer ". $self->custnum unless $self->cust_main;
   }
 
-  return "Unknown pkgpart"
+  return "Unknown pkgpart: ". $self->pkgpart
     unless qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
 
   $self->otaker(getotaker) unless $self->otaker;
-  $self->otaker =~ /^(\w{0,16})$/ or return "Illegal otaker";
+  $self->otaker =~ /^([\w\.\-]{0,16})$/ or return "Illegal otaker";
   $self->otaker($1);
 
   if ( $self->dbdef_table->column('manual_flag') ) {
@@ -262,33 +267,11 @@ sub cancel {
   foreach my $cust_svc (
     qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
   ) {
-    my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
-
-    $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
-      $dbh->rollback if $oldAutoCommit;
-      return "Illegal svcdb value in part_svc!";
-    };
-    my $svcdb = $1;
-    require "FS/$svcdb.pm";
-
-    my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
-    if ($svc) {
-      $error = $svc->cancel;
-      if ( $error ) {
-        $dbh->rollback if $oldAutoCommit;
-        return "Error cancelling service: $error" 
-      }
-      $error = $svc->delete;
-      if ( $error ) {
-        $dbh->rollback if $oldAutoCommit;
-        return "Error deleting service: $error";
-      }
-    }
+    my $error = $cust_svc->cancel;
 
-    $error = $cust_svc->delete;
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
-      return "Error deleting cust_svc: $error";
+      return "Error cancelling cust_svc: $error";
     }
 
   }
@@ -577,7 +560,9 @@ sub order {
     push @cust_svc, [
       map {
         ( $svcnum{$_} && @{ $svcnum{$_} } ) ? shift @{ $svcnum{$_} } : ();
-      } map { $_->svcpart } qsearch('pkg_svc', { 'pkgpart' => $pkgpart })
+      } map { $_->svcpart }
+          qsearch('pkg_svc', { pkgpart  => $pkgpart,
+                               quantity => { op=>'>', value=>'0', } } )
     ];
   }
 
@@ -597,7 +582,11 @@ sub order {
     #find an empty place to put one
     my $i = 0;
     foreach my $pkgpart ( @{$pkgparts} ) {
-      my @pkg_svc = qsearch('pkg_svc', { pkgpart=>$pkgpart } );
+      my @pkg_svc =
+        qsearch('pkg_svc', { pkgpart  => $pkgpart,
+                             quantity => { op=>'>', value=>'0', } } );
+      #my @pkg_svc =
+      #  grep { $_->quantity > 0 } qsearch('pkg_svc', { pkgpart=>$pkgpart } );
       if ( ! @{$cust_svc[$i]} #find an empty place to put them with 
            && grep { $svcdb eq $_->part_svc->svcdb } #with appropriate svcdb
                 @pkg_svc
@@ -689,7 +678,7 @@ sub order {
 
 =head1 VERSION
 
-$Id: cust_pkg.pm,v 1.17 2002-04-12 15:14:58 ivan Exp $
+$Id: cust_pkg.pm,v 1.24 2002-09-17 09:19:06 ivan Exp $
 
 =head1 BUGS
 
@@ -700,11 +689,12 @@ In sub order, the @pkgparts array (passed by reference) is clobbered.
 Also in sub order, no money is adjusted.  Once FS::part_pkg defines a standard
 method to pass dates to the recur_prog expression, it should do so.
 
-FS::svc_acct, FS::svc_acct_sm, and FS::svc_domain are loaded via 'use' at 
-compile time, rather than via 'require' in sub { setup, suspend, unsuspend,
-cancel } because they use %FS::UID::callback to load configuration values.
-Probably need a subroutine which decides what to do based on whether or not
-we've fetched the user yet, rather than a hash.  See FS::UID and the TODO.
+FS::svc_acct, FS::svc_domain, FS::svc_www and FS::svc_forward are loaded via
+'use' at compile time, rather than via 'require' in sub
+{ setup, suspend, unsuspend, cancel } because they use %FS::UID::callback to
+load configuration values.  Probably need a subroutine which decides what to
+do based on whether or not we've fetched the user yet, rather than a hash.
+See FS::UID and the TODO.
 
 Now that things are transactional should the check in the insert method be
 moved to check ?