radacct update: use sqlradius for datasrc, not plandata options (whew)
[freeside.git] / FS / FS / cust_pkg.pm
index b16d081..55ee37d 100644 (file)
@@ -9,6 +9,7 @@ use FS::part_pkg;
 use FS::cust_main;
 use FS::type_pkgs;
 use FS::pkg_svc;
+use FS::cust_bill_pkg;
 
 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
 # setup }
@@ -90,7 +91,7 @@ inherits from FS::Record.  The following fields are currently supported:
 
 =item setup - date
 
-=item bill - date
+=item bill - date (next bill date)
 
 =item susp - date
 
@@ -418,6 +419,20 @@ sub unsuspend {
   ''; #no errors
 }
 
+=item last_bill
+
+Returns the last bill date, or if there is no last bill date, the setup date.
+Useful for billing metered services.
+
+=cut
+
+sub last_bill {
+  my $self = shift;
+  my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
+                                                  'edate'  => $self->bill,  } );
+  $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
+}
+
 =item part_pkg
 
 Returns the definition for this billing item, as an FS::part_pkg object (see
@@ -496,17 +511,11 @@ sub seconds_since {
 
 }
 
-=item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END DATASRC DB_USERNAME DB_PASSWORD
+=item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
 
 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.
+(exclusive).
 
 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
@@ -516,17 +525,14 @@ 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($self, $start, $end) = @_;
 
   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 += $cust_svc->seconds_since_sqlradacct($start, $end);
   }
 
   $seconds;
@@ -715,7 +721,7 @@ sub order {
 
 =head1 VERSION
 
-$Id: cust_pkg.pm,v 1.25 2002-10-12 13:26:45 ivan Exp $
+$Id: cust_pkg.pm,v 1.27 2002-10-17 14:16:17 ivan Exp $
 
 =head1 BUGS