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
243 my $conf = new FS::Conf;
245 my $info = skin_info($p);
247 use vars qw( $cust_paybys ); #cache for performance
248 unless ( $cust_paybys ) {
250 my %cust_paybys = map { $_ => 1 }
251 map { FS::payby->payby2payment($_) }
252 $conf->config('signup_server-payby');
254 $cust_paybys = [ keys %cust_paybys ];
257 $info->{'cust_paybys'} = $cust_paybys;
259 my($context, $session, $custnum) = _custoragent_session_custnum($p);
260 return { 'error' => $session } if $context eq 'error';
262 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
263 or return { 'error' => "unknown custnum $custnum" };
265 $info->{hide_payment_fields} =
268 if ( FS::payby->realtime($_) ) {
269 $pg = $cust_main->agent->payment_gateway(
270 'method' => FS::payby->payby2bop($_),
274 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
276 @{ $info->{cust_paybys} }
279 $info->{'self_suspend_reason'} =
280 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
283 'custnum' => $custnum,
284 'access_pkgnum' => $session->{'pkgnum'},
285 'access_svcnum' => $session->{'svcnum'},
292 my($context, $session, $custnum) = _custoragent_session_custnum($p);
293 return { 'error' => $session } if $context eq 'error';
297 my $conf = new FS::Conf;
298 if ($conf->exists('cust_main-require_address2')) {
299 $return{'require_address2'} = '1';
301 $return{'require_address2'} = '';
304 if ( $custnum ) { #customer record
306 my $search = { 'custnum' => $custnum };
307 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
308 my $cust_main = qsearchs('cust_main', $search )
309 or return { 'error' => "unknown custnum $custnum" };
311 if ( $session->{'pkgnum'} ) {
312 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
314 $return{balance} = $cust_main->balance;
317 $return{tickets} = [ ($cust_main->tickets) ];
319 unless ( $session->{'pkgnum'} ) {
322 invnum => $_->invnum,
323 date => time2str("%b %o, %Y", $_->_date),
326 } $cust_main->open_cust_bill;
327 $return{open_invoices} = \@open;
330 $return{small_custview} =
331 small_custview( $cust_main,
332 scalar($conf->config('countrydefault')),
333 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
336 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
338 for (@cust_main_editable_fields) {
339 $return{$_} = $cust_main->get($_);
342 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
343 $return{payinfo} = $cust_main->paymask;
344 @return{'month', 'year'} = $cust_main->paydate_monthyear;
347 $return{'invoicing_list'} =
348 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
349 $return{'postal_invoicing'} =
350 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
352 if (scalar($conf->config('support_packages'))) {
353 my @support_services = ();
354 foreach ($cust_main->support_services) {
355 my $seconds = $_->svc_x->seconds;
356 my $time_remaining = (($seconds < 0) ? '-' : '' ).
357 int(abs($seconds)/3600)."h".
358 sprintf("%02d",(abs($seconds)%3600)/60)."m";
359 my $cust_pkg = $_->cust_pkg;
362 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
363 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
364 push @support_services, { svcnum => $_->svcnum,
365 time => $time_remaining,
370 $return{support_services} = \@support_services;
373 if ( $conf->config('prepayment_discounts-credit_type') ) {
375 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
378 } elsif ( $session->{'svcnum'} ) { #no customer record
380 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
381 or die "unknown svcnum";
382 $return{name} = $svc_acct->email;
386 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
390 return { 'error' => '',
391 'custnum' => $custnum,
399 my $session = _cache->get($p->{'session_id'})
400 or return { 'error' => "Can't resume session" }; #better error message
402 my $custnum = $session->{'custnum'}
403 or return { 'error' => "no customer record" };
405 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
406 or return { 'error' => "unknown custnum $custnum" };
408 my $new = new FS::cust_main { $cust_main->hash };
409 $new->set( $_ => $p->{$_} )
410 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
413 if (exists($p->{'payby'})) {
414 $p->{'payby'} =~ /^([A-Z]{4})$/
415 or return { 'error' => "illegal_payby " . $p->{'payby'} };
419 if ( $payby =~ /^(CARD|DCRD)$/ ) {
421 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
423 if ( $new->payinfo eq $cust_main->paymask ) {
424 $new->payinfo($cust_main->payinfo);
426 $new->payinfo($p->{'payinfo'});
429 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
431 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
434 $p->{'payinfo1'} =~ /^([\dx]+)$/
435 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
437 $p->{'payinfo2'} =~ /^([\dx]+)$/
438 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
440 $payinfo = $payinfo1. '@'. $payinfo2;
442 $new->payinfo( ($payinfo eq $cust_main->paymask)
443 ? $cust_main->payinfo
447 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
449 } elsif ( $payby =~ /^(BILL)$/ ) {
451 } elsif ( $payby ) { #notyet ready
452 return { 'error' => "unknown payby $payby" };
456 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
457 #false laziness with httemplate/edit/process/cust_main.cgi
458 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
459 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
461 @invoicing_list = $cust_main->invoicing_list;
464 my $error = $new->replace($cust_main, \@invoicing_list);
465 return { 'error' => $error } if $error;
468 return { 'error' => '' };
473 my $session = _cache->get($p->{'session_id'})
474 or return { 'error' => "Can't resume session" }; #better error message
480 my $conf = new FS::Conf;
481 use vars qw($payment_info); #cache for performance
482 unless ( $payment_info ) {
484 my %states = map { $_->state => 1 }
485 qsearch('cust_main_county', {
486 'country' => $conf->config('countrydefault') || 'US'
489 my %cust_paybys = map { $_ => 1 }
490 map { FS::payby->payby2payment($_) }
491 $conf->config('signup_server-payby');
493 my @cust_paybys = keys %cust_paybys;
497 #list all counties/states/countries
498 'cust_main_county' =>
499 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
501 #shortcut for one-country folks
503 [ sort { $a cmp $b } keys %states ],
505 'card_types' => card_types(),
507 'paytypes' => [ @FS::cust_main::paytypes ],
509 'paybys' => [ $conf->config('signup_server-payby') ],
510 'cust_paybys' => \@cust_paybys,
512 'stateid_label' => FS::Msgcat::_gettext('stateid'),
513 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
515 'show_ss' => $conf->exists('show_ss'),
516 'show_stateid' => $conf->exists('show_stateid'),
517 'show_paystate' => $conf->exists('show_bankstate'),
519 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
528 my %return = %$payment_info;
530 my $custnum = $session->{'custnum'};
532 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
533 or return { 'error' => "unknown custnum $custnum" };
535 $return{hide_payment_fields} =
538 if ( FS::payby->realtime($_) ) {
539 $pg = $cust_main->agent->payment_gateway(
540 'method' => FS::payby->payby2bop($_),
544 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
546 @{ $return{cust_paybys} }
549 $return{balance} = $cust_main->balance; #XXX pkg-balances?
551 $return{payname} = $cust_main->payname
552 || ( $cust_main->first. ' '. $cust_main->get('last') );
554 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
556 $return{payby} = $cust_main->payby;
557 $return{stateid_state} = $cust_main->stateid_state;
559 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
560 $return{card_type} = cardtype($cust_main->payinfo);
561 $return{payinfo} = $cust_main->paymask;
563 @return{'month', 'year'} = $cust_main->paydate_monthyear;
567 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
568 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
569 $return{payinfo1} = $payinfo1;
570 $return{payinfo2} = $payinfo2;
571 $return{paytype} = $cust_main->paytype;
572 $return{paystate} = $cust_main->paystate;
576 if ( $conf->config('prepayment_discounts-credit_type') ) {
578 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
581 #doubleclick protection
583 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
585 return { 'error' => '',
591 #some false laziness with httemplate/process/payment.cgi - look there for
592 #ACH and CVV support stuff
593 sub process_payment {
597 my $session = _cache->get($p->{'session_id'})
598 or return { 'error' => "Can't resume session" }; #better error message
602 my $custnum = $session->{'custnum'};
604 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
605 or return { 'error' => "unknown custnum $custnum" };
607 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
608 or return { 'error' => gettext('illegal_amount') };
610 return { error => 'Amount must be greater than 0' } unless $amount > 0;
612 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
613 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
614 my $discount_term = $1;
616 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
617 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
620 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
621 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
624 $p->{'payby'} ||= 'CARD';
625 $p->{'payby'} =~ /^([A-Z]{4})$/
626 or return { 'error' => "illegal_payby " . $p->{'payby'} };
629 #false laziness w/process/payment.cgi
632 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
634 $p->{'payinfo1'} =~ /^([\dx]+)$/
635 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
637 $p->{'payinfo2'} =~ /^([\dx]+)$/
638 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
640 $payinfo = $payinfo1. '@'. $payinfo2;
642 $payinfo = $cust_main->payinfo
643 if $cust_main->paymask eq $payinfo;
645 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
647 $payinfo = $p->{'payinfo'};
649 #more intelligent mathing will be needed here if you change
650 #card_masking_method and don't remove existing paymasks
651 $payinfo = $cust_main->payinfo
652 if $cust_main->paymask eq $payinfo;
655 $payinfo =~ /^(\d{13,16})$/
656 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
660 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
661 return { 'error' => gettext('unknown_card_type') }
662 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
664 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
665 if ( cardtype($payinfo) eq 'American Express card' ) {
666 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
667 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
670 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
671 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
677 die "unknown payby $payby";
681 'CARD' => [ qw( paystart_month paystart_year payissue payip
682 address1 address2 city state zip country ) ],
683 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
686 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
688 'payinfo' => $payinfo,
689 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
690 'payname' => $payname,
691 'paybatch' => $paybatch, #this doesn't actually do anything
693 'pkgnum' => $session->{'pkgnum'},
694 'discount_term' => $discount_term,
695 map { $_ => $p->{$_} } @{ $payby2fields{$payby} }
697 return { 'error' => $error } if $error;
699 $cust_main->apply_payments;
701 if ( $p->{'save'} ) {
702 my $new = new FS::cust_main { $cust_main->hash };
703 if ($payby eq 'CARD' || $payby eq 'DCRD') {
704 $new->set( $_ => $p->{$_} )
705 foreach qw( payname paystart_month paystart_year payissue payip
706 address1 address2 city state zip country );
707 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
708 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
709 $new->set( $_ => $p->{$_} )
710 foreach qw( payname payip paytype paystate
711 stateid stateid_state );
712 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
714 $new->set( 'payinfo' => $cust_main->card_token || $payinfo );
715 $new->set( 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01' );
716 my $error = $new->replace($cust_main);
718 #no, this causes customers to process their payments again
719 #return { 'error' => $error };
720 #XXX just warn verosely for now so i can figure out how these happen in
721 # the first place, eventually should redirect them to the "change
722 #address" page but indicate the payment did process??
723 delete($p->{'payinfo'}); #don't want to log this!
724 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
725 "NEW: ". Dumper($new)."\n".
726 "OLD: ". Dumper($cust_main)."\n".
727 "PACKET: ". Dumper($p)."\n";
734 return { 'error' => '' };
738 sub realtime_collect {
741 my $session = _cache->get($p->{'session_id'})
742 or return { 'error' => "Can't resume session" }; #better error message
744 my $custnum = $session->{'custnum'};
746 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
747 or return { 'error' => "unknown custnum $custnum" };
749 my $error = $cust_main->realtime_collect(
750 'method' => $p->{'method'},
751 'pkgnum' => $session->{'pkgnum'},
752 'session_id' => $p->{'session_id'},
755 return { 'error' => $error } unless ref( $error );
757 my $amount = $session->{'pkgnum'}
758 ? $cust_main->balance_pkgnum( $session->{'pkgnum'} )
759 : $cust_main->balance;
761 return { 'error' => '', amount => $amount, %$error };
764 sub process_payment_order_pkg {
767 my $hr = process_payment($p);
768 return $hr if $hr->{'error'};
773 sub process_payment_order_renew {
776 my $hr = process_payment($p);
777 return $hr if $hr->{'error'};
786 my $session = _cache->get($p->{'session_id'})
787 or return { 'error' => "Can't resume session" }; #better error message
791 my $custnum = $session->{'custnum'};
793 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
794 or return { 'error' => "unknown custnum $custnum" };
796 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
797 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
805 return { 'error' => $error } if $error;
807 return { 'error' => '',
809 'seconds' => $seconds,
810 'duration' => duration_exact($seconds),
811 'upbytes' => $upbytes,
812 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
813 'downbytes' => $downbytes,
814 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
815 'totalbytes'=> $totalbytes,
816 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
823 my $session = _cache->get($p->{'session_id'})
824 or return { 'error' => "Can't resume session" }; #better error message
826 my $custnum = $session->{'custnum'};
828 my $invnum = $p->{'invnum'};
830 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
831 'custnum' => $custnum } )
832 or return { 'error' => "Can't find invnum" };
836 return { 'error' => '',
838 'invoice_text' => join('', $cust_bill->print_text ),
839 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
847 #sessioning for this? how do we get the session id to the backend invoice
848 # template so it can add it to the link, blah
851 if ( $p->{'invnum'} ) {
852 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
853 or return { 'error' => 'unknown invnum' };
854 $agentnum = $cust_bill->cust_main->agentnum;
857 my $templatename = $p->{'template'} || $p->{'templatename'};
859 #false laziness-ish w/view/cust_bill-logo.cgi
861 my $conf = new FS::Conf;
862 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
863 $templatename = "_$1";
868 my $filename = "logo$templatename.png";
870 return { 'error' => '',
871 'logo' => $conf->config_binary($filename, $agentnum),
872 'content_type' => 'image/png', #should allow gif, jpg too
879 my $session = _cache->get($p->{'session_id'})
880 or return { 'error' => "Can't resume session" }; #better error message
882 my $custnum = $session->{'custnum'};
884 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
885 or return { 'error' => "unknown custnum $custnum" };
887 my @cust_bill = $cust_main->cust_bill;
889 return { 'error' => '',
890 'invoices' => [ map { { 'invnum' => $_->invnum,
891 '_date' => $_->_date,
892 'date' => time2str("%b %o, %Y", $_->_date),
901 my $session = _cache->get($p->{'session_id'})
902 or return { 'error' => "Can't resume session" }; #better error message
904 my $custnum = $session->{'custnum'};
906 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
907 or return { 'error' => "unknown custnum $custnum" };
909 my @errors = $cust_main->cancel( 'quiet'=>1 );
911 my $error = scalar(@errors) ? join(' / ', @errors) : '';
913 return { 'error' => $error };
920 my($context, $session, $custnum) = _custoragent_session_custnum($p);
921 return { 'error' => $session } if $context eq 'error';
923 my $search = { 'custnum' => $custnum };
924 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
925 my $cust_main = qsearchs('cust_main', $search )
926 or return { 'error' => "unknown custnum $custnum" };
928 my $conf = new FS::Conf;
930 # the duplication below is necessary:
931 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
932 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
933 # non-backwards-compatible change breaking the software of anyone using the API
934 # instead of the stock selfservice
935 # 2. to return cancelled packages as well - for wholesale and non-wholesale
936 if( $conf->exists('selfservice_server-view-wholesale') ) {
937 return { 'svcnum' => $session->{'svcnum'},
938 'custnum' => $custnum,
939 'cust_pkg' => [ map {
941 part_pkg => [ map $_->hashref, $_->part_pkg ],
943 [ map $_->hashref, $_->available_part_svc ],
945 [ map { my $ref = { $_->hash,
946 label => [ $_->label ],
948 $ref->{_password} = $_->svc_x->_password
949 if $context eq 'agent'
950 && $conf->exists('agent-showpasswords')
951 && $_->part_svc->svcdb eq 'svc_acct';
956 } $cust_main->cust_pkg
959 small_custview( $cust_main, $conf->config('countrydefault') ),
960 'wholesale_view' => 1,
961 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
962 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
966 { 'svcnum' => $session->{'svcnum'},
967 'custnum' => $custnum,
968 'cust_pkg' => [ map {
972 [ map $_->hashref, $_->available_part_svc ],
974 [ map { my $ref = { $_->hash,
975 label => [ $_->label ],
977 $ref->{_password} = $_->svc_x->_password
978 if $context eq 'agent'
979 && $conf->exists('agent-showpasswords')
980 && $_->part_svc->svcdb eq 'svc_acct';
985 } $cust_main->ncancelled_pkgs
988 small_custview( $cust_main, $conf->config('countrydefault') ),
996 my($context, $session, $custnum) = _custoragent_session_custnum($p);
997 return { 'error' => $session } if $context eq 'error';
999 my $search = { 'custnum' => $custnum };
1000 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1001 my $cust_main = qsearchs('cust_main', $search )
1002 or return { 'error' => "unknown custnum $custnum" };
1005 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1006 foreach my $cust_pkg ( $p->{'ncancelled'}
1007 ? $cust_main->ncancelled_pkgs
1008 : $cust_main->unsuspended_pkgs ) {
1009 next if $session->{'pkgnum'} && $cust_pkg->pkgnum != $session->{'pkgnum'};
1010 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1012 if ( $p->{'svcdb'} ) {
1013 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1015 : ref($p->{'svcdb'}) eq 'ARRAY'
1016 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1017 : { $p->{'svcdb'} => 1 };
1018 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1021 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1025 'svcnum' => $session->{'svcnum'},
1026 'custnum' => $custnum,
1029 my $svc_x = $_->svc_x;
1030 my($label, $value) = $_->label;
1031 my $svcdb = $_->part_svc->svcdb;
1032 my $part_pkg = $_->cust_pkg->part_pkg;
1035 'svcnum' => $_->svcnum,
1041 if ( $svcdb eq 'svc_acct' ) {
1044 'username' => $svc_x->username,
1045 'email' => $svc_x->email,
1046 'seconds' => $svc_x->seconds,
1047 'upbytes' => display_bytecount($svc_x->upbytes),
1048 'downbytes' => display_bytecount($svc_x->downbytes),
1049 'totalbytes' => display_bytecount($svc_x->totalbytes),
1051 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1052 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1053 'recharge_upbytes' =>
1054 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1055 'recharge_downbytes' =>
1056 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1057 'recharge_totalbytes' =>
1058 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1062 } elsif ( $svcdb eq 'svc_phone' ) {
1076 sub _list_svc_usage {
1077 my($svc_acct, $begin, $end) = @_;
1079 foreach my $part_export (
1080 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1081 qw( sqlradius sqlradius_withdomain )
1083 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1088 sub list_svc_usage {
1089 _usage_details(\&_list_svc_usage, @_);
1092 sub _list_support_usage {
1093 my($svc_acct, $begin, $end) = @_;
1095 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1096 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1098 push @usage, { 'seconds' => $_->seconds,
1099 'support' => $_->support,
1100 '_date' => $_->_date,
1101 'id' => $_->transaction_id,
1102 'creator' => $_->creator,
1103 'subject' => $_->subject,
1104 'status' => $_->status,
1105 'ticketid' => $_->ticketid,
1111 sub list_support_usage {
1112 _usage_details(\&_list_support_usage, @_);
1115 sub _list_cdr_usage {
1116 my($svc_phone, $begin, $end) = @_;
1117 map [ $_->downstream_csv('format' => 'default') ], #XXX config for format
1118 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1121 sub list_cdr_usage {
1123 _usage_details( \&_list_cdr_usage, $p,
1124 'svcdb' => 'svc_phone',
1128 sub _usage_details {
1129 my($callback, $p, %opt) = @_;
1131 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1132 return { 'error' => $session } if $context eq 'error';
1134 my $search = { 'svcnum' => $p->{'svcnum'} };
1135 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1137 my $svcdb = $opt{'svcdb'} || 'svc_acct';
1139 my $svc_x = qsearchs( $svcdb, $search );
1140 return { 'error' => 'No service selected in list_svc_usage' }
1143 my $header = $svcdb eq 'svc_phone'
1144 ? [ split(',', FS::cdr::invoice_header('default') ) ] #XXX
1147 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
1148 my $freq = $cust_pkg->part_pkg->freq;
1149 my $start = $cust_pkg->setup;
1150 #my $end = $cust_pkg->bill; # or time?
1153 unless ( $p->{beginning} ) {
1154 $p->{beginning} = $cust_pkg->last_bill;
1155 $p->{ending} = $end;
1158 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending});
1160 #kinda false laziness with FS::cust_main::bill, but perhaps
1161 #we should really change this bit to DateTime and DateTime::Duration
1163 #change this bit to use Date::Manip? CAREFUL with timezones (see
1164 # mailing list archive)
1165 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
1166 (localtime($p->{ending}) )[0,1,2,3,4,5];
1167 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
1168 (localtime($p->{beginning}) )[0,1,2,3,4,5];
1170 if ( $freq =~ /^\d+$/ ) {
1172 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
1174 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
1175 } elsif ( $freq =~ /^(\d+)w$/ ) {
1177 $nmday += $weeks * 7;
1178 $pmday -= $weeks * 7;
1179 } elsif ( $freq =~ /^(\d+)d$/ ) {
1183 } elsif ( $freq =~ /^(\d+)h$/ ) {
1188 return { 'error' => "unparsable frequency: ". $freq };
1191 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
1192 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
1196 'svcnum' => $p->{svcnum},
1197 'beginning' => $p->{beginning},
1198 'ending' => $p->{ending},
1199 'previous' => ($previous > $start) ? $previous : $start,
1200 'next' => ($next < $end) ? $next : $end,
1201 'header' => $header,
1209 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1210 return { 'error' => $session } if $context eq 'error';
1212 my $search = { 'custnum' => $custnum };
1213 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1214 my $cust_main = qsearchs('cust_main', $search )
1215 or return { 'error' => "unknown custnum $custnum" };
1217 my $status = $cust_main->status;
1218 #false laziness w/ClientAPI/Signup.pm
1220 my $cust_pkg = new FS::cust_pkg ( {
1221 'custnum' => $custnum,
1222 'pkgpart' => $p->{'pkgpart'},
1224 my $error = $cust_pkg->check;
1225 return { 'error' => $error } if $error;
1228 unless ( $p->{'svcpart'} eq 'none' ) {
1232 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
1234 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1235 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
1236 $svcdb = $part_svc->svcdb;
1238 $svcdb = 'svc_acct';
1240 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
1243 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
1244 'svc_domain' => [ qw( domain ) ],
1245 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
1246 'svc_external' => [ qw( id title ) ],
1247 'svc_pbx' => [ qw( id name ) ],
1250 my $svc_x = "FS::$svcdb"->new( {
1251 'svcpart' => $svcpart,
1252 map { $_ => $p->{$_} } @{$fields{$svcdb}}
1255 if ( $svcdb eq 'svc_acct' ) {
1258 while ( length($p->{"snarf_machine$snarfnum"}) ) {
1259 my $acct_snarf = new FS::acct_snarf ( {
1260 'machine' => $p->{"snarf_machine$snarfnum"},
1261 'protocol' => $p->{"snarf_protocol$snarfnum"},
1262 'username' => $p->{"snarf_username$snarfnum"},
1263 '_password' => $p->{"snarf_password$snarfnum"},
1266 push @acct_snarf, $acct_snarf;
1268 $svc_x->child_objects( \@acct_snarf );
1271 my $y = $svc_x->setdefault; # arguably should be in new method
1272 return { 'error' => $y } if $y && !ref($y);
1274 $error = $svc_x->check;
1275 return { 'error' => $error } if $error;
1282 tie my %hash, 'Tie::RefHash';
1283 %hash = ( $cust_pkg => \@svc );
1285 $error = $cust_main->order_pkgs( \%hash, '', 'noexport' => 1 );
1286 return { 'error' => $error } if $error;
1288 my $conf = new FS::Conf;
1289 if ( $conf->exists('signup_server-realtime') ) {
1291 my $bill_error = _do_bop_realtime( $cust_main, $status );
1294 $cust_pkg->cancel('quiet'=>1);
1297 $cust_pkg->reexport;
1301 $cust_pkg->reexport;
1304 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
1306 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
1313 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1314 return { 'error' => $session } if $context eq 'error';
1316 my $search = { 'custnum' => $custnum };
1317 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1318 my $cust_main = qsearchs('cust_main', $search )
1319 or return { 'error' => "unknown custnum $custnum" };
1321 my $status = $cust_main->status;
1322 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
1323 or return { 'error' => "unknown package $p->{pkgnum}" };
1326 my $error = FS::cust_pkg::order( $custnum,
1332 my $conf = new FS::Conf;
1333 if ( $conf->exists('signup_server-realtime') ) {
1335 my $bill_error = _do_bop_realtime( $cust_main, $status );
1338 $newpkg[0]->suspend;
1341 $newpkg[0]->reexport;
1345 $newpkg[0]->reexport;
1348 return { error => '', pkgnum => $cust_pkg->pkgnum };
1352 sub order_recharge {
1355 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1356 return { 'error' => $session } if $context eq 'error';
1358 my $search = { 'custnum' => $custnum };
1359 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1360 my $cust_main = qsearchs('cust_main', $search )
1361 or return { 'error' => "unknown custnum $custnum" };
1363 my $status = $cust_main->status;
1364 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
1365 or return { 'error' => "unknown service " . $p->{'svcnum'} };
1367 my $svc_x = $cust_svc->svc_x;
1368 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
1371 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
1372 qw ( recharge_seconds recharge_upbytes recharge_downbytes
1373 recharge_totalbytes );
1374 my $amount = $part_pkg->option('recharge_amount', 1);
1376 my ($l, $v, $d) = $cust_svc->label; # blah
1377 my $pkg = "Recharge $v";
1379 my $bill_error = $cust_main->charge($amount, $pkg,
1380 "time: $vhash{seconds}, up: $vhash{upbytes}," .
1381 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
1382 $part_pkg->taxclass); #meh
1384 my $conf = new FS::Conf;
1385 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
1387 $bill_error = _do_bop_realtime( $cust_main, $status );
1392 my $error = $svc_x->recharge (\%vhash);
1393 return { 'error' => $error } if $error;
1397 my $error = $bill_error;
1398 $error ||= $svc_x->recharge (\%vhash);
1399 return { 'error' => $error } if $error;
1402 return { error => '', svc => $cust_svc->part_svc->svc };
1406 sub _do_bop_realtime {
1407 my ($cust_main, $status) = (shift, shift);
1409 my $old_balance = $cust_main->balance;
1411 my $bill_error = $cust_main->bill
1412 || $cust_main->apply_payments_and_credits
1413 || $cust_main->realtime_collect;
1415 if ( $cust_main->balance > $old_balance
1416 && $cust_main->balance > 0
1417 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
1418 1 : $status eq 'suspended' ) ) {
1419 #this makes sense. credit is "un-doing" the invoice
1420 my $conf = new FS::Conf;
1421 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
1422 'self-service decline',
1423 'reason_type' => $conf->config('signup_credit_type'),
1425 $cust_main->apply_credits( 'order' => 'newest' );
1427 return { 'error' => '_decline', 'bill_error' => $bill_error };
1436 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1437 return { 'error' => $session } if $context eq 'error';
1439 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1440 or return { 'error' => "unknown custnum $custnum" };
1442 my @cust_pkg = sort { $a->bill <=> $b->bill }
1443 grep { $_->part_pkg->freq ne '0' }
1444 $cust_main->ncancelled_pkgs;
1446 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
1448 my $total = $cust_main->balance;
1451 my $bill = $_->bill;
1452 $total += $_->part_pkg->base_recur($_, \$bill);
1453 my $renew_date = $_->part_pkg->add_freq($_->bill);
1455 'pkgnum' => $_->pkgnum,
1456 'amount' => sprintf('%.2f', $total),
1457 'bill_date' => $_->bill,
1458 'bill_date_pretty' => time2str('%x', $_->bill),
1459 'renew_date' => $renew_date,
1460 'renew_date_pretty' => time2str('%x', $renew_date),
1461 'expire_date' => $_->expire,
1462 'expire_date_pretty' => time2str('%x', $_->expire),
1467 return { 'dates' => \@array };
1471 sub payment_info_renew_info {
1473 my $renew_info = renew_info($p);
1474 my $payment_info = payment_info($p);
1475 return { %$renew_info,
1483 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1484 return { 'error' => $session } if $context eq 'error';
1486 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1487 or return { 'error' => "unknown custnum $custnum" };
1489 my $date = $p->{'date'};
1493 #freeside-daily -n -d $date fs_daily $custnum
1494 $cust_main->bill_and_collect( 'time' => $date,
1495 'invoice_time' => $now,
1496 'actual_time' => $now,
1497 'check_freq' => '1d',
1500 return { 'error' => '' };
1506 my $session = _cache->get($p->{'session_id'})
1507 or return { 'error' => "Can't resume session" }; #better error message
1509 my $custnum = $session->{'custnum'};
1511 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1512 or return { 'error' => "unknown custnum $custnum" };
1514 my $conf = new FS::Conf;
1516 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
1517 or return { 'error' => 'Permission denied' };
1519 my $pkgnum = $p->{'pkgnum'};
1521 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1522 'pkgnum' => $pkgnum, } )
1523 or return { 'error' => "unknown pkgnum $pkgnum" };
1525 my $error = $cust_pkg->suspend(reason => $reasonnum);
1526 return { 'error' => $error };
1532 my $session = _cache->get($p->{'session_id'})
1533 or return { 'error' => "Can't resume session" }; #better error message
1535 my $custnum = $session->{'custnum'};
1537 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1538 or return { 'error' => "unknown custnum $custnum" };
1540 my $pkgnum = $p->{'pkgnum'};
1542 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1543 'pkgnum' => $pkgnum, } )
1544 or return { 'error' => "unknown pkgnum $pkgnum" };
1546 my $error = $cust_pkg->cancel('quiet' => 1);
1547 return { 'error' => $error };
1551 sub provision_phone {
1553 my @bulkdid = @{$p->{'bulkdid'}};
1554 unless (scalar(@bulkdid)) {
1555 return _provision( 'FS::svc_phone',
1556 [qw(phonenum countrycode)],
1557 [qw(phonenum countrycode)],
1562 #XXX: finish bulk orders
1565 sub provision_acct {
1567 warn "provision_acct called\n"
1570 return { 'error' => gettext('passwords_dont_match') }
1571 if $p->{'_password'} ne $p->{'_password2'};
1572 return { 'error' => gettext('empty_password') }
1573 unless length($p->{'_password'});
1575 if ($p->{'domsvc'}) {
1576 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
1577 qw ( svcpart pkgnum ) );
1578 return { 'error' => gettext('invalid_domain') }
1579 unless ($domains{$p->{'domsvc'}});
1582 warn "provision_acct calling _provision\n"
1584 _provision( 'FS::svc_acct',
1585 [qw(username _password domsvc)],
1586 [qw(username _password domsvc)],
1592 sub provision_external {
1594 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
1595 _provision( 'FS::svc_external',
1604 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
1605 warn "_provision called for $class\n"
1608 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1609 return { 'error' => $session } if $context eq 'error';
1611 my $search = { 'custnum' => $custnum };
1612 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1613 my $cust_main = qsearchs('cust_main', $search )
1614 or return { 'error' => "unknown custnum $custnum" };
1616 my $pkgnum = $p->{'pkgnum'};
1618 warn "searching for custnum $custnum pkgnum $pkgnum\n"
1620 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1621 'pkgnum' => $pkgnum,
1623 or return { 'error' => "unknown pkgnum $pkgnum" };
1625 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
1627 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
1628 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
1630 warn "creating $class record\n"
1632 my $svc_x = $class->new( {
1633 'pkgnum' => $p->{'pkgnum'},
1634 'svcpart' => $p->{'svcpart'},
1635 map { $_ => $p->{$_} } @$fields
1637 warn "inserting $class record\n"
1639 my $error = $svc_x->insert;
1642 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
1644 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
1647 my $return = { 'svc' => $part_svc->svc,
1649 map { $_ => $svc_x->get($_) } @$return_fields
1651 warn "_provision returning ". Dumper($return). "\n"
1660 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1661 return { 'error' => $session } if $context eq 'error';
1663 my $search = { 'custnum' => $custnum };
1664 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1665 my $cust_main = qsearchs('cust_main', $search )
1666 or return { 'error' => "unknown custnum $custnum" };
1668 my $pkgnum = $p->{'pkgnum'};
1670 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1671 'pkgnum' => $pkgnum,
1673 or return { 'error' => "unknown pkgnum $pkgnum" };
1675 my $svcpart = $p->{'svcpart'};
1677 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
1678 'svcpart' => $svcpart, } )
1679 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
1680 my $part_svc = $pkg_svc->part_svc;
1682 my $conf = new FS::Conf;
1685 'svc' => $part_svc->svc,
1686 'svcdb' => $part_svc->svcdb,
1687 'pkgnum' => $pkgnum,
1688 'svcpart' => $svcpart,
1689 'custnum' => $custnum,
1691 'security_phrase' => 0, #XXX !
1692 'svc_acct_pop' => [], #XXX !
1694 'init_popstate' => '',
1699 small_custview( $cust_main, $conf->config('countrydefault') ),
1705 sub unprovision_svc {
1708 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1709 return { 'error' => $session } if $context eq 'error';
1711 my $search = { 'custnum' => $custnum };
1712 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1713 my $cust_main = qsearchs('cust_main', $search )
1714 or return { 'error' => "unknown custnum $custnum" };
1716 my $svcnum = $p->{'svcnum'};
1718 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
1719 or return { 'error' => "unknown svcnum $svcnum" };
1721 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
1722 unless $cust_svc->cust_pkg->custnum == $custnum;
1724 my $conf = new FS::Conf;
1726 return { 'svc' => $cust_svc->part_svc->svc,
1727 'error' => $cust_svc->cancel,
1729 small_custview( $cust_main, $conf->config('countrydefault') ),
1734 sub myaccount_passwd {
1736 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1737 return { 'error' => $session } if $context eq 'error';
1739 return { 'error' => "New passwords don't match." }
1740 if $p->{'new_password'} ne $p->{'new_password2'};
1742 return { 'error' => 'Enter new password' }
1743 unless length($p->{'new_password'});
1745 #my $search = { 'custnum' => $custnum };
1746 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1747 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1748 my $search = " AND custnum = $1";
1749 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1751 my $svc_acct = qsearchs( {
1752 'table' => 'svc_acct',
1753 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1754 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
1755 'LEFT JOIN cust_main USING ( custnum ) ',
1756 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
1757 'extra_sql' => $search, #important
1759 or return { 'error' => "Service not found" };
1761 $svc_acct->_password($p->{'new_password'});
1762 my $error = $svc_acct->replace();
1764 my($label, $value) = $svc_acct->cust_svc->label;
1766 return { 'error' => $error,
1775 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1776 return { 'error' => $session } if $context eq 'error';
1778 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
1779 FS::TicketSystem->init();
1781 my $conf = new FS::Conf;
1782 my $queue = $p->{'queue'}
1783 || $conf->config('ticket_system-selfservice_queueid')
1784 || $conf->config('ticket_system-default_queueid');
1786 warn "$me create_ticket: creating ticket\n" if $DEBUG;
1787 my $err_or_ticket = FS::TicketSystem->create_ticket(
1788 '', #create RT session based on FS CurrentUser (fs_selfservice)
1790 'custnum' => $custnum,
1791 'svcnum' => $session->{'svcnum'},
1792 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
1795 if ( ref($err_or_ticket) ) {
1796 warn "$me create_ticket: sucessful: ". $err_or_ticket->id. "\n"
1798 return { 'error' => '',
1799 'ticket_id' => $err_or_ticket->id,
1802 warn "$me create_ticket: unsucessful: $err_or_ticket\n"
1804 return { 'error' => $err_or_ticket };
1812 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1813 return { 'error' => $session } if $context eq 'error';
1815 return { error => 'requested format not implemented' }
1816 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
1818 my $conf = new FS::Conf;
1819 my $age_threshold = 0;
1820 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
1821 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
1823 my $search = { 'custnum' => $custnum };
1824 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1825 my $cust_main = qsearchs('cust_main', $search )
1826 or return { 'error' => "unknown custnum $custnum" };
1828 # does it make more sense to just run one sql query for this instead of all the
1829 # insanity below? would increase performance greately for large data sets?
1831 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1832 my @part_svc = $cust_pkg->part_svc;
1833 foreach my $part_svc ( @part_svc ) {
1834 if($part_svc->svcdb eq 'svc_phone'){
1835 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
1836 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
1837 push @svc_phone, $cust_pkg_svc->svc_x
1838 if $cust_pkg_svc->date_inserted >= $age_threshold;
1846 my($xls_r,$xls_c) = (0,0);
1849 my @fields = qw( countrycode phonenum pin sip_password phone_name );
1850 if($p->{'format'} eq 'csv') {
1851 $csv = new Text::CSV_XS { 'always_quote' => 1,
1854 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
1855 $content .= $csv->string;
1857 elsif($p->{'format'} eq 'xls') {
1858 my $XLS1 = new IO::Scalar \$content;
1859 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
1860 or return { 'error' => "Error opening .xls file: $!" };
1861 $xls = $xls_workbook->add_worksheet('DIDs');
1862 foreach ( @fields ) {
1863 $xls->write(0,$xls_c++,$_);
1868 foreach my $svc_phone ( @svc_phone ) {
1869 my @cols = map { $svc_phone->$_ } @fields;
1870 if($p->{'format'} eq 'csv') {
1871 return { 'error' => 'Unable to create CSV' }
1872 unless $csv->combine(@cols);
1873 $content .= $csv->string;
1875 elsif($p->{'format'} eq 'xls') {
1878 $xls->write($xls_r,$xls_c++,$_);
1884 $xls_workbook->close() if $p->{'format'} eq 'xls';
1886 { content => $content, format => $p->{'format'}, };
1891 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1892 return { 'error' => $session } if $context eq 'error';
1894 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
1895 FS::TicketSystem->init();
1897 if(length($p->{'reply'})) {
1898 # currently this allows anyone to correspond on any ticket as fs_selfservice
1900 my @err_or_res = FS::TicketSystem->correspond_ticket(
1901 '', #create RT session based on FS CurrentUser (fs_selfservice)
1902 'ticket_id' => $p->{'ticket_id'},
1903 'content' => $p->{'reply'},
1906 return { 'error' => 'unable to reply to ticket' }
1907 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
1910 warn "$me get_ticket: getting ticket\n" if $DEBUG;
1911 my $err_or_ticket = FS::TicketSystem->get_ticket(
1912 '', #create RT session based on FS CurrentUser (fs_selfservice)
1913 'ticket_id' => $p->{'ticket_id'},
1916 if ( ref($err_or_ticket) ) {
1918 # since we're bypassing the RT security/permissions model by always using
1919 # fs_selfservice as the RT user (as opposed to a requestor, which we
1920 # can't do since we want all tickets linked to a cust), we check below whether
1921 # the requested ticket was actually linked to this customer
1922 my @custs = @{$err_or_ticket->{'custs'}};
1923 my @txns = @{$err_or_ticket->{'txns'}};
1925 return { 'error' => 'no customer' } unless ( $custnum && scalar(@custs) );
1927 return { 'error' => 'invalid ticket requested' }
1928 unless grep($_ eq $custnum, @custs);
1930 warn "$me get_ticket: sucessful: \n"
1932 return { 'error' => '',
1933 'transactions' => \@txns,
1934 'ticket_id' => $p->{'ticket_id'},
1937 warn "$me create_ticket: unsucessful: $err_or_ticket\n"
1939 return { 'error' => $err_or_ticket };
1946 sub _custoragent_session_custnum {
1949 my($context, $session, $custnum);
1950 if ( $p->{'session_id'} ) {
1952 $context = 'customer';
1953 $session = _cache->get($p->{'session_id'})
1954 or return ( 'error' => "Can't resume session" ); #better error message
1955 $custnum = $session->{'custnum'};
1957 } elsif ( $p->{'agent_session_id'} ) {
1960 my $agent_cache = new FS::ClientAPI_SessionCache( {
1961 'namespace' => 'FS::ClientAPI::Agent',
1963 $session = $agent_cache->get($p->{'agent_session_id'})
1964 or return ( 'error' => "Can't resume session" ); #better error message
1965 $custnum = $p->{'custnum'};
1969 return ( 'error' => "Can't resume session" ); #better error message
1972 ($context, $session, $custnum);