discounts, RT#6679
[freeside.git] / FS / FS / ClientAPI / MyAccount.pm
index b6934f8..a05c6f2 100644 (file)
@@ -1,7 +1,7 @@
 package FS::ClientAPI::MyAccount;
 
 use strict;
-use vars qw( $cache $DEBUG );
+use vars qw( $cache $DEBUG $me );
 use subs qw( _cache _provision );
 use Data::Dumper;
 use Digest::MD5 qw(md5_hex);
@@ -12,7 +12,8 @@ use FS::UI::Web::small_custview qw(small_custview); #less doh
 use FS::UI::Web;
 use FS::UI::bytecount qw( display_bytecount );
 use FS::Conf;
-use FS::Record qw(qsearch qsearchs);
+#use FS::UID qw(dbh);
+use FS::Record qw(qsearch qsearchs dbh);
 use FS::Msgcat qw(gettext);
 use FS::Misc qw(card_types);
 use FS::ClientAPI_SessionCache;
@@ -29,7 +30,8 @@ use FS::payby;
 use FS::acct_rt_transaction;
 use HTML::Entities;
 
-$DEBUG = 0;
+$DEBUG = 2;
+$me = '[FS::ClientAPI::MyAccount]';
 
 #false laziness with FS::cust_main
 BEGIN {
@@ -56,24 +58,72 @@ sub _cache {
 }
 
 sub skin_info {
-  #my $p = shift;
+  my $p = shift;
+
+  my($context, $session, $custnum) = _custoragent_session_custnum($p);
+  #return { 'error' => $session } if $context eq 'error';
+
+  my $agentnum = '';
+  if ( $context eq 'customer' ) {
+
+    my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
+      or die dbh->errstr;
+
+    $sth->execute($custnum) or die $sth->errstr;
+
+    $agentnum = $sth->fetchrow_arrayref->[0]
+      or die "no agentnum for custnum $custnum";
+
+  #} elsif ( $context eq 'agent' ) {
+  } elsif ( $p->{'agentnum'} =~ /^(\d+)$/ ) {
+    $agentnum = $1;
+  }
 
   my $conf = new FS::Conf;
 
-  use vars qw($skin_info); #cache for performance.
-  #agentnum eventually...?  but if they're not logged in yet.. ?
+  #false laziness w/Signup.pm
 
-  $skin_info ||= {
-    'head'           => join("\n", $conf->config('selfservice-head') ),
-    'body_header'    => join("\n", $conf->config('selfservice-body_header') ),
-    'body_footer'    => join("\n", $conf->config('selfservice-body_footer') ),
-    'body_bgcolor'   => scalar( $conf->config('selfservice-body_bgcolor') ),
-    'box_bgcolor'    => scalar( $conf->config('selfservice-box_bgcolor')  ),
+  my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
 
-    'company_name'   => scalar($conf->config('company_name')),
-  };
+  if ( $skin_info_cache_agent ) {
+
+    warn "$me loading cached skin info for agentnum $agentnum\n"
+      if $DEBUG > 1;
+
+  } else {
+
+    warn "$me populating skin info cache for agentnum $agentnum\n"
+      if $DEBUG > 1;
+
+    $skin_info_cache_agent = {
+      'agentnum' => $agentnum,
+      ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
+        qw( company_name ) ),
+      ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
+        qw( body_bgcolor box_bgcolor
+            text_color link_color vlink_color hlink_color alink_color
+            font title_color title_align title_size menu_bgcolor menu_fontsize
+          )
+      ),
+      ( map { $_ => $conf->exists("selfservice-$_", $agentnum ) }
+        qw( menu_skipblanks menu_skipheadings menu_nounderline )
+      ),
+      ( map { $_ => scalar($conf->config_binary("selfservice-$_", $agentnum)) }
+        qw( title_left_image title_right_image
+            menu_top_image menu_body_image menu_bottom_image
+          )
+      ),
+      'logo' => scalar($conf->config_binary('logo.png', $agentnum )),
+      ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
+        qw( head body_header body_footer company_address ) ),
+    };
+
+    _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
+
+  }
 
-  $skin_info;
+  #{ %$skin_info_cache_agent };
+  $skin_info_cache_agent;
 
 }
 
