1 package FS::ClientAPI::MyAccount;
3 use 5.008; #require 5.8+ for Time::Local 1.05+
5 use vars qw( $cache $DEBUG $me );
6 use subs qw( _cache _provision );
9 use Digest::MD5 qw(md5_hex);
10 use Digest::SHA qw(sha512_hex);
13 use Time::Local qw(timelocal_nocheck);
14 use Business::CreditCard 0.35;
17 use Spreadsheet::WriteExcel;
18 use OLE::Storage_Lite;
19 use FS::UI::Web::small_custview qw(small_custview); #less doh
21 use FS::UI::bytecount qw( display_bytecount );
24 use FS::Record qw(qsearch qsearchs dbh);
25 use FS::Msgcat qw(gettext);
26 use FS::Misc qw(card_types money_pretty);
27 use FS::Misc::DateTime qw(parse_datetime);
29 use FS::ClientAPI_SessionCache;
41 use FS::legacy_cust_bill;
42 use FS::cust_main_county;
46 use FS::acct_rt_transaction;
49 use FS::cust_location;
51 # for code organization
52 use FS::ClientAPI::MyAccount::contact;
53 use FS::ClientAPI::MyAccount::quotation;
56 $me = '[FS::ClientAPI::MyAccount]';
58 use vars qw( @cust_main_editable_fields @location_editable_fields );
59 @cust_main_editable_fields = qw(
60 first last company daytime night fax mobile
62 payby payinfo payname paystart_month paystart_year payissue payip
63 ss paytype paystate stateid stateid_state
65 @location_editable_fields = qw(
66 address1 address2 city county state zip country
70 BEGIN { #preload to reduce time customer_info takes
71 if ( $FS::TicketSystem::system ) {
72 warn "$me: initializing ticket system\n" if $DEBUG;
73 FS::TicketSystem->init();
78 $cache ||= new FS::ClientAPI_SessionCache( {
79 'namespace' => 'FS::ClientAPI::MyAccount',
86 my($context, $session, $custnum) = _custoragent_session_custnum($p);
87 #return { 'error' => $session } if $context eq 'error';
90 if ( $context eq 'customer' ) {
92 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
95 $sth->execute($custnum) or die $sth->errstr;
97 $agentnum = $sth->fetchrow_arrayref->[0]
98 or die "no agentnum for custnum $custnum";
100 #} elsif ( $context eq 'agent' ) {
101 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
104 $p->{'agentnum'} = $agentnum;
106 my $conf = new FS::Conf;
108 #false laziness w/Signup.pm
110 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
112 if ( $skin_info_cache_agent ) {
114 warn "$me loading cached skin info for agentnum $agentnum\n"
119 warn "$me populating skin info cache for agentnum $agentnum\n"
122 $skin_info_cache_agent = {
123 'agentnum' => $agentnum,
124 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
125 qw( company_name date_format ) ),
126 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
127 qw( body_bgcolor box_bgcolor stripe1_bgcolor stripe2_bgcolor
128 text_color link_color vlink_color hlink_color alink_color
129 font title_color title_align title_size menu_bgcolor menu_fontsize
132 'menu_disable' => [ $conf->config('selfservice-menu_disable',$agentnum) ],
133 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
134 qw( menu_skipblanks menu_skipheadings menu_nounderline no_logo enable_payment_without_balance )
136 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
137 qw( title_left_image title_right_image
138 menu_top_image menu_body_image menu_bottom_image
141 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
142 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
143 qw( head body_header body_footer company_address ) ),
144 'money_char' => $conf->config("money_char") || '$',
145 'menu' => join("\n", $conf->config("ng_selfservice-menu", $agentnum ) ) ||
148 services.php Services
149 services.php My Services
150 services_new.php Order a new service
153 personal.php Personal Information
154 password.php Change Password
157 payment_cc.php Credit Card Payment
158 payment_ach.php Electronic Check Payment
159 payment_paypal.php PayPal Payment
160 payment_webpay.php Webpay Payments
163 usage_data.php Data usage
164 usage_cdr.php Call usage
166 tickets.php Help Desk
167 tickets.php Open Tickets
168 tickets_resolved.php Resolved Tickets
169 ticket_create.php Create a new ticket
177 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
181 #{ %$skin_info_cache_agent };
182 $skin_info_cache_agent;
186 sub get_mac_address {
189 ## access radius exports acct tables to get mac
190 my @part_export = ();
192 qsearch( 'part_export', { 'exporttype' => 'sqlradius' } ),
193 qsearch( 'part_export', { 'exporttype' => 'sqlradius_withdomain' } ),
194 qsearch( 'part_export', { 'exporttype' => 'broadband_sqlradius' } ),
198 foreach my $part_export (@part_export) {
199 push @sessions, ( @{ $part_export->usage_sessions( {
204 my $mac = $sessions[0]->{'callingstationid'};
206 return { 'mac_address' => $mac, };
212 my $conf = new FS::Conf;
216 'phone_login' => $conf->exists('selfservice_server-phone_login'),
217 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
218 'banner_url' => scalar($conf->config('selfservice-login_banner_url')),
219 'banner_image_md5' =>
220 md5_hex($conf->config_binary('selfservice-login_banner_image')),
227 sub login_banner_image {
229 my $conf = new FS::Conf;
230 my $image = $conf->config_binary('selfservice-login_banner_image');
232 'md5' => md5_hex($image),
237 #false laziness w/FS::ClientAPI::passwd::passwd
241 my $conf = new FS::Conf;
245 if ( $p->{'domain'} eq 'svc_phone'
246 && $conf->exists('selfservice_server-phone_login') ) {
248 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
249 return { error => 'Number not found.' } unless $svc_phone;
252 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
253 #return { error => 'Only primary user may log in.' }
254 # if $conf->exists('selfservice_server-primary_only')
255 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
257 return { error => 'Incorrect PIN.' }
258 unless $svc_phone->check_pin($p->{'password'});
262 } elsif ( $p->{'domain'} eq 'ip_mac' ) {
264 my $mac_address = $p->{'username'};
265 $mac_address =~ s/\://g;
267 my $svc_broadband = qsearchs( 'svc_broadband', { 'mac_addr' => $mac_address } );
268 return { error => 'MAC address not found '.$p->{'username'} }
269 unless $svc_broadband;
270 $svc_x = $svc_broadband;
272 } elsif ( $p->{email}
273 && (my $contact = FS::contact->by_selfservice_email($p->{email}))
276 return { error => 'Incorrect contact password.' }
277 unless $contact->authenticate_password($p->{'password'});
279 $session->{'contactnum'} = $contact->contactnum;
281 $session->{'custnum'} = $contact->custnum;
285 ( $p->{username}, $p->{domain} ) = split('@', $p->{email}) if $p->{email};
287 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
288 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
290 my @svc_acct = qsearch( 'svc_acct', { 'username' => $p->{'username'},
291 'domsvc' => $svc_domain->svcnum, }
294 if ( $conf->exists('selfservice_server-login_svcpart') ) {
295 my @svcpart = $conf->config('selfservice_server-login_svcpart');
296 @svc_acct = grep { my $svcpart = $_->cust_svc->svcpart;
297 scalar( grep( $_ eq $svcpart, @svcpart ) );
302 if ( $conf->exists('selfservice_server-primary_only') ) {
305 my $cust_svc = $_->cust_svc;
306 $cust_svc->cust_pkg->part_pkg->svcpart([qw( svc_acct svc_phone )])
307 == $cust_svc->svcpart
312 return { error => 'User not found.' } unless @svc_acct;
314 #return { error => 'Multiple users.' } if scalar(@svc_acct) > 1;
316 my $svc_acct = $svc_acct[0];
318 if ( $conf->exists('selfservice_server-login_svcpart') ) {
319 my @svcpart = $conf->config('selfservice_server-login_svcpart');
320 my $svcpart = $svc_acct->cust_svc->svcpart;
321 return { error => 'Invalid user.' }
322 unless grep($_ eq $svcpart, @svcpart);
325 return { error => 'Incorrect password.' }
326 unless $svc_acct->check_password($p->{'password'});
334 $session->{'svcnum'} = $svc_x->svcnum;
336 my $cust_svc = $svc_x->cust_svc;
337 my $cust_pkg = $cust_svc->cust_pkg;
339 my $cust_main = $cust_pkg->cust_main;
340 $session->{'custnum'} = $cust_main->custnum;
341 if ( $conf->exists('pkg-balances') ) {
342 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
343 $cust_main->ncancelled_pkgs;
344 $session->{'pkgnum'} = $cust_pkg->pkgnum
345 if scalar(@cust_pkg) > 1;
349 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
350 #return { error => 'Only primary user may log in.' }
351 # if $conf->exists('selfservice_server-primary_only')
352 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
353 my $part_pkg = $cust_pkg->part_pkg;
354 return { error => 'Only primary user may log in.' }
355 if $conf->exists('selfservice_server-primary_only')
356 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
362 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
363 } until ( ! defined _cache->get($session_id) ); #just in case
365 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
366 _cache->set( $session_id, $session, $timeout );
368 return { 'error' => '',
369 'session_id' => $session_id,
375 my $skin_info = skin_info($p);
376 if ( $p->{'session_id'} ) {
377 _cache->remove($p->{'session_id'});
378 return { %$skin_info, 'error' => '' };
380 return { %$skin_info, 'error' => "Can't resume session" }; #better error message
387 my($context, $session, $custnum) = _custoragent_session_custnum($p);
388 return { 'error' => $session } if $context eq 'error';
390 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
391 or return { 'error' => "Service not found" };
393 $session->{'svcnum'} = $svc_acct->svcnum;
395 my $conf = new FS::Conf;
396 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
397 _cache->set( $p->{'session_id'}, $session, $timeout );
399 return { 'error' => '' };
403 sub payment_gateway {
405 # takes a cust_main and a cust_payby entry, returns the payment_gateway
406 my $conf = new FS::Conf;
407 my $cust_main = shift;
408 my $cust_payby = shift;
409 my $gatewaynum = $conf->config('selfservice-payment_gateway');
411 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
412 die "configured gatewaynum $gatewaynum not found!" if !$pg;
416 return '' if ! FS::payby->realtime($cust_payby);
417 my $pg = $cust_main->agent->payment_gateway(
418 'method' => FS::payby->payby2bop($cust_payby),
428 my $conf = new FS::Conf;
430 my $info = skin_info($p);
432 use vars qw( $cust_paybys ); #cache for performance
433 unless ( $cust_paybys ) {
435 my %cust_paybys = map { $_ => 1 }
436 map { FS::payby->payby2payment($_) }
437 $conf->config('signup_server-payby');
439 $cust_paybys = [ keys %cust_paybys ];
442 $info->{'cust_paybys'} = $cust_paybys;
444 my($context, $session, $custnum) = _custoragent_session_custnum($p);
445 return { 'error' => $session } if $context eq 'error';
447 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
448 or return { 'error' => "unknown custnum $custnum" };
450 $info->{'hide_payment_fields'} = [
452 my $pg = payment_gateway($cust_main, $_);
453 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
454 } @{ $info->{cust_paybys} }
457 $info->{'self_suspend_reason'} =
458 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
460 $info->{'edit_ticket_subject'} =
461 $conf->exists('ticket_system-selfservice_edit_subject') &&
462 $cust_main->edit_subject;
464 $info->{'timeout'} = $conf->config('selfservice-timeout') || 3600;
466 $info->{'hide_usage'} = $conf->exists('selfservice_hide-usage');
469 'custnum' => $custnum,
470 'access_pkgnum' => $session->{'pkgnum'},
471 'access_svcnum' => $session->{'svcnum'},
478 my($context, $session, $custnum) = _custoragent_session_custnum($p);
479 return { 'error' => $session } if $context eq 'error';
483 my $conf = new FS::Conf;
484 $return{'require_address2'} = $conf->exists('cust_main-require_address2');
486 # if ( $FS::TicketSystem::system ) {
487 # warn "$me customer_info: initializing ticket system\n" if $DEBUG;
488 # FS::TicketSystem->init();
491 if ( $custnum ) { #customer record
493 %return = ( %return, %{ customer_info_short($p) } );
495 #redundant with customer_info_short, but we need it for several things below
496 my $search = { 'custnum' => $custnum };
497 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
498 my $cust_main = qsearchs('cust_main', $search )
499 or return { 'error' => "unknown custnum $custnum" };
501 my $list_tickets = list_tickets($p);
502 $return{'tickets'} = $list_tickets->{'tickets'};
504 if ( $session->{'pkgnum'} ) {
505 #XXX open invoices in the pkg-balances case
507 $return{'money_char'} = $conf->config("money_char") || '$';
510 invnum => $_->invnum,
511 date => time2str("%b %o, %Y", $_->_date),
513 charged => $_->charged,
515 } $cust_main->open_cust_bill;
516 $return{open_invoices} = \@open;
518 my $sql = 'SELECT MAX(_date) FROM cust_bill WHERE custnum = ?';
519 my $sth = dbh->prepare($sql) or die dbh->errstr;
520 $sth->execute($custnum) or die $sth->errstr;
521 $return{'last_invoice_date'} = $sth->fetchrow_arrayref->[0];
522 $return{'last_invoice_date_pretty'} =
523 time2str('%m/%d/%Y', $return{'last_invoice_date'} );
526 #customer_info_short always has nobalance on..
527 $return{small_custview} =
528 small_custview( $cust_main,
529 $return{countrydefault},
530 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
533 $return{has_ship_address} = $cust_main->has_ship_address;
534 $return{status} = $cust_main->status;
535 $return{statuscolor} = $cust_main->statuscolor;
537 # compatibility: some places in selfservice use this to determine
538 # if there's a ship address
539 if ( $return{has_ship_address} ) {
540 $return{ship_last} = $cust_main->last;
541 $return{ship_first} = $cust_main->first;
544 if (scalar($conf->config('support_packages'))) {
545 my @support_services = ();
546 foreach ($cust_main->support_services) {
547 my $seconds = $_->svc_x->seconds || 0;
548 my $time_remaining = (($seconds < 0) ? '-' : '' ).
549 int(abs($seconds)/3600)."h".
550 sprintf("%02d",(abs($seconds)%3600)/60)."m";
551 my $cust_pkg = $_->cust_pkg;
554 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
555 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
556 push @support_services, { svcnum => $_->svcnum,
557 time => $time_remaining,
562 $return{support_services} = \@support_services;
565 if ( $conf->config('prepayment_discounts-credit_type') ) {
567 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
570 } elsif ( $session->{'svcnum'} ) { #no customer record
572 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
573 or die "unknown svcnum";
574 $return{name} = $svc_acct->email;
578 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
582 return { 'error' => '',
583 'custnum' => $custnum,
589 sub customer_info_short {
592 my($context, $session, $custnum) = _custoragent_session_custnum($p);
593 return { 'error' => $session } if $context eq 'error';
597 my $conf = new FS::Conf;
599 if ( $custnum ) { #customer record
601 my $search = { 'custnum' => $custnum };
602 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
603 my $cust_main = qsearchs('cust_main', $search )
604 or return { 'error' => "unknown custnum $custnum" };
606 $return{display_custnum} = $cust_main->display_custnum;
608 if ( $session->{'pkgnum'} ) {
609 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
610 #next_bill_date from cust_pkg?
612 $return{balance} = $cust_main->balance;
613 $return{next_bill_date} = $cust_main->next_bill_date;
614 $return{next_bill_date_pretty} =
615 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
618 $return{balance_pretty} = money_pretty($return{balance});
620 $return{countrydefault} = scalar($conf->config('countrydefault'));
622 $return{small_custview} =
623 small_custview( $cust_main,
624 $return{countrydefault},
628 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
630 $return{payby} = $cust_main->payby;
632 #none of these are terribly expensive if we want 'em...
633 for (@cust_main_editable_fields) {
634 $return{$_} = $cust_main->get($_);
636 $return{$_} = $cust_main->masked($_) for qw/ss stateid/;
638 #maybe a little more expensive, but it should be cached by now
639 for (@location_editable_fields) {
640 $return{$_} = $cust_main->bill_location->get($_);
641 $return{'ship_'.$_} = $cust_main->ship_location->get($_);
644 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
645 $return{payinfo} = $cust_main->paymask;
646 @return{'month', 'year'} = $cust_main->paydate_monthyear;
649 my @invoicing_list = $cust_main->invoicing_list;
650 $return{'invoicing_list'} =
651 join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list );
652 $return{'postal_invoicing'} =
653 0 < ( grep { $_ eq 'POST' } @invoicing_list );
655 if ( $session->{'svcnum'} ) {
656 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
657 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
658 $return{'svcnum'} = $session->{'svcnum'};
661 } elsif ( $session->{'svcnum'} ) { #no customer record
663 #uuh, not supproted yet... die?
664 return { 'error' => 'customer_info_short not yet supported as agent' };
668 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
672 # this is here because this routine is called by both fs_ and ng_ main pages, where it appears
673 # it is not customer-specific, though it is only shown to authenticated customers
674 # it is not currently agent-specific, though at some point it might be
675 $return{'announcement'} = join(' ',$conf->config('selfservice-announcement')) || '';
677 return { 'error' => '',
678 'custnum' => $custnum,
683 sub customer_recurring {
686 my($context, $session, $custnum) = _custoragent_session_custnum($p);
687 return { 'error' => $session } if $context eq 'error';
691 my $conf = new FS::Conf;
693 my $search = { 'custnum' => $custnum };
694 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
695 my $cust_main = qsearchs('cust_main', $search )
696 or return { 'error' => "customer_info_short: unknown custnum $custnum" };
698 $return{'display_recurring'} = [ $cust_main->display_recurring ];
700 return { 'error' => '',
701 'custnum' => $custnum,
706 sub billing_history {
709 my($context, $session, $custnum) = _custoragent_session_custnum($p);
710 return { 'error' => $session } if $context eq 'error';
712 return { 'error' => 'No customer' } unless $custnum;
714 my $search = { 'custnum' => $custnum };
715 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
716 my $cust_main = qsearchs('cust_main', $search )
717 or return { 'error' => "unknown custnum $custnum" };
721 if ( $session->{'pkgnum'} ) {
722 #$return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
723 #next_bill_date from cust_pkg?
724 return { 'error' => 'No history for package' };
727 $return{balance} = $cust_main->balance;
728 $return{balance_pretty} = money_pretty($return{balance});
729 $return{next_bill_date} = $cust_main->next_bill_date;
730 $return{next_bill_date_pretty} =
731 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
734 my $conf = new FS::Conf;
736 $return{'history'} = [
737 $cust_main->payment_history(
738 'line_items' => $conf->exists('selfservice-billing_history-line_items'),
743 $return{'money_char'} = $conf->config("money_char") || '$',
751 my $session = _cache->get($p->{'session_id'})
752 or return { 'error' => "Can't resume session" }; #better error message
754 my $custnum = $session->{'custnum'}
755 or return { 'error' => "no customer record" };
757 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
758 or return { 'error' => "unknown custnum $custnum" };
760 my $conf = new FS::Conf;
761 if (($p->{payby} eq "CHEK" || $p->{payby} eq "DCHK") && $conf->exists('selfservice-ACH_info_readonly')) {
762 return { 'error' => "You do not have authority to add a bank account" };
765 my $new = new FS::cust_main { $cust_main->hash };
767 $new->set( $_ => $p->{$_} )
768 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
770 if ( exists($p->{address1}) ) {
771 my $bill_location = FS::cust_location->new({
772 map { $_ => $p->{$_} } @location_editable_fields
774 # if this is unchanged from before, cust_main::replace will ignore it
775 $new->set('bill_location' => $bill_location);
778 if ( exists($p->{ship_address1}) ) {
779 my $ship_location = FS::cust_location->new({
780 map { $_ => $p->{"ship_$_"} } @location_editable_fields
782 if ( !grep { length($p->{"ship_$_"}) } @location_editable_fields ) {
783 # Selfservice unfortunately tries to indicate "same as billing
784 # address" by sending all fields empty. Did this ever work?
785 $ship_location = $cust_main->bill_location;
787 $new->set('ship_location' => $ship_location);
789 # but if it hasn't been passed in at all, leave ship_location alone--
790 # DON'T change it to match bill_location.
793 if (exists($p->{'payby'})) {
794 $p->{'payby'} =~ /^([A-Z]{4})$/
795 or return { 'error' => "illegal_payby " . $p->{'payby'} };
799 if ( $payby =~ /^(CARD|DCRD)$/ ) {
801 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
803 if ( $new->payinfo eq $cust_main->paymask ) {
804 $new->payinfo($cust_main->payinfo);
805 $new->paycvv( $p->{'paycvv'} || $cust_main->paycvv );
807 $new->payinfo($p->{'payinfo'});
808 return { 'error' => 'CVV2 is required' }
809 if ! $p->{'paycvv'} && $conf->exists('selfservice-onfile_require_cvv');
810 $new->paycvv( $p->{'paycvv'} )
813 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
815 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
818 $p->{'payinfo1'} =~ /^([\dx]+)$/
819 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
821 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
822 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
824 $payinfo = $payinfo1. '@'. $payinfo2;
826 $new->payinfo( ($payinfo eq $cust_main->paymask)
827 ? $cust_main->payinfo
831 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
833 } elsif ( $payby =~ /^(BILL)$/ ) {
835 } elsif ( $payby ) { #notyet ready
836 return { 'error' => "unknown payby $payby" };
840 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
841 #false laziness with httemplate/edit/process/cust_main.cgi
842 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
843 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
845 @invoicing_list = $cust_main->invoicing_list;
848 my $error = $new->replace($cust_main, \@invoicing_list);
849 return { 'error' => $error } if $error;
852 return { 'error' => '' };
857 my $session = _cache->get($p->{'session_id'})
858 or return { 'error' => "Can't resume session" }; #better error message
864 my $conf = new FS::Conf;
865 use vars qw($payment_info); #cache for performance
866 unless ( $payment_info ) {
868 my %states = map { $_->state => 1 }
869 qsearch('cust_main_county', {
870 'country' => $conf->config('countrydefault') || 'US'
873 my %cust_paybys = map { $_ => 1 }
874 map { FS::payby->payby2payment($_) }
875 $conf->config('signup_server-payby');
877 my @cust_paybys = keys %cust_paybys;
881 #list all counties/states/countries
882 'cust_main_county' =>
883 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
885 #shortcut for one-country folks
887 [ sort { $a cmp $b } keys %states ],
889 'card_types' => card_types(),
891 'withcvv' => $conf->exists('selfservice-require_cvv'), #or enable optional cvv?
892 'require_cvv' => $conf->exists('selfservice-require_cvv'),
893 'onfile_require_cvv' => $conf->exists('selfservice-onfile_require_cvv'),
895 'paytypes' => [ @FS::cust_main::paytypes ],
897 'paybys' => [ $conf->config('signup_server-payby') ],
898 'cust_paybys' => \@cust_paybys,
900 'stateid_label' => FS::Msgcat::_gettext('stateid'),
901 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
903 'show_ss' => $conf->exists('show_ss'),
904 'show_stateid' => $conf->exists('show_stateid'),
905 'show_paystate' => $conf->exists('show_bankstate'),
907 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
908 'ach_read_only' => $conf->exists('selfservice-ACH_info_readonly'),
918 my %return = %$payment_info;
920 delete $return{'cust_main_county'} if $p->{'omit_cust_main_county'};
922 my $custnum = $session->{'custnum'};
924 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
925 or return { 'error' => "unknown custnum $custnum" };
927 $return{'hide_payment_fields'} = [
929 my $pg = payment_gateway($cust_main, $_);
930 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
931 } @{ $return{cust_paybys} }
934 $return{balance} = $cust_main->balance; #XXX pkg-balances?
936 $return{payname} = $cust_main->payname
937 || ( $cust_main->first. ' '. $cust_main->get('last') );
939 $return{$_} = $cust_main->bill_location->get($_)
940 for qw(address1 address2 city state zip);
942 $return{payby} = $cust_main->payby;
943 $return{stateid_state} = $cust_main->stateid_state;
945 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
946 $return{card_type} = cardtype($cust_main->payinfo);
947 $return{payinfo} = $cust_main->paymask;
949 @return{'month', 'year'} = $cust_main->paydate_monthyear;
953 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
954 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
955 $return{payinfo1} = $payinfo1;
956 $return{payinfo2} = $payinfo2;
957 $return{paytype} = $cust_main->paytype;
958 $return{paystate} = $cust_main->paystate;
959 $return{payname} = $cust_main->payname; # override 'first/last name' default from above, if any. Is instution-name here. (#15819)
962 if ( $conf->config('prepayment_discounts-credit_type') ) {
964 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
967 #doubleclick protection
969 $return{payunique} = "webui-MyAccount-$_date-$$-". rand() * 2**32; #new
970 $return{paybatch} = $return{payunique}; #back compat
972 $return{credit_card_surcharge_percentage} = $conf->config('credit-card-surcharge-percentage', $cust_main->agentnum);
973 $return{credit_card_surcharge_flatfee} = $conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum);
975 return { 'error' => '',
981 #some false laziness with httemplate/process/payment.cgi - look there for
982 #ACH and CVV support stuff
984 sub validate_payment {
987 my $session = _cache->get($p->{'session_id'})
988 or return { 'error' => "Can't resume session" }; #better error message
990 my $custnum = $session->{'custnum'};
992 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
993 or return { 'error' => "unknown custnum $custnum" };
995 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
996 or return { 'error' => gettext('illegal_amount') };
998 return { error => 'Amount must be greater than 0' } unless $amount > 0;
1000 #false laziness w/tr-amount_fee.html, but we don't want selfservice users
1001 #changing the hidden form values
1002 my $conf = new FS::Conf;
1003 my $fee_display = $conf->config('selfservice_process-display') || 'add';
1004 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
1005 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
1006 if ( $fee_display eq 'add'
1008 and ! $fee_skip_first || scalar($cust_main->cust_pay)
1011 my $fee_pkg = qsearchs('part_pkg', { pkgpart=>$fee_pkgpart } );
1012 $amount = sprintf('%.2f', $amount + $fee_pkg->option('setup_fee') );
1015 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
1016 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
1017 my $discount_term = $1;
1019 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
1020 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
1023 $p->{'payunique'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
1024 or return { 'error' => gettext('illegal_text'). " payunique: ". $p->{'payunique'} };
1027 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
1028 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
1031 $payunique = $paybatch if ! length($payunique) && length($paybatch);
1033 $p->{'payby'} ||= 'CARD';
1034 $p->{'payby'} =~ /^([A-Z]{4})$/
1035 or return { 'error' => "illegal_payby " . $p->{'payby'} };
1038 #false laziness w/process/payment.cgi
1041 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
1043 $p->{'payinfo1'} =~ /^([\dx]+)$/
1044 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1046 $p->{'payinfo2'} =~ /^([\dx]+)$/
1047 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1049 $payinfo = $payinfo1. '@'. $payinfo2;
1051 $payinfo = $cust_main->payinfo
1052 if $cust_main->paymask eq $payinfo;
1055 if ( $cust_main->paymask eq $payinfo ) {
1056 $payinfo = $cust_main->payinfo;
1060 if ($conf->exists('selfservice-ACH_info_readonly') && !$achonfile) {
1061 return { 'error' => "You are not allowed to change your payment information." };
1064 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
1066 $payinfo = $p->{'payinfo'};
1070 #more intelligent matching will be needed here if you change
1071 #card_masking_method and don't remove existing paymasks
1072 if ( $cust_main->paymask eq $payinfo ) {
1073 $payinfo = $cust_main->payinfo;
1077 $payinfo =~ s/\D//g;
1078 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
1079 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1083 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1084 return { 'error' => gettext('unknown_card_type') }
1085 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
1087 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
1088 if ( cardtype($payinfo) eq 'American Express card' ) {
1089 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
1090 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
1093 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
1094 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
1097 } elsif ( $conf->exists('selfservice-onfile_require_cvv') ) {
1098 return { 'error' => 'CVV2 is required' };
1099 } elsif ( !$onfile && $conf->exists('selfservice-require_cvv') ) {
1100 return { 'error' => 'CVV2 is required' };
1104 die "unknown payby $payby";
1107 my %payby2fields = (
1108 'CARD' => [ qw( paystart_month paystart_year payissue payip
1109 address1 address2 city state zip country ) ],
1110 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
1114 $card_type = cardtype($payinfo) if $payby eq 'CARD';
1117 'cust_main' => $cust_main, #XXX or just custnum??
1118 'amount' => sprintf('%.2f', $amount),
1120 'payinfo' => $payinfo,
1121 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
1122 'card_type' => $card_type,
1123 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
1124 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
1125 'month' => $p->{'month'},
1126 'year' => $p->{'year'},
1127 'payname' => $payname,
1128 'payunique' => $payunique,
1129 'paybatch' => $paybatch,
1130 'paycvv' => $paycvv,
1131 'payname' => $payname,
1132 'discount_term' => $discount_term,
1133 'pkgnum' => $session->{'pkgnum'},
1134 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
1144 my $validate = validate_payment($p);
1145 return $validate if $validate->{'error'};
1147 my $conf = new FS::Conf;
1148 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
1149 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
1151 +{ map { $_=>$validate->{$_} }
1152 qw( card_type paymask payname paydate_pretty month year amount
1153 address1 address2 city state zip country
1159 sub process_stored_payment {
1162 my $session_id = $p->{'session_id'};
1164 my $payment_info = _cache->get( "payment_$session_id" )
1165 or return { 'error' => "Can't resume session" }; #better error message
1167 do_process_payment($payment_info);
1171 sub process_payment {
1174 my $payment_info = validate_payment($p);
1175 return $payment_info if $payment_info->{'error'};
1177 do_process_payment($payment_info);
1181 sub do_process_payment {
1182 my $validate = shift;
1184 my $cust_main = $validate->{'cust_main'};
1186 my $amount = delete $validate->{'amount'};
1189 my $payby = delete $validate->{'payby'};
1191 if ( $validate->{'save'} ) {
1192 my $new = new FS::cust_main { $cust_main->hash };
1193 if ($payby eq 'CARD' || $payby eq 'DCRD') {
1194 $new->set( $_ => $validate->{$_} )
1195 foreach qw( payname paystart_month paystart_year payissue payip );
1196 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
1198 my $bill_location = FS::cust_location->new({
1199 map { $_ => $validate->{$_} }
1200 qw(address1 address2 city state country zip)
1202 $new->set('bill_location' => $bill_location);
1203 # but don't allow the service address to change this way.
1205 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
1206 $new->set( $_ => $validate->{$_} )
1207 foreach qw( payname payip paytype paystate
1208 stateid stateid_state );
1209 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
1211 $new->payinfo( $validate->{'payinfo'} ); #to properly set paymask
1212 $new->set( 'paydate' => $validate->{'paydate'} );
1213 my $error = $new->replace($cust_main);
1215 #no, this causes customers to process their payments again
1216 #return { 'error' => $error };
1217 #XXX just warn verosely for now so i can figure out how these happen in
1218 # the first place, eventually should redirect them to the "change
1219 #address" page but indicate if the payment processed?
1220 delete($validate->{'payinfo'}); #don't want to log this!
1221 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
1222 "NEW: ". Dumper($new)."\n".
1223 "OLD: ". Dumper($cust_main)."\n".
1224 "PACKET: ". Dumper($validate)."\n";
1230 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
1234 'paynum_ref' => \$paynum,
1237 return { 'error' => $error } if $error;
1239 #no error, so order the fee package if applicable...
1240 my $conf = new FS::Conf;
1241 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
1242 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
1244 if ( $fee_pkgpart and ! $fee_skip_first || scalar($cust_main->cust_pay) ) {
1246 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $fee_pkgpart };
1248 $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
1249 return { 'error' => "payment processed successfully, but error ordering fee: $error" }
1252 #and generate an invoice for it now too
1253 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1254 return { 'error' => "payment processed and fee ordered successfully, but error billing fee: $error" }
1259 $cust_main->apply_payments;
1262 my $receipt_html = '';
1264 # currently supported for realtime CC only; send receipt data to SS
1265 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
1268 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
1271 <TD ALIGN="right">Payment#</TD>
1272 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
1276 <TD ALIGN="right">Date</TD>
1278 <TD BGCOLOR="#FFFFFF"><B>! .
1279 time2str("%a %b %o, %Y %r", $cust_pay->_date)
1285 <TD ALIGN="right">Amount</TD>
1286 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
1291 <TD ALIGN="right">Payment method</TD>
1292 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
1305 'amount' => sprintf('%.2f', $cust_pay->paid),
1306 'date' => $cust_pay->_date,
1307 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
1308 'time_pretty' => time2str('%T', $cust_pay->_date),
1309 'auth_num' => $cust_pay->auth,
1310 'order_num' => $cust_pay->order_number,
1311 'receipt_html' => $receipt_html,
1318 'receipt_html' => '',
1325 sub realtime_collect {
1328 my $session = _cache->get($p->{'session_id'})
1329 or return { 'error' => "Can't resume session" }; #better error message
1331 my $custnum = $session->{'custnum'};
1333 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1334 or return { 'error' => "unknown custnum $custnum" };
1337 if ( $p->{'amount'} ) {
1338 $amount = $p->{'amount'};
1340 elsif ( $session->{'pkgnum'} ) {
1341 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1344 $amount = $cust_main->balance;
1347 my $error = $cust_main->realtime_collect(
1348 'method' => $p->{'method'},
1349 'amount' => $amount,
1350 'pkgnum' => $session->{'pkgnum'},
1351 'session_id' => $p->{'session_id'},
1355 return { 'error' => $error } unless ref( $error );
1357 return { 'error' => '', amount => $amount, %$error };
1360 sub start_thirdparty {
1362 my $session = _cache->get($p->{'session_id'})
1363 or return { 'error' => "Can't resume session" }; #better error message
1364 my $custnum = $session->{'custnum'};
1365 my $cust_main = FS::cust_main->by_key($custnum);
1367 my $amount = $p->{'amount'}
1368 or return { error => 'no amount' };
1370 my $result = $cust_main->create_payment(
1371 'method' => $p->{'method'},
1372 'amount' => $p->{'amount'},
1373 'pkgnum' => $session->{'pkgnum'},
1374 'session_id' => $p->{'session_id'},
1377 if ( ref($result) ) { # hashref or error
1380 return { error => $result };
1384 sub finish_thirdparty {
1386 my $session_id = delete $p->{'session_id'};
1387 my $session = _cache->get($session_id)
1388 or return { 'error' => "Can't resume session" };
1389 my $custnum = $session->{'custnum'};
1390 my $cust_main = FS::cust_main->by_key($custnum);
1392 if ( $p->{_cancel} ) {
1393 # customer backed out of making a payment
1394 return $cust_main->cancel_payment( $session_id );
1396 my $result = $cust_main->execute_payment( $session_id, %$p );
1397 if ( ref($result) ) {
1400 return { error => $result };
1404 sub process_payment_order_pkg {
1407 my $hr = process_payment($p);
1408 return $hr if $hr->{'error'};
1413 sub process_payment_order_renew {
1416 my $hr = process_payment($p);
1417 return $hr if $hr->{'error'};
1422 sub process_prepay {
1426 my $session = _cache->get($p->{'session_id'})
1427 or return { 'error' => "Can't resume session" }; #better error message
1431 my $custnum = $session->{'custnum'};
1433 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1434 or return { 'error' => "unknown custnum $custnum" };
1436 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1437 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1445 return { 'error' => $error } if $error;
1447 return { 'error' => '',
1448 'amount' => $amount,
1449 'seconds' => $seconds,
1450 'duration' => duration_exact($seconds),
1451 'upbytes' => $upbytes,
1452 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1453 'downbytes' => $downbytes,
1454 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1455 'totalbytes'=> $totalbytes,
1456 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1463 my $session = _cache->get($p->{'session_id'})
1464 or return { 'error' => "Can't resume session" }; #better error message
1466 my $custnum = $session->{'custnum'};
1468 my $invnum = $p->{'invnum'};
1470 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1471 'custnum' => $custnum } )
1472 or return { 'error' => "Can't find invnum" };
1476 return { 'error' => '',
1477 'invnum' => $invnum,
1478 'invoice_text' => join('', $cust_bill->print_text ),
1479 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1486 my $session = _cache->get($p->{'session_id'})
1487 or return { 'error' => "Can't resume session" }; #better error message
1489 my $custnum = $session->{'custnum'};
1491 my $invnum = $p->{'invnum'};
1493 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1494 'custnum' => $custnum } )
1495 or return { 'error' => "Can't find invnum" };
1499 return { 'error' => '',
1500 'invnum' => $invnum,
1501 'invoice_pdf' => $cust_bill->print_pdf({
1502 'unsquelch_cdr' => 1,
1503 'locale' => $p->{'locale'},
1509 sub legacy_invoice {
1511 my $session = _cache->get($p->{'session_id'})
1512 or return { 'error' => "Can't resume session" }; #better error message
1514 my $custnum = $session->{'custnum'};
1516 my $legacyinvnum = $p->{'legacyinvnum'};
1519 'legacyinvnum' => $legacyinvnum,
1520 'custnum' => $custnum,
1523 my $legacy_cust_bill =
1524 qsearchs('legacy_cust_bill', { %hash, 'locale' => $p->{'locale'} } )
1525 || qsearchs('legacy_cust_bill', \%hash )
1526 or return { 'error' => "Can't find legacyinvnum" };
1530 return { 'error' => '',
1531 'legacyinvnum' => $legacyinvnum,
1532 'legacyid' => $legacy_cust_bill->legacyid,
1533 'invoice_html' => $legacy_cust_bill->content_html,
1538 sub legacy_invoice_pdf {
1540 my $session = _cache->get($p->{'session_id'})
1541 or return { 'error' => "Can't resume session" }; #better error message
1543 my $custnum = $session->{'custnum'};
1545 my $legacyinvnum = $p->{'legacyinvnum'};
1547 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1548 'legacyinvnum' => $legacyinvnum,
1549 'custnum' => $custnum,
1550 }) or return { 'error' => "Can't find legacyinvnum" };
1554 return { 'error' => '',
1555 'legacyinvnum' => $legacyinvnum,
1556 'legacyid' => $legacy_cust_bill->legacyid,
1557 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1565 #sessioning for this? how do we get the session id to the backend invoice
1566 # template so it can add it to the link, blah
1569 if ( $p->{'invnum'} ) {
1570 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1571 or return { 'error' => 'unknown invnum' };
1572 $agentnum = $cust_bill->cust_main->agentnum;
1575 my $templatename = $p->{'template'} || $p->{'templatename'};
1577 #false laziness-ish w/view/cust_bill-logo.cgi
1579 my $conf = new FS::Conf;
1580 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1581 $templatename = "_$1";
1586 my $filename = "logo$templatename.png";
1588 return { 'error' => '',
1589 'logo' => $conf->config_binary($filename, $agentnum),
1590 'content_type' => 'image/png', #should allow gif, jpg too
1597 my $session = _cache->get($p->{'session_id'})
1598 or return { 'error' => "Can't resume session" }; #better error message
1600 my $custnum = $session->{'custnum'};
1602 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1603 or return { 'error' => "unknown custnum $custnum" };
1605 my $conf = new FS::Conf;
1607 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1609 my @cust_bill = grep ! $_->hide, $cust_main->cust_bill;
1614 #not super efficient, we also run cust_bill_pay/cust_credited inside owed
1615 my @payments_and_credits = sort {$b->_date <=> $a->_date} ($_->cust_bill_pay,$_->cust_credited);
1616 my $owed = $_->owed;
1618 +{ 'invnum' => $_->invnum,
1619 '_date' => $_->_date,
1620 'date' => time2str("%b %o, %Y", $_->_date),
1621 'date_short' => time2str("%m-%d-%Y", $_->_date),
1622 'previous' => sprintf('%.2f', ($_->previous)[0]),
1623 'charged' => sprintf('%.2f', $_->charged),
1624 'owed' => sprintf('%.2f', $owed),
1625 'balance' => sprintf('%.2f', $balance),
1626 'lastpay' => @payments_and_credits
1627 ? time2str("%b %o, %Y", $payments_and_credits[0]->_date)
1633 return { 'error' => '',
1634 'balance' => $cust_main->balance,
1635 'money_char' => $conf->config("money_char") || '$',
1636 'invoices' => $invoices,
1637 'legacy_invoices' => [
1639 +{ 'legacyinvnum' => $_->legacyinvnum,
1640 'legacyid' => $_->legacyid,
1641 '_date' => $_->_date,
1642 'date' => time2str("%b %o, %Y", $_->_date),
1643 'date_short' => time2str("%m-%d-%Y", $_->_date),
1644 'charged' => sprintf('%.2f', $_->charged),
1645 'has_content' => ( length($_->content_pdf)
1646 || length($_->content_html) ),
1656 my $session = _cache->get($p->{'session_id'})
1657 or return { 'error' => "Can't resume session" }; #better error message
1659 my $custnum = $session->{'custnum'};
1661 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1662 or return { 'error' => "unknown custnum $custnum" };
1664 return { 'error' => '',
1665 'balance' => $cust_main->balance,
1666 'money_char' => FS::Conf->new->config("money_char") || '$',
1667 'payments' => [ map $_->SSAPI_getinfo, $cust_main->cust_pay ],
1671 sub payment_receipt {
1673 my $session = _cache->get($p->{'session_id'})
1674 or return { 'error' => "Can't resume session" }; #better error message
1676 my $custnum = $session->{'custnum'};
1678 my $cust_pay = qsearchs('cust_pay', { 'custnum' => $custnum,
1679 'paynum' => $p->{'paynum'},
1682 or return { 'error' => "unknown payment ". $p->{'paynum'} };
1686 %{ $cust_pay->SSAPI_getinfo },
1693 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1694 return { 'error' => $session } if $context eq 'error';
1696 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1697 or return { 'error' => "unknown custnum $custnum" };
1701 my $cust_payby = $_;
1703 map { $_ => $cust_payby->$_ }
1704 qw( custpaybynum weight payby paymask paydate
1705 payname paystate paytype
1709 $cust_main->cust_payby
1717 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1718 return { 'error' => $session } if $context eq 'error';
1720 #XXX payinfo1 + payinfo2 for CHEK?
1721 #or take the opportunity to use separate, more well- named fields?
1723 # $p->{'payinfo1'} =~ /^([\dx]+)$/
1724 # or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1725 # my $payinfo1 = $1;
1726 # $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
1727 # or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1728 # my $payinfo2 = $1;
1729 # $payinfo = $payinfo1. '@'. $payinfo2;
1731 my $cust_payby = new FS::cust_payby {
1732 'custnum' => $custnum,
1733 map { $_ => $p->{$_} } qw( weight payby payinfo paycvv paydate payname
1734 paystate paytype payip
1738 my $error = $cust_payby->insert;
1740 return { 'error' => $error };
1742 return { 'custpaybynum' => $cust_payby->custpaybynum };
1750 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1751 return { 'error' => $session } if $context eq 'error';
1753 my $cust_payby = qsearchs('cust_payby', {
1754 'custnum' => $custnum,
1755 'custpaybynum' => $p->{'custpaybynum'},
1757 or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
1760 qw( weight payby payinfo paycvv paydate payname paystate paytype payip )
1762 next unless exists($p->{$field});
1763 $cust_payby->set($field,$p->{$field});
1766 my $error = $cust_payby->replace;
1768 return { 'error' => $error };
1770 return { 'custpaybynum' => $cust_payby->custpaybynum };
1778 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1779 return { 'error' => $session } if $context eq 'error';
1781 my $cust_payby = qsearchs('cust_payby', {
1782 'custnum' => $custnum,
1783 'custpaybynum' => $p->{'custpaybynum'},
1785 or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
1787 return { 'error' => $cust_payby->verify };
1794 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1795 return { 'error' => $session } if $context eq 'error';
1797 my $cust_payby = qsearchs('cust_payby', {
1798 'custnum' => $custnum,
1799 'custpaybynum' => $p->{'custpaybynum'},
1801 or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
1803 my $conf = new FS::Conf;
1804 if (($cust_payby->payby eq "DCHK" || $cust_payby->payby eq "CHEK") && $conf->exists('selfservice-ACH_info_readonly')) {
1805 return { 'error' => "Sorry you do not have permission to delete bank information." };
1808 return { 'error' => $cust_payby->delete };
1814 my $session = _cache->get($p->{'session_id'})
1815 or return { 'error' => "Can't resume session" }; #better error message
1817 my $custnum = $session->{'custnum'};
1819 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1820 or return { 'error' => "unknown custnum $custnum" };
1822 my @errors = $cust_main->cancel( 'quiet'=>1 );
1824 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1826 return { 'error' => $error };
1833 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1834 return { 'error' => $session } if $context eq 'error';
1836 my $pkg = qsearchs({
1837 'table' => 'cust_pkg',
1838 'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
1840 'custnum' => $custnum,
1841 'pkgnum' => $p->{'pkgnum'},
1844 or return {'error' => 'unknown pkg num $pkgnum'};
1847 pkg_label => $pkg->pkg,
1848 pkgpart => $pkg->pkgpart,
1849 classnum => $pkg->classnum,
1857 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1858 return { 'error' => $session } if $context eq 'error';
1860 my $search = { 'custnum' => $custnum };
1861 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1862 my $cust_main = qsearchs('cust_main', $search )
1863 or return { 'error' => "unknown custnum $custnum" };
1865 my $conf = new FS::Conf;
1866 my $immutable = $conf->exists('selfservice_immutable-package');
1868 # the duplication below is necessary:
1869 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1870 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1871 # non-backwards-compatible change breaking the software of anyone using the API
1872 # instead of the stock selfservice
1873 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1874 if( $conf->exists('selfservice_server-view-wholesale') ) {
1875 return { 'svcnum' => $session->{'svcnum'},
1876 'custnum' => $custnum,
1877 'cust_pkg' => [ map {
1879 immutable => $immutable,
1880 part_pkg => [ map $_->hashref, $_->part_pkg ],
1882 [ map $_->hashref, $_->available_part_svc ],
1884 [ map { my $ref = { $_->hash,
1885 label => [ $_->label ],
1887 $ref->{_password} = $_->svc_x->_password
1888 if $context eq 'agent'
1889 && $conf->exists('agent-showpasswords')
1890 && $_->part_svc->svcdb eq 'svc_acct';
1895 } $cust_main->cust_pkg
1898 small_custview( $cust_main, $conf->config('countrydefault') ),
1899 'wholesale_view' => 1,
1900 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1901 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1902 'lnp' => $conf->exists('svc_phone-lnp'),
1906 { 'svcnum' => $session->{'svcnum'},
1907 'custnum' => $custnum,
1908 'cust_pkg' => [ map {
1909 my $primary_cust_svc = $_->primary_cust_svc;
1912 immutable => $immutable,
1913 pkg_label => $_->pkg_locale,
1914 status => $_->status,
1915 statuscolor => $_->statuscolor,
1917 [ map { $_->hashref }
1918 grep { $_->selfservice_access ne 'hidden' }
1919 $_->available_part_svc
1922 [ map { my $ref = { $_->hash,
1923 label => [ $_->label ],
1925 $ref->{_password} = $_->svc_x->_password
1926 if $context eq 'agent'
1927 && $conf->exists('agent-showpasswords')
1928 && $_->part_svc->svcdb eq 'svc_acct';
1929 $ref->{svchash} = { $_->svc_x->hash } if
1930 $_->part_svc->svcdb eq 'svc_phone';
1931 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1932 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1935 grep { $_->part_svc->selfservice_access ne 'hidden' }
1940 ? { $primary_cust_svc->hash,
1941 label => [ $primary_cust_svc->label ],
1942 finger => $primary_cust_svc->svc_x->finger, #uuh
1943 $primary_cust_svc->part_svc->hash,
1947 } $cust_main->ncancelled_pkgs
1950 small_custview( $cust_main, $conf->config('countrydefault') ),
1951 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1959 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1960 return { 'error' => $session } if $context eq 'error';
1962 my $conf = new FS::Conf;
1964 my $hide_usage = $conf->exists('selfservice_hide-usage') ? 1 : 0;
1965 my $search = { 'custnum' => $custnum };
1966 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1967 my $cust_main = qsearchs('cust_main', $search )
1968 or return { 'error' => "unknown custnum $custnum" };
1970 my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
1971 if ( ! $pkgnum && $p->{'svcnum'} ) {
1972 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
1973 $pkgnum = $cust_svc->pkgnum if $cust_svc;
1977 my @cust_pkg_usage = ();
1978 foreach my $cust_pkg ( $p->{'ncancelled'}
1979 ? $cust_main->ncancelled_pkgs
1980 : $cust_main->unsuspended_pkgs ) {
1981 next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
1982 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1983 push @cust_pkg_usage, $cust_pkg->cust_pkg_usage;
1986 @cust_svc = grep { $_->part_svc->selfservice_access ne 'hidden' } @cust_svc;
1989 foreach (@cust_pkg_usage) {
1990 my $part = $_->part_pkg_usage;
1991 my $tag = $part->description . ($part->shared ? 1 : 0);
1992 my $row = $usage_pools{$tag}
1993 ||= [ $part->description, 0, 0, $part->shared ? 1 : 0 ];
1994 $row->[1] += sprintf('%.1f', $_->minutes); # minutes remaining
1995 $row->[2] += $part->minutes; # minutes total
1997 } # otherwise just leave them empty
1999 if ( $p->{'svcdb'} ) {
2000 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
2002 : ref($p->{'svcdb'}) eq 'ARRAY'
2003 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
2004 : { $p->{'svcdb'} => 1 };
2005 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
2008 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
2011 my @svcs; # stuff to return to the client
2012 my %bytes_used_total; # for _used columns only
2013 foreach my $cust_svc (@cust_svc) {
2014 my $svc_x = $cust_svc->svc_x;
2015 my($label, $value) = $cust_svc->label;
2016 my $part_svc = $cust_svc->part_svc;
2017 my $svcdb = $part_svc->svcdb;
2018 my $cust_pkg = $cust_svc->cust_pkg;
2019 my $part_pkg = $cust_pkg->part_pkg;
2022 'svcnum' => $cust_svc->svcnum,
2023 'display_svcnum' => $cust_svc->display_svcnum,
2027 'pkg_label' => $cust_pkg->pkg_locale,
2028 'pkg_status' => $cust_pkg->status,
2029 'readonly' => ($part_svc->selfservice_access eq 'readonly'),
2032 # would it make sense to put this in a svc_* method?
2034 if (!$hide_usage and grep(/^$svcdb$/, qw(svc_acct svc_broadband)) and $part_svc->part_export_usage) {
2035 my $last_bill = $cust_pkg->last_bill || 0;
2037 my $up_used = $cust_svc->attribute_since_sqlradacct($last_bill,$now,'AcctInputOctets');
2038 my $down_used = $cust_svc->attribute_since_sqlradacct($last_bill,$now,'AcctOutputOctets');
2041 'seconds_used' => $cust_svc->seconds_since_sqlradacct($last_bill,$now),
2042 'upbytes_used' => display_bytecount($up_used),
2043 'downbytes_used' => display_bytecount($down_used),
2044 'totalbytes_used' => display_bytecount($up_used + $down_used)
2046 $bytes_used_total{'seconds_used'} += $hash{'seconds_used'};
2047 $bytes_used_total{'upbytes_used'} += $up_used;
2048 $bytes_used_total{'downbytes_used'} += $down_used;
2049 $bytes_used_total{'totalbytes_used'} += $up_used + $down_used;
2052 if ( $svcdb eq 'svc_acct' ) {
2053 foreach (qw(username email finger seconds)) {
2054 $hash{$_} = $svc_x->$_;
2060 'upbytes' => display_bytecount($svc_x->upbytes),
2061 'downbytes' => display_bytecount($svc_x->downbytes),
2062 'totalbytes' => display_bytecount($svc_x->totalbytes),
2064 'recharge_amount' => $part_pkg->option('recharge_amount',1),
2065 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
2066 'recharge_upbytes' =>
2067 display_bytecount($part_pkg->option('recharge_upbytes',1)),
2068 'recharge_downbytes' =>
2069 display_bytecount($part_pkg->option('recharge_downbytes',1)),
2070 'recharge_totalbytes' =>
2071 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
2076 } elsif ( $svcdb eq 'svc_dsl' ) {
2078 $hash{'phonenum'} = $svc_x->phonenum;
2079 if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
2080 $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
2081 $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
2084 $hash{'name'} = $cust_main->name;
2086 # no usage to hide here
2088 } elsif ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
2090 # could potentially show lots of things...
2091 $hash{'outbound'} = 1;
2092 $hash{'inbound'} = 0;
2093 if ( $svcdb eq 'svc_phone' ) {
2094 if ( $part_pkg->plan eq 'voip_inbound' ) {
2095 $hash{'outbound'} = 0;
2096 $hash{'inbound'} = 1;
2097 } elsif ( $part_pkg->option('selfservice_inbound_format')
2098 or $conf->config('selfservice-default_inbound_cdr_format')
2100 $hash{'inbound'} = 1;
2103 foreach (qw(inbound outbound)) {
2104 # hmm...we can't filter by status here, because there might
2105 # not be cdr_terminations at all. have to go by date.
2106 # find all since the last bill date.
2107 # XXX cdr types? we are going to need them.
2109 my $sum_cdr = $svc_x->sum_cdrs(
2110 'inbound' => ( $_ eq 'inbound' ? 1 : 0 ),
2111 'begin' => ($cust_pkg->last_bill || 0),
2113 'disable_charged_party' => 1,
2115 $hash{$_} = $sum_cdr->hashref;
2118 } # not hiding usage
2122 } # foreach $cust_svc
2124 foreach my $field (keys %bytes_used_total) {
2125 if ($field =~ /bytes/) {
2126 $bytes_used_total{$field} = display_bytecount($bytes_used_total{$field});
2131 'svcnum' => $session->{'svcnum'},
2132 'custnum' => $custnum,
2133 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
2134 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
2136 'bytes_used_total' => \%bytes_used_total,
2138 map { $usage_pools{$_} }
2142 'hide_usage' => $hide_usage,
2147 sub _customer_svc_x {
2148 my($custnum, $svcnum, $table) = (shift, shift, shift);
2149 my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
2151 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2152 my $search = " AND custnum = $1";
2153 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2156 'table' => ($table || 'svc_acct'),
2157 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2158 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
2159 #'LEFT JOIN cust_main USING ( custnum ) ',
2160 'hashref' => $hashref,
2161 'extra_sql' => $search, #important
2166 sub svc_status_html {
2169 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2170 return { 'error' => $session } if $context eq 'error';
2172 #XXX only svc_dsl for now
2173 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
2174 or return { 'error' => "Service not found" };
2176 my $html = $svc_x->getstatus_html;
2178 return { 'html' => $html };
2182 sub svc_status_hash {
2185 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2186 return { 'error' => $session } if $context eq 'error';
2188 #XXX only svc_acct for now
2189 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
2190 or return { 'error' => "Service not found" };
2192 my ( $html, $hashref ) = $svc_x->export_getstatus;
2197 sub set_svc_status_hash { _svc_method_X(shift, 'export_setstatus') }
2198 sub set_svc_status_listadd { _svc_method_X(shift, 'export_setstatus_listadd') }
2199 sub set_svc_status_listdel { _svc_method_X(shift, 'export_setstatus_listdel') }
2200 sub set_svc_status_vacationadd { _svc_method_X(shift, 'export_setstatus_vacationadd') }
2201 sub set_svc_status_vacationdel { _svc_method_X(shift, 'export_setstatus_vacationdel') }
2204 my( $p, $method ) = @_;
2206 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2207 return { 'error' => $session } if $context eq 'error';
2209 #XXX only svc_acct for now
2210 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
2211 or return { 'error' => "Service not found" };
2213 warn "$method ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
2215 my $error = $svc_x->$method($p); #$p? returns error?
2216 return { 'error' => $error } if $error;
2218 return {}; #? { 'error' => '' }
2222 sub acct_forward_info {
2225 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2226 return { 'error' => $session } if $context eq 'error';
2228 my $svc_forward = _customer_svc_x( $custnum,
2229 { 'srcsvc' => $p->{'svcnum'} },
2232 or return { 'error' => '',
2236 return { 'error' => '',
2237 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
2242 sub process_acct_forward {
2244 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2245 return { 'error' => $session } if $context eq 'error';
2247 my $old = _customer_svc_x( $custnum,
2248 { 'srcsvc' => $p->{'svcnum'} },
2252 if ( $p->{'dst'} eq '' ) {
2254 my $error = $old->delete;
2255 return { 'error' => $error };
2257 return { 'error' => '' };
2260 my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
2261 'dst' => $p->{'dst'},
2266 $new->svcnum($old->svcnum);
2267 my $cust_svc = $old->cust_svc;
2268 $new->svcpart($old->svcpart);
2269 $new->pkgnuym($old->pkgnum);
2270 $error = $new->replace($old);
2272 my $conf = new FS::Conf;
2273 $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
2275 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
2276 or return { 'error' => 'No service' }; #how would we even get here?
2278 $new->pkgnum( $svc_acct->cust_svc->pkgnum );
2280 $error = $new->insert;
2283 return { 'error' => $error };
2287 sub list_dsl_devices {
2290 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2291 return { 'error' => $session } if $context eq 'error';
2293 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2294 or return { 'error' => "Service not found" };
2297 'devices' => [ map {
2298 +{ 'mac_addr' => $_->mac_addr };
2299 } $svc_dsl->dsl_device
2305 sub add_dsl_device {
2308 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2309 return { 'error' => $session } if $context eq 'error';
2311 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2312 or return { 'error' => "Service not found" };
2314 return { 'error' => 'No MAC address supplied' }
2315 unless length($p->{'mac_addr'});
2317 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
2318 'mac_addr' => scalar($p->{'mac_addr'}),
2320 my $error = $dsl_device->insert;
2321 return { 'error' => $error };
2325 sub delete_dsl_device {
2328 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2329 return { 'error' => $session } if $context eq 'error';
2331 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2332 or return { 'error' => "Service not found" };
2334 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
2335 'mac_addr' => scalar($p->{'mac_addr'}),
2338 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
2340 my $error = $dsl_device->delete;
2341 return { 'error' => $error };
2347 _usage_details( \&_port_graph, $p,
2348 'svcdb' => 'svc_port',
2353 my($svc_port, $begin, $end) = @_;
2355 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
2356 push @usage, { 'png' => $pngOrError };
2360 sub _list_svc_usage {
2361 my($svc_acct, $begin, $end) = @_;
2363 foreach my $part_export (
2364 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
2365 qw( sqlradius sqlradius_withdomain )
2367 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
2372 sub list_svc_usage {
2373 _usage_details(\&_list_svc_usage, @_);
2376 sub _list_support_usage {
2377 my($svc_acct, $begin, $end) = @_;
2379 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
2380 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
2382 push @usage, { 'seconds' => $_->seconds,
2383 'support' => $_->support,
2384 '_date' => $_->_date,
2385 'id' => $_->transaction_id,
2386 'creator' => $_->creator,
2387 'subject' => $_->subject,
2388 'status' => $_->status,
2389 'ticketid' => $_->ticketid,
2395 sub list_support_usage {
2396 _usage_details(\&_list_support_usage, @_);
2399 sub _list_cdr_usage {
2400 # XXX CDR type support...
2401 # XXX any way to do a paged search on this?
2402 # we have to return the results all at once...
2403 my($svc_x, $begin, $end, %opt) = @_;
2404 map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
2408 'disable_charged_party' => 1,
2413 sub list_cdr_usage {
2415 _usage_details( \&_list_cdr_usage, $p );
2418 sub _usage_details {
2419 my($callback, $p, %opt) = @_;
2420 my $conf = FS::Conf->new;
2422 if ( $conf->exists('selfservice_hide-usage') ) {
2423 return { 'error' => 'Viewing usage is not allowed.' };
2426 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2427 return { 'error' => $session } if $context eq 'error';
2429 my $search = { 'svcnum' => $p->{'svcnum'} };
2430 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2432 my $cust_svc = qsearchs( 'cust_svc', $search );
2433 return { 'error' => 'No service selected in list_svc_usage' }
2436 my $svc_x = $cust_svc->svc_x;
2437 my $svcdb = $svc_x->table;
2438 my $cust_pkg = $cust_svc->cust_pkg;
2439 my $freq = $cust_pkg->part_pkg->freq;
2442 if ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
2444 if ( $p->{inbound} ) {
2445 $format = $cust_pkg->part_pkg->option('selfservice_inbound_format')
2446 || $conf->config('selfservice-default_inbound_cdr_format')
2447 || 'source_default';
2448 $callback_opt{inbound} = 1;
2450 $format = $cust_pkg->part_pkg->option('selfservice_format')
2451 || $conf->config('selfservice-default_cdr_format')
2455 $callback_opt{format} = $format;
2456 $callback_opt{use_clid} = 1;
2457 $header = [ split(',', FS::cdr::invoice_header($format) ) ];
2460 my $start = $cust_pkg->setup;
2461 #my $end = $cust_pkg->bill; # or time?
2464 unless ( $p->{beginning} ) {
2465 $p->{beginning} = $cust_pkg->last_bill;
2466 $p->{ending} = $end;
2469 die "illegal beginning" if $p->{beginning} !~ /^\d*$/;
2470 die "illegal ending" if $p->{ending} !~ /^\d*$/;
2472 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
2476 if ( $conf->exists('selfservice-hide_cdr_price') ) {
2477 # ugly kludge, I know
2478 my ($delete_col) = grep { $header->[$_] eq 'Price' } (0..scalar(@$header));
2479 if (defined $delete_col) {
2480 delete($_->[$delete_col]) foreach ($header, @usage);
2484 #kinda false laziness with FS::cust_main::bill, but perhaps
2485 #we should really change this bit to DateTime and DateTime::Duration
2487 #change this bit to use Date::Manip? CAREFUL with timezones (see
2488 # mailing list archive)
2489 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
2490 (localtime($p->{ending}) )[0,1,2,3,4,5];
2491 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
2492 (localtime($p->{beginning}) )[0,1,2,3,4,5];
2494 if ( $freq =~ /^\d+$/ ) {
2496 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
2498 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
2499 } elsif ( $freq =~ /^(\d+)w$/ ) {
2501 $nmday += $weeks * 7;
2502 $pmday -= $weeks * 7;
2503 } elsif ( $freq =~ /^(\d+)d$/ ) {
2507 } elsif ( $freq =~ /^(\d+)h$/ ) {
2512 return { 'error' => "unparsable frequency: ". $freq };
2515 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
2516 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
2520 'svcnum' => $p->{svcnum},
2521 'beginning' => $p->{beginning},
2522 'ending' => $p->{ending},
2523 'inbound' => $p->{inbound},
2524 'previous' => ($previous > $start) ? $previous : $start,
2525 'next' => ($next < $end) ? $next : $end,
2526 'header' => $header,
2534 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2535 return { 'error' => $session } if $context eq 'error';
2537 my $search = { 'custnum' => $custnum };
2538 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2539 my $cust_main = qsearchs('cust_main', $search )
2540 or return { 'error' => "unknown custnum $custnum" };
2542 my $status = $cust_main->status;
2544 my %order_pkg_options = ();
2545 if ( $p->{locationnum} > 0 ) {
2546 $order_pkg_options{locationnum} = delete($p->{locationnum});
2547 } elsif ( $p->{address1} ) {
2548 $order_pkg_options{'cust_location'} = new FS::cust_location {
2549 map { $_ => $p->{$_} }
2550 qw( address1 address2 city county state zip country )
2554 #false laziness w/ClientAPI/Signup.pm
2556 my $cust_pkg = new FS::cust_pkg ( {
2557 'custnum' => $custnum,
2558 'pkgpart' => $p->{'pkgpart'},
2559 'quantity' => $p->{'quantity'} || 1,
2561 my $error = $cust_pkg->check;
2562 return { 'error' => $error } if $error;
2565 unless ( $p->{'svcpart'} eq 'none' ) {
2569 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
2571 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2572 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
2573 $svcdb = $part_svc->svcdb;
2575 $svcdb = 'svc_acct';
2577 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
2580 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
2581 'svc_domain' => [ qw( domain ) ],
2582 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
2583 'svc_external' => [ qw( id title ) ],
2584 'svc_pbx' => [ qw( id title ) ],
2587 my $svc_x = "FS::$svcdb"->new( {
2588 'svcpart' => $svcpart,
2589 map { $_ => $p->{$_} } @{$fields{$svcdb}}
2592 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
2595 while ( length($p->{"snarf_machine$snarfnum"}) ) {
2596 my $acct_snarf = new FS::acct_snarf ( {
2597 'machine' => $p->{"snarf_machine$snarfnum"},
2598 'protocol' => $p->{"snarf_protocol$snarfnum"},
2599 'username' => $p->{"snarf_username$snarfnum"},
2600 '_password' => $p->{"snarf_password$snarfnum"},
2603 push @acct_snarf, $acct_snarf;
2605 $svc_x->child_objects( \@acct_snarf );
2608 my $y = $svc_x->setdefault; # arguably should be in new method
2609 return { 'error' => $y } if $y && !ref($y);
2611 $error = $svc_x->check;
2612 return { 'error' => $error } if $error;
2618 $error = $cust_main->order_pkg(
2619 'cust_pkg' => $cust_pkg,
2624 return { 'error' => $error } if $error;
2626 my $conf = new FS::Conf;
2627 if ( $conf->exists('signup_server-realtime') ) {
2629 my $bill_error = _do_bop_realtime( $cust_main, $status, 'collect'=>$p->{run_bill_events} );
2632 $cust_pkg->cancel('quiet'=>1);
2635 $cust_pkg->reexport;
2639 $cust_pkg->reexport;
2642 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
2644 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
2651 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2652 return { 'error' => $session } if $context eq 'error';
2654 my $conf = new FS::Conf;
2655 my $immutable = $conf->exists('selfservice_immutable-package');
2656 return { 'error' => "Package modification disabled" } if $immutable;
2658 my $search = { 'custnum' => $custnum };
2659 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2660 my $cust_main = qsearchs('cust_main', $search )
2661 or return { 'error' => "unknown custnum $custnum" };
2663 my $status = $cust_main->status;
2664 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
2665 or return { 'error' => "unknown package $p->{pkgnum}" };
2667 #if someone does need self-service package change of suspended packages,
2668 # figure out how to be more discriminating
2669 return { error=>"Can't change a suspended package", pkgnum=>$cust_pkg->pkgnum}
2670 if $cust_pkg->status eq 'suspended';
2672 my $err_or_cust_pkg = $cust_pkg->change( 'pkgpart' => $p->{'pkgpart'},
2673 'quantity' => $p->{'quantity'} || 1,
2676 my $new_pkg = qsearchs('part_pkg', { 'pkgpart' => $p->{pkgpart} } )
2677 or return { 'error' => "unknown package $p->{pkgpart}" };
2679 return { error=>$err_or_cust_pkg, pkgnum=>$cust_pkg->pkgnum }
2680 unless ref($err_or_cust_pkg);
2683 if ( $conf->exists('signup_server-realtime') ) {
2685 my $bill_error = _do_bop_realtime( $cust_main, $status, 'no_invoice_void'=>1 );
2688 $err_or_cust_pkg->suspend;
2691 $err_or_cust_pkg->reexport;
2695 $err_or_cust_pkg->reexport;
2698 return { error => '', pkg => $new_pkg->pkg, pkgnum => $err_or_cust_pkg->pkgnum };
2702 sub order_recharge {
2705 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2706 return { 'error' => $session } if $context eq 'error';
2708 my $search = { 'custnum' => $custnum };
2709 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2710 my $cust_main = qsearchs('cust_main', $search )
2711 or return { 'error' => "unknown custnum $custnum" };
2713 my $status = $cust_main->status;
2714 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
2715 or return { 'error' => "unknown service " . $p->{'svcnum'} };
2717 my $svc_x = $cust_svc->svc_x;
2718 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
2721 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
2722 qw ( recharge_seconds recharge_upbytes recharge_downbytes
2723 recharge_totalbytes );
2724 my $amount = $part_pkg->option('recharge_amount', 1);
2726 my ($l, $v, $d) = $cust_svc->label; # blah
2727 my $pkg = "Recharge $v";
2729 my $bill_error = $cust_main->charge($amount, $pkg,
2730 "time: $vhash{seconds}, up: $vhash{upbytes}," .
2731 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
2732 $part_pkg->taxclass); #meh
2734 my $conf = new FS::Conf;
2735 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
2737 $bill_error = _do_bop_realtime( $cust_main, $status );
2742 my $error = $svc_x->recharge (\%vhash);
2743 return { 'error' => $error } if $error;
2747 my $error = $bill_error;
2748 $error ||= $svc_x->recharge (\%vhash);
2749 return { 'error' => $error } if $error;
2752 return { error => '', svc => $cust_svc->part_svc->svc };
2756 sub _do_bop_realtime {
2757 my ($cust_main, $status, %opt) = @_;
2759 my $old_balance = $cust_main->balance;
2762 my $bill_error = $cust_main->bill(
2763 'return_bill' => \@cust_bill,
2766 $bill_error ||= $cust_main->apply_payments_and_credits;
2768 $bill_error ||= $cust_main->realtime_collect('selfservice' => 1)
2769 if $cust_main->payby =~ /^(CARD|CHEK)$/;
2771 if ( $cust_main->balance > $old_balance
2772 && $cust_main->balance > 0
2773 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/
2774 || $status eq 'suspended'
2778 unless ( $opt{'no_invoice_void'} ) {
2780 #this used to apply a credit, but now we can void invoices...
2781 foreach my $cust_bill (@cust_bill) {
2782 my $voiderror = $cust_bill->void('automatic payment failed');
2783 warn "Error voiding cust bill after decline: $voiderror" if $voiderror;
2788 return { 'error' => '_decline', 'bill_error' => $bill_error };
2791 if ( $opt{'collect'} ) {
2792 my $collect_error = $cust_main->collect();
2793 return { 'error' => '_decline', 'bill_error' => $collect_error }
2794 if $collect_error; #?
2803 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2804 return { 'error' => $session } if $context eq 'error';
2806 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2807 or return { 'error' => "unknown custnum $custnum" };
2809 my @cust_pkg = sort { $a->bill <=> $b->bill }
2810 grep { $_->part_pkg->freq ne '0' }
2811 $cust_main->ncancelled_pkgs;
2813 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
2815 my $total = $cust_main->balance;
2818 my $bill = $_->bill;
2819 $total += $_->part_pkg->base_recur($_, \$bill);
2820 my $renew_date = $_->part_pkg->add_freq($_->bill);
2822 'pkgnum' => $_->pkgnum,
2823 'amount' => sprintf('%.2f', $total),
2824 'bill_date' => $_->bill,
2825 'bill_date_pretty' => time2str('%x', $_->bill),
2826 'renew_date' => $renew_date,
2827 'renew_date_pretty' => time2str('%x', $renew_date),
2828 'expire_date' => $_->expire,
2829 'expire_date_pretty' => time2str('%x', $_->expire),
2834 return { 'dates' => \@array };
2838 sub payment_info_renew_info {
2840 my $renew_info = renew_info($p);
2841 my $payment_info = payment_info($p);
2842 return { %$renew_info,
2850 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2851 return { 'error' => $session } if $context eq 'error';
2853 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2854 or return { 'error' => "unknown custnum $custnum" };
2856 my $date = $p->{'date'};
2860 #freeside-daily -n -d $date fs_daily $custnum
2861 $cust_main->bill_and_collect( 'time' => $date,
2862 'invoice_time' => $now,
2863 'actual_time' => $now,
2864 'check_freq' => '1d',
2867 return { 'error' => '' };
2873 my $session = _cache->get($p->{'session_id'})
2874 or return { 'error' => "Can't resume session" }; #better error message
2876 my $custnum = $session->{'custnum'};
2878 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2879 or return { 'error' => "unknown custnum $custnum" };
2881 my $conf = new FS::Conf;
2883 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2884 or return { 'error' => 'Permission denied' };
2886 my $pkgnum = $p->{'pkgnum'};
2888 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2889 'pkgnum' => $pkgnum, } )
2890 or return { 'error' => "unknown pkgnum $pkgnum" };
2892 my $error = $cust_pkg->suspend(reason => $reasonnum);
2893 return { 'error' => $error };
2899 my $session = _cache->get($p->{'session_id'})
2900 or return { 'error' => "Can't resume session" }; #better error message
2902 my $custnum = $session->{'custnum'};
2903 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2904 or return { 'error' => "unknown custnum $custnum" };
2906 my $pkgnum = $p->{'pkgnum'};
2907 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2908 'pkgnum' => $pkgnum, } )
2909 or return { 'error' => "unknown pkgnum $pkgnum" };
2911 my $error = $cust_pkg->cancel( 'quiet' => 1,
2912 'date' => $p->{'date'},
2914 return { 'error' => $error };
2917 sub provision_phone {
2920 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2922 #editing an existing phone number
2923 if ( $p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/ ) {
2924 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2925 return { 'error' => $session } if $context eq 'error';
2927 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2928 return { 'error' => 'service not found' } unless $svc_phone;
2929 return { 'error' => 'invalid svcnum' }
2930 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2932 $svc_phone->email($p->{'email'})
2933 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2934 $svc_phone->forwarddst($p->{'forwarddst'})
2935 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2936 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2937 return { 'error' => $svc_phone->replace };
2941 unless ( $p->{'lnp'} ) {
2942 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2943 $p->{'lnp_status'} = "portingin";
2944 return _provision( 'FS::svc_phone',
2945 [qw(lnp_desired_due_date lnp_other_provider
2946 lnp_other_provider_account phonenum countrycode lnp_status)],
2947 [qw(phonenum countrycode)],
2953 # single DID order (the usual case)
2954 unless (scalar(@bulkdid)) {
2955 return _provision( 'FS::svc_phone',
2956 [qw(phonenum countrycode)],
2957 [qw(phonenum countrycode)],
2963 # bulk DID order case
2965 foreach my $did ( @bulkdid ) {
2966 $did =~ s/[^0-9]//g;
2967 $error = _provision( 'FS::svc_phone',
2968 [qw(phonenum countrycode)],
2969 [qw(phonenum countrycode)],
2971 'pkgnum' => $p->{'pkgnum'},
2972 'svcpart' => $p->{'svcpart'},
2974 'countrycode' => $p->{'countrycode'},
2975 'session_id' => $p->{'session_id'},
2978 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2980 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2985 warn "provision_pbx called\n"
2988 warn "provision_pbx calling _provision\n"
2990 _provision( 'FS::svc_pbx',
2991 [qw(id title max_extensions max_simultaneous ip_addr)],
2992 [qw(id title max_extensions max_simultaneous ip_addr)],
2998 sub provision_acct {
3000 warn "provision_acct called\n"
3003 return { 'error' => gettext('passwords_dont_match') }
3004 if $p->{'_password'} ne $p->{'_password2'};
3005 return { 'error' => gettext('empty_password') }
3006 unless length($p->{'_password'});
3008 if ($p->{'domsvc'}) {
3009 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
3010 qw ( svcpart pkgnum ) );
3011 return { 'error' => gettext('invalid_domain') }
3012 unless ($domains{$p->{'domsvc'}});
3015 warn "provision_acct calling _provision\n"
3017 _provision( 'FS::svc_acct',
3018 [qw(username _password domsvc)],
3019 [qw(username _password domsvc)],
3025 sub provision_external {
3027 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
3028 _provision( 'FS::svc_external',
3036 sub provision_forward {
3038 _provision( 'FS::svc_forward',
3039 ['srcsvc','src','dstsvc','dst'],
3046 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
3047 warn "_provision called for $class\n"
3050 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3051 return { 'error' => $session } if $context eq 'error';
3053 my $search = { 'custnum' => $custnum };
3054 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3055 my $cust_main = qsearchs('cust_main', $search )
3056 or return { 'error' => "unknown custnum $custnum" };
3058 my $pkgnum = $p->{'pkgnum'};
3060 warn "searching for custnum $custnum pkgnum $pkgnum\n"
3062 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3063 'pkgnum' => $pkgnum,
3065 or return { 'error' => "unknown pkgnum $pkgnum" };
3067 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
3069 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
3070 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
3072 return { error=> 'svcpart '. $p->{'svcpart'}. " is not a $class definition" }
3073 if $class ne 'FS::'. $part_svc->svcdb;
3075 warn "creating $class record\n"
3077 my $svc_x = $class->new( {
3078 'pkgnum' => $p->{'pkgnum'},
3079 'svcpart' => $p->{'svcpart'},
3080 map { $_ => $p->{$_} } @$fields
3083 my %insert_args = ();
3084 #i shouldn't be a special case here (pass an option or something)
3085 if ( $class eq 'FS::svc_phone'
3086 && grep length($p->{$_}), @location_editable_fields
3089 $insert_args{'cust_location'} = new FS::cust_location {
3090 map { $_ => $p->{$_} } @location_editable_fields
3094 warn "inserting $class record\n"
3096 my $error = $svc_x->insert(%insert_args);
3099 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
3101 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
3104 my $return = { 'svc' => $part_svc->svc,
3106 map { $_ => $svc_x->get($_) } @$return_fields
3108 warn "_provision returning ". Dumper($return). "\n"
3117 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3118 return { 'error' => $session } if $context eq 'error';
3120 my $search = { 'custnum' => $custnum };
3121 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3122 my $cust_main = qsearchs('cust_main', $search )
3123 or return { 'error' => "unknown custnum $custnum" };
3125 my $pkgnum = $p->{'pkgnum'};
3127 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3128 'pkgnum' => $pkgnum,
3130 or return { 'error' => "unknown pkgnum $pkgnum" };
3132 my $svcpart = $p->{'svcpart'};
3134 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
3135 'svcpart' => $svcpart, } )
3136 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
3137 my $part_svc = $pkg_svc->part_svc;
3139 my $conf = new FS::Conf;
3142 'svc' => $part_svc->svc,
3143 'svcdb' => $part_svc->svcdb,
3144 'pkgnum' => $pkgnum,
3145 'svcpart' => $svcpart,
3146 'custnum' => $custnum,
3148 'security_phrase' => 0, #XXX !
3149 'svc_acct_pop' => [], #XXX !
3151 'init_popstate' => '',
3156 small_custview( $cust_main, $conf->config('countrydefault') ),
3160 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
3161 && $ret->{'svcdb'} eq 'svc_phone') {
3162 $ret->{'svcnum'} = $p->{'svcnum'};
3163 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
3164 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
3165 $ret->{'email'} = $svc_phone->email;
3166 $ret->{'forwarddst'} = $svc_phone->forwarddst;
3170 if ($ret->{'svcdb'} eq 'svc_forward') {
3171 $ret->{'forward_emails'} = {$cust_pkg->forward_emails()};
3177 sub unprovision_svc {
3180 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3181 return { 'error' => $session } if $context eq 'error';
3183 my $search = { 'custnum' => $custnum };
3184 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3185 my $cust_main = qsearchs('cust_main', $search )
3186 or return { 'error' => "unknown custnum $custnum" };
3188 my $svcnum = $p->{'svcnum'};
3190 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
3191 or return { 'error' => "unknown svcnum $svcnum" };
3193 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
3194 unless $cust_svc->cust_pkg->custnum == $custnum;
3196 my $conf = new FS::Conf;
3198 return { 'svc' => $cust_svc->part_svc->svc,
3199 'error' => $cust_svc->cancel,
3201 small_custview( $cust_main, $conf->config('countrydefault') ),
3206 sub myaccount_passwd {
3208 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3209 return { 'error' => $session } if $context eq 'error';
3211 return { 'error' => "New passwords don't match." }
3212 if $p->{'new_password'} ne $p->{'new_password2'};
3214 return { 'error' => 'Enter new password' }
3215 unless length($p->{'new_password'});
3217 #my $search = { 'custnum' => $custnum };
3218 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3219 $custnum =~ /^(\d+)$/ or die "illegal custnum";
3220 my $search = " AND custnum = $1";
3221 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
3223 my $svc_acct = qsearchs( {
3224 'table' => 'svc_acct',
3225 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
3226 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
3227 'LEFT JOIN cust_main USING ( custnum ) ',
3228 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
3229 'extra_sql' => $search, #important
3231 or return { 'error' => "Service not found" };
3235 my $conf = new FS::Conf;
3237 return { 'error' => 'Incorrect current password.' }
3238 if ( exists($p->{'old_password'})
3239 || $conf->exists('selfservice-password_change_oldpass')
3241 && ! $svc_acct->check_password($p->{'old_password'});
3243 $error ||= $svc_acct->is_password_allowed($p->{'new_password'})
3244 || $svc_acct->set_password($p->{'new_password'})
3245 || $svc_acct->replace();
3247 #regular pw change in self-service should change contact pw too, otherwise its
3248 #way too confusing. hell its confusing they're separate at all, but alas.
3249 #need to support the "ISP provides email that's used as a contact email" case
3251 my $contact = FS::contact->by_selfservice_email($svc_acct->email);
3252 if ( $contact && $contact->custnum == $custnum ) {
3253 #svc_acct was successful but this one returns an error? "shouldn't happen"
3254 #don't recheck is_password_allowed here; if the svc_acct password was
3255 #legal, that's good enough
3256 $error ||= $contact->change_password($p->{'new_password'});
3259 my($label, $value) = $svc_acct->cust_svc->label;
3261 return { 'error' => $error,
3271 my $info = skin_info($p);
3273 my $conf = new FS::Conf;
3274 my $verification = $conf->config('selfservice-password_reset_verification')
3275 or return { %$info, 'error' => 'Password resets disabled' };
3280 if ( $p->{'email'} ) { #new-style, changes contact and svc_acct
3282 $contact = FS::contact->by_selfservice_email($p->{'email'});
3284 $cust_main = $contact->cust_main if $contact;
3286 #also look for an svc_acct, otherwise it would be super confusing
3288 my($username, $domain) = split('@', $p->{'email'});
3289 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } );
3290 if ( $svc_domain ) {
3291 $svc_acct = qsearchs('svc_acct', { 'username' => $username,
3292 'domsvc' => $svc_domain->svcnum }
3295 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
3296 $cust_main ||= $cust_pkg->cust_main if $cust_pkg;
3298 #precaution: don't change svc_acct password not part of the same
3299 # customer as contact
3300 $svc_acct = '' if ! $cust_pkg
3301 || $cust_pkg->custnum != $cust_main->custnum;
3306 return { %$info, 'error' => 'Email address not found' }
3307 unless $contact || $svc_acct;
3309 } elsif ( $p->{'username'} ) { #old style, looks in svc_acct only
3311 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
3312 or return { %$info, 'error' => 'Account not found' };
3314 $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
3315 'domsvc' => $svc_domain->svcnum }
3317 or return { %$info, 'error' => 'Account not found' };
3319 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
3320 or return { %$info, 'error' => 'Account not found' };
3322 $cust_main = $cust_pkg->cust_main;
3327 'email' => sub { 1; },
3329 my( $p, $cust_main ) = @_;
3330 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
3331 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
3334 my( $p, $cust_main ) = @_;
3335 my $cust_pay = qsearchs({
3336 'table' => 'cust_pay',
3337 'hashref' => { 'custnum' => $cust_main->custnum },
3338 'order_by' => 'ORDER BY _date DESC LIMIT 1',
3342 $p->{'amount'} == $cust_pay->paid;
3345 my( $p, $cust_main ) = @_;
3346 $p->{'zip'} eq $cust_main->zip
3347 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
3351 foreach my $verify ( split(',', $verification) ) {
3353 &{ $verify{$verify} }( $p, $cust_main )
3354 or return { %$info, 'error' => 'Account not found' };
3358 #okay, we're verified
3362 my $error = $contact->send_reset_email(
3363 'svcnum' => ($svc_acct ? $svc_acct->svcnum : ''),
3367 return { %$info, 'error' => $error }; #????
3370 } elsif ( $svc_acct ) {
3372 #create a unique session
3374 my $reset_session = {
3375 'svcnum' => $svc_acct->svcnum,
3376 'agentnum' => $svc_acct->cust_main->agentnum,
3379 my $timeout = '1 hour'; #?
3381 my $reset_session_id;
3383 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
3384 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") );
3387 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
3391 my $msgnum = $conf->config('selfservice-password_reset_msgnum',
3392 $cust_main->agentnum);
3393 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
3394 return { %$info, 'error' => "selfservice-password_reset_msgnum unset" }
3396 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
3397 my $error = $msg_template->send( 'cust_main' => $cust_main,
3398 'object' => $svc_acct,
3399 'substitutions' => {
3400 'session_id' => $reset_session_id,
3404 return { %$info, 'error' => $error }; #????
3409 return { %$info, 'error' => '' };
3412 sub check_reset_passwd {
3415 my $conf = new FS::Conf;
3416 my $verification = $conf->config('selfservice-password_reset_verification')
3417 or return { 'error' => 'Password resets disabled' };
3419 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3420 or return { 'error' => "Can't resume session" }; #better error message
3422 if ( $reset_session->{'svcnum'} ) {
3424 my $svcnum = $reset_session->{'svcnum'};
3426 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3427 or return { 'error' => "Service not found" };
3429 $p->{'agentnum'} = $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3430 my $info = skin_info($p);
3434 'session_id' => $p->{'session_id'},
3435 'username' => $svc_acct->username,
3438 } elsif ( $reset_session->{'contactnum'} ) {
3440 my $contactnum = $reset_session->{'contactnum'};
3442 my $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3443 or return { 'error' => "Contact not found" };
3445 my @contact_email = $contact->contact_email;
3446 return { 'error' => 'No contact email' } unless @contact_email;
3448 $p->{'agentnum'} = $contact->cust_main->agentnum;
3449 my $info = skin_info($p);
3453 'session_id' => $p->{'session_id'},
3454 'email' => $contact_email[0]->email, #the first?
3459 return { 'error' => 'No svcnum or contactnum in session' }; #??
3465 sub process_reset_passwd {
3468 my $conf = new FS::Conf;
3469 my $verification = $conf->config('selfservice-password_reset_verification')
3470 or return { 'error' => 'Password resets disabled' };
3472 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3473 or return { 'error' => "Can't resume session" }; #better error message
3478 if ( $reset_session->{'svcnum'} ) {
3480 my $svcnum = $reset_session->{'svcnum'};
3482 $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3483 or return { 'error' => "Service not found" };
3485 $p->{'agentnum'} ||= $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3486 $info ||= skin_info($p);
3491 if ( $reset_session->{'contactnum'} ) {
3493 my $contactnum = $reset_session->{'contactnum'};
3495 $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3496 or return { 'error' => "Contact not found" };
3498 $p->{'agentnum'} ||= $contact->cust_main->agentnum;
3499 $info ||= skin_info($p);
3503 return { %$info, 'error' => "New passwords don't match." }
3504 if $p->{'new_password'} ne $p->{'new_password2'};
3506 return { %$info, 'error' => 'Enter new password' }
3507 unless length($p->{'new_password'});
3511 my $error ||= $svc_acct->is_password_allowed($p->{'new_password'})
3512 || $svc_acct->set_password($p->{'new_password'})
3513 || $svc_acct->replace();
3515 return { %$info, 'error' => $error } if $error;
3517 #my($label, $value) = $svc_acct->cust_svc->label;
3518 #return { 'error' => $error,
3519 # #'label' => $label,
3520 # #'value' => $value,
3527 my $error = $contact->is_password_allowed($p->{'new_password'})
3528 || $contact->change_password($p->{'new_password'});
3530 return { %$info, 'error' => $error }; # if $error;
3534 #password changed ,so remove session, don't want it reused
3535 _cache->remove($p->{'session_id'});
3537 return { %$info, 'error' => '' };
3541 sub validate_passwd {
3545 %result = ( 'fieldid' => $p->{'fieldid'} )
3546 if $p->{'fieldid'} =~ /^\w+$/;
3548 return { %result, 'password_invalid' => 'Enter new password' }
3549 unless length($p->{'check_password'});
3552 if ($p->{'svcnum'}) {
3553 # false laziness with myaccount_passwd
3554 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3555 return { %result, 'error' => $session } if $context eq 'error';
3557 $custnum =~ /^(\d+)$/ or die "illegal custnum";
3558 my $search = " AND custnum = $1";
3559 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
3561 $svc_acct = qsearchs( {
3562 'table' => 'svc_acct',
3563 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
3564 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
3565 'LEFT JOIN cust_main USING ( custnum ) ',
3566 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
3567 'extra_sql' => $search, #important
3569 or return { %result, 'error' => "Service not found" };
3570 # end false laziness
3573 unless ($svc_acct) {
3574 my $conf = new FS::Conf;
3575 my $agentnum = $p->{'agentnum'};
3576 return { %result, 'password_valid' => 1 }
3577 if $conf->config_bool('password-insecure', $p->{'agentnum'});
3580 $svc_acct ||= new FS::svc_acct {};
3582 my $error = $svc_acct->is_password_allowed($p->{'check_password'});
3583 return { %result, 'password_invalid' => $error } if $error;
3584 return { %result, 'password_valid' => 1 };
3589 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3590 return { 'error' => $session } if $context eq 'error';
3593 if ( $session->{'pkgnum'} ) {
3595 #tickets for specific service with pkg-balances on
3596 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3597 'pkgnum' => $session->{'pkgnum'} })
3598 or return { 'error' => 'unknown package' };
3599 foreach my $cust_svc ( $cust_pkg->cust_svc ) {
3600 push @tickets, $cust_svc->tickets( $p->{status} );
3605 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
3606 or return { 'error' => "unknown custnum $custnum" };
3608 @tickets = $cust_main->tickets( $p->{status} );
3611 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
3612 if ( $FS::TicketSystem::system && FS::TicketSystem->selfservice_priority ) {
3613 my $conf = new FS::Conf;
3614 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
3618 ($a->{'_selfservice_priority'} eq '') <=>
3619 ($b->{'_selfservice_priority'} eq '')
3622 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
3628 +{ tickets => \@tickets };
3635 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3636 return { 'error' => $session } if $context eq 'error';
3638 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
3639 FS::TicketSystem->init();
3641 my $conf = new FS::Conf;
3642 my $queue = $p->{'queue'}
3643 || $conf->config('ticket_system-selfservice_queueid')
3644 || $conf->config('ticket_system-default_queueid');
3646 warn "$me create_ticket: creating ticket\n" if $DEBUG;
3647 my $err_or_ticket = FS::TicketSystem->create_ticket(
3648 '', #create RT session based on FS CurrentUser (fs_selfservice)
3650 'custnum' => $custnum,
3651 'svcnum' => $session->{'svcnum'},
3652 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
3655 if ( ref($err_or_ticket) ) {
3656 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
3658 return { 'error' => '',
3659 'ticket_id' => $err_or_ticket->id,
3662 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
3664 return { 'error' => $err_or_ticket };
3672 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3673 return { 'error' => $session } if $context eq 'error';
3675 return { error => 'requested format not implemented' }
3676 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
3678 my $conf = new FS::Conf;
3679 my $age_threshold = 0;
3680 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
3681 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
3683 my $search = { 'custnum' => $custnum };
3684 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3685 my $cust_main = qsearchs('cust_main', $search )
3686 or return { 'error' => "unknown custnum $custnum" };
3688 # does it make more sense to just run one sql query for this instead of all the
3689 # insanity below? would increase performance greately for large data sets?
3691 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
3692 my @part_svc = $cust_pkg->part_svc;
3693 foreach my $part_svc ( @part_svc ) {
3694 if($part_svc->svcdb eq 'svc_phone'){
3695 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
3696 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
3697 push @svc_phone, $cust_pkg_svc->svc_x
3698 if $cust_pkg_svc->date_inserted >= $age_threshold;
3706 my($xls_r,$xls_c) = (0,0);
3709 my @fields = qw( countrycode phonenum pin sip_password phone_name );
3710 if($p->{'format'} eq 'csv') {
3711 $csv = new Text::CSV_XS { 'always_quote' => 1,
3714 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
3715 $content .= $csv->string;
3717 elsif($p->{'format'} eq 'xls') {
3718 my $XLS1 = new IO::Scalar \$content;
3719 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
3720 or return { 'error' => "Error opening .xls file: $!" };
3721 $xls = $xls_workbook->add_worksheet('DIDs');
3722 foreach ( @fields ) {
3723 $xls->write(0,$xls_c++,$_);
3728 foreach my $svc_phone ( @svc_phone ) {
3729 my @cols = map { $svc_phone->$_ } @fields;
3730 if($p->{'format'} eq 'csv') {
3731 return { 'error' => 'Unable to create CSV' }
3732 unless $csv->combine(@cols);
3733 $content .= $csv->string;
3735 elsif($p->{'format'} eq 'xls') {
3738 $xls->write($xls_r,$xls_c++,$_);
3744 $xls_workbook->close() if $p->{'format'} eq 'xls';
3746 { content => $content, format => $p->{'format'}, };
3751 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3752 return { 'error' => $session } if $context eq 'error';
3754 # warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
3755 # FS::TicketSystem->init();
3756 # return { 'error' => 'get_ticket configuration error' }
3757 # if $FS::TicketSystem::system ne 'RT_Internal';
3759 # check existence and ownership as part of this
3760 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
3761 my $rt_session = FS::TicketSystem->session('');
3762 my $Ticket = FS::TicketSystem->get_ticket_object(
3764 ticket_id => $p->{'ticket_id'},
3767 return { 'error' => 'ticket not found' } if !$Ticket;
3769 if ( length( $p->{'subject'} || '' ) ) {
3771 if ( $p->{'subject'} ne $Ticket->Subject ) {
3772 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
3773 return { 'error' => "unable to set subject: $msg" } if !$val;
3777 if(length($p->{'reply'})) {
3778 my @err_or_res = FS::TicketSystem->correspond_ticket(
3780 'ticket_id' => $p->{'ticket_id'},
3781 'content' => $p->{'reply'},
3784 return { 'error' => 'unable to reply to ticket' }
3785 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
3788 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
3789 my $err_or_ticket = FS::TicketSystem->get_ticket(
3791 'ticket_id' => $p->{'ticket_id'},
3794 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
3795 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
3797 return { 'error' => $err_or_ticket };
3800 my @custs = @{$err_or_ticket->{'custs'}};
3801 my @txns = @{$err_or_ticket->{'txns'}};
3804 # superseded by check in get_ticket_object
3805 #return { 'error' => 'invalid ticket requested' }
3806 #unless grep($_ eq $custnum, @custs);
3808 foreach my $txn ( @txns ) {
3809 push @filtered_txns, $txn
3810 if ($txn->{'type'} eq 'EmailRecord'
3811 || $txn->{'type'} eq 'Correspond'
3812 || $txn->{'type'} eq 'Create');
3815 warn "$me get_ticket: successful: \n"
3817 return { 'error' => '',
3818 'transactions' => \@filtered_txns,
3819 'ticket_fields' => $err_or_ticket->{'fields'},
3820 'ticket_id' => $p->{'ticket_id'},
3824 sub adjust_ticket_priority {
3826 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3827 return { 'error' => $session } if $context eq 'error';
3829 # warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
3830 # FS::TicketSystem->init;
3831 my $ss_priority = FS::TicketSystem->selfservice_priority;
3833 return { 'error' => 'adjust_ticket_priority configuration error' }
3834 if $FS::TicketSystem::system ne 'RT_Internal'
3837 my $values = $p->{'values'}; #hashref, id => priority value
3840 foreach my $id (keys %$values) {
3841 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
3842 my $Ticket = FS::TicketSystem->get_ticket_object('',
3844 'custnum' => $custnum,
3847 $ticket_error{$id} = 'ticket not found';
3851 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
3852 # We're not going to implement it for RT_External.
3853 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
3854 my $new_value = $values->{$id};
3855 next if $old_value eq $new_value;
3857 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
3859 # AddCustomFieldValue works fine (replacing any existing value) if it's
3860 # a single-valued custom field, which it should be. If it's not, you're
3861 # doing something wrong.
3863 if ( length($new_value) ) {
3864 ($val, $msg) = $Ticket->AddCustomFieldValue(
3865 Field => $ss_priority,
3866 Value => $new_value,
3870 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
3871 Field => $ss_priority,
3872 Value => $old_value,
3876 $ticket_error{$id} = $msg if !$val;
3877 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
3879 return { 'error' => '',
3880 'ticket_error' => \%ticket_error,
3881 %{ customer_info($p) } # send updated customer info back
3887 sub _custoragent_session_custnum {
3890 my($context, $session, $custnum);
3891 if ( $p->{'session_id'} ) {
3893 $context = 'customer';
3894 $session = _cache->get($p->{'session_id'})
3895 or return ( 'error' => "Can't resume session" ); #better error message
3896 $custnum = $session->{'custnum'};
3898 } elsif ( $p->{'agent_session_id'} ) {
3901 my $agent_cache = new FS::ClientAPI_SessionCache( {
3902 'namespace' => 'FS::ClientAPI::Agent',
3904 $session = $agent_cache->get($p->{'agent_session_id'})
3905 or return ( 'error' => "Can't resume session" ); #better error message
3906 $custnum = $p->{'custnum'};
3910 return ( 'error' => "Can't resume session" ); #better error message
3913 ($context, $session, $custnum);