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 $me = '[FS::ClientAPI::MyAccount]';
39 use vars qw( @cust_main_editable_fields );
40 @cust_main_editable_fields = qw(
41 first last company address1 address2 city
42 county state zip country daytime night fax
43 ship_first ship_last ship_company ship_address1 ship_address2 ship_city
44 ship_state ship_zip ship_country ship_daytime ship_night ship_fax
45 payby payinfo payname paystart_month paystart_year payissue payip
46 ss paytype paystate stateid stateid_state
50 $cache ||= new FS::ClientAPI_SessionCache( {
51 'namespace' => 'FS::ClientAPI::MyAccount',
58 my($context, $session, $custnum) = _custoragent_session_custnum($p);
59 #return { 'error' => $session } if $context eq 'error';
62 if ( $context eq 'customer' ) {
64 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
67 $sth->execute($custnum) or die $sth->errstr;
69 $agentnum = $sth->fetchrow_arrayref->[0]
70 or die "no agentnum for custnum $custnum";
72 #} elsif ( $context eq 'agent' ) {
73 } elsif ( $p->{'agentnum'} =~ /^(\d+)$/ ) {
77 my $conf = new FS::Conf;
79 #false laziness w/Signup.pm
81 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
83 if ( $skin_info_cache_agent ) {
85 warn "$me loading cached skin info for agentnum $agentnum\n"
90 warn "$me populating skin info cache for agentnum $agentnum\n"
93 $skin_info_cache_agent = {
94 'agentnum' => $agentnum,
95 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
97 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
98 qw( body_bgcolor box_bgcolor
99 text_color link_color vlink_color hlink_color alink_color
100 font title_color title_align title_size menu_bgcolor menu_fontsize
103 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
104 qw( menu_skipblanks menu_skipheadings menu_nounderline )
106 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
107 qw( title_left_image title_right_image
108 menu_top_image menu_body_image menu_bottom_image
111 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
112 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
113 qw( head body_header body_footer company_address ) ),
116 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
120 #{ %$skin_info_cache_agent };
121 $skin_info_cache_agent;
128 my $conf = new FS::Conf;
132 'phone_login' => $conf->exists('selfservice_server-phone_login'),
133 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
140 #false laziness w/FS::ClientAPI::passwd::passwd
144 my $conf = new FS::Conf;
147 if ( $p->{'domain'} eq 'svc_phone'
148 && $conf->exists('selfservice_server-phone_login') ) {
150 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
151 return { error => 'Number not found.' } unless $svc_phone;
154 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
155 #return { error => 'Only primary user may log in.' }
156 # if $conf->exists('selfservice_server-primary_only')
157 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
159 return { error => 'Incorrect PIN.' }
160 unless $svc_phone->check_pin($p->{'password'});
166 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
167 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
169 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
170 'domsvc' => $svc_domain->svcnum, }
172 return { error => 'User not found.' } unless $svc_acct;
174 if($conf->exists('selfservice_server-login_svcpart')) {
175 my @svcpart = $conf->config('selfservice_server-login_svcpart');
176 my $svcpart = $svc_acct->cust_svc->svcpart;
177 return { error => 'Invalid user.' }
178 unless grep($_ eq $svcpart, @svcpart);
181 return { error => 'Incorrect password.' }
182 unless $svc_acct->check_password($p->{'password'});
189 'svcnum' => $svc_x->svcnum,
192 my $cust_svc = $svc_x->cust_svc;
193 my $cust_pkg = $cust_svc->cust_pkg;
195 my $cust_main = $cust_pkg->cust_main;
196 $session->{'custnum'} = $cust_main->custnum;
197 if ( $conf->exists('pkg-balances') ) {
198 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
199 $cust_main->ncancelled_pkgs;
200 $session->{'pkgnum'} = $cust_pkg->pkgnum
201 if scalar(@cust_pkg) > 1;
205 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
206 #return { error => 'Only primary user may log in.' }
207 # if $conf->exists('selfservice_server-primary_only')
208 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
209 my $part_pkg = $cust_pkg->part_pkg;
210 return { error => 'Only primary user may log in.' }
211 if $conf->exists('selfservice_server-primary_only')
212 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
216 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
217 } until ( ! defined _cache->get($session_id) ); #just in case
219 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
220 _cache->set( $session_id, $session, $timeout );
222 return { 'error' => '',
223 'session_id' => $session_id,
229 if ( $p->{'session_id'} ) {
230 _cache->remove($p->{'session_id'});
231 return { %{ skin_info($p) }, 'error' => '' };
233 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
240 my $conf = new FS::Conf;
242 my $info = skin_info($p);
244 use vars qw( $cust_paybys ); #cache for performance
245 unless ( $cust_paybys ) {
247 my %cust_paybys = map { $_ => 1 }
248 map { FS::payby->payby2payment($_) }
249 $conf->config('signup_server-payby');
251 $cust_paybys = [ keys %cust_paybys ];
254 $info->{'cust_paybys'} = $cust_paybys;
256 my($context, $session, $custnum) = _custoragent_session_custnum($p);
257 return { 'error' => $session } if $context eq 'error';
259 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
260 or return { 'error' => "unknown custnum $custnum" };
262 $info->{hide_payment_fields} =
265 if ( FS::payby->realtime($_) ) {
266 $pg = $cust_main->agent->payment_gateway(
267 'method' => FS::payby->payby2bop($_),
271 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
273 @{ $info->{cust_paybys} }
276 $info->{'self_suspend_reason'} =
277 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
280 'custnum' => $custnum,
281 'access_pkgnum' => $session->{'pkgnum'},
282 'access_svcnum' => $session->{'svcnum'},
289 my($context, $session, $custnum) = _custoragent_session_custnum($p);
290 return { 'error' => $session } if $context eq 'error';
294 my $conf = new FS::Conf;
295 if ($conf->exists('cust_main-require_address2')) {
296 $return{'require_address2'} = '1';
298 $return{'require_address2'} = '';
301 if ( $custnum ) { #customer record
303 my $search = { 'custnum' => $custnum };
304 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
305 my $cust_main = qsearchs('cust_main', $search )
306 or return { 'error' => "unknown custnum $custnum" };
308 if ( $session->{'pkgnum'} ) {
309 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
311 $return{balance} = $cust_main->balance;
314 $return{tickets} = [ ($cust_main->tickets) ];
316 unless ( $session->{'pkgnum'} ) {
319 invnum => $_->invnum,
320 date => time2str("%b %o, %Y", $_->_date),
323 } $cust_main->open_cust_bill;
324 $return{open_invoices} = \@open;
327 $return{small_custview} =
328 small_custview( $cust_main,
329 scalar($conf->config('countrydefault')),
330 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
333 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
335 for (@cust_main_editable_fields) {
336 $return{$_} = $cust_main->get($_);
339 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
340 $return{payinfo} = $cust_main->paymask;
341 @return{'month', 'year'} = $cust_main->paydate_monthyear;
344 $return{'invoicing_list'} =
345 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
346 $return{'postal_invoicing'} =
347 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
349 if (scalar($conf->config('support_packages'))) {
350 my @support_services = ();
351 foreach ($cust_main->support_services) {
352 my $seconds = $_->svc_x->seconds;
353 my $time_remaining = (($seconds < 0) ? '-' : '' ).
354 int(abs($seconds)/3600)."h".
355 sprintf("%02d",(abs($seconds)%3600)/60)."m";
356 my $cust_pkg = $_->cust_pkg;
359 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
360 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
361 push @support_services, { svcnum => $_->svcnum,
362 time => $time_remaining,
367 $return{support_services} = \@support_services;
370 if ( $conf->config('prepayment_discounts-credit_type') ) {
372 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
375 } elsif ( $session->{'svcnum'} ) { #no customer record
377 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
378 or die "unknown svcnum";
379 $return{name} = $svc_acct->email;
383 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
387 return { 'error' => '',
388 'custnum' => $custnum,
396 my $session = _cache->get($p->{'session_id'})
397 or return { 'error' => "Can't resume session" }; #better error message
399 my $custnum = $session->{'custnum'}
400 or return { 'error' => "no customer record" };
402 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
403 or return { 'error' => "unknown custnum $custnum" };
405 my $new = new FS::cust_main { $cust_main->hash };
406 $new->set( $_ => $p->{$_} )
407 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
410 if (exists($p->{'payby'})) {
411 $p->{'payby'} =~ /^([A-Z]{4})$/
412 or return { 'error' => "illegal_payby " . $p->{'payby'} };
416 if ( $payby =~ /^(CARD|DCRD)$/ ) {
418 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
420 if ( $new->payinfo eq $cust_main->paymask ) {
421 $new->payinfo($cust_main->payinfo);
423 $new->payinfo($p->{'payinfo'});
426 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
428 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
431 $p->{'payinfo1'} =~ /^([\dx]+)$/
432 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
434 $p->{'payinfo2'} =~ /^([\dx]+)$/
435 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
437 $payinfo = $payinfo1. '@'. $payinfo2;
439 $new->payinfo( ($payinfo eq $cust_main->paymask)
440 ? $cust_main->payinfo
444 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
446 } elsif ( $payby =~ /^(BILL)$/ ) {
448 } elsif ( $payby ) { #notyet ready
449 return { 'error' => "unknown payby $payby" };
453 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
454 #false laziness with httemplate/edit/process/cust_main.cgi
455 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
456 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
458 @invoicing_list = $cust_main->invoicing_list;
461 my $error = $new->replace($cust_main, \@invoicing_list);
462 return { 'error' => $error } if $error;
465 return { 'error' => '' };
470 my $session = _cache->get($p->{'session_id'})
471 or return { 'error' => "Can't resume session" }; #better error message
477 my $conf = new FS::Conf;
478 use vars qw($payment_info); #cache for performance
479 unless ( $payment_info ) {
481 my %states = map { $_->state => 1 }
482 qsearch('cust_main_county', {
483 'country' => $conf->config('countrydefault') || 'US'
486 my %cust_paybys = map { $_ => 1 }
487 map { FS::payby->payby2payment($_) }
488 $conf->config('signup_server-payby');
490 my @cust_paybys = keys %cust_paybys;
494 #list all counties/states/countries
495 'cust_main_county' =>
496 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
498 #shortcut for one-country folks
500 [ sort { $a cmp $b } keys %states ],
502 'card_types' => card_types(),
504 'paytypes' => [ @FS::cust_main::paytypes ],
506 'paybys' => [ $conf->config('signup_server-payby') ],
507 'cust_paybys' => \@cust_paybys,
509 'stateid_label' => FS::Msgcat::_gettext('stateid'),
510 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
512 'show_ss' => $conf->exists('show_ss'),
513 'show_stateid' => $conf->exists('show_stateid'),
514 'show_paystate' => $conf->exists('show_bankstate'),
516 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
525 my %return = %$payment_info;
527 my $custnum = $session->{'custnum'};
529 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
530 or return { 'error' => "unknown custnum $custnum" };
532 $return{hide_payment_fields} =
535 if ( FS::payby->realtime($_) ) {
536 $pg = $cust_main->agent->payment_gateway(
537 'method' => FS::payby->payby2bop($_),
541 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
543 @{ $return{cust_paybys} }
546 $return{balance} = $cust_main->balance; #XXX pkg-balances?
548 $return{payname} = $cust_main->payname
549 || ( $cust_main->first. ' '. $cust_main->get('last') );
551 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
553 $return{payby} = $cust_main->payby;
554 $return{stateid_state} = $cust_main->stateid_state;
556 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
557 $return{card_type} = cardtype($cust_main->payinfo);
558 $return{payinfo} = $cust_main->paymask;
560 @return{'month', 'year'} = $cust_main->paydate_monthyear;
564 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
565 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
566 $return{payinfo1} = $payinfo1;
567 $return{payinfo2} = $payinfo2;
568 $return{paytype} = $cust_main->paytype;
569 $return{paystate} = $cust_main->paystate;
573 if ( $conf->config('prepayment_discounts-credit_type') ) {
575 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
578 #doubleclick protection
580 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
582 return { 'error' => '',
588 #some false laziness with httemplate/process/payment.cgi - look there for
589 #ACH and CVV support stuff
590 sub process_payment {
594 my $session = _cache->get($p->{'session_id'})
595 or return { 'error' => "Can't resume session" }; #better error message
599 my $custnum = $session->{'custnum'};
601 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
602 or return { 'error' => "unknown custnum $custnum" };
604 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
605 or return { 'error' => gettext('illegal_amount') };
607 return { error => 'Amount must be greater than 0' } unless $amount > 0;
609 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
610 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
611 my $discount_term = $1;
613 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
614 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
617 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
618 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
621 $p->{'payby'} ||= 'CARD';
622 $p->{'payby'} =~ /^([A-Z]{4})$/
623 or return { 'error' => "illegal_payby " . $p->{'payby'} };
626 #false laziness w/process/payment.cgi
629 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
631 $p->{'payinfo1'} =~ /^([\dx]+)$/
632 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
634 $p->{'payinfo2'} =~ /^([\dx]+)$/
635 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
637 $payinfo = $payinfo1. '@'. $payinfo2;
639 $payinfo = $cust_main->payinfo
640 if $cust_main->paymask eq $payinfo;
642 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
644 $payinfo = $p->{'payinfo'};
646 #more intelligent mathing will be needed here if you change
647 #card_masking_method and don't remove existing paymasks
648 $payinfo = $cust_main->payinfo
649 if $cust_main->paymask eq $payinfo;
652 $payinfo =~ /^(\d{13,16})$/
653 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
657 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
658 return { 'error' => gettext('unknown_card_type') }
659 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
661 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
662 if ( cardtype($payinfo) eq 'American Express card' ) {
663 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
664 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
667 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
668 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
674 die "unknown payby $payby";
678 'CARD' => [ qw( paystart_month paystart_year payissue payip
679 address1 address2 city state zip country ) ],
680 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
683 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
685 'payinfo' => $payinfo,
686 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
687 'payname' => $payname,
688 'paybatch' => $paybatch, #this doesn't actually do anything
690 'pkgnum' => $session->{'pkgnum'},
691 'discount_term' => $discount_term,
692 map { $_ => $p->{$_} } @{ $payby2fields{$payby} }
694 return { 'error' => $error } if $error;
696 $cust_main->apply_payments;
698 if ( $p->{'save'} ) {
699 my $new = new FS::cust_main { $cust_main->hash };
700 if ($payby eq 'CARD' || $payby eq 'DCRD') {
701 $new->set( $_ => $p->{$_} )
702 foreach qw( payname paystart_month paystart_year payissue payip
703 address1 address2 city state zip country );
704 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
705 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
706 $new->set( $_ => $p->{$_} )
707 foreach qw( payname payip paytype paystate
708 stateid stateid_state );
709 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
711 $new->set( 'payinfo' => $cust_main->card_token || $payinfo );
712 $new->set( 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01' );
713 my $error = $new->replace($cust_main);
715 #no, this causes customers to process their payments again
716 #return { 'error' => $error };
717 #XXX just warn verosely for now so i can figure out how these happen in
718 # the first place, eventually should redirect them to the "change
719 #address" page but indicate the payment did process??
720 delete($p->{'payinfo'}); #don't want to log this!
721 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
722 "NEW: ". Dumper($new)."\n".
723 "OLD: ". Dumper($cust_main)."\n".
724 "PACKET: ". Dumper($p)."\n";
731 return { 'error' => '' };
735 sub realtime_collect {
738 my $session = _cache->get($p->{'session_id'})
739 or return { 'error' => "Can't resume session" }; #better error message
741 my $custnum = $session->{'custnum'};
743 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
744 or return { 'error' => "unknown custnum $custnum" };
746 my $error = $cust_main->realtime_collect(
747 'method' => $p->{'method'},
748 'pkgnum' => $session->{'pkgnum'},
749 'session_id' => $p->{'session_id'},
752 return { 'error' => $error } unless ref( $error );
754 my $amount = $session->{'pkgnum'}
755 ? $cust_main->balance_pkgnum( $session->{'pkgnum'} )
756 : $cust_main->balance;
758 return { 'error' => '', amount => $amount, %$error };
761 sub process_payment_order_pkg {
764 my $hr = process_payment($p);
765 return $hr if $hr->{'error'};
770 sub process_payment_order_renew {
773 my $hr = process_payment($p);
774 return $hr if $hr->{'error'};
783 my $session = _cache->get($p->{'session_id'})
784 or return { 'error' => "Can't resume session" }; #better error message
788 my $custnum = $session->{'custnum'};
790 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
791 or return { 'error' => "unknown custnum $custnum" };
793 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
794 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
802 return { 'error' => $error } if $error;
804 return { 'error' => '',
806 'seconds' => $seconds,
807 'duration' => duration_exact($seconds),
808 'upbytes' => $upbytes,
809 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
810 'downbytes' => $downbytes,
811 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
812 'totalbytes'=> $totalbytes,
813 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
820 my $session = _cache->get($p->{'session_id'})
821 or return { 'error' => "Can't resume session" }; #better error message
823 my $custnum = $session->{'custnum'};
825 my $invnum = $p->{'invnum'};
827 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
828 'custnum' => $custnum } )
829 or return { 'error' => "Can't find invnum" };
833 return { 'error' => '',
835 'invoice_text' => join('', $cust_bill->print_text ),
836 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
844 #sessioning for this? how do we get the session id to the backend invoice
845 # template so it can add it to the link, blah
848 if ( $p->{'invnum'} ) {
849 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
850 or return { 'error' => 'unknown invnum' };
851 $agentnum = $cust_bill->cust_main->agentnum;
854 my $templatename = $p->{'template'} || $p->{'templatename'};
856 #false laziness-ish w/view/cust_bill-logo.cgi
858 my $conf = new FS::Conf;
859 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
860 $templatename = "_$1";
865 my $filename = "logo$templatename.png";
867 return { 'error' => '',
868 'logo' => $conf->config_binary($filename, $agentnum),
869 'content_type' => 'image/png', #should allow gif, jpg too
876 my $session = _cache->get($p->{'session_id'})
877 or return { 'error' => "Can't resume session" }; #better error message
879 my $custnum = $session->{'custnum'};
881 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
882 or return { 'error' => "unknown custnum $custnum" };
884 my @cust_bill = $cust_main->cust_bill;
886 return { 'error' => '',
887 'invoices' => [ map { { 'invnum' => $_->invnum,
888 '_date' => $_->_date,
889 'date' => time2str("%b %o, %Y", $_->_date),
898 my $session = _cache->get($p->{'session_id'})
899 or return { 'error' => "Can't resume session" }; #better error message
901 my $custnum = $session->{'custnum'};
903 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
904 or return { 'error' => "unknown custnum $custnum" };
906 my @errors = $cust_main->cancel( 'quiet'=>1 );
908 my $error = scalar(@errors) ? join(' / ', @errors) : '';
910 return { 'error' => $error };
917 my($context, $session, $custnum) = _custoragent_session_custnum($p);
918 return { 'error' => $session } if $context eq 'error';
920 my $search = { 'custnum' => $custnum };
921 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
922 my $cust_main = qsearchs('cust_main', $search )
923 or return { 'error' => "unknown custnum $custnum" };
925 my $conf = new FS::Conf;
927 # the duplication below is necessary:
928 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
929 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
930 # non-backwards-compatible change breaking the software of anyone using the API
931 # instead of the stock selfservice
932 # 2. to return cancelled packages as well - for wholesale and non-wholesale
933 if( $conf->exists('selfservice_server-view-wholesale') ) {
934 return { 'svcnum' => $session->{'svcnum'},
935 'custnum' => $custnum,
936 'cust_pkg' => [ map {
938 part_pkg => [ map $_->hashref, $_->part_pkg ],
940 [ map $_->hashref, $_->available_part_svc ],
942 [ map { my $ref = { $_->hash,
943 label => [ $_->label ],
945 $ref->{_password} = $_->svc_x->_password
946 if $context eq 'agent'
947 && $conf->exists('agent-showpasswords')
948 && $_->part_svc->svcdb eq 'svc_acct';
953 } $cust_main->cust_pkg
956 small_custview( $cust_main, $conf->config('countrydefault') ),
957 'wholesale_view' => 1,
958 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
959 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
963 { 'svcnum' => $session->{'svcnum'},
964 'custnum' => $custnum,
965 'cust_pkg' => [ map {
969 [ map $_->hashref, $_->available_part_svc ],
971 [ map { my $ref = { $_->hash,
972 label => [ $_->label ],
974 $ref->{_password} = $_->svc_x->_password
975 if $context eq 'agent'
976 && $conf->exists('agent-showpasswords')
977 && $_->part_svc->svcdb eq 'svc_acct';
982 } $cust_main->ncancelled_pkgs
985 small_custview( $cust_main, $conf->config('countrydefault') ),
993 my($context, $session, $custnum) = _custoragent_session_custnum($p);
994 return { 'error' => $session } if $context eq 'error';
996 my $search = { 'custnum' => $custnum };
997 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
998 my $cust_main = qsearchs('cust_main', $search )
999 or return { 'error' => "unknown custnum $custnum" };
1002 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1003 foreach my $cust_pkg ( $p->{'ncancelled'}
1004 ? $cust_main->ncancelled_pkgs
1005 : $cust_main->unsuspended_pkgs ) {
1006 next if $session->{'pkgnum'} && $cust_pkg->pkgnum != $session->{'pkgnum'};
1007 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1009 if ( $p->{'svcdb'} ) {
1010 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1012 : ref($p->{'svcdb'}) eq 'ARRAY'
1013 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1014 : { $p->{'svcdb'} => 1 };
1015 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1018 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1022 'svcnum' => $session->{'svcnum'},
1023 'custnum' => $custnum,
1026 my $svc_x = $_->svc_x;
1027 my($label, $value) = $_->label;
1028 my $svcdb = $_->part_svc->svcdb;
1029 my $part_pkg = $_->cust_pkg->part_pkg;
1032 'svcnum' => $_->svcnum,
1038 if ( $svcdb eq 'svc_acct' ) {
1041 'username' => $svc_x->username,
1042 'email' => $svc_x->email,
1043 'seconds' => $svc_x->seconds,
1044 'upbytes' => display_bytecount($svc_x->upbytes),
1045 'downbytes' => display_bytecount($svc_x->downbytes),
1046 'totalbytes' => display_bytecount($svc_x->totalbytes),
1048 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1049 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1050 'recharge_upbytes' =>
1051 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1052 'recharge_downbytes' =>
1053 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1054 'recharge_totalbytes' =>
1055 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1059 } elsif ( $svcdb eq 'svc_phone' ) {
1073 sub _list_svc_usage {
1074 my($svc_acct, $begin, $end) = @_;
1076 foreach my $part_export (
1077 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1078 qw( sqlradius sqlradius_withdomain )
1080 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1085 sub list_svc_usage {
1086 _usage_details(\&_list_svc_usage, @_);
1089 sub _list_support_usage {
1090 my($svc_acct, $begin, $end) = @_;
1092 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1093 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1095 push @usage, { 'seconds' => $_->seconds,
1096 'support' => $_->support,
1097 '_date' => $_->_date,
1098 'id' => $_->transaction_id,
1099 'creator' => $_->creator,
1100 'subject' => $_->subject,
1101 'status' => $_->status,
1102 'ticketid' => $_->ticketid,
1108 sub list_support_usage {
1109 _usage_details(\&_list_support_usage, @_);
1112 sub _list_cdr_usage {
1113 my($svc_phone, $begin, $end) = @_;
1114 map [ $_->downstream_csv('format' => 'default') ], #XXX config for format
1115 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1118 sub list_cdr_usage {
1120 _usage_details( \&_list_cdr_usage, $p,
1121 'svcdb' => 'svc_phone',
1125 sub _usage_details {
1126 my($callback, $p, %opt) = @_;
1128 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1129 return { 'error' => $session } if $context eq 'error';
1131 my $search = { 'svcnum' => $p->{'svcnum'} };
1132 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1134 my $svcdb = $opt{'svcdb'} || 'svc_acct';
1136 my $svc_x = qsearchs( $svcdb, $search );
1137 return { 'error' => 'No service selected in list_svc_usage' }
1140 my $header = $svcdb eq 'svc_phone'
1141 ? [ split(',', FS::cdr::invoice_header('default') ) ] #XXX
1144 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
1145 my $freq = $cust_pkg->part_pkg->freq;
1146 my $start = $cust_pkg->setup;
1147 #my $end = $cust_pkg->bill; # or time?
1150 unless ( $p->{beginning} ) {
1151 $p->{beginning} = $cust_pkg->last_bill;
1152 $p->{ending} = $end;
1155 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending});
1157 #kinda false laziness with FS::cust_main::bill, but perhaps
1158 #we should really change this bit to DateTime and DateTime::Duration
1160 #change this bit to use Date::Manip? CAREFUL with timezones (see
1161 # mailing list archive)
1162 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
1163 (localtime($p->{ending}) )[0,1,2,3,4,5];
1164 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
1165 (localtime($p->{beginning}) )[0,1,2,3,4,5];
1167 if ( $freq =~ /^\d+$/ ) {
1169 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
1171 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
1172 } elsif ( $freq =~ /^(\d+)w$/ ) {
1174 $nmday += $weeks * 7;
1175 $pmday -= $weeks * 7;
1176 } elsif ( $freq =~ /^(\d+)d$/ ) {
1180 } elsif ( $freq =~ /^(\d+)h$/ ) {
1185 return { 'error' => "unparsable frequency: ". $freq };
1188 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
1189 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
1193 'svcnum' => $p->{svcnum},
1194 'beginning' => $p->{beginning},
1195 'ending' => $p->{ending},
1196 'previous' => ($previous > $start) ? $previous : $start,
1197 'next' => ($next < $end) ? $next : $end,
1198 'header' => $header,
1206 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1207 return { 'error' => $session } if $context eq 'error';
1209 my $search = { 'custnum' => $custnum };
1210 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1211 my $cust_main = qsearchs('cust_main', $search )
1212 or return { 'error' => "unknown custnum $custnum" };
1214 my $status = $cust_main->status;
1215 #false laziness w/ClientAPI/Signup.pm
1217 my $cust_pkg = new FS::cust_pkg ( {
1218 'custnum' => $custnum,
1219 'pkgpart' => $p->{'pkgpart'},
1221 my $error = $cust_pkg->check;
1222 return { 'error' => $error } if $error;
1225 unless ( $p->{'svcpart'} eq 'none' ) {
1229 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
1231 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1232 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
1233 $svcdb = $part_svc->svcdb;
1235 $svcdb = 'svc_acct';
1237 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
1240 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
1241 'svc_domain' => [ qw( domain ) ],
1242 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
1243 'svc_external' => [ qw( id title ) ],
1244 'svc_pbx' => [ qw( id name ) ],
1247 my $svc_x = "FS::$svcdb"->new( {
1248 'svcpart' => $svcpart,
1249 map { $_ => $p->{$_} } @{$fields{$svcdb}}
1252 if ( $svcdb eq 'svc_acct' ) {
1255 while ( length($p->{"snarf_machine$snarfnum"}) ) {
1256 my $acct_snarf = new FS::acct_snarf ( {
1257 'machine' => $p->{"snarf_machine$snarfnum"},
1258 'protocol' => $p->{"snarf_protocol$snarfnum"},
1259 'username' => $p->{"snarf_username$snarfnum"},
1260 '_password' => $p->{"snarf_password$snarfnum"},
1263 push @acct_snarf, $acct_snarf;
1265 $svc_x->child_objects( \@acct_snarf );
1268 my $y = $svc_x->setdefault; # arguably should be in new method
1269 return { 'error' => $y } if $y && !ref($y);
1271 $error = $svc_x->check;
1272 return { 'error' => $error } if $error;
1279 tie my %hash, 'Tie::RefHash';
1280 %hash = ( $cust_pkg => \@svc );
1282 $error = $cust_main->order_pkgs( \%hash, '', 'noexport' => 1 );
1283 return { 'error' => $error } if $error;
1285 my $conf = new FS::Conf;
1286 if ( $conf->exists('signup_server-realtime') ) {
1288 my $bill_error = _do_bop_realtime( $cust_main, $status );
1291 $cust_pkg->cancel('quiet'=>1);
1294 $cust_pkg->reexport;
1298 $cust_pkg->reexport;
1301 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
1303 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
1310 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1311 return { 'error' => $session } if $context eq 'error';
1313 my $search = { 'custnum' => $custnum };
1314 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1315 my $cust_main = qsearchs('cust_main', $search )
1316 or return { 'error' => "unknown custnum $custnum" };
1318 my $status = $cust_main->status;
1319 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
1320 or return { 'error' => "unknown package $p->{pkgnum}" };
1323 my $error = FS::cust_pkg::order( $custnum,
1329 my $conf = new FS::Conf;
1330 if ( $conf->exists('signup_server-realtime') ) {
1332 my $bill_error = _do_bop_realtime( $cust_main, $status );
1335 $newpkg[0]->suspend;
1338 $newpkg[0]->reexport;
1342 $newpkg[0]->reexport;
1345 return { error => '', pkgnum => $cust_pkg->pkgnum };
1349 sub order_recharge {
1352 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1353 return { 'error' => $session } if $context eq 'error';
1355 my $search = { 'custnum' => $custnum };
1356 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1357 my $cust_main = qsearchs('cust_main', $search )
1358 or return { 'error' => "unknown custnum $custnum" };
1360 my $status = $cust_main->status;
1361 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
1362 or return { 'error' => "unknown service " . $p->{'svcnum'} };
1364 my $svc_x = $cust_svc->svc_x;
1365 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
1368 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
1369 qw ( recharge_seconds recharge_upbytes recharge_downbytes
1370 recharge_totalbytes );
1371 my $amount = $part_pkg->option('recharge_amount', 1);
1373 my ($l, $v, $d) = $cust_svc->label; # blah
1374 my $pkg = "Recharge $v";
1376 my $bill_error = $cust_main->charge($amount, $pkg,
1377 "time: $vhash{seconds}, up: $vhash{upbytes}," .
1378 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
1379 $part_pkg->taxclass); #meh
1381 my $conf = new FS::Conf;
1382 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
1384 $bill_error = _do_bop_realtime( $cust_main, $status );
1389 my $error = $svc_x->recharge (\%vhash);
1390 return { 'error' => $error } if $error;
1394 my $error = $bill_error;
1395 $error ||= $svc_x->recharge (\%vhash);
1396 return { 'error' => $error } if $error;
1399 return { error => '', svc => $cust_svc->part_svc->svc };
1403 sub _do_bop_realtime {
1404 my ($cust_main, $status) = (shift, shift);
1406 my $old_balance = $cust_main->balance;
1408 my $bill_error = $cust_main->bill
1409 || $cust_main->apply_payments_and_credits
1410 || $cust_main->realtime_collect;
1412 if ( $cust_main->balance > $old_balance
1413 && $cust_main->balance > 0
1414 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
1415 1 : $status eq 'suspended' ) ) {
1416 #this makes sense. credit is "un-doing" the invoice
1417 my $conf = new FS::Conf;
1418 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
1419 'self-service decline',
1420 'reason_type' => $conf->config('signup_credit_type'),
1422 $cust_main->apply_credits( 'order' => 'newest' );
1424 return { 'error' => '_decline', 'bill_error' => $bill_error };
1433 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1434 return { 'error' => $session } if $context eq 'error';
1436 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1437 or return { 'error' => "unknown custnum $custnum" };
1439 my @cust_pkg = sort { $a->bill <=> $b->bill }
1440 grep { $_->part_pkg->freq ne '0' }
1441 $cust_main->ncancelled_pkgs;
1443 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
1445 my $total = $cust_main->balance;
1448 my $bill = $_->bill;
1449 $total += $_->part_pkg->base_recur($_, \$bill);
1450 my $renew_date = $_->part_pkg->add_freq($_->bill);
1452 'pkgnum' => $_->pkgnum,
1453 'amount' => sprintf('%.2f', $total),
1454 'bill_date' => $_->bill,
1455 'bill_date_pretty' => time2str('%x', $_->bill),
1456 'renew_date' => $renew_date,
1457 'renew_date_pretty' => time2str('%x', $renew_date),
1458 'expire_date' => $_->expire,
1459 'expire_date_pretty' => time2str('%x', $_->expire),
1464 return { 'dates' => \@array };
1468 sub payment_info_renew_info {
1470 my $renew_info = renew_info($p);
1471 my $payment_info = payment_info($p);
1472 return { %$renew_info,
1480 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1481 return { 'error' => $session } if $context eq 'error';
1483 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1484 or return { 'error' => "unknown custnum $custnum" };
1486 my $date = $p->{'date'};
1490 #freeside-daily -n -d $date fs_daily $custnum
1491 $cust_main->bill_and_collect( 'time' => $date,
1492 'invoice_time' => $now,
1493 'actual_time' => $now,
1494 'check_freq' => '1d',
1497 return { 'error' => '' };
1503 my $session = _cache->get($p->{'session_id'})
1504 or return { 'error' => "Can't resume session" }; #better error message
1506 my $custnum = $session->{'custnum'};
1508 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1509 or return { 'error' => "unknown custnum $custnum" };
1511 my $conf = new FS::Conf;
1513 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
1514 or return { 'error' => 'Permission denied' };
1516 my $pkgnum = $p->{'pkgnum'};
1518 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1519 'pkgnum' => $pkgnum, } )
1520 or return { 'error' => "unknown pkgnum $pkgnum" };
1522 my $error = $cust_pkg->suspend(reason => $reasonnum);
1523 return { 'error' => $error };
1529 my $session = _cache->get($p->{'session_id'})
1530 or return { 'error' => "Can't resume session" }; #better error message
1532 my $custnum = $session->{'custnum'};
1534 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1535 or return { 'error' => "unknown custnum $custnum" };
1537 my $pkgnum = $p->{'pkgnum'};
1539 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1540 'pkgnum' => $pkgnum, } )
1541 or return { 'error' => "unknown pkgnum $pkgnum" };
1543 my $error = $cust_pkg->cancel('quiet' => 1);
1544 return { 'error' => $error };
1548 sub provision_acct {
1550 warn "provision_acct called\n"
1553 return { 'error' => gettext('passwords_dont_match') }
1554 if $p->{'_password'} ne $p->{'_password2'};
1555 return { 'error' => gettext('empty_password') }
1556 unless length($p->{'_password'});
1558 if ($p->{'domsvc'}) {
1559 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
1560 qw ( svcpart pkgnum ) );
1561 return { 'error' => gettext('invalid_domain') }
1562 unless ($domains{$p->{'domsvc'}});
1565 warn "provision_acct calling _provision\n"
1567 _provision( 'FS::svc_acct',
1568 [qw(username _password domsvc)],
1569 [qw(username _password domsvc)],
1575 sub provision_external {
1577 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
1578 _provision( 'FS::svc_external',
1587 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
1588 warn "_provision called for $class\n"
1591 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1592 return { 'error' => $session } if $context eq 'error';
1594 my $search = { 'custnum' => $custnum };
1595 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1596 my $cust_main = qsearchs('cust_main', $search )
1597 or return { 'error' => "unknown custnum $custnum" };
1599 my $pkgnum = $p->{'pkgnum'};
1601 warn "searching for custnum $custnum pkgnum $pkgnum\n"
1603 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1604 'pkgnum' => $pkgnum,
1606 or return { 'error' => "unknown pkgnum $pkgnum" };
1608 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
1610 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
1611 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
1613 warn "creating $class record\n"
1615 my $svc_x = $class->new( {
1616 'pkgnum' => $p->{'pkgnum'},
1617 'svcpart' => $p->{'svcpart'},
1618 map { $_ => $p->{$_} } @$fields
1620 warn "inserting $class record\n"
1622 my $error = $svc_x->insert;
1625 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
1627 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
1630 my $return = { 'svc' => $part_svc->svc,
1632 map { $_ => $svc_x->get($_) } @$return_fields
1634 warn "_provision returning ". Dumper($return). "\n"
1643 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1644 return { 'error' => $session } if $context eq 'error';
1646 my $search = { 'custnum' => $custnum };
1647 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1648 my $cust_main = qsearchs('cust_main', $search )
1649 or return { 'error' => "unknown custnum $custnum" };
1651 my $pkgnum = $p->{'pkgnum'};
1653 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1654 'pkgnum' => $pkgnum,
1656 or return { 'error' => "unknown pkgnum $pkgnum" };
1658 my $svcpart = $p->{'svcpart'};
1660 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
1661 'svcpart' => $svcpart, } )
1662 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
1663 my $part_svc = $pkg_svc->part_svc;
1665 my $conf = new FS::Conf;
1668 'svc' => $part_svc->svc,
1669 'svcdb' => $part_svc->svcdb,
1670 'pkgnum' => $pkgnum,
1671 'svcpart' => $svcpart,
1672 'custnum' => $custnum,
1674 'security_phrase' => 0, #XXX !
1675 'svc_acct_pop' => [], #XXX !
1677 'init_popstate' => '',
1682 small_custview( $cust_main, $conf->config('countrydefault') ),
1688 sub unprovision_svc {
1691 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1692 return { 'error' => $session } if $context eq 'error';
1694 my $search = { 'custnum' => $custnum };
1695 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1696 my $cust_main = qsearchs('cust_main', $search )
1697 or return { 'error' => "unknown custnum $custnum" };
1699 my $svcnum = $p->{'svcnum'};
1701 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
1702 or return { 'error' => "unknown svcnum $svcnum" };
1704 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
1705 unless $cust_svc->cust_pkg->custnum == $custnum;
1707 my $conf = new FS::Conf;
1709 return { 'svc' => $cust_svc->part_svc->svc,
1710 'error' => $cust_svc->cancel,
1712 small_custview( $cust_main, $conf->config('countrydefault') ),
1717 sub myaccount_passwd {
1719 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1720 return { 'error' => $session } if $context eq 'error';
1722 return { 'error' => "New passwords don't match." }
1723 if $p->{'new_password'} ne $p->{'new_password2'};
1725 return { 'error' => 'Enter new password' }
1726 unless length($p->{'new_password'});
1728 #my $search = { 'custnum' => $custnum };
1729 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1730 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1731 my $search = " AND custnum = $1";
1732 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1734 my $svc_acct = qsearchs( {
1735 'table' => 'svc_acct',
1736 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1737 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
1738 'LEFT JOIN cust_main USING ( custnum ) ',
1739 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
1740 'extra_sql' => $search, #important
1742 or return { 'error' => "Service not found" };
1744 $svc_acct->_password($p->{'new_password'});
1745 my $error = $svc_acct->replace();
1747 my($label, $value) = $svc_acct->cust_svc->label;
1749 return { 'error' => $error,
1758 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1759 return { 'error' => $session } if $context eq 'error';
1761 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
1762 FS::TicketSystem->init();
1764 my $conf = new FS::Conf;
1765 my $queue = $p->{'queue'}
1766 || $conf->config('ticket_system-selfservice_queueid')
1767 || $conf->config('ticket_system-default_queueid');
1769 warn "$me create_ticket: creating ticket\n" if $DEBUG;
1770 my $err_or_ticket = FS::TicketSystem->create_ticket(
1771 '', #create RT session based on FS CurrentUser (fs_selfservice)
1773 'custnum' => $custnum,
1774 'svcnum' => $session->{'svcnum'},
1775 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
1778 if ( ref($err_or_ticket) ) {
1779 warn "$me create_ticket: sucessful: ". $err_or_ticket->id. "\n"
1781 return { 'error' => '',
1782 'ticket_id' => $err_or_ticket->id,
1785 warn "$me create_ticket: unsucessful: $err_or_ticket\n"
1787 return { 'error' => $err_or_ticket };
1795 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1796 return { 'error' => $session } if $context eq 'error';
1798 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
1799 FS::TicketSystem->init();
1801 if(length($p->{'reply'})) {
1802 my @err_or_res = FS::TicketSystem->correspond_ticket(
1803 '', #create RT session based on FS CurrentUser (fs_selfservice)
1804 'ticket_id' => $p->{'ticket_id'},
1805 'content' => $p->{'reply'},
1808 return { 'error' => 'unable to reply to ticket' }
1809 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
1812 warn "$me get_ticket: getting ticket\n" if $DEBUG;
1813 my $err_or_ticket = FS::TicketSystem->get_ticket(
1814 '', #create RT session based on FS CurrentUser (fs_selfservice)
1815 'ticket_id' => $p->{'ticket_id'},
1818 if ( ref($err_or_ticket) ) {
1820 # since we're bypassing the RT security/permissions model by always using
1821 # fs_selfservice as the RT user (as opposed to a requestor, which we
1822 # can't do since we want all tickets linked to a cust), we check below whether
1823 # the requested ticket was actually linked to this customer
1824 my @custs = @{$err_or_ticket->{'custs'}};
1825 my @txns = @{$err_or_ticket->{'txns'}};
1827 return { 'error' => 'no customer' } unless ( $custnum && scalar(@custs) );
1829 return { 'error' => 'invalid ticket requested' }
1830 unless grep($_ eq $custnum, @custs);
1832 warn "$me get_ticket: sucessful: \n"
1834 return { 'error' => '',
1835 'transactions' => \@txns,
1836 'ticket_id' => $p->{'ticket_id'},
1839 warn "$me create_ticket: unsucessful: $err_or_ticket\n"
1841 return { 'error' => $err_or_ticket };
1848 sub _custoragent_session_custnum {
1851 my($context, $session, $custnum);
1852 if ( $p->{'session_id'} ) {
1854 $context = 'customer';
1855 $session = _cache->get($p->{'session_id'})
1856 or return ( 'error' => "Can't resume session" ); #better error message
1857 $custnum = $session->{'custnum'};
1859 } elsif ( $p->{'agent_session_id'} ) {
1862 my $agent_cache = new FS::ClientAPI_SessionCache( {
1863 'namespace' => 'FS::ClientAPI::Agent',
1865 $session = $agent_cache->get($p->{'agent_session_id'})
1866 or return ( 'error' => "Can't resume session" ); #better error message
1867 $custnum = $p->{'custnum'};
1871 return ( 'error' => "Can't resume session" ); #better error message
1874 ($context, $session, $custnum);