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 );
9 use Digest::MD5 qw(md5_hex);
10 use Digest::SHA qw(sha512_hex);
13 use Time::Local qw(timelocal_nocheck);
14 use Business::CreditCard 0.35;
17 use Spreadsheet::WriteExcel;
18 use OLE::Storage_Lite;
19 use FS::UI::Web::small_custview qw(small_custview); #less doh
21 use FS::UI::bytecount qw( display_bytecount );
24 use FS::Record qw(qsearch qsearchs dbh);
25 use FS::Msgcat qw(gettext);
26 use FS::Misc qw(card_types money_pretty);
27 use FS::Misc::DateTime qw(parse_datetime);
29 use FS::ClientAPI_SessionCache;
41 use FS::legacy_cust_bill;
42 use FS::cust_main_county;
46 use FS::acct_rt_transaction;
50 use FS::cust_location;
53 # for code organization
54 use FS::ClientAPI::MyAccount::contact;
55 use FS::ClientAPI::MyAccount::quotation;
58 $me = '[FS::ClientAPI::MyAccount]';
60 use vars qw( @cust_main_editable_fields @location_editable_fields );
61 @cust_main_editable_fields = qw(
62 first last company daytime night fax mobile
64 ss stateid stateid_state
66 @location_editable_fields = qw(
67 address1 address2 city county state zip country
71 BEGIN { #preload to reduce time customer_info takes
72 if ( $FS::TicketSystem::system ) {
73 warn "$me: initializing ticket system\n" if $DEBUG;
74 FS::TicketSystem->init();
79 $cache ||= new FS::ClientAPI_SessionCache( {
80 'namespace' => 'FS::ClientAPI::MyAccount',
87 my($context, $session, $custnum) = _custoragent_session_custnum($p);
88 #return { 'error' => $session } if $context eq 'error';
91 if ( $context eq 'customer' && $custnum ) {
93 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
96 $sth->execute($custnum) or die $sth->errstr;
98 $agentnum = $sth->fetchrow_arrayref->[0]
99 or die "no agentnum for custnum $custnum";
101 #} elsif ( $context eq 'agent' ) {
102 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
105 $p->{'agentnum'} = $agentnum;
107 my $conf = new FS::Conf;
109 #false laziness w/Signup.pm
111 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
113 if ( $skin_info_cache_agent ) {
115 warn "$me loading cached skin info for agentnum $agentnum\n"
120 warn "$me populating skin info cache for agentnum $agentnum\n"
123 $skin_info_cache_agent = {
124 'agentnum' => $agentnum,
125 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
126 qw( company_name date_format ) ),
127 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
128 qw( body_bgcolor box_bgcolor stripe1_bgcolor stripe2_bgcolor
129 text_color link_color vlink_color hlink_color alink_color
130 font title_color title_align title_size menu_bgcolor menu_fontsize
133 'menu_disable' => [ $conf->config('selfservice-menu_disable',$agentnum) ],
134 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
135 qw( menu_skipblanks menu_skipheadings menu_nounderline no_logo enable_payment_without_balance )
137 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
138 qw( title_left_image title_right_image
139 menu_top_image menu_body_image menu_bottom_image
142 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
143 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
144 qw( head body_header body_footer company_address ) ),
145 'money_char' => $conf->config("money_char") || '$',
146 'menu' => join("\n", $conf->config("ng_selfservice-menu", $agentnum ) ) ||
149 services.php Services
150 services.php My Services
151 services_new.php Order a new service
154 personal.php Personal Information
155 password.php Change Password
158 payment_cc.php Credit Card Payment
159 payment_ach.php Electronic Check Payment
160 payment_paypal.php PayPal Payment
161 payment_webpay.php Webpay Payments
164 usage_data.php Data usage
165 usage_cdr.php Call usage
167 tickets.php Help Desk
168 tickets.php Open Tickets
169 tickets_resolved.php Resolved Tickets
170 ticket_create.php Create a new ticket
178 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
182 #{ %$skin_info_cache_agent };
183 $skin_info_cache_agent;
187 sub get_mac_address {
190 ## access radius exports acct tables to get mac
191 my @part_export = ();
193 qsearch( 'part_export', { 'exporttype' => 'sqlradius' } ),
194 qsearch( 'part_export', { 'exporttype' => 'sqlradius_withdomain' } ),
195 qsearch( 'part_export', { 'exporttype' => 'broadband_sqlradius' } ),
199 foreach my $part_export (@part_export) {
200 push @sessions, ( @{ $part_export->usage_sessions( {
205 my $mac = $sessions[0]->{'callingstationid'};
207 return { 'mac_address' => $mac, };
213 my $conf = new FS::Conf;
217 'phone_login' => $conf->exists('selfservice_server-phone_login'),
218 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
219 'banner_url' => scalar($conf->config('selfservice-login_banner_url')),
220 'banner_image_md5' =>
221 md5_hex($conf->config_binary('selfservice-login_banner_image')),
228 sub login_banner_image {
230 my $conf = new FS::Conf;
231 my $image = $conf->config_binary('selfservice-login_banner_image');
233 'md5' => md5_hex($image),
238 #false laziness w/FS::ClientAPI::passwd::passwd
242 my $conf = new FS::Conf;
246 if ( $p->{'domain'} eq 'svc_phone'
247 && $conf->exists('selfservice_server-phone_login') ) {
249 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
250 return { error => 'Number not found.' } unless $svc_phone;
253 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
254 #return { error => 'Only primary user may log in.' }
255 # if $conf->exists('selfservice_server-primary_only')
256 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
258 return { error => 'Incorrect PIN.' }
259 unless $svc_phone->check_pin($p->{'password'});
263 } elsif ( $p->{'domain'} eq 'ip_mac' ) {
265 my $mac_address = $p->{'username'};
266 $mac_address =~ s/\://g;
268 my $svc_broadband = qsearchs( 'svc_broadband', { 'mac_addr' => $mac_address } );
269 return { error => 'MAC address not found '.$p->{'username'} }
270 unless $svc_broadband;
271 $svc_x = $svc_broadband;
273 } elsif ( $p->{email}
274 && (my $contact = FS::contact->by_selfservice_email($p->{email}))
277 return { error => 'Incorrect contact password.' }
278 unless $contact->authenticate_password($p->{'password'});
280 $session->{'contactnum'} = $contact->contactnum;
282 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
283 if ( scalar(@cust_contact) == 1 ) {
284 $session->{'custnum'} = $cust_contact[0]->custnum;
285 } elsif ( scalar(@cust_contact) ) {
286 $session->{'customers'} = { map { $_->custnum => $_->cust_main->name }
290 return { error => 'No customer self-service access for contact' }; #??
295 ( $p->{username}, $p->{domain} ) = split('@', $p->{email}) if $p->{email};
297 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
298 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
300 my @svc_acct = qsearch( 'svc_acct', { 'username' => $p->{'username'},
301 'domsvc' => $svc_domain->svcnum, }
304 if ( $conf->exists('selfservice_server-login_svcpart') ) {
305 my @svcpart = $conf->config('selfservice_server-login_svcpart');
306 @svc_acct = grep { my $svcpart = $_->cust_svc->svcpart;
307 scalar( grep( $_ eq $svcpart, @svcpart ) );
312 if ( $conf->exists('selfservice_server-primary_only') ) {
315 my $cust_svc = $_->cust_svc;
316 $cust_svc->cust_pkg->part_pkg->svcpart([qw( svc_acct svc_phone )])
317 == $cust_svc->svcpart
322 return { error => 'User not found.' } unless @svc_acct;
324 return { error => 'Multiple users.' } if scalar(@svc_acct) > 1;
326 my $svc_acct = $svc_acct[0];
328 if ( $conf->exists('selfservice_server-login_svcpart') ) {
329 my @svcpart = $conf->config('selfservice_server-login_svcpart');
330 my $svcpart = $svc_acct->cust_svc->svcpart;
331 return { error => 'Invalid user.' }
332 unless grep($_ eq $svcpart, @svcpart);
335 return { error => 'Incorrect password.' }
336 unless $svc_acct->check_password($p->{'password'});
344 $session->{'svcnum'} = $svc_x->svcnum;
346 my $cust_svc = $svc_x->cust_svc;
347 my $cust_pkg = $cust_svc->cust_pkg;
349 my $cust_main = $cust_pkg->cust_main;
350 $session->{'custnum'} = $cust_main->custnum;
351 if ( $conf->exists('pkg-balances') ) {
352 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
353 $cust_main->ncancelled_pkgs;
354 $session->{'pkgnum'} = $cust_pkg->pkgnum
355 if scalar(@cust_pkg) > 1;
359 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
360 #return { error => 'Only primary user may log in.' }
361 # if $conf->exists('selfservice_server-primary_only')
362 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
363 my $part_pkg = $cust_pkg->part_pkg;
364 return { error => 'Only primary user may log in.' }
365 if $conf->exists('selfservice_server-primary_only')
366 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
372 $session_id = sha512_hex(time(). {}. rand(). $$)
373 } until ( ! defined _cache->get($session_id) ); #just in case
375 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
376 _cache->set( $session_id, $session, $timeout );
378 return { 'error' => '',
379 'session_id' => $session_id,
386 my $skin_info = skin_info($p);
387 if ( $p->{'session_id'} ) {
388 _cache->remove($p->{'session_id'});
389 return { %$skin_info, 'error' => '' };
391 return { %$skin_info, 'error' => "Can't resume session" }; #better error message
398 my($context, $session, $custnum) = _custoragent_session_custnum($p);
399 return { 'error' => $session } if $context eq 'error';
401 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
402 or return { 'error' => "Service not found" };
404 $session->{'svcnum'} = $svc_acct->svcnum;
406 my $conf = new FS::Conf;
407 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
408 _cache->set( $p->{'session_id'}, $session, $timeout );
410 return { 'error' => '' };
416 my($context, $session, $custnum) = _custoragent_session_custnum($p);
417 return { 'error' => $session } if $context eq 'error';
419 $session->{'custnum'} = $p->{'custnum'}
420 if exists $session->{'customers'}{ $p->{'custnum'} };
422 my $conf = new FS::Conf;
423 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
424 _cache->set( $p->{'session_id'}, $session, $timeout );
426 return { 'error' => '',
427 %{ customer_info( { session_id=>$p->{'session_id'} } ) },
431 sub payment_gateway {
433 # takes a cust_main and a cust_payby entry, returns the payment_gateway
434 my $conf = new FS::Conf;
435 my $cust_main = shift;
436 my $cust_payby = shift;
437 return '' if ! FS::payby->realtime($cust_payby);
438 my $pg = $cust_main->agent->payment_gateway(
439 'method' => FS::payby->payby2bop($cust_payby),
448 my $conf = new FS::Conf;
450 my $info = skin_info($p);
452 use vars qw( $cust_paybys ); #cache for performance
453 unless ( $cust_paybys ) {
455 my %cust_paybys = map { $_ => 1 }
456 map { FS::payby->payby2payment($_) }
457 $conf->config('signup_server-payby');
459 $cust_paybys = [ keys %cust_paybys ];
462 $info->{'cust_paybys'} = $cust_paybys;
464 my($context, $session, $custnum) = _custoragent_session_custnum($p);
465 return { 'error' => $session } if $context eq 'error';
467 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
469 $info->{'hide_payment_fields'} = [
471 my $pg = $cust_main && payment_gateway($cust_main, $_);
472 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
473 } @{ $info->{cust_paybys} }
476 $info->{'self_suspend_reason'} =
477 $conf->config('selfservice-self_suspend_reason',
478 $cust_main ? $cust_main->agentnum : ''
481 $info->{'edit_ticket_subject'} =
482 $conf->exists('ticket_system-selfservice_edit_subject') &&
483 $cust_main && $cust_main->edit_subject;
485 $info->{'timeout'} = $conf->config('selfservice-timeout') || 3600;
487 $info->{'hide_usage'} = $conf->exists('selfservice_hide-usage');
490 'custnum' => $custnum,
491 'access_pkgnum' => $session->{'pkgnum'},
492 'access_svcnum' => $session->{'svcnum'},
499 my($context, $session, $custnum) = _custoragent_session_custnum($p);
500 return { 'error' => $session } if $context eq 'error';
504 my $conf = new FS::Conf;
505 $return{'require_address2'} = $conf->exists('cust_main-require_address2');
507 # if ( $FS::TicketSystem::system ) {
508 # warn "$me customer_info: initializing ticket system\n" if $DEBUG;
509 # FS::TicketSystem->init();
512 if ( $custnum ) { #customer record
514 %return = ( %return, %{ customer_info_short($p) } );
516 #redundant with customer_info_short, but we need it for several things below
517 my $search = { 'custnum' => $custnum };
518 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
519 my $cust_main = qsearchs('cust_main', $search )
520 or return { 'error' => "customer_info: unknown custnum $custnum" };
522 my $list_tickets = list_tickets($p);
523 $return{'tickets'} = $list_tickets->{'tickets'};
525 if ( $session->{'pkgnum'} ) {
526 #XXX open invoices in the pkg-balances case
528 $return{'money_char'} = $conf->config("money_char") || '$';
531 invnum => $_->invnum,
532 date => time2str("%b %o, %Y", $_->_date),
534 charged => $_->charged,
536 } $cust_main->open_cust_bill;
537 $return{open_invoices} = \@open;
539 my $sql = 'SELECT MAX(_date) FROM cust_bill WHERE custnum = ?';
540 my $sth = dbh->prepare($sql) or die dbh->errstr;
541 $sth->execute($custnum) or die $sth->errstr;
542 $return{'last_invoice_date'} = $sth->fetchrow_arrayref->[0];
543 $return{'last_invoice_date_pretty'} =
544 time2str('%m/%d/%Y', $return{'last_invoice_date'} );
547 #customer_info_short always has nobalance on..
548 $return{small_custview} =
549 small_custview( $cust_main,
550 $return{countrydefault},
551 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
554 $return{has_ship_address} = $cust_main->has_ship_address;
555 $return{status} = $cust_main->status_label; #$cust_main->status; #better to break anyone obscurely testing for strings in self-service than to have to upgrade every front-end to get the new status to display
556 $return{statuscolor} = $cust_main->statuscolor;
557 $return{status_label} = $cust_main->status_label;
559 # compatibility: some places in selfservice use this to determine
560 # if there's a ship address
561 if ( $return{has_ship_address} ) {
562 $return{ship_last} = $cust_main->last;
563 $return{ship_first} = $cust_main->first;
566 if (scalar($conf->config('support_packages'))) {
567 my @support_services = ();
568 foreach ($cust_main->support_services) {
569 my $seconds = $_->svc_x->seconds || 0;
570 my $time_remaining = (($seconds < 0) ? '-' : '' ).
571 int(abs($seconds)/3600)."h".
572 sprintf("%02d",(abs($seconds)%3600)/60)."m";
573 my $cust_pkg = $_->cust_pkg;
576 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
577 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
578 push @support_services, { svcnum => $_->svcnum,
579 time => $time_remaining,
584 $return{support_services} = \@support_services;
587 if ( $conf->config('prepayment_discounts-credit_type') ) {
589 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
592 } elsif ( $session->{'svcnum'} ) { #no customer record
594 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
595 or die "unknown svcnum";
596 $return{name} = $svc_acct->email;
600 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
604 return { 'error' => '',
605 'custnum' => $custnum,
611 sub customer_info_short {
614 my($context, $session, $custnum) = _custoragent_session_custnum($p);
615 return { 'error' => $session } if $context eq 'error';
619 my $conf = new FS::Conf;
621 if ( $custnum ) { #customer record
623 my $search = { 'custnum' => $custnum };
624 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
625 my $cust_main = qsearchs('cust_main', $search )
626 or return { 'error' => "customer_info_short: unknown custnum $custnum" };
628 $return{display_custnum} = $cust_main->display_custnum;
629 $return{max_invnum} = $cust_main->max_invnum;
631 if ( $session->{'pkgnum'} ) {
632 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
633 #next_bill_date from cust_pkg?
635 $return{balance} = $cust_main->balance;
636 $return{next_bill_date} = $cust_main->next_bill_date;
637 $return{next_bill_date_pretty} =
638 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
641 $return{balance_pretty} = money_pretty($return{balance});
643 $return{countrydefault} = scalar($conf->config('countrydefault'));
645 $return{small_custview} =
646 small_custview( $cust_main,
647 $return{countrydefault},
651 $return{first} = $cust_main->first;
652 $return{'last'} = $cust_main->get('last');
653 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
655 #none of these are terribly expensive if we want 'em...
656 for (@cust_main_editable_fields) {
657 $return{$_} = $cust_main->get($_);
659 $return{$_} = $cust_main->masked($_) for qw/ss stateid/;
661 #maybe a little more expensive, but it should be cached by now
662 for (@location_editable_fields) {
663 $return{$_} = $cust_main->bill_location->get($_)
664 if $cust_main->bill_locationnum;
665 $return{'ship_'.$_} = $cust_main->ship_location->get($_)
666 if $cust_main->ship_locationnum;
669 my @invoicing_list = $cust_main->invoicing_list;
670 $return{'invoicing_list'} =
671 join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list );
672 $return{'postal_invoicing'} =
673 0 < ( grep { $_ eq 'POST' } @invoicing_list );
675 if ( $session->{'svcnum'} ) {
676 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
677 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
678 $return{'svcnum'} = $session->{'svcnum'};
681 } elsif ( $session->{'svcnum'} ) { #no customer record
683 #uuh, not supproted yet... die?
684 return { 'error' => 'customer_info_short not yet supported as agent' };
688 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
692 # this is here because this routine is called by both fs_ and ng_ main pages, where it appears
693 # it is not customer-specific, though it is only shown to authenticated customers
694 # it is not currently agent-specific, though at some point it might be
695 $return{'announcement'} = join(' ',$conf->config('selfservice-announcement')) || '';
697 return { 'error' => '',
698 'custnum' => $custnum,
703 sub customer_recurring {
706 my($context, $session, $custnum) = _custoragent_session_custnum($p);
707 return { 'error' => $session } if $context eq 'error';
711 my $conf = new FS::Conf;
713 my $search = { 'custnum' => $custnum };
714 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
715 my $cust_main = qsearchs('cust_main', $search )
716 or return { 'error' => "customer_info_short: unknown custnum $custnum" };
718 $return{'display_recurring'} = [ $cust_main->display_recurring ];
720 return { 'error' => '',
721 'custnum' => $custnum,
726 sub billing_history {
729 my($context, $session, $custnum) = _custoragent_session_custnum($p);
730 return { 'error' => $session } if $context eq 'error';
732 return { 'error' => 'No customer' } unless $custnum;
734 my $search = { 'custnum' => $custnum };
735 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
736 my $cust_main = qsearchs('cust_main', $search )
737 or return { 'error' => "unknown custnum $custnum" };
741 if ( $session->{'pkgnum'} ) {
742 #$return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
743 #next_bill_date from cust_pkg?
744 return { 'error' => 'No history for package' };
747 $return{balance} = $cust_main->balance;
748 $return{balance_pretty} = money_pretty($return{balance});
749 $return{next_bill_date} = $cust_main->next_bill_date;
750 $return{next_bill_date_pretty} =
751 $return{next_bill_date} ? time2str('%m/%d/%Y', $return{next_bill_date} )
754 my $conf = new FS::Conf;
756 $return{'history'} = [
757 $cust_main->payment_history(
758 'line_items' => $conf->exists('selfservice-billing_history-line_items'),
763 $return{'money_char'} = $conf->config("money_char") || '$',
771 my $session = _cache->get($p->{'session_id'})
772 or return { 'error' => "Can't resume session" }; #better error message
774 my $custnum = $session->{'custnum'}
775 or return { 'error' => "no customer record" };
777 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
778 or return { 'error' => "unknown custnum $custnum" };
780 my $conf = new FS::Conf;
781 if (($p->{payby} eq "CHEK" || $p->{payby} eq "DCHEK") && $conf->exists('selfservice-ACH_info_readonly')) {
782 return { 'error' => "You do not have authority to add a bank account" };
785 my $new = new FS::cust_main { $cust_main->hash };
787 $new->set( $_ => $p->{$_} )
788 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
790 if ( exists($p->{address1}) ) {
791 my $bill_location = FS::cust_location->new({
792 map { $_ => $p->{$_} } @location_editable_fields
794 # if this is unchanged from before, cust_main::replace will ignore it
795 $new->set('bill_location' => $bill_location);
798 if ( exists($p->{ship_address1}) ) {
799 my $ship_location = FS::cust_location->new({
800 map { $_ => $p->{"ship_$_"} } @location_editable_fields
802 if ( !grep { length($p->{"ship_$_"}) } @location_editable_fields ) {
803 # Selfservice unfortunately tries to indicate "same as billing
804 # address" by sending all fields empty. Did this ever work?
805 $ship_location = $cust_main->bill_location;
807 $new->set('ship_location' => $ship_location);
809 # but if it hasn't been passed in at all, leave ship_location alone--
810 # DON'T change it to match bill_location.
813 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
814 #false laziness with httemplate/edit/process/cust_main.cgi
815 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
816 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
818 @invoicing_list = $cust_main->invoicing_list;
821 my $error = $new->replace($cust_main, \@invoicing_list);
822 return { 'error' => $error } if $error;
825 return { 'error' => '' };
830 my $session = _cache->get($p->{'session_id'})
831 or return { 'error' => "Can't resume session" }; #better error message
837 my $conf = new FS::Conf;
838 use vars qw($payment_info); #cache for performance
839 unless ( $payment_info ) {
841 my %states = map { $_->state => 1 }
842 qsearch('cust_main_county', {
843 'country' => $conf->config('countrydefault') || 'US'
846 my %cust_paybys = map { $_ => 1 }
847 map { FS::payby->payby2payment($_) }
848 $conf->config('signup_server-payby');
850 my @cust_paybys = keys %cust_paybys;
854 #list all counties/states/countries
855 'cust_main_county' =>
856 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
858 #shortcut for one-country folks
860 [ sort { $a cmp $b } keys %states ],
862 'card_types' => card_types(),
864 'withcvv' => $conf->exists('selfservice-require_cvv'), #or enable optional cvv?
865 'require_cvv' => $conf->exists('selfservice-require_cvv'),
866 'onfile_require_cvv' => $conf->exists('selfservice-onfile_require_cvv'),
868 'paytypes' => [ FS::cust_payby::paytypes ],
870 'paybys' => [ $conf->config('signup_server-payby') ],
871 'cust_paybys' => \@cust_paybys,
873 'stateid_label' => FS::Msgcat::_gettext('stateid'),
874 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
876 'show_ss' => $conf->exists('show_ss'),
877 'show_stateid' => $conf->exists('show_stateid'),
878 'show_paystate' => $conf->exists('show_bankstate'),
880 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
881 'ach_read_only' => $conf->exists('selfservice-ACH_info_readonly'),
891 my %return = %$payment_info;
893 delete $return{'cust_main_county'} if $p->{'omit_cust_main_county'};
895 my $custnum = $session->{'custnum'};
897 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
898 or return { 'error' => "unknown custnum $custnum" };
900 $return{'hide_payment_fields'} = [
902 my $pg = payment_gateway($cust_main, $_);
903 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
904 } @{ $return{cust_paybys} }
907 $return{balance} = $cust_main->balance; #XXX pkg-balances?
909 $return{$_} = $cust_main->bill_location->get($_)
910 for qw(address1 address2 city state zip);
912 # look for stored cust_payby info
913 # only if we've been given a clear payment_payby (to avoid payname conflicts)
914 if ($p->{'payment_payby'} =~ /^(CARD|CHEK)$/) {
915 my @search_payby = ($p->{'payment_payby'} eq 'CARD') ? ('CARD','DCRD') : ('CHEK','DCHK');
916 my ($cust_payby) = $cust_main->cust_payby(@search_payby);
918 $return{payname} = $cust_payby->payname
919 || ( $cust_main->first. ' '. $cust_main->get('last') );
920 $return{custpaybynum} = $cust_payby->custpaybynum;
922 if ( $cust_payby->payby =~ /^(CARD|DCRD)$/ ) {
923 $return{card_type} = cardtype($cust_payby->payinfo);
924 $return{payinfo} = $cust_payby->paymask;
926 @return{'month', 'year'} = $cust_payby->paydate_monthyear;
930 if ( $cust_payby->payby =~ /^(CHEK|DCHK)$/ ) {
931 my ($payinfo1, $payinfo2) = split '@', $cust_payby->paymask;
932 $return{payinfo1} = $payinfo1;
933 $return{payinfo2} = $payinfo2;
934 $return{paytype} = $cust_payby->paytype;
935 $return{paystate} = $cust_payby->paystate;
936 $return{payname} = $cust_payby->payname; # override 'first/last name' default from above, if any. Is instution-name here. (#15819)
941 if ( $conf->config('prepayment_discounts-credit_type') ) {
943 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
946 #doubleclick protection
948 $return{payunique} = "webui-MyAccount-$_date-$$-". rand() * 2**32; #new
949 $return{paybatch} = $return{payunique}; #back compat
951 $return{credit_card_surcharge_percentage} = $conf->config('credit-card-surcharge-percentage', $cust_main->agentnum);
952 $return{credit_card_surcharge_flatfee} = $conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum);
954 return { 'error' => '',
960 #some false laziness with httemplate/process/payment.cgi - look there for
961 #ACH and CVV support stuff
963 sub validate_payment {
966 my $session = _cache->get($p->{'session_id'})
967 or return { 'error' => "Can't resume session" }; #better error message
969 my $custnum = $session->{'custnum'};
971 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
972 or return { 'error' => "unknown custnum $custnum" };
974 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
975 or return { 'error' => gettext('illegal_amount') };
977 return { error => 'Amount must be greater than 0' } unless $amount > 0;
979 #false laziness w/tr-amount_fee.html, but we don't want selfservice users
980 #changing the hidden form values
981 my $conf = new FS::Conf;
982 my $fee_display = $conf->config('selfservice_process-display') || 'add';
983 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
984 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
985 if ( $fee_display eq 'add'
987 and ! $fee_skip_first || scalar($cust_main->cust_pay)
990 my $fee_pkg = qsearchs('part_pkg', { pkgpart=>$fee_pkgpart } );
991 $amount = sprintf('%.2f', $amount + $fee_pkg->option('setup_fee') );
994 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
995 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
996 my $discount_term = $1;
998 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
999 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
1002 $p->{'payunique'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
1003 or return { 'error' => gettext('illegal_text'). " payunique: ". $p->{'payunique'} };
1006 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
1007 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
1010 $payunique = $paybatch if ! length($payunique) && length($paybatch);
1012 $p->{'payby'} ||= 'CARD';
1013 $p->{'payby'} =~ /^([A-Z]{4})$/
1014 or return { 'error' => "illegal_payby " . $p->{'payby'} };
1017 #false laziness w/process/payment.cgi
1020 my $replace_cust_payby;
1021 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
1023 $p->{'payinfo1'} =~ /^([\dx]+)$/
1024 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1026 $p->{'payinfo2'} =~ /^([\dx]+)$/
1027 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1029 $payinfo = $payinfo1. '@'. $payinfo2;
1032 foreach my $cust_payby ($cust_main->cust_payby('CHEK','DCHK')) {
1033 if ( $cust_payby->paymask eq $payinfo ) {
1034 $payinfo = $cust_payby->payinfo;
1035 $replace_cust_payby = $cust_payby;
1041 if ($conf->exists('selfservice-ACH_info_readonly') && !$achonfile) {
1042 return { 'error' => "You are not allowed to change your payment information." };
1045 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
1047 $payinfo = $p->{'payinfo'};
1051 #more intelligent matching will be needed here if you change
1052 #card_masking_method and don't remove existing paymasks
1053 foreach my $cust_payby ($cust_main->cust_payby('CARD','DCRD')) {
1054 if ( $cust_payby->paymask eq $payinfo ) {
1055 $payinfo = $cust_payby->payinfo;
1056 $replace_cust_payby = $cust_payby;
1062 $payinfo =~ s/\D//g;
1063 $payinfo =~ /^(\d{13,19}|\d{8,9})$/
1064 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1068 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
1069 return { 'error' => gettext('unknown_card_type') }
1070 if !$cust_main->tokenized($payinfo) && cardtype($payinfo) eq "Unknown";
1072 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
1073 if ( cardtype($payinfo) eq 'American Express card' ) {
1074 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
1075 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
1078 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
1079 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
1082 } elsif ( $conf->exists('selfservice-onfile_require_cvv') ) {
1083 return { 'error' => 'CVV2 is required' };
1084 } elsif ( !$onfile && $conf->exists('selfservice-require_cvv') ) {
1085 return { 'error' => 'CVV2 is required' };
1089 die "unknown payby $payby";
1092 my %payby2fields = (
1093 'CARD' => [ qw( paystart_month paystart_year payissue payip
1094 address1 address2 city state zip country ) ],
1095 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
1098 my %replace = ( 'replace' => $replace_cust_payby, );
1101 $card_type = cardtype($payinfo) if $payby eq 'CARD';
1104 'cust_main' => $cust_main, #XXX or just custnum??
1105 'amount' => sprintf('%.2f', $amount),
1107 'payinfo' => $payinfo,
1108 'paymask' => FS::payinfo_Mixin->mask_payinfo( $payby, $payinfo ),
1109 'card_type' => $card_type,
1110 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
1111 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
1112 'month' => $p->{'month'},
1113 'year' => $p->{'year'},
1114 'payname' => $payname,
1115 'payunique' => $payunique,
1116 'paybatch' => $paybatch,
1117 'paycvv' => $paycvv,
1118 'payname' => $payname,
1119 'discount_term' => $discount_term,
1120 'pkgnum' => $session->{'pkgnum'},
1122 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
1132 my $validate = validate_payment($p);
1133 return $validate if $validate->{'error'};
1135 my $conf = new FS::Conf;
1136 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
1137 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
1139 +{ map { $_=>$validate->{$_} }
1140 qw( card_type paymask payname paydate_pretty month year amount
1141 address1 address2 city state zip country
1147 sub process_stored_payment {
1150 my $session_id = $p->{'session_id'};
1152 my $payment_info = _cache->get( "payment_$session_id" )
1153 or return { 'error' => "Can't resume session" }; #better error message
1155 do_process_payment($payment_info);
1159 sub process_payment {
1162 my $payment_info = validate_payment($p);
1163 return $payment_info if $payment_info->{'error'};
1165 do_process_payment($payment_info);
1169 sub do_process_payment {
1170 my $validate = shift;
1172 my $cust_main = $validate->{'cust_main'};
1174 my $amount = delete $validate->{'amount'};
1177 my $payby = delete $validate->{'payby'};
1179 if ( $validate->{'save'} ) {
1182 foreach my $field ( qw( auto payinfo paymask payname payip ) ) {
1183 $saveopt{$field} = $validate->{$field};
1186 if ( $payby eq 'CARD' ) {
1187 my $bill_location = FS::cust_location->new({
1188 map { $_ => $validate->{$_} }
1189 qw(address1 address2 city state country zip)
1191 $saveopt{'bill_location'} = $bill_location;
1192 foreach my $field ( qw( paydate paystart_month paystart_year payissue ) ) {
1193 $saveopt{$field} = $validate->{$field};
1196 # stateid/stateid_state won't be saved, might be broken as of 4.x
1197 foreach my $field ( qw( paytype paystate ) ) {
1198 $saveopt{$field} = $validate->{$field};
1202 my $error = $cust_main->save_cust_payby(
1203 'payment_payby' => $payby,
1204 'replace' => $validate->{'replace'}, # cust_payby object to replace
1209 #no, this causes customers to process their payments again
1210 #return { 'error' => $error };
1211 #XXX just warn verosely for now so i can figure out how these happen in
1212 # the first place, eventually should redirect them to the "change
1213 #address" page but indicate if the payment processed?
1214 delete($validate->{'payinfo'}); #don't want to log this!
1215 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
1217 "SAVEOPT: ".Dumper(\%saveopt)."\n".
1218 "CUST_MAIN: ". Dumper($cust_main)."\n".
1219 "PACKET: ". Dumper($validate)."\n";
1223 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
1227 'paynum_ref' => \$paynum,
1230 return { 'error' => $error } if $error;
1232 #no error, so order the fee package if applicable...
1233 my $conf = new FS::Conf;
1234 my $fee_pkgpart = $conf->config('selfservice_process-pkgpart', $cust_main->agentnum);
1235 my $fee_skip_first = $conf->exists('selfservice_process-skip_first');
1237 if ( $fee_pkgpart and ! $fee_skip_first || scalar($cust_main->cust_pay) ) {
1239 my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $fee_pkgpart };
1241 $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
1242 return { 'error' => "payment processed successfully, but error ordering fee: $error" }
1245 #and generate an invoice for it now too
1246 $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1247 return { 'error' => "payment processed and fee ordered successfully, but error billing fee: $error" }
1252 $cust_main->apply_payments;
1255 my $receipt_html = '';
1257 # currently supported for realtime CC only; send receipt data to SS
1258 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
1261 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
1264 <TD ALIGN="right">Payment#</TD>
1265 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
1269 <TD ALIGN="right">Date</TD>
1271 <TD BGCOLOR="#FFFFFF"><B>! .
1272 time2str("%a %b %o, %Y %r", $cust_pay->_date)
1278 <TD ALIGN="right">Amount</TD>
1279 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
1284 <TD ALIGN="right">Payment method</TD>
1285 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
1298 'amount' => sprintf('%.2f', $cust_pay->paid),
1299 'date' => $cust_pay->_date,
1300 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
1301 'time_pretty' => time2str('%T', $cust_pay->_date),
1302 'auth_num' => $cust_pay->auth,
1303 'order_num' => $cust_pay->order_number,
1304 'receipt_html' => $receipt_html,
1311 'receipt_html' => '',
1318 sub realtime_collect {
1321 my $session = _cache->get($p->{'session_id'})
1322 or return { 'error' => "Can't resume session" }; #better error message
1324 my $custnum = $session->{'custnum'};
1326 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1327 or return { 'error' => "unknown custnum $custnum" };
1330 if ( $p->{'amount'} ) {
1331 $amount = $p->{'amount'};
1333 elsif ( $session->{'pkgnum'} ) {
1334 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1337 $amount = $cust_main->balance;
1340 my $error = $cust_main->realtime_collect(
1341 'method' => $p->{'method'},
1342 'amount' => $amount,
1343 'pkgnum' => $session->{'pkgnum'},
1344 'session_id' => $p->{'session_id'},
1348 return { 'error' => $error } unless ref( $error );
1350 return { 'error' => '', amount => $amount, %$error };
1353 sub start_thirdparty {
1355 my $session = _cache->get($p->{'session_id'})
1356 or return { 'error' => "Can't resume session" }; #better error message
1357 my $custnum = $session->{'custnum'};
1358 my $cust_main = FS::cust_main->by_key($custnum);
1360 my $amount = $p->{'amount'}
1361 or return { error => 'no amount' };
1363 my $result = $cust_main->create_payment(
1364 'method' => $p->{'method'},
1365 'amount' => $p->{'amount'},
1366 'pkgnum' => $session->{'pkgnum'},
1367 'session_id' => $p->{'session_id'},
1370 if ( ref($result) ) { # hashref or error
1373 return { error => $result };
1377 sub finish_thirdparty {
1379 my $session_id = delete $p->{'session_id'};
1380 my $session = _cache->get($session_id)
1381 or return { 'error' => "Can't resume session" };
1382 my $custnum = $session->{'custnum'};
1383 my $cust_main = FS::cust_main->by_key($custnum);
1385 if ( $p->{_cancel} ) {
1386 # customer backed out of making a payment
1387 return $cust_main->cancel_payment( $session_id );
1389 my $result = $cust_main->execute_payment( $session_id, %$p );
1390 if ( ref($result) ) {
1393 return { error => $result };
1397 sub process_payment_order_pkg {
1400 my $hr = process_payment($p);
1401 return $hr if $hr->{'error'};
1406 sub process_payment_order_renew {
1409 my $hr = process_payment($p);
1410 return $hr if $hr->{'error'};
1415 sub process_prepay {
1419 my $session = _cache->get($p->{'session_id'})
1420 or return { 'error' => "Can't resume session" }; #better error message
1424 my $custnum = $session->{'custnum'};
1426 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1427 or return { 'error' => "unknown custnum $custnum" };
1429 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1430 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1438 return { 'error' => $error } if $error;
1440 return { 'error' => '',
1441 'amount' => $amount,
1442 'seconds' => $seconds,
1443 'duration' => duration_exact($seconds),
1444 'upbytes' => $upbytes,
1445 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1446 'downbytes' => $downbytes,
1447 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1448 'totalbytes'=> $totalbytes,
1449 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1456 my $session = _cache->get($p->{'session_id'})
1457 or return { 'error' => "Can't resume session" }; #better error message
1459 my $custnum = $session->{'custnum'};
1461 my $invnum = $p->{'invnum'};
1463 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1464 'custnum' => $custnum } )
1465 or return { 'error' => "Can't find invnum" };
1469 return { 'error' => '',
1470 'invnum' => $invnum,
1471 'invoice_text' => join('', $cust_bill->print_text ),
1472 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1479 my $session = _cache->get($p->{'session_id'})
1480 or return { 'error' => "Can't resume session" }; #better error message
1482 my $custnum = $session->{'custnum'};
1484 my $invnum = $p->{'invnum'};
1486 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1487 'custnum' => $custnum } )
1488 or return { 'error' => "Can't find invnum" };
1492 return { 'error' => '',
1493 'invnum' => $invnum,
1494 'invoice_pdf' => $cust_bill->print_pdf({
1495 'unsquelch_cdr' => 1,
1496 'locale' => $p->{'locale'},
1502 sub legacy_invoice {
1504 my $session = _cache->get($p->{'session_id'})
1505 or return { 'error' => "Can't resume session" }; #better error message
1507 my $custnum = $session->{'custnum'};
1509 my $legacyinvnum = $p->{'legacyinvnum'};
1512 'legacyinvnum' => $legacyinvnum,
1513 'custnum' => $custnum,
1516 my $legacy_cust_bill =
1517 qsearchs('legacy_cust_bill', { %hash, 'locale' => $p->{'locale'} } )
1518 || qsearchs('legacy_cust_bill', \%hash )
1519 or return { 'error' => "Can't find legacyinvnum" };
1523 return { 'error' => '',
1524 'legacyinvnum' => $legacyinvnum,
1525 'legacyid' => $legacy_cust_bill->legacyid,
1526 'invoice_html' => $legacy_cust_bill->content_html,
1531 sub legacy_invoice_pdf {
1533 my $session = _cache->get($p->{'session_id'})
1534 or return { 'error' => "Can't resume session" }; #better error message
1536 my $custnum = $session->{'custnum'};
1538 my $legacyinvnum = $p->{'legacyinvnum'};
1540 my $legacy_cust_bill = qsearchs('legacy_cust_bill', {
1541 'legacyinvnum' => $legacyinvnum,
1542 'custnum' => $custnum,
1543 }) or return { 'error' => "Can't find legacyinvnum" };
1547 return { 'error' => '',
1548 'legacyinvnum' => $legacyinvnum,
1549 'legacyid' => $legacy_cust_bill->legacyid,
1550 'invoice_pdf' => $legacy_cust_bill->content_pdf,
1558 #sessioning for this? how do we get the session id to the backend invoice
1559 # template so it can add it to the link, blah
1562 if ( $p->{'invnum'} ) {
1563 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1564 or return { 'error' => 'unknown invnum' };
1565 $agentnum = $cust_bill->cust_main->agentnum;
1568 my $templatename = $p->{'template'} || $p->{'templatename'};
1570 #false laziness-ish w/view/cust_bill-logo.cgi
1572 my $conf = new FS::Conf;
1573 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1574 $templatename = "_$1";
1579 my $filename = "logo$templatename.png";
1581 return { 'error' => '',
1582 'logo' => $conf->config_binary($filename, $agentnum),
1583 'content_type' => 'image/png', #should allow gif, jpg too
1589 my $session = _cache->get($p->{'session_id'})
1590 or return { 'error' => "Can't resume session" }; #better error message
1592 my $custnum = $session->{'custnum'};
1594 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1595 or return { 'error' => "unknown custnum $custnum" };
1597 my $conf = new FS::Conf;
1599 my @legacy_cust_bill = $cust_main->legacy_cust_bill;
1601 my @cust_bill = grep ! $_->hide, $cust_main->cust_bill;
1606 #not super efficient, we also run cust_bill_pay/cust_credited inside owed
1607 my @payments_and_credits = sort {$b->_date <=> $a->_date} ($_->cust_bill_pay,$_->cust_credited);
1608 my $owed = $_->owed;
1610 +{ 'invnum' => $_->invnum,
1611 '_date' => $_->_date,
1612 'date' => time2str("%b %o, %Y", $_->_date),
1613 'date_short' => time2str("%m-%d-%Y", $_->_date),
1614 'previous' => sprintf('%.2f', ($_->previous)[0]),
1615 'charged' => sprintf('%.2f', $_->charged),
1616 'owed' => sprintf('%.2f', $owed),
1617 'balance' => sprintf('%.2f', $balance),
1618 'lastpay' => @payments_and_credits
1619 ? time2str("%b %o, %Y", $payments_and_credits[0]->_date)
1625 return { 'error' => '',
1626 'balance' => $cust_main->balance,
1627 'money_char' => $conf->config("money_char") || '$',
1628 'invoices' => $invoices,
1629 'legacy_invoices' => [
1631 +{ 'legacyinvnum' => $_->legacyinvnum,
1632 'legacyid' => $_->legacyid,
1633 '_date' => $_->_date,
1634 'date' => time2str("%b %o, %Y", $_->_date),
1635 'date_short' => time2str("%m-%d-%Y", $_->_date),
1636 'charged' => sprintf('%.2f', $_->charged),
1637 'has_content' => ( length($_->content_pdf)
1638 || length($_->content_html) ),
1648 my $session = _cache->get($p->{'session_id'})
1649 or return { 'error' => "Can't resume session" }; #better error message
1651 my $custnum = $session->{'custnum'};
1653 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1654 or return { 'error' => "unknown custnum $custnum" };
1656 return { 'error' => '',
1657 'balance' => $cust_main->balance,
1658 'money_char' => FS::Conf->new->config("money_char") || '$',
1659 'payments' => [ map $_->SSAPI_getinfo, $cust_main->cust_pay ],
1663 sub payment_receipt {
1665 my $session = _cache->get($p->{'session_id'})
1666 or return { 'error' => "Can't resume session" }; #better error message
1668 my $custnum = $session->{'custnum'};
1670 my $cust_pay = qsearchs('cust_pay', { 'custnum' => $custnum,
1671 'paynum' => $p->{'paynum'},
1674 or return { 'error' => "unknown payment ". $p->{'paynum'} };
1678 %{ $cust_pay->SSAPI_getinfo },
1685 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1686 return { 'error' => $session } if $context eq 'error';
1688 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1689 or return { 'error' => "unknown custnum $custnum" };
1693 my $cust_payby = $_;
1695 map { $_ => $cust_payby->$_ }
1696 qw( custpaybynum weight payby paymask paydate
1697 payname paystate paytype
1701 $cust_main->cust_payby
1709 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1710 return { 'error' => $session } if $context eq 'error';
1712 #XXX payinfo1 + payinfo2 for CHEK?
1713 #or take the opportunity to use separate, more well- named fields?
1714 if ($p->{'payby'} eq 'CHEK') {
1715 $p->{'payinfo1'} =~ /^([\dx]+)$/
1716 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1718 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
1719 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1721 $p->{'payinfo'} = $payinfo1. '@'. $payinfo2;
1724 my $cust_payby = new FS::cust_payby {
1725 'custnum' => $custnum,
1726 map { $_ => $p->{$_} } qw( weight payby payinfo paycvv paydate payname
1727 paystate paytype payip
1731 my $error = $cust_payby->insert;
1733 return { 'error' => $error };
1735 return { 'custpaybynum' => $cust_payby->custpaybynum };
1743 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1744 return { 'error' => $session } if $context eq 'error';
1746 if ($p->{'payby'} eq 'CHEK') {
1747 $p->{'payinfo1'} =~ /^([\dx]+)$/
1748 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
1750 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
1751 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
1753 $p->{'payinfo'} = $payinfo1. '@'. $payinfo2;
1756 my $cust_payby = qsearchs('cust_payby', {
1757 'custnum' => $custnum,
1758 'custpaybynum' => $p->{'custpaybynum'},
1760 or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
1762 my $cust_main = qsearchs( 'cust_main', {custnum => $cust_payby->custnum} )
1763 or return { 'error' => 'unknown custnum '.$cust_payby->custnum };
1766 qw( weight payby payinfo paycvv paydate payname paystate paytype payip )
1768 next unless exists($p->{$field});
1769 $cust_payby->set($field,$p->{$field});
1771 $cust_payby->set( 'paymask' => $cust_payby->mask_payinfo );
1773 # Update column if given a value, and the given value wasn't
1774 # the value generated by $cust_main->masked($column);
1775 $cust_main->set( $_, $p->{$_} )
1776 for grep{ $p->{$_} !~ /^x/i; }
1777 grep{ exists $p->{$_} }
1780 # Perform updates within a transaction
1781 local $FS::UID::AutoCommit = 0;
1783 if ( my $error = $cust_payby->replace || $cust_main->replace ) {
1785 return { error => $error };
1789 return { custpaybynum => $cust_payby->custpaybynum };
1795 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1796 return { 'error' => $session } if $context eq 'error';
1798 my $cust_payby = qsearchs('cust_payby', {
1799 'custnum' => $custnum,
1800 'custpaybynum' => $p->{'custpaybynum'},
1802 or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
1804 return { 'error' => $cust_payby->verify };
1811 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1812 return { 'error' => $session } if $context eq 'error';
1814 my $cust_payby = qsearchs('cust_payby', {
1815 'custnum' => $custnum,
1816 'custpaybynum' => $p->{'custpaybynum'},
1818 or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
1820 my $conf = new FS::Conf;
1821 if (($cust_payby->payby eq "DCHK" || $cust_payby->payby eq "CHEK") && $conf->exists('selfservice-ACH_info_readonly')) {
1822 return { 'error' => "Sorry you do not have permission to delete bank information." };
1825 return { 'error' => $cust_payby->delete };
1831 my $session = _cache->get($p->{'session_id'})
1832 or return { 'error' => "Can't resume session" }; #better error message
1834 my $custnum = $session->{'custnum'};
1836 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1837 or return { 'error' => "unknown custnum $custnum" };
1839 my @errors = $cust_main->cancel( 'quiet'=>1 );
1841 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1843 return { 'error' => $error };
1850 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1851 return { 'error' => $session } if $context eq 'error';
1853 my $pkg = qsearchs({
1854 'table' => 'cust_pkg',
1855 'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
1857 'custnum' => $custnum,
1858 'pkgnum' => $p->{'pkgnum'},
1861 or return {'error' => 'unknown pkg num $pkgnum'};
1864 pkg_label => $pkg->pkg,
1865 pkgpart => $pkg->pkgpart,
1866 classnum => $pkg->classnum,
1874 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1875 return { 'error' => $session } if $context eq 'error';
1877 my $search = { 'custnum' => $custnum };
1878 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1879 my $cust_main = qsearchs('cust_main', $search )
1880 or return { 'error' => "unknown custnum $custnum" };
1882 my $conf = new FS::Conf;
1883 my $immutable = $conf->exists('selfservice_immutable-package');
1885 # the duplication below is necessary:
1886 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1887 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1888 # non-backwards-compatible change breaking the software of anyone using the API
1889 # instead of the stock selfservice
1890 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1891 if( $conf->exists('selfservice_server-view-wholesale') ) {
1892 return { 'svcnum' => $session->{'svcnum'},
1893 'custnum' => $custnum,
1894 'cust_pkg' => [ map {
1896 immutable => $immutable,
1897 part_pkg => [ map $_->hashref, $_->part_pkg ],
1899 [ map $_->hashref, $_->available_part_svc ],
1901 [ map { my $ref = { $_->hash,
1902 label => [ $_->label ],
1904 $ref->{_password} = $_->svc_x->_password
1905 if $context eq 'agent'
1906 && $conf->exists('agent-showpasswords')
1907 && $_->part_svc->svcdb eq 'svc_acct';
1912 } $cust_main->cust_pkg
1915 small_custview( $cust_main, $conf->config('countrydefault') ),
1916 'wholesale_view' => 1,
1917 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1918 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1919 'lnp' => $conf->exists('svc_phone-lnp'),
1923 { 'svcnum' => $session->{'svcnum'},
1924 'custnum' => $custnum,
1925 'cust_pkg' => [ map {
1926 my $primary_cust_svc = $_->primary_cust_svc;
1929 immutable => $immutable,
1930 pkg_label => $_->pkg_locale,
1931 status => $_->status,
1932 statuscolor => $_->statuscolor,
1934 [ map { $_->hashref }
1935 grep { $_->selfservice_access ne 'hidden' }
1936 $_->available_part_svc
1939 [ map { my $ref = { $_->hash,
1940 label => [ $_->label ],
1942 $ref->{_password} = $_->svc_x->_password
1943 if $context eq 'agent'
1944 && $conf->exists('agent-showpasswords')
1945 && $_->part_svc->svcdb eq 'svc_acct';
1946 $ref->{svchash} = { $_->svc_x->hash } if
1947 $_->part_svc->svcdb eq 'svc_phone';
1948 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1949 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1952 grep { $_->part_svc->selfservice_access ne 'hidden' }
1957 ? { $primary_cust_svc->hash,
1958 label => [ $primary_cust_svc->label ],
1959 finger => $primary_cust_svc->svc_x->finger, #uuh
1960 $primary_cust_svc->part_svc->hash,
1964 } $cust_main->ncancelled_pkgs
1967 small_custview( $cust_main, $conf->config('countrydefault') ),
1968 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1976 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1977 return { 'error' => $session } if $context eq 'error';
1979 my $conf = new FS::Conf;
1981 my $hide_usage = $conf->exists('selfservice_hide-usage') ? 1 : 0;
1982 my $search = { 'custnum' => $custnum };
1983 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1984 my $cust_main = qsearchs('cust_main', $search )
1985 or return { 'error' => "unknown custnum $custnum" };
1987 my $pkgnum = $session->{'pkgnum'} || $p->{'pkgnum'} || '';
1988 if ( ! $pkgnum && $p->{'svcnum'} ) {
1989 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $p->{'svcnum'} } );
1990 $pkgnum = $cust_svc->pkgnum if $cust_svc;
1994 my @cust_pkg_usage = ();
1995 foreach my $cust_pkg ( $p->{'ncancelled'}
1996 ? $cust_main->ncancelled_pkgs
1997 : $cust_main->unsuspended_pkgs ) {
1998 next if $pkgnum && $cust_pkg->pkgnum != $pkgnum;
1999 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
2000 push @cust_pkg_usage, $cust_pkg->cust_pkg_usage;
2003 @cust_svc = grep { $_->part_svc->selfservice_access ne 'hidden' } @cust_svc;
2006 foreach (@cust_pkg_usage) {
2007 my $part = $_->part_pkg_usage;
2008 my $tag = $part->description . ($part->shared ? 1 : 0);
2009 my $row = $usage_pools{$tag}
2010 ||= [ $part->description, 0, 0, $part->shared ? 1 : 0 ];
2011 $row->[1] += sprintf('%.1f', $_->minutes); # minutes remaining
2012 $row->[2] += $part->minutes; # minutes total
2014 } # otherwise just leave them empty
2016 if ( $p->{'svcdb'} ) {
2017 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
2019 : ref($p->{'svcdb'}) eq 'ARRAY'
2020 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
2021 : { $p->{'svcdb'} => 1 };
2022 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
2025 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
2028 my @svcs; # stuff to return to the client
2029 my %bytes_used_total; # for _used columns only
2030 foreach my $cust_svc (@cust_svc) {
2031 my $svc_x = $cust_svc->svc_x;
2032 my($label, $value) = $cust_svc->label;
2033 my $part_svc = $cust_svc->part_svc;
2034 my $svcdb = $part_svc->svcdb;
2035 my $cust_pkg = $cust_svc->cust_pkg;
2036 my $part_pkg = $cust_pkg->part_pkg;
2039 'svcnum' => $cust_svc->svcnum,
2040 'display_svcnum' => $cust_svc->display_svcnum,
2044 'pkg_label' => $cust_pkg->pkg_locale,
2045 'pkg_status' => $cust_pkg->status,
2046 'readonly' => ($part_svc->selfservice_access eq 'readonly'),
2049 # would it make sense to put this in a svc_* method?
2051 if (!$hide_usage and grep(/^$svcdb$/, qw(svc_acct svc_broadband)) and $part_svc->part_export_usage) {
2052 my $last_bill = $cust_pkg->last_bill || 0;
2054 my $up_used = $cust_svc->attribute_since_sqlradacct($last_bill,$now,'AcctInputOctets');
2055 my $down_used = $cust_svc->attribute_since_sqlradacct($last_bill,$now,'AcctOutputOctets');
2058 'seconds_used' => $cust_svc->seconds_since_sqlradacct($last_bill,$now),
2059 'upbytes_used' => display_bytecount($up_used),
2060 'downbytes_used' => display_bytecount($down_used),
2061 'totalbytes_used' => display_bytecount($up_used + $down_used)
2063 $bytes_used_total{'seconds_used'} += $hash{'seconds_used'};
2064 $bytes_used_total{'upbytes_used'} += $up_used;
2065 $bytes_used_total{'downbytes_used'} += $down_used;
2066 $bytes_used_total{'totalbytes_used'} += $up_used + $down_used;
2069 if ( $svcdb eq 'svc_acct' ) {
2070 foreach (qw(username email finger seconds)) {
2071 $hash{$_} = $svc_x->$_;
2077 'upbytes' => display_bytecount($svc_x->upbytes),
2078 'downbytes' => display_bytecount($svc_x->downbytes),
2079 'totalbytes' => display_bytecount($svc_x->totalbytes),
2081 'recharge_amount' => $part_pkg->option('recharge_amount',1),
2082 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
2083 'recharge_upbytes' =>
2084 display_bytecount($part_pkg->option('recharge_upbytes',1)),
2085 'recharge_downbytes' =>
2086 display_bytecount($part_pkg->option('recharge_downbytes',1)),
2087 'recharge_totalbytes' =>
2088 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
2093 } elsif ( $svcdb eq 'svc_dsl' ) {
2095 $hash{'phonenum'} = $svc_x->phonenum;
2096 if ( $svc_x->first || $svc_x->get('last') || $svc_x->company ) {
2097 $hash{'name'} = $svc_x->first. ' '. $svc_x->get('last');
2098 $hash{'name'} = $svc_x->company. ' ('. $hash{'name'}. ')'
2101 $hash{'name'} = $cust_main->name;
2103 # no usage to hide here
2105 } elsif ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
2107 # could potentially show lots of things...
2108 $hash{'outbound'} = 1;
2109 $hash{'inbound'} = 0;
2110 if ( $svcdb eq 'svc_phone' ) {
2111 if ( $part_pkg->plan eq 'voip_inbound' ) {
2112 $hash{'outbound'} = 0;
2113 $hash{'inbound'} = 1;
2114 } elsif ( $part_pkg->option('selfservice_inbound_format')
2115 or $conf->config('selfservice-default_inbound_cdr_format')
2117 $hash{'inbound'} = 1;
2120 foreach (qw(inbound outbound)) {
2121 # hmm...we can't filter by status here, because there might
2122 # not be cdr_terminations at all. have to go by date.
2123 # find all since the last bill date.
2124 # XXX cdr types? we are going to need them.
2126 my $sum_cdr = $svc_x->sum_cdrs(
2127 'inbound' => ( $_ eq 'inbound' ? 1 : 0 ),
2128 'begin' => ($cust_pkg->last_bill || 0),
2130 'disable_charged_party' => 1,
2132 $hash{$_} = $sum_cdr->hashref;
2135 } # not hiding usage
2139 } # foreach $cust_svc
2141 foreach my $field (keys %bytes_used_total) {
2142 if ($field =~ /bytes/) {
2143 $bytes_used_total{$field} = display_bytecount($bytes_used_total{$field});
2148 'svcnum' => $session->{'svcnum'},
2149 'custnum' => $custnum,
2150 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
2151 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
2153 'bytes_used_total' => \%bytes_used_total,
2155 map { $usage_pools{$_} }
2159 'hide_usage' => $hide_usage,
2164 sub _customer_svc_x {
2165 my($custnum, $svcnum, $table) = (shift, shift, shift);
2166 my $hashref = ref($svcnum) ? $svcnum : { 'svcnum' => $svcnum };
2168 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2169 my $search = " AND custnum = $1";
2170 #$search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2173 'table' => ($table || 'svc_acct'),
2174 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2175 'LEFT JOIN cust_pkg USING ( pkgnum ) ',#.
2176 #'LEFT JOIN cust_main USING ( custnum ) ',
2177 'hashref' => $hashref,
2178 'extra_sql' => $search, #important
2183 sub svc_status_html {
2186 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2187 return { 'error' => $session } if $context eq 'error';
2189 #XXX only svc_dsl for now
2190 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl')
2191 or return { 'error' => "Service not found" };
2193 my $html = $svc_x->getstatus_html;
2195 return { 'html' => $html };
2199 sub svc_status_hash {
2202 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2203 return { 'error' => $session } if $context eq 'error';
2205 #XXX only svc_acct for now
2206 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
2207 or return { 'error' => "Service not found" };
2209 my ( $html, $hashref ) = $svc_x->export_getstatus;
2214 sub set_svc_status_hash { _svc_method_X(shift, 'export_setstatus') }
2215 sub set_svc_status_listadd { _svc_method_X(shift, 'export_setstatus_listadd') }
2216 sub set_svc_status_listdel { _svc_method_X(shift, 'export_setstatus_listdel') }
2217 sub set_svc_status_vacationadd { _svc_method_X(shift, 'export_setstatus_vacationadd') }
2218 sub set_svc_status_vacationdel { _svc_method_X(shift, 'export_setstatus_vacationdel') }
2221 my( $p, $method ) = @_;
2223 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2224 return { 'error' => $session } if $context eq 'error';
2226 #XXX only svc_acct for now
2227 my $svc_x = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct')
2228 or return { 'error' => "Service not found" };
2230 warn "$method ". join(' / ', map "$_=>".$p->{$_}, keys %$p )
2232 my $error = $svc_x->$method($p); #$p? returns error?
2233 return { 'error' => $error } if $error;
2235 return {}; #? { 'error' => '' }
2239 sub acct_forward_info {
2242 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2243 return { 'error' => $session } if $context eq 'error';
2245 my $svc_forward = _customer_svc_x( $custnum,
2246 { 'srcsvc' => $p->{'svcnum'} },
2249 or return { 'error' => '',
2253 return { 'error' => '',
2254 'dst' => $svc_forward->dst || $svc_forward->dstsvc_acct->email,
2259 sub process_acct_forward {
2261 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2262 return { 'error' => $session } if $context eq 'error';
2264 my $old = _customer_svc_x( $custnum,
2265 { 'srcsvc' => $p->{'svcnum'} },
2269 if ( $p->{'dst'} eq '' ) {
2271 my $error = $old->delete;
2272 return { 'error' => $error };
2274 return { 'error' => '' };
2277 my $new = new FS::svc_forward { 'srcsvc' => $p->{'svcnum'},
2278 'dst' => $p->{'dst'},
2283 $new->svcnum($old->svcnum);
2284 my $cust_svc = $old->cust_svc;
2285 $new->svcpart($old->svcpart);
2286 $new->pkgnuym($old->pkgnum);
2287 $error = $new->replace($old);
2289 my $conf = new FS::Conf;
2290 $new->svcpart($conf->config('selfservice-svc_forward_svcpart'));
2292 my $svc_acct = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_acct' )
2293 or return { 'error' => 'No service' }; #how would we even get here?
2295 $new->pkgnum( $svc_acct->cust_svc->pkgnum );
2297 $error = $new->insert;
2300 return { 'error' => $error };
2304 sub list_dsl_devices {
2307 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2308 return { 'error' => $session } if $context eq 'error';
2310 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2311 or return { 'error' => "Service not found" };
2314 'devices' => [ map {
2315 +{ 'mac_addr' => $_->mac_addr };
2316 } $svc_dsl->dsl_device
2322 sub add_dsl_device {
2325 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2326 return { 'error' => $session } if $context eq 'error';
2328 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2329 or return { 'error' => "Service not found" };
2331 return { 'error' => 'No MAC address supplied' }
2332 unless length($p->{'mac_addr'});
2334 my $dsl_device = new FS::dsl_device { 'svcnum' => $svc_dsl->svcnum,
2335 'mac_addr' => scalar($p->{'mac_addr'}),
2337 my $error = $dsl_device->insert;
2338 return { 'error' => $error };
2342 sub delete_dsl_device {
2345 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2346 return { 'error' => $session } if $context eq 'error';
2348 my $svc_dsl = _customer_svc_x( $custnum, $p->{'svcnum'}, 'svc_dsl' )
2349 or return { 'error' => "Service not found" };
2351 my $dsl_device = qsearchs('dsl_device', { 'svcnum' => $svc_dsl->svcnum,
2352 'mac_addr' => scalar($p->{'mac_addr'}),
2355 or return { 'error' => 'Unknown MAC address: '. $p->{'mac_addr'} };
2357 my $error = $dsl_device->delete;
2358 return { 'error' => $error };
2364 _usage_details( \&_port_graph, $p,
2365 'svcdb' => 'svc_port',
2370 my($svc_port, $begin, $end) = @_;
2372 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
2373 push @usage, { 'png' => $pngOrError };
2377 sub _list_svc_usage {
2378 my($svc_acct, $begin, $end) = @_;
2380 foreach my $part_export (
2381 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
2382 qw( sqlradius sqlradius_withdomain )
2384 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
2389 sub list_svc_usage {
2390 _usage_details(\&_list_svc_usage, @_);
2393 sub _list_support_usage {
2394 my($svc_acct, $begin, $end) = @_;
2396 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
2397 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
2399 push @usage, { 'seconds' => $_->seconds,
2400 'support' => $_->support,
2401 '_date' => $_->_date,
2402 'id' => $_->transaction_id,
2403 'creator' => $_->creator,
2404 'subject' => $_->subject,
2405 'status' => $_->status,
2406 'ticketid' => $_->ticketid,
2412 sub list_support_usage {
2413 _usage_details(\&_list_support_usage, @_);
2416 sub _list_cdr_usage {
2417 # XXX CDR type support...
2418 # XXX any way to do a paged search on this?
2419 # we have to return the results all at once...
2420 my($svc_x, $begin, $end, %opt) = @_;
2421 map [ $_->downstream_csv(%opt, 'keeparray' => 1) ],
2425 'disable_charged_party' => 1,
2430 sub list_cdr_usage {
2432 _usage_details( \&_list_cdr_usage, $p );
2435 sub _usage_details {
2436 my($callback, $p, %opt) = @_;
2437 my $conf = FS::Conf->new;
2439 if ( $conf->exists('selfservice_hide-usage') ) {
2440 return { 'error' => 'Viewing usage is not allowed.' };
2443 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2444 return { 'error' => $session } if $context eq 'error';
2446 my $search = { 'svcnum' => $p->{'svcnum'} };
2447 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2449 my $cust_svc = qsearchs( 'cust_svc', $search );
2450 return { 'error' => 'No service selected in list_svc_usage' }
2453 my $svc_x = $cust_svc->svc_x;
2454 my $svcdb = $svc_x->table;
2455 my $cust_pkg = $cust_svc->cust_pkg;
2456 my $freq = $cust_pkg->part_pkg->freq;
2459 if ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) {
2461 if ( $p->{inbound} ) {
2462 $format = $cust_pkg->part_pkg->option('selfservice_inbound_format')
2463 || $conf->config('selfservice-default_inbound_cdr_format')
2464 || 'source_default';
2465 $callback_opt{inbound} = 1;
2467 $format = $cust_pkg->part_pkg->option('selfservice_format')
2468 || $conf->config('selfservice-default_cdr_format')
2472 $callback_opt{format} = $format;
2473 $callback_opt{use_clid} = 1;
2474 $header = [ split(',', FS::cdr::invoice_header($format) ) ];
2477 my $start = $cust_pkg->setup;
2478 #my $end = $cust_pkg->bill; # or time?
2481 unless ( $p->{beginning} ) {
2482 $p->{beginning} = $cust_pkg->last_bill;
2483 $p->{ending} = $end;
2486 die "illegal beginning" if $p->{beginning} !~ /^\d*$/;
2487 die "illegal ending" if $p->{ending} !~ /^\d*$/;
2489 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending},
2493 if ( $conf->exists('selfservice-hide_cdr_price') ) {
2494 # ugly kludge, I know
2495 my ($delete_col) = grep { $header->[$_] eq 'Price' } (0..scalar(@$header));
2496 if (defined $delete_col) {
2497 delete($_->[$delete_col]) foreach ($header, @usage);
2501 #kinda false laziness with FS::cust_main::bill, but perhaps
2502 #we should really change this bit to DateTime and DateTime::Duration
2504 #change this bit to use Date::Manip? CAREFUL with timezones (see
2505 # mailing list archive)
2506 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
2507 (localtime($p->{ending}) )[0,1,2,3,4,5];
2508 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
2509 (localtime($p->{beginning}) )[0,1,2,3,4,5];
2511 if ( $freq =~ /^\d+$/ ) {
2513 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
2515 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
2516 } elsif ( $freq =~ /^(\d+)w$/ ) {
2518 $nmday += $weeks * 7;
2519 $pmday -= $weeks * 7;
2520 } elsif ( $freq =~ /^(\d+)d$/ ) {
2524 } elsif ( $freq =~ /^(\d+)h$/ ) {
2529 return { 'error' => "unparsable frequency: ". $freq };
2532 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
2533 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
2537 'svcnum' => $p->{svcnum},
2538 'beginning' => $p->{beginning},
2539 'ending' => $p->{ending},
2540 'inbound' => $p->{inbound},
2541 'previous' => ($previous > $start) ? $previous : $start,
2542 'next' => ($next < $end) ? $next : $end,
2543 'header' => $header,
2551 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2552 return { 'error' => $session } if $context eq 'error';
2554 my $search = { 'custnum' => $custnum };
2555 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2556 my $cust_main = qsearchs('cust_main', $search )
2557 or return { 'error' => "unknown custnum $custnum" };
2559 my $status = $cust_main->status;
2561 my %order_pkg_options = ();
2562 if ( $p->{locationnum} > 0 ) {
2563 $order_pkg_options{locationnum} = delete($p->{locationnum});
2564 } elsif ( $p->{address1} ) {
2565 $order_pkg_options{'cust_location'} = new FS::cust_location {
2566 map { $_ => $p->{$_} }
2567 qw( address1 address2 city county state zip country )
2571 #false laziness w/ClientAPI/Signup.pm
2573 my $cust_pkg = new FS::cust_pkg ( {
2574 'custnum' => $custnum,
2575 'pkgpart' => $p->{'pkgpart'},
2576 'quantity' => $p->{'quantity'} || 1,
2578 my $error = $cust_pkg->check;
2579 return { 'error' => $error } if $error;
2582 unless ( $p->{'svcpart'} eq 'none' ) {
2586 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
2588 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2589 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
2590 $svcdb = $part_svc->svcdb;
2592 $svcdb = 'svc_acct';
2594 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
2597 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
2598 'svc_domain' => [ qw( domain ) ],
2599 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
2600 'svc_external' => [ qw( id title ) ],
2601 'svc_pbx' => [ qw( id title ) ],
2604 my $svc_x = "FS::$svcdb"->new( {
2605 'svcpart' => $svcpart,
2606 map { $_ => $p->{$_} } @{$fields{$svcdb}}
2609 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
2612 while ( length($p->{"snarf_machine$snarfnum"}) ) {
2613 my $acct_snarf = new FS::acct_snarf ( {
2614 'machine' => $p->{"snarf_machine$snarfnum"},
2615 'protocol' => $p->{"snarf_protocol$snarfnum"},
2616 'username' => $p->{"snarf_username$snarfnum"},
2617 '_password' => $p->{"snarf_password$snarfnum"},
2620 push @acct_snarf, $acct_snarf;
2622 $svc_x->child_objects( \@acct_snarf );
2625 my $y = $svc_x->setdefault; # arguably should be in new method
2626 return { 'error' => $y } if $y && !ref($y);
2628 $error = $svc_x->check;
2629 return { 'error' => $error } if $error;
2635 $error = $cust_main->order_pkg(
2636 'cust_pkg' => $cust_pkg,
2641 return { 'error' => $error } if $error;
2643 my $conf = new FS::Conf;
2644 if ( $conf->exists('signup_server-realtime') ) {
2646 my $bill_error = _do_bop_realtime( $cust_main, $status, 'collect'=>$p->{run_bill_events} );
2649 $cust_pkg->cancel('quiet'=>1);
2652 $cust_pkg->reexport;
2656 $cust_pkg->reexport;
2659 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
2661 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
2668 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2669 return { 'error' => $session } if $context eq 'error';
2671 my $conf = new FS::Conf;
2672 my $immutable = $conf->exists('selfservice_immutable-package');
2673 return { 'error' => "Package modification disabled" } if $immutable;
2675 my $search = { 'custnum' => $custnum };
2676 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2677 my $cust_main = qsearchs('cust_main', $search )
2678 or return { 'error' => "unknown custnum $custnum" };
2680 my $status = $cust_main->status;
2681 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
2682 or return { 'error' => "unknown package $p->{pkgnum}" };
2684 #if someone does need self-service package change of suspended packages,
2685 # figure out how to be more discriminating
2686 return { error=>"Can't change a suspended package", pkgnum=>$cust_pkg->pkgnum}
2687 if $cust_pkg->status eq 'suspended';
2689 my $err_or_cust_pkg = $cust_pkg->change( 'pkgpart' => $p->{'pkgpart'},
2690 'quantity' => $p->{'quantity'} || 1,
2693 my $new_pkg = qsearchs('part_pkg', { 'pkgpart' => $p->{pkgpart} } )
2694 or return { 'error' => "unknown package $p->{pkgpart}" };
2696 return { error=>$err_or_cust_pkg, pkgnum=>$cust_pkg->pkgnum }
2697 unless ref($err_or_cust_pkg);
2700 if ( $conf->exists('signup_server-realtime') ) {
2702 my $bill_error = _do_bop_realtime( $cust_main, $status, 'no_invoice_void'=>1 );
2705 $err_or_cust_pkg->suspend;
2708 $err_or_cust_pkg->reexport;
2712 $err_or_cust_pkg->reexport;
2715 return { error => '', pkg => $new_pkg->pkg, pkgnum => $err_or_cust_pkg->pkgnum };
2719 sub order_recharge {
2722 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2723 return { 'error' => $session } if $context eq 'error';
2725 my $search = { 'custnum' => $custnum };
2726 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2727 my $cust_main = qsearchs('cust_main', $search )
2728 or return { 'error' => "unknown custnum $custnum" };
2730 my $status = $cust_main->status;
2731 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
2732 or return { 'error' => "unknown service " . $p->{'svcnum'} };
2734 my $svc_x = $cust_svc->svc_x;
2735 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
2738 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
2739 qw ( recharge_seconds recharge_upbytes recharge_downbytes
2740 recharge_totalbytes );
2741 my $amount = $part_pkg->option('recharge_amount', 1);
2743 my ($l, $v, $d) = $cust_svc->label; # blah
2744 my $pkg = "Recharge $v";
2746 my $bill_error = $cust_main->charge($amount, $pkg,
2747 "time: $vhash{seconds}, up: $vhash{upbytes}," .
2748 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
2749 $part_pkg->taxclass); #meh
2751 my $conf = new FS::Conf;
2752 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
2754 $bill_error = _do_bop_realtime( $cust_main, $status );
2759 my $error = $svc_x->recharge (\%vhash);
2760 return { 'error' => $error } if $error;
2764 my $error = $bill_error;
2765 $error ||= $svc_x->recharge (\%vhash);
2766 return { 'error' => $error } if $error;
2769 return { error => '', svc => $cust_svc->part_svc->svc };
2773 sub _do_bop_realtime {
2774 my ($cust_main, $status, %opt) = @_;
2776 my $old_balance = $cust_main->balance;
2779 my $bill_error = $cust_main->bill(
2780 'return_bill' => \@cust_bill,
2783 $bill_error ||= $cust_main->apply_payments_and_credits;
2785 $bill_error ||= $cust_main->realtime_collect('selfservice' => 1);
2787 if ( $cust_main->balance > $old_balance
2788 && $cust_main->balance > 0
2789 && ( $cust_main->has_cust_payby_auto || $status eq 'suspended' )
2792 unless ( $opt{'no_invoice_void'} ) {
2794 #this used to apply a credit, but now we can void invoices...
2795 foreach my $cust_bill (@cust_bill) {
2796 my $voiderror = $cust_bill->void('automatic payment failed');
2797 warn "Error voiding cust bill after decline: $voiderror" if $voiderror;
2802 return { 'error' => '_decline', 'bill_error' => $bill_error };
2805 if ( $opt{'collect'} ) {
2806 my $collect_error = $cust_main->collect();
2807 return { 'error' => '_decline', 'bill_error' => $collect_error }
2808 if $collect_error; #?
2817 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2818 return { 'error' => $session } if $context eq 'error';
2820 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2821 or return { 'error' => "unknown custnum $custnum" };
2823 my @cust_pkg = sort { $a->bill <=> $b->bill }
2824 grep { $_->part_pkg->freq ne '0' }
2825 $cust_main->ncancelled_pkgs;
2827 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
2829 my $total = $cust_main->balance;
2832 my $bill = $_->bill;
2833 $total += $_->part_pkg->base_recur($_, \$bill);
2834 my $renew_date = $_->part_pkg->add_freq($_->bill);
2836 'pkgnum' => $_->pkgnum,
2837 'amount' => sprintf('%.2f', $total),
2838 'bill_date' => $_->bill,
2839 'bill_date_pretty' => time2str('%x', $_->bill),
2840 'renew_date' => $renew_date,
2841 'renew_date_pretty' => time2str('%x', $renew_date),
2842 'expire_date' => $_->expire,
2843 'expire_date_pretty' => time2str('%x', $_->expire),
2848 return { 'dates' => \@array };
2852 sub payment_info_renew_info {
2854 my $renew_info = renew_info($p);
2855 my $payment_info = payment_info($p);
2856 return { %$renew_info,
2864 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2865 return { 'error' => $session } if $context eq 'error';
2867 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2868 or return { 'error' => "unknown custnum $custnum" };
2870 my $date = $p->{'date'};
2874 #freeside-daily -n -d $date fs_daily $custnum
2875 $cust_main->bill_and_collect( 'time' => $date,
2876 'invoice_time' => $now,
2877 'actual_time' => $now,
2878 'check_freq' => '1d',
2881 return { 'error' => '' };
2887 my $session = _cache->get($p->{'session_id'})
2888 or return { 'error' => "Can't resume session" }; #better error message
2890 my $custnum = $session->{'custnum'};
2892 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2893 or return { 'error' => "unknown custnum $custnum" };
2895 my $conf = new FS::Conf;
2897 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
2898 or return { 'error' => 'Permission denied' };
2900 my $pkgnum = $p->{'pkgnum'};
2902 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2903 'pkgnum' => $pkgnum, } )
2904 or return { 'error' => "unknown pkgnum $pkgnum" };
2906 my $error = $cust_pkg->suspend(reason => $reasonnum);
2907 return { 'error' => $error };
2913 my $session = _cache->get($p->{'session_id'})
2914 or return { 'error' => "Can't resume session" }; #better error message
2916 my $custnum = $session->{'custnum'};
2917 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
2918 or return { 'error' => "unknown custnum $custnum" };
2920 my $pkgnum = $p->{'pkgnum'};
2921 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2922 'pkgnum' => $pkgnum, } )
2923 or return { 'error' => "unknown pkgnum $pkgnum" };
2925 my $error = $cust_pkg->cancel( 'quiet' => 1,
2926 'date' => $p->{'date'},
2928 return { 'error' => $error };
2931 sub provision_phone {
2934 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
2936 #editing an existing phone number
2937 if ( $p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/ ) {
2938 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2939 return { 'error' => $session } if $context eq 'error';
2941 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2942 return { 'error' => 'service not found' } unless $svc_phone;
2943 return { 'error' => 'invalid svcnum' }
2944 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
2946 $svc_phone->email($p->{'email'})
2947 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
2948 $svc_phone->forwarddst($p->{'forwarddst'})
2949 if $svc_phone->forwarddst ne $p->{'forwarddst'}
2950 && $p->{'forwarddst'} =~ /^(\d+|)$/;
2951 return { 'error' => $svc_phone->replace };
2955 unless ( $p->{'lnp'} ) {
2956 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
2957 $p->{'lnp_status'} = "portingin";
2958 return _provision( 'FS::svc_phone',
2959 [qw(lnp_desired_due_date lnp_other_provider
2960 lnp_other_provider_account phonenum countrycode lnp_status)],
2961 [qw(phonenum countrycode)],
2967 # single DID order (the usual case)
2968 unless (scalar(@bulkdid)) {
2969 return _provision( 'FS::svc_phone',
2970 [qw(phonenum countrycode)],
2971 [qw(phonenum countrycode)],
2977 # bulk DID order case
2979 foreach my $did ( @bulkdid ) {
2980 $did =~ s/[^0-9]//g;
2981 $error = _provision( 'FS::svc_phone',
2982 [qw(phonenum countrycode)],
2983 [qw(phonenum countrycode)],
2985 'pkgnum' => $p->{'pkgnum'},
2986 'svcpart' => $p->{'svcpart'},
2988 'countrycode' => $p->{'countrycode'},
2989 'session_id' => $p->{'session_id'},
2992 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
2994 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
2999 warn "provision_pbx called\n"
3002 warn "provision_pbx calling _provision\n"
3004 _provision( 'FS::svc_pbx',
3005 [qw(id title max_extensions max_simultaneous ip_addr)],
3006 [qw(id title max_extensions max_simultaneous ip_addr)],
3012 sub provision_acct {
3014 warn "provision_acct called\n"
3017 return { 'error' => gettext('passwords_dont_match') }
3018 if $p->{'_password'} ne $p->{'_password2'};
3019 return { 'error' => gettext('empty_password') }
3020 unless length($p->{'_password'});
3022 if ($p->{'domsvc'}) {
3023 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
3024 qw ( svcpart pkgnum ) );
3025 return { 'error' => gettext('invalid_domain') }
3026 unless ($domains{$p->{'domsvc'}});
3029 warn "provision_acct calling _provision\n"
3031 _provision( 'FS::svc_acct',
3032 [qw(username _password domsvc)],
3033 [qw(username _password domsvc)],
3039 sub provision_external {
3041 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
3042 _provision( 'FS::svc_external',
3050 sub provision_forward {
3052 _provision( 'FS::svc_forward',
3053 ['srcsvc','src','dstsvc','dst'],
3060 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
3061 warn "_provision called for $class\n"
3064 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3065 return { 'error' => $session } if $context eq 'error';
3067 my $search = { 'custnum' => $custnum };
3068 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3069 my $cust_main = qsearchs('cust_main', $search )
3070 or return { 'error' => "unknown custnum $custnum" };
3072 my $pkgnum = $p->{'pkgnum'};
3074 warn "searching for custnum $custnum pkgnum $pkgnum\n"
3076 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3077 'pkgnum' => $pkgnum,
3079 or return { 'error' => "unknown pkgnum $pkgnum" };
3081 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
3083 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
3084 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
3086 return { error=> 'svcpart '. $p->{'svcpart'}. " is not a $class definition" }
3087 if $class ne 'FS::'. $part_svc->svcdb;
3089 warn "creating $class record\n"
3091 my $svc_x = $class->new( {
3092 'pkgnum' => $p->{'pkgnum'},
3093 'svcpart' => $p->{'svcpart'},
3094 map { $_ => $p->{$_} } @$fields
3097 my %insert_args = ();
3098 #i shouldn't be a special case here (pass an option or something)
3099 if ( $class eq 'FS::svc_phone'
3100 && grep length($p->{$_}), @location_editable_fields
3103 $insert_args{'cust_location'} = new FS::cust_location {
3104 map { $_ => $p->{$_} } @location_editable_fields
3108 warn "inserting $class record\n"
3110 my $error = $svc_x->insert(%insert_args);
3113 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
3115 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
3118 my $return = { 'svc' => $part_svc->svc,
3120 map { $_ => $svc_x->get($_) } @$return_fields
3122 warn "_provision returning ". Dumper($return). "\n"
3131 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3132 return { 'error' => $session } if $context eq 'error';
3134 my $search = { 'custnum' => $custnum };
3135 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3136 my $cust_main = qsearchs('cust_main', $search )
3137 or return { 'error' => "unknown custnum $custnum" };
3139 my $pkgnum = $p->{'pkgnum'};
3141 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3142 'pkgnum' => $pkgnum,
3144 or return { 'error' => "unknown pkgnum $pkgnum" };
3146 my $svcpart = $p->{'svcpart'};
3148 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
3149 'svcpart' => $svcpart, } )
3150 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
3151 my $part_svc = $pkg_svc->part_svc;
3153 my $conf = new FS::Conf;
3156 'svc' => $part_svc->svc,
3157 'svcdb' => $part_svc->svcdb,
3158 'pkgnum' => $pkgnum,
3159 'svcpart' => $svcpart,
3160 'custnum' => $custnum,
3162 'security_phrase' => 0, #XXX !
3163 'svc_acct_pop' => [], #XXX !
3165 'init_popstate' => '',
3170 small_custview( $cust_main, $conf->config('countrydefault') ),
3174 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
3175 && $ret->{'svcdb'} eq 'svc_phone') {
3176 $ret->{'svcnum'} = $p->{'svcnum'};
3177 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
3178 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
3179 $ret->{'email'} = $svc_phone->email;
3180 $ret->{'forwarddst'} = $svc_phone->forwarddst;
3184 if ($ret->{'svcdb'} eq 'svc_forward') {
3185 $ret->{'forward_emails'} = {$cust_pkg->forward_emails()};
3191 sub unprovision_svc {
3194 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3195 return { 'error' => $session } if $context eq 'error';
3197 my $search = { 'custnum' => $custnum };
3198 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3199 my $cust_main = qsearchs('cust_main', $search )
3200 or return { 'error' => "unknown custnum $custnum" };
3202 my $svcnum = $p->{'svcnum'};
3204 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
3205 or return { 'error' => "unknown svcnum $svcnum" };
3207 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
3208 unless $cust_svc->cust_pkg->custnum == $custnum;
3210 my $conf = new FS::Conf;
3212 return { 'svc' => $cust_svc->part_svc->svc,
3213 'error' => $cust_svc->cancel,
3215 small_custview( $cust_main, $conf->config('countrydefault') ),
3220 sub myaccount_passwd {
3222 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3223 return { 'error' => $session } if $context eq 'error';
3225 return { 'error' => "New passwords don't match." }
3226 if $p->{'new_password'} ne $p->{'new_password2'};
3228 return { 'error' => 'Enter new password' }
3229 unless length($p->{'new_password'});
3231 #my $search = { 'custnum' => $custnum };
3232 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3233 $custnum =~ /^(\d+)$/ or die "illegal custnum";
3234 my $search = " AND custnum = $1";
3235 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
3237 my $svc_acct = qsearchs( {
3238 'table' => 'svc_acct',
3239 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
3240 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
3241 'LEFT JOIN cust_main USING ( custnum ) ',
3242 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
3243 'extra_sql' => $search, #important
3245 or return { 'error' => "Service not found" };
3249 my $conf = new FS::Conf;
3251 return { 'error' => 'Incorrect current password.' }
3252 if ( exists($p->{'old_password'})
3253 || $conf->exists('selfservice-password_change_oldpass')
3255 && ! $svc_acct->check_password($p->{'old_password'});
3257 $error ||= $svc_acct->is_password_allowed($p->{'new_password'})
3258 || $svc_acct->set_password($p->{'new_password'})
3259 || $svc_acct->replace();
3261 #regular pw change in self-service should change contact pw too, otherwise its
3262 #way too confusing. hell its confusing they're separate at all, but alas.
3263 #need to support the "ISP provides email that's used as a contact email" case
3265 my $contact = FS::contact->by_selfservice_email($svc_acct->email);
3266 if ( $contact && qsearchs('cust_contact', { contactnum=> $contact->contactnum,
3267 custnum => $custnum,
3268 selfservice_access => 'Y',
3272 #svc_acct was successful but this one returns an error? "shouldn't happen"
3273 #don't recheck is_password_allowed here; if the svc_acct password was
3274 #legal, that's good enough
3275 $error ||= $contact->change_password($p->{'new_password'});
3278 my($label, $value) = $svc_acct->cust_svc->label;
3280 return { 'error' => $error,
3290 my $info = skin_info($p);
3292 my $conf = new FS::Conf;
3293 my $verification = $conf->config('selfservice-password_reset_verification')
3294 or return { %$info, 'error' => 'Password resets disabled' };
3299 if ( $p->{'email'} ) { #new-style, changes contact and svc_acct
3301 $contact = FS::contact->by_selfservice_email($p->{'email'});
3304 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
3305 $cust_main = $cust_contact[0]->cust_main if scalar(@cust_contact) == 1;
3308 #also look for an svc_acct, otherwise it would be super confusing
3310 my($username, $domain) = split('@', $p->{'email'});
3311 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } );
3312 if ( $svc_domain ) {
3313 $svc_acct = qsearchs('svc_acct', { 'username' => $username,
3314 'domsvc' => $svc_domain->svcnum }
3317 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
3318 $cust_main ||= $cust_pkg->cust_main if $cust_pkg;
3320 #precaution: don't change svc_acct password not part of the same
3321 # customer as contact
3322 $svc_acct = '' if ! $cust_pkg
3323 || $cust_pkg->custnum != $cust_main->custnum;
3328 return { %$info, 'error' => 'Email address not found' }
3329 unless $contact || $svc_acct;
3331 } elsif ( $p->{'username'} ) { #old style, looks in svc_acct only
3333 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
3334 or return { %$info, 'error' => 'Account not found' };
3336 $svc_acct = qsearchs('svc_acct', { 'username' => $p->{'username'},
3337 'domsvc' => $svc_domain->svcnum }
3339 or return { %$info, 'error' => 'Account not found' };
3341 my $cust_pkg = $svc_acct->cust_svc->cust_pkg
3342 or return { %$info, 'error' => 'Account not found' };
3344 $cust_main = $cust_pkg->cust_main;
3348 return { %$info, 'error' => 'Multi-customer contacts incompatible with customer-based verification' }
3349 if ! $cust_main && $verification ne 'email';
3352 'email' => sub { 1; },
3354 my( $p, $cust_main ) = @_;
3355 $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/
3356 && $p->{'paymask'} eq substr($cust_main->paymask, -4)
3359 my( $p, $cust_main ) = @_;
3360 my $cust_pay = qsearchs({
3361 'table' => 'cust_pay',
3362 'hashref' => { 'custnum' => $cust_main->custnum },
3363 'order_by' => 'ORDER BY _date DESC LIMIT 1',
3367 $p->{'amount'} == $cust_pay->paid;
3370 my( $p, $cust_main ) = @_;
3371 $p->{'zip'} eq $cust_main->zip
3372 || ( $cust_main->ship_zip && $p->{'zip'} eq $cust_main->ship_zip );
3376 foreach my $verify ( split(',', $verification) ) {
3378 &{ $verify{$verify} }( $p, $cust_main )
3379 or return { %$info, 'error' => 'Account not found' };
3383 #okay, we're verified
3387 my $error = $contact->send_reset_email(
3388 'svcnum' => ($svc_acct ? $svc_acct->svcnum : ''),
3392 return { %$info, 'error' => $error }; #????
3395 } elsif ( $svc_acct ) {
3397 #create a unique session
3399 my $reset_session = {
3400 'svcnum' => $svc_acct->svcnum,
3401 'agentnum' => $svc_acct->cust_main->agentnum,
3404 my $timeout = '1 hour'; #?
3406 my $reset_session_id;
3408 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
3409 } until ( ! defined _cache->get("reset_passwd_$reset_session_id") );
3412 _cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
3416 my $msgnum = $conf->config('selfservice-password_reset_msgnum',
3417 $cust_main->agentnum);
3418 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
3419 return { %$info, 'error' => "selfservice-password_reset_msgnum unset" }
3421 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
3422 my $error = $msg_template->send( 'cust_main' => $cust_main,
3423 'object' => $svc_acct,
3424 'substitutions' => {
3425 'session_id' => $reset_session_id,
3429 return { %$info, 'error' => $error }; #????
3434 return { %$info, 'error' => '' };
3437 sub check_reset_passwd {
3440 my $conf = new FS::Conf;
3441 my $verification = $conf->config('selfservice-password_reset_verification')
3442 or return { 'error' => 'Password resets disabled' };
3444 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3445 or return { 'error' => "Can't resume session" }; #better error message
3447 if ( $reset_session->{'svcnum'} ) {
3449 my $svcnum = $reset_session->{'svcnum'};
3451 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3452 or return { 'error' => "Service not found" };
3454 $p->{'agentnum'} = $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3455 my $info = skin_info($p);
3459 'session_id' => $p->{'session_id'},
3460 'username' => $svc_acct->username,
3463 } elsif ( $reset_session->{'contactnum'} ) {
3465 my $contactnum = $reset_session->{'contactnum'};
3467 my $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3468 or return { 'error' => "Contact not found" };
3470 my @contact_email = $contact->contact_email;
3471 return { 'error' => 'No contact email' } unless @contact_email;
3473 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
3474 $p->{'agentnum'} = $cust_contact[0]->cust_main->agentnum
3475 if scalar(@cust_contact) == 1;
3476 my $info = skin_info($p);
3480 'session_id' => $p->{'session_id'},
3481 'email' => $contact_email[0]->email, #the first?
3486 return { 'error' => 'No svcnum or contactnum in session' }; #??
3492 sub process_reset_passwd {
3495 my $conf = new FS::Conf;
3496 my $verification = $conf->config('selfservice-password_reset_verification')
3497 or return { 'error' => 'Password resets disabled' };
3499 my $reset_session = _cache->get('reset_passwd_'. $p->{'session_id'})
3500 or return { 'error' => "Can't resume session" }; #better error message
3505 if ( $reset_session->{'svcnum'} ) {
3507 my $svcnum = $reset_session->{'svcnum'};
3509 $svc_acct = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
3510 or return { 'error' => "Service not found" };
3512 $p->{'agentnum'} ||= $svc_acct->cust_svc->cust_pkg->cust_main->agentnum;
3513 $info ||= skin_info($p);
3518 if ( $reset_session->{'contactnum'} ) {
3520 my $contactnum = $reset_session->{'contactnum'};
3522 $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
3523 or return { 'error' => "Contact not found" };
3525 my @cust_contact = grep $_->selfservice_access, $contact->cust_contact;
3526 $p->{'agentnum'} = $cust_contact[0]->cust_main->agentnum
3527 if scalar(@cust_contact) == 1;
3528 $info ||= skin_info($p);
3532 return { %$info, 'error' => "New passwords don't match." }
3533 if $p->{'new_password'} ne $p->{'new_password2'};
3535 return { %$info, 'error' => 'Enter new password' }
3536 unless length($p->{'new_password'});
3540 my $error ||= $svc_acct->is_password_allowed($p->{'new_password'})
3541 || $svc_acct->set_password($p->{'new_password'})
3542 || $svc_acct->replace();
3544 return { %$info, 'error' => $error } if $error;
3546 #my($label, $value) = $svc_acct->cust_svc->label;
3547 #return { 'error' => $error,
3548 # #'label' => $label,
3549 # #'value' => $value,
3556 my $error = $contact->is_password_allowed($p->{'new_password'})
3557 || $contact->change_password($p->{'new_password'});
3559 return { %$info, 'error' => $error }; # if $error;
3563 #password changed ,so remove session, don't want it reused
3564 _cache->remove($p->{'session_id'});
3566 return { %$info, 'error' => '' };
3570 sub validate_passwd {
3574 %result = ( 'fieldid' => $p->{'fieldid'} )
3575 if $p->{'fieldid'} =~ /^\w+$/;
3577 return { %result, 'password_invalid' => 'Enter new password' }
3578 unless length($p->{'check_password'});
3581 if ($p->{'svcnum'}) {
3582 # false laziness with myaccount_passwd
3583 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3584 return { %result, 'error' => $session } if $context eq 'error';
3586 $custnum =~ /^(\d+)$/ or die "illegal custnum";
3587 my $search = " AND custnum = $1";
3588 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
3590 $svc_acct = qsearchs( {
3591 'table' => 'svc_acct',
3592 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
3593 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
3594 'LEFT JOIN cust_main USING ( custnum ) ',
3595 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
3596 'extra_sql' => $search, #important
3598 or return { %result, 'error' => "Service not found" };
3599 # end false laziness
3602 $svc_acct ||= new FS::svc_acct {};
3604 my $error = $svc_acct->is_password_allowed($p->{'check_password'});
3605 return { %result, 'password_invalid' => $error } if $error;
3606 return { %result, 'password_valid' => 1 };
3611 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3612 return { 'error' => $session } if $context eq 'error';
3615 if ( $session->{'pkgnum'} ) {
3617 #tickets for specific service with pkg-balances on
3618 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
3619 'pkgnum' => $session->{'pkgnum'} })
3620 or return { 'error' => 'unknown package' };
3621 foreach my $cust_svc ( $cust_pkg->cust_svc ) {
3622 push @tickets, $cust_svc->tickets( $p->{status} );
3627 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
3628 or return { 'error' => "unknown custnum $custnum" };
3630 @tickets = $cust_main->tickets( $p->{status} );
3633 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
3634 if ( $FS::TicketSystem::system && FS::TicketSystem->selfservice_priority ) {
3636 @tickets = grep { $_->{'_selfservice_priority'}
3637 !~ /^\s*(closed?|resolved?|done)\s*/i }
3640 my $conf = new FS::Conf;
3641 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
3645 ($a->{'_selfservice_priority'} eq '') <=>
3646 ($b->{'_selfservice_priority'} eq '')
3649 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
3655 +{ tickets => \@tickets };
3662 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3663 return { 'error' => $session } if $context eq 'error';
3665 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
3666 FS::TicketSystem->init();
3668 my $conf = new FS::Conf;
3669 my $queue = $p->{'queue'}
3670 || $conf->config('ticket_system-selfservice_queueid')
3671 || $conf->config('ticket_system-default_queueid');
3673 warn "$me create_ticket: creating ticket\n" if $DEBUG;
3674 my $err_or_ticket = FS::TicketSystem->create_ticket(
3675 '', #create RT session based on FS CurrentUser (fs_selfservice)
3677 'custnum' => $custnum,
3678 'svcnum' => $session->{'svcnum'},
3679 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
3682 if ( ref($err_or_ticket) ) {
3683 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
3685 return { 'error' => '',
3686 'ticket_id' => $err_or_ticket->id,
3689 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
3691 return { 'error' => $err_or_ticket };
3699 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3700 return { 'error' => $session } if $context eq 'error';
3702 return { error => 'requested format not implemented' }
3703 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
3705 my $conf = new FS::Conf;
3706 my $age_threshold = 0;
3707 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
3708 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
3710 my $search = { 'custnum' => $custnum };
3711 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
3712 my $cust_main = qsearchs('cust_main', $search )
3713 or return { 'error' => "unknown custnum $custnum" };
3715 # does it make more sense to just run one sql query for this instead of all the
3716 # insanity below? would increase performance greately for large data sets?
3718 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
3719 my @part_svc = $cust_pkg->part_svc;
3720 foreach my $part_svc ( @part_svc ) {
3721 if($part_svc->svcdb eq 'svc_phone'){
3722 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
3723 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
3724 push @svc_phone, $cust_pkg_svc->svc_x
3725 if $cust_pkg_svc->date_inserted >= $age_threshold;
3733 my($xls_r,$xls_c) = (0,0);
3736 my @fields = qw( countrycode phonenum pin sip_password phone_name );
3737 if($p->{'format'} eq 'csv') {
3738 $csv = new Text::CSV_XS { 'always_quote' => 1,
3741 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
3742 $content .= $csv->string;
3744 elsif($p->{'format'} eq 'xls') {
3745 my $XLS1 = new IO::Scalar \$content;
3746 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
3747 or return { 'error' => "Error opening .xls file: $!" };
3748 $xls = $xls_workbook->add_worksheet('DIDs');
3749 foreach ( @fields ) {
3750 $xls->write(0,$xls_c++,$_);
3755 foreach my $svc_phone ( @svc_phone ) {
3756 my @cols = map { $svc_phone->$_ } @fields;
3757 if($p->{'format'} eq 'csv') {
3758 return { 'error' => 'Unable to create CSV' }
3759 unless $csv->combine(@cols);
3760 $content .= $csv->string;
3762 elsif($p->{'format'} eq 'xls') {
3765 $xls->write($xls_r,$xls_c++,$_);
3771 $xls_workbook->close() if $p->{'format'} eq 'xls';
3773 { content => $content, format => $p->{'format'}, };
3778 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3779 return { 'error' => $session } if $context eq 'error';
3781 # warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
3782 # FS::TicketSystem->init();
3783 # return { 'error' => 'get_ticket configuration error' }
3784 # if $FS::TicketSystem::system ne 'RT_Internal';
3786 # check existence and ownership as part of this
3787 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
3788 my $rt_session = FS::TicketSystem->session('');
3789 my $Ticket = FS::TicketSystem->get_ticket_object(
3791 ticket_id => $p->{'ticket_id'},
3794 return { 'error' => 'ticket not found' } if !$Ticket;
3796 if ( length( $p->{'subject'} || '' ) ) {
3798 if ( $p->{'subject'} ne $Ticket->Subject ) {
3799 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
3800 return { 'error' => "unable to set subject: $msg" } if !$val;
3804 if(length($p->{'reply'})) {
3805 my @err_or_res = FS::TicketSystem->correspond_ticket(
3807 'ticket_id' => $p->{'ticket_id'},
3808 'content' => $p->{'reply'},
3811 return { 'error' => 'unable to reply to ticket' }
3812 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
3815 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
3816 my $err_or_ticket = FS::TicketSystem->get_ticket(
3818 'ticket_id' => $p->{'ticket_id'},
3821 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
3822 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
3824 return { 'error' => $err_or_ticket };
3827 my @custs = @{$err_or_ticket->{'custs'}};
3828 my @txns = @{$err_or_ticket->{'txns'}};
3831 # superseded by check in get_ticket_object
3832 #return { 'error' => 'invalid ticket requested' }
3833 #unless grep($_ eq $custnum, @custs);
3835 foreach my $txn ( @txns ) {
3836 push @filtered_txns, $txn
3837 if ($txn->{'type'} eq 'EmailRecord'
3838 || $txn->{'type'} eq 'Correspond'
3839 || $txn->{'type'} eq 'Create');
3842 warn "$me get_ticket: successful: \n"
3844 return { 'error' => '',
3845 'transactions' => \@filtered_txns,
3846 'ticket_fields' => $err_or_ticket->{'fields'},
3847 'ticket_id' => $p->{'ticket_id'},
3851 sub adjust_ticket_priority {
3853 my($context, $session, $custnum) = _custoragent_session_custnum($p);
3854 return { 'error' => $session } if $context eq 'error';
3856 # warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
3857 # FS::TicketSystem->init;
3858 my $ss_priority = FS::TicketSystem->selfservice_priority;
3860 return { 'error' => 'adjust_ticket_priority configuration error' }
3861 if $FS::TicketSystem::system ne 'RT_Internal'
3864 my $values = $p->{'values'}; #hashref, id => priority value
3867 foreach my $id (keys %$values) {
3868 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
3869 my $Ticket = FS::TicketSystem->get_ticket_object('',
3871 'custnum' => $custnum,
3874 $ticket_error{$id} = 'ticket not found';
3878 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
3879 # We're not going to implement it for RT_External.
3880 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
3881 my $new_value = $values->{$id};
3882 next if $old_value eq $new_value;
3884 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
3886 # AddCustomFieldValue works fine (replacing any existing value) if it's
3887 # a single-valued custom field, which it should be. If it's not, you're
3888 # doing something wrong.
3890 if ( length($new_value) ) {
3891 ($val, $msg) = $Ticket->AddCustomFieldValue(
3892 Field => $ss_priority,
3893 Value => $new_value,
3897 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
3898 Field => $ss_priority,
3899 Value => $old_value,
3903 $ticket_error{$id} = $msg if !$val;
3904 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
3906 return { 'error' => '',
3907 'ticket_error' => \%ticket_error,
3908 %{ customer_info($p) } # send updated customer info back
3914 sub _custoragent_session_custnum {
3917 my($context, $session, $custnum);
3918 if ( $p->{'session_id'} ) {
3920 $context = 'customer';
3921 $session = _cache->get($p->{'session_id'})
3922 or return ( 'error' => "Can't resume session" ); #better error message
3923 $custnum = $session->{'custnum'};
3925 } elsif ( $p->{'agent_session_id'} ) {
3928 my $agent_cache = new FS::ClientAPI_SessionCache( {
3929 'namespace' => 'FS::ClientAPI::Agent',
3931 $session = $agent_cache->get($p->{'agent_session_id'})
3932 or return ( 'error' => "Can't resume session" ); #better error message
3933 $custnum = $p->{'custnum'};
3937 return ( 'error' => "Can't resume session" ); #better error message
3940 ($context, $session, $custnum);