This commit was manufactured by cvs2svn to create tag 'freeside_2_1_1'.
[freeside.git] / FS / FS / ClientAPI / Signup.pm
index c376476..ec45e79 100644 (file)
@@ -1,7 +1,7 @@
 package FS::ClientAPI::Signup;
 
 use strict;
-use vars qw($DEBUG $me);
+use vars qw( $DEBUG $me );
 use Data::Dumper;
 use Tie::RefHash;
 use FS::Conf;
@@ -26,6 +26,15 @@ use FS::payby;
 $DEBUG = 0;
 $me = '[FS::ClientAPI::Signup]';
 
+sub clear_cache {
+  warn "$me clear_cache called\n" if $DEBUG;
+  my $cache = new FS::ClientAPI_SessionCache( {
+      'namespace' => 'FS::ClientAPI::Signup',
+  } );
+  $cache->clear();
+  return {};
+}
+
 sub signup_info {
   my $packet = shift;
 
@@ -90,7 +99,7 @@ sub signup_info {
                             ],
 
       'agent' => [ map { my $agent = $_;
-                         map { $_ => $agent->get($_) } @agent_fields;
+                         +{ map { $_ => $agent->get($_) } @agent_fields }
                        }
                        qsearch('agent', { 'disabled' => '' } )
                  ],
@@ -111,6 +120,9 @@ sub signup_info {
 
       'payby' => [ $conf->config('signup_server-payby') ],
 
+      'payby_longname' => [ map { FS::payby->longname($_) } 
+                            $conf->config('signup_server-payby') ],
+
       'card_types' => card_types(),
 
       'paytypes' => [ @FS::cust_main::paytypes ],
@@ -338,6 +350,36 @@ sub signup_info {
         foreach qw( address1 city county state zip country );
     }
 
+    #some of the above could probably be cached, too
+
+    my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
+
+    if ( $signup_info_cache_agent ) {
+
+      warn "$me loading cached signup info for agentnum $agentnum\n"
+        if $DEBUG > 1;
+
+    } else {
+
+      warn "$me populating signup info cache for agentnum $agentnum\n"
+        if $DEBUG > 1;
+
+      $signup_info_cache_agent = {
+        #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
+        #  qw( company_name ) ),
+        ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
+          qw( body_bgcolor box_bgcolor) ),
+        ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
+          qw( head body_header body_footer ) ),
+      };
+
+      $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
+
+    }
+
+    $signup_info->{$_} = $signup_info_cache_agent->{$_}
+      foreach keys %$signup_info_cache_agent;
+
   }
   # else {
   # delete $signup_info->{'part_pkg'};
@@ -353,9 +395,14 @@ sub signup_info {
     my $agent_signup_info = { %$signup_info };
     delete $agent_signup_info->{agentnum2part_pkg};
     $agent_signup_info->{'agent'} = $session->{'agent'};
-    $agent_signup_info;
-  } else {
-    $signup_info;
+    return $agent_signup_info;
+  } 
+  elsif ( exists $packet->{'keys'} ) {
+    my @keys = @{ $packet->{'keys'} };
+    return { map { $_ => $signup_info->{$_} } @keys };
+  }
+  else {
+    return $signup_info;
   }
 
 }
@@ -404,6 +451,9 @@ sub new_customer {
       unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
 
   }
+  elsif ( $svc_x eq 'svc_pbx' ) {
+    #possibly some validation will be needed
+  }
 
   my $agentnum;
   if ( exists $packet->{'session_id'} ) {
@@ -538,18 +588,23 @@ sub new_customer {
     }
     $svc->child_objects( \@acct_snarf );
 
-    push @svc, $svc;
 
   } elsif ( $svc_x eq 'svc_phone' ) {
 
-    my $svc = new FS::svc_phone ( {
+    push @svc, new FS::svc_phone ( {
       'svcpart' => $svcpart,
        map { $_ => $packet->{$_} }
          qw( countrycode phonenum sip_password pin ),
     } );
 
-    push @svc, $svc;
+  } elsif ( $svc_x eq 'svc_pbx' ) {
 
+    push @svc, new FS::svc_pbx ( {
+        'svcpart' => $svcpart,
+        map { $_ => $packet->{$_} } 
+          qw( id title ),
+        } );
+  
   } else {
     die "unknown signup service $svc_x";
   }
@@ -608,26 +663,22 @@ sub new_customer {
 
   if ( $conf->exists('signup_server-realtime') ) {
 
-    #warn "[fs_signup_server] Billing customer...\n" if $Debug;
+    #warn "$me Billing customer...\n" if $Debug;
 
     my $bill_error = $cust_main->bill;
-    #warn "[fs_signup_server] error billing new customer: $bill_error"
+    #warn "$me error billing new customer: $bill_error"
     #  if $bill_error;
 
     $bill_error = $cust_main->apply_payments_and_credits;
-    #warn "[fs_signup_server] error applying payments and credits for".
+    #warn "$me error applying payments and credits for".
     #     " new customer: $bill_error"
     #  if $bill_error;
 
-    if ($cust_main->_new_bop_required()) {
-      $bill_error = $cust_main->realtime_collect(
-         method        => FS::payby->payby2bop( $packet->{payby} ),
-         depend_jobnum => $placeholder->jobnum,
-      );
-    } else {
-      $bill_error = $cust_main->collect('realtime' => 1);
-    }
-    #warn "[fs_signup_server] error collecting from new customer: $bill_error"
+    $bill_error = $cust_main->realtime_collect(
+       method        => FS::payby->payby2bop( $packet->{payby} ),
+       depend_jobnum => $placeholder->jobnum,
+    );
+    #warn "$me error collecting from new customer: $bill_error"
     #  if $bill_error;
 
     if ($bill_error && ref($bill_error) eq 'HASH') {
@@ -639,6 +690,11 @@ sub new_customer {
              };
     }
 
+    $bill_error = $cust_main->apply_payments_and_credits;
+    #warn "$me error applying payments and credits for".
+    #     " new customer: $bill_error"
+    #  if $bill_error;
+
     if ( $cust_main->balance > 0 ) {
 
       #this makes sense.  credit is "un-doing" the invoice
@@ -674,14 +730,20 @@ sub new_customer {
 
   my %return = ( 'error'          => '',
                  'signup_service' => $svc_x,
+                 'svcnum'         => $svc[0]->svcnum,
+                 'custnum'        => $cust_main->custnum,
                );
 
   if ( $svc_x eq 'svc_acct' ) {
     $return{$_} = $svc[0]->$_() for qw( username _password );
   } elsif ( $svc_x eq 'svc_phone' ) {
     $return{$_} = $svc[0]->$_() for qw( countrycode phonenum sip_password pin );
+  } elsif ( $svc_x eq 'svc_pbx' ) {
+    #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
   } else {
-    die "unknown signup service $svc_x";
+    return { 'error' => "configuration error: unknown signup service $svc_x" };
+    #die "unknown signup service $svc_x";
+    # return an error that's visible to someone somewhere
   }
 
   return \%return;