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 );
8 use Digest::MD5 qw(md5_hex);
10 use Business::CreditCard;
12 use Time::Local qw(timelocal_nocheck);
13 use FS::UI::Web::small_custview qw(small_custview); #less doh
15 use FS::UI::bytecount qw( display_bytecount );
18 use FS::Record qw(qsearch qsearchs dbh);
19 use FS::Msgcat qw(gettext);
20 use FS::Misc qw(card_types);
21 use FS::Misc::DateTime qw(parse_datetime);
22 use FS::ClientAPI_SessionCache;
31 use FS::cust_main_county;
34 use FS::acct_rt_transaction;
39 use Spreadsheet::WriteExcel;
42 $me = '[FS::ClientAPI::MyAccount]';
44 use vars qw( @cust_main_editable_fields );
45 @cust_main_editable_fields = qw(
46 first last company address1 address2 city
47 county state zip country daytime night fax
48 ship_first ship_last ship_company ship_address1 ship_address2 ship_city
49 ship_state ship_zip ship_country ship_daytime ship_night ship_fax
50 payby payinfo payname paystart_month paystart_year payissue payip
51 ss paytype paystate stateid stateid_state
55 $cache ||= new FS::ClientAPI_SessionCache( {
56 'namespace' => 'FS::ClientAPI::MyAccount',
63 my($context, $session, $custnum) = _custoragent_session_custnum($p);
64 #return { 'error' => $session } if $context eq 'error';
67 if ( $context eq 'customer' ) {
69 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
72 $sth->execute($custnum) or die $sth->errstr;
74 $agentnum = $sth->fetchrow_arrayref->[0]
75 or die "no agentnum for custnum $custnum";
77 #} elsif ( $context eq 'agent' ) {
78 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
82 my $conf = new FS::Conf;
84 #false laziness w/Signup.pm
86 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
88 if ( $skin_info_cache_agent ) {
90 warn "$me loading cached skin info for agentnum $agentnum\n"
95 warn "$me populating skin info cache for agentnum $agentnum\n"
98 $skin_info_cache_agent = {
99 'agentnum' => $agentnum,
100 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
101 qw( company_name date_format ) ),
102 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
103 qw( body_bgcolor box_bgcolor
104 text_color link_color vlink_color hlink_color alink_color
105 font title_color title_align title_size menu_bgcolor menu_fontsize
108 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
109 qw( menu_skipblanks menu_skipheadings menu_nounderline )
111 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
112 qw( title_left_image title_right_image
113 menu_top_image menu_body_image menu_bottom_image
116 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
117 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
118 qw( head body_header body_footer company_address ) ),
121 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
125 #{ %$skin_info_cache_agent };
126 $skin_info_cache_agent;
133 my $conf = new FS::Conf;
137 'phone_login' => $conf->exists('selfservice_server-phone_login'),
138 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
145 #false laziness w/FS::ClientAPI::passwd::passwd
149 my $conf = new FS::Conf;
152 if ( $p->{'domain'} eq 'svc_phone'
153 && $conf->exists('selfservice_server-phone_login') ) {
155 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
156 return { error => 'Number not found.' } unless $svc_phone;
159 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
160 #return { error => 'Only primary user may log in.' }
161 # if $conf->exists('selfservice_server-primary_only')
162 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
164 return { error => 'Incorrect PIN.' }
165 unless $svc_phone->check_pin($p->{'password'});
171 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
172 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
174 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
175 'domsvc' => $svc_domain->svcnum, }
177 return { error => 'User not found.' } unless $svc_acct;
179 if($conf->exists('selfservice_server-login_svcpart')) {
180 my @svcpart = $conf->config('selfservice_server-login_svcpart');
181 my $svcpart = $svc_acct->cust_svc->svcpart;
182 return { error => 'Invalid user.' }
183 unless grep($_ eq $svcpart, @svcpart);
186 return { error => 'Incorrect password.' }
187 unless $svc_acct->check_password($p->{'password'});
194 'svcnum' => $svc_x->svcnum,
197 my $cust_svc = $svc_x->cust_svc;
198 my $cust_pkg = $cust_svc->cust_pkg;
200 my $cust_main = $cust_pkg->cust_main;
201 $session->{'custnum'} = $cust_main->custnum;
202 if ( $conf->exists('pkg-balances') ) {
203 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
204 $cust_main->ncancelled_pkgs;
205 $session->{'pkgnum'} = $cust_pkg->pkgnum
206 if scalar(@cust_pkg) > 1;
210 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
211 #return { error => 'Only primary user may log in.' }
212 # if $conf->exists('selfservice_server-primary_only')
213 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
214 my $part_pkg = $cust_pkg->part_pkg;
215 return { error => 'Only primary user may log in.' }
216 if $conf->exists('selfservice_server-primary_only')
217 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
221 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
222 } until ( ! defined _cache->get($session_id) ); #just in case
224 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
225 _cache->set( $session_id, $session, $timeout );
227 return { 'error' => '',
228 'session_id' => $session_id,
234 if ( $p->{'session_id'} ) {
235 _cache->remove($p->{'session_id'});
236 return { %{ skin_info($p) }, 'error' => '' };
238 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
242 sub payment_gateway {
244 # takes a cust_main and a cust_payby entry, returns the payment_gateway
245 my $conf = new FS::Conf;
246 my $cust_main = shift;
247 my $cust_payby = shift;
248 my $gatewaynum = $conf->config('selfservice-payment_gateway');
250 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
251 die "configured gatewaynum $gatewaynum not found!" if !$pg;
255 return '' if ! FS::payby->realtime($cust_payby);
256 my $pg = $cust_main->agent->payment_gateway(
257 'method' => FS::payby->payby2bop($cust_payby),
267 my $conf = new FS::Conf;
269 my $info = skin_info($p);
271 use vars qw( $cust_paybys ); #cache for performance
272 unless ( $cust_paybys ) {
274 my %cust_paybys = map { $_ => 1 }
275 map { FS::payby->payby2payment($_) }
276 $conf->config('signup_server-payby');
278 $cust_paybys = [ keys %cust_paybys ];
281 $info->{'cust_paybys'} = $cust_paybys;
283 my($context, $session, $custnum) = _custoragent_session_custnum($p);
284 return { 'error' => $session } if $context eq 'error';
286 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
287 or return { 'error' => "unknown custnum $custnum" };
289 $info->{'hide_payment_fields'} = [
291 my $pg = payment_gateway($cust_main, $_);
292 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
293 } @{ $info->{cust_paybys} }
296 $info->{'self_suspend_reason'} =
297 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
299 $info->{'edit_ticket_subject'} =
300 $conf->exists('ticket_system-selfservice_edit_subject') &&
301 $cust_main->edit_subject;
304 'custnum' => $custnum,
305 'access_pkgnum' => $session->{'pkgnum'},
306 'access_svcnum' => $session->{'svcnum'},
313 my($context, $session, $custnum) = _custoragent_session_custnum($p);
314 return { 'error' => $session } if $context eq 'error';
318 my $conf = new FS::Conf;
319 if ($conf->exists('cust_main-require_address2')) {
320 $return{'require_address2'} = '1';
322 $return{'require_address2'} = '';
325 if ( $FS::TicketSystem::system ) {
326 warn "$me customer_info: initializing ticket system\n" if $DEBUG;
327 FS::TicketSystem->init();
330 if ( $custnum ) { #customer record
332 my $search = { 'custnum' => $custnum };
333 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
334 my $cust_main = qsearchs('cust_main', $search )
335 or return { 'error' => "unknown custnum $custnum" };
337 if ( $session->{'pkgnum'} ) {
338 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
340 $return{balance} = $cust_main->balance;
343 my @tickets = $cust_main->tickets;
344 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
345 if ( FS::TicketSystem->selfservice_priority ) {
346 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
350 ($a->{'_selfservice_priority'} eq '') <=>
351 ($b->{'_selfservice_priority'} eq '')
354 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
360 $return{tickets} = \@tickets;
363 unless ( $session->{'pkgnum'} ) {
366 invnum => $_->invnum,
367 date => time2str("%b %o, %Y", $_->_date),
370 } $cust_main->open_cust_bill;
371 $return{open_invoices} = \@open;
374 $return{small_custview} =
375 small_custview( $cust_main,
376 scalar($conf->config('countrydefault')),
377 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
380 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
382 for (@cust_main_editable_fields) {
383 $return{$_} = $cust_main->get($_);
386 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
387 $return{payinfo} = $cust_main->paymask;
388 @return{'month', 'year'} = $cust_main->paydate_monthyear;
391 $return{'invoicing_list'} =
392 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
393 $return{'postal_invoicing'} =
394 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
396 if (scalar($conf->config('support_packages'))) {
397 my @support_services = ();
398 foreach ($cust_main->support_services) {
399 my $seconds = $_->svc_x->seconds;
400 my $time_remaining = (($seconds < 0) ? '-' : '' ).
401 int(abs($seconds)/3600)."h".
402 sprintf("%02d",(abs($seconds)%3600)/60)."m";
403 my $cust_pkg = $_->cust_pkg;
406 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
407 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
408 push @support_services, { svcnum => $_->svcnum,
409 time => $time_remaining,
414 $return{support_services} = \@support_services;
417 if ( $conf->config('prepayment_discounts-credit_type') ) {
419 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
422 if ( $session->{'svcnum'} ) {
423 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
424 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
427 } elsif ( $session->{'svcnum'} ) { #no customer record
429 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
430 or die "unknown svcnum";
431 $return{name} = $svc_acct->email;
435 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
439 return { 'error' => '',
440 'custnum' => $custnum,
448 my $session = _cache->get($p->{'session_id'})
449 or return { 'error' => "Can't resume session" }; #better error message
451 my $custnum = $session->{'custnum'}
452 or return { 'error' => "no customer record" };
454 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
455 or return { 'error' => "unknown custnum $custnum" };
457 my $new = new FS::cust_main { $cust_main->hash };
458 $new->set( $_ => $p->{$_} )
459 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
462 if (exists($p->{'payby'})) {
463 $p->{'payby'} =~ /^([A-Z]{4})$/
464 or return { 'error' => "illegal_payby " . $p->{'payby'} };
468 if ( $payby =~ /^(CARD|DCRD)$/ ) {
470 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
472 if ( $new->payinfo eq $cust_main->paymask ) {
473 $new->payinfo($cust_main->payinfo);
475 $new->payinfo($p->{'payinfo'});
478 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
480 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
483 $p->{'payinfo1'} =~ /^([\dx]+)$/
484 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
486 $p->{'payinfo2'} =~ /^([\dx]+)$/
487 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
489 $payinfo = $payinfo1. '@'. $payinfo2;
491 $new->payinfo( ($payinfo eq $cust_main->paymask)
492 ? $cust_main->payinfo
496 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
498 } elsif ( $payby =~ /^(BILL)$/ ) {
500 } elsif ( $payby ) { #notyet ready
501 return { 'error' => "unknown payby $payby" };
505 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
506 #false laziness with httemplate/edit/process/cust_main.cgi
507 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
508 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
510 @invoicing_list = $cust_main->invoicing_list;
513 my $error = $new->replace($cust_main, \@invoicing_list);
514 return { 'error' => $error } if $error;
517 return { 'error' => '' };
522 my $session = _cache->get($p->{'session_id'})
523 or return { 'error' => "Can't resume session" }; #better error message
529 my $conf = new FS::Conf;
530 use vars qw($payment_info); #cache for performance
531 unless ( $payment_info ) {
533 my %states = map { $_->state => 1 }
534 qsearch('cust_main_county', {
535 'country' => $conf->config('countrydefault') || 'US'
538 my %cust_paybys = map { $_ => 1 }
539 map { FS::payby->payby2payment($_) }
540 $conf->config('signup_server-payby');
542 my @cust_paybys = keys %cust_paybys;
546 #list all counties/states/countries
547 'cust_main_county' =>
548 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
550 #shortcut for one-country folks
552 [ sort { $a cmp $b } keys %states ],
554 'card_types' => card_types(),
556 'paytypes' => [ @FS::cust_main::paytypes ],
558 'paybys' => [ $conf->config('signup_server-payby') ],
559 'cust_paybys' => \@cust_paybys,
561 'stateid_label' => FS::Msgcat::_gettext('stateid'),
562 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
564 'show_ss' => $conf->exists('show_ss'),
565 'show_stateid' => $conf->exists('show_stateid'),
566 'show_paystate' => $conf->exists('show_bankstate'),
568 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
570 'credit_card_surcharge_percentage' => $conf->config('credit-card-surcharge-percentage'),
579 my %return = %$payment_info;
581 my $custnum = $session->{'custnum'};
583 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
584 or return { 'error' => "unknown custnum $custnum" };
586 $return{'hide_payment_fields'} = [
588 my $pg = payment_gateway($cust_main, $_);
589 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
590 } @{ $return{cust_paybys} }
593 $return{balance} = $cust_main->balance; #XXX pkg-balances?
595 $return{payname} = $cust_main->payname
596 || ( $cust_main->first. ' '. $cust_main->get('last') );
598 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
600 $return{payby} = $cust_main->payby;
601 $return{stateid_state} = $cust_main->stateid_state;
603 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
604 $return{card_type} = cardtype($cust_main->payinfo);
605 $return{payinfo} = $cust_main->paymask;
607 @return{'month', 'year'} = $cust_main->paydate_monthyear;
611 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
612 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
613 $return{payinfo1} = $payinfo1;
614 $return{payinfo2} = $payinfo2;
615 $return{paytype} = $cust_main->paytype;
616 $return{paystate} = $cust_main->paystate;
620 if ( $conf->config('prepayment_discounts-credit_type') ) {
622 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
625 #doubleclick protection
627 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
629 return { 'error' => '',
635 #some false laziness with httemplate/process/payment.cgi - look there for
636 #ACH and CVV support stuff
637 sub process_payment {
641 my $session = _cache->get($p->{'session_id'})
642 or return { 'error' => "Can't resume session" }; #better error message
646 my $custnum = $session->{'custnum'};
648 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
649 or return { 'error' => "unknown custnum $custnum" };
651 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
652 or return { 'error' => gettext('illegal_amount') };
654 return { error => 'Amount must be greater than 0' } unless $amount > 0;
656 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
657 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
658 my $discount_term = $1;
660 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
661 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
664 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
665 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
668 $p->{'payby'} ||= 'CARD';
669 $p->{'payby'} =~ /^([A-Z]{4})$/
670 or return { 'error' => "illegal_payby " . $p->{'payby'} };
673 #false laziness w/process/payment.cgi
677 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
679 $p->{'payinfo1'} =~ /^([\dx]+)$/
680 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
682 $p->{'payinfo2'} =~ /^([\dx]+)$/
683 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
685 $payinfo = $payinfo1. '@'. $payinfo2;
687 $payinfo = $cust_main->payinfo
688 if $cust_main->paymask eq $payinfo;
690 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
692 $payinfo = $p->{'payinfo'};
694 #more intelligent mathing will be needed here if you change
695 #card_masking_method and don't remove existing paymasks
696 $payinfo = $cust_main->payinfo
697 if $cust_main->paymask eq $payinfo;
700 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
701 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
705 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
706 return { 'error' => gettext('unknown_card_type') }
707 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
709 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
710 if ( cardtype($payinfo) eq 'American Express card' ) {
711 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
712 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
715 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
716 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
722 die "unknown payby $payby";
726 'CARD' => [ qw( paystart_month paystart_year payissue payip
727 address1 address2 city state zip country ) ],
728 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
731 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
733 'payinfo' => $payinfo,
734 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
735 'payname' => $payname,
736 'paybatch' => $paybatch, #this doesn't actually do anything
738 'paynum_ref' => \$paynum,
739 'pkgnum' => $session->{'pkgnum'},
740 'discount_term' => $discount_term,
742 map { $_ => $p->{$_} } @{ $payby2fields{$payby} }
744 return { 'error' => $error } if $error;
746 $cust_main->apply_payments;
748 if ( $p->{'save'} ) {
749 my $new = new FS::cust_main { $cust_main->hash };
750 if ($payby eq 'CARD' || $payby eq 'DCRD') {
751 $new->set( $_ => $p->{$_} )
752 foreach qw( payname paystart_month paystart_year payissue payip
753 address1 address2 city state zip country );
754 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
755 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
756 $new->set( $_ => $p->{$_} )
757 foreach qw( payname payip paytype paystate
758 stateid stateid_state );
759 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
761 $new->set( 'payinfo' => $cust_main->card_token || $payinfo );
762 $new->set( 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01' );
763 my $error = $new->replace($cust_main);
765 #no, this causes customers to process their payments again
766 #return { 'error' => $error };
767 #XXX just warn verosely for now so i can figure out how these happen in
768 # the first place, eventually should redirect them to the "change
769 #address" page but indicate the payment did process??
770 delete($p->{'payinfo'}); #don't want to log this!
771 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
772 "NEW: ". Dumper($new)."\n".
773 "OLD: ". Dumper($cust_main)."\n".
774 "PACKET: ". Dumper($p)."\n";
781 my $receipt_html = '';
783 # currently supported for realtime CC only; send receipt data to SS
784 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
787 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
790 <TD ALIGN="right">Payment#</TD>
791 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
795 <TD ALIGN="right">Date</TD>
797 <TD BGCOLOR="#FFFFFF"><B>! .
798 time2str("%a %b %o, %Y %r", $cust_pay->_date)
804 <TD ALIGN="right">Amount</TD>
805 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paid . qq!</B></TD>
810 <TD ALIGN="right">Payment method</TD>
811 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
820 return { 'error' => '', 'receipt_html' => $receipt_html, };
824 sub realtime_collect {
827 my $session = _cache->get($p->{'session_id'})
828 or return { 'error' => "Can't resume session" }; #better error message
830 my $custnum = $session->{'custnum'};
832 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
833 or return { 'error' => "unknown custnum $custnum" };
836 if ( $p->{'amount'} ) {
837 $amount = $p->{'amount'};
839 elsif ( $session->{'pkgnum'} ) {
840 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
843 $amount = $cust_main->balance;
846 my $error = $cust_main->realtime_collect(
847 'method' => $p->{'method'},
849 'pkgnum' => $session->{'pkgnum'},
850 'session_id' => $p->{'session_id'},
854 return { 'error' => $error } unless ref( $error );
856 return { 'error' => '', amount => $amount, %$error };
859 sub process_payment_order_pkg {
862 my $hr = process_payment($p);
863 return $hr if $hr->{'error'};
868 sub process_payment_order_renew {
871 my $hr = process_payment($p);
872 return $hr if $hr->{'error'};
881 my $session = _cache->get($p->{'session_id'})
882 or return { 'error' => "Can't resume session" }; #better error message
886 my $custnum = $session->{'custnum'};
888 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
889 or return { 'error' => "unknown custnum $custnum" };
891 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
892 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
900 return { 'error' => $error } if $error;
902 return { 'error' => '',
904 'seconds' => $seconds,
905 'duration' => duration_exact($seconds),
906 'upbytes' => $upbytes,
907 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
908 'downbytes' => $downbytes,
909 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
910 'totalbytes'=> $totalbytes,
911 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
918 my $session = _cache->get($p->{'session_id'})
919 or return { 'error' => "Can't resume session" }; #better error message
921 my $custnum = $session->{'custnum'};
923 my $invnum = $p->{'invnum'};
925 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
926 'custnum' => $custnum } )
927 or return { 'error' => "Can't find invnum" };
931 return { 'error' => '',
933 'invoice_text' => join('', $cust_bill->print_text ),
934 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
942 #sessioning for this? how do we get the session id to the backend invoice
943 # template so it can add it to the link, blah
946 if ( $p->{'invnum'} ) {
947 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
948 or return { 'error' => 'unknown invnum' };
949 $agentnum = $cust_bill->cust_main->agentnum;
952 my $templatename = $p->{'template'} || $p->{'templatename'};
954 #false laziness-ish w/view/cust_bill-logo.cgi
956 my $conf = new FS::Conf;
957 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
958 $templatename = "_$1";
963 my $filename = "logo$templatename.png";
965 return { 'error' => '',
966 'logo' => $conf->config_binary($filename, $agentnum),
967 'content_type' => 'image/png', #should allow gif, jpg too
974 my $session = _cache->get($p->{'session_id'})
975 or return { 'error' => "Can't resume session" }; #better error message
977 my $custnum = $session->{'custnum'};
979 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
980 or return { 'error' => "unknown custnum $custnum" };
982 my @cust_bill = $cust_main->cust_bill;
984 return { 'error' => '',
985 'invoices' => [ map { { 'invnum' => $_->invnum,
986 '_date' => $_->_date,
987 'date' => time2str("%b %o, %Y", $_->_date),
996 my $session = _cache->get($p->{'session_id'})
997 or return { 'error' => "Can't resume session" }; #better error message
999 my $custnum = $session->{'custnum'};
1001 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1002 or return { 'error' => "unknown custnum $custnum" };
1004 my @errors = $cust_main->cancel( 'quiet'=>1 );
1006 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1008 return { 'error' => $error };
1015 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1016 return { 'error' => $session } if $context eq 'error';
1018 my $search = { 'custnum' => $custnum };
1019 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1020 my $cust_main = qsearchs('cust_main', $search )
1021 or return { 'error' => "unknown custnum $custnum" };
1023 my $conf = new FS::Conf;
1025 # the duplication below is necessary:
1026 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1027 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1028 # non-backwards-compatible change breaking the software of anyone using the API
1029 # instead of the stock selfservice
1030 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1031 if( $conf->exists('selfservice_server-view-wholesale') ) {
1032 return { 'svcnum' => $session->{'svcnum'},
1033 'custnum' => $custnum,
1034 'cust_pkg' => [ map {
1036 part_pkg => [ map $_->hashref, $_->part_pkg ],
1038 [ map $_->hashref, $_->available_part_svc ],
1040 [ map { my $ref = { $_->hash,
1041 label => [ $_->label ],
1043 $ref->{_password} = $_->svc_x->_password
1044 if $context eq 'agent'
1045 && $conf->exists('agent-showpasswords')
1046 && $_->part_svc->svcdb eq 'svc_acct';
1051 } $cust_main->cust_pkg
1054 small_custview( $cust_main, $conf->config('countrydefault') ),
1055 'wholesale_view' => 1,
1056 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1057 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1058 'lnp' => $conf->exists('svc_phone-lnp'),
1062 { 'svcnum' => $session->{'svcnum'},
1063 'custnum' => $custnum,
1064 'cust_pkg' => [ map {
1065 my $primary_cust_svc = $_->primary_cust_svc;
1068 #status => $_->status,
1070 [ map $_->hashref, $_->available_part_svc ],
1072 [ map { my $ref = { $_->hash,
1073 label => [ $_->label ],
1075 $ref->{_password} = $_->svc_x->_password
1076 if $context eq 'agent'
1077 && $conf->exists('agent-showpasswords')
1078 && $_->part_svc->svcdb eq 'svc_acct';
1079 $ref->{svchash} = { $_->svc_x->hash } if
1080 $_->part_svc->svcdb eq 'svc_phone';
1081 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1082 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1088 ? { $primary_cust_svc->hash,
1089 label => [ $primary_cust_svc->label ],
1090 finger => $primary_cust_svc->svc_x->finger, #uuh
1091 $primary_cust_svc->part_svc->hash,
1095 } $cust_main->ncancelled_pkgs
1098 small_custview( $cust_main, $conf->config('countrydefault') ),
1106 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1107 return { 'error' => $session } if $context eq 'error';
1109 my $search = { 'custnum' => $custnum };
1110 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1111 my $cust_main = qsearchs('cust_main', $search )
1112 or return { 'error' => "unknown custnum $custnum" };
1115 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1116 foreach my $cust_pkg ( $p->{'ncancelled'}
1117 ? $cust_main->ncancelled_pkgs
1118 : $cust_main->unsuspended_pkgs ) {
1119 next if $session->{'pkgnum'} && $cust_pkg->pkgnum != $session->{'pkgnum'};
1120 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1122 if ( $p->{'svcdb'} ) {
1123 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1125 : ref($p->{'svcdb'}) eq 'ARRAY'
1126 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1127 : { $p->{'svcdb'} => 1 };
1128 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1131 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1134 my $conf = new FS::Conf;
1137 'svcnum' => $session->{'svcnum'},
1138 'custnum' => $custnum,
1139 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1140 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1143 my $svc_x = $_->svc_x;
1144 my($label, $value) = $_->label;
1145 my $svcdb = $_->part_svc->svcdb;
1146 my $part_pkg = $_->cust_pkg->part_pkg;
1149 'svcnum' => $_->svcnum,
1155 if ( $svcdb eq 'svc_acct' ) {
1158 'username' => $svc_x->username,
1159 'email' => $svc_x->email,
1160 'seconds' => $svc_x->seconds,
1161 'upbytes' => display_bytecount($svc_x->upbytes),
1162 'downbytes' => display_bytecount($svc_x->downbytes),
1163 'totalbytes' => display_bytecount($svc_x->totalbytes),
1165 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1166 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1167 'recharge_upbytes' =>
1168 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1169 'recharge_downbytes' =>
1170 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1171 'recharge_totalbytes' =>
1172 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1176 } elsif ( $svcdb eq 'svc_phone' || $svcdb eq 'svc_port' ) {
1192 _usage_details( \&_port_graph, $p,
1193 'svcdb' => 'svc_port',
1198 my($svc_port, $begin, $end) = @_;
1200 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
1201 push @usage, { 'png' => $pngOrError };
1205 sub _list_svc_usage {
1206 my($svc_acct, $begin, $end) = @_;
1208 foreach my $part_export (
1209 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1210 qw( sqlradius sqlradius_withdomain )
1212 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1217 sub list_svc_usage {
1218 _usage_details(\&_list_svc_usage, @_);
1221 sub _list_support_usage {
1222 my($svc_acct, $begin, $end) = @_;
1224 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1225 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1227 push @usage, { 'seconds' => $_->seconds,
1228 'support' => $_->support,
1229 '_date' => $_->_date,
1230 'id' => $_->transaction_id,
1231 'creator' => $_->creator,
1232 'subject' => $_->subject,
1233 'status' => $_->status,
1234 'ticketid' => $_->ticketid,
1240 sub list_support_usage {
1241 _usage_details(\&_list_support_usage, @_);
1244 sub _list_cdr_usage {
1245 my($svc_phone, $begin, $end) = @_;
1246 map [ $_->downstream_csv('format' => 'default', 'keeparray' => 1) ], #XXX config for format
1247 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1250 sub list_cdr_usage {
1252 _usage_details( \&_list_cdr_usage, $p,
1253 'svcdb' => 'svc_phone',
1257 sub _usage_details {
1258 my($callback, $p, %opt) = @_;
1260 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1261 return { 'error' => $session } if $context eq 'error';
1263 my $search = { 'svcnum' => $p->{'svcnum'} };
1264 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1266 my $svcdb = $opt{'svcdb'} || 'svc_acct';
1268 my $svc_x = qsearchs( $svcdb, $search );
1269 return { 'error' => 'No service selected in list_svc_usage' }
1272 my $header = $svcdb eq 'svc_phone'
1273 ? [ split(',', FS::cdr::invoice_header('default') ) ] #XXX
1276 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
1277 my $freq = $cust_pkg->part_pkg->freq;
1278 my $start = $cust_pkg->setup;
1279 #my $end = $cust_pkg->bill; # or time?
1282 unless ( $p->{beginning} ) {
1283 $p->{beginning} = $cust_pkg->last_bill;
1284 $p->{ending} = $end;
1287 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending});
1289 #kinda false laziness with FS::cust_main::bill, but perhaps
1290 #we should really change this bit to DateTime and DateTime::Duration
1292 #change this bit to use Date::Manip? CAREFUL with timezones (see
1293 # mailing list archive)
1294 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
1295 (localtime($p->{ending}) )[0,1,2,3,4,5];
1296 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
1297 (localtime($p->{beginning}) )[0,1,2,3,4,5];
1299 if ( $freq =~ /^\d+$/ ) {
1301 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
1303 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
1304 } elsif ( $freq =~ /^(\d+)w$/ ) {
1306 $nmday += $weeks * 7;
1307 $pmday -= $weeks * 7;
1308 } elsif ( $freq =~ /^(\d+)d$/ ) {
1312 } elsif ( $freq =~ /^(\d+)h$/ ) {
1317 return { 'error' => "unparsable frequency: ". $freq };
1320 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
1321 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
1325 'svcnum' => $p->{svcnum},
1326 'beginning' => $p->{beginning},
1327 'ending' => $p->{ending},
1328 'previous' => ($previous > $start) ? $previous : $start,
1329 'next' => ($next < $end) ? $next : $end,
1330 'header' => $header,
1338 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1339 return { 'error' => $session } if $context eq 'error';
1341 my $search = { 'custnum' => $custnum };
1342 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1343 my $cust_main = qsearchs('cust_main', $search )
1344 or return { 'error' => "unknown custnum $custnum" };
1346 my $status = $cust_main->status;
1347 #false laziness w/ClientAPI/Signup.pm
1349 my $cust_pkg = new FS::cust_pkg ( {
1350 'custnum' => $custnum,
1351 'pkgpart' => $p->{'pkgpart'},
1353 my $error = $cust_pkg->check;
1354 return { 'error' => $error } if $error;
1357 unless ( $p->{'svcpart'} eq 'none' ) {
1361 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
1363 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1364 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
1365 $svcdb = $part_svc->svcdb;
1367 $svcdb = 'svc_acct';
1369 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
1372 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
1373 'svc_domain' => [ qw( domain ) ],
1374 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
1375 'svc_external' => [ qw( id title ) ],
1376 'svc_pbx' => [ qw( id title ) ],
1379 my $svc_x = "FS::$svcdb"->new( {
1380 'svcpart' => $svcpart,
1381 map { $_ => $p->{$_} } @{$fields{$svcdb}}
1384 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
1387 while ( length($p->{"snarf_machine$snarfnum"}) ) {
1388 my $acct_snarf = new FS::acct_snarf ( {
1389 'machine' => $p->{"snarf_machine$snarfnum"},
1390 'protocol' => $p->{"snarf_protocol$snarfnum"},
1391 'username' => $p->{"snarf_username$snarfnum"},
1392 '_password' => $p->{"snarf_password$snarfnum"},
1395 push @acct_snarf, $acct_snarf;
1397 $svc_x->child_objects( \@acct_snarf );
1400 my $y = $svc_x->setdefault; # arguably should be in new method
1401 return { 'error' => $y } if $y && !ref($y);
1403 $error = $svc_x->check;
1404 return { 'error' => $error } if $error;
1411 tie my %hash, 'Tie::RefHash';
1412 %hash = ( $cust_pkg => \@svc );
1414 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
1415 return { 'error' => $error } if $error;
1417 my $conf = new FS::Conf;
1418 if ( $conf->exists('signup_server-realtime') ) {
1420 my $bill_error = _do_bop_realtime( $cust_main, $status );
1423 $cust_pkg->cancel('quiet'=>1);
1426 $cust_pkg->reexport;
1430 $cust_pkg->reexport;
1433 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
1435 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
1442 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1443 return { 'error' => $session } if $context eq 'error';
1445 my $search = { 'custnum' => $custnum };
1446 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1447 my $cust_main = qsearchs('cust_main', $search )
1448 or return { 'error' => "unknown custnum $custnum" };
1450 my $status = $cust_main->status;
1451 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
1452 or return { 'error' => "unknown package $p->{pkgnum}" };
1455 my $error = FS::cust_pkg::order( $custnum,
1461 my $conf = new FS::Conf;
1462 if ( $conf->exists('signup_server-realtime') ) {
1464 my $bill_error = _do_bop_realtime( $cust_main, $status );
1467 $newpkg[0]->suspend;
1470 $newpkg[0]->reexport;
1474 $newpkg[0]->reexport;
1477 return { error => '', pkgnum => $cust_pkg->pkgnum };
1481 sub order_recharge {
1484 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1485 return { 'error' => $session } if $context eq 'error';
1487 my $search = { 'custnum' => $custnum };
1488 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1489 my $cust_main = qsearchs('cust_main', $search )
1490 or return { 'error' => "unknown custnum $custnum" };
1492 my $status = $cust_main->status;
1493 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
1494 or return { 'error' => "unknown service " . $p->{'svcnum'} };
1496 my $svc_x = $cust_svc->svc_x;
1497 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
1500 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
1501 qw ( recharge_seconds recharge_upbytes recharge_downbytes
1502 recharge_totalbytes );
1503 my $amount = $part_pkg->option('recharge_amount', 1);
1505 my ($l, $v, $d) = $cust_svc->label; # blah
1506 my $pkg = "Recharge $v";
1508 my $bill_error = $cust_main->charge($amount, $pkg,
1509 "time: $vhash{seconds}, up: $vhash{upbytes}," .
1510 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
1511 $part_pkg->taxclass); #meh
1513 my $conf = new FS::Conf;
1514 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
1516 $bill_error = _do_bop_realtime( $cust_main, $status );
1521 my $error = $svc_x->recharge (\%vhash);
1522 return { 'error' => $error } if $error;
1526 my $error = $bill_error;
1527 $error ||= $svc_x->recharge (\%vhash);
1528 return { 'error' => $error } if $error;
1531 return { error => '', svc => $cust_svc->part_svc->svc };
1535 sub _do_bop_realtime {
1536 my ($cust_main, $status) = (shift, shift);
1538 my $old_balance = $cust_main->balance;
1540 my $bill_error = $cust_main->bill
1541 || $cust_main->apply_payments_and_credits
1542 || $cust_main->realtime_collect('selfservice' => 1);
1544 if ( $cust_main->balance > $old_balance
1545 && $cust_main->balance > 0
1546 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
1547 1 : $status eq 'suspended' ) ) {
1548 #this makes sense. credit is "un-doing" the invoice
1549 my $conf = new FS::Conf;
1550 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
1551 'self-service decline',
1552 'reason_type' => $conf->config('signup_credit_type'),
1554 $cust_main->apply_credits( 'order' => 'newest' );
1556 return { 'error' => '_decline', 'bill_error' => $bill_error };
1565 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1566 return { 'error' => $session } if $context eq 'error';
1568 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1569 or return { 'error' => "unknown custnum $custnum" };
1571 my @cust_pkg = sort { $a->bill <=> $b->bill }
1572 grep { $_->part_pkg->freq ne '0' }
1573 $cust_main->ncancelled_pkgs;
1575 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
1577 my $total = $cust_main->balance;
1580 my $bill = $_->bill;
1581 $total += $_->part_pkg->base_recur($_, \$bill);
1582 my $renew_date = $_->part_pkg->add_freq($_->bill);
1584 'pkgnum' => $_->pkgnum,
1585 'amount' => sprintf('%.2f', $total),
1586 'bill_date' => $_->bill,
1587 'bill_date_pretty' => time2str('%x', $_->bill),
1588 'renew_date' => $renew_date,
1589 'renew_date_pretty' => time2str('%x', $renew_date),
1590 'expire_date' => $_->expire,
1591 'expire_date_pretty' => time2str('%x', $_->expire),
1596 return { 'dates' => \@array };
1600 sub payment_info_renew_info {
1602 my $renew_info = renew_info($p);
1603 my $payment_info = payment_info($p);
1604 return { %$renew_info,
1612 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1613 return { 'error' => $session } if $context eq 'error';
1615 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1616 or return { 'error' => "unknown custnum $custnum" };
1618 my $date = $p->{'date'};
1622 #freeside-daily -n -d $date fs_daily $custnum
1623 $cust_main->bill_and_collect( 'time' => $date,
1624 'invoice_time' => $now,
1625 'actual_time' => $now,
1626 'check_freq' => '1d',
1629 return { 'error' => '' };
1635 my $session = _cache->get($p->{'session_id'})
1636 or return { 'error' => "Can't resume session" }; #better error message
1638 my $custnum = $session->{'custnum'};
1640 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1641 or return { 'error' => "unknown custnum $custnum" };
1643 my $conf = new FS::Conf;
1645 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
1646 or return { 'error' => 'Permission denied' };
1648 my $pkgnum = $p->{'pkgnum'};
1650 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1651 'pkgnum' => $pkgnum, } )
1652 or return { 'error' => "unknown pkgnum $pkgnum" };
1654 my $error = $cust_pkg->suspend(reason => $reasonnum);
1655 return { 'error' => $error };
1661 my $session = _cache->get($p->{'session_id'})
1662 or return { 'error' => "Can't resume session" }; #better error message
1664 my $custnum = $session->{'custnum'};
1666 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1667 or return { 'error' => "unknown custnum $custnum" };
1669 my $pkgnum = $p->{'pkgnum'};
1671 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1672 'pkgnum' => $pkgnum, } )
1673 or return { 'error' => "unknown pkgnum $pkgnum" };
1675 my $error = $cust_pkg->cancel('quiet' => 1);
1676 return { 'error' => $error };
1680 sub provision_phone {
1683 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
1685 if($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/){
1686 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1687 return { 'error' => $session } if $context eq 'error';
1689 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
1690 return { 'error' => 'service not found' } unless $svc_phone;
1691 return { 'error' => 'invalid svcnum' }
1692 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
1694 $svc_phone->email($p->{'email'})
1695 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
1696 $svc_phone->forwarddst($p->{'forwarddst'})
1697 if $svc_phone->forwarddst ne $p->{'forwarddst'}
1698 && $p->{'forwarddst'} =~ /^(\d+|)$/;
1699 return { 'error' => $svc_phone->replace };
1703 unless($p->{'lnp'}) {
1704 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
1705 $p->{'lnp_status'} = "portingin";
1706 return _provision( 'FS::svc_phone',
1707 [qw(lnp_desired_due_date lnp_other_provider
1708 lnp_other_provider_account phonenum countrycode lnp_status)],
1709 [qw(phonenum countrycode)],
1716 unless (scalar(@bulkdid)) {
1717 return _provision( 'FS::svc_phone',
1718 [qw(phonenum countrycode)],
1719 [qw(phonenum countrycode)],
1725 # bulk DID order case
1727 foreach my $did ( @bulkdid ) {
1728 $did =~ s/[^0-9]//g;
1729 $error = _provision( 'FS::svc_phone',
1730 [qw(phonenum countrycode)],
1731 [qw(phonenum countrycode)],
1733 'pkgnum' => $p->{'pkgnum'},
1734 'svcpart' => $p->{'svcpart'},
1736 'countrycode' => $p->{'countrycode'},
1737 'session_id' => $p->{'session_id'},
1740 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
1742 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
1745 sub provision_acct {
1747 warn "provision_acct called\n"
1750 return { 'error' => gettext('passwords_dont_match') }
1751 if $p->{'_password'} ne $p->{'_password2'};
1752 return { 'error' => gettext('empty_password') }
1753 unless length($p->{'_password'});
1755 if ($p->{'domsvc'}) {
1756 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
1757 qw ( svcpart pkgnum ) );
1758 return { 'error' => gettext('invalid_domain') }
1759 unless ($domains{$p->{'domsvc'}});
1762 warn "provision_acct calling _provision\n"
1764 _provision( 'FS::svc_acct',
1765 [qw(username _password domsvc)],
1766 [qw(username _password domsvc)],
1772 sub provision_external {
1774 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
1775 _provision( 'FS::svc_external',
1784 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
1785 warn "_provision called for $class\n"
1788 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1789 return { 'error' => $session } if $context eq 'error';
1791 my $search = { 'custnum' => $custnum };
1792 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1793 my $cust_main = qsearchs('cust_main', $search )
1794 or return { 'error' => "unknown custnum $custnum" };
1796 my $pkgnum = $p->{'pkgnum'};
1798 warn "searching for custnum $custnum pkgnum $pkgnum\n"
1800 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1801 'pkgnum' => $pkgnum,
1803 or return { 'error' => "unknown pkgnum $pkgnum" };
1805 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
1807 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
1808 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
1810 warn "creating $class record\n"
1812 my $svc_x = $class->new( {
1813 'pkgnum' => $p->{'pkgnum'},
1814 'svcpart' => $p->{'svcpart'},
1815 map { $_ => $p->{$_} } @$fields
1817 warn "inserting $class record\n"
1819 my $error = $svc_x->insert;
1822 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
1824 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
1827 my $return = { 'svc' => $part_svc->svc,
1829 map { $_ => $svc_x->get($_) } @$return_fields
1831 warn "_provision returning ". Dumper($return). "\n"
1840 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1841 return { 'error' => $session } if $context eq 'error';
1843 my $search = { 'custnum' => $custnum };
1844 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1845 my $cust_main = qsearchs('cust_main', $search )
1846 or return { 'error' => "unknown custnum $custnum" };
1848 my $pkgnum = $p->{'pkgnum'};
1850 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1851 'pkgnum' => $pkgnum,
1853 or return { 'error' => "unknown pkgnum $pkgnum" };
1855 my $svcpart = $p->{'svcpart'};
1857 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
1858 'svcpart' => $svcpart, } )
1859 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
1860 my $part_svc = $pkg_svc->part_svc;
1862 my $conf = new FS::Conf;
1865 'svc' => $part_svc->svc,
1866 'svcdb' => $part_svc->svcdb,
1867 'pkgnum' => $pkgnum,
1868 'svcpart' => $svcpart,
1869 'custnum' => $custnum,
1871 'security_phrase' => 0, #XXX !
1872 'svc_acct_pop' => [], #XXX !
1874 'init_popstate' => '',
1879 small_custview( $cust_main, $conf->config('countrydefault') ),
1883 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
1884 && $ret->{'svcdb'} eq 'svc_phone') {
1885 $ret->{'svcnum'} = $p->{'svcnum'};
1886 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
1887 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
1888 $ret->{'email'} = $svc_phone->email;
1889 $ret->{'forwarddst'} = $svc_phone->forwarddst;
1896 sub unprovision_svc {
1899 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1900 return { 'error' => $session } if $context eq 'error';
1902 my $search = { 'custnum' => $custnum };
1903 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1904 my $cust_main = qsearchs('cust_main', $search )
1905 or return { 'error' => "unknown custnum $custnum" };
1907 my $svcnum = $p->{'svcnum'};
1909 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
1910 or return { 'error' => "unknown svcnum $svcnum" };
1912 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
1913 unless $cust_svc->cust_pkg->custnum == $custnum;
1915 my $conf = new FS::Conf;
1917 return { 'svc' => $cust_svc->part_svc->svc,
1918 'error' => $cust_svc->cancel,
1920 small_custview( $cust_main, $conf->config('countrydefault') ),
1925 sub myaccount_passwd {
1927 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1928 return { 'error' => $session } if $context eq 'error';
1930 return { 'error' => "New passwords don't match." }
1931 if $p->{'new_password'} ne $p->{'new_password2'};
1933 return { 'error' => 'Enter new password' }
1934 unless length($p->{'new_password'});
1936 #my $search = { 'custnum' => $custnum };
1937 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1938 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1939 my $search = " AND custnum = $1";
1940 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1942 my $svc_acct = qsearchs( {
1943 'table' => 'svc_acct',
1944 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1945 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
1946 'LEFT JOIN cust_main USING ( custnum ) ',
1947 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
1948 'extra_sql' => $search, #important
1950 or return { 'error' => "Service not found" };
1952 $svc_acct->_password($p->{'new_password'});
1953 my $error = $svc_acct->replace();
1955 my($label, $value) = $svc_acct->cust_svc->label;
1957 return { 'error' => $error,
1966 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1967 return { 'error' => $session } if $context eq 'error';
1969 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
1970 FS::TicketSystem->init();
1972 my $conf = new FS::Conf;
1973 my $queue = $p->{'queue'}
1974 || $conf->config('ticket_system-selfservice_queueid')
1975 || $conf->config('ticket_system-default_queueid');
1977 warn "$me create_ticket: creating ticket\n" if $DEBUG;
1978 my $err_or_ticket = FS::TicketSystem->create_ticket(
1979 '', #create RT session based on FS CurrentUser (fs_selfservice)
1981 'custnum' => $custnum,
1982 'svcnum' => $session->{'svcnum'},
1983 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
1986 if ( ref($err_or_ticket) ) {
1987 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
1989 return { 'error' => '',
1990 'ticket_id' => $err_or_ticket->id,
1993 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
1995 return { 'error' => $err_or_ticket };
2003 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2004 return { 'error' => $session } if $context eq 'error';
2006 return { error => 'requested format not implemented' }
2007 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
2009 my $conf = new FS::Conf;
2010 my $age_threshold = 0;
2011 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
2012 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
2014 my $search = { 'custnum' => $custnum };
2015 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2016 my $cust_main = qsearchs('cust_main', $search )
2017 or return { 'error' => "unknown custnum $custnum" };
2019 # does it make more sense to just run one sql query for this instead of all the
2020 # insanity below? would increase performance greately for large data sets?
2022 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
2023 my @part_svc = $cust_pkg->part_svc;
2024 foreach my $part_svc ( @part_svc ) {
2025 if($part_svc->svcdb eq 'svc_phone'){
2026 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
2027 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
2028 push @svc_phone, $cust_pkg_svc->svc_x
2029 if $cust_pkg_svc->date_inserted >= $age_threshold;
2037 my($xls_r,$xls_c) = (0,0);
2040 my @fields = qw( countrycode phonenum pin sip_password phone_name );
2041 if($p->{'format'} eq 'csv') {
2042 $csv = new Text::CSV_XS { 'always_quote' => 1,
2045 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
2046 $content .= $csv->string;
2048 elsif($p->{'format'} eq 'xls') {
2049 my $XLS1 = new IO::Scalar \$content;
2050 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
2051 or return { 'error' => "Error opening .xls file: $!" };
2052 $xls = $xls_workbook->add_worksheet('DIDs');
2053 foreach ( @fields ) {
2054 $xls->write(0,$xls_c++,$_);
2059 foreach my $svc_phone ( @svc_phone ) {
2060 my @cols = map { $svc_phone->$_ } @fields;
2061 if($p->{'format'} eq 'csv') {
2062 return { 'error' => 'Unable to create CSV' }
2063 unless $csv->combine(@cols);
2064 $content .= $csv->string;
2066 elsif($p->{'format'} eq 'xls') {
2069 $xls->write($xls_r,$xls_c++,$_);
2075 $xls_workbook->close() if $p->{'format'} eq 'xls';
2077 { content => $content, format => $p->{'format'}, };
2082 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2083 return { 'error' => $session } if $context eq 'error';
2085 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
2086 FS::TicketSystem->init();
2087 return { 'error' => 'get_ticket configuration error' }
2088 if $FS::TicketSystem::system ne 'RT_Internal';
2090 # check existence and ownership as part of this
2091 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
2092 my $rt_session = FS::TicketSystem->session('');
2093 my $Ticket = FS::TicketSystem->get_ticket_object(
2095 ticket_id => $p->{'ticket_id'},
2098 return { 'error' => 'ticket not found' } if !$Ticket;
2100 if ( length( $p->{'subject'} || '' ) ) {
2102 if ( $p->{'subject'} ne $Ticket->Subject ) {
2103 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
2104 return { 'error' => "unable to set subject: $msg" } if !$val;
2108 if(length($p->{'reply'})) {
2109 my @err_or_res = FS::TicketSystem->correspond_ticket(
2111 'ticket_id' => $p->{'ticket_id'},
2112 'content' => $p->{'reply'},
2115 return { 'error' => 'unable to reply to ticket' }
2116 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
2119 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
2120 my $err_or_ticket = FS::TicketSystem->get_ticket(
2122 'ticket_id' => $p->{'ticket_id'},
2125 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
2126 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
2128 return { 'error' => $err_or_ticket };
2131 my @custs = @{$err_or_ticket->{'custs'}};
2132 my @txns = @{$err_or_ticket->{'txns'}};
2135 # superseded by check in get_ticket_object
2136 #return { 'error' => 'invalid ticket requested' }
2137 #unless grep($_ eq $custnum, @custs);
2139 foreach my $txn ( @txns ) {
2140 push @filtered_txns, $txn
2141 if ($txn->{'type'} eq 'EmailRecord'
2142 || $txn->{'type'} eq 'Correspond'
2143 || $txn->{'type'} eq 'Create');
2146 warn "$me get_ticket: successful: \n"
2148 return { 'error' => '',
2149 'transactions' => \@filtered_txns,
2150 'ticket_fields' => $err_or_ticket->{'fields'},
2151 'ticket_id' => $p->{'ticket_id'},
2155 sub adjust_ticket_priority {
2157 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2158 return { 'error' => $session } if $context eq 'error';
2160 warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
2161 FS::TicketSystem->init;
2162 my $ss_priority = FS::TicketSystem->selfservice_priority;
2164 return { 'error' => 'adjust_ticket_priority configuration error' }
2165 if $FS::TicketSystem::system ne 'RT_Internal'
2168 my $values = $p->{'values'}; #hashref, id => priority value
2171 foreach my $id (keys %$values) {
2172 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
2173 my $Ticket = FS::TicketSystem->get_ticket_object('',
2175 'custnum' => $custnum,
2178 $ticket_error{$id} = 'ticket not found';
2182 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
2183 # We're not going to implement it for RT_External.
2184 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
2185 my $new_value = $values->{$id};
2186 next if $old_value eq $new_value;
2188 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
2190 # AddCustomFieldValue works fine (replacing any existing value) if it's
2191 # a single-valued custom field, which it should be. If it's not, you're
2192 # doing something wrong.
2194 if ( length($new_value) ) {
2195 ($val, $msg) = $Ticket->AddCustomFieldValue(
2196 Field => $ss_priority,
2197 Value => $new_value,
2201 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
2202 Field => $ss_priority,
2203 Value => $old_value,
2207 $ticket_error{$id} = $msg if !$val;
2208 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
2210 return { 'error' => '',
2211 'ticket_error' => \%ticket_error,
2212 %{ customer_info($p) } # send updated customer info back
2218 sub _custoragent_session_custnum {
2221 my($context, $session, $custnum);
2222 if ( $p->{'session_id'} ) {
2224 $context = 'customer';
2225 $session = _cache->get($p->{'session_id'})
2226 or return ( 'error' => "Can't resume session" ); #better error message
2227 $custnum = $session->{'custnum'};
2229 } elsif ( $p->{'agent_session_id'} ) {
2232 my $agent_cache = new FS::ClientAPI_SessionCache( {
2233 'namespace' => 'FS::ClientAPI::Agent',
2235 $session = $agent_cache->get($p->{'agent_session_id'})
2236 or return ( 'error' => "Can't resume session" ); #better error message
2237 $custnum = $p->{'custnum'};
2241 return ( 'error' => "Can't resume session" ); #better error message
2244 ($context, $session, $custnum);