2 use base qw( FS::cust_main_Mixin FS::option_Common ); #FS::Record );
5 use vars qw( $DEBUG $me $ignore_quantity $conf $ticket_system );
7 #use Scalar::Util qw( blessed );
8 use List::Util qw( max );
10 use FS::Record qw( qsearch qsearchs dbh str2time_sql str2time_sql_closing );
14 use FS::part_svc_link;
15 use FS::domain_record;
19 use FS::export_cust_svc;
21 #most FS::svc_ classes are autoloaded in svc_x emthod
22 use FS::svc_acct; #this one is used in the cache stuff
30 #ask FS::UID to run this stuff for us later
31 FS::UID->install_callback( sub {
33 $ticket_system = $conf->config('ticket_system')
36 our $cache_enabled = 0;
39 my( $self, $hashref ) = @_;
40 if ( $cache_enabled && $hashref->{'svc'} ) {
41 $self->{'_svcpart'} = FS::part_svc->new($hashref);
47 my ( $hashref, $cache ) = @_;
48 if ( $hashref->{'username'} ) {
49 $self->{'_svc_acct'} = FS::svc_acct->new($hashref, '');
51 if ( $hashref->{'svc'} ) {
52 $self->{'_svcpart'} = FS::part_svc->new($hashref);
58 FS::cust_svc - Object method for cust_svc objects
64 $record = new FS::cust_svc \%hash
65 $record = new FS::cust_svc { 'column' => 'value' };
67 $error = $record->insert;
69 $error = $new_record->replace($old_record);
71 $error = $record->delete;
73 $error = $record->check;
75 ($label, $value) = $record->label;
79 An FS::cust_svc represents a service. FS::cust_svc inherits from FS::Record.
80 The following fields are currently supported:
84 =item svcnum - primary key (assigned automatically for new services)
86 =item pkgnum - Package (see L<FS::cust_pkg>)
88 =item svcpart - Service definition (see L<FS::part_svc>)
90 =item agent_svcid - Optional legacy service ID
92 =item overlimit - date the service exceeded its usage limit
102 Creates a new service. To add the refund to the database, see L<"insert">.
103 Services are normally created by creating FS::svc_ objects (see
104 L<FS::svc_acct>, L<FS::svc_domain>, and L<FS::svc_forward>, among others).
108 sub table { 'cust_svc'; }
112 Adds this service to the database. If there is an error, returns the error,
113 otherwise returns false.
120 local $SIG{HUP} = 'IGNORE';
121 local $SIG{INT} = 'IGNORE';
122 local $SIG{QUIT} = 'IGNORE';
123 local $SIG{TERM} = 'IGNORE';
124 local $SIG{TSTP} = 'IGNORE';
125 local $SIG{PIPE} = 'IGNORE';
127 my $oldAutoCommit = $FS::UID::AutoCommit;
128 local $FS::UID::AutoCommit = 0;
131 my $error = $self->SUPER::insert;
133 #check if this releases a hold (see FS::pkg_svc provision_hold)
134 $error ||= $self->_check_provision_hold;
137 $dbh->rollback if $oldAutoCommit;
138 return $error if $error
141 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
148 Deletes this service from the database. If there is an error, returns the
149 error, otherwise returns false. Note that this only removes the cust_svc
150 record - you should probably use the B<cancel> method instead.
159 my $cust_pkg = $self->cust_pkg;
160 my $custnum = $cust_pkg->custnum if $cust_pkg;
162 local $SIG{HUP} = 'IGNORE';
163 local $SIG{INT} = 'IGNORE';
164 local $SIG{QUIT} = 'IGNORE';
165 local $SIG{TERM} = 'IGNORE';
166 local $SIG{TSTP} = 'IGNORE';
167 local $SIG{PIPE} = 'IGNORE';
169 my $oldAutoCommit = $FS::UID::AutoCommit;
170 local $FS::UID::AutoCommit = 0;
173 # delete associated export_cust_svc
174 foreach my $export_cust_svc (
175 qsearch('export_cust_svc',{ 'svcnum' => $self->svcnum })
177 my $error = $export_cust_svc->delete;
179 $dbh->rollback if $oldAutoCommit;
184 my $error = $self->SUPER::delete;
186 $dbh->rollback if $oldAutoCommit;
190 foreach my $part_svc_link ( $self->part_svc_link(
191 link_type => 'cust_svc_unprovision_cascade',
194 foreach my $cust_svc ( qsearch( 'cust_svc', {
195 'pkgnum' => $self->pkgnum,
196 'svcpart' => $part_svc_link->dst_svcpart,
199 my $error = $cust_svc->svc_x->delete;
201 $dbh->rollback if $oldAutoCommit;
208 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
210 if ( $ticket_system eq 'RT_Internal' ) {
211 unless ( $rt_session ) {
212 FS::TicketSystem->init;
213 $rt_session = FS::TicketSystem->session;
215 my $links = RT::Links->new($rt_session->{CurrentUser});
216 my $svcnum = $self->svcnum;
217 $links->Limit(FIELD => 'Target',
218 VALUE => 'freeside://freeside/cust_svc/'.$svcnum);
219 while ( my $l = $links->Next ) {
222 # re-link to point to the customer instead
224 $l->SetTarget('freeside://freeside/cust_main/'.$custnum);
227 ($val, $msg) = $l->Delete;
229 # can't do anything useful on error
230 warn "error unlinking ticket $svcnum: $msg\n" if !$val;
240 Suspends the relevant service by calling the B<suspend> method of the associated
241 FS::svc_XXX object (i.e. an FS::svc_acct object or FS::svc_domain object).
243 If there is an error, returns the error, otherwise returns false.
248 my( $self, %opt ) = @_;
250 $self->part_svc->svcdb =~ /^([\w\-]+)$/ or return 'Illegal part_svc.svcdb';
252 require "FS/$svcdb.pm";
254 my $svc = qsearchs( $svcdb, { 'svcnum' => $self->svcnum } )
257 my $error = $svc->suspend;
258 return $error if $error;
260 if ( $opt{labels_arryref} ) {
261 my( $label, $value ) = $self->label;
262 push @{ $opt{labels_arrayref} }, "$label: $value";
271 Cancels the relevant service by calling the B<cancel> method of the associated
272 FS::svc_XXX object (i.e. an FS::svc_acct object or FS::svc_domain object),
273 deleting the FS::svc_XXX record and then deleting this record.
275 If there is an error, returns the error, otherwise returns false.
282 local $SIG{HUP} = 'IGNORE';
283 local $SIG{INT} = 'IGNORE';
284 local $SIG{QUIT} = 'IGNORE';
285 local $SIG{TERM} = 'IGNORE';
286 local $SIG{TSTP} = 'IGNORE';
287 local $SIG{PIPE} = 'IGNORE';
289 my $oldAutoCommit = $FS::UID::AutoCommit;
290 local $FS::UID::AutoCommit = 0;
293 my $part_svc = $self->part_svc;
295 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
296 $dbh->rollback if $oldAutoCommit;
297 return "Illegal svcdb value in part_svc!";
300 require "FS/$svcdb.pm";
302 my $svc = $self->svc_x;
304 if ( %opt && $opt{'date'} ) {
305 my $error = $svc->expire($opt{'date'});
307 $dbh->rollback if $oldAutoCommit;
308 return "Error expiring service: $error";
311 my $error = $svc->cancel;
313 $dbh->rollback if $oldAutoCommit;
314 return "Error canceling service: $error";
316 $error = $svc->delete; #this deletes this cust_svc record as well
318 $dbh->rollback if $oldAutoCommit;
319 return "Error deleting service: $error";
326 warn "WARNING: no svc_ record found for svcnum ". $self->svcnum.
327 "; deleting cust_svc only\n";
329 my $error = $self->delete;
331 $dbh->rollback if $oldAutoCommit;
332 return "Error deleting cust_svc: $error";
337 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
343 =item overlimit [ ACTION ]
345 Retrieves or sets the overlimit date. If ACTION is absent, return
346 the present value of overlimit. If ACTION is present, it can
347 have the value 'suspend' or 'unsuspend'. In the case of 'suspend' overlimit
348 is set to the current time if it is not already set. The 'unsuspend' value
349 causes the time to be cleared.
351 If there is an error on setting, returns the error, otherwise returns false.
357 my $action = shift or return $self->getfield('overlimit');
359 local $SIG{HUP} = 'IGNORE';
360 local $SIG{INT} = 'IGNORE';
361 local $SIG{QUIT} = 'IGNORE';
362 local $SIG{TERM} = 'IGNORE';
363 local $SIG{TSTP} = 'IGNORE';
364 local $SIG{PIPE} = 'IGNORE';
366 my $oldAutoCommit = $FS::UID::AutoCommit;
367 local $FS::UID::AutoCommit = 0;
370 if ( $action eq 'suspend' ) {
371 $self->setfield('overlimit', time) unless $self->getfield('overlimit');
372 }elsif ( $action eq 'unsuspend' ) {
373 $self->setfield('overlimit', '');
375 die "unexpected action value: $action";
378 local $ignore_quantity = 1;
379 my $error = $self->replace;
381 $dbh->rollback if $oldAutoCommit;
382 return "Error setting overlimit: $error";
385 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
391 =item replace OLD_RECORD
393 Replaces the OLD_RECORD with this one in the database. If there is an error,
394 returns the error, otherwise returns false.
401 # my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
403 # : $new->replace_old;
404 my ( $new, $old ) = ( shift, shift );
405 $old = $new->replace_old unless defined($old);
407 local $SIG{HUP} = 'IGNORE';
408 local $SIG{INT} = 'IGNORE';
409 local $SIG{QUIT} = 'IGNORE';
410 local $SIG{TERM} = 'IGNORE';
411 local $SIG{TSTP} = 'IGNORE';
412 local $SIG{PIPE} = 'IGNORE';
414 my $oldAutoCommit = $FS::UID::AutoCommit;
415 local $FS::UID::AutoCommit = 0;
418 if ( $new->svcpart != $old->svcpart ) {
419 my $svc_x = $new->svc_x;
420 my $new_svc_x = ref($svc_x)->new({$svc_x->hash, svcpart=>$new->svcpart });
421 local($FS::Record::nowarn_identical) = 1;
422 my $error = $new_svc_x->replace($svc_x);
424 $dbh->rollback if $oldAutoCommit;
425 return $error if $error;
429 # #trigger a re-export on pkgnum changes?
430 # # (of prepaid packages), for Expiration RADIUS attribute
431 # if ( $new->pkgnum != $old->pkgnum && $new->cust_pkg->part_pkg->is_prepaid ) {
432 # my $svc_x = $new->svc_x;
433 # local($FS::Record::nowarn_identical) = 1;
434 # my $error = $svc_x->export('replace');
436 # $dbh->rollback if $oldAutoCommit;
437 # return $error if $error;
441 #trigger a pkg_change export on pkgnum changes
442 if ( $new->pkgnum != $old->pkgnum ) {
443 my $error = $new->svc_x->export('pkg_change', $new->cust_pkg,
448 $dbh->rollback if $oldAutoCommit;
449 return $error if $error;
451 } # if pkgnum is changing
453 #my $error = $new->SUPER::replace($old, @_);
454 my $error = $new->SUPER::replace($old);
456 #trigger a relocate export on location changes
457 if ( $new->cust_pkg->locationnum != $old->cust_pkg->locationnum ) {
458 my $svc_x = $new->svc_x;
459 if ( $svc_x->locationnum ) {
460 if ( $svc_x->locationnum == $old->cust_pkg->locationnum ) {
461 # in this case, set the service location to be the same as the new
463 $svc_x->set('locationnum', $new->cust_pkg->locationnum);
464 # and replace it, which triggers a relocate export so we don't
466 $error ||= $svc_x->replace;
468 # the service already has a different location from its package
472 # the service doesn't have a locationnum (either isn't of a type
473 # that has the locationnum field, or the locationnum is null and
474 # defaults to cust_pkg->locationnum)
475 # so just trigger the export here
476 $error ||= $new->svc_x->export('relocate',
477 $new->cust_pkg->cust_location,
478 $old->cust_pkg->cust_location,
480 } # if ($svc_x->locationnum)
481 } # if this is a location change
483 #check if this releases a hold (see FS::pkg_svc provision_hold)
484 $error ||= $new->_check_provision_hold;
487 $dbh->rollback if $oldAutoCommit;
488 return $error if $error
491 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
498 Checks all fields to make sure this is a valid service. If there is an error,
499 returns the error, otherwise returns false. Called by the insert and
508 $self->ut_numbern('svcnum')
509 || $self->ut_numbern('pkgnum')
510 || $self->ut_number('svcpart')
511 || $self->ut_numbern('agent_svcid')
512 || $self->ut_numbern('overlimit')
514 return $error if $error;
516 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
517 return "Unknown svcpart" unless $part_svc;
519 if ( $self->pkgnum && ! $ignore_quantity ) {
521 #slightly inefficient since ->pkg_svc will also look it up, but fixing
522 # a much larger perf problem and have bigger fish to fry
523 my $cust_pkg = $self->cust_pkg;
525 my $pkg_svc = $self->pkg_svc
526 || new FS::pkg_svc { 'svcpart' => $self->svcpart,
527 'pkgpart' => $cust_pkg->pkgpart,
531 #service add-ons, kinda false laziness/reimplementation of part_pkg->pkg_svc
532 foreach my $part_pkg_link ( $cust_pkg->part_pkg->svc_part_pkg_link ) {
533 my $addon_pkg_svc = qsearchs('pkg_svc', {
534 pkgpart => $part_pkg_link->dst_pkgpart,
535 svcpart => $self->svcpart,
537 $pkg_svc->quantity( $pkg_svc->quantity + $addon_pkg_svc->quantity )
541 #better error message? UI shouldn't get here
542 return "No svcpart ". $self->svcpart.
543 " services in pkgpart ". $cust_pkg->pkgpart
544 unless $pkg_svc->quantity > 0;
546 my $num_cust_svc = $cust_pkg->num_cust_svc( $self->svcpart );
548 #false laziness w/cust_pkg->part_svc
549 my $num_avail = max( 0, ($cust_pkg->quantity || 1) * $pkg_svc->quantity
553 #better error message? again, UI shouldn't get here
554 return "Already $num_cust_svc ". $pkg_svc->part_svc->svc.
555 " services for pkgnum ". $self->pkgnum
558 #part_svc_link rules (only make sense in pkgpart context, and
559 # skipping this when ignore_quantity is set DTRT when we're "forcing"
560 # an implicit change here (location change triggered pkgpart change,
561 # ->overlimit, bulk customer service changes)
562 foreach my $part_svc_link ( $self->part_svc_link(
563 link_type => 'cust_svc_provision_restrict',
566 return $part_svc_link->dst_svc. ' must be provisioned before '.
567 $part_svc_link->src_svc
569 'table' => 'cust_svc',
570 'hashref' => { 'pkgnum' => $self->pkgnum,
571 'svcpart' => $part_svc_link->dst_svcpart,
573 'order_by' => 'LIMIT 1',
582 =item check_part_svc_link_unprovision
584 Checks service dependency unprovision rules for this service.
586 If there is an error, returns the error, otherwise returns false.
590 sub check_part_svc_link_unprovision {
593 foreach my $part_svc_link ( $self->part_svc_link(
594 link_type => 'cust_svc_unprovision_restrict',
597 return $part_svc_link->dst_svc. ' must be unprovisioned before '.
598 $part_svc_link->src_svc
600 'table' => 'cust_svc',
601 'hashref' => { 'pkgnum' => $self->pkgnum,
602 'svcpart' => $part_svc_link->dst_svcpart,
604 'order_by' => 'LIMIT 1',
613 Returns the service dependencies (see L<FS::part_svc_link>) for the given
614 search options, taking into account this service definition as source and
615 this customer's agent.
617 Available options are any field in part_svc_link. Typically used options are
624 my $agentnum = $self->pkgnum ? $self->cust_pkg->cust_main->agentnum : '';
625 FS::part_svc_link->by_agentnum($agentnum,
626 src_svcpart=>$self->svcpart,
634 Returns the displayed service number for this service: agent_svcid if it has a
635 value, svcnum otherwise
641 $self->agent_svcid || $self->svcnum;
646 Returns the definition for this service, as a FS::part_svc object (see
653 return $self->{_svcpart} if $self->{_svcpart};
654 cluck 'cust_svc->part_svc called' if $DEBUG;
655 qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
660 Returns the package this service belongs to, as a FS::cust_pkg object (see
665 Returns the pkg_svc record for for this service, if applicable.
671 my $cust_pkg = $self->cust_pkg;
672 return undef unless $cust_pkg;
674 qsearchs( 'pkg_svc', { 'svcpart' => $self->svcpart,
675 'pkgpart' => $cust_pkg->pkgpart,
682 Returns the date this service was inserted.
688 $self->h_date('insert');
691 =item pkg_cancel_date
693 Returns the date this service's package was canceled. This normally only
694 exists for a service that's been preserved through cancellation with the
695 part_pkg.preserve flag.
699 sub pkg_cancel_date {
701 my $cust_pkg = $self->cust_pkg or return;
702 return $cust_pkg->getfield('cancel') || '';
705 =item label [ LOCALE ]
707 Returns a list consisting of:
708 - The name of this service (from part_svc), optionally localized
709 - A meaningful identifier (username, domain, or mail alias)
710 - The table name (i.e. svc_domain) for this service
715 my($label, $value, $svcdb) = $cust_svc->label;
717 =item label_long [ LOCALE ]
719 Like the B<label> method, except the second item in the list ("meaningful
720 identifier") may be longer - typically, a full name is included.
724 sub label { shift->_label('svc_label', @_); }
725 sub label_long { shift->_label('svc_label_long', @_); }
731 my $svc_x = $self->svc_x
732 or return "can't find ". $self->part_svc->svcdb. '.svcnum '. $self->svcnum;
734 $self->$method($svc_x, undef, undef, $locale);
737 # svc_label(_long) takes three arguments: end date, start date, locale
738 # and FS::svc_*::label methods must accept those also, if they even care
740 sub svc_label { shift->_svc_label('label', @_); }
741 sub svc_label_long { shift->_svc_label('label_long', @_); }
744 my( $self, $method, $svc_x ) = ( shift, shift, shift );
745 my ($end, $start, $locale) = @_;
748 $self->part_svc->svc_locale($locale),
750 $self->part_svc->svcdb,
758 Returns a listref of html elements associated with this service's exports.
764 my $svc_x = $self->svc_x
765 or return [ "can't find ". $self->part_svc->svcdb. '.svcnum '. $self->svcnum ];
767 $svc_x->export_links;
770 =item export_getsettings
772 Returns two hashrefs of settings associated with this service's exports.
776 sub export_getsettings {
778 my $svc_x = $self->svc_x
779 or return "can't find ". $self->part_svc->svcdb. '.svcnum '. $self->svcnum;
781 $svc_x->export_getsettings;
787 Returns the FS::svc_XXX object for this service (i.e. an FS::svc_acct object or
788 FS::svc_domain object, etc.)
794 my $svcdb = $self->part_svc->svcdb;
795 if ( $svcdb eq 'svc_acct' && $self->{'_svc_acct'} ) {
796 $self->{'_svc_acct'};
798 require "FS/$svcdb.pm";
799 warn "$me svc_x: part_svc.svcpart ". $self->part_svc->svcpart.
800 ", so searching for $svcdb.svcnum ". $self->svcnum. "\n"
802 qsearchs( $svcdb, { 'svcnum' => $self->svcnum } );
806 =item seconds_since TIMESTAMP
808 See L<FS::svc_acct/seconds_since>. Equivalent to
809 $cust_svc->svc_x->seconds_since, but more efficient. Meaningless for records
810 where B<svcdb> is not "svc_acct".
814 #internal session db deprecated (or at least on hold)
815 sub seconds_since { 'internal session db deprecated'; };
816 ##note: implementation here, POD in FS::svc_acct
818 # my($self, $since) = @_;
820 # my $sth = $dbh->prepare(' SELECT SUM(logout-login) FROM session
823 # AND logout IS NOT NULL'
824 # ) or die $dbh->errstr;
825 # $sth->execute($self->svcnum, $since) or die $sth->errstr;
826 # $sth->fetchrow_arrayref->[0];
829 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
831 Equivalent to $cust_svc->svc_x->seconds_since_sqlradacct, but
832 more efficient. Meaningless for records where B<svcdb> is not
833 svc_acct or svc_broadband.
837 sub seconds_since_sqlradacct {
838 my($self, $start, $end) = @_;
840 my $mes = "$me seconds_since_sqlradacct:";
842 my $svc_x = $self->svc_x;
844 my @part_export = $self->part_svc->part_export_usage;
845 die "no accounting-capable exports are enabled for ". $self->part_svc->svc.
846 " service definition"
851 foreach my $part_export ( @part_export ) {
853 next if $part_export->option('ignore_accounting');
855 warn "$mes connecting to sqlradius database\n"
858 my $dbh = DBI->connect( map { $part_export->option($_) }
859 qw(datasrc username password) )
860 or die "can't connect to sqlradius database: ". $DBI::errstr;
862 warn "$mes connected to sqlradius database\n"
865 #select a unix time conversion function based on database type
866 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
867 my $closing = str2time_sql_closing( $dbh->{Driver}->{Name} );
869 my $username = $part_export->export_username($svc_x);
873 warn "$mes finding closed sessions completely within the given range\n"
878 if ($part_export->option('process_single_realm')) {
879 $realm = 'AND Realm = ?';
880 $realmparam = $part_export->option('realm');
883 my $sth = $dbh->prepare("SELECT SUM(acctsessiontime)
887 AND $str2time AcctStartTime $closing >= ?
888 AND $str2time AcctStopTime $closing < ?
889 AND $str2time AcctStopTime $closing > 0
890 AND AcctStopTime IS NOT NULL"
891 ) or die $dbh->errstr;
892 $sth->execute($username, ($realm ? $realmparam : ()), $start, $end)
894 my $regular = $sth->fetchrow_arrayref->[0];
896 warn "$mes finding open sessions which start in the range\n"
899 # count session start->range end
900 $query = "SELECT SUM( ? - $str2time AcctStartTime $closing )
904 AND $str2time AcctStartTime $closing >= ?
905 AND $str2time AcctStartTime $closing < ?
906 AND ( ? - $str2time AcctStartTime $closing ) < 86400
907 AND ( $str2time AcctStopTime $closing = 0
908 OR AcctStopTime IS NULL )";
909 $sth = $dbh->prepare($query) or die $dbh->errstr;
912 ($realm ? $realmparam : ()),
916 or die $sth->errstr. " executing query $query";
917 my $start_during = $sth->fetchrow_arrayref->[0];
919 warn "$mes finding closed sessions which start before the range but stop during\n"
922 #count range start->session end
923 $sth = $dbh->prepare("SELECT SUM( $str2time AcctStopTime $closing - ? )
927 AND $str2time AcctStartTime $closing < ?
928 AND $str2time AcctStopTime $closing >= ?
929 AND $str2time AcctStopTime $closing < ?
930 AND $str2time AcctStopTime $closing > 0
931 AND AcctStopTime IS NOT NULL"
932 ) or die $dbh->errstr;
933 $sth->execute( $start,
935 ($realm ? $realmparam : ()),
940 my $end_during = $sth->fetchrow_arrayref->[0];
942 warn "$mes finding closed sessions which start before the range but stop after\n"
945 # count range start->range end
946 # don't count open sessions anymore (probably missing stop record)
947 $sth = $dbh->prepare("SELECT COUNT(*)
951 AND $str2time AcctStartTime $closing < ?
952 AND ( $str2time AcctStopTime $closing >= ?
954 # OR AcctStopTime = 0
955 # OR AcctStopTime IS NULL )"
956 ) or die $dbh->errstr;
957 $sth->execute($username, ($realm ? $realmparam : ()), $start, $end )
959 my $entire_range = ($end-$start) * $sth->fetchrow_arrayref->[0];
961 $seconds += $regular + $end_during + $start_during + $entire_range;
963 warn "$mes done finding sessions\n"
972 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
974 See L<FS::svc_acct/attribute_since_sqlradacct>. Equivalent to
975 $cust_svc->svc_x->attribute_since_sqlradacct, but more efficient.
976 Meaningless for records where B<svcdb> is not svc_acct or svc_broadband.
980 #(false laziness w/seconds_since_sqlradacct above)
981 sub attribute_since_sqlradacct {
982 my($self, $start, $end, $attrib) = @_;
984 my $mes = "$me attribute_since_sqlradacct:";
986 my $svc_x = $self->svc_x;
988 my @part_export = $self->part_svc->part_export_usage;
989 die "no accounting-capable exports are enabled for ". $self->part_svc->svc.
990 " service definition"
996 foreach my $part_export ( @part_export ) {
998 next if $part_export->option('ignore_accounting');
1000 warn "$mes connecting to sqlradius database\n"
1003 my $dbh = DBI->connect( map { $part_export->option($_) }
1004 qw(datasrc username password) )
1005 or die "can't connect to sqlradius database: ". $DBI::errstr;
1007 warn "$mes connected to sqlradius database\n"
1010 #select a unix time conversion function based on database type
1011 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
1012 my $closing = str2time_sql_closing( $dbh->{Driver}->{Name} );
1014 my $username = $part_export->export_username($svc_x);
1016 warn "$mes SUMing $attrib sessions\n"
1020 my $realmparam = '';
1021 if ($part_export->option('process_single_realm')) {
1022 $realm = 'AND Realm = ?';
1023 $realmparam = $part_export->option('realm');
1026 my $sth = $dbh->prepare("SELECT SUM($attrib)
1030 AND $str2time AcctStopTime $closing >= ?
1031 AND $str2time AcctStopTime $closing < ?
1032 AND AcctStopTime IS NOT NULL"
1033 ) or die $dbh->errstr;
1034 $sth->execute($username, ($realm ? $realmparam : ()), $start, $end)
1035 or die $sth->errstr;
1037 my $row = $sth->fetchrow_arrayref;
1038 $sum += $row->[0] if defined($row->[0]);
1040 warn "$mes done SUMing sessions\n"
1049 #note: implementation here, POD in FS::svc_acct
1050 # false laziness w/above
1051 sub attribute_last_sqlradacct {
1052 my($self, $attrib) = @_;
1054 my $mes = "$me attribute_last_sqlradacct:";
1056 my $svc_x = $self->svc_x;
1058 my @part_export = $self->part_svc->part_export_usage;
1059 die "no accounting-capable exports are enabled for ". $self->part_svc->svc.
1060 " service definition"
1061 unless @part_export;
1065 my $AcctStartTime = 0;
1067 foreach my $part_export ( @part_export ) {
1069 next if $part_export->option('ignore_accounting');
1071 warn "$mes connecting to sqlradius database\n"
1074 my $dbh = DBI->connect( map { $part_export->option($_) }
1075 qw(datasrc username password) )
1076 or die "can't connect to sqlradius database: ". $DBI::errstr;
1078 warn "$mes connected to sqlradius database\n"
1081 #select a unix time conversion function based on database type
1082 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
1083 my $closing = str2time_sql_closing( $dbh->{Driver}->{Name} );
1085 my $username = $part_export->export_username($svc_x);
1087 warn "$mes finding most-recent $attrib\n"
1091 my $realmparam = '';
1092 if ($part_export->option('process_single_realm')) {
1093 $realm = 'AND Realm = ?';
1094 $realmparam = $part_export->option('realm');
1097 my $sth = $dbh->prepare("SELECT $attrib, $str2time AcctStartTime $closing
1101 ORDER BY AcctStartTime DESC LIMIT 1
1102 ") or die $dbh->errstr;
1103 $sth->execute($username, ($realm ? $realmparam : ()) )
1104 or die $sth->errstr;
1106 my $row = $sth->fetchrow_arrayref;
1107 if ( defined($row->[0]) && $row->[1] > $AcctStartTime ) {
1109 $AcctStartTime = $row->[1];
1121 =item get_session_history TIMESTAMP_START TIMESTAMP_END
1123 See L<FS::svc_acct/get_session_history>. Equivalent to
1124 $cust_svc->svc_x->get_session_history, but more efficient. Meaningless for
1125 records where B<svcdb> is not "svc_acct".
1129 sub get_session_history {
1130 my($self, $start, $end, $attrib) = @_;
1134 my @part_export = $self->part_svc->part_export_usage;
1135 die "no accounting-capable exports are enabled for ". $self->part_svc->svc.
1136 " service definition"
1137 unless @part_export;
1142 foreach my $part_export ( @part_export ) {
1144 @{ $part_export->usage_sessions( $start, $end, $self->svc_x ) };
1151 =item tickets [ STATUS ]
1153 Returns an array of hashes representing the tickets linked to this service.
1155 An optional status (or arrayref or hashref of statuses) may be specified.
1161 my $status = ( @_ && $_[0] ) ? shift : '';
1163 my $conf = FS::Conf->new;
1164 my $num = $conf->config('cust_main-max_tickets') || 10;
1167 if ( $conf->config('ticket_system') ) {
1168 unless ( $conf->config('ticket_system-custom_priority_field') ) {
1170 @tickets = @{ FS::TicketSystem->service_tickets( $self->svcnum,
1179 foreach my $priority (
1180 $conf->config('ticket_system-custom_priority_field-values'), ''
1182 last if scalar(@tickets) >= $num;
1184 @{ FS::TicketSystem->service_tickets( $self->svcnum,
1185 $num - scalar(@tickets),
1198 my $svc_x = $self->svc_x;
1199 +{ ( map { $_=>$self->$_ } $self->fields ),
1200 ( map { $svc_x=>$svc_x->$_ } $svc_x->fields ),
1210 =item smart_search OPTION => VALUE ...
1212 Accepts the option I<search>, the string to search for. The string will
1213 be searched for as a username, email address, IP address, MAC address,
1214 phone number, and hardware serial number. Unlike the I<smart_search> on
1215 customers, this always requires an exact match.
1219 # though perhaps it should be fuzzy in some cases?
1222 my %param = __PACKAGE__->smart_search_param(@_);
1226 sub smart_search_param {
1230 my $string = $opt{'search'};
1231 $string =~ s/(^\s+|\s+$)//; #trim leading & trailing whitespace
1234 map { my $table = $_;
1235 my $search_sql = "FS::$table"->search_sql($string);
1236 my $addl_from = "FS::$table"->search_sql_addl_from();
1238 "SELECT $table.svcnum AS svcnum, '$table' AS svcdb ".
1239 "FROM $table $addl_from WHERE $search_sql";
1241 FS::part_svc->svc_tables;
1243 if ( $string =~ /^(\d+)$/ ) {
1244 unshift @or, "SELECT cust_svc.svcnum, NULL as svcdb FROM cust_svc WHERE agent_svcid = $1";
1247 my $addl_from = " RIGHT JOIN (\n" . join("\nUNION\n", @or) . "\n) AS svc_all ".
1248 " ON (svc_all.svcnum = cust_svc.svcnum) ";
1252 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
1253 'null_right' => 'View/link unlinked services'
1255 my $extra_sql = ' WHERE '.join(' AND ', @extra_sql);
1257 $addl_from .= ' LEFT JOIN cust_pkg USING ( pkgnum )'.
1258 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg').
1259 ' LEFT JOIN part_svc USING ( svcpart )';
1262 'table' => 'cust_svc',
1263 'select' => 'svc_all.svcnum AS svcnum, '.
1264 'COALESCE(svc_all.svcdb, part_svc.svcdb) AS svcdb, '.
1266 'addl_from' => $addl_from,
1268 'extra_sql' => $extra_sql,
1272 # If the associated cust_pkg is 'on hold'
1273 # and the associated pkg_svc has the provision_hold flag
1274 # and there are no more available_part_svcs on the cust_pkg similarly flagged,
1275 # then removes hold from pkg
1276 # returns $error or '' on success,
1277 # does not indicate if pkg status was changed
1278 sub _check_provision_hold {
1281 # check status of cust_pkg
1282 my $cust_pkg = $self->cust_pkg;
1283 return '' unless $cust_pkg && $cust_pkg->status eq 'on hold';
1285 # check flag on this svc
1286 # small false laziness with $self->pkg_svc
1287 # to avoid looking up cust_pkg twice
1288 my $pkg_svc = qsearchs( 'pkg_svc', {
1289 'svcpart' => $self->svcpart,
1290 'pkgpart' => $cust_pkg->pkgpart,
1292 return '' unless $pkg_svc->provision_hold;
1294 # check for any others available with that flag
1295 return '' if $cust_pkg->available_part_svc( 'provision_hold' => 1 );
1297 # conditions met, remove hold
1298 return $cust_pkg->unsuspend;
1304 # fix missing (deleted by mistake) svc_x records
1305 warn "searching for missing svc_x records...\n";
1307 'table' => 'cust_svc',
1308 'select' => 'cust_svc.*',
1309 'addl_from' => ' LEFT JOIN ( ' .
1311 map { "SELECT svcnum FROM $_" }
1312 FS::part_svc->svc_tables
1313 ) . ' ) AS svc_all ON cust_svc.svcnum = svc_all.svcnum',
1314 'extra_sql' => ' WHERE svc_all.svcnum IS NULL',
1316 my @svcs = qsearch(\%search);
1317 warn "found ".scalar(@svcs)."\n";
1319 local $FS::Record::nowarn_classload = 1; # for h_svc_
1320 local $FS::svc_Common::noexport_hack = 1; # because we're inserting services
1323 'hashref' => { history_action => 'delete' },
1324 'order_by' => ' ORDER BY history_date DESC LIMIT 1',
1326 foreach my $cust_svc (@svcs) {
1327 my $svcnum = $cust_svc->svcnum;
1328 my $svcdb = $cust_svc->part_svc->svcdb;
1329 $h_search{'hashref'}{'svcnum'} = $svcnum;
1330 $h_search{'table'} = "h_$svcdb";
1331 my $h_svc_x = qsearchs(\%h_search);
1333 my $class = "FS::$svcdb";
1334 my $new_svc_x = $class->new({ $h_svc_x->hash });
1335 my $error = $new_svc_x->insert;
1336 warn "error repairing svcnum $svcnum ($svcdb) from history:\n$error\n"
1339 # can't be fixed, so remove the dangling cust_svc to avoid breaking
1341 my $error = $cust_svc->delete;
1342 warn "error cleaning up missing svcnum $svcnum ($svcdb):\n$error\n";
1353 Behaviour of changing the svcpart of cust_svc records is undefined and should
1354 possibly be prohibited, and pkg_svc records are not checked.
1356 pkg_svc records are not checked in general (here).
1358 Deleting this record doesn't check or delete the svc_* record associated
1361 In seconds_since_sqlradacct, specifying a DATASRC/USERNAME/PASSWORD instead of
1362 a DBI database handle is not yet implemented.
1366 L<FS::Record>, L<FS::cust_pkg>, L<FS::part_svc>, L<FS::pkg_svc>,
1367 schema.html from the base documentation