1 package FS::ClientAPI::MyAccount;
3 use 5.008; #require 5.8+ for Time::Local 1.05+
5 use vars qw( $cache $DEBUG $me );
6 use subs qw( _cache _provision );
9 use Digest::MD5 qw(md5_hex);
12 use Time::Local qw(timelocal_nocheck);
13 use Business::CreditCard;
16 use Spreadsheet::WriteExcel;
17 use FS::UI::Web::small_custview qw(small_custview); #less doh
19 use FS::UI::bytecount qw( display_bytecount );
22 use FS::Record qw(qsearch qsearchs dbh);
23 use FS::Msgcat qw(gettext);
24 use FS::Misc qw(card_types);
25 use FS::Misc::DateTime qw(parse_datetime);
27 use FS::ClientAPI_SessionCache;
39 use FS::legacy_cust_bill;
40 use FS::cust_main_county;
43 use FS::acct_rt_transaction;
47 $me = '[FS::ClientAPI::MyAccount]';
49 use vars qw( @cust_main_editable_fields );
50 @cust_main_editable_fields = qw(
51 first last company address1 address2 city
52 county state zip country
53 daytime night fax mobile
54 ship_first ship_last ship_company ship_address1 ship_address2 ship_city
55 ship_state ship_zip ship_country
56 ship_daytime ship_night ship_fax ship_mobile
58 payby payinfo payname paystart_month paystart_year payissue payip
59 ss paytype paystate stateid stateid_state
62 BEGIN { #preload to reduce time customer_info takes
63 if ( $FS::TicketSystem::system ) {
64 warn "$me: initializing ticket system\n" if $DEBUG;
65 FS::TicketSystem->init();
70 $cache ||= new FS::ClientAPI_SessionCache( {
71 'namespace' => 'FS::ClientAPI::MyAccount',
78 my($context, $session, $custnum) = _custoragent_session_custnum($p);
79 #return { 'error' => $session } if $context eq 'error';
82 if ( $context eq 'customer' ) {
84 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
87 $sth->execute($custnum) or die $sth->errstr;
89 $agentnum = $sth->fetchrow_arrayref->[0]
90 or die "no agentnum for custnum $custnum";
92 #} elsif ( $context eq 'agent' ) {
93 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
97 my $conf = new FS::Conf;
99 #false laziness w/Signup.pm
101 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
103 if ( $skin_info_cache_agent ) {
105 warn "$me loading cached skin info for agentnum $agentnum\n"
110 warn "$me populating skin info cache for agentnum $agentnum\n"
113 $skin_info_cache_agent = {
114 'agentnum' => $agentnum,
115 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
116 qw( company_name date_format ) ),
117 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
118 qw( body_bgcolor box_bgcolor
119 text_color link_color vlink_color hlink_color alink_color
120 font title_color title_align title_size menu_bgcolor menu_fontsize
123 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
124 qw( menu_skipblanks menu_skipheadings menu_nounderline )
126 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
127 qw( title_left_image title_right_image
128 menu_top_image menu_body_image menu_bottom_image
131 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
132 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
133 qw( head body_header body_footer company_address ) ),
136 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
140 #{ %$skin_info_cache_agent };
141 $skin_info_cache_agent;
148 my $conf = new FS::Conf;
152 'phone_login' => $conf->exists('selfservice_server-phone_login'),
153 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
160 #false laziness w/FS::ClientAPI::passwd::passwd
164 my $conf = new FS::Conf;
167 if ( $p->{'domain'} eq 'svc_phone'
168 && $conf->exists('selfservice_server-phone_login') ) {
170 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
171 return { error => 'Number not found.' } unless $svc_phone;
174 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
175 #return { error => 'Only primary user may log in.' }
176 # if $conf->exists('selfservice_server-primary_only')
177 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
179 return { error => 'Incorrect PIN.' }
180 unless $svc_phone->check_pin($p->{'password'});
186 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
187 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
189 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
190 'domsvc' => $svc_domain->svcnum, }
192 return { error => 'User not found.' } unless $svc_acct;
194 if($conf->exists('selfservice_server-login_svcpart')) {
195 my @svcpart = $conf->config('selfservice_server-login_svcpart');
196 my $svcpart = $svc_acct->cust_svc->svcpart;
197 return { error => 'Invalid user.' }
198 unless grep($_ eq $svcpart, @svcpart);
201 return { error => 'Incorrect password.' }
202 unless $svc_acct->check_password($p->{'password'});
209 'svcnum' => $svc_x->svcnum,
212 my $cust_svc = $svc_x->cust_svc;
213 my $cust_pkg = $cust_svc->cust_pkg;
215 my $cust_main = $cust_pkg->cust_main;
216 $session->{'custnum'} = $cust_main->custnum;
217 if ( $conf->exists('pkg-balances') ) {
218 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
219 $cust_main->ncancelled_pkgs;
220 $session->{'pkgnum'} = $cust_pkg->pkgnum
221 if scalar(@cust_pkg) > 1;
225 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
226 #return { error => 'Only primary user may log in.' }
227 # if $conf->exists('selfservice_server-primary_only')
228 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
229 my $part_pkg = $cust_pkg->part_pkg;
230 return { error => 'Only primary user may log in.' }
231 if $conf->exists('selfservice_server-primary_only')
232 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
236 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
237 } until ( ! defined _cache->get($session_id) ); #just in case
239 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
240 _cache->set( $session_id, $session, $timeout );
242 return { 'error' => '',
243 'session_id' => $session_id,
249 if ( $p->{'session_id'} ) {
250 _cache->remove($p->{'session_id'});
251 return { %{ skin_info($p) }, 'error' => '' };
253 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
260 my($context, $session, $custnum) = _custoragent_session_custnum($p);
261 return { 'error' => $session } if $context eq 'error';
263 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
264 or return { 'error' => "Service not found" };
266 $session->{'svcnum'} = $svc_acct->svcnum;
268 my $conf = new FS::Conf;
269 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
270 _cache->set( $p->{'session_id'}, $session, $timeout );
272 return { 'error' => '' };
276 sub payment_gateway {
278 # takes a cust_main and a cust_payby entry, returns the payment_gateway
279 my $conf = new FS::Conf;
280 my $cust_main = shift;
281 my $cust_payby = shift;
282 my $gatewaynum = $conf->config('selfservice-payment_gateway');
284 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
285 die "configured gatewaynum $gatewaynum not found!" if !$pg;
289 return '' if ! FS::payby->realtime($cust_payby);
290 my $pg = $cust_main->agent->payment_gateway(
291 'method' => FS::payby->payby2bop($cust_payby),
301 my $conf = new FS::Conf;
303 my $info = skin_info($p);
305 use vars qw( $cust_paybys ); #cache for performance
306 unless ( $cust_paybys ) {
308 my %cust_paybys = map { $_ => 1 }
309 map { FS::payby->payby2payment($_) }
310 $conf->config('signup_server-payby');
312 $cust_paybys = [ keys %cust_paybys ];
315 $info->{'cust_paybys'} = $cust_paybys;
317 my($context, $session, $custnum) = _custoragent_session_custnum($p);
318 return { 'error' => $session } if $context eq 'error';
320 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
321 or return { 'error' => "unknown custnum $custnum" };
323 $info->{'hide_payment_fields'} = [
325 my $pg = payment_gateway($cust_main, $_);
326 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
327 } @{ $info->{cust_paybys} }
330 $info->{'self_suspend_reason'} =
331 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
333 $info->{'edit_ticket_subject'} =
334 $conf->exists('ticket_system-selfservice_edit_subject') &&
335 $cust_main->edit_subject;
338 'custnum' => $custnum,
339 'access_pkgnum' => $session->{'pkgnum'},
340 'access_svcnum' => $session->{'svcnum'},
347 my($context, $session, $custnum) = _custoragent_session_custnum($p);
348 return { 'error' => $session } if $context eq 'error';
352 my $conf = new FS::Conf;
353 if ($conf->exists('cust_main-require_address2')) {
354 $return{'require_address2'} = '1';
356 $return{'require_address2'} = '';
359 if ( $FS::TicketSystem::system ) {
360 warn "$me customer_info: initializing ticket system\n" if $DEBUG;
361 FS::TicketSystem->init();
364 if ( $custnum ) { #customer record
366 my $search = { 'custnum' => $custnum };
367 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
368 my $cust_main = qsearchs('cust_main', $search )
369 or return { 'error' => "unknown custnum $custnum" };
371 if ( $session->{'pkgnum'} ) {
372 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
374 $return{balance} = $cust_main->balance;
377 my @tickets = $cust_main->tickets;
378 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
379 if ( FS::TicketSystem->selfservice_priority ) {
380 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
384 ($a->{'_selfservice_priority'} eq '') <=>
385 ($b->{'_selfservice_priority'} eq '')
388 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
394 $return{tickets} = \@tickets;
397 unless ( $session->{'pkgnum'} ) {
400 invnum => $_->invnum,
401 date => time2str("%b %o, %Y", $_->_date),
404 } $cust_main->open_cust_bill;
405 $return{open_invoices} = \@open;
408 $return{small_custview} =
409 small_custview( $cust_main,
410 scalar($conf->config('countrydefault')),
411 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
414 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
415 $return{ship_name} = $cust_main->ship_first. ' '. $cust_main->get('ship_last');
417 for (@cust_main_editable_fields) {
418 $return{$_} = $cust_main->get($_);
421 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
422 $return{payinfo} = $cust_main->paymask;
423 @return{'month', 'year'} = $cust_main->paydate_monthyear;
426 $return{'invoicing_list'} =
427 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
428 $return{'postal_invoicing'} =
429 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
431 if (scalar($conf->config('support_packages'))) {
432 my @support_services = ();
433 foreach ($cust_main->support_services) {
434 my $seconds = $_->svc_x->seconds;
435 my $time_remaining = (($seconds < 0) ? '-' : '' ).
436 int(abs($seconds)/3600)."h".
437 sprintf("%02d",(abs($seconds)%3600)/60)."m";
438 my $cust_pkg = $_->cust_pkg;
441 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
442 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
443 push @support_services, { svcnum => $_->svcnum,
444 time => $time_remaining,
449 $return{support_services} = \@support_services;
452 if ( $conf->config('prepayment_discounts-credit_type') ) {
454 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
457 if ( $session->{'svcnum'} ) {
458 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
459 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
460 $return{'svcnum'} = $session->{'svcnum'};
463 } elsif ( $session->{'svcnum'} ) { #no customer record
465 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
466 or die "unknown svcnum";
467 $return{name} = $svc_acct->email;
471 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
475 return { 'error' => '',
476 'custnum' => $custnum,
482 sub customer_info_short {
485 my($context, $session, $custnum) = _custoragent_session_custnum($p);
486 return { 'error' => $session } if $context eq 'error';
490 my $conf = new FS::Conf;
492 if ( $custnum ) { #customer record
494 my $search = { 'custnum' => $custnum };
495 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
496 my $cust_main = qsearchs('cust_main', $search )
497 or return { 'error' => "unknown custnum $custnum" };
499 $return{small_custview} =
500 small_custview( $cust_main,
501 scalar($conf->config('countrydefault')),
505 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
506 $return{ship_name} = $cust_main->ship_first. ' '. $cust_main->get('ship_last');
508 $return{payby} = $cust_main->payby;
510 #none of these are terribly expensive if we want 'em...
511 for (@cust_main_editable_fields) {
512 $return{$_} = $cust_main->get($_);
515 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
516 $return{payinfo} = $cust_main->paymask;
517 @return{'month', 'year'} = $cust_main->paydate_monthyear;
520 $return{'invoicing_list'} =
521 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
522 #$return{'postal_invoicing'} =
523 # 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
525 if ( $session->{'svcnum'} ) {
526 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
527 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
528 $return{'svcnum'} = $session->{'svcnum'};
531 } elsif ( $session->{'svcnum'} ) { #no customer record
533 #uuh, not supproted yet... die?
534 return { 'error' => 'customer_info_short not yet supported as agent' };
538 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
542 return { 'error' => '',
543 'custnum' => $custnum,
550 my $session = _cache->get($p->{'session_id'})
551 or return { 'error' => "Can't resume session" }; #better error message
553 my $custnum = $session->{'custnum'}
554 or return { 'error' => "no customer record" };
556 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
557 or return { 'error' => "unknown custnum $custnum" };
559 my $new = new FS::cust_main { $cust_main->hash };
560 $new->set( $_ => $p->{$_} )
561 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
564 if (exists($p->{'payby'})) {
565 $p->{'payby'} =~ /^([A-Z]{4})$/
566 or return { 'error' => "illegal_payby " . $p->{'payby'} };
570 if ( $payby =~ /^(CARD|DCRD)$/ ) {
572 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
574 if ( $new->payinfo eq $cust_main->paymask ) {
575 $new->payinfo($cust_main->payinfo);
577 $new->payinfo($p->{'payinfo'});
580 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
582 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
585 $p->{'payinfo1'} =~ /^([\dx]+)$/
586 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
588 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by -require-bank-branch?
589 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
591 $payinfo = $payinfo1. '@'. $payinfo2;
593 $new->payinfo( ($payinfo eq $cust_main->paymask)
594 ? $cust_main->payinfo
598 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
600 } elsif ( $payby =~ /^(BILL)$/ ) {
602 } elsif ( $payby ) { #notyet ready
603 return { 'error' => "unknown payby $payby" };
607 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
608 #false laziness with httemplate/edit/process/cust_main.cgi
609 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
610 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
612 @invoicing_list = $cust_main->invoicing_list;
615 my $error = $new->replace($cust_main, \@invoicing_list);
616 return { 'error' => $error } if $error;
619 return { 'error' => '' };
624 my $session = _cache->get($p->{'session_id'})
625 or return { 'error' => "Can't resume session" }; #better error message
631 my $conf = new FS::Conf;
632 use vars qw($payment_info); #cache for performance
633 unless ( $payment_info ) {
635 my %states = map { $_->state => 1 }
636 qsearch('cust_main_county', {
637 'country' => $conf->config('countrydefault') || 'US'
640 my %cust_paybys = map { $_ => 1 }
641 map { FS::payby->payby2payment($_) }
642 $conf->config('signup_server-payby');
644 my @cust_paybys = keys %cust_paybys;
648 #list all counties/states/countries
649 'cust_main_county' =>
650 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
652 #shortcut for one-country folks
654 [ sort { $a cmp $b } keys %states ],
656 'card_types' => card_types(),
658 'paytypes' => [ @FS::cust_main::paytypes ],
660 'paybys' => [ $conf->config('signup_server-payby') ],
661 'cust_paybys' => \@cust_paybys,
663 'stateid_label' => FS::Msgcat::_gettext('stateid'),
664 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
666 'show_ss' => $conf->exists('show_ss'),
667 'show_stateid' => $conf->exists('show_stateid'),
668 'show_paystate' => $conf->exists('show_bankstate'),
670 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
672 'credit_card_surcharge_percentage' => $conf->config('credit-card-surcharge-percentage'),
681 my %return = %$payment_info;
683 my $custnum = $session->{'custnum'};
685 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
686 or return { 'error' => "unknown custnum $custnum" };
688 $return{'hide_payment_fields'} = [
690 my $pg = payment_gateway($cust_main, $_);
691 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
692 } @{ $return{cust_paybys} }
695 $return{balance} = $cust_main->balance; #XXX pkg-balances?
697 $return{payname} = $cust_main->payname
698 || ( $cust_main->first. ' '. $cust_main->get('last') );
700 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
702 $return{payby} = $cust_main->payby;
703 $return{stateid_state} = $cust_main->stateid_state;
705 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
706 $return{card_type} = cardtype($cust_main->payinfo);
707 $return{payinfo} = $cust_main->paymask;
709 @return{'month', 'year'} = $cust_main->paydate_monthyear;
713 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
714 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
715 $return{payinfo1} = $payinfo1;
716 $return{payinfo2} = $payinfo2;
717 $return{paytype} = $cust_main->paytype;
718 $return{paystate} = $cust_main->paystate;
722 if ( $conf->config('prepayment_discounts-credit_type') ) {
724 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
727 #doubleclick protection
729 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
731 return { 'error' => '',
737 #some false laziness with httemplate/process/payment.cgi - look there for
738 #ACH and CVV support stuff
740 sub validate_payment {
743 my $session = _cache->get($p->{'session_id'})
744 or return { 'error' => "Can't resume session" }; #better error message
746 my $custnum = $session->{'custnum'};
748 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
749 or return { 'error' => "unknown custnum $custnum" };
751 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
752 or return { 'error' => gettext('illegal_amount') };
754 return { error => 'Amount must be greater than 0' } unless $amount > 0;
756 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
757 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
758 my $discount_term = $1;
760 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
761 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
764 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
765 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
768 $p->{'payby'} ||= 'CARD';
769 $p->{'payby'} =~ /^([A-Z]{4})$/
770 or return { 'error' => "illegal_payby " . $p->{'payby'} };
773 #false laziness w/process/payment.cgi
776 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
778 $p->{'payinfo1'} =~ /^([\dx]+)$/
779 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
781 $p->{'payinfo2'} =~ /^([\dx]+)$/
782 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
784 $payinfo = $payinfo1. '@'. $payinfo2;
786 $payinfo = $cust_main->payinfo
787 if $cust_main->paymask eq $payinfo;
789 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
791 $payinfo = $p->{'payinfo'};
793 #more intelligent mathing will be needed here if you change
794 #card_masking_method and don't remove existing paymasks
795 $payinfo = $cust_main->payinfo
796 if $cust_main->paymask eq $payinfo;
799 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
800 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
804 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
805 return { 'error' => gettext('unknown_card_type') }
806 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
808 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
809 if ( cardtype($payinfo) eq 'American Express card' ) {
810 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
811 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
814 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
815 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
821 die "unknown payby $payby";
825 'CARD' => [ qw( paystart_month paystart_year payissue payip
826 address1 address2 city state zip country ) ],
827 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
831 $card_type = cardtype($payinfo) if $payby eq 'CARD';
834 'cust_main' => $cust_main, #XXX or just custnum??
837 'payinfo' => $payinfo,
838 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
839 'card_type' => $card_type,
840 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
841 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
842 'payname' => $payname,
843 'paybatch' => $paybatch, #this doesn't actually do anything
845 'payname' => $payname,
846 'discount_term' => $discount_term,
847 'pkgnum' => $session->{'pkgnum'},
848 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
858 my $validate = validate_payment($p);
859 return $validate if $validate->{'error'};
861 my $conf = new FS::Conf;
862 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
863 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
865 +{ map { $_=>$validate->{$_} }
866 qw( card_type paymask payname paydate_pretty amount )
871 sub process_stored_payment {
874 my $session_id = $p->{'session_id'};
876 my $payment_info = _cache->get( "payment_$session_id" )
877 or return { 'error' => "Can't resume session" }; #better error message
879 do_process_payment($payment_info);
883 sub process_payment {
886 my $payment_info = validate_payment($p);
887 return $payment_info if $payment_info->{'error'};
889 do_process_payment($payment_info);
893 sub do_process_payment {
894 my $validate = shift;
896 my $cust_main = $validate->{'cust_main'};
898 my $amount = delete $validate->{'amount'};
901 my $payby = delete $validate->{'payby'};
903 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
906 'paynum_ref' => \$paynum,
909 return { 'error' => $error } if $error;
911 $cust_main->apply_payments;
913 if ( $validate->{'save'} ) {
914 my $new = new FS::cust_main { $cust_main->hash };
915 if ($validate->{'payby'} eq 'CARD' || $validate->{'payby'} eq 'DCRD') {
916 $new->set( $_ => $validate->{$_} )
917 foreach qw( payname paystart_month paystart_year payissue payip
918 address1 address2 city state zip country );
919 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
920 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
921 $new->set( $_ => $validate->{$_} )
922 foreach qw( payname payip paytype paystate
923 stateid stateid_state );
924 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
926 $new->set( 'payinfo' => $cust_main->card_token || $validate->{'payinfo'} );
927 $new->set( 'paydate' => $validate->{'paydate'} );
928 my $error = $new->replace($cust_main);
930 #no, this causes customers to process their payments again
931 #return { 'error' => $error };
932 #XXX just warn verosely for now so i can figure out how these happen in
933 # the first place, eventually should redirect them to the "change
934 #address" page but indicate the payment did process??
935 delete($validate->{'payinfo'}); #don't want to log this!
936 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
937 "NEW: ". Dumper($new)."\n".
938 "OLD: ". Dumper($cust_main)."\n".
939 "PACKET: ". Dumper($validate)."\n";
947 my $receipt_html = '';
949 # currently supported for realtime CC only; send receipt data to SS
950 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
953 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
956 <TD ALIGN="right">Payment#</TD>
957 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
961 <TD ALIGN="right">Date</TD>
963 <TD BGCOLOR="#FFFFFF"><B>! .
964 time2str("%a %b %o, %Y %r", $cust_pay->_date)
970 <TD ALIGN="right">Amount</TD>
971 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
976 <TD ALIGN="right">Payment method</TD>
977 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
988 my($gw, $auth, $order) = split(':', $cust_pay->paybatch);
992 'amount' => sprintf('%.2f', $cust_pay->paid),
993 'date' => $cust_pay->_date,
994 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
995 'time_pretty' => time2str('%T', $cust_pay->_date),
997 'order_num' => $order,
998 'receipt_html' => $receipt_html,
1005 'receipt_html' => '',
1012 sub realtime_collect {
1015 my $session = _cache->get($p->{'session_id'})
1016 or return { 'error' => "Can't resume session" }; #better error message
1018 my $custnum = $session->{'custnum'};
1020 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1021 or return { 'error' => "unknown custnum $custnum" };
1024 if ( $p->{'amount'} ) {
1025 $amount = $p->{'amount'};
1027 elsif ( $session->{'pkgnum'} ) {
1028 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1031 $amount = $cust_main->balance;
1034 my $error = $cust_main->realtime_collect(
1035 'method' => $p->{'method'},
1036 'amount' => $amount,
1037 'pkgnum' => $session->{'pkgnum'},
1038 'session_id' => $p->{'session_id'},
1042 return { 'error' => $error } unless ref( $error );
1044 return { 'error' => '', amount => $amount, %$error };
1047 sub process_payment_order_pkg {
1050 my $hr = process_payment($p);
1051 return $hr if $hr->{'error'};
1056 sub process_payment_order_renew {
1059 my $hr = process_payment($p);
1060 return $hr if $hr->{'error'};
1065 sub process_prepay {
1069 my $session = _cache->get($p->{'session_id'})
1070 or return { 'error' => "Can't resume session" }; #better error message
1074 my $custnum = $session->{'custnum'};
1076 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1077 or return { 'error' => "unknown custnum $custnum" };
1079 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1080 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1088 return { 'error' => $error } if $error;
1090 return { 'error' => '',
1091 'amount' => $amount,
1092 'seconds' => $seconds,
1093 'duration' => duration_exact($seconds),
1094 'upbytes' => $upbytes,
1095 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1096 'downbytes' => $downbytes,
1097 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1098 'totalbytes'=> $totalbytes,
1099 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1106 my $session = _cache->get($p->{'session_id'})
1107 or return { 'error' => "Can't resume session" }; #better error message
1109 my $custnum = $session->{'custnum'};
1111 my $invnum = $p->{'invnum'};
1113 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1114 'custnum' => $custnum } )
1115 or return { 'error' => "Can't find invnum" };
1119 return { 'error' => '',
1120 'invnum' => $invnum,
1121 'invoice_text' => join('', $cust_bill->print_text ),
1122 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1129 my $session = _cache->get($p->{'session_id'})
1130 or return { 'error' => "Can't resume session" }; #better error message
1132 my $custnum = $session->{'custnum'};
1134 my $invnum = $p->{'invnum'};
1136 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1137 'custnum' => $custnum } )
1138 or return { 'error' => "Can't find invnum" };
1142 return { 'error' => '',
1143 'invnum' => $invnum,
1144 'invoice_pdf' => $cust_bill->print_pdf({
1145 'unsquelch_cdr' => 1,
1146 'locale' => $p->{'locale'},
1152 sub legacy_invoice {
1154 my $session = _cache->get($p->{'session_id'})
1155 or return { 'error' => "Can't resume session" }; #better error message
1157 my $custnum = $session->{'custnum'};
1159 my $legacyinvnum = $p->{'legacyinvnum'};
1162 'legacyinvnum' => $legacyinvnum,
1163 'custnum' => $custnum,
1166 my $legacy_cust_bill =
1167 qsearchs('legacy_cust_bill', { %hash, 'locale' => $p->{'locale'} } )
1168 || qsearchs('legacy_cust_bill', \%hash )
1169 or return { 'error' => "Can't find legacyinvnum" };
1173 return { 'error' => '',
1174 'legacyinvnum' => $legacyinvnum,
1175 'legacyid' => $legacy_cust_bill->legacyid,
1176 'invoice_html' => $legacy_cust_bill->content_html,
1181 sub legacy_invoice_pdf {
1183 my $session = _cache->get($p->{'session_id'})
1184 or return { 'error' => "Can't resume session" }; #better error message
1186 my $custnum = $session->{'custnum'};
1188 my $legacyinvnum = $p->{'legacyinvnum'};
1190 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1191 'legacyinvnum' => $legacyinvnum,
1192 'custnum' => $custnum,
1193 }) or return { 'error' => "Can't find legacyinvnum" };
1197 return { 'error' => '',
1198 'legacyinvnum' => $legacyinvnum,
1199 'legacyid' => $legacy_cust_bill->legacyid,
1200 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1208 #sessioning for this? how do we get the session id to the backend invoice
1209 # template so it can add it to the link, blah
1212 if ( $p->{'invnum'} ) {
1213 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1214 or return { 'error' => 'unknown invnum' };
1215 $agentnum = $cust_bill->cust_main->agentnum;
1218 my $templatename = $p->{'template'} || $p->{'templatename'};
1220 #false laziness-ish w/view/cust_bill-logo.cgi
1222 my $conf = new FS::Conf;
1223 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1224 $templatename = "_$1";
1229 my $filename = "logo$templatename.png";
1231 return { 'error' => '',
1232 'logo' => $conf->config_binary($filename, $agentnum),
1233 'content_type' => 'image/png', #should allow gif, jpg too
1240 my $session = _cache->get($p->{'session_id'})
1241 or return { 'error' => "Can't resume session" }; #better error message
1243 my $custnum = $session->{'custnum'};
1245 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1246 or return { 'error' => "unknown custnum $custnum" };
1248 my $conf = new FS::Conf;
1250 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1252 my @cust_bill = grep ! $_->hide, $cust_main->cust_bill;
1256 return { 'error' => '',
1257 'balance' => $cust_main->balance,
1260 my $owed = $_->owed;
1262 +{ 'invnum' => $_->invnum,
1263 '_date' => $_->_date,
1264 'date' => time2str("%b %o, %Y", $_->_date),
1265 'date_short' => time2str("%m-%d-%Y", $_->_date),
1266 'previous' => sprintf('%.2f', ($_->previous)[0]),
1267 'charged' => sprintf('%.2f', $_->charged),
1268 'owed' => sprintf('%.2f', $owed),
1269 'balance' => sprintf('%.2f', $balance),
1274 'legacy_invoices' => [
1276 +{ 'legacyinvnum' => $_->legacyinvnum,
1277 'legacyid' => $_->legacyid,
1278 '_date' => $_->_date,
1279 'date' => time2str("%b %o, %Y", $_->_date),
1280 'date_short' => time2str("%m-%d-%Y", $_->_date),
1281 'charged' => sprintf('%.2f', $_->charged),
1282 'has_content' => ( length($_->content_pdf)
1283 || length($_->content_html) ),
1293 my $session = _cache->get($p->{'session_id'})
1294 or return { 'error' => "Can't resume session" }; #better error message
1296 my $custnum = $session->{'custnum'};
1298 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1299 or return { 'error' => "unknown custnum $custnum" };
1301 my @errors = $cust_main->cancel( 'quiet'=>1 );
1303 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1305 return { 'error' => $error };
1312 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1313 return { 'error' => $session } if $context eq 'error';
1315 my $search = { 'custnum' => $custnum };
1316 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1317 my $cust_main = qsearchs('cust_main', $search )
1318 or return { 'error' => "unknown custnum $custnum" };
1320 my $conf = new FS::Conf;
1322 # the duplication below is necessary:
1323 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1324 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1325 # non-backwards-compatible change breaking the software of anyone using the API
1326 # instead of the stock selfservice
1327 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1328 if( $conf->exists('selfservice_server-view-wholesale') ) {
1329 return { 'svcnum' => $session->{'svcnum'},
1330 'custnum' => $custnum,
1331 'cust_pkg' => [ map {
1333 part_pkg => [ map $_->hashref, $_->part_pkg ],
1335 [ map $_->hashref, $_->available_part_svc ],
1337 [ map { my $ref = { $_->hash,
1338 label => [ $_->label ],
1340 $ref->{_password} = $_->svc_x->_password
1341 if $context eq 'agent'
1342 && $conf->exists('agent-showpasswords')
1343 && $_->part_svc->svcdb eq 'svc_acct';
1348 } $cust_main->cust_pkg
1351 small_custview( $cust_main, $conf->config('countrydefault') ),
1352 'wholesale_view' => 1,
1353 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1354 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1355 'lnp' => $conf->exists('svc_phone-lnp'),
1359 { 'svcnum' => $session->{'svcnum'},
1360 'custnum' => $custnum,
1361 'cust_pkg' => [ map {
1362 my $primary_cust_svc = $_->primary_cust_svc;
1365 status => $_->status,
1367 [ map $_->hashref, $_->available_part_svc ],
1369 [ map { my $ref = { $_->hash,
1370 label => [ $_->label ],
1372 $ref->{_password} = $_->svc_x->_password
1373 if $context eq 'agent'
1374 && $conf->exists('agent-showpasswords')
1375 && $_->part_svc->svcdb eq 'svc_acct';
1376 $ref->{svchash} = { $_->svc_x->hash } if
1377 $_->part_svc->svcdb eq 'svc_phone';
1378 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1379 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1385 ? { $primary_cust_svc->hash,
1386 label => [ $primary_cust_svc->label ],
1387 finger => $primary_cust_svc->svc_x->finger, #uuh
1388 $primary_cust_svc->part_svc->hash,
1392 } $cust_main->ncancelled_pkgs
1395 small_custview( $cust_main, $conf->config('countrydefault') ),
1403 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1404 return { 'error' => $session } if $context eq 'error';
1406 my $search = { 'custnum' => $custnum };
1407 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1408 my $cust_main = qsearchs('cust_main', $search )
1409 or return { 'error' => "unknown custnum $custnum" };
1411 my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
1412 if ( ! $pkgnum && $p->{'svcnum'} ) {
1413 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
1414 $pkgnum = $cust_svc->pkgnum if $cust_svc;
1418 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1419 foreach my $cust_pkg ( $p->{'ncancelled'}
1420 ? $cust_main->ncancelled_pkgs
1421 : $cust_main->unsuspended_pkgs ) {
1422 next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
1423 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1425 if ( $p->{'svcdb'} ) {
1426 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1428 : ref($p->{'svcdb'}) eq 'ARRAY'
1429 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1430 : { $p->{'svcdb'} => 1 };
1431 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1434 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1437 my $conf = new FS::Conf;
1440 'svcnum' => $session->{'svcnum'},
1441 'custnum' => $custnum,
1442 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1443 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1446 my $svc_x = $_->svc_x;
1447 my($label, $value) = $_->label;
1448 my $svcdb = $_->part_svc->svcdb;
1449 my $cust_pkg = $_->cust_pkg;
1450 my $part_pkg = $cust_pkg->part_pkg;
1453 'svcnum' => $_->svcnum,
1457 'pkg_status' => $cust_pkg->status,
1460 if ( $svcdb eq 'svc_acct' ) {
1463 'username' => $svc_x->username,
1464 'email' => $svc_x->email,
1465 'finger' => $svc_x->finger,
1466 'seconds' => $svc_x->seconds,
1467 'upbytes' => display_bytecount($svc_x->upbytes),
1468 'downbytes' => display_bytecount($svc_x->downbytes),
1469 'totalbytes' => display_bytecount($svc_x->totalbytes),
1471 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1472 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1473 'recharge_upbytes' =>
1474 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1475 'recharge_downbytes' =>
1476 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1477 'recharge_totalbytes' =>
1478 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1482 } elsif ( $svcdb eq 'svc_dsl' ) {
1483 $hash{'phonenum'} = $svc_x->phonenum;
1484 if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
1485 $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
1486 $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
1489 $hash{'name'} = $cust_main->name;
1492 # elsif ( $svcdb eq 'svc_phone' || $svcdb eq 'svc_port' ) {
1506 sub _customer_svc_x {
1507 my($custnum, $svcnum, $table) = (shift, shift, shift);
1508 my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
1510 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1511 my $search = " AND custnum = $1";
1512 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1515 'table' => ($table || 'svc_acct'),
1516 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1517 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
1518 #'LEFT JOIN cust_main USING ( custnum ) ',
1519 'hashref' => $hashref,
1520 'extra_sql' => $search, #important
1525 sub svc_status_html {
1528 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1529 return { 'error' => $session } if $context eq 'error';
1531 #XXX only svc_dsl for now
1532 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
1533 or return { 'error' => "Service not found" };
1535 my $html = $svc_x->getstatus_html;
1537 return { 'html' => $html };
1541 sub acct_forward_info {
1544 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1545 return { 'error' => $session } if $context eq 'error';
1547 my $svc_forward = _customer_svc_x( $custnum,
1548 { 'srcsvc' => $p->{'svcnum'} },
1551 or return { 'error' => '',
1555 return { 'error' => '',
1556 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
1561 sub process_acct_forward {
1563 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1564 return { 'error' => $session } if $context eq 'error';
1566 my $old = _customer_svc_x( $custnum,
1567 { 'srcsvc' => $p->{'svcnum'} },
1571 if ( $p->{'dst'} eq '' ) {
1573 my $error = $old->delete;
1574 return { 'error' => $error };
1576 return { 'error' => '' };
1579 my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
1580 'dst' => $p->{'dst'},
1585 $new->svcnum($old->svcnum);
1586 my $cust_svc = $old->cust_svc;
1587 $new->svcpart($old->svcpart);
1588 $new->pkgnuym($old->pkgnum);
1589 $error = $new->replace($old);
1591 my $conf = new FS::Conf;
1592 $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
1594 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
1595 or return { 'error' => 'No service' }; #how would we even get here?
1597 $new->pkgnum( $svc_acct->cust_svc->pkgnum );
1599 $error = $new->insert;
1602 return { 'error' => $error };
1606 sub list_dsl_devices {
1609 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1610 return { 'error' => $session } if $context eq 'error';
1612 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1613 or return { 'error' => "Service not found" };
1616 'devices' => [ map {
1617 +{ 'mac_addr' => $_->mac_addr };
1618 } $svc_dsl->dsl_device
1624 sub add_dsl_device {
1627 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1628 return { 'error' => $session } if $context eq 'error';
1630 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1631 or return { 'error' => "Service not found" };
1633 return { 'error' => 'No MAC address supplied' }
1634 unless length($p->{'mac_addr'});
1636 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
1637 'mac_addr' => scalar($p->{'mac_addr'}),
1639 my $error = $dsl_device->insert;
1640 return { 'error' => $error };
1644 sub delete_dsl_device {
1647 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1648 return { 'error' => $session } if $context eq 'error';
1650 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1651 or return { 'error' => "Service not found" };
1653 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
1654 'mac_addr' => scalar($p->{'mac_addr'}),
1657 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
1659 my $error = $dsl_device->delete;
1660 return { 'error' => $error };
1666 _usage_details( \&_port_graph, $p,
1667 'svcdb' => 'svc_port',
1672 my($svc_port, $begin, $end) = @_;
1674 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
1675 push @usage, { 'png' => $pngOrError };
1679 sub _list_svc_usage {
1680 my($svc_acct, $begin, $end) = @_;
1682 foreach my $part_export (
1683 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1684 qw( sqlradius sqlradius_withdomain )
1686 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1691 sub list_svc_usage {
1692 _usage_details(\&_list_svc_usage, @_);
1695 sub _list_support_usage {
1696 my($svc_acct, $begin, $end) = @_;
1698 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1699 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1701 push @usage, { 'seconds' => $_->seconds,
1702 'support' => $_->support,
1703 '_date' => $_->_date,
1704 'id' => $_->transaction_id,
1705 'creator' => $_->creator,
1706 'subject' => $_->subject,
1707 'status' => $_->status,
1708 'ticketid' => $_->ticketid,
1714 sub list_support_usage {
1715 _usage_details(\&_list_support_usage, @_);
1718 sub _list_cdr_usage {
1719 # XXX CDR type support...
1720 my($svc_phone, $begin, $end, %opt) = @_;
1721 map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
1722 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1725 sub list_cdr_usage {
1727 _usage_details( \&_list_cdr_usage, $p,
1728 'svcdb' => 'svc_phone',
1732 sub _usage_details {
1733 my($callback, $p, %opt) = @_;
1735 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1736 return { 'error' => $session } if $context eq 'error';
1738 my $search = { 'svcnum' => $p->{'svcnum'} };
1739 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1741 my $svcdb = $opt{'svcdb'} || 'svc_acct';
1743 my $svc_x = qsearchs( $svcdb, $search );
1744 return { 'error' => 'No service selected in list_svc_usage' }
1747 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
1748 my $freq = $cust_pkg->part_pkg->freq;
1751 if ( $svcdb eq 'svc_phone' ) {
1752 my $format = $cust_pkg->part_pkg->option('output_format') || '';
1753 $format = '' if $format =~ /^sum_/;
1754 # sensible default if there is no format or it's a summary format
1755 if ( $cust_pkg->part_pkg->plan eq 'voip_inbound' ) {
1756 $format ||= 'source_default';
1757 $callback_opt{inbound} = 1;
1760 $format ||= 'default';
1763 $callback_opt{format} = $format;
1764 $header = [ split(',', FS::cdr::invoice_header($format) ) ];
1767 my $start = $cust_pkg->setup;
1768 #my $end = $cust_pkg->bill; # or time?
1771 unless ( $p->{beginning} ) {
1772 $p->{beginning} = $cust_pkg->last_bill;
1773 $p->{ending} = $end;
1776 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
1780 #kinda false laziness with FS::cust_main::bill, but perhaps
1781 #we should really change this bit to DateTime and DateTime::Duration
1783 #change this bit to use Date::Manip? CAREFUL with timezones (see
1784 # mailing list archive)
1785 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
1786 (localtime($p->{ending}) )[0,1,2,3,4,5];
1787 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
1788 (localtime($p->{beginning}) )[0,1,2,3,4,5];
1790 if ( $freq =~ /^\d+$/ ) {
1792 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
1794 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
1795 } elsif ( $freq =~ /^(\d+)w$/ ) {
1797 $nmday += $weeks * 7;
1798 $pmday -= $weeks * 7;
1799 } elsif ( $freq =~ /^(\d+)d$/ ) {
1803 } elsif ( $freq =~ /^(\d+)h$/ ) {
1808 return { 'error' => "unparsable frequency: ". $freq };
1811 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
1812 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
1816 'svcnum' => $p->{svcnum},
1817 'beginning' => $p->{beginning},
1818 'ending' => $p->{ending},
1819 'previous' => ($previous > $start) ? $previous : $start,
1820 'next' => ($next < $end) ? $next : $end,
1821 'header' => $header,
1829 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1830 return { 'error' => $session } if $context eq 'error';
1832 my $search = { 'custnum' => $custnum };
1833 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1834 my $cust_main = qsearchs('cust_main', $search )
1835 or return { 'error' => "unknown custnum $custnum" };
1837 my $status = $cust_main->status;
1838 #false laziness w/ClientAPI/Signup.pm
1840 my $cust_pkg = new FS::cust_pkg ( {
1841 'custnum' => $custnum,
1842 'pkgpart' => $p->{'pkgpart'},
1844 my $error = $cust_pkg->check;
1845 return { 'error' => $error } if $error;
1848 unless ( $p->{'svcpart'} eq 'none' ) {
1852 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
1854 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1855 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
1856 $svcdb = $part_svc->svcdb;
1858 $svcdb = 'svc_acct';
1860 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
1863 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
1864 'svc_domain' => [ qw( domain ) ],
1865 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
1866 'svc_external' => [ qw( id title ) ],
1867 'svc_pbx' => [ qw( id title ) ],
1870 my $svc_x = "FS::$svcdb"->new( {
1871 'svcpart' => $svcpart,
1872 map { $_ => $p->{$_} } @{$fields{$svcdb}}
1875 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
1878 while ( length($p->{"snarf_machine$snarfnum"}) ) {
1879 my $acct_snarf = new FS::acct_snarf ( {
1880 'machine' => $p->{"snarf_machine$snarfnum"},
1881 'protocol' => $p->{"snarf_protocol$snarfnum"},
1882 'username' => $p->{"snarf_username$snarfnum"},
1883 '_password' => $p->{"snarf_password$snarfnum"},
1886 push @acct_snarf, $acct_snarf;
1888 $svc_x->child_objects( \@acct_snarf );
1891 my $y = $svc_x->setdefault; # arguably should be in new method
1892 return { 'error' => $y } if $y && !ref($y);
1894 $error = $svc_x->check;
1895 return { 'error' => $error } if $error;
1902 tie my %hash, 'Tie::RefHash';
1903 %hash = ( $cust_pkg => \@svc );
1905 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
1906 return { 'error' => $error } if $error;
1908 my $conf = new FS::Conf;
1909 if ( $conf->exists('signup_server-realtime') ) {
1911 my $bill_error = _do_bop_realtime( $cust_main, $status );
1914 $cust_pkg->cancel('quiet'=>1);
1917 $cust_pkg->reexport;
1921 $cust_pkg->reexport;
1924 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
1926 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
1933 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1934 return { 'error' => $session } if $context eq 'error';
1936 my $search = { 'custnum' => $custnum };
1937 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1938 my $cust_main = qsearchs('cust_main', $search )
1939 or return { 'error' => "unknown custnum $custnum" };
1941 my $status = $cust_main->status;
1942 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
1943 or return { 'error' => "unknown package $p->{pkgnum}" };
1946 my $error = FS::cust_pkg::order( $custnum,
1952 my $conf = new FS::Conf;
1953 if ( $conf->exists('signup_server-realtime') ) {
1955 my $bill_error = _do_bop_realtime( $cust_main, $status );
1958 $newpkg[0]->suspend;
1961 $newpkg[0]->reexport;
1965 $newpkg[0]->reexport;
1968 return { error => '', pkgnum => $cust_pkg->pkgnum };
1972 sub order_recharge {
1975 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1976 return { 'error' => $session } if $context eq 'error';
1978 my $search = { 'custnum' => $custnum };
1979 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1980 my $cust_main = qsearchs('cust_main', $search )
1981 or return { 'error' => "unknown custnum $custnum" };
1983 my $status = $cust_main->status;
1984 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
1985 or return { 'error' => "unknown service " . $p->{'svcnum'} };
1987 my $svc_x = $cust_svc->svc_x;
1988 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
1991 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
1992 qw ( recharge_seconds recharge_upbytes recharge_downbytes
1993 recharge_totalbytes );
1994 my $amount = $part_pkg->option('recharge_amount', 1);
1996 my ($l, $v, $d) = $cust_svc->label; # blah
1997 my $pkg = "Recharge $v";
1999 my $bill_error = $cust_main->charge($amount, $pkg,
2000 "time: $vhash{seconds}, up: $vhash{upbytes}," .
2001 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
2002 $part_pkg->taxclass); #meh
2004 my $conf = new FS::Conf;
2005 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
2007 $bill_error = _do_bop_realtime( $cust_main, $status );
2012 my $error = $svc_x->recharge (\%vhash);
2013 return { 'error' => $error } if $error;
2017 my $error = $bill_error;
2018 $error ||= $svc_x->recharge (\%vhash);
2019 return { 'error' => $error } if $error;
2022 return { error => '', svc => $cust_svc->part_svc->svc };
2026 sub _do_bop_realtime {
2027 my ($cust_main, $status) = (shift, shift);
2029 my $old_balance = $cust_main->balance;
2031 my $bill_error = $cust_main->bill
2032 || $cust_main->apply_payments_and_credits
2033 || $cust_main->realtime_collect('selfservice' => 1);
2035 if ( $cust_main->balance > $old_balance
2036 && $cust_main->balance > 0
2037 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
2038 1 : $status eq 'suspended' ) ) {
2039 #this makes sense. credit is "un-doing" the invoice
2040 my $conf = new FS::Conf;
2041 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
2042 'self-service decline',
2043 'reason_type' => $conf->config('signup_credit_type'),
2045 $cust_main->apply_credits( 'order' => 'newest' );
2047 return { 'error' => '_decline', 'bill_error' => $bill_error };
2056 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2057 return { 'error' => $session } if $context eq 'error';
2059 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2060 or return { 'error' => "unknown custnum $custnum" };
2062 my @cust_pkg = sort { $a->bill <=> $b->bill }
2063 grep { $_->part_pkg->freq ne '0' }
2064 $cust_main->ncancelled_pkgs;
2066 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
2068 my $total = $cust_main->balance;
2071 my $bill = $_->bill;
2072 $total += $_->part_pkg->base_recur($_, \$bill);
2073 my $renew_date = $_->part_pkg->add_freq($_->bill);
2075 'pkgnum' => $_->pkgnum,
2076 'amount' => sprintf('%.2f', $total),
2077 'bill_date' => $_->bill,
2078 'bill_date_pretty' => time2str('%x', $_->bill),
2079 'renew_date' => $renew_date,
2080 'renew_date_pretty' => time2str('%x', $renew_date),
2081 'expire_date' => $_->expire,
2082 'expire_date_pretty' => time2str('%x', $_->expire),
2087 return { 'dates' => \@array };
2091 sub payment_info_renew_info {
2093 my $renew_info = renew_info($p);
2094 my $payment_info = payment_info($p);
2095 return { %$renew_info,
2103 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2104 return { 'error' => $session } if $context eq 'error';
2106 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2107 or return { 'error' => "unknown custnum $custnum" };
2109 my $date = $p->{'date'};
2113 #freeside-daily -n -d $date fs_daily $custnum
2114 $cust_main->bill_and_collect( 'time' => $date,
2115 'invoice_time' => $now,
2116 'actual_time' => $now,
2117 'check_freq' => '1d',
2120 return { 'error' => '' };
2126 my $session = _cache->get($p->{'session_id'})
2127 or return { 'error' => "Can't resume session" }; #better error message
2129 my $custnum = $session->{'custnum'};
2131 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2132 or return { 'error' => "unknown custnum $custnum" };
2134 my $conf = new FS::Conf;
2136 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2137 or return { 'error' => 'Permission denied' };
2139 my $pkgnum = $p->{'pkgnum'};
2141 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2142 'pkgnum' => $pkgnum, } )
2143 or return { 'error' => "unknown pkgnum $pkgnum" };
2145 my $error = $cust_pkg->suspend(reason => $reasonnum);
2146 return { 'error' => $error };
2152 my $session = _cache->get($p->{'session_id'})
2153 or return { 'error' => "Can't resume session" }; #better error message
2155 my $custnum = $session->{'custnum'};
2157 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2158 or return { 'error' => "unknown custnum $custnum" };
2160 my $pkgnum = $p->{'pkgnum'};
2162 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2163 'pkgnum' => $pkgnum, } )
2164 or return { 'error' => "unknown pkgnum $pkgnum" };
2166 my $error = $cust_pkg->cancel('quiet' => 1);
2167 return { 'error' => $error };
2171 sub provision_phone {
2174 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2176 if($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/){
2177 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2178 return { 'error' => $session } if $context eq 'error';
2180 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2181 return { 'error' => 'service not found' } unless $svc_phone;
2182 return { 'error' => 'invalid svcnum' }
2183 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2185 $svc_phone->email($p->{'email'})
2186 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2187 $svc_phone->forwarddst($p->{'forwarddst'})
2188 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2189 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2190 return { 'error' => $svc_phone->replace };
2194 unless($p->{'lnp'}) {
2195 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2196 $p->{'lnp_status'} = "portingin";
2197 return _provision( 'FS::svc_phone',
2198 [qw(lnp_desired_due_date lnp_other_provider
2199 lnp_other_provider_account phonenum countrycode lnp_status)],
2200 [qw(phonenum countrycode)],
2207 unless (scalar(@bulkdid)) {
2208 return _provision( 'FS::svc_phone',
2209 [qw(phonenum countrycode)],
2210 [qw(phonenum countrycode)],
2216 # bulk DID order case
2218 foreach my $did ( @bulkdid ) {
2219 $did =~ s/[^0-9]//g;
2220 $error = _provision( 'FS::svc_phone',
2221 [qw(phonenum countrycode)],
2222 [qw(phonenum countrycode)],
2224 'pkgnum' => $p->{'pkgnum'},
2225 'svcpart' => $p->{'svcpart'},
2227 'countrycode' => $p->{'countrycode'},
2228 'session_id' => $p->{'session_id'},
2231 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2233 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2236 sub provision_acct {
2238 warn "provision_acct called\n"
2241 return { 'error' => gettext('passwords_dont_match') }
2242 if $p->{'_password'} ne $p->{'_password2'};
2243 return { 'error' => gettext('empty_password') }
2244 unless length($p->{'_password'});
2246 if ($p->{'domsvc'}) {
2247 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
2248 qw ( svcpart pkgnum ) );
2249 return { 'error' => gettext('invalid_domain') }
2250 unless ($domains{$p->{'domsvc'}});
2253 warn "provision_acct calling _provision\n"
2255 _provision( 'FS::svc_acct',
2256 [qw(username _password domsvc)],
2257 [qw(username _password domsvc)],
2263 sub provision_external {
2265 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
2266 _provision( 'FS::svc_external',
2275 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
2276 warn "_provision called for $class\n"
2279 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2280 return { 'error' => $session } if $context eq 'error';
2282 my $search = { 'custnum' => $custnum };
2283 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2284 my $cust_main = qsearchs('cust_main', $search )
2285 or return { 'error' => "unknown custnum $custnum" };
2287 my $pkgnum = $p->{'pkgnum'};
2289 warn "searching for custnum $custnum pkgnum $pkgnum\n"
2291 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2292 'pkgnum' => $pkgnum,
2294 or return { 'error' => "unknown pkgnum $pkgnum" };
2296 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
2298 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
2299 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
2301 warn "creating $class record\n"
2303 my $svc_x = $class->new( {
2304 'pkgnum' => $p->{'pkgnum'},
2305 'svcpart' => $p->{'svcpart'},
2306 map { $_ => $p->{$_} } @$fields
2308 warn "inserting $class record\n"
2310 my $error = $svc_x->insert;
2313 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
2315 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
2318 my $return = { 'svc' => $part_svc->svc,
2320 map { $_ => $svc_x->get($_) } @$return_fields
2322 warn "_provision returning ". Dumper($return). "\n"
2331 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2332 return { 'error' => $session } if $context eq 'error';
2334 my $search = { 'custnum' => $custnum };
2335 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2336 my $cust_main = qsearchs('cust_main', $search )
2337 or return { 'error' => "unknown custnum $custnum" };
2339 my $pkgnum = $p->{'pkgnum'};
2341 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2342 'pkgnum' => $pkgnum,
2344 or return { 'error' => "unknown pkgnum $pkgnum" };
2346 my $svcpart = $p->{'svcpart'};
2348 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
2349 'svcpart' => $svcpart, } )
2350 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
2351 my $part_svc = $pkg_svc->part_svc;
2353 my $conf = new FS::Conf;
2356 'svc' => $part_svc->svc,
2357 'svcdb' => $part_svc->svcdb,
2358 'pkgnum' => $pkgnum,
2359 'svcpart' => $svcpart,
2360 'custnum' => $custnum,
2362 'security_phrase' => 0, #XXX !
2363 'svc_acct_pop' => [], #XXX !
2365 'init_popstate' => '',
2370 small_custview( $cust_main, $conf->config('countrydefault') ),
2374 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
2375 && $ret->{'svcdb'} eq 'svc_phone') {
2376 $ret->{'svcnum'} = $p->{'svcnum'};
2377 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2378 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
2379 $ret->{'email'} = $svc_phone->email;
2380 $ret->{'forwarddst'} = $svc_phone->forwarddst;
2387 sub unprovision_svc {
2390 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2391 return { 'error' => $session } if $context eq 'error';
2393 my $search = { 'custnum' => $custnum };
2394 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2395 my $cust_main = qsearchs('cust_main', $search )
2396 or return { 'error' => "unknown custnum $custnum" };
2398 my $svcnum = $p->{'svcnum'};
2400 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
2401 or return { 'error' => "unknown svcnum $svcnum" };
2403 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
2404 unless $cust_svc->cust_pkg->custnum == $custnum;
2406 my $conf = new FS::Conf;
2408 return { 'svc' => $cust_svc->part_svc->svc,
2409 'error' => $cust_svc->cancel,
2411 small_custview( $cust_main, $conf->config('countrydefault') ),
2416 sub myaccount_passwd {
2418 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2419 return { 'error' => $session } if $context eq 'error';
2421 return { 'error' => "New passwords don't match." }
2422 if $p->{'new_password'} ne $p->{'new_password2'};
2424 return { 'error' => 'Enter new password' }
2425 unless length($p->{'new_password'});
2427 #my $search = { 'custnum' => $custnum };
2428 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2429 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2430 my $search = " AND custnum = $1";
2431 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2433 my $svc_acct = qsearchs( {
2434 'table' => 'svc_acct',
2435 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2436 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
2437 'LEFT JOIN cust_main USING ( custnum ) ',
2438 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
2439 'extra_sql' => $search, #important
2441 or return { 'error' => "Service not found" };
2443 if ( exists($p->{'old_password'}) ) {
2444 return { 'error' => "Incorrect password." }
2445 unless $svc_acct->check_password($p->{'old_password'});
2448 $svc_acct->_password($p->{'new_password'});
2449 my $error = $svc_acct->replace();
2451 my($label, $value) = $svc_acct->cust_svc->label;
2453 return { 'error' => $error,
2463 my $conf = new FS::Conf;
2464 my $verification = $conf->config('selfservice-password_reset_verification')
2465 or return { 'error' => 'Password resets disabled' };
2467 my $username = $p->{'username'};
2469 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
2470 or return { 'error' => 'Account not found' };
2472 my $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
2473 'domsvc' => $svc_domain->svcnum }
2475 or return { 'error' => 'Account not found' };
2477 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
2478 or return { 'error' => 'Account not found' };
2480 my $cust_main = $cust_pkg->cust_main;
2484 my( $p, $cust_main ) = @_;
2485 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
2486 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
2489 my( $p, $cust_main ) = @_;
2490 my $cust_pay = qsearchs({
2491 'table' => 'cust_pay',
2492 'hashref' => { 'custnum' => $cust_main->custnum },
2493 'order_by' => 'ORDER BY _date DESC LIMIT 1',
2497 $p->{'amount'} == $cust_pay->paid;
2500 my( $p, $cust_main ) = @_;
2501 $p->{'zip'} eq $cust_main->zip
2502 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
2506 foreach my $verify ( split(',', $verification) ) {
2508 &{ $verify{$verify} }( $p, $cust_main )
2509 or return { 'error' => 'Account not found' };
2513 #okay, we're verified, now create a unique session
2515 my $reset_session = {
2516 'svcnum' => $svc_acct->svcnum,
2519 my $timeout = '1 hour'; #?
2521 my $reset_session_id;
2523 $reset_session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
2524 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") ); #just in case
2526 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
2530 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $cust_main->agentnum);
2531 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
2532 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
2533 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
2534 my $error = $msg_template->send( 'cust_main' => $cust_main,
2535 'object' => $svc_acct,
2536 'substitutions' => {
2537 'session_id' => $reset_session_id,
2541 return { 'error' => $error }; #????
2544 return { 'error' => '' };
2547 sub check_reset_passwd {
2550 my $conf = new FS::Conf;
2551 my $verification = $conf->config('selfservice-password_reset_verification')
2552 or return { 'error' => 'Password resets disabled' };
2554 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
2555 or return { 'error' => "Can't resume session" }; #better error message
2557 my $svcnum = $reset_session->{'svcnum'};
2559 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
2560 or return { 'error' => "Service not found" };
2562 return { 'error' => '',
2563 'username' => $svc_acct->username,
2568 sub process_reset_passwd {
2571 my $conf = new FS::Conf;
2572 my $verification = $conf->config('selfservice-password_reset_verification')
2573 or return { 'error' => 'Password resets disabled' };
2575 return { 'error' => "New passwords don't match." }
2576 if $p->{'new_password'} ne $p->{'new_password2'};
2578 return { 'error' => 'Enter new password' }
2579 unless length($p->{'new_password'});
2581 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
2582 or return { 'error' => "Can't resume session" }; #better error message
2584 my $svcnum = $reset_session->{'svcnum'};
2586 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
2587 or return { 'error' => "Service not found" };
2589 $svc_acct->_password($p->{'new_password'});
2590 my $error = $svc_acct->replace();
2592 my($label, $value) = $svc_acct->cust_svc->label;
2594 return { 'error' => $error,
2603 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2604 return { 'error' => $session } if $context eq 'error';
2606 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
2607 FS::TicketSystem->init();
2609 my $conf = new FS::Conf;
2610 my $queue = $p->{'queue'}
2611 || $conf->config('ticket_system-selfservice_queueid')
2612 || $conf->config('ticket_system-default_queueid');
2614 warn "$me create_ticket: creating ticket\n" if $DEBUG;
2615 my $err_or_ticket = FS::TicketSystem->create_ticket(
2616 '', #create RT session based on FS CurrentUser (fs_selfservice)
2618 'custnum' => $custnum,
2619 'svcnum' => $session->{'svcnum'},
2620 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
2623 if ( ref($err_or_ticket) ) {
2624 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
2626 return { 'error' => '',
2627 'ticket_id' => $err_or_ticket->id,
2630 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
2632 return { 'error' => $err_or_ticket };
2640 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2641 return { 'error' => $session } if $context eq 'error';
2643 return { error => 'requested format not implemented' }
2644 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
2646 my $conf = new FS::Conf;
2647 my $age_threshold = 0;
2648 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
2649 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
2651 my $search = { 'custnum' => $custnum };
2652 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2653 my $cust_main = qsearchs('cust_main', $search )
2654 or return { 'error' => "unknown custnum $custnum" };
2656 # does it make more sense to just run one sql query for this instead of all the
2657 # insanity below? would increase performance greately for large data sets?
2659 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
2660 my @part_svc = $cust_pkg->part_svc;
2661 foreach my $part_svc ( @part_svc ) {
2662 if($part_svc->svcdb eq 'svc_phone'){
2663 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
2664 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
2665 push @svc_phone, $cust_pkg_svc->svc_x
2666 if $cust_pkg_svc->date_inserted >= $age_threshold;
2674 my($xls_r,$xls_c) = (0,0);
2677 my @fields = qw( countrycode phonenum pin sip_password phone_name );
2678 if($p->{'format'} eq 'csv') {
2679 $csv = new Text::CSV_XS { 'always_quote' => 1,
2682 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
2683 $content .= $csv->string;
2685 elsif($p->{'format'} eq 'xls') {
2686 my $XLS1 = new IO::Scalar \$content;
2687 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
2688 or return { 'error' => "Error opening .xls file: $!" };
2689 $xls = $xls_workbook->add_worksheet('DIDs');
2690 foreach ( @fields ) {
2691 $xls->write(0,$xls_c++,$_);
2696 foreach my $svc_phone ( @svc_phone ) {
2697 my @cols = map { $svc_phone->$_ } @fields;
2698 if($p->{'format'} eq 'csv') {
2699 return { 'error' => 'Unable to create CSV' }
2700 unless $csv->combine(@cols);
2701 $content .= $csv->string;
2703 elsif($p->{'format'} eq 'xls') {
2706 $xls->write($xls_r,$xls_c++,$_);
2712 $xls_workbook->close() if $p->{'format'} eq 'xls';
2714 { content => $content, format => $p->{'format'}, };
2719 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2720 return { 'error' => $session } if $context eq 'error';
2722 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
2723 FS::TicketSystem->init();
2724 return { 'error' => 'get_ticket configuration error' }
2725 if $FS::TicketSystem::system ne 'RT_Internal';
2727 # check existence and ownership as part of this
2728 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
2729 my $rt_session = FS::TicketSystem->session('');
2730 my $Ticket = FS::TicketSystem->get_ticket_object(
2732 ticket_id => $p->{'ticket_id'},
2735 return { 'error' => 'ticket not found' } if !$Ticket;
2737 if ( length( $p->{'subject'} || '' ) ) {
2739 if ( $p->{'subject'} ne $Ticket->Subject ) {
2740 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
2741 return { 'error' => "unable to set subject: $msg" } if !$val;
2745 if(length($p->{'reply'})) {
2746 my @err_or_res = FS::TicketSystem->correspond_ticket(
2748 'ticket_id' => $p->{'ticket_id'},
2749 'content' => $p->{'reply'},
2752 return { 'error' => 'unable to reply to ticket' }
2753 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
2756 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
2757 my $err_or_ticket = FS::TicketSystem->get_ticket(
2759 'ticket_id' => $p->{'ticket_id'},
2762 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
2763 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
2765 return { 'error' => $err_or_ticket };
2768 my @custs = @{$err_or_ticket->{'custs'}};
2769 my @txns = @{$err_or_ticket->{'txns'}};
2772 # superseded by check in get_ticket_object
2773 #return { 'error' => 'invalid ticket requested' }
2774 #unless grep($_ eq $custnum, @custs);
2776 foreach my $txn ( @txns ) {
2777 push @filtered_txns, $txn
2778 if ($txn->{'type'} eq 'EmailRecord'
2779 || $txn->{'type'} eq 'Correspond'
2780 || $txn->{'type'} eq 'Create');
2783 warn "$me get_ticket: successful: \n"
2785 return { 'error' => '',
2786 'transactions' => \@filtered_txns,
2787 'ticket_fields' => $err_or_ticket->{'fields'},
2788 'ticket_id' => $p->{'ticket_id'},
2792 sub adjust_ticket_priority {
2794 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2795 return { 'error' => $session } if $context eq 'error';
2797 warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
2798 FS::TicketSystem->init;
2799 my $ss_priority = FS::TicketSystem->selfservice_priority;
2801 return { 'error' => 'adjust_ticket_priority configuration error' }
2802 if $FS::TicketSystem::system ne 'RT_Internal'
2805 my $values = $p->{'values'}; #hashref, id => priority value
2808 foreach my $id (keys %$values) {
2809 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
2810 my $Ticket = FS::TicketSystem->get_ticket_object('',
2812 'custnum' => $custnum,
2815 $ticket_error{$id} = 'ticket not found';
2819 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
2820 # We're not going to implement it for RT_External.
2821 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
2822 my $new_value = $values->{$id};
2823 next if $old_value eq $new_value;
2825 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
2827 # AddCustomFieldValue works fine (replacing any existing value) if it's
2828 # a single-valued custom field, which it should be. If it's not, you're
2829 # doing something wrong.
2831 if ( length($new_value) ) {
2832 ($val, $msg) = $Ticket->AddCustomFieldValue(
2833 Field => $ss_priority,
2834 Value => $new_value,
2838 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
2839 Field => $ss_priority,
2840 Value => $old_value,
2844 $ticket_error{$id} = $msg if !$val;
2845 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
2847 return { 'error' => '',
2848 'ticket_error' => \%ticket_error,
2849 %{ customer_info($p) } # send updated customer info back
2855 sub _custoragent_session_custnum {
2858 my($context, $session, $custnum);
2859 if ( $p->{'session_id'} ) {
2861 $context = 'customer';
2862 $session = _cache->get($p->{'session_id'})
2863 or return ( 'error' => "Can't resume session" ); #better error message
2864 $custnum = $session->{'custnum'};
2866 } elsif ( $p->{'agent_session_id'} ) {
2869 my $agent_cache = new FS::ClientAPI_SessionCache( {
2870 'namespace' => 'FS::ClientAPI::Agent',
2872 $session = $agent_cache->get($p->{'agent_session_id'})
2873 or return ( 'error' => "Can't resume session" ); #better error message
2874 $custnum = $p->{'custnum'};
2878 return ( 'error' => "Can't resume session" ); #better error message
2881 ($context, $session, $custnum);