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::ClientAPI_SessionCache;
29 use FS::cust_main_county;
32 use FS::acct_rt_transaction;
37 use Spreadsheet::WriteExcel;
40 $me = '[FS::ClientAPI::MyAccount]';
42 use vars qw( @cust_main_editable_fields );
43 @cust_main_editable_fields = qw(
44 first last company address1 address2 city
45 county state zip country daytime night fax
46 ship_first ship_last ship_company ship_address1 ship_address2 ship_city
47 ship_state ship_zip ship_country ship_daytime ship_night ship_fax
48 payby payinfo payname paystart_month paystart_year payissue payip
49 ss paytype paystate stateid stateid_state
53 $cache ||= new FS::ClientAPI_SessionCache( {
54 'namespace' => 'FS::ClientAPI::MyAccount',
61 my($context, $session, $custnum) = _custoragent_session_custnum($p);
62 #return { 'error' => $session } if $context eq 'error';
65 if ( $context eq 'customer' ) {
67 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
70 $sth->execute($custnum) or die $sth->errstr;
72 $agentnum = $sth->fetchrow_arrayref->[0]
73 or die "no agentnum for custnum $custnum";
75 #} elsif ( $context eq 'agent' ) {
76 } elsif ( $p->{'agentnum'} =~ /^(\d+)$/ ) {
80 my $conf = new FS::Conf;
82 #false laziness w/Signup.pm
84 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
86 if ( $skin_info_cache_agent ) {
88 warn "$me loading cached skin info for agentnum $agentnum\n"
93 warn "$me populating skin info cache for agentnum $agentnum\n"
96 $skin_info_cache_agent = {
97 'agentnum' => $agentnum,
98 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
100 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
101 qw( body_bgcolor box_bgcolor
102 text_color link_color vlink_color hlink_color alink_color
103 font title_color title_align title_size menu_bgcolor menu_fontsize
106 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
107 qw( menu_skipblanks menu_skipheadings menu_nounderline )
109 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
110 qw( title_left_image title_right_image
111 menu_top_image menu_body_image menu_bottom_image
114 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
115 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
116 qw( head body_header body_footer company_address ) ),
119 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
123 #{ %$skin_info_cache_agent };
124 $skin_info_cache_agent;
131 my $conf = new FS::Conf;
135 'phone_login' => $conf->exists('selfservice_server-phone_login'),
136 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
143 #false laziness w/FS::ClientAPI::passwd::passwd
147 my $conf = new FS::Conf;
150 if ( $p->{'domain'} eq 'svc_phone'
151 && $conf->exists('selfservice_server-phone_login') ) {
153 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
154 return { error => 'Number not found.' } unless $svc_phone;
157 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
158 #return { error => 'Only primary user may log in.' }
159 # if $conf->exists('selfservice_server-primary_only')
160 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
162 return { error => 'Incorrect PIN.' }
163 unless $svc_phone->check_pin($p->{'password'});
169 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
170 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
172 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
173 'domsvc' => $svc_domain->svcnum, }
175 return { error => 'User not found.' } unless $svc_acct;
177 if($conf->exists('selfservice_server-login_svcpart')) {
178 my @svcpart = $conf->config('selfservice_server-login_svcpart');
179 my $svcpart = $svc_acct->cust_svc->svcpart;
180 return { error => 'Invalid user.' }
181 unless grep($_ eq $svcpart, @svcpart);
184 return { error => 'Incorrect password.' }
185 unless $svc_acct->check_password($p->{'password'});
192 'svcnum' => $svc_x->svcnum,
195 my $cust_svc = $svc_x->cust_svc;
196 my $cust_pkg = $cust_svc->cust_pkg;
198 my $cust_main = $cust_pkg->cust_main;
199 $session->{'custnum'} = $cust_main->custnum;
200 if ( $conf->exists('pkg-balances') ) {
201 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
202 $cust_main->ncancelled_pkgs;
203 $session->{'pkgnum'} = $cust_pkg->pkgnum
204 if scalar(@cust_pkg) > 1;
208 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
209 #return { error => 'Only primary user may log in.' }
210 # if $conf->exists('selfservice_server-primary_only')
211 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
212 my $part_pkg = $cust_pkg->part_pkg;
213 return { error => 'Only primary user may log in.' }
214 if $conf->exists('selfservice_server-primary_only')
215 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
219 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
220 } until ( ! defined _cache->get($session_id) ); #just in case
222 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
223 _cache->set( $session_id, $session, $timeout );
225 return { 'error' => '',
226 'session_id' => $session_id,
232 if ( $p->{'session_id'} ) {
233 _cache->remove($p->{'session_id'});
234 return { %{ skin_info($p) }, 'error' => '' };
236 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
240 sub payment_gateway {
242 # takes a cust_main and a cust_payby entry, returns the payment_gateway
243 my $conf = new FS::Conf;
244 my $cust_main = shift;
245 my $cust_payby = shift;
246 my $gatewaynum = $conf->config('selfservice-payment_gateway');
248 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
249 die "configured gatewaynum $gatewaynum not found!" if !$pg;
253 return '' if ! FS::payby->realtime($cust_payby);
254 my $pg = $cust_main->agent->payment_gateway(
255 'method' => FS::payby->payby2bop($cust_payby),
265 my $conf = new FS::Conf;
267 my $info = skin_info($p);
269 use vars qw( $cust_paybys ); #cache for performance
270 unless ( $cust_paybys ) {
272 my %cust_paybys = map { $_ => 1 }
273 map { FS::payby->payby2payment($_) }
274 $conf->config('signup_server-payby');
276 $cust_paybys = [ keys %cust_paybys ];
279 $info->{'cust_paybys'} = $cust_paybys;
281 my($context, $session, $custnum) = _custoragent_session_custnum($p);
282 return { 'error' => $session } if $context eq 'error';
284 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
285 or return { 'error' => "unknown custnum $custnum" };
287 $info->{'hide_payment_fields'} = [
289 my $pg = payment_gateway($cust_main, $_);
290 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
291 } @{ $info->{cust_paybys} }
294 $info->{'self_suspend_reason'} =
295 $conf->config('selfservice-self_suspend_reason');
298 'custnum' => $custnum,
299 'access_pkgnum' => $session->{'pkgnum'},
300 'access_svcnum' => $session->{'svcnum'},
307 my($context, $session, $custnum) = _custoragent_session_custnum($p);
308 return { 'error' => $session } if $context eq 'error';
312 my $conf = new FS::Conf;
313 if ($conf->exists('cust_main-require_address2')) {
314 $return{'require_address2'} = '1';
316 $return{'require_address2'} = '';
319 if ( $custnum ) { #customer record
321 my $search = { 'custnum' => $custnum };
322 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
323 my $cust_main = qsearchs('cust_main', $search )
324 or return { 'error' => "unknown custnum $custnum" };
326 if ( $session->{'pkgnum'} ) {
327 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
329 $return{balance} = $cust_main->balance;
332 $return{tickets} = [ ($cust_main->tickets) ];
334 unless ( $session->{'pkgnum'} ) {
337 invnum => $_->invnum,
338 date => time2str("%b %o, %Y", $_->_date),
341 } $cust_main->open_cust_bill;
342 $return{open_invoices} = \@open;
345 $return{small_custview} =
346 small_custview( $cust_main,
347 scalar($conf->config('countrydefault')),
348 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
351 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
353 for (@cust_main_editable_fields) {
354 $return{$_} = $cust_main->get($_);
357 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
358 $return{payinfo} = $cust_main->paymask;
359 @return{'month', 'year'} = $cust_main->paydate_monthyear;
362 $return{'invoicing_list'} =
363 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
364 $return{'postal_invoicing'} =
365 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
367 if (scalar($conf->config('support_packages'))) {
368 my @support_services = ();
369 foreach ($cust_main->support_services) {
370 my $seconds = $_->svc_x->seconds;
371 my $time_remaining = (($seconds < 0) ? '-' : '' ).
372 int(abs($seconds)/3600)."h".
373 sprintf("%02d",(abs($seconds)%3600)/60)."m";
374 my $cust_pkg = $_->cust_pkg;
377 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
378 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
379 push @support_services, { svcnum => $_->svcnum,
380 time => $time_remaining,
385 $return{support_services} = \@support_services;
388 if ( $conf->config('prepayment_discounts-credit_type') ) {
390 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
393 } elsif ( $session->{'svcnum'} ) { #no customer record
395 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
396 or die "unknown svcnum";
397 $return{name} = $svc_acct->email;
401 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
405 return { 'error' => '',
406 'custnum' => $custnum,
414 my $session = _cache->get($p->{'session_id'})
415 or return { 'error' => "Can't resume session" }; #better error message
417 my $custnum = $session->{'custnum'}
418 or return { 'error' => "no customer record" };
420 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
421 or return { 'error' => "unknown custnum $custnum" };
423 my $new = new FS::cust_main { $cust_main->hash };
424 $new->set( $_ => $p->{$_} )
425 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
428 if (exists($p->{'payby'})) {
429 $p->{'payby'} =~ /^([A-Z]{4})$/
430 or return { 'error' => "illegal_payby " . $p->{'payby'} };
434 if ( $payby =~ /^(CARD|DCRD)$/ ) {
436 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
438 if ( $new->payinfo eq $cust_main->paymask ) {
439 $new->payinfo($cust_main->payinfo);
441 $new->payinfo($p->{'payinfo'});
444 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
446 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
449 $p->{'payinfo1'} =~ /^([\dx]+)$/
450 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
452 $p->{'payinfo2'} =~ /^([\dx]+)$/
453 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
455 $payinfo = $payinfo1. '@'. $payinfo2;
457 $new->payinfo( ($payinfo eq $cust_main->paymask)
458 ? $cust_main->payinfo
462 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
464 } elsif ( $payby =~ /^(BILL)$/ ) {
466 } elsif ( $payby ) { #notyet ready
467 return { 'error' => "unknown payby $payby" };
471 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
472 #false laziness with httemplate/edit/process/cust_main.cgi
473 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
474 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
476 @invoicing_list = $cust_main->invoicing_list;
479 my $error = $new->replace($cust_main, \@invoicing_list);
480 return { 'error' => $error } if $error;
483 return { 'error' => '' };
488 my $session = _cache->get($p->{'session_id'})
489 or return { 'error' => "Can't resume session" }; #better error message
495 my $conf = new FS::Conf;
496 use vars qw($payment_info); #cache for performance
497 unless ( $payment_info ) {
499 my %states = map { $_->state => 1 }
500 qsearch('cust_main_county', {
501 'country' => $conf->config('countrydefault') || 'US'
504 my %cust_paybys = map { $_ => 1 }
505 map { FS::payby->payby2payment($_) }
506 $conf->config('signup_server-payby');
508 my @cust_paybys = keys %cust_paybys;
512 #list all counties/states/countries
513 'cust_main_county' =>
514 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
516 #shortcut for one-country folks
518 [ sort { $a cmp $b } keys %states ],
520 'card_types' => card_types(),
522 'paytypes' => [ @FS::cust_main::paytypes ],
524 'paybys' => [ $conf->config('signup_server-payby') ],
525 'cust_paybys' => \@cust_paybys,
527 'stateid_label' => FS::Msgcat::_gettext('stateid'),
528 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
530 'show_ss' => $conf->exists('show_ss'),
531 'show_stateid' => $conf->exists('show_stateid'),
532 'show_paystate' => $conf->exists('show_bankstate'),
534 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
543 my %return = %$payment_info;
545 my $custnum = $session->{'custnum'};
547 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
548 or return { 'error' => "unknown custnum $custnum" };
550 $return{'hide_payment_fields'} = [
552 my $pg = payment_gateway($cust_main, $_);
553 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
554 } @{ $return{cust_paybys} }
557 $return{balance} = $cust_main->balance; #XXX pkg-balances?
559 $return{payname} = $cust_main->payname
560 || ( $cust_main->first. ' '. $cust_main->get('last') );
562 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
564 $return{payby} = $cust_main->payby;
565 $return{stateid_state} = $cust_main->stateid_state;
567 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
568 $return{card_type} = cardtype($cust_main->payinfo);
569 $return{payinfo} = $cust_main->paymask;
571 @return{'month', 'year'} = $cust_main->paydate_monthyear;
575 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
576 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
577 $return{payinfo1} = $payinfo1;
578 $return{payinfo2} = $payinfo2;
579 $return{paytype} = $cust_main->paytype;
580 $return{paystate} = $cust_main->paystate;
584 if ( $conf->config('prepayment_discounts-credit_type') ) {
586 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
589 #doubleclick protection
591 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
593 return { 'error' => '',
599 #some false laziness with httemplate/process/payment.cgi - look there for
600 #ACH and CVV support stuff
601 sub process_payment {
605 my $session = _cache->get($p->{'session_id'})
606 or return { 'error' => "Can't resume session" }; #better error message
610 my $custnum = $session->{'custnum'};
612 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
613 or return { 'error' => "unknown custnum $custnum" };
615 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
616 or return { 'error' => gettext('illegal_amount') };
618 return { error => 'Amount must be greater than 0' } unless $amount > 0;
620 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
621 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
622 my $discount_term = $1;
624 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
625 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
628 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
629 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
632 $p->{'payby'} ||= 'CARD';
633 $p->{'payby'} =~ /^([A-Z]{4})$/
634 or return { 'error' => "illegal_payby " . $p->{'payby'} };
637 #false laziness w/process/payment.cgi
640 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
642 $p->{'payinfo1'} =~ /^([\dx]+)$/
643 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
645 $p->{'payinfo2'} =~ /^([\dx]+)$/
646 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
648 $payinfo = $payinfo1. '@'. $payinfo2;
650 $payinfo = $cust_main->payinfo
651 if $cust_main->paymask eq $payinfo;
653 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
655 $payinfo = $p->{'payinfo'};
657 #more intelligent mathing will be needed here if you change
658 #card_masking_method and don't remove existing paymasks
659 $payinfo = $cust_main->payinfo
660 if $cust_main->paymask eq $payinfo;
663 $payinfo =~ /^(\d{13,16})$/
664 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
668 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
669 return { 'error' => gettext('unknown_card_type') }
670 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
672 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
673 if ( cardtype($payinfo) eq 'American Express card' ) {
674 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
675 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
678 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
679 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
685 die "unknown payby $payby";
689 'CARD' => [ qw( paystart_month paystart_year payissue payip
690 address1 address2 city state zip country ) ],
691 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
694 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
696 'payinfo' => $payinfo,
697 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
698 'payname' => $payname,
699 'paybatch' => $paybatch, #this doesn't actually do anything
701 'pkgnum' => $session->{'pkgnum'},
702 'discount_term' => $discount_term,
704 map { $_ => $p->{$_} } @{ $payby2fields{$payby} }
706 return { 'error' => $error } if $error;
708 $cust_main->apply_payments;
710 if ( $p->{'save'} ) {
711 my $new = new FS::cust_main { $cust_main->hash };
712 if ($payby eq 'CARD' || $payby eq 'DCRD') {
713 $new->set( $_ => $p->{$_} )
714 foreach qw( payname paystart_month paystart_year payissue payip
715 address1 address2 city state zip country );
716 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
717 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
718 $new->set( $_ => $p->{$_} )
719 foreach qw( payname payip paytype paystate
720 stateid stateid_state );
721 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
723 $new->set( 'payinfo' => $cust_main->card_token || $payinfo );
724 $new->set( 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01' );
725 my $error = $new->replace($cust_main);
727 #no, this causes customers to process their payments again
728 #return { 'error' => $error };
729 #XXX just warn verosely for now so i can figure out how these happen in
730 # the first place, eventually should redirect them to the "change
731 #address" page but indicate the payment did process??
732 delete($p->{'payinfo'}); #don't want to log this!
733 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
734 "NEW: ". Dumper($new)."\n".
735 "OLD: ". Dumper($cust_main)."\n".
736 "PACKET: ". Dumper($p)."\n";
743 return { 'error' => '' };
747 sub realtime_collect {
750 my $session = _cache->get($p->{'session_id'})
751 or return { 'error' => "Can't resume session" }; #better error message
753 my $custnum = $session->{'custnum'};
755 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
756 or return { 'error' => "unknown custnum $custnum" };
759 if ( $p->{'amount'} ) {
760 $amount = $p->{'amount'};
762 elsif ( $session->{'pkgnum'} ) {
763 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
766 $amount = $cust_main->balance;
769 my $error = $cust_main->realtime_collect(
770 'method' => $p->{'method'},
772 'pkgnum' => $session->{'pkgnum'},
773 'session_id' => $p->{'session_id'},
777 return { 'error' => $error } unless ref( $error );
779 return { 'error' => '', amount => $amount, %$error };
782 sub process_payment_order_pkg {
785 my $hr = process_payment($p);
786 return $hr if $hr->{'error'};
791 sub process_payment_order_renew {
794 my $hr = process_payment($p);
795 return $hr if $hr->{'error'};
804 my $session = _cache->get($p->{'session_id'})
805 or return { 'error' => "Can't resume session" }; #better error message
809 my $custnum = $session->{'custnum'};
811 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
812 or return { 'error' => "unknown custnum $custnum" };
814 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
815 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
823 return { 'error' => $error } if $error;
825 return { 'error' => '',
827 'seconds' => $seconds,
828 'duration' => duration_exact($seconds),
829 'upbytes' => $upbytes,
830 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
831 'downbytes' => $downbytes,
832 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
833 'totalbytes'=> $totalbytes,
834 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
841 my $session = _cache->get($p->{'session_id'})
842 or return { 'error' => "Can't resume session" }; #better error message
844 my $custnum = $session->{'custnum'};
846 my $invnum = $p->{'invnum'};
848 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
849 'custnum' => $custnum } )
850 or return { 'error' => "Can't find invnum" };
854 return { 'error' => '',
856 'invoice_text' => join('', $cust_bill->print_text ),
857 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
865 #sessioning for this? how do we get the session id to the backend invoice
866 # template so it can add it to the link, blah
869 if ( $p->{'invnum'} ) {
870 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
871 or return { 'error' => 'unknown invnum' };
872 $agentnum = $cust_bill->cust_main->agentnum;
875 my $templatename = $p->{'template'} || $p->{'templatename'};
877 #false laziness-ish w/view/cust_bill-logo.cgi
879 my $conf = new FS::Conf;
880 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
881 $templatename = "_$1";
886 my $filename = "logo$templatename.png";
888 return { 'error' => '',
889 'logo' => $conf->config_binary($filename, $agentnum),
890 'content_type' => 'image/png', #should allow gif, jpg too
897 my $session = _cache->get($p->{'session_id'})
898 or return { 'error' => "Can't resume session" }; #better error message
900 my $custnum = $session->{'custnum'};
902 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
903 or return { 'error' => "unknown custnum $custnum" };
905 my @cust_bill = $cust_main->cust_bill;
907 return { 'error' => '',
908 'invoices' => [ map { { 'invnum' => $_->invnum,
909 '_date' => $_->_date,
910 'date' => time2str("%b %o, %Y", $_->_date),
919 my $session = _cache->get($p->{'session_id'})
920 or return { 'error' => "Can't resume session" }; #better error message
922 my $custnum = $session->{'custnum'};
924 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
925 or return { 'error' => "unknown custnum $custnum" };
927 my @errors = $cust_main->cancel( 'quiet'=>1 );
929 my $error = scalar(@errors) ? join(' / ', @errors) : '';
931 return { 'error' => $error };
938 my($context, $session, $custnum) = _custoragent_session_custnum($p);
939 return { 'error' => $session } if $context eq 'error';
941 my $search = { 'custnum' => $custnum };
942 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
943 my $cust_main = qsearchs('cust_main', $search )
944 or return { 'error' => "unknown custnum $custnum" };
946 my $conf = new FS::Conf;
948 # the duplication below is necessary:
949 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
950 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
951 # non-backwards-compatible change breaking the software of anyone using the API
952 # instead of the stock selfservice
953 # 2. to return cancelled packages as well - for wholesale and non-wholesale
954 if( $conf->exists('selfservice_server-view-wholesale') ) {
955 return { 'svcnum' => $session->{'svcnum'},
956 'custnum' => $custnum,
957 'cust_pkg' => [ map {
959 part_pkg => [ map $_->hashref, $_->part_pkg ],
961 [ map $_->hashref, $_->available_part_svc ],
963 [ map { my $ref = { $_->hash,
964 label => [ $_->label ],
966 $ref->{_password} = $_->svc_x->_password
967 if $context eq 'agent'
968 && $conf->exists('agent-showpasswords')
969 && $_->part_svc->svcdb eq 'svc_acct';
974 } $cust_main->cust_pkg
977 small_custview( $cust_main, $conf->config('countrydefault') ),
978 'wholesale_view' => 1,
979 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
980 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
984 { 'svcnum' => $session->{'svcnum'},
985 'custnum' => $custnum,
986 'cust_pkg' => [ map {
990 [ map $_->hashref, $_->available_part_svc ],
992 [ map { my $ref = { $_->hash,
993 label => [ $_->label ],
995 $ref->{_password} = $_->svc_x->_password
996 if $context eq 'agent'
997 && $conf->exists('agent-showpasswords')
998 && $_->part_svc->svcdb eq 'svc_acct';
1003 } $cust_main->ncancelled_pkgs
1006 small_custview( $cust_main, $conf->config('countrydefault') ),
1014 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1015 return { 'error' => $session } if $context eq 'error';
1017 my $search = { 'custnum' => $custnum };
1018 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1019 my $cust_main = qsearchs('cust_main', $search )
1020 or return { 'error' => "unknown custnum $custnum" };
1023 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1024 foreach my $cust_pkg ( $p->{'ncancelled'}
1025 ? $cust_main->ncancelled_pkgs
1026 : $cust_main->unsuspended_pkgs ) {
1027 next if $session->{'pkgnum'} && $cust_pkg->pkgnum != $session->{'pkgnum'};
1028 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1030 if ( $p->{'svcdb'} ) {
1031 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1033 : ref($p->{'svcdb'}) eq 'ARRAY'
1034 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1035 : { $p->{'svcdb'} => 1 };
1036 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1039 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1043 'svcnum' => $session->{'svcnum'},
1044 'custnum' => $custnum,
1047 my $svc_x = $_->svc_x;
1048 my($label, $value) = $_->label;
1049 my $svcdb = $_->part_svc->svcdb;
1050 my $part_pkg = $_->cust_pkg->part_pkg;
1053 'svcnum' => $_->svcnum,
1059 if ( $svcdb eq 'svc_acct' ) {
1062 'username' => $svc_x->username,
1063 'email' => $svc_x->email,
1064 'seconds' => $svc_x->seconds,
1065 'upbytes' => display_bytecount($svc_x->upbytes),
1066 'downbytes' => display_bytecount($svc_x->downbytes),
1067 'totalbytes' => display_bytecount($svc_x->totalbytes),
1069 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1070 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1071 'recharge_upbytes' =>
1072 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1073 'recharge_downbytes' =>
1074 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1075 'recharge_totalbytes' =>
1076 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1080 } elsif ( $svcdb eq 'svc_phone' ) {
1094 sub _list_svc_usage {
1095 my($svc_acct, $begin, $end) = @_;
1097 foreach my $part_export (
1098 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1099 qw( sqlradius sqlradius_withdomain )
1101 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1106 sub list_svc_usage {
1107 _usage_details(\&_list_svc_usage, @_);
1110 sub _list_support_usage {
1111 my($svc_acct, $begin, $end) = @_;
1113 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1114 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1116 push @usage, { 'seconds' => $_->seconds,
1117 'support' => $_->support,
1118 '_date' => $_->_date,
1119 'id' => $_->transaction_id,
1120 'creator' => $_->creator,
1121 'subject' => $_->subject,
1122 'status' => $_->status,
1123 'ticketid' => $_->ticketid,
1129 sub list_support_usage {
1130 _usage_details(\&_list_support_usage, @_);
1133 sub _list_cdr_usage {
1134 my($svc_phone, $begin, $end) = @_;
1135 map [ $_->downstream_csv('format' => 'default') ], #XXX config for format
1136 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1139 sub list_cdr_usage {
1141 _usage_details( \&_list_cdr_usage, $p,
1142 'svcdb' => 'svc_phone',
1146 sub _usage_details {
1147 my($callback, $p, %opt) = @_;
1149 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1150 return { 'error' => $session } if $context eq 'error';
1152 my $search = { 'svcnum' => $p->{'svcnum'} };
1153 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1155 my $svcdb = $opt{'svcdb'} || 'svc_acct';
1157 my $svc_x = qsearchs( $svcdb, $search );
1158 return { 'error' => 'No service selected in list_svc_usage' }
1161 my $header = $svcdb eq 'svc_phone'
1162 ? [ split(',', FS::cdr::invoice_header('default') ) ] #XXX
1165 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
1166 my $freq = $cust_pkg->part_pkg->freq;
1167 my $start = $cust_pkg->setup;
1168 #my $end = $cust_pkg->bill; # or time?
1171 unless ( $p->{beginning} ) {
1172 $p->{beginning} = $cust_pkg->last_bill;
1173 $p->{ending} = $end;
1176 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending});
1178 #kinda false laziness with FS::cust_main::bill, but perhaps
1179 #we should really change this bit to DateTime and DateTime::Duration
1181 #change this bit to use Date::Manip? CAREFUL with timezones (see
1182 # mailing list archive)
1183 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
1184 (localtime($p->{ending}) )[0,1,2,3,4,5];
1185 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
1186 (localtime($p->{beginning}) )[0,1,2,3,4,5];
1188 if ( $freq =~ /^\d+$/ ) {
1190 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
1192 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
1193 } elsif ( $freq =~ /^(\d+)w$/ ) {
1195 $nmday += $weeks * 7;
1196 $pmday -= $weeks * 7;
1197 } elsif ( $freq =~ /^(\d+)d$/ ) {
1201 } elsif ( $freq =~ /^(\d+)h$/ ) {
1206 return { 'error' => "unparsable frequency: ". $freq };
1209 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
1210 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
1214 'svcnum' => $p->{svcnum},
1215 'beginning' => $p->{beginning},
1216 'ending' => $p->{ending},
1217 'previous' => ($previous > $start) ? $previous : $start,
1218 'next' => ($next < $end) ? $next : $end,
1219 'header' => $header,
1227 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1228 return { 'error' => $session } if $context eq 'error';
1230 my $search = { 'custnum' => $custnum };
1231 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1232 my $cust_main = qsearchs('cust_main', $search )
1233 or return { 'error' => "unknown custnum $custnum" };
1235 my $status = $cust_main->status;
1236 #false laziness w/ClientAPI/Signup.pm
1238 my $cust_pkg = new FS::cust_pkg ( {
1239 'custnum' => $custnum,
1240 'pkgpart' => $p->{'pkgpart'},
1242 my $error = $cust_pkg->check;
1243 return { 'error' => $error } if $error;
1246 unless ( $p->{'svcpart'} eq 'none' ) {
1250 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
1252 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1253 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
1254 $svcdb = $part_svc->svcdb;
1256 $svcdb = 'svc_acct';
1258 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
1261 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
1262 'svc_domain' => [ qw( domain ) ],
1263 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
1264 'svc_external' => [ qw( id title ) ],
1265 'svc_pbx' => [ qw( id name ) ],
1268 my $svc_x = "FS::$svcdb"->new( {
1269 'svcpart' => $svcpart,
1270 map { $_ => $p->{$_} } @{$fields{$svcdb}}
1273 if ( $svcdb eq 'svc_acct' ) {
1276 while ( length($p->{"snarf_machine$snarfnum"}) ) {
1277 my $acct_snarf = new FS::acct_snarf ( {
1278 'machine' => $p->{"snarf_machine$snarfnum"},
1279 'protocol' => $p->{"snarf_protocol$snarfnum"},
1280 'username' => $p->{"snarf_username$snarfnum"},
1281 '_password' => $p->{"snarf_password$snarfnum"},
1284 push @acct_snarf, $acct_snarf;
1286 $svc_x->child_objects( \@acct_snarf );
1289 my $y = $svc_x->setdefault; # arguably should be in new method
1290 return { 'error' => $y } if $y && !ref($y);
1292 $error = $svc_x->check;
1293 return { 'error' => $error } if $error;
1300 tie my %hash, 'Tie::RefHash';
1301 %hash = ( $cust_pkg => \@svc );
1303 $error = $cust_main->order_pkgs( \%hash, '', 'noexport' => 1 );
1304 return { 'error' => $error } if $error;
1306 my $conf = new FS::Conf;
1307 if ( $conf->exists('signup_server-realtime') ) {
1309 my $bill_error = _do_bop_realtime( $cust_main, $status );
1312 $cust_pkg->cancel('quiet'=>1);
1315 $cust_pkg->reexport;
1319 $cust_pkg->reexport;
1322 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
1324 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
1331 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1332 return { 'error' => $session } if $context eq 'error';
1334 my $search = { 'custnum' => $custnum };
1335 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1336 my $cust_main = qsearchs('cust_main', $search )
1337 or return { 'error' => "unknown custnum $custnum" };
1339 my $status = $cust_main->status;
1340 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
1341 or return { 'error' => "unknown package $p->{pkgnum}" };
1344 my $error = FS::cust_pkg::order( $custnum,
1350 my $conf = new FS::Conf;
1351 if ( $conf->exists('signup_server-realtime') ) {
1353 my $bill_error = _do_bop_realtime( $cust_main, $status );
1356 $newpkg[0]->suspend;
1359 $newpkg[0]->reexport;
1363 $newpkg[0]->reexport;
1366 return { error => '', pkgnum => $cust_pkg->pkgnum };
1370 sub order_recharge {
1373 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1374 return { 'error' => $session } if $context eq 'error';
1376 my $search = { 'custnum' => $custnum };
1377 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1378 my $cust_main = qsearchs('cust_main', $search )
1379 or return { 'error' => "unknown custnum $custnum" };
1381 my $status = $cust_main->status;
1382 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
1383 or return { 'error' => "unknown service " . $p->{'svcnum'} };
1385 my $svc_x = $cust_svc->svc_x;
1386 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
1389 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
1390 qw ( recharge_seconds recharge_upbytes recharge_downbytes
1391 recharge_totalbytes );
1392 my $amount = $part_pkg->option('recharge_amount', 1);
1394 my ($l, $v, $d) = $cust_svc->label; # blah
1395 my $pkg = "Recharge $v";
1397 my $bill_error = $cust_main->charge($amount, $pkg,
1398 "time: $vhash{seconds}, up: $vhash{upbytes}," .
1399 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
1400 $part_pkg->taxclass); #meh
1402 my $conf = new FS::Conf;
1403 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
1405 $bill_error = _do_bop_realtime( $cust_main, $status );
1410 my $error = $svc_x->recharge (\%vhash);
1411 return { 'error' => $error } if $error;
1415 my $error = $bill_error;
1416 $error ||= $svc_x->recharge (\%vhash);
1417 return { 'error' => $error } if $error;
1420 return { error => '', svc => $cust_svc->part_svc->svc };
1424 sub _do_bop_realtime {
1425 my ($cust_main, $status) = (shift, shift);
1427 my $old_balance = $cust_main->balance;
1429 my $bill_error = $cust_main->bill
1430 || $cust_main->apply_payments_and_credits
1431 || $cust_main->realtime_collect('selfservice' => 1);
1433 if ( $cust_main->balance > $old_balance
1434 && $cust_main->balance > 0
1435 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
1436 1 : $status eq 'suspended' ) ) {
1437 #this makes sense. credit is "un-doing" the invoice
1438 my $conf = new FS::Conf;
1439 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
1440 'self-service decline',
1441 'reason_type' => $conf->config('signup_credit_type'),
1443 $cust_main->apply_credits( 'order' => 'newest' );
1445 return { 'error' => '_decline', 'bill_error' => $bill_error };
1454 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1455 return { 'error' => $session } if $context eq 'error';
1457 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1458 or return { 'error' => "unknown custnum $custnum" };
1460 my @cust_pkg = sort { $a->bill <=> $b->bill }
1461 grep { $_->part_pkg->freq ne '0' }
1462 $cust_main->ncancelled_pkgs;
1464 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
1466 my $total = $cust_main->balance;
1469 my $bill = $_->bill;
1470 $total += $_->part_pkg->base_recur($_, \$bill);
1471 my $renew_date = $_->part_pkg->add_freq($_->bill);
1473 'pkgnum' => $_->pkgnum,
1474 'amount' => sprintf('%.2f', $total),
1475 'bill_date' => $_->bill,
1476 'bill_date_pretty' => time2str('%x', $_->bill),
1477 'renew_date' => $renew_date,
1478 'renew_date_pretty' => time2str('%x', $renew_date),
1479 'expire_date' => $_->expire,
1480 'expire_date_pretty' => time2str('%x', $_->expire),
1485 return { 'dates' => \@array };
1489 sub payment_info_renew_info {
1491 my $renew_info = renew_info($p);
1492 my $payment_info = payment_info($p);
1493 return { %$renew_info,
1501 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1502 return { 'error' => $session } if $context eq 'error';
1504 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1505 or return { 'error' => "unknown custnum $custnum" };
1507 my $date = $p->{'date'};
1511 #freeside-daily -n -d $date fs_daily $custnum
1512 $cust_main->bill_and_collect( 'time' => $date,
1513 'invoice_time' => $now,
1514 'actual_time' => $now,
1515 'check_freq' => '1d',
1518 return { 'error' => '' };
1522 # these are basically the same
1523 sub cancel_pkg { alter_pkg(shift, 'cancel', 'quiet' => 1) }
1525 my $conf = new FS::Conf;
1526 my $reasonnum = $conf->config('selfservice-self_suspend_reason')
1527 or return { 'error' => "Permission denied" };
1528 alter_pkg(shift, 'suspend', 'reason' => $reasonnum)
1534 my $session = _cache->get($p->{'session_id'})
1535 or return { 'error' => "Can't resume session" }; #better error message
1537 my $custnum = $session->{'custnum'};
1539 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1540 or return { 'error' => "unknown custnum $custnum" };
1542 my $pkgnum = $p->{'pkgnum'};
1544 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1545 'pkgnum' => $pkgnum, } )
1546 or return { 'error' => "unknown pkgnum $pkgnum" };
1548 my $error = $cust_pkg->$method(@_);
1549 return { 'error' => $error };
1553 sub provision_phone {
1555 my @bulkdid = @{$p->{'bulkdid'}};
1556 unless (scalar(@bulkdid)) {
1557 return _provision( 'FS::svc_phone',
1558 [qw(phonenum countrycode)],
1559 [qw(phonenum countrycode)],
1567 foreach my $did ( @bulkdid ) {
1568 $did =~ s/[^0-9]//g;
1569 $error = _provision( 'FS::svc_phone',
1570 [qw(phonenum countrycode)],
1571 [qw(phonenum countrycode)],
1573 'pkgnum' => $p->{'pkgnum'},
1574 'svcpart' => $p->{'svcpart'},
1576 'countrycode' => $p->{'countrycode'},
1577 'session_id' => $p->{'session_id'},
1580 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
1582 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
1585 sub provision_acct {
1587 warn "provision_acct called\n"
1590 return { 'error' => gettext('passwords_dont_match') }
1591 if $p->{'_password'} ne $p->{'_password2'};
1592 return { 'error' => gettext('empty_password') }
1593 unless length($p->{'_password'});
1595 if ($p->{'domsvc'}) {
1596 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
1597 qw ( svcpart pkgnum ) );
1598 return { 'error' => gettext('invalid_domain') }
1599 unless ($domains{$p->{'domsvc'}});
1602 warn "provision_acct calling _provision\n"
1604 _provision( 'FS::svc_acct',
1605 [qw(username _password domsvc)],
1606 [qw(username _password domsvc)],
1612 sub provision_external {
1614 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
1615 _provision( 'FS::svc_external',
1624 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
1625 warn "_provision called for $class\n"
1628 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1629 return { 'error' => $session } if $context eq 'error';
1631 my $search = { 'custnum' => $custnum };
1632 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1633 my $cust_main = qsearchs('cust_main', $search )
1634 or return { 'error' => "unknown custnum $custnum" };
1636 my $pkgnum = $p->{'pkgnum'};
1638 warn "searching for custnum $custnum pkgnum $pkgnum\n"
1640 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1641 'pkgnum' => $pkgnum,
1643 or return { 'error' => "unknown pkgnum $pkgnum" };
1645 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
1647 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
1648 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
1650 warn "creating $class record\n"
1652 my $svc_x = $class->new( {
1653 'pkgnum' => $p->{'pkgnum'},
1654 'svcpart' => $p->{'svcpart'},
1655 map { $_ => $p->{$_} } @$fields
1657 warn "inserting $class record\n"
1659 my $error = $svc_x->insert;
1662 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
1664 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
1667 my $return = { 'svc' => $part_svc->svc,
1669 map { $_ => $svc_x->get($_) } @$return_fields
1671 warn "_provision returning ". Dumper($return). "\n"
1680 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1681 return { 'error' => $session } if $context eq 'error';
1683 my $search = { 'custnum' => $custnum };
1684 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1685 my $cust_main = qsearchs('cust_main', $search )
1686 or return { 'error' => "unknown custnum $custnum" };
1688 my $pkgnum = $p->{'pkgnum'};
1690 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1691 'pkgnum' => $pkgnum,
1693 or return { 'error' => "unknown pkgnum $pkgnum" };
1695 my $svcpart = $p->{'svcpart'};
1697 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
1698 'svcpart' => $svcpart, } )
1699 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
1700 my $part_svc = $pkg_svc->part_svc;
1702 my $conf = new FS::Conf;
1705 'svc' => $part_svc->svc,
1706 'svcdb' => $part_svc->svcdb,
1707 'pkgnum' => $pkgnum,
1708 'svcpart' => $svcpart,
1709 'custnum' => $custnum,
1711 'security_phrase' => 0, #XXX !
1712 'svc_acct_pop' => [], #XXX !
1714 'init_popstate' => '',
1719 small_custview( $cust_main, $conf->config('countrydefault') ),
1725 sub unprovision_svc {
1728 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1729 return { 'error' => $session } if $context eq 'error';
1731 my $search = { 'custnum' => $custnum };
1732 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1733 my $cust_main = qsearchs('cust_main', $search )
1734 or return { 'error' => "unknown custnum $custnum" };
1736 my $svcnum = $p->{'svcnum'};
1738 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
1739 or return { 'error' => "unknown svcnum $svcnum" };
1741 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
1742 unless $cust_svc->cust_pkg->custnum == $custnum;
1744 my $conf = new FS::Conf;
1746 return { 'svc' => $cust_svc->part_svc->svc,
1747 'error' => $cust_svc->cancel,
1749 small_custview( $cust_main, $conf->config('countrydefault') ),
1754 sub myaccount_passwd {
1756 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1757 return { 'error' => $session } if $context eq 'error';
1759 return { 'error' => "New passwords don't match." }
1760 if $p->{'new_password'} ne $p->{'new_password2'};
1762 return { 'error' => 'Enter new password' }
1763 unless length($p->{'new_password'});
1765 #my $search = { 'custnum' => $custnum };
1766 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1767 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1768 my $search = " AND custnum = $1";
1769 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1771 my $svc_acct = qsearchs( {
1772 'table' => 'svc_acct',
1773 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1774 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
1775 'LEFT JOIN cust_main USING ( custnum ) ',
1776 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
1777 'extra_sql' => $search, #important
1779 or return { 'error' => "Service not found" };
1781 $svc_acct->_password($p->{'new_password'});
1782 my $error = $svc_acct->replace();
1784 my($label, $value) = $svc_acct->cust_svc->label;
1786 return { 'error' => $error,
1795 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1796 return { 'error' => $session } if $context eq 'error';
1798 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
1799 FS::TicketSystem->init();
1801 my $conf = new FS::Conf;
1802 my $queue = $p->{'queue'}
1803 || $conf->config('ticket_system-selfservice_queueid')
1804 || $conf->config('ticket_system-default_queueid');
1806 warn "$me create_ticket: creating ticket\n" if $DEBUG;
1807 my $err_or_ticket = FS::TicketSystem->create_ticket(
1808 '', #create RT session based on FS CurrentUser (fs_selfservice)
1810 'custnum' => $custnum,
1811 'svcnum' => $session->{'svcnum'},
1812 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
1815 if ( ref($err_or_ticket) ) {
1816 warn "$me create_ticket: sucessful: ". $err_or_ticket->id. "\n"
1818 return { 'error' => '',
1819 'ticket_id' => $err_or_ticket->id,
1822 warn "$me create_ticket: unsucessful: $err_or_ticket\n"
1824 return { 'error' => $err_or_ticket };
1832 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1833 return { 'error' => $session } if $context eq 'error';
1835 return { error => 'requested format not implemented' }
1836 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
1838 my $conf = new FS::Conf;
1839 my $age_threshold = 0;
1840 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
1841 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
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 # does it make more sense to just run one sql query for this instead of all the
1849 # insanity below? would increase performance greately for large data sets?
1851 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1852 my @part_svc = $cust_pkg->part_svc;
1853 foreach my $part_svc ( @part_svc ) {
1854 if($part_svc->svcdb eq 'svc_phone'){
1855 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
1856 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
1857 push @svc_phone, $cust_pkg_svc->svc_x
1858 if $cust_pkg_svc->date_inserted >= $age_threshold;
1866 my($xls_r,$xls_c) = (0,0);
1869 my @fields = qw( countrycode phonenum pin sip_password phone_name );
1870 if($p->{'format'} eq 'csv') {
1871 $csv = new Text::CSV_XS { 'always_quote' => 1,
1874 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
1875 $content .= $csv->string;
1877 elsif($p->{'format'} eq 'xls') {
1878 my $XLS1 = new IO::Scalar \$content;
1879 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
1880 or return { 'error' => "Error opening .xls file: $!" };
1881 $xls = $xls_workbook->add_worksheet('DIDs');
1882 foreach ( @fields ) {
1883 $xls->write(0,$xls_c++,$_);
1888 foreach my $svc_phone ( @svc_phone ) {
1889 my @cols = map { $svc_phone->$_ } @fields;
1890 if($p->{'format'} eq 'csv') {
1891 return { 'error' => 'Unable to create CSV' }
1892 unless $csv->combine(@cols);
1893 $content .= $csv->string;
1895 elsif($p->{'format'} eq 'xls') {
1898 $xls->write($xls_r,$xls_c++,$_);
1904 $xls_workbook->close() if $p->{'format'} eq 'xls';
1906 { content => $content, format => $p->{'format'}, };
1911 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1912 return { 'error' => $session } if $context eq 'error';
1914 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
1915 FS::TicketSystem->init();
1917 if(length($p->{'reply'})) {
1918 # currently this allows anyone to correspond on any ticket as fs_selfservice
1920 my @err_or_res = FS::TicketSystem->correspond_ticket(
1921 '', #create RT session based on FS CurrentUser (fs_selfservice)
1922 'ticket_id' => $p->{'ticket_id'},
1923 'content' => $p->{'reply'},
1926 return { 'error' => 'unable to reply to ticket' }
1927 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
1930 warn "$me get_ticket: getting ticket\n" if $DEBUG;
1931 my $err_or_ticket = FS::TicketSystem->get_ticket(
1932 '', #create RT session based on FS CurrentUser (fs_selfservice)
1933 'ticket_id' => $p->{'ticket_id'},
1936 if ( ref($err_or_ticket) ) {
1938 # since we're bypassing the RT security/permissions model by always using
1939 # fs_selfservice as the RT user (as opposed to a requestor, which we
1940 # can't do since we want all tickets linked to a cust), we check below whether
1941 # the requested ticket was actually linked to this customer
1942 my @custs = @{$err_or_ticket->{'custs'}};
1943 my @txns = @{$err_or_ticket->{'txns'}};
1945 return { 'error' => 'no customer' } unless ( $custnum && scalar(@custs) );
1947 return { 'error' => 'invalid ticket requested' }
1948 unless grep($_ eq $custnum, @custs);
1950 warn "$me get_ticket: sucessful: \n"
1952 return { 'error' => '',
1953 'transactions' => \@txns,
1954 'ticket_id' => $p->{'ticket_id'},
1957 warn "$me create_ticket: unsucessful: $err_or_ticket\n"
1959 return { 'error' => $err_or_ticket };
1966 sub _custoragent_session_custnum {
1969 my($context, $session, $custnum);
1970 if ( $p->{'session_id'} ) {
1972 $context = 'customer';
1973 $session = _cache->get($p->{'session_id'})
1974 or return ( 'error' => "Can't resume session" ); #better error message
1975 $custnum = $session->{'custnum'};
1977 } elsif ( $p->{'agent_session_id'} ) {
1980 my $agent_cache = new FS::ClientAPI_SessionCache( {
1981 'namespace' => 'FS::ClientAPI::Agent',
1983 $session = $agent_cache->get($p->{'agent_session_id'})
1984 or return ( 'error' => "Can't resume session" ); #better error message
1985 $custnum = $p->{'custnum'};
1989 return ( 'error' => "Can't resume session" ); #better error message
1992 ($context, $session, $custnum);