RT# 83091 - fixed selfserivce to insert payment account if one does not exits
[freeside.git] / FS / FS / ClientAPI / MyAccount.pm
index ab11415..e813016 100644 (file)
@@ -87,8 +87,6 @@ sub skin_info {
   my($context, $session, $custnum) = _custoragent_session_custnum($p);
   #return { 'error' => $session } if $context eq 'error';
 
-  my $domain = $session->{'domain'};
-
   my $agentnum = '';
   if ( $context eq 'customer' && $custnum ) {
 
@@ -122,8 +120,6 @@ sub skin_info {
     warn "$me populating skin info cache for agentnum $agentnum\n"
       if $DEBUG > 1;
 
-    my $menu = $conf->config("ng_selfservice-menu", $agentnum );
-
     $skin_info_cache_agent = {
       'agentnum' => $agentnum,
       ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
@@ -147,93 +143,7 @@ sub skin_info {
       ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
         qw( head body_header body_footer company_address ) ),
       'money_char' => $conf->config("money_char") || '$',
-      'menu'  => _menu($domain,$menu),
-    };
-
-    _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
-
-  }
-
-  #{ %$skin_info_cache_agent };
-  $skin_info_cache_agent;
-
-}
-
-## checks if page is in menu listing, if not sends to main with error.
-sub check_access {
- my $p = shift;
- my $error;
-
- return if $p->{'page'} eq "index.php";
- return if $p->{'page'} eq "ip_login.php";
-
- return if substr($p->{'page'}, 0, length("process_")) eq "process_";
-
- my $conf = new FS::Conf;
-
- my($context, $session, $custnum) = _custoragent_session_custnum($p);
-
- my $domain = ref($session) ? $session->{'domain'} : '';
-
- my $agentnum = '';
- if ( $context eq 'customer' && $custnum ) {
-
-  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 ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
-    $agentnum = $1;
-  }
-  $p->{'agentnum'} = $agentnum;
-
- my $menu = $conf->config("ng_selfservice-menu", $agentnum );
-
- my $allowed_pages = _menu($domain,$menu);
-
- my %allowed;
- my @lines = split /\n/, $allowed_pages;
- foreach my $line (@lines) {
-  chomp; # remove newlines
-  $line =~ s/^\s+//;  # remove leading whitespace
-  next unless length($line);
-  my (@pages) = split(/ /, $line, 2);
-  $allowed{$pages[0]} = $pages[1];
- }
-
- $error = "You do not have access to the page ".$allowed{$p->{page}} unless $allowed{$p->{page}};
-
- return { 'error' => $error, };
-
-}
-
-sub _menu {
- my $p = shift;
- my $m = shift;
-
- my $menu;
-
- if ($p eq 'ip_mac') {
-   $menu =       'main.php Home
-
-                 payment.php Payments
-                 payment_cc.php Credit Card Payment
-                 payment_ach.php Electronic Check Payment
-                 payment_paypal.php PayPal Payment
-                 payment_webpay.php Webpay Payments
-
-                 docs.php FAQs
-
-                 logout.php Logout
-                ';
- }
- else {
-   $menu = join("\n", $m ) ||
+      'menu' => join("\n", $conf->config("ng_selfservice-menu", $agentnum ) ) ||
                 'main.php Home
 
                  services.php Services
@@ -262,31 +172,16 @@ sub _menu {
                  docs.php FAQs
 
                  logout.php Logout
-                ';
- }
- return $menu;
-}
-
-sub get_mac_address {
-  my $p = shift;
+                ',
+    };
 
-## access radius exports acct tables to get mac
-  my @part_export = ();
-  @part_export = (
-    qsearch( 'part_export', { 'exporttype' => 'sqlradius' } ),
-    qsearch( 'part_export', { 'exporttype' => 'sqlradius_withdomain' } ),
-    qsearch( 'part_export', { 'exporttype' => 'broadband_sqlradius' } ),
-  );
+    _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
 
-  my @sessions;
-  foreach my $part_export (@part_export) {
-    push @sessions, ( @{ $part_export->usage_sessions( {
-      'ip' => $p->{'ip'},
-      'session_status' => 'open',
-    } ) } );
   }
 
-  return { 'mac_address' => $sessions[0]->{'callingstationid'}, };
+  #{ %$skin_info_cache_agent };
+  $skin_info_cache_agent;
+
 }
 
 sub login_info {
@@ -296,8 +191,8 @@ sub login_info {
 
   my %info = (
     %{ skin_info($p) },
-    'phone_login'      => $conf->exists('selfservice_server-phone_login'),
-    'single_domain'    => scalar($conf->config('selfservice_server-single_domain')),
+    'phone_login'  => $conf->exists('selfservice_server-phone_login'),
+    'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
     'banner_url'       => scalar($conf->config('selfservice-login_banner_url')),
     'banner_image_md5' => 
       md5_hex($conf->config_binary('selfservice-login_banner_image')),
@@ -342,22 +237,6 @@ sub login {
 
     $svc_x = $svc_phone;
 
-  } elsif ( $p->{'domain'} eq 'ip_mac' ) {
-
-      return { error => 'MAC address empty '.$p->{'username'} }
-        unless $p->{'username'};
-
-      my $mac_address = $p->{'username'};
-      $mac_address =~ s/[\:\,\-\. ]//g;
-      $mac_address =~ tr/[a-z]/[A-Z/;
-
-      my $svc_broadband = qsearchs( 'svc_broadband', { 'mac_addr' => $mac_address } );
-      return { error => 'MAC address not found '.$p->{'username'} }
-        unless $svc_broadband;
-      $svc_x = $svc_broadband;
-
-      $session->{'domain'} = $p->{'domain'};
-
   } elsif ( $p->{email}
               && (my $contact = FS::contact->by_selfservice_email($p->{email}))
           )
@@ -866,8 +745,20 @@ sub edit_info {
     or return { 'error' => "unknown custnum $custnum" };
 
   my $conf = new FS::Conf;
-  if (($p->{payby} eq "CHEK" || $p->{payby} eq "DCHEK") && $conf->exists('selfservice-ACH_info_readonly')) {
-    return { 'error' => "You do not have authority to add a bank account" };
+
+  if ($p->{payby}) {
+    return { 'error' => "You do not have authority to add a bank account" }
+      if (($p->{payby} eq "CHEK" || $p->{payby} eq "DCHEK") && $conf->exists('selfservice-ACH_info_readonly'));
+
+    ## get default cust_payby and change it. For old v3 selfservice that upgraded to v4.  this is for v4 only
+    my ($cust_payby) = $cust_main->cust_payby();
+    if ($cust_payby) {
+      $p->{'custpaybynum'} = $cust_payby->custpaybynum;
+      update_payby($p);
+    }
+    else {
+      insert_payby($p);
+    }
   }
 
   my $new = new FS::cust_main { $cust_main->hash };
@@ -998,31 +889,34 @@ sub payment_info {
     for qw(address1 address2 city state zip);
 
   # look for stored cust_payby info
-  #   only if we've been given a clear payment_payby (to avoid payname conflicts)
-  if ($p->{'payment_payby'} =~ /^(CARD|CHEK)$/) {
-    my @search_payby = ($p->{'payment_payby'} eq 'CARD') ? ('CARD','DCRD') : ('CHEK','DCHK');
-    my ($cust_payby) = $cust_main->cust_payby(@search_payby);
-    if ($cust_payby) {
-      $return{payname} = $cust_payby->payname
+  #   v3 to v4 upgrade would break change_pay because change_pay does not send payment_payby
+  #   so for change_pay to work need to search for all allowed paybys and grab default payment account
+  my @search_payby = ();
+  @search_payby = ($p->{'payment_payby'} eq 'CARD') ? ('CARD','DCRD') : ('CHEK','DCHK')
+    if ($p->{'payment_payby'} =~ /^(CARD|CHEK)$/);
+
+  my ($cust_payby) = $cust_main->cust_payby(@search_payby);
+  if ($cust_payby) {
+    $return{payby} = $cust_payby->payby;
+    $return{payname} = $cust_payby->payname
                          || ( $cust_main->first. ' '. $cust_main->get('last') );
-      $return{custpaybynum} = $cust_payby->custpaybynum;
+    $return{custpaybynum} = $cust_payby->custpaybynum;
 
-      if ( $cust_payby->payby =~ /^(CARD|DCRD)$/ ) {
-        $return{card_type} = cardtype($cust_payby->payinfo);
-        $return{payinfo} = $cust_payby->paymask;
+    if ( $cust_payby->payby =~ /^(CARD|DCRD)$/ ) {
+      $return{card_type} = cardtype($cust_payby->payinfo);
+      $return{payinfo} = $cust_payby->paymask;
 
-        @return{'month', 'year'} = $cust_payby->paydate_monthyear;
+      @return{'month', 'year'} = $cust_payby->paydate_monthyear;
 
-      }
+    }
 
-      if ( $cust_payby->payby =~ /^(CHEK|DCHK)$/ ) {
-        my ($payinfo1, $payinfo2) = split '@', $cust_payby->paymask;
-        $return{payinfo1} = $payinfo1;
-        $return{payinfo2} = $payinfo2;
-        $return{paytype}  = $cust_payby->paytype;
-        $return{paystate} = $cust_payby->paystate;
-        $return{payname}  = $cust_payby->payname;      # override 'first/last name' default from above, if any.  Is instution-name here.  (#15819)
-      }
+    if ( $cust_payby->payby =~ /^(CHEK|DCHK)$/ ) {
+      my ($payinfo1, $payinfo2) = split '@', $cust_payby->paymask;
+      $return{payinfo1} = $payinfo1;
+      $return{payinfo2} = $payinfo2;
+      $return{paytype}  = $cust_payby->paytype;
+      $return{paystate} = $cust_payby->paystate;
+      $return{payname}  = $cust_payby->payname;        # override 'first/last name' default from above, if any.  Is instution-name here.  (#15819)
     }
   }
 
@@ -1039,6 +933,12 @@ sub payment_info {
   $return{credit_card_surcharge_percentage} = $conf->config('credit-card-surcharge-percentage', $cust_main->agentnum);
   $return{credit_card_surcharge_flatfee} = $conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum);
 
+  # A value for 'payby' must be defined in %return
+  $return{payby} = $return{paybys}->[0]
+    if !$return{payby}
+    && ref $return{paybys}
+    && scalar @{ $return{paybys} };
+
   return { 'error' => '',
            %return,
          };
@@ -1808,6 +1708,9 @@ sub insert_payby {
      my $payinfo2 = $1;
      $p->{'payinfo'} = $payinfo1. '@'. $payinfo2;
    }
+   elsif ($p->{'payby'} eq 'CARD') {
+    $p->{paydate} = $p->{year} . '-' . $p->{month} . '-01' unless $p->{paydate};
+   }
 
   my $cust_payby = new FS::cust_payby {
     'custnum' => $custnum,
@@ -1839,7 +1742,10 @@ sub update_payby {
        or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
      my $payinfo2 = $1;
      $p->{'payinfo'} = $payinfo1. '@'. $payinfo2;
-   }
+  }
+  elsif ($p->{'payby'} eq 'CARD') {
+    $p->{paydate} = $p->{year} . '-' . $p->{month} . '-01' unless $p->{paydate};
+  }
 
   my $cust_payby = qsearchs('cust_payby', {
                               'custnum'      => $custnum,