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;
30 use FS::cust_main_county;
33 use FS::acct_rt_transaction;
38 use Spreadsheet::WriteExcel;
41 $me = '[FS::ClientAPI::MyAccount]';
43 use vars qw( @cust_main_editable_fields );
44 @cust_main_editable_fields = qw(
45 first last company address1 address2 city
46 county state zip country daytime night fax
47 ship_first ship_last ship_company ship_address1 ship_address2 ship_city
48 ship_state ship_zip ship_country ship_daytime ship_night ship_fax
49 payby payinfo payname paystart_month paystart_year payissue payip
50 ss paytype paystate stateid stateid_state
54 $cache ||= new FS::ClientAPI_SessionCache( {
55 'namespace' => 'FS::ClientAPI::MyAccount',
62 my($context, $session, $custnum) = _custoragent_session_custnum($p);
63 #return { 'error' => $session } if $context eq 'error';
66 if ( $context eq 'customer' ) {
68 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
71 $sth->execute($custnum) or die $sth->errstr;
73 $agentnum = $sth->fetchrow_arrayref->[0]
74 or die "no agentnum for custnum $custnum";
76 #} elsif ( $context eq 'agent' ) {
77 } elsif ( $p->{'agentnum'} =~ /^(\d+)$/ ) {
81 my $conf = new FS::Conf;
83 #false laziness w/Signup.pm
85 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
87 if ( $skin_info_cache_agent ) {
89 warn "$me loading cached skin info for agentnum $agentnum\n"
94 warn "$me populating skin info cache for agentnum $agentnum\n"
97 $skin_info_cache_agent = {
98 'agentnum' => $agentnum,
99 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
100 qw( company_name ) ),
101 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
102 qw( body_bgcolor box_bgcolor
103 text_color link_color vlink_color hlink_color alink_color
104 font title_color title_align title_size menu_bgcolor menu_fontsize
107 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
108 qw( menu_skipblanks menu_skipheadings menu_nounderline )
110 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
111 qw( title_left_image title_right_image
112 menu_top_image menu_body_image menu_bottom_image
115 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
116 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
117 qw( head body_header body_footer company_address ) ),
120 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
124 #{ %$skin_info_cache_agent };
125 $skin_info_cache_agent;
132 my $conf = new FS::Conf;
136 'phone_login' => $conf->exists('selfservice_server-phone_login'),
137 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
144 #false laziness w/FS::ClientAPI::passwd::passwd
148 my $conf = new FS::Conf;
151 if ( $p->{'domain'} eq 'svc_phone'
152 && $conf->exists('selfservice_server-phone_login') ) {
154 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
155 return { error => 'Number not found.' } unless $svc_phone;
158 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
159 #return { error => 'Only primary user may log in.' }
160 # if $conf->exists('selfservice_server-primary_only')
161 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
163 return { error => 'Incorrect PIN.' }
164 unless $svc_phone->check_pin($p->{'password'});
170 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
171 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
173 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
174 'domsvc' => $svc_domain->svcnum, }
176 return { error => 'User not found.' } unless $svc_acct;
178 if($conf->exists('selfservice_server-login_svcpart')) {
179 my @svcpart = $conf->config('selfservice_server-login_svcpart');
180 my $svcpart = $svc_acct->cust_svc->svcpart;
181 return { error => 'Invalid user.' }
182 unless grep($_ eq $svcpart, @svcpart);
185 return { error => 'Incorrect password.' }
186 unless $svc_acct->check_password($p->{'password'});
193 'svcnum' => $svc_x->svcnum,
196 my $cust_svc = $svc_x->cust_svc;
197 my $cust_pkg = $cust_svc->cust_pkg;
199 my $cust_main = $cust_pkg->cust_main;
200 $session->{'custnum'} = $cust_main->custnum;
201 if ( $conf->exists('pkg-balances') ) {
202 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
203 $cust_main->ncancelled_pkgs;
204 $session->{'pkgnum'} = $cust_pkg->pkgnum
205 if scalar(@cust_pkg) > 1;
209 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
210 #return { error => 'Only primary user may log in.' }
211 # if $conf->exists('selfservice_server-primary_only')
212 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
213 my $part_pkg = $cust_pkg->part_pkg;
214 return { error => 'Only primary user may log in.' }
215 if $conf->exists('selfservice_server-primary_only')
216 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
220 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
221 } until ( ! defined _cache->get($session_id) ); #just in case
223 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
224 _cache->set( $session_id, $session, $timeout );
226 return { 'error' => '',
227 'session_id' => $session_id,
233 if ( $p->{'session_id'} ) {
234 _cache->remove($p->{'session_id'});
235 return { %{ skin_info($p) }, 'error' => '' };
237 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
241 sub payment_gateway {
243 # takes a cust_main and a cust_payby entry, returns the payment_gateway
244 my $conf = new FS::Conf;
245 my $cust_main = shift;
246 my $cust_payby = shift;
247 my $gatewaynum = $conf->config('selfservice-payment_gateway');
249 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
250 die "configured gatewaynum $gatewaynum not found!" if !$pg;
254 return '' if ! FS::payby->realtime($cust_payby);
255 my $pg = $cust_main->agent->payment_gateway(
256 'method' => FS::payby->payby2bop($cust_payby),
266 my $conf = new FS::Conf;
268 my $info = skin_info($p);
270 use vars qw( $cust_paybys ); #cache for performance
271 unless ( $cust_paybys ) {
273 my %cust_paybys = map { $_ => 1 }
274 map { FS::payby->payby2payment($_) }
275 $conf->config('signup_server-payby');
277 $cust_paybys = [ keys %cust_paybys ];
280 $info->{'cust_paybys'} = $cust_paybys;
282 my($context, $session, $custnum) = _custoragent_session_custnum($p);
283 return { 'error' => $session } if $context eq 'error';
285 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
286 or return { 'error' => "unknown custnum $custnum" };
288 $info->{'hide_payment_fields'} = [
290 my $pg = payment_gateway($cust_main, $_);
291 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
292 } @{ $info->{cust_paybys} }
295 $info->{'self_suspend_reason'} =
296 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
299 'custnum' => $custnum,
300 'access_pkgnum' => $session->{'pkgnum'},
301 'access_svcnum' => $session->{'svcnum'},
308 my($context, $session, $custnum) = _custoragent_session_custnum($p);
309 return { 'error' => $session } if $context eq 'error';
313 my $conf = new FS::Conf;
314 if ($conf->exists('cust_main-require_address2')) {
315 $return{'require_address2'} = '1';
317 $return{'require_address2'} = '';
320 if ( $custnum ) { #customer record
322 my $search = { 'custnum' => $custnum };
323 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
324 my $cust_main = qsearchs('cust_main', $search )
325 or return { 'error' => "unknown custnum $custnum" };
327 if ( $session->{'pkgnum'} ) {
328 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
330 $return{balance} = $cust_main->balance;
333 $return{tickets} = [ ($cust_main->tickets) ];
335 unless ( $session->{'pkgnum'} ) {
338 invnum => $_->invnum,
339 date => time2str("%b %o, %Y", $_->_date),
342 } $cust_main->open_cust_bill;
343 $return{open_invoices} = \@open;
346 $return{small_custview} =
347 small_custview( $cust_main,
348 scalar($conf->config('countrydefault')),
349 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
352 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
354 for (@cust_main_editable_fields) {
355 $return{$_} = $cust_main->get($_);
358 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
359 $return{payinfo} = $cust_main->paymask;
360 @return{'month', 'year'} = $cust_main->paydate_monthyear;
363 $return{'invoicing_list'} =
364 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
365 $return{'postal_invoicing'} =
366 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
368 if (scalar($conf->config('support_packages'))) {
369 my @support_services = ();
370 foreach ($cust_main->support_services) {
371 my $seconds = $_->svc_x->seconds;
372 my $time_remaining = (($seconds < 0) ? '-' : '' ).
373 int(abs($seconds)/3600)."h".
374 sprintf("%02d",(abs($seconds)%3600)/60)."m";
375 my $cust_pkg = $_->cust_pkg;
378 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
379 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
380 push @support_services, { svcnum => $_->svcnum,
381 time => $time_remaining,
386 $return{support_services} = \@support_services;
389 if ( $conf->config('prepayment_discounts-credit_type') ) {
391 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
394 } elsif ( $session->{'svcnum'} ) { #no customer record
396 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
397 or die "unknown svcnum";
398 $return{name} = $svc_acct->email;
402 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
406 return { 'error' => '',
407 'custnum' => $custnum,
415 my $session = _cache->get($p->{'session_id'})
416 or return { 'error' => "Can't resume session" }; #better error message
418 my $custnum = $session->{'custnum'}
419 or return { 'error' => "no customer record" };
421 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
422 or return { 'error' => "unknown custnum $custnum" };
424 my $new = new FS::cust_main { $cust_main->hash };
425 $new->set( $_ => $p->{$_} )
426 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
429 if (exists($p->{'payby'})) {
430 $p->{'payby'} =~ /^([A-Z]{4})$/
431 or return { 'error' => "illegal_payby " . $p->{'payby'} };
435 if ( $payby =~ /^(CARD|DCRD)$/ ) {
437 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
439 if ( $new->payinfo eq $cust_main->paymask ) {
440 $new->payinfo($cust_main->payinfo);
442 $new->payinfo($p->{'payinfo'});
445 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
447 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
450 $p->{'payinfo1'} =~ /^([\dx]+)$/
451 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
453 $p->{'payinfo2'} =~ /^([\dx]+)$/
454 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
456 $payinfo = $payinfo1. '@'. $payinfo2;
458 $new->payinfo( ($payinfo eq $cust_main->paymask)
459 ? $cust_main->payinfo
463 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
465 } elsif ( $payby =~ /^(BILL)$/ ) {
467 } elsif ( $payby ) { #notyet ready
468 return { 'error' => "unknown payby $payby" };
472 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
473 #false laziness with httemplate/edit/process/cust_main.cgi
474 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
475 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
477 @invoicing_list = $cust_main->invoicing_list;
480 my $error = $new->replace($cust_main, \@invoicing_list);
481 return { 'error' => $error } if $error;
484 return { 'error' => '' };
489 my $session = _cache->get($p->{'session_id'})
490 or return { 'error' => "Can't resume session" }; #better error message
496 my $conf = new FS::Conf;
497 use vars qw($payment_info); #cache for performance
498 unless ( $payment_info ) {
500 my %states = map { $_->state => 1 }
501 qsearch('cust_main_county', {
502 'country' => $conf->config('countrydefault') || 'US'
505 my %cust_paybys = map { $_ => 1 }
506 map { FS::payby->payby2payment($_) }
507 $conf->config('signup_server-payby');
509 my @cust_paybys = keys %cust_paybys;
513 #list all counties/states/countries
514 'cust_main_county' =>
515 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
517 #shortcut for one-country folks
519 [ sort { $a cmp $b } keys %states ],
521 'card_types' => card_types(),
523 'paytypes' => [ @FS::cust_main::paytypes ],
525 'paybys' => [ $conf->config('signup_server-payby') ],
526 'cust_paybys' => \@cust_paybys,
528 'stateid_label' => FS::Msgcat::_gettext('stateid'),
529 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
531 'show_ss' => $conf->exists('show_ss'),
532 'show_stateid' => $conf->exists('show_stateid'),
533 'show_paystate' => $conf->exists('show_bankstate'),
535 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
537 'credit_card_surcharge_percentage' => $conf->config('credit-card-surcharge-percentage'),
546 my %return = %$payment_info;
548 my $custnum = $session->{'custnum'};
550 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
551 or return { 'error' => "unknown custnum $custnum" };
553 $return{'hide_payment_fields'} = [
555 my $pg = payment_gateway($cust_main, $_);
556 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
557 } @{ $return{cust_paybys} }
560 $return{balance} = $cust_main->balance; #XXX pkg-balances?
562 $return{payname} = $cust_main->payname
563 || ( $cust_main->first. ' '. $cust_main->get('last') );
565 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
567 $return{payby} = $cust_main->payby;
568 $return{stateid_state} = $cust_main->stateid_state;
570 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
571 $return{card_type} = cardtype($cust_main->payinfo);
572 $return{payinfo} = $cust_main->paymask;
574 @return{'month', 'year'} = $cust_main->paydate_monthyear;
578 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
579 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
580 $return{payinfo1} = $payinfo1;
581 $return{payinfo2} = $payinfo2;
582 $return{paytype} = $cust_main->paytype;
583 $return{paystate} = $cust_main->paystate;
587 if ( $conf->config('prepayment_discounts-credit_type') ) {
589 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
592 #doubleclick protection
594 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
596 return { 'error' => '',
602 #some false laziness with httemplate/process/payment.cgi - look there for
603 #ACH and CVV support stuff
604 sub process_payment {
608 my $session = _cache->get($p->{'session_id'})
609 or return { 'error' => "Can't resume session" }; #better error message
613 my $custnum = $session->{'custnum'};
615 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
616 or return { 'error' => "unknown custnum $custnum" };
618 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
619 or return { 'error' => gettext('illegal_amount') };
621 return { error => 'Amount must be greater than 0' } unless $amount > 0;
623 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
624 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
625 my $discount_term = $1;
627 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
628 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
631 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
632 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
635 $p->{'payby'} ||= 'CARD';
636 $p->{'payby'} =~ /^([A-Z]{4})$/
637 or return { 'error' => "illegal_payby " . $p->{'payby'} };
640 #false laziness w/process/payment.cgi
644 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
646 $p->{'payinfo1'} =~ /^([\dx]+)$/
647 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
649 $p->{'payinfo2'} =~ /^([\dx]+)$/
650 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
652 $payinfo = $payinfo1. '@'. $payinfo2;
654 $payinfo = $cust_main->payinfo
655 if $cust_main->paymask eq $payinfo;
657 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
659 $payinfo = $p->{'payinfo'};
661 #more intelligent mathing will be needed here if you change
662 #card_masking_method and don't remove existing paymasks
663 $payinfo = $cust_main->payinfo
664 if $cust_main->paymask eq $payinfo;
667 $payinfo =~ /^(\d{13,16})$/
668 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
672 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
673 return { 'error' => gettext('unknown_card_type') }
674 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
676 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
677 if ( cardtype($payinfo) eq 'American Express card' ) {
678 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
679 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
682 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
683 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
689 die "unknown payby $payby";
693 'CARD' => [ qw( paystart_month paystart_year payissue payip
694 address1 address2 city state zip country ) ],
695 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
698 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
700 'payinfo' => $payinfo,
701 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
702 'payname' => $payname,
703 'paybatch' => $paybatch, #this doesn't actually do anything
705 'paynum_ref' => \$paynum,
706 'pkgnum' => $session->{'pkgnum'},
707 'discount_term' => $discount_term,
709 map { $_ => $p->{$_} } @{ $payby2fields{$payby} }
711 return { 'error' => $error } if $error;
713 $cust_main->apply_payments;
715 if ( $p->{'save'} ) {
716 my $new = new FS::cust_main { $cust_main->hash };
717 if ($payby eq 'CARD' || $payby eq 'DCRD') {
718 $new->set( $_ => $p->{$_} )
719 foreach qw( payname paystart_month paystart_year payissue payip
720 address1 address2 city state zip country );
721 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
722 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
723 $new->set( $_ => $p->{$_} )
724 foreach qw( payname payip paytype paystate
725 stateid stateid_state );
726 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
728 $new->set( 'payinfo' => $cust_main->card_token || $payinfo );
729 $new->set( 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01' );
730 my $error = $new->replace($cust_main);
732 #no, this causes customers to process their payments again
733 #return { 'error' => $error };
734 #XXX just warn verosely for now so i can figure out how these happen in
735 # the first place, eventually should redirect them to the "change
736 #address" page but indicate the payment did process??
737 delete($p->{'payinfo'}); #don't want to log this!
738 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
739 "NEW: ". Dumper($new)."\n".
740 "OLD: ". Dumper($cust_main)."\n".
741 "PACKET: ". Dumper($p)."\n";
748 my $receipt_html = '';
750 # currently supported for realtime CC only; send receipt data to SS
751 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
754 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
757 <TD ALIGN="right">Payment#</TD>
758 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
762 <TD ALIGN="right">Date</TD>
764 <TD BGCOLOR="#FFFFFF"><B>! .
765 time2str("%a %b %o, %Y %r", $cust_pay->_date)
771 <TD ALIGN="right">Amount</TD>
772 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paid . qq!</B></TD>
777 <TD ALIGN="right">Payment method</TD>
778 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
787 return { 'error' => '', 'receipt_html' => $receipt_html, };
791 sub realtime_collect {
794 my $session = _cache->get($p->{'session_id'})
795 or return { 'error' => "Can't resume session" }; #better error message
797 my $custnum = $session->{'custnum'};
799 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
800 or return { 'error' => "unknown custnum $custnum" };
803 if ( $p->{'amount'} ) {
804 $amount = $p->{'amount'};
806 elsif ( $session->{'pkgnum'} ) {
807 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
810 $amount = $cust_main->balance;
813 my $error = $cust_main->realtime_collect(
814 'method' => $p->{'method'},
816 'pkgnum' => $session->{'pkgnum'},
817 'session_id' => $p->{'session_id'},
821 return { 'error' => $error } unless ref( $error );
823 return { 'error' => '', amount => $amount, %$error };
826 sub process_payment_order_pkg {
829 my $hr = process_payment($p);
830 return $hr if $hr->{'error'};
835 sub process_payment_order_renew {
838 my $hr = process_payment($p);
839 return $hr if $hr->{'error'};
848 my $session = _cache->get($p->{'session_id'})
849 or return { 'error' => "Can't resume session" }; #better error message
853 my $custnum = $session->{'custnum'};
855 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
856 or return { 'error' => "unknown custnum $custnum" };
858 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
859 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
867 return { 'error' => $error } if $error;
869 return { 'error' => '',
871 'seconds' => $seconds,
872 'duration' => duration_exact($seconds),
873 'upbytes' => $upbytes,
874 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
875 'downbytes' => $downbytes,
876 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
877 'totalbytes'=> $totalbytes,
878 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
885 my $session = _cache->get($p->{'session_id'})
886 or return { 'error' => "Can't resume session" }; #better error message
888 my $custnum = $session->{'custnum'};
890 my $invnum = $p->{'invnum'};
892 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
893 'custnum' => $custnum } )
894 or return { 'error' => "Can't find invnum" };
898 return { 'error' => '',
900 'invoice_text' => join('', $cust_bill->print_text ),
901 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
909 #sessioning for this? how do we get the session id to the backend invoice
910 # template so it can add it to the link, blah
913 if ( $p->{'invnum'} ) {
914 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
915 or return { 'error' => 'unknown invnum' };
916 $agentnum = $cust_bill->cust_main->agentnum;
919 my $templatename = $p->{'template'} || $p->{'templatename'};
921 #false laziness-ish w/view/cust_bill-logo.cgi
923 my $conf = new FS::Conf;
924 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
925 $templatename = "_$1";
930 my $filename = "logo$templatename.png";
932 return { 'error' => '',
933 'logo' => $conf->config_binary($filename, $agentnum),
934 'content_type' => 'image/png', #should allow gif, jpg too
941 my $session = _cache->get($p->{'session_id'})
942 or return { 'error' => "Can't resume session" }; #better error message
944 my $custnum = $session->{'custnum'};
946 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
947 or return { 'error' => "unknown custnum $custnum" };
949 my @cust_bill = $cust_main->cust_bill;
951 return { 'error' => '',
952 'invoices' => [ map { { 'invnum' => $_->invnum,
953 '_date' => $_->_date,
954 'date' => time2str("%b %o, %Y", $_->_date),
963 my $session = _cache->get($p->{'session_id'})
964 or return { 'error' => "Can't resume session" }; #better error message
966 my $custnum = $session->{'custnum'};
968 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
969 or return { 'error' => "unknown custnum $custnum" };
971 my @errors = $cust_main->cancel( 'quiet'=>1 );
973 my $error = scalar(@errors) ? join(' / ', @errors) : '';
975 return { 'error' => $error };
982 my($context, $session, $custnum) = _custoragent_session_custnum($p);
983 return { 'error' => $session } if $context eq 'error';
985 my $search = { 'custnum' => $custnum };
986 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
987 my $cust_main = qsearchs('cust_main', $search )
988 or return { 'error' => "unknown custnum $custnum" };
990 my $conf = new FS::Conf;
992 # the duplication below is necessary:
993 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
994 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
995 # non-backwards-compatible change breaking the software of anyone using the API
996 # instead of the stock selfservice
997 # 2. to return cancelled packages as well - for wholesale and non-wholesale
998 if( $conf->exists('selfservice_server-view-wholesale') ) {
999 return { 'svcnum' => $session->{'svcnum'},
1000 'custnum' => $custnum,
1001 'cust_pkg' => [ map {
1003 part_pkg => [ map $_->hashref, $_->part_pkg ],
1005 [ map $_->hashref, $_->available_part_svc ],
1007 [ map { my $ref = { $_->hash,
1008 label => [ $_->label ],
1010 $ref->{_password} = $_->svc_x->_password
1011 if $context eq 'agent'
1012 && $conf->exists('agent-showpasswords')
1013 && $_->part_svc->svcdb eq 'svc_acct';
1018 } $cust_main->cust_pkg
1021 small_custview( $cust_main, $conf->config('countrydefault') ),
1022 'wholesale_view' => 1,
1023 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1024 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1025 'lnp' => $conf->exists('svc_phone-lnp'),
1029 { 'svcnum' => $session->{'svcnum'},
1030 'custnum' => $custnum,
1031 'cust_pkg' => [ map {
1035 [ map $_->hashref, $_->available_part_svc ],
1037 [ map { my $ref = { $_->hash,
1038 label => [ $_->label ],
1040 $ref->{_password} = $_->svc_x->_password
1041 if $context eq 'agent'
1042 && $conf->exists('agent-showpasswords')
1043 && $_->part_svc->svcdb eq 'svc_acct';
1048 } $cust_main->ncancelled_pkgs
1051 small_custview( $cust_main, $conf->config('countrydefault') ),
1059 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1060 return { 'error' => $session } if $context eq 'error';
1062 my $search = { 'custnum' => $custnum };
1063 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1064 my $cust_main = qsearchs('cust_main', $search )
1065 or return { 'error' => "unknown custnum $custnum" };
1068 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1069 foreach my $cust_pkg ( $p->{'ncancelled'}
1070 ? $cust_main->ncancelled_pkgs
1071 : $cust_main->unsuspended_pkgs ) {
1072 next if $session->{'pkgnum'} && $cust_pkg->pkgnum != $session->{'pkgnum'};
1073 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1075 if ( $p->{'svcdb'} ) {
1076 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1078 : ref($p->{'svcdb'}) eq 'ARRAY'
1079 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1080 : { $p->{'svcdb'} => 1 };
1081 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1084 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1087 my $conf = new FS::Conf;
1090 'svcnum' => $session->{'svcnum'},
1091 'custnum' => $custnum,
1092 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1093 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1096 my $svc_x = $_->svc_x;
1097 my($label, $value) = $_->label;
1098 my $svcdb = $_->part_svc->svcdb;
1099 my $part_pkg = $_->cust_pkg->part_pkg;
1102 'svcnum' => $_->svcnum,
1108 if ( $svcdb eq 'svc_acct' ) {
1111 'username' => $svc_x->username,
1112 'email' => $svc_x->email,
1113 'seconds' => $svc_x->seconds,
1114 'upbytes' => display_bytecount($svc_x->upbytes),
1115 'downbytes' => display_bytecount($svc_x->downbytes),
1116 'totalbytes' => display_bytecount($svc_x->totalbytes),
1118 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1119 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1120 'recharge_upbytes' =>
1121 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1122 'recharge_downbytes' =>
1123 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1124 'recharge_totalbytes' =>
1125 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1129 } elsif ( $svcdb eq 'svc_phone' || $svcdb eq 'svc_port' ) {
1145 _usage_details( \&_port_graph, $p,
1146 'svcdb' => 'svc_port',
1151 my($svc_port, $begin, $end) = @_;
1153 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
1154 push @usage, { 'png' => $pngOrError };
1158 sub _list_svc_usage {
1159 my($svc_acct, $begin, $end) = @_;
1161 foreach my $part_export (
1162 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1163 qw( sqlradius sqlradius_withdomain )
1165 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1170 sub list_svc_usage {
1171 _usage_details(\&_list_svc_usage, @_);
1174 sub _list_support_usage {
1175 my($svc_acct, $begin, $end) = @_;
1177 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1178 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1180 push @usage, { 'seconds' => $_->seconds,
1181 'support' => $_->support,
1182 '_date' => $_->_date,
1183 'id' => $_->transaction_id,
1184 'creator' => $_->creator,
1185 'subject' => $_->subject,
1186 'status' => $_->status,
1187 'ticketid' => $_->ticketid,
1193 sub list_support_usage {
1194 _usage_details(\&_list_support_usage, @_);
1197 sub _list_cdr_usage {
1198 my($svc_phone, $begin, $end) = @_;
1199 map [ $_->downstream_csv('format' => 'default', 'keeparray' => 1) ], #XXX config for format
1200 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1203 sub list_cdr_usage {
1205 _usage_details( \&_list_cdr_usage, $p,
1206 'svcdb' => 'svc_phone',
1210 sub _usage_details {
1211 my($callback, $p, %opt) = @_;
1213 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1214 return { 'error' => $session } if $context eq 'error';
1216 my $search = { 'svcnum' => $p->{'svcnum'} };
1217 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1219 my $svcdb = $opt{'svcdb'} || 'svc_acct';
1221 my $svc_x = qsearchs( $svcdb, $search );
1222 return { 'error' => 'No service selected in list_svc_usage' }
1225 my $header = $svcdb eq 'svc_phone'
1226 ? [ split(',', FS::cdr::invoice_header('default') ) ] #XXX
1229 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
1230 my $freq = $cust_pkg->part_pkg->freq;
1231 my $start = $cust_pkg->setup;
1232 #my $end = $cust_pkg->bill; # or time?
1235 unless ( $p->{beginning} ) {
1236 $p->{beginning} = $cust_pkg->last_bill;
1237 $p->{ending} = $end;
1240 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending});
1242 #kinda false laziness with FS::cust_main::bill, but perhaps
1243 #we should really change this bit to DateTime and DateTime::Duration
1245 #change this bit to use Date::Manip? CAREFUL with timezones (see
1246 # mailing list archive)
1247 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
1248 (localtime($p->{ending}) )[0,1,2,3,4,5];
1249 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
1250 (localtime($p->{beginning}) )[0,1,2,3,4,5];
1252 if ( $freq =~ /^\d+$/ ) {
1254 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
1256 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
1257 } elsif ( $freq =~ /^(\d+)w$/ ) {
1259 $nmday += $weeks * 7;
1260 $pmday -= $weeks * 7;
1261 } elsif ( $freq =~ /^(\d+)d$/ ) {
1265 } elsif ( $freq =~ /^(\d+)h$/ ) {
1270 return { 'error' => "unparsable frequency: ". $freq };
1273 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
1274 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
1278 'svcnum' => $p->{svcnum},
1279 'beginning' => $p->{beginning},
1280 'ending' => $p->{ending},
1281 'previous' => ($previous > $start) ? $previous : $start,
1282 'next' => ($next < $end) ? $next : $end,
1283 'header' => $header,
1291 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1292 return { 'error' => $session } if $context eq 'error';
1294 my $search = { 'custnum' => $custnum };
1295 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1296 my $cust_main = qsearchs('cust_main', $search )
1297 or return { 'error' => "unknown custnum $custnum" };
1299 my $status = $cust_main->status;
1300 #false laziness w/ClientAPI/Signup.pm
1302 my $cust_pkg = new FS::cust_pkg ( {
1303 'custnum' => $custnum,
1304 'pkgpart' => $p->{'pkgpart'},
1306 my $error = $cust_pkg->check;
1307 return { 'error' => $error } if $error;
1310 unless ( $p->{'svcpart'} eq 'none' ) {
1314 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
1316 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1317 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
1318 $svcdb = $part_svc->svcdb;
1320 $svcdb = 'svc_acct';
1322 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
1325 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
1326 'svc_domain' => [ qw( domain ) ],
1327 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
1328 'svc_external' => [ qw( id title ) ],
1329 'svc_pbx' => [ qw( id name ) ],
1332 my $svc_x = "FS::$svcdb"->new( {
1333 'svcpart' => $svcpart,
1334 map { $_ => $p->{$_} } @{$fields{$svcdb}}
1337 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
1340 while ( length($p->{"snarf_machine$snarfnum"}) ) {
1341 my $acct_snarf = new FS::acct_snarf ( {
1342 'machine' => $p->{"snarf_machine$snarfnum"},
1343 'protocol' => $p->{"snarf_protocol$snarfnum"},
1344 'username' => $p->{"snarf_username$snarfnum"},
1345 '_password' => $p->{"snarf_password$snarfnum"},
1348 push @acct_snarf, $acct_snarf;
1350 $svc_x->child_objects( \@acct_snarf );
1353 my $y = $svc_x->setdefault; # arguably should be in new method
1354 return { 'error' => $y } if $y && !ref($y);
1356 $error = $svc_x->check;
1357 return { 'error' => $error } if $error;
1364 tie my %hash, 'Tie::RefHash';
1365 %hash = ( $cust_pkg => \@svc );
1367 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
1368 return { 'error' => $error } if $error;
1370 my $conf = new FS::Conf;
1371 if ( $conf->exists('signup_server-realtime') ) {
1373 my $bill_error = _do_bop_realtime( $cust_main, $status );
1376 $cust_pkg->cancel('quiet'=>1);
1379 $cust_pkg->reexport;
1383 $cust_pkg->reexport;
1386 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
1388 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
1395 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1396 return { 'error' => $session } if $context eq 'error';
1398 my $search = { 'custnum' => $custnum };
1399 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1400 my $cust_main = qsearchs('cust_main', $search )
1401 or return { 'error' => "unknown custnum $custnum" };
1403 my $status = $cust_main->status;
1404 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
1405 or return { 'error' => "unknown package $p->{pkgnum}" };
1408 my $error = FS::cust_pkg::order( $custnum,
1414 my $conf = new FS::Conf;
1415 if ( $conf->exists('signup_server-realtime') ) {
1417 my $bill_error = _do_bop_realtime( $cust_main, $status );
1420 $newpkg[0]->suspend;
1423 $newpkg[0]->reexport;
1427 $newpkg[0]->reexport;
1430 return { error => '', pkgnum => $cust_pkg->pkgnum };
1434 sub order_recharge {
1437 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1438 return { 'error' => $session } if $context eq 'error';
1440 my $search = { 'custnum' => $custnum };
1441 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1442 my $cust_main = qsearchs('cust_main', $search )
1443 or return { 'error' => "unknown custnum $custnum" };
1445 my $status = $cust_main->status;
1446 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
1447 or return { 'error' => "unknown service " . $p->{'svcnum'} };
1449 my $svc_x = $cust_svc->svc_x;
1450 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
1453 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
1454 qw ( recharge_seconds recharge_upbytes recharge_downbytes
1455 recharge_totalbytes );
1456 my $amount = $part_pkg->option('recharge_amount', 1);
1458 my ($l, $v, $d) = $cust_svc->label; # blah
1459 my $pkg = "Recharge $v";
1461 my $bill_error = $cust_main->charge($amount, $pkg,
1462 "time: $vhash{seconds}, up: $vhash{upbytes}," .
1463 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
1464 $part_pkg->taxclass); #meh
1466 my $conf = new FS::Conf;
1467 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
1469 $bill_error = _do_bop_realtime( $cust_main, $status );
1474 my $error = $svc_x->recharge (\%vhash);
1475 return { 'error' => $error } if $error;
1479 my $error = $bill_error;
1480 $error ||= $svc_x->recharge (\%vhash);
1481 return { 'error' => $error } if $error;
1484 return { error => '', svc => $cust_svc->part_svc->svc };
1488 sub _do_bop_realtime {
1489 my ($cust_main, $status) = (shift, shift);
1491 my $old_balance = $cust_main->balance;
1493 my $bill_error = $cust_main->bill
1494 || $cust_main->apply_payments_and_credits
1495 || $cust_main->realtime_collect('selfservice' => 1);
1497 if ( $cust_main->balance > $old_balance
1498 && $cust_main->balance > 0
1499 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
1500 1 : $status eq 'suspended' ) ) {
1501 #this makes sense. credit is "un-doing" the invoice
1502 my $conf = new FS::Conf;
1503 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
1504 'self-service decline',
1505 'reason_type' => $conf->config('signup_credit_type'),
1507 $cust_main->apply_credits( 'order' => 'newest' );
1509 return { 'error' => '_decline', 'bill_error' => $bill_error };
1518 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1519 return { 'error' => $session } if $context eq 'error';
1521 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1522 or return { 'error' => "unknown custnum $custnum" };
1524 my @cust_pkg = sort { $a->bill <=> $b->bill }
1525 grep { $_->part_pkg->freq ne '0' }
1526 $cust_main->ncancelled_pkgs;
1528 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
1530 my $total = $cust_main->balance;
1533 my $bill = $_->bill;
1534 $total += $_->part_pkg->base_recur($_, \$bill);
1535 my $renew_date = $_->part_pkg->add_freq($_->bill);
1537 'pkgnum' => $_->pkgnum,
1538 'amount' => sprintf('%.2f', $total),
1539 'bill_date' => $_->bill,
1540 'bill_date_pretty' => time2str('%x', $_->bill),
1541 'renew_date' => $renew_date,
1542 'renew_date_pretty' => time2str('%x', $renew_date),
1543 'expire_date' => $_->expire,
1544 'expire_date_pretty' => time2str('%x', $_->expire),
1549 return { 'dates' => \@array };
1553 sub payment_info_renew_info {
1555 my $renew_info = renew_info($p);
1556 my $payment_info = payment_info($p);
1557 return { %$renew_info,
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 $date = $p->{'date'};
1575 #freeside-daily -n -d $date fs_daily $custnum
1576 $cust_main->bill_and_collect( 'time' => $date,
1577 'invoice_time' => $now,
1578 'actual_time' => $now,
1579 'check_freq' => '1d',
1582 return { 'error' => '' };
1588 my $session = _cache->get($p->{'session_id'})
1589 or return { 'error' => "Can't resume session" }; #better error message
1591 my $custnum = $session->{'custnum'};
1593 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1594 or return { 'error' => "unknown custnum $custnum" };
1596 my $conf = new FS::Conf;
1598 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
1599 or return { 'error' => 'Permission denied' };
1601 my $pkgnum = $p->{'pkgnum'};
1603 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1604 'pkgnum' => $pkgnum, } )
1605 or return { 'error' => "unknown pkgnum $pkgnum" };
1607 my $error = $cust_pkg->suspend(reason => $reasonnum);
1608 return { 'error' => $error };
1614 my $session = _cache->get($p->{'session_id'})
1615 or return { 'error' => "Can't resume session" }; #better error message
1617 my $custnum = $session->{'custnum'};
1619 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1620 or return { 'error' => "unknown custnum $custnum" };
1622 my $pkgnum = $p->{'pkgnum'};
1624 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1625 'pkgnum' => $pkgnum, } )
1626 or return { 'error' => "unknown pkgnum $pkgnum" };
1628 my $error = $cust_pkg->cancel('quiet' => 1);
1629 return { 'error' => $error };
1633 sub provision_phone {
1636 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
1639 unless($p->{'lnp'}) {
1640 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
1641 $p->{'lnp_status'} = "portingin";
1642 return _provision( 'FS::svc_phone',
1643 [qw(lnp_desired_due_date lnp_other_provider
1644 lnp_other_provider_account phonenum countrycode lnp_status)],
1645 [qw(phonenum countrycode)],
1652 unless (scalar(@bulkdid)) {
1653 return _provision( 'FS::svc_phone',
1654 [qw(phonenum countrycode)],
1655 [qw(phonenum countrycode)],
1661 # bulk DID order case
1663 foreach my $did ( @bulkdid ) {
1664 $did =~ s/[^0-9]//g;
1665 $error = _provision( 'FS::svc_phone',
1666 [qw(phonenum countrycode)],
1667 [qw(phonenum countrycode)],
1669 'pkgnum' => $p->{'pkgnum'},
1670 'svcpart' => $p->{'svcpart'},
1672 'countrycode' => $p->{'countrycode'},
1673 'session_id' => $p->{'session_id'},
1676 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
1678 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
1681 sub provision_acct {
1683 warn "provision_acct called\n"
1686 return { 'error' => gettext('passwords_dont_match') }
1687 if $p->{'_password'} ne $p->{'_password2'};
1688 return { 'error' => gettext('empty_password') }
1689 unless length($p->{'_password'});
1691 if ($p->{'domsvc'}) {
1692 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
1693 qw ( svcpart pkgnum ) );
1694 return { 'error' => gettext('invalid_domain') }
1695 unless ($domains{$p->{'domsvc'}});
1698 warn "provision_acct calling _provision\n"
1700 _provision( 'FS::svc_acct',
1701 [qw(username _password domsvc)],
1702 [qw(username _password domsvc)],
1708 sub provision_external {
1710 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
1711 _provision( 'FS::svc_external',
1720 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
1721 warn "_provision called for $class\n"
1724 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1725 return { 'error' => $session } if $context eq 'error';
1727 my $search = { 'custnum' => $custnum };
1728 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1729 my $cust_main = qsearchs('cust_main', $search )
1730 or return { 'error' => "unknown custnum $custnum" };
1732 my $pkgnum = $p->{'pkgnum'};
1734 warn "searching for custnum $custnum pkgnum $pkgnum\n"
1736 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1737 'pkgnum' => $pkgnum,
1739 or return { 'error' => "unknown pkgnum $pkgnum" };
1741 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
1743 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
1744 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
1746 warn "creating $class record\n"
1748 my $svc_x = $class->new( {
1749 'pkgnum' => $p->{'pkgnum'},
1750 'svcpart' => $p->{'svcpart'},
1751 map { $_ => $p->{$_} } @$fields
1753 warn "inserting $class record\n"
1755 my $error = $svc_x->insert;
1758 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
1760 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
1763 my $return = { 'svc' => $part_svc->svc,
1765 map { $_ => $svc_x->get($_) } @$return_fields
1767 warn "_provision returning ". Dumper($return). "\n"
1776 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1777 return { 'error' => $session } if $context eq 'error';
1779 my $search = { 'custnum' => $custnum };
1780 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1781 my $cust_main = qsearchs('cust_main', $search )
1782 or return { 'error' => "unknown custnum $custnum" };
1784 my $pkgnum = $p->{'pkgnum'};
1786 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1787 'pkgnum' => $pkgnum,
1789 or return { 'error' => "unknown pkgnum $pkgnum" };
1791 my $svcpart = $p->{'svcpart'};
1793 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
1794 'svcpart' => $svcpart, } )
1795 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
1796 my $part_svc = $pkg_svc->part_svc;
1798 my $conf = new FS::Conf;
1801 'svc' => $part_svc->svc,
1802 'svcdb' => $part_svc->svcdb,
1803 'pkgnum' => $pkgnum,
1804 'svcpart' => $svcpart,
1805 'custnum' => $custnum,
1807 'security_phrase' => 0, #XXX !
1808 'svc_acct_pop' => [], #XXX !
1810 'init_popstate' => '',
1815 small_custview( $cust_main, $conf->config('countrydefault') ),
1821 sub unprovision_svc {
1824 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1825 return { 'error' => $session } if $context eq 'error';
1827 my $search = { 'custnum' => $custnum };
1828 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1829 my $cust_main = qsearchs('cust_main', $search )
1830 or return { 'error' => "unknown custnum $custnum" };
1832 my $svcnum = $p->{'svcnum'};
1834 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
1835 or return { 'error' => "unknown svcnum $svcnum" };
1837 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
1838 unless $cust_svc->cust_pkg->custnum == $custnum;
1840 my $conf = new FS::Conf;
1842 return { 'svc' => $cust_svc->part_svc->svc,
1843 'error' => $cust_svc->cancel,
1845 small_custview( $cust_main, $conf->config('countrydefault') ),
1850 sub myaccount_passwd {
1852 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1853 return { 'error' => $session } if $context eq 'error';
1855 return { 'error' => "New passwords don't match." }
1856 if $p->{'new_password'} ne $p->{'new_password2'};
1858 return { 'error' => 'Enter new password' }
1859 unless length($p->{'new_password'});
1861 #my $search = { 'custnum' => $custnum };
1862 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1863 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1864 my $search = " AND custnum = $1";
1865 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1867 my $svc_acct = qsearchs( {
1868 'table' => 'svc_acct',
1869 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1870 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
1871 'LEFT JOIN cust_main USING ( custnum ) ',
1872 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
1873 'extra_sql' => $search, #important
1875 or return { 'error' => "Service not found" };
1877 $svc_acct->_password($p->{'new_password'});
1878 my $error = $svc_acct->replace();
1880 my($label, $value) = $svc_acct->cust_svc->label;
1882 return { 'error' => $error,
1891 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1892 return { 'error' => $session } if $context eq 'error';
1894 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
1895 FS::TicketSystem->init();
1897 my $conf = new FS::Conf;
1898 my $queue = $p->{'queue'}
1899 || $conf->config('ticket_system-selfservice_queueid')
1900 || $conf->config('ticket_system-default_queueid');
1902 warn "$me create_ticket: creating ticket\n" if $DEBUG;
1903 my $err_or_ticket = FS::TicketSystem->create_ticket(
1904 '', #create RT session based on FS CurrentUser (fs_selfservice)
1906 'custnum' => $custnum,
1907 'svcnum' => $session->{'svcnum'},
1908 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
1911 if ( ref($err_or_ticket) ) {
1912 warn "$me create_ticket: sucessful: ". $err_or_ticket->id. "\n"
1914 return { 'error' => '',
1915 'ticket_id' => $err_or_ticket->id,
1918 warn "$me create_ticket: unsucessful: $err_or_ticket\n"
1920 return { 'error' => $err_or_ticket };
1928 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1929 return { 'error' => $session } if $context eq 'error';
1931 return { error => 'requested format not implemented' }
1932 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
1934 my $conf = new FS::Conf;
1935 my $age_threshold = 0;
1936 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
1937 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
1939 my $search = { 'custnum' => $custnum };
1940 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1941 my $cust_main = qsearchs('cust_main', $search )
1942 or return { 'error' => "unknown custnum $custnum" };
1944 # does it make more sense to just run one sql query for this instead of all the
1945 # insanity below? would increase performance greately for large data sets?
1947 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1948 my @part_svc = $cust_pkg->part_svc;
1949 foreach my $part_svc ( @part_svc ) {
1950 if($part_svc->svcdb eq 'svc_phone'){
1951 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
1952 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
1953 push @svc_phone, $cust_pkg_svc->svc_x
1954 if $cust_pkg_svc->date_inserted >= $age_threshold;
1962 my($xls_r,$xls_c) = (0,0);
1965 my @fields = qw( countrycode phonenum pin sip_password phone_name );
1966 if($p->{'format'} eq 'csv') {
1967 $csv = new Text::CSV_XS { 'always_quote' => 1,
1970 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
1971 $content .= $csv->string;
1973 elsif($p->{'format'} eq 'xls') {
1974 my $XLS1 = new IO::Scalar \$content;
1975 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
1976 or return { 'error' => "Error opening .xls file: $!" };
1977 $xls = $xls_workbook->add_worksheet('DIDs');
1978 foreach ( @fields ) {
1979 $xls->write(0,$xls_c++,$_);
1984 foreach my $svc_phone ( @svc_phone ) {
1985 my @cols = map { $svc_phone->$_ } @fields;
1986 if($p->{'format'} eq 'csv') {
1987 return { 'error' => 'Unable to create CSV' }
1988 unless $csv->combine(@cols);
1989 $content .= $csv->string;
1991 elsif($p->{'format'} eq 'xls') {
1994 $xls->write($xls_r,$xls_c++,$_);
2000 $xls_workbook->close() if $p->{'format'} eq 'xls';
2002 { content => $content, format => $p->{'format'}, };
2007 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2008 return { 'error' => $session } if $context eq 'error';
2010 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
2011 FS::TicketSystem->init();
2013 if(length($p->{'reply'})) {
2014 # currently this allows anyone to correspond on any ticket as fs_selfservice
2016 my @err_or_res = FS::TicketSystem->correspond_ticket(
2017 '', #create RT session based on FS CurrentUser (fs_selfservice)
2018 'ticket_id' => $p->{'ticket_id'},
2019 'content' => $p->{'reply'},
2022 return { 'error' => 'unable to reply to ticket' }
2023 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
2026 warn "$me get_ticket: getting ticket\n" if $DEBUG;
2027 my $err_or_ticket = FS::TicketSystem->get_ticket(
2028 '', #create RT session based on FS CurrentUser (fs_selfservice)
2029 'ticket_id' => $p->{'ticket_id'},
2032 if ( ref($err_or_ticket) ) {
2034 # since we're bypassing the RT security/permissions model by always using
2035 # fs_selfservice as the RT user (as opposed to a requestor, which we
2036 # can't do since we want all tickets linked to a cust), we check below whether
2037 # the requested ticket was actually linked to this customer
2038 my @custs = @{$err_or_ticket->{'custs'}};
2039 my @txns = @{$err_or_ticket->{'txns'}};
2042 return { 'error' => 'no customer' } unless ( $custnum && scalar(@custs) );
2044 return { 'error' => 'invalid ticket requested' }
2045 unless grep($_ eq $custnum, @custs);
2047 foreach my $txn ( @txns ) {
2048 push @filtered_txns, $txn
2049 if ($txn->{'type'} eq 'EmailRecord'
2050 || $txn->{'type'} eq 'Correspond'
2051 || $txn->{'type'} eq 'Create');
2054 warn "$me get_ticket: sucessful: \n"
2056 return { 'error' => '',
2057 'transactions' => \@filtered_txns,
2058 'ticket_id' => $p->{'ticket_id'},
2061 warn "$me create_ticket: unsucessful: $err_or_ticket\n"
2063 return { 'error' => $err_or_ticket };
2070 sub _custoragent_session_custnum {
2073 my($context, $session, $custnum);
2074 if ( $p->{'session_id'} ) {
2076 $context = 'customer';
2077 $session = _cache->get($p->{'session_id'})
2078 or return ( 'error' => "Can't resume session" ); #better error message
2079 $custnum = $session->{'custnum'};
2081 } elsif ( $p->{'agent_session_id'} ) {
2084 my $agent_cache = new FS::ClientAPI_SessionCache( {
2085 'namespace' => 'FS::ClientAPI::Agent',
2087 $session = $agent_cache->get($p->{'agent_session_id'})
2088 or return ( 'error' => "Can't resume session" ); #better error message
2089 $custnum = $p->{'custnum'};
2093 return ( 'error' => "Can't resume session" ); #better error message
2096 ($context, $session, $custnum);