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;
49 use FS::cust_location;
51 use FS::ClientAPI::MyAccount::quotation; # just for code organization
54 $me = '[FS::ClientAPI::MyAccount]';
56 use vars qw( @cust_main_editable_fields @location_editable_fields );
57 @cust_main_editable_fields = qw(
58 first last company daytime night fax mobile
60 payby payinfo payname paystart_month paystart_year payissue payip
61 ss paytype paystate stateid stateid_state
63 @location_editable_fields = qw(
64 address1 address2 city county state zip country
68 BEGIN { #preload to reduce time customer_info takes
69 if ( $FS::TicketSystem::system ) {
70 warn "$me: initializing ticket system\n" if $DEBUG;
71 FS::TicketSystem->init();
76 $cache ||= new FS::ClientAPI_SessionCache( {
77 'namespace' => 'FS::ClientAPI::MyAccount',
84 my($context, $session, $custnum) = _custoragent_session_custnum($p);
85 #return { 'error' => $session } if $context eq 'error';
88 if ( $context eq 'customer' ) {
90 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
93 $sth->execute($custnum) or die $sth->errstr;
95 $agentnum = $sth->fetchrow_arrayref->[0]
96 or die "no agentnum for custnum $custnum";
98 #} elsif ( $context eq 'agent' ) {
99 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
102 $p->{'agentnum'} = $agentnum;
104 my $conf = new FS::Conf;
106 #false laziness w/Signup.pm
108 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
110 if ( $skin_info_cache_agent ) {
112 warn "$me loading cached skin info for agentnum $agentnum\n"
117 warn "$me populating skin info cache for agentnum $agentnum\n"
120 $skin_info_cache_agent = {
121 'agentnum' => $agentnum,
122 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
123 qw( company_name date_format ) ),
124 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
125 qw( body_bgcolor box_bgcolor stripe1_bgcolor stripe2_bgcolor
126 text_color link_color vlink_color hlink_color alink_color
127 font title_color title_align title_size menu_bgcolor menu_fontsize
130 'menu_disable' => [ $conf->config('selfservice-menu_disable',$agentnum) ],
131 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
132 qw( menu_skipblanks menu_skipheadings menu_nounderline no_logo )
134 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
135 qw( title_left_image title_right_image
136 menu_top_image menu_body_image menu_bottom_image
139 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
140 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
141 qw( head body_header body_footer company_address ) ),
142 'money_char' => $conf->config("money_char") || '$',
143 'menu' => join("\n", $conf->config("ng_selfservice-menu", $agentnum ) ) ||
146 services.php Services
147 services.php My Services
148 services_new.php Order a new service
151 personal.php Personal Information
152 password.php Change Password
155 payment_cc.php Credit Card Payment
156 payment_ach.php Electronic Check Payment
157 payment_paypal.php PayPal Payment
158 payment_webpay.php Webpay Payments
161 usage_data.php Data usage
162 usage_cdr.php Call usage
164 tickets.php Help Desk
165 tickets.php Open Tickets
166 tickets_resolved.php Resolved Tickets
167 ticket_create.php Create a new ticket
175 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
179 #{ %$skin_info_cache_agent };
180 $skin_info_cache_agent;
187 my $conf = new FS::Conf;
191 'phone_login' => $conf->exists('selfservice_server-phone_login'),
192 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
193 'banner_url' => scalar($conf->config('selfservice-login_banner_url')),
194 'banner_image_md5' =>
195 md5_hex($conf->config_binary('selfservice-login_banner_image')),
202 sub login_banner_image {
204 my $conf = new FS::Conf;
205 my $image = $conf->config_binary('selfservice-login_banner_image');
207 'md5' => md5_hex($image),
212 #false laziness w/FS::ClientAPI::passwd::passwd
216 my $conf = new FS::Conf;
220 if ( $p->{'domain'} eq 'svc_phone'
221 && $conf->exists('selfservice_server-phone_login') ) {
223 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
224 return { error => 'Number not found.' } unless $svc_phone;
227 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
228 #return { error => 'Only primary user may log in.' }
229 # if $conf->exists('selfservice_server-primary_only')
230 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
232 return { error => 'Incorrect PIN.' }
233 unless $svc_phone->check_pin($p->{'password'});
237 } elsif ( $p->{email}
238 && (my $contact = FS::contact->by_selfservice_email($p->{email}))
241 return { error => 'Incorrect contact password.' }
242 unless $contact->authenticate_password($p->{'password'});
244 $session->{'custnum'} = $contact->custnum;
248 ( $p->{username}, $p->{domain} ) = split('@', $p->{email}) if $p->{email};
250 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
251 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
253 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
254 'domsvc' => $svc_domain->svcnum, }
256 return { error => 'User not found.' } unless $svc_acct;
258 if($conf->exists('selfservice_server-login_svcpart')) {
259 my @svcpart = $conf->config('selfservice_server-login_svcpart');
260 my $svcpart = $svc_acct->cust_svc->svcpart;
261 return { error => 'Invalid user.' }
262 unless grep($_ eq $svcpart, @svcpart);
265 return { error => 'Incorrect password.' }
266 unless $svc_acct->check_password($p->{'password'});
274 $session->{'svcnum'} = $svc_x->svcnum;
276 my $cust_svc = $svc_x->cust_svc;
277 my $cust_pkg = $cust_svc->cust_pkg;
279 my $cust_main = $cust_pkg->cust_main;
280 $session->{'custnum'} = $cust_main->custnum;
281 if ( $conf->exists('pkg-balances') ) {
282 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
283 $cust_main->ncancelled_pkgs;
284 $session->{'pkgnum'} = $cust_pkg->pkgnum
285 if scalar(@cust_pkg) > 1;
289 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
290 #return { error => 'Only primary user may log in.' }
291 # if $conf->exists('selfservice_server-primary_only')
292 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
293 my $part_pkg = $cust_pkg->part_pkg;
294 return { error => 'Only primary user may log in.' }
295 if $conf->exists('selfservice_server-primary_only')
296 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
302 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
303 } until ( ! defined _cache->get($session_id) ); #just in case
305 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
306 _cache->set( $session_id, $session, $timeout );
308 return { 'error' => '',
309 'session_id' => $session_id,
315 my $skin_info = skin_info($p);
316 if ( $p->{'session_id'} ) {
317 _cache->remove($p->{'session_id'});
318 return { %$skin_info, 'error' => '' };
320 return { %$skin_info, 'error' => "Can't resume session" }; #better error message
327 my($context, $session, $custnum) = _custoragent_session_custnum($p);
328 return { 'error' => $session } if $context eq 'error';
330 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
331 or return { 'error' => "Service not found" };
333 $session->{'svcnum'} = $svc_acct->svcnum;
335 my $conf = new FS::Conf;
336 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
337 _cache->set( $p->{'session_id'}, $session, $timeout );
339 return { 'error' => '' };
343 sub payment_gateway {
345 # takes a cust_main and a cust_payby entry, returns the payment_gateway
346 my $conf = new FS::Conf;
347 my $cust_main = shift;
348 my $cust_payby = shift;
349 my $gatewaynum = $conf->config('selfservice-payment_gateway');
351 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
352 die "configured gatewaynum $gatewaynum not found!" if !$pg;
356 return '' if ! FS::payby->realtime($cust_payby);
357 my $pg = $cust_main->agent->payment_gateway(
358 'method' => FS::payby->payby2bop($cust_payby),
368 my $conf = new FS::Conf;
370 my $info = skin_info($p);
372 use vars qw( $cust_paybys ); #cache for performance
373 unless ( $cust_paybys ) {
375 my %cust_paybys = map { $_ => 1 }
376 map { FS::payby->payby2payment($_) }
377 $conf->config('signup_server-payby');
379 $cust_paybys = [ keys %cust_paybys ];
382 $info->{'cust_paybys'} = $cust_paybys;
384 my($context, $session, $custnum) = _custoragent_session_custnum($p);
385 return { 'error' => $session } if $context eq 'error';
387 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
388 or return { 'error' => "unknown custnum $custnum" };
390 $info->{'hide_payment_fields'} = [
392 my $pg = payment_gateway($cust_main, $_);
393 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
394 } @{ $info->{cust_paybys} }
397 $info->{'self_suspend_reason'} =
398 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
400 $info->{'edit_ticket_subject'} =
401 $conf->exists('ticket_system-selfservice_edit_subject') &&
402 $cust_main->edit_subject;
404 $info->{'timeout'} = $conf->config('selfservice-timeout') || 3600;
406 $info->{'hide_usage'} = $conf->exists('selfservice_hide-usage');
409 'custnum' => $custnum,
410 'access_pkgnum' => $session->{'pkgnum'},
411 'access_svcnum' => $session->{'svcnum'},
418 my($context, $session, $custnum) = _custoragent_session_custnum($p);
419 return { 'error' => $session } if $context eq 'error';
423 my $conf = new FS::Conf;
424 $return{'require_address2'} = $conf->exists('cust_main-require_address2');
426 # if ( $FS::TicketSystem::system ) {
427 # warn "$me customer_info: initializing ticket system\n" if $DEBUG;
428 # FS::TicketSystem->init();
431 if ( $custnum ) { #customer record
433 %return = ( %return, %{ customer_info_short($p) } );
435 #redundant with customer_info_short, but we need it for several things below
436 my $search = { 'custnum' => $custnum };
437 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
438 my $cust_main = qsearchs('cust_main', $search )
439 or return { 'error' => "unknown custnum $custnum" };
441 my $list_tickets = list_tickets($p);
442 $return{'tickets'} = $list_tickets->{'tickets'};
444 if ( $session->{'pkgnum'} ) {
445 #XXX open invoices in the pkg-balances case
449 invnum => $_->invnum,
450 date => time2str("%b %o, %Y", $_->_date),
453 } $cust_main->open_cust_bill;
454 $return{open_invoices} = \@open;
456 my $sql = 'SELECT MAX(_date) FROM cust_bill WHERE custnum = ?';
457 my $sth = dbh->prepare($sql) or die dbh->errstr;
458 $sth->execute($custnum) or die $sth->errstr;
459 $return{'last_invoice_date'} = $sth->fetchrow_arrayref->[0];
460 $return{'last_invoice_date_pretty'} =
461 time2str('%m/%d/%Y', $return{'last_invoice_date'} );
464 #customer_info_short always has nobalance on..
465 $return{small_custview} =
466 small_custview( $cust_main,
467 $return{countrydefault},
468 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
471 $return{has_ship_address} = $cust_main->has_ship_address;
472 $return{status} = $cust_main->status;
473 $return{statuscolor} = $cust_main->statuscolor;
475 # compatibility: some places in selfservice use this to determine
476 # if there's a ship address
477 if ( $return{has_ship_address} ) {
478 $return{ship_last} = $cust_main->last;
479 $return{ship_first} = $cust_main->first;
482 if (scalar($conf->config('support_packages'))) {
483 my @support_services = ();
484 foreach ($cust_main->support_services) {
485 my $seconds = $_->svc_x->seconds || 0;
486 my $time_remaining = (($seconds < 0) ? '-' : '' ).
487 int(abs($seconds)/3600)."h".
488 sprintf("%02d",(abs($seconds)%3600)/60)."m";
489 my $cust_pkg = $_->cust_pkg;
492 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
493 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
494 push @support_services, { svcnum => $_->svcnum,
495 time => $time_remaining,
500 $return{support_services} = \@support_services;
503 if ( $conf->config('prepayment_discounts-credit_type') ) {
505 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
508 } elsif ( $session->{'svcnum'} ) { #no customer record
510 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
511 or die "unknown svcnum";
512 $return{name} = $svc_acct->email;
516 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
520 return { 'error' => '',
521 'custnum' => $custnum,
527 sub customer_info_short {
530 my($context, $session, $custnum) = _custoragent_session_custnum($p);
531 return { 'error' => $session } if $context eq 'error';
535 my $conf = new FS::Conf;
537 if ( $custnum ) { #customer record
539 my $search = { 'custnum' => $custnum };
540 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
541 my $cust_main = qsearchs('cust_main', $search )
542 or return { 'error' => "unknown custnum $custnum" };
544 $return{display_custnum} = $cust_main->display_custnum;
546 if ( $session->{'pkgnum'} ) {
547 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
548 #next_bill_date from cust_pkg?
550 $return{balance} = $cust_main->balance;
551 $return{next_bill_date} = $cust_main->next_bill_date;
552 $return{next_bill_date_pretty} =
553 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
557 $return{countrydefault} = scalar($conf->config('countrydefault'));
559 $return{small_custview} =
560 small_custview( $cust_main,
561 $return{countrydefault},
565 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
567 $return{payby} = $cust_main->payby;
569 #none of these are terribly expensive if we want 'em...
570 for (@cust_main_editable_fields) {
571 $return{$_} = $cust_main->get($_);
573 #maybe a little more expensive, but it should be cached by now
574 for (@location_editable_fields) {
575 $return{$_} = $cust_main->bill_location->get($_);
576 $return{'ship_'.$_} = $cust_main->ship_location->get($_);
579 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
580 $return{payinfo} = $cust_main->paymask;
581 @return{'month', 'year'} = $cust_main->paydate_monthyear;
584 my @invoicing_list = $cust_main->invoicing_list;
585 $return{'invoicing_list'} =
586 join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list );
587 $return{'postal_invoicing'} =
588 0 < ( grep { $_ eq 'POST' } @invoicing_list );
590 if ( $session->{'svcnum'} ) {
591 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
592 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
593 $return{'svcnum'} = $session->{'svcnum'};
596 } elsif ( $session->{'svcnum'} ) { #no customer record
598 #uuh, not supproted yet... die?
599 return { 'error' => 'customer_info_short not yet supported as agent' };
603 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
607 return { 'error' => '',
608 'custnum' => $custnum,
613 sub billing_history {
616 my($context, $session, $custnum) = _custoragent_session_custnum($p);
617 return { 'error' => $session } if $context eq 'error';
619 return { 'error' => 'No customer' } unless $custnum;
621 my $search = { 'custnum' => $custnum };
622 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
623 my $cust_main = qsearchs('cust_main', $search )
624 or return { 'error' => "unknown custnum $custnum" };
628 if ( $session->{'pkgnum'} ) {
629 #$return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
630 #next_bill_date from cust_pkg?
631 return { 'error' => 'No history for package' };
634 $return{balance} = $cust_main->balance;
635 $return{next_bill_date} = $cust_main->next_bill_date;
636 $return{next_bill_date_pretty} =
637 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
642 my $conf = new FS::Conf;
644 if ( $conf->exists('selfservice-billing_history-line_items') ) {
646 foreach my $cust_bill ( $cust_main->cust_bill ) {
649 'type' => 'Line item',
650 'description' => $_->desc( $cust_main->locale ).
651 ( $_->sdate && $_->edate
652 ? ' '. time2str('%d-%b-%Y', $_->sdate).
653 ' To '. time2str('%d-%b-%Y', $_->edate)
656 'amount' => sprintf('%.2f', $_->setup + $_->recur ),
657 'date' => $cust_bill->_date,
658 'date_pretty' => time2str('%m/%d/%Y', $cust_bill->_date ),
660 foreach $cust_bill->cust_bill_pkg;
668 'description' => 'Invoice #'. $_->display_invnum,
669 'amount' => sprintf('%.2f', $_->charged ),
671 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
673 foreach $cust_main->cust_bill;
679 'description' => 'Payment', #XXX type
680 'amount' => sprintf('%.2f', 0 - $_->paid ),
682 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
684 foreach $cust_main->cust_pay;
688 'description' => 'Credit', #more info?
689 'amount' => sprintf('%.2f', 0 -$_->amount ),
691 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
693 foreach $cust_main->cust_credit;
697 'description' => 'Refund', #more info? type, like payment?
698 'amount' => $_->refund,
700 'date_pretty' => time2str('%m/%d/%Y', $_->_date ),
702 foreach $cust_main->cust_refund;
704 @history = sort { $b->{'date'} <=> $a->{'date'} } @history;
706 $return{'history'} = \@history;
714 my $session = _cache->get($p->{'session_id'})
715 or return { 'error' => "Can't resume session" }; #better error message
717 my $custnum = $session->{'custnum'}
718 or return { 'error' => "no customer record" };
720 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
721 or return { 'error' => "unknown custnum $custnum" };
723 my $new = new FS::cust_main { $cust_main->hash };
725 $new->set( $_ => $p->{$_} )
726 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
728 if ( exists($p->{address1}) ) {
729 my $bill_location = FS::cust_location->new({
730 map { $_ => $p->{$_} } @location_editable_fields
732 # if this is unchanged from before, cust_main::replace will ignore it
733 $new->set('bill_location' => $bill_location);
736 if ( exists($p->{ship_address1}) ) {
737 my $ship_location = FS::cust_location->new({
738 map { $_ => $p->{"ship_$_"} } @location_editable_fields
740 if ( !grep { length($p->{"ship_$_"}) } @location_editable_fields ) {
741 # Selfservice unfortunately tries to indicate "same as billing
742 # address" by sending all fields empty. Did this ever work?
743 $ship_location = $cust_main->bill_location;
745 $new->set('ship_location' => $ship_location);
747 # but if it hasn't been passed in at all, leave ship_location alone--
748 # DON'T change it to match bill_location.
751 if (exists($p->{'payby'})) {
752 $p->{'payby'} =~ /^([A-Z]{4})$/
753 or return { 'error' => "illegal_payby " . $p->{'payby'} };
757 my $conf = new FS::Conf;
759 if ( $payby =~ /^(CARD|DCRD)$/ ) {
761 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
763 if ( $new->payinfo eq $cust_main->paymask ) {
764 $new->payinfo($cust_main->payinfo);
766 $new->payinfo($p->{'payinfo'});
769 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
771 if ( $conf->exists('selfservice-onfile_require_cvv') ){
772 return { 'error' => 'CVV2 is required' } unless $p->{'paycvv'};
775 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
778 $p->{'payinfo1'} =~ /^([\dx]+)$/
779 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
781 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
782 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
784 $payinfo = $payinfo1. '@'. $payinfo2;
786 $new->payinfo( ($payinfo eq $cust_main->paymask)
787 ? $cust_main->payinfo
791 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
793 } elsif ( $payby =~ /^(BILL)$/ ) {
795 } elsif ( $payby ) { #notyet ready
796 return { 'error' => "unknown payby $payby" };
800 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
801 #false laziness with httemplate/edit/process/cust_main.cgi
802 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
803 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
805 @invoicing_list = $cust_main->invoicing_list;
808 my $error = $new->replace($cust_main, \@invoicing_list);
809 return { 'error' => $error } if $error;
812 return { 'error' => '' };
817 my $session = _cache->get($p->{'session_id'})
818 or return { 'error' => "Can't resume session" }; #better error message
824 my $conf = new FS::Conf;
825 use vars qw($payment_info); #cache for performance
826 unless ( $payment_info ) {
828 my %states = map { $_->state => 1 }
829 qsearch('cust_main_county', {
830 'country' => $conf->config('countrydefault') || 'US'
833 my %cust_paybys = map { $_ => 1 }
834 map { FS::payby->payby2payment($_) }
835 $conf->config('signup_server-payby');
837 my @cust_paybys = keys %cust_paybys;
841 #list all counties/states/countries
842 'cust_main_county' =>
843 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
845 #shortcut for one-country folks
847 [ sort { $a cmp $b } keys %states ],
849 'card_types' => card_types(),
851 'withcvv' => $conf->exists('selfservice-require_cvv'), #or enable optional cvv?
852 'require_cvv' => $conf->exists('selfservice-require_cvv'),
853 'onfile_require_cvv' => $conf->exists('selfservice-onfile_require_cvv'),
855 'paytypes' => [ @FS::cust_main::paytypes ],
857 'paybys' => [ $conf->config('signup_server-payby') ],
858 'cust_paybys' => \@cust_paybys,
860 'stateid_label' => FS::Msgcat::_gettext('stateid'),
861 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
863 'show_ss' => $conf->exists('show_ss'),
864 'show_stateid' => $conf->exists('show_stateid'),
865 'show_paystate' => $conf->exists('show_bankstate'),
867 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
869 'credit_card_surcharge_percentage' => scalar($conf->config('credit-card-surcharge-percentage')),
878 my %return = %$payment_info;
880 my $custnum = $session->{'custnum'};
882 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
883 or return { 'error' => "unknown custnum $custnum" };
885 $return{'hide_payment_fields'} = [
887 my $pg = payment_gateway($cust_main, $_);
888 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
889 } @{ $return{cust_paybys} }
892 $return{balance} = $cust_main->balance; #XXX pkg-balances?
894 $return{payname} = $cust_main->payname
895 || ( $cust_main->first. ' '. $cust_main->get('last') );
897 $return{$_} = $cust_main->bill_location->get($_)
898 for qw(address1 address2 city state zip);
900 $return{payby} = $cust_main->payby;
901 $return{stateid_state} = $cust_main->stateid_state;
903 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
904 $return{card_type} = cardtype($cust_main->payinfo);
905 $return{payinfo} = $cust_main->paymask;
907 @return{'month', 'year'} = $cust_main->paydate_monthyear;
911 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
912 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
913 $return{payinfo1} = $payinfo1;
914 $return{payinfo2} = $payinfo2;
915 $return{paytype} = $cust_main->paytype;
916 $return{paystate} = $cust_main->paystate;
917 $return{payname} = $cust_main->payname; # override 'first/last name' default from above, if any. Is instution-name here. (#15819)
920 if ( $conf->config('prepayment_discounts-credit_type') ) {
922 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
925 #doubleclick protection
927 $return{payunique} = "webui-MyAccount-$_date-$$-". rand() * 2**32; #new
928 $return{paybatch} = $return{payunique}; #back compat
930 return { 'error' => '',
936 #some false laziness with httemplate/process/payment.cgi - look there for
937 #ACH and CVV support stuff
939 sub validate_payment {
942 my $session = _cache->get($p->{'session_id'})
943 or return { 'error' => "Can't resume session" }; #better error message
945 my $custnum = $session->{'custnum'};
947 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
948 or return { 'error' => "unknown custnum $custnum" };
950 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
951 or return { 'error' => gettext('illegal_amount') };
953 return { error => 'Amount must be greater than 0' } unless $amount > 0;
955 #false laziness w/tr-amount_fee.html, but we don't want selfservice users
956 #changing the hidden form values
957 my $conf = new FS::Conf;
958 my $fee_display = $conf->config('selfservice_process-display') || 'add';
959 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
960 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
961 if ( $fee_display eq 'add'
963 and ! $fee_skip_first || scalar($cust_main->cust_pay)
966 my $fee_pkg = qsearchs('part_pkg', { pkgpart=>$fee_pkgpart } );
967 $amount = sprintf('%.2f', $amount + $fee_pkg->option('setup_fee') );
970 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
971 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
972 my $discount_term = $1;
974 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
975 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
978 $p->{'payunique'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
979 or return { 'error' => gettext('illegal_text'). " payunique: ". $p->{'payunique'} };
982 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
983 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
986 $payunique = $paybatch if ! length($payunique) && length($paybatch);
988 $p->{'payby'} ||= 'CARD';
989 $p->{'payby'} =~ /^([A-Z]{4})$/
990 or return { 'error' => "illegal_payby " . $p->{'payby'} };
993 #false laziness w/process/payment.cgi
996 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
998 $p->{'payinfo1'} =~ /^([\dx]+)$/
999 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1001 $p->{'payinfo2'} =~ /^([\dx]+)$/
1002 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1004 $payinfo = $payinfo1. '@'. $payinfo2;
1006 $payinfo = $cust_main->payinfo
1007 if $cust_main->paymask eq $payinfo;
1009 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
1011 $payinfo = $p->{'payinfo'};
1015 #more intelligent matching will be needed here if you change
1016 #card_masking_method and don't remove existing paymasks
1017 if ( $cust_main->paymask eq $payinfo ) {
1018 $payinfo = $cust_main->payinfo;
1022 $payinfo =~ s/\D//g;
1023 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
1024 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1028 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1029 return { 'error' => gettext('unknown_card_type') }
1030 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
1032 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
1033 if ( cardtype($payinfo) eq 'American Express card' ) {
1034 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
1035 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
1038 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
1039 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
1042 } elsif ( $conf->exists('selfservice-onfile_require_cvv') ) {
1043 return { 'error' => 'CVV2 is required' };
1044 } elsif ( !$onfile && $conf->exists('selfservice-require_cvv') ) {
1045 return { 'error' => 'CVV2 is required' };
1049 die "unknown payby $payby";
1052 my %payby2fields = (
1053 'CARD' => [ qw( paystart_month paystart_year payissue payip
1054 address1 address2 city state zip country ) ],
1055 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
1059 $card_type = cardtype($payinfo) if $payby eq 'CARD';
1062 'cust_main' => $cust_main, #XXX or just custnum??
1063 'amount' => sprintf('%.2f', $amount),
1065 'payinfo' => $payinfo,
1066 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
1067 'card_type' => $card_type,
1068 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
1069 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
1070 'month' => $p->{'month'},
1071 'year' => $p->{'year'},
1072 'payname' => $payname,
1073 'payunique' => $payunique,
1074 'paybatch' => $paybatch,
1075 'paycvv' => $paycvv,
1076 'payname' => $payname,
1077 'discount_term' => $discount_term,
1078 'pkgnum' => $session->{'pkgnum'},
1079 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
1089 my $validate = validate_payment($p);
1090 return $validate if $validate->{'error'};
1092 my $conf = new FS::Conf;
1093 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
1094 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
1096 +{ map { $_=>$validate->{$_} }
1097 qw( card_type paymask payname paydate_pretty month year amount
1098 address1 address2 city state zip country
1104 sub process_stored_payment {
1107 my $session_id = $p->{'session_id'};
1109 my $payment_info = _cache->get( "payment_$session_id" )
1110 or return { 'error' => "Can't resume session" }; #better error message
1112 do_process_payment($payment_info);
1116 sub process_payment {
1119 my $payment_info = validate_payment($p);
1120 return $payment_info if $payment_info->{'error'};
1122 do_process_payment($payment_info);
1126 sub do_process_payment {
1127 my $validate = shift;
1129 my $cust_main = $validate->{'cust_main'};
1131 my $amount = delete $validate->{'amount'};
1134 my $payby = delete $validate->{'payby'};
1136 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
1140 'paynum_ref' => \$paynum,
1143 return { 'error' => $error } if $error;
1145 #no error, so order the fee package if applicable...
1146 my $conf = new FS::Conf;
1147 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
1148 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
1150 if ( $fee_pkgpart and ! $fee_skip_first || scalar($cust_main->cust_pay) ) {
1152 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $fee_pkgpart };
1154 $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
1155 return { 'error' => "payment processed successfully, but error ordering fee: $error" }
1158 #and generate an invoice for it now too
1159 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1160 return { 'error' => "payment processed and fee ordered sucessfully, but error billing fee: $error" }
1165 $cust_main->apply_payments;
1167 if ( $validate->{'save'} ) {
1168 my $new = new FS::cust_main { $cust_main->hash };
1169 if ($payby eq 'CARD' || $payby eq 'DCRD') {
1170 $new->set( $_ => $validate->{$_} )
1171 foreach qw( payname paystart_month paystart_year payissue payip );
1172 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
1174 my $bill_location = FS::cust_location->new({
1175 map { $_ => $validate->{$_} }
1176 qw(address1 address2 city state country zip)
1178 $new->set('bill_location' => $bill_location);
1179 # but don't allow the service address to change this way.
1181 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
1182 $new->set( $_ => $validate->{$_} )
1183 foreach qw( payname payip paytype paystate
1184 stateid stateid_state );
1185 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
1187 $new->set( 'payinfo' => $cust_main->card_token || $validate->{'payinfo'} );
1188 $new->set( 'paydate' => $validate->{'paydate'} );
1189 my $error = $new->replace($cust_main);
1191 #no, this causes customers to process their payments again
1192 #return { 'error' => $error };
1193 #XXX just warn verosely for now so i can figure out how these happen in
1194 # the first place, eventually should redirect them to the "change
1195 #address" page but indicate the payment did process??
1196 delete($validate->{'payinfo'}); #don't want to log this!
1197 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
1198 "NEW: ". Dumper($new)."\n".
1199 "OLD: ". Dumper($cust_main)."\n".
1200 "PACKET: ". Dumper($validate)."\n";
1208 my $receipt_html = '';
1210 # currently supported for realtime CC only; send receipt data to SS
1211 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
1214 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
1217 <TD ALIGN="right">Payment#</TD>
1218 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
1222 <TD ALIGN="right">Date</TD>
1224 <TD BGCOLOR="#FFFFFF"><B>! .
1225 time2str("%a %b %o, %Y %r", $cust_pay->_date)
1231 <TD ALIGN="right">Amount</TD>
1232 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
1237 <TD ALIGN="right">Payment method</TD>
1238 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
1251 'amount' => sprintf('%.2f', $cust_pay->paid),
1252 'date' => $cust_pay->_date,
1253 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
1254 'time_pretty' => time2str('%T', $cust_pay->_date),
1255 'auth_num' => $cust_pay->auth,
1256 'order_num' => $cust_pay->order_number,
1257 'receipt_html' => $receipt_html,
1264 'receipt_html' => '',
1271 sub realtime_collect {
1274 my $session = _cache->get($p->{'session_id'})
1275 or return { 'error' => "Can't resume session" }; #better error message
1277 my $custnum = $session->{'custnum'};
1279 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1280 or return { 'error' => "unknown custnum $custnum" };
1283 if ( $p->{'amount'} ) {
1284 $amount = $p->{'amount'};
1286 elsif ( $session->{'pkgnum'} ) {
1287 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1290 $amount = $cust_main->balance;
1293 my $error = $cust_main->realtime_collect(
1294 'method' => $p->{'method'},
1295 'amount' => $amount,
1296 'pkgnum' => $session->{'pkgnum'},
1297 'session_id' => $p->{'session_id'},
1301 return { 'error' => $error } unless ref( $error );
1303 return { 'error' => '', amount => $amount, %$error };
1306 sub start_thirdparty {
1308 my $session = _cache->get($p->{'session_id'})
1309 or return { 'error' => "Can't resume session" }; #better error message
1310 my $custnum = $session->{'custnum'};
1311 my $cust_main = FS::cust_main->by_key($custnum);
1313 my $amount = $p->{'amount'}
1314 or return { error => 'no amount' };
1316 my $result = $cust_main->create_payment(
1317 'method' => $p->{'method'},
1318 'amount' => $p->{'amount'},
1319 'pkgnum' => $session->{'pkgnum'},
1320 'session_id' => $p->{'session_id'},
1323 if ( ref($result) ) { # hashref or error
1326 return { error => $result };
1330 sub finish_thirdparty {
1332 my $session_id = delete $p->{'session_id'};
1333 my $session = _cache->get($session_id)
1334 or return { 'error' => "Can't resume session" };
1335 my $custnum = $session->{'custnum'};
1336 my $cust_main = FS::cust_main->by_key($custnum);
1338 if ( $p->{_cancel} ) {
1339 # customer backed out of making a payment
1340 return $cust_main->cancel_payment( $session_id );
1342 my $result = $cust_main->execute_payment( $session_id, %$p );
1343 if ( ref($result) ) {
1346 return { error => $result };
1350 sub process_payment_order_pkg {
1353 my $hr = process_payment($p);
1354 return $hr if $hr->{'error'};
1359 sub process_payment_order_renew {
1362 my $hr = process_payment($p);
1363 return $hr if $hr->{'error'};
1368 sub process_prepay {
1372 my $session = _cache->get($p->{'session_id'})
1373 or return { 'error' => "Can't resume session" }; #better error message
1377 my $custnum = $session->{'custnum'};
1379 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1380 or return { 'error' => "unknown custnum $custnum" };
1382 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1383 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1391 return { 'error' => $error } if $error;
1393 return { 'error' => '',
1394 'amount' => $amount,
1395 'seconds' => $seconds,
1396 'duration' => duration_exact($seconds),
1397 'upbytes' => $upbytes,
1398 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1399 'downbytes' => $downbytes,
1400 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1401 'totalbytes'=> $totalbytes,
1402 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1409 my $session = _cache->get($p->{'session_id'})
1410 or return { 'error' => "Can't resume session" }; #better error message
1412 my $custnum = $session->{'custnum'};
1414 my $invnum = $p->{'invnum'};
1416 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1417 'custnum' => $custnum } )
1418 or return { 'error' => "Can't find invnum" };
1422 return { 'error' => '',
1423 'invnum' => $invnum,
1424 'invoice_text' => join('', $cust_bill->print_text ),
1425 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1432 my $session = _cache->get($p->{'session_id'})
1433 or return { 'error' => "Can't resume session" }; #better error message
1435 my $custnum = $session->{'custnum'};
1437 my $invnum = $p->{'invnum'};
1439 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1440 'custnum' => $custnum } )
1441 or return { 'error' => "Can't find invnum" };
1445 return { 'error' => '',
1446 'invnum' => $invnum,
1447 'invoice_pdf' => $cust_bill->print_pdf({
1448 'unsquelch_cdr' => 1,
1449 'locale' => $p->{'locale'},
1455 sub legacy_invoice {
1457 my $session = _cache->get($p->{'session_id'})
1458 or return { 'error' => "Can't resume session" }; #better error message
1460 my $custnum = $session->{'custnum'};
1462 my $legacyinvnum = $p->{'legacyinvnum'};
1465 'legacyinvnum' => $legacyinvnum,
1466 'custnum' => $custnum,
1469 my $legacy_cust_bill =
1470 qsearchs('legacy_cust_bill', { %hash, 'locale' => $p->{'locale'} } )
1471 || qsearchs('legacy_cust_bill', \%hash )
1472 or return { 'error' => "Can't find legacyinvnum" };
1476 return { 'error' => '',
1477 'legacyinvnum' => $legacyinvnum,
1478 'legacyid' => $legacy_cust_bill->legacyid,
1479 'invoice_html' => $legacy_cust_bill->content_html,
1484 sub legacy_invoice_pdf {
1486 my $session = _cache->get($p->{'session_id'})
1487 or return { 'error' => "Can't resume session" }; #better error message
1489 my $custnum = $session->{'custnum'};
1491 my $legacyinvnum = $p->{'legacyinvnum'};
1493 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1494 'legacyinvnum' => $legacyinvnum,
1495 'custnum' => $custnum,
1496 }) or return { 'error' => "Can't find legacyinvnum" };
1500 return { 'error' => '',
1501 'legacyinvnum' => $legacyinvnum,
1502 'legacyid' => $legacy_cust_bill->legacyid,
1503 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1511 #sessioning for this? how do we get the session id to the backend invoice
1512 # template so it can add it to the link, blah
1515 if ( $p->{'invnum'} ) {
1516 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1517 or return { 'error' => 'unknown invnum' };
1518 $agentnum = $cust_bill->cust_main->agentnum;
1521 my $templatename = $p->{'template'} || $p->{'templatename'};
1523 #false laziness-ish w/view/cust_bill-logo.cgi
1525 my $conf = new FS::Conf;
1526 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1527 $templatename = "_$1";
1532 my $filename = "logo$templatename.png";
1534 return { 'error' => '',
1535 'logo' => $conf->config_binary($filename, $agentnum),
1536 'content_type' => 'image/png', #should allow gif, jpg too
1543 my $session = _cache->get($p->{'session_id'})
1544 or return { 'error' => "Can't resume session" }; #better error message
1546 my $custnum = $session->{'custnum'};
1548 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1549 or return { 'error' => "unknown custnum $custnum" };
1551 my $conf = new FS::Conf;
1553 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1555 my @cust_bill = grep ! $_->hide, $cust_main->cust_bill;
1559 return { 'error' => '',
1560 'balance' => $cust_main->balance,
1563 my $owed = $_->owed;
1565 +{ 'invnum' => $_->invnum,
1566 '_date' => $_->_date,
1567 'date' => time2str("%b %o, %Y", $_->_date),
1568 'date_short' => time2str("%m-%d-%Y", $_->_date),
1569 'previous' => sprintf('%.2f', ($_->previous)[0]),
1570 'charged' => sprintf('%.2f', $_->charged),
1571 'owed' => sprintf('%.2f', $owed),
1572 'balance' => sprintf('%.2f', $balance),
1577 'legacy_invoices' => [
1579 +{ 'legacyinvnum' => $_->legacyinvnum,
1580 'legacyid' => $_->legacyid,
1581 '_date' => $_->_date,
1582 'date' => time2str("%b %o, %Y", $_->_date),
1583 'date_short' => time2str("%m-%d-%Y", $_->_date),
1584 'charged' => sprintf('%.2f', $_->charged),
1585 'has_content' => ( length($_->content_pdf)
1586 || length($_->content_html) ),
1596 my $session = _cache->get($p->{'session_id'})
1597 or return { 'error' => "Can't resume session" }; #better error message
1599 my $custnum = $session->{'custnum'};
1601 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1602 or return { 'error' => "unknown custnum $custnum" };
1604 my @errors = $cust_main->cancel( 'quiet'=>1 );
1606 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1608 return { 'error' => $error };
1615 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1616 return { 'error' => $session } if $context eq 'error';
1618 my $search = { 'custnum' => $custnum };
1619 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1620 my $cust_main = qsearchs('cust_main', $search )
1621 or return { 'error' => "unknown custnum $custnum" };
1623 my $conf = new FS::Conf;
1624 my $immutable = $conf->exists('selfservice_immutable-package');
1626 # the duplication below is necessary:
1627 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1628 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1629 # non-backwards-compatible change breaking the software of anyone using the API
1630 # instead of the stock selfservice
1631 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1632 if( $conf->exists('selfservice_server-view-wholesale') ) {
1633 return { 'svcnum' => $session->{'svcnum'},
1634 'custnum' => $custnum,
1635 'cust_pkg' => [ map {
1637 immutable => $immutable,
1638 part_pkg => [ map $_->hashref, $_->part_pkg ],
1640 [ map $_->hashref, $_->available_part_svc ],
1642 [ map { my $ref = { $_->hash,
1643 label => [ $_->label ],
1645 $ref->{_password} = $_->svc_x->_password
1646 if $context eq 'agent'
1647 && $conf->exists('agent-showpasswords')
1648 && $_->part_svc->svcdb eq 'svc_acct';
1653 } $cust_main->cust_pkg
1656 small_custview( $cust_main, $conf->config('countrydefault') ),
1657 'wholesale_view' => 1,
1658 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1659 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1660 'lnp' => $conf->exists('svc_phone-lnp'),
1664 { 'svcnum' => $session->{'svcnum'},
1665 'custnum' => $custnum,
1666 'cust_pkg' => [ map {
1667 my $primary_cust_svc = $_->primary_cust_svc;
1670 immutable => $immutable,
1671 pkg_label => $_->pkg_locale,
1672 status => $_->status,
1673 statuscolor => $_->statuscolor,
1675 [ map { $_->hashref }
1676 grep { $_->selfservice_access ne 'hidden' }
1677 $_->available_part_svc
1680 [ map { my $ref = { $_->hash,
1681 label => [ $_->label ],
1683 $ref->{_password} = $_->svc_x->_password
1684 if $context eq 'agent'
1685 && $conf->exists('agent-showpasswords')
1686 && $_->part_svc->svcdb eq 'svc_acct';
1687 $ref->{svchash} = { $_->svc_x->hash } if
1688 $_->part_svc->svcdb eq 'svc_phone';
1689 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1690 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1693 grep { $_->part_svc->selfservice_access ne 'hidden' }
1698 ? { $primary_cust_svc->hash,
1699 label => [ $primary_cust_svc->label ],
1700 finger => $primary_cust_svc->svc_x->finger, #uuh
1701 $primary_cust_svc->part_svc->hash,
1705 } $cust_main->ncancelled_pkgs
1708 small_custview( $cust_main, $conf->config('countrydefault') ),
1709 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1717 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1718 return { 'error' => $session } if $context eq 'error';
1720 my $conf = new FS::Conf;
1722 my $hide_usage = $conf->exists('selfservice_hide-usage') ? 1 : 0;
1723 my $search = { 'custnum' => $custnum };
1724 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1725 my $cust_main = qsearchs('cust_main', $search )
1726 or return { 'error' => "unknown custnum $custnum" };
1728 my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
1729 if ( ! $pkgnum && $p->{'svcnum'} ) {
1730 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
1731 $pkgnum = $cust_svc->pkgnum if $cust_svc;
1735 my @cust_pkg_usage = ();
1736 foreach my $cust_pkg ( $p->{'ncancelled'}
1737 ? $cust_main->ncancelled_pkgs
1738 : $cust_main->unsuspended_pkgs ) {
1739 next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
1740 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1741 push @cust_pkg_usage, $cust_pkg->cust_pkg_usage;
1744 @cust_svc = grep { $_->part_svc->selfservice_access ne 'hidden' } @cust_svc;
1747 foreach (@cust_pkg_usage) {
1748 my $part = $_->part_pkg_usage;
1749 my $tag = $part->description . ($part->shared ? 1 : 0);
1750 my $row = $usage_pools{$tag}
1751 ||= [ $part->description, 0, 0, $part->shared ? 1 : 0 ];
1752 $row->[1] += sprintf('%.1f', $_->minutes); # minutes remaining
1753 $row->[2] += $part->minutes; # minutes total
1755 } # otherwise just leave them empty
1757 if ( $p->{'svcdb'} ) {
1758 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1760 : ref($p->{'svcdb'}) eq 'ARRAY'
1761 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1762 : { $p->{'svcdb'} => 1 };
1763 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1766 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1769 my @svcs; # stuff to return to the client
1770 foreach my $cust_svc (@cust_svc) {
1771 my $svc_x = $cust_svc->svc_x;
1772 my($label, $value) = $cust_svc->label;
1773 my $part_svc = $cust_svc->part_svc;
1774 my $svcdb = $part_svc->svcdb;
1775 my $cust_pkg = $cust_svc->cust_pkg;
1776 my $part_pkg = $cust_pkg->part_pkg;
1779 'svcnum' => $cust_svc->svcnum,
1780 'display_svcnum' => $cust_svc->display_svcnum,
1784 'pkg_label' => $cust_pkg->pkg_locale,
1785 'pkg_status' => $cust_pkg->status,
1786 'readonly' => ($part_svc->selfservice_access eq 'readonly'),
1789 # would it make sense to put this in a svc_* method?
1791 if ( $svcdb eq 'svc_acct' ) {
1792 foreach (qw(username email finger seconds)) {
1793 $hash{$_} = $svc_x->$_;
1799 'upbytes' => display_bytecount($svc_x->upbytes),
1800 'downbytes' => display_bytecount($svc_x->downbytes),
1801 'totalbytes' => display_bytecount($svc_x->totalbytes),
1803 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1804 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1805 'recharge_upbytes' =>
1806 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1807 'recharge_downbytes' =>
1808 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1809 'recharge_totalbytes' =>
1810 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1815 } elsif ( $svcdb eq 'svc_dsl' ) {
1817 $hash{'phonenum'} = $svc_x->phonenum;
1818 if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
1819 $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
1820 $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
1823 $hash{'name'} = $cust_main->name;
1825 # no usage to hide here
1827 } elsif ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
1829 # could potentially show lots of things...
1830 $hash{'outbound'} = 1;
1831 $hash{'inbound'} = 0;
1832 if ( $svcdb eq 'svc_phone' ) {
1833 if ( $part_pkg->plan eq 'voip_inbound' ) {
1834 $hash{'outbound'} = 0;
1835 $hash{'inbound'} = 1;
1836 } elsif ( $part_pkg->option('selfservice_inbound_format')
1837 or $conf->config('selfservice-default_inbound_cdr_format')
1839 $hash{'inbound'} = 1;
1842 foreach (qw(inbound outbound)) {
1843 # hmm...we can't filter by status here, because there might
1844 # not be cdr_terminations at all. have to go by date.
1845 # find all since the last bill date.
1846 # XXX cdr types? we are going to need them.
1848 my $sum_cdr = $svc_x->sum_cdrs(
1849 'inbound' => ( $_ eq 'inbound' ? 1 : 0 ),
1850 'begin' => ($cust_pkg->last_bill || 0),
1852 'disable_charged_party' => 1,
1854 $hash{$_} = $sum_cdr->hashref;
1857 } # not hiding usage
1861 } # foreach $cust_svc
1864 'svcnum' => $session->{'svcnum'},
1865 'custnum' => $custnum,
1866 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1867 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1870 map { $usage_pools{$_} }
1874 'hide_usage' => $hide_usage,
1879 sub _customer_svc_x {
1880 my($custnum, $svcnum, $table) = (shift, shift, shift);
1881 my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
1883 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1884 my $search = " AND custnum = $1";
1885 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1888 'table' => ($table || 'svc_acct'),
1889 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1890 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
1891 #'LEFT JOIN cust_main USING ( custnum ) ',
1892 'hashref' => $hashref,
1893 'extra_sql' => $search, #important
1898 sub svc_status_html {
1901 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1902 return { 'error' => $session } if $context eq 'error';
1904 #XXX only svc_dsl for now
1905 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
1906 or return { 'error' => "Service not found" };
1908 my $html = $svc_x->getstatus_html;
1910 return { 'html' => $html };
1914 sub svc_status_hash {
1917 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1918 return { 'error' => $session } if $context eq 'error';
1920 #XXX only svc_acct for now
1921 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1922 or return { 'error' => "Service not found" };
1924 my ( $html, $hashref ) = $svc_x->export_getstatus;
1929 sub set_svc_status_hash { _svc_method_X(shift, 'export_setstatus') }
1930 sub set_svc_status_listadd { _svc_method_X(shift, 'export_setstatus_listadd') }
1931 sub set_svc_status_listdel { _svc_method_X(shift, 'export_setstatus_listdel') }
1932 sub set_svc_status_vacationadd { _svc_method_X(shift, 'export_setstatus_vacationadd') }
1933 sub set_svc_status_vacationdel { _svc_method_X(shift, 'export_setstatus_vacationdel') }
1936 my( $p, $method ) = @_;
1938 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1939 return { 'error' => $session } if $context eq 'error';
1941 #XXX only svc_acct for now
1942 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
1943 or return { 'error' => "Service not found" };
1945 warn "$method ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
1947 my $error = $svc_x->$method($p); #$p? returns error?
1948 return { 'error' => $error } if $error;
1950 return {}; #? { 'error' => '' }
1954 sub acct_forward_info {
1957 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1958 return { 'error' => $session } if $context eq 'error';
1960 my $svc_forward = _customer_svc_x( $custnum,
1961 { 'srcsvc' => $p->{'svcnum'} },
1964 or return { 'error' => '',
1968 return { 'error' => '',
1969 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
1974 sub process_acct_forward {
1976 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1977 return { 'error' => $session } if $context eq 'error';
1979 my $old = _customer_svc_x( $custnum,
1980 { 'srcsvc' => $p->{'svcnum'} },
1984 if ( $p->{'dst'} eq '' ) {
1986 my $error = $old->delete;
1987 return { 'error' => $error };
1989 return { 'error' => '' };
1992 my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
1993 'dst' => $p->{'dst'},
1998 $new->svcnum($old->svcnum);
1999 my $cust_svc = $old->cust_svc;
2000 $new->svcpart($old->svcpart);
2001 $new->pkgnuym($old->pkgnum);
2002 $error = $new->replace($old);
2004 my $conf = new FS::Conf;
2005 $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
2007 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
2008 or return { 'error' => 'No service' }; #how would we even get here?
2010 $new->pkgnum( $svc_acct->cust_svc->pkgnum );
2012 $error = $new->insert;
2015 return { 'error' => $error };
2019 sub list_dsl_devices {
2022 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2023 return { 'error' => $session } if $context eq 'error';
2025 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2026 or return { 'error' => "Service not found" };
2029 'devices' => [ map {
2030 +{ 'mac_addr' => $_->mac_addr };
2031 } $svc_dsl->dsl_device
2037 sub add_dsl_device {
2040 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2041 return { 'error' => $session } if $context eq 'error';
2043 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2044 or return { 'error' => "Service not found" };
2046 return { 'error' => 'No MAC address supplied' }
2047 unless length($p->{'mac_addr'});
2049 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
2050 'mac_addr' => scalar($p->{'mac_addr'}),
2052 my $error = $dsl_device->insert;
2053 return { 'error' => $error };
2057 sub delete_dsl_device {
2060 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2061 return { 'error' => $session } if $context eq 'error';
2063 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2064 or return { 'error' => "Service not found" };
2066 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
2067 'mac_addr' => scalar($p->{'mac_addr'}),
2070 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
2072 my $error = $dsl_device->delete;
2073 return { 'error' => $error };
2079 _usage_details( \&_port_graph, $p,
2080 'svcdb' => 'svc_port',
2085 my($svc_port, $begin, $end) = @_;
2087 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
2088 push @usage, { 'png' => $pngOrError };
2092 sub _list_svc_usage {
2093 my($svc_acct, $begin, $end) = @_;
2095 foreach my $part_export (
2096 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
2097 qw( sqlradius sqlradius_withdomain )
2099 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
2104 sub list_svc_usage {
2105 _usage_details(\&_list_svc_usage, @_);
2108 sub _list_support_usage {
2109 my($svc_acct, $begin, $end) = @_;
2111 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
2112 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
2114 push @usage, { 'seconds' => $_->seconds,
2115 'support' => $_->support,
2116 '_date' => $_->_date,
2117 'id' => $_->transaction_id,
2118 'creator' => $_->creator,
2119 'subject' => $_->subject,
2120 'status' => $_->status,
2121 'ticketid' => $_->ticketid,
2127 sub list_support_usage {
2128 _usage_details(\&_list_support_usage, @_);
2131 sub _list_cdr_usage {
2132 # XXX CDR type support...
2133 # XXX any way to do a paged search on this?
2134 # we have to return the results all at once...
2135 my($svc_x, $begin, $end, %opt) = @_;
2136 map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
2140 'disable_charged_party' => 1,
2145 sub list_cdr_usage {
2147 _usage_details( \&_list_cdr_usage, $p );
2150 sub _usage_details {
2151 my($callback, $p, %opt) = @_;
2152 my $conf = FS::Conf->new;
2154 if ( $conf->exists('selfservice_hide-usage') ) {
2155 return { 'error' => 'Viewing usage is not allowed.' };
2158 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2159 return { 'error' => $session } if $context eq 'error';
2161 my $search = { 'svcnum' => $p->{'svcnum'} };
2162 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2164 my $cust_svc = qsearchs( 'cust_svc', $search );
2165 return { 'error' => 'No service selected in list_svc_usage' }
2168 my $svc_x = $cust_svc->svc_x;
2169 my $svcdb = $svc_x->table;
2170 my $cust_pkg = $cust_svc->cust_pkg;
2171 my $freq = $cust_pkg->part_pkg->freq;
2174 if ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
2176 if ( $p->{inbound} ) {
2177 $format = $cust_pkg->part_pkg->option('selfservice_inbound_format')
2178 || $conf->config('selfservice-default_inbound_cdr_format')
2179 || 'source_default';
2180 $callback_opt{inbound} = 1;
2182 $format = $cust_pkg->part_pkg->option('selfservice_format')
2183 || $conf->config('selfservice-default_cdr_format')
2187 $callback_opt{format} = $format;
2188 $callback_opt{use_clid} = 1;
2189 $header = [ split(',', FS::cdr::invoice_header($format) ) ];
2192 my $start = $cust_pkg->setup;
2193 #my $end = $cust_pkg->bill; # or time?
2196 unless ( $p->{beginning} ) {
2197 $p->{beginning} = $cust_pkg->last_bill;
2198 $p->{ending} = $end;
2201 die "illegal beginning" if $p->{beginning} !~ /^\d*$/;
2202 die "illegal ending" if $p->{ending} !~ /^\d*$/;
2204 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
2208 if ( $conf->exists('selfservice-hide_cdr_price') ) {
2209 # ugly kludge, I know
2210 my ($delete_col) = grep { $header->[$_] eq 'Price' } (0..scalar(@$header));
2211 if (defined $delete_col) {
2212 delete($_->[$delete_col]) foreach ($header, @usage);
2216 #kinda false laziness with FS::cust_main::bill, but perhaps
2217 #we should really change this bit to DateTime and DateTime::Duration
2219 #change this bit to use Date::Manip? CAREFUL with timezones (see
2220 # mailing list archive)
2221 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
2222 (localtime($p->{ending}) )[0,1,2,3,4,5];
2223 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
2224 (localtime($p->{beginning}) )[0,1,2,3,4,5];
2226 if ( $freq =~ /^\d+$/ ) {
2228 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
2230 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
2231 } elsif ( $freq =~ /^(\d+)w$/ ) {
2233 $nmday += $weeks * 7;
2234 $pmday -= $weeks * 7;
2235 } elsif ( $freq =~ /^(\d+)d$/ ) {
2239 } elsif ( $freq =~ /^(\d+)h$/ ) {
2244 return { 'error' => "unparsable frequency: ". $freq };
2247 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
2248 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
2252 'svcnum' => $p->{svcnum},
2253 'beginning' => $p->{beginning},
2254 'ending' => $p->{ending},
2255 'inbound' => $p->{inbound},
2256 'previous' => ($previous > $start) ? $previous : $start,
2257 'next' => ($next < $end) ? $next : $end,
2258 'header' => $header,
2266 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2267 return { 'error' => $session } if $context eq 'error';
2269 my $search = { 'custnum' => $custnum };
2270 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2271 my $cust_main = qsearchs('cust_main', $search )
2272 or return { 'error' => "unknown custnum $custnum" };
2274 my $status = $cust_main->status;
2276 my %order_pkg_options = ();
2277 if ( $p->{locationnum} > 0 ) {
2278 $order_pkg_options{locationnum} = delete($p->{locationnum});
2279 } elsif ( $p->{address1} ) {
2280 $order_pkg_options{'cust_location'} = new FS::cust_location {
2281 map { $_ => $p->{$_} }
2282 qw( address1 address2 city county state zip country )
2286 #false laziness w/ClientAPI/Signup.pm
2288 my $cust_pkg = new FS::cust_pkg ( {
2289 'custnum' => $custnum,
2290 'pkgpart' => $p->{'pkgpart'},
2291 'quantity' => $p->{'quantity'} || 1,
2293 my $error = $cust_pkg->check;
2294 return { 'error' => $error } if $error;
2297 unless ( $p->{'svcpart'} eq 'none' ) {
2301 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
2303 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2304 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
2305 $svcdb = $part_svc->svcdb;
2307 $svcdb = 'svc_acct';
2309 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
2312 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
2313 'svc_domain' => [ qw( domain ) ],
2314 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
2315 'svc_external' => [ qw( id title ) ],
2316 'svc_pbx' => [ qw( id title ) ],
2319 my $svc_x = "FS::$svcdb"->new( {
2320 'svcpart' => $svcpart,
2321 map { $_ => $p->{$_} } @{$fields{$svcdb}}
2324 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
2327 while ( length($p->{"snarf_machine$snarfnum"}) ) {
2328 my $acct_snarf = new FS::acct_snarf ( {
2329 'machine' => $p->{"snarf_machine$snarfnum"},
2330 'protocol' => $p->{"snarf_protocol$snarfnum"},
2331 'username' => $p->{"snarf_username$snarfnum"},
2332 '_password' => $p->{"snarf_password$snarfnum"},
2335 push @acct_snarf, $acct_snarf;
2337 $svc_x->child_objects( \@acct_snarf );
2340 my $y = $svc_x->setdefault; # arguably should be in new method
2341 return { 'error' => $y } if $y && !ref($y);
2343 $error = $svc_x->check;
2344 return { 'error' => $error } if $error;
2350 $error = $cust_main->order_pkg(
2351 'cust_pkg' => $cust_pkg,
2356 return { 'error' => $error } if $error;
2358 my $conf = new FS::Conf;
2359 if ( $conf->exists('signup_server-realtime') ) {
2361 my $bill_error = _do_bop_realtime( $cust_main, $status );
2364 $cust_pkg->cancel('quiet'=>1);
2367 $cust_pkg->reexport;
2371 $cust_pkg->reexport;
2374 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
2376 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
2383 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2384 return { 'error' => $session } if $context eq 'error';
2386 my $conf = new FS::Conf;
2387 my $immutable = $conf->exists('selfservice_immutable-package');
2388 return { 'error' => "Package modification disabled" } if $immutable;
2390 my $search = { 'custnum' => $custnum };
2391 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2392 my $cust_main = qsearchs('cust_main', $search )
2393 or return { 'error' => "unknown custnum $custnum" };
2395 my $status = $cust_main->status;
2396 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
2397 or return { 'error' => "unknown package $p->{pkgnum}" };
2399 #if someone does need self-service package change of suspended packages,
2400 # figure out how to be more discriminating
2401 return { error=>"Can't change a suspended package", pkgnum=>$cust_pkg->pkgnum}
2402 if $cust_pkg->status eq 'suspended';
2404 my $err_or_cust_pkg = $cust_pkg->change( 'pkgpart' => $p->{'pkgpart'},
2405 'quantity' => $p->{'quantity'} || 1,
2408 return { error=>$err_or_cust_pkg, pkgnum=>$cust_pkg->pkgnum }
2409 unless ref($err_or_cust_pkg);
2411 if ( $conf->exists('signup_server-realtime') ) {
2413 my $bill_error = _do_bop_realtime( $cust_main, $status, 'no_credit'=>1 );
2416 $err_or_cust_pkg->suspend;
2419 $err_or_cust_pkg->reexport;
2423 $err_or_cust_pkg->reexport;
2426 return { error => '', pkgnum => $cust_pkg->pkgnum };
2430 sub order_recharge {
2433 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2434 return { 'error' => $session } if $context eq 'error';
2436 my $search = { 'custnum' => $custnum };
2437 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2438 my $cust_main = qsearchs('cust_main', $search )
2439 or return { 'error' => "unknown custnum $custnum" };
2441 my $status = $cust_main->status;
2442 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
2443 or return { 'error' => "unknown service " . $p->{'svcnum'} };
2445 my $svc_x = $cust_svc->svc_x;
2446 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
2449 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
2450 qw ( recharge_seconds recharge_upbytes recharge_downbytes
2451 recharge_totalbytes );
2452 my $amount = $part_pkg->option('recharge_amount', 1);
2454 my ($l, $v, $d) = $cust_svc->label; # blah
2455 my $pkg = "Recharge $v";
2457 my $bill_error = $cust_main->charge($amount, $pkg,
2458 "time: $vhash{seconds}, up: $vhash{upbytes}," .
2459 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
2460 $part_pkg->taxclass); #meh
2462 my $conf = new FS::Conf;
2463 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
2465 $bill_error = _do_bop_realtime( $cust_main, $status );
2470 my $error = $svc_x->recharge (\%vhash);
2471 return { 'error' => $error } if $error;
2475 my $error = $bill_error;
2476 $error ||= $svc_x->recharge (\%vhash);
2477 return { 'error' => $error } if $error;
2480 return { error => '', svc => $cust_svc->part_svc->svc };
2484 sub _do_bop_realtime {
2485 my ($cust_main, $status, %opt) = @_;
2487 my $old_balance = $cust_main->balance;
2489 my $bill_error = $cust_main->bill
2490 || $cust_main->apply_payments_and_credits;
2492 $bill_error ||= $cust_main->realtime_collect('selfservice' => 1)
2493 if $cust_main->payby =~ /^(CARD|CHEK)$/;
2495 if ( $cust_main->balance > $old_balance
2496 && $cust_main->balance > 0
2497 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/
2498 || $status eq 'suspended'
2502 unless ( $opt{'no_credit'} ) {
2503 #this makes sense. credit is "un-doing" the invoice
2504 my $conf = new FS::Conf;
2505 $cust_main->credit( sprintf("%.2f", $cust_main->balance-$old_balance ),
2506 'self-service decline',
2507 reason_type=>$conf->config('signup_credit_type'),
2509 $cust_main->apply_credits( 'order' => 'newest' );
2512 return { 'error' => '_decline', 'bill_error' => $bill_error };
2521 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2522 return { 'error' => $session } if $context eq 'error';
2524 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2525 or return { 'error' => "unknown custnum $custnum" };
2527 my @cust_pkg = sort { $a->bill <=> $b->bill }
2528 grep { $_->part_pkg->freq ne '0' }
2529 $cust_main->ncancelled_pkgs;
2531 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
2533 my $total = $cust_main->balance;
2536 my $bill = $_->bill;
2537 $total += $_->part_pkg->base_recur($_, \$bill);
2538 my $renew_date = $_->part_pkg->add_freq($_->bill);
2540 'pkgnum' => $_->pkgnum,
2541 'amount' => sprintf('%.2f', $total),
2542 'bill_date' => $_->bill,
2543 'bill_date_pretty' => time2str('%x', $_->bill),
2544 'renew_date' => $renew_date,
2545 'renew_date_pretty' => time2str('%x', $renew_date),
2546 'expire_date' => $_->expire,
2547 'expire_date_pretty' => time2str('%x', $_->expire),
2552 return { 'dates' => \@array };
2556 sub payment_info_renew_info {
2558 my $renew_info = renew_info($p);
2559 my $payment_info = payment_info($p);
2560 return { %$renew_info,
2568 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2569 return { 'error' => $session } if $context eq 'error';
2571 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2572 or return { 'error' => "unknown custnum $custnum" };
2574 my $date = $p->{'date'};
2578 #freeside-daily -n -d $date fs_daily $custnum
2579 $cust_main->bill_and_collect( 'time' => $date,
2580 'invoice_time' => $now,
2581 'actual_time' => $now,
2582 'check_freq' => '1d',
2585 return { 'error' => '' };
2591 my $session = _cache->get($p->{'session_id'})
2592 or return { 'error' => "Can't resume session" }; #better error message
2594 my $custnum = $session->{'custnum'};
2596 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2597 or return { 'error' => "unknown custnum $custnum" };
2599 my $conf = new FS::Conf;
2601 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2602 or return { 'error' => 'Permission denied' };
2604 my $pkgnum = $p->{'pkgnum'};
2606 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2607 'pkgnum' => $pkgnum, } )
2608 or return { 'error' => "unknown pkgnum $pkgnum" };
2610 my $error = $cust_pkg->suspend(reason => $reasonnum);
2611 return { 'error' => $error };
2617 my $session = _cache->get($p->{'session_id'})
2618 or return { 'error' => "Can't resume session" }; #better error message
2620 my $custnum = $session->{'custnum'};
2622 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2623 or return { 'error' => "unknown custnum $custnum" };
2625 my $pkgnum = $p->{'pkgnum'};
2627 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2628 'pkgnum' => $pkgnum, } )
2629 or return { 'error' => "unknown pkgnum $pkgnum" };
2631 my $error = $cust_pkg->cancel('quiet' => 1);
2632 return { 'error' => $error };
2636 sub provision_phone {
2639 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2641 #editing an existing phone number
2642 if ( $p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/ ) {
2643 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2644 return { 'error' => $session } if $context eq 'error';
2646 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2647 return { 'error' => 'service not found' } unless $svc_phone;
2648 return { 'error' => 'invalid svcnum' }
2649 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2651 $svc_phone->email($p->{'email'})
2652 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2653 $svc_phone->forwarddst($p->{'forwarddst'})
2654 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2655 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2656 return { 'error' => $svc_phone->replace };
2660 unless ( $p->{'lnp'} ) {
2661 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2662 $p->{'lnp_status'} = "portingin";
2663 return _provision( 'FS::svc_phone',
2664 [qw(lnp_desired_due_date lnp_other_provider
2665 lnp_other_provider_account phonenum countrycode lnp_status)],
2666 [qw(phonenum countrycode)],
2672 # single DID order (the usual case)
2673 unless (scalar(@bulkdid)) {
2674 return _provision( 'FS::svc_phone',
2675 [qw(phonenum countrycode)],
2676 [qw(phonenum countrycode)],
2682 # bulk DID order case
2684 foreach my $did ( @bulkdid ) {
2685 $did =~ s/[^0-9]//g;
2686 $error = _provision( 'FS::svc_phone',
2687 [qw(phonenum countrycode)],
2688 [qw(phonenum countrycode)],
2690 'pkgnum' => $p->{'pkgnum'},
2691 'svcpart' => $p->{'svcpart'},
2693 'countrycode' => $p->{'countrycode'},
2694 'session_id' => $p->{'session_id'},
2697 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2699 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2704 warn "provision_pbx called\n"
2707 warn "provision_pbx calling _provision\n"
2709 _provision( 'FS::svc_pbx',
2710 [qw(id title max_extensions max_simultaneous ip_addr)],
2711 [qw(id title max_extensions max_simultaneous ip_addr)],
2717 sub provision_acct {
2719 warn "provision_acct called\n"
2722 return { 'error' => gettext('passwords_dont_match') }
2723 if $p->{'_password'} ne $p->{'_password2'};
2724 return { 'error' => gettext('empty_password') }
2725 unless length($p->{'_password'});
2727 if ($p->{'domsvc'}) {
2728 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
2729 qw ( svcpart pkgnum ) );
2730 return { 'error' => gettext('invalid_domain') }
2731 unless ($domains{$p->{'domsvc'}});
2734 warn "provision_acct calling _provision\n"
2736 _provision( 'FS::svc_acct',
2737 [qw(username _password domsvc)],
2738 [qw(username _password domsvc)],
2744 sub provision_external {
2746 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
2747 _provision( 'FS::svc_external',
2756 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
2757 warn "_provision called for $class\n"
2760 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2761 return { 'error' => $session } if $context eq 'error';
2763 my $search = { 'custnum' => $custnum };
2764 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2765 my $cust_main = qsearchs('cust_main', $search )
2766 or return { 'error' => "unknown custnum $custnum" };
2768 my $pkgnum = $p->{'pkgnum'};
2770 warn "searching for custnum $custnum pkgnum $pkgnum\n"
2772 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2773 'pkgnum' => $pkgnum,
2775 or return { 'error' => "unknown pkgnum $pkgnum" };
2777 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
2779 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
2780 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
2782 return { error=> 'svcpart '. $p->{'svcpart'}. " is not a $class definition" }
2783 if $class ne 'FS::'. $part_svc->svcdb;
2785 warn "creating $class record\n"
2787 my $svc_x = $class->new( {
2788 'pkgnum' => $p->{'pkgnum'},
2789 'svcpart' => $p->{'svcpart'},
2790 map { $_ => $p->{$_} } @$fields
2793 my %insert_args = ();
2794 #i shouldn't be a special case here (pass an option or something)
2795 if ( $class eq 'FS::svc_phone'
2796 && grep length($p->{$_}), @location_editable_fields
2799 $insert_args{'cust_location'} = new FS::cust_location {
2800 map { $_ => $p->{$_} } @location_editable_fields
2804 warn "inserting $class record\n"
2806 my $error = $svc_x->insert(%insert_args);
2809 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
2811 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
2814 my $return = { 'svc' => $part_svc->svc,
2816 map { $_ => $svc_x->get($_) } @$return_fields
2818 warn "_provision returning ". Dumper($return). "\n"
2827 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2828 return { 'error' => $session } if $context eq 'error';
2830 my $search = { 'custnum' => $custnum };
2831 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2832 my $cust_main = qsearchs('cust_main', $search )
2833 or return { 'error' => "unknown custnum $custnum" };
2835 my $pkgnum = $p->{'pkgnum'};
2837 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2838 'pkgnum' => $pkgnum,
2840 or return { 'error' => "unknown pkgnum $pkgnum" };
2842 my $svcpart = $p->{'svcpart'};
2844 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
2845 'svcpart' => $svcpart, } )
2846 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
2847 my $part_svc = $pkg_svc->part_svc;
2849 my $conf = new FS::Conf;
2852 'svc' => $part_svc->svc,
2853 'svcdb' => $part_svc->svcdb,
2854 'pkgnum' => $pkgnum,
2855 'svcpart' => $svcpart,
2856 'custnum' => $custnum,
2858 'security_phrase' => 0, #XXX !
2859 'svc_acct_pop' => [], #XXX !
2861 'init_popstate' => '',
2866 small_custview( $cust_main, $conf->config('countrydefault') ),
2870 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
2871 && $ret->{'svcdb'} eq 'svc_phone') {
2872 $ret->{'svcnum'} = $p->{'svcnum'};
2873 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2874 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
2875 $ret->{'email'} = $svc_phone->email;
2876 $ret->{'forwarddst'} = $svc_phone->forwarddst;
2883 sub unprovision_svc {
2886 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2887 return { 'error' => $session } if $context eq 'error';
2889 my $search = { 'custnum' => $custnum };
2890 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2891 my $cust_main = qsearchs('cust_main', $search )
2892 or return { 'error' => "unknown custnum $custnum" };
2894 my $svcnum = $p->{'svcnum'};
2896 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
2897 or return { 'error' => "unknown svcnum $svcnum" };
2899 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
2900 unless $cust_svc->cust_pkg->custnum == $custnum;
2902 my $conf = new FS::Conf;
2904 return { 'svc' => $cust_svc->part_svc->svc,
2905 'error' => $cust_svc->cancel,
2907 small_custview( $cust_main, $conf->config('countrydefault') ),
2912 sub myaccount_passwd {
2914 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2915 return { 'error' => $session } if $context eq 'error';
2917 return { 'error' => "New passwords don't match." }
2918 if $p->{'new_password'} ne $p->{'new_password2'};
2920 return { 'error' => 'Enter new password' }
2921 unless length($p->{'new_password'});
2923 #my $search = { 'custnum' => $custnum };
2924 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2925 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2926 my $search = " AND custnum = $1";
2927 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2929 my $svc_acct = qsearchs( {
2930 'table' => 'svc_acct',
2931 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2932 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
2933 'LEFT JOIN cust_main USING ( custnum ) ',
2934 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
2935 'extra_sql' => $search, #important
2937 or return { 'error' => "Service not found" };
2941 my $conf = new FS::Conf;
2943 return { 'error' => 'Incorrect current password.' }
2944 if ( exists($p->{'old_password'})
2945 || $conf->exists('selfservice-password_change_oldpass')
2947 && ! $svc_acct->check_password($p->{'old_password'});
2949 $error = 'Password too short.'
2950 if length($p->{'new_password'}) < ($conf->config('passwordmin') || 6);
2951 $error = 'Password too long.'
2952 if length($p->{'new_password'}) > ($conf->config('passwordmax') || 8);
2954 $svc_acct->set_password($p->{'new_password'});
2955 $error ||= $svc_acct->replace();
2957 #regular pw change in self-service should change contact pw too, otherwise its
2958 #way too confusing. hell its confusing they're separate at all, but alas.
2959 #need to support the "ISP provides email that's used as a contact email" case
2961 my $contact = FS::contact->by_selfservice_email($svc_acct->email);
2962 if ( $contact && $contact->custnum == $custnum ) {
2963 #svc_acct was successful but this one returns an error? "shouldn't happen"
2964 $error ||= $contact->change_password($p->{'new_password'});
2967 my($label, $value) = $svc_acct->cust_svc->label;
2969 return { 'error' => $error,
2976 # sub contact_passwd {
2978 # my($context, $session, $custnum) = _custoragent_session_custnum($p);
2979 # return { 'error' => $session } if $context eq 'error';
2981 # return { 'error' => 'Not logged in as a contact.' }
2982 # unless $session->{'contactnum'};
2984 # return { 'error' => "New passwords don't match." }
2985 # if $p->{'new_password'} ne $p->{'new_password2'};
2987 # return { 'error' => 'Enter new password' }
2988 # unless length($p->{'new_password'});
2990 # #my $search = { 'custnum' => $custnum };
2991 # #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2992 # $custnum =~ /^(\d+)$/ or die "illegal custnum";
2993 # my $search = " AND selfservice_access IS NOT NULL ".
2994 # " AND selfservice_access = 'Y' ".
2995 # " AND ( disabled IS NULL OR disabled = '' )".
2996 # " AND custnum IS NOT NULL AND custnum = $1";
2997 # $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2999 # my $contact = qsearchs( {
3000 # 'table' => 'contact',
3001 # 'addl_from' => 'LEFT JOIN cust_main USING ( custnum ) ',
3002 # 'hashref' => { 'contactnum' => $session->{'contactnum'}, },
3003 # 'extra_sql' => $search, #important
3005 # or return { 'error' => "Email not found" }; #? how did we get logged in?
3006 # # deleted since then?
3010 # # use these svc_acct length restrictions??
3011 # my $conf = new FS::Conf;
3012 # $error = 'Password too short.'
3013 # if length($p->{'new_password'}) < ($conf->config('passwordmin') || 6);
3014 # $error = 'Password too long.'
3015 # if length($p->{'new_password'}) > ($conf->config('passwordmax') || 8);
3017 # $error ||= $contact->change_password($p->{'new_password'});
3019 # return { 'error' => $error, };
3026 my $info = skin_info($p);
3028 my $conf = new FS::Conf;
3029 my $verification = $conf->config('selfservice-password_reset_verification')
3030 or return { %$info, 'error' => 'Password resets disabled' };
3035 if ( $p->{'email'} ) { #new-style, changes contact and svc_acct
3037 $contact = FS::contact->by_selfservice_email($p->{'email'});
3039 $cust_main = $contact->cust_main if $contact;
3041 #also look for an svc_acct, otherwise it would be super confusing
3043 my($username, $domain) = split('@', $p->{'email'});
3044 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } );
3045 if ( $svc_domain ) {
3046 $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
3047 'domsvc' => $svc_domain->svcnum }
3050 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
3051 $cust_main ||= $cust_pkg->cust_main if $cust_pkg;
3053 #precaution: don't change svc_acct password not part of the same
3054 # customer as contact
3055 $svc_acct = '' if ! $cust_pkg
3056 || $cust_pkg->custnum != $cust_main->custnum;
3061 return { %$info, 'error' => 'Email address not found' }
3062 unless $contact || $svc_acct;
3064 } elsif ( $p->{'username'} ) { #old style, looks in svc_acct only
3066 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
3067 or return { %$info, 'error' => 'Account not found' };
3069 $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
3070 'domsvc' => $svc_domain->svcnum }
3072 or return { %$info, 'error' => 'Account not found' };
3074 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
3075 or return { %$info, 'error' => 'Account not found' };
3077 $cust_main = $cust_pkg->cust_main;
3082 'email' => sub { 1; },
3084 my( $p, $cust_main ) = @_;
3085 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
3086 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
3089 my( $p, $cust_main ) = @_;
3090 my $cust_pay = qsearchs({
3091 'table' => 'cust_pay',
3092 'hashref' => { 'custnum' => $cust_main->custnum },
3093 'order_by' => 'ORDER BY _date DESC LIMIT 1',
3097 $p->{'amount'} == $cust_pay->paid;
3100 my( $p, $cust_main ) = @_;
3101 $p->{'zip'} eq $cust_main->zip
3102 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
3106 foreach my $verify ( split(',', $verification) ) {
3108 &{ $verify{$verify} }( $p, $cust_main )
3109 or return { %$info, 'error' => 'Account not found' };
3113 #okay, we're verified
3117 my $error = $contact->send_reset_email(
3118 'svcnum' => ($svc_acct ? $svc_acct->svcnum : ''),
3122 return { %$info, 'error' => $error }; #????
3125 } elsif ( $svc_acct ) {
3127 #create a unique session
3129 my $reset_session = {
3130 'svcnum' => $svc_acct->svcnum,
3134 my $timeout = '1 hour'; #?
3136 my $reset_session_id;
3138 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
3139 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") );
3142 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
3146 my $msgnum = $conf->config('selfservice-password_reset_msgnum',
3147 $cust_main->agentnum);
3148 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
3149 return { %$info, 'error' => "selfservice-password_reset_msgnum unset" }
3151 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
3152 my $error = $msg_template->send( 'cust_main' => $cust_main,
3153 'object' => $svc_acct,
3154 'substitutions' => {
3155 'session_id' => $reset_session_id,
3159 return { %$info, 'error' => $error }; #????
3164 return { %$info, 'error' => '' };
3167 sub check_reset_passwd {
3170 my $conf = new FS::Conf;
3171 my $verification = $conf->config('selfservice-password_reset_verification')
3172 or return { 'error' => 'Password resets disabled' };
3174 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3175 or return { 'error' => "Can't resume session" }; #better error message
3177 if ( $reset_session->{'svcnum'} ) {
3179 my $svcnum = $reset_session->{'svcnum'};
3181 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3182 or return { 'error' => "Service not found" };
3184 $p->{'agentnum'} = $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3185 my $info = skin_info($p);
3189 'session_id' => $p->{'session_id'},
3190 'username' => $svc_acct->username,
3193 } elsif ( $reset_session->{'contactnum'} ) {
3195 my $contactnum = $reset_session->{'contactnum'};
3197 my $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3198 or return { 'error' => "Contact not found" };
3200 my @contact_email = $contact->contact_email;
3201 return { 'error' => 'No contact email' } unless @contact_email;
3203 $p->{'agentnum'} = $contact->cust_main->agentnum;
3204 my $info = skin_info($p);
3208 'session_id' => $p->{'session_id'},
3209 'email' => $contact_email[0]->email, #the first?
3214 return { 'error' => 'No svcnum or contactnum in session' }; #??
3220 sub process_reset_passwd {
3223 my $conf = new FS::Conf;
3224 my $verification = $conf->config('selfservice-password_reset_verification')
3225 or return { 'error' => 'Password resets disabled' };
3227 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3228 or return { 'error' => "Can't resume session" }; #better error message
3233 if ( $reset_session->{'svcnum'} ) {
3235 my $svcnum = $reset_session->{'svcnum'};
3237 $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3238 or return { 'error' => "Service not found" };
3240 $p->{'agentnum'} ||= $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3241 $info ||= skin_info($p);
3246 if ( $reset_session->{'contactnum'} ) {
3248 my $contactnum = $reset_session->{'contactnum'};
3250 $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3251 or return { 'error' => "Contact not found" };
3253 $p->{'agentnum'} ||= $contact->cust_main->agentnum;
3254 $info ||= skin_info($p);
3258 return { %$info, 'error' => "New passwords don't match." }
3259 if $p->{'new_password'} ne $p->{'new_password2'};
3261 return { %$info, 'error' => 'Enter new password' }
3262 unless length($p->{'new_password'});
3266 $svc_acct->set_password($p->{'new_password'});
3267 my $error = $svc_acct->replace();
3269 return { %$info, 'error' => $error } if $error;
3271 #my($label, $value) = $svc_acct->cust_svc->label;
3272 #return { 'error' => $error,
3273 # #'label' => $label,
3274 # #'value' => $value,
3281 my $error = $contact->change_password($p->{'new_password'});
3283 return { %$info, 'error' => $error }; # if $error;
3287 #password changed ,so remove session, don't want it reused
3288 _cache->remove($p->{'session_id'});
3290 return { %$info, 'error' => '' };
3296 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3297 return { 'error' => $session } if $context eq 'error';
3300 if ( $session->{'pkgnum'} ) {
3302 #tickets for specific service with pkg-balances on
3303 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3304 'pkgnum' => $session->{'pkgnum'} })
3305 or return { 'error' => 'unknown package' };
3306 foreach my $cust_svc ( $cust_pkg->cust_svc ) {
3307 push @tickets, $cust_svc->tickets( $p->{status} );
3312 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
3313 or return { 'error' => "unknown custnum $custnum" };
3315 @tickets = $cust_main->tickets( $p->{status} );
3318 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
3319 if ( $FS::TicketSystem::system && FS::TicketSystem->selfservice_priority ) {
3320 my $conf = new FS::Conf;
3321 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
3325 ($a->{'_selfservice_priority'} eq '') <=>
3326 ($b->{'_selfservice_priority'} eq '')
3329 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
3335 +{ tickets => \@tickets };
3342 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3343 return { 'error' => $session } if $context eq 'error';
3345 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
3346 FS::TicketSystem->init();
3348 my $conf = new FS::Conf;
3349 my $queue = $p->{'queue'}
3350 || $conf->config('ticket_system-selfservice_queueid')
3351 || $conf->config('ticket_system-default_queueid');
3353 warn "$me create_ticket: creating ticket\n" if $DEBUG;
3354 my $err_or_ticket = FS::TicketSystem->create_ticket(
3355 '', #create RT session based on FS CurrentUser (fs_selfservice)
3357 'custnum' => $custnum,
3358 'svcnum' => $session->{'svcnum'},
3359 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
3362 if ( ref($err_or_ticket) ) {
3363 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
3365 return { 'error' => '',
3366 'ticket_id' => $err_or_ticket->id,
3369 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
3371 return { 'error' => $err_or_ticket };
3379 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3380 return { 'error' => $session } if $context eq 'error';
3382 return { error => 'requested format not implemented' }
3383 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
3385 my $conf = new FS::Conf;
3386 my $age_threshold = 0;
3387 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
3388 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
3390 my $search = { 'custnum' => $custnum };
3391 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3392 my $cust_main = qsearchs('cust_main', $search )
3393 or return { 'error' => "unknown custnum $custnum" };
3395 # does it make more sense to just run one sql query for this instead of all the
3396 # insanity below? would increase performance greately for large data sets?
3398 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
3399 my @part_svc = $cust_pkg->part_svc;
3400 foreach my $part_svc ( @part_svc ) {
3401 if($part_svc->svcdb eq 'svc_phone'){
3402 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
3403 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
3404 push @svc_phone, $cust_pkg_svc->svc_x
3405 if $cust_pkg_svc->date_inserted >= $age_threshold;
3413 my($xls_r,$xls_c) = (0,0);
3416 my @fields = qw( countrycode phonenum pin sip_password phone_name );
3417 if($p->{'format'} eq 'csv') {
3418 $csv = new Text::CSV_XS { 'always_quote' => 1,
3421 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
3422 $content .= $csv->string;
3424 elsif($p->{'format'} eq 'xls') {
3425 my $XLS1 = new IO::Scalar \$content;
3426 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
3427 or return { 'error' => "Error opening .xls file: $!" };
3428 $xls = $xls_workbook->add_worksheet('DIDs');
3429 foreach ( @fields ) {
3430 $xls->write(0,$xls_c++,$_);
3435 foreach my $svc_phone ( @svc_phone ) {
3436 my @cols = map { $svc_phone->$_ } @fields;
3437 if($p->{'format'} eq 'csv') {
3438 return { 'error' => 'Unable to create CSV' }
3439 unless $csv->combine(@cols);
3440 $content .= $csv->string;
3442 elsif($p->{'format'} eq 'xls') {
3445 $xls->write($xls_r,$xls_c++,$_);
3451 $xls_workbook->close() if $p->{'format'} eq 'xls';
3453 { content => $content, format => $p->{'format'}, };
3458 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3459 return { 'error' => $session } if $context eq 'error';
3461 # warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
3462 # FS::TicketSystem->init();
3463 # return { 'error' => 'get_ticket configuration error' }
3464 # if $FS::TicketSystem::system ne 'RT_Internal';
3466 # check existence and ownership as part of this
3467 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
3468 my $rt_session = FS::TicketSystem->session('');
3469 my $Ticket = FS::TicketSystem->get_ticket_object(
3471 ticket_id => $p->{'ticket_id'},
3474 return { 'error' => 'ticket not found' } if !$Ticket;
3476 if ( length( $p->{'subject'} || '' ) ) {
3478 if ( $p->{'subject'} ne $Ticket->Subject ) {
3479 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
3480 return { 'error' => "unable to set subject: $msg" } if !$val;
3484 if(length($p->{'reply'})) {
3485 my @err_or_res = FS::TicketSystem->correspond_ticket(
3487 'ticket_id' => $p->{'ticket_id'},
3488 'content' => $p->{'reply'},
3491 return { 'error' => 'unable to reply to ticket' }
3492 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
3495 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
3496 my $err_or_ticket = FS::TicketSystem->get_ticket(
3498 'ticket_id' => $p->{'ticket_id'},
3501 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
3502 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
3504 return { 'error' => $err_or_ticket };
3507 my @custs = @{$err_or_ticket->{'custs'}};
3508 my @txns = @{$err_or_ticket->{'txns'}};
3511 # superseded by check in get_ticket_object
3512 #return { 'error' => 'invalid ticket requested' }
3513 #unless grep($_ eq $custnum, @custs);
3515 foreach my $txn ( @txns ) {
3516 push @filtered_txns, $txn
3517 if ($txn->{'type'} eq 'EmailRecord'
3518 || $txn->{'type'} eq 'Correspond'
3519 || $txn->{'type'} eq 'Create');
3522 warn "$me get_ticket: successful: \n"
3524 return { 'error' => '',
3525 'transactions' => \@filtered_txns,
3526 'ticket_fields' => $err_or_ticket->{'fields'},
3527 'ticket_id' => $p->{'ticket_id'},
3531 sub adjust_ticket_priority {
3533 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3534 return { 'error' => $session } if $context eq 'error';
3536 # warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
3537 # FS::TicketSystem->init;
3538 my $ss_priority = FS::TicketSystem->selfservice_priority;
3540 return { 'error' => 'adjust_ticket_priority configuration error' }
3541 if $FS::TicketSystem::system ne 'RT_Internal'
3544 my $values = $p->{'values'}; #hashref, id => priority value
3547 foreach my $id (keys %$values) {
3548 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
3549 my $Ticket = FS::TicketSystem->get_ticket_object('',
3551 'custnum' => $custnum,
3554 $ticket_error{$id} = 'ticket not found';
3558 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
3559 # We're not going to implement it for RT_External.
3560 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
3561 my $new_value = $values->{$id};
3562 next if $old_value eq $new_value;
3564 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
3566 # AddCustomFieldValue works fine (replacing any existing value) if it's
3567 # a single-valued custom field, which it should be. If it's not, you're
3568 # doing something wrong.
3570 if ( length($new_value) ) {
3571 ($val, $msg) = $Ticket->AddCustomFieldValue(
3572 Field => $ss_priority,
3573 Value => $new_value,
3577 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
3578 Field => $ss_priority,
3579 Value => $old_value,
3583 $ticket_error{$id} = $msg if !$val;
3584 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
3586 return { 'error' => '',
3587 'ticket_error' => \%ticket_error,
3588 %{ customer_info($p) } # send updated customer info back
3594 sub _custoragent_session_custnum {
3597 my($context, $session, $custnum);
3598 if ( $p->{'session_id'} ) {
3600 $context = 'customer';
3601 $session = _cache->get($p->{'session_id'})
3602 or return ( 'error' => "Can't resume session" ); #better error message
3603 $custnum = $session->{'custnum'};
3605 } elsif ( $p->{'agent_session_id'} ) {
3608 my $agent_cache = new FS::ClientAPI_SessionCache( {
3609 'namespace' => 'FS::ClientAPI::Agent',
3611 $session = $agent_cache->get($p->{'agent_session_id'})
3612 or return ( 'error' => "Can't resume session" ); #better error message
3613 $custnum = $p->{'custnum'};
3617 return ( 'error' => "Can't resume session" ); #better error message
3620 ($context, $session, $custnum);