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);
12 use Time::Local qw(timelocal_nocheck);
13 use Business::CreditCard;
16 use Spreadsheet::WriteExcel;
17 use OLE::Storage_Lite;
18 use FS::UI::Web::small_custview qw(small_custview); #less doh
20 use FS::UI::bytecount qw( display_bytecount );
23 use FS::Record qw(qsearch qsearchs dbh);
24 use FS::Msgcat qw(gettext);
25 use FS::Misc qw(card_types);
26 use FS::Misc::DateTime qw(parse_datetime);
28 use FS::ClientAPI_SessionCache;
40 use FS::legacy_cust_bill;
41 use FS::cust_main_county;
45 use FS::acct_rt_transaction;
49 $me = '[FS::ClientAPI::MyAccount]';
51 use vars qw( @cust_main_editable_fields );
52 @cust_main_editable_fields = qw(
53 first last company address1 address2 city
54 county state zip country
55 daytime night fax mobile
56 ship_first ship_last ship_company ship_address1 ship_address2 ship_city
57 ship_state ship_zip ship_country
58 ship_daytime ship_night ship_fax ship_mobile
60 payby payinfo payname paystart_month paystart_year payissue payip
61 ss paytype paystate stateid stateid_state
64 BEGIN { #preload to reduce time customer_info takes
65 if ( $FS::TicketSystem::system ) {
66 warn "$me: initializing ticket system\n" if $DEBUG;
67 FS::TicketSystem->init();
72 $cache ||= new FS::ClientAPI_SessionCache( {
73 'namespace' => 'FS::ClientAPI::MyAccount',
80 my($context, $session, $custnum) = _custoragent_session_custnum($p);
81 #return { 'error' => $session } if $context eq 'error';
84 if ( $context eq 'customer' ) {
86 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
89 $sth->execute($custnum) or die $sth->errstr;
91 $agentnum = $sth->fetchrow_arrayref->[0]
92 or die "no agentnum for custnum $custnum";
94 #} elsif ( $context eq 'agent' ) {
95 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
99 my $conf = new FS::Conf;
101 #false laziness w/Signup.pm
103 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
105 if ( $skin_info_cache_agent ) {
107 warn "$me loading cached skin info for agentnum $agentnum\n"
112 warn "$me populating skin info cache for agentnum $agentnum\n"
115 $skin_info_cache_agent = {
116 'agentnum' => $agentnum,
117 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
118 qw( company_name date_format ) ),
119 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
120 qw( body_bgcolor box_bgcolor stripe1_bgcolor stripe2_bgcolor
121 text_color link_color vlink_color hlink_color alink_color
122 font title_color title_align title_size menu_bgcolor menu_fontsize
125 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
126 qw( menu_skipblanks menu_skipheadings menu_nounderline no_logo )
128 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
129 qw( title_left_image title_right_image
130 menu_top_image menu_body_image menu_bottom_image
133 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
134 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
135 qw( head body_header body_footer company_address ) ),
138 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
142 #{ %$skin_info_cache_agent };
143 $skin_info_cache_agent;
150 my $conf = new FS::Conf;
154 'phone_login' => $conf->exists('selfservice_server-phone_login'),
155 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
156 'banner_url' => scalar($conf->config('selfservice-login_banner_url')),
157 'banner_image_md5' =>
158 md5_hex($conf->config_binary('selfservice-login_banner_image')),
165 sub login_banner_image {
167 my $conf = new FS::Conf;
168 my $image = $conf->config_binary('selfservice-login_banner_image');
170 'md5' => md5_hex($image),
175 #false laziness w/FS::ClientAPI::passwd::passwd
179 my $conf = new FS::Conf;
182 if ( $p->{'domain'} eq 'svc_phone'
183 && $conf->exists('selfservice_server-phone_login') ) {
185 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
186 return { error => 'Number not found.' } unless $svc_phone;
189 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
190 #return { error => 'Only primary user may log in.' }
191 # if $conf->exists('selfservice_server-primary_only')
192 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
194 return { error => 'Incorrect PIN.' }
195 unless $svc_phone->check_pin($p->{'password'});
201 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
202 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
204 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
205 'domsvc' => $svc_domain->svcnum, }
207 return { error => 'User not found.' } unless $svc_acct;
209 if($conf->exists('selfservice_server-login_svcpart')) {
210 my @svcpart = $conf->config('selfservice_server-login_svcpart');
211 my $svcpart = $svc_acct->cust_svc->svcpart;
212 return { error => 'Invalid user.' }
213 unless grep($_ eq $svcpart, @svcpart);
216 return { error => 'Incorrect password.' }
217 unless $svc_acct->check_password($p->{'password'});
224 'svcnum' => $svc_x->svcnum,
227 my $cust_svc = $svc_x->cust_svc;
228 my $cust_pkg = $cust_svc->cust_pkg;
230 my $cust_main = $cust_pkg->cust_main;
231 $session->{'custnum'} = $cust_main->custnum;
232 if ( $conf->exists('pkg-balances') ) {
233 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
234 $cust_main->ncancelled_pkgs;
235 $session->{'pkgnum'} = $cust_pkg->pkgnum
236 if scalar(@cust_pkg) > 1;
240 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
241 #return { error => 'Only primary user may log in.' }
242 # if $conf->exists('selfservice_server-primary_only')
243 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
244 my $part_pkg = $cust_pkg->part_pkg;
245 return { error => 'Only primary user may log in.' }
246 if $conf->exists('selfservice_server-primary_only')
247 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
251 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
252 } until ( ! defined _cache->get($session_id) ); #just in case
254 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
255 _cache->set( $session_id, $session, $timeout );
257 return { 'error' => '',
258 'session_id' => $session_id,
264 if ( $p->{'session_id'} ) {
265 _cache->remove($p->{'session_id'});
266 return { %{ skin_info($p) }, 'error' => '' };
268 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
275 my($context, $session, $custnum) = _custoragent_session_custnum($p);
276 return { 'error' => $session } if $context eq 'error';
278 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
279 or return { 'error' => "Service not found" };
281 $session->{'svcnum'} = $svc_acct->svcnum;
283 my $conf = new FS::Conf;
284 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
285 _cache->set( $p->{'session_id'}, $session, $timeout );
287 return { 'error' => '' };
291 sub payment_gateway {
293 # takes a cust_main and a cust_payby entry, returns the payment_gateway
294 my $conf = new FS::Conf;
295 my $cust_main = shift;
296 my $cust_payby = shift;
297 my $gatewaynum = $conf->config('selfservice-payment_gateway');
299 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
300 die "configured gatewaynum $gatewaynum not found!" if !$pg;
304 return '' if ! FS::payby->realtime($cust_payby);
305 my $pg = $cust_main->agent->payment_gateway(
306 'method' => FS::payby->payby2bop($cust_payby),
316 my $conf = new FS::Conf;
318 my $info = skin_info($p);
320 use vars qw( $cust_paybys ); #cache for performance
321 unless ( $cust_paybys ) {
323 my %cust_paybys = map { $_ => 1 }
324 map { FS::payby->payby2payment($_) }
325 $conf->config('signup_server-payby');
327 $cust_paybys = [ keys %cust_paybys ];
330 $info->{'cust_paybys'} = $cust_paybys;
332 my($context, $session, $custnum) = _custoragent_session_custnum($p);
333 return { 'error' => $session } if $context eq 'error';
335 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
336 or return { 'error' => "unknown custnum $custnum" };
338 $info->{'hide_payment_fields'} = [
340 my $pg = payment_gateway($cust_main, $_);
341 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
342 } @{ $info->{cust_paybys} }
345 $info->{'self_suspend_reason'} =
346 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
348 $info->{'edit_ticket_subject'} =
349 $conf->exists('ticket_system-selfservice_edit_subject') &&
350 $cust_main->edit_subject;
353 'custnum' => $custnum,
354 'access_pkgnum' => $session->{'pkgnum'},
355 'access_svcnum' => $session->{'svcnum'},
362 my($context, $session, $custnum) = _custoragent_session_custnum($p);
363 return { 'error' => $session } if $context eq 'error';
367 my $conf = new FS::Conf;
368 if ($conf->exists('cust_main-require_address2')) {
369 $return{'require_address2'} = '1';
371 $return{'require_address2'} = '';
374 if ( $FS::TicketSystem::system ) {
375 warn "$me customer_info: initializing ticket system\n" if $DEBUG;
376 FS::TicketSystem->init();
379 if ( $custnum ) { #customer record
381 my $search = { 'custnum' => $custnum };
382 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
383 my $cust_main = qsearchs('cust_main', $search )
384 or return { 'error' => "unknown custnum $custnum" };
386 $return{display_custnum} = $cust_main->display_custnum;
388 if ( $session->{'pkgnum'} ) {
389 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
390 #next_bill_date from cust_pkg?
392 $return{balance} = $cust_main->balance;
393 $return{next_bill_date} = $cust_main->next_bill_date;
394 $return{next_bill_date_pretty} =
395 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
399 my @tickets = $cust_main->tickets;
400 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
401 if ( $FS::TicketSystem::system && FS::TicketSystem->selfservice_priority ) {
402 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
406 ($a->{'_selfservice_priority'} eq '') <=>
407 ($b->{'_selfservice_priority'} eq '')
410 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
416 $return{tickets} = \@tickets;
419 unless ( $session->{'pkgnum'} ) {
422 invnum => $_->invnum,
423 date => time2str("%b %o, %Y", $_->_date),
426 } $cust_main->open_cust_bill;
427 $return{open_invoices} = \@open;
429 my $sql = 'SELECT MAX(_date) FROM cust_bill WHERE custnum = ?';
430 my $sth = dbh->prepare($sql) or die dbh->errstr;
431 $sth->execute($custnum) or die $sth->errstr;
432 $return{'last_invoice_date'} = $sth->fetchrow_arrayref->[0];
433 $return{'last_invoice_date_pretty'} =
434 time2str('%m/%d/%Y', $return{'last_invoice_date'} );
437 $return{countrydefault} = scalar($conf->config('countrydefault'));
439 $return{small_custview} =
440 small_custview( $cust_main,
441 $return{countrydefault},
442 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
445 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
446 $return{ship_name} = $cust_main->ship_first. ' '. $cust_main->get('ship_last');
448 $return{has_ship_address} = $cust_main->has_ship_address;
449 $return{status} = $cust_main->status;
450 $return{statuscolor} = $cust_main->statuscolor;
452 for (@cust_main_editable_fields) {
453 $return{$_} = $cust_main->get($_);
456 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
457 $return{payinfo} = $cust_main->paymask;
458 @return{'month', 'year'} = $cust_main->paydate_monthyear;
461 $return{'invoicing_list'} =
462 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
463 $return{'postal_invoicing'} =
464 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
466 if (scalar($conf->config('support_packages'))) {
467 my @support_services = ();
468 foreach ($cust_main->support_services) {
469 my $seconds = $_->svc_x->seconds;
470 my $time_remaining = (($seconds < 0) ? '-' : '' ).
471 int(abs($seconds)/3600)."h".
472 sprintf("%02d",(abs($seconds)%3600)/60)."m";
473 my $cust_pkg = $_->cust_pkg;
476 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
477 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
478 push @support_services, { svcnum => $_->svcnum,
479 time => $time_remaining,
484 $return{support_services} = \@support_services;
487 if ( $conf->config('prepayment_discounts-credit_type') ) {
489 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
492 if ( $session->{'svcnum'} ) {
493 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
494 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
495 $return{'svcnum'} = $session->{'svcnum'};
498 } elsif ( $session->{'svcnum'} ) { #no customer record
500 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
501 or die "unknown svcnum";
502 $return{name} = $svc_acct->email;
506 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
510 return { 'error' => '',
511 'custnum' => $custnum,
517 sub customer_info_short {
520 my($context, $session, $custnum) = _custoragent_session_custnum($p);
521 return { 'error' => $session } if $context eq 'error';
525 my $conf = new FS::Conf;
527 if ( $custnum ) { #customer record
529 my $search = { 'custnum' => $custnum };
530 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
531 my $cust_main = qsearchs('cust_main', $search )
532 or return { 'error' => "unknown custnum $custnum" };
534 $return{display_custnum} = $cust_main->display_custnum;
536 $return{countrydefault} = scalar($conf->config('countrydefault'));
538 $return{small_custview} =
539 small_custview( $cust_main,
540 $return{countrydefault},
544 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
545 $return{ship_name} = $cust_main->ship_first. ' '. $cust_main->get('ship_last');
547 $return{payby} = $cust_main->payby;
549 #none of these are terribly expensive if we want 'em...
550 for (@cust_main_editable_fields) {
551 $return{$_} = $cust_main->get($_);
554 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
555 $return{payinfo} = $cust_main->paymask;
556 @return{'month', 'year'} = $cust_main->paydate_monthyear;
559 $return{'invoicing_list'} =
560 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
561 #$return{'postal_invoicing'} =
562 # 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
564 if ( $session->{'svcnum'} ) {
565 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
566 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
567 $return{'svcnum'} = $session->{'svcnum'};
570 } elsif ( $session->{'svcnum'} ) { #no customer record
572 #uuh, not supproted yet... die?
573 return { 'error' => 'customer_info_short not yet supported as agent' };
577 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
581 return { 'error' => '',
582 'custnum' => $custnum,
587 sub billing_history {
590 my($context, $session, $custnum) = _custoragent_session_custnum($p);
591 return { 'error' => $session } if $context eq 'error';
593 return { 'error' => 'No customer' } unless $custnum;
595 my $search = { 'custnum' => $custnum };
596 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
597 my $cust_main = qsearchs('cust_main', $search )
598 or return { 'error' => "unknown custnum $custnum" };
602 if ( $session->{'pkgnum'} ) {
603 #$return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
604 #next_bill_date from cust_pkg?
605 return { 'error' => 'No history for package' };
608 $return{balance} = $cust_main->balance;
609 $return{next_bill_date} = $cust_main->next_bill_date;
610 $return{next_bill_date_pretty} =
611 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
616 my $conf = new FS::Conf;
618 if ( $conf->exists('selfservice-billing_history-line_items') ) {
620 foreach my $cust_bill ( $cust_main->cust_bill ) {
623 'type' => 'Line item',
624 'description' => $_->desc. ( $_->sdate && $_->edate
625 ? ' '. time2str('%d-%b-%Y', $_->sdate).
626 ' To '. time2str('%d-%b-%Y', $_->edate)
629 'amount' => sprintf('%.2f', $_->setup + $_->recur ),
630 'date' => $cust_bill->_date,
631 'date_pretty' => time2str('%m/%d/%Y', $cust_bill->_date ),
633 foreach $cust_bill->cust_bill_pkg;
641 'description' => 'Invoice #'. $_->display_invnum,
642 'amount' => sprintf('%.2f', $_->charged ),
644 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
646 foreach $cust_main->cust_bill;
652 'description' => 'Payment', #XXX type
653 'amount' => sprintf('%.2f', 0 - $_->paid ),
655 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
657 foreach $cust_main->cust_pay;
661 'description' => 'Credit', #more info?
662 'amount' => sprintf('%.2f', 0 -$_->amount ),
664 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
666 foreach $cust_main->cust_credit;
670 'description' => 'Refund', #more info? type, like payment?
671 'amount' => $_->refund,
673 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
675 foreach $cust_main->cust_refund;
677 @history = sort { $b->{'date'} <=> $a->{'date'} } @history;
679 $return{'history'} = \@history;
687 my $session = _cache->get($p->{'session_id'})
688 or return { 'error' => "Can't resume session" }; #better error message
690 my $custnum = $session->{'custnum'}
691 or return { 'error' => "no customer record" };
693 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
694 or return { 'error' => "unknown custnum $custnum" };
696 my $new = new FS::cust_main { $cust_main->hash };
697 # Avoid accidentally changing the service address.
698 if ( !$new->has_ship_address ) {
699 $new->set( $_ => $new->get($_) )
700 foreach $new->addr_fields;
703 $new->set( $_ => $p->{$_} )
704 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
707 if (exists($p->{'payby'})) {
708 $p->{'payby'} =~ /^([A-Z]{4})$/
709 or return { 'error' => "illegal_payby " . $p->{'payby'} };
713 if ( $payby =~ /^(CARD|DCRD)$/ ) {
715 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
717 if ( $new->payinfo eq $cust_main->paymask ) {
718 $new->payinfo($cust_main->payinfo);
720 $new->payinfo($p->{'payinfo'});
723 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
725 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
728 $p->{'payinfo1'} =~ /^([\dx]+)$/
729 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
731 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
732 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
734 $payinfo = $payinfo1. '@'. $payinfo2;
736 $new->payinfo( ($payinfo eq $cust_main->paymask)
737 ? $cust_main->payinfo
741 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
743 } elsif ( $payby =~ /^(BILL)$/ ) {
745 } elsif ( $payby ) { #notyet ready
746 return { 'error' => "unknown payby $payby" };
750 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
751 #false laziness with httemplate/edit/process/cust_main.cgi
752 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
753 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
755 @invoicing_list = $cust_main->invoicing_list;
758 my $error = $new->replace($cust_main, \@invoicing_list);
759 return { 'error' => $error } if $error;
762 return { 'error' => '' };
767 my $session = _cache->get($p->{'session_id'})
768 or return { 'error' => "Can't resume session" }; #better error message
774 my $conf = new FS::Conf;
775 use vars qw($payment_info); #cache for performance
776 unless ( $payment_info ) {
778 my %states = map { $_->state => 1 }
779 qsearch('cust_main_county', {
780 'country' => $conf->config('countrydefault') || 'US'
783 my %cust_paybys = map { $_ => 1 }
784 map { FS::payby->payby2payment($_) }
785 $conf->config('signup_server-payby');
787 my @cust_paybys = keys %cust_paybys;
791 #list all counties/states/countries
792 'cust_main_county' =>
793 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
795 #shortcut for one-country folks
797 [ sort { $a cmp $b } keys %states ],
799 'card_types' => card_types(),
801 'paytypes' => [ @FS::cust_main::paytypes ],
803 'paybys' => [ $conf->config('signup_server-payby') ],
804 'cust_paybys' => \@cust_paybys,
806 'stateid_label' => FS::Msgcat::_gettext('stateid'),
807 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
809 'show_ss' => $conf->exists('show_ss'),
810 'show_stateid' => $conf->exists('show_stateid'),
811 'show_paystate' => $conf->exists('show_bankstate'),
813 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
815 'credit_card_surcharge_percentage' => $conf->config('credit-card-surcharge-percentage'),
824 my %return = %$payment_info;
826 my $custnum = $session->{'custnum'};
828 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
829 or return { 'error' => "unknown custnum $custnum" };
831 $return{'hide_payment_fields'} = [
833 my $pg = payment_gateway($cust_main, $_);
834 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
835 } @{ $return{cust_paybys} }
838 $return{balance} = $cust_main->balance; #XXX pkg-balances?
840 $return{payname} = $cust_main->payname
841 || ( $cust_main->first. ' '. $cust_main->get('last') );
843 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
845 $return{payby} = $cust_main->payby;
846 $return{stateid_state} = $cust_main->stateid_state;
848 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
849 $return{card_type} = cardtype($cust_main->payinfo);
850 $return{payinfo} = $cust_main->paymask;
852 @return{'month', 'year'} = $cust_main->paydate_monthyear;
856 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
857 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
858 $return{payinfo1} = $payinfo1;
859 $return{payinfo2} = $payinfo2;
860 $return{paytype} = $cust_main->paytype;
861 $return{paystate} = $cust_main->paystate;
862 $return{payname} = $cust_main->payname; # override 'first/last name' default from above, if any. Is instution-name here. (#15819)
865 if ( $conf->config('prepayment_discounts-credit_type') ) {
867 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
870 #doubleclick protection
872 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
874 return { 'error' => '',
880 #some false laziness with httemplate/process/payment.cgi - look there for
881 #ACH and CVV support stuff
883 sub validate_payment {
886 my $session = _cache->get($p->{'session_id'})
887 or return { 'error' => "Can't resume session" }; #better error message
889 my $custnum = $session->{'custnum'};
891 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
892 or return { 'error' => "unknown custnum $custnum" };
894 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
895 or return { 'error' => gettext('illegal_amount') };
897 return { error => 'Amount must be greater than 0' } unless $amount > 0;
899 #false laziness w/tr-amount_fee.html, but we don't want selfservice users
900 #changing the hidden form values
901 my $conf = new FS::Conf;
902 my $fee_display = $conf->config('selfservice_process-display') || 'add';
903 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
904 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
905 if ( $fee_display eq 'add'
907 and ! $fee_skip_first || scalar($cust_main->cust_pay)
910 my $fee_pkg = qsearchs('part_pkg', { pkgpart=>$fee_pkgpart } );
911 $amount = sprintf('%.2f', $amount + $fee_pkg->option('setup_fee') );
914 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
915 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
916 my $discount_term = $1;
918 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
919 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
922 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
923 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
926 $p->{'payby'} ||= 'CARD';
927 $p->{'payby'} =~ /^([A-Z]{4})$/
928 or return { 'error' => "illegal_payby " . $p->{'payby'} };
931 #false laziness w/process/payment.cgi
934 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
936 $p->{'payinfo1'} =~ /^([\dx]+)$/
937 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
939 $p->{'payinfo2'} =~ /^([\dx]+)$/
940 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
942 $payinfo = $payinfo1. '@'. $payinfo2;
944 $payinfo = $cust_main->payinfo
945 if $cust_main->paymask eq $payinfo;
947 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
949 $payinfo = $p->{'payinfo'};
951 #more intelligent matching will be needed here if you change
952 #card_masking_method and don't remove existing paymasks
953 $payinfo = $cust_main->payinfo
954 if $cust_main->paymask eq $payinfo;
957 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
958 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
962 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
963 return { 'error' => gettext('unknown_card_type') }
964 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
966 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
967 if ( cardtype($payinfo) eq 'American Express card' ) {
968 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
969 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
972 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
973 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
979 die "unknown payby $payby";
983 'CARD' => [ qw( paystart_month paystart_year payissue payip
984 address1 address2 city state zip country ) ],
985 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
989 $card_type = cardtype($payinfo) if $payby eq 'CARD';
992 'cust_main' => $cust_main, #XXX or just custnum??
995 'payinfo' => $payinfo,
996 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
997 'card_type' => $card_type,
998 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
999 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
1000 'month' => $p->{'month'},
1001 'year' => $p->{'year'},
1002 'payname' => $payname,
1003 'paybatch' => $paybatch, #this doesn't actually do anything
1004 'paycvv' => $paycvv,
1005 'payname' => $payname,
1006 'discount_term' => $discount_term,
1007 'pkgnum' => $session->{'pkgnum'},
1008 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
1018 my $validate = validate_payment($p);
1019 return $validate if $validate->{'error'};
1021 my $conf = new FS::Conf;
1022 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
1023 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
1025 +{ map { $_=>$validate->{$_} }
1026 qw( card_type paymask payname paydate_pretty month year amount
1027 address1 address2 city state zip country
1033 sub process_stored_payment {
1036 my $session_id = $p->{'session_id'};
1038 my $payment_info = _cache->get( "payment_$session_id" )
1039 or return { 'error' => "Can't resume session" }; #better error message
1041 do_process_payment($payment_info);
1045 sub process_payment {
1048 my $payment_info = validate_payment($p);
1049 return $payment_info if $payment_info->{'error'};
1051 do_process_payment($payment_info);
1055 sub do_process_payment {
1056 my $validate = shift;
1058 my $cust_main = $validate->{'cust_main'};
1060 my $amount = delete $validate->{'amount'};
1063 my $payby = delete $validate->{'payby'};
1065 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
1068 'paynum_ref' => \$paynum,
1071 return { 'error' => $error } if $error;
1073 #no error, so order the fee package if applicable...
1074 my $conf = new FS::Conf;
1075 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
1076 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
1078 if ( $fee_pkgpart and ! $fee_skip_first || scalar($cust_main->cust_pay) ) {
1080 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $fee_pkgpart };
1082 $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
1083 return { 'error' => "payment processed successfully, but error ordering fee: $error" }
1086 #and generate an invoice for it now too
1087 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1088 return { 'error' => "payment processed and fee ordered sucessfully, but error billing fee: $error" }
1093 $cust_main->apply_payments;
1095 if ( $validate->{'save'} ) {
1096 my $new = new FS::cust_main { $cust_main->hash };
1097 if ($payby eq 'CARD' || $payby eq 'DCRD') {
1098 $new->set( $_ => $validate->{$_} )
1099 foreach qw( payname paystart_month paystart_year payissue payip );
1100 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
1102 # Avoid accidentally changing the service address.
1103 if ( !$new->has_ship_address ) {
1104 $new->set( "ship_$_" => $new->get($_) )
1105 foreach $new->addr_fields;
1107 $new->set( $_ => $validate->{$_} )
1108 foreach qw(address1 address2 city state country zip);
1110 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
1111 $new->set( $_ => $validate->{$_} )
1112 foreach qw( payname payip paytype paystate
1113 stateid stateid_state );
1114 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
1116 $new->set( 'payinfo' => $cust_main->card_token || $validate->{'payinfo'} );
1117 $new->set( 'paydate' => $validate->{'paydate'} );
1118 my $error = $new->replace($cust_main);
1120 #no, this causes customers to process their payments again
1121 #return { 'error' => $error };
1122 #XXX just warn verosely for now so i can figure out how these happen in
1123 # the first place, eventually should redirect them to the "change
1124 #address" page but indicate the payment did process??
1125 delete($validate->{'payinfo'}); #don't want to log this!
1126 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
1127 "NEW: ". Dumper($new)."\n".
1128 "OLD: ". Dumper($cust_main)."\n".
1129 "PACKET: ". Dumper($validate)."\n";
1137 my $receipt_html = '';
1139 # currently supported for realtime CC only; send receipt data to SS
1140 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
1143 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
1146 <TD ALIGN="right">Payment#</TD>
1147 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
1151 <TD ALIGN="right">Date</TD>
1153 <TD BGCOLOR="#FFFFFF"><B>! .
1154 time2str("%a %b %o, %Y %r", $cust_pay->_date)
1160 <TD ALIGN="right">Amount</TD>
1161 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
1166 <TD ALIGN="right">Payment method</TD>
1167 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
1178 my($gw, $auth, $order) = split(':', $cust_pay->paybatch);
1182 'amount' => sprintf('%.2f', $cust_pay->paid),
1183 'date' => $cust_pay->_date,
1184 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
1185 'time_pretty' => time2str('%T', $cust_pay->_date),
1186 'auth_num' => $auth,
1187 'order_num' => $order,
1188 'receipt_html' => $receipt_html,
1195 'receipt_html' => '',
1202 sub realtime_collect {
1205 my $session = _cache->get($p->{'session_id'})
1206 or return { 'error' => "Can't resume session" }; #better error message
1208 my $custnum = $session->{'custnum'};
1210 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1211 or return { 'error' => "unknown custnum $custnum" };
1214 if ( $p->{'amount'} ) {
1215 $amount = $p->{'amount'};
1217 elsif ( $session->{'pkgnum'} ) {
1218 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1221 $amount = $cust_main->balance;
1224 my $error = $cust_main->realtime_collect(
1225 'method' => $p->{'method'},
1226 'amount' => $amount,
1227 'pkgnum' => $session->{'pkgnum'},
1228 'session_id' => $p->{'session_id'},
1232 return { 'error' => $error } unless ref( $error );
1234 return { 'error' => '', amount => $amount, %$error };
1237 sub process_payment_order_pkg {
1240 my $hr = process_payment($p);
1241 return $hr if $hr->{'error'};
1246 sub process_payment_order_renew {
1249 my $hr = process_payment($p);
1250 return $hr if $hr->{'error'};
1255 sub process_prepay {
1259 my $session = _cache->get($p->{'session_id'})
1260 or return { 'error' => "Can't resume session" }; #better error message
1264 my $custnum = $session->{'custnum'};
1266 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1267 or return { 'error' => "unknown custnum $custnum" };
1269 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1270 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1278 return { 'error' => $error } if $error;
1280 return { 'error' => '',
1281 'amount' => $amount,
1282 'seconds' => $seconds,
1283 'duration' => duration_exact($seconds),
1284 'upbytes' => $upbytes,
1285 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1286 'downbytes' => $downbytes,
1287 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1288 'totalbytes'=> $totalbytes,
1289 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1296 my $session = _cache->get($p->{'session_id'})
1297 or return { 'error' => "Can't resume session" }; #better error message
1299 my $custnum = $session->{'custnum'};
1301 my $invnum = $p->{'invnum'};
1303 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1304 'custnum' => $custnum } )
1305 or return { 'error' => "Can't find invnum" };
1309 return { 'error' => '',
1310 'invnum' => $invnum,
1311 'invoice_text' => join('', $cust_bill->print_text ),
1312 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1319 my $session = _cache->get($p->{'session_id'})
1320 or return { 'error' => "Can't resume session" }; #better error message
1322 my $custnum = $session->{'custnum'};
1324 my $invnum = $p->{'invnum'};
1326 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1327 'custnum' => $custnum } )
1328 or return { 'error' => "Can't find invnum" };
1332 return { 'error' => '',
1333 'invnum' => $invnum,
1334 'invoice_pdf' => $cust_bill->print_pdf({
1335 'unsquelch_cdr' => 1,
1336 'locale' => $p->{'locale'},
1342 sub legacy_invoice {
1344 my $session = _cache->get($p->{'session_id'})
1345 or return { 'error' => "Can't resume session" }; #better error message
1347 my $custnum = $session->{'custnum'};
1349 my $legacyinvnum = $p->{'legacyinvnum'};
1352 'legacyinvnum' => $legacyinvnum,
1353 'custnum' => $custnum,
1356 my $legacy_cust_bill =
1357 qsearchs('legacy_cust_bill', { %hash, 'locale' => $p->{'locale'} } )
1358 || qsearchs('legacy_cust_bill', \%hash )
1359 or return { 'error' => "Can't find legacyinvnum" };
1363 return { 'error' => '',
1364 'legacyinvnum' => $legacyinvnum,
1365 'legacyid' => $legacy_cust_bill->legacyid,
1366 'invoice_html' => $legacy_cust_bill->content_html,
1371 sub legacy_invoice_pdf {
1373 my $session = _cache->get($p->{'session_id'})
1374 or return { 'error' => "Can't resume session" }; #better error message
1376 my $custnum = $session->{'custnum'};
1378 my $legacyinvnum = $p->{'legacyinvnum'};
1380 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1381 'legacyinvnum' => $legacyinvnum,
1382 'custnum' => $custnum,
1383 }) or return { 'error' => "Can't find legacyinvnum" };
1387 return { 'error' => '',
1388 'legacyinvnum' => $legacyinvnum,
1389 'legacyid' => $legacy_cust_bill->legacyid,
1390 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1398 #sessioning for this? how do we get the session id to the backend invoice
1399 # template so it can add it to the link, blah
1402 if ( $p->{'invnum'} ) {
1403 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1404 or return { 'error' => 'unknown invnum' };
1405 $agentnum = $cust_bill->cust_main->agentnum;
1408 my $templatename = $p->{'template'} || $p->{'templatename'};
1410 #false laziness-ish w/view/cust_bill-logo.cgi
1412 my $conf = new FS::Conf;
1413 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1414 $templatename = "_$1";
1419 my $filename = "logo$templatename.png";
1421 return { 'error' => '',
1422 'logo' => $conf->config_binary($filename, $agentnum),
1423 'content_type' => 'image/png', #should allow gif, jpg too
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 $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1436 or return { 'error' => "unknown custnum $custnum" };
1438 my $conf = new FS::Conf;
1440 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1442 my @cust_bill = grep ! $_->hide, $cust_main->cust_bill;
1446 return { 'error' => '',
1447 'balance' => $cust_main->balance,
1450 my $owed = $_->owed;
1452 +{ 'invnum' => $_->invnum,
1453 '_date' => $_->_date,
1454 'date' => time2str("%b %o, %Y", $_->_date),
1455 'date_short' => time2str("%m-%d-%Y", $_->_date),
1456 'previous' => sprintf('%.2f', ($_->previous)[0]),
1457 'charged' => sprintf('%.2f', $_->charged),
1458 'owed' => sprintf('%.2f', $owed),
1459 'balance' => sprintf('%.2f', $balance),
1464 'legacy_invoices' => [
1466 +{ 'legacyinvnum' => $_->legacyinvnum,
1467 'legacyid' => $_->legacyid,
1468 '_date' => $_->_date,
1469 'date' => time2str("%b %o, %Y", $_->_date),
1470 'date_short' => time2str("%m-%d-%Y", $_->_date),
1471 'charged' => sprintf('%.2f', $_->charged),
1472 'has_content' => ( length($_->content_pdf)
1473 || length($_->content_html) ),
1483 my $session = _cache->get($p->{'session_id'})
1484 or return { 'error' => "Can't resume session" }; #better error message
1486 my $custnum = $session->{'custnum'};
1488 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1489 or return { 'error' => "unknown custnum $custnum" };
1491 my @errors = $cust_main->cancel( 'quiet'=>1 );
1493 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1495 return { 'error' => $error };
1502 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1503 return { 'error' => $session } if $context eq 'error';
1505 my $search = { 'custnum' => $custnum };
1506 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1507 my $cust_main = qsearchs('cust_main', $search )
1508 or return { 'error' => "unknown custnum $custnum" };
1510 my $conf = new FS::Conf;
1512 # the duplication below is necessary:
1513 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1514 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1515 # non-backwards-compatible change breaking the software of anyone using the API
1516 # instead of the stock selfservice
1517 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1518 if( $conf->exists('selfservice_server-view-wholesale') ) {
1519 return { 'svcnum' => $session->{'svcnum'},
1520 'custnum' => $custnum,
1521 'cust_pkg' => [ map {
1523 part_pkg => [ map $_->hashref, $_->part_pkg ],
1525 [ map $_->hashref, $_->available_part_svc ],
1527 [ map { my $ref = { $_->hash,
1528 label => [ $_->label ],
1530 $ref->{_password} = $_->svc_x->_password
1531 if $context eq 'agent'
1532 && $conf->exists('agent-showpasswords')
1533 && $_->part_svc->svcdb eq 'svc_acct';
1538 } $cust_main->cust_pkg
1541 small_custview( $cust_main, $conf->config('countrydefault') ),
1542 'wholesale_view' => 1,
1543 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1544 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1545 'lnp' => $conf->exists('svc_phone-lnp'),
1549 { 'svcnum' => $session->{'svcnum'},
1550 'custnum' => $custnum,
1551 'cust_pkg' => [ map {
1552 my $primary_cust_svc = $_->primary_cust_svc;
1555 pkg_label => $_->pkg_label,
1556 status => $_->status,
1558 [ map { $_->hashref }
1559 grep { $_->selfservice_access ne 'hidden' }
1560 $_->available_part_svc
1563 [ map { my $ref = { $_->hash,
1564 label => [ $_->label ],
1566 $ref->{_password} = $_->svc_x->_password
1567 if $context eq 'agent'
1568 && $conf->exists('agent-showpasswords')
1569 && $_->part_svc->svcdb eq 'svc_acct';
1570 $ref->{svchash} = { $_->svc_x->hash } if
1571 $_->part_svc->svcdb eq 'svc_phone';
1572 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1573 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1576 grep { $_->part_svc->selfservice_access ne 'hidden' }
1581 ? { $primary_cust_svc->hash,
1582 label => [ $primary_cust_svc->label ],
1583 finger => $primary_cust_svc->svc_x->finger, #uuh
1584 $primary_cust_svc->part_svc->hash,
1588 } $cust_main->ncancelled_pkgs
1591 small_custview( $cust_main, $conf->config('countrydefault') ),
1599 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1600 return { 'error' => $session } if $context eq 'error';
1602 my $search = { 'custnum' => $custnum };
1603 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1604 my $cust_main = qsearchs('cust_main', $search )
1605 or return { 'error' => "unknown custnum $custnum" };
1607 my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
1608 if ( ! $pkgnum && $p->{'svcnum'} ) {
1609 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
1610 $pkgnum = $cust_svc->pkgnum if $cust_svc;
1614 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1615 foreach my $cust_pkg ( $p->{'ncancelled'}
1616 ? $cust_main->ncancelled_pkgs
1617 : $cust_main->unsuspended_pkgs ) {
1618 next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
1619 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1622 @cust_svc = grep { $_->part_svc->selfservice_access ne 'hidden' } @cust_svc;
1624 if ( $p->{'svcdb'} ) {
1625 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1627 : ref($p->{'svcdb'}) eq 'ARRAY'
1628 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1629 : { $p->{'svcdb'} => 1 };
1630 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1633 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1636 my $conf = new FS::Conf;
1639 'svcnum' => $session->{'svcnum'},
1640 'custnum' => $custnum,
1641 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1642 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1645 my $svc_x = $_->svc_x;
1646 my($label, $value) = $_->label;
1647 my $part_svc = $_->part_svc;
1648 my $svcdb = $part_svc->svcdb;
1649 my $cust_pkg = $_->cust_pkg;
1650 my $part_pkg = $cust_pkg->part_pkg;
1653 'svcnum' => $_->svcnum,
1654 'display_svcnum' => $_->display_svcnum,
1658 'pkg_label' => $cust_pkg->pkg_label,
1659 'pkg_status' => $cust_pkg->status,
1660 'readonly' => ($part_svc->selfservice_access eq 'readonly'),
1663 if ( $svcdb eq 'svc_acct' ) {
1666 'username' => $svc_x->username,
1667 'email' => $svc_x->email,
1668 'finger' => $svc_x->finger,
1669 'seconds' => $svc_x->seconds,
1670 'upbytes' => display_bytecount($svc_x->upbytes),
1671 'downbytes' => display_bytecount($svc_x->downbytes),
1672 'totalbytes' => display_bytecount($svc_x->totalbytes),
1674 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1675 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1676 'recharge_upbytes' =>
1677 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1678 'recharge_downbytes' =>
1679 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1680 'recharge_totalbytes' =>
1681 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1685 } elsif ( $svcdb eq 'svc_dsl' ) {
1686 $hash{'phonenum'} = $svc_x->phonenum;
1687 if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
1688 $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
1689 $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
1692 $hash{'name'} = $cust_main->name;
1695 # elsif ( $svcdb eq 'svc_phone' || $svcdb eq 'svc_port' ) {
1709 sub _customer_svc_x {
1710 my($custnum, $svcnum, $table) = (shift, shift, shift);
1711 my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
1713 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1714 my $search = " AND custnum = $1";
1715 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1718 'table' => ($table || 'svc_acct'),
1719 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1720 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
1721 #'LEFT JOIN cust_main USING ( custnum ) ',
1722 'hashref' => $hashref,
1723 'extra_sql' => $search, #important
1728 sub svc_status_html {
1731 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1732 return { 'error' => $session } if $context eq 'error';
1734 #XXX only svc_dsl for now
1735 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
1736 or return { 'error' => "Service not found" };
1738 my $html = $svc_x->getstatus_html;
1740 return { 'html' => $html };
1744 sub svc_status_hash {
1747 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1748 return { 'error' => $session } if $context eq 'error';
1750 #XXX only svc_acct for now
1751 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1752 or return { 'error' => "Service not found" };
1754 my ( $html, $hashref ) = $svc_x->export_getstatus;
1759 sub set_svc_status_hash {
1762 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1763 return { 'error' => $session } if $context eq 'error';
1765 #XXX only svc_acct for now
1766 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1767 or return { 'error' => "Service not found" };
1769 warn "set_svc_status_hash ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
1771 my $error = $svc_x->export_setstatus($p); #$p? returns error?
1772 return { 'error' => $error } if $error;
1774 return {}; #? { 'error' => '' }
1778 sub set_svc_status_listadd {
1781 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1782 return { 'error' => $session } if $context eq 'error';
1784 #XXX only svc_acct for now
1785 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1786 or return { 'error' => "Service not found" };
1788 warn "set_svc_status_listadd ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
1790 my $error = $svc_x->export_setstatus_listadd($p); #$p? returns error?
1791 return { 'error' => $error } if $error;
1793 return {}; #? { 'error' => '' }
1797 sub set_svc_status_listdel {
1800 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1801 return { 'error' => $session } if $context eq 'error';
1803 #XXX only svc_acct for now
1804 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1805 or return { 'error' => "Service not found" };
1807 warn "set_svc_status_listdel ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
1809 my $error = $svc_x->export_setstatus_listdel($p); #$p? returns error?
1810 return { 'error' => $error } if $error;
1812 return {}; #? { 'error' => '' }
1817 sub acct_forward_info {
1820 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1821 return { 'error' => $session } if $context eq 'error';
1823 my $svc_forward = _customer_svc_x( $custnum,
1824 { 'srcsvc' => $p->{'svcnum'} },
1827 or return { 'error' => '',
1831 return { 'error' => '',
1832 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
1837 sub process_acct_forward {
1839 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1840 return { 'error' => $session } if $context eq 'error';
1842 my $old = _customer_svc_x( $custnum,
1843 { 'srcsvc' => $p->{'svcnum'} },
1847 if ( $p->{'dst'} eq '' ) {
1849 my $error = $old->delete;
1850 return { 'error' => $error };
1852 return { 'error' => '' };
1855 my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
1856 'dst' => $p->{'dst'},
1861 $new->svcnum($old->svcnum);
1862 my $cust_svc = $old->cust_svc;
1863 $new->svcpart($old->svcpart);
1864 $new->pkgnuym($old->pkgnum);
1865 $error = $new->replace($old);
1867 my $conf = new FS::Conf;
1868 $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
1870 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
1871 or return { 'error' => 'No service' }; #how would we even get here?
1873 $new->pkgnum( $svc_acct->cust_svc->pkgnum );
1875 $error = $new->insert;
1878 return { 'error' => $error };
1882 sub list_dsl_devices {
1885 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1886 return { 'error' => $session } if $context eq 'error';
1888 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1889 or return { 'error' => "Service not found" };
1892 'devices' => [ map {
1893 +{ 'mac_addr' => $_->mac_addr };
1894 } $svc_dsl->dsl_device
1900 sub add_dsl_device {
1903 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1904 return { 'error' => $session } if $context eq 'error';
1906 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1907 or return { 'error' => "Service not found" };
1909 return { 'error' => 'No MAC address supplied' }
1910 unless length($p->{'mac_addr'});
1912 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
1913 'mac_addr' => scalar($p->{'mac_addr'}),
1915 my $error = $dsl_device->insert;
1916 return { 'error' => $error };
1920 sub delete_dsl_device {
1923 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1924 return { 'error' => $session } if $context eq 'error';
1926 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1927 or return { 'error' => "Service not found" };
1929 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
1930 'mac_addr' => scalar($p->{'mac_addr'}),
1933 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
1935 my $error = $dsl_device->delete;
1936 return { 'error' => $error };
1942 _usage_details( \&_port_graph, $p,
1943 'svcdb' => 'svc_port',
1948 my($svc_port, $begin, $end) = @_;
1950 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
1951 push @usage, { 'png' => $pngOrError };
1955 sub _list_svc_usage {
1956 my($svc_acct, $begin, $end) = @_;
1958 foreach my $part_export (
1959 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1960 qw( sqlradius sqlradius_withdomain )
1962 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1967 sub list_svc_usage {
1968 _usage_details(\&_list_svc_usage, @_);
1971 sub _list_support_usage {
1972 my($svc_acct, $begin, $end) = @_;
1974 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1975 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1977 push @usage, { 'seconds' => $_->seconds,
1978 'support' => $_->support,
1979 '_date' => $_->_date,
1980 'id' => $_->transaction_id,
1981 'creator' => $_->creator,
1982 'subject' => $_->subject,
1983 'status' => $_->status,
1984 'ticketid' => $_->ticketid,
1990 sub list_support_usage {
1991 _usage_details(\&_list_support_usage, @_);
1994 sub _list_cdr_usage {
1995 # XXX CDR type support...
1996 my($svc_phone, $begin, $end, %opt) = @_;
1997 map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
1998 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
2001 sub list_cdr_usage {
2003 _usage_details( \&_list_cdr_usage, $p,
2004 'svcdb' => 'svc_phone',
2008 sub _usage_details {
2009 my($callback, $p, %opt) = @_;
2011 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2012 return { 'error' => $session } if $context eq 'error';
2014 my $search = { 'svcnum' => $p->{'svcnum'} };
2015 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2017 my $svcdb = $opt{'svcdb'} || 'svc_acct';
2019 my $svc_x = qsearchs( $svcdb, $search );
2020 return { 'error' => 'No service selected in list_svc_usage' }
2023 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
2024 my $freq = $cust_pkg->part_pkg->freq;
2027 if ( $svcdb eq 'svc_phone' ) {
2028 my $format = $cust_pkg->part_pkg->option('output_format') || '';
2029 $format = '' if $format =~ /^sum_/;
2030 # sensible default if there is no format or it's a summary format
2031 if ( $cust_pkg->part_pkg->plan eq 'voip_inbound' ) {
2032 $format ||= 'source_default';
2033 $callback_opt{inbound} = 1;
2036 $format ||= 'default';
2039 $callback_opt{format} = $format;
2040 $header = [ split(',', FS::cdr::invoice_header($format) ) ];
2043 my $start = $cust_pkg->setup;
2044 #my $end = $cust_pkg->bill; # or time?
2047 unless ( $p->{beginning} ) {
2048 $p->{beginning} = $cust_pkg->last_bill;
2049 $p->{ending} = $end;
2052 die "illegal beginning" if $p->{beginning} !~ /^\d*$/;
2053 die "illegal ending" if $p->{ending} !~ /^\d*$/;
2055 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
2059 #kinda false laziness with FS::cust_main::bill, but perhaps
2060 #we should really change this bit to DateTime and DateTime::Duration
2062 #change this bit to use Date::Manip? CAREFUL with timezones (see
2063 # mailing list archive)
2064 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
2065 (localtime($p->{ending}) )[0,1,2,3,4,5];
2066 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
2067 (localtime($p->{beginning}) )[0,1,2,3,4,5];
2069 if ( $freq =~ /^\d+$/ ) {
2071 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
2073 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
2074 } elsif ( $freq =~ /^(\d+)w$/ ) {
2076 $nmday += $weeks * 7;
2077 $pmday -= $weeks * 7;
2078 } elsif ( $freq =~ /^(\d+)d$/ ) {
2082 } elsif ( $freq =~ /^(\d+)h$/ ) {
2087 return { 'error' => "unparsable frequency: ". $freq };
2090 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
2091 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
2095 'svcnum' => $p->{svcnum},
2096 'beginning' => $p->{beginning},
2097 'ending' => $p->{ending},
2098 'previous' => ($previous > $start) ? $previous : $start,
2099 'next' => ($next < $end) ? $next : $end,
2100 'header' => $header,
2108 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2109 return { 'error' => $session } if $context eq 'error';
2111 my $search = { 'custnum' => $custnum };
2112 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2113 my $cust_main = qsearchs('cust_main', $search )
2114 or return { 'error' => "unknown custnum $custnum" };
2116 my $status = $cust_main->status;
2117 #false laziness w/ClientAPI/Signup.pm
2119 my $cust_pkg = new FS::cust_pkg ( {
2120 'custnum' => $custnum,
2121 'pkgpart' => $p->{'pkgpart'},
2123 my $error = $cust_pkg->check;
2124 return { 'error' => $error } if $error;
2127 unless ( $p->{'svcpart'} eq 'none' ) {
2131 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
2133 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2134 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
2135 $svcdb = $part_svc->svcdb;
2137 $svcdb = 'svc_acct';
2139 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
2142 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
2143 'svc_domain' => [ qw( domain ) ],
2144 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
2145 'svc_external' => [ qw( id title ) ],
2146 'svc_pbx' => [ qw( id title ) ],
2149 my $svc_x = "FS::$svcdb"->new( {
2150 'svcpart' => $svcpart,
2151 map { $_ => $p->{$_} } @{$fields{$svcdb}}
2154 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
2157 while ( length($p->{"snarf_machine$snarfnum"}) ) {
2158 my $acct_snarf = new FS::acct_snarf ( {
2159 'machine' => $p->{"snarf_machine$snarfnum"},
2160 'protocol' => $p->{"snarf_protocol$snarfnum"},
2161 'username' => $p->{"snarf_username$snarfnum"},
2162 '_password' => $p->{"snarf_password$snarfnum"},
2165 push @acct_snarf, $acct_snarf;
2167 $svc_x->child_objects( \@acct_snarf );
2170 my $y = $svc_x->setdefault; # arguably should be in new method
2171 return { 'error' => $y } if $y && !ref($y);
2173 $error = $svc_x->check;
2174 return { 'error' => $error } if $error;
2181 tie my %hash, 'Tie::RefHash';
2182 %hash = ( $cust_pkg => \@svc );
2184 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
2185 return { 'error' => $error } if $error;
2187 my $conf = new FS::Conf;
2188 if ( $conf->exists('signup_server-realtime') ) {
2190 my $bill_error = _do_bop_realtime( $cust_main, $status );
2193 $cust_pkg->cancel('quiet'=>1);
2196 $cust_pkg->reexport;
2200 $cust_pkg->reexport;
2203 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
2205 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
2212 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2213 return { 'error' => $session } if $context eq 'error';
2215 my $search = { 'custnum' => $custnum };
2216 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2217 my $cust_main = qsearchs('cust_main', $search )
2218 or return { 'error' => "unknown custnum $custnum" };
2220 my $status = $cust_main->status;
2221 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
2222 or return { 'error' => "unknown package $p->{pkgnum}" };
2225 my $error = FS::cust_pkg::order( $custnum,
2231 my $conf = new FS::Conf;
2232 if ( $conf->exists('signup_server-realtime') ) {
2234 my $bill_error = _do_bop_realtime( $cust_main, $status );
2237 $newpkg[0]->suspend;
2240 $newpkg[0]->reexport;
2244 $newpkg[0]->reexport;
2247 return { error => '', pkgnum => $cust_pkg->pkgnum };
2251 sub order_recharge {
2254 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2255 return { 'error' => $session } if $context eq 'error';
2257 my $search = { 'custnum' => $custnum };
2258 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2259 my $cust_main = qsearchs('cust_main', $search )
2260 or return { 'error' => "unknown custnum $custnum" };
2262 my $status = $cust_main->status;
2263 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
2264 or return { 'error' => "unknown service " . $p->{'svcnum'} };
2266 my $svc_x = $cust_svc->svc_x;
2267 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
2270 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
2271 qw ( recharge_seconds recharge_upbytes recharge_downbytes
2272 recharge_totalbytes );
2273 my $amount = $part_pkg->option('recharge_amount', 1);
2275 my ($l, $v, $d) = $cust_svc->label; # blah
2276 my $pkg = "Recharge $v";
2278 my $bill_error = $cust_main->charge($amount, $pkg,
2279 "time: $vhash{seconds}, up: $vhash{upbytes}," .
2280 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
2281 $part_pkg->taxclass); #meh
2283 my $conf = new FS::Conf;
2284 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
2286 $bill_error = _do_bop_realtime( $cust_main, $status );
2291 my $error = $svc_x->recharge (\%vhash);
2292 return { 'error' => $error } if $error;
2296 my $error = $bill_error;
2297 $error ||= $svc_x->recharge (\%vhash);
2298 return { 'error' => $error } if $error;
2301 return { error => '', svc => $cust_svc->part_svc->svc };
2305 sub _do_bop_realtime {
2306 my ($cust_main, $status) = (shift, shift);
2308 my $old_balance = $cust_main->balance;
2310 my $bill_error = $cust_main->bill
2311 || $cust_main->apply_payments_and_credits
2312 || $cust_main->realtime_collect('selfservice' => 1);
2314 if ( $cust_main->balance > $old_balance
2315 && $cust_main->balance > 0
2316 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
2317 1 : $status eq 'suspended' ) ) {
2318 #this makes sense. credit is "un-doing" the invoice
2319 my $conf = new FS::Conf;
2320 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
2321 'self-service decline',
2322 'reason_type' => $conf->config('signup_credit_type'),
2324 $cust_main->apply_credits( 'order' => 'newest' );
2326 return { 'error' => '_decline', 'bill_error' => $bill_error };
2335 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2336 return { 'error' => $session } if $context eq 'error';
2338 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2339 or return { 'error' => "unknown custnum $custnum" };
2341 my @cust_pkg = sort { $a->bill <=> $b->bill }
2342 grep { $_->part_pkg->freq ne '0' }
2343 $cust_main->ncancelled_pkgs;
2345 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
2347 my $total = $cust_main->balance;
2350 my $bill = $_->bill;
2351 $total += $_->part_pkg->base_recur($_, \$bill);
2352 my $renew_date = $_->part_pkg->add_freq($_->bill);
2354 'pkgnum' => $_->pkgnum,
2355 'amount' => sprintf('%.2f', $total),
2356 'bill_date' => $_->bill,
2357 'bill_date_pretty' => time2str('%x', $_->bill),
2358 'renew_date' => $renew_date,
2359 'renew_date_pretty' => time2str('%x', $renew_date),
2360 'expire_date' => $_->expire,
2361 'expire_date_pretty' => time2str('%x', $_->expire),
2366 return { 'dates' => \@array };
2370 sub payment_info_renew_info {
2372 my $renew_info = renew_info($p);
2373 my $payment_info = payment_info($p);
2374 return { %$renew_info,
2382 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2383 return { 'error' => $session } if $context eq 'error';
2385 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2386 or return { 'error' => "unknown custnum $custnum" };
2388 my $date = $p->{'date'};
2392 #freeside-daily -n -d $date fs_daily $custnum
2393 $cust_main->bill_and_collect( 'time' => $date,
2394 'invoice_time' => $now,
2395 'actual_time' => $now,
2396 'check_freq' => '1d',
2399 return { 'error' => '' };
2405 my $session = _cache->get($p->{'session_id'})
2406 or return { 'error' => "Can't resume session" }; #better error message
2408 my $custnum = $session->{'custnum'};
2410 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2411 or return { 'error' => "unknown custnum $custnum" };
2413 my $conf = new FS::Conf;
2415 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2416 or return { 'error' => 'Permission denied' };
2418 my $pkgnum = $p->{'pkgnum'};
2420 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2421 'pkgnum' => $pkgnum, } )
2422 or return { 'error' => "unknown pkgnum $pkgnum" };
2424 my $error = $cust_pkg->suspend(reason => $reasonnum);
2425 return { 'error' => $error };
2431 my $session = _cache->get($p->{'session_id'})
2432 or return { 'error' => "Can't resume session" }; #better error message
2434 my $custnum = $session->{'custnum'};
2436 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2437 or return { 'error' => "unknown custnum $custnum" };
2439 my $pkgnum = $p->{'pkgnum'};
2441 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2442 'pkgnum' => $pkgnum, } )
2443 or return { 'error' => "unknown pkgnum $pkgnum" };
2445 my $error = $cust_pkg->cancel('quiet' => 1);
2446 return { 'error' => $error };
2450 sub provision_phone {
2453 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2455 if($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/){
2456 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2457 return { 'error' => $session } if $context eq 'error';
2459 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2460 return { 'error' => 'service not found' } unless $svc_phone;
2461 return { 'error' => 'invalid svcnum' }
2462 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2464 $svc_phone->email($p->{'email'})
2465 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2466 $svc_phone->forwarddst($p->{'forwarddst'})
2467 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2468 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2469 return { 'error' => $svc_phone->replace };
2473 unless($p->{'lnp'}) {
2474 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2475 $p->{'lnp_status'} = "portingin";
2476 return _provision( 'FS::svc_phone',
2477 [qw(lnp_desired_due_date lnp_other_provider
2478 lnp_other_provider_account phonenum countrycode lnp_status)],
2479 [qw(phonenum countrycode)],
2486 unless (scalar(@bulkdid)) {
2487 return _provision( 'FS::svc_phone',
2488 [qw(phonenum countrycode)],
2489 [qw(phonenum countrycode)],
2495 # bulk DID order case
2497 foreach my $did ( @bulkdid ) {
2498 $did =~ s/[^0-9]//g;
2499 $error = _provision( 'FS::svc_phone',
2500 [qw(phonenum countrycode)],
2501 [qw(phonenum countrycode)],
2503 'pkgnum' => $p->{'pkgnum'},
2504 'svcpart' => $p->{'svcpart'},
2506 'countrycode' => $p->{'countrycode'},
2507 'session_id' => $p->{'session_id'},
2510 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2512 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2515 sub provision_acct {
2517 warn "provision_acct called\n"
2520 return { 'error' => gettext('passwords_dont_match') }
2521 if $p->{'_password'} ne $p->{'_password2'};
2522 return { 'error' => gettext('empty_password') }
2523 unless length($p->{'_password'});
2525 if ($p->{'domsvc'}) {
2526 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
2527 qw ( svcpart pkgnum ) );
2528 return { 'error' => gettext('invalid_domain') }
2529 unless ($domains{$p->{'domsvc'}});
2532 warn "provision_acct calling _provision\n"
2534 _provision( 'FS::svc_acct',
2535 [qw(username _password domsvc)],
2536 [qw(username _password domsvc)],
2542 sub provision_external {
2544 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
2545 _provision( 'FS::svc_external',
2554 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
2555 warn "_provision called for $class\n"
2558 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2559 return { 'error' => $session } if $context eq 'error';
2561 my $search = { 'custnum' => $custnum };
2562 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2563 my $cust_main = qsearchs('cust_main', $search )
2564 or return { 'error' => "unknown custnum $custnum" };
2566 my $pkgnum = $p->{'pkgnum'};
2568 warn "searching for custnum $custnum pkgnum $pkgnum\n"
2570 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2571 'pkgnum' => $pkgnum,
2573 or return { 'error' => "unknown pkgnum $pkgnum" };
2575 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
2577 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
2578 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
2580 warn "creating $class record\n"
2582 my $svc_x = $class->new( {
2583 'pkgnum' => $p->{'pkgnum'},
2584 'svcpart' => $p->{'svcpart'},
2585 map { $_ => $p->{$_} } @$fields
2587 warn "inserting $class record\n"
2589 my $error = $svc_x->insert;
2592 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
2594 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
2597 my $return = { 'svc' => $part_svc->svc,
2599 map { $_ => $svc_x->get($_) } @$return_fields
2601 warn "_provision returning ". Dumper($return). "\n"
2610 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2611 return { 'error' => $session } if $context eq 'error';
2613 my $search = { 'custnum' => $custnum };
2614 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2615 my $cust_main = qsearchs('cust_main', $search )
2616 or return { 'error' => "unknown custnum $custnum" };
2618 my $pkgnum = $p->{'pkgnum'};
2620 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2621 'pkgnum' => $pkgnum,
2623 or return { 'error' => "unknown pkgnum $pkgnum" };
2625 my $svcpart = $p->{'svcpart'};
2627 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
2628 'svcpart' => $svcpart, } )
2629 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
2630 my $part_svc = $pkg_svc->part_svc;
2632 my $conf = new FS::Conf;
2635 'svc' => $part_svc->svc,
2636 'svcdb' => $part_svc->svcdb,
2637 'pkgnum' => $pkgnum,
2638 'svcpart' => $svcpart,
2639 'custnum' => $custnum,
2641 'security_phrase' => 0, #XXX !
2642 'svc_acct_pop' => [], #XXX !
2644 'init_popstate' => '',
2649 small_custview( $cust_main, $conf->config('countrydefault') ),
2653 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
2654 && $ret->{'svcdb'} eq 'svc_phone') {
2655 $ret->{'svcnum'} = $p->{'svcnum'};
2656 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2657 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
2658 $ret->{'email'} = $svc_phone->email;
2659 $ret->{'forwarddst'} = $svc_phone->forwarddst;
2666 sub unprovision_svc {
2669 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2670 return { 'error' => $session } if $context eq 'error';
2672 my $search = { 'custnum' => $custnum };
2673 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2674 my $cust_main = qsearchs('cust_main', $search )
2675 or return { 'error' => "unknown custnum $custnum" };
2677 my $svcnum = $p->{'svcnum'};
2679 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
2680 or return { 'error' => "unknown svcnum $svcnum" };
2682 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
2683 unless $cust_svc->cust_pkg->custnum == $custnum;
2685 my $conf = new FS::Conf;
2687 return { 'svc' => $cust_svc->part_svc->svc,
2688 'error' => $cust_svc->cancel,
2690 small_custview( $cust_main, $conf->config('countrydefault') ),
2695 sub myaccount_passwd {
2697 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2698 return { 'error' => $session } if $context eq 'error';
2700 return { 'error' => "New passwords don't match." }
2701 if $p->{'new_password'} ne $p->{'new_password2'};
2703 return { 'error' => 'Enter new password' }
2704 unless length($p->{'new_password'});
2706 #my $search = { 'custnum' => $custnum };
2707 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2708 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2709 my $search = " AND custnum = $1";
2710 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2712 my $svc_acct = qsearchs( {
2713 'table' => 'svc_acct',
2714 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2715 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
2716 'LEFT JOIN cust_main USING ( custnum ) ',
2717 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
2718 'extra_sql' => $search, #important
2720 or return { 'error' => "Service not found" };
2722 if ( exists($p->{'old_password'}) ) {
2723 return { 'error' => "Incorrect password." }
2724 unless $svc_acct->check_password($p->{'old_password'});
2727 $svc_acct->set_password($p->{'new_password'});
2728 my $error = $svc_acct->replace();
2730 my($label, $value) = $svc_acct->cust_svc->label;
2732 return { 'error' => $error,
2742 my $conf = new FS::Conf;
2743 my $verification = $conf->config('selfservice-password_reset_verification')
2744 or return { 'error' => 'Password resets disabled' };
2746 my $username = $p->{'username'};
2748 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
2749 or return { 'error' => 'Account not found' };
2751 my $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
2752 'domsvc' => $svc_domain->svcnum }
2754 or return { 'error' => 'Account not found' };
2756 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
2757 or return { 'error' => 'Account not found' };
2759 my $cust_main = $cust_pkg->cust_main;
2763 my( $p, $cust_main ) = @_;
2764 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
2765 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
2768 my( $p, $cust_main ) = @_;
2769 my $cust_pay = qsearchs({
2770 'table' => 'cust_pay',
2771 'hashref' => { 'custnum' => $cust_main->custnum },
2772 'order_by' => 'ORDER BY _date DESC LIMIT 1',
2776 $p->{'amount'} == $cust_pay->paid;
2779 my( $p, $cust_main ) = @_;
2780 $p->{'zip'} eq $cust_main->zip
2781 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
2785 foreach my $verify ( split(',', $verification) ) {
2787 &{ $verify{$verify} }( $p, $cust_main )
2788 or return { 'error' => 'Account not found' };
2792 #okay, we're verified, now create a unique session
2794 my $reset_session = {
2795 'svcnum' => $svc_acct->svcnum,
2798 my $timeout = '1 hour'; #?
2800 my $reset_session_id;
2802 $reset_session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
2803 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") ); #just in case
2805 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
2809 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $cust_main->agentnum);
2810 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
2811 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
2812 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
2813 my $error = $msg_template->send( 'cust_main' => $cust_main,
2814 'object' => $svc_acct,
2815 'substitutions' => {
2816 'session_id' => $reset_session_id,
2820 return { 'error' => $error }; #????
2823 return { 'error' => '' };
2826 sub check_reset_passwd {
2829 my $conf = new FS::Conf;
2830 my $verification = $conf->config('selfservice-password_reset_verification')
2831 or return { 'error' => 'Password resets disabled' };
2833 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
2834 or return { 'error' => "Can't resume session" }; #better error message
2836 my $svcnum = $reset_session->{'svcnum'};
2838 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
2839 or return { 'error' => "Service not found" };
2841 return { 'error' => '',
2842 'username' => $svc_acct->username,
2847 sub process_reset_passwd {
2850 my $conf = new FS::Conf;
2851 my $verification = $conf->config('selfservice-password_reset_verification')
2852 or return { 'error' => 'Password resets disabled' };
2854 return { 'error' => "New passwords don't match." }
2855 if $p->{'new_password'} ne $p->{'new_password2'};
2857 return { 'error' => 'Enter new password' }
2858 unless length($p->{'new_password'});
2860 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
2861 or return { 'error' => "Can't resume session" }; #better error message
2863 my $svcnum = $reset_session->{'svcnum'};
2865 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
2866 or return { 'error' => "Service not found" };
2868 $svc_acct->set_password($p->{'new_password'});
2869 my $error = $svc_acct->replace();
2871 my($label, $value) = $svc_acct->cust_svc->label;
2873 return { 'error' => $error,
2882 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2883 return { 'error' => $session } if $context eq 'error';
2885 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
2886 FS::TicketSystem->init();
2888 my $conf = new FS::Conf;
2889 my $queue = $p->{'queue'}
2890 || $conf->config('ticket_system-selfservice_queueid')
2891 || $conf->config('ticket_system-default_queueid');
2893 warn "$me create_ticket: creating ticket\n" if $DEBUG;
2894 my $err_or_ticket = FS::TicketSystem->create_ticket(
2895 '', #create RT session based on FS CurrentUser (fs_selfservice)
2897 'custnum' => $custnum,
2898 'svcnum' => $session->{'svcnum'},
2899 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
2902 if ( ref($err_or_ticket) ) {
2903 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
2905 return { 'error' => '',
2906 'ticket_id' => $err_or_ticket->id,
2909 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
2911 return { 'error' => $err_or_ticket };
2919 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2920 return { 'error' => $session } if $context eq 'error';
2922 return { error => 'requested format not implemented' }
2923 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
2925 my $conf = new FS::Conf;
2926 my $age_threshold = 0;
2927 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
2928 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
2930 my $search = { 'custnum' => $custnum };
2931 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2932 my $cust_main = qsearchs('cust_main', $search )
2933 or return { 'error' => "unknown custnum $custnum" };
2935 # does it make more sense to just run one sql query for this instead of all the
2936 # insanity below? would increase performance greately for large data sets?
2938 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
2939 my @part_svc = $cust_pkg->part_svc;
2940 foreach my $part_svc ( @part_svc ) {
2941 if($part_svc->svcdb eq 'svc_phone'){
2942 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
2943 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
2944 push @svc_phone, $cust_pkg_svc->svc_x
2945 if $cust_pkg_svc->date_inserted >= $age_threshold;
2953 my($xls_r,$xls_c) = (0,0);
2956 my @fields = qw( countrycode phonenum pin sip_password phone_name );
2957 if($p->{'format'} eq 'csv') {
2958 $csv = new Text::CSV_XS { 'always_quote' => 1,
2961 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
2962 $content .= $csv->string;
2964 elsif($p->{'format'} eq 'xls') {
2965 my $XLS1 = new IO::Scalar \$content;
2966 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
2967 or return { 'error' => "Error opening .xls file: $!" };
2968 $xls = $xls_workbook->add_worksheet('DIDs');
2969 foreach ( @fields ) {
2970 $xls->write(0,$xls_c++,$_);
2975 foreach my $svc_phone ( @svc_phone ) {
2976 my @cols = map { $svc_phone->$_ } @fields;
2977 if($p->{'format'} eq 'csv') {
2978 return { 'error' => 'Unable to create CSV' }
2979 unless $csv->combine(@cols);
2980 $content .= $csv->string;
2982 elsif($p->{'format'} eq 'xls') {
2985 $xls->write($xls_r,$xls_c++,$_);
2991 $xls_workbook->close() if $p->{'format'} eq 'xls';
2993 { content => $content, format => $p->{'format'}, };
2998 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2999 return { 'error' => $session } if $context eq 'error';
3001 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
3002 FS::TicketSystem->init();
3003 return { 'error' => 'get_ticket configuration error' }
3004 if $FS::TicketSystem::system ne 'RT_Internal';
3006 # check existence and ownership as part of this
3007 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
3008 my $rt_session = FS::TicketSystem->session('');
3009 my $Ticket = FS::TicketSystem->get_ticket_object(
3011 ticket_id => $p->{'ticket_id'},
3014 return { 'error' => 'ticket not found' } if !$Ticket;
3016 if ( length( $p->{'subject'} || '' ) ) {
3018 if ( $p->{'subject'} ne $Ticket->Subject ) {
3019 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
3020 return { 'error' => "unable to set subject: $msg" } if !$val;
3024 if(length($p->{'reply'})) {
3025 my @err_or_res = FS::TicketSystem->correspond_ticket(
3027 'ticket_id' => $p->{'ticket_id'},
3028 'content' => $p->{'reply'},
3031 return { 'error' => 'unable to reply to ticket' }
3032 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
3035 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
3036 my $err_or_ticket = FS::TicketSystem->get_ticket(
3038 'ticket_id' => $p->{'ticket_id'},
3041 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
3042 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
3044 return { 'error' => $err_or_ticket };
3047 my @custs = @{$err_or_ticket->{'custs'}};
3048 my @txns = @{$err_or_ticket->{'txns'}};
3051 # superseded by check in get_ticket_object
3052 #return { 'error' => 'invalid ticket requested' }
3053 #unless grep($_ eq $custnum, @custs);
3055 foreach my $txn ( @txns ) {
3056 push @filtered_txns, $txn
3057 if ($txn->{'type'} eq 'EmailRecord'
3058 || $txn->{'type'} eq 'Correspond'
3059 || $txn->{'type'} eq 'Create');
3062 warn "$me get_ticket: successful: \n"
3064 return { 'error' => '',
3065 'transactions' => \@filtered_txns,
3066 'ticket_fields' => $err_or_ticket->{'fields'},
3067 'ticket_id' => $p->{'ticket_id'},
3071 sub adjust_ticket_priority {
3073 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3074 return { 'error' => $session } if $context eq 'error';
3076 warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
3077 FS::TicketSystem->init;
3078 my $ss_priority = FS::TicketSystem->selfservice_priority;
3080 return { 'error' => 'adjust_ticket_priority configuration error' }
3081 if $FS::TicketSystem::system ne 'RT_Internal'
3084 my $values = $p->{'values'}; #hashref, id => priority value
3087 foreach my $id (keys %$values) {
3088 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
3089 my $Ticket = FS::TicketSystem->get_ticket_object('',
3091 'custnum' => $custnum,
3094 $ticket_error{$id} = 'ticket not found';
3098 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
3099 # We're not going to implement it for RT_External.
3100 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
3101 my $new_value = $values->{$id};
3102 next if $old_value eq $new_value;
3104 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
3106 # AddCustomFieldValue works fine (replacing any existing value) if it's
3107 # a single-valued custom field, which it should be. If it's not, you're
3108 # doing something wrong.
3110 if ( length($new_value) ) {
3111 ($val, $msg) = $Ticket->AddCustomFieldValue(
3112 Field => $ss_priority,
3113 Value => $new_value,
3117 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
3118 Field => $ss_priority,
3119 Value => $old_value,
3123 $ticket_error{$id} = $msg if !$val;
3124 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
3126 return { 'error' => '',
3127 'ticket_error' => \%ticket_error,
3128 %{ customer_info($p) } # send updated customer info back
3134 sub _custoragent_session_custnum {
3137 my($context, $session, $custnum);
3138 if ( $p->{'session_id'} ) {
3140 $context = 'customer';
3141 $session = _cache->get($p->{'session_id'})
3142 or return ( 'error' => "Can't resume session" ); #better error message
3143 $custnum = $session->{'custnum'};
3145 } elsif ( $p->{'agent_session_id'} ) {
3148 my $agent_cache = new FS::ClientAPI_SessionCache( {
3149 'namespace' => 'FS::ClientAPI::Agent',
3151 $session = $agent_cache->get($p->{'agent_session_id'})
3152 or return ( 'error' => "Can't resume session" ); #better error message
3153 $custnum = $p->{'custnum'};
3157 return ( 'error' => "Can't resume session" ); #better error message
3160 ($context, $session, $custnum);