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;
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') ) {
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";
}
}
=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.
}
+=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
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', } } )
];
}
#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
=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