This commit was manufactured by cvs2svn to create tag 'freeside_1_4_1beta4'.
[freeside.git] / FS / FS / cust_pkg.pm
index f858a99..0978ac1 100644 (file)
@@ -139,7 +139,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 +225,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 +268,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";
     }
 
   }
@@ -492,7 +476,7 @@ sub cust_main {
 =item seconds_since TIMESTAMP
 
 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
-package have been online since TIMESTAMP.
+package have been online since TIMESTAMP, according to the session monitor.
 
 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
 L<Time::Local> and L<Date::Parse> for conversion functions.
@@ -513,6 +497,43 @@ sub seconds_since {
 
 }
 
+=item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END DATASRC DB_USERNAME DB_PASSWORD
+
+Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
+package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
+(exclusive), according to an external SQL radacct table, such as those
+generated by ICRADIUS or FreeRADIUS.  Sessions which started in the specified
+range but are still open are counted from session start to the end of the
+range.  Also, sessions which end in the range but started earlier are counted
+from the start of the range to session end.  Finally, sessions which start
+before the range but end after (or are still open) are counted for the entire
+range.
+
+TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
+L<perlfunc/"time">.  Also see L<Time::Local> and L<Date::Parse> for conversion
+functions.
+
+
+=cut
+
+sub seconds_since_sqlradacct {
+  my($self, $start, $end, $datasrc, $db_user, $db_pass) = @_;
+
+  my $dbh = DBI->connect($datasrc, $db_user, $db_pass)
+    or die "can't connect to $datasrc: ". $DBI::errstr;
+
+  my $seconds = 0;
+
+  foreach my $cust_svc (
+    grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
+  ) {
+    $seconds += $cust_svc->seconds_since_sqlradacct($start, $end, $dbh);
+  }
+
+  $seconds;
+
+}
+
 =back
 
 =head1 SUBROUTINES
@@ -577,7 +598,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 +620,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 +716,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.23.4.1 2002-10-12 13:26:43 ivan Exp $
 
 =head1 BUGS