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;
17 use Spreadsheet::WriteExcel;
18 use OLE::Storage_Lite;
19 use FS::UI::Web::small_custview qw(small_custview); #less doh
21 use FS::UI::bytecount qw( display_bytecount );
24 use FS::Record qw(qsearch qsearchs dbh);
25 use FS::Msgcat qw(gettext);
26 use FS::Misc qw(card_types money_pretty);
27 use FS::Misc::DateTime qw(parse_datetime);
29 use FS::ClientAPI_SessionCache;
41 use FS::legacy_cust_bill;
42 use FS::cust_main_county;
46 use FS::acct_rt_transaction;
50 use FS::cust_location;
52 # for code organization
53 use FS::ClientAPI::MyAccount::contact;
54 use FS::ClientAPI::MyAccount::quotation;
57 $me = '[FS::ClientAPI::MyAccount]';
59 use vars qw( @cust_main_editable_fields @location_editable_fields );
60 @cust_main_editable_fields = qw(
61 first last company daytime night fax mobile
63 payby payinfo payname paystart_month paystart_year payissue payip
64 ss paytype paystate stateid stateid_state
66 @location_editable_fields = qw(
67 address1 address2 city county state zip country
71 BEGIN { #preload to reduce time customer_info takes
72 if ( $FS::TicketSystem::system ) {
73 warn "$me: initializing ticket system\n" if $DEBUG;
74 FS::TicketSystem->init();
79 $cache ||= new FS::ClientAPI_SessionCache( {
80 'namespace' => 'FS::ClientAPI::MyAccount',
87 my($context, $session, $custnum) = _custoragent_session_custnum($p);
88 #return { 'error' => $session } if $context eq 'error';
91 if ( $context eq 'customer' && $custnum ) {
93 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
96 $sth->execute($custnum) or die $sth->errstr;
98 $agentnum = $sth->fetchrow_arrayref->[0]
99 or die "no agentnum for custnum $custnum";
101 #} elsif ( $context eq 'agent' ) {
102 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
105 $p->{'agentnum'} = $agentnum;
107 my $conf = new FS::Conf;
109 #false laziness w/Signup.pm
111 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
113 if ( $skin_info_cache_agent ) {
115 warn "$me loading cached skin info for agentnum $agentnum\n"
120 warn "$me populating skin info cache for agentnum $agentnum\n"
123 $skin_info_cache_agent = {
124 'agentnum' => $agentnum,
125 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
126 qw( company_name date_format ) ),
127 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
128 qw( body_bgcolor box_bgcolor stripe1_bgcolor stripe2_bgcolor
129 text_color link_color vlink_color hlink_color alink_color
130 font title_color title_align title_size menu_bgcolor menu_fontsize
133 'menu_disable' => [ $conf->config('selfservice-menu_disable',$agentnum) ],
134 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
135 qw( menu_skipblanks menu_skipheadings menu_nounderline no_logo enable_payment_without_balance )
137 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
138 qw( title_left_image title_right_image
139 menu_top_image menu_body_image menu_bottom_image
142 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
143 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
144 qw( head body_header body_footer company_address ) ),
145 'money_char' => $conf->config("money_char") || '$',
146 'menu' => join("\n", $conf->config("ng_selfservice-menu", $agentnum ) ) ||
149 services.php Services
150 services.php My Services
151 services_new.php Order a new service
154 personal.php Personal Information
155 password.php Change Password
158 payment_cc.php Credit Card Payment
159 payment_ach.php Electronic Check Payment
160 payment_paypal.php PayPal Payment
161 payment_webpay.php Webpay Payments
164 usage_data.php Data usage
165 usage_cdr.php Call usage
167 tickets.php Help Desk
168 tickets.php Open Tickets
169 tickets_resolved.php Resolved Tickets
170 ticket_create.php Create a new ticket
178 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
182 #{ %$skin_info_cache_agent };
183 $skin_info_cache_agent;
190 my $conf = new FS::Conf;
194 'phone_login' => $conf->exists('selfservice_server-phone_login'),
195 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
196 'banner_url' => scalar($conf->config('selfservice-login_banner_url')),
197 'banner_image_md5' =>
198 md5_hex($conf->config_binary('selfservice-login_banner_image')),
205 sub login_banner_image {
207 my $conf = new FS::Conf;
208 my $image = $conf->config_binary('selfservice-login_banner_image');
210 'md5' => md5_hex($image),
215 #false laziness w/FS::ClientAPI::passwd::passwd
219 my $conf = new FS::Conf;
223 if ( $p->{'domain'} eq 'svc_phone'
224 && $conf->exists('selfservice_server-phone_login') ) {
226 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
227 return { error => 'Number not found.' } unless $svc_phone;
230 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
231 #return { error => 'Only primary user may log in.' }
232 # if $conf->exists('selfservice_server-primary_only')
233 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
235 return { error => 'Incorrect PIN.' }
236 unless $svc_phone->check_pin($p->{'password'});
240 } elsif ( $p->{email}
241 && (my $contact = FS::contact->by_selfservice_email($p->{email}))
244 return { error => 'Incorrect contact password.' }
245 unless $contact->authenticate_password($p->{'password'});
247 $session->{'contactnum'} = $contact->contactnum;
249 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
250 if ( scalar(@cust_contact) == 1 ) {
251 $session->{'custnum'} = $cust_contact[0]->custnum;
252 } elsif ( scalar(@cust_contact) ) {
253 $session->{'customers'} = { map { $_->custnum => $_->cust_main->name }
257 return { error => 'No customer self-service access for contact' }; #??
262 ( $p->{username}, $p->{domain} ) = split('@', $p->{email}) if $p->{email};
264 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
265 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
267 my @svc_acct = qsearch( 'svc_acct', { 'username' => $p->{'username'},
268 'domsvc' => $svc_domain->svcnum, }
271 if ( $conf->exists('selfservice_server-login_svcpart') ) {
272 my @svcpart = $conf->config('selfservice_server-login_svcpart');
273 @svc_acct = grep { my $svcpart = $_->cust_svc->svcpart;
274 scalar( grep( $_ eq $svcpart, @svcpart ) );
279 if ( $conf->exists('selfservice_server-primary_only') ) {
282 my $cust_svc = $_->cust_svc;
283 $cust_svc->cust_pkg->part_pkg->svcpart([qw( svc_acct svc_phone )])
284 == $cust_svc->svcpart
289 return { error => 'User not found.' } unless @svc_acct;
291 return { error => 'Multiple users.' } if scalar(@svc_acct) > 1;
293 my $svc_acct = $svc_acct[0];
295 if ( $conf->exists('selfservice_server-login_svcpart') ) {
296 my @svcpart = $conf->config('selfservice_server-login_svcpart');
297 my $svcpart = $svc_acct->cust_svc->svcpart;
298 return { error => 'Invalid user.' }
299 unless grep($_ eq $svcpart, @svcpart);
302 return { error => 'Incorrect password.' }
303 unless $svc_acct->check_password($p->{'password'});
311 $session->{'svcnum'} = $svc_x->svcnum;
313 my $cust_svc = $svc_x->cust_svc;
314 my $cust_pkg = $cust_svc->cust_pkg;
316 my $cust_main = $cust_pkg->cust_main;
317 $session->{'custnum'} = $cust_main->custnum;
318 if ( $conf->exists('pkg-balances') ) {
319 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
320 $cust_main->ncancelled_pkgs;
321 $session->{'pkgnum'} = $cust_pkg->pkgnum
322 if scalar(@cust_pkg) > 1;
326 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
327 #return { error => 'Only primary user may log in.' }
328 # if $conf->exists('selfservice_server-primary_only')
329 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
330 my $part_pkg = $cust_pkg->part_pkg;
331 return { error => 'Only primary user may log in.' }
332 if $conf->exists('selfservice_server-primary_only')
333 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
339 $session_id = sha512_hex(time(). {}. rand(). $$)
340 } until ( ! defined _cache->get($session_id) ); #just in case
342 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
343 _cache->set( $session_id, $session, $timeout );
345 return { 'error' => '',
346 'session_id' => $session_id,
353 my $skin_info = skin_info($p);
354 if ( $p->{'session_id'} ) {
355 _cache->remove($p->{'session_id'});
356 return { %$skin_info, 'error' => '' };
358 return { %$skin_info, 'error' => "Can't resume session" }; #better error message
365 my($context, $session, $custnum) = _custoragent_session_custnum($p);
366 return { 'error' => $session } if $context eq 'error';
368 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
369 or return { 'error' => "Service not found" };
371 $session->{'svcnum'} = $svc_acct->svcnum;
373 my $conf = new FS::Conf;
374 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
375 _cache->set( $p->{'session_id'}, $session, $timeout );
377 return { 'error' => '' };
383 my($context, $session, $custnum) = _custoragent_session_custnum($p);
384 return { 'error' => $session } if $context eq 'error';
386 $session->{'custnum'} = $p->{'custnum'}
387 if exists $session->{'customers'}{ $p->{'custnum'} };
389 my $conf = new FS::Conf;
390 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
391 _cache->set( $p->{'session_id'}, $session, $timeout );
393 return { 'error' => '',
394 %{ customer_info( { session_id=>$p->{'session_id'} } ) },
398 sub payment_gateway {
400 # takes a cust_main and a cust_payby entry, returns the payment_gateway
401 my $conf = new FS::Conf;
402 my $cust_main = shift;
403 my $cust_payby = shift;
404 my $gatewaynum = $conf->config('selfservice-payment_gateway');
406 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
407 die "configured gatewaynum $gatewaynum not found!" if !$pg;
411 return '' if ! FS::payby->realtime($cust_payby);
412 my $pg = $cust_main->agent->payment_gateway(
413 'method' => FS::payby->payby2bop($cust_payby),
423 my $conf = new FS::Conf;
425 my $info = skin_info($p);
427 use vars qw( $cust_paybys ); #cache for performance
428 unless ( $cust_paybys ) {
430 my %cust_paybys = map { $_ => 1 }
431 map { FS::payby->payby2payment($_) }
432 $conf->config('signup_server-payby');
434 $cust_paybys = [ keys %cust_paybys ];
437 $info->{'cust_paybys'} = $cust_paybys;
439 my($context, $session, $custnum) = _custoragent_session_custnum($p);
440 return { 'error' => $session } if $context eq 'error';
442 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
444 $info->{'hide_payment_fields'} = [
446 my $pg = $cust_main && payment_gateway($cust_main, $_);
447 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
448 } @{ $info->{cust_paybys} }
451 $info->{'self_suspend_reason'} =
452 $conf->config('selfservice-self_suspend_reason',
453 $cust_main ? $cust_main->agentnum : ''
456 $info->{'edit_ticket_subject'} =
457 $conf->exists('ticket_system-selfservice_edit_subject') &&
458 $cust_main && $cust_main->edit_subject;
460 $info->{'timeout'} = $conf->config('selfservice-timeout') || 3600;
462 $info->{'hide_usage'} = $conf->exists('selfservice_hide-usage');
465 'custnum' => $custnum,
466 'access_pkgnum' => $session->{'pkgnum'},
467 'access_svcnum' => $session->{'svcnum'},
474 my($context, $session, $custnum) = _custoragent_session_custnum($p);
475 return { 'error' => $session } if $context eq 'error';
479 my $conf = new FS::Conf;
480 $return{'require_address2'} = $conf->exists('cust_main-require_address2');
482 # if ( $FS::TicketSystem::system ) {
483 # warn "$me customer_info: initializing ticket system\n" if $DEBUG;
484 # FS::TicketSystem->init();
487 if ( $custnum ) { #customer record
489 %return = ( %return, %{ customer_info_short($p) } );
491 #redundant with customer_info_short, but we need it for several things below
492 my $search = { 'custnum' => $custnum };
493 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
494 my $cust_main = qsearchs('cust_main', $search )
495 or return { 'error' => "customer_info: unknown custnum $custnum" };
497 my $list_tickets = list_tickets($p);
498 $return{'tickets'} = $list_tickets->{'tickets'};
500 if ( $session->{'pkgnum'} ) {
501 #XXX open invoices in the pkg-balances case
503 $return{'money_char'} = $conf->config("money_char") || '$';
506 invnum => $_->invnum,
507 date => time2str("%b %o, %Y", $_->_date),
509 charged => $_->charged,
511 } $cust_main->open_cust_bill;
512 $return{open_invoices} = \@open;
514 my $sql = 'SELECT MAX(_date) FROM cust_bill WHERE custnum = ?';
515 my $sth = dbh->prepare($sql) or die dbh->errstr;
516 $sth->execute($custnum) or die $sth->errstr;
517 $return{'last_invoice_date'} = $sth->fetchrow_arrayref->[0];
518 $return{'last_invoice_date_pretty'} =
519 time2str('%m/%d/%Y', $return{'last_invoice_date'} );
522 #customer_info_short always has nobalance on..
523 $return{small_custview} =
524 small_custview( $cust_main,
525 $return{countrydefault},
526 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
529 $return{has_ship_address} = $cust_main->has_ship_address;
530 $return{status} = $cust_main->status_label; #$cust_main->status; #better to break anyone obscurely testing for strings in self-service than to have to upgrade every front-end to get the new status to display
531 $return{statuscolor} = $cust_main->statuscolor;
532 $return{status_label} = $cust_main->status_label;
534 # compatibility: some places in selfservice use this to determine
535 # if there's a ship address
536 if ( $return{has_ship_address} ) {
537 $return{ship_last} = $cust_main->last;
538 $return{ship_first} = $cust_main->first;
541 if (scalar($conf->config('support_packages'))) {
542 my @support_services = ();
543 foreach ($cust_main->support_services) {
544 my $seconds = $_->svc_x->seconds || 0;
545 my $time_remaining = (($seconds < 0) ? '-' : '' ).
546 int(abs($seconds)/3600)."h".
547 sprintf("%02d",(abs($seconds)%3600)/60)."m";
548 my $cust_pkg = $_->cust_pkg;
551 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
552 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
553 push @support_services, { svcnum => $_->svcnum,
554 time => $time_remaining,
559 $return{support_services} = \@support_services;
562 if ( $conf->config('prepayment_discounts-credit_type') ) {
564 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
567 } elsif ( $session->{'svcnum'} ) { #no customer record
569 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
570 or die "unknown svcnum";
571 $return{name} = $svc_acct->email;
575 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
579 return { 'error' => '',
580 'custnum' => $custnum,
586 sub customer_info_short {
589 my($context, $session, $custnum) = _custoragent_session_custnum($p);
590 return { 'error' => $session } if $context eq 'error';
594 my $conf = new FS::Conf;
596 if ( $custnum ) { #customer record
598 my $search = { 'custnum' => $custnum };
599 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
600 my $cust_main = qsearchs('cust_main', $search )
601 or return { 'error' => "customer_info_short: unknown custnum $custnum" };
603 $return{display_custnum} = $cust_main->display_custnum;
605 if ( $session->{'pkgnum'} ) {
606 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
607 #next_bill_date from cust_pkg?
609 $return{balance} = $cust_main->balance;
610 $return{next_bill_date} = $cust_main->next_bill_date;
611 $return{next_bill_date_pretty} =
612 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
615 $return{balance_pretty} = money_pretty($return{balance});
617 $return{countrydefault} = scalar($conf->config('countrydefault'));
619 $return{small_custview} =
620 small_custview( $cust_main,
621 $return{countrydefault},
625 $return{first} = $cust_main->first;
626 $return{'last'} = $cust_main->get('last');
627 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
629 $return{payby} = $cust_main->payby;
631 #none of these are terribly expensive if we want 'em...
632 for (@cust_main_editable_fields) {
633 $return{$_} = $cust_main->get($_);
635 #maybe a little more expensive, but it should be cached by now
636 for (@location_editable_fields) {
637 $return{$_} = $cust_main->bill_location->get($_)
638 if $cust_main->bill_locationnum;
639 $return{'ship_'.$_} = $cust_main->ship_location->get($_)
640 if $cust_main->ship_locationnum;
643 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
644 $return{payinfo} = $cust_main->paymask;
645 @return{'month', 'year'} = $cust_main->paydate_monthyear;
648 my @invoicing_list = $cust_main->invoicing_list;
649 $return{'invoicing_list'} =
650 join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list );
651 $return{'postal_invoicing'} =
652 0 < ( grep { $_ eq 'POST' } @invoicing_list );
654 if ( $session->{'svcnum'} ) {
655 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
656 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
657 $return{'svcnum'} = $session->{'svcnum'};
660 } elsif ( $session->{'svcnum'} ) { #no customer record
662 #uuh, not supproted yet... die?
663 return { 'error' => 'customer_info_short not yet supported as agent' };
667 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
671 return { 'error' => '',
672 'custnum' => $custnum,
677 sub billing_history {
680 my($context, $session, $custnum) = _custoragent_session_custnum($p);
681 return { 'error' => $session } if $context eq 'error';
683 return { 'error' => 'No customer' } unless $custnum;
685 my $search = { 'custnum' => $custnum };
686 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
687 my $cust_main = qsearchs('cust_main', $search )
688 or return { 'error' => "unknown custnum $custnum" };
692 if ( $session->{'pkgnum'} ) {
693 #$return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
694 #next_bill_date from cust_pkg?
695 return { 'error' => 'No history for package' };
698 $return{balance} = $cust_main->balance;
699 $return{balance_pretty} = money_pretty($return{balance});
700 $return{next_bill_date} = $cust_main->next_bill_date;
701 $return{next_bill_date_pretty} =
702 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
705 my $conf = new FS::Conf;
707 $return{'history'} = [
708 $cust_main->payment_history(
709 'line_items' => $conf->exists('selfservice-billing_history-line_items'),
714 $return{'money_char'} = $conf->config("money_char") || '$',
722 my $session = _cache->get($p->{'session_id'})
723 or return { 'error' => "Can't resume session" }; #better error message
725 my $custnum = $session->{'custnum'}
726 or return { 'error' => "no customer record" };
728 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
729 or return { 'error' => "unknown custnum $custnum" };
731 my $new = new FS::cust_main { $cust_main->hash };
733 $new->set( $_ => $p->{$_} )
734 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
736 if ( exists($p->{address1}) ) {
737 my $bill_location = FS::cust_location->new({
738 map { $_ => $p->{$_} } @location_editable_fields
740 # if this is unchanged from before, cust_main::replace will ignore it
741 $new->set('bill_location' => $bill_location);
744 if ( exists($p->{ship_address1}) ) {
745 my $ship_location = FS::cust_location->new({
746 map { $_ => $p->{"ship_$_"} } @location_editable_fields
748 if ( !grep { length($p->{"ship_$_"}) } @location_editable_fields ) {
749 # Selfservice unfortunately tries to indicate "same as billing
750 # address" by sending all fields empty. Did this ever work?
751 $ship_location = $cust_main->bill_location;
753 $new->set('ship_location' => $ship_location);
755 # but if it hasn't been passed in at all, leave ship_location alone--
756 # DON'T change it to match bill_location.
759 if (exists($p->{'payby'})) {
760 $p->{'payby'} =~ /^([A-Z]{4})$/
761 or return { 'error' => "illegal_payby " . $p->{'payby'} };
765 my $conf = new FS::Conf;
767 if ( $payby =~ /^(CARD|DCRD)$/ ) {
769 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
771 if ( $new->payinfo eq $cust_main->paymask ) {
772 $new->payinfo($cust_main->payinfo);
773 $new->paycvv( $p->{'paycvv'} || $cust_main->paycvv );
775 $new->payinfo($p->{'payinfo'});
776 return { 'error' => 'CVV2 is required' }
777 if ! $p->{'paycvv'} && $conf->exists('selfservice-onfile_require_cvv');
778 $new->paycvv( $p->{'paycvv'} )
781 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
783 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
786 $p->{'payinfo1'} =~ /^([\dx]+)$/
787 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
789 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
790 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
792 $payinfo = $payinfo1. '@'. $payinfo2;
794 $new->payinfo( ($payinfo eq $cust_main->paymask)
795 ? $cust_main->payinfo
799 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
801 } elsif ( $payby =~ /^(BILL)$/ ) {
803 } elsif ( $payby ) { #notyet ready
804 return { 'error' => "unknown payby $payby" };
808 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
809 #false laziness with httemplate/edit/process/cust_main.cgi
810 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
811 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
813 @invoicing_list = $cust_main->invoicing_list;
816 my $error = $new->replace($cust_main, \@invoicing_list);
817 return { 'error' => $error } if $error;
820 return { 'error' => '' };
825 my $session = _cache->get($p->{'session_id'})
826 or return { 'error' => "Can't resume session" }; #better error message
832 my $conf = new FS::Conf;
833 use vars qw($payment_info); #cache for performance
834 unless ( $payment_info ) {
836 my %states = map { $_->state => 1 }
837 qsearch('cust_main_county', {
838 'country' => $conf->config('countrydefault') || 'US'
841 my %cust_paybys = map { $_ => 1 }
842 map { FS::payby->payby2payment($_) }
843 $conf->config('signup_server-payby');
845 my @cust_paybys = keys %cust_paybys;
849 #list all counties/states/countries
850 'cust_main_county' =>
851 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
853 #shortcut for one-country folks
855 [ sort { $a cmp $b } keys %states ],
857 'card_types' => card_types(),
859 'withcvv' => $conf->exists('selfservice-require_cvv'), #or enable optional cvv?
860 'require_cvv' => $conf->exists('selfservice-require_cvv'),
861 'onfile_require_cvv' => $conf->exists('selfservice-onfile_require_cvv'),
863 'paytypes' => [ @FS::cust_main::paytypes ],
865 'paybys' => [ $conf->config('signup_server-payby') ],
866 'cust_paybys' => \@cust_paybys,
868 'stateid_label' => FS::Msgcat::_gettext('stateid'),
869 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
871 'show_ss' => $conf->exists('show_ss'),
872 'show_stateid' => $conf->exists('show_stateid'),
873 'show_paystate' => $conf->exists('show_bankstate'),
875 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
877 'credit_card_surcharge_percentage' => scalar($conf->config('credit-card-surcharge-percentage')),
886 my %return = %$payment_info;
888 my $custnum = $session->{'custnum'};
890 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
891 or return { 'error' => "unknown custnum $custnum" };
893 $return{'hide_payment_fields'} = [
895 my $pg = payment_gateway($cust_main, $_);
896 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
897 } @{ $return{cust_paybys} }
900 $return{balance} = $cust_main->balance; #XXX pkg-balances?
902 $return{payname} = $cust_main->payname
903 || ( $cust_main->first. ' '. $cust_main->get('last') );
905 $return{$_} = $cust_main->bill_location->get($_)
906 for qw(address1 address2 city state zip);
908 $return{payby} = $cust_main->payby;
909 $return{stateid_state} = $cust_main->stateid_state;
911 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
912 $return{card_type} = cardtype($cust_main->payinfo);
913 $return{payinfo} = $cust_main->paymask;
915 @return{'month', 'year'} = $cust_main->paydate_monthyear;
919 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
920 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
921 $return{payinfo1} = $payinfo1;
922 $return{payinfo2} = $payinfo2;
923 $return{paytype} = $cust_main->paytype;
924 $return{paystate} = $cust_main->paystate;
925 $return{payname} = $cust_main->payname; # override 'first/last name' default from above, if any. Is instution-name here. (#15819)
928 if ( $conf->config('prepayment_discounts-credit_type') ) {
930 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
933 #doubleclick protection
935 $return{payunique} = "webui-MyAccount-$_date-$$-". rand() * 2**32; #new
936 $return{paybatch} = $return{payunique}; #back compat
938 return { 'error' => '',
944 #some false laziness with httemplate/process/payment.cgi - look there for
945 #ACH and CVV support stuff
947 sub validate_payment {
950 my $session = _cache->get($p->{'session_id'})
951 or return { 'error' => "Can't resume session" }; #better error message
953 my $custnum = $session->{'custnum'};
955 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
956 or return { 'error' => "unknown custnum $custnum" };
958 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
959 or return { 'error' => gettext('illegal_amount') };
961 return { error => 'Amount must be greater than 0' } unless $amount > 0;
963 #false laziness w/tr-amount_fee.html, but we don't want selfservice users
964 #changing the hidden form values
965 my $conf = new FS::Conf;
966 my $fee_display = $conf->config('selfservice_process-display') || 'add';
967 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
968 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
969 if ( $fee_display eq 'add'
971 and ! $fee_skip_first || scalar($cust_main->cust_pay)
974 my $fee_pkg = qsearchs('part_pkg', { pkgpart=>$fee_pkgpart } );
975 $amount = sprintf('%.2f', $amount + $fee_pkg->option('setup_fee') );
978 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
979 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
980 my $discount_term = $1;
982 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
983 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
986 $p->{'payunique'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
987 or return { 'error' => gettext('illegal_text'). " payunique: ". $p->{'payunique'} };
990 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
991 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
994 $payunique = $paybatch if ! length($payunique) && length($paybatch);
996 $p->{'payby'} ||= 'CARD';
997 $p->{'payby'} =~ /^([A-Z]{4})$/
998 or return { 'error' => "illegal_payby " . $p->{'payby'} };
1001 #false laziness w/process/payment.cgi
1004 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
1006 $p->{'payinfo1'} =~ /^([\dx]+)$/
1007 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1009 $p->{'payinfo2'} =~ /^([\dx]+)$/
1010 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1012 $payinfo = $payinfo1. '@'. $payinfo2;
1014 $payinfo = $cust_main->payinfo
1015 if $cust_main->paymask eq $payinfo;
1017 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
1019 $payinfo = $p->{'payinfo'};
1023 #more intelligent matching will be needed here if you change
1024 #card_masking_method and don't remove existing paymasks
1025 if ( $cust_main->paymask eq $payinfo ) {
1026 $payinfo = $cust_main->payinfo;
1030 $payinfo =~ s/\D//g;
1031 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
1032 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1036 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1037 return { 'error' => gettext('unknown_card_type') }
1038 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
1040 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
1041 if ( cardtype($payinfo) eq 'American Express card' ) {
1042 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
1043 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
1046 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
1047 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
1050 } elsif ( $conf->exists('selfservice-onfile_require_cvv') ) {
1051 return { 'error' => 'CVV2 is required' };
1052 } elsif ( !$onfile && $conf->exists('selfservice-require_cvv') ) {
1053 return { 'error' => 'CVV2 is required' };
1057 die "unknown payby $payby";
1060 my %payby2fields = (
1061 'CARD' => [ qw( paystart_month paystart_year payissue payip
1062 address1 address2 city state zip country ) ],
1063 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
1067 $card_type = cardtype($payinfo) if $payby eq 'CARD';
1070 'cust_main' => $cust_main, #XXX or just custnum??
1071 'amount' => sprintf('%.2f', $amount),
1073 'payinfo' => $payinfo,
1074 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
1075 'card_type' => $card_type,
1076 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
1077 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
1078 'month' => $p->{'month'},
1079 'year' => $p->{'year'},
1080 'payname' => $payname,
1081 'payunique' => $payunique,
1082 'paybatch' => $paybatch,
1083 'paycvv' => $paycvv,
1084 'payname' => $payname,
1085 'discount_term' => $discount_term,
1086 'pkgnum' => $session->{'pkgnum'},
1087 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
1097 my $validate = validate_payment($p);
1098 return $validate if $validate->{'error'};
1100 my $conf = new FS::Conf;
1101 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
1102 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
1104 +{ map { $_=>$validate->{$_} }
1105 qw( card_type paymask payname paydate_pretty month year amount
1106 address1 address2 city state zip country
1112 sub process_stored_payment {
1115 my $session_id = $p->{'session_id'};
1117 my $payment_info = _cache->get( "payment_$session_id" )
1118 or return { 'error' => "Can't resume session" }; #better error message
1120 do_process_payment($payment_info);
1124 sub process_payment {
1127 my $payment_info = validate_payment($p);
1128 return $payment_info if $payment_info->{'error'};
1130 do_process_payment($payment_info);
1134 sub do_process_payment {
1135 my $validate = shift;
1137 my $cust_main = $validate->{'cust_main'};
1139 my $amount = delete $validate->{'amount'};
1142 my $payby = delete $validate->{'payby'};
1144 if ( $validate->{'save'} ) {
1145 my $new = new FS::cust_main { $cust_main->hash };
1146 if ($payby eq 'CARD' || $payby eq 'DCRD') {
1147 $new->set( $_ => $validate->{$_} )
1148 foreach qw( payname paystart_month paystart_year payissue payip );
1149 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
1151 my $bill_location = FS::cust_location->new({
1152 map { $_ => $validate->{$_} }
1153 qw(address1 address2 city state country zip)
1155 $new->set('bill_location' => $bill_location);
1156 # but don't allow the service address to change this way.
1158 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
1159 $new->set( $_ => $validate->{$_} )
1160 foreach qw( payname payip paytype paystate
1161 stateid stateid_state );
1162 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
1164 $new->payinfo( $validate->{'payinfo'} ); #to properly set paymask
1165 $new->set( 'paydate' => $validate->{'paydate'} );
1166 my $error = $new->replace($cust_main);
1168 #no, this causes customers to process their payments again
1169 #return { 'error' => $error };
1170 #XXX just warn verosely for now so i can figure out how these happen in
1171 # the first place, eventually should redirect them to the "change
1172 #address" page but indicate if the payment processed?
1173 delete($validate->{'payinfo'}); #don't want to log this!
1174 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
1175 "NEW: ". Dumper($new)."\n".
1176 "OLD: ". Dumper($cust_main)."\n".
1177 "PACKET: ". Dumper($validate)."\n";
1183 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
1187 'paynum_ref' => \$paynum,
1190 return { 'error' => $error } if $error;
1192 #no error, so order the fee package if applicable...
1193 my $conf = new FS::Conf;
1194 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
1195 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
1197 if ( $fee_pkgpart and ! $fee_skip_first || scalar($cust_main->cust_pay) ) {
1199 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $fee_pkgpart };
1201 $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
1202 return { 'error' => "payment processed successfully, but error ordering fee: $error" }
1205 #and generate an invoice for it now too
1206 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1207 return { 'error' => "payment processed and fee ordered sucessfully, but error billing fee: $error" }
1212 $cust_main->apply_payments;
1215 my $receipt_html = '';
1217 # currently supported for realtime CC only; send receipt data to SS
1218 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
1221 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
1224 <TD ALIGN="right">Payment#</TD>
1225 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
1229 <TD ALIGN="right">Date</TD>
1231 <TD BGCOLOR="#FFFFFF"><B>! .
1232 time2str("%a %b %o, %Y %r", $cust_pay->_date)
1238 <TD ALIGN="right">Amount</TD>
1239 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
1244 <TD ALIGN="right">Payment method</TD>
1245 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
1258 'amount' => sprintf('%.2f', $cust_pay->paid),
1259 'date' => $cust_pay->_date,
1260 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
1261 'time_pretty' => time2str('%T', $cust_pay->_date),
1262 'auth_num' => $cust_pay->auth,
1263 'order_num' => $cust_pay->order_number,
1264 'receipt_html' => $receipt_html,
1271 'receipt_html' => '',
1278 sub realtime_collect {
1281 my $session = _cache->get($p->{'session_id'})
1282 or return { 'error' => "Can't resume session" }; #better error message
1284 my $custnum = $session->{'custnum'};
1286 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1287 or return { 'error' => "unknown custnum $custnum" };
1290 if ( $p->{'amount'} ) {
1291 $amount = $p->{'amount'};
1293 elsif ( $session->{'pkgnum'} ) {
1294 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1297 $amount = $cust_main->balance;
1300 my $error = $cust_main->realtime_collect(
1301 'method' => $p->{'method'},
1302 'amount' => $amount,
1303 'pkgnum' => $session->{'pkgnum'},
1304 'session_id' => $p->{'session_id'},
1308 return { 'error' => $error } unless ref( $error );
1310 return { 'error' => '', amount => $amount, %$error };
1313 sub start_thirdparty {
1315 my $session = _cache->get($p->{'session_id'})
1316 or return { 'error' => "Can't resume session" }; #better error message
1317 my $custnum = $session->{'custnum'};
1318 my $cust_main = FS::cust_main->by_key($custnum);
1320 my $amount = $p->{'amount'}
1321 or return { error => 'no amount' };
1323 my $result = $cust_main->create_payment(
1324 'method' => $p->{'method'},
1325 'amount' => $p->{'amount'},
1326 'pkgnum' => $session->{'pkgnum'},
1327 'session_id' => $p->{'session_id'},
1330 if ( ref($result) ) { # hashref or error
1333 return { error => $result };
1337 sub finish_thirdparty {
1339 my $session_id = delete $p->{'session_id'};
1340 my $session = _cache->get($session_id)
1341 or return { 'error' => "Can't resume session" };
1342 my $custnum = $session->{'custnum'};
1343 my $cust_main = FS::cust_main->by_key($custnum);
1345 if ( $p->{_cancel} ) {
1346 # customer backed out of making a payment
1347 return $cust_main->cancel_payment( $session_id );
1349 my $result = $cust_main->execute_payment( $session_id, %$p );
1350 if ( ref($result) ) {
1353 return { error => $result };
1357 sub process_payment_order_pkg {
1360 my $hr = process_payment($p);
1361 return $hr if $hr->{'error'};
1366 sub process_payment_order_renew {
1369 my $hr = process_payment($p);
1370 return $hr if $hr->{'error'};
1375 sub process_prepay {
1379 my $session = _cache->get($p->{'session_id'})
1380 or return { 'error' => "Can't resume session" }; #better error message
1384 my $custnum = $session->{'custnum'};
1386 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1387 or return { 'error' => "unknown custnum $custnum" };
1389 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1390 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1398 return { 'error' => $error } if $error;
1400 return { 'error' => '',
1401 'amount' => $amount,
1402 'seconds' => $seconds,
1403 'duration' => duration_exact($seconds),
1404 'upbytes' => $upbytes,
1405 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1406 'downbytes' => $downbytes,
1407 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1408 'totalbytes'=> $totalbytes,
1409 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1416 my $session = _cache->get($p->{'session_id'})
1417 or return { 'error' => "Can't resume session" }; #better error message
1419 my $custnum = $session->{'custnum'};
1421 my $invnum = $p->{'invnum'};
1423 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1424 'custnum' => $custnum } )
1425 or return { 'error' => "Can't find invnum" };
1429 return { 'error' => '',
1430 'invnum' => $invnum,
1431 'invoice_text' => join('', $cust_bill->print_text ),
1432 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1439 my $session = _cache->get($p->{'session_id'})
1440 or return { 'error' => "Can't resume session" }; #better error message
1442 my $custnum = $session->{'custnum'};
1444 my $invnum = $p->{'invnum'};
1446 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1447 'custnum' => $custnum } )
1448 or return { 'error' => "Can't find invnum" };
1452 return { 'error' => '',
1453 'invnum' => $invnum,
1454 'invoice_pdf' => $cust_bill->print_pdf({
1455 'unsquelch_cdr' => 1,
1456 'locale' => $p->{'locale'},
1462 sub legacy_invoice {
1464 my $session = _cache->get($p->{'session_id'})
1465 or return { 'error' => "Can't resume session" }; #better error message
1467 my $custnum = $session->{'custnum'};
1469 my $legacyinvnum = $p->{'legacyinvnum'};
1472 'legacyinvnum' => $legacyinvnum,
1473 'custnum' => $custnum,
1476 my $legacy_cust_bill =
1477 qsearchs('legacy_cust_bill', { %hash, 'locale' => $p->{'locale'} } )
1478 || qsearchs('legacy_cust_bill', \%hash )
1479 or return { 'error' => "Can't find legacyinvnum" };
1483 return { 'error' => '',
1484 'legacyinvnum' => $legacyinvnum,
1485 'legacyid' => $legacy_cust_bill->legacyid,
1486 'invoice_html' => $legacy_cust_bill->content_html,
1491 sub legacy_invoice_pdf {
1493 my $session = _cache->get($p->{'session_id'})
1494 or return { 'error' => "Can't resume session" }; #better error message
1496 my $custnum = $session->{'custnum'};
1498 my $legacyinvnum = $p->{'legacyinvnum'};
1500 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1501 'legacyinvnum' => $legacyinvnum,
1502 'custnum' => $custnum,
1503 }) or return { 'error' => "Can't find legacyinvnum" };
1507 return { 'error' => '',
1508 'legacyinvnum' => $legacyinvnum,
1509 'legacyid' => $legacy_cust_bill->legacyid,
1510 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1518 #sessioning for this? how do we get the session id to the backend invoice
1519 # template so it can add it to the link, blah
1522 if ( $p->{'invnum'} ) {
1523 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1524 or return { 'error' => 'unknown invnum' };
1525 $agentnum = $cust_bill->cust_main->agentnum;
1528 my $templatename = $p->{'template'} || $p->{'templatename'};
1530 #false laziness-ish w/view/cust_bill-logo.cgi
1532 my $conf = new FS::Conf;
1533 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1534 $templatename = "_$1";
1539 my $filename = "logo$templatename.png";
1541 return { 'error' => '',
1542 'logo' => $conf->config_binary($filename, $agentnum),
1543 'content_type' => 'image/png', #should allow gif, jpg too
1550 my $session = _cache->get($p->{'session_id'})
1551 or return { 'error' => "Can't resume session" }; #better error message
1553 my $custnum = $session->{'custnum'};
1555 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1556 or return { 'error' => "unknown custnum $custnum" };
1558 my $conf = new FS::Conf;
1560 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1562 my @cust_bill = grep ! $_->hide, $cust_main->cust_bill;
1567 #not super efficient, we also run cust_bill_pay/cust_credited inside owed
1568 my @payments_and_credits = sort {$b->_date <=> $a->_date} ($_->cust_bill_pay,$_->cust_credited);
1569 my $owed = $_->owed;
1571 +{ 'invnum' => $_->invnum,
1572 '_date' => $_->_date,
1573 'date' => time2str("%b %o, %Y", $_->_date),
1574 'date_short' => time2str("%m-%d-%Y", $_->_date),
1575 'previous' => sprintf('%.2f', ($_->previous)[0]),
1576 'charged' => sprintf('%.2f', $_->charged),
1577 'owed' => sprintf('%.2f', $owed),
1578 'balance' => sprintf('%.2f', $balance),
1579 'lastpay' => @payments_and_credits
1580 ? time2str("%b %o, %Y", $payments_and_credits[0]->_date)
1586 return { 'error' => '',
1587 'balance' => $cust_main->balance,
1588 'money_char' => $conf->config("money_char") || '$',
1589 'invoices' => $invoices,
1590 'legacy_invoices' => [
1592 +{ 'legacyinvnum' => $_->legacyinvnum,
1593 'legacyid' => $_->legacyid,
1594 '_date' => $_->_date,
1595 'date' => time2str("%b %o, %Y", $_->_date),
1596 'date_short' => time2str("%m-%d-%Y", $_->_date),
1597 'charged' => sprintf('%.2f', $_->charged),
1598 'has_content' => ( length($_->content_pdf)
1599 || length($_->content_html) ),
1609 my $session = _cache->get($p->{'session_id'})
1610 or return { 'error' => "Can't resume session" }; #better error message
1612 my $custnum = $session->{'custnum'};
1614 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1615 or return { 'error' => "unknown custnum $custnum" };
1617 my @errors = $cust_main->cancel( 'quiet'=>1 );
1619 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1621 return { 'error' => $error };
1628 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1629 return { 'error' => $session } if $context eq 'error';
1631 my $search = { 'custnum' => $custnum };
1632 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1633 my $cust_main = qsearchs('cust_main', $search )
1634 or return { 'error' => "unknown custnum $custnum" };
1636 my $conf = new FS::Conf;
1637 my $immutable = $conf->exists('selfservice_immutable-package');
1639 # the duplication below is necessary:
1640 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1641 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1642 # non-backwards-compatible change breaking the software of anyone using the API
1643 # instead of the stock selfservice
1644 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1645 if( $conf->exists('selfservice_server-view-wholesale') ) {
1646 return { 'svcnum' => $session->{'svcnum'},
1647 'custnum' => $custnum,
1648 'cust_pkg' => [ map {
1650 immutable => $immutable,
1651 part_pkg => [ map $_->hashref, $_->part_pkg ],
1653 [ map $_->hashref, $_->available_part_svc ],
1655 [ map { my $ref = { $_->hash,
1656 label => [ $_->label ],
1658 $ref->{_password} = $_->svc_x->_password
1659 if $context eq 'agent'
1660 && $conf->exists('agent-showpasswords')
1661 && $_->part_svc->svcdb eq 'svc_acct';
1666 } $cust_main->cust_pkg
1669 small_custview( $cust_main, $conf->config('countrydefault') ),
1670 'wholesale_view' => 1,
1671 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1672 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1673 'lnp' => $conf->exists('svc_phone-lnp'),
1677 { 'svcnum' => $session->{'svcnum'},
1678 'custnum' => $custnum,
1679 'cust_pkg' => [ map {
1680 my $primary_cust_svc = $_->primary_cust_svc;
1683 immutable => $immutable,
1684 pkg_label => $_->pkg_locale,
1685 status => $_->status,
1686 statuscolor => $_->statuscolor,
1688 [ map { $_->hashref }
1689 grep { $_->selfservice_access ne 'hidden' }
1690 $_->available_part_svc
1693 [ map { my $ref = { $_->hash,
1694 label => [ $_->label ],
1696 $ref->{_password} = $_->svc_x->_password
1697 if $context eq 'agent'
1698 && $conf->exists('agent-showpasswords')
1699 && $_->part_svc->svcdb eq 'svc_acct';
1700 $ref->{svchash} = { $_->svc_x->hash } if
1701 $_->part_svc->svcdb eq 'svc_phone';
1702 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1703 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1706 grep { $_->part_svc->selfservice_access ne 'hidden' }
1711 ? { $primary_cust_svc->hash,
1712 label => [ $primary_cust_svc->label ],
1713 finger => $primary_cust_svc->svc_x->finger, #uuh
1714 $primary_cust_svc->part_svc->hash,
1718 } $cust_main->ncancelled_pkgs
1721 small_custview( $cust_main, $conf->config('countrydefault') ),
1722 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1730 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1731 return { 'error' => $session } if $context eq 'error';
1733 my $conf = new FS::Conf;
1735 my $hide_usage = $conf->exists('selfservice_hide-usage') ? 1 : 0;
1736 my $search = { 'custnum' => $custnum };
1737 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1738 my $cust_main = qsearchs('cust_main', $search )
1739 or return { 'error' => "unknown custnum $custnum" };
1741 my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
1742 if ( ! $pkgnum && $p->{'svcnum'} ) {
1743 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
1744 $pkgnum = $cust_svc->pkgnum if $cust_svc;
1748 my @cust_pkg_usage = ();
1749 foreach my $cust_pkg ( $p->{'ncancelled'}
1750 ? $cust_main->ncancelled_pkgs
1751 : $cust_main->unsuspended_pkgs ) {
1752 next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
1753 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1754 push @cust_pkg_usage, $cust_pkg->cust_pkg_usage;
1757 @cust_svc = grep { $_->part_svc->selfservice_access ne 'hidden' } @cust_svc;
1760 foreach (@cust_pkg_usage) {
1761 my $part = $_->part_pkg_usage;
1762 my $tag = $part->description . ($part->shared ? 1 : 0);
1763 my $row = $usage_pools{$tag}
1764 ||= [ $part->description, 0, 0, $part->shared ? 1 : 0 ];
1765 $row->[1] += sprintf('%.1f', $_->minutes); # minutes remaining
1766 $row->[2] += $part->minutes; # minutes total
1768 } # otherwise just leave them empty
1770 if ( $p->{'svcdb'} ) {
1771 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1773 : ref($p->{'svcdb'}) eq 'ARRAY'
1774 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1775 : { $p->{'svcdb'} => 1 };
1776 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1779 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1782 my @svcs; # stuff to return to the client
1783 foreach my $cust_svc (@cust_svc) {
1784 my $svc_x = $cust_svc->svc_x;
1785 my($label, $value) = $cust_svc->label;
1786 my $part_svc = $cust_svc->part_svc;
1787 my $svcdb = $part_svc->svcdb;
1788 my $cust_pkg = $cust_svc->cust_pkg;
1789 my $part_pkg = $cust_pkg->part_pkg;
1792 'svcnum' => $cust_svc->svcnum,
1793 'display_svcnum' => $cust_svc->display_svcnum,
1797 'pkg_label' => $cust_pkg->pkg_locale,
1798 'pkg_status' => $cust_pkg->status,
1799 'readonly' => ($part_svc->selfservice_access eq 'readonly'),
1802 # would it make sense to put this in a svc_* method?
1804 if ( $svcdb eq 'svc_acct' ) {
1805 foreach (qw(username email finger seconds)) {
1806 $hash{$_} = $svc_x->$_;
1812 'upbytes' => display_bytecount($svc_x->upbytes),
1813 'downbytes' => display_bytecount($svc_x->downbytes),
1814 'totalbytes' => display_bytecount($svc_x->totalbytes),
1816 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1817 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1818 'recharge_upbytes' =>
1819 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1820 'recharge_downbytes' =>
1821 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1822 'recharge_totalbytes' =>
1823 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1828 } elsif ( $svcdb eq 'svc_dsl' ) {
1830 $hash{'phonenum'} = $svc_x->phonenum;
1831 if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
1832 $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
1833 $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
1836 $hash{'name'} = $cust_main->name;
1838 # no usage to hide here
1840 } elsif ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
1842 # could potentially show lots of things...
1843 $hash{'outbound'} = 1;
1844 $hash{'inbound'} = 0;
1845 if ( $svcdb eq 'svc_phone' ) {
1846 if ( $part_pkg->plan eq 'voip_inbound' ) {
1847 $hash{'outbound'} = 0;
1848 $hash{'inbound'} = 1;
1849 } elsif ( $part_pkg->option('selfservice_inbound_format')
1850 or $conf->config('selfservice-default_inbound_cdr_format')
1852 $hash{'inbound'} = 1;
1855 foreach (qw(inbound outbound)) {
1856 # hmm...we can't filter by status here, because there might
1857 # not be cdr_terminations at all. have to go by date.
1858 # find all since the last bill date.
1859 # XXX cdr types? we are going to need them.
1861 my $sum_cdr = $svc_x->sum_cdrs(
1862 'inbound' => ( $_ eq 'inbound' ? 1 : 0 ),
1863 'begin' => ($cust_pkg->last_bill || 0),
1865 'disable_charged_party' => 1,
1867 $hash{$_} = $sum_cdr->hashref;
1870 } # not hiding usage
1874 } # foreach $cust_svc
1877 'svcnum' => $session->{'svcnum'},
1878 'custnum' => $custnum,
1879 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1880 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1883 map { $usage_pools{$_} }
1887 'hide_usage' => $hide_usage,
1892 sub _customer_svc_x {
1893 my($custnum, $svcnum, $table) = (shift, shift, shift);
1894 my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
1896 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1897 my $search = " AND custnum = $1";
1898 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1901 'table' => ($table || 'svc_acct'),
1902 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1903 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
1904 #'LEFT JOIN cust_main USING ( custnum ) ',
1905 'hashref' => $hashref,
1906 'extra_sql' => $search, #important
1911 sub svc_status_html {
1914 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1915 return { 'error' => $session } if $context eq 'error';
1917 #XXX only svc_dsl for now
1918 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
1919 or return { 'error' => "Service not found" };
1921 my $html = $svc_x->getstatus_html;
1923 return { 'html' => $html };
1927 sub svc_status_hash {
1930 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1931 return { 'error' => $session } if $context eq 'error';
1933 #XXX only svc_acct for now
1934 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1935 or return { 'error' => "Service not found" };
1937 my ( $html, $hashref ) = $svc_x->export_getstatus;
1942 sub set_svc_status_hash { _svc_method_X(shift, 'export_setstatus') }
1943 sub set_svc_status_listadd { _svc_method_X(shift, 'export_setstatus_listadd') }
1944 sub set_svc_status_listdel { _svc_method_X(shift, 'export_setstatus_listdel') }
1945 sub set_svc_status_vacationadd { _svc_method_X(shift, 'export_setstatus_vacationadd') }
1946 sub set_svc_status_vacationdel { _svc_method_X(shift, 'export_setstatus_vacationdel') }
1949 my( $p, $method ) = @_;
1951 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1952 return { 'error' => $session } if $context eq 'error';
1954 #XXX only svc_acct for now
1955 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1956 or return { 'error' => "Service not found" };
1958 warn "$method ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
1960 my $error = $svc_x->$method($p); #$p? returns error?
1961 return { 'error' => $error } if $error;
1963 return {}; #? { 'error' => '' }
1967 sub acct_forward_info {
1970 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1971 return { 'error' => $session } if $context eq 'error';
1973 my $svc_forward = _customer_svc_x( $custnum,
1974 { 'srcsvc' => $p->{'svcnum'} },
1977 or return { 'error' => '',
1981 return { 'error' => '',
1982 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
1987 sub process_acct_forward {
1989 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1990 return { 'error' => $session } if $context eq 'error';
1992 my $old = _customer_svc_x( $custnum,
1993 { 'srcsvc' => $p->{'svcnum'} },
1997 if ( $p->{'dst'} eq '' ) {
1999 my $error = $old->delete;
2000 return { 'error' => $error };
2002 return { 'error' => '' };
2005 my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
2006 'dst' => $p->{'dst'},
2011 $new->svcnum($old->svcnum);
2012 my $cust_svc = $old->cust_svc;
2013 $new->svcpart($old->svcpart);
2014 $new->pkgnuym($old->pkgnum);
2015 $error = $new->replace($old);
2017 my $conf = new FS::Conf;
2018 $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
2020 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
2021 or return { 'error' => 'No service' }; #how would we even get here?
2023 $new->pkgnum( $svc_acct->cust_svc->pkgnum );
2025 $error = $new->insert;
2028 return { 'error' => $error };
2032 sub list_dsl_devices {
2035 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2036 return { 'error' => $session } if $context eq 'error';
2038 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2039 or return { 'error' => "Service not found" };
2042 'devices' => [ map {
2043 +{ 'mac_addr' => $_->mac_addr };
2044 } $svc_dsl->dsl_device
2050 sub add_dsl_device {
2053 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2054 return { 'error' => $session } if $context eq 'error';
2056 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2057 or return { 'error' => "Service not found" };
2059 return { 'error' => 'No MAC address supplied' }
2060 unless length($p->{'mac_addr'});
2062 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
2063 'mac_addr' => scalar($p->{'mac_addr'}),
2065 my $error = $dsl_device->insert;
2066 return { 'error' => $error };
2070 sub delete_dsl_device {
2073 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2074 return { 'error' => $session } if $context eq 'error';
2076 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2077 or return { 'error' => "Service not found" };
2079 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
2080 'mac_addr' => scalar($p->{'mac_addr'}),
2083 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
2085 my $error = $dsl_device->delete;
2086 return { 'error' => $error };
2092 _usage_details( \&_port_graph, $p,
2093 'svcdb' => 'svc_port',
2098 my($svc_port, $begin, $end) = @_;
2100 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
2101 push @usage, { 'png' => $pngOrError };
2105 sub _list_svc_usage {
2106 my($svc_acct, $begin, $end) = @_;
2108 foreach my $part_export (
2109 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
2110 qw( sqlradius sqlradius_withdomain )
2112 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
2117 sub list_svc_usage {
2118 _usage_details(\&_list_svc_usage, @_);
2121 sub _list_support_usage {
2122 my($svc_acct, $begin, $end) = @_;
2124 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
2125 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
2127 push @usage, { 'seconds' => $_->seconds,
2128 'support' => $_->support,
2129 '_date' => $_->_date,
2130 'id' => $_->transaction_id,
2131 'creator' => $_->creator,
2132 'subject' => $_->subject,
2133 'status' => $_->status,
2134 'ticketid' => $_->ticketid,
2140 sub list_support_usage {
2141 _usage_details(\&_list_support_usage, @_);
2144 sub _list_cdr_usage {
2145 # XXX CDR type support...
2146 # XXX any way to do a paged search on this?
2147 # we have to return the results all at once...
2148 my($svc_x, $begin, $end, %opt) = @_;
2149 map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
2153 'disable_charged_party' => 1,
2158 sub list_cdr_usage {
2160 _usage_details( \&_list_cdr_usage, $p );
2163 sub _usage_details {
2164 my($callback, $p, %opt) = @_;
2165 my $conf = FS::Conf->new;
2167 if ( $conf->exists('selfservice_hide-usage') ) {
2168 return { 'error' => 'Viewing usage is not allowed.' };
2171 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2172 return { 'error' => $session } if $context eq 'error';
2174 my $search = { 'svcnum' => $p->{'svcnum'} };
2175 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2177 my $cust_svc = qsearchs( 'cust_svc', $search );
2178 return { 'error' => 'No service selected in list_svc_usage' }
2181 my $svc_x = $cust_svc->svc_x;
2182 my $svcdb = $svc_x->table;
2183 my $cust_pkg = $cust_svc->cust_pkg;
2184 my $freq = $cust_pkg->part_pkg->freq;
2187 if ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
2189 if ( $p->{inbound} ) {
2190 $format = $cust_pkg->part_pkg->option('selfservice_inbound_format')
2191 || $conf->config('selfservice-default_inbound_cdr_format')
2192 || 'source_default';
2193 $callback_opt{inbound} = 1;
2195 $format = $cust_pkg->part_pkg->option('selfservice_format')
2196 || $conf->config('selfservice-default_cdr_format')
2200 $callback_opt{format} = $format;
2201 $callback_opt{use_clid} = 1;
2202 $header = [ split(',', FS::cdr::invoice_header($format) ) ];
2205 my $start = $cust_pkg->setup;
2206 #my $end = $cust_pkg->bill; # or time?
2209 unless ( $p->{beginning} ) {
2210 $p->{beginning} = $cust_pkg->last_bill;
2211 $p->{ending} = $end;
2214 die "illegal beginning" if $p->{beginning} !~ /^\d*$/;
2215 die "illegal ending" if $p->{ending} !~ /^\d*$/;
2217 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
2221 if ( $conf->exists('selfservice-hide_cdr_price') ) {
2222 # ugly kludge, I know
2223 my ($delete_col) = grep { $header->[$_] eq 'Price' } (0..scalar(@$header));
2224 if (defined $delete_col) {
2225 delete($_->[$delete_col]) foreach ($header, @usage);
2229 #kinda false laziness with FS::cust_main::bill, but perhaps
2230 #we should really change this bit to DateTime and DateTime::Duration
2232 #change this bit to use Date::Manip? CAREFUL with timezones (see
2233 # mailing list archive)
2234 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
2235 (localtime($p->{ending}) )[0,1,2,3,4,5];
2236 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
2237 (localtime($p->{beginning}) )[0,1,2,3,4,5];
2239 if ( $freq =~ /^\d+$/ ) {
2241 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
2243 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
2244 } elsif ( $freq =~ /^(\d+)w$/ ) {
2246 $nmday += $weeks * 7;
2247 $pmday -= $weeks * 7;
2248 } elsif ( $freq =~ /^(\d+)d$/ ) {
2252 } elsif ( $freq =~ /^(\d+)h$/ ) {
2257 return { 'error' => "unparsable frequency: ". $freq };
2260 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
2261 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
2265 'svcnum' => $p->{svcnum},
2266 'beginning' => $p->{beginning},
2267 'ending' => $p->{ending},
2268 'inbound' => $p->{inbound},
2269 'previous' => ($previous > $start) ? $previous : $start,
2270 'next' => ($next < $end) ? $next : $end,
2271 'header' => $header,
2279 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2280 return { 'error' => $session } if $context eq 'error';
2282 my $search = { 'custnum' => $custnum };
2283 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2284 my $cust_main = qsearchs('cust_main', $search )
2285 or return { 'error' => "unknown custnum $custnum" };
2287 my $status = $cust_main->status;
2289 my %order_pkg_options = ();
2290 if ( $p->{locationnum} > 0 ) {
2291 $order_pkg_options{locationnum} = delete($p->{locationnum});
2292 } elsif ( $p->{address1} ) {
2293 $order_pkg_options{'cust_location'} = new FS::cust_location {
2294 map { $_ => $p->{$_} }
2295 qw( address1 address2 city county state zip country )
2299 #false laziness w/ClientAPI/Signup.pm
2301 my $cust_pkg = new FS::cust_pkg ( {
2302 'custnum' => $custnum,
2303 'pkgpart' => $p->{'pkgpart'},
2304 'quantity' => $p->{'quantity'} || 1,
2306 my $error = $cust_pkg->check;
2307 return { 'error' => $error } if $error;
2310 unless ( $p->{'svcpart'} eq 'none' ) {
2314 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
2316 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2317 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
2318 $svcdb = $part_svc->svcdb;
2320 $svcdb = 'svc_acct';
2322 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
2325 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
2326 'svc_domain' => [ qw( domain ) ],
2327 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
2328 'svc_external' => [ qw( id title ) ],
2329 'svc_pbx' => [ qw( id title ) ],
2332 my $svc_x = "FS::$svcdb"->new( {
2333 'svcpart' => $svcpart,
2334 map { $_ => $p->{$_} } @{$fields{$svcdb}}
2337 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
2340 while ( length($p->{"snarf_machine$snarfnum"}) ) {
2341 my $acct_snarf = new FS::acct_snarf ( {
2342 'machine' => $p->{"snarf_machine$snarfnum"},
2343 'protocol' => $p->{"snarf_protocol$snarfnum"},
2344 'username' => $p->{"snarf_username$snarfnum"},
2345 '_password' => $p->{"snarf_password$snarfnum"},
2348 push @acct_snarf, $acct_snarf;
2350 $svc_x->child_objects( \@acct_snarf );
2353 my $y = $svc_x->setdefault; # arguably should be in new method
2354 return { 'error' => $y } if $y && !ref($y);
2356 $error = $svc_x->check;
2357 return { 'error' => $error } if $error;
2363 $error = $cust_main->order_pkg(
2364 'cust_pkg' => $cust_pkg,
2369 return { 'error' => $error } if $error;
2371 my $conf = new FS::Conf;
2372 if ( $conf->exists('signup_server-realtime') ) {
2374 my $bill_error = _do_bop_realtime( $cust_main, $status );
2377 $cust_pkg->cancel('quiet'=>1);
2380 $cust_pkg->reexport;
2384 $cust_pkg->reexport;
2387 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
2389 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
2396 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2397 return { 'error' => $session } if $context eq 'error';
2399 my $conf = new FS::Conf;
2400 my $immutable = $conf->exists('selfservice_immutable-package');
2401 return { 'error' => "Package modification disabled" } if $immutable;
2403 my $search = { 'custnum' => $custnum };
2404 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2405 my $cust_main = qsearchs('cust_main', $search )
2406 or return { 'error' => "unknown custnum $custnum" };
2408 my $status = $cust_main->status;
2409 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
2410 or return { 'error' => "unknown package $p->{pkgnum}" };
2412 #if someone does need self-service package change of suspended packages,
2413 # figure out how to be more discriminating
2414 return { error=>"Can't change a suspended package", pkgnum=>$cust_pkg->pkgnum}
2415 if $cust_pkg->status eq 'suspended';
2417 my $err_or_cust_pkg = $cust_pkg->change( 'pkgpart' => $p->{'pkgpart'},
2418 'quantity' => $p->{'quantity'} || 1,
2421 return { error=>$err_or_cust_pkg, pkgnum=>$cust_pkg->pkgnum }
2422 unless ref($err_or_cust_pkg);
2424 if ( $conf->exists('signup_server-realtime') ) {
2426 my $bill_error = _do_bop_realtime( $cust_main, $status, 'no_credit'=>1 );
2429 $err_or_cust_pkg->suspend;
2432 $err_or_cust_pkg->reexport;
2436 $err_or_cust_pkg->reexport;
2439 return { error => '', pkgnum => $cust_pkg->pkgnum };
2443 sub order_recharge {
2446 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2447 return { 'error' => $session } if $context eq 'error';
2449 my $search = { 'custnum' => $custnum };
2450 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2451 my $cust_main = qsearchs('cust_main', $search )
2452 or return { 'error' => "unknown custnum $custnum" };
2454 my $status = $cust_main->status;
2455 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
2456 or return { 'error' => "unknown service " . $p->{'svcnum'} };
2458 my $svc_x = $cust_svc->svc_x;
2459 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
2462 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
2463 qw ( recharge_seconds recharge_upbytes recharge_downbytes
2464 recharge_totalbytes );
2465 my $amount = $part_pkg->option('recharge_amount', 1);
2467 my ($l, $v, $d) = $cust_svc->label; # blah
2468 my $pkg = "Recharge $v";
2470 my $bill_error = $cust_main->charge($amount, $pkg,
2471 "time: $vhash{seconds}, up: $vhash{upbytes}," .
2472 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
2473 $part_pkg->taxclass); #meh
2475 my $conf = new FS::Conf;
2476 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
2478 $bill_error = _do_bop_realtime( $cust_main, $status );
2483 my $error = $svc_x->recharge (\%vhash);
2484 return { 'error' => $error } if $error;
2488 my $error = $bill_error;
2489 $error ||= $svc_x->recharge (\%vhash);
2490 return { 'error' => $error } if $error;
2493 return { error => '', svc => $cust_svc->part_svc->svc };
2497 sub _do_bop_realtime {
2498 my ($cust_main, $status, %opt) = @_;
2500 my $old_balance = $cust_main->balance;
2502 my $bill_error = $cust_main->bill
2503 || $cust_main->apply_payments_and_credits;
2505 $bill_error ||= $cust_main->realtime_collect('selfservice' => 1)
2506 if $cust_main->payby =~ /^(CARD|CHEK)$/;
2508 if ( $cust_main->balance > $old_balance
2509 && $cust_main->balance > 0
2510 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/
2511 || $status eq 'suspended'
2515 unless ( $opt{'no_credit'} ) {
2516 #this makes sense. credit is "un-doing" the invoice
2517 my $conf = new FS::Conf;
2518 $cust_main->credit( sprintf("%.2f", $cust_main->balance-$old_balance ),
2519 'self-service decline',
2520 reason_type=>$conf->config('signup_credit_type'),
2522 $cust_main->apply_credits( 'order' => 'newest' );
2525 return { 'error' => '_decline', 'bill_error' => $bill_error };
2534 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2535 return { 'error' => $session } if $context eq 'error';
2537 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2538 or return { 'error' => "unknown custnum $custnum" };
2540 my @cust_pkg = sort { $a->bill <=> $b->bill }
2541 grep { $_->part_pkg->freq ne '0' }
2542 $cust_main->ncancelled_pkgs;
2544 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
2546 my $total = $cust_main->balance;
2549 my $bill = $_->bill;
2550 $total += $_->part_pkg->base_recur($_, \$bill);
2551 my $renew_date = $_->part_pkg->add_freq($_->bill);
2553 'pkgnum' => $_->pkgnum,
2554 'amount' => sprintf('%.2f', $total),
2555 'bill_date' => $_->bill,
2556 'bill_date_pretty' => time2str('%x', $_->bill),
2557 'renew_date' => $renew_date,
2558 'renew_date_pretty' => time2str('%x', $renew_date),
2559 'expire_date' => $_->expire,
2560 'expire_date_pretty' => time2str('%x', $_->expire),
2565 return { 'dates' => \@array };
2569 sub payment_info_renew_info {
2571 my $renew_info = renew_info($p);
2572 my $payment_info = payment_info($p);
2573 return { %$renew_info,
2581 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2582 return { 'error' => $session } if $context eq 'error';
2584 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2585 or return { 'error' => "unknown custnum $custnum" };
2587 my $date = $p->{'date'};
2591 #freeside-daily -n -d $date fs_daily $custnum
2592 $cust_main->bill_and_collect( 'time' => $date,
2593 'invoice_time' => $now,
2594 'actual_time' => $now,
2595 'check_freq' => '1d',
2598 return { 'error' => '' };
2604 my $session = _cache->get($p->{'session_id'})
2605 or return { 'error' => "Can't resume session" }; #better error message
2607 my $custnum = $session->{'custnum'};
2609 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2610 or return { 'error' => "unknown custnum $custnum" };
2612 my $conf = new FS::Conf;
2614 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2615 or return { 'error' => 'Permission denied' };
2617 my $pkgnum = $p->{'pkgnum'};
2619 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2620 'pkgnum' => $pkgnum, } )
2621 or return { 'error' => "unknown pkgnum $pkgnum" };
2623 my $error = $cust_pkg->suspend(reason => $reasonnum);
2624 return { 'error' => $error };
2630 my $session = _cache->get($p->{'session_id'})
2631 or return { 'error' => "Can't resume session" }; #better error message
2633 my $custnum = $session->{'custnum'};
2635 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2636 or return { 'error' => "unknown custnum $custnum" };
2638 my $pkgnum = $p->{'pkgnum'};
2640 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2641 'pkgnum' => $pkgnum, } )
2642 or return { 'error' => "unknown pkgnum $pkgnum" };
2644 my $error = $cust_pkg->cancel('quiet' => 1);
2645 return { 'error' => $error };
2649 sub provision_phone {
2652 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2654 #editing an existing phone number
2655 if ( $p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/ ) {
2656 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2657 return { 'error' => $session } if $context eq 'error';
2659 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2660 return { 'error' => 'service not found' } unless $svc_phone;
2661 return { 'error' => 'invalid svcnum' }
2662 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2664 $svc_phone->email($p->{'email'})
2665 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2666 $svc_phone->forwarddst($p->{'forwarddst'})
2667 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2668 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2669 return { 'error' => $svc_phone->replace };
2673 unless ( $p->{'lnp'} ) {
2674 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2675 $p->{'lnp_status'} = "portingin";
2676 return _provision( 'FS::svc_phone',
2677 [qw(lnp_desired_due_date lnp_other_provider
2678 lnp_other_provider_account phonenum countrycode lnp_status)],
2679 [qw(phonenum countrycode)],
2685 # single DID order (the usual case)
2686 unless (scalar(@bulkdid)) {
2687 return _provision( 'FS::svc_phone',
2688 [qw(phonenum countrycode)],
2689 [qw(phonenum countrycode)],
2695 # bulk DID order case
2697 foreach my $did ( @bulkdid ) {
2698 $did =~ s/[^0-9]//g;
2699 $error = _provision( 'FS::svc_phone',
2700 [qw(phonenum countrycode)],
2701 [qw(phonenum countrycode)],
2703 'pkgnum' => $p->{'pkgnum'},
2704 'svcpart' => $p->{'svcpart'},
2706 'countrycode' => $p->{'countrycode'},
2707 'session_id' => $p->{'session_id'},
2710 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2712 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2717 warn "provision_pbx called\n"
2720 warn "provision_pbx calling _provision\n"
2722 _provision( 'FS::svc_pbx',
2723 [qw(id title max_extensions max_simultaneous ip_addr)],
2724 [qw(id title max_extensions max_simultaneous ip_addr)],
2730 sub provision_acct {
2732 warn "provision_acct called\n"
2735 return { 'error' => gettext('passwords_dont_match') }
2736 if $p->{'_password'} ne $p->{'_password2'};
2737 return { 'error' => gettext('empty_password') }
2738 unless length($p->{'_password'});
2740 if ($p->{'domsvc'}) {
2741 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
2742 qw ( svcpart pkgnum ) );
2743 return { 'error' => gettext('invalid_domain') }
2744 unless ($domains{$p->{'domsvc'}});
2747 warn "provision_acct calling _provision\n"
2749 _provision( 'FS::svc_acct',
2750 [qw(username _password domsvc)],
2751 [qw(username _password domsvc)],
2757 sub provision_external {
2759 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
2760 _provision( 'FS::svc_external',
2768 sub provision_forward {
2770 _provision( 'FS::svc_forward',
2771 ['srcsvc','src','dstsvc','dst'],
2778 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
2779 warn "_provision called for $class\n"
2782 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2783 return { 'error' => $session } if $context eq 'error';
2785 my $search = { 'custnum' => $custnum };
2786 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2787 my $cust_main = qsearchs('cust_main', $search )
2788 or return { 'error' => "unknown custnum $custnum" };
2790 my $pkgnum = $p->{'pkgnum'};
2792 warn "searching for custnum $custnum pkgnum $pkgnum\n"
2794 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2795 'pkgnum' => $pkgnum,
2797 or return { 'error' => "unknown pkgnum $pkgnum" };
2799 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
2801 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
2802 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
2804 return { error=> 'svcpart '. $p->{'svcpart'}. " is not a $class definition" }
2805 if $class ne 'FS::'. $part_svc->svcdb;
2807 warn "creating $class record\n"
2809 my $svc_x = $class->new( {
2810 'pkgnum' => $p->{'pkgnum'},
2811 'svcpart' => $p->{'svcpart'},
2812 map { $_ => $p->{$_} } @$fields
2815 my %insert_args = ();
2816 #i shouldn't be a special case here (pass an option or something)
2817 if ( $class eq 'FS::svc_phone'
2818 && grep length($p->{$_}), @location_editable_fields
2821 $insert_args{'cust_location'} = new FS::cust_location {
2822 map { $_ => $p->{$_} } @location_editable_fields
2826 warn "inserting $class record\n"
2828 my $error = $svc_x->insert(%insert_args);
2831 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
2833 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
2836 my $return = { 'svc' => $part_svc->svc,
2838 map { $_ => $svc_x->get($_) } @$return_fields
2840 warn "_provision returning ". Dumper($return). "\n"
2849 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2850 return { 'error' => $session } if $context eq 'error';
2852 my $search = { 'custnum' => $custnum };
2853 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2854 my $cust_main = qsearchs('cust_main', $search )
2855 or return { 'error' => "unknown custnum $custnum" };
2857 my $pkgnum = $p->{'pkgnum'};
2859 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2860 'pkgnum' => $pkgnum,
2862 or return { 'error' => "unknown pkgnum $pkgnum" };
2864 my $svcpart = $p->{'svcpart'};
2866 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
2867 'svcpart' => $svcpart, } )
2868 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
2869 my $part_svc = $pkg_svc->part_svc;
2871 my $conf = new FS::Conf;
2874 'svc' => $part_svc->svc,
2875 'svcdb' => $part_svc->svcdb,
2876 'pkgnum' => $pkgnum,
2877 'svcpart' => $svcpart,
2878 'custnum' => $custnum,
2880 'security_phrase' => 0, #XXX !
2881 'svc_acct_pop' => [], #XXX !
2883 'init_popstate' => '',
2888 small_custview( $cust_main, $conf->config('countrydefault') ),
2892 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
2893 && $ret->{'svcdb'} eq 'svc_phone') {
2894 $ret->{'svcnum'} = $p->{'svcnum'};
2895 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2896 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
2897 $ret->{'email'} = $svc_phone->email;
2898 $ret->{'forwarddst'} = $svc_phone->forwarddst;
2902 if ($ret->{'svcdb'} eq 'svc_forward') {
2903 $ret->{'forward_emails'} = {$cust_pkg->forward_emails()};
2909 sub unprovision_svc {
2912 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2913 return { 'error' => $session } if $context eq 'error';
2915 my $search = { 'custnum' => $custnum };
2916 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2917 my $cust_main = qsearchs('cust_main', $search )
2918 or return { 'error' => "unknown custnum $custnum" };
2920 my $svcnum = $p->{'svcnum'};
2922 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
2923 or return { 'error' => "unknown svcnum $svcnum" };
2925 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
2926 unless $cust_svc->cust_pkg->custnum == $custnum;
2928 my $conf = new FS::Conf;
2930 return { 'svc' => $cust_svc->part_svc->svc,
2931 'error' => $cust_svc->cancel,
2933 small_custview( $cust_main, $conf->config('countrydefault') ),
2938 sub myaccount_passwd {
2940 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2941 return { 'error' => $session } if $context eq 'error';
2943 return { 'error' => "New passwords don't match." }
2944 if $p->{'new_password'} ne $p->{'new_password2'};
2946 return { 'error' => 'Enter new password' }
2947 unless length($p->{'new_password'});
2949 #my $search = { 'custnum' => $custnum };
2950 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2951 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2952 my $search = " AND custnum = $1";
2953 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2955 my $svc_acct = qsearchs( {
2956 'table' => 'svc_acct',
2957 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2958 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
2959 'LEFT JOIN cust_main USING ( custnum ) ',
2960 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
2961 'extra_sql' => $search, #important
2963 or return { 'error' => "Service not found" };
2967 my $conf = new FS::Conf;
2969 return { 'error' => 'Incorrect current password.' }
2970 if ( exists($p->{'old_password'})
2971 || $conf->exists('selfservice-password_change_oldpass')
2973 && ! $svc_acct->check_password($p->{'old_password'});
2975 $error = 'Password too short.'
2976 if length($p->{'new_password'}) < ($conf->config('passwordmin') || 6);
2977 $error = 'Password too long.'
2978 if length($p->{'new_password'}) > ($conf->config('passwordmax') || 8);
2980 $svc_acct->set_password($p->{'new_password'});
2981 $error ||= $svc_acct->replace();
2983 #regular pw change in self-service should change contact pw too, otherwise its
2984 #way too confusing. hell its confusing they're separate at all, but alas.
2985 #need to support the "ISP provides email that's used as a contact email" case
2987 my $contact = FS::contact->by_selfservice_email($svc_acct->email);
2988 if ( $contact && qsearchs('cust_contact', { contactnum=> $contact->contactnum,
2989 custnum => $custnum,
2990 selfservice_access => 'Y',
2994 #svc_acct was successful but this one returns an error? "shouldn't happen"
2995 $error ||= $contact->change_password($p->{'new_password'});
2998 my($label, $value) = $svc_acct->cust_svc->label;
3000 return { 'error' => $error,
3010 my $info = skin_info($p);
3012 my $conf = new FS::Conf;
3013 my $verification = $conf->config('selfservice-password_reset_verification')
3014 or return { %$info, 'error' => 'Password resets disabled' };
3019 if ( $p->{'email'} ) { #new-style, changes contact and svc_acct
3021 $contact = FS::contact->by_selfservice_email($p->{'email'});
3024 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
3025 $cust_main = $cust_contact[0]->cust_main if scalar(@cust_contact) == 1;
3028 #also look for an svc_acct, otherwise it would be super confusing
3030 my($username, $domain) = split('@', $p->{'email'});
3031 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } );
3032 if ( $svc_domain ) {
3033 $svc_acct = qsearchs('svc_acct', { 'username' => $username,
3034 'domsvc' => $svc_domain->svcnum }
3037 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
3038 $cust_main ||= $cust_pkg->cust_main if $cust_pkg;
3040 #precaution: don't change svc_acct password not part of the same
3041 # customer as contact
3042 $svc_acct = '' if ! $cust_pkg
3043 || $cust_pkg->custnum != $cust_main->custnum;
3048 return { %$info, 'error' => 'Email address not found' }
3049 unless $contact || $svc_acct;
3051 } elsif ( $p->{'username'} ) { #old style, looks in svc_acct only
3053 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
3054 or return { %$info, 'error' => 'Account not found' };
3056 $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
3057 'domsvc' => $svc_domain->svcnum }
3059 or return { %$info, 'error' => 'Account not found' };
3061 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
3062 or return { %$info, 'error' => 'Account not found' };
3064 $cust_main = $cust_pkg->cust_main;
3068 return { %$info, 'error' => 'Multi-customer contacts incompatible with customer-based verification' }
3069 if ! $cust_main && $verification ne 'email';
3072 'email' => sub { 1; },
3074 my( $p, $cust_main ) = @_;
3075 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
3076 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
3079 my( $p, $cust_main ) = @_;
3080 my $cust_pay = qsearchs({
3081 'table' => 'cust_pay',
3082 'hashref' => { 'custnum' => $cust_main->custnum },
3083 'order_by' => 'ORDER BY _date DESC LIMIT 1',
3087 $p->{'amount'} == $cust_pay->paid;
3090 my( $p, $cust_main ) = @_;
3091 $p->{'zip'} eq $cust_main->zip
3092 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
3096 foreach my $verify ( split(',', $verification) ) {
3098 &{ $verify{$verify} }( $p, $cust_main )
3099 or return { %$info, 'error' => 'Account not found' };
3103 #okay, we're verified
3107 my $error = $contact->send_reset_email(
3108 'svcnum' => ($svc_acct ? $svc_acct->svcnum : ''),
3112 return { %$info, 'error' => $error }; #????
3115 } elsif ( $svc_acct ) {
3117 #create a unique session
3119 my $reset_session = {
3120 'svcnum' => $svc_acct->svcnum,
3121 'agentnum' => $svc_acct->cust_main->agentnum,
3124 my $timeout = '1 hour'; #?
3126 my $reset_session_id;
3128 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
3129 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") );
3132 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
3136 my $msgnum = $conf->config('selfservice-password_reset_msgnum',
3137 $cust_main->agentnum);
3138 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
3139 return { %$info, 'error' => "selfservice-password_reset_msgnum unset" }
3141 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
3142 my $error = $msg_template->send( 'cust_main' => $cust_main,
3143 'object' => $svc_acct,
3144 'substitutions' => {
3145 'session_id' => $reset_session_id,
3149 return { %$info, 'error' => $error }; #????
3154 return { %$info, 'error' => '' };
3157 sub check_reset_passwd {
3160 my $conf = new FS::Conf;
3161 my $verification = $conf->config('selfservice-password_reset_verification')
3162 or return { 'error' => 'Password resets disabled' };
3164 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3165 or return { 'error' => "Can't resume session" }; #better error message
3167 if ( $reset_session->{'svcnum'} ) {
3169 my $svcnum = $reset_session->{'svcnum'};
3171 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3172 or return { 'error' => "Service not found" };
3174 $p->{'agentnum'} = $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3175 my $info = skin_info($p);
3179 'session_id' => $p->{'session_id'},
3180 'username' => $svc_acct->username,
3183 } elsif ( $reset_session->{'contactnum'} ) {
3185 my $contactnum = $reset_session->{'contactnum'};
3187 my $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3188 or return { 'error' => "Contact not found" };
3190 my @contact_email = $contact->contact_email;
3191 return { 'error' => 'No contact email' } unless @contact_email;
3193 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
3194 $p->{'agentnum'} = $cust_contact[0]->cust_main->agentnum
3195 if scalar(@cust_contact) == 1;
3196 my $info = skin_info($p);
3200 'session_id' => $p->{'session_id'},
3201 'email' => $contact_email[0]->email, #the first?
3206 return { 'error' => 'No svcnum or contactnum in session' }; #??
3212 sub process_reset_passwd {
3215 my $conf = new FS::Conf;
3216 my $verification = $conf->config('selfservice-password_reset_verification')
3217 or return { 'error' => 'Password resets disabled' };
3219 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3220 or return { 'error' => "Can't resume session" }; #better error message
3225 if ( $reset_session->{'svcnum'} ) {
3227 my $svcnum = $reset_session->{'svcnum'};
3229 $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3230 or return { 'error' => "Service not found" };
3232 $p->{'agentnum'} ||= $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3233 $info ||= skin_info($p);
3238 if ( $reset_session->{'contactnum'} ) {
3240 my $contactnum = $reset_session->{'contactnum'};
3242 $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3243 or return { 'error' => "Contact not found" };
3245 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
3246 $p->{'agentnum'} = $cust_contact[0]->cust_main->agentnum
3247 if scalar(@cust_contact) == 1;
3248 $info ||= skin_info($p);
3252 return { %$info, 'error' => "New passwords don't match." }
3253 if $p->{'new_password'} ne $p->{'new_password2'};
3255 return { %$info, 'error' => 'Enter new password' }
3256 unless length($p->{'new_password'});
3260 $svc_acct->set_password($p->{'new_password'});
3261 my $error = $svc_acct->replace();
3263 return { %$info, 'error' => $error } if $error;
3265 #my($label, $value) = $svc_acct->cust_svc->label;
3266 #return { 'error' => $error,
3267 # #'label' => $label,
3268 # #'value' => $value,
3275 my $error = $contact->change_password($p->{'new_password'});
3277 return { %$info, 'error' => $error }; # if $error;
3281 #password changed ,so remove session, don't want it reused
3282 _cache->remove($p->{'session_id'});
3284 return { %$info, 'error' => '' };
3290 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3291 return { 'error' => $session } if $context eq 'error';
3294 if ( $session->{'pkgnum'} ) {
3296 #tickets for specific service with pkg-balances on
3297 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3298 'pkgnum' => $session->{'pkgnum'} })
3299 or return { 'error' => 'unknown package' };
3300 foreach my $cust_svc ( $cust_pkg->cust_svc ) {
3301 push @tickets, $cust_svc->tickets( $p->{status} );
3306 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
3307 or return { 'error' => "unknown custnum $custnum" };
3309 @tickets = $cust_main->tickets( $p->{status} );
3312 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
3313 if ( $FS::TicketSystem::system && FS::TicketSystem->selfservice_priority ) {
3314 my $conf = new FS::Conf;
3315 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
3319 ($a->{'_selfservice_priority'} eq '') <=>
3320 ($b->{'_selfservice_priority'} eq '')
3323 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
3329 +{ tickets => \@tickets };
3336 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3337 return { 'error' => $session } if $context eq 'error';
3339 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
3340 FS::TicketSystem->init();
3342 my $conf = new FS::Conf;
3343 my $queue = $p->{'queue'}
3344 || $conf->config('ticket_system-selfservice_queueid')
3345 || $conf->config('ticket_system-default_queueid');
3347 warn "$me create_ticket: creating ticket\n" if $DEBUG;
3348 my $err_or_ticket = FS::TicketSystem->create_ticket(
3349 '', #create RT session based on FS CurrentUser (fs_selfservice)
3351 'custnum' => $custnum,
3352 'svcnum' => $session->{'svcnum'},
3353 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
3356 if ( ref($err_or_ticket) ) {
3357 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
3359 return { 'error' => '',
3360 'ticket_id' => $err_or_ticket->id,
3363 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
3365 return { 'error' => $err_or_ticket };
3373 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3374 return { 'error' => $session } if $context eq 'error';
3376 return { error => 'requested format not implemented' }
3377 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
3379 my $conf = new FS::Conf;
3380 my $age_threshold = 0;
3381 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
3382 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
3384 my $search = { 'custnum' => $custnum };
3385 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3386 my $cust_main = qsearchs('cust_main', $search )
3387 or return { 'error' => "unknown custnum $custnum" };
3389 # does it make more sense to just run one sql query for this instead of all the
3390 # insanity below? would increase performance greately for large data sets?
3392 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
3393 my @part_svc = $cust_pkg->part_svc;
3394 foreach my $part_svc ( @part_svc ) {
3395 if($part_svc->svcdb eq 'svc_phone'){
3396 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
3397 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
3398 push @svc_phone, $cust_pkg_svc->svc_x
3399 if $cust_pkg_svc->date_inserted >= $age_threshold;
3407 my($xls_r,$xls_c) = (0,0);
3410 my @fields = qw( countrycode phonenum pin sip_password phone_name );
3411 if($p->{'format'} eq 'csv') {
3412 $csv = new Text::CSV_XS { 'always_quote' => 1,
3415 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
3416 $content .= $csv->string;
3418 elsif($p->{'format'} eq 'xls') {
3419 my $XLS1 = new IO::Scalar \$content;
3420 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
3421 or return { 'error' => "Error opening .xls file: $!" };
3422 $xls = $xls_workbook->add_worksheet('DIDs');
3423 foreach ( @fields ) {
3424 $xls->write(0,$xls_c++,$_);
3429 foreach my $svc_phone ( @svc_phone ) {
3430 my @cols = map { $svc_phone->$_ } @fields;
3431 if($p->{'format'} eq 'csv') {
3432 return { 'error' => 'Unable to create CSV' }
3433 unless $csv->combine(@cols);
3434 $content .= $csv->string;
3436 elsif($p->{'format'} eq 'xls') {
3439 $xls->write($xls_r,$xls_c++,$_);
3445 $xls_workbook->close() if $p->{'format'} eq 'xls';
3447 { content => $content, format => $p->{'format'}, };
3452 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3453 return { 'error' => $session } if $context eq 'error';
3455 # warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
3456 # FS::TicketSystem->init();
3457 # return { 'error' => 'get_ticket configuration error' }
3458 # if $FS::TicketSystem::system ne 'RT_Internal';
3460 # check existence and ownership as part of this
3461 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
3462 my $rt_session = FS::TicketSystem->session('');
3463 my $Ticket = FS::TicketSystem->get_ticket_object(
3465 ticket_id => $p->{'ticket_id'},
3468 return { 'error' => 'ticket not found' } if !$Ticket;
3470 if ( length( $p->{'subject'} || '' ) ) {
3472 if ( $p->{'subject'} ne $Ticket->Subject ) {
3473 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
3474 return { 'error' => "unable to set subject: $msg" } if !$val;
3478 if(length($p->{'reply'})) {
3479 my @err_or_res = FS::TicketSystem->correspond_ticket(
3481 'ticket_id' => $p->{'ticket_id'},
3482 'content' => $p->{'reply'},
3485 return { 'error' => 'unable to reply to ticket' }
3486 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
3489 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
3490 my $err_or_ticket = FS::TicketSystem->get_ticket(
3492 'ticket_id' => $p->{'ticket_id'},
3495 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
3496 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
3498 return { 'error' => $err_or_ticket };
3501 my @custs = @{$err_or_ticket->{'custs'}};
3502 my @txns = @{$err_or_ticket->{'txns'}};
3505 # superseded by check in get_ticket_object
3506 #return { 'error' => 'invalid ticket requested' }
3507 #unless grep($_ eq $custnum, @custs);
3509 foreach my $txn ( @txns ) {
3510 push @filtered_txns, $txn
3511 if ($txn->{'type'} eq 'EmailRecord'
3512 || $txn->{'type'} eq 'Correspond'
3513 || $txn->{'type'} eq 'Create');
3516 warn "$me get_ticket: successful: \n"
3518 return { 'error' => '',
3519 'transactions' => \@filtered_txns,
3520 'ticket_fields' => $err_or_ticket->{'fields'},
3521 'ticket_id' => $p->{'ticket_id'},
3525 sub adjust_ticket_priority {
3527 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3528 return { 'error' => $session } if $context eq 'error';
3530 # warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
3531 # FS::TicketSystem->init;
3532 my $ss_priority = FS::TicketSystem->selfservice_priority;
3534 return { 'error' => 'adjust_ticket_priority configuration error' }
3535 if $FS::TicketSystem::system ne 'RT_Internal'
3538 my $values = $p->{'values'}; #hashref, id => priority value
3541 foreach my $id (keys %$values) {
3542 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
3543 my $Ticket = FS::TicketSystem->get_ticket_object('',
3545 'custnum' => $custnum,
3548 $ticket_error{$id} = 'ticket not found';
3552 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
3553 # We're not going to implement it for RT_External.
3554 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
3555 my $new_value = $values->{$id};
3556 next if $old_value eq $new_value;
3558 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
3560 # AddCustomFieldValue works fine (replacing any existing value) if it's
3561 # a single-valued custom field, which it should be. If it's not, you're
3562 # doing something wrong.
3564 if ( length($new_value) ) {
3565 ($val, $msg) = $Ticket->AddCustomFieldValue(
3566 Field => $ss_priority,
3567 Value => $new_value,
3571 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
3572 Field => $ss_priority,
3573 Value => $old_value,
3577 $ticket_error{$id} = $msg if !$val;
3578 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
3580 return { 'error' => '',
3581 'ticket_error' => \%ticket_error,
3582 %{ customer_info($p) } # send updated customer info back
3588 sub _custoragent_session_custnum {
3591 my($context, $session, $custnum);
3592 if ( $p->{'session_id'} ) {
3594 $context = 'customer';
3595 $session = _cache->get($p->{'session_id'})
3596 or return ( 'error' => "Can't resume session" ); #better error message
3597 $custnum = $session->{'custnum'};
3599 } elsif ( $p->{'agent_session_id'} ) {
3602 my $agent_cache = new FS::ClientAPI_SessionCache( {
3603 'namespace' => 'FS::ClientAPI::Agent',
3605 $session = $agent_cache->get($p->{'agent_session_id'})
3606 or return ( 'error' => "Can't resume session" ); #better error message
3607 $custnum = $p->{'custnum'};
3611 return ( 'error' => "Can't resume session" ); #better error message
3614 ($context, $session, $custnum);