@@ -83,7 +133,7 @@ sub login_info {
   my $conf = new FS::Conf;
 
   my %info = (
-    %{ skin_info() },
+    %{ skin_info($p) },
     'phone_login'  => $conf->exists('selfservice_server-phone_login'),
     'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
   );
@@ -176,9 +226,9 @@ sub logout {
   my $p = shift;
   if ( $p->{'session_id'} ) {
     _cache->remove($p->{'session_id'});
-    return { %{ skin_info() }, 'error' => '' };
+    return { %{ skin_info($p) }, 'error' => '' };
   } else {
-    return { %{ skin_info() }, 'error' => "Can't resume session" }; #better error message
+    return { %{ skin_info($p) }, 'error' => "Can't resume session" }; #better error message
   }
 }
 
@@ -209,21 +259,22 @@ sub access_info {
 
   $info->{hide_payment_fields} =
   [
-    map { FS::payby->realtime($_) &&
-          $cust_main
-            ->agent
-            ->payment_gateway( 'method' => FS::payby->payby2bop($_) )
-            ->gateway_namespace
-            eq 'Business::OnlineThirdPartyPayment'
+    map { my $pg = '';
+          if ( FS::payby->realtime($_) ) {
+            $pg = $cust_main->agent->payment_gateway(
+              'method'  => FS::payby->payby2bop($_),
+              'nofatal' => 1,
+            );
+          }
+          $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
         }
     @{ $info->{cust_paybys} }
   ];
 
   return { %$info,
-           'custnum'    => $custnum,
-           'pkgnum'     => $session->{'pkgnum'},
-           'svcnum'     => $session->{'svcnum'},
-           'nonprimary' => $session->{'nonprimary'},
+           'custnum'       => $custnum,
+           'access_pkgnum' => $session->{'pkgnum'},
+           'access_svcnum' => $session->{'svcnum'},
          };
 }
 
@@ -274,8 +325,6 @@ sub customer_info {
                       ( $session->{'pkgnum'} ? 1 : 0 ), #nobalance
                     );
 
-    warn $return{small_custview};
-
     $return{name} = $cust_main->first. ' '. $cust_main->get('last');
 
     for (@cust_main_editable_fields) {
@@ -366,7 +415,8 @@ sub edit_info {
 
     $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
 
-  }elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
+  } elsif ( $payby =~ /^(CHEK|DCHK)$/ ) {
+
     my $payinfo;
     $p->{'payinfo1'} =~ /^([\dx]+)$/
       or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
@@ -376,15 +426,15 @@ sub edit_info {
     my $payinfo2 = $1;
     $payinfo = $payinfo1. '@'. $payinfo2;
 
-    if ( $payinfo eq $cust_main->paymask ) {
-      $new->payinfo($cust_main->payinfo);
-    } else {
-      $new->payinfo($payinfo);
-    }
+    $new->payinfo( ($payinfo eq $cust_main->paymask)
+                     ? $cust_main->payinfo
+                     : $payinfo
+                 );
 
     $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
 
-  }elsif ( $payby =~ /^(BILL)$/ ) {
+  } elsif ( $payby =~ /^(BILL)$/ ) {
+    #no-op
   } elsif ( $payby ) {  #notyet ready
     return { 'error' => "unknown payby $payby" };
   }
@@ -469,12 +519,14 @@ sub payment_info {
 
   $return{hide_payment_fields} =
   [
-    map { FS::payby->realtime($_) &&
-          $cust_main
-            ->agent
-            ->payment_gateway( 'method' => FS::payby->payby2bop($_) )
-            ->gateway_namespace
-            eq 'Business::OnlineThirdPartyPayment'
+    map { my $pg = '';
+          if ( FS::payby->realtime($_) ) {
+            $pg = $cust_main->agent->payment_gateway(
+              'method'  => FS::payby->payby2bop($_),
+              'nofatal' => 1,
+            );
+          }
+          $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
         }
     @{ $return{cust_paybys} }
   ];
@@ -532,6 +584,11 @@ sub process_payment {
   my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
     or return { 'error' => "unknown custnum $custnum" };
 
+  $p->{'amount'} =~ /^\s*(\d+(\.\d{2})?)\s*$/
+    or return { 'error' => gettext('illegal_amount') };
+  my $amount = $1;
+  return { error => 'Amount must be greater than 0' } unless $amount > 0;
+
   $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
     or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
   my $payname = $1;
@@ -565,6 +622,8 @@ sub process_payment {
    
     $payinfo = $p->{'payinfo'};
 
+    #more intelligent mathing will be needed here if you change
+    #card_masking_method and don't remove existing paymasks
     $payinfo = $cust_main->payinfo
       if $cust_main->paymask eq $payinfo;
 
@@ -600,7 +659,7 @@ sub process_payment {
     'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
   );
 
-  my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $p->{'amount'},
+  my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
     'quiet'    => 1,
     'payinfo'  => $payinfo,
     'paydate'  => $p->{'year'}. '-'. $p->{'month'}. '-01',
@@ -618,16 +677,16 @@ sub process_payment {
     my $new = new FS::cust_main { $cust_main->hash };
     if ($payby eq 'CARD' || $payby eq 'DCRD') {
       $new->set( $_ => $p->{$_} )
-        foreach qw( payinfo payname paystart_month paystart_year payissue payip
+        foreach qw( payname paystart_month paystart_year payissue payip
                     address1 address2 city state zip country );
       $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
     } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
       $new->set( $_ => $p->{$_} )
         foreach qw( payname payip paytype paystate
                     stateid stateid_state );
-      $new->set( 'payinfo' => $payinfo );
       $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
     }
+    $new->set( 'payinfo' => $payinfo );
     $new->set( 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01' );
     my $error = $new->replace($cust_main);
     return { 'error' => $error } if $error;
@@ -639,7 +698,6 @@ sub process_payment {
 }
 
 sub realtime_collect {
-
   my $p = shift;
 
   my $session = _cache->get($p->{'session_id'})
@@ -654,6 +712,7 @@ sub realtime_collect {
     'method'     => $p->{'method'},
     'pkgnum'     => $session->{'pkgnum'},
     'session_id' => $p->{'session_id'},
+    'apply'      => 1,
   );
   return { 'error' => $error } unless ref( $error );
 
@@ -890,7 +949,7 @@ sub list_svcs {
   #              @svc_x;
 
   { 
-    #no#'svcnum'   => $session->{'svcnum'},
+    'svcnum'   => $session->{'svcnum'},
     'custnum'  => $custnum,
     'svcs'     => [
       map { 
@@ -1110,7 +1169,7 @@ sub order_pkg {
     my %fields = (
       'svc_acct'     => [ qw( username domsvc _password sec_phrase popnum ) ],
       'svc_domain'   => [ qw( domain ) ],
-      'svc_phone'    => [ qw( phonenum pin sip_password ) ],
+      'svc_phone'    => [ qw( phonenum pin sip_password phone_name ) ],
       'svc_external' => [ qw( id title ) ],
     );