X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_svc.pm;h=8a8dbe7c933735e9e7dbfe71a002aca360c06900;hb=bb88a75467993505e2e3d37e8ce313f254ca5325;hp=77151b80e9eec8c2461635677be547a04a28001f;hpb=9b81123e5ff5825d2c3c000432e4dd18b5fd764f;p=freeside.git diff --git a/FS/FS/cust_svc.pm b/FS/FS/cust_svc.pm index 77151b80e..8a8dbe7c9 100644 --- a/FS/FS/cust_svc.pm +++ b/FS/FS/cust_svc.pm @@ -3,17 +3,30 @@ package FS::cust_svc; use strict; use vars qw( @ISA ); use Carp qw( cluck ); -use FS::Record qw( qsearchs ); +use FS::Record qw( qsearch qsearchs dbh ); use FS::cust_pkg; use FS::part_pkg; use FS::part_svc; +use FS::pkg_svc; use FS::svc_acct; use FS::svc_acct_sm; use FS::svc_domain; use FS::svc_forward; +use FS::domain_record; @ISA = qw( FS::Record ); +sub _cache { + my $self = shift; + my ( $hashref, $cache ) = @_; + if ( $hashref->{'username'} ) { + $self->{'_svc_acct'} = FS::svc_acct->new($hashref, ''); + } + if ( $hashref->{'svc'} ) { + $self->{'_svcpart'} = FS::part_svc->new($hashref); + } +} + =head1 NAME FS::cust_svc - Object method for cust_svc objects @@ -58,7 +71,7 @@ The following fields are currently supported: Creates a new service. To add the refund to the database, see L<"insert">. Services are normally created by creating FS::svc_ objects (see -L, L, and L, among others). +L, L, and L, among others). =cut @@ -99,16 +112,54 @@ sub check { ; return $error if $error; - return "Unknown pkgnum" - unless ! $self->pkgnum - || qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } ); - - return "Unknown svcpart" unless - qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } ); + my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } ); + return "Unknown svcpart" unless $part_svc; + + if ( $self->pkgnum ) { + my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } ); + return "Unknown pkgnum" unless $cust_pkg; + my $pkg_svc = qsearchs( 'pkg_svc', { + 'pkgpart' => $cust_pkg->pkgpart, + 'svcpart' => $self->svcpart, + }); + my @cust_svc = qsearch('cust_svc', { + 'pkgnum' => $self->pkgnum, + 'svcpart' => $self->svcpart, + }); + return "Already ". scalar(@cust_svc). " ". $part_svc->svc. + " services for pkgnum ". $self->pkgnum + if $pkg_svc->quantity >= scalar(@cust_svc); + } ''; #no error } +=item part_svc + +Returns the definition for this service, as a FS::part_svc object (see +L). + +=cut + +sub part_svc { + my $self = shift; + $self->{'_svcpart'} + ? $self->{'_svcpart'} + : qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } ); +} + +=item cust_pkg + +Returns the definition for this service, as a FS::part_svc object (see +L). + +=cut + +sub cust_pkg { + my $self = shift; + qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } ); +} + =item label Returns a list consisting of: @@ -120,11 +171,9 @@ Returns a list consisting of: sub label { my $self = shift; - my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } ); - my $svcdb = $part_svc->svcdb; - my $svc_x = qsearchs( $svcdb, { 'svcnum' => $self->svcnum } ) + my $svcdb = $self->part_svc->svcdb; + my $svc_x = $self->svc_x or die "can't find $svcdb.svcnum ". $self->svcnum; - my $svc = $part_svc->svc; my $tag; if ( $svcdb eq 'svc_acct' ) { $tag = $svc_x->email; @@ -144,18 +193,59 @@ sub label { } } elsif ( $svcdb eq 'svc_domain' ) { $tag = $svc_x->getfield('domain'); + } elsif ( $svcdb eq 'svc_www' ) { + my $domain = qsearchs( 'domain_record', { 'recnum' => $svc_x->recnum } ); + $tag = $domain->reczone; } else { cluck "warning: asked for label of unsupported svcdb; using svcnum"; $tag = $svc_x->getfield('svcnum'); } - $svc, $tag, $svcdb; + $self->part_svc->svc, $tag, $svcdb; +} + +=item svc_x + +Returns the FS::svc_XXX object for this service (i.e. an FS::svc_acct object or +FS::svc_domain object, etc.) + +=cut + +sub svc_x { + my $self = shift; + my $svcdb = $self->part_svc->svcdb; + if ( $svcdb eq 'svc_acct' && $self->{'_svc_acct'} ) { + $self->{'_svc_acct'}; + } else { + qsearchs( $svcdb, { 'svcnum' => $self->svcnum } ); + } +} + +=item seconds_since TIMESTAMP + +See L. Equivalent to +$cust_svc->svc_x->seconds_since, but more efficient. Meaningless for records +where B is not "svc_acct". + +=cut + +#note: implementation here, POD in FS::svc_acct +sub seconds_since { + my($self, $since) = @_; + my $dbh = dbh; + my $sth = $dbh->prepare(' SELECT SUM(logout-login) FROM session + WHERE svcnum = ? + AND login >= ? + AND logout IS NOT NULL' + ) or die $dbh->errstr; + $sth->execute($self->svcnum, $since) or die $sth->errstr; + $sth->fetchrow_arrayref->[0]; } =back =head1 VERSION -$Id: cust_svc.pm,v 1.4 2001-09-02 04:51:11 ivan Exp $ +$Id: cust_svc.pm,v 1.11 2002-02-10 21:37:24 ivan Exp $ =head1 BUGS