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);
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;
51 $me = '[FS::ClientAPI::MyAccount]';
53 use vars qw( @cust_main_editable_fields @location_editable_fields );
54 @cust_main_editable_fields = qw(
55 first last company daytime night fax mobile
57 payby payinfo payname paystart_month paystart_year payissue payip
58 ss paytype paystate stateid stateid_state
60 @location_editable_fields = qw(
61 address1 address2 city county state zip country
65 BEGIN { #preload to reduce time customer_info takes
66 if ( $FS::TicketSystem::system ) {
67 warn "$me: initializing ticket system\n" if $DEBUG;
68 FS::TicketSystem->init();
73 $cache ||= new FS::ClientAPI_SessionCache( {
74 'namespace' => 'FS::ClientAPI::MyAccount',
81 my($context, $session, $custnum) = _custoragent_session_custnum($p);
82 #return { 'error' => $session } if $context eq 'error';
85 if ( $context eq 'customer' ) {
87 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
90 $sth->execute($custnum) or die $sth->errstr;
92 $agentnum = $sth->fetchrow_arrayref->[0]
93 or die "no agentnum for custnum $custnum";
95 #} elsif ( $context eq 'agent' ) {
96 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
99 $p->{'agentnum'} = $agentnum;
101 my $conf = new FS::Conf;
103 #false laziness w/Signup.pm
105 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
107 if ( $skin_info_cache_agent ) {
109 warn "$me loading cached skin info for agentnum $agentnum\n"
114 warn "$me populating skin info cache for agentnum $agentnum\n"
117 $skin_info_cache_agent = {
118 'agentnum' => $agentnum,
119 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
120 qw( company_name date_format ) ),
121 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
122 qw( body_bgcolor box_bgcolor stripe1_bgcolor stripe2_bgcolor
123 text_color link_color vlink_color hlink_color alink_color
124 font title_color title_align title_size menu_bgcolor menu_fontsize
127 'menu_disable' => [ $conf->config('selfservice-menu_disable',$agentnum) ],
128 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
129 qw( menu_skipblanks menu_skipheadings menu_nounderline no_logo )
131 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
132 qw( title_left_image title_right_image
133 menu_top_image menu_body_image menu_bottom_image
136 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
137 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
138 qw( head body_header body_footer company_address ) ),
139 'menu' => join("\n", $conf->config("ng_selfservice-menu", $agentnum ) ) ||
142 services.php Services
143 services.php My Services
144 services_new.php Order a new service
147 personal.php Personal Information
148 password.php Change Password
151 payment_cc.php Credit Card Payment
152 payment_ach.php Electronic Check Payment
153 payment_paypal.php PayPal Payment
154 payment_webpay.php Webpay Payments
157 usage_data.php Data usage
158 usage_cdr.php Call usage
160 tickets.php Help Desk
161 tickets.php Open Tickets
162 tickets_resolved.php Resolved Tickets
163 ticket_create.php Create a new ticket
171 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
175 #{ %$skin_info_cache_agent };
176 $skin_info_cache_agent;
183 my $conf = new FS::Conf;
187 'phone_login' => $conf->exists('selfservice_server-phone_login'),
188 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
189 'banner_url' => scalar($conf->config('selfservice-login_banner_url')),
190 'banner_image_md5' =>
191 md5_hex($conf->config_binary('selfservice-login_banner_image')),
198 sub login_banner_image {
200 my $conf = new FS::Conf;
201 my $image = $conf->config_binary('selfservice-login_banner_image');
203 'md5' => md5_hex($image),
208 #false laziness w/FS::ClientAPI::passwd::passwd
212 my $conf = new FS::Conf;
216 if ( $p->{'domain'} eq 'svc_phone'
217 && $conf->exists('selfservice_server-phone_login') ) {
219 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
220 return { error => 'Number not found.' } unless $svc_phone;
223 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
224 #return { error => 'Only primary user may log in.' }
225 # if $conf->exists('selfservice_server-primary_only')
226 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
228 return { error => 'Incorrect PIN.' }
229 unless $svc_phone->check_pin($p->{'password'});
233 } elsif ( $p->{email}
234 && (my $contact = FS::contact->by_selfservice_email($p->{email}))
237 return { error => 'Incorrect contact password.' }
238 unless $contact->authenticate_password($p->{'password'});
240 $session->{'custnum'} = $contact->custnum;
244 ( $p->{username}, $p->{domain} ) = split('@', $p->{email}) if $p->{email};
246 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
247 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
249 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
250 'domsvc' => $svc_domain->svcnum, }
252 return { error => 'User not found.' } unless $svc_acct;
254 if($conf->exists('selfservice_server-login_svcpart')) {
255 my @svcpart = $conf->config('selfservice_server-login_svcpart');
256 my $svcpart = $svc_acct->cust_svc->svcpart;
257 return { error => 'Invalid user.' }
258 unless grep($_ eq $svcpart, @svcpart);
261 return { error => 'Incorrect password.' }
262 unless $svc_acct->check_password($p->{'password'});
270 $session->{'svcnum'} = $svc_x->svcnum;
272 my $cust_svc = $svc_x->cust_svc;
273 my $cust_pkg = $cust_svc->cust_pkg;
275 my $cust_main = $cust_pkg->cust_main;
276 $session->{'custnum'} = $cust_main->custnum;
277 if ( $conf->exists('pkg-balances') ) {
278 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
279 $cust_main->ncancelled_pkgs;
280 $session->{'pkgnum'} = $cust_pkg->pkgnum
281 if scalar(@cust_pkg) > 1;
285 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
286 #return { error => 'Only primary user may log in.' }
287 # if $conf->exists('selfservice_server-primary_only')
288 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
289 my $part_pkg = $cust_pkg->part_pkg;
290 return { error => 'Only primary user may log in.' }
291 if $conf->exists('selfservice_server-primary_only')
292 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
298 $session_id = sha512_hex(time(). {}. rand(). $$)
299 } until ( ! defined _cache->get($session_id) ); #just in case
301 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
302 _cache->set( $session_id, $session, $timeout );
304 return { 'error' => '',
305 'session_id' => $session_id,
311 if ( $p->{'session_id'} ) {
312 _cache->remove($p->{'session_id'});
313 return { %{ skin_info($p) }, 'error' => '' };
315 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
322 my($context, $session, $custnum) = _custoragent_session_custnum($p);
323 return { 'error' => $session } if $context eq 'error';
325 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
326 or return { 'error' => "Service not found" };
328 $session->{'svcnum'} = $svc_acct->svcnum;
330 my $conf = new FS::Conf;
331 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
332 _cache->set( $p->{'session_id'}, $session, $timeout );
334 return { 'error' => '' };
338 sub payment_gateway {
340 # takes a cust_main and a cust_payby entry, returns the payment_gateway
341 my $conf = new FS::Conf;
342 my $cust_main = shift;
343 my $cust_payby = shift;
344 my $gatewaynum = $conf->config('selfservice-payment_gateway');
346 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
347 die "configured gatewaynum $gatewaynum not found!" if !$pg;
351 return '' if ! FS::payby->realtime($cust_payby);
352 my $pg = $cust_main->agent->payment_gateway(
353 'method' => FS::payby->payby2bop($cust_payby),
363 my $conf = new FS::Conf;
365 my $info = skin_info($p);
367 use vars qw( $cust_paybys ); #cache for performance
368 unless ( $cust_paybys ) {
370 my %cust_paybys = map { $_ => 1 }
371 map { FS::payby->payby2payment($_) }
372 $conf->config('signup_server-payby');
374 $cust_paybys = [ keys %cust_paybys ];
377 $info->{'cust_paybys'} = $cust_paybys;
379 my($context, $session, $custnum) = _custoragent_session_custnum($p);
380 return { 'error' => $session } if $context eq 'error';
382 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
383 or return { 'error' => "unknown custnum $custnum" };
385 $info->{'hide_payment_fields'} = [
387 my $pg = payment_gateway($cust_main, $_);
388 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
389 } @{ $info->{cust_paybys} }
392 $info->{'self_suspend_reason'} =
393 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
395 $info->{'edit_ticket_subject'} =
396 $conf->exists('ticket_system-selfservice_edit_subject') &&
397 $cust_main->edit_subject;
399 $info->{'timeout'} = $conf->config('selfservice-timeout') || 3600;
402 'custnum' => $custnum,
403 'access_pkgnum' => $session->{'pkgnum'},
404 'access_svcnum' => $session->{'svcnum'},
411 my($context, $session, $custnum) = _custoragent_session_custnum($p);
412 return { 'error' => $session } if $context eq 'error';
416 my $conf = new FS::Conf;
417 $return{'require_address2'} = $conf->exists('cust_main-require_address2');
419 # if ( $FS::TicketSystem::system ) {
420 # warn "$me customer_info: initializing ticket system\n" if $DEBUG;
421 # FS::TicketSystem->init();
424 if ( $custnum ) { #customer record
426 %return = ( %return, %{ customer_info_short($p) } );
428 #redundant with customer_info_short, but we need it for several things below
429 my $search = { 'custnum' => $custnum };
430 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
431 my $cust_main = qsearchs('cust_main', $search )
432 or return { 'error' => "unknown custnum $custnum" };
434 my $list_tickets = list_tickets($p);
435 $return{'tickets'} = $list_tickets->{'tickets'};
437 if ( $session->{'pkgnum'} ) {
438 #XXX open invoices in the pkg-balances case
442 invnum => $_->invnum,
443 date => time2str("%b %o, %Y", $_->_date),
446 } $cust_main->open_cust_bill;
447 $return{open_invoices} = \@open;
449 my $sql = 'SELECT MAX(_date) FROM cust_bill WHERE custnum = ?';
450 my $sth = dbh->prepare($sql) or die dbh->errstr;
451 $sth->execute($custnum) or die $sth->errstr;
452 $return{'last_invoice_date'} = $sth->fetchrow_arrayref->[0];
453 $return{'last_invoice_date_pretty'} =
454 time2str('%m/%d/%Y', $return{'last_invoice_date'} );
457 #customer_info_short always has nobalance on..
458 $return{small_custview} =
459 small_custview( $cust_main,
460 $return{countrydefault},
461 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
464 $return{has_ship_address} = $cust_main->has_ship_address;
465 $return{status} = $cust_main->status;
466 $return{statuscolor} = $cust_main->statuscolor;
468 # compatibility: some places in selfservice use this to determine
469 # if there's a ship address
470 if ( $return{has_ship_address} ) {
471 $return{ship_last} = $cust_main->last;
472 $return{ship_first} = $cust_main->first;
475 if (scalar($conf->config('support_packages'))) {
476 my @support_services = ();
477 foreach ($cust_main->support_services) {
478 my $seconds = $_->svc_x->seconds || 0;
479 my $time_remaining = (($seconds < 0) ? '-' : '' ).
480 int(abs($seconds)/3600)."h".
481 sprintf("%02d",(abs($seconds)%3600)/60)."m";
482 my $cust_pkg = $_->cust_pkg;
485 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
486 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
487 push @support_services, { svcnum => $_->svcnum,
488 time => $time_remaining,
493 $return{support_services} = \@support_services;
496 if ( $conf->config('prepayment_discounts-credit_type') ) {
498 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
501 } elsif ( $session->{'svcnum'} ) { #no customer record
503 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
504 or die "unknown svcnum";
505 $return{name} = $svc_acct->email;
509 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
513 return { 'error' => '',
514 'custnum' => $custnum,
520 sub customer_info_short {
523 my($context, $session, $custnum) = _custoragent_session_custnum($p);
524 return { 'error' => $session } if $context eq 'error';
528 my $conf = new FS::Conf;
530 if ( $custnum ) { #customer record
532 my $search = { 'custnum' => $custnum };
533 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
534 my $cust_main = qsearchs('cust_main', $search )
535 or return { 'error' => "unknown custnum $custnum" };
537 $return{display_custnum} = $cust_main->display_custnum;
539 if ( $session->{'pkgnum'} ) {
540 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
541 #next_bill_date from cust_pkg?
543 $return{balance} = $cust_main->balance;
544 $return{next_bill_date} = $cust_main->next_bill_date;
545 $return{next_bill_date_pretty} =
546 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
550 $return{countrydefault} = scalar($conf->config('countrydefault'));
552 $return{small_custview} =
553 small_custview( $cust_main,
554 $return{countrydefault},
558 warn $return{first} = $cust_main->first;
559 warn $return{'last'} = $cust_main->get('last');
560 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
562 $return{payby} = $cust_main->payby;
564 #none of these are terribly expensive if we want 'em...
565 for (@cust_main_editable_fields) {
566 $return{$_} = $cust_main->get($_);
568 #maybe a little more expensive, but it should be cached by now
569 for (@location_editable_fields) {
570 $return{$_} = $cust_main->bill_location->get($_)
571 if $cust_main->bill_locationnum;
572 $return{'ship_'.$_} = $cust_main->ship_location->get($_)
573 if $cust_main->ship_locationnum;
576 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
577 $return{payinfo} = $cust_main->paymask;
578 @return{'month', 'year'} = $cust_main->paydate_monthyear;
581 my @invoicing_list = $cust_main->invoicing_list;
582 $return{'invoicing_list'} =
583 join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list );
584 $return{'postal_invoicing'} =
585 0 < ( grep { $_ eq 'POST' } @invoicing_list );
587 if ( $session->{'svcnum'} ) {
588 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
589 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
590 $return{'svcnum'} = $session->{'svcnum'};
593 } elsif ( $session->{'svcnum'} ) { #no customer record
595 #uuh, not supproted yet... die?
596 return { 'error' => 'customer_info_short not yet supported as agent' };
600 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
604 return { 'error' => '',
605 'custnum' => $custnum,
610 sub billing_history {
613 my($context, $session, $custnum) = _custoragent_session_custnum($p);
614 return { 'error' => $session } if $context eq 'error';
616 return { 'error' => 'No customer' } unless $custnum;
618 my $search = { 'custnum' => $custnum };
619 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
620 my $cust_main = qsearchs('cust_main', $search )
621 or return { 'error' => "unknown custnum $custnum" };
625 if ( $session->{'pkgnum'} ) {
626 #$return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
627 #next_bill_date from cust_pkg?
628 return { 'error' => 'No history for package' };
631 $return{balance} = $cust_main->balance;
632 $return{next_bill_date} = $cust_main->next_bill_date;
633 $return{next_bill_date_pretty} =
634 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
639 my $conf = new FS::Conf;
641 if ( $conf->exists('selfservice-billing_history-line_items') ) {
643 foreach my $cust_bill ( $cust_main->cust_bill ) {
646 'type' => 'Line item',
647 'description' => $_->desc( $cust_main->locale ).
648 ( $_->sdate && $_->edate
649 ? ' '. time2str('%d-%b-%Y', $_->sdate).
650 ' To '. time2str('%d-%b-%Y', $_->edate)
653 'amount' => sprintf('%.2f', $_->setup + $_->recur ),
654 'date' => $cust_bill->_date,
655 'date_pretty' => time2str('%m/%d/%Y', $cust_bill->_date ),
657 foreach $cust_bill->cust_bill_pkg;
665 'description' => 'Invoice #'. $_->display_invnum,
666 'amount' => sprintf('%.2f', $_->charged ),
668 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
670 foreach $cust_main->cust_bill;
676 'description' => 'Payment', #XXX type
677 'amount' => sprintf('%.2f', 0 - $_->paid ),
679 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
681 foreach $cust_main->cust_pay;
685 'description' => 'Credit', #more info?
686 'amount' => sprintf('%.2f', 0 -$_->amount ),
688 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
690 foreach $cust_main->cust_credit;
694 'description' => 'Refund', #more info? type, like payment?
695 'amount' => $_->refund,
697 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
699 foreach $cust_main->cust_refund;
701 @history = sort { $b->{'date'} <=> $a->{'date'} } @history;
703 $return{'history'} = \@history;
711 my $session = _cache->get($p->{'session_id'})
712 or return { 'error' => "Can't resume session" }; #better error message
714 my $custnum = $session->{'custnum'}
715 or return { 'error' => "no customer record" };
717 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
718 or return { 'error' => "unknown custnum $custnum" };
720 my $new = new FS::cust_main { $cust_main->hash };
722 $new->set( $_ => $p->{$_} )
723 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
725 if ( exists($p->{address1}) ) {
726 my $bill_location = FS::cust_location->new({
727 map { $_ => $p->{$_} } @location_editable_fields
729 # if this is unchanged from before, cust_main::replace will ignore it
730 $new->set('bill_location' => $bill_location);
733 if ( exists($p->{ship_address1}) ) {
734 my $ship_location = FS::cust_location->new({
735 map { $_ => $p->{"ship_$_"} } @location_editable_fields
737 if ( !grep { length($p->{"ship_$_"}) } @location_editable_fields ) {
738 # Selfservice unfortunately tries to indicate "same as billing
739 # address" by sending all fields empty. Did this ever work?
740 $ship_location = $cust_main->bill_location;
742 $new->set('ship_location' => $ship_location);
744 # but if it hasn't been passed in at all, leave ship_location alone--
745 # DON'T change it to match bill_location.
748 if (exists($p->{'payby'})) {
749 $p->{'payby'} =~ /^([A-Z]{4})$/
750 or return { 'error' => "illegal_payby " . $p->{'payby'} };
754 if ( $payby =~ /^(CARD|DCRD)$/ ) {
756 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
758 if ( $new->payinfo eq $cust_main->paymask ) {
759 $new->payinfo($cust_main->payinfo);
761 $new->payinfo($p->{'payinfo'});
764 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
766 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
769 $p->{'payinfo1'} =~ /^([\dx]+)$/
770 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
772 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
773 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
775 $payinfo = $payinfo1. '@'. $payinfo2;
777 $new->payinfo( ($payinfo eq $cust_main->paymask)
778 ? $cust_main->payinfo
782 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
784 } elsif ( $payby =~ /^(BILL)$/ ) {
786 } elsif ( $payby ) { #notyet ready
787 return { 'error' => "unknown payby $payby" };
791 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
792 #false laziness with httemplate/edit/process/cust_main.cgi
793 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
794 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
796 @invoicing_list = $cust_main->invoicing_list;
799 my $error = $new->replace($cust_main, \@invoicing_list);
800 return { 'error' => $error } if $error;
803 return { 'error' => '' };
808 my $session = _cache->get($p->{'session_id'})
809 or return { 'error' => "Can't resume session" }; #better error message
815 my $conf = new FS::Conf;
816 use vars qw($payment_info); #cache for performance
817 unless ( $payment_info ) {
819 my %states = map { $_->state => 1 }
820 qsearch('cust_main_county', {
821 'country' => $conf->config('countrydefault') || 'US'
824 my %cust_paybys = map { $_ => 1 }
825 map { FS::payby->payby2payment($_) }
826 $conf->config('signup_server-payby');
828 my @cust_paybys = keys %cust_paybys;
832 #list all counties/states/countries
833 'cust_main_county' =>
834 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
836 #shortcut for one-country folks
838 [ sort { $a cmp $b } keys %states ],
840 'card_types' => card_types(),
842 'paytypes' => [ @FS::cust_main::paytypes ],
844 'paybys' => [ $conf->config('signup_server-payby') ],
845 'cust_paybys' => \@cust_paybys,
847 'stateid_label' => FS::Msgcat::_gettext('stateid'),
848 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
850 'show_ss' => $conf->exists('show_ss'),
851 'show_stateid' => $conf->exists('show_stateid'),
852 'show_paystate' => $conf->exists('show_bankstate'),
854 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
856 'credit_card_surcharge_percentage' => scalar($conf->config('credit-card-surcharge-percentage')),
865 my %return = %$payment_info;
867 my $custnum = $session->{'custnum'};
869 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
870 or return { 'error' => "unknown custnum $custnum" };
872 $return{'hide_payment_fields'} = [
874 my $pg = payment_gateway($cust_main, $_);
875 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
876 } @{ $return{cust_paybys} }
879 $return{balance} = $cust_main->balance; #XXX pkg-balances?
881 $return{payname} = $cust_main->payname
882 || ( $cust_main->first. ' '. $cust_main->get('last') );
884 $return{$_} = $cust_main->bill_location->get($_)
885 for qw(address1 address2 city state zip);
887 $return{payby} = $cust_main->payby;
888 $return{stateid_state} = $cust_main->stateid_state;
890 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
891 $return{card_type} = cardtype($cust_main->payinfo);
892 $return{payinfo} = $cust_main->paymask;
894 @return{'month', 'year'} = $cust_main->paydate_monthyear;
898 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
899 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
900 $return{payinfo1} = $payinfo1;
901 $return{payinfo2} = $payinfo2;
902 $return{paytype} = $cust_main->paytype;
903 $return{paystate} = $cust_main->paystate;
904 $return{payname} = $cust_main->payname; # override 'first/last name' default from above, if any. Is instution-name here. (#15819)
907 if ( $conf->config('prepayment_discounts-credit_type') ) {
909 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
912 #doubleclick protection
914 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
916 return { 'error' => '',
922 #some false laziness with httemplate/process/payment.cgi - look there for
923 #ACH and CVV support stuff
925 sub validate_payment {
928 my $session = _cache->get($p->{'session_id'})
929 or return { 'error' => "Can't resume session" }; #better error message
931 my $custnum = $session->{'custnum'};
933 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
934 or return { 'error' => "unknown custnum $custnum" };
936 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
937 or return { 'error' => gettext('illegal_amount') };
939 return { error => 'Amount must be greater than 0' } unless $amount > 0;
941 #false laziness w/tr-amount_fee.html, but we don't want selfservice users
942 #changing the hidden form values
943 my $conf = new FS::Conf;
944 my $fee_display = $conf->config('selfservice_process-display') || 'add';
945 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
946 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
947 if ( $fee_display eq 'add'
949 and ! $fee_skip_first || scalar($cust_main->cust_pay)
952 my $fee_pkg = qsearchs('part_pkg', { pkgpart=>$fee_pkgpart } );
953 $amount = sprintf('%.2f', $amount + $fee_pkg->option('setup_fee') );
956 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
957 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
958 my $discount_term = $1;
960 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
961 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
964 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
965 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
968 $p->{'payby'} ||= 'CARD';
969 $p->{'payby'} =~ /^([A-Z]{4})$/
970 or return { 'error' => "illegal_payby " . $p->{'payby'} };
973 #false laziness w/process/payment.cgi
976 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
978 $p->{'payinfo1'} =~ /^([\dx]+)$/
979 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
981 $p->{'payinfo2'} =~ /^([\dx]+)$/
982 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
984 $payinfo = $payinfo1. '@'. $payinfo2;
986 $payinfo = $cust_main->payinfo
987 if $cust_main->paymask eq $payinfo;
989 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
991 $payinfo = $p->{'payinfo'};
993 #more intelligent matching will be needed here if you change
994 #card_masking_method and don't remove existing paymasks
995 $payinfo = $cust_main->payinfo
996 if $cust_main->paymask eq $payinfo;
999 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
1000 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1004 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1005 return { 'error' => gettext('unknown_card_type') }
1006 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
1008 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
1009 if ( cardtype($payinfo) eq 'American Express card' ) {
1010 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
1011 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
1014 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
1015 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
1021 die "unknown payby $payby";
1024 my %payby2fields = (
1025 'CARD' => [ qw( paystart_month paystart_year payissue payip
1026 address1 address2 city state zip country ) ],
1027 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
1031 $card_type = cardtype($payinfo) if $payby eq 'CARD';
1034 'cust_main' => $cust_main, #XXX or just custnum??
1035 'amount' => sprintf('%.2f', $amount),
1037 'payinfo' => $payinfo,
1038 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
1039 'card_type' => $card_type,
1040 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
1041 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
1042 'month' => $p->{'month'},
1043 'year' => $p->{'year'},
1044 'payname' => $payname,
1045 'paybatch' => $paybatch, #this doesn't actually do anything
1046 'paycvv' => $paycvv,
1047 'payname' => $payname,
1048 'discount_term' => $discount_term,
1049 'pkgnum' => $session->{'pkgnum'},
1050 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
1060 my $validate = validate_payment($p);
1061 return $validate if $validate->{'error'};
1063 my $conf = new FS::Conf;
1064 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
1065 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
1067 +{ map { $_=>$validate->{$_} }
1068 qw( card_type paymask payname paydate_pretty month year amount
1069 address1 address2 city state zip country
1075 sub process_stored_payment {
1078 my $session_id = $p->{'session_id'};
1080 my $payment_info = _cache->get( "payment_$session_id" )
1081 or return { 'error' => "Can't resume session" }; #better error message
1083 do_process_payment($payment_info);
1087 sub process_payment {
1090 my $payment_info = validate_payment($p);
1091 return $payment_info if $payment_info->{'error'};
1093 do_process_payment($payment_info);
1097 sub do_process_payment {
1098 my $validate = shift;
1100 my $cust_main = $validate->{'cust_main'};
1102 my $amount = delete $validate->{'amount'};
1105 my $payby = delete $validate->{'payby'};
1107 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
1110 'paynum_ref' => \$paynum,
1113 return { 'error' => $error } if $error;
1115 #no error, so order the fee package if applicable...
1116 my $conf = new FS::Conf;
1117 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
1118 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
1120 if ( $fee_pkgpart and ! $fee_skip_first || scalar($cust_main->cust_pay) ) {
1122 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $fee_pkgpart };
1124 $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
1125 return { 'error' => "payment processed successfully, but error ordering fee: $error" }
1128 #and generate an invoice for it now too
1129 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1130 return { 'error' => "payment processed and fee ordered sucessfully, but error billing fee: $error" }
1135 $cust_main->apply_payments;
1137 if ( $validate->{'save'} ) {
1138 my $new = new FS::cust_main { $cust_main->hash };
1139 if ($payby eq 'CARD' || $payby eq 'DCRD') {
1140 $new->set( $_ => $validate->{$_} )
1141 foreach qw( payname paystart_month paystart_year payissue payip );
1142 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
1144 my $bill_location = FS::cust_location->new({
1145 map { $_ => $validate->{$_} }
1146 qw(address1 address2 city state country zip)
1148 $new->set('bill_location' => $bill_location);
1149 # but don't allow the service address to change this way.
1151 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
1152 $new->set( $_ => $validate->{$_} )
1153 foreach qw( payname payip paytype paystate
1154 stateid stateid_state );
1155 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
1157 $new->set( 'payinfo' => $cust_main->card_token || $validate->{'payinfo'} );
1158 $new->set( 'paydate' => $validate->{'paydate'} );
1159 my $error = $new->replace($cust_main);
1161 #no, this causes customers to process their payments again
1162 #return { 'error' => $error };
1163 #XXX just warn verosely for now so i can figure out how these happen in
1164 # the first place, eventually should redirect them to the "change
1165 #address" page but indicate the payment did process??
1166 delete($validate->{'payinfo'}); #don't want to log this!
1167 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
1168 "NEW: ". Dumper($new)."\n".
1169 "OLD: ". Dumper($cust_main)."\n".
1170 "PACKET: ". Dumper($validate)."\n";
1178 my $receipt_html = '';
1180 # currently supported for realtime CC only; send receipt data to SS
1181 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
1184 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
1187 <TD ALIGN="right">Payment#</TD>
1188 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
1192 <TD ALIGN="right">Date</TD>
1194 <TD BGCOLOR="#FFFFFF"><B>! .
1195 time2str("%a %b %o, %Y %r", $cust_pay->_date)
1201 <TD ALIGN="right">Amount</TD>
1202 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
1207 <TD ALIGN="right">Payment method</TD>
1208 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
1219 my($gw, $auth, $order) = split(':', $cust_pay->paybatch);
1223 'amount' => sprintf('%.2f', $cust_pay->paid),
1224 'date' => $cust_pay->_date,
1225 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
1226 'time_pretty' => time2str('%T', $cust_pay->_date),
1227 'auth_num' => $auth,
1228 'order_num' => $order,
1229 'receipt_html' => $receipt_html,
1236 'receipt_html' => '',
1243 sub realtime_collect {
1246 my $session = _cache->get($p->{'session_id'})
1247 or return { 'error' => "Can't resume session" }; #better error message
1249 my $custnum = $session->{'custnum'};
1251 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1252 or return { 'error' => "unknown custnum $custnum" };
1255 if ( $p->{'amount'} ) {
1256 $amount = $p->{'amount'};
1258 elsif ( $session->{'pkgnum'} ) {
1259 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1262 $amount = $cust_main->balance;
1265 my $error = $cust_main->realtime_collect(
1266 'method' => $p->{'method'},
1267 'amount' => $amount,
1268 'pkgnum' => $session->{'pkgnum'},
1269 'session_id' => $p->{'session_id'},
1273 return { 'error' => $error } unless ref( $error );
1275 return { 'error' => '', amount => $amount, %$error };
1278 sub start_thirdparty {
1280 my $session = _cache->get($p->{'session_id'})
1281 or return { 'error' => "Can't resume session" }; #better error message
1282 my $custnum = $session->{'custnum'};
1283 my $cust_main = FS::cust_main->by_key($custnum);
1285 my $amount = $p->{'amount'}
1286 or return { error => 'no amount' };
1288 my $result = $cust_main->create_payment(
1289 'method' => $p->{'method'},
1290 'amount' => $p->{'amount'},
1291 'pkgnum' => $session->{'pkgnum'},
1292 'session_id' => $p->{'session_id'},
1295 if ( ref($result) ) { # hashref or error
1298 return { error => $result };
1302 sub finish_thirdparty {
1304 my $session_id = delete $p->{'session_id'};
1305 my $session = _cache->get($session_id)
1306 or return { 'error' => "Can't resume session" };
1307 my $custnum = $session->{'custnum'};
1308 my $cust_main = FS::cust_main->by_key($custnum);
1310 if ( $p->{_cancel} ) {
1311 # customer backed out of making a payment
1312 return $cust_main->cancel_payment( $session_id );
1314 my $result = $cust_main->execute_payment( $session_id, %$p );
1315 if ( ref($result) ) {
1318 return { error => $result };
1322 sub process_payment_order_pkg {
1325 my $hr = process_payment($p);
1326 return $hr if $hr->{'error'};
1331 sub process_payment_order_renew {
1334 my $hr = process_payment($p);
1335 return $hr if $hr->{'error'};
1340 sub process_prepay {
1344 my $session = _cache->get($p->{'session_id'})
1345 or return { 'error' => "Can't resume session" }; #better error message
1349 my $custnum = $session->{'custnum'};
1351 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1352 or return { 'error' => "unknown custnum $custnum" };
1354 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1355 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1363 return { 'error' => $error } if $error;
1365 return { 'error' => '',
1366 'amount' => $amount,
1367 'seconds' => $seconds,
1368 'duration' => duration_exact($seconds),
1369 'upbytes' => $upbytes,
1370 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1371 'downbytes' => $downbytes,
1372 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1373 'totalbytes'=> $totalbytes,
1374 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1381 my $session = _cache->get($p->{'session_id'})
1382 or return { 'error' => "Can't resume session" }; #better error message
1384 my $custnum = $session->{'custnum'};
1386 my $invnum = $p->{'invnum'};
1388 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1389 'custnum' => $custnum } )
1390 or return { 'error' => "Can't find invnum" };
1394 return { 'error' => '',
1395 'invnum' => $invnum,
1396 'invoice_text' => join('', $cust_bill->print_text ),
1397 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1404 my $session = _cache->get($p->{'session_id'})
1405 or return { 'error' => "Can't resume session" }; #better error message
1407 my $custnum = $session->{'custnum'};
1409 my $invnum = $p->{'invnum'};
1411 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1412 'custnum' => $custnum } )
1413 or return { 'error' => "Can't find invnum" };
1417 return { 'error' => '',
1418 'invnum' => $invnum,
1419 'invoice_pdf' => $cust_bill->print_pdf({
1420 'unsquelch_cdr' => 1,
1421 'locale' => $p->{'locale'},
1427 sub legacy_invoice {
1429 my $session = _cache->get($p->{'session_id'})
1430 or return { 'error' => "Can't resume session" }; #better error message
1432 my $custnum = $session->{'custnum'};
1434 my $legacyinvnum = $p->{'legacyinvnum'};
1437 'legacyinvnum' => $legacyinvnum,
1438 'custnum' => $custnum,
1441 my $legacy_cust_bill =
1442 qsearchs('legacy_cust_bill', { %hash, 'locale' => $p->{'locale'} } )
1443 || qsearchs('legacy_cust_bill', \%hash )
1444 or return { 'error' => "Can't find legacyinvnum" };
1448 return { 'error' => '',
1449 'legacyinvnum' => $legacyinvnum,
1450 'legacyid' => $legacy_cust_bill->legacyid,
1451 'invoice_html' => $legacy_cust_bill->content_html,
1456 sub legacy_invoice_pdf {
1458 my $session = _cache->get($p->{'session_id'})
1459 or return { 'error' => "Can't resume session" }; #better error message
1461 my $custnum = $session->{'custnum'};
1463 my $legacyinvnum = $p->{'legacyinvnum'};
1465 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1466 'legacyinvnum' => $legacyinvnum,
1467 'custnum' => $custnum,
1468 }) or return { 'error' => "Can't find legacyinvnum" };
1472 return { 'error' => '',
1473 'legacyinvnum' => $legacyinvnum,
1474 'legacyid' => $legacy_cust_bill->legacyid,
1475 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1483 #sessioning for this? how do we get the session id to the backend invoice
1484 # template so it can add it to the link, blah
1487 if ( $p->{'invnum'} ) {
1488 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1489 or return { 'error' => 'unknown invnum' };
1490 $agentnum = $cust_bill->cust_main->agentnum;
1493 my $templatename = $p->{'template'} || $p->{'templatename'};
1495 #false laziness-ish w/view/cust_bill-logo.cgi
1497 my $conf = new FS::Conf;
1498 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1499 $templatename = "_$1";
1504 my $filename = "logo$templatename.png";
1506 return { 'error' => '',
1507 'logo' => $conf->config_binary($filename, $agentnum),
1508 'content_type' => 'image/png', #should allow gif, jpg too
1515 my $session = _cache->get($p->{'session_id'})
1516 or return { 'error' => "Can't resume session" }; #better error message
1518 my $custnum = $session->{'custnum'};
1520 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1521 or return { 'error' => "unknown custnum $custnum" };
1523 my $conf = new FS::Conf;
1525 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1527 my @cust_bill = grep ! $_->hide, $cust_main->cust_bill;
1531 return { 'error' => '',
1532 'balance' => $cust_main->balance,
1535 my $owed = $_->owed;
1537 +{ 'invnum' => $_->invnum,
1538 '_date' => $_->_date,
1539 'date' => time2str("%b %o, %Y", $_->_date),
1540 'date_short' => time2str("%m-%d-%Y", $_->_date),
1541 'previous' => sprintf('%.2f', ($_->previous)[0]),
1542 'charged' => sprintf('%.2f', $_->charged),
1543 'owed' => sprintf('%.2f', $owed),
1544 'balance' => sprintf('%.2f', $balance),
1549 'legacy_invoices' => [
1551 +{ 'legacyinvnum' => $_->legacyinvnum,
1552 'legacyid' => $_->legacyid,
1553 '_date' => $_->_date,
1554 'date' => time2str("%b %o, %Y", $_->_date),
1555 'date_short' => time2str("%m-%d-%Y", $_->_date),
1556 'charged' => sprintf('%.2f', $_->charged),
1557 'has_content' => ( length($_->content_pdf)
1558 || length($_->content_html) ),
1568 my $session = _cache->get($p->{'session_id'})
1569 or return { 'error' => "Can't resume session" }; #better error message
1571 my $custnum = $session->{'custnum'};
1573 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1574 or return { 'error' => "unknown custnum $custnum" };
1576 my @errors = $cust_main->cancel( 'quiet'=>1 );
1578 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1580 return { 'error' => $error };
1587 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1588 return { 'error' => $session } if $context eq 'error';
1590 my $search = { 'custnum' => $custnum };
1591 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1592 my $cust_main = qsearchs('cust_main', $search )
1593 or return { 'error' => "unknown custnum $custnum" };
1595 my $conf = new FS::Conf;
1597 # the duplication below is necessary:
1598 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1599 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1600 # non-backwards-compatible change breaking the software of anyone using the API
1601 # instead of the stock selfservice
1602 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1603 if( $conf->exists('selfservice_server-view-wholesale') ) {
1604 return { 'svcnum' => $session->{'svcnum'},
1605 'custnum' => $custnum,
1606 'cust_pkg' => [ map {
1608 part_pkg => [ map $_->hashref, $_->part_pkg ],
1610 [ map $_->hashref, $_->available_part_svc ],
1612 [ map { my $ref = { $_->hash,
1613 label => [ $_->label ],
1615 $ref->{_password} = $_->svc_x->_password
1616 if $context eq 'agent'
1617 && $conf->exists('agent-showpasswords')
1618 && $_->part_svc->svcdb eq 'svc_acct';
1623 } $cust_main->cust_pkg
1626 small_custview( $cust_main, $conf->config('countrydefault') ),
1627 'wholesale_view' => 1,
1628 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1629 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1630 'lnp' => $conf->exists('svc_phone-lnp'),
1634 { 'svcnum' => $session->{'svcnum'},
1635 'custnum' => $custnum,
1636 'cust_pkg' => [ map {
1637 my $primary_cust_svc = $_->primary_cust_svc;
1640 pkg_label => $_->pkg_locale,
1641 status => $_->status,
1642 statuscolor => $_->statuscolor,
1644 [ map { $_->hashref }
1645 grep { $_->selfservice_access ne 'hidden' }
1646 $_->available_part_svc
1649 [ map { my $ref = { $_->hash,
1650 label => [ $_->label ],
1652 $ref->{_password} = $_->svc_x->_password
1653 if $context eq 'agent'
1654 && $conf->exists('agent-showpasswords')
1655 && $_->part_svc->svcdb eq 'svc_acct';
1656 $ref->{svchash} = { $_->svc_x->hash } if
1657 $_->part_svc->svcdb eq 'svc_phone';
1658 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1659 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1662 grep { $_->part_svc->selfservice_access ne 'hidden' }
1667 ? { $primary_cust_svc->hash,
1668 label => [ $primary_cust_svc->label ],
1669 finger => $primary_cust_svc->svc_x->finger, #uuh
1670 $primary_cust_svc->part_svc->hash,
1674 } $cust_main->ncancelled_pkgs
1677 small_custview( $cust_main, $conf->config('countrydefault') ),
1678 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1686 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1687 return { 'error' => $session } if $context eq 'error';
1689 my $search = { 'custnum' => $custnum };
1690 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1691 my $cust_main = qsearchs('cust_main', $search )
1692 or return { 'error' => "unknown custnum $custnum" };
1694 my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
1695 if ( ! $pkgnum && $p->{'svcnum'} ) {
1696 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
1697 $pkgnum = $cust_svc->pkgnum if $cust_svc;
1701 my @cust_pkg_usage = ();
1702 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1703 foreach my $cust_pkg ( $p->{'ncancelled'}
1704 ? $cust_main->ncancelled_pkgs
1705 : $cust_main->unsuspended_pkgs ) {
1706 next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
1707 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1708 push @cust_pkg_usage, $cust_pkg->cust_pkg_usage;
1711 @cust_svc = grep { $_->part_svc->selfservice_access ne 'hidden' } @cust_svc;
1713 foreach (@cust_pkg_usage) {
1714 my $part = $_->part_pkg_usage;
1715 my $tag = $part->description . ($part->shared ? 1 : 0);
1716 my $row = $usage_pools{$tag}
1717 ||= [ $part->description, 0, 0, $part->shared ? 1 : 0 ];
1718 $row->[1] += $_->minutes; # minutes remaining
1719 $row->[2] += $part->minutes; # minutes total
1722 if ( $p->{'svcdb'} ) {
1723 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1725 : ref($p->{'svcdb'}) eq 'ARRAY'
1726 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1727 : { $p->{'svcdb'} => 1 };
1728 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1731 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1734 my $conf = new FS::Conf;
1737 'svcnum' => $session->{'svcnum'},
1738 'custnum' => $custnum,
1739 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1740 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1743 my $svc_x = $_->svc_x;
1744 my($label, $value) = $_->label;
1745 my $part_svc = $_->part_svc;
1746 my $svcdb = $part_svc->svcdb;
1747 my $cust_pkg = $_->cust_pkg;
1748 my $part_pkg = $cust_pkg->part_pkg;
1751 'svcnum' => $_->svcnum,
1752 'display_svcnum' => $_->display_svcnum,
1756 'pkg_label' => $cust_pkg->pkg_locale,
1757 'pkg_status' => $cust_pkg->status,
1758 'readonly' => ($part_svc->selfservice_access eq 'readonly'),
1761 if ( $svcdb eq 'svc_acct' ) {
1764 'username' => $svc_x->username,
1765 'email' => $svc_x->email,
1766 'finger' => $svc_x->finger,
1767 'seconds' => $svc_x->seconds,
1768 'upbytes' => display_bytecount($svc_x->upbytes),
1769 'downbytes' => display_bytecount($svc_x->downbytes),
1770 'totalbytes' => display_bytecount($svc_x->totalbytes),
1772 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1773 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1774 'recharge_upbytes' =>
1775 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1776 'recharge_downbytes' =>
1777 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1778 'recharge_totalbytes' =>
1779 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1783 } elsif ( $svcdb eq 'svc_dsl' ) {
1784 $hash{'phonenum'} = $svc_x->phonenum;
1785 if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
1786 $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
1787 $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
1790 $hash{'name'} = $cust_main->name;
1792 } elsif ( $svcdb eq 'svc_phone' ) {
1793 # could potentially show lots of things...
1794 $hash{'outbound'} = 1;
1795 $hash{'inbound'} = 0;
1796 if ( $part_pkg->plan eq 'voip_inbound' ) {
1797 $hash{'outbound'} = 0;
1798 $hash{'inbound'} = 1;
1799 } elsif ( $part_pkg->option('selfservice_inbound_format')
1800 or $conf->config('selfservice-default_inbound_cdr_format')
1802 $hash{'inbound'} = 1;
1804 foreach (qw(inbound outbound)) {
1805 # hmm...we can't filter by status here, because there might
1806 # not be cdr_terminations at all. have to go by date.
1807 # find all since the last bill date.
1808 # XXX cdr types? we are going to need them.
1810 my $sum_cdr = $svc_x->sum_cdrs(
1811 'inbound' => ( $_ eq 'inbound' ? 1 : 0 ),
1812 'begin' => ($cust_pkg->last_bill || 0),
1815 $hash{$_} = $sum_cdr->hashref;
1820 # elsif ( $svcdb eq 'svc_phone' || $svcdb eq 'svc_port' ) {
1831 map { $usage_pools{$_} }
1839 sub _customer_svc_x {
1840 my($custnum, $svcnum, $table) = (shift, shift, shift);
1841 my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
1843 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1844 my $search = " AND custnum = $1";
1845 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1848 'table' => ($table || 'svc_acct'),
1849 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1850 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
1851 #'LEFT JOIN cust_main USING ( custnum ) ',
1852 'hashref' => $hashref,
1853 'extra_sql' => $search, #important
1858 sub svc_status_html {
1861 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1862 return { 'error' => $session } if $context eq 'error';
1864 #XXX only svc_dsl for now
1865 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
1866 or return { 'error' => "Service not found" };
1868 my $html = $svc_x->getstatus_html;
1870 return { 'html' => $html };
1874 sub svc_status_hash {
1877 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1878 return { 'error' => $session } if $context eq 'error';
1880 #XXX only svc_acct for now
1881 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1882 or return { 'error' => "Service not found" };
1884 my ( $html, $hashref ) = $svc_x->export_getstatus;
1889 sub set_svc_status_hash { _svc_method_X(shift, 'export_setstatus') }
1890 sub set_svc_status_listadd { _svc_method_X(shift, 'export_setstatus_listadd') }
1891 sub set_svc_status_listdel { _svc_method_X(shift, 'export_setstatus_listdel') }
1892 sub set_svc_status_vacationadd { _svc_method_X(shift, 'export_setstatus_vacationadd') }
1893 sub set_svc_status_vacationdel { _svc_method_X(shift, 'export_setstatus_vacationdel') }
1896 my( $p, $method ) = @_;
1898 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1899 return { 'error' => $session } if $context eq 'error';
1901 #XXX only svc_acct for now
1902 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1903 or return { 'error' => "Service not found" };
1905 warn "$method ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
1907 my $error = $svc_x->$method($p); #$p? returns error?
1908 return { 'error' => $error } if $error;
1910 return {}; #? { 'error' => '' }
1914 sub acct_forward_info {
1917 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1918 return { 'error' => $session } if $context eq 'error';
1920 my $svc_forward = _customer_svc_x( $custnum,
1921 { 'srcsvc' => $p->{'svcnum'} },
1924 or return { 'error' => '',
1928 return { 'error' => '',
1929 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
1934 sub process_acct_forward {
1936 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1937 return { 'error' => $session } if $context eq 'error';
1939 my $old = _customer_svc_x( $custnum,
1940 { 'srcsvc' => $p->{'svcnum'} },
1944 if ( $p->{'dst'} eq '' ) {
1946 my $error = $old->delete;
1947 return { 'error' => $error };
1949 return { 'error' => '' };
1952 my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
1953 'dst' => $p->{'dst'},
1958 $new->svcnum($old->svcnum);
1959 my $cust_svc = $old->cust_svc;
1960 $new->svcpart($old->svcpart);
1961 $new->pkgnuym($old->pkgnum);
1962 $error = $new->replace($old);
1964 my $conf = new FS::Conf;
1965 $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
1967 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
1968 or return { 'error' => 'No service' }; #how would we even get here?
1970 $new->pkgnum( $svc_acct->cust_svc->pkgnum );
1972 $error = $new->insert;
1975 return { 'error' => $error };
1979 sub list_dsl_devices {
1982 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1983 return { 'error' => $session } if $context eq 'error';
1985 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1986 or return { 'error' => "Service not found" };
1989 'devices' => [ map {
1990 +{ 'mac_addr' => $_->mac_addr };
1991 } $svc_dsl->dsl_device
1997 sub add_dsl_device {
2000 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2001 return { 'error' => $session } if $context eq 'error';
2003 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2004 or return { 'error' => "Service not found" };
2006 return { 'error' => 'No MAC address supplied' }
2007 unless length($p->{'mac_addr'});
2009 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
2010 'mac_addr' => scalar($p->{'mac_addr'}),
2012 my $error = $dsl_device->insert;
2013 return { 'error' => $error };
2017 sub delete_dsl_device {
2020 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2021 return { 'error' => $session } if $context eq 'error';
2023 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2024 or return { 'error' => "Service not found" };
2026 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
2027 'mac_addr' => scalar($p->{'mac_addr'}),
2030 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
2032 my $error = $dsl_device->delete;
2033 return { 'error' => $error };
2039 _usage_details( \&_port_graph, $p,
2040 'svcdb' => 'svc_port',
2045 my($svc_port, $begin, $end) = @_;
2047 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
2048 push @usage, { 'png' => $pngOrError };
2052 sub _list_svc_usage {
2053 my($svc_acct, $begin, $end) = @_;
2055 foreach my $part_export (
2056 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
2057 qw( sqlradius sqlradius_withdomain )
2059 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
2064 sub list_svc_usage {
2065 _usage_details(\&_list_svc_usage, @_);
2068 sub _list_support_usage {
2069 my($svc_acct, $begin, $end) = @_;
2071 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
2072 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
2074 push @usage, { 'seconds' => $_->seconds,
2075 'support' => $_->support,
2076 '_date' => $_->_date,
2077 'id' => $_->transaction_id,
2078 'creator' => $_->creator,
2079 'subject' => $_->subject,
2080 'status' => $_->status,
2081 'ticketid' => $_->ticketid,
2087 sub list_support_usage {
2088 _usage_details(\&_list_support_usage, @_);
2091 sub _list_cdr_usage {
2092 # XXX CDR type support...
2093 # XXX any way to do a paged search on this?
2094 # we have to return the results all at once...
2095 my($svc_phone, $begin, $end, %opt) = @_;
2096 map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
2097 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, %opt );
2100 sub list_cdr_usage {
2102 _usage_details( \&_list_cdr_usage, $p,
2103 'svcdb' => 'svc_phone',
2107 sub _usage_details {
2108 my($callback, $p, %opt) = @_;
2110 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2111 return { 'error' => $session } if $context eq 'error';
2113 my $search = { 'svcnum' => $p->{'svcnum'} };
2114 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2116 my $svcdb = $opt{'svcdb'} || 'svc_acct';
2118 my $svc_x = qsearchs( $svcdb, $search );
2119 return { 'error' => 'No service selected in list_svc_usage' }
2122 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
2123 my $freq = $cust_pkg->part_pkg->freq;
2126 if ( $svcdb eq 'svc_phone' ) {
2127 my $conf = FS::Conf->new;
2129 if ( $p->{inbound} ) {
2130 $format = $cust_pkg->part_pkg->option('selfservice_inbound_format')
2131 || $conf->config('selfservice-default_inbound_cdr_format')
2132 || 'source_default';
2133 $callback_opt{inbound} = 1;
2135 $format = $cust_pkg->part_pkg->option('selfservice_format')
2136 || $conf->config('selfservice-default_cdr_format')
2140 $callback_opt{format} = $format;
2141 $callback_opt{use_clid} = 1;
2142 $header = [ split(',', FS::cdr::invoice_header($format) ) ];
2145 my $start = $cust_pkg->setup;
2146 #my $end = $cust_pkg->bill; # or time?
2149 unless ( $p->{beginning} ) {
2150 $p->{beginning} = $cust_pkg->last_bill;
2151 $p->{ending} = $end;
2154 die "illegal beginning" if $p->{beginning} !~ /^\d*$/;
2155 die "illegal ending" if $p->{ending} !~ /^\d*$/;
2157 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
2161 #kinda false laziness with FS::cust_main::bill, but perhaps
2162 #we should really change this bit to DateTime and DateTime::Duration
2164 #change this bit to use Date::Manip? CAREFUL with timezones (see
2165 # mailing list archive)
2166 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
2167 (localtime($p->{ending}) )[0,1,2,3,4,5];
2168 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
2169 (localtime($p->{beginning}) )[0,1,2,3,4,5];
2171 if ( $freq =~ /^\d+$/ ) {
2173 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
2175 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
2176 } elsif ( $freq =~ /^(\d+)w$/ ) {
2178 $nmday += $weeks * 7;
2179 $pmday -= $weeks * 7;
2180 } elsif ( $freq =~ /^(\d+)d$/ ) {
2184 } elsif ( $freq =~ /^(\d+)h$/ ) {
2189 return { 'error' => "unparsable frequency: ". $freq };
2192 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
2193 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
2197 'svcnum' => $p->{svcnum},
2198 'beginning' => $p->{beginning},
2199 'ending' => $p->{ending},
2200 'inbound' => $p->{inbound},
2201 'previous' => ($previous > $start) ? $previous : $start,
2202 'next' => ($next < $end) ? $next : $end,
2203 'header' => $header,
2211 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2212 return { 'error' => $session } if $context eq 'error';
2214 my $search = { 'custnum' => $custnum };
2215 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2216 my $cust_main = qsearchs('cust_main', $search )
2217 or return { 'error' => "unknown custnum $custnum" };
2219 my $status = $cust_main->status;
2220 #false laziness w/ClientAPI/Signup.pm
2222 my $cust_pkg = new FS::cust_pkg ( {
2223 'custnum' => $custnum,
2224 'pkgpart' => $p->{'pkgpart'},
2226 my $error = $cust_pkg->check;
2227 return { 'error' => $error } if $error;
2230 unless ( $p->{'svcpart'} eq 'none' ) {
2234 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
2236 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2237 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
2238 $svcdb = $part_svc->svcdb;
2240 $svcdb = 'svc_acct';
2242 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
2245 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
2246 'svc_domain' => [ qw( domain ) ],
2247 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
2248 'svc_external' => [ qw( id title ) ],
2249 'svc_pbx' => [ qw( id title ) ],
2252 my $svc_x = "FS::$svcdb"->new( {
2253 'svcpart' => $svcpart,
2254 map { $_ => $p->{$_} } @{$fields{$svcdb}}
2257 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
2260 while ( length($p->{"snarf_machine$snarfnum"}) ) {
2261 my $acct_snarf = new FS::acct_snarf ( {
2262 'machine' => $p->{"snarf_machine$snarfnum"},
2263 'protocol' => $p->{"snarf_protocol$snarfnum"},
2264 'username' => $p->{"snarf_username$snarfnum"},
2265 '_password' => $p->{"snarf_password$snarfnum"},
2268 push @acct_snarf, $acct_snarf;
2270 $svc_x->child_objects( \@acct_snarf );
2273 my $y = $svc_x->setdefault; # arguably should be in new method
2274 return { 'error' => $y } if $y && !ref($y);
2276 $error = $svc_x->check;
2277 return { 'error' => $error } if $error;
2284 tie my %hash, 'Tie::RefHash';
2285 %hash = ( $cust_pkg => \@svc );
2287 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
2288 return { 'error' => $error } if $error;
2290 my $conf = new FS::Conf;
2291 if ( $conf->exists('signup_server-realtime') ) {
2293 my $bill_error = _do_bop_realtime( $cust_main, $status );
2296 $cust_pkg->cancel('quiet'=>1);
2299 $cust_pkg->reexport;
2303 $cust_pkg->reexport;
2306 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
2308 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
2315 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2316 return { 'error' => $session } if $context eq 'error';
2318 my $search = { 'custnum' => $custnum };
2319 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2320 my $cust_main = qsearchs('cust_main', $search )
2321 or return { 'error' => "unknown custnum $custnum" };
2323 my $status = $cust_main->status;
2324 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
2325 or return { 'error' => "unknown package $p->{pkgnum}" };
2327 #if someone does need self-service package change of suspended packages,
2328 # figure out how to be more discriminating
2329 return { error=>"Can't change a suspended package", pkgnum=>$cust_pkg->pkgnum}
2330 if $cust_pkg->status eq 'suspended';
2333 my $error = FS::cust_pkg::order( $custnum,
2339 my $conf = new FS::Conf;
2340 if ( $conf->exists('signup_server-realtime') ) {
2342 my $bill_error = _do_bop_realtime( $cust_main, $status, 'no_credit'=>1 );
2345 $newpkg[0]->suspend;
2348 $newpkg[0]->reexport;
2352 $newpkg[0]->reexport;
2355 return { error => '', pkgnum => $cust_pkg->pkgnum };
2359 sub order_recharge {
2362 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2363 return { 'error' => $session } if $context eq 'error';
2365 my $search = { 'custnum' => $custnum };
2366 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2367 my $cust_main = qsearchs('cust_main', $search )
2368 or return { 'error' => "unknown custnum $custnum" };
2370 my $status = $cust_main->status;
2371 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
2372 or return { 'error' => "unknown service " . $p->{'svcnum'} };
2374 my $svc_x = $cust_svc->svc_x;
2375 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
2378 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
2379 qw ( recharge_seconds recharge_upbytes recharge_downbytes
2380 recharge_totalbytes );
2381 my $amount = $part_pkg->option('recharge_amount', 1);
2383 my ($l, $v, $d) = $cust_svc->label; # blah
2384 my $pkg = "Recharge $v";
2386 my $bill_error = $cust_main->charge($amount, $pkg,
2387 "time: $vhash{seconds}, up: $vhash{upbytes}," .
2388 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
2389 $part_pkg->taxclass); #meh
2391 my $conf = new FS::Conf;
2392 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
2394 $bill_error = _do_bop_realtime( $cust_main, $status );
2399 my $error = $svc_x->recharge (\%vhash);
2400 return { 'error' => $error } if $error;
2404 my $error = $bill_error;
2405 $error ||= $svc_x->recharge (\%vhash);
2406 return { 'error' => $error } if $error;
2409 return { error => '', svc => $cust_svc->part_svc->svc };
2413 sub _do_bop_realtime {
2414 my ($cust_main, $status, %opt) = @_;
2416 my $old_balance = $cust_main->balance;
2418 my $bill_error = $cust_main->bill
2419 || $cust_main->apply_payments_and_credits;
2421 $bill_error ||= $cust_main->realtime_collect('selfservice' => 1)
2422 if $cust_main->payby =~ /^(CARD|CHEK)$/;
2424 if ( $cust_main->balance > $old_balance
2425 && $cust_main->balance > 0
2426 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/
2427 || $status eq 'suspended'
2431 unless ( $opt{'no_credit'} ) {
2432 #this makes sense. credit is "un-doing" the invoice
2433 my $conf = new FS::Conf;
2434 $cust_main->credit( sprintf("%.2f", $cust_main->balance-$old_balance ),
2435 'self-service decline',
2436 reason_type=>$conf->config('signup_credit_type'),
2438 $cust_main->apply_credits( 'order' => 'newest' );
2441 return { 'error' => '_decline', 'bill_error' => $bill_error };
2450 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2451 return { 'error' => $session } if $context eq 'error';
2453 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2454 or return { 'error' => "unknown custnum $custnum" };
2456 my @cust_pkg = sort { $a->bill <=> $b->bill }
2457 grep { $_->part_pkg->freq ne '0' }
2458 $cust_main->ncancelled_pkgs;
2460 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
2462 my $total = $cust_main->balance;
2465 my $bill = $_->bill;
2466 $total += $_->part_pkg->base_recur($_, \$bill);
2467 my $renew_date = $_->part_pkg->add_freq($_->bill);
2469 'pkgnum' => $_->pkgnum,
2470 'amount' => sprintf('%.2f', $total),
2471 'bill_date' => $_->bill,
2472 'bill_date_pretty' => time2str('%x', $_->bill),
2473 'renew_date' => $renew_date,
2474 'renew_date_pretty' => time2str('%x', $renew_date),
2475 'expire_date' => $_->expire,
2476 'expire_date_pretty' => time2str('%x', $_->expire),
2481 return { 'dates' => \@array };
2485 sub payment_info_renew_info {
2487 my $renew_info = renew_info($p);
2488 my $payment_info = payment_info($p);
2489 return { %$renew_info,
2497 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2498 return { 'error' => $session } if $context eq 'error';
2500 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2501 or return { 'error' => "unknown custnum $custnum" };
2503 my $date = $p->{'date'};
2507 #freeside-daily -n -d $date fs_daily $custnum
2508 $cust_main->bill_and_collect( 'time' => $date,
2509 'invoice_time' => $now,
2510 'actual_time' => $now,
2511 'check_freq' => '1d',
2514 return { 'error' => '' };
2520 my $session = _cache->get($p->{'session_id'})
2521 or return { 'error' => "Can't resume session" }; #better error message
2523 my $custnum = $session->{'custnum'};
2525 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2526 or return { 'error' => "unknown custnum $custnum" };
2528 my $conf = new FS::Conf;
2530 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2531 or return { 'error' => 'Permission denied' };
2533 my $pkgnum = $p->{'pkgnum'};
2535 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2536 'pkgnum' => $pkgnum, } )
2537 or return { 'error' => "unknown pkgnum $pkgnum" };
2539 my $error = $cust_pkg->suspend(reason => $reasonnum);
2540 return { 'error' => $error };
2546 my $session = _cache->get($p->{'session_id'})
2547 or return { 'error' => "Can't resume session" }; #better error message
2549 my $custnum = $session->{'custnum'};
2551 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2552 or return { 'error' => "unknown custnum $custnum" };
2554 my $pkgnum = $p->{'pkgnum'};
2556 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2557 'pkgnum' => $pkgnum, } )
2558 or return { 'error' => "unknown pkgnum $pkgnum" };
2560 my $error = $cust_pkg->cancel('quiet' => 1);
2561 return { 'error' => $error };
2565 sub provision_phone {
2568 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2570 if($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/){
2571 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2572 return { 'error' => $session } if $context eq 'error';
2574 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2575 return { 'error' => 'service not found' } unless $svc_phone;
2576 return { 'error' => 'invalid svcnum' }
2577 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2579 $svc_phone->email($p->{'email'})
2580 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2581 $svc_phone->forwarddst($p->{'forwarddst'})
2582 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2583 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2584 return { 'error' => $svc_phone->replace };
2588 unless($p->{'lnp'}) {
2589 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2590 $p->{'lnp_status'} = "portingin";
2591 return _provision( 'FS::svc_phone',
2592 [qw(lnp_desired_due_date lnp_other_provider
2593 lnp_other_provider_account phonenum countrycode lnp_status)],
2594 [qw(phonenum countrycode)],
2601 unless (scalar(@bulkdid)) {
2602 return _provision( 'FS::svc_phone',
2603 [qw(phonenum countrycode)],
2604 [qw(phonenum countrycode)],
2610 # bulk DID order case
2612 foreach my $did ( @bulkdid ) {
2613 $did =~ s/[^0-9]//g;
2614 $error = _provision( 'FS::svc_phone',
2615 [qw(phonenum countrycode)],
2616 [qw(phonenum countrycode)],
2618 'pkgnum' => $p->{'pkgnum'},
2619 'svcpart' => $p->{'svcpart'},
2621 'countrycode' => $p->{'countrycode'},
2622 'session_id' => $p->{'session_id'},
2625 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2627 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2630 sub provision_acct {
2632 warn "provision_acct called\n"
2635 return { 'error' => gettext('passwords_dont_match') }
2636 if $p->{'_password'} ne $p->{'_password2'};
2637 return { 'error' => gettext('empty_password') }
2638 unless length($p->{'_password'});
2640 if ($p->{'domsvc'}) {
2641 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
2642 qw ( svcpart pkgnum ) );
2643 return { 'error' => gettext('invalid_domain') }
2644 unless ($domains{$p->{'domsvc'}});
2647 warn "provision_acct calling _provision\n"
2649 _provision( 'FS::svc_acct',
2650 [qw(username _password domsvc)],
2651 [qw(username _password domsvc)],
2657 sub provision_external {
2659 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
2660 _provision( 'FS::svc_external',
2669 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
2670 warn "_provision called for $class\n"
2673 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2674 return { 'error' => $session } if $context eq 'error';
2676 my $search = { 'custnum' => $custnum };
2677 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2678 my $cust_main = qsearchs('cust_main', $search )
2679 or return { 'error' => "unknown custnum $custnum" };
2681 my $pkgnum = $p->{'pkgnum'};
2683 warn "searching for custnum $custnum pkgnum $pkgnum\n"
2685 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2686 'pkgnum' => $pkgnum,
2688 or return { 'error' => "unknown pkgnum $pkgnum" };
2690 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
2692 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
2693 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
2695 warn "creating $class record\n"
2697 my $svc_x = $class->new( {
2698 'pkgnum' => $p->{'pkgnum'},
2699 'svcpart' => $p->{'svcpart'},
2700 map { $_ => $p->{$_} } @$fields
2702 warn "inserting $class record\n"
2704 my $error = $svc_x->insert;
2707 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
2709 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
2712 my $return = { 'svc' => $part_svc->svc,
2714 map { $_ => $svc_x->get($_) } @$return_fields
2716 warn "_provision returning ". Dumper($return). "\n"
2725 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2726 return { 'error' => $session } if $context eq 'error';
2728 my $search = { 'custnum' => $custnum };
2729 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2730 my $cust_main = qsearchs('cust_main', $search )
2731 or return { 'error' => "unknown custnum $custnum" };
2733 my $pkgnum = $p->{'pkgnum'};
2735 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2736 'pkgnum' => $pkgnum,
2738 or return { 'error' => "unknown pkgnum $pkgnum" };
2740 my $svcpart = $p->{'svcpart'};
2742 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
2743 'svcpart' => $svcpart, } )
2744 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
2745 my $part_svc = $pkg_svc->part_svc;
2747 my $conf = new FS::Conf;
2750 'svc' => $part_svc->svc,
2751 'svcdb' => $part_svc->svcdb,
2752 'pkgnum' => $pkgnum,
2753 'svcpart' => $svcpart,
2754 'custnum' => $custnum,
2756 'security_phrase' => 0, #XXX !
2757 'svc_acct_pop' => [], #XXX !
2759 'init_popstate' => '',
2764 small_custview( $cust_main, $conf->config('countrydefault') ),
2768 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
2769 && $ret->{'svcdb'} eq 'svc_phone') {
2770 $ret->{'svcnum'} = $p->{'svcnum'};
2771 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2772 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
2773 $ret->{'email'} = $svc_phone->email;
2774 $ret->{'forwarddst'} = $svc_phone->forwarddst;
2781 sub unprovision_svc {
2784 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2785 return { 'error' => $session } if $context eq 'error';
2787 my $search = { 'custnum' => $custnum };
2788 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2789 my $cust_main = qsearchs('cust_main', $search )
2790 or return { 'error' => "unknown custnum $custnum" };
2792 my $svcnum = $p->{'svcnum'};
2794 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
2795 or return { 'error' => "unknown svcnum $svcnum" };
2797 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
2798 unless $cust_svc->cust_pkg->custnum == $custnum;
2800 my $conf = new FS::Conf;
2802 return { 'svc' => $cust_svc->part_svc->svc,
2803 'error' => $cust_svc->cancel,
2805 small_custview( $cust_main, $conf->config('countrydefault') ),
2810 sub myaccount_passwd {
2812 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2813 return { 'error' => $session } if $context eq 'error';
2815 return { 'error' => "New passwords don't match." }
2816 if $p->{'new_password'} ne $p->{'new_password2'};
2818 return { 'error' => 'Enter new password' }
2819 unless length($p->{'new_password'});
2821 #my $search = { 'custnum' => $custnum };
2822 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2823 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2824 my $search = " AND custnum = $1";
2825 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2827 my $svc_acct = qsearchs( {
2828 'table' => 'svc_acct',
2829 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2830 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
2831 'LEFT JOIN cust_main USING ( custnum ) ',
2832 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
2833 'extra_sql' => $search, #important
2835 or return { 'error' => "Service not found" };
2839 my $conf = new FS::Conf;
2840 $error = 'Password too short.'
2841 if length($p->{'new_password'}) < ($conf->config('passwordmin') || 6);
2842 $error = 'Password too long.'
2843 if length($p->{'new_password'}) > ($conf->config('passwordmax') || 8);
2845 $svc_acct->set_password($p->{'new_password'});
2846 $error ||= $svc_acct->replace();
2848 #regular pw change in self-service should change contact pw too, otherwise its
2849 #way too confusing. hell its confusing they're separate at all, but alas.
2850 #need to support the "ISP provides email that's used as a contact email" case
2852 my $contact = FS::contact->by_selfservice_email($svc_acct->email);
2853 if ( $contact && $contact->custnum == $custnum ) {
2854 #svc_acct was successful but this one returns an error? "shouldn't happen"
2855 $error ||= $contact->change_password($p->{'new_password'});
2858 my($label, $value) = $svc_acct->cust_svc->label;
2860 return { 'error' => $error,
2867 # sub contact_passwd {
2869 # my($context, $session, $custnum) = _custoragent_session_custnum($p);
2870 # return { 'error' => $session } if $context eq 'error';
2872 # return { 'error' => 'Not logged in as a contact.' }
2873 # unless $session->{'contactnum'};
2875 # return { 'error' => "New passwords don't match." }
2876 # if $p->{'new_password'} ne $p->{'new_password2'};
2878 # return { 'error' => 'Enter new password' }
2879 # unless length($p->{'new_password'});
2881 # #my $search = { 'custnum' => $custnum };
2882 # #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2883 # $custnum =~ /^(\d+)$/ or die "illegal custnum";
2884 # my $search = " AND selfservice_access IS NOT NULL ".
2885 # " AND selfservice_access = 'Y' ".
2886 # " AND ( disabled IS NULL OR disabled = '' )".
2887 # " AND custnum IS NOT NULL AND custnum = $1";
2888 # $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2890 # my $contact = qsearchs( {
2891 # 'table' => 'contact',
2892 # 'addl_from' => 'LEFT JOIN cust_main USING ( custnum ) ',
2893 # 'hashref' => { 'contactnum' => $session->{'contactnum'}, },
2894 # 'extra_sql' => $search, #important
2896 # or return { 'error' => "Email not found" }; #? how did we get logged in?
2897 # # deleted since then?
2901 # # use these svc_acct length restrictions??
2902 # my $conf = new FS::Conf;
2903 # $error = 'Password too short.'
2904 # if length($p->{'new_password'}) < ($conf->config('passwordmin') || 6);
2905 # $error = 'Password too long.'
2906 # if length($p->{'new_password'}) > ($conf->config('passwordmax') || 8);
2908 # $error ||= $contact->change_password($p->{'new_password'});
2910 # return { 'error' => $error, };
2917 my $info = skin_info($p);
2919 my $conf = new FS::Conf;
2920 my $verification = $conf->config('selfservice-password_reset_verification')
2921 or return { %$info, 'error' => 'Password resets disabled' };
2926 if ( $p->{'email'} ) { #new-style, changes contact and svc_acct
2928 $contact = FS::contact->by_selfservice_email($p->{'email'});
2930 $cust_main = $contact->cust_main if $contact;
2932 #also look for an svc_acct, otherwise it would be super confusing
2934 my($username, $domain) = split('@', $p->{'email'});
2935 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } );
2936 if ( $svc_domain ) {
2937 $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
2938 'domsvc' => $svc_domain->svcnum }
2941 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
2942 $cust_main ||= $cust_pkg->cust_main if $cust_pkg;
2944 #precaution: don't change svc_acct password not part of the same
2945 # customer as contact
2946 $svc_acct = '' if ! $cust_pkg
2947 || $cust_pkg->custnum != $cust_main->custnum;
2952 return { %$info, 'error' => 'Email address not found' }
2953 unless $contact || $svc_acct;
2955 } elsif ( $p->{'username'} ) { #old style, looks in svc_acct only
2957 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
2958 or return { %$info, 'error' => 'Account not found' };
2960 $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
2961 'domsvc' => $svc_domain->svcnum }
2963 or return { %$info, 'error' => 'Account not found' };
2965 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
2966 or return { %$info, 'error' => 'Account not found' };
2968 $cust_main = $cust_pkg->cust_main;
2973 'email' => sub { 1; },
2975 my( $p, $cust_main ) = @_;
2976 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
2977 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
2980 my( $p, $cust_main ) = @_;
2981 my $cust_pay = qsearchs({
2982 'table' => 'cust_pay',
2983 'hashref' => { 'custnum' => $cust_main->custnum },
2984 'order_by' => 'ORDER BY _date DESC LIMIT 1',
2988 $p->{'amount'} == $cust_pay->paid;
2991 my( $p, $cust_main ) = @_;
2992 $p->{'zip'} eq $cust_main->zip
2993 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
2997 foreach my $verify ( split(',', $verification) ) {
2999 &{ $verify{$verify} }( $p, $cust_main )
3000 or return { %$info, 'error' => 'Account not found' };
3004 #okay, we're verified
3008 my $error = $contact->send_reset_email(
3009 'svcnum' => ($svc_acct ? $svc_acct->svcnum : ''),
3013 return { %$info, 'error' => $error }; #????
3016 } elsif ( $svc_acct ) {
3018 #create a unique session
3020 my $reset_session = {
3021 'svcnum' => $svc_acct->svcnum,
3025 my $timeout = '1 hour'; #?
3027 my $reset_session_id;
3029 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
3030 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") );
3033 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
3037 my $msgnum = $conf->config('selfservice-password_reset_msgnum',
3038 $cust_main->agentnum);
3039 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
3040 return { %$info, 'error' => "selfservice-password_reset_msgnum unset" }
3042 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
3043 my $error = $msg_template->send( 'cust_main' => $cust_main,
3044 'object' => $svc_acct,
3045 'substitutions' => {
3046 'session_id' => $reset_session_id,
3050 return { %$info, 'error' => $error }; #????
3055 return { %$info, 'error' => '' };
3058 sub check_reset_passwd {
3061 my $conf = new FS::Conf;
3062 my $verification = $conf->config('selfservice-password_reset_verification')
3063 or return { 'error' => 'Password resets disabled' };
3065 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3066 or return { 'error' => "Can't resume session" }; #better error message
3068 if ( $reset_session->{'svcnum'} ) {
3070 my $svcnum = $reset_session->{'svcnum'};
3072 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3073 or return { 'error' => "Service not found" };
3075 $p->{'agentnum'} = $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3076 my $info = skin_info($p);
3080 'session_id' => $p->{'session_id'},
3081 'username' => $svc_acct->username,
3084 } elsif ( $reset_session->{'contactnum'} ) {
3086 my $contactnum = $reset_session->{'contactnum'};
3088 my $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3089 or return { 'error' => "Contact not found" };
3091 my @contact_email = $contact->contact_email;
3092 return { 'error' => 'No contact email' } unless @contact_email;
3094 $p->{'agentnum'} = $contact->cust_main->agentnum;
3095 my $info = skin_info($p);
3099 'session_id' => $p->{'session_id'},
3100 'email' => $contact_email[0]->email, #the first?
3105 return { 'error' => 'No svcnum or contactnum in session' }; #??
3111 sub process_reset_passwd {
3114 my $conf = new FS::Conf;
3115 my $verification = $conf->config('selfservice-password_reset_verification')
3116 or return { 'error' => 'Password resets disabled' };
3118 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3119 or return { 'error' => "Can't resume session" }; #better error message
3124 if ( $reset_session->{'svcnum'} ) {
3126 my $svcnum = $reset_session->{'svcnum'};
3128 $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3129 or return { 'error' => "Service not found" };
3131 $p->{'agentnum'} ||= $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3132 $info ||= skin_info($p);
3137 if ( $reset_session->{'contactnum'} ) {
3139 my $contactnum = $reset_session->{'contactnum'};
3141 $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3142 or return { 'error' => "Contact not found" };
3144 $p->{'agentnum'} ||= $contact->cust_main->agentnum;
3145 $info ||= skin_info($p);
3149 return { %$info, 'error' => "New passwords don't match." }
3150 if $p->{'new_password'} ne $p->{'new_password2'};
3152 return { %$info, 'error' => 'Enter new password' }
3153 unless length($p->{'new_password'});
3157 $svc_acct->set_password($p->{'new_password'});
3158 my $error = $svc_acct->replace();
3160 return { %$info, 'error' => $error } if $error;
3162 #my($label, $value) = $svc_acct->cust_svc->label;
3163 #return { 'error' => $error,
3164 # #'label' => $label,
3165 # #'value' => $value,
3172 my $error = $contact->change_password($p->{'new_password'});
3174 return { %$info, 'error' => $error }; # if $error;
3178 #password changed ,so remove session, don't want it reused
3179 _cache->remove($p->{'session_id'});
3181 return { %$info, 'error' => '' };
3187 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3188 return { 'error' => $session } if $context eq 'error';
3191 if ( $session->{'pkgnum'} ) {
3193 #tickets for specific service with pkg-balances on
3194 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3195 'pkgnum' => $session->{'pkgnum'} })
3196 or return { 'error' => 'unknown package' };
3197 foreach my $cust_svc ( $cust_pkg->cust_svc ) {
3198 push @tickets, $cust_svc->tickets( $p->{status} );
3203 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
3204 or return { 'error' => "unknown custnum $custnum" };
3206 @tickets = $cust_main->tickets( $p->{status} );
3209 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
3210 if ( $FS::TicketSystem::system && FS::TicketSystem->selfservice_priority ) {
3211 my $conf = new FS::Conf;
3212 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
3216 ($a->{'_selfservice_priority'} eq '') <=>
3217 ($b->{'_selfservice_priority'} eq '')
3220 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
3226 +{ tickets => \@tickets };
3233 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3234 return { 'error' => $session } if $context eq 'error';
3236 # warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
3237 # FS::TicketSystem->init();
3239 my $conf = new FS::Conf;
3240 my $queue = $p->{'queue'}
3241 || $conf->config('ticket_system-selfservice_queueid')
3242 || $conf->config('ticket_system-default_queueid');
3244 warn "$me create_ticket: creating ticket\n" if $DEBUG;
3245 my $err_or_ticket = FS::TicketSystem->create_ticket(
3246 '', #create RT session based on FS CurrentUser (fs_selfservice)
3248 'custnum' => $custnum,
3249 'svcnum' => $session->{'svcnum'},
3250 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
3253 if ( ref($err_or_ticket) ) {
3254 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
3256 return { 'error' => '',
3257 'ticket_id' => $err_or_ticket->id,
3260 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
3262 return { 'error' => $err_or_ticket };
3270 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3271 return { 'error' => $session } if $context eq 'error';
3273 return { error => 'requested format not implemented' }
3274 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
3276 my $conf = new FS::Conf;
3277 my $age_threshold = 0;
3278 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
3279 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
3281 my $search = { 'custnum' => $custnum };
3282 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3283 my $cust_main = qsearchs('cust_main', $search )
3284 or return { 'error' => "unknown custnum $custnum" };
3286 # does it make more sense to just run one sql query for this instead of all the
3287 # insanity below? would increase performance greately for large data sets?
3289 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
3290 my @part_svc = $cust_pkg->part_svc;
3291 foreach my $part_svc ( @part_svc ) {
3292 if($part_svc->svcdb eq 'svc_phone'){
3293 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
3294 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
3295 push @svc_phone, $cust_pkg_svc->svc_x
3296 if $cust_pkg_svc->date_inserted >= $age_threshold;
3304 my($xls_r,$xls_c) = (0,0);
3307 my @fields = qw( countrycode phonenum pin sip_password phone_name );
3308 if($p->{'format'} eq 'csv') {
3309 $csv = new Text::CSV_XS { 'always_quote' => 1,
3312 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
3313 $content .= $csv->string;
3315 elsif($p->{'format'} eq 'xls') {
3316 my $XLS1 = new IO::Scalar \$content;
3317 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
3318 or return { 'error' => "Error opening .xls file: $!" };
3319 $xls = $xls_workbook->add_worksheet('DIDs');
3320 foreach ( @fields ) {
3321 $xls->write(0,$xls_c++,$_);
3326 foreach my $svc_phone ( @svc_phone ) {
3327 my @cols = map { $svc_phone->$_ } @fields;
3328 if($p->{'format'} eq 'csv') {
3329 return { 'error' => 'Unable to create CSV' }
3330 unless $csv->combine(@cols);
3331 $content .= $csv->string;
3333 elsif($p->{'format'} eq 'xls') {
3336 $xls->write($xls_r,$xls_c++,$_);
3342 $xls_workbook->close() if $p->{'format'} eq 'xls';
3344 { content => $content, format => $p->{'format'}, };
3349 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3350 return { 'error' => $session } if $context eq 'error';
3352 # warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
3353 # FS::TicketSystem->init();
3354 # return { 'error' => 'get_ticket configuration error' }
3355 # if $FS::TicketSystem::system ne 'RT_Internal';
3357 # check existence and ownership as part of this
3358 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
3359 my $rt_session = FS::TicketSystem->session('');
3360 my $Ticket = FS::TicketSystem->get_ticket_object(
3362 ticket_id => $p->{'ticket_id'},
3365 return { 'error' => 'ticket not found' } if !$Ticket;
3367 if ( length( $p->{'subject'} || '' ) ) {
3369 if ( $p->{'subject'} ne $Ticket->Subject ) {
3370 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
3371 return { 'error' => "unable to set subject: $msg" } if !$val;
3375 if(length($p->{'reply'})) {
3376 my @err_or_res = FS::TicketSystem->correspond_ticket(
3378 'ticket_id' => $p->{'ticket_id'},
3379 'content' => $p->{'reply'},
3382 return { 'error' => 'unable to reply to ticket' }
3383 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
3386 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
3387 my $err_or_ticket = FS::TicketSystem->get_ticket(
3389 'ticket_id' => $p->{'ticket_id'},
3392 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
3393 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
3395 return { 'error' => $err_or_ticket };
3398 my @custs = @{$err_or_ticket->{'custs'}};
3399 my @txns = @{$err_or_ticket->{'txns'}};
3402 # superseded by check in get_ticket_object
3403 #return { 'error' => 'invalid ticket requested' }
3404 #unless grep($_ eq $custnum, @custs);
3406 foreach my $txn ( @txns ) {
3407 push @filtered_txns, $txn
3408 if ($txn->{'type'} eq 'EmailRecord'
3409 || $txn->{'type'} eq 'Correspond'
3410 || $txn->{'type'} eq 'Create');
3413 warn "$me get_ticket: successful: \n"
3415 return { 'error' => '',
3416 'transactions' => \@filtered_txns,
3417 'ticket_fields' => $err_or_ticket->{'fields'},
3418 'ticket_id' => $p->{'ticket_id'},
3422 sub adjust_ticket_priority {
3424 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3425 return { 'error' => $session } if $context eq 'error';
3427 # warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
3428 # FS::TicketSystem->init;
3429 my $ss_priority = FS::TicketSystem->selfservice_priority;
3431 return { 'error' => 'adjust_ticket_priority configuration error' }
3432 if $FS::TicketSystem::system ne 'RT_Internal'
3435 my $values = $p->{'values'}; #hashref, id => priority value
3438 foreach my $id (keys %$values) {
3439 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
3440 my $Ticket = FS::TicketSystem->get_ticket_object('',
3442 'custnum' => $custnum,
3445 $ticket_error{$id} = 'ticket not found';
3449 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
3450 # We're not going to implement it for RT_External.
3451 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
3452 my $new_value = $values->{$id};
3453 next if $old_value eq $new_value;
3455 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
3457 # AddCustomFieldValue works fine (replacing any existing value) if it's
3458 # a single-valued custom field, which it should be. If it's not, you're
3459 # doing something wrong.
3461 if ( length($new_value) ) {
3462 ($val, $msg) = $Ticket->AddCustomFieldValue(
3463 Field => $ss_priority,
3464 Value => $new_value,
3468 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
3469 Field => $ss_priority,
3470 Value => $old_value,
3474 $ticket_error{$id} = $msg if !$val;
3475 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
3477 return { 'error' => '',
3478 'ticket_error' => \%ticket_error,
3479 %{ customer_info($p) } # send updated customer info back
3485 sub _custoragent_session_custnum {
3488 my($context, $session, $custnum);
3489 if ( $p->{'session_id'} ) {
3491 $context = 'customer';
3492 $session = _cache->get($p->{'session_id'})
3493 or return ( 'error' => "Can't resume session" ); #better error message
3494 $custnum = $session->{'custnum'};
3496 } elsif ( $p->{'agent_session_id'} ) {
3499 my $agent_cache = new FS::ClientAPI_SessionCache( {
3500 'namespace' => 'FS::ClientAPI::Agent',
3502 $session = $agent_cache->get($p->{'agent_session_id'})
3503 or return ( 'error' => "Can't resume session" ); #better error message
3504 $custnum = $p->{'custnum'};
3508 return ( 'error' => "Can't resume session" ); #better error message
3511 ($context, $session, $custnum);