+sub list_svcs {
+ my $p = shift;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ 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 ( $cust_main->ncancelled_pkgs ) {
+ 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;
+ 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] += $_->minutes; # minutes remaining
+ $row->[2] += $part->minutes; # minutes total
+ }
+
+ 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 $conf = new FS::Conf;
+
+ {
+ 'svcnum' => $session->{'svcnum'},
+ 'custnum' => $custnum,
+ 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
+ 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
+ 'svcs' => [
+ map {
+ my $svc_x = $_->svc_x;
+ my($label, $value) = $_->label;
+ my $part_svc = $_->part_svc;
+ my $svcdb = $part_svc->svcdb;
+ my $cust_pkg = $_->cust_pkg;
+ my $part_pkg = $cust_pkg->part_pkg;
+
+ my %hash = (
+ 'svcnum' => $_->svcnum,
+ 'display_svcnum' => $_->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'),
+ );
+
+ if ( $svcdb eq 'svc_acct' ) {
+ %hash = (
+ %hash,
+ 'username' => $svc_x->username,
+ 'email' => $svc_x->email,
+ 'finger' => $svc_x->finger,
+ 'seconds' => $svc_x->seconds,
+ '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;
+ }
+ } elsif ( $svcdb eq 'svc_phone' ) {
+ # could potentially show lots of things...
+ $hash{'outbound'} = 1;
+ $hash{'inbound'} = 0;
+ 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,
+ );
+ $hash{$_} = $sum_cdr->hashref;
+ }
+ }
+ }
+
+ # elsif ( $svcdb eq 'svc_phone' || $svcdb eq 'svc_port' ) {
+ # %hash = (
+ # %hash,
+ # );
+ #}
+
+ \%hash;
+ }
+ @cust_svc
+ ],
+ 'usage_pools' => [
+ map { $usage_pools{$_} }
+ sort { $a cmp $b }
+ keys %usage_pools
+ ],
+ };
+
+}
+
+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" };
+
+ 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 };
+
+}
+
+sub port_graph {
+ my $p = shift;
+ _usage_details( \&_port_graph, $p,
+ 'svcdb' => 'svc_port',
+ );
+}
+
+sub _port_graph {
+ my($svc_port, $begin, $end) = @_;
+ my @usage = ();
+ my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
+ push @usage, { 'png' => $pngOrError };
+ (@usage);
+}
+
+sub _list_svc_usage {
+ my($svc_acct, $begin, $end) = @_;
+ my @usage = ();
+ foreach my $part_export (
+ map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
+ qw( sqlradius sqlradius_withdomain )
+ ) {
+ push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
+ }
+ (@usage);
+}
+
+sub list_svc_usage {
+ _usage_details(\&_list_svc_usage, @_);
+}
+
+sub _list_support_usage {
+ my($svc_acct, $begin, $end) = @_;
+ my @usage = ();
+ foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
+ qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
+ ) {
+ push @usage, { 'seconds' => $_->seconds,
+ 'support' => $_->support,
+ '_date' => $_->_date,
+ 'id' => $_->transaction_id,
+ 'creator' => $_->creator,
+ 'subject' => $_->subject,
+ 'status' => $_->status,
+ 'ticketid' => $_->ticketid,
+ };
+ }
+ (@usage);
+}
+
+sub list_support_usage {
+ _usage_details(\&_list_support_usage, @_);
+}
+
+sub _list_cdr_usage {
+ # XXX CDR type support...
+ # XXX any way to do a paged search on this?
+ # we have to return the results all at once...
+ my($svc_phone, $begin, $end, %opt) = @_;
+ map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
+ $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, %opt );
+}
+
+sub list_cdr_usage {
+ my $p = shift;
+ _usage_details( \&_list_cdr_usage, $p,
+ 'svcdb' => 'svc_phone',
+ );
+}
+
+sub _usage_details {
+ my($callback, $p, %opt) = @_;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+ return { 'error' => $session } if $context eq 'error';
+
+ my $search = { 'svcnum' => $p->{'svcnum'} };
+ $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
+
+ my $svcdb = $opt{'svcdb'} || 'svc_acct';
+
+ my $svc_x = qsearchs( $svcdb, $search );
+ return { 'error' => 'No service selected in list_svc_usage' }
+ unless $svc_x;
+
+ my $cust_pkg = $svc_x->cust_svc->cust_pkg;
+ my $freq = $cust_pkg->part_pkg->freq;
+ my %callback_opt;
+ my $header = [];
+ if ( $svcdb eq 'svc_phone' ) {
+ my $conf = FS::Conf->new;
+ my $format = '';
+ if ( $p->{inbound} ) {
+ $format = $cust_pkg->part_pkg->option('selfservice_inbound_format')
+ || $conf->config('selfservice-default_inbound_cdr_format')
+ || 'source_default';
+ $callback_opt{inbound} = 1;
+ } else {
+ $format = $cust_pkg->part_pkg->option('selfservice_format')
+ || $conf->config('selfservice-default_cdr_format')
+ || 'default';
+ }
+
+ $callback_opt{format} = $format;
+ $callback_opt{use_clid} = 1;
+ $header = [ split(',', FS::cdr::invoice_header($format) ) ];
+ }
+
+ my $start = $cust_pkg->setup;
+ #my $end = $cust_pkg->bill; # or time?
+ my $end = time;
+
+ unless ( $p->{beginning} ) {
+ $p->{beginning} = $cust_pkg->last_bill;
+ $p->{ending} = $end;
+ }
+
+ die "illegal beginning" if $p->{beginning} !~ /^\d*$/;
+ die "illegal ending" if $p->{ending} !~ /^\d*$/;
+
+ my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
+ %callback_opt
+ );
+
+ #kinda false laziness with FS::cust_main::bill, but perhaps
+ #we should really change this bit to DateTime and DateTime::Duration
+ #
+ #change this bit to use Date::Manip? CAREFUL with timezones (see
+ # mailing list archive)
+ my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
+ (localtime($p->{ending}) )[0,1,2,3,4,5];
+ my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
+ (localtime($p->{beginning}) )[0,1,2,3,4,5];
+
+ if ( $freq =~ /^\d+$/ ) {
+ $nmon += $freq;
+ until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
+ $pmon -= $freq;
+ until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
+ } elsif ( $freq =~ /^(\d+)w$/ ) {
+ my $weeks = $1;
+ $nmday += $weeks * 7;
+ $pmday -= $weeks * 7;
+ } elsif ( $freq =~ /^(\d+)d$/ ) {
+ my $days = $1;
+ $nmday += $days;
+ $pmday -= $days;
+ } elsif ( $freq =~ /^(\d+)h$/ ) {
+ my $hours = $1;
+ $nhour += $hours;
+ $phour -= $hours;
+ } else {
+ return { 'error' => "unparsable frequency: ". $freq };
+ }
+
+ my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
+ my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
+
+ {
+ 'error' => '',
+ 'svcnum' => $p->{svcnum},
+ 'beginning' => $p->{beginning},
+ 'ending' => $p->{ending},
+ 'inbound' => $p->{inbound},
+ 'previous' => ($previous > $start) ? $previous : $start,
+ 'next' => ($next < $end) ? $next : $end,
+ 'header' => $header,
+ 'usage' => \@usage,
+ };
+}
+