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->first. ' '. $cust_main->get('last');
391 $return{ship_name} = $cust_main->ship_first. ' '. $cust_main->get('ship_last');
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;
436 $return{'svcnum'} = $session->{'svcnum'};
439 } elsif ( $session->{'svcnum'} ) { #no customer record
441 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
442 or die "unknown svcnum";
443 $return{name} = $svc_acct->email;
447 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
451 return { 'error' => '',
452 'custnum' => $custnum,
458 sub customer_info_short {
461 my($context, $session, $custnum) = _custoragent_session_custnum($p);
462 return { 'error' => $session } if $context eq 'error';
466 my $conf = new FS::Conf;
468 if ( $custnum ) { #customer record
470 my $search = { 'custnum' => $custnum };
471 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
472 my $cust_main = qsearchs('cust_main', $search )
473 or return { 'error' => "unknown custnum $custnum" };
475 $return{small_custview} =
476 small_custview( $cust_main,
477 scalar($conf->config('countrydefault')),
481 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
482 $return{ship_name} = $cust_main->ship_first. ' '. $cust_main->get('ship_last');
484 $return{payby} = $cust_main->payby;
486 #none of these are terribly expensive if we want 'em...
487 for (@cust_main_editable_fields) {
488 $return{$_} = $cust_main->get($_);
491 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
492 $return{payinfo} = $cust_main->paymask;
493 @return{'month', 'year'} = $cust_main->paydate_monthyear;
496 $return{'invoicing_list'} =
497 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
498 #$return{'postal_invoicing'} =
499 # 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
501 if ( $session->{'svcnum'} ) {
502 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $session->{'svcnum'} });
503 $return{'svc_label'} = ($cust_svc->label)[1] if $cust_svc;
504 $return{'svcnum'} = $session->{'svcnum'};
507 } elsif ( $session->{'svcnum'} ) { #no customer record
509 #uuh, not supproted yet... die?
510 return { 'error' => 'customer_info_short not yet supported as agent' };
514 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
518 return { 'error' => '',
519 'custnum' => $custnum,
526 my $session = _cache->get($p->{'session_id'})
527 or return { 'error' => "Can't resume session" }; #better error message
529 my $custnum = $session->{'custnum'}
530 or return { 'error' => "no customer record" };
532 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
533 or return { 'error' => "unknown custnum $custnum" };
535 my $new = new FS::cust_main { $cust_main->hash };
536 $new->set( $_ => $p->{$_} )
537 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
540 if (exists($p->{'payby'})) {
541 $p->{'payby'} =~ /^([A-Z]{4})$/
542 or return { 'error' => "illegal_payby " . $p->{'payby'} };
546 if ( $payby =~ /^(CARD|DCRD)$/ ) {
548 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
550 if ( $new->payinfo eq $cust_main->paymask ) {
551 $new->payinfo($cust_main->payinfo);
553 $new->payinfo($p->{'payinfo'});
556 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
558 } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
561 $p->{'payinfo1'} =~ /^([\dx]+)$/
562 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
564 $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by -require-bank-branch?
565 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
567 $payinfo = $payinfo1. '@'. $payinfo2;
569 $new->payinfo( ($payinfo eq $cust_main->paymask)
570 ? $cust_main->payinfo
574 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
576 } elsif ( $payby =~ /^(BILL)$/ ) {
578 } elsif ( $payby ) { #notyet ready
579 return { 'error' => "unknown payby $payby" };
583 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
584 #false laziness with httemplate/edit/process/cust_main.cgi
585 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
586 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
588 @invoicing_list = $cust_main->invoicing_list;
591 my $error = $new->replace($cust_main, \@invoicing_list);
592 return { 'error' => $error } if $error;
595 return { 'error' => '' };
600 my $session = _cache->get($p->{'session_id'})
601 or return { 'error' => "Can't resume session" }; #better error message
607 my $conf = new FS::Conf;
608 use vars qw($payment_info); #cache for performance
609 unless ( $payment_info ) {
611 my %states = map { $_->state => 1 }
612 qsearch('cust_main_county', {
613 'country' => $conf->config('countrydefault') || 'US'
616 my %cust_paybys = map { $_ => 1 }
617 map { FS::payby->payby2payment($_) }
618 $conf->config('signup_server-payby');
620 my @cust_paybys = keys %cust_paybys;
624 #list all counties/states/countries
625 'cust_main_county' =>
626 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
628 #shortcut for one-country folks
630 [ sort { $a cmp $b } keys %states ],
632 'card_types' => card_types(),
634 'paytypes' => [ @FS::cust_main::paytypes ],
636 'paybys' => [ $conf->config('signup_server-payby') ],
637 'cust_paybys' => \@cust_paybys,
639 'stateid_label' => FS::Msgcat::_gettext('stateid'),
640 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
642 'show_ss' => $conf->exists('show_ss'),
643 'show_stateid' => $conf->exists('show_stateid'),
644 'show_paystate' => $conf->exists('show_bankstate'),
646 'save_unchecked' => $conf->exists('selfservice-save_unchecked'),
648 'credit_card_surcharge_percentage' => $conf->config('credit-card-surcharge-percentage'),
657 my %return = %$payment_info;
659 my $custnum = $session->{'custnum'};
661 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
662 or return { 'error' => "unknown custnum $custnum" };
664 $return{'hide_payment_fields'} = [
666 my $pg = payment_gateway($cust_main, $_);
667 $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
668 } @{ $return{cust_paybys} }
671 $return{balance} = $cust_main->balance; #XXX pkg-balances?
673 $return{payname} = $cust_main->payname
674 || ( $cust_main->first. ' '. $cust_main->get('last') );
676 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
678 $return{payby} = $cust_main->payby;
679 $return{stateid_state} = $cust_main->stateid_state;
681 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
682 $return{card_type} = cardtype($cust_main->payinfo);
683 $return{payinfo} = $cust_main->paymask;
685 @return{'month', 'year'} = $cust_main->paydate_monthyear;
689 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
690 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
691 $return{payinfo1} = $payinfo1;
692 $return{payinfo2} = $payinfo2;
693 $return{paytype} = $cust_main->paytype;
694 $return{paystate} = $cust_main->paystate;
698 if ( $conf->config('prepayment_discounts-credit_type') ) {
700 $return{discount_terms_hash} = { $cust_main->discount_terms_hash };
703 #doubleclick protection
705 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
707 return { 'error' => '',
713 #some false laziness with httemplate/process/payment.cgi - look there for
714 #ACH and CVV support stuff
716 sub validate_payment {
719 my $session = _cache->get($p->{'session_id'})
720 or return { 'error' => "Can't resume session" }; #better error message
722 my $custnum = $session->{'custnum'};
724 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
725 or return { 'error' => "unknown custnum $custnum" };
727 $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
728 or return { 'error' => gettext('illegal_amount') };
730 return { error => 'Amount must be greater than 0' } unless $amount > 0;
732 $p->{'discount_term'} =~ /^\s*(\d*)\s*$/
733 or return { 'error' => gettext('illegal_discount_term'). ': '. $p->{'discount_term'} };
734 my $discount_term = $1;
736 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
737 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
740 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
741 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
744 $p->{'payby'} ||= 'CARD';
745 $p->{'payby'} =~ /^([A-Z]{4})$/
746 or return { 'error' => "illegal_payby " . $p->{'payby'} };
749 #false laziness w/process/payment.cgi
752 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
754 $p->{'payinfo1'} =~ /^([\dx]+)$/
755 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
757 $p->{'payinfo2'} =~ /^([\dx]+)$/
758 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
760 $payinfo = $payinfo1. '@'. $payinfo2;
762 $payinfo = $cust_main->payinfo
763 if $cust_main->paymask eq $payinfo;
765 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
767 $payinfo = $p->{'payinfo'};
769 #more intelligent mathing will be needed here if you change
770 #card_masking_method and don't remove existing paymasks
771 $payinfo = $cust_main->payinfo
772 if $cust_main->paymask eq $payinfo;
775 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
776 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
780 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
781 return { 'error' => gettext('unknown_card_type') }
782 if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
784 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
785 if ( cardtype($payinfo) eq 'American Express card' ) {
786 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
787 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
790 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
791 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
797 die "unknown payby $payby";
801 'CARD' => [ qw( paystart_month paystart_year payissue payip
802 address1 address2 city state zip country ) ],
803 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
807 $card_type = cardtype($payinfo) if $payby eq 'CARD';
810 'cust_main' => $cust_main, #XXX or just custnum??
813 'payinfo' => $payinfo,
814 'paymask' => $cust_main->mask_payinfo( $payby, $payinfo ),
815 'card_type' => $card_type,
816 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
817 'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
818 'payname' => $payname,
819 'paybatch' => $paybatch, #this doesn't actually do anything
821 'payname' => $payname,
822 'discount_term' => $discount_term,
823 'pkgnum' => $session->{'pkgnum'},
824 map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
834 my $validate = validate_payment($p);
835 return $validate if $validate->{'error'};
837 my $conf = new FS::Conf;
838 my $timeout = $conf->config('selfservice-session_timeout') || '1 hour'; #?
839 _cache->set( 'payment_'.$p->{'session_id'}, $validate, $timeout );
841 +{ map { $_=>$validate->{$_} }
842 qw( card_type paymask payname paydate_pretty amount )
847 sub process_stored_payment {
850 my $session_id = $p->{'session_id'};
852 my $payment_info = _cache->get( "payment_$session_id" )
853 or return { 'error' => "Can't resume session" }; #better error message
855 do_process_payment($payment_info);
859 sub process_payment {
862 my $payment_info = validate_payment($p);
863 return $payment_info if $payment_info->{'error'};
865 do_process_payment($payment_info);
869 sub do_process_payment {
870 my $validate = shift;
872 my $cust_main = $validate->{'cust_main'};
874 my $amount = delete $validate->{'amount'};
877 my $payby = delete $validate->{'payby'};
879 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
882 'paynum_ref' => \$paynum,
885 return { 'error' => $error } if $error;
887 $cust_main->apply_payments;
889 if ( $validate->{'save'} ) {
890 my $new = new FS::cust_main { $cust_main->hash };
891 if ($validate->{'payby'} eq 'CARD' || $validate->{'payby'} eq 'DCRD') {
892 $new->set( $_ => $validate->{$_} )
893 foreach qw( payname paystart_month paystart_year payissue payip
894 address1 address2 city state zip country );
895 $new->set( 'payby' => $validate->{'auto'} ? 'CARD' : 'DCRD' );
896 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
897 $new->set( $_ => $validate->{$_} )
898 foreach qw( payname payip paytype paystate
899 stateid stateid_state );
900 $new->set( 'payby' => $validate->{'auto'} ? 'CHEK' : 'DCHK' );
902 $new->set( 'payinfo' => $cust_main->card_token || $validate->{'payinfo'} );
903 $new->set( 'paydate' => $validate->{'paydate'} );
904 my $error = $new->replace($cust_main);
906 #no, this causes customers to process their payments again
907 #return { 'error' => $error };
908 #XXX just warn verosely for now so i can figure out how these happen in
909 # the first place, eventually should redirect them to the "change
910 #address" page but indicate the payment did process??
911 delete($validate->{'payinfo'}); #don't want to log this!
912 warn "WARNING: error changing customer info when processing payment (not returning to customer as a processing error): $error\n".
913 "NEW: ". Dumper($new)."\n".
914 "OLD: ". Dumper($cust_main)."\n".
915 "PACKET: ". Dumper($validate)."\n";
923 my $receipt_html = '';
925 # currently supported for realtime CC only; send receipt data to SS
926 $cust_pay = qsearchs('cust_pay', { 'paynum' => $paynum } );
929 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
932 <TD ALIGN="right">Payment#</TD>
933 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->paynum . qq!</B></TD>
937 <TD ALIGN="right">Date</TD>
939 <TD BGCOLOR="#FFFFFF"><B>! .
940 time2str("%a %b %o, %Y %r", $cust_pay->_date)
946 <TD ALIGN="right">Amount</TD>
947 <TD BGCOLOR="#FFFFFF"><B>! . sprintf('%.2f', $cust_pay->paid) . qq!</B></TD>
952 <TD ALIGN="right">Payment method</TD>
953 <TD BGCOLOR="#FFFFFF"><B>! . $cust_pay->payby_name .' #'. $cust_pay->paymask
964 my($gw, $auth, $order) = split(':', $cust_pay->paybatch);
968 'amount' => sprintf('%.2f', $cust_pay->paid),
969 'date' => $cust_pay->_date,
970 'date_pretty' => time2str('%Y-%m-%d', $cust_pay->_date),
971 'time_pretty' => time2str('%T', $cust_pay->_date),
973 'order_num' => $order,
974 'receipt_html' => $receipt_html,
981 'receipt_html' => '',
988 sub realtime_collect {
991 my $session = _cache->get($p->{'session_id'})
992 or return { 'error' => "Can't resume session" }; #better error message
994 my $custnum = $session->{'custnum'};
996 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
997 or return { 'error' => "unknown custnum $custnum" };
1000 if ( $p->{'amount'} ) {
1001 $amount = $p->{'amount'};
1003 elsif ( $session->{'pkgnum'} ) {
1004 $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
1007 $amount = $cust_main->balance;
1010 my $error = $cust_main->realtime_collect(
1011 'method' => $p->{'method'},
1012 'amount' => $amount,
1013 'pkgnum' => $session->{'pkgnum'},
1014 'session_id' => $p->{'session_id'},
1018 return { 'error' => $error } unless ref( $error );
1020 return { 'error' => '', amount => $amount, %$error };
1023 sub process_payment_order_pkg {
1026 my $hr = process_payment($p);
1027 return $hr if $hr->{'error'};
1032 sub process_payment_order_renew {
1035 my $hr = process_payment($p);
1036 return $hr if $hr->{'error'};
1041 sub process_prepay {
1045 my $session = _cache->get($p->{'session_id'})
1046 or return { 'error' => "Can't resume session" }; #better error message
1050 my $custnum = $session->{'custnum'};
1052 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1053 or return { 'error' => "unknown custnum $custnum" };
1055 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
1056 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
1064 return { 'error' => $error } if $error;
1066 return { 'error' => '',
1067 'amount' => $amount,
1068 'seconds' => $seconds,
1069 'duration' => duration_exact($seconds),
1070 'upbytes' => $upbytes,
1071 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
1072 'downbytes' => $downbytes,
1073 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
1074 'totalbytes'=> $totalbytes,
1075 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
1082 my $session = _cache->get($p->{'session_id'})
1083 or return { 'error' => "Can't resume session" }; #better error message
1085 my $custnum = $session->{'custnum'};
1087 my $invnum = $p->{'invnum'};
1089 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1090 'custnum' => $custnum } )
1091 or return { 'error' => "Can't find invnum" };
1095 return { 'error' => '',
1096 'invnum' => $invnum,
1097 'invoice_text' => join('', $cust_bill->print_text ),
1098 'invoice_html' => $cust_bill->print_html( { unsquelch_cdr => 1 } ),
1105 my $session = _cache->get($p->{'session_id'})
1106 or return { 'error' => "Can't resume session" }; #better error message
1108 my $custnum = $session->{'custnum'};
1110 my $invnum = $p->{'invnum'};
1112 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
1113 'custnum' => $custnum } )
1114 or return { 'error' => "Can't find invnum" };
1118 return { 'error' => '',
1119 'invnum' => $invnum,
1120 'invoice_pdf' => $cust_bill->print_pdf( { unsquelch_cdr => 1 } ),
1128 #sessioning for this? how do we get the session id to the backend invoice
1129 # template so it can add it to the link, blah
1132 if ( $p->{'invnum'} ) {
1133 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $p->{'invnum'} } )
1134 or return { 'error' => 'unknown invnum' };
1135 $agentnum = $cust_bill->cust_main->agentnum;
1138 my $templatename = $p->{'template'} || $p->{'templatename'};
1140 #false laziness-ish w/view/cust_bill-logo.cgi
1142 my $conf = new FS::Conf;
1143 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
1144 $templatename = "_$1";
1149 my $filename = "logo$templatename.png";
1151 return { 'error' => '',
1152 'logo' => $conf->config_binary($filename, $agentnum),
1153 'content_type' => 'image/png', #should allow gif, jpg too
1160 my $session = _cache->get($p->{'session_id'})
1161 or return { 'error' => "Can't resume session" }; #better error message
1163 my $custnum = $session->{'custnum'};
1165 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1166 or return { 'error' => "unknown custnum $custnum" };
1168 my @cust_bill = $cust_main->cust_bill;
1172 return { 'error' => '',
1173 'balance' => $cust_main->balance,
1176 my $owed = $_->owed;
1178 +{ 'invnum' => $_->invnum,
1179 '_date' => $_->_date,
1180 'date' => time2str("%b %o, %Y", $_->_date),
1181 'date_short' => time2str("%m-%d-%Y", $_->_date),
1182 'previous' => sprintf('%.2f', ($_->previous)[0]),
1183 'charged' => sprintf('%.2f', $_->charged),
1184 'owed' => sprintf('%.2f', $owed),
1185 'balance' => sprintf('%.2f', $balance),
1195 my $session = _cache->get($p->{'session_id'})
1196 or return { 'error' => "Can't resume session" }; #better error message
1198 my $custnum = $session->{'custnum'};
1200 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1201 or return { 'error' => "unknown custnum $custnum" };
1203 my @errors = $cust_main->cancel( 'quiet'=>1 );
1205 my $error = scalar(@errors) ? join(' / ', @errors) : '';
1207 return { 'error' => $error };
1214 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1215 return { 'error' => $session } if $context eq 'error';
1217 my $search = { 'custnum' => $custnum };
1218 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1219 my $cust_main = qsearchs('cust_main', $search )
1220 or return { 'error' => "unknown custnum $custnum" };
1222 my $conf = new FS::Conf;
1224 # the duplication below is necessary:
1225 # 1. to maintain the current buggy behaviour wrt the cust_pkg and part_pkg
1226 # hashes overwriting each other (setup and no_auto fields). Fixing that is a
1227 # non-backwards-compatible change breaking the software of anyone using the API
1228 # instead of the stock selfservice
1229 # 2. to return cancelled packages as well - for wholesale and non-wholesale
1230 if( $conf->exists('selfservice_server-view-wholesale') ) {
1231 return { 'svcnum' => $session->{'svcnum'},
1232 'custnum' => $custnum,
1233 'cust_pkg' => [ map {
1235 part_pkg => [ map $_->hashref, $_->part_pkg ],
1237 [ map $_->hashref, $_->available_part_svc ],
1239 [ map { my $ref = { $_->hash,
1240 label => [ $_->label ],
1242 $ref->{_password} = $_->svc_x->_password
1243 if $context eq 'agent'
1244 && $conf->exists('agent-showpasswords')
1245 && $_->part_svc->svcdb eq 'svc_acct';
1250 } $cust_main->cust_pkg
1253 small_custview( $cust_main, $conf->config('countrydefault') ),
1254 'wholesale_view' => 1,
1255 'login_svcpart' => [ $conf->config('selfservice_server-login_svcpart') ],
1256 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1257 'lnp' => $conf->exists('svc_phone-lnp'),
1261 { 'svcnum' => $session->{'svcnum'},
1262 'custnum' => $custnum,
1263 'cust_pkg' => [ map {
1264 my $primary_cust_svc = $_->primary_cust_svc;
1267 status => $_->status,
1269 [ map $_->hashref, $_->available_part_svc ],
1271 [ map { my $ref = { $_->hash,
1272 label => [ $_->label ],
1274 $ref->{_password} = $_->svc_x->_password
1275 if $context eq 'agent'
1276 && $conf->exists('agent-showpasswords')
1277 && $_->part_svc->svcdb eq 'svc_acct';
1278 $ref->{svchash} = { $_->svc_x->hash } if
1279 $_->part_svc->svcdb eq 'svc_phone';
1280 $ref->{svchash}->{svcpart} = $_->part_svc->svcpart
1281 if $_->part_svc->svcdb eq 'svc_phone'; # hack
1287 ? { $primary_cust_svc->hash,
1288 label => [ $primary_cust_svc->label ],
1289 finger => $primary_cust_svc->svc_x->finger, #uuh
1290 $primary_cust_svc->part_svc->hash,
1294 } $cust_main->ncancelled_pkgs
1297 small_custview( $cust_main, $conf->config('countrydefault') ),
1305 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1306 return { 'error' => $session } if $context eq 'error';
1308 my $search = { 'custnum' => $custnum };
1309 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1310 my $cust_main = qsearchs('cust_main', $search )
1311 or return { 'error' => "unknown custnum $custnum" };
1314 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
1315 foreach my $cust_pkg ( $p->{'ncancelled'}
1316 ? $cust_main->ncancelled_pkgs
1317 : $cust_main->unsuspended_pkgs ) {
1318 next if $session->{'pkgnum'} && $cust_pkg->pkgnum != $session->{'pkgnum'};
1319 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
1321 if ( $p->{'svcdb'} ) {
1322 my $svcdb = ref($p->{'svcdb'}) eq 'HASH'
1324 : ref($p->{'svcdb'}) eq 'ARRAY'
1325 ? { map { $_=>1 } @{ $p->{'svcdb'} } }
1326 : { $p->{'svcdb'} => 1 };
1327 @cust_svc = grep $svcdb->{ $_->part_svc->svcdb }, @cust_svc
1330 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
1333 my $conf = new FS::Conf;
1336 'svcnum' => $session->{'svcnum'},
1337 'custnum' => $custnum,
1338 'date_format' => $conf->config('date_format') || '%m/%d/%Y',
1339 'view_usage_nodomain' => $conf->exists('selfservice-view_usage_nodomain'),
1342 my $svc_x = $_->svc_x;
1343 my($label, $value) = $_->label;
1344 my $svcdb = $_->part_svc->svcdb;
1345 my $part_pkg = $_->cust_pkg->part_pkg;
1348 'svcnum' => $_->svcnum,
1354 if ( $svcdb eq 'svc_acct' ) {
1357 'username' => $svc_x->username,
1358 'email' => $svc_x->email,
1359 'seconds' => $svc_x->seconds,
1360 'upbytes' => display_bytecount($svc_x->upbytes),
1361 'downbytes' => display_bytecount($svc_x->downbytes),
1362 'totalbytes' => display_bytecount($svc_x->totalbytes),
1364 'recharge_amount' => $part_pkg->option('recharge_amount',1),
1365 'recharge_seconds' => $part_pkg->option('recharge_seconds',1),
1366 'recharge_upbytes' =>
1367 display_bytecount($part_pkg->option('recharge_upbytes',1)),
1368 'recharge_downbytes' =>
1369 display_bytecount($part_pkg->option('recharge_downbytes',1)),
1370 'recharge_totalbytes' =>
1371 display_bytecount($part_pkg->option('recharge_totalbytes',1)),
1375 } elsif ( $svcdb eq 'svc_phone' || $svcdb eq 'svc_port' ) {
1391 _usage_details( \&_port_graph, $p,
1392 'svcdb' => 'svc_port',
1397 my($svc_port, $begin, $end) = @_;
1399 my $pngOrError = $svc_port->graph_png( start=>$begin, end=> $end );
1400 push @usage, { 'png' => $pngOrError };
1404 sub _list_svc_usage {
1405 my($svc_acct, $begin, $end) = @_;
1407 foreach my $part_export (
1408 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
1409 qw( sqlradius sqlradius_withdomain )
1411 push @usage, @ { $part_export->usage_sessions($begin, $end, $svc_acct) };
1416 sub list_svc_usage {
1417 _usage_details(\&_list_svc_usage, @_);
1420 sub _list_support_usage {
1421 my($svc_acct, $begin, $end) = @_;
1423 foreach ( grep { $begin <= $_->_date && $_->_date <= $end }
1424 qsearch('acct_rt_transaction', { 'svcnum' => $svc_acct->svcnum })
1426 push @usage, { 'seconds' => $_->seconds,
1427 'support' => $_->support,
1428 '_date' => $_->_date,
1429 'id' => $_->transaction_id,
1430 'creator' => $_->creator,
1431 'subject' => $_->subject,
1432 'status' => $_->status,
1433 'ticketid' => $_->ticketid,
1439 sub list_support_usage {
1440 _usage_details(\&_list_support_usage, @_);
1443 sub _list_cdr_usage {
1444 my($svc_phone, $begin, $end) = @_;
1445 map [ $_->downstream_csv('format' => 'default', 'keeparray' => 1) ], #XXX config for format
1446 $svc_phone->get_cdrs( 'begin'=>$begin, 'end'=>$end, );
1449 sub list_cdr_usage {
1451 _usage_details( \&_list_cdr_usage, $p,
1452 'svcdb' => 'svc_phone',
1456 sub _usage_details {
1457 my($callback, $p, %opt) = @_;
1459 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1460 return { 'error' => $session } if $context eq 'error';
1462 my $search = { 'svcnum' => $p->{'svcnum'} };
1463 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1465 my $svcdb = $opt{'svcdb'} || 'svc_acct';
1467 my $svc_x = qsearchs( $svcdb, $search );
1468 return { 'error' => 'No service selected in list_svc_usage' }
1471 my $header = $svcdb eq 'svc_phone'
1472 ? [ split(',', FS::cdr::invoice_header('default') ) ] #XXX
1475 my $cust_pkg = $svc_x->cust_svc->cust_pkg;
1476 my $freq = $cust_pkg->part_pkg->freq;
1477 my $start = $cust_pkg->setup;
1478 #my $end = $cust_pkg->bill; # or time?
1481 unless ( $p->{beginning} ) {
1482 $p->{beginning} = $cust_pkg->last_bill;
1483 $p->{ending} = $end;
1486 my (@usage) = &$callback($svc_x, $p->{beginning}, $p->{ending});
1488 #kinda false laziness with FS::cust_main::bill, but perhaps
1489 #we should really change this bit to DateTime and DateTime::Duration
1491 #change this bit to use Date::Manip? CAREFUL with timezones (see
1492 # mailing list archive)
1493 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
1494 (localtime($p->{ending}) )[0,1,2,3,4,5];
1495 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
1496 (localtime($p->{beginning}) )[0,1,2,3,4,5];
1498 if ( $freq =~ /^\d+$/ ) {
1500 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
1502 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
1503 } elsif ( $freq =~ /^(\d+)w$/ ) {
1505 $nmday += $weeks * 7;
1506 $pmday -= $weeks * 7;
1507 } elsif ( $freq =~ /^(\d+)d$/ ) {
1511 } elsif ( $freq =~ /^(\d+)h$/ ) {
1516 return { 'error' => "unparsable frequency: ". $freq };
1519 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
1520 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
1524 'svcnum' => $p->{svcnum},
1525 'beginning' => $p->{beginning},
1526 'ending' => $p->{ending},
1527 'previous' => ($previous > $start) ? $previous : $start,
1528 'next' => ($next < $end) ? $next : $end,
1529 'header' => $header,
1537 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1538 return { 'error' => $session } if $context eq 'error';
1540 my $search = { 'custnum' => $custnum };
1541 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1542 my $cust_main = qsearchs('cust_main', $search )
1543 or return { 'error' => "unknown custnum $custnum" };
1545 my $status = $cust_main->status;
1546 #false laziness w/ClientAPI/Signup.pm
1548 my $cust_pkg = new FS::cust_pkg ( {
1549 'custnum' => $custnum,
1550 'pkgpart' => $p->{'pkgpart'},
1552 my $error = $cust_pkg->check;
1553 return { 'error' => $error } if $error;
1556 unless ( $p->{'svcpart'} eq 'none' ) {
1560 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
1562 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1563 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
1564 $svcdb = $part_svc->svcdb;
1566 $svcdb = 'svc_acct';
1568 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
1571 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
1572 'svc_domain' => [ qw( domain ) ],
1573 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
1574 'svc_external' => [ qw( id title ) ],
1575 'svc_pbx' => [ qw( id title ) ],
1578 my $svc_x = "FS::$svcdb"->new( {
1579 'svcpart' => $svcpart,
1580 map { $_ => $p->{$_} } @{$fields{$svcdb}}
1583 if ( $svcdb eq 'svc_acct' && exists($p->{"snarf_machine1"}) ) {
1586 while ( length($p->{"snarf_machine$snarfnum"}) ) {
1587 my $acct_snarf = new FS::acct_snarf ( {
1588 'machine' => $p->{"snarf_machine$snarfnum"},
1589 'protocol' => $p->{"snarf_protocol$snarfnum"},
1590 'username' => $p->{"snarf_username$snarfnum"},
1591 '_password' => $p->{"snarf_password$snarfnum"},
1594 push @acct_snarf, $acct_snarf;
1596 $svc_x->child_objects( \@acct_snarf );
1599 my $y = $svc_x->setdefault; # arguably should be in new method
1600 return { 'error' => $y } if $y && !ref($y);
1602 $error = $svc_x->check;
1603 return { 'error' => $error } if $error;
1610 tie my %hash, 'Tie::RefHash';
1611 %hash = ( $cust_pkg => \@svc );
1613 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
1614 return { 'error' => $error } if $error;
1616 my $conf = new FS::Conf;
1617 if ( $conf->exists('signup_server-realtime') ) {
1619 my $bill_error = _do_bop_realtime( $cust_main, $status );
1622 $cust_pkg->cancel('quiet'=>1);
1625 $cust_pkg->reexport;
1629 $cust_pkg->reexport;
1632 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
1634 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
1641 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1642 return { 'error' => $session } if $context eq 'error';
1644 my $search = { 'custnum' => $custnum };
1645 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1646 my $cust_main = qsearchs('cust_main', $search )
1647 or return { 'error' => "unknown custnum $custnum" };
1649 my $status = $cust_main->status;
1650 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
1651 or return { 'error' => "unknown package $p->{pkgnum}" };
1654 my $error = FS::cust_pkg::order( $custnum,
1660 my $conf = new FS::Conf;
1661 if ( $conf->exists('signup_server-realtime') ) {
1663 my $bill_error = _do_bop_realtime( $cust_main, $status );
1666 $newpkg[0]->suspend;
1669 $newpkg[0]->reexport;
1673 $newpkg[0]->reexport;
1676 return { error => '', pkgnum => $cust_pkg->pkgnum };
1680 sub order_recharge {
1683 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1684 return { 'error' => $session } if $context eq 'error';
1686 my $search = { 'custnum' => $custnum };
1687 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1688 my $cust_main = qsearchs('cust_main', $search )
1689 or return { 'error' => "unknown custnum $custnum" };
1691 my $status = $cust_main->status;
1692 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
1693 or return { 'error' => "unknown service " . $p->{'svcnum'} };
1695 my $svc_x = $cust_svc->svc_x;
1696 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
1699 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
1700 qw ( recharge_seconds recharge_upbytes recharge_downbytes
1701 recharge_totalbytes );
1702 my $amount = $part_pkg->option('recharge_amount', 1);
1704 my ($l, $v, $d) = $cust_svc->label; # blah
1705 my $pkg = "Recharge $v";
1707 my $bill_error = $cust_main->charge($amount, $pkg,
1708 "time: $vhash{seconds}, up: $vhash{upbytes}," .
1709 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
1710 $part_pkg->taxclass); #meh
1712 my $conf = new FS::Conf;
1713 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
1715 $bill_error = _do_bop_realtime( $cust_main, $status );
1720 my $error = $svc_x->recharge (\%vhash);
1721 return { 'error' => $error } if $error;
1725 my $error = $bill_error;
1726 $error ||= $svc_x->recharge (\%vhash);
1727 return { 'error' => $error } if $error;
1730 return { error => '', svc => $cust_svc->part_svc->svc };
1734 sub _do_bop_realtime {
1735 my ($cust_main, $status) = (shift, shift);
1737 my $old_balance = $cust_main->balance;
1739 my $bill_error = $cust_main->bill
1740 || $cust_main->apply_payments_and_credits
1741 || $cust_main->realtime_collect('selfservice' => 1);
1743 if ( $cust_main->balance > $old_balance
1744 && $cust_main->balance > 0
1745 && ( $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ?
1746 1 : $status eq 'suspended' ) ) {
1747 #this makes sense. credit is "un-doing" the invoice
1748 my $conf = new FS::Conf;
1749 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
1750 'self-service decline',
1751 'reason_type' => $conf->config('signup_credit_type'),
1753 $cust_main->apply_credits( 'order' => 'newest' );
1755 return { 'error' => '_decline', 'bill_error' => $bill_error };
1764 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1765 return { 'error' => $session } if $context eq 'error';
1767 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1768 or return { 'error' => "unknown custnum $custnum" };
1770 my @cust_pkg = sort { $a->bill <=> $b->bill }
1771 grep { $_->part_pkg->freq ne '0' }
1772 $cust_main->ncancelled_pkgs;
1774 #return { 'error' => 'No active packages to renew.' } unless @cust_pkg;
1776 my $total = $cust_main->balance;
1779 my $bill = $_->bill;
1780 $total += $_->part_pkg->base_recur($_, \$bill);
1781 my $renew_date = $_->part_pkg->add_freq($_->bill);
1783 'pkgnum' => $_->pkgnum,
1784 'amount' => sprintf('%.2f', $total),
1785 'bill_date' => $_->bill,
1786 'bill_date_pretty' => time2str('%x', $_->bill),
1787 'renew_date' => $renew_date,
1788 'renew_date_pretty' => time2str('%x', $renew_date),
1789 'expire_date' => $_->expire,
1790 'expire_date_pretty' => time2str('%x', $_->expire),
1795 return { 'dates' => \@array };
1799 sub payment_info_renew_info {
1801 my $renew_info = renew_info($p);
1802 my $payment_info = payment_info($p);
1803 return { %$renew_info,
1811 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1812 return { 'error' => $session } if $context eq 'error';
1814 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1815 or return { 'error' => "unknown custnum $custnum" };
1817 my $date = $p->{'date'};
1821 #freeside-daily -n -d $date fs_daily $custnum
1822 $cust_main->bill_and_collect( 'time' => $date,
1823 'invoice_time' => $now,
1824 'actual_time' => $now,
1825 'check_freq' => '1d',
1828 return { 'error' => '' };
1834 my $session = _cache->get($p->{'session_id'})
1835 or return { 'error' => "Can't resume session" }; #better error message
1837 my $custnum = $session->{'custnum'};
1839 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1840 or return { 'error' => "unknown custnum $custnum" };
1842 my $conf = new FS::Conf;
1844 $conf->config('selfservice-self_suspend_reason', $cust_main->agentnum)
1845 or return { 'error' => 'Permission denied' };
1847 my $pkgnum = $p->{'pkgnum'};
1849 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1850 'pkgnum' => $pkgnum, } )
1851 or return { 'error' => "unknown pkgnum $pkgnum" };
1853 my $error = $cust_pkg->suspend(reason => $reasonnum);
1854 return { 'error' => $error };
1860 my $session = _cache->get($p->{'session_id'})
1861 or return { 'error' => "Can't resume session" }; #better error message
1863 my $custnum = $session->{'custnum'};
1865 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
1866 or return { 'error' => "unknown custnum $custnum" };
1868 my $pkgnum = $p->{'pkgnum'};
1870 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1871 'pkgnum' => $pkgnum, } )
1872 or return { 'error' => "unknown pkgnum $pkgnum" };
1874 my $error = $cust_pkg->cancel('quiet' => 1);
1875 return { 'error' => $error };
1879 sub provision_phone {
1882 @bulkdid = @{$p->{'bulkdid'}} if $p->{'bulkdid'};
1884 if($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/){
1885 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1886 return { 'error' => $session } if $context eq 'error';
1888 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
1889 return { 'error' => 'service not found' } unless $svc_phone;
1890 return { 'error' => 'invalid svcnum' }
1891 if $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum != $custnum;
1893 $svc_phone->email($p->{'email'})
1894 if $svc_phone->email ne $p->{'email'} && $p->{'email'} =~ /^([\w\.\d@]+|)$/;
1895 $svc_phone->forwarddst($p->{'forwarddst'})
1896 if $svc_phone->forwarddst ne $p->{'forwarddst'}
1897 && $p->{'forwarddst'} =~ /^(\d+|)$/;
1898 return { 'error' => $svc_phone->replace };
1902 unless($p->{'lnp'}) {
1903 $p->{'lnp_desired_due_date'} = parse_datetime($p->{'lnp_desired_due_date'});
1904 $p->{'lnp_status'} = "portingin";
1905 return _provision( 'FS::svc_phone',
1906 [qw(lnp_desired_due_date lnp_other_provider
1907 lnp_other_provider_account phonenum countrycode lnp_status)],
1908 [qw(phonenum countrycode)],
1915 unless (scalar(@bulkdid)) {
1916 return _provision( 'FS::svc_phone',
1917 [qw(phonenum countrycode)],
1918 [qw(phonenum countrycode)],
1924 # bulk DID order case
1926 foreach my $did ( @bulkdid ) {
1927 $did =~ s/[^0-9]//g;
1928 $error = _provision( 'FS::svc_phone',
1929 [qw(phonenum countrycode)],
1930 [qw(phonenum countrycode)],
1932 'pkgnum' => $p->{'pkgnum'},
1933 'svcpart' => $p->{'svcpart'},
1935 'countrycode' => $p->{'countrycode'},
1936 'session_id' => $p->{'session_id'},
1939 return $error if ($error->{'error'} && length($error->{'error'}) > 1);
1941 { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
1944 sub provision_acct {
1946 warn "provision_acct called\n"
1949 return { 'error' => gettext('passwords_dont_match') }
1950 if $p->{'_password'} ne $p->{'_password2'};
1951 return { 'error' => gettext('empty_password') }
1952 unless length($p->{'_password'});
1954 if ($p->{'domsvc'}) {
1955 my %domains = domain_select_hash FS::svc_acct(map { $_ => $p->{$_} }
1956 qw ( svcpart pkgnum ) );
1957 return { 'error' => gettext('invalid_domain') }
1958 unless ($domains{$p->{'domsvc'}});
1961 warn "provision_acct calling _provision\n"
1963 _provision( 'FS::svc_acct',
1964 [qw(username _password domsvc)],
1965 [qw(username _password domsvc)],
1971 sub provision_external {
1973 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
1974 _provision( 'FS::svc_external',
1983 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
1984 warn "_provision called for $class\n"
1987 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1988 return { 'error' => $session } if $context eq 'error';
1990 my $search = { 'custnum' => $custnum };
1991 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1992 my $cust_main = qsearchs('cust_main', $search )
1993 or return { 'error' => "unknown custnum $custnum" };
1995 my $pkgnum = $p->{'pkgnum'};
1997 warn "searching for custnum $custnum pkgnum $pkgnum\n"
1999 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2000 'pkgnum' => $pkgnum,
2002 or return { 'error' => "unknown pkgnum $pkgnum" };
2004 warn "searching for svcpart ". $p->{'svcpart'}. "\n"
2006 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
2007 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
2009 warn "creating $class record\n"
2011 my $svc_x = $class->new( {
2012 'pkgnum' => $p->{'pkgnum'},
2013 'svcpart' => $p->{'svcpart'},
2014 map { $_ => $p->{$_} } @$fields
2016 warn "inserting $class record\n"
2018 my $error = $svc_x->insert;
2021 warn "finding inserted record for svcnum ". $svc_x->svcnum. "\n"
2023 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
2026 my $return = { 'svc' => $part_svc->svc,
2028 map { $_ => $svc_x->get($_) } @$return_fields
2030 warn "_provision returning ". Dumper($return). "\n"
2039 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2040 return { 'error' => $session } if $context eq 'error';
2042 my $search = { 'custnum' => $custnum };
2043 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2044 my $cust_main = qsearchs('cust_main', $search )
2045 or return { 'error' => "unknown custnum $custnum" };
2047 my $pkgnum = $p->{'pkgnum'};
2049 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
2050 'pkgnum' => $pkgnum,
2052 or return { 'error' => "unknown pkgnum $pkgnum" };
2054 my $svcpart = $p->{'svcpart'};
2056 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
2057 'svcpart' => $svcpart, } )
2058 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
2059 my $part_svc = $pkg_svc->part_svc;
2061 my $conf = new FS::Conf;
2064 'svc' => $part_svc->svc,
2065 'svcdb' => $part_svc->svcdb,
2066 'pkgnum' => $pkgnum,
2067 'svcpart' => $svcpart,
2068 'custnum' => $custnum,
2070 'security_phrase' => 0, #XXX !
2071 'svc_acct_pop' => [], #XXX !
2073 'init_popstate' => '',
2078 small_custview( $cust_main, $conf->config('countrydefault') ),
2082 if ($p->{'svcnum'} && $p->{'svcnum'} =~ /^\d+$/
2083 && $ret->{'svcdb'} eq 'svc_phone') {
2084 $ret->{'svcnum'} = $p->{'svcnum'};
2085 my $svc_phone = qsearchs('svc_phone', { svcnum => $p->{'svcnum'} });
2086 if ( $svc_phone && $svc_phone->cust_svc->cust_pkg->custnum == $custnum ) {
2087 $ret->{'email'} = $svc_phone->email;
2088 $ret->{'forwarddst'} = $svc_phone->forwarddst;
2095 sub unprovision_svc {
2098 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2099 return { 'error' => $session } if $context eq 'error';
2101 my $search = { 'custnum' => $custnum };
2102 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2103 my $cust_main = qsearchs('cust_main', $search )
2104 or return { 'error' => "unknown custnum $custnum" };
2106 my $svcnum = $p->{'svcnum'};
2108 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
2109 or return { 'error' => "unknown svcnum $svcnum" };
2111 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
2112 unless $cust_svc->cust_pkg->custnum == $custnum;
2114 my $conf = new FS::Conf;
2116 return { 'svc' => $cust_svc->part_svc->svc,
2117 'error' => $cust_svc->cancel,
2119 small_custview( $cust_main, $conf->config('countrydefault') ),
2124 sub myaccount_passwd {
2126 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2127 return { 'error' => $session } if $context eq 'error';
2129 return { 'error' => "New passwords don't match." }
2130 if $p->{'new_password'} ne $p->{'new_password2'};
2132 return { 'error' => 'Enter new password' }
2133 unless length($p->{'new_password'});
2135 #my $search = { 'custnum' => $custnum };
2136 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2137 $custnum =~ /^(\d+)$/ or die "illegal custnum";
2138 my $search = " AND custnum = $1";
2139 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
2141 my $svc_acct = qsearchs( {
2142 'table' => 'svc_acct',
2143 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
2144 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
2145 'LEFT JOIN cust_main USING ( custnum ) ',
2146 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
2147 'extra_sql' => $search, #important
2149 or return { 'error' => "Service not found" };
2151 if ( exists($p->{'old_password'}) ) {
2152 return "Incorrect password."
2153 unless $svc_acct->check_password($p->{'old_password'});
2156 $svc_acct->_password($p->{'new_password'});
2157 my $error = $svc_acct->replace();
2159 my($label, $value) = $svc_acct->cust_svc->label;
2161 return { 'error' => $error,
2170 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2171 return { 'error' => $session } if $context eq 'error';
2173 warn "$me create_ticket: initializing ticket system\n" if $DEBUG;
2174 FS::TicketSystem->init();
2176 my $conf = new FS::Conf;
2177 my $queue = $p->{'queue'}
2178 || $conf->config('ticket_system-selfservice_queueid')
2179 || $conf->config('ticket_system-default_queueid');
2181 warn "$me create_ticket: creating ticket\n" if $DEBUG;
2182 my $err_or_ticket = FS::TicketSystem->create_ticket(
2183 '', #create RT session based on FS CurrentUser (fs_selfservice)
2185 'custnum' => $custnum,
2186 'svcnum' => $session->{'svcnum'},
2187 map { $_ => $p->{$_} } qw( requestor cc subject message mime_type )
2190 if ( ref($err_or_ticket) ) {
2191 warn "$me create_ticket: successful: ". $err_or_ticket->id. "\n"
2193 return { 'error' => '',
2194 'ticket_id' => $err_or_ticket->id,
2197 warn "$me create_ticket: unsuccessful: $err_or_ticket\n"
2199 return { 'error' => $err_or_ticket };
2207 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2208 return { 'error' => $session } if $context eq 'error';
2210 return { error => 'requested format not implemented' }
2211 unless ($p->{'format'} eq 'csv' || $p->{'format'} eq 'xls');
2213 my $conf = new FS::Conf;
2214 my $age_threshold = 0;
2215 $age_threshold = time() - $conf->config('selfservice-recent-did-age')
2216 if ($p->{'recentonly'} && $conf->exists('selfservice-recent-did-age'));
2218 my $search = { 'custnum' => $custnum };
2219 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
2220 my $cust_main = qsearchs('cust_main', $search )
2221 or return { 'error' => "unknown custnum $custnum" };
2223 # does it make more sense to just run one sql query for this instead of all the
2224 # insanity below? would increase performance greately for large data sets?
2226 foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
2227 my @part_svc = $cust_pkg->part_svc;
2228 foreach my $part_svc ( @part_svc ) {
2229 if($part_svc->svcdb eq 'svc_phone'){
2230 my @cust_pkg_svc = @{$part_svc->cust_pkg_svc};
2231 foreach my $cust_pkg_svc ( @cust_pkg_svc ) {
2232 push @svc_phone, $cust_pkg_svc->svc_x
2233 if $cust_pkg_svc->date_inserted >= $age_threshold;
2241 my($xls_r,$xls_c) = (0,0);
2244 my @fields = qw( countrycode phonenum pin sip_password phone_name );
2245 if($p->{'format'} eq 'csv') {
2246 $csv = new Text::CSV_XS { 'always_quote' => 1,
2249 return { 'error' => 'Unable to create CSV' } unless $csv->combine(@fields);
2250 $content .= $csv->string;
2252 elsif($p->{'format'} eq 'xls') {
2253 my $XLS1 = new IO::Scalar \$content;
2254 $xls_workbook = Spreadsheet::WriteExcel->new($XLS1)
2255 or return { 'error' => "Error opening .xls file: $!" };
2256 $xls = $xls_workbook->add_worksheet('DIDs');
2257 foreach ( @fields ) {
2258 $xls->write(0,$xls_c++,$_);
2263 foreach my $svc_phone ( @svc_phone ) {
2264 my @cols = map { $svc_phone->$_ } @fields;
2265 if($p->{'format'} eq 'csv') {
2266 return { 'error' => 'Unable to create CSV' }
2267 unless $csv->combine(@cols);
2268 $content .= $csv->string;
2270 elsif($p->{'format'} eq 'xls') {
2273 $xls->write($xls_r,$xls_c++,$_);
2279 $xls_workbook->close() if $p->{'format'} eq 'xls';
2281 { content => $content, format => $p->{'format'}, };
2286 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2287 return { 'error' => $session } if $context eq 'error';
2289 warn "$me get_ticket: initializing ticket system\n" if $DEBUG;
2290 FS::TicketSystem->init();
2291 return { 'error' => 'get_ticket configuration error' }
2292 if $FS::TicketSystem::system ne 'RT_Internal';
2294 # check existence and ownership as part of this
2295 warn "$me get_ticket: fetching ticket\n" if $DEBUG;
2296 my $rt_session = FS::TicketSystem->session('');
2297 my $Ticket = FS::TicketSystem->get_ticket_object(
2299 ticket_id => $p->{'ticket_id'},
2302 return { 'error' => 'ticket not found' } if !$Ticket;
2304 if ( length( $p->{'subject'} || '' ) ) {
2306 if ( $p->{'subject'} ne $Ticket->Subject ) {
2307 my ($val, $msg) = $Ticket->SetSubject($p->{'subject'});
2308 return { 'error' => "unable to set subject: $msg" } if !$val;
2312 if(length($p->{'reply'})) {
2313 my @err_or_res = FS::TicketSystem->correspond_ticket(
2315 'ticket_id' => $p->{'ticket_id'},
2316 'content' => $p->{'reply'},
2319 return { 'error' => 'unable to reply to ticket' }
2320 unless ( $err_or_res[0] != 0 && defined $err_or_res[2] );
2323 warn "$me get_ticket: getting ticket history\n" if $DEBUG;
2324 my $err_or_ticket = FS::TicketSystem->get_ticket(
2326 'ticket_id' => $p->{'ticket_id'},
2329 if ( !ref($err_or_ticket) ) { # there is no way this should ever happen
2330 warn "$me get_ticket: unsuccessful: $err_or_ticket\n"
2332 return { 'error' => $err_or_ticket };
2335 my @custs = @{$err_or_ticket->{'custs'}};
2336 my @txns = @{$err_or_ticket->{'txns'}};
2339 # superseded by check in get_ticket_object
2340 #return { 'error' => 'invalid ticket requested' }
2341 #unless grep($_ eq $custnum, @custs);
2343 foreach my $txn ( @txns ) {
2344 push @filtered_txns, $txn
2345 if ($txn->{'type'} eq 'EmailRecord'
2346 || $txn->{'type'} eq 'Correspond'
2347 || $txn->{'type'} eq 'Create');
2350 warn "$me get_ticket: successful: \n"
2352 return { 'error' => '',
2353 'transactions' => \@filtered_txns,
2354 'ticket_fields' => $err_or_ticket->{'fields'},
2355 'ticket_id' => $p->{'ticket_id'},
2359 sub adjust_ticket_priority {
2361 my($context, $session, $custnum) = _custoragent_session_custnum($p);
2362 return { 'error' => $session } if $context eq 'error';
2364 warn "$me adjust_ticket_priority: initializing ticket system\n" if $DEBUG;
2365 FS::TicketSystem->init;
2366 my $ss_priority = FS::TicketSystem->selfservice_priority;
2368 return { 'error' => 'adjust_ticket_priority configuration error' }
2369 if $FS::TicketSystem::system ne 'RT_Internal'
2372 my $values = $p->{'values'}; #hashref, id => priority value
2375 foreach my $id (keys %$values) {
2376 warn "$me adjust_ticket_priority: fetching ticket $id\n" if $DEBUG;
2377 my $Ticket = FS::TicketSystem->get_ticket_object('',
2379 'custnum' => $custnum,
2382 $ticket_error{$id} = 'ticket not found';
2386 # RT API stuff--would we gain anything by wrapping this in FS::TicketSystem?
2387 # We're not going to implement it for RT_External.
2388 my $old_value = $Ticket->FirstCustomFieldValue($ss_priority);
2389 my $new_value = $values->{$id};
2390 next if $old_value eq $new_value;
2392 warn "$me adjust_ticket_priority: updating ticket $id\n" if $DEBUG;
2394 # AddCustomFieldValue works fine (replacing any existing value) if it's
2395 # a single-valued custom field, which it should be. If it's not, you're
2396 # doing something wrong.
2398 if ( length($new_value) ) {
2399 ($val, $msg) = $Ticket->AddCustomFieldValue(
2400 Field => $ss_priority,
2401 Value => $new_value,
2405 ($val, $msg) = $Ticket->DeleteCustomFieldValue(
2406 Field => $ss_priority,
2407 Value => $old_value,
2411 $ticket_error{$id} = $msg if !$val;
2412 warn "$me adjust_ticket_priority: $id: $msg\n" if $DEBUG and !$val;
2414 return { 'error' => '',
2415 'ticket_error' => \%ticket_error,
2416 %{ customer_info($p) } # send updated customer info back
2422 sub _custoragent_session_custnum {
2425 my($context, $session, $custnum);
2426 if ( $p->{'session_id'} ) {
2428 $context = 'customer';
2429 $session = _cache->get($p->{'session_id'})
2430 or return ( 'error' => "Can't resume session" ); #better error message
2431 $custnum = $session->{'custnum'};
2433 } elsif ( $p->{'agent_session_id'} ) {
2436 my $agent_cache = new FS::ClientAPI_SessionCache( {
2437 'namespace' => 'FS::ClientAPI::Agent',
2439 $session = $agent_cache->get($p->{'agent_session_id'})
2440 or return ( 'error' => "Can't resume session" ); #better error message
2441 $custnum = $p->{'custnum'};
2445 return ( 'error' => "Can't resume session" ); #better error message
2448 ($context, $session, $custnum);