1 package FS::ClientAPI::MyAccount;
3 use 5.008; #require 5.8+ for Time::Local 1.05+
5 use vars qw( $cache $DEBUG $me );
6 use subs qw( _cache _provision );
8 use Digest::MD5 qw(md5_hex);
10 use Business::CreditCard;
12 use Time::Local qw(timelocal_nocheck);
13 use FS::UI::Web::small_custview qw(small_custview); #less doh
15 use FS::UI::bytecount qw( display_bytecount );
18 use FS::Record qw(qsearch qsearchs dbh);
19 use FS::Msgcat qw(gettext);
20 use FS::Misc qw(card_types);
21 use FS::Misc::DateTime qw(parse_datetime);
22 use FS::ClientAPI_SessionCache;
31 use FS::cust_main_county;
34 use FS::acct_rt_transaction;
39 use Spreadsheet::WriteExcel;
42 $me = '[FS::ClientAPI::MyAccount]';
44 use vars qw( @cust_main_editable_fields );
45 @cust_main_editable_fields = qw(
46 first last company address1 address2 city
47 county state zip country
48 daytime night fax mobile
49 ship_first ship_last ship_company ship_address1 ship_address2 ship_city
50 ship_state ship_zip ship_country
51 ship_daytime ship_night ship_fax ship_mobile
53 payby payinfo payname paystart_month paystart_year payissue payip
54 ss paytype paystate stateid stateid_state
57 BEGIN { #preload to reduce time customer_info takes
58 if ( $FS::TicketSystem::system ) {
59 warn "$me: initializing ticket system\n" if $DEBUG;
60 FS::TicketSystem->init();
65 $cache ||= new FS::ClientAPI_SessionCache( {
66 'namespace' => 'FS::ClientAPI::MyAccount',
73 my($context, $session, $custnum) = _custoragent_session_custnum($p);
74 #return { 'error' => $session } if $context eq 'error';
77 if ( $context eq 'customer' ) {
79 my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
82 $sth->execute($custnum) or die $sth->errstr;
84 $agentnum = $sth->fetchrow_arrayref->[0]
85 or die "no agentnum for custnum $custnum";
87 #} elsif ( $context eq 'agent' ) {
88 } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
92 my $conf = new FS::Conf;
94 #false laziness w/Signup.pm
96 my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
98 if ( $skin_info_cache_agent ) {
100 warn "$me loading cached skin info for agentnum $agentnum\n"
105 warn "$me populating skin info cache for agentnum $agentnum\n"
108 $skin_info_cache_agent = {
109 'agentnum' => $agentnum,
110 ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
111 qw( company_name date_format ) ),
112 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
113 qw( body_bgcolor box_bgcolor
114 text_color link_color vlink_color hlink_color alink_color
115 font title_color title_align title_size menu_bgcolor menu_fontsize
118 ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
119 qw( menu_skipblanks menu_skipheadings menu_nounderline )
121 ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
122 qw( title_left_image title_right_image
123 menu_top_image menu_body_image menu_bottom_image
126 'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
127 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
128 qw( head body_header body_footer company_address ) ),
131 _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
135 #{ %$skin_info_cache_agent };
136 $skin_info_cache_agent;
143 my $conf = new FS::Conf;
147 'phone_login' => $conf->exists('selfservice_server-phone_login'),
148 'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
155 #false laziness w/FS::ClientAPI::passwd::passwd
159 my $conf = new FS::Conf;
162 if ( $p->{'domain'} eq 'svc_phone'
163 && $conf->exists('selfservice_server-phone_login') ) {
165 my $svc_phone = qsearchs( 'svc_phone', { 'phonenum' => $p->{'username'} } );
166 return { error => 'Number not found.' } unless $svc_phone;
169 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
170 #return { error => 'Only primary user may log in.' }
171 # if $conf->exists('selfservice_server-primary_only')
172 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
174 return { error => 'Incorrect PIN.' }
175 unless $svc_phone->check_pin($p->{'password'});
181 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
182 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
184 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
185 'domsvc' => $svc_domain->svcnum, }
187 return { error => 'User not found.' } unless $svc_acct;
189 if($conf->exists('selfservice_server-login_svcpart')) {
190 my @svcpart = $conf->config('selfservice_server-login_svcpart');
191 my $svcpart = $svc_acct->cust_svc->svcpart;
192 return { error => 'Invalid user.' }
193 unless grep($_ eq $svcpart, @svcpart);
196 return { error => 'Incorrect password.' }
197 unless $svc_acct->check_password($p->{'password'});
204 'svcnum' => $svc_x->svcnum,
207 my $cust_svc = $svc_x->cust_svc;
208 my $cust_pkg = $cust_svc->cust_pkg;
210 my $cust_main = $cust_pkg->cust_main;
211 $session->{'custnum'} = $cust_main->custnum;
212 if ( $conf->exists('pkg-balances') ) {
213 my @cust_pkg = grep { $_->part_pkg->freq !~ /^(0|$)/ }
214 $cust_main->ncancelled_pkgs;
215 $session->{'pkgnum'} = $cust_pkg->pkgnum
216 if scalar(@cust_pkg) > 1;
220 #my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
221 #return { error => 'Only primary user may log in.' }
222 # if $conf->exists('selfservice_server-primary_only')
223 # && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
224 my $part_pkg = $cust_pkg->part_pkg;
225 return { error => 'Only primary user may log in.' }
226 if $conf->exists('selfservice_server-primary_only')
227 && $cust_svc->svcpart != $part_pkg->svcpart([qw( svc_acct svc_phone )]);
231 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
232 } until ( ! defined _cache->get($session_id) ); #just in case
234 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
235 _cache->set( $session_id, $session, $timeout );
237 return { 'error' => '',
238 'session_id' => $session_id,
244 if ( $p->{'session_id'} ) {
245 _cache->remove($p->{'session_id'});
246 return { %{ skin_info($p) }, 'error' => '' };
248 return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
252 sub payment_gateway {
254 # takes a cust_main and a cust_payby entry, returns the payment_gateway
255 my $conf = new FS::Conf;
256 my $cust_main = shift;
257 my $cust_payby = shift;
258 my $gatewaynum = $conf->config('selfservice-payment_gateway');
260 my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
261 die "configured gatewaynum $gatewaynum not found!" if !$pg;
265 return '' if ! FS::payby->realtime($cust_payby);
266 my $pg = $cust_main->agent->payment_gateway(
267 'method' => FS::payby->payby2bop($cust_payby),
277 my $conf = new FS::Conf;
279 my $info = skin_info($p);
281 use vars qw( $cust_paybys ); #cache for performance
282 unless ( $cust_paybys ) {
284 my %cust_paybys = map { $_ => 1 }
285 map { FS::payby->payby2payment($_) }
286 $conf->config('signup_server-payby');
288 $cust_paybys = [ keys %cust_paybys ];
291 $info->{'cust_paybys'} = $cust_paybys;
293 my($context, $session, $custnum) = _custoragent_session_custnum($p);
294 return { 'error' => $session } if $context eq 'error';
296 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
297 or return { 'error' => "unknown custnum $custnum" };
299 $info->{'hide_payment_fields'} = [
301 my $pg = payment_gateway($cust_main, $_);
302 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
303 } @{ $info->{cust_paybys} }
306 $info->{'self_suspend_reason'} =
307 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum);
309 $info->{'edit_ticket_subject'} =
310 $conf->exists('ticket_system-selfservice_edit_subject') &&
311 $cust_main->edit_subject;
314 'custnum' => $custnum,
315 'access_pkgnum' => $session->{'pkgnum'},
316 'access_svcnum' => $session->{'svcnum'},
323 my($context, $session, $custnum) = _custoragent_session_custnum($p);
324 return { 'error' => $session } if $context eq 'error';
328 my $conf = new FS::Conf;
329 if ($conf->exists('cust_main-require_address2')) {
330 $return{'require_address2'} = '1';
332 $return{'require_address2'} = '';
335 if ( $FS::TicketSystem::system ) {
336 warn "$me customer_info: initializing ticket system\n" if $DEBUG;
337 FS::TicketSystem->init();
340 if ( $custnum ) { #customer record
342 my $search = { 'custnum' => $custnum };
343 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
344 my $cust_main = qsearchs('cust_main', $search )
345 or return { 'error' => "unknown custnum $custnum" };
347 if ( $session->{'pkgnum'} ) {
348 $return{balance} = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
350 $return{balance} = $cust_main->balance;
353 my @tickets = $cust_main->tickets;
354 # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
355 if ( FS::TicketSystem->selfservice_priority ) {
356 my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
360 ($a->{'_selfservice_priority'} eq '') <=>
361 ($b->{'_selfservice_priority'} eq '')
364 ($b->{'_selfservice_priority'} <=> $a->{'_selfservice_priority'})
370 $return{tickets} = \@tickets;
373 unless ( $session->{'pkgnum'} ) {
376 invnum => $_->invnum,
377 date => time2str("%b %o, %Y", $_->_date),
380 } $cust_main->open_cust_bill;
381 $return{open_invoices} = \@open;
384 $return{small_custview} =
385 small_custview( $cust_main,
386 scalar($conf->config('countrydefault')),
387 ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
390 $return{name} = $cust_main->name;
391 $return{ship_name} = $cust_main->ship_name;
393 for (@cust_main_editable_fields) {
394 $return{$_} = $cust_main->get($_);
397 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
398 $return{payinfo} = $cust_main->paymask;
399 @return{'month', 'year'} = $cust_main->paydate_monthyear;
402 $return{'invoicing_list'} =
403 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
404 $return{'postal_invoicing'} =
405 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
407 if (scalar($conf->config('support_packages'))) {
408 my @support_services = ();
409 foreach ($cust_main->support_services) {
410 my $seconds = $_->svc_x->seconds;
411 my $time_remaining = (($seconds < 0) ? '-' : '' ).
412 int(abs($seconds)/3600)."h".
413 sprintf("%02d",(abs($seconds)%3600)/60)."m";
414 my $cust_pkg = $_->cust_pkg;
417 $pkgnum = $cust_pkg->pkgnum if $cust_pkg;
418 $pkg = $cust_pkg->part_pkg->pkg if $cust_pkg;
419 push @support_services, { svcnum => $_->svcnum,
420 time => $time_remaining,
425 $return{support_services} = \@support_services;
428 if ( $conf->config('prepayment_discounts-credit_type') ) {
430 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
433 if ( $session->{'svcnum'} ) {
434 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
435 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
438 } elsif ( $session->{'svcnum'} ) { #no customer record
440 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
441 or die "unknown svcnum";
442 $return{name} = $svc_acct->email;
446 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
450 return { 'error' => '',
451 'custnum' => $custnum,
457 sub customer_info_short {
460 my($context, $session, $custnum) = _custoragent_session_custnum($p);
461 return { 'error' => $session } if $context eq 'error';
465 my $conf = new FS::Conf;
467 if ( $custnum ) { #customer record
469 my $search = { 'custnum' => $custnum };
470 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
471 my $cust_main = qsearchs('cust_main', $search )
472 or return { 'error' => "unknown custnum $custnum" };
474 $return{small_custview} =
475 small_custview( $cust_main,
476 scalar($conf->config('countrydefault')),
480 $return{name} = $cust_main->name;
481 $return{ship_name} = $cust_main->ship_name;
483 $return{payby} = $cust_main->payby;
485 #none of these are terribly expensive if we want 'em...
486 for (@cust_main_editable_fields) {
487 $return{$_} = $cust_main->get($_);
490 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
491 $return{payinfo} = $cust_main->paymask;
492 @return{'month', 'year'} = $cust_main->paydate_monthyear;
495 $return{'invoicing_list'} =
496 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
497 #$return{'postal_invoicing'} =
498 # 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
500 if ( $session->{'svcnum'} ) {
501 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
502 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
505 } elsif ( $session->{'svcnum'} ) { #no customer record
507 #uuh, not supproted yet... die?
508 return { 'error' => 'customer_info_short not yet supported as agent' };
512 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
516 return { 'error' => '',
517 'custnum' => $custnum,
524 my $session = _cache->get($p->{'session_id'})
525 or return { 'error' => "Can't resume session" }; #better error message
527 my $custnum = $session->{'custnum'}
528 or return { 'error' => "no customer record" };
530 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
531 or return { 'error' => "unknown custnum $custnum" };
533 my $new = new FS::cust_main { $cust_main->hash };
534 $new->set( $_ => $p->{$_} )
535 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
538 if (exists($p->{'payby'})) {
539 $p->{'payby'} =~ /^([A-Z]{4})$/
540 or return { 'error' => "illegal_payby " . $p->{'payby'} };
544 if ( $payby =~ /^(CARD|DCRD)$/ ) {
546 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
548 if ( $new->payinfo eq $cust_main->paymask ) {
549 $new->payinfo($cust_main->payinfo);
551 $new->payinfo($p->{'payinfo'});
554 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
556 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
559 $p->{'payinfo1'} =~ /^([\dx]+)$/
560 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
562 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by -require-bank-branch?
563 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
565 $payinfo = $payinfo1. '@'. $payinfo2;
567 $new->payinfo( ($payinfo eq $cust_main->paymask)
568 ? $cust_main->payinfo
572 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
574 } elsif ( $payby =~ /^(BILL)$/ ) {
576 } elsif ( $payby ) { #notyet ready
577 return { 'error' => "unknown payby $payby" };
581 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
582 #false laziness with httemplate/edit/process/cust_main.cgi
583 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
584 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
586 @invoicing_list = $cust_main->invoicing_list;
589 my $error = $new->replace($cust_main, \@invoicing_list);
590 return { 'error' => $error } if $error;
593 return { 'error' => '' };
598 my $session = _cache->get($p->{'session_id'})
599 or return { 'error' => "Can't resume session" }; #better error message
605 my $conf = new FS::Conf;
606 use vars qw($payment_info); #cache for performance
607 unless ( $payment_info ) {
609 my %states = map { $_->state => 1 }
610 qsearch('cust_main_county', {
611 'country' => $conf->config('countrydefault') || 'US'
614 my %cust_paybys = map { $_ => 1 }
615 map { FS::payby->payby2payment($_) }
616 $conf->config('signup_server-payby');
618 my @cust_paybys = keys %cust_paybys;
622 #list all counties/states/countries
623 'cust_main_county' =>
624 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
626 #shortcut for one-country folks
628 [ sort { $a cmp $b } keys %states ],
630 'card_types' => card_types(),
632 'paytypes' => [ @FS::cust_main::paytypes ],
634 'paybys' => [ $conf->config('signup_server-payby') ],
635 'cust_paybys' => \@cust_paybys,
637 'stateid_label' => FS::Msgcat::_gettext('stateid'),
638 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
640 'show_ss' => $conf->exists('show_ss'),
641 'show_stateid' => $conf->exists('show_stateid'),
642 'show_paystate' => $conf->exists('show_bankstate'),
644 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
646 'credit_card_surcharge_percentage' => $conf->config('credit-card-surcharge-percentage'),
655 my %return = %$payment_info;
657 my $custnum = $session->{'custnum'};
659 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
660 or return { 'error' => "unknown custnum $custnum" };
662 $return{'hide_payment_fields'} = [
664 my $pg = payment_gateway($cust_main, $_);
665 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
666 } @{ $return{cust_paybys} }
669 $return{balance} = $cust_main->balance; #XXX pkg-balances?
671 $return{payname} = $cust_main->payname
672 || ( $cust_main->first. ' '. $cust_main->get('last') );
674 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
676 $return{payby} = $cust_main->payby;
677 $return{stateid_state} = $cust_main->stateid_state;
679 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
680 $return{card_type} = cardtype($cust_main->payinfo);
681 $return{payinfo} = $cust_main->paymask;
683 @return{'month', 'year'} = $cust_main->paydate_monthyear;
687 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
688 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
689 $return{payinfo1} = $payinfo1;
690 $return{payinfo2} = $payinfo2;
691 $return{paytype} = $cust_main->paytype;
692 $return{paystate} = $cust_main->paystate;
696 if ( $conf->config('prepayment_discounts-credit_type') ) {
698 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
701 #doubleclick protection
703 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
705 return { 'error' => '',
711 #some false laziness with httemplate/process/payment.cgi - look there for
712 #ACH and CVV support stuff
714 sub validate_payment {
717 my $session = _cache->get($p->{'session_id'})
718 or return { 'error' => "Can't resume session" }; #better error message
720 my $custnum = $session->{'custnum'};
722 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
723 or return { 'error' => "unknown custnum $custnum" };
725 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
726 or return { 'error' => gettext('illegal_amount') };
728 return { error => 'Amount must be greater than 0' } unless $amount > 0;
730 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
731 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
732 my $discount_term = $1;
734 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
735 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
738 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
739 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
742 $p->{'payby'} ||= 'CARD';
743 $p->{'payby'} =~ /^([A-Z]{4})$/
744 or return { 'error' => "illegal_payby " . $p->{'payby'} };
747 #false laziness w/process/payment.cgi
750 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
752 $p->{'payinfo1'} =~ /^([\dx]+)$/
753 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
755 $p->{'payinfo2'} =~ /^([\dx]+)$/
756 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
758 $payinfo = $payinfo1. '@'. $payinfo2;
760 $payinfo = $cust_main->payinfo
761 if $cust_main->paymask eq $payinfo;
763 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
765 $payinfo = $p->{'payinfo'};
767 #more intelligent mathing will be needed here if you change
768 #card_masking_method and don't remove existing paymasks
769 $payinfo = $cust_main->payinfo
770 if $cust_main->paymask eq $payinfo;
773 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
774 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
778 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
779 return { 'error' => gettext('unknown_card_type') }
780 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
782 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
783 if ( cardtype($payinfo) eq 'American Express card' ) {
784 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
785 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
788 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
789 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
795 die "unknown payby $payby";
799 'CARD' => [ qw( paystart_month paystart_year payissue payip
800 address1 address2 city state zip country ) ],
801 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
805 $card_type = cardtype($payinfo) if $payby eq 'CARD';
808 'cust_main' => $cust_main, #XXX or just custnum??
811 'payinfo' => $payinfo,
812 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
813 'card_type' => $card_type,
814 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
815 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
816 'payname' => $payname,
817 'paybatch' => $paybatch, #this doesn't actually do anything
819 'payname' => $payname,
820 'discount_term' => $discount_term,
821 'pkgnum' => $session->{'pkgnum'},
822 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
832 my $validate = validate_payment($p);
833 return $validate if $validate->{'error'};
835 my $conf = new FS::Conf;
836 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
837 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
839 +{ map { $_=>$validate->{$_} }
840 qw( card_type paymask payname paydate_pretty amount )
845 sub process_stored_payment {
848 my $session_id = $p->{'session_id'};
850 my $payment_info = _cache->get( "payment_$session_id" )
851 or return { 'error' => "Can't resume session" }; #better error message
853 do_process_payment($payment_info);
857 sub process_payment {
860 my $payment_info = validate_payment($p);
861 return $payment_info if $payment_info->{'error'};
863 do_process_payment($payment_info);
867 sub do_process_payment {
868 my $validate = shift;
870 my $cust_main = $validate->{'cust_main'};
872 my $amount = delete $validate->{'amount'};
875 my $payby = delete $validate->{'payby'};
877 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
880 'paynum_ref' => \$paynum,
883 return { 'error' => $error } if $error;
885 $cust_main->apply_payments;
887 if ( $validate->{'save'} ) {
888 my $new = new FS::cust_main { $cust_main->hash };
889 if ($validate->{'payby'} eq 'CARD' || $validate->{'payby'} eq 'DCRD') {
890 $new->set( $_ => $validate->{$_} )
891 foreach qw( payname paystart_month paystart_year payissue payip
892 address1 address2 city state zip country );
893 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
894 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
895 $new->set( $_ => $validate->{$_} )
896 foreach qw( payname payip paytype paystate
897 stateid stateid_state );
898 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
900 $new->set( 'payinfo' => $cust_main->card_token || $validate->{'payinfo'} );
901 $new->set( 'paydate' => $validate->{'paydate'} );
902 my $error = $new->replace($cust_main);
904 #no, this causes customers to process their payments again
905 #return { 'error' => $error };
906 #XXX just warn verosely for now so i can figure out how these happen in
907 # the first place, eventually should redirect them to the "change
908 #address" page but indicate the payment did process??
909 delete($validate->{'payinfo'}); #don't want to log this!
910 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
911 "NEW: ". Dumper($new)."\n".
912 "OLD: ". Dumper($cust_main)."\n".
913 "PACKET: ". Dumper($validate)."\n";
921 my $receipt_html = '';
923 # currently supported for realtime CC only; send receipt data to SS
924 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
927 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
930 <TD ALIGN="right">Payment#</TD>
931 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
935 <TD ALIGN="right">Date</TD>
937 <TD BGCOLOR="#FFFFFF"><B>! .
938 time2str("%a %b %o, %Y %r", $cust_pay->_date)
944 <TD ALIGN="right">Amount</TD>
945 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
950 <TD ALIGN="right">Payment method</TD>
951 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
962 my($gw, $auth, $order) = split(':', $cust_pay->paybatch);
966 'amount' => sprintf('%.2f', $cust_pay->paid),
967 'date' => $cust_pay->_date,
968 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
969 'time_pretty' => time2str('%T', $cust_pay->_date),
971 'order_num' => $order,
972 'receipt_html' => $receipt_html,
979 'receipt_html' => '',
986 sub realtime_collect {
989 my $session = _cache->get($p->{'session_id'})
990 or return { 'error' => "Can't resume session" }; #better error message
992 my $custnum = $session->{'custnum'};
994 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
995 or return { 'error' => "unknown custnum $custnum" };
998 if ( $p->{'amount'} ) {
999 $amount = $p->{'amount'};
1001 elsif ( $session->{'pkgnum'} ) {
1002 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1005 $amount = $cust_main->balance;
1008 my $error = $cust_main->realtime_collect(
1009 'method' => $p->{'method'},
1010 'amount' => $amount,
1011 'pkgnum' => $session->{'pkgnum'},
1012 'session_id' => $p->{'session_id'},
1016 return { 'error' => $error } unless ref( $error );
1018 return { 'error' => '', amount => $amount, %$error };
1021 sub process_payment_order_pkg {
1024 my $hr = process_payment($p);
1025 return $hr if $hr->{'error'};
1030 sub process_payment_order_renew {
1033 my $hr = process_payment($p);
1034 return $hr if $hr->{'error'};
1039 sub process_prepay {
1043 my $session = _cache->get($p->{'session_id'})
1044 or return { 'error' => "Can't resume session" }; #better error message
1048 my $custnum = $session->{'custnum'};
1050 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1051 or return { 'error' => "unknown custnum $custnum" };
1053 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1054 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1062 return { 'error' => $error } if $error;
1064 return { 'error' => '',
1065 'amount' => $amount,
1066 'seconds' => $seconds,
1067 'duration' => duration_exact($seconds),
1068 'upbytes' => $upbytes,
1069 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1070 'downbytes' => $downbytes,
1071 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1072 'totalbytes'=> $totalbytes,
1073 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1080 my $session = _cache->get($p->{'session_id'})
1081 or return { 'error' => "Can't resume session" }; #better error message
1083 my $custnum = $session->{'custnum'};
1085 my $invnum = $p->{'invnum'};
1087 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1088 'custnum' => $custnum } )
1089 or return { 'error' => "Can't find invnum" };
1093 return { 'error' => '',
1094 'invnum' => $invnum,
1095 'invoice_text' => join('', $cust_bill->print_text ),
1096 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1103 my $session = _cache->get($p->{'session_id'})
1104 or return { 'error' => "Can't resume session" }; #better error message
1106 my $custnum = $session->{'custnum'};
1108 my $invnum = $p->{'invnum'};
1110 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1111 'custnum' => $custnum } )
1112 or return { 'error' => "Can't find invnum" };
1116 return { 'error' => '',
1117 'invnum' => $invnum,
1118 'invoice_pdf' => $cust_bill->print_pdf( { unsquelch_cdr => 1 } ),
1126 #sessioning for this? how do we get the session id to the backend invoice
1127 # template so it can add it to the link, blah
1130 if ( $p->{'invnum'} ) {
1131 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1132 or return { 'error' => 'unknown invnum' };
1133 $agentnum = $cust_bill->cust_main->agentnum;
1136 my $templatename = $p->{'template'} || $p->{'templatename'};
1138 #false laziness-ish w/view/cust_bill-logo.cgi
1140 my $conf = new FS::Conf;
1141 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1142 $templatename = "_$1";
1147 my $filename = "logo$templatename.png";
1149 return { 'error' => '',
1150 'logo' => $conf->config_binary($filename, $agentnum),
1151 'content_type' => 'image/png', #should allow gif, jpg too
1158 my $session = _cache->get($p->{'session_id'})
1159 or return { 'error' => "Can't resume session" }; #better error message
1161 my $custnum = $session->{'custnum'};
1163 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1164 or return { 'error' => "unknown custnum $custnum" };
1166 my @cust_bill = $cust_main->cust_bill;
1170 return { 'error' => '',
1171 'balance' => $cust_main->balance,
1174 my $owed = $_->owed;
1176 +{ 'invnum' => $_->invnum,
1177 '_date' => $_->_date,
1178 'date' => time2str("%b %o, %Y", $_->_date),
1179 'date_short' => time2str("%m-%d-%Y", $_->_date),
1180 'previous' => sprintf('%.2f', ($_->previous)[0]),
1181 'charged' => sprintf('%.2f', $_->charged),
1182 'owed' => sprintf('%.2f', $owed),
1183 'balance' => sprintf('%.2f', $balance),
1193 my $session = _cache->get($p->{'session_id'})
1194 or return { 'error' => "Can't resume session" }; #better error message
1196 my $custnum = $session->{'custnum'};
1198 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1199 or return { 'error' => "unknown custnum $custnum" };
1201 my @errors = $cust_main->cancel( 'quiet'=>1 );
1203 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1205 return { 'error' => $error };
1212 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1213 return { 'error' => $session } if $context eq 'error';
1215 my $search = { 'custnum' => $custnum };
1216 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1217 my $cust_main = qsearchs('cust_main', $search )
1218 or return { 'error' => "unknown custnum $custnum" };
1220 my $conf = new FS::Conf;
1222 # the duplication below is necessary:
1223 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1224 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1225 # non-backwards-compatible change breaking the software of anyone using the API
1226 # instead of the stock selfservice
1227 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1228 if( $conf->exists('selfservice_server-view-wholesale') ) {
1229 return { 'svcnum' => $session->{'svcnum'},
1230 'custnum' => $custnum,
1231 'cust_pkg' => [ map {
1233 part_pkg => [ map $_->hashref, $_->part_pkg ],
1235 [ map $_->hashref, $_->available_part_svc ],
1237 [ map { my $ref = { $_->hash,
1238 label => [ $_->label ],
1240 $ref->{_password} = $_->svc_x->_password
1241 if $context eq 'agent'
1242 && $conf->exists('agent-showpasswords')
1243 && $_->part_svc->svcdb eq 'svc_acct';
1248 } $cust_main->cust_pkg
1251 small_custview( $cust_main, $conf->config('countrydefault') ),
1252 'wholesale_view' => 1,
1253 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1254 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1255 'lnp' => $conf->exists('svc_phone-lnp'),
1259 { 'svcnum' => $session->{'svcnum'},
1260 'custnum' => $custnum,
1261 'cust_pkg' => [ map {
1262 my $primary_cust_svc = $_->primary_cust_svc;
1265 status => $_->status,
1267 [ map $_->hashref, $_->available_part_svc ],
1269 [ map { my $ref = { $_->hash,
1270 label => [ $_->label ],
1272 $ref->{_password} = $_->svc_x->_password
1273 if $context eq 'agent'
1274 && $conf->exists('agent-showpasswords')
1275 && $_->part_svc->svcdb eq 'svc_acct';
1276 $ref->{svchash} = { $_->svc_x->hash } if
1277 $_->part_svc->svcdb eq 'svc_phone';
1278 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1279 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1285 ? { $primary_cust_svc->hash,
1286 label => [ $primary_cust_svc->label ],
1287 finger => $primary_cust_svc->svc_x->finger, #uuh
1288 $primary_cust_svc->part_svc->hash,
1292 } $cust_main->ncancelled_pkgs
1295 small_custview( $cust_main, $conf->config('countrydefault') ),
1303 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1304 return { 'error' => $session } if $context eq 'error';
1306 my $search = { 'custnum' => $custnum };
1307 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1308 my $cust_main = qsearchs('cust_main', $search )
1309 or return { 'error' => "unknown custnum $custnum" };
1312 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1313 foreach my $cust_pkg ( $p->{'ncancelled'}
1314 ? $cust_main->ncancelled_pkgs
1315 : $cust_main->unsuspended_pkgs ) {
1316 next if $session->{'pkgnum'} && $cust_pkg->pkgnum != $session->{'pkgnum'};
1317 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1319 if ( $p->{'svcdb'} ) {
1320 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1322 : ref($p->{'svcdb'}) eq 'ARRAY'
1323 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1324 : { $p->{'svcdb'} => 1 };
1325 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1328 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1331 my $conf = new FS::Conf;
1334 'svcnum' => $session->{'svcnum'},
1335 'custnum' => $custnum,
1336 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1337 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1340 my $svc_x = $_->svc_x;
1341 my($label, $value) = $_->label;
1342 my $svcdb = $_->part_svc->svcdb;
1343 my $part_pkg = $_->cust_pkg->part_pkg;
1346 'svcnum' => $_->svcnum,
1352 if ( $svcdb eq 'svc_acct' ) {
1355 'username' => $svc_x->username,
1356 'email' => $svc_x->email,
1357 'seconds' => $svc_x->seconds,
1358 'upbytes' => display_bytecount($svc_x->upbytes),
1359 'downbytes' => display_bytecount($svc_x->downbytes),
1360 'totalbytes' => display_bytecount($svc_x->totalbytes),
1362 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1363 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1364 'recharge_upbytes' =>
1365 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1366 'recharge_downbytes' =>
1367 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1368 'recharge_totalbytes' =>
1369 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1373 } elsif ( $svcdb eq 'svc_phone' || $svcdb eq 'svc_port' ) {
1389 _usage_details( \&_port_graph, $p,
1390 'svcdb' => 'svc_port',
1395 my($svc_port, $begin, $end) = @_;
1397 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
1398 push @usage, { 'png' => $pngOrError };
1402 sub _list_svc_usage {
1403 my($svc_acct, $begin, $end) = @_;
1405 foreach my $part_export (
1406 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1407 qw( sqlradius sqlradius_withdomain )
1409 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1414 sub list_svc_usage {
1415 _usage_details(\&_list_svc_usage, @_);
1418 sub _list_support_usage {
1419 my($svc_acct, $begin, $end) = @_;
1421 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1422 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1424 push @usage, { 'seconds' => $_->seconds,
1425 'support' => $_->support,
1426 '_date' => $_->_date,
1427 'id' => $_->transaction_id,
1428 'creator' => $_->creator,
1429 'subject' => $_->subject,
1430 'status' => $_->status,
1431 'ticketid' => $_->ticketid,
1437 sub list_support_usage {
1438 _usage_details(\&_list_support_usage, @_);
1441 sub _list_cdr_usage {
1442 my($svc_phone, $begin, $end) = @_;
1443 map [ $_->downstream_csv('format' => 'default', 'keeparray' => 1) ], #XXX config for format
1444 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1447 sub list_cdr_usage {
1449 _usage_details( \&_list_cdr_usage, $p,
1450 'svcdb' => 'svc_phone',
1454 sub _usage_details {
1455 my($callback, $p, %opt) = @_;
1457 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1458 return { 'error' => $session } if $context eq 'error';
1460 my $search = { 'svcnum' => $p->{'svcnum'} };
1461 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1463 my $svcdb = $opt{'svcdb'} || 'svc_acct';
1465 my $svc_x = qsearchs( $svcdb, $search );
1466 return { 'error' => 'No service selected in list_svc_usage' }
1469 my $header = $svcdb eq 'svc_phone'
1470 ? [ split(',', FS::cdr::invoice_header('default') ) ] #XXX
1473 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
1474 my $freq = $cust_pkg->part_pkg->freq;
1475 my $start = $cust_pkg->setup;
1476 #my $end = $cust_pkg->bill; # or time?
1479 unless ( $p->{beginning} ) {
1480 $p->{beginning} = $cust_pkg->last_bill;
1481 $p->{ending} = $end;
1484 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending});
1486 #kinda false laziness with FS::cust_main::bill, but perhaps
1487 #we should really change this bit to DateTime and DateTime::Duration
1489 #change this bit to use Date::Manip? CAREFUL with timezones (see
1490 # mailing list archive)
1491 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
1492 (localtime($p->{ending}) )[0,1,2,3,4,5];
1493 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
1494 (localtime($p->{beginning}) )[0,1,2,3,4,5];
1496 if ( $freq =~ /^\d+$/ ) {
1498 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
1500 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
1501 } elsif ( $freq =~ /^(\d+)w$/ ) {
1503 $nmday += $weeks * 7;
1504 $pmday -= $weeks * 7;
1505 } elsif ( $freq =~ /^(\d+)d$/ ) {
1509 } elsif ( $freq =~ /^(\d+)h$/ ) {
1514 return { 'error' => "unparsable frequency: ". $freq };
1517 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
1518 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
1522 'svcnum' => $p->{svcnum},
1523 'beginning' => $p->{beginning},
1524 'ending' => $p->{ending},
1525 'previous' => ($previous > $start) ? $previous : $start,
1526 'next' => ($next < $end) ? $next : $end,
1527 'header' => $header,
1535 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1536 return { 'error' => $session } if $context eq 'error';
1538 my $search = { 'custnum' => $custnum };
1539 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1540 my $cust_main = qsearchs('cust_main', $search )
1541 or return { 'error' => "unknown custnum $custnum" };
1543 my $status = $cust_main->status;
1544 #false laziness w/ClientAPI/Signup.pm
1546 my $cust_pkg = new FS::cust_pkg ( {
1547 'custnum' => $custnum,
1548 'pkgpart' => $p->{'pkgpart'},
1550 my $error = $cust_pkg->check;
1551 return { 'error' => $error } if $error;
1554 unless ( $p->{'svcpart'} eq 'none' ) {
1558 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
1560 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1561 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
1562 $svcdb = $part_svc->svcdb;
1564 $svcdb = 'svc_acct';
1566 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
1569 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
1570 'svc_domain' => [ qw( domain ) ],
1571 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
1572 'svc_external' => [ qw( id title ) ],
1573 'svc_pbx' => [ qw( id title ) ],
1576 my $svc_x = "FS::$svcdb"->new( {
1577 'svcpart' => $svcpart,
1578 map { $_ => $p->{$_} } @{$fields{$svcdb}}
1581 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
1584 while ( length($p->{"snarf_machine$snarfnum"}) ) {
1585 my $acct_snarf = new FS::acct_snarf ( {
1586 'machine' => $p->{"snarf_machine$snarfnum"},
1587 'protocol' => $p->{"snarf_protocol$snarfnum"},
1588 'username' => $p->{"snarf_username$snarfnum"},
1589 '_password' => $p->{"snarf_password$snarfnum"},
1592 push @acct_snarf, $acct_snarf;
1594 $svc_x->child_objects( \@acct_snarf );
1597 my $y = $svc_x->setdefault; # arguably should be in new method
1598 return { 'error' => $y } if $y && !ref($y);
1600 $error = $svc_x->check;
1601 return { 'error' => $error } if $error;
1608 tie my %hash, 'Tie::RefHash';
1609 %hash = ( $cust_pkg => \@svc );
1611 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
1612 return { 'error' => $error } if $error;
1614 my $conf = new FS::Conf;
1615 if ( $conf->exists('signup_server-realtime') ) {
1617 my $bill_error = _do_bop_realtime( $cust_main, $status );
1620 $cust_pkg->cancel('quiet'=>1);
1623 $cust_pkg->reexport;
1627 $cust_pkg->reexport;
1630 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
1632 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
1639 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1640 return { 'error' => $session } if $context eq 'error';
1642 my $search = { 'custnum' => $custnum };
1643 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1644 my $cust_main = qsearchs('cust_main', $search )
1645 or return { 'error' => "unknown custnum $custnum" };
1647 my $status = $cust_main->status;
1648 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
1649 or return { 'error' => "unknown package $p->{pkgnum}" };
1652 my $error = FS::cust_pkg::order( $custnum,
1658 my $conf = new FS::Conf;
1659 if ( $conf->exists('signup_server-realtime') ) {
1661 my $bill_error = _do_bop_realtime( $cust_main, $status );
1664 $newpkg[0]->suspend;
1667 $newpkg[0]->reexport;
1671 $newpkg[0]->reexport;
1674 return { error => '', pkgnum => $cust_pkg->pkgnum };
1678 sub order_recharge {
1681 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1682 return { 'error' => $session } if $context eq 'error';
1684 my $search = { 'custnum' => $custnum };
1685 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1686 my $cust_main = qsearchs('cust_main', $search )
1687 or return { 'error' => "unknown custnum $custnum" };
1689 my $status = $cust_main->status;
1690 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
1691 or return { 'error' => "unknown service " . $p->{'svcnum'} };
1693 my $svc_x = $cust_svc->svc_x;
1694 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
1697 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
1698 qw ( recharge_seconds recharge_upbytes recharge_downbytes
1699 recharge_totalbytes );
1700 my $amount = $part_pkg->option('recharge_amount', 1);
1702 my ($l, $v, $d) = $cust_svc->label; # blah
1703 my $pkg = "Recharge $v";
1705 my $bill_error = $cust_main->charge($amount, $pkg,
1706 "time: $vhash{seconds}, up: $vhash{upbytes}," .
1707 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
1708 $part_pkg->taxclass); #meh
1710 my $conf = new FS::Conf;
1711 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
1713 $bill_error = _do_bop_realtime( $cust_main, $status );
1718 my $error = $svc_x->recharge (\%vhash);
1719 return { 'error' => $error } if $error;
1723 my $error = $bill_error;
1724 $error ||= $svc_x->recharge (\%vhash);
1725 return { 'error' => $error } if $error;
1728 return { error => '', svc => $cust_svc->part_svc->svc };
1732 sub _do_bop_realtime {
1733 my ($cust_main, $status) = (shift, shift);
1735 my $old_balance = $cust_main->balance;
1737 my $bill_error = $cust_main->bill
1738 || $cust_main->apply_payments_and_credits
1739 || $cust_main->realtime_collect('selfservice' => 1);
1741 if ( $cust_main->balance > $old_balance
1742 && $cust_main->balance > 0
1743 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
1744 1 : $status eq 'suspended' ) ) {
1745 #this makes sense. credit is "un-doing" the invoice
1746 my $conf = new FS::Conf;
1747 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
1748 'self-service decline',
1749 'reason_type' => $conf->config('signup_credit_type'),
1751 $cust_main->apply_credits( 'order' => 'newest' );
1753 return { 'error' => '_decline', 'bill_error' => $bill_error };
1762 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1763 return { 'error' => $session } if $context eq 'error';
1765 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1766 or return { 'error' => "unknown custnum $custnum" };
1768 my @cust_pkg = sort { $a->bill <=> $b->bill }
1769 grep { $_->part_pkg->freq ne '0' }
1770 $cust_main->ncancelled_pkgs;
1772 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
1774 my $total = $cust_main->balance;
1777 my $bill = $_->bill;
1778 $total += $_->part_pkg->base_recur($_, \$bill);
1779 my $renew_date = $_->part_pkg->add_freq($_->bill);
1781 'pkgnum' => $_->pkgnum,
1782 'amount' => sprintf('%.2f', $total),
1783 'bill_date' => $_->bill,
1784 'bill_date_pretty' => time2str('%x', $_->bill),
1785 'renew_date' => $renew_date,
1786 'renew_date_pretty' => time2str('%x', $renew_date),
1787 'expire_date' => $_->expire,
1788 'expire_date_pretty' => time2str('%x', $_->expire),
1793 return { 'dates' => \@array };
1797 sub payment_info_renew_info {
1799 my $renew_info = renew_info($p);
1800 my $payment_info = payment_info($p);
1801 return { %$renew_info,
1809 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1810 return { 'error' => $session } if $context eq 'error';
1812 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1813 or return { 'error' => "unknown custnum $custnum" };
1815 my $date = $p->{'date'};
1819 #freeside-daily -n -d $date fs_daily $custnum
1820 $cust_main->bill_and_collect( 'time' => $date,
1821 'invoice_time' => $now,
1822 'actual_time' => $now,
1823 'check_freq' => '1d',
1826 return { 'error' => '' };
1832 my $session = _cache->get($p->{'session_id'})
1833 or return { 'error' => "Can't resume session" }; #better error message
1835 my $custnum = $session->{'custnum'};
1837 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1838 or return { 'error' => "unknown custnum $custnum" };
1840 my $conf = new FS::Conf;
1842 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
1843 or return { 'error' => 'Permission denied' };
1845 my $pkgnum = $p->{'pkgnum'};
1847 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1848 'pkgnum' => $pkgnum, } )
1849 or return { 'error' => "unknown pkgnum $pkgnum" };
1851 my $error = $cust_pkg->suspend(reason => $reasonnum);
1852 return { 'error' => $error };
1858 my $session = _cache->get($p->{'session_id'})
1859 or return { 'error' => "Can't resume session" }; #better error message
1861 my $custnum = $session->{'custnum'};
1863 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1864 or return { 'error' => "unknown custnum $custnum" };
1866 my $pkgnum = $p->{'pkgnum'};
1868 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1869 'pkgnum' => $pkgnum, } )
1870 or return { 'error' => "unknown pkgnum $pkgnum" };
1872 my $error = $cust_pkg->cancel('quiet' => 1);
1873 return { 'error' => $error };
1877 sub provision_phone {
1880 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
1882 if($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/){
1883 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1884 return { 'error' => $session } if $context eq 'error';
1886 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
1887 return { 'error' => 'service not found' } unless $svc_phone;
1888 return { 'error' => 'invalid svcnum' }
1889 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
1891 $svc_phone->email($p->{'email'})
1892 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
1893 $svc_phone->forwarddst($p->{'forwarddst'})
1894 if $svc_phone->forwarddst ne $p->{'forwarddst'}
1895 && $p->{'forwarddst'} =~ /^(\d+|)$/;
1896 return { 'error' => $svc_phone->replace };
1900 unless($p->{'lnp'}) {
1901 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
1902 $p->{'lnp_status'} = "portingin";
1903 return _provision( 'FS::svc_phone',
1904 [qw(lnp_desired_due_date lnp_other_provider
1905 lnp_other_provider_account phonenum countrycode lnp_status)],
1906 [qw(phonenum countrycode)],
1913 unless (scalar(@bulkdid)) {
1914 return _provision( 'FS::svc_phone',
1915 [qw(phonenum countrycode)],
1916 [qw(phonenum countrycode)],
1922 # bulk DID order case
1924 foreach my $did ( @bulkdid ) {
1925 $did =~ s/[^0-9]//g;
1926 $error = _provision( 'FS::svc_phone',
1927 [qw(phonenum countrycode)],
1928 [qw(phonenum countrycode)],
1930 'pkgnum' => $p->{'pkgnum'},
1931 'svcpart' => $p->{'svcpart'},
1933 'countrycode' => $p->{'countrycode'},
1934 'session_id' => $p->{'session_id'},
1937 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
1939 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
1942 sub provision_acct {
1944 warn "provision_acct called\n"
1947 return { 'error' => gettext('passwords_dont_match') }
1948 if $p->{'_password'} ne $p->{'_password2'};
1949 return { 'error' => gettext('empty_password') }
1950 unless length($p->{'_password'});
1952 if ($p->{'domsvc'}) {
1953 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
1954 qw ( svcpart pkgnum ) );
1955 return { 'error' => gettext('invalid_domain') }
1956 unless ($domains{$p->{'domsvc'}});
1959 warn "provision_acct calling _provision\n"
1961 _provision( 'FS::svc_acct',
1962 [qw(username _password domsvc)],
1963 [qw(username _password domsvc)],
1969 sub provision_external {
1971 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
1972 _provision( 'FS::svc_external',
1981 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
1982 warn "_provision called for $class\n"
1985 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1986 return { 'error' => $session } if $context eq 'error';
1988 my $search = { 'custnum' => $custnum };
1989 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1990 my $cust_main = qsearchs('cust_main', $search )
1991 or return { 'error' => "unknown custnum $custnum" };
1993 my $pkgnum = $p->{'pkgnum'};
1995 warn "searching for custnum $custnum pkgnum $pkgnum\n"
1997 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1998 'pkgnum' => $pkgnum,
2000 or return { 'error' => "unknown pkgnum $pkgnum" };
2002 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
2004 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
2005 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
2007 warn "creating $class record\n"
2009 my $svc_x = $class->new( {
2010 'pkgnum' => $p->{'pkgnum'},
2011 'svcpart' => $p->{'svcpart'},
2012 map { $_ => $p->{$_} } @$fields
2014 warn "inserting $class record\n"
2016 my $error = $svc_x->insert;
2019 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
2021 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
2024 my $return = { 'svc' => $part_svc->svc,
2026 map { $_ => $svc_x->get($_) } @$return_fields
2028 warn "_provision returning ". Dumper($return). "\n"
2037 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2038 return { 'error' => $session } if $context eq 'error';
2040 my $search = { 'custnum' => $custnum };
2041 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2042 my $cust_main = qsearchs('cust_main', $search )
2043 or return { 'error' => "unknown custnum $custnum" };
2045 my $pkgnum = $p->{'pkgnum'};
2047 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2048 'pkgnum' => $pkgnum,
2050 or return { 'error' => "unknown pkgnum $pkgnum" };
2052 my $svcpart = $p->{'svcpart'};
2054 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
2055 'svcpart' => $svcpart, } )
2056 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
2057 my $part_svc = $pkg_svc->part_svc;
2059 my $conf = new FS::Conf;
2062 'svc' => $part_svc->svc,
2063 'svcdb' => $part_svc->svcdb,
2064 'pkgnum' => $pkgnum,
2065 'svcpart' => $svcpart,
2066 'custnum' => $custnum,
2068 'security_phrase' => 0, #XXX !
2069 'svc_acct_pop' => [], #XXX !
2071 'init_popstate' => '',
2076 small_custview( $cust_main, $conf->config('countrydefault') ),
2080 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
2081 && $ret->{'svcdb'} eq 'svc_phone') {
2082 $ret->{'svcnum'} = $p->{'svcnum'};
2083 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2084 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
2085 $ret->{'email'} = $svc_phone->email;
2086 $ret->{'forwarddst'} = $svc_phone->forwarddst;
2093 sub unprovision_svc {
2096 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2097 return { 'error' => $session } if $context eq 'error';
2099 my $search = { 'custnum' => $custnum };
2100 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2101 my $cust_main = qsearchs('cust_main', $search )
2102 or return { 'error' => "unknown custnum $custnum" };
2104 my $svcnum = $p->{'svcnum'};
2106 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
2107 or return { 'error' => "unknown svcnum $svcnum" };
2109 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
2110 unless $cust_svc->cust_pkg->custnum == $custnum;
2112 my $conf = new FS::Conf;
2114 return { 'svc' => $cust_svc->part_svc->svc,
2115 'error' => $cust_svc->cancel,
2117 small_custview( $cust_main, $conf->config('countrydefault') ),
2122 sub myaccount_passwd {
2124 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2125 return { 'error' => $session } if $context eq 'error';
2127 return { 'error' => "New passwords don't match." }
2128 if $p->{'new_password'} ne $p->{'new_password2'};
2130 return { 'error' => 'Enter new password' }
2131 unless length($p->{'new_password'});
2133 #my $search = { 'custnum' => $custnum };
2134 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2135 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2136 my $search = " AND custnum = $1";
2137 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2139 my $svc_acct = qsearchs( {
2140 'table' => 'svc_acct',
2141 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2142 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
2143 'LEFT JOIN cust_main USING ( custnum ) ',
2144 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
2145 'extra_sql' => $search, #important
2147 or return { 'error' => "Service not found" };
2149 $svc_acct->_password($p->{'new_password'});
2150 my $error = $svc_acct->replace();
2152 my($label, $value) = $svc_acct->cust_svc->label;
2154 return { 'error' => $error,
2163 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2164 return { 'error' => $session } if $context eq 'error';
2166 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
2167 FS::TicketSystem->init();
2169 my $conf = new FS::Conf;
2170 my $queue = $p->{'queue'}
2171 || $conf->config('ticket_system-selfservice_queueid')
2172 || $conf->config('ticket_system-default_queueid');
2174 warn "$me create_ticket: creating ticket\n" if $DEBUG;
2175 my $err_or_ticket = FS::TicketSystem->create_ticket(
2176 '', #create RT session based on FS CurrentUser (fs_selfservice)
2178 'custnum' => $custnum,
2179 'svcnum' => $session->{'svcnum'},
2180 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
2183 if ( ref($err_or_ticket) ) {
2184 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
2186 return { 'error' => '',
2187 'ticket_id' => $err_or_ticket->id,
2190 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
2192 return { 'error' => $err_or_ticket };
2200 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2201 return { 'error' => $session } if $context eq 'error';
2203 return { error => 'requested format not implemented' }
2204 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
2206 my $conf = new FS::Conf;
2207 my $age_threshold = 0;
2208 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
2209 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
2211 my $search = { 'custnum' => $custnum };
2212 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2213 my $cust_main = qsearchs('cust_main', $search )
2214 or return { 'error' => "unknown custnum $custnum" };
2216 # does it make more sense to just run one sql query for this instead of all the
2217 # insanity below? would increase performance greately for large data sets?
2219 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
2220 my @part_svc = $cust_pkg->part_svc;
2221 foreach my $part_svc ( @part_svc ) {
2222 if($part_svc->svcdb eq 'svc_phone'){
2223 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
2224 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
2225 push @svc_phone, $cust_pkg_svc->svc_x
2226 if $cust_pkg_svc->date_inserted >= $age_threshold;
2234 my($xls_r,$xls_c) = (0,0);
2237 my @fields = qw( countrycode phonenum pin sip_password phone_name );
2238 if($p->{'format'} eq 'csv') {
2239 $csv = new Text::CSV_XS { 'always_quote' => 1,
2242 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
2243 $content .= $csv->string;
2245 elsif($p->{'format'} eq 'xls') {
2246 my $XLS1 = new IO::Scalar \$content;
2247 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
2248 or return { 'error' => "Error opening .xls file: $!" };
2249 $xls = $xls_workbook->add_worksheet('DIDs');
2250 foreach ( @fields ) {
2251 $xls->write(0,$xls_c++,$_);
2256 foreach my $svc_phone ( @svc_phone ) {
2257 my @cols = map { $svc_phone->$_ } @fields;
2258 if($p->{'format'} eq 'csv') {
2259 return { 'error' => 'Unable to create CSV' }
2260 unless $csv->combine(@cols);
2261 $content .= $csv->string;
2263 elsif($p->{'format'} eq 'xls') {
2266 $xls->write($xls_r,$xls_c++,$_);
2272 $xls_workbook->close() if $p->{'format'} eq 'xls';
2274 { content => $content, format => $p->{'format'}, };
2279 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2280 return { 'error' => $session } if $context eq 'error';
2282 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
2283 FS::TicketSystem->init();
2284 return { 'error' => 'get_ticket configuration error' }
2285 if $FS::TicketSystem::system ne 'RT_Internal';
2287 # check existence and ownership as part of this
2288 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
2289 my $rt_session = FS::TicketSystem->session('');
2290 my $Ticket = FS::TicketSystem->get_ticket_object(
2292 ticket_id => $p->{'ticket_id'},
2295 return { 'error' => 'ticket not found' } if !$Ticket;
2297 if ( length( $p->{'subject'} || '' ) ) {
2299 if ( $p->{'subject'} ne $Ticket->Subject ) {
2300 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
2301 return { 'error' => "unable to set subject: $msg" } if !$val;
2305 if(length($p->{'reply'})) {
2306 my @err_or_res = FS::TicketSystem->correspond_ticket(
2308 'ticket_id' => $p->{'ticket_id'},
2309 'content' => $p->{'reply'},
2312 return { 'error' => 'unable to reply to ticket' }
2313 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
2316 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
2317 my $err_or_ticket = FS::TicketSystem->get_ticket(
2319 'ticket_id' => $p->{'ticket_id'},
2322 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
2323 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
2325 return { 'error' => $err_or_ticket };
2328 my @custs = @{$err_or_ticket->{'custs'}};
2329 my @txns = @{$err_or_ticket->{'txns'}};
2332 # superseded by check in get_ticket_object
2333 #return { 'error' => 'invalid ticket requested' }
2334 #unless grep($_ eq $custnum, @custs);
2336 foreach my $txn ( @txns ) {
2337 push @filtered_txns, $txn
2338 if ($txn->{'type'} eq 'EmailRecord'
2339 || $txn->{'type'} eq 'Correspond'
2340 || $txn->{'type'} eq 'Create');
2343 warn "$me get_ticket: successful: \n"
2345 return { 'error' => '',
2346 'transactions' => \@filtered_txns,
2347 'ticket_fields' => $err_or_ticket->{'fields'},
2348 'ticket_id' => $p->{'ticket_id'},
2352 sub adjust_ticket_priority {
2354 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2355 return { 'error' => $session } if $context eq 'error';
2357 warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
2358 FS::TicketSystem->init;
2359 my $ss_priority = FS::TicketSystem->selfservice_priority;
2361 return { 'error' => 'adjust_ticket_priority configuration error' }
2362 if $FS::TicketSystem::system ne 'RT_Internal'
2365 my $values = $p->{'values'}; #hashref, id => priority value
2368 foreach my $id (keys %$values) {
2369 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
2370 my $Ticket = FS::TicketSystem->get_ticket_object('',
2372 'custnum' => $custnum,
2375 $ticket_error{$id} = 'ticket not found';
2379 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
2380 # We're not going to implement it for RT_External.
2381 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
2382 my $new_value = $values->{$id};
2383 next if $old_value eq $new_value;
2385 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
2387 # AddCustomFieldValue works fine (replacing any existing value) if it's
2388 # a single-valued custom field, which it should be. If it's not, you're
2389 # doing something wrong.
2391 if ( length($new_value) ) {
2392 ($val, $msg) = $Ticket->AddCustomFieldValue(
2393 Field => $ss_priority,
2394 Value => $new_value,
2398 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
2399 Field => $ss_priority,
2400 Value => $old_value,
2404 $ticket_error{$id} = $msg if !$val;
2405 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
2407 return { 'error' => '',
2408 'ticket_error' => \%ticket_error,
2409 %{ customer_info($p) } # send updated customer info back
2415 sub _custoragent_session_custnum {
2418 my($context, $session, $custnum);
2419 if ( $p->{'session_id'} ) {
2421 $context = 'customer';
2422 $session = _cache->get($p->{'session_id'})
2423 or return ( 'error' => "Can't resume session" ); #better error message
2424 $custnum = $session->{'custnum'};
2426 } elsif ( $p->{'agent_session_id'} ) {
2429 my $agent_cache = new FS::ClientAPI_SessionCache( {
2430 'namespace' => 'FS::ClientAPI::Agent',
2432 $session = $agent_cache->get($p->{'agent_session_id'})
2433 or return ( 'error' => "Can't resume session" ); #better error message
2434 $custnum = $p->{'custnum'};
2438 return ( 'error' => "Can't resume session" ); #better error message
2441 ($context, $session, $custnum);