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 @location_editable_fields );
52 @cust_main_editable_fields = qw(
53 first last daytime night fax mobile
55 payby payinfo payname paystart_month paystart_year payissue payip
56 ss paytype paystate stateid stateid_state
58 @location_editable_fields = qw(
59 address1 address2 city county state zip country
63 BEGIN { #preload to reduce time customer_info takes
64 if ( $FS::TicketSystem::system ) {
65 warn "$me: initializing ticket system\n" if $DEBUG;
66 FS::TicketSystem->init();
71 $cache ||= new FS::ClientAPI_SessionCache( {
72 'namespace' => 'FS::ClientAPI::MyAccount',
79 my($context, $session, $custnum) = _custoragent_session_custnum($p);
80 #return { 'error' => $session } if $context eq 'error';
83 if ( $context eq 'customer' ) {
85 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
88 $sth->execute($custnum) or die $sth->errstr;
90 $agentnum = $sth->fetchrow_arrayref->[0]
91 or die "no agentnum for custnum $custnum";
93 #} elsif ( $context eq 'agent' ) {
94 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
98 my $conf = new FS::Conf;
100 #false laziness w/Signup.pm
102 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
104 if ( $skin_info_cache_agent ) {
106 warn "$me loading cached skin info for agentnum $agentnum\n"
111 warn "$me populating skin info cache for agentnum $agentnum\n"
114 $skin_info_cache_agent = {
115 'agentnum' => $agentnum,
116 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
117 qw( company_name date_format ) ),
118 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
119 qw( body_bgcolor box_bgcolor stripe1_bgcolor stripe2_bgcolor
120 text_color link_color vlink_color hlink_color alink_color
121 font title_color title_align title_size menu_bgcolor menu_fontsize
124 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
125 qw( menu_skipblanks menu_skipheadings menu_nounderline no_logo )
127 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
128 qw( title_left_image title_right_image
129 menu_top_image menu_body_image menu_bottom_image
132 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
133 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
134 qw( head body_header body_footer company_address ) ),
137 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
141 #{ %$skin_info_cache_agent };
142 $skin_info_cache_agent;
149 my $conf = new FS::Conf;
153 'phone_login' => $conf->exists('selfservice_server-phone_login'),
154 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
155 'banner_url' => scalar($conf->config('selfservice-login_banner_url')),
156 'banner_image_md5' =>
157 md5_hex($conf->config_binary('selfservice-login_banner_image')),
164 sub login_banner_image {
166 my $conf = new FS::Conf;
167 my $image = $conf->config_binary('selfservice-login_banner_image');
169 'md5' => md5_hex($image),
174 #false laziness w/FS::ClientAPI::passwd::passwd
178 my $conf = new FS::Conf;
181 if ( $p->{'domain'} eq 'svc_phone'
182 && $conf->exists('selfservice_server-phone_login') ) {
184 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
185 return { error => 'Number not found.' } unless $svc_phone;
188 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
189 #return { error => 'Only primary user may log in.' }
190 # if $conf->exists('selfservice_server-primary_only')
191 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
193 return { error => 'Incorrect PIN.' }
194 unless $svc_phone->check_pin($p->{'password'});
200 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
201 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
203 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
204 'domsvc' => $svc_domain->svcnum, }
206 return { error => 'User not found.' } unless $svc_acct;
208 if($conf->exists('selfservice_server-login_svcpart')) {
209 my @svcpart = $conf->config('selfservice_server-login_svcpart');
210 my $svcpart = $svc_acct->cust_svc->svcpart;
211 return { error => 'Invalid user.' }
212 unless grep($_ eq $svcpart, @svcpart);
215 return { error => 'Incorrect password.' }
216 unless $svc_acct->check_password($p->{'password'});
223 'svcnum' => $svc_x->svcnum,
226 my $cust_svc = $svc_x->cust_svc;
227 my $cust_pkg = $cust_svc->cust_pkg;
229 my $cust_main = $cust_pkg->cust_main;
230 $session->{'custnum'} = $cust_main->custnum;
231 if ( $conf->exists('pkg-balances') ) {
232 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
233 $cust_main->ncancelled_pkgs;
234 $session->{'pkgnum'} = $cust_pkg->pkgnum
235 if scalar(@cust_pkg) > 1;
239 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
240 #return { error => 'Only primary user may log in.' }
241 # if $conf->exists('selfservice_server-primary_only')
242 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
243 my $part_pkg = $cust_pkg->part_pkg;
244 return { error => 'Only primary user may log in.' }
245 if $conf->exists('selfservice_server-primary_only')
246 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
250 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
251 } until ( ! defined _cache->get($session_id) ); #just in case
253 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
254 _cache->set( $session_id, $session, $timeout );
256 return { 'error' => '',
257 'session_id' => $session_id,
263 if ( $p->{'session_id'} ) {
264 _cache->remove($p->{'session_id'});
265 return { %{ skin_info($p) }, 'error' => '' };
267 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
274 my($context, $session, $custnum) = _custoragent_session_custnum($p);
275 return { 'error' => $session } if $context eq 'error';
277 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
278 or return { 'error' => "Service not found" };
280 $session->{'svcnum'} = $svc_acct->svcnum;
282 my $conf = new FS::Conf;
283 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
284 _cache->set( $p->{'session_id'}, $session, $timeout );
286 return { 'error' => '' };
290 sub payment_gateway {
292 # takes a cust_main and a cust_payby entry, returns the payment_gateway
293 my $conf = new FS::Conf;
294 my $cust_main = shift;
295 my $cust_payby = shift;
296 my $gatewaynum = $conf->config('selfservice-payment_gateway');
298 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
299 die "configured gatewaynum $gatewaynum not found!" if !$pg;
303 return '' if ! FS::payby->realtime($cust_payby);
304 my $pg = $cust_main->agent->payment_gateway(
305 'method' => FS::payby->payby2bop($cust_payby),
315 my $conf = new FS::Conf;
317 my $info = skin_info($p);
319 use vars qw( $cust_paybys ); #cache for performance
320 unless ( $cust_paybys ) {
322 my %cust_paybys = map { $_ => 1 }
323 map { FS::payby->payby2payment($_) }
324 $conf->config('signup_server-payby');
326 $cust_paybys = [ keys %cust_paybys ];
329 $info->{'cust_paybys'} = $cust_paybys;
331 my($context, $session, $custnum) = _custoragent_session_custnum($p);
332 return { 'error' => $session } if $context eq 'error';
334 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
335 or return { 'error' => "unknown custnum $custnum" };
337 $info->{'hide_payment_fields'} = [
339 my $pg = payment_gateway($cust_main, $_);
340 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
341 } @{ $info->{cust_paybys} }
344 $info->{'self_suspend_reason'} =
345 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
347 $info->{'edit_ticket_subject'} =
348 $conf->exists('ticket_system-selfservice_edit_subject') &&
349 $cust_main->edit_subject;
352 'custnum' => $custnum,
353 'access_pkgnum' => $session->{'pkgnum'},
354 'access_svcnum' => $session->{'svcnum'},
361 my($context, $session, $custnum) = _custoragent_session_custnum($p);
362 return { 'error' => $session } if $context eq 'error';
366 my $conf = new FS::Conf;
367 if ($conf->exists('cust_main-require_address2')) {
368 $return{'require_address2'} = '1';
370 $return{'require_address2'} = '';
373 if ( $FS::TicketSystem::system ) {
374 warn "$me customer_info: initializing ticket system\n" if $DEBUG;
375 FS::TicketSystem->init();
378 if ( $custnum ) { #customer record
380 my $search = { 'custnum' => $custnum };
381 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
382 my $cust_main = qsearchs('cust_main', $search )
383 or return { 'error' => "unknown custnum $custnum" };
385 $return{display_custnum} = $cust_main->display_custnum;
387 if ( $session->{'pkgnum'} ) {
388 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
389 #next_bill_date from cust_pkg?
391 $return{balance} = $cust_main->balance;
392 $return{next_bill_date} = $cust_main->next_bill_date;
393 $return{next_bill_date_pretty} =
394 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
398 my @tickets = $cust_main->tickets;
399 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
400 if ( $FS::TicketSystem::system && FS::TicketSystem->selfservice_priority ) {
401 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
405 ($a->{'_selfservice_priority'} eq '') <=>
406 ($b->{'_selfservice_priority'} eq '')
409 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
415 $return{tickets} = \@tickets;
418 unless ( $session->{'pkgnum'} ) {
421 invnum => $_->invnum,
422 date => time2str("%b %o, %Y", $_->_date),
425 } $cust_main->open_cust_bill;
426 $return{open_invoices} = \@open;
428 my $sql = 'SELECT MAX(_date) FROM cust_bill WHERE custnum = ?';
429 my $sth = dbh->prepare($sql) or die dbh->errstr;
430 $sth->execute($custnum) or die $sth->errstr;
431 $return{'last_invoice_date'} = $sth->fetchrow_arrayref->[0];
432 $return{'last_invoice_date_pretty'} =
433 time2str('%m/%d/%Y', $return{'last_invoice_date'} );
436 $return{countrydefault} = scalar($conf->config('countrydefault'));
438 $return{small_custview} =
439 small_custview( $cust_main,
440 $return{countrydefault},
441 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
444 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
446 $return{has_ship_address} = $cust_main->has_ship_address;
447 $return{status} = $cust_main->status;
448 $return{statuscolor} = $cust_main->statuscolor;
450 for (@cust_main_editable_fields) {
451 $return{$_} = $cust_main->get($_);
454 for (@location_editable_fields) {
455 $return{$_} = $cust_main->bill_location->get($_);
456 $return{'ship_'.$_} = $cust_main->ship_location->get($_);
458 $return{has_ship_address} = $cust_main->has_ship_address;
459 # compatibility: some places in selfservice use this to determine
460 # if there's a ship address
461 if ( $return{has_ship_address} ) {
462 $return{ship_last} = $cust_main->last;
463 $return{ship_first} = $cust_main->first;
466 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
467 $return{payinfo} = $cust_main->paymask;
468 @return{'month', 'year'} = $cust_main->paydate_monthyear;
471 $return{'invoicing_list'} =
472 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
473 $return{'postal_invoicing'} =
474 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
476 if (scalar($conf->config('support_packages'))) {
477 my @support_services = ();
478 foreach ($cust_main->support_services) {
479 my $seconds = $_->svc_x->seconds || 0;
480 my $time_remaining = (($seconds < 0) ? '-' : '' ).
481 int(abs($seconds)/3600)."h".
482 sprintf("%02d",(abs($seconds)%3600)/60)."m";
483 my $cust_pkg = $_->cust_pkg;
486 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
487 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
488 push @support_services, { svcnum => $_->svcnum,
489 time => $time_remaining,
494 $return{support_services} = \@support_services;
497 if ( $conf->config('prepayment_discounts-credit_type') ) {
499 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
502 if ( $session->{'svcnum'} ) {
503 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
504 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
505 $return{'svcnum'} = $session->{'svcnum'};
508 } elsif ( $session->{'svcnum'} ) { #no customer record
510 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
511 or die "unknown svcnum";
512 $return{name} = $svc_acct->email;
516 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
520 return { 'error' => '',
521 'custnum' => $custnum,
527 sub customer_info_short {
530 my($context, $session, $custnum) = _custoragent_session_custnum($p);
531 return { 'error' => $session } if $context eq 'error';
535 my $conf = new FS::Conf;
537 if ( $custnum ) { #customer record
539 my $search = { 'custnum' => $custnum };
540 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
541 my $cust_main = qsearchs('cust_main', $search )
542 or return { 'error' => "unknown custnum $custnum" };
544 $return{display_custnum} = $cust_main->display_custnum;
546 $return{countrydefault} = scalar($conf->config('countrydefault'));
548 $return{small_custview} =
549 small_custview( $cust_main,
550 $return{countrydefault},
554 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
556 $return{payby} = $cust_main->payby;
558 #none of these are terribly expensive if we want 'em...
559 for (@cust_main_editable_fields) {
560 $return{$_} = $cust_main->get($_);
562 #maybe a little more expensive, but it should be cached by now
563 for (@location_editable_fields) {
564 $return{$_} = $cust_main->bill_location->get($_);
565 $return{'ship_'.$_} = $cust_main->ship_location->get($_);
568 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
569 $return{payinfo} = $cust_main->paymask;
570 @return{'month', 'year'} = $cust_main->paydate_monthyear;
573 $return{'invoicing_list'} =
574 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
575 #$return{'postal_invoicing'} =
576 # 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
578 if ( $session->{'svcnum'} ) {
579 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
580 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
581 $return{'svcnum'} = $session->{'svcnum'};
584 } elsif ( $session->{'svcnum'} ) { #no customer record
586 #uuh, not supproted yet... die?
587 return { 'error' => 'customer_info_short not yet supported as agent' };
591 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
595 return { 'error' => '',
596 'custnum' => $custnum,
601 sub billing_history {
604 my($context, $session, $custnum) = _custoragent_session_custnum($p);
605 return { 'error' => $session } if $context eq 'error';
607 return { 'error' => 'No customer' } unless $custnum;
609 my $search = { 'custnum' => $custnum };
610 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
611 my $cust_main = qsearchs('cust_main', $search )
612 or return { 'error' => "unknown custnum $custnum" };
616 if ( $session->{'pkgnum'} ) {
617 #$return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
618 #next_bill_date from cust_pkg?
619 return { 'error' => 'No history for package' };
622 $return{balance} = $cust_main->balance;
623 $return{next_bill_date} = $cust_main->next_bill_date;
624 $return{next_bill_date_pretty} =
625 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
630 my $conf = new FS::Conf;
632 if ( $conf->exists('selfservice-billing_history-line_items') ) {
634 foreach my $cust_bill ( $cust_main->cust_bill ) {
637 'type' => 'Line item',
638 'description' => $_->desc. ( $_->sdate && $_->edate
639 ? ' '. time2str('%d-%b-%Y', $_->sdate).
640 ' To '. time2str('%d-%b-%Y', $_->edate)
643 'amount' => sprintf('%.2f', $_->setup + $_->recur ),
644 'date' => $cust_bill->_date,
645 'date_pretty' => time2str('%m/%d/%Y', $cust_bill->_date ),
647 foreach $cust_bill->cust_bill_pkg;
655 'description' => 'Invoice #'. $_->display_invnum,
656 'amount' => sprintf('%.2f', $_->charged ),
658 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
660 foreach $cust_main->cust_bill;
666 'description' => 'Payment', #XXX type
667 'amount' => sprintf('%.2f', 0 - $_->paid ),
669 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
671 foreach $cust_main->cust_pay;
675 'description' => 'Credit', #more info?
676 'amount' => sprintf('%.2f', 0 -$_->amount ),
678 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
680 foreach $cust_main->cust_credit;
684 'description' => 'Refund', #more info? type, like payment?
685 'amount' => $_->refund,
687 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
689 foreach $cust_main->cust_refund;
691 @history = sort { $b->{'date'} <=> $a->{'date'} } @history;
693 $return{'history'} = \@history;
701 my $session = _cache->get($p->{'session_id'})
702 or return { 'error' => "Can't resume session" }; #better error message
704 my $custnum = $session->{'custnum'}
705 or return { 'error' => "no customer record" };
707 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
708 or return { 'error' => "unknown custnum $custnum" };
710 my $new = new FS::cust_main { $cust_main->hash };
712 $new->set( $_ => $p->{$_} )
713 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
715 if ( exists($p->{address1}) ) {
716 my $bill_location = FS::cust_location->new({
717 map { $_ => $p->{$_} } @location_editable_fields
719 # if this is unchanged from before, cust_main::replace will ignore it
720 $new->set('bill_location' => $bill_location);
723 if ( exists($p->{ship_address1}) ) {
724 my $ship_location = FS::cust_location->new({
725 map { $_ => $p->{"ship_$_"} } @location_editable_fields
727 if ( !grep { length($p->{"ship_$_"}) } @location_editable_fields ) {
728 # Selfservice unfortunately tries to indicate "same as billing
729 # address" by sending all fields empty. Did this ever work?
730 $ship_location = $cust_main->bill_location;
732 $new->set('ship_location' => $ship_location);
734 # but if it hasn't been passed in at all, leave ship_location alone--
735 # DON'T change it to match bill_location.
738 if (exists($p->{'payby'})) {
739 $p->{'payby'} =~ /^([A-Z]{4})$/
740 or return { 'error' => "illegal_payby " . $p->{'payby'} };
744 if ( $payby =~ /^(CARD|DCRD)$/ ) {
746 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
748 if ( $new->payinfo eq $cust_main->paymask ) {
749 $new->payinfo($cust_main->payinfo);
751 $new->payinfo($p->{'payinfo'});
754 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
756 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
759 $p->{'payinfo1'} =~ /^([\dx]+)$/
760 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
762 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
763 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
765 $payinfo = $payinfo1. '@'. $payinfo2;
767 $new->payinfo( ($payinfo eq $cust_main->paymask)
768 ? $cust_main->payinfo
772 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
774 } elsif ( $payby =~ /^(BILL)$/ ) {
776 } elsif ( $payby ) { #notyet ready
777 return { 'error' => "unknown payby $payby" };
781 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
782 #false laziness with httemplate/edit/process/cust_main.cgi
783 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
784 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
786 @invoicing_list = $cust_main->invoicing_list;
789 my $error = $new->replace($cust_main, \@invoicing_list);
790 return { 'error' => $error } if $error;
793 return { 'error' => '' };
798 my $session = _cache->get($p->{'session_id'})
799 or return { 'error' => "Can't resume session" }; #better error message
805 my $conf = new FS::Conf;
806 use vars qw($payment_info); #cache for performance
807 unless ( $payment_info ) {
809 my %states = map { $_->state => 1 }
810 qsearch('cust_main_county', {
811 'country' => $conf->config('countrydefault') || 'US'
814 my %cust_paybys = map { $_ => 1 }
815 map { FS::payby->payby2payment($_) }
816 $conf->config('signup_server-payby');
818 my @cust_paybys = keys %cust_paybys;
822 #list all counties/states/countries
823 'cust_main_county' =>
824 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
826 #shortcut for one-country folks
828 [ sort { $a cmp $b } keys %states ],
830 'card_types' => card_types(),
832 'paytypes' => [ @FS::cust_main::paytypes ],
834 'paybys' => [ $conf->config('signup_server-payby') ],
835 'cust_paybys' => \@cust_paybys,
837 'stateid_label' => FS::Msgcat::_gettext('stateid'),
838 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
840 'show_ss' => $conf->exists('show_ss'),
841 'show_stateid' => $conf->exists('show_stateid'),
842 'show_paystate' => $conf->exists('show_bankstate'),
844 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
846 'credit_card_surcharge_percentage' => $conf->config('credit-card-surcharge-percentage'),
855 my %return = %$payment_info;
857 my $custnum = $session->{'custnum'};
859 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
860 or return { 'error' => "unknown custnum $custnum" };
862 $return{'hide_payment_fields'} = [
864 my $pg = payment_gateway($cust_main, $_);
865 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
866 } @{ $return{cust_paybys} }
869 $return{balance} = $cust_main->balance; #XXX pkg-balances?
871 $return{payname} = $cust_main->payname
872 || ( $cust_main->first. ' '. $cust_main->get('last') );
874 $return{$_} = $cust_main->bill_location->get($_)
875 for qw(address1 address2 city state zip);
877 $return{payby} = $cust_main->payby;
878 $return{stateid_state} = $cust_main->stateid_state;
880 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
881 $return{card_type} = cardtype($cust_main->payinfo);
882 $return{payinfo} = $cust_main->paymask;
884 @return{'month', 'year'} = $cust_main->paydate_monthyear;
888 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
889 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
890 $return{payinfo1} = $payinfo1;
891 $return{payinfo2} = $payinfo2;
892 $return{paytype} = $cust_main->paytype;
893 $return{paystate} = $cust_main->paystate;
894 $return{payname} = $cust_main->payname; # override 'first/last name' default from above, if any. Is instution-name here. (#15819)
897 if ( $conf->config('prepayment_discounts-credit_type') ) {
899 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
902 #doubleclick protection
904 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
906 return { 'error' => '',
912 #some false laziness with httemplate/process/payment.cgi - look there for
913 #ACH and CVV support stuff
915 sub validate_payment {
918 my $session = _cache->get($p->{'session_id'})
919 or return { 'error' => "Can't resume session" }; #better error message
921 my $custnum = $session->{'custnum'};
923 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
924 or return { 'error' => "unknown custnum $custnum" };
926 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
927 or return { 'error' => gettext('illegal_amount') };
929 return { error => 'Amount must be greater than 0' } unless $amount > 0;
931 #false laziness w/tr-amount_fee.html, but we don't want selfservice users
932 #changing the hidden form values
933 my $conf = new FS::Conf;
934 my $fee_display = $conf->config('selfservice_process-display') || 'add';
935 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
936 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
937 if ( $fee_display eq 'add'
939 and ! $fee_skip_first || scalar($cust_main->cust_pay)
942 my $fee_pkg = qsearchs('part_pkg', { pkgpart=>$fee_pkgpart } );
943 $amount = sprintf('%.2f', $amount + $fee_pkg->option('setup_fee') );
946 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
947 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
948 my $discount_term = $1;
950 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
951 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
954 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
955 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
958 $p->{'payby'} ||= 'CARD';
959 $p->{'payby'} =~ /^([A-Z]{4})$/
960 or return { 'error' => "illegal_payby " . $p->{'payby'} };
963 #false laziness w/process/payment.cgi
966 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
968 $p->{'payinfo1'} =~ /^([\dx]+)$/
969 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
971 $p->{'payinfo2'} =~ /^([\dx]+)$/
972 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
974 $payinfo = $payinfo1. '@'. $payinfo2;
976 $payinfo = $cust_main->payinfo
977 if $cust_main->paymask eq $payinfo;
979 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
981 $payinfo = $p->{'payinfo'};
983 #more intelligent matching will be needed here if you change
984 #card_masking_method and don't remove existing paymasks
985 $payinfo = $cust_main->payinfo
986 if $cust_main->paymask eq $payinfo;
989 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
990 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
994 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
995 return { 'error' => gettext('unknown_card_type') }
996 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
998 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
999 if ( cardtype($payinfo) eq 'American Express card' ) {
1000 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
1001 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
1004 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
1005 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
1011 die "unknown payby $payby";
1014 my %payby2fields = (
1015 'CARD' => [ qw( paystart_month paystart_year payissue payip
1016 address1 address2 city state zip country ) ],
1017 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
1021 $card_type = cardtype($payinfo) if $payby eq 'CARD';
1024 'cust_main' => $cust_main, #XXX or just custnum??
1025 'amount' => $amount,
1027 'payinfo' => $payinfo,
1028 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
1029 'card_type' => $card_type,
1030 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
1031 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
1032 'month' => $p->{'month'},
1033 'year' => $p->{'year'},
1034 'payname' => $payname,
1035 'paybatch' => $paybatch, #this doesn't actually do anything
1036 'paycvv' => $paycvv,
1037 'payname' => $payname,
1038 'discount_term' => $discount_term,
1039 'pkgnum' => $session->{'pkgnum'},
1040 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
1050 my $validate = validate_payment($p);
1051 return $validate if $validate->{'error'};
1053 my $conf = new FS::Conf;
1054 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
1055 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
1057 +{ map { $_=>$validate->{$_} }
1058 qw( card_type paymask payname paydate_pretty month year amount
1059 address1 address2 city state zip country
1065 sub process_stored_payment {
1068 my $session_id = $p->{'session_id'};
1070 my $payment_info = _cache->get( "payment_$session_id" )
1071 or return { 'error' => "Can't resume session" }; #better error message
1073 do_process_payment($payment_info);
1077 sub process_payment {
1080 my $payment_info = validate_payment($p);
1081 return $payment_info if $payment_info->{'error'};
1083 do_process_payment($payment_info);
1087 sub do_process_payment {
1088 my $validate = shift;
1090 my $cust_main = $validate->{'cust_main'};
1092 my $amount = delete $validate->{'amount'};
1095 my $payby = delete $validate->{'payby'};
1097 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
1100 'paynum_ref' => \$paynum,
1103 return { 'error' => $error } if $error;
1105 #no error, so order the fee package if applicable...
1106 my $conf = new FS::Conf;
1107 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
1108 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
1110 if ( $fee_pkgpart and ! $fee_skip_first || scalar($cust_main->cust_pay) ) {
1112 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $fee_pkgpart };
1114 $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
1115 return { 'error' => "payment processed successfully, but error ordering fee: $error" }
1118 #and generate an invoice for it now too
1119 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1120 return { 'error' => "payment processed and fee ordered sucessfully, but error billing fee: $error" }
1125 $cust_main->apply_payments;
1127 if ( $validate->{'save'} ) {
1128 my $new = new FS::cust_main { $cust_main->hash };
1129 if ($payby eq 'CARD' || $payby eq 'DCRD') {
1130 $new->set( $_ => $validate->{$_} )
1131 foreach qw( payname paystart_month paystart_year payissue payip );
1132 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
1134 my $bill_location = FS::cust_location->new({
1135 map { $_ => $validate->{$_} }
1136 qw(address1 address2 city state country zip)
1138 $new->set('bill_location' => $bill_location);
1139 # but don't allow the service address to change this way.
1141 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
1142 $new->set( $_ => $validate->{$_} )
1143 foreach qw( payname payip paytype paystate
1144 stateid stateid_state );
1145 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
1147 $new->set( 'payinfo' => $cust_main->card_token || $validate->{'payinfo'} );
1148 $new->set( 'paydate' => $validate->{'paydate'} );
1149 my $error = $new->replace($cust_main);
1151 #no, this causes customers to process their payments again
1152 #return { 'error' => $error };
1153 #XXX just warn verosely for now so i can figure out how these happen in
1154 # the first place, eventually should redirect them to the "change
1155 #address" page but indicate the payment did process??
1156 delete($validate->{'payinfo'}); #don't want to log this!
1157 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
1158 "NEW: ". Dumper($new)."\n".
1159 "OLD: ". Dumper($cust_main)."\n".
1160 "PACKET: ". Dumper($validate)."\n";
1168 my $receipt_html = '';
1170 # currently supported for realtime CC only; send receipt data to SS
1171 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
1174 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
1177 <TD ALIGN="right">Payment#</TD>
1178 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
1182 <TD ALIGN="right">Date</TD>
1184 <TD BGCOLOR="#FFFFFF"><B>! .
1185 time2str("%a %b %o, %Y %r", $cust_pay->_date)
1191 <TD ALIGN="right">Amount</TD>
1192 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
1197 <TD ALIGN="right">Payment method</TD>
1198 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
1209 my($gw, $auth, $order) = split(':', $cust_pay->paybatch);
1213 'amount' => sprintf('%.2f', $cust_pay->paid),
1214 'date' => $cust_pay->_date,
1215 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
1216 'time_pretty' => time2str('%T', $cust_pay->_date),
1217 'auth_num' => $auth,
1218 'order_num' => $order,
1219 'receipt_html' => $receipt_html,
1226 'receipt_html' => '',
1233 sub realtime_collect {
1236 my $session = _cache->get($p->{'session_id'})
1237 or return { 'error' => "Can't resume session" }; #better error message
1239 my $custnum = $session->{'custnum'};
1241 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1242 or return { 'error' => "unknown custnum $custnum" };
1245 if ( $p->{'amount'} ) {
1246 $amount = $p->{'amount'};
1248 elsif ( $session->{'pkgnum'} ) {
1249 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1252 $amount = $cust_main->balance;
1255 my $error = $cust_main->realtime_collect(
1256 'method' => $p->{'method'},
1257 'amount' => $amount,
1258 'pkgnum' => $session->{'pkgnum'},
1259 'session_id' => $p->{'session_id'},
1263 return { 'error' => $error } unless ref( $error );
1265 return { 'error' => '', amount => $amount, %$error };
1268 sub process_payment_order_pkg {
1271 my $hr = process_payment($p);
1272 return $hr if $hr->{'error'};
1277 sub process_payment_order_renew {
1280 my $hr = process_payment($p);
1281 return $hr if $hr->{'error'};
1286 sub process_prepay {
1290 my $session = _cache->get($p->{'session_id'})
1291 or return { 'error' => "Can't resume session" }; #better error message
1295 my $custnum = $session->{'custnum'};
1297 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1298 or return { 'error' => "unknown custnum $custnum" };
1300 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1301 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1309 return { 'error' => $error } if $error;
1311 return { 'error' => '',
1312 'amount' => $amount,
1313 'seconds' => $seconds,
1314 'duration' => duration_exact($seconds),
1315 'upbytes' => $upbytes,
1316 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1317 'downbytes' => $downbytes,
1318 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1319 'totalbytes'=> $totalbytes,
1320 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1327 my $session = _cache->get($p->{'session_id'})
1328 or return { 'error' => "Can't resume session" }; #better error message
1330 my $custnum = $session->{'custnum'};
1332 my $invnum = $p->{'invnum'};
1334 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1335 'custnum' => $custnum } )
1336 or return { 'error' => "Can't find invnum" };
1340 return { 'error' => '',
1341 'invnum' => $invnum,
1342 'invoice_text' => join('', $cust_bill->print_text ),
1343 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1350 my $session = _cache->get($p->{'session_id'})
1351 or return { 'error' => "Can't resume session" }; #better error message
1353 my $custnum = $session->{'custnum'};
1355 my $invnum = $p->{'invnum'};
1357 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1358 'custnum' => $custnum } )
1359 or return { 'error' => "Can't find invnum" };
1363 return { 'error' => '',
1364 'invnum' => $invnum,
1365 'invoice_pdf' => $cust_bill->print_pdf({
1366 'unsquelch_cdr' => 1,
1367 'locale' => $p->{'locale'},
1373 sub legacy_invoice {
1375 my $session = _cache->get($p->{'session_id'})
1376 or return { 'error' => "Can't resume session" }; #better error message
1378 my $custnum = $session->{'custnum'};
1380 my $legacyinvnum = $p->{'legacyinvnum'};
1383 'legacyinvnum' => $legacyinvnum,
1384 'custnum' => $custnum,
1387 my $legacy_cust_bill =
1388 qsearchs('legacy_cust_bill', { %hash, 'locale' => $p->{'locale'} } )
1389 || qsearchs('legacy_cust_bill', \%hash )
1390 or return { 'error' => "Can't find legacyinvnum" };
1394 return { 'error' => '',
1395 'legacyinvnum' => $legacyinvnum,
1396 'legacyid' => $legacy_cust_bill->legacyid,
1397 'invoice_html' => $legacy_cust_bill->content_html,
1402 sub legacy_invoice_pdf {
1404 my $session = _cache->get($p->{'session_id'})
1405 or return { 'error' => "Can't resume session" }; #better error message
1407 my $custnum = $session->{'custnum'};
1409 my $legacyinvnum = $p->{'legacyinvnum'};
1411 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1412 'legacyinvnum' => $legacyinvnum,
1413 'custnum' => $custnum,
1414 }) or return { 'error' => "Can't find legacyinvnum" };
1418 return { 'error' => '',
1419 'legacyinvnum' => $legacyinvnum,
1420 'legacyid' => $legacy_cust_bill->legacyid,
1421 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1429 #sessioning for this? how do we get the session id to the backend invoice
1430 # template so it can add it to the link, blah
1433 if ( $p->{'invnum'} ) {
1434 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1435 or return { 'error' => 'unknown invnum' };
1436 $agentnum = $cust_bill->cust_main->agentnum;
1439 my $templatename = $p->{'template'} || $p->{'templatename'};
1441 #false laziness-ish w/view/cust_bill-logo.cgi
1443 my $conf = new FS::Conf;
1444 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1445 $templatename = "_$1";
1450 my $filename = "logo$templatename.png";
1452 return { 'error' => '',
1453 'logo' => $conf->config_binary($filename, $agentnum),
1454 'content_type' => 'image/png', #should allow gif, jpg too
1461 my $session = _cache->get($p->{'session_id'})
1462 or return { 'error' => "Can't resume session" }; #better error message
1464 my $custnum = $session->{'custnum'};
1466 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1467 or return { 'error' => "unknown custnum $custnum" };
1469 my $conf = new FS::Conf;
1471 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1473 my @cust_bill = grep ! $_->hide, $cust_main->cust_bill;
1477 return { 'error' => '',
1478 'balance' => $cust_main->balance,
1481 my $owed = $_->owed;
1483 +{ 'invnum' => $_->invnum,
1484 '_date' => $_->_date,
1485 'date' => time2str("%b %o, %Y", $_->_date),
1486 'date_short' => time2str("%m-%d-%Y", $_->_date),
1487 'previous' => sprintf('%.2f', ($_->previous)[0]),
1488 'charged' => sprintf('%.2f', $_->charged),
1489 'owed' => sprintf('%.2f', $owed),
1490 'balance' => sprintf('%.2f', $balance),
1495 'legacy_invoices' => [
1497 +{ 'legacyinvnum' => $_->legacyinvnum,
1498 'legacyid' => $_->legacyid,
1499 '_date' => $_->_date,
1500 'date' => time2str("%b %o, %Y", $_->_date),
1501 'date_short' => time2str("%m-%d-%Y", $_->_date),
1502 'charged' => sprintf('%.2f', $_->charged),
1503 'has_content' => ( length($_->content_pdf)
1504 || length($_->content_html) ),
1514 my $session = _cache->get($p->{'session_id'})
1515 or return { 'error' => "Can't resume session" }; #better error message
1517 my $custnum = $session->{'custnum'};
1519 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1520 or return { 'error' => "unknown custnum $custnum" };
1522 my @errors = $cust_main->cancel( 'quiet'=>1 );
1524 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1526 return { 'error' => $error };
1533 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1534 return { 'error' => $session } if $context eq 'error';
1536 my $search = { 'custnum' => $custnum };
1537 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1538 my $cust_main = qsearchs('cust_main', $search )
1539 or return { 'error' => "unknown custnum $custnum" };
1541 my $conf = new FS::Conf;
1543 # the duplication below is necessary:
1544 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1545 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1546 # non-backwards-compatible change breaking the software of anyone using the API
1547 # instead of the stock selfservice
1548 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1549 if( $conf->exists('selfservice_server-view-wholesale') ) {
1550 return { 'svcnum' => $session->{'svcnum'},
1551 'custnum' => $custnum,
1552 'cust_pkg' => [ map {
1554 part_pkg => [ map $_->hashref, $_->part_pkg ],
1556 [ map $_->hashref, $_->available_part_svc ],
1558 [ map { my $ref = { $_->hash,
1559 label => [ $_->label ],
1561 $ref->{_password} = $_->svc_x->_password
1562 if $context eq 'agent'
1563 && $conf->exists('agent-showpasswords')
1564 && $_->part_svc->svcdb eq 'svc_acct';
1569 } $cust_main->cust_pkg
1572 small_custview( $cust_main, $conf->config('countrydefault') ),
1573 'wholesale_view' => 1,
1574 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1575 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1576 'lnp' => $conf->exists('svc_phone-lnp'),
1580 { 'svcnum' => $session->{'svcnum'},
1581 'custnum' => $custnum,
1582 'cust_pkg' => [ map {
1583 my $primary_cust_svc = $_->primary_cust_svc;
1586 pkg_label => $_->pkg_label,
1587 status => $_->status,
1589 [ map $_->hashref, $_->available_part_svc ],
1591 [ map { my $ref = { $_->hash,
1592 label => [ $_->label ],
1594 $ref->{_password} = $_->svc_x->_password
1595 if $context eq 'agent'
1596 && $conf->exists('agent-showpasswords')
1597 && $_->part_svc->svcdb eq 'svc_acct';
1598 $ref->{svchash} = { $_->svc_x->hash } if
1599 $_->part_svc->svcdb eq 'svc_phone';
1600 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1601 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1607 ? { $primary_cust_svc->hash,
1608 label => [ $primary_cust_svc->label ],
1609 finger => $primary_cust_svc->svc_x->finger, #uuh
1610 $primary_cust_svc->part_svc->hash,
1614 } $cust_main->ncancelled_pkgs
1617 small_custview( $cust_main, $conf->config('countrydefault') ),
1625 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1626 return { 'error' => $session } if $context eq 'error';
1628 my $search = { 'custnum' => $custnum };
1629 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1630 my $cust_main = qsearchs('cust_main', $search )
1631 or return { 'error' => "unknown custnum $custnum" };
1633 my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
1634 if ( ! $pkgnum && $p->{'svcnum'} ) {
1635 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
1636 $pkgnum = $cust_svc->pkgnum if $cust_svc;
1640 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1641 foreach my $cust_pkg ( $p->{'ncancelled'}
1642 ? $cust_main->ncancelled_pkgs
1643 : $cust_main->unsuspended_pkgs ) {
1644 next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
1645 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1648 @cust_svc = grep { $_->part_svc->selfservice_access ne 'hidden' } @cust_svc;
1650 if ( $p->{'svcdb'} ) {
1651 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1653 : ref($p->{'svcdb'}) eq 'ARRAY'
1654 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1655 : { $p->{'svcdb'} => 1 };
1656 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1659 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1662 my $conf = new FS::Conf;
1665 'svcnum' => $session->{'svcnum'},
1666 'custnum' => $custnum,
1667 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1668 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1671 my $svc_x = $_->svc_x;
1672 my($label, $value) = $_->label;
1673 my $part_svc = $_->part_svc;
1674 my $svcdb = $part_svc->svcdb;
1675 my $cust_pkg = $_->cust_pkg;
1676 my $part_pkg = $cust_pkg->part_pkg;
1679 'svcnum' => $_->svcnum,
1680 'display_svcnum' => $_->display_svcnum,
1684 'pkg_label' => $cust_pkg->pkg_label,
1685 'pkg_status' => $cust_pkg->status,
1686 'readonly' => ($part_svc->selfservice_access eq 'readonly'),
1689 if ( $svcdb eq 'svc_acct' ) {
1692 'username' => $svc_x->username,
1693 'email' => $svc_x->email,
1694 'finger' => $svc_x->finger,
1695 'seconds' => $svc_x->seconds,
1696 'upbytes' => display_bytecount($svc_x->upbytes),
1697 'downbytes' => display_bytecount($svc_x->downbytes),
1698 'totalbytes' => display_bytecount($svc_x->totalbytes),
1700 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1701 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1702 'recharge_upbytes' =>
1703 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1704 'recharge_downbytes' =>
1705 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1706 'recharge_totalbytes' =>
1707 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1711 } elsif ( $svcdb eq 'svc_dsl' ) {
1712 $hash{'phonenum'} = $svc_x->phonenum;
1713 if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
1714 $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
1715 $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
1718 $hash{'name'} = $cust_main->name;
1721 # elsif ( $svcdb eq 'svc_phone' || $svcdb eq 'svc_port' ) {
1735 sub _customer_svc_x {
1736 my($custnum, $svcnum, $table) = (shift, shift, shift);
1737 my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
1739 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1740 my $search = " AND custnum = $1";
1741 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1744 'table' => ($table || 'svc_acct'),
1745 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1746 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
1747 #'LEFT JOIN cust_main USING ( custnum ) ',
1748 'hashref' => $hashref,
1749 'extra_sql' => $search, #important
1754 sub svc_status_html {
1757 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1758 return { 'error' => $session } if $context eq 'error';
1760 #XXX only svc_dsl for now
1761 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
1762 or return { 'error' => "Service not found" };
1764 my $html = $svc_x->getstatus_html;
1766 return { 'html' => $html };
1770 sub svc_status_hash {
1773 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1774 return { 'error' => $session } if $context eq 'error';
1776 #XXX only svc_acct for now
1777 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1778 or return { 'error' => "Service not found" };
1780 my ( $html, $hashref ) = $svc_x->export_getstatus;
1785 sub set_svc_status_hash {
1788 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1789 return { 'error' => $session } if $context eq 'error';
1791 #XXX only svc_acct for now
1792 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1793 or return { 'error' => "Service not found" };
1795 warn "set_svc_status_hash ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
1797 my $error = $svc_x->export_setstatus($p); #$p? returns error?
1798 return { 'error' => $error } if $error;
1800 return {}; #? { 'error' => '' }
1805 sub acct_forward_info {
1808 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1809 return { 'error' => $session } if $context eq 'error';
1811 my $svc_forward = _customer_svc_x( $custnum,
1812 { 'srcsvc' => $p->{'svcnum'} },
1815 or return { 'error' => '',
1819 return { 'error' => '',
1820 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
1825 sub process_acct_forward {
1827 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1828 return { 'error' => $session } if $context eq 'error';
1830 my $old = _customer_svc_x( $custnum,
1831 { 'srcsvc' => $p->{'svcnum'} },
1835 if ( $p->{'dst'} eq '' ) {
1837 my $error = $old->delete;
1838 return { 'error' => $error };
1840 return { 'error' => '' };
1843 my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
1844 'dst' => $p->{'dst'},
1849 $new->svcnum($old->svcnum);
1850 my $cust_svc = $old->cust_svc;
1851 $new->svcpart($old->svcpart);
1852 $new->pkgnuym($old->pkgnum);
1853 $error = $new->replace($old);
1855 my $conf = new FS::Conf;
1856 $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
1858 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
1859 or return { 'error' => 'No service' }; #how would we even get here?
1861 $new->pkgnum( $svc_acct->cust_svc->pkgnum );
1863 $error = $new->insert;
1866 return { 'error' => $error };
1870 sub list_dsl_devices {
1873 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1874 return { 'error' => $session } if $context eq 'error';
1876 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1877 or return { 'error' => "Service not found" };
1880 'devices' => [ map {
1881 +{ 'mac_addr' => $_->mac_addr };
1882 } $svc_dsl->dsl_device
1888 sub add_dsl_device {
1891 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1892 return { 'error' => $session } if $context eq 'error';
1894 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1895 or return { 'error' => "Service not found" };
1897 return { 'error' => 'No MAC address supplied' }
1898 unless length($p->{'mac_addr'});
1900 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
1901 'mac_addr' => scalar($p->{'mac_addr'}),
1903 my $error = $dsl_device->insert;
1904 return { 'error' => $error };
1908 sub delete_dsl_device {
1911 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1912 return { 'error' => $session } if $context eq 'error';
1914 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1915 or return { 'error' => "Service not found" };
1917 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
1918 'mac_addr' => scalar($p->{'mac_addr'}),
1921 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
1923 my $error = $dsl_device->delete;
1924 return { 'error' => $error };
1930 _usage_details( \&_port_graph, $p,
1931 'svcdb' => 'svc_port',
1936 my($svc_port, $begin, $end) = @_;
1938 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
1939 push @usage, { 'png' => $pngOrError };
1943 sub _list_svc_usage {
1944 my($svc_acct, $begin, $end) = @_;
1946 foreach my $part_export (
1947 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1948 qw( sqlradius sqlradius_withdomain )
1950 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1955 sub list_svc_usage {
1956 _usage_details(\&_list_svc_usage, @_);
1959 sub _list_support_usage {
1960 my($svc_acct, $begin, $end) = @_;
1962 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1963 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1965 push @usage, { 'seconds' => $_->seconds,
1966 'support' => $_->support,
1967 '_date' => $_->_date,
1968 'id' => $_->transaction_id,
1969 'creator' => $_->creator,
1970 'subject' => $_->subject,
1971 'status' => $_->status,
1972 'ticketid' => $_->ticketid,
1978 sub list_support_usage {
1979 _usage_details(\&_list_support_usage, @_);
1982 sub _list_cdr_usage {
1983 # XXX CDR type support...
1984 # XXX any way to do a paged search on this?
1985 # we have to return the results all at once...
1986 my($svc_phone, $begin, $end, %opt) = @_;
1987 map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
1988 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1991 sub list_cdr_usage {
1993 _usage_details( \&_list_cdr_usage, $p,
1994 'svcdb' => 'svc_phone',
1998 sub _usage_details {
1999 my($callback, $p, %opt) = @_;
2001 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2002 return { 'error' => $session } if $context eq 'error';
2004 my $search = { 'svcnum' => $p->{'svcnum'} };
2005 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2007 my $svcdb = $opt{'svcdb'} || 'svc_acct';
2009 my $svc_x = qsearchs( $svcdb, $search );
2010 return { 'error' => 'No service selected in list_svc_usage' }
2013 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
2014 my $freq = $cust_pkg->part_pkg->freq;
2017 if ( $svcdb eq 'svc_phone' ) {
2018 my $format = $cust_pkg->part_pkg->option('output_format') || '';
2019 $format = '' if $format =~ /^sum_/;
2020 # sensible default if there is no format or it's a summary format
2021 if ( $cust_pkg->part_pkg->plan eq 'voip_inbound' ) {
2022 $format ||= 'source_default';
2023 $callback_opt{inbound} = 1;
2026 $format ||= 'default';
2029 $callback_opt{format} = $format;
2030 $header = [ split(',', FS::cdr::invoice_header($format) ) ];
2033 my $start = $cust_pkg->setup;
2034 #my $end = $cust_pkg->bill; # or time?
2037 unless ( $p->{beginning} ) {
2038 $p->{beginning} = $cust_pkg->last_bill;
2039 $p->{ending} = $end;
2042 die "illegal beginning" if $p->{beginning} !~ /^\d*$/;
2043 die "illegal ending" if $p->{ending} !~ /^\d*$/;
2045 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
2049 #kinda false laziness with FS::cust_main::bill, but perhaps
2050 #we should really change this bit to DateTime and DateTime::Duration
2052 #change this bit to use Date::Manip? CAREFUL with timezones (see
2053 # mailing list archive)
2054 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
2055 (localtime($p->{ending}) )[0,1,2,3,4,5];
2056 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
2057 (localtime($p->{beginning}) )[0,1,2,3,4,5];
2059 if ( $freq =~ /^\d+$/ ) {
2061 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
2063 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
2064 } elsif ( $freq =~ /^(\d+)w$/ ) {
2066 $nmday += $weeks * 7;
2067 $pmday -= $weeks * 7;
2068 } elsif ( $freq =~ /^(\d+)d$/ ) {
2072 } elsif ( $freq =~ /^(\d+)h$/ ) {
2077 return { 'error' => "unparsable frequency: ". $freq };
2080 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
2081 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
2085 'svcnum' => $p->{svcnum},
2086 'beginning' => $p->{beginning},
2087 'ending' => $p->{ending},
2088 'previous' => ($previous > $start) ? $previous : $start,
2089 'next' => ($next < $end) ? $next : $end,
2090 'header' => $header,
2098 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2099 return { 'error' => $session } if $context eq 'error';
2101 my $search = { 'custnum' => $custnum };
2102 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2103 my $cust_main = qsearchs('cust_main', $search )
2104 or return { 'error' => "unknown custnum $custnum" };
2106 my $status = $cust_main->status;
2107 #false laziness w/ClientAPI/Signup.pm
2109 my $cust_pkg = new FS::cust_pkg ( {
2110 'custnum' => $custnum,
2111 'pkgpart' => $p->{'pkgpart'},
2113 my $error = $cust_pkg->check;
2114 return { 'error' => $error } if $error;
2117 unless ( $p->{'svcpart'} eq 'none' ) {
2121 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
2123 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2124 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
2125 $svcdb = $part_svc->svcdb;
2127 $svcdb = 'svc_acct';
2129 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
2132 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
2133 'svc_domain' => [ qw( domain ) ],
2134 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
2135 'svc_external' => [ qw( id title ) ],
2136 'svc_pbx' => [ qw( id title ) ],
2139 my $svc_x = "FS::$svcdb"->new( {
2140 'svcpart' => $svcpart,
2141 map { $_ => $p->{$_} } @{$fields{$svcdb}}
2144 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
2147 while ( length($p->{"snarf_machine$snarfnum"}) ) {
2148 my $acct_snarf = new FS::acct_snarf ( {
2149 'machine' => $p->{"snarf_machine$snarfnum"},
2150 'protocol' => $p->{"snarf_protocol$snarfnum"},
2151 'username' => $p->{"snarf_username$snarfnum"},
2152 '_password' => $p->{"snarf_password$snarfnum"},
2155 push @acct_snarf, $acct_snarf;
2157 $svc_x->child_objects( \@acct_snarf );
2160 my $y = $svc_x->setdefault; # arguably should be in new method
2161 return { 'error' => $y } if $y && !ref($y);
2163 $error = $svc_x->check;
2164 return { 'error' => $error } if $error;
2171 tie my %hash, 'Tie::RefHash';
2172 %hash = ( $cust_pkg => \@svc );
2174 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
2175 return { 'error' => $error } if $error;
2177 my $conf = new FS::Conf;
2178 if ( $conf->exists('signup_server-realtime') ) {
2180 my $bill_error = _do_bop_realtime( $cust_main, $status );
2183 $cust_pkg->cancel('quiet'=>1);
2186 $cust_pkg->reexport;
2190 $cust_pkg->reexport;
2193 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
2195 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
2202 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2203 return { 'error' => $session } if $context eq 'error';
2205 my $search = { 'custnum' => $custnum };
2206 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2207 my $cust_main = qsearchs('cust_main', $search )
2208 or return { 'error' => "unknown custnum $custnum" };
2210 my $status = $cust_main->status;
2211 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
2212 or return { 'error' => "unknown package $p->{pkgnum}" };
2215 my $error = FS::cust_pkg::order( $custnum,
2221 my $conf = new FS::Conf;
2222 if ( $conf->exists('signup_server-realtime') ) {
2224 my $bill_error = _do_bop_realtime( $cust_main, $status );
2227 $newpkg[0]->suspend;
2230 $newpkg[0]->reexport;
2234 $newpkg[0]->reexport;
2237 return { error => '', pkgnum => $cust_pkg->pkgnum };
2241 sub order_recharge {
2244 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2245 return { 'error' => $session } if $context eq 'error';
2247 my $search = { 'custnum' => $custnum };
2248 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2249 my $cust_main = qsearchs('cust_main', $search )
2250 or return { 'error' => "unknown custnum $custnum" };
2252 my $status = $cust_main->status;
2253 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
2254 or return { 'error' => "unknown service " . $p->{'svcnum'} };
2256 my $svc_x = $cust_svc->svc_x;
2257 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
2260 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
2261 qw ( recharge_seconds recharge_upbytes recharge_downbytes
2262 recharge_totalbytes );
2263 my $amount = $part_pkg->option('recharge_amount', 1);
2265 my ($l, $v, $d) = $cust_svc->label; # blah
2266 my $pkg = "Recharge $v";
2268 my $bill_error = $cust_main->charge($amount, $pkg,
2269 "time: $vhash{seconds}, up: $vhash{upbytes}," .
2270 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
2271 $part_pkg->taxclass); #meh
2273 my $conf = new FS::Conf;
2274 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
2276 $bill_error = _do_bop_realtime( $cust_main, $status );
2281 my $error = $svc_x->recharge (\%vhash);
2282 return { 'error' => $error } if $error;
2286 my $error = $bill_error;
2287 $error ||= $svc_x->recharge (\%vhash);
2288 return { 'error' => $error } if $error;
2291 return { error => '', svc => $cust_svc->part_svc->svc };
2295 sub _do_bop_realtime {
2296 my ($cust_main, $status) = (shift, shift);
2298 my $old_balance = $cust_main->balance;
2300 my $bill_error = $cust_main->bill
2301 || $cust_main->apply_payments_and_credits
2302 || $cust_main->realtime_collect('selfservice' => 1);
2304 if ( $cust_main->balance > $old_balance
2305 && $cust_main->balance > 0
2306 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
2307 1 : $status eq 'suspended' ) ) {
2308 #this makes sense. credit is "un-doing" the invoice
2309 my $conf = new FS::Conf;
2310 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
2311 'self-service decline',
2312 'reason_type' => $conf->config('signup_credit_type'),
2314 $cust_main->apply_credits( 'order' => 'newest' );
2316 return { 'error' => '_decline', 'bill_error' => $bill_error };
2325 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2326 return { 'error' => $session } if $context eq 'error';
2328 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2329 or return { 'error' => "unknown custnum $custnum" };
2331 my @cust_pkg = sort { $a->bill <=> $b->bill }
2332 grep { $_->part_pkg->freq ne '0' }
2333 $cust_main->ncancelled_pkgs;
2335 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
2337 my $total = $cust_main->balance;
2340 my $bill = $_->bill;
2341 $total += $_->part_pkg->base_recur($_, \$bill);
2342 my $renew_date = $_->part_pkg->add_freq($_->bill);
2344 'pkgnum' => $_->pkgnum,
2345 'amount' => sprintf('%.2f', $total),
2346 'bill_date' => $_->bill,
2347 'bill_date_pretty' => time2str('%x', $_->bill),
2348 'renew_date' => $renew_date,
2349 'renew_date_pretty' => time2str('%x', $renew_date),
2350 'expire_date' => $_->expire,
2351 'expire_date_pretty' => time2str('%x', $_->expire),
2356 return { 'dates' => \@array };
2360 sub payment_info_renew_info {
2362 my $renew_info = renew_info($p);
2363 my $payment_info = payment_info($p);
2364 return { %$renew_info,
2372 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2373 return { 'error' => $session } if $context eq 'error';
2375 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2376 or return { 'error' => "unknown custnum $custnum" };
2378 my $date = $p->{'date'};
2382 #freeside-daily -n -d $date fs_daily $custnum
2383 $cust_main->bill_and_collect( 'time' => $date,
2384 'invoice_time' => $now,
2385 'actual_time' => $now,
2386 'check_freq' => '1d',
2389 return { 'error' => '' };
2395 my $session = _cache->get($p->{'session_id'})
2396 or return { 'error' => "Can't resume session" }; #better error message
2398 my $custnum = $session->{'custnum'};
2400 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2401 or return { 'error' => "unknown custnum $custnum" };
2403 my $conf = new FS::Conf;
2405 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2406 or return { 'error' => 'Permission denied' };
2408 my $pkgnum = $p->{'pkgnum'};
2410 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2411 'pkgnum' => $pkgnum, } )
2412 or return { 'error' => "unknown pkgnum $pkgnum" };
2414 my $error = $cust_pkg->suspend(reason => $reasonnum);
2415 return { 'error' => $error };
2421 my $session = _cache->get($p->{'session_id'})
2422 or return { 'error' => "Can't resume session" }; #better error message
2424 my $custnum = $session->{'custnum'};
2426 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2427 or return { 'error' => "unknown custnum $custnum" };
2429 my $pkgnum = $p->{'pkgnum'};
2431 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2432 'pkgnum' => $pkgnum, } )
2433 or return { 'error' => "unknown pkgnum $pkgnum" };
2435 my $error = $cust_pkg->cancel('quiet' => 1);
2436 return { 'error' => $error };
2440 sub provision_phone {
2443 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2445 if($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/){
2446 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2447 return { 'error' => $session } if $context eq 'error';
2449 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2450 return { 'error' => 'service not found' } unless $svc_phone;
2451 return { 'error' => 'invalid svcnum' }
2452 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2454 $svc_phone->email($p->{'email'})
2455 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2456 $svc_phone->forwarddst($p->{'forwarddst'})
2457 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2458 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2459 return { 'error' => $svc_phone->replace };
2463 unless($p->{'lnp'}) {
2464 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2465 $p->{'lnp_status'} = "portingin";
2466 return _provision( 'FS::svc_phone',
2467 [qw(lnp_desired_due_date lnp_other_provider
2468 lnp_other_provider_account phonenum countrycode lnp_status)],
2469 [qw(phonenum countrycode)],
2476 unless (scalar(@bulkdid)) {
2477 return _provision( 'FS::svc_phone',
2478 [qw(phonenum countrycode)],
2479 [qw(phonenum countrycode)],
2485 # bulk DID order case
2487 foreach my $did ( @bulkdid ) {
2488 $did =~ s/[^0-9]//g;
2489 $error = _provision( 'FS::svc_phone',
2490 [qw(phonenum countrycode)],
2491 [qw(phonenum countrycode)],
2493 'pkgnum' => $p->{'pkgnum'},
2494 'svcpart' => $p->{'svcpart'},
2496 'countrycode' => $p->{'countrycode'},
2497 'session_id' => $p->{'session_id'},
2500 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2502 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2505 sub provision_acct {
2507 warn "provision_acct called\n"
2510 return { 'error' => gettext('passwords_dont_match') }
2511 if $p->{'_password'} ne $p->{'_password2'};
2512 return { 'error' => gettext('empty_password') }
2513 unless length($p->{'_password'});
2515 if ($p->{'domsvc'}) {
2516 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
2517 qw ( svcpart pkgnum ) );
2518 return { 'error' => gettext('invalid_domain') }
2519 unless ($domains{$p->{'domsvc'}});
2522 warn "provision_acct calling _provision\n"
2524 _provision( 'FS::svc_acct',
2525 [qw(username _password domsvc)],
2526 [qw(username _password domsvc)],
2532 sub provision_external {
2534 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
2535 _provision( 'FS::svc_external',
2544 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
2545 warn "_provision called for $class\n"
2548 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2549 return { 'error' => $session } if $context eq 'error';
2551 my $search = { 'custnum' => $custnum };
2552 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2553 my $cust_main = qsearchs('cust_main', $search )
2554 or return { 'error' => "unknown custnum $custnum" };
2556 my $pkgnum = $p->{'pkgnum'};
2558 warn "searching for custnum $custnum pkgnum $pkgnum\n"
2560 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2561 'pkgnum' => $pkgnum,
2563 or return { 'error' => "unknown pkgnum $pkgnum" };
2565 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
2567 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
2568 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
2570 warn "creating $class record\n"
2572 my $svc_x = $class->new( {
2573 'pkgnum' => $p->{'pkgnum'},
2574 'svcpart' => $p->{'svcpart'},
2575 map { $_ => $p->{$_} } @$fields
2577 warn "inserting $class record\n"
2579 my $error = $svc_x->insert;
2582 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
2584 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
2587 my $return = { 'svc' => $part_svc->svc,
2589 map { $_ => $svc_x->get($_) } @$return_fields
2591 warn "_provision returning ". Dumper($return). "\n"
2600 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2601 return { 'error' => $session } if $context eq 'error';
2603 my $search = { 'custnum' => $custnum };
2604 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2605 my $cust_main = qsearchs('cust_main', $search )
2606 or return { 'error' => "unknown custnum $custnum" };
2608 my $pkgnum = $p->{'pkgnum'};
2610 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2611 'pkgnum' => $pkgnum,
2613 or return { 'error' => "unknown pkgnum $pkgnum" };
2615 my $svcpart = $p->{'svcpart'};
2617 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
2618 'svcpart' => $svcpart, } )
2619 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
2620 my $part_svc = $pkg_svc->part_svc;
2622 my $conf = new FS::Conf;
2625 'svc' => $part_svc->svc,
2626 'svcdb' => $part_svc->svcdb,
2627 'pkgnum' => $pkgnum,
2628 'svcpart' => $svcpart,
2629 'custnum' => $custnum,
2631 'security_phrase' => 0, #XXX !
2632 'svc_acct_pop' => [], #XXX !
2634 'init_popstate' => '',
2639 small_custview( $cust_main, $conf->config('countrydefault') ),
2643 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
2644 && $ret->{'svcdb'} eq 'svc_phone') {
2645 $ret->{'svcnum'} = $p->{'svcnum'};
2646 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2647 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
2648 $ret->{'email'} = $svc_phone->email;
2649 $ret->{'forwarddst'} = $svc_phone->forwarddst;
2656 sub unprovision_svc {
2659 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2660 return { 'error' => $session } if $context eq 'error';
2662 my $search = { 'custnum' => $custnum };
2663 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2664 my $cust_main = qsearchs('cust_main', $search )
2665 or return { 'error' => "unknown custnum $custnum" };
2667 my $svcnum = $p->{'svcnum'};
2669 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
2670 or return { 'error' => "unknown svcnum $svcnum" };
2672 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
2673 unless $cust_svc->cust_pkg->custnum == $custnum;
2675 my $conf = new FS::Conf;
2677 return { 'svc' => $cust_svc->part_svc->svc,
2678 'error' => $cust_svc->cancel,
2680 small_custview( $cust_main, $conf->config('countrydefault') ),
2685 sub myaccount_passwd {
2687 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2688 return { 'error' => $session } if $context eq 'error';
2690 return { 'error' => "New passwords don't match." }
2691 if $p->{'new_password'} ne $p->{'new_password2'};
2693 return { 'error' => 'Enter new password' }
2694 unless length($p->{'new_password'});
2696 #my $search = { 'custnum' => $custnum };
2697 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2698 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2699 my $search = " AND custnum = $1";
2700 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2702 my $svc_acct = qsearchs( {
2703 'table' => 'svc_acct',
2704 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2705 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
2706 'LEFT JOIN cust_main USING ( custnum ) ',
2707 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
2708 'extra_sql' => $search, #important
2710 or return { 'error' => "Service not found" };
2712 if ( exists($p->{'old_password'}) ) {
2713 return { 'error' => "Incorrect password." }
2714 unless $svc_acct->check_password($p->{'old_password'});
2717 $svc_acct->set_password($p->{'new_password'});
2718 my $error = $svc_acct->replace();
2720 my($label, $value) = $svc_acct->cust_svc->label;
2722 return { 'error' => $error,
2732 my $conf = new FS::Conf;
2733 my $verification = $conf->config('selfservice-password_reset_verification')
2734 or return { 'error' => 'Password resets disabled' };
2736 my $username = $p->{'username'};
2738 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
2739 or return { 'error' => 'Account not found' };
2741 my $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
2742 'domsvc' => $svc_domain->svcnum }
2744 or return { 'error' => 'Account not found' };
2746 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
2747 or return { 'error' => 'Account not found' };
2749 my $cust_main = $cust_pkg->cust_main;
2753 my( $p, $cust_main ) = @_;
2754 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
2755 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
2758 my( $p, $cust_main ) = @_;
2759 my $cust_pay = qsearchs({
2760 'table' => 'cust_pay',
2761 'hashref' => { 'custnum' => $cust_main->custnum },
2762 'order_by' => 'ORDER BY _date DESC LIMIT 1',
2766 $p->{'amount'} == $cust_pay->paid;
2769 my( $p, $cust_main ) = @_;
2770 $p->{'zip'} eq $cust_main->zip
2771 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
2775 foreach my $verify ( split(',', $verification) ) {
2777 &{ $verify{$verify} }( $p, $cust_main )
2778 or return { 'error' => 'Account not found' };
2782 #okay, we're verified, now create a unique session
2784 my $reset_session = {
2785 'svcnum' => $svc_acct->svcnum,
2788 my $timeout = '1 hour'; #?
2790 my $reset_session_id;
2792 $reset_session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
2793 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") ); #just in case
2795 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
2799 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $cust_main->agentnum);
2800 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
2801 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
2802 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
2803 my $error = $msg_template->send( 'cust_main' => $cust_main,
2804 'object' => $svc_acct,
2805 'substitutions' => {
2806 'session_id' => $reset_session_id,
2810 return { 'error' => $error }; #????
2813 return { 'error' => '' };
2816 sub check_reset_passwd {
2819 my $conf = new FS::Conf;
2820 my $verification = $conf->config('selfservice-password_reset_verification')
2821 or return { 'error' => 'Password resets disabled' };
2823 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
2824 or return { 'error' => "Can't resume session" }; #better error message
2826 my $svcnum = $reset_session->{'svcnum'};
2828 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
2829 or return { 'error' => "Service not found" };
2831 return { 'error' => '',
2832 'username' => $svc_acct->username,
2837 sub process_reset_passwd {
2840 my $conf = new FS::Conf;
2841 my $verification = $conf->config('selfservice-password_reset_verification')
2842 or return { 'error' => 'Password resets disabled' };
2844 return { 'error' => "New passwords don't match." }
2845 if $p->{'new_password'} ne $p->{'new_password2'};
2847 return { 'error' => 'Enter new password' }
2848 unless length($p->{'new_password'});
2850 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
2851 or return { 'error' => "Can't resume session" }; #better error message
2853 my $svcnum = $reset_session->{'svcnum'};
2855 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
2856 or return { 'error' => "Service not found" };
2858 $svc_acct->set_password($p->{'new_password'});
2859 my $error = $svc_acct->replace();
2861 my($label, $value) = $svc_acct->cust_svc->label;
2863 return { 'error' => $error,
2872 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2873 return { 'error' => $session } if $context eq 'error';
2875 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
2876 FS::TicketSystem->init();
2878 my $conf = new FS::Conf;
2879 my $queue = $p->{'queue'}
2880 || $conf->config('ticket_system-selfservice_queueid')
2881 || $conf->config('ticket_system-default_queueid');
2883 warn "$me create_ticket: creating ticket\n" if $DEBUG;
2884 my $err_or_ticket = FS::TicketSystem->create_ticket(
2885 '', #create RT session based on FS CurrentUser (fs_selfservice)
2887 'custnum' => $custnum,
2888 'svcnum' => $session->{'svcnum'},
2889 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
2892 if ( ref($err_or_ticket) ) {
2893 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
2895 return { 'error' => '',
2896 'ticket_id' => $err_or_ticket->id,
2899 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
2901 return { 'error' => $err_or_ticket };
2909 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2910 return { 'error' => $session } if $context eq 'error';
2912 return { error => 'requested format not implemented' }
2913 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
2915 my $conf = new FS::Conf;
2916 my $age_threshold = 0;
2917 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
2918 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
2920 my $search = { 'custnum' => $custnum };
2921 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2922 my $cust_main = qsearchs('cust_main', $search )
2923 or return { 'error' => "unknown custnum $custnum" };
2925 # does it make more sense to just run one sql query for this instead of all the
2926 # insanity below? would increase performance greately for large data sets?
2928 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
2929 my @part_svc = $cust_pkg->part_svc;
2930 foreach my $part_svc ( @part_svc ) {
2931 if($part_svc->svcdb eq 'svc_phone'){
2932 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
2933 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
2934 push @svc_phone, $cust_pkg_svc->svc_x
2935 if $cust_pkg_svc->date_inserted >= $age_threshold;
2943 my($xls_r,$xls_c) = (0,0);
2946 my @fields = qw( countrycode phonenum pin sip_password phone_name );
2947 if($p->{'format'} eq 'csv') {
2948 $csv = new Text::CSV_XS { 'always_quote' => 1,
2951 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
2952 $content .= $csv->string;
2954 elsif($p->{'format'} eq 'xls') {
2955 my $XLS1 = new IO::Scalar \$content;
2956 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
2957 or return { 'error' => "Error opening .xls file: $!" };
2958 $xls = $xls_workbook->add_worksheet('DIDs');
2959 foreach ( @fields ) {
2960 $xls->write(0,$xls_c++,$_);
2965 foreach my $svc_phone ( @svc_phone ) {
2966 my @cols = map { $svc_phone->$_ } @fields;
2967 if($p->{'format'} eq 'csv') {
2968 return { 'error' => 'Unable to create CSV' }
2969 unless $csv->combine(@cols);
2970 $content .= $csv->string;
2972 elsif($p->{'format'} eq 'xls') {
2975 $xls->write($xls_r,$xls_c++,$_);
2981 $xls_workbook->close() if $p->{'format'} eq 'xls';
2983 { content => $content, format => $p->{'format'}, };
2988 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2989 return { 'error' => $session } if $context eq 'error';
2991 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
2992 FS::TicketSystem->init();
2993 return { 'error' => 'get_ticket configuration error' }
2994 if $FS::TicketSystem::system ne 'RT_Internal';
2996 # check existence and ownership as part of this
2997 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
2998 my $rt_session = FS::TicketSystem->session('');
2999 my $Ticket = FS::TicketSystem->get_ticket_object(
3001 ticket_id => $p->{'ticket_id'},
3004 return { 'error' => 'ticket not found' } if !$Ticket;
3006 if ( length( $p->{'subject'} || '' ) ) {
3008 if ( $p->{'subject'} ne $Ticket->Subject ) {
3009 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
3010 return { 'error' => "unable to set subject: $msg" } if !$val;
3014 if(length($p->{'reply'})) {
3015 my @err_or_res = FS::TicketSystem->correspond_ticket(
3017 'ticket_id' => $p->{'ticket_id'},
3018 'content' => $p->{'reply'},
3021 return { 'error' => 'unable to reply to ticket' }
3022 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
3025 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
3026 my $err_or_ticket = FS::TicketSystem->get_ticket(
3028 'ticket_id' => $p->{'ticket_id'},
3031 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
3032 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
3034 return { 'error' => $err_or_ticket };
3037 my @custs = @{$err_or_ticket->{'custs'}};
3038 my @txns = @{$err_or_ticket->{'txns'}};
3041 # superseded by check in get_ticket_object
3042 #return { 'error' => 'invalid ticket requested' }
3043 #unless grep($_ eq $custnum, @custs);
3045 foreach my $txn ( @txns ) {
3046 push @filtered_txns, $txn
3047 if ($txn->{'type'} eq 'EmailRecord'
3048 || $txn->{'type'} eq 'Correspond'
3049 || $txn->{'type'} eq 'Create');
3052 warn "$me get_ticket: successful: \n"
3054 return { 'error' => '',
3055 'transactions' => \@filtered_txns,
3056 'ticket_fields' => $err_or_ticket->{'fields'},
3057 'ticket_id' => $p->{'ticket_id'},
3061 sub adjust_ticket_priority {
3063 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3064 return { 'error' => $session } if $context eq 'error';
3066 warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
3067 FS::TicketSystem->init;
3068 my $ss_priority = FS::TicketSystem->selfservice_priority;
3070 return { 'error' => 'adjust_ticket_priority configuration error' }
3071 if $FS::TicketSystem::system ne 'RT_Internal'
3074 my $values = $p->{'values'}; #hashref, id => priority value
3077 foreach my $id (keys %$values) {
3078 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
3079 my $Ticket = FS::TicketSystem->get_ticket_object('',
3081 'custnum' => $custnum,
3084 $ticket_error{$id} = 'ticket not found';
3088 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
3089 # We're not going to implement it for RT_External.
3090 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
3091 my $new_value = $values->{$id};
3092 next if $old_value eq $new_value;
3094 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
3096 # AddCustomFieldValue works fine (replacing any existing value) if it's
3097 # a single-valued custom field, which it should be. If it's not, you're
3098 # doing something wrong.
3100 if ( length($new_value) ) {
3101 ($val, $msg) = $Ticket->AddCustomFieldValue(
3102 Field => $ss_priority,
3103 Value => $new_value,
3107 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
3108 Field => $ss_priority,
3109 Value => $old_value,
3113 $ticket_error{$id} = $msg if !$val;
3114 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
3116 return { 'error' => '',
3117 'ticket_error' => \%ticket_error,
3118 %{ customer_info($p) } # send updated customer info back
3124 sub _custoragent_session_custnum {
3127 my($context, $session, $custnum);
3128 if ( $p->{'session_id'} ) {
3130 $context = 'customer';
3131 $session = _cache->get($p->{'session_id'})
3132 or return ( 'error' => "Can't resume session" ); #better error message
3133 $custnum = $session->{'custnum'};
3135 } elsif ( $p->{'agent_session_id'} ) {
3138 my $agent_cache = new FS::ClientAPI_SessionCache( {
3139 'namespace' => 'FS::ClientAPI::Agent',
3141 $session = $agent_cache->get($p->{'agent_session_id'})
3142 or return ( 'error' => "Can't resume session" ); #better error message
3143 $custnum = $p->{'custnum'};
3147 return ( 'error' => "Can't resume session" ); #better error message
3150 ($context, $session, $custnum);