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;
38 use FS::legacy_cust_bill;
39 use FS::cust_main_county;
42 use FS::acct_rt_transaction;
46 $me = '[FS::ClientAPI::MyAccount]';
48 use vars qw( @cust_main_editable_fields );
49 @cust_main_editable_fields = qw(
50 first last company address1 address2 city
51 county state zip country
52 daytime night fax mobile
53 ship_first ship_last ship_company ship_address1 ship_address2 ship_city
54 ship_state ship_zip ship_country
55 ship_daytime ship_night ship_fax ship_mobile
57 payby payinfo payname paystart_month paystart_year payissue payip
58 ss paytype paystate stateid stateid_state
61 BEGIN { #preload to reduce time customer_info takes
62 if ( $FS::TicketSystem::system ) {
63 warn "$me: initializing ticket system\n" if $DEBUG;
64 FS::TicketSystem->init();
69 $cache ||= new FS::ClientAPI_SessionCache( {
70 'namespace' => 'FS::ClientAPI::MyAccount',
77 my($context, $session, $custnum) = _custoragent_session_custnum($p);
78 #return { 'error' => $session } if $context eq 'error';
81 if ( $context eq 'customer' ) {
83 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
86 $sth->execute($custnum) or die $sth->errstr;
88 $agentnum = $sth->fetchrow_arrayref->[0]
89 or die "no agentnum for custnum $custnum";
91 #} elsif ( $context eq 'agent' ) {
92 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
96 my $conf = new FS::Conf;
98 #false laziness w/Signup.pm
100 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
102 if ( $skin_info_cache_agent ) {
104 warn "$me loading cached skin info for agentnum $agentnum\n"
109 warn "$me populating skin info cache for agentnum $agentnum\n"
112 $skin_info_cache_agent = {
113 'agentnum' => $agentnum,
114 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
115 qw( company_name date_format ) ),
116 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
117 qw( body_bgcolor box_bgcolor
118 text_color link_color vlink_color hlink_color alink_color
119 font title_color title_align title_size menu_bgcolor menu_fontsize
122 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
123 qw( menu_skipblanks menu_skipheadings menu_nounderline )
125 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
126 qw( title_left_image title_right_image
127 menu_top_image menu_body_image menu_bottom_image
130 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
131 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
132 qw( head body_header body_footer company_address ) ),
135 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
139 #{ %$skin_info_cache_agent };
140 $skin_info_cache_agent;
147 my $conf = new FS::Conf;
151 'phone_login' => $conf->exists('selfservice_server-phone_login'),
152 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
159 #false laziness w/FS::ClientAPI::passwd::passwd
163 my $conf = new FS::Conf;
166 if ( $p->{'domain'} eq 'svc_phone'
167 && $conf->exists('selfservice_server-phone_login') ) {
169 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
170 return { error => 'Number not found.' } unless $svc_phone;
173 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
174 #return { error => 'Only primary user may log in.' }
175 # if $conf->exists('selfservice_server-primary_only')
176 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
178 return { error => 'Incorrect PIN.' }
179 unless $svc_phone->check_pin($p->{'password'});
185 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
186 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
188 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
189 'domsvc' => $svc_domain->svcnum, }
191 return { error => 'User not found.' } unless $svc_acct;
193 if($conf->exists('selfservice_server-login_svcpart')) {
194 my @svcpart = $conf->config('selfservice_server-login_svcpart');
195 my $svcpart = $svc_acct->cust_svc->svcpart;
196 return { error => 'Invalid user.' }
197 unless grep($_ eq $svcpart, @svcpart);
200 return { error => 'Incorrect password.' }
201 unless $svc_acct->check_password($p->{'password'});
208 'svcnum' => $svc_x->svcnum,
211 my $cust_svc = $svc_x->cust_svc;
212 my $cust_pkg = $cust_svc->cust_pkg;
214 my $cust_main = $cust_pkg->cust_main;
215 $session->{'custnum'} = $cust_main->custnum;
216 if ( $conf->exists('pkg-balances') ) {
217 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
218 $cust_main->ncancelled_pkgs;
219 $session->{'pkgnum'} = $cust_pkg->pkgnum
220 if scalar(@cust_pkg) > 1;
224 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
225 #return { error => 'Only primary user may log in.' }
226 # if $conf->exists('selfservice_server-primary_only')
227 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
228 my $part_pkg = $cust_pkg->part_pkg;
229 return { error => 'Only primary user may log in.' }
230 if $conf->exists('selfservice_server-primary_only')
231 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
235 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
236 } until ( ! defined _cache->get($session_id) ); #just in case
238 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
239 _cache->set( $session_id, $session, $timeout );
241 return { 'error' => '',
242 'session_id' => $session_id,
248 if ( $p->{'session_id'} ) {
249 _cache->remove($p->{'session_id'});
250 return { %{ skin_info($p) }, 'error' => '' };
252 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
259 my($context, $session, $custnum) = _custoragent_session_custnum($p);
260 return { 'error' => $session } if $context eq 'error';
262 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
263 or return { 'error' => "Service not found" };
265 $session->{'svcnum'} = $svc_acct->svcnum;
267 my $conf = new FS::Conf;
268 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
269 _cache->set( $p->{'session_id'}, $session, $timeout );
271 return { 'error' => '' };
275 sub payment_gateway {
277 # takes a cust_main and a cust_payby entry, returns the payment_gateway
278 my $conf = new FS::Conf;
279 my $cust_main = shift;
280 my $cust_payby = shift;
281 my $gatewaynum = $conf->config('selfservice-payment_gateway');
283 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
284 die "configured gatewaynum $gatewaynum not found!" if !$pg;
288 return '' if ! FS::payby->realtime($cust_payby);
289 my $pg = $cust_main->agent->payment_gateway(
290 'method' => FS::payby->payby2bop($cust_payby),
300 my $conf = new FS::Conf;
302 my $info = skin_info($p);
304 use vars qw( $cust_paybys ); #cache for performance
305 unless ( $cust_paybys ) {
307 my %cust_paybys = map { $_ => 1 }
308 map { FS::payby->payby2payment($_) }
309 $conf->config('signup_server-payby');
311 $cust_paybys = [ keys %cust_paybys ];
314 $info->{'cust_paybys'} = $cust_paybys;
316 my($context, $session, $custnum) = _custoragent_session_custnum($p);
317 return { 'error' => $session } if $context eq 'error';
319 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
320 or return { 'error' => "unknown custnum $custnum" };
322 $info->{'hide_payment_fields'} = [
324 my $pg = payment_gateway($cust_main, $_);
325 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
326 } @{ $info->{cust_paybys} }
329 $info->{'self_suspend_reason'} =
330 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
332 $info->{'edit_ticket_subject'} =
333 $conf->exists('ticket_system-selfservice_edit_subject') &&
334 $cust_main->edit_subject;
337 'custnum' => $custnum,
338 'access_pkgnum' => $session->{'pkgnum'},
339 'access_svcnum' => $session->{'svcnum'},
346 my($context, $session, $custnum) = _custoragent_session_custnum($p);
347 return { 'error' => $session } if $context eq 'error';
351 my $conf = new FS::Conf;
352 if ($conf->exists('cust_main-require_address2')) {
353 $return{'require_address2'} = '1';
355 $return{'require_address2'} = '';
358 if ( $FS::TicketSystem::system ) {
359 warn "$me customer_info: initializing ticket system\n" if $DEBUG;
360 FS::TicketSystem->init();
363 if ( $custnum ) { #customer record
365 my $search = { 'custnum' => $custnum };
366 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
367 my $cust_main = qsearchs('cust_main', $search )
368 or return { 'error' => "unknown custnum $custnum" };
370 if ( $session->{'pkgnum'} ) {
371 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
373 $return{balance} = $cust_main->balance;
376 my @tickets = $cust_main->tickets;
377 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
378 if ( FS::TicketSystem->selfservice_priority ) {
379 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
383 ($a->{'_selfservice_priority'} eq '') <=>
384 ($b->{'_selfservice_priority'} eq '')
387 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
393 $return{tickets} = \@tickets;
396 unless ( $session->{'pkgnum'} ) {
399 invnum => $_->invnum,
400 date => time2str("%b %o, %Y", $_->_date),
403 } $cust_main->open_cust_bill;
404 $return{open_invoices} = \@open;
407 $return{small_custview} =
408 small_custview( $cust_main,
409 scalar($conf->config('countrydefault')),
410 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
413 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
414 $return{ship_name} = $cust_main->ship_first. ' '. $cust_main->get('ship_last');
416 for (@cust_main_editable_fields) {
417 $return{$_} = $cust_main->get($_);
420 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
421 $return{payinfo} = $cust_main->paymask;
422 @return{'month', 'year'} = $cust_main->paydate_monthyear;
425 $return{'invoicing_list'} =
426 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
427 $return{'postal_invoicing'} =
428 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
430 if (scalar($conf->config('support_packages'))) {
431 my @support_services = ();
432 foreach ($cust_main->support_services) {
433 my $seconds = $_->svc_x->seconds;
434 my $time_remaining = (($seconds < 0) ? '-' : '' ).
435 int(abs($seconds)/3600)."h".
436 sprintf("%02d",(abs($seconds)%3600)/60)."m";
437 my $cust_pkg = $_->cust_pkg;
440 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
441 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
442 push @support_services, { svcnum => $_->svcnum,
443 time => $time_remaining,
448 $return{support_services} = \@support_services;
451 if ( $conf->config('prepayment_discounts-credit_type') ) {
453 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
456 if ( $session->{'svcnum'} ) {
457 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
458 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
459 $return{'svcnum'} = $session->{'svcnum'};
462 } elsif ( $session->{'svcnum'} ) { #no customer record
464 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
465 or die "unknown svcnum";
466 $return{name} = $svc_acct->email;
470 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
474 return { 'error' => '',
475 'custnum' => $custnum,
481 sub customer_info_short {
484 my($context, $session, $custnum) = _custoragent_session_custnum($p);
485 return { 'error' => $session } if $context eq 'error';
489 my $conf = new FS::Conf;
491 if ( $custnum ) { #customer record
493 my $search = { 'custnum' => $custnum };
494 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
495 my $cust_main = qsearchs('cust_main', $search )
496 or return { 'error' => "unknown custnum $custnum" };
498 $return{small_custview} =
499 small_custview( $cust_main,
500 scalar($conf->config('countrydefault')),
504 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
505 $return{ship_name} = $cust_main->ship_first. ' '. $cust_main->get('ship_last');
507 $return{payby} = $cust_main->payby;
509 #none of these are terribly expensive if we want 'em...
510 for (@cust_main_editable_fields) {
511 $return{$_} = $cust_main->get($_);
514 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
515 $return{payinfo} = $cust_main->paymask;
516 @return{'month', 'year'} = $cust_main->paydate_monthyear;
519 $return{'invoicing_list'} =
520 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
521 #$return{'postal_invoicing'} =
522 # 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
524 if ( $session->{'svcnum'} ) {
525 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
526 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
527 $return{'svcnum'} = $session->{'svcnum'};
530 } elsif ( $session->{'svcnum'} ) { #no customer record
532 #uuh, not supproted yet... die?
533 return { 'error' => 'customer_info_short not yet supported as agent' };
537 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
541 return { 'error' => '',
542 'custnum' => $custnum,
549 my $session = _cache->get($p->{'session_id'})
550 or return { 'error' => "Can't resume session" }; #better error message
552 my $custnum = $session->{'custnum'}
553 or return { 'error' => "no customer record" };
555 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
556 or return { 'error' => "unknown custnum $custnum" };
558 my $new = new FS::cust_main { $cust_main->hash };
559 $new->set( $_ => $p->{$_} )
560 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
563 if (exists($p->{'payby'})) {
564 $p->{'payby'} =~ /^([A-Z]{4})$/
565 or return { 'error' => "illegal_payby " . $p->{'payby'} };
569 if ( $payby =~ /^(CARD|DCRD)$/ ) {
571 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
573 if ( $new->payinfo eq $cust_main->paymask ) {
574 $new->payinfo($cust_main->payinfo);
576 $new->payinfo($p->{'payinfo'});
579 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
581 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
584 $p->{'payinfo1'} =~ /^([\dx]+)$/
585 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
587 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by -require-bank-branch?
588 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
590 $payinfo = $payinfo1. '@'. $payinfo2;
592 $new->payinfo( ($payinfo eq $cust_main->paymask)
593 ? $cust_main->payinfo
597 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
599 } elsif ( $payby =~ /^(BILL)$/ ) {
601 } elsif ( $payby ) { #notyet ready
602 return { 'error' => "unknown payby $payby" };
606 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
607 #false laziness with httemplate/edit/process/cust_main.cgi
608 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
609 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
611 @invoicing_list = $cust_main->invoicing_list;
614 my $error = $new->replace($cust_main, \@invoicing_list);
615 return { 'error' => $error } if $error;
618 return { 'error' => '' };
623 my $session = _cache->get($p->{'session_id'})
624 or return { 'error' => "Can't resume session" }; #better error message
630 my $conf = new FS::Conf;
631 use vars qw($payment_info); #cache for performance
632 unless ( $payment_info ) {
634 my %states = map { $_->state => 1 }
635 qsearch('cust_main_county', {
636 'country' => $conf->config('countrydefault') || 'US'
639 my %cust_paybys = map { $_ => 1 }
640 map { FS::payby->payby2payment($_) }
641 $conf->config('signup_server-payby');
643 my @cust_paybys = keys %cust_paybys;
647 #list all counties/states/countries
648 'cust_main_county' =>
649 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
651 #shortcut for one-country folks
653 [ sort { $a cmp $b } keys %states ],
655 'card_types' => card_types(),
657 'paytypes' => [ @FS::cust_main::paytypes ],
659 'paybys' => [ $conf->config('signup_server-payby') ],
660 'cust_paybys' => \@cust_paybys,
662 'stateid_label' => FS::Msgcat::_gettext('stateid'),
663 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
665 'show_ss' => $conf->exists('show_ss'),
666 'show_stateid' => $conf->exists('show_stateid'),
667 'show_paystate' => $conf->exists('show_bankstate'),
669 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
671 'credit_card_surcharge_percentage' => $conf->config('credit-card-surcharge-percentage'),
680 my %return = %$payment_info;
682 my $custnum = $session->{'custnum'};
684 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
685 or return { 'error' => "unknown custnum $custnum" };
687 $return{'hide_payment_fields'} = [
689 my $pg = payment_gateway($cust_main, $_);
690 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
691 } @{ $return{cust_paybys} }
694 $return{balance} = $cust_main->balance; #XXX pkg-balances?
696 $return{payname} = $cust_main->payname
697 || ( $cust_main->first. ' '. $cust_main->get('last') );
699 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
701 $return{payby} = $cust_main->payby;
702 $return{stateid_state} = $cust_main->stateid_state;
704 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
705 $return{card_type} = cardtype($cust_main->payinfo);
706 $return{payinfo} = $cust_main->paymask;
708 @return{'month', 'year'} = $cust_main->paydate_monthyear;
712 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
713 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
714 $return{payinfo1} = $payinfo1;
715 $return{payinfo2} = $payinfo2;
716 $return{paytype} = $cust_main->paytype;
717 $return{paystate} = $cust_main->paystate;
721 if ( $conf->config('prepayment_discounts-credit_type') ) {
723 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
726 #doubleclick protection
728 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
730 return { 'error' => '',
736 #some false laziness with httemplate/process/payment.cgi - look there for
737 #ACH and CVV support stuff
739 sub validate_payment {
742 my $session = _cache->get($p->{'session_id'})
743 or return { 'error' => "Can't resume session" }; #better error message
745 my $custnum = $session->{'custnum'};
747 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
748 or return { 'error' => "unknown custnum $custnum" };
750 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
751 or return { 'error' => gettext('illegal_amount') };
753 return { error => 'Amount must be greater than 0' } unless $amount > 0;
755 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
756 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
757 my $discount_term = $1;
759 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
760 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
763 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
764 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
767 $p->{'payby'} ||= 'CARD';
768 $p->{'payby'} =~ /^([A-Z]{4})$/
769 or return { 'error' => "illegal_payby " . $p->{'payby'} };
772 #false laziness w/process/payment.cgi
775 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
777 $p->{'payinfo1'} =~ /^([\dx]+)$/
778 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
780 $p->{'payinfo2'} =~ /^([\dx]+)$/
781 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
783 $payinfo = $payinfo1. '@'. $payinfo2;
785 $payinfo = $cust_main->payinfo
786 if $cust_main->paymask eq $payinfo;
788 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
790 $payinfo = $p->{'payinfo'};
792 #more intelligent mathing will be needed here if you change
793 #card_masking_method and don't remove existing paymasks
794 $payinfo = $cust_main->payinfo
795 if $cust_main->paymask eq $payinfo;
798 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
799 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
803 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
804 return { 'error' => gettext('unknown_card_type') }
805 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
807 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
808 if ( cardtype($payinfo) eq 'American Express card' ) {
809 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
810 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
813 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
814 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
820 die "unknown payby $payby";
824 'CARD' => [ qw( paystart_month paystart_year payissue payip
825 address1 address2 city state zip country ) ],
826 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
830 $card_type = cardtype($payinfo) if $payby eq 'CARD';
833 'cust_main' => $cust_main, #XXX or just custnum??
836 'payinfo' => $payinfo,
837 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
838 'card_type' => $card_type,
839 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
840 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
841 'payname' => $payname,
842 'paybatch' => $paybatch, #this doesn't actually do anything
844 'payname' => $payname,
845 'discount_term' => $discount_term,
846 'pkgnum' => $session->{'pkgnum'},
847 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
857 my $validate = validate_payment($p);
858 return $validate if $validate->{'error'};
860 my $conf = new FS::Conf;
861 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
862 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
864 +{ map { $_=>$validate->{$_} }
865 qw( card_type paymask payname paydate_pretty amount )
870 sub process_stored_payment {
873 my $session_id = $p->{'session_id'};
875 my $payment_info = _cache->get( "payment_$session_id" )
876 or return { 'error' => "Can't resume session" }; #better error message
878 do_process_payment($payment_info);
882 sub process_payment {
885 my $payment_info = validate_payment($p);
886 return $payment_info if $payment_info->{'error'};
888 do_process_payment($payment_info);
892 sub do_process_payment {
893 my $validate = shift;
895 my $cust_main = $validate->{'cust_main'};
897 my $amount = delete $validate->{'amount'};
900 my $payby = delete $validate->{'payby'};
902 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
905 'paynum_ref' => \$paynum,
908 return { 'error' => $error } if $error;
910 $cust_main->apply_payments;
912 if ( $validate->{'save'} ) {
913 my $new = new FS::cust_main { $cust_main->hash };
914 if ($validate->{'payby'} eq 'CARD' || $validate->{'payby'} eq 'DCRD') {
915 $new->set( $_ => $validate->{$_} )
916 foreach qw( payname paystart_month paystart_year payissue payip
917 address1 address2 city state zip country );
918 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
919 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
920 $new->set( $_ => $validate->{$_} )
921 foreach qw( payname payip paytype paystate
922 stateid stateid_state );
923 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
925 $new->set( 'payinfo' => $cust_main->card_token || $validate->{'payinfo'} );
926 $new->set( 'paydate' => $validate->{'paydate'} );
927 my $error = $new->replace($cust_main);
929 #no, this causes customers to process their payments again
930 #return { 'error' => $error };
931 #XXX just warn verosely for now so i can figure out how these happen in
932 # the first place, eventually should redirect them to the "change
933 #address" page but indicate the payment did process??
934 delete($validate->{'payinfo'}); #don't want to log this!
935 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
936 "NEW: ". Dumper($new)."\n".
937 "OLD: ". Dumper($cust_main)."\n".
938 "PACKET: ". Dumper($validate)."\n";
946 my $receipt_html = '';
948 # currently supported for realtime CC only; send receipt data to SS
949 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
952 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
955 <TD ALIGN="right">Payment#</TD>
956 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
960 <TD ALIGN="right">Date</TD>
962 <TD BGCOLOR="#FFFFFF"><B>! .
963 time2str("%a %b %o, %Y %r", $cust_pay->_date)
969 <TD ALIGN="right">Amount</TD>
970 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
975 <TD ALIGN="right">Payment method</TD>
976 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
987 my($gw, $auth, $order) = split(':', $cust_pay->paybatch);
991 'amount' => sprintf('%.2f', $cust_pay->paid),
992 'date' => $cust_pay->_date,
993 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
994 'time_pretty' => time2str('%T', $cust_pay->_date),
996 'order_num' => $order,
997 'receipt_html' => $receipt_html,
1004 'receipt_html' => '',
1011 sub realtime_collect {
1014 my $session = _cache->get($p->{'session_id'})
1015 or return { 'error' => "Can't resume session" }; #better error message
1017 my $custnum = $session->{'custnum'};
1019 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1020 or return { 'error' => "unknown custnum $custnum" };
1023 if ( $p->{'amount'} ) {
1024 $amount = $p->{'amount'};
1026 elsif ( $session->{'pkgnum'} ) {
1027 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1030 $amount = $cust_main->balance;
1033 my $error = $cust_main->realtime_collect(
1034 'method' => $p->{'method'},
1035 'amount' => $amount,
1036 'pkgnum' => $session->{'pkgnum'},
1037 'session_id' => $p->{'session_id'},
1041 return { 'error' => $error } unless ref( $error );
1043 return { 'error' => '', amount => $amount, %$error };
1046 sub process_payment_order_pkg {
1049 my $hr = process_payment($p);
1050 return $hr if $hr->{'error'};
1055 sub process_payment_order_renew {
1058 my $hr = process_payment($p);
1059 return $hr if $hr->{'error'};
1064 sub process_prepay {
1068 my $session = _cache->get($p->{'session_id'})
1069 or return { 'error' => "Can't resume session" }; #better error message
1073 my $custnum = $session->{'custnum'};
1075 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1076 or return { 'error' => "unknown custnum $custnum" };
1078 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1079 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1087 return { 'error' => $error } if $error;
1089 return { 'error' => '',
1090 'amount' => $amount,
1091 'seconds' => $seconds,
1092 'duration' => duration_exact($seconds),
1093 'upbytes' => $upbytes,
1094 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1095 'downbytes' => $downbytes,
1096 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1097 'totalbytes'=> $totalbytes,
1098 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1105 my $session = _cache->get($p->{'session_id'})
1106 or return { 'error' => "Can't resume session" }; #better error message
1108 my $custnum = $session->{'custnum'};
1110 my $invnum = $p->{'invnum'};
1112 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1113 'custnum' => $custnum } )
1114 or return { 'error' => "Can't find invnum" };
1118 return { 'error' => '',
1119 'invnum' => $invnum,
1120 'invoice_text' => join('', $cust_bill->print_text ),
1121 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1128 my $session = _cache->get($p->{'session_id'})
1129 or return { 'error' => "Can't resume session" }; #better error message
1131 my $custnum = $session->{'custnum'};
1133 my $invnum = $p->{'invnum'};
1135 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1136 'custnum' => $custnum } )
1137 or return { 'error' => "Can't find invnum" };
1141 return { 'error' => '',
1142 'invnum' => $invnum,
1143 'invoice_pdf' => $cust_bill->print_pdf( { unsquelch_cdr => 1 } ),
1148 sub legacy_invoice {
1150 my $session = _cache->get($p->{'session_id'})
1151 or return { 'error' => "Can't resume session" }; #better error message
1153 my $custnum = $session->{'custnum'};
1155 my $legacyinvnum = $p->{'legacyinvnum'};
1157 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1158 'legacyinvnum' => $legacyinvnum,
1159 'custnum' => $custnum,
1160 }) or return { 'error' => "Can't find legacyinvnum" };
1164 return { 'error' => '',
1165 'legacyinvnum' => $legacyinvnum,
1166 'legacyid' => $legacy_cust_bill->legacyid,
1167 'invoice_html' => $legacy_cust_bill->content_html,
1172 sub legacy_invoice_pdf {
1174 my $session = _cache->get($p->{'session_id'})
1175 or return { 'error' => "Can't resume session" }; #better error message
1177 my $custnum = $session->{'custnum'};
1179 my $legacyinvnum = $p->{'legacyinvnum'};
1181 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1182 'legacyinvnum' => $legacyinvnum,
1183 'custnum' => $custnum,
1184 }) or return { 'error' => "Can't find legacyinvnum" };
1188 return { 'error' => '',
1189 'legacyinvnum' => $legacyinvnum,
1190 'legacyid' => $legacy_cust_bill->legacyid,
1191 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1199 #sessioning for this? how do we get the session id to the backend invoice
1200 # template so it can add it to the link, blah
1203 if ( $p->{'invnum'} ) {
1204 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1205 or return { 'error' => 'unknown invnum' };
1206 $agentnum = $cust_bill->cust_main->agentnum;
1209 my $templatename = $p->{'template'} || $p->{'templatename'};
1211 #false laziness-ish w/view/cust_bill-logo.cgi
1213 my $conf = new FS::Conf;
1214 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1215 $templatename = "_$1";
1220 my $filename = "logo$templatename.png";
1222 return { 'error' => '',
1223 'logo' => $conf->config_binary($filename, $agentnum),
1224 'content_type' => 'image/png', #should allow gif, jpg too
1231 my $session = _cache->get($p->{'session_id'})
1232 or return { 'error' => "Can't resume session" }; #better error message
1234 my $custnum = $session->{'custnum'};
1236 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1237 or return { 'error' => "unknown custnum $custnum" };
1239 my $conf = new FS::Conf;
1241 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1243 my @cust_bill = $cust_main->cust_bill;
1245 my $hide_taxclass = $conf->config('selfservice-hide_invoices-taxclass');
1246 if ( $hide_taxclass ) {
1247 @cust_bill = grep { my @cust_bill_pkg = $_->cust_bill_pkg;
1248 my @part_pkg= grep $_, map $_->part_pkg, @cust_bill_pkg;
1249 grep { $_->taxclass ne $hide_taxclass } @part_pkg;
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) = @_;
1509 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1510 my $search = " AND custnum = $1";
1511 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1514 'table' => ($table || 'svc_acct'),
1515 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1516 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
1517 #'LEFT JOIN cust_main USING ( custnum ) ',
1518 'hashref' => { 'svcnum' => $svcnum, },
1519 'extra_sql' => $search, #important
1524 sub list_dsl_devices {
1527 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1528 return { 'error' => $session } if $context eq 'error';
1530 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1531 or return { 'error' => "Service not found" };
1534 'devices' => [ map {
1535 +{ 'mac_addr' => $_->mac_addr };
1536 } $svc_dsl->dsl_device
1542 sub add_dsl_device {
1545 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1546 return { 'error' => $session } if $context eq 'error';
1548 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1549 or return { 'error' => "Service not found" };
1551 return { 'error' => 'No MAC address supplied' }
1552 unless length($p->{'mac_addr'});
1554 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
1555 'mac_addr' => scalar($p->{'mac_addr'}),
1557 my $error = $dsl_device->insert;
1558 return { 'error' => $error };
1562 sub delete_dsl_device {
1565 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1566 return { 'error' => $session } if $context eq 'error';
1568 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
1569 or return { 'error' => "Service not found" };
1571 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
1572 'mac_addr' => scalar($p->{'mac_addr'}),
1575 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
1577 my $error = $dsl_device->delete;
1578 return { 'error' => $error };
1584 _usage_details( \&_port_graph, $p,
1585 'svcdb' => 'svc_port',
1590 my($svc_port, $begin, $end) = @_;
1592 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
1593 push @usage, { 'png' => $pngOrError };
1597 sub _list_svc_usage {
1598 my($svc_acct, $begin, $end) = @_;
1600 foreach my $part_export (
1601 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1602 qw( sqlradius sqlradius_withdomain )
1604 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1609 sub list_svc_usage {
1610 _usage_details(\&_list_svc_usage, @_);
1613 sub _list_support_usage {
1614 my($svc_acct, $begin, $end) = @_;
1616 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1617 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1619 push @usage, { 'seconds' => $_->seconds,
1620 'support' => $_->support,
1621 '_date' => $_->_date,
1622 'id' => $_->transaction_id,
1623 'creator' => $_->creator,
1624 'subject' => $_->subject,
1625 'status' => $_->status,
1626 'ticketid' => $_->ticketid,
1632 sub list_support_usage {
1633 _usage_details(\&_list_support_usage, @_);
1636 sub _list_cdr_usage {
1637 my($svc_phone, $begin, $end) = @_;
1638 map [ $_->downstream_csv('format' => 'default', 'keeparray' => 1) ], #XXX config for format
1639 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1642 sub list_cdr_usage {
1644 _usage_details( \&_list_cdr_usage, $p,
1645 'svcdb' => 'svc_phone',
1649 sub _usage_details {
1650 my($callback, $p, %opt) = @_;
1652 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1653 return { 'error' => $session } if $context eq 'error';
1655 my $search = { 'svcnum' => $p->{'svcnum'} };
1656 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1658 my $svcdb = $opt{'svcdb'} || 'svc_acct';
1660 my $svc_x = qsearchs( $svcdb, $search );
1661 return { 'error' => 'No service selected in list_svc_usage' }
1664 my $header = $svcdb eq 'svc_phone'
1665 ? [ split(',', FS::cdr::invoice_header('default') ) ] #XXX
1668 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
1669 my $freq = $cust_pkg->part_pkg->freq;
1670 my $start = $cust_pkg->setup;
1671 #my $end = $cust_pkg->bill; # or time?
1674 unless ( $p->{beginning} ) {
1675 $p->{beginning} = $cust_pkg->last_bill;
1676 $p->{ending} = $end;
1679 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending});
1681 #kinda false laziness with FS::cust_main::bill, but perhaps
1682 #we should really change this bit to DateTime and DateTime::Duration
1684 #change this bit to use Date::Manip? CAREFUL with timezones (see
1685 # mailing list archive)
1686 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
1687 (localtime($p->{ending}) )[0,1,2,3,4,5];
1688 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
1689 (localtime($p->{beginning}) )[0,1,2,3,4,5];
1691 if ( $freq =~ /^\d+$/ ) {
1693 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
1695 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
1696 } elsif ( $freq =~ /^(\d+)w$/ ) {
1698 $nmday += $weeks * 7;
1699 $pmday -= $weeks * 7;
1700 } elsif ( $freq =~ /^(\d+)d$/ ) {
1704 } elsif ( $freq =~ /^(\d+)h$/ ) {
1709 return { 'error' => "unparsable frequency: ". $freq };
1712 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
1713 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
1717 'svcnum' => $p->{svcnum},
1718 'beginning' => $p->{beginning},
1719 'ending' => $p->{ending},
1720 'previous' => ($previous > $start) ? $previous : $start,
1721 'next' => ($next < $end) ? $next : $end,
1722 'header' => $header,
1730 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1731 return { 'error' => $session } if $context eq 'error';
1733 my $search = { 'custnum' => $custnum };
1734 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1735 my $cust_main = qsearchs('cust_main', $search )
1736 or return { 'error' => "unknown custnum $custnum" };
1738 my $status = $cust_main->status;
1739 #false laziness w/ClientAPI/Signup.pm
1741 my $cust_pkg = new FS::cust_pkg ( {
1742 'custnum' => $custnum,
1743 'pkgpart' => $p->{'pkgpart'},
1745 my $error = $cust_pkg->check;
1746 return { 'error' => $error } if $error;
1749 unless ( $p->{'svcpart'} eq 'none' ) {
1753 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
1755 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1756 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
1757 $svcdb = $part_svc->svcdb;
1759 $svcdb = 'svc_acct';
1761 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
1764 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
1765 'svc_domain' => [ qw( domain ) ],
1766 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
1767 'svc_external' => [ qw( id title ) ],
1768 'svc_pbx' => [ qw( id title ) ],
1771 my $svc_x = "FS::$svcdb"->new( {
1772 'svcpart' => $svcpart,
1773 map { $_ => $p->{$_} } @{$fields{$svcdb}}
1776 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
1779 while ( length($p->{"snarf_machine$snarfnum"}) ) {
1780 my $acct_snarf = new FS::acct_snarf ( {
1781 'machine' => $p->{"snarf_machine$snarfnum"},
1782 'protocol' => $p->{"snarf_protocol$snarfnum"},
1783 'username' => $p->{"snarf_username$snarfnum"},
1784 '_password' => $p->{"snarf_password$snarfnum"},
1787 push @acct_snarf, $acct_snarf;
1789 $svc_x->child_objects( \@acct_snarf );
1792 my $y = $svc_x->setdefault; # arguably should be in new method
1793 return { 'error' => $y } if $y && !ref($y);
1795 $error = $svc_x->check;
1796 return { 'error' => $error } if $error;
1803 tie my %hash, 'Tie::RefHash';
1804 %hash = ( $cust_pkg => \@svc );
1806 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
1807 return { 'error' => $error } if $error;
1809 my $conf = new FS::Conf;
1810 if ( $conf->exists('signup_server-realtime') ) {
1812 my $bill_error = _do_bop_realtime( $cust_main, $status );
1815 $cust_pkg->cancel('quiet'=>1);
1818 $cust_pkg->reexport;
1822 $cust_pkg->reexport;
1825 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
1827 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
1834 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1835 return { 'error' => $session } if $context eq 'error';
1837 my $search = { 'custnum' => $custnum };
1838 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1839 my $cust_main = qsearchs('cust_main', $search )
1840 or return { 'error' => "unknown custnum $custnum" };
1842 my $status = $cust_main->status;
1843 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
1844 or return { 'error' => "unknown package $p->{pkgnum}" };
1847 my $error = FS::cust_pkg::order( $custnum,
1853 my $conf = new FS::Conf;
1854 if ( $conf->exists('signup_server-realtime') ) {
1856 my $bill_error = _do_bop_realtime( $cust_main, $status );
1859 $newpkg[0]->suspend;
1862 $newpkg[0]->reexport;
1866 $newpkg[0]->reexport;
1869 return { error => '', pkgnum => $cust_pkg->pkgnum };
1873 sub order_recharge {
1876 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1877 return { 'error' => $session } if $context eq 'error';
1879 my $search = { 'custnum' => $custnum };
1880 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1881 my $cust_main = qsearchs('cust_main', $search )
1882 or return { 'error' => "unknown custnum $custnum" };
1884 my $status = $cust_main->status;
1885 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
1886 or return { 'error' => "unknown service " . $p->{'svcnum'} };
1888 my $svc_x = $cust_svc->svc_x;
1889 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
1892 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
1893 qw ( recharge_seconds recharge_upbytes recharge_downbytes
1894 recharge_totalbytes );
1895 my $amount = $part_pkg->option('recharge_amount', 1);
1897 my ($l, $v, $d) = $cust_svc->label; # blah
1898 my $pkg = "Recharge $v";
1900 my $bill_error = $cust_main->charge($amount, $pkg,
1901 "time: $vhash{seconds}, up: $vhash{upbytes}," .
1902 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
1903 $part_pkg->taxclass); #meh
1905 my $conf = new FS::Conf;
1906 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
1908 $bill_error = _do_bop_realtime( $cust_main, $status );
1913 my $error = $svc_x->recharge (\%vhash);
1914 return { 'error' => $error } if $error;
1918 my $error = $bill_error;
1919 $error ||= $svc_x->recharge (\%vhash);
1920 return { 'error' => $error } if $error;
1923 return { error => '', svc => $cust_svc->part_svc->svc };
1927 sub _do_bop_realtime {
1928 my ($cust_main, $status) = (shift, shift);
1930 my $old_balance = $cust_main->balance;
1932 my $bill_error = $cust_main->bill
1933 || $cust_main->apply_payments_and_credits
1934 || $cust_main->realtime_collect('selfservice' => 1);
1936 if ( $cust_main->balance > $old_balance
1937 && $cust_main->balance > 0
1938 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
1939 1 : $status eq 'suspended' ) ) {
1940 #this makes sense. credit is "un-doing" the invoice
1941 my $conf = new FS::Conf;
1942 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
1943 'self-service decline',
1944 'reason_type' => $conf->config('signup_credit_type'),
1946 $cust_main->apply_credits( 'order' => 'newest' );
1948 return { 'error' => '_decline', 'bill_error' => $bill_error };
1957 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1958 return { 'error' => $session } if $context eq 'error';
1960 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1961 or return { 'error' => "unknown custnum $custnum" };
1963 my @cust_pkg = sort { $a->bill <=> $b->bill }
1964 grep { $_->part_pkg->freq ne '0' }
1965 $cust_main->ncancelled_pkgs;
1967 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
1969 my $total = $cust_main->balance;
1972 my $bill = $_->bill;
1973 $total += $_->part_pkg->base_recur($_, \$bill);
1974 my $renew_date = $_->part_pkg->add_freq($_->bill);
1976 'pkgnum' => $_->pkgnum,
1977 'amount' => sprintf('%.2f', $total),
1978 'bill_date' => $_->bill,
1979 'bill_date_pretty' => time2str('%x', $_->bill),
1980 'renew_date' => $renew_date,
1981 'renew_date_pretty' => time2str('%x', $renew_date),
1982 'expire_date' => $_->expire,
1983 'expire_date_pretty' => time2str('%x', $_->expire),
1988 return { 'dates' => \@array };
1992 sub payment_info_renew_info {
1994 my $renew_info = renew_info($p);
1995 my $payment_info = payment_info($p);
1996 return { %$renew_info,
2004 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2005 return { 'error' => $session } if $context eq 'error';
2007 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2008 or return { 'error' => "unknown custnum $custnum" };
2010 my $date = $p->{'date'};
2014 #freeside-daily -n -d $date fs_daily $custnum
2015 $cust_main->bill_and_collect( 'time' => $date,
2016 'invoice_time' => $now,
2017 'actual_time' => $now,
2018 'check_freq' => '1d',
2021 return { 'error' => '' };
2027 my $session = _cache->get($p->{'session_id'})
2028 or return { 'error' => "Can't resume session" }; #better error message
2030 my $custnum = $session->{'custnum'};
2032 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2033 or return { 'error' => "unknown custnum $custnum" };
2035 my $conf = new FS::Conf;
2037 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2038 or return { 'error' => 'Permission denied' };
2040 my $pkgnum = $p->{'pkgnum'};
2042 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2043 'pkgnum' => $pkgnum, } )
2044 or return { 'error' => "unknown pkgnum $pkgnum" };
2046 my $error = $cust_pkg->suspend(reason => $reasonnum);
2047 return { 'error' => $error };
2053 my $session = _cache->get($p->{'session_id'})
2054 or return { 'error' => "Can't resume session" }; #better error message
2056 my $custnum = $session->{'custnum'};
2058 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2059 or return { 'error' => "unknown custnum $custnum" };
2061 my $pkgnum = $p->{'pkgnum'};
2063 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2064 'pkgnum' => $pkgnum, } )
2065 or return { 'error' => "unknown pkgnum $pkgnum" };
2067 my $error = $cust_pkg->cancel('quiet' => 1);
2068 return { 'error' => $error };
2072 sub provision_phone {
2075 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2077 if($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/){
2078 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2079 return { 'error' => $session } if $context eq 'error';
2081 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2082 return { 'error' => 'service not found' } unless $svc_phone;
2083 return { 'error' => 'invalid svcnum' }
2084 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2086 $svc_phone->email($p->{'email'})
2087 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2088 $svc_phone->forwarddst($p->{'forwarddst'})
2089 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2090 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2091 return { 'error' => $svc_phone->replace };
2095 unless($p->{'lnp'}) {
2096 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2097 $p->{'lnp_status'} = "portingin";
2098 return _provision( 'FS::svc_phone',
2099 [qw(lnp_desired_due_date lnp_other_provider
2100 lnp_other_provider_account phonenum countrycode lnp_status)],
2101 [qw(phonenum countrycode)],
2108 unless (scalar(@bulkdid)) {
2109 return _provision( 'FS::svc_phone',
2110 [qw(phonenum countrycode)],
2111 [qw(phonenum countrycode)],
2117 # bulk DID order case
2119 foreach my $did ( @bulkdid ) {
2120 $did =~ s/[^0-9]//g;
2121 $error = _provision( 'FS::svc_phone',
2122 [qw(phonenum countrycode)],
2123 [qw(phonenum countrycode)],
2125 'pkgnum' => $p->{'pkgnum'},
2126 'svcpart' => $p->{'svcpart'},
2128 'countrycode' => $p->{'countrycode'},
2129 'session_id' => $p->{'session_id'},
2132 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2134 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2137 sub provision_acct {
2139 warn "provision_acct called\n"
2142 return { 'error' => gettext('passwords_dont_match') }
2143 if $p->{'_password'} ne $p->{'_password2'};
2144 return { 'error' => gettext('empty_password') }
2145 unless length($p->{'_password'});
2147 if ($p->{'domsvc'}) {
2148 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
2149 qw ( svcpart pkgnum ) );
2150 return { 'error' => gettext('invalid_domain') }
2151 unless ($domains{$p->{'domsvc'}});
2154 warn "provision_acct calling _provision\n"
2156 _provision( 'FS::svc_acct',
2157 [qw(username _password domsvc)],
2158 [qw(username _password domsvc)],
2164 sub provision_external {
2166 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
2167 _provision( 'FS::svc_external',
2176 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
2177 warn "_provision called for $class\n"
2180 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2181 return { 'error' => $session } if $context eq 'error';
2183 my $search = { 'custnum' => $custnum };
2184 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2185 my $cust_main = qsearchs('cust_main', $search )
2186 or return { 'error' => "unknown custnum $custnum" };
2188 my $pkgnum = $p->{'pkgnum'};
2190 warn "searching for custnum $custnum pkgnum $pkgnum\n"
2192 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2193 'pkgnum' => $pkgnum,
2195 or return { 'error' => "unknown pkgnum $pkgnum" };
2197 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
2199 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
2200 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
2202 warn "creating $class record\n"
2204 my $svc_x = $class->new( {
2205 'pkgnum' => $p->{'pkgnum'},
2206 'svcpart' => $p->{'svcpart'},
2207 map { $_ => $p->{$_} } @$fields
2209 warn "inserting $class record\n"
2211 my $error = $svc_x->insert;
2214 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
2216 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
2219 my $return = { 'svc' => $part_svc->svc,
2221 map { $_ => $svc_x->get($_) } @$return_fields
2223 warn "_provision returning ". Dumper($return). "\n"
2232 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2233 return { 'error' => $session } if $context eq 'error';
2235 my $search = { 'custnum' => $custnum };
2236 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2237 my $cust_main = qsearchs('cust_main', $search )
2238 or return { 'error' => "unknown custnum $custnum" };
2240 my $pkgnum = $p->{'pkgnum'};
2242 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2243 'pkgnum' => $pkgnum,
2245 or return { 'error' => "unknown pkgnum $pkgnum" };
2247 my $svcpart = $p->{'svcpart'};
2249 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
2250 'svcpart' => $svcpart, } )
2251 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
2252 my $part_svc = $pkg_svc->part_svc;
2254 my $conf = new FS::Conf;
2257 'svc' => $part_svc->svc,
2258 'svcdb' => $part_svc->svcdb,
2259 'pkgnum' => $pkgnum,
2260 'svcpart' => $svcpart,
2261 'custnum' => $custnum,
2263 'security_phrase' => 0, #XXX !
2264 'svc_acct_pop' => [], #XXX !
2266 'init_popstate' => '',
2271 small_custview( $cust_main, $conf->config('countrydefault') ),
2275 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
2276 && $ret->{'svcdb'} eq 'svc_phone') {
2277 $ret->{'svcnum'} = $p->{'svcnum'};
2278 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2279 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
2280 $ret->{'email'} = $svc_phone->email;
2281 $ret->{'forwarddst'} = $svc_phone->forwarddst;
2288 sub unprovision_svc {
2291 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2292 return { 'error' => $session } if $context eq 'error';
2294 my $search = { 'custnum' => $custnum };
2295 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2296 my $cust_main = qsearchs('cust_main', $search )
2297 or return { 'error' => "unknown custnum $custnum" };
2299 my $svcnum = $p->{'svcnum'};
2301 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
2302 or return { 'error' => "unknown svcnum $svcnum" };
2304 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
2305 unless $cust_svc->cust_pkg->custnum == $custnum;
2307 my $conf = new FS::Conf;
2309 return { 'svc' => $cust_svc->part_svc->svc,
2310 'error' => $cust_svc->cancel,
2312 small_custview( $cust_main, $conf->config('countrydefault') ),
2317 sub myaccount_passwd {
2319 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2320 return { 'error' => $session } if $context eq 'error';
2322 return { 'error' => "New passwords don't match." }
2323 if $p->{'new_password'} ne $p->{'new_password2'};
2325 return { 'error' => 'Enter new password' }
2326 unless length($p->{'new_password'});
2328 #my $search = { 'custnum' => $custnum };
2329 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2330 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2331 my $search = " AND custnum = $1";
2332 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2334 my $svc_acct = qsearchs( {
2335 'table' => 'svc_acct',
2336 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2337 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
2338 'LEFT JOIN cust_main USING ( custnum ) ',
2339 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
2340 'extra_sql' => $search, #important
2342 or return { 'error' => "Service not found" };
2344 if ( exists($p->{'old_password'}) ) {
2345 return { 'error' => "Incorrect password." }
2346 unless $svc_acct->check_password($p->{'old_password'});
2349 $svc_acct->_password($p->{'new_password'});
2350 my $error = $svc_acct->replace();
2352 my($label, $value) = $svc_acct->cust_svc->label;
2354 return { 'error' => $error,
2364 my $conf = new FS::Conf;
2365 my $verification = $conf->config('selfservice-password_reset_verification')
2366 or return { 'error' => 'Password resets disabled' };
2368 my $username = $p->{'username'};
2370 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
2371 or return { 'error' => 'Account not found' };
2373 my $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
2374 'domsvc' => $svc_domain->svcnum }
2376 or return { 'error' => 'Account not found' };
2378 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
2379 or return { 'error' => 'Account not found' };
2381 my $cust_main = $cust_pkg->cust_main;
2385 my( $p, $cust_main ) = @_;
2386 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
2387 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
2390 my( $p, $cust_main ) = @_;
2391 my $cust_pay = qsearchs({
2392 'table' => 'cust_pay',
2393 'hashref' => { 'custnum' => $cust_main->custnum },
2394 'order_by' => 'ORDER BY _date DESC LIMIT 1',
2398 $p->{'amount'} == $cust_pay->paid;
2401 my( $p, $cust_main ) = @_;
2402 $p->{'zip'} eq $cust_main->zip
2403 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
2407 foreach my $verify ( split(',', $verification) ) {
2409 &{ $verify{$verify} }( $p, $cust_main )
2410 or return { 'error' => 'Account not found' };
2414 #okay, we're verified, now create a unique session
2416 my $reset_session = {
2417 'svcnum' => $svc_acct->svcnum,
2420 my $timeout = '1 hour'; #?
2422 my $reset_session_id;
2424 $reset_session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
2425 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") ); #just in case
2427 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
2431 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $cust_main->agentnum);
2432 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
2433 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
2434 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
2435 my $error = $msg_template->send( 'cust_main' => $cust_main,
2436 'object' => $svc_acct,
2437 'substitutions' => {
2438 'session_id' => $reset_session_id,
2442 return { 'error' => $error }; #????
2445 return { 'error' => '' };
2448 sub check_reset_passwd {
2451 my $conf = new FS::Conf;
2452 my $verification = $conf->config('selfservice-password_reset_verification')
2453 or return { 'error' => 'Password resets disabled' };
2455 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
2456 or return { 'error' => "Can't resume session" }; #better error message
2458 my $svcnum = $reset_session->{'svcnum'};
2460 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
2461 or return { 'error' => "Service not found" };
2463 return { 'error' => '',
2464 'username' => $svc_acct->username,
2469 sub process_reset_passwd {
2472 my $conf = new FS::Conf;
2473 my $verification = $conf->config('selfservice-password_reset_verification')
2474 or return { 'error' => 'Password resets disabled' };
2476 return { 'error' => "New passwords don't match." }
2477 if $p->{'new_password'} ne $p->{'new_password2'};
2479 return { 'error' => 'Enter new password' }
2480 unless length($p->{'new_password'});
2482 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
2483 or return { 'error' => "Can't resume session" }; #better error message
2485 my $svcnum = $reset_session->{'svcnum'};
2487 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
2488 or return { 'error' => "Service not found" };
2490 $svc_acct->_password($p->{'new_password'});
2491 my $error = $svc_acct->replace();
2493 my($label, $value) = $svc_acct->cust_svc->label;
2495 return { 'error' => $error,
2504 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2505 return { 'error' => $session } if $context eq 'error';
2507 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
2508 FS::TicketSystem->init();
2510 my $conf = new FS::Conf;
2511 my $queue = $p->{'queue'}
2512 || $conf->config('ticket_system-selfservice_queueid')
2513 || $conf->config('ticket_system-default_queueid');
2515 warn "$me create_ticket: creating ticket\n" if $DEBUG;
2516 my $err_or_ticket = FS::TicketSystem->create_ticket(
2517 '', #create RT session based on FS CurrentUser (fs_selfservice)
2519 'custnum' => $custnum,
2520 'svcnum' => $session->{'svcnum'},
2521 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
2524 if ( ref($err_or_ticket) ) {
2525 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
2527 return { 'error' => '',
2528 'ticket_id' => $err_or_ticket->id,
2531 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
2533 return { 'error' => $err_or_ticket };
2541 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2542 return { 'error' => $session } if $context eq 'error';
2544 return { error => 'requested format not implemented' }
2545 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
2547 my $conf = new FS::Conf;
2548 my $age_threshold = 0;
2549 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
2550 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
2552 my $search = { 'custnum' => $custnum };
2553 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2554 my $cust_main = qsearchs('cust_main', $search )
2555 or return { 'error' => "unknown custnum $custnum" };
2557 # does it make more sense to just run one sql query for this instead of all the
2558 # insanity below? would increase performance greately for large data sets?
2560 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
2561 my @part_svc = $cust_pkg->part_svc;
2562 foreach my $part_svc ( @part_svc ) {
2563 if($part_svc->svcdb eq 'svc_phone'){
2564 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
2565 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
2566 push @svc_phone, $cust_pkg_svc->svc_x
2567 if $cust_pkg_svc->date_inserted >= $age_threshold;
2575 my($xls_r,$xls_c) = (0,0);
2578 my @fields = qw( countrycode phonenum pin sip_password phone_name );
2579 if($p->{'format'} eq 'csv') {
2580 $csv = new Text::CSV_XS { 'always_quote' => 1,
2583 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
2584 $content .= $csv->string;
2586 elsif($p->{'format'} eq 'xls') {
2587 my $XLS1 = new IO::Scalar \$content;
2588 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
2589 or return { 'error' => "Error opening .xls file: $!" };
2590 $xls = $xls_workbook->add_worksheet('DIDs');
2591 foreach ( @fields ) {
2592 $xls->write(0,$xls_c++,$_);
2597 foreach my $svc_phone ( @svc_phone ) {
2598 my @cols = map { $svc_phone->$_ } @fields;
2599 if($p->{'format'} eq 'csv') {
2600 return { 'error' => 'Unable to create CSV' }
2601 unless $csv->combine(@cols);
2602 $content .= $csv->string;
2604 elsif($p->{'format'} eq 'xls') {
2607 $xls->write($xls_r,$xls_c++,$_);
2613 $xls_workbook->close() if $p->{'format'} eq 'xls';
2615 { content => $content, format => $p->{'format'}, };
2620 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2621 return { 'error' => $session } if $context eq 'error';
2623 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
2624 FS::TicketSystem->init();
2625 return { 'error' => 'get_ticket configuration error' }
2626 if $FS::TicketSystem::system ne 'RT_Internal';
2628 # check existence and ownership as part of this
2629 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
2630 my $rt_session = FS::TicketSystem->session('');
2631 my $Ticket = FS::TicketSystem->get_ticket_object(
2633 ticket_id => $p->{'ticket_id'},
2636 return { 'error' => 'ticket not found' } if !$Ticket;
2638 if ( length( $p->{'subject'} || '' ) ) {
2640 if ( $p->{'subject'} ne $Ticket->Subject ) {
2641 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
2642 return { 'error' => "unable to set subject: $msg" } if !$val;
2646 if(length($p->{'reply'})) {
2647 my @err_or_res = FS::TicketSystem->correspond_ticket(
2649 'ticket_id' => $p->{'ticket_id'},
2650 'content' => $p->{'reply'},
2653 return { 'error' => 'unable to reply to ticket' }
2654 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
2657 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
2658 my $err_or_ticket = FS::TicketSystem->get_ticket(
2660 'ticket_id' => $p->{'ticket_id'},
2663 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
2664 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
2666 return { 'error' => $err_or_ticket };
2669 my @custs = @{$err_or_ticket->{'custs'}};
2670 my @txns = @{$err_or_ticket->{'txns'}};
2673 # superseded by check in get_ticket_object
2674 #return { 'error' => 'invalid ticket requested' }
2675 #unless grep($_ eq $custnum, @custs);
2677 foreach my $txn ( @txns ) {
2678 push @filtered_txns, $txn
2679 if ($txn->{'type'} eq 'EmailRecord'
2680 || $txn->{'type'} eq 'Correspond'
2681 || $txn->{'type'} eq 'Create');
2684 warn "$me get_ticket: successful: \n"
2686 return { 'error' => '',
2687 'transactions' => \@filtered_txns,
2688 'ticket_fields' => $err_or_ticket->{'fields'},
2689 'ticket_id' => $p->{'ticket_id'},
2693 sub adjust_ticket_priority {
2695 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2696 return { 'error' => $session } if $context eq 'error';
2698 warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
2699 FS::TicketSystem->init;
2700 my $ss_priority = FS::TicketSystem->selfservice_priority;
2702 return { 'error' => 'adjust_ticket_priority configuration error' }
2703 if $FS::TicketSystem::system ne 'RT_Internal'
2706 my $values = $p->{'values'}; #hashref, id => priority value
2709 foreach my $id (keys %$values) {
2710 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
2711 my $Ticket = FS::TicketSystem->get_ticket_object('',
2713 'custnum' => $custnum,
2716 $ticket_error{$id} = 'ticket not found';
2720 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
2721 # We're not going to implement it for RT_External.
2722 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
2723 my $new_value = $values->{$id};
2724 next if $old_value eq $new_value;
2726 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
2728 # AddCustomFieldValue works fine (replacing any existing value) if it's
2729 # a single-valued custom field, which it should be. If it's not, you're
2730 # doing something wrong.
2732 if ( length($new_value) ) {
2733 ($val, $msg) = $Ticket->AddCustomFieldValue(
2734 Field => $ss_priority,
2735 Value => $new_value,
2739 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
2740 Field => $ss_priority,
2741 Value => $old_value,
2745 $ticket_error{$id} = $msg if !$val;
2746 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
2748 return { 'error' => '',
2749 'ticket_error' => \%ticket_error,
2750 %{ customer_info($p) } # send updated customer info back
2756 sub _custoragent_session_custnum {
2759 my($context, $session, $custnum);
2760 if ( $p->{'session_id'} ) {
2762 $context = 'customer';
2763 $session = _cache->get($p->{'session_id'})
2764 or return ( 'error' => "Can't resume session" ); #better error message
2765 $custnum = $session->{'custnum'};
2767 } elsif ( $p->{'agent_session_id'} ) {
2770 my $agent_cache = new FS::ClientAPI_SessionCache( {
2771 'namespace' => 'FS::ClientAPI::Agent',
2773 $session = $agent_cache->get($p->{'agent_session_id'})
2774 or return ( 'error' => "Can't resume session" ); #better error message
2775 $custnum = $p->{'custnum'};
2779 return ( 'error' => "Can't resume session" ); #better error message
2782 ($context, $session, $custnum);