+sub _customer_svc_x {
+ my($custnum, $svcnum, $table) = (shift, shift, shift);
+ my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
+
+ $custnum =~ /^(\d+)$/ or die "illegal custnum";
+ my $search = " AND custnum = $1";
+ #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
+
+ qsearchs( {
+ 'table' => ($table || 'svc_acct'),
+ 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
+ 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
+ #'LEFT JOIN cust_main USING ( custnum ) ',
+ 'hashref' => $hashref,
+ 'extra_sql' => $search, #important
+ } );
+
+}
+
+sub svc_status_html {
+ my $p = shift;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ #XXX only svc_dsl for now
+ my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
+ or return { 'error' => "Service not found" };
+
+ my $html = $svc_x->getstatus_html;
+
+ return { 'html' => $html };
+
+}
+
+sub svc_status_hash {
+ my $p = shift;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ #XXX only svc_acct for now
+ my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
+ or return { 'error' => "Service not found" };
+
+ my ( $html, $hashref ) = $svc_x->export_getstatus;
+ return $hashref;
+
+}
+
+sub set_svc_status_hash {
+ my $p = shift;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ #XXX only svc_acct for now
+ my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
+ or return { 'error' => "Service not found" };
+
+ warn "set_svc_status_hash ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
+ if $DEBUG;
+ my $error = $svc_x->export_setstatus($p); #$p? returns error?
+ return { 'error' => $error } if $error;
+
+ return {}; #? { 'error' => '' }
+
+}
+
+
+sub acct_forward_info {
+ my $p = shift;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ my $svc_forward = _customer_svc_x( $custnum,
+ { 'srcsvc' => $p->{'svcnum'} },
+ 'svc_forward',
+ )
+ or return { 'error' => '',
+ 'dst' => '',
+ };
+
+ return { 'error' => '',
+ 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
+ };
+
+}
+
+sub process_acct_forward {
+ my $p = shift;
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ my $old = _customer_svc_x( $custnum,
+ { 'srcsvc' => $p->{'svcnum'} },
+ 'svc_forward',
+ );
+
+ if ( $p->{'dst'} eq '' ) {
+ if ( $old ) {
+ my $error = $old->delete;
+ return { 'error' => $error };
+ }
+ return { 'error' => '' };
+ }
+
+ my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
+ 'dst' => $p->{'dst'},
+ };
+
+ my $error;
+ if ( $old ) {
+ $new->svcnum($old->svcnum);
+ my $cust_svc = $old->cust_svc;
+ $new->svcpart($old->svcpart);
+ $new->pkgnuym($old->pkgnum);
+ $error = $new->replace($old);
+ } else {
+ my $conf = new FS::Conf;
+ $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
+
+ my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
+ or return { 'error' => 'No service' }; #how would we even get here?
+
+ $new->pkgnum( $svc_acct->cust_svc->pkgnum );
+
+ $error = $new->insert;
+ }
+
+ return { 'error' => $error };
+
+}
+
+sub list_dsl_devices {
+ my $p = shift;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
+ or return { 'error' => "Service not found" };
+
+ return {
+ 'devices' => [ map {
+ +{ 'mac_addr' => $_->mac_addr };
+ } $svc_dsl->dsl_device
+ ],
+ };
+
+}
+
+sub add_dsl_device {
+ my $p = shift;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
+ or return { 'error' => "Service not found" };
+
+ return { 'error' => 'No MAC address supplied' }
+ unless length($p->{'mac_addr'});
+
+ my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
+ 'mac_addr' => scalar($p->{'mac_addr'}),
+ };
+ my $error = $dsl_device->insert;
+ return { 'error' => $error };
+
+}
+
+sub delete_dsl_device {
+ my $p = shift;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
+ or return { 'error' => "Service not found" };
+
+ my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
+ 'mac_addr' => scalar($p->{'mac_addr'}),
+ }
+ )
+ or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
+
+ my $error = $dsl_device->delete;
+ return { 'error' => $error };
+
+}
+