1 package FS::ClientAPI::MyAccount;
3 use 5.008; #require 5.8+ for Time::Local 1.05+
5 use vars qw( $cache $DEBUG $me );
6 use subs qw( _cache _provision );
9 use Digest::MD5 qw(md5_hex);
10 use Digest::SHA qw(sha512_hex);
13 use Time::Local qw(timelocal_nocheck);
14 use Business::CreditCard 0.35;
17 use Spreadsheet::WriteExcel;
18 use OLE::Storage_Lite;
19 use FS::UI::Web::small_custview qw(small_custview); #less doh
21 use FS::UI::bytecount qw( display_bytecount );
24 use FS::Record qw(qsearch qsearchs dbh);
25 use FS::Msgcat qw(gettext);
26 use FS::Misc qw(card_types money_pretty);
27 use FS::Misc::DateTime qw(parse_datetime);
29 use FS::ClientAPI_SessionCache;
41 use FS::legacy_cust_bill;
42 use FS::cust_main_county;
46 use FS::acct_rt_transaction;
50 use FS::cust_location;
53 # for code organization
54 use FS::ClientAPI::MyAccount::contact;
55 use FS::ClientAPI::MyAccount::quotation;
58 $me = '[FS::ClientAPI::MyAccount]';
60 use vars qw( @cust_main_editable_fields @location_editable_fields );
61 @cust_main_editable_fields = qw(
62 first last company daytime night fax mobile
64 ss stateid stateid_state
66 @location_editable_fields = qw(
67 address1 address2 city county state zip country
71 BEGIN { #preload to reduce time customer_info takes
72 if ( $FS::TicketSystem::system ) {
73 warn "$me: initializing ticket system\n" if $DEBUG;
74 FS::TicketSystem->init();
79 $cache ||= new FS::ClientAPI_SessionCache( {
80 'namespace' => 'FS::ClientAPI::MyAccount',
87 my($context, $session, $custnum) = _custoragent_session_custnum($p);
88 #return { 'error' => $session } if $context eq 'error';
91 if ( $context eq 'customer' && $custnum ) {
93 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
96 $sth->execute($custnum) or die $sth->errstr;
98 $agentnum = $sth->fetchrow_arrayref->[0]
99 or die "no agentnum for custnum $custnum";
101 #} elsif ( $context eq 'agent' ) {
102 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
105 $p->{'agentnum'} = $agentnum;
107 my $conf = new FS::Conf;
109 #false laziness w/Signup.pm
111 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
113 if ( $skin_info_cache_agent ) {
115 warn "$me loading cached skin info for agentnum $agentnum\n"
120 warn "$me populating skin info cache for agentnum $agentnum\n"
123 $skin_info_cache_agent = {
124 'agentnum' => $agentnum,
125 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
126 qw( company_name date_format ) ),
127 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
128 qw( body_bgcolor box_bgcolor stripe1_bgcolor stripe2_bgcolor
129 text_color link_color vlink_color hlink_color alink_color
130 font title_color title_align title_size menu_bgcolor menu_fontsize
133 'menu_disable' => [ $conf->config('selfservice-menu_disable',$agentnum) ],
134 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
135 qw( menu_skipblanks menu_skipheadings menu_nounderline no_logo enable_payment_without_balance )
137 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
138 qw( title_left_image title_right_image
139 menu_top_image menu_body_image menu_bottom_image
142 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
143 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
144 qw( head body_header body_footer company_address ) ),
145 'money_char' => $conf->config("money_char") || '$',
146 'menu' => join("\n", $conf->config("ng_selfservice-menu", $agentnum ) ) ||
149 services.php Services
150 services.php My Services
151 services_new.php Order a new service
154 personal.php Personal Information
155 password.php Change Password
158 payment_cc.php Credit Card Payment
159 payment_ach.php Electronic Check Payment
160 payment_paypal.php PayPal Payment
161 payment_webpay.php Webpay Payments
164 usage_data.php Data usage
165 usage_cdr.php Call usage
167 tickets.php Help Desk
168 tickets.php Open Tickets
169 tickets_resolved.php Resolved Tickets
170 ticket_create.php Create a new ticket
178 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
182 #{ %$skin_info_cache_agent };
183 $skin_info_cache_agent;
190 my $conf = new FS::Conf;
194 'phone_login' => $conf->exists('selfservice_server-phone_login'),
195 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
196 'banner_url' => scalar($conf->config('selfservice-login_banner_url')),
197 'banner_image_md5' =>
198 md5_hex($conf->config_binary('selfservice-login_banner_image')),
205 sub login_banner_image {
207 my $conf = new FS::Conf;
208 my $image = $conf->config_binary('selfservice-login_banner_image');
210 'md5' => md5_hex($image),
215 #false laziness w/FS::ClientAPI::passwd::passwd
219 my $conf = new FS::Conf;
223 if ( $p->{'domain'} eq 'svc_phone'
224 && $conf->exists('selfservice_server-phone_login') ) {
226 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
227 return { error => 'Number not found.' } unless $svc_phone;
230 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
231 #return { error => 'Only primary user may log in.' }
232 # if $conf->exists('selfservice_server-primary_only')
233 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
235 return { error => 'Incorrect PIN.' }
236 unless $svc_phone->check_pin($p->{'password'});
240 } elsif ( $p->{'domain'} eq 'ip_mac' ) {
242 my $svc_broadband = qsearchs( 'svc_broadband', { 'mac_addr' => $p->{'username'} } );
243 return { error => 'IP address not found' }
244 unless $svc_broadband;
245 $svc_x = $svc_broadband;
247 } elsif ( $p->{email}
248 && (my $contact = FS::contact->by_selfservice_email($p->{email}))
251 return { error => 'Incorrect contact password.' }
252 unless $contact->authenticate_password($p->{'password'});
254 $session->{'contactnum'} = $contact->contactnum;
256 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
257 if ( scalar(@cust_contact) == 1 ) {
258 $session->{'custnum'} = $cust_contact[0]->custnum;
259 } elsif ( scalar(@cust_contact) ) {
260 $session->{'customers'} = { map { $_->custnum => $_->cust_main->name }
264 return { error => 'No customer self-service access for contact' }; #??
269 ( $p->{username}, $p->{domain} ) = split('@', $p->{email}) if $p->{email};
271 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
272 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
274 my @svc_acct = qsearch( 'svc_acct', { 'username' => $p->{'username'},
275 'domsvc' => $svc_domain->svcnum, }
278 if ( $conf->exists('selfservice_server-login_svcpart') ) {
279 my @svcpart = $conf->config('selfservice_server-login_svcpart');
280 @svc_acct = grep { my $svcpart = $_->cust_svc->svcpart;
281 scalar( grep( $_ eq $svcpart, @svcpart ) );
286 if ( $conf->exists('selfservice_server-primary_only') ) {
289 my $cust_svc = $_->cust_svc;
290 $cust_svc->cust_pkg->part_pkg->svcpart([qw( svc_acct svc_phone )])
291 == $cust_svc->svcpart
296 return { error => 'User not found.' } unless @svc_acct;
298 return { error => 'Multiple users.' } if scalar(@svc_acct) > 1;
300 my $svc_acct = $svc_acct[0];
302 if ( $conf->exists('selfservice_server-login_svcpart') ) {
303 my @svcpart = $conf->config('selfservice_server-login_svcpart');
304 my $svcpart = $svc_acct->cust_svc->svcpart;
305 return { error => 'Invalid user.' }
306 unless grep($_ eq $svcpart, @svcpart);
309 return { error => 'Incorrect password.' }
310 unless $svc_acct->check_password($p->{'password'});
318 $session->{'svcnum'} = $svc_x->svcnum;
320 my $cust_svc = $svc_x->cust_svc;
321 my $cust_pkg = $cust_svc->cust_pkg;
323 my $cust_main = $cust_pkg->cust_main;
324 $session->{'custnum'} = $cust_main->custnum;
325 if ( $conf->exists('pkg-balances') ) {
326 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
327 $cust_main->ncancelled_pkgs;
328 $session->{'pkgnum'} = $cust_pkg->pkgnum
329 if scalar(@cust_pkg) > 1;
333 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
334 #return { error => 'Only primary user may log in.' }
335 # if $conf->exists('selfservice_server-primary_only')
336 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
337 my $part_pkg = $cust_pkg->part_pkg;
338 return { error => 'Only primary user may log in.' }
339 if $conf->exists('selfservice_server-primary_only')
340 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
346 $session_id = sha512_hex(time(). {}. rand(). $$)
347 } until ( ! defined _cache->get($session_id) ); #just in case
349 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
350 _cache->set( $session_id, $session, $timeout );
352 return { 'error' => '',
353 'session_id' => $session_id,
360 my $skin_info = skin_info($p);
361 if ( $p->{'session_id'} ) {
362 _cache->remove($p->{'session_id'});
363 return { %$skin_info, 'error' => '' };
365 return { %$skin_info, 'error' => "Can't resume session" }; #better error message
372 my($context, $session, $custnum) = _custoragent_session_custnum($p);
373 return { 'error' => $session } if $context eq 'error';
375 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
376 or return { 'error' => "Service not found" };
378 $session->{'svcnum'} = $svc_acct->svcnum;
380 my $conf = new FS::Conf;
381 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
382 _cache->set( $p->{'session_id'}, $session, $timeout );
384 return { 'error' => '' };
390 my($context, $session, $custnum) = _custoragent_session_custnum($p);
391 return { 'error' => $session } if $context eq 'error';
393 $session->{'custnum'} = $p->{'custnum'}
394 if exists $session->{'customers'}{ $p->{'custnum'} };
396 my $conf = new FS::Conf;
397 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
398 _cache->set( $p->{'session_id'}, $session, $timeout );
400 return { 'error' => '',
401 %{ customer_info( { session_id=>$p->{'session_id'} } ) },
405 sub payment_gateway {
407 # takes a cust_main and a cust_payby entry, returns the payment_gateway
408 my $conf = new FS::Conf;
409 my $cust_main = shift;
410 my $cust_payby = shift;
411 return '' if ! FS::payby->realtime($cust_payby);
412 my $pg = $cust_main->agent->payment_gateway(
413 'method' => FS::payby->payby2bop($cust_payby),
422 my $conf = new FS::Conf;
424 my $info = skin_info($p);
426 use vars qw( $cust_paybys ); #cache for performance
427 unless ( $cust_paybys ) {
429 my %cust_paybys = map { $_ => 1 }
430 map { FS::payby->payby2payment($_) }
431 $conf->config('signup_server-payby');
433 $cust_paybys = [ keys %cust_paybys ];
436 $info->{'cust_paybys'} = $cust_paybys;
438 my($context, $session, $custnum) = _custoragent_session_custnum($p);
439 return { 'error' => $session } if $context eq 'error';
441 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
443 $info->{'hide_payment_fields'} = [
445 my $pg = $cust_main && payment_gateway($cust_main, $_);
446 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
447 } @{ $info->{cust_paybys} }
450 $info->{'self_suspend_reason'} =
451 $conf->config('selfservice-self_suspend_reason',
452 $cust_main ? $cust_main->agentnum : ''
455 $info->{'edit_ticket_subject'} =
456 $conf->exists('ticket_system-selfservice_edit_subject') &&
457 $cust_main && $cust_main->edit_subject;
459 $info->{'timeout'} = $conf->config('selfservice-timeout') || 3600;
461 $info->{'hide_usage'} = $conf->exists('selfservice_hide-usage');
464 'custnum' => $custnum,
465 'access_pkgnum' => $session->{'pkgnum'},
466 'access_svcnum' => $session->{'svcnum'},
473 my($context, $session, $custnum) = _custoragent_session_custnum($p);
474 return { 'error' => $session } if $context eq 'error';
478 my $conf = new FS::Conf;
479 $return{'require_address2'} = $conf->exists('cust_main-require_address2');
481 # if ( $FS::TicketSystem::system ) {
482 # warn "$me customer_info: initializing ticket system\n" if $DEBUG;
483 # FS::TicketSystem->init();
486 if ( $custnum ) { #customer record
488 %return = ( %return, %{ customer_info_short($p) } );
490 #redundant with customer_info_short, but we need it for several things below
491 my $search = { 'custnum' => $custnum };
492 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
493 my $cust_main = qsearchs('cust_main', $search )
494 or return { 'error' => "customer_info: unknown custnum $custnum" };
496 my $list_tickets = list_tickets($p);
497 $return{'tickets'} = $list_tickets->{'tickets'};
499 if ( $session->{'pkgnum'} ) {
500 #XXX open invoices in the pkg-balances case
502 $return{'money_char'} = $conf->config("money_char") || '$';
505 invnum => $_->invnum,
506 date => time2str("%b %o, %Y", $_->_date),
508 charged => $_->charged,
510 } $cust_main->open_cust_bill;
511 $return{open_invoices} = \@open;
513 my $sql = 'SELECT MAX(_date) FROM cust_bill WHERE custnum = ?';
514 my $sth = dbh->prepare($sql) or die dbh->errstr;
515 $sth->execute($custnum) or die $sth->errstr;
516 $return{'last_invoice_date'} = $sth->fetchrow_arrayref->[0];
517 $return{'last_invoice_date_pretty'} =
518 time2str('%m/%d/%Y', $return{'last_invoice_date'} );
521 #customer_info_short always has nobalance on..
522 $return{small_custview} =
523 small_custview( $cust_main,
524 $return{countrydefault},
525 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
528 $return{has_ship_address} = $cust_main->has_ship_address;
529 $return{status} = $cust_main->status_label; #$cust_main->status; #better to break anyone obscurely testing for strings in self-service than to have to upgrade every front-end to get the new status to display
530 $return{statuscolor} = $cust_main->statuscolor;
531 $return{status_label} = $cust_main->status_label;
533 # compatibility: some places in selfservice use this to determine
534 # if there's a ship address
535 if ( $return{has_ship_address} ) {
536 $return{ship_last} = $cust_main->last;
537 $return{ship_first} = $cust_main->first;
540 if (scalar($conf->config('support_packages'))) {
541 my @support_services = ();
542 foreach ($cust_main->support_services) {
543 my $seconds = $_->svc_x->seconds || 0;
544 my $time_remaining = (($seconds < 0) ? '-' : '' ).
545 int(abs($seconds)/3600)."h".
546 sprintf("%02d",(abs($seconds)%3600)/60)."m";
547 my $cust_pkg = $_->cust_pkg;
550 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
551 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
552 push @support_services, { svcnum => $_->svcnum,
553 time => $time_remaining,
558 $return{support_services} = \@support_services;
561 if ( $conf->config('prepayment_discounts-credit_type') ) {
563 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
566 } elsif ( $session->{'svcnum'} ) { #no customer record
568 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
569 or die "unknown svcnum";
570 $return{name} = $svc_acct->email;
574 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
578 return { 'error' => '',
579 'custnum' => $custnum,
585 sub customer_info_short {
588 my($context, $session, $custnum) = _custoragent_session_custnum($p);
589 return { 'error' => $session } if $context eq 'error';
593 my $conf = new FS::Conf;
595 if ( $custnum ) { #customer record
597 my $search = { 'custnum' => $custnum };
598 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
599 my $cust_main = qsearchs('cust_main', $search )
600 or return { 'error' => "customer_info_short: unknown custnum $custnum" };
602 $return{display_custnum} = $cust_main->display_custnum;
603 $return{max_invnum} = $cust_main->max_invnum;
605 if ( $session->{'pkgnum'} ) {
606 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
607 #next_bill_date from cust_pkg?
609 $return{balance} = $cust_main->balance;
610 $return{next_bill_date} = $cust_main->next_bill_date;
611 $return{next_bill_date_pretty} =
612 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
615 $return{balance_pretty} = money_pretty($return{balance});
617 $return{countrydefault} = scalar($conf->config('countrydefault'));
619 $return{small_custview} =
620 small_custview( $cust_main,
621 $return{countrydefault},
625 $return{first} = $cust_main->first;
626 $return{'last'} = $cust_main->get('last');
627 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
629 #none of these are terribly expensive if we want 'em...
630 for (@cust_main_editable_fields) {
631 $return{$_} = $cust_main->get($_);
633 $return{$_} = $cust_main->masked($_) for qw/ss stateid/;
635 #maybe a little more expensive, but it should be cached by now
636 for (@location_editable_fields) {
637 $return{$_} = $cust_main->bill_location->get($_)
638 if $cust_main->bill_locationnum;
639 $return{'ship_'.$_} = $cust_main->ship_location->get($_)
640 if $cust_main->ship_locationnum;
643 my @invoicing_list = $cust_main->invoicing_list;
644 $return{'invoicing_list'} =
645 join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list );
646 $return{'postal_invoicing'} =
647 0 < ( grep { $_ eq 'POST' } @invoicing_list );
649 if ( $session->{'svcnum'} ) {
650 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
651 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
652 $return{'svcnum'} = $session->{'svcnum'};
655 } elsif ( $session->{'svcnum'} ) { #no customer record
657 #uuh, not supproted yet... die?
658 return { 'error' => 'customer_info_short not yet supported as agent' };
662 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
666 # this is here because this routine is called by both fs_ and ng_ main pages, where it appears
667 # it is not customer-specific, though it is only shown to authenticated customers
668 # it is not currently agent-specific, though at some point it might be
669 $return{'announcement'} = join(' ',$conf->config('selfservice-announcement')) || '';
671 return { 'error' => '',
672 'custnum' => $custnum,
677 sub customer_recurring {
680 my($context, $session, $custnum) = _custoragent_session_custnum($p);
681 return { 'error' => $session } if $context eq 'error';
685 my $conf = new FS::Conf;
687 my $search = { 'custnum' => $custnum };
688 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
689 my $cust_main = qsearchs('cust_main', $search )
690 or return { 'error' => "customer_info_short: unknown custnum $custnum" };
692 $return{'display_recurring'} = [ $cust_main->display_recurring ];
694 return { 'error' => '',
695 'custnum' => $custnum,
700 sub billing_history {
703 my($context, $session, $custnum) = _custoragent_session_custnum($p);
704 return { 'error' => $session } if $context eq 'error';
706 return { 'error' => 'No customer' } unless $custnum;
708 my $search = { 'custnum' => $custnum };
709 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
710 my $cust_main = qsearchs('cust_main', $search )
711 or return { 'error' => "unknown custnum $custnum" };
715 if ( $session->{'pkgnum'} ) {
716 #$return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
717 #next_bill_date from cust_pkg?
718 return { 'error' => 'No history for package' };
721 $return{balance} = $cust_main->balance;
722 $return{balance_pretty} = money_pretty($return{balance});
723 $return{next_bill_date} = $cust_main->next_bill_date;
724 $return{next_bill_date_pretty} =
725 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
728 my $conf = new FS::Conf;
730 $return{'history'} = [
731 $cust_main->payment_history(
732 'line_items' => $conf->exists('selfservice-billing_history-line_items'),
737 $return{'money_char'} = $conf->config("money_char") || '$',
745 my $session = _cache->get($p->{'session_id'})
746 or return { 'error' => "Can't resume session" }; #better error message
748 my $custnum = $session->{'custnum'}
749 or return { 'error' => "no customer record" };
751 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
752 or return { 'error' => "unknown custnum $custnum" };
754 my $conf = new FS::Conf;
755 if (($p->{payby} eq "CHEK" || $p->{payby} eq "DCHEK") && $conf->exists('selfservice-ACH_info_readonly')) {
756 return { 'error' => "You do not have authority to add a bank account" };
759 my $new = new FS::cust_main { $cust_main->hash };
761 $new->set( $_ => $p->{$_} )
762 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
764 if ( exists($p->{address1}) ) {
765 my $bill_location = FS::cust_location->new({
766 map { $_ => $p->{$_} } @location_editable_fields
768 # if this is unchanged from before, cust_main::replace will ignore it
769 $new->set('bill_location' => $bill_location);
772 if ( exists($p->{ship_address1}) ) {
773 my $ship_location = FS::cust_location->new({
774 map { $_ => $p->{"ship_$_"} } @location_editable_fields
776 if ( !grep { length($p->{"ship_$_"}) } @location_editable_fields ) {
777 # Selfservice unfortunately tries to indicate "same as billing
778 # address" by sending all fields empty. Did this ever work?
779 $ship_location = $cust_main->bill_location;
781 $new->set('ship_location' => $ship_location);
783 # but if it hasn't been passed in at all, leave ship_location alone--
784 # DON'T change it to match bill_location.
787 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
788 #false laziness with httemplate/edit/process/cust_main.cgi
789 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
790 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
792 @invoicing_list = $cust_main->invoicing_list;
795 my $error = $new->replace($cust_main, \@invoicing_list);
796 return { 'error' => $error } if $error;
799 return { 'error' => '' };
804 my $session = _cache->get($p->{'session_id'})
805 or return { 'error' => "Can't resume session" }; #better error message
811 my $conf = new FS::Conf;
812 use vars qw($payment_info); #cache for performance
813 unless ( $payment_info ) {
815 my %states = map { $_->state => 1 }
816 qsearch('cust_main_county', {
817 'country' => $conf->config('countrydefault') || 'US'
820 my %cust_paybys = map { $_ => 1 }
821 map { FS::payby->payby2payment($_) }
822 $conf->config('signup_server-payby');
824 my @cust_paybys = keys %cust_paybys;
828 #list all counties/states/countries
829 'cust_main_county' =>
830 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
832 #shortcut for one-country folks
834 [ sort { $a cmp $b } keys %states ],
836 'card_types' => card_types(),
838 'withcvv' => $conf->exists('selfservice-require_cvv'), #or enable optional cvv?
839 'require_cvv' => $conf->exists('selfservice-require_cvv'),
840 'onfile_require_cvv' => $conf->exists('selfservice-onfile_require_cvv'),
842 'paytypes' => [ FS::cust_payby::paytypes ],
844 'paybys' => [ $conf->config('signup_server-payby') ],
845 'cust_paybys' => \@cust_paybys,
847 'stateid_label' => FS::Msgcat::_gettext('stateid'),
848 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
850 'show_ss' => $conf->exists('show_ss'),
851 'show_stateid' => $conf->exists('show_stateid'),
852 'show_paystate' => $conf->exists('show_bankstate'),
854 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
855 'ach_read_only' => $conf->exists('selfservice-ACH_info_readonly'),
865 my %return = %$payment_info;
867 delete $return{'cust_main_county'} if $p->{'omit_cust_main_county'};
869 my $custnum = $session->{'custnum'};
871 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
872 or return { 'error' => "unknown custnum $custnum" };
874 $return{'hide_payment_fields'} = [
876 my $pg = payment_gateway($cust_main, $_);
877 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
878 } @{ $return{cust_paybys} }
881 $return{balance} = $cust_main->balance; #XXX pkg-balances?
883 $return{$_} = $cust_main->bill_location->get($_)
884 for qw(address1 address2 city state zip);
886 # look for stored cust_payby info
887 # only if we've been given a clear payment_payby (to avoid payname conflicts)
888 if ($p->{'payment_payby'} =~ /^(CARD|CHEK)$/) {
889 my @search_payby = ($p->{'payment_payby'} eq 'CARD') ? ('CARD','DCRD') : ('CHEK','DCHK');
890 my ($cust_payby) = $cust_main->cust_payby(@search_payby);
892 $return{payname} = $cust_payby->payname
893 || ( $cust_main->first. ' '. $cust_main->get('last') );
894 $return{custpaybynum} = $cust_payby->custpaybynum;
896 if ( $cust_payby->payby =~ /^(CARD|DCRD)$/ ) {
897 $return{card_type} = cardtype($cust_payby->payinfo);
898 $return{payinfo} = $cust_payby->paymask;
900 @return{'month', 'year'} = $cust_payby->paydate_monthyear;
904 if ( $cust_payby->payby =~ /^(CHEK|DCHK)$/ ) {
905 my ($payinfo1, $payinfo2) = split '@', $cust_payby->paymask;
906 $return{payinfo1} = $payinfo1;
907 $return{payinfo2} = $payinfo2;
908 $return{paytype} = $cust_payby->paytype;
909 $return{paystate} = $cust_payby->paystate;
910 $return{payname} = $cust_payby->payname; # override 'first/last name' default from above, if any. Is instution-name here. (#15819)
915 if ( $conf->config('prepayment_discounts-credit_type') ) {
917 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
920 #doubleclick protection
922 $return{payunique} = "webui-MyAccount-$_date-$$-". rand() * 2**32; #new
923 $return{paybatch} = $return{payunique}; #back compat
925 $return{credit_card_surcharge_percentage} = $conf->config('credit-card-surcharge-percentage', $cust_main->agentnum);
926 $return{credit_card_surcharge_flatfee} = $conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum);
928 return { 'error' => '',
934 #some false laziness with httemplate/process/payment.cgi - look there for
935 #ACH and CVV support stuff
937 sub validate_payment {
940 my $session = _cache->get($p->{'session_id'})
941 or return { 'error' => "Can't resume session" }; #better error message
943 my $custnum = $session->{'custnum'};
945 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
946 or return { 'error' => "unknown custnum $custnum" };
948 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
949 or return { 'error' => gettext('illegal_amount') };
951 return { error => 'Amount must be greater than 0' } unless $amount > 0;
953 #false laziness w/tr-amount_fee.html, but we don't want selfservice users
954 #changing the hidden form values
955 my $conf = new FS::Conf;
956 my $fee_display = $conf->config('selfservice_process-display') || 'add';
957 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
958 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
959 if ( $fee_display eq 'add'
961 and ! $fee_skip_first || scalar($cust_main->cust_pay)
964 my $fee_pkg = qsearchs('part_pkg', { pkgpart=>$fee_pkgpart } );
965 $amount = sprintf('%.2f', $amount + $fee_pkg->option('setup_fee') );
968 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
969 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
970 my $discount_term = $1;
972 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
973 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
976 $p->{'payunique'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
977 or return { 'error' => gettext('illegal_text'). " payunique: ". $p->{'payunique'} };
980 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
981 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
984 $payunique = $paybatch if ! length($payunique) && length($paybatch);
986 $p->{'payby'} ||= 'CARD';
987 $p->{'payby'} =~ /^([A-Z]{4})$/
988 or return { 'error' => "illegal_payby " . $p->{'payby'} };
991 #false laziness w/process/payment.cgi
994 my $replace_cust_payby;
995 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
997 $p->{'payinfo1'} =~ /^([\dx]+)$/
998 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1000 $p->{'payinfo2'} =~ /^([\dx]+)$/
1001 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1003 $payinfo = $payinfo1. '@'. $payinfo2;
1006 foreach my $cust_payby ($cust_main->cust_payby('CHEK','DCHK')) {
1007 if ( $cust_payby->paymask eq $payinfo ) {
1008 $payinfo = $cust_payby->payinfo;
1009 $replace_cust_payby = $cust_payby;
1015 if ($conf->exists('selfservice-ACH_info_readonly') && !$achonfile) {
1016 return { 'error' => "You are not allowed to change your payment information." };
1019 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
1021 $payinfo = $p->{'payinfo'};
1025 #more intelligent matching will be needed here if you change
1026 #card_masking_method and don't remove existing paymasks
1027 foreach my $cust_payby ($cust_main->cust_payby('CARD','DCRD')) {
1028 if ( $cust_payby->paymask eq $payinfo ) {
1029 $payinfo = $cust_payby->payinfo;
1030 $replace_cust_payby = $cust_payby;
1036 $payinfo =~ s/\D//g;
1037 $payinfo =~ /^(\d{13,19}|\d{8,9})$/
1038 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1042 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1043 return { 'error' => gettext('unknown_card_type') }
1044 if !$cust_main->tokenized($payinfo) && cardtype($payinfo) eq "Unknown";
1046 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
1047 if ( cardtype($payinfo) eq 'American Express card' ) {
1048 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
1049 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
1052 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
1053 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
1056 } elsif ( $conf->exists('selfservice-onfile_require_cvv') ) {
1057 return { 'error' => 'CVV2 is required' };
1058 } elsif ( !$onfile && $conf->exists('selfservice-require_cvv') ) {
1059 return { 'error' => 'CVV2 is required' };
1063 die "unknown payby $payby";
1066 my %payby2fields = (
1067 'CARD' => [ qw( paystart_month paystart_year payissue payip
1068 address1 address2 city state zip country ) ],
1069 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
1072 my %replace = ( 'replace' => $replace_cust_payby, );
1075 $card_type = cardtype($payinfo) if $payby eq 'CARD';
1078 'cust_main' => $cust_main, #XXX or just custnum??
1079 'amount' => sprintf('%.2f', $amount),
1081 'payinfo' => $payinfo,
1082 'paymask' => FS::payinfo_Mixin->mask_payinfo( $payby, $payinfo ),
1083 'card_type' => $card_type,
1084 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
1085 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
1086 'month' => $p->{'month'},
1087 'year' => $p->{'year'},
1088 'payname' => $payname,
1089 'payunique' => $payunique,
1090 'paybatch' => $paybatch,
1091 'paycvv' => $paycvv,
1092 'payname' => $payname,
1093 'discount_term' => $discount_term,
1094 'pkgnum' => $session->{'pkgnum'},
1096 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
1106 my $validate = validate_payment($p);
1107 return $validate if $validate->{'error'};
1109 my $conf = new FS::Conf;
1110 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
1111 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
1113 +{ map { $_=>$validate->{$_} }
1114 qw( card_type paymask payname paydate_pretty month year amount
1115 address1 address2 city state zip country
1121 sub process_stored_payment {
1124 my $session_id = $p->{'session_id'};
1126 my $payment_info = _cache->get( "payment_$session_id" )
1127 or return { 'error' => "Can't resume session" }; #better error message
1129 do_process_payment($payment_info);
1133 sub process_payment {
1136 my $payment_info = validate_payment($p);
1137 return $payment_info if $payment_info->{'error'};
1139 do_process_payment($payment_info);
1143 sub do_process_payment {
1144 my $validate = shift;
1146 my $cust_main = $validate->{'cust_main'};
1148 my $amount = delete $validate->{'amount'};
1151 my $payby = delete $validate->{'payby'};
1153 if ( $validate->{'save'} ) {
1156 foreach my $field ( qw( auto payinfo paymask payname payip ) ) {
1157 $saveopt{$field} = $validate->{$field};
1160 if ( $payby eq 'CARD' ) {
1161 my $bill_location = FS::cust_location->new({
1162 map { $_ => $validate->{$_} }
1163 qw(address1 address2 city state country zip)
1165 $saveopt{'bill_location'} = $bill_location;
1166 foreach my $field ( qw( paydate paystart_month paystart_year payissue ) ) {
1167 $saveopt{$field} = $validate->{$field};
1170 # stateid/stateid_state won't be saved, might be broken as of 4.x
1171 foreach my $field ( qw( paytype paystate ) ) {
1172 $saveopt{$field} = $validate->{$field};
1176 my $error = $cust_main->save_cust_payby(
1177 'payment_payby' => $payby,
1178 'replace' => $validate->{'replace'}, # cust_payby object to replace
1183 #no, this causes customers to process their payments again
1184 #return { 'error' => $error };
1185 #XXX just warn verosely for now so i can figure out how these happen in
1186 # the first place, eventually should redirect them to the "change
1187 #address" page but indicate if the payment processed?
1188 delete($validate->{'payinfo'}); #don't want to log this!
1189 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
1191 "SAVEOPT: ".Dumper(\%saveopt)."\n".
1192 "CUST_MAIN: ". Dumper($cust_main)."\n".
1193 "PACKET: ". Dumper($validate)."\n";
1197 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
1201 'paynum_ref' => \$paynum,
1204 return { 'error' => $error } if $error;
1206 #no error, so order the fee package if applicable...
1207 my $conf = new FS::Conf;
1208 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
1209 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
1211 if ( $fee_pkgpart and ! $fee_skip_first || scalar($cust_main->cust_pay) ) {
1213 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $fee_pkgpart };
1215 $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
1216 return { 'error' => "payment processed successfully, but error ordering fee: $error" }
1219 #and generate an invoice for it now too
1220 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1221 return { 'error' => "payment processed and fee ordered successfully, but error billing fee: $error" }
1226 $cust_main->apply_payments;
1229 my $receipt_html = '';
1231 # currently supported for realtime CC only; send receipt data to SS
1232 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
1235 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
1238 <TD ALIGN="right">Payment#</TD>
1239 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
1243 <TD ALIGN="right">Date</TD>
1245 <TD BGCOLOR="#FFFFFF"><B>! .
1246 time2str("%a %b %o, %Y %r", $cust_pay->_date)
1252 <TD ALIGN="right">Amount</TD>
1253 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
1258 <TD ALIGN="right">Payment method</TD>
1259 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
1272 'amount' => sprintf('%.2f', $cust_pay->paid),
1273 'date' => $cust_pay->_date,
1274 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
1275 'time_pretty' => time2str('%T', $cust_pay->_date),
1276 'auth_num' => $cust_pay->auth,
1277 'order_num' => $cust_pay->order_number,
1278 'receipt_html' => $receipt_html,
1285 'receipt_html' => '',
1292 sub realtime_collect {
1295 my $session = _cache->get($p->{'session_id'})
1296 or return { 'error' => "Can't resume session" }; #better error message
1298 my $custnum = $session->{'custnum'};
1300 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1301 or return { 'error' => "unknown custnum $custnum" };
1304 if ( $p->{'amount'} ) {
1305 $amount = $p->{'amount'};
1307 elsif ( $session->{'pkgnum'} ) {
1308 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1311 $amount = $cust_main->balance;
1314 my $error = $cust_main->realtime_collect(
1315 'method' => $p->{'method'},
1316 'amount' => $amount,
1317 'pkgnum' => $session->{'pkgnum'},
1318 'session_id' => $p->{'session_id'},
1322 return { 'error' => $error } unless ref( $error );
1324 return { 'error' => '', amount => $amount, %$error };
1327 sub start_thirdparty {
1329 my $session = _cache->get($p->{'session_id'})
1330 or return { 'error' => "Can't resume session" }; #better error message
1331 my $custnum = $session->{'custnum'};
1332 my $cust_main = FS::cust_main->by_key($custnum);
1334 my $amount = $p->{'amount'}
1335 or return { error => 'no amount' };
1337 my $result = $cust_main->create_payment(
1338 'method' => $p->{'method'},
1339 'amount' => $p->{'amount'},
1340 'pkgnum' => $session->{'pkgnum'},
1341 'session_id' => $p->{'session_id'},
1344 if ( ref($result) ) { # hashref or error
1347 return { error => $result };
1351 sub finish_thirdparty {
1353 my $session_id = delete $p->{'session_id'};
1354 my $session = _cache->get($session_id)
1355 or return { 'error' => "Can't resume session" };
1356 my $custnum = $session->{'custnum'};
1357 my $cust_main = FS::cust_main->by_key($custnum);
1359 if ( $p->{_cancel} ) {
1360 # customer backed out of making a payment
1361 return $cust_main->cancel_payment( $session_id );
1363 my $result = $cust_main->execute_payment( $session_id, %$p );
1364 if ( ref($result) ) {
1367 return { error => $result };
1371 sub process_payment_order_pkg {
1374 my $hr = process_payment($p);
1375 return $hr if $hr->{'error'};
1380 sub process_payment_order_renew {
1383 my $hr = process_payment($p);
1384 return $hr if $hr->{'error'};
1389 sub process_prepay {
1393 my $session = _cache->get($p->{'session_id'})
1394 or return { 'error' => "Can't resume session" }; #better error message
1398 my $custnum = $session->{'custnum'};
1400 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1401 or return { 'error' => "unknown custnum $custnum" };
1403 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1404 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1412 return { 'error' => $error } if $error;
1414 return { 'error' => '',
1415 'amount' => $amount,
1416 'seconds' => $seconds,
1417 'duration' => duration_exact($seconds),
1418 'upbytes' => $upbytes,
1419 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1420 'downbytes' => $downbytes,
1421 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1422 'totalbytes'=> $totalbytes,
1423 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1430 my $session = _cache->get($p->{'session_id'})
1431 or return { 'error' => "Can't resume session" }; #better error message
1433 my $custnum = $session->{'custnum'};
1435 my $invnum = $p->{'invnum'};
1437 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1438 'custnum' => $custnum } )
1439 or return { 'error' => "Can't find invnum" };
1443 return { 'error' => '',
1444 'invnum' => $invnum,
1445 'invoice_text' => join('', $cust_bill->print_text ),
1446 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1453 my $session = _cache->get($p->{'session_id'})
1454 or return { 'error' => "Can't resume session" }; #better error message
1456 my $custnum = $session->{'custnum'};
1458 my $invnum = $p->{'invnum'};
1460 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1461 'custnum' => $custnum } )
1462 or return { 'error' => "Can't find invnum" };
1466 return { 'error' => '',
1467 'invnum' => $invnum,
1468 'invoice_pdf' => $cust_bill->print_pdf({
1469 'unsquelch_cdr' => 1,
1470 'locale' => $p->{'locale'},
1476 sub legacy_invoice {
1478 my $session = _cache->get($p->{'session_id'})
1479 or return { 'error' => "Can't resume session" }; #better error message
1481 my $custnum = $session->{'custnum'};
1483 my $legacyinvnum = $p->{'legacyinvnum'};
1486 'legacyinvnum' => $legacyinvnum,
1487 'custnum' => $custnum,
1490 my $legacy_cust_bill =
1491 qsearchs('legacy_cust_bill', { %hash, 'locale' => $p->{'locale'} } )
1492 || qsearchs('legacy_cust_bill', \%hash )
1493 or return { 'error' => "Can't find legacyinvnum" };
1497 return { 'error' => '',
1498 'legacyinvnum' => $legacyinvnum,
1499 'legacyid' => $legacy_cust_bill->legacyid,
1500 'invoice_html' => $legacy_cust_bill->content_html,
1505 sub legacy_invoice_pdf {
1507 my $session = _cache->get($p->{'session_id'})
1508 or return { 'error' => "Can't resume session" }; #better error message
1510 my $custnum = $session->{'custnum'};
1512 my $legacyinvnum = $p->{'legacyinvnum'};
1514 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1515 'legacyinvnum' => $legacyinvnum,
1516 'custnum' => $custnum,
1517 }) or return { 'error' => "Can't find legacyinvnum" };
1521 return { 'error' => '',
1522 'legacyinvnum' => $legacyinvnum,
1523 'legacyid' => $legacy_cust_bill->legacyid,
1524 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1532 #sessioning for this? how do we get the session id to the backend invoice
1533 # template so it can add it to the link, blah
1536 if ( $p->{'invnum'} ) {
1537 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1538 or return { 'error' => 'unknown invnum' };
1539 $agentnum = $cust_bill->cust_main->agentnum;
1542 my $templatename = $p->{'template'} || $p->{'templatename'};
1544 #false laziness-ish w/view/cust_bill-logo.cgi
1546 my $conf = new FS::Conf;
1547 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1548 $templatename = "_$1";
1553 my $filename = "logo$templatename.png";
1555 return { 'error' => '',
1556 'logo' => $conf->config_binary($filename, $agentnum),
1557 'content_type' => 'image/png', #should allow gif, jpg too
1563 my $session = _cache->get($p->{'session_id'})
1564 or return { 'error' => "Can't resume session" }; #better error message
1566 my $custnum = $session->{'custnum'};
1568 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1569 or return { 'error' => "unknown custnum $custnum" };
1571 my $conf = new FS::Conf;
1573 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1575 my @cust_bill = grep ! $_->hide, $cust_main->cust_bill;
1580 #not super efficient, we also run cust_bill_pay/cust_credited inside owed
1581 my @payments_and_credits = sort {$b->_date <=> $a->_date} ($_->cust_bill_pay,$_->cust_credited);
1582 my $owed = $_->owed;
1584 +{ 'invnum' => $_->invnum,
1585 '_date' => $_->_date,
1586 'date' => time2str("%b %o, %Y", $_->_date),
1587 'date_short' => time2str("%m-%d-%Y", $_->_date),
1588 'previous' => sprintf('%.2f', ($_->previous)[0]),
1589 'charged' => sprintf('%.2f', $_->charged),
1590 'owed' => sprintf('%.2f', $owed),
1591 'balance' => sprintf('%.2f', $balance),
1592 'lastpay' => @payments_and_credits
1593 ? time2str("%b %o, %Y", $payments_and_credits[0]->_date)
1599 return { 'error' => '',
1600 'balance' => $cust_main->balance,
1601 'money_char' => $conf->config("money_char") || '$',
1602 'invoices' => $invoices,
1603 'legacy_invoices' => [
1605 +{ 'legacyinvnum' => $_->legacyinvnum,
1606 'legacyid' => $_->legacyid,
1607 '_date' => $_->_date,
1608 'date' => time2str("%b %o, %Y", $_->_date),
1609 'date_short' => time2str("%m-%d-%Y", $_->_date),
1610 'charged' => sprintf('%.2f', $_->charged),
1611 'has_content' => ( length($_->content_pdf)
1612 || length($_->content_html) ),
1622 my $session = _cache->get($p->{'session_id'})
1623 or return { 'error' => "Can't resume session" }; #better error message
1625 my $custnum = $session->{'custnum'};
1627 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1628 or return { 'error' => "unknown custnum $custnum" };
1630 return { 'error' => '',
1631 'balance' => $cust_main->balance,
1632 'money_char' => FS::Conf->new->config("money_char") || '$',
1633 'payments' => [ map $_->SSAPI_getinfo, $cust_main->cust_pay ],
1637 sub payment_receipt {
1639 my $session = _cache->get($p->{'session_id'})
1640 or return { 'error' => "Can't resume session" }; #better error message
1642 my $custnum = $session->{'custnum'};
1644 my $cust_pay = qsearchs('cust_pay', { 'custnum' => $custnum,
1645 'paynum' => $p->{'paynum'},
1648 or return { 'error' => "unknown payment ". $p->{'paynum'} };
1652 %{ $cust_pay->SSAPI_getinfo },
1659 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1660 return { 'error' => $session } if $context eq 'error';
1662 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1663 or return { 'error' => "unknown custnum $custnum" };
1667 my $cust_payby = $_;
1669 map { $_ => $cust_payby->$_ }
1670 qw( custpaybynum weight payby paymask paydate
1671 payname paystate paytype
1675 $cust_main->cust_payby
1683 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1684 return { 'error' => $session } if $context eq 'error';
1686 #XXX payinfo1 + payinfo2 for CHEK?
1687 #or take the opportunity to use separate, more well- named fields?
1688 if ($p->{'payby'} eq 'CHEK') {
1689 $p->{'payinfo1'} =~ /^([\dx]+)$/
1690 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1692 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
1693 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1695 $p->{'payinfo'} = $payinfo1. '@'. $payinfo2;
1698 my $cust_payby = new FS::cust_payby {
1699 'custnum' => $custnum,
1700 map { $_ => $p->{$_} } qw( weight payby payinfo paycvv paydate payname
1701 paystate paytype payip
1705 my $error = $cust_payby->insert;
1707 return { 'error' => $error };
1709 return { 'custpaybynum' => $cust_payby->custpaybynum };
1717 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1718 return { 'error' => $session } if $context eq 'error';
1720 if ($p->{'payby'} eq 'CHEK') {
1721 $p->{'payinfo1'} =~ /^([\dx]+)$/
1722 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1724 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
1725 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1727 $p->{'payinfo'} = $payinfo1. '@'. $payinfo2;
1730 my $cust_payby = qsearchs('cust_payby', {
1731 'custnum' => $custnum,
1732 'custpaybynum' => $p->{'custpaybynum'},
1734 or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
1737 qw( weight payby payinfo paycvv paydate payname paystate paytype payip )
1739 next unless exists($p->{$field});
1740 $cust_payby->set($field,$p->{$field});
1743 my $error = $cust_payby->replace;
1745 return { 'error' => $error };
1747 return { 'custpaybynum' => $cust_payby->custpaybynum };
1755 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1756 return { 'error' => $session } if $context eq 'error';
1758 my $cust_payby = qsearchs('cust_payby', {
1759 'custnum' => $custnum,
1760 'custpaybynum' => $p->{'custpaybynum'},
1762 or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
1764 return { 'error' => $cust_payby->verify };
1771 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1772 return { 'error' => $session } if $context eq 'error';
1774 my $cust_payby = qsearchs('cust_payby', {
1775 'custnum' => $custnum,
1776 'custpaybynum' => $p->{'custpaybynum'},
1778 or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
1780 my $conf = new FS::Conf;
1781 if (($cust_payby->payby eq "DCHK" || $cust_payby->payby eq "CHEK") && $conf->exists('selfservice-ACH_info_readonly')) {
1782 return { 'error' => "Sorry you do not have permission to delete bank information." };
1785 return { 'error' => $cust_payby->delete };
1791 my $session = _cache->get($p->{'session_id'})
1792 or return { 'error' => "Can't resume session" }; #better error message
1794 my $custnum = $session->{'custnum'};
1796 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1797 or return { 'error' => "unknown custnum $custnum" };
1799 my @errors = $cust_main->cancel( 'quiet'=>1 );
1801 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1803 return { 'error' => $error };
1810 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1811 return { 'error' => $session } if $context eq 'error';
1813 my $pkg = qsearchs({
1814 'table' => 'cust_pkg',
1815 'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
1817 'custnum' => $custnum,
1818 'pkgnum' => $p->{'pkgnum'},
1821 or return {'error' => 'unknown pkg num $pkgnum'};
1824 pkg_label => $pkg->pkg,
1825 pkgpart => $pkg->pkgpart,
1826 classnum => $pkg->classnum,
1834 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1835 return { 'error' => $session } if $context eq 'error';
1837 my $search = { 'custnum' => $custnum };
1838 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1839 my $cust_main = qsearchs('cust_main', $search )
1840 or return { 'error' => "unknown custnum $custnum" };
1842 my $conf = new FS::Conf;
1843 my $immutable = $conf->exists('selfservice_immutable-package');
1845 # the duplication below is necessary:
1846 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1847 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1848 # non-backwards-compatible change breaking the software of anyone using the API
1849 # instead of the stock selfservice
1850 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1851 if( $conf->exists('selfservice_server-view-wholesale') ) {
1852 return { 'svcnum' => $session->{'svcnum'},
1853 'custnum' => $custnum,
1854 'cust_pkg' => [ map {
1856 immutable => $immutable,
1857 part_pkg => [ map $_->hashref, $_->part_pkg ],
1859 [ map $_->hashref, $_->available_part_svc ],
1861 [ map { my $ref = { $_->hash,
1862 label => [ $_->label ],
1864 $ref->{_password} = $_->svc_x->_password
1865 if $context eq 'agent'
1866 && $conf->exists('agent-showpasswords')
1867 && $_->part_svc->svcdb eq 'svc_acct';
1872 } $cust_main->cust_pkg
1875 small_custview( $cust_main, $conf->config('countrydefault') ),
1876 'wholesale_view' => 1,
1877 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1878 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1879 'lnp' => $conf->exists('svc_phone-lnp'),
1883 { 'svcnum' => $session->{'svcnum'},
1884 'custnum' => $custnum,
1885 'cust_pkg' => [ map {
1886 my $primary_cust_svc = $_->primary_cust_svc;
1889 immutable => $immutable,
1890 pkg_label => $_->pkg_locale,
1891 status => $_->status,
1892 statuscolor => $_->statuscolor,
1894 [ map { $_->hashref }
1895 grep { $_->selfservice_access ne 'hidden' }
1896 $_->available_part_svc
1899 [ map { my $ref = { $_->hash,
1900 label => [ $_->label ],
1902 $ref->{_password} = $_->svc_x->_password
1903 if $context eq 'agent'
1904 && $conf->exists('agent-showpasswords')
1905 && $_->part_svc->svcdb eq 'svc_acct';
1906 $ref->{svchash} = { $_->svc_x->hash } if
1907 $_->part_svc->svcdb eq 'svc_phone';
1908 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1909 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1912 grep { $_->part_svc->selfservice_access ne 'hidden' }
1917 ? { $primary_cust_svc->hash,
1918 label => [ $primary_cust_svc->label ],
1919 finger => $primary_cust_svc->svc_x->finger, #uuh
1920 $primary_cust_svc->part_svc->hash,
1924 } $cust_main->ncancelled_pkgs
1927 small_custview( $cust_main, $conf->config('countrydefault') ),
1928 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1936 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1937 return { 'error' => $session } if $context eq 'error';
1939 my $conf = new FS::Conf;
1941 my $hide_usage = $conf->exists('selfservice_hide-usage') ? 1 : 0;
1942 my $search = { 'custnum' => $custnum };
1943 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1944 my $cust_main = qsearchs('cust_main', $search )
1945 or return { 'error' => "unknown custnum $custnum" };
1947 my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
1948 if ( ! $pkgnum && $p->{'svcnum'} ) {
1949 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
1950 $pkgnum = $cust_svc->pkgnum if $cust_svc;
1954 my @cust_pkg_usage = ();
1955 foreach my $cust_pkg ( $p->{'ncancelled'}
1956 ? $cust_main->ncancelled_pkgs
1957 : $cust_main->unsuspended_pkgs ) {
1958 next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
1959 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1960 push @cust_pkg_usage, $cust_pkg->cust_pkg_usage;
1963 @cust_svc = grep { $_->part_svc->selfservice_access ne 'hidden' } @cust_svc;
1966 foreach (@cust_pkg_usage) {
1967 my $part = $_->part_pkg_usage;
1968 my $tag = $part->description . ($part->shared ? 1 : 0);
1969 my $row = $usage_pools{$tag}
1970 ||= [ $part->description, 0, 0, $part->shared ? 1 : 0 ];
1971 $row->[1] += sprintf('%.1f', $_->minutes); # minutes remaining
1972 $row->[2] += $part->minutes; # minutes total
1974 } # otherwise just leave them empty
1976 if ( $p->{'svcdb'} ) {
1977 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1979 : ref($p->{'svcdb'}) eq 'ARRAY'
1980 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1981 : { $p->{'svcdb'} => 1 };
1982 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1985 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1988 my @svcs; # stuff to return to the client
1989 my %bytes_used_total; # for _used columns only
1990 foreach my $cust_svc (@cust_svc) {
1991 my $svc_x = $cust_svc->svc_x;
1992 my($label, $value) = $cust_svc->label;
1993 my $part_svc = $cust_svc->part_svc;
1994 my $svcdb = $part_svc->svcdb;
1995 my $cust_pkg = $cust_svc->cust_pkg;
1996 my $part_pkg = $cust_pkg->part_pkg;
1999 'svcnum' => $cust_svc->svcnum,
2000 'display_svcnum' => $cust_svc->display_svcnum,
2004 'pkg_label' => $cust_pkg->pkg_locale,
2005 'pkg_status' => $cust_pkg->status,
2006 'readonly' => ($part_svc->selfservice_access eq 'readonly'),
2009 # would it make sense to put this in a svc_* method?
2011 if (!$hide_usage and grep(/^$svcdb$/, qw(svc_acct svc_broadband)) and $part_svc->part_export_usage) {
2012 my $last_bill = $cust_pkg->last_bill || 0;
2014 my $up_used = $cust_svc->attribute_since_sqlradacct($last_bill,$now,'AcctInputOctets');
2015 my $down_used = $cust_svc->attribute_since_sqlradacct($last_bill,$now,'AcctOutputOctets');
2018 'seconds_used' => $cust_svc->seconds_since_sqlradacct($last_bill,$now),
2019 'upbytes_used' => display_bytecount($up_used),
2020 'downbytes_used' => display_bytecount($down_used),
2021 'totalbytes_used' => display_bytecount($up_used + $down_used)
2023 $bytes_used_total{'seconds_used'} += $hash{'seconds_used'};
2024 $bytes_used_total{'upbytes_used'} += $up_used;
2025 $bytes_used_total{'downbytes_used'} += $down_used;
2026 $bytes_used_total{'totalbytes_used'} += $up_used + $down_used;
2029 if ( $svcdb eq 'svc_acct' ) {
2030 foreach (qw(username email finger seconds)) {
2031 $hash{$_} = $svc_x->$_;
2037 'upbytes' => display_bytecount($svc_x->upbytes),
2038 'downbytes' => display_bytecount($svc_x->downbytes),
2039 'totalbytes' => display_bytecount($svc_x->totalbytes),
2041 'recharge_amount' => $part_pkg->option('recharge_amount',1),
2042 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
2043 'recharge_upbytes' =>
2044 display_bytecount($part_pkg->option('recharge_upbytes',1)),
2045 'recharge_downbytes' =>
2046 display_bytecount($part_pkg->option('recharge_downbytes',1)),
2047 'recharge_totalbytes' =>
2048 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
2053 } elsif ( $svcdb eq 'svc_dsl' ) {
2055 $hash{'phonenum'} = $svc_x->phonenum;
2056 if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
2057 $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
2058 $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
2061 $hash{'name'} = $cust_main->name;
2063 # no usage to hide here
2065 } elsif ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
2067 # could potentially show lots of things...
2068 $hash{'outbound'} = 1;
2069 $hash{'inbound'} = 0;
2070 if ( $svcdb eq 'svc_phone' ) {
2071 if ( $part_pkg->plan eq 'voip_inbound' ) {
2072 $hash{'outbound'} = 0;
2073 $hash{'inbound'} = 1;
2074 } elsif ( $part_pkg->option('selfservice_inbound_format')
2075 or $conf->config('selfservice-default_inbound_cdr_format')
2077 $hash{'inbound'} = 1;
2080 foreach (qw(inbound outbound)) {
2081 # hmm...we can't filter by status here, because there might
2082 # not be cdr_terminations at all. have to go by date.
2083 # find all since the last bill date.
2084 # XXX cdr types? we are going to need them.
2086 my $sum_cdr = $svc_x->sum_cdrs(
2087 'inbound' => ( $_ eq 'inbound' ? 1 : 0 ),
2088 'begin' => ($cust_pkg->last_bill || 0),
2090 'disable_charged_party' => 1,
2092 $hash{$_} = $sum_cdr->hashref;
2095 } # not hiding usage
2099 } # foreach $cust_svc
2101 foreach my $field (keys %bytes_used_total) {
2102 if ($field =~ /bytes/) {
2103 $bytes_used_total{$field} = display_bytecount($bytes_used_total{$field});
2108 'svcnum' => $session->{'svcnum'},
2109 'custnum' => $custnum,
2110 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
2111 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
2113 'bytes_used_total' => \%bytes_used_total,
2115 map { $usage_pools{$_} }
2119 'hide_usage' => $hide_usage,
2124 sub _customer_svc_x {
2125 my($custnum, $svcnum, $table) = (shift, shift, shift);
2126 my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
2128 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2129 my $search = " AND custnum = $1";
2130 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2133 'table' => ($table || 'svc_acct'),
2134 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2135 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
2136 #'LEFT JOIN cust_main USING ( custnum ) ',
2137 'hashref' => $hashref,
2138 'extra_sql' => $search, #important
2143 sub svc_status_html {
2146 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2147 return { 'error' => $session } if $context eq 'error';
2149 #XXX only svc_dsl for now
2150 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
2151 or return { 'error' => "Service not found" };
2153 my $html = $svc_x->getstatus_html;
2155 return { 'html' => $html };
2159 sub svc_status_hash {
2162 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2163 return { 'error' => $session } if $context eq 'error';
2165 #XXX only svc_acct for now
2166 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
2167 or return { 'error' => "Service not found" };
2169 my ( $html, $hashref ) = $svc_x->export_getstatus;
2174 sub set_svc_status_hash { _svc_method_X(shift, 'export_setstatus') }
2175 sub set_svc_status_listadd { _svc_method_X(shift, 'export_setstatus_listadd') }
2176 sub set_svc_status_listdel { _svc_method_X(shift, 'export_setstatus_listdel') }
2177 sub set_svc_status_vacationadd { _svc_method_X(shift, 'export_setstatus_vacationadd') }
2178 sub set_svc_status_vacationdel { _svc_method_X(shift, 'export_setstatus_vacationdel') }
2181 my( $p, $method ) = @_;
2183 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2184 return { 'error' => $session } if $context eq 'error';
2186 #XXX only svc_acct for now
2187 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
2188 or return { 'error' => "Service not found" };
2190 warn "$method ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
2192 my $error = $svc_x->$method($p); #$p? returns error?
2193 return { 'error' => $error } if $error;
2195 return {}; #? { 'error' => '' }
2199 sub acct_forward_info {
2202 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2203 return { 'error' => $session } if $context eq 'error';
2205 my $svc_forward = _customer_svc_x( $custnum,
2206 { 'srcsvc' => $p->{'svcnum'} },
2209 or return { 'error' => '',
2213 return { 'error' => '',
2214 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
2219 sub process_acct_forward {
2221 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2222 return { 'error' => $session } if $context eq 'error';
2224 my $old = _customer_svc_x( $custnum,
2225 { 'srcsvc' => $p->{'svcnum'} },
2229 if ( $p->{'dst'} eq '' ) {
2231 my $error = $old->delete;
2232 return { 'error' => $error };
2234 return { 'error' => '' };
2237 my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
2238 'dst' => $p->{'dst'},
2243 $new->svcnum($old->svcnum);
2244 my $cust_svc = $old->cust_svc;
2245 $new->svcpart($old->svcpart);
2246 $new->pkgnuym($old->pkgnum);
2247 $error = $new->replace($old);
2249 my $conf = new FS::Conf;
2250 $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
2252 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
2253 or return { 'error' => 'No service' }; #how would we even get here?
2255 $new->pkgnum( $svc_acct->cust_svc->pkgnum );
2257 $error = $new->insert;
2260 return { 'error' => $error };
2264 sub list_dsl_devices {
2267 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2268 return { 'error' => $session } if $context eq 'error';
2270 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2271 or return { 'error' => "Service not found" };
2274 'devices' => [ map {
2275 +{ 'mac_addr' => $_->mac_addr };
2276 } $svc_dsl->dsl_device
2282 sub add_dsl_device {
2285 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2286 return { 'error' => $session } if $context eq 'error';
2288 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2289 or return { 'error' => "Service not found" };
2291 return { 'error' => 'No MAC address supplied' }
2292 unless length($p->{'mac_addr'});
2294 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
2295 'mac_addr' => scalar($p->{'mac_addr'}),
2297 my $error = $dsl_device->insert;
2298 return { 'error' => $error };
2302 sub delete_dsl_device {
2305 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2306 return { 'error' => $session } if $context eq 'error';
2308 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2309 or return { 'error' => "Service not found" };
2311 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
2312 'mac_addr' => scalar($p->{'mac_addr'}),
2315 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
2317 my $error = $dsl_device->delete;
2318 return { 'error' => $error };
2324 _usage_details( \&_port_graph, $p,
2325 'svcdb' => 'svc_port',
2330 my($svc_port, $begin, $end) = @_;
2332 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
2333 push @usage, { 'png' => $pngOrError };
2337 sub _list_svc_usage {
2338 my($svc_acct, $begin, $end) = @_;
2340 foreach my $part_export (
2341 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
2342 qw( sqlradius sqlradius_withdomain )
2344 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
2349 sub list_svc_usage {
2350 _usage_details(\&_list_svc_usage, @_);
2353 sub _list_support_usage {
2354 my($svc_acct, $begin, $end) = @_;
2356 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
2357 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
2359 push @usage, { 'seconds' => $_->seconds,
2360 'support' => $_->support,
2361 '_date' => $_->_date,
2362 'id' => $_->transaction_id,
2363 'creator' => $_->creator,
2364 'subject' => $_->subject,
2365 'status' => $_->status,
2366 'ticketid' => $_->ticketid,
2372 sub list_support_usage {
2373 _usage_details(\&_list_support_usage, @_);
2376 sub _list_cdr_usage {
2377 # XXX CDR type support...
2378 # XXX any way to do a paged search on this?
2379 # we have to return the results all at once...
2380 my($svc_x, $begin, $end, %opt) = @_;
2381 map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
2385 'disable_charged_party' => 1,
2390 sub list_cdr_usage {
2392 _usage_details( \&_list_cdr_usage, $p );
2395 sub _usage_details {
2396 my($callback, $p, %opt) = @_;
2397 my $conf = FS::Conf->new;
2399 if ( $conf->exists('selfservice_hide-usage') ) {
2400 return { 'error' => 'Viewing usage is not allowed.' };
2403 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2404 return { 'error' => $session } if $context eq 'error';
2406 my $search = { 'svcnum' => $p->{'svcnum'} };
2407 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2409 my $cust_svc = qsearchs( 'cust_svc', $search );
2410 return { 'error' => 'No service selected in list_svc_usage' }
2413 my $svc_x = $cust_svc->svc_x;
2414 my $svcdb = $svc_x->table;
2415 my $cust_pkg = $cust_svc->cust_pkg;
2416 my $freq = $cust_pkg->part_pkg->freq;
2419 if ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
2421 if ( $p->{inbound} ) {
2422 $format = $cust_pkg->part_pkg->option('selfservice_inbound_format')
2423 || $conf->config('selfservice-default_inbound_cdr_format')
2424 || 'source_default';
2425 $callback_opt{inbound} = 1;
2427 $format = $cust_pkg->part_pkg->option('selfservice_format')
2428 || $conf->config('selfservice-default_cdr_format')
2432 $callback_opt{format} = $format;
2433 $callback_opt{use_clid} = 1;
2434 $header = [ split(',', FS::cdr::invoice_header($format) ) ];
2437 my $start = $cust_pkg->setup;
2438 #my $end = $cust_pkg->bill; # or time?
2441 unless ( $p->{beginning} ) {
2442 $p->{beginning} = $cust_pkg->last_bill;
2443 $p->{ending} = $end;
2446 die "illegal beginning" if $p->{beginning} !~ /^\d*$/;
2447 die "illegal ending" if $p->{ending} !~ /^\d*$/;
2449 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
2453 if ( $conf->exists('selfservice-hide_cdr_price') ) {
2454 # ugly kludge, I know
2455 my ($delete_col) = grep { $header->[$_] eq 'Price' } (0..scalar(@$header));
2456 if (defined $delete_col) {
2457 delete($_->[$delete_col]) foreach ($header, @usage);
2461 #kinda false laziness with FS::cust_main::bill, but perhaps
2462 #we should really change this bit to DateTime and DateTime::Duration
2464 #change this bit to use Date::Manip? CAREFUL with timezones (see
2465 # mailing list archive)
2466 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
2467 (localtime($p->{ending}) )[0,1,2,3,4,5];
2468 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
2469 (localtime($p->{beginning}) )[0,1,2,3,4,5];
2471 if ( $freq =~ /^\d+$/ ) {
2473 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
2475 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
2476 } elsif ( $freq =~ /^(\d+)w$/ ) {
2478 $nmday += $weeks * 7;
2479 $pmday -= $weeks * 7;
2480 } elsif ( $freq =~ /^(\d+)d$/ ) {
2484 } elsif ( $freq =~ /^(\d+)h$/ ) {
2489 return { 'error' => "unparsable frequency: ". $freq };
2492 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
2493 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
2497 'svcnum' => $p->{svcnum},
2498 'beginning' => $p->{beginning},
2499 'ending' => $p->{ending},
2500 'inbound' => $p->{inbound},
2501 'previous' => ($previous > $start) ? $previous : $start,
2502 'next' => ($next < $end) ? $next : $end,
2503 'header' => $header,
2511 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2512 return { 'error' => $session } if $context eq 'error';
2514 my $search = { 'custnum' => $custnum };
2515 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2516 my $cust_main = qsearchs('cust_main', $search )
2517 or return { 'error' => "unknown custnum $custnum" };
2519 my $status = $cust_main->status;
2521 my %order_pkg_options = ();
2522 if ( $p->{locationnum} > 0 ) {
2523 $order_pkg_options{locationnum} = delete($p->{locationnum});
2524 } elsif ( $p->{address1} ) {
2525 $order_pkg_options{'cust_location'} = new FS::cust_location {
2526 map { $_ => $p->{$_} }
2527 qw( address1 address2 city county state zip country )
2531 #false laziness w/ClientAPI/Signup.pm
2533 my $cust_pkg = new FS::cust_pkg ( {
2534 'custnum' => $custnum,
2535 'pkgpart' => $p->{'pkgpart'},
2536 'quantity' => $p->{'quantity'} || 1,
2538 my $error = $cust_pkg->check;
2539 return { 'error' => $error } if $error;
2542 unless ( $p->{'svcpart'} eq 'none' ) {
2546 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
2548 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2549 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
2550 $svcdb = $part_svc->svcdb;
2552 $svcdb = 'svc_acct';
2554 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
2557 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
2558 'svc_domain' => [ qw( domain ) ],
2559 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
2560 'svc_external' => [ qw( id title ) ],
2561 'svc_pbx' => [ qw( id title ) ],
2564 my $svc_x = "FS::$svcdb"->new( {
2565 'svcpart' => $svcpart,
2566 map { $_ => $p->{$_} } @{$fields{$svcdb}}
2569 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
2572 while ( length($p->{"snarf_machine$snarfnum"}) ) {
2573 my $acct_snarf = new FS::acct_snarf ( {
2574 'machine' => $p->{"snarf_machine$snarfnum"},
2575 'protocol' => $p->{"snarf_protocol$snarfnum"},
2576 'username' => $p->{"snarf_username$snarfnum"},
2577 '_password' => $p->{"snarf_password$snarfnum"},
2580 push @acct_snarf, $acct_snarf;
2582 $svc_x->child_objects( \@acct_snarf );
2585 my $y = $svc_x->setdefault; # arguably should be in new method
2586 return { 'error' => $y } if $y && !ref($y);
2588 $error = $svc_x->check;
2589 return { 'error' => $error } if $error;
2595 $error = $cust_main->order_pkg(
2596 'cust_pkg' => $cust_pkg,
2601 return { 'error' => $error } if $error;
2603 my $conf = new FS::Conf;
2604 if ( $conf->exists('signup_server-realtime') ) {
2606 my $bill_error = _do_bop_realtime( $cust_main, $status, 'collect'=>$p->{run_bill_events} );
2609 $cust_pkg->cancel('quiet'=>1);
2612 $cust_pkg->reexport;
2616 $cust_pkg->reexport;
2619 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
2621 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
2628 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2629 return { 'error' => $session } if $context eq 'error';
2631 my $conf = new FS::Conf;
2632 my $immutable = $conf->exists('selfservice_immutable-package');
2633 return { 'error' => "Package modification disabled" } if $immutable;
2635 my $search = { 'custnum' => $custnum };
2636 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2637 my $cust_main = qsearchs('cust_main', $search )
2638 or return { 'error' => "unknown custnum $custnum" };
2640 my $status = $cust_main->status;
2641 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
2642 or return { 'error' => "unknown package $p->{pkgnum}" };
2644 #if someone does need self-service package change of suspended packages,
2645 # figure out how to be more discriminating
2646 return { error=>"Can't change a suspended package", pkgnum=>$cust_pkg->pkgnum}
2647 if $cust_pkg->status eq 'suspended';
2649 my $err_or_cust_pkg = $cust_pkg->change( 'pkgpart' => $p->{'pkgpart'},
2650 'quantity' => $p->{'quantity'} || 1,
2653 my $new_pkg = qsearchs('part_pkg', { 'pkgpart' => $p->{pkgpart} } )
2654 or return { 'error' => "unknown package $p->{pkgpart}" };
2656 return { error=>$err_or_cust_pkg, pkgnum=>$cust_pkg->pkgnum }
2657 unless ref($err_or_cust_pkg);
2660 if ( $conf->exists('signup_server-realtime') ) {
2662 my $bill_error = _do_bop_realtime( $cust_main, $status, 'no_invoice_void'=>1 );
2665 $err_or_cust_pkg->suspend;
2668 $err_or_cust_pkg->reexport;
2672 $err_or_cust_pkg->reexport;
2675 return { error => '', pkg => $new_pkg->pkg, pkgnum => $err_or_cust_pkg->pkgnum };
2679 sub order_recharge {
2682 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2683 return { 'error' => $session } if $context eq 'error';
2685 my $search = { 'custnum' => $custnum };
2686 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2687 my $cust_main = qsearchs('cust_main', $search )
2688 or return { 'error' => "unknown custnum $custnum" };
2690 my $status = $cust_main->status;
2691 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
2692 or return { 'error' => "unknown service " . $p->{'svcnum'} };
2694 my $svc_x = $cust_svc->svc_x;
2695 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
2698 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
2699 qw ( recharge_seconds recharge_upbytes recharge_downbytes
2700 recharge_totalbytes );
2701 my $amount = $part_pkg->option('recharge_amount', 1);
2703 my ($l, $v, $d) = $cust_svc->label; # blah
2704 my $pkg = "Recharge $v";
2706 my $bill_error = $cust_main->charge($amount, $pkg,
2707 "time: $vhash{seconds}, up: $vhash{upbytes}," .
2708 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
2709 $part_pkg->taxclass); #meh
2711 my $conf = new FS::Conf;
2712 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
2714 $bill_error = _do_bop_realtime( $cust_main, $status );
2719 my $error = $svc_x->recharge (\%vhash);
2720 return { 'error' => $error } if $error;
2724 my $error = $bill_error;
2725 $error ||= $svc_x->recharge (\%vhash);
2726 return { 'error' => $error } if $error;
2729 return { error => '', svc => $cust_svc->part_svc->svc };
2733 sub _do_bop_realtime {
2734 my ($cust_main, $status, %opt) = @_;
2736 my $old_balance = $cust_main->balance;
2739 my $bill_error = $cust_main->bill(
2740 'return_bill' => \@cust_bill,
2743 $bill_error ||= $cust_main->apply_payments_and_credits;
2745 $bill_error ||= $cust_main->realtime_collect('selfservice' => 1);
2747 if ( $cust_main->balance > $old_balance
2748 && $cust_main->balance > 0
2749 && ( $cust_main->has_cust_payby_auto || $status eq 'suspended' )
2752 unless ( $opt{'no_invoice_void'} ) {
2754 #this used to apply a credit, but now we can void invoices...
2755 foreach my $cust_bill (@cust_bill) {
2756 my $voiderror = $cust_bill->void('automatic payment failed');
2757 warn "Error voiding cust bill after decline: $voiderror" if $voiderror;
2762 return { 'error' => '_decline', 'bill_error' => $bill_error };
2765 if ( $opt{'collect'} ) {
2766 my $collect_error = $cust_main->collect();
2767 return { 'error' => '_decline', 'bill_error' => $collect_error }
2768 if $collect_error; #?
2777 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2778 return { 'error' => $session } if $context eq 'error';
2780 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2781 or return { 'error' => "unknown custnum $custnum" };
2783 my @cust_pkg = sort { $a->bill <=> $b->bill }
2784 grep { $_->part_pkg->freq ne '0' }
2785 $cust_main->ncancelled_pkgs;
2787 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
2789 my $total = $cust_main->balance;
2792 my $bill = $_->bill;
2793 $total += $_->part_pkg->base_recur($_, \$bill);
2794 my $renew_date = $_->part_pkg->add_freq($_->bill);
2796 'pkgnum' => $_->pkgnum,
2797 'amount' => sprintf('%.2f', $total),
2798 'bill_date' => $_->bill,
2799 'bill_date_pretty' => time2str('%x', $_->bill),
2800 'renew_date' => $renew_date,
2801 'renew_date_pretty' => time2str('%x', $renew_date),
2802 'expire_date' => $_->expire,
2803 'expire_date_pretty' => time2str('%x', $_->expire),
2808 return { 'dates' => \@array };
2812 sub payment_info_renew_info {
2814 my $renew_info = renew_info($p);
2815 my $payment_info = payment_info($p);
2816 return { %$renew_info,
2824 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2825 return { 'error' => $session } if $context eq 'error';
2827 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2828 or return { 'error' => "unknown custnum $custnum" };
2830 my $date = $p->{'date'};
2834 #freeside-daily -n -d $date fs_daily $custnum
2835 $cust_main->bill_and_collect( 'time' => $date,
2836 'invoice_time' => $now,
2837 'actual_time' => $now,
2838 'check_freq' => '1d',
2841 return { 'error' => '' };
2847 my $session = _cache->get($p->{'session_id'})
2848 or return { 'error' => "Can't resume session" }; #better error message
2850 my $custnum = $session->{'custnum'};
2852 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2853 or return { 'error' => "unknown custnum $custnum" };
2855 my $conf = new FS::Conf;
2857 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2858 or return { 'error' => 'Permission denied' };
2860 my $pkgnum = $p->{'pkgnum'};
2862 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2863 'pkgnum' => $pkgnum, } )
2864 or return { 'error' => "unknown pkgnum $pkgnum" };
2866 my $error = $cust_pkg->suspend(reason => $reasonnum);
2867 return { 'error' => $error };
2873 my $session = _cache->get($p->{'session_id'})
2874 or return { 'error' => "Can't resume session" }; #better error message
2876 my $custnum = $session->{'custnum'};
2877 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2878 or return { 'error' => "unknown custnum $custnum" };
2880 my $pkgnum = $p->{'pkgnum'};
2881 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2882 'pkgnum' => $pkgnum, } )
2883 or return { 'error' => "unknown pkgnum $pkgnum" };
2885 my $error = $cust_pkg->cancel( 'quiet' => 1,
2886 'date' => $p->{'date'},
2888 return { 'error' => $error };
2891 sub provision_phone {
2894 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2896 #editing an existing phone number
2897 if ( $p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/ ) {
2898 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2899 return { 'error' => $session } if $context eq 'error';
2901 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2902 return { 'error' => 'service not found' } unless $svc_phone;
2903 return { 'error' => 'invalid svcnum' }
2904 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2906 $svc_phone->email($p->{'email'})
2907 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2908 $svc_phone->forwarddst($p->{'forwarddst'})
2909 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2910 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2911 return { 'error' => $svc_phone->replace };
2915 unless ( $p->{'lnp'} ) {
2916 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2917 $p->{'lnp_status'} = "portingin";
2918 return _provision( 'FS::svc_phone',
2919 [qw(lnp_desired_due_date lnp_other_provider
2920 lnp_other_provider_account phonenum countrycode lnp_status)],
2921 [qw(phonenum countrycode)],
2927 # single DID order (the usual case)
2928 unless (scalar(@bulkdid)) {
2929 return _provision( 'FS::svc_phone',
2930 [qw(phonenum countrycode)],
2931 [qw(phonenum countrycode)],
2937 # bulk DID order case
2939 foreach my $did ( @bulkdid ) {
2940 $did =~ s/[^0-9]//g;
2941 $error = _provision( 'FS::svc_phone',
2942 [qw(phonenum countrycode)],
2943 [qw(phonenum countrycode)],
2945 'pkgnum' => $p->{'pkgnum'},
2946 'svcpart' => $p->{'svcpart'},
2948 'countrycode' => $p->{'countrycode'},
2949 'session_id' => $p->{'session_id'},
2952 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2954 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2959 warn "provision_pbx called\n"
2962 warn "provision_pbx calling _provision\n"
2964 _provision( 'FS::svc_pbx',
2965 [qw(id title max_extensions max_simultaneous ip_addr)],
2966 [qw(id title max_extensions max_simultaneous ip_addr)],
2972 sub provision_acct {
2974 warn "provision_acct called\n"
2977 return { 'error' => gettext('passwords_dont_match') }
2978 if $p->{'_password'} ne $p->{'_password2'};
2979 return { 'error' => gettext('empty_password') }
2980 unless length($p->{'_password'});
2982 if ($p->{'domsvc'}) {
2983 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
2984 qw ( svcpart pkgnum ) );
2985 return { 'error' => gettext('invalid_domain') }
2986 unless ($domains{$p->{'domsvc'}});
2989 warn "provision_acct calling _provision\n"
2991 _provision( 'FS::svc_acct',
2992 [qw(username _password domsvc)],
2993 [qw(username _password domsvc)],
2999 sub provision_external {
3001 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
3002 _provision( 'FS::svc_external',
3010 sub provision_forward {
3012 _provision( 'FS::svc_forward',
3013 ['srcsvc','src','dstsvc','dst'],
3020 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
3021 warn "_provision called for $class\n"
3024 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3025 return { 'error' => $session } if $context eq 'error';
3027 my $search = { 'custnum' => $custnum };
3028 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3029 my $cust_main = qsearchs('cust_main', $search )
3030 or return { 'error' => "unknown custnum $custnum" };
3032 my $pkgnum = $p->{'pkgnum'};
3034 warn "searching for custnum $custnum pkgnum $pkgnum\n"
3036 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3037 'pkgnum' => $pkgnum,
3039 or return { 'error' => "unknown pkgnum $pkgnum" };
3041 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
3043 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
3044 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
3046 return { error=> 'svcpart '. $p->{'svcpart'}. " is not a $class definition" }
3047 if $class ne 'FS::'. $part_svc->svcdb;
3049 warn "creating $class record\n"
3051 my $svc_x = $class->new( {
3052 'pkgnum' => $p->{'pkgnum'},
3053 'svcpart' => $p->{'svcpart'},
3054 map { $_ => $p->{$_} } @$fields
3057 my %insert_args = ();
3058 #i shouldn't be a special case here (pass an option or something)
3059 if ( $class eq 'FS::svc_phone'
3060 && grep length($p->{$_}), @location_editable_fields
3063 $insert_args{'cust_location'} = new FS::cust_location {
3064 map { $_ => $p->{$_} } @location_editable_fields
3068 warn "inserting $class record\n"
3070 my $error = $svc_x->insert(%insert_args);
3073 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
3075 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
3078 my $return = { 'svc' => $part_svc->svc,
3080 map { $_ => $svc_x->get($_) } @$return_fields
3082 warn "_provision returning ". Dumper($return). "\n"
3091 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3092 return { 'error' => $session } if $context eq 'error';
3094 my $search = { 'custnum' => $custnum };
3095 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3096 my $cust_main = qsearchs('cust_main', $search )
3097 or return { 'error' => "unknown custnum $custnum" };
3099 my $pkgnum = $p->{'pkgnum'};
3101 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3102 'pkgnum' => $pkgnum,
3104 or return { 'error' => "unknown pkgnum $pkgnum" };
3106 my $svcpart = $p->{'svcpart'};
3108 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
3109 'svcpart' => $svcpart, } )
3110 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
3111 my $part_svc = $pkg_svc->part_svc;
3113 my $conf = new FS::Conf;
3116 'svc' => $part_svc->svc,
3117 'svcdb' => $part_svc->svcdb,
3118 'pkgnum' => $pkgnum,
3119 'svcpart' => $svcpart,
3120 'custnum' => $custnum,
3122 'security_phrase' => 0, #XXX !
3123 'svc_acct_pop' => [], #XXX !
3125 'init_popstate' => '',
3130 small_custview( $cust_main, $conf->config('countrydefault') ),
3134 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
3135 && $ret->{'svcdb'} eq 'svc_phone') {
3136 $ret->{'svcnum'} = $p->{'svcnum'};
3137 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
3138 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
3139 $ret->{'email'} = $svc_phone->email;
3140 $ret->{'forwarddst'} = $svc_phone->forwarddst;
3144 if ($ret->{'svcdb'} eq 'svc_forward') {
3145 $ret->{'forward_emails'} = {$cust_pkg->forward_emails()};
3151 sub unprovision_svc {
3154 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3155 return { 'error' => $session } if $context eq 'error';
3157 my $search = { 'custnum' => $custnum };
3158 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3159 my $cust_main = qsearchs('cust_main', $search )
3160 or return { 'error' => "unknown custnum $custnum" };
3162 my $svcnum = $p->{'svcnum'};
3164 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
3165 or return { 'error' => "unknown svcnum $svcnum" };
3167 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
3168 unless $cust_svc->cust_pkg->custnum == $custnum;
3170 my $conf = new FS::Conf;
3172 return { 'svc' => $cust_svc->part_svc->svc,
3173 'error' => $cust_svc->cancel,
3175 small_custview( $cust_main, $conf->config('countrydefault') ),
3180 sub myaccount_passwd {
3182 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3183 return { 'error' => $session } if $context eq 'error';
3185 return { 'error' => "New passwords don't match." }
3186 if $p->{'new_password'} ne $p->{'new_password2'};
3188 return { 'error' => 'Enter new password' }
3189 unless length($p->{'new_password'});
3191 #my $search = { 'custnum' => $custnum };
3192 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3193 $custnum =~ /^(\d+)$/ or die "illegal custnum";
3194 my $search = " AND custnum = $1";
3195 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
3197 my $svc_acct = qsearchs( {
3198 'table' => 'svc_acct',
3199 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
3200 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
3201 'LEFT JOIN cust_main USING ( custnum ) ',
3202 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
3203 'extra_sql' => $search, #important
3205 or return { 'error' => "Service not found" };
3209 my $conf = new FS::Conf;
3211 return { 'error' => 'Incorrect current password.' }
3212 if ( exists($p->{'old_password'})
3213 || $conf->exists('selfservice-password_change_oldpass')
3215 && ! $svc_acct->check_password($p->{'old_password'});
3217 $error ||= $svc_acct->is_password_allowed($p->{'new_password'})
3218 || $svc_acct->set_password($p->{'new_password'})
3219 || $svc_acct->replace();
3221 #regular pw change in self-service should change contact pw too, otherwise its
3222 #way too confusing. hell its confusing they're separate at all, but alas.
3223 #need to support the "ISP provides email that's used as a contact email" case
3225 my $contact = FS::contact->by_selfservice_email($svc_acct->email);
3226 if ( $contact && qsearchs('cust_contact', { contactnum=> $contact->contactnum,
3227 custnum => $custnum,
3228 selfservice_access => 'Y',
3232 #svc_acct was successful but this one returns an error? "shouldn't happen"
3233 #don't recheck is_password_allowed here; if the svc_acct password was
3234 #legal, that's good enough
3235 $error ||= $contact->change_password($p->{'new_password'});
3238 my($label, $value) = $svc_acct->cust_svc->label;
3240 return { 'error' => $error,
3250 my $info = skin_info($p);
3252 my $conf = new FS::Conf;
3253 my $verification = $conf->config('selfservice-password_reset_verification')
3254 or return { %$info, 'error' => 'Password resets disabled' };
3259 if ( $p->{'email'} ) { #new-style, changes contact and svc_acct
3261 $contact = FS::contact->by_selfservice_email($p->{'email'});
3264 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
3265 $cust_main = $cust_contact[0]->cust_main if scalar(@cust_contact) == 1;
3268 #also look for an svc_acct, otherwise it would be super confusing
3270 my($username, $domain) = split('@', $p->{'email'});
3271 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } );
3272 if ( $svc_domain ) {
3273 $svc_acct = qsearchs('svc_acct', { 'username' => $username,
3274 'domsvc' => $svc_domain->svcnum }
3277 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
3278 $cust_main ||= $cust_pkg->cust_main if $cust_pkg;
3280 #precaution: don't change svc_acct password not part of the same
3281 # customer as contact
3282 $svc_acct = '' if ! $cust_pkg
3283 || $cust_pkg->custnum != $cust_main->custnum;
3288 return { %$info, 'error' => 'Email address not found' }
3289 unless $contact || $svc_acct;
3291 } elsif ( $p->{'username'} ) { #old style, looks in svc_acct only
3293 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
3294 or return { %$info, 'error' => 'Account not found' };
3296 $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
3297 'domsvc' => $svc_domain->svcnum }
3299 or return { %$info, 'error' => 'Account not found' };
3301 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
3302 or return { %$info, 'error' => 'Account not found' };
3304 $cust_main = $cust_pkg->cust_main;
3308 return { %$info, 'error' => 'Multi-customer contacts incompatible with customer-based verification' }
3309 if ! $cust_main && $verification ne 'email';
3312 'email' => sub { 1; },
3314 my( $p, $cust_main ) = @_;
3315 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
3316 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
3319 my( $p, $cust_main ) = @_;
3320 my $cust_pay = qsearchs({
3321 'table' => 'cust_pay',
3322 'hashref' => { 'custnum' => $cust_main->custnum },
3323 'order_by' => 'ORDER BY _date DESC LIMIT 1',
3327 $p->{'amount'} == $cust_pay->paid;
3330 my( $p, $cust_main ) = @_;
3331 $p->{'zip'} eq $cust_main->zip
3332 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
3336 foreach my $verify ( split(',', $verification) ) {
3338 &{ $verify{$verify} }( $p, $cust_main )
3339 or return { %$info, 'error' => 'Account not found' };
3343 #okay, we're verified
3347 my $error = $contact->send_reset_email(
3348 'svcnum' => ($svc_acct ? $svc_acct->svcnum : ''),
3352 return { %$info, 'error' => $error }; #????
3355 } elsif ( $svc_acct ) {
3357 #create a unique session
3359 my $reset_session = {
3360 'svcnum' => $svc_acct->svcnum,
3361 'agentnum' => $svc_acct->cust_main->agentnum,
3364 my $timeout = '1 hour'; #?
3366 my $reset_session_id;
3368 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
3369 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") );
3372 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
3376 my $msgnum = $conf->config('selfservice-password_reset_msgnum',
3377 $cust_main->agentnum);
3378 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
3379 return { %$info, 'error' => "selfservice-password_reset_msgnum unset" }
3381 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
3382 my $error = $msg_template->send( 'cust_main' => $cust_main,
3383 'object' => $svc_acct,
3384 'substitutions' => {
3385 'session_id' => $reset_session_id,
3389 return { %$info, 'error' => $error }; #????
3394 return { %$info, 'error' => '' };
3397 sub check_reset_passwd {
3400 my $conf = new FS::Conf;
3401 my $verification = $conf->config('selfservice-password_reset_verification')
3402 or return { 'error' => 'Password resets disabled' };
3404 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3405 or return { 'error' => "Can't resume session" }; #better error message
3407 if ( $reset_session->{'svcnum'} ) {
3409 my $svcnum = $reset_session->{'svcnum'};
3411 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3412 or return { 'error' => "Service not found" };
3414 $p->{'agentnum'} = $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3415 my $info = skin_info($p);
3419 'session_id' => $p->{'session_id'},
3420 'username' => $svc_acct->username,
3423 } elsif ( $reset_session->{'contactnum'} ) {
3425 my $contactnum = $reset_session->{'contactnum'};
3427 my $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3428 or return { 'error' => "Contact not found" };
3430 my @contact_email = $contact->contact_email;
3431 return { 'error' => 'No contact email' } unless @contact_email;
3433 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
3434 $p->{'agentnum'} = $cust_contact[0]->cust_main->agentnum
3435 if scalar(@cust_contact) == 1;
3436 my $info = skin_info($p);
3440 'session_id' => $p->{'session_id'},
3441 'email' => $contact_email[0]->email, #the first?
3446 return { 'error' => 'No svcnum or contactnum in session' }; #??
3452 sub process_reset_passwd {
3455 my $conf = new FS::Conf;
3456 my $verification = $conf->config('selfservice-password_reset_verification')
3457 or return { 'error' => 'Password resets disabled' };
3459 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3460 or return { 'error' => "Can't resume session" }; #better error message
3465 if ( $reset_session->{'svcnum'} ) {
3467 my $svcnum = $reset_session->{'svcnum'};
3469 $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3470 or return { 'error' => "Service not found" };
3472 $p->{'agentnum'} ||= $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3473 $info ||= skin_info($p);
3478 if ( $reset_session->{'contactnum'} ) {
3480 my $contactnum = $reset_session->{'contactnum'};
3482 $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3483 or return { 'error' => "Contact not found" };
3485 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
3486 $p->{'agentnum'} = $cust_contact[0]->cust_main->agentnum
3487 if scalar(@cust_contact) == 1;
3488 $info ||= skin_info($p);
3492 return { %$info, 'error' => "New passwords don't match." }
3493 if $p->{'new_password'} ne $p->{'new_password2'};
3495 return { %$info, 'error' => 'Enter new password' }
3496 unless length($p->{'new_password'});
3500 my $error ||= $svc_acct->is_password_allowed($p->{'new_password'})
3501 || $svc_acct->set_password($p->{'new_password'})
3502 || $svc_acct->replace();
3504 return { %$info, 'error' => $error } if $error;
3506 #my($label, $value) = $svc_acct->cust_svc->label;
3507 #return { 'error' => $error,
3508 # #'label' => $label,
3509 # #'value' => $value,
3516 my $error = $contact->is_password_allowed($p->{'new_password'})
3517 || $contact->change_password($p->{'new_password'});
3519 return { %$info, 'error' => $error }; # if $error;
3523 #password changed ,so remove session, don't want it reused
3524 _cache->remove($p->{'session_id'});
3526 return { %$info, 'error' => '' };
3530 sub validate_passwd {
3534 %result = ( 'fieldid' => $p->{'fieldid'} )
3535 if $p->{'fieldid'} =~ /^\w+$/;
3537 return { %result, 'password_invalid' => 'Enter new password' }
3538 unless length($p->{'check_password'});
3541 if ($p->{'svcnum'}) {
3542 # false laziness with myaccount_passwd
3543 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3544 return { %result, 'error' => $session } if $context eq 'error';
3546 $custnum =~ /^(\d+)$/ or die "illegal custnum";
3547 my $search = " AND custnum = $1";
3548 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
3550 $svc_acct = qsearchs( {
3551 'table' => 'svc_acct',
3552 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
3553 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
3554 'LEFT JOIN cust_main USING ( custnum ) ',
3555 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
3556 'extra_sql' => $search, #important
3558 or return { %result, 'error' => "Service not found" };
3559 # end false laziness
3562 $svc_acct ||= new FS::svc_acct {};
3564 my $error = $svc_acct->is_password_allowed($p->{'check_password'});
3565 return { %result, 'password_invalid' => $error } if $error;
3566 return { %result, 'password_valid' => 1 };
3571 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3572 return { 'error' => $session } if $context eq 'error';
3575 if ( $session->{'pkgnum'} ) {
3577 #tickets for specific service with pkg-balances on
3578 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3579 'pkgnum' => $session->{'pkgnum'} })
3580 or return { 'error' => 'unknown package' };
3581 foreach my $cust_svc ( $cust_pkg->cust_svc ) {
3582 push @tickets, $cust_svc->tickets( $p->{status} );
3587 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
3588 or return { 'error' => "unknown custnum $custnum" };
3590 @tickets = $cust_main->tickets( $p->{status} );
3593 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
3594 if ( $FS::TicketSystem::system && FS::TicketSystem->selfservice_priority ) {
3596 @tickets = grep { $_->{'_selfservice_priority'}
3597 !~ /^\s*(closed?|resolved?|done)\s*/i }
3600 my $conf = new FS::Conf;
3601 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
3605 ($a->{'_selfservice_priority'} eq '') <=>
3606 ($b->{'_selfservice_priority'} eq '')
3609 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
3615 +{ tickets => \@tickets };
3622 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3623 return { 'error' => $session } if $context eq 'error';
3625 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
3626 FS::TicketSystem->init();
3628 my $conf = new FS::Conf;
3629 my $queue = $p->{'queue'}
3630 || $conf->config('ticket_system-selfservice_queueid')
3631 || $conf->config('ticket_system-default_queueid');
3633 warn "$me create_ticket: creating ticket\n" if $DEBUG;
3634 my $err_or_ticket = FS::TicketSystem->create_ticket(
3635 '', #create RT session based on FS CurrentUser (fs_selfservice)
3637 'custnum' => $custnum,
3638 'svcnum' => $session->{'svcnum'},
3639 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
3642 if ( ref($err_or_ticket) ) {
3643 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
3645 return { 'error' => '',
3646 'ticket_id' => $err_or_ticket->id,
3649 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
3651 return { 'error' => $err_or_ticket };
3659 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3660 return { 'error' => $session } if $context eq 'error';
3662 return { error => 'requested format not implemented' }
3663 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
3665 my $conf = new FS::Conf;
3666 my $age_threshold = 0;
3667 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
3668 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
3670 my $search = { 'custnum' => $custnum };
3671 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3672 my $cust_main = qsearchs('cust_main', $search )
3673 or return { 'error' => "unknown custnum $custnum" };
3675 # does it make more sense to just run one sql query for this instead of all the
3676 # insanity below? would increase performance greately for large data sets?
3678 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
3679 my @part_svc = $cust_pkg->part_svc;
3680 foreach my $part_svc ( @part_svc ) {
3681 if($part_svc->svcdb eq 'svc_phone'){
3682 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
3683 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
3684 push @svc_phone, $cust_pkg_svc->svc_x
3685 if $cust_pkg_svc->date_inserted >= $age_threshold;
3693 my($xls_r,$xls_c) = (0,0);
3696 my @fields = qw( countrycode phonenum pin sip_password phone_name );
3697 if($p->{'format'} eq 'csv') {
3698 $csv = new Text::CSV_XS { 'always_quote' => 1,
3701 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
3702 $content .= $csv->string;
3704 elsif($p->{'format'} eq 'xls') {
3705 my $XLS1 = new IO::Scalar \$content;
3706 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
3707 or return { 'error' => "Error opening .xls file: $!" };
3708 $xls = $xls_workbook->add_worksheet('DIDs');
3709 foreach ( @fields ) {
3710 $xls->write(0,$xls_c++,$_);
3715 foreach my $svc_phone ( @svc_phone ) {
3716 my @cols = map { $svc_phone->$_ } @fields;
3717 if($p->{'format'} eq 'csv') {
3718 return { 'error' => 'Unable to create CSV' }
3719 unless $csv->combine(@cols);
3720 $content .= $csv->string;
3722 elsif($p->{'format'} eq 'xls') {
3725 $xls->write($xls_r,$xls_c++,$_);
3731 $xls_workbook->close() if $p->{'format'} eq 'xls';
3733 { content => $content, format => $p->{'format'}, };
3738 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3739 return { 'error' => $session } if $context eq 'error';
3741 # warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
3742 # FS::TicketSystem->init();
3743 # return { 'error' => 'get_ticket configuration error' }
3744 # if $FS::TicketSystem::system ne 'RT_Internal';
3746 # check existence and ownership as part of this
3747 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
3748 my $rt_session = FS::TicketSystem->session('');
3749 my $Ticket = FS::TicketSystem->get_ticket_object(
3751 ticket_id => $p->{'ticket_id'},
3754 return { 'error' => 'ticket not found' } if !$Ticket;
3756 if ( length( $p->{'subject'} || '' ) ) {
3758 if ( $p->{'subject'} ne $Ticket->Subject ) {
3759 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
3760 return { 'error' => "unable to set subject: $msg" } if !$val;
3764 if(length($p->{'reply'})) {
3765 my @err_or_res = FS::TicketSystem->correspond_ticket(
3767 'ticket_id' => $p->{'ticket_id'},
3768 'content' => $p->{'reply'},
3771 return { 'error' => 'unable to reply to ticket' }
3772 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
3775 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
3776 my $err_or_ticket = FS::TicketSystem->get_ticket(
3778 'ticket_id' => $p->{'ticket_id'},
3781 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
3782 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
3784 return { 'error' => $err_or_ticket };
3787 my @custs = @{$err_or_ticket->{'custs'}};
3788 my @txns = @{$err_or_ticket->{'txns'}};
3791 # superseded by check in get_ticket_object
3792 #return { 'error' => 'invalid ticket requested' }
3793 #unless grep($_ eq $custnum, @custs);
3795 foreach my $txn ( @txns ) {
3796 push @filtered_txns, $txn
3797 if ($txn->{'type'} eq 'EmailRecord'
3798 || $txn->{'type'} eq 'Correspond'
3799 || $txn->{'type'} eq 'Create');
3802 warn "$me get_ticket: successful: \n"
3804 return { 'error' => '',
3805 'transactions' => \@filtered_txns,
3806 'ticket_fields' => $err_or_ticket->{'fields'},
3807 'ticket_id' => $p->{'ticket_id'},
3811 sub adjust_ticket_priority {
3813 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3814 return { 'error' => $session } if $context eq 'error';
3816 # warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
3817 # FS::TicketSystem->init;
3818 my $ss_priority = FS::TicketSystem->selfservice_priority;
3820 return { 'error' => 'adjust_ticket_priority configuration error' }
3821 if $FS::TicketSystem::system ne 'RT_Internal'
3824 my $values = $p->{'values'}; #hashref, id => priority value
3827 foreach my $id (keys %$values) {
3828 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
3829 my $Ticket = FS::TicketSystem->get_ticket_object('',
3831 'custnum' => $custnum,
3834 $ticket_error{$id} = 'ticket not found';
3838 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
3839 # We're not going to implement it for RT_External.
3840 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
3841 my $new_value = $values->{$id};
3842 next if $old_value eq $new_value;
3844 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
3846 # AddCustomFieldValue works fine (replacing any existing value) if it's
3847 # a single-valued custom field, which it should be. If it's not, you're
3848 # doing something wrong.
3850 if ( length($new_value) ) {
3851 ($val, $msg) = $Ticket->AddCustomFieldValue(
3852 Field => $ss_priority,
3853 Value => $new_value,
3857 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
3858 Field => $ss_priority,
3859 Value => $old_value,
3863 $ticket_error{$id} = $msg if !$val;
3864 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
3866 return { 'error' => '',
3867 'ticket_error' => \%ticket_error,
3868 %{ customer_info($p) } # send updated customer info back
3874 sub _custoragent_session_custnum {
3877 my($context, $session, $custnum);
3878 if ( $p->{'session_id'} ) {
3880 $context = 'customer';
3881 $session = _cache->get($p->{'session_id'})
3882 or return ( 'error' => "Can't resume session" ); #better error message
3883 $custnum = $session->{'custnum'};
3885 } elsif ( $p->{'agent_session_id'} ) {
3888 my $agent_cache = new FS::ClientAPI_SessionCache( {
3889 'namespace' => 'FS::ClientAPI::Agent',
3891 $session = $agent_cache->get($p->{'agent_session_id'})
3892 or return ( 'error' => "Can't resume session" ); #better error message
3893 $custnum = $p->{'custnum'};
3897 return ( 'error' => "Can't resume session" ); #better error message
3900 ($context, $session, $custnum);