+ my $conf = new FS::Conf;
+
+ my $hide_usage = $conf->exists('selfservice_hide-usage') ? 1 : 0;
+ my $search = { 'custnum' => $custnum };
+ $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
+ my $cust_main = qsearchs('cust_main', $search )
+ or return { 'error' => "unknown custnum $custnum" };
+
+ my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
+ if ( ! $pkgnum && $p->{'svcnum'} ) {
+ my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
+ $pkgnum = $cust_svc->pkgnum if $cust_svc;
+ }
+
+ my @cust_svc = ();
+ my @cust_pkg_usage = ();
+ foreach my $cust_pkg ( $p->{'ncancelled'}
+ ? $cust_main->ncancelled_pkgs
+ : $cust_main->unsuspended_pkgs ) {
+ next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
+ push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
+ push @cust_pkg_usage, $cust_pkg->cust_pkg_usage;
+ }
+
+ @cust_svc = grep { $_->part_svc->selfservice_access ne 'hidden' } @cust_svc;
+ my %usage_pools;
+ if (!$hide_usage) {
+ foreach (@cust_pkg_usage) {
+ my $part = $_->part_pkg_usage;
+ my $tag = $part->description . ($part->shared ? 1 : 0);
+ my $row = $usage_pools{$tag}
+ ||= [ $part->description, 0, 0, $part->shared ? 1 : 0 ];
+ $row->[1] += sprintf('%.1f', $_->minutes); # minutes remaining
+ $row->[2] += $part->minutes; # minutes total
+ }
+ } # otherwise just leave them empty
+
+ if ( $p->{'svcdb'} ) {
+ my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
+ ? $p->{'svcdb'}
+ : ref($p->{'svcdb'}) eq 'ARRAY'
+ ? { map { $_=>1 } @{ $p->{'svcdb'} } }
+ : { $p->{'svcdb'} => 1 };
+ @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
+ }
+
+ #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
+ # @svc_x;
+
+ my @svcs; # stuff to return to the client
+ my %bytes_used_total; # for _used columns only
+ foreach my $cust_svc (@cust_svc) {
+ my $svc_x = $cust_svc->svc_x;
+ my($label, $value) = $cust_svc->label;
+ my $part_svc = $cust_svc->part_svc;
+ my $svcdb = $part_svc->svcdb;
+ my $cust_pkg = $cust_svc->cust_pkg;
+ my $part_pkg = $cust_pkg->part_pkg;
+
+ my %hash = (
+ 'svcnum' => $cust_svc->svcnum,
+ 'display_svcnum' => $cust_svc->display_svcnum,
+ 'svcdb' => $svcdb,
+ 'label' => $label,
+ 'value' => $value,
+ 'pkg_label' => $cust_pkg->pkg_locale,
+ 'pkg_status' => $cust_pkg->status,
+ 'readonly' => ($part_svc->selfservice_access eq 'readonly'),
+ );
+
+ # would it make sense to put this in a svc_* method?
+
+ if (!$hide_usage and grep(/^$svcdb$/, qw(svc_acct svc_broadband)) and $part_svc->part_export_usage) {
+ my $last_bill = $cust_pkg->last_bill || 0;
+ my $now = time;
+ my $up_used = $cust_svc->attribute_since_sqlradacct($last_bill,$now,'AcctInputOctets');
+ my $down_used = $cust_svc->attribute_since_sqlradacct($last_bill,$now,'AcctOutputOctets');
+ %hash = (
+ %hash,
+ 'seconds_used' => $cust_svc->seconds_since_sqlradacct($last_bill,$now),
+ 'upbytes_used' => display_bytecount($up_used),
+ 'downbytes_used' => display_bytecount($down_used),
+ 'totalbytes_used' => display_bytecount($up_used + $down_used)
+ );
+ $bytes_used_total{'seconds_used'} += $hash{'seconds_used'};
+ $bytes_used_total{'upbytes_used'} += $up_used;
+ $bytes_used_total{'downbytes_used'} += $down_used;
+ $bytes_used_total{'totalbytes_used'} += $up_used + $down_used;
+ }
+
+ if ( $svcdb eq 'svc_acct' ) {
+ foreach (qw(username email finger seconds)) {
+ $hash{$_} = $svc_x->$_;
+ }
+
+ if (!$hide_usage) {
+ %hash = (
+ %hash,
+ 'upbytes' => display_bytecount($svc_x->upbytes),
+ 'downbytes' => display_bytecount($svc_x->downbytes),
+ 'totalbytes' => display_bytecount($svc_x->totalbytes),
+
+ 'recharge_amount' => $part_pkg->option('recharge_amount',1),
+ 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
+ 'recharge_upbytes' =>
+ display_bytecount($part_pkg->option('recharge_upbytes',1)),
+ 'recharge_downbytes' =>
+ display_bytecount($part_pkg->option('recharge_downbytes',1)),
+ 'recharge_totalbytes' =>
+ display_bytecount($part_pkg->option('recharge_totalbytes',1)),
+ # more...
+ );
+ }
+
+ } elsif ( $svcdb eq 'svc_dsl' ) {
+
+ $hash{'phonenum'} = $svc_x->phonenum;
+ if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
+ $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
+ $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
+ if $svc_x->company;
+ } else {
+ $hash{'name'} = $cust_main->name;
+ }
+ # no usage to hide here
+
+ } elsif ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
+ if (!$hide_usage) {
+ # could potentially show lots of things...
+ $hash{'outbound'} = 1;
+ $hash{'inbound'} = 0;
+ if ( $svcdb eq 'svc_phone' ) {
+ if ( $part_pkg->plan eq 'voip_inbound' ) {
+ $hash{'outbound'} = 0;
+ $hash{'inbound'} = 1;
+ } elsif ( $part_pkg->option('selfservice_inbound_format')
+ or $conf->config('selfservice-default_inbound_cdr_format')
+ ) {
+ $hash{'inbound'} = 1;
+ }
+ }
+ foreach (qw(inbound outbound)) {
+ # hmm...we can't filter by status here, because there might
+ # not be cdr_terminations at all. have to go by date.
+ # find all since the last bill date.
+ # XXX cdr types? we are going to need them.
+ if ( $hash{$_} ) {
+ my $sum_cdr = $svc_x->sum_cdrs(
+ 'inbound' => ( $_ eq 'inbound' ? 1 : 0 ),
+ 'begin' => ($cust_pkg->last_bill || 0),
+ 'nonzero' => 1,
+ 'disable_charged_party' => 1,
+ );
+ $hash{$_} = $sum_cdr->hashref;
+ }
+ }
+ } # not hiding usage
+ } # svcdb
+
+ push @svcs, \%hash;
+ } # foreach $cust_svc
+
+ foreach my $field (keys %bytes_used_total) {
+ if ($field =~ /bytes/) {
+ $bytes_used_total{$field} = display_bytecount($bytes_used_total{$field});
+ }
+ }
+
+ return {
+ 'svcnum' => $session->{'svcnum'},
+ 'custnum' => $custnum,
+ 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
+ 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
+ 'svcs' => \@svcs,
+ 'bytes_used_total' => \%bytes_used_total,
+ 'usage_pools' => [
+ map { $usage_pools{$_} }
+ sort { $a cmp $b }
+ keys %usage_pools
+ ],
+ 'hide_usage' => $hide_usage,
+ };
+
+}
+
+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 { _svc_method_X(shift, 'export_setstatus') }
+sub set_svc_status_listadd { _svc_method_X(shift, 'export_setstatus_listadd') }
+sub set_svc_status_listdel { _svc_method_X(shift, 'export_setstatus_listdel') }
+sub set_svc_status_vacationadd { _svc_method_X(shift, 'export_setstatus_vacationadd') }
+sub set_svc_status_vacationdel { _svc_method_X(shift, 'export_setstatus_vacationdel') }
+
+sub _svc_method_X {
+ my( $p, $method ) = @_;
+
+ 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 "$method ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
+ if $DEBUG;
+ my $error = $svc_x->$method($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" };