This commit was generated by cvs2svn to compensate for changes in r6252,
[freeside.git] / FS / FS / ClientAPI / Signup.pm
index ac211ec..61325b9 100644 (file)
@@ -1,6 +1,8 @@
 package FS::ClientAPI::Signup;
 
 use strict;
+use vars qw($DEBUG $me);
+use Data::Dumper;
 use Tie::RefHash;
 use FS::Conf;
 use FS::Record qw(qsearch qsearchs dbdef);
@@ -18,29 +20,30 @@ use FS::acct_snarf;
 use FS::queue;
 use FS::reg_code;
 
+$DEBUG = 0;
+$me = '[FS::ClientAPI::Signup]';
+
 sub signup_info {
   my $packet = shift;
 
+  warn "$me signup_info called on $packet\n" if $DEBUG;
+
   my $conf = new FS::Conf;
 
-  use vars qw($signup_info_cache); #cache for performance;
-  $signup_info_cache ||= {
-    'cust_main_county' =>
-      [ map { $_->hashref } qsearch('cust_main_county', {}) ],
+  my $cache = new FS::ClientAPI_SessionCache( {
+    'namespace' => 'FS::ClientAPI::Signup',
+  } );
+  my $signup_info_cache = $cache->get('signup_info_cache');
 
-    'agent' =>
-      [
-        map { $_->hashref }
-          qsearch('agent', { 'disabled' => '' } )
-      ],
+  if ( $signup_info_cache ) {
 
-    'part_referral' =>
-      [
-        map { $_->hashref }
-          qsearch('part_referral', { 'disabled' => '' })
-      ],
+    warn "$me loading cached signup info\n" if $DEBUG > 1;
+
+  } else {
 
-    'agentnum2part_pkg' =>
+    warn "$me populating signup info cache\n" if $DEBUG > 1;
+
+    my $agentnum2part_pkg = 
       {
         map {
           my $href = $_->pkgpart_hashref;
@@ -55,37 +58,75 @@ sub signup_info {
                   qsearch( 'part_pkg', { 'disabled' => '' } )
             ];
         } qsearch('agent', { 'disabled' => '' })
-      },
+      };
+
+    my $msgcat = { map { $_=>gettext($_) }
+                       qw( passwords_dont_match invalid_card unknown_card_type
+                           not_a empty_password illegal_or_empty_text )
+                 };
+    warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
+
+    my $label = { map { $_ => FS::Msgcat::_gettext($_) }
+                      qw( stateid stateid_state )
+                };
+    warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
 
-    'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
+    $signup_info_cache = {
+      'cust_main_county' => [ map $_->hashref,
+                                  qsearch('cust_main_county', {} )
+                            ],
 
-    'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
+      'agent' => [ map $_->hashref,
+                       qsearch('agent', { 'disabled' => '' } )
+                 ],
 
-    'security_phrase' => $conf->exists('security_phrase'),
+      'part_referral' => [ map $_->hashref,
+                               qsearch('part_referral', { 'disabled' => '' } )
+                         ],
 
-    'payby' => [ $conf->config('signup_server-payby') ],
+      'agentnum2part_pkg' => $agentnum2part_pkg,
 
-    'card_types' => card_types(),
+      'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
 
-    'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'), # 1,
+      'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
 
-    'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),#1,
+      'security_phrase' => $conf->exists('security_phrase'),
 
-    'msgcat' => { map { $_=>gettext($_) } qw(
-      passwords_dont_match invalid_card unknown_card_type not_a empty_password illegal_or_empty_text
-    ) },
+      'payby' => [ $conf->config('signup_server-payby') ],
 
-    'statedefault' => $conf->config('statedefault') || 'CA',
+      'card_types' => card_types(),
 
-    'countrydefault' => $conf->config('countrydefault') || 'US',
+      'paytypes' => [ @FS::cust_main::paytypes ],
 
-    'refnum' => $conf->config('signup_server-default_refnum'),
+      'cvv_enabled' => 1,
 
-    'default_pkgpart' => $conf->config('signup_server-default_pkgpart'),
+      'stateid_enabled' => $conf->exists('show_stateid'),
 
-  };
+      'paystate_enabled' => $conf->exists('show_bankstate'),
+
+      'ship_enabled' => 1,
+
+      'msgcat' => $msgcat,
+
+      'label' => $label,
+
+      'statedefault' => scalar($conf->config('statedefault')) || 'CA',
+
+      'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
+
+      'refnum' => scalar($conf->config('signup_server-default_refnum')),
+
+      'default_pkgpart' => scalar($conf->config('signup_server-default_pkgpart')),
+
+    };
+
+    $cache->set('signup_info_cache', $signup_info_cache);
+
+  }
 
   my $signup_info = { %$signup_info_cache };
+  warn "$me signup info loaded\n" if $DEBUG > 1;
+  warn Dumper($signup_info). "\n" if $DEBUG > 2;
 
   my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
 
@@ -94,6 +135,9 @@ sub signup_info {
     $signup_info->{optional_packages} = [];
 
     foreach my $addl ( @addl ) {
+
+      warn "$me adding optional package info\n" if $DEBUG > 1;
+
       my $classnum = $conf->config($addl) or next;
 
       my @pkgs = map { {
@@ -106,6 +150,8 @@ sub signup_info {
 
       push @{$signup_info->{optional_packages}}, \@pkgs;
 
+      warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
+
     }
 
   }
@@ -117,6 +163,8 @@ sub signup_info {
 
   my $session = '';
   if ( exists $packet->{'session_id'} ) {
+
+    warn "$me loading agent session\n" if $DEBUG > 1;
     my $cache = new FS::ClientAPI_SessionCache( {
       'namespace' => 'FS::ClientAPI::Agent',
     } );
@@ -126,7 +174,11 @@ sub signup_info {
     } else {
       return { 'error' => "Can't resume session" }; #better error message
     }
-  }elsif( exists $packet->{'customer_session_id'} ) {
+    warn "$me done loading agent session\n" if $DEBUG > 1;
+
+  } elsif ( exists $packet->{'customer_session_id'} ) {
+
+    warn "$me loading customer session\n" if $DEBUG > 1;
     my $cache = new FS::ClientAPI_SessionCache( {
       'namespace' => 'FS::ClientAPI::MyAccount',
     } );
@@ -139,11 +191,16 @@ sub signup_info {
     } else {
       return { 'error' => "Can't resume session" }; #better error message
     }
+    warn "$me done loading customer session\n" if $DEBUG > 1;
+
   }
 
   $signup_info->{'part_pkg'} = [];
 
   if ( $packet->{'reg_code'} ) {
+
+    warn "$me setting package list via reg_code\n" if $DEBUG > 1;
+
     $signup_info->{'part_pkg'} = 
       [ map { { 'payby'       => [ $_->payby ],
                 'freq_pretty' => $_->freq_pretty,
@@ -161,8 +218,12 @@ sub signup_info {
     $signup_info->{'error'} = 'Unknown registration code'
       unless @{ $signup_info->{'part_pkg'} };
 
+    warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
+
   } elsif ( $packet->{'promo_code'} ) {
 
+    warn "$me setting package list via promo_code\n" if $DEBUG > 1;
+
     $signup_info->{'part_pkg'} =
       [ map { { 'payby'   => [ $_->payby ],
                 'freq_pretty' => $_->freq_pretty,
@@ -179,11 +240,18 @@ sub signup_info {
 
     $signup_info->{'error'} = 'Unknown promotional code'
       unless @{ $signup_info->{'part_pkg'} };
+
+    warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
   }
 
-  if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
-    $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
+  if ( $agentnum ) {
+
+    warn "$me setting agent-specific package list\n" if $DEBUG > 1;
+    $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
+      unless @{ $signup_info->{'part_pkg'} };
+    warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
 
+    warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
     $signup_info->{'part_referral'} =
       [
         map { $_->hashref }
@@ -195,15 +263,18 @@ sub signup_info {
                    },
                  )
       ];
+    warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
 
   }
   # else {
   # delete $signup_info->{'part_pkg'};
   #}
 
+  warn "$me sorting package list\n" if $DEBUG > 1;
   $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} }  # case?
                                       @{ $signup_info->{'part_pkg'} }
                                ];
+  warn "$me done sorting package list\n" if $DEBUG > 1;
 
   if ( exists $packet->{'session_id'} ) {
     my $agent_signup_info = { %$signup_info };
@@ -216,6 +287,32 @@ sub signup_info {
 
 }
 
+sub domain_select_hash {
+  my $packet = shift;
+
+  my $response = {};
+
+  if ($packet->{pkgpart}) {
+    my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
+    #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
+    $packet->{svcpart} = $part_pkg->svcpart
+      if $part_pkg;
+  }
+
+  if ($packet->{svcpart}) {
+    my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
+    $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
+      if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag  eq 'D');
+  }
+
+  $response->{'domains'}
+    = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
+                                                 qw(svcpart pkgnum)
+                                       ) };
+
+  $response;
+}
+
 sub new_customer {
   my $packet = shift;
 
@@ -258,14 +355,14 @@ sub new_customer {
 
       last first ss company address1 address2
       city county state zip country
-      daytime night fax
+      daytime night fax stateid stateid_state
 
       ship_last ship_first ship_ss ship_company ship_address1 ship_address2
       ship_city ship_county ship_state ship_zip ship_country
       ship_daytime ship_night ship_fax
 
       payby
-      payinfo paycvv paydate payname
+      payinfo paycvv paydate payname paystate paytype
       paystart_month paystart_year payissue
       payip
 
@@ -368,7 +465,10 @@ sub new_customer {
     #warn "[fs_signup_server] error billing new customer: $bill_error"
     #  if $bill_error;
 
-    $cust_main->apply_payments_and_credits;
+    $bill_error = $cust_main->apply_payments_and_credits;
+    #warn "[fs_signup_server] error applying payments and credits for".
+    #     " new customer: $bill_error"
+    #  if $bill_error;
 
     $bill_error = $cust_main->collect('realtime' => 1);
     #warn "[fs_signup_server] error collecting from new customer: $bill_error"
@@ -377,7 +477,9 @@ sub new_customer {
     if ( $cust_main->balance > 0 ) {
 
       #this makes sense.  credit is "un-doing" the invoice
-      $cust_main->credit( $cust_main->balance, 'signup server decline' );
+      $cust_main->credit( $cust_main->balance, 'signup server decline',
+                          'reason_type' => $conf->config('signup_credit_type'),
+                        );
       $cust_main->apply_credits;
 
       #should check list for errors...