6 use FS::Record qw( qsearch qsearchs dbh );
15 use FS::domain_record;
18 @ISA = qw( FS::Record );
22 my ( $hashref, $cache ) = @_;
23 if ( $hashref->{'username'} ) {
24 $self->{'_svc_acct'} = FS::svc_acct->new($hashref, '');
26 if ( $hashref->{'svc'} ) {
27 $self->{'_svcpart'} = FS::part_svc->new($hashref);
33 FS::cust_svc - Object method for cust_svc objects
39 $record = new FS::cust_svc \%hash
40 $record = new FS::cust_svc { 'column' => 'value' };
42 $error = $record->insert;
44 $error = $new_record->replace($old_record);
46 $error = $record->delete;
48 $error = $record->check;
50 ($label, $value) = $record->label;
54 An FS::cust_svc represents a service. FS::cust_svc inherits from FS::Record.
55 The following fields are currently supported:
59 =item svcnum - primary key (assigned automatically for new services)
61 =item pkgnum - Package (see L<FS::cust_pkg>)
63 =item svcpart - Service definition (see L<FS::part_svc>)
73 Creates a new service. To add the refund to the database, see L<"insert">.
74 Services are normally created by creating FS::svc_ objects (see
75 L<FS::svc_acct>, L<FS::svc_domain>, and L<FS::svc_forward>, among others).
79 sub table { 'cust_svc'; }
83 Adds this service to the database. If there is an error, returns the error,
84 otherwise returns false.
88 Deletes this service from the database. If there is an error, returns the
89 error, otherwise returns false. Note that this only removes the cust_svc
90 record - you should probably use the B<cancel> method instead.
94 Cancels the relevant service by calling the B<cancel> method of the associated
95 FS::svc_XXX object (i.e. an FS::svc_acct object or FS::svc_domain object),
96 deleting the FS::svc_XXX record and then deleting this record.
98 If there is an error, returns the error, otherwise returns false.
105 local $SIG{HUP} = 'IGNORE';
106 local $SIG{INT} = 'IGNORE';
107 local $SIG{QUIT} = 'IGNORE';
108 local $SIG{TERM} = 'IGNORE';
109 local $SIG{TSTP} = 'IGNORE';
110 local $SIG{PIPE} = 'IGNORE';
112 my $oldAutoCommit = $FS::UID::AutoCommit;
113 local $FS::UID::AutoCommit = 0;
116 my $part_svc = $self->part_svc;
118 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
119 $dbh->rollback if $oldAutoCommit;
120 return "Illegal svcdb value in part_svc!";
123 require "FS/$svcdb.pm";
125 my $svc = $self->svc_x;
127 my $error = $svc->cancel;
129 $dbh->rollback if $oldAutoCommit;
130 return "Error canceling service: $error";
132 $error = $svc->delete;
134 $dbh->rollback if $oldAutoCommit;
135 return "Error deleting service: $error";
139 my $error = $self->delete;
141 $dbh->rollback if $oldAutoCommit;
142 return "Error deleting cust_svc: $error";
145 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
151 =item replace OLD_RECORD
153 Replaces the OLD_RECORD with this one in the database. If there is an error,
154 returns the error, otherwise returns false.
159 my ( $new, $old ) = ( shift, shift );
161 local $SIG{HUP} = 'IGNORE';
162 local $SIG{INT} = 'IGNORE';
163 local $SIG{QUIT} = 'IGNORE';
164 local $SIG{TERM} = 'IGNORE';
165 local $SIG{TSTP} = 'IGNORE';
166 local $SIG{PIPE} = 'IGNORE';
168 my $oldAutoCommit = $FS::UID::AutoCommit;
169 local $FS::UID::AutoCommit = 0;
172 my $error = $new->SUPER::replace($old);
174 $dbh->rollback if $oldAutoCommit;
175 return $error if $error;
178 if ( $new->svcpart != $old->svcpart ) {
179 my $svc_x = $new->svc_x;
180 my $new_svc_x = ref($svc_x)->new({$svc_x->hash});
181 my $error = $new_svc_x->replace($svc_x);
183 $dbh->rollback if $oldAutoCommit;
184 return $error if $error;
188 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
195 Checks all fields to make sure this is a valid service. If there is an error,
196 returns the error, otehrwise returns false. Called by the insert and
205 $self->ut_numbern('svcnum')
206 || $self->ut_numbern('pkgnum')
207 || $self->ut_number('svcpart')
209 return $error if $error;
211 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
212 return "Unknown svcpart" unless $part_svc;
214 if ( $self->pkgnum ) {
215 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
216 return "Unknown pkgnum" unless $cust_pkg;
217 my $pkg_svc = qsearchs( 'pkg_svc', {
218 'pkgpart' => $cust_pkg->pkgpart,
219 'svcpart' => $self->svcpart,
221 # or new FS::pkg_svc ( { 'pkgpart' => $cust_pkg->pkgpart,
222 # 'svcpart' => $self->svcpart,
223 # 'quantity' => 0 } );
225 my @cust_svc = qsearch('cust_svc', {
226 'pkgnum' => $self->pkgnum,
227 'svcpart' => $self->svcpart,
229 return "Already ". scalar(@cust_svc). " ". $part_svc->svc.
230 " services for pkgnum ". $self->pkgnum
231 if scalar(@cust_svc) >= $pkg_svc->quantity;
239 Returns the definition for this service, as a FS::part_svc object (see
247 ? $self->{'_svcpart'}
248 : qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
253 Returns the definition for this service, as a FS::part_svc object (see
260 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
265 Returns a list consisting of:
266 - The name of this service (from part_svc)
267 - A meaningful identifier (username, domain, or mail alias)
268 - The table name (i.e. svc_domain) for this service
274 my $svcdb = $self->part_svc->svcdb;
275 my $svc_x = $self->svc_x
276 or die "can't find $svcdb.svcnum ". $self->svcnum;
278 if ( $svcdb eq 'svc_acct' ) {
279 $tag = $svc_x->email;
280 } elsif ( $svcdb eq 'svc_acct_sm' ) {
281 my $domuser = $svc_x->domuser eq '*' ? '(anything)' : $svc_x->domuser;
282 my $svc_domain = qsearchs ( 'svc_domain', { 'svcnum' => $svc_x->domsvc } );
283 my $domain = $svc_domain->domain;
284 $tag = "$domuser\@$domain";
285 } elsif ( $svcdb eq 'svc_forward' ) {
286 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $svc_x->srcsvc } );
287 $tag = $svc_acct->email. '->';
288 if ( $svc_x->dstsvc ) {
289 $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $svc_x->dstsvc } );
290 $tag .= $svc_acct->email;
294 } elsif ( $svcdb eq 'svc_domain' ) {
295 $tag = $svc_x->getfield('domain');
296 } elsif ( $svcdb eq 'svc_www' ) {
297 my $domain = qsearchs( 'domain_record', { 'recnum' => $svc_x->recnum } );
298 $tag = $domain->reczone;
300 cluck "warning: asked for label of unsupported svcdb; using svcnum";
301 $tag = $svc_x->getfield('svcnum');
303 $self->part_svc->svc, $tag, $svcdb;
308 Returns the FS::svc_XXX object for this service (i.e. an FS::svc_acct object or
309 FS::svc_domain object, etc.)
315 my $svcdb = $self->part_svc->svcdb;
316 if ( $svcdb eq 'svc_acct' && $self->{'_svc_acct'} ) {
317 $self->{'_svc_acct'};
319 qsearchs( $svcdb, { 'svcnum' => $self->svcnum } );
323 =item seconds_since TIMESTAMP
325 See L<FS::svc_acct/seconds_since>. Equivalent to
326 $cust_svc->svc_x->seconds_since, but more efficient. Meaningless for records
327 where B<svcdb> is not "svc_acct".
331 #note: implementation here, POD in FS::svc_acct
333 my($self, $since) = @_;
335 my $sth = $dbh->prepare(' SELECT SUM(logout-login) FROM session
338 AND logout IS NOT NULL'
339 ) or die $dbh->errstr;
340 $sth->execute($self->svcnum, $since) or die $sth->errstr;
341 $sth->fetchrow_arrayref->[0];
344 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
346 See L<FS::svc_acct/seconds_since_sqlradacct>. Equivalent to
347 $cust_svc->svc_x->seconds_since, but more efficient. Meaningless for records
348 where B<svcdb> is not "svc_acct".
352 #note: implementation here, POD in FS::svc_acct
353 sub seconds_since_sqlradacct {
354 my($self, $start, $end) = @_;
356 my $username = $self->svc_x->username;
358 my @part_export = $self->part_svc->part_export('sqlradius')
359 or die "no sqlradius export configured for this service type";
363 foreach my $part_export ( @part_export ) {
365 my $dbh = DBI->connect( map { $part_export->option($_) }
366 qw(datasrc username password) )
367 or die "can't connect to sqlradius database: ". $DBI::errstr;
369 #select a unix time conversion function based on database type
371 if ( $dbh->{Driver}->{Name} eq 'mysql' ) {
372 $str2time = 'UNIX_TIMESTAMP(';
373 } elsif ( $dbh->{Driver}->{Name} eq 'Pg' ) {
374 $str2time = 'EXTRACT( EPOCH FROM ';
376 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
377 "; guessing how to convert to UNIX timestamps";
378 $str2time = 'extract(epoch from ';
381 #find closed sessions completely within the given range
382 my $sth = $dbh->prepare("SELECT SUM(acctsessiontime)
385 AND $str2time AcctStartTime) >= ?
386 AND $str2time AcctStopTime ) < ?
387 AND $str2time AcctStopTime ) =! 0
388 AND AcctStopTime IS NOT NULL"
389 ) or die $dbh->errstr;
390 $sth->execute($username, $start, $end) or die $sth->errstr;
391 my $regular = $sth->fetchrow_arrayref->[0];
393 #find open sessions which start in the range, count session start->range end
394 $sth = $dbh->prepare("SELECT SUM( ? - $str2time AcctStartTime ) )
397 AND $str2time AcctStartTime ) >= ?
398 AND ( ? - $str2time AcctStartTime ) < 86400
399 AND ( $str2time AcctStopTime ) = 0
400 OR AcctStopTime IS NULL )"
401 ) or die $dbh->errstr;
402 $sth->execute($end, $username, $start, $end) or die $sth->errstr;
403 my $start_during = $sth->fetchrow_arrayref->[0];
405 #find closed sessions which start before the range but stop during,
406 #count range start->session end
407 $sth = $dbh->prepare("SELECT SUM( $str2time AcctStopTime ) - ? )
410 AND $str2time AcctStartTime ) < ?
411 AND $str2time AcctStopTime ) >= ?
412 AND $str2time AcctStopTime ) < ?
413 AND $str2time AcctStopTime ) != 0
414 AND AcctStopTime IS NOT NULL"
415 ) or die $dbh->errstr;
416 $sth->execute($start, $username, $start, $start, $end ) or die $sth->errstr;
417 my $end_during = $sth->fetchrow_arrayref->[0];
419 #find closed (not anymore - or open) sessions which start before the range
420 # but stop after, or are still open, count range start->range end
421 # don't count open sessions (probably missing stop record)
422 $sth = $dbh->prepare("SELECT COUNT(*)
425 AND $str2time AcctStartTime ) < ?
426 AND ( $str2time AcctStopTime ) >= ?
428 # OR AcctStopTime = 0
429 # OR AcctStopTime IS NULL )"
430 ) or die $dbh->errstr;
431 $sth->execute($username, $start, $end ) or die $sth->errstr;
432 my $entire_range = ($end-$start) * $sth->fetchrow_arrayref->[0];
434 $seconds += $regular + $end_during + $start_during + $entire_range;
445 Behaviour of changing the svcpart of cust_svc records is undefined and should
446 possibly be prohibited, and pkg_svc records are not checked.
448 pkg_svc records are not checked in general (here).
450 Deleting this record doesn't check or delete the svc_* record associated
453 In seconds_since_sqlradacct, specifying a DATASRC/USERNAME/PASSWORD instead of
454 a DBI database handle is not yet implemented.
458 L<FS::Record>, L<FS::cust_pkg>, L<FS::part_svc>, L<FS::pkg_svc>,
459 schema.html from the base documentation