RT# 81557 - Added checkbox to validate cc on import, and cc validation.
[freeside.git] / FS / FS / cust_main / Import.pm
index f477323..dcedc16 100644 (file)
@@ -2,18 +2,19 @@ package FS::cust_main::Import;
 
 use strict;
 use vars qw( $DEBUG $conf );
-use Storable qw(thaw);
 use Data::Dumper;
-use MIME::Base64;
-use Date::Parse;
 use File::Slurp qw( slurp );
+use FS::Misc::DateTime qw( parse_datetime );
 use FS::UID qw( dbh );
 use FS::Record qw( qsearchs );
 use FS::cust_main;
 use FS::svc_acct;
+use FS::svc_broadband;
 use FS::svc_external;
 use FS::svc_phone;
+use FS::svc_hardware;
 use FS::part_referral;
+use Business::CreditCard 0.35;
 
 $DEBUG = 0;
 
@@ -21,6 +22,8 @@ install_callback FS::UID sub {
   $conf = new FS::Conf;
 };
 
+my %is_location = map { $_ => 1 } FS::cust_main::Location->location_fields;
+
 =head1 NAME
 
 FS::cust_main::Import - Batch customer importing
@@ -34,7 +37,8 @@ FS::cust_main::Import - Batch customer importing
     file      => $file,      #filename
     type      => $type,      #csv or xls
     format    => $format,    #extended, extended-plus_company, svc_external,
-                             # or svc_external_svc_phone
+                             #extended-plus_company_and_options
+                             #extended-plus_options, or svc_external_svc_phone
     agentnum  => $agentnum,
     refnum    => $refnum,
     pkgpart   => $pkgpart,
@@ -63,8 +67,7 @@ Load a batch import as a queued JSRPC job
 
 sub process_batch_import {
   my $job = shift;
-
-  my $param = thaw(decode_base64(shift));
+  my $param = shift;
   warn Dumper($param) if $DEBUG;
   
   my $files = $param->{'uploaded_files'}
@@ -93,6 +96,7 @@ sub process_batch_import {
       agentnum  => $param->{'agentnum'},
       refnum    => $param->{'refnum'},
       pkgpart   => $param->{'pkgpart'},
+      validate_cc => $param->{'validate_cc'},
       #'fields'  => [qw( cust_pkg.setup dayphone first last address1 address2
       #                 city state zip comments                          )],
       'format'  => $param->{'format'},
@@ -123,15 +127,14 @@ sub batch_import {
   my $agentnum  = $param->{agentnum};
   my $refnum    = $param->{refnum};
   my $pkgpart   = $param->{pkgpart};
+  my $validate_cc = $param->{validate_cc};
 
   my $format    = $param->{'format'};
 
   my @fields;
-  my $payby;
   if ( $format eq 'simple' ) {
     @fields = qw( cust_pkg.setup dayphone first last
                   address1 address2 city state zip comments );
-    $payby = 'BILL';
   } elsif ( $format eq 'extended' ) {
     @fields = qw( agent_custid refnum
                   last first address1 address2 city state zip country
@@ -143,7 +146,18 @@ sub batch_import {
                   cust_pkg.pkgpart
                   svc_acct.username svc_acct._password 
                 );
-    $payby = 'BILL';
+ } elsif ( $format eq 'extended-plus_options' ) {
+    @fields = qw( agent_custid refnum
+                  last first address1 address2 city state zip country
+                  daytime night
+                  ship_last ship_first ship_address1 ship_address2
+                  ship_city ship_state ship_zip ship_country
+                  payinfo paycvv paydate
+                  invoicing_list
+                  cust_pkg.pkgpart
+                  svc_acct.username svc_acct._password 
+                  customer_options
+                );
  } elsif ( $format eq 'extended-plus_company' ) {
     @fields = qw( agent_custid refnum
                   last first company address1 address2 city state zip country
@@ -155,7 +169,29 @@ sub batch_import {
                   cust_pkg.pkgpart
                   svc_acct.username svc_acct._password 
                 );
-    $payby = 'BILL';
+ } elsif ( $format eq 'extended-plus_company_and_options' ) {
+    @fields = qw( agent_custid refnum
+                  last first company address1 address2 city state zip country
+                  daytime night
+                  ship_last ship_first ship_company ship_address1 ship_address2
+                  ship_city ship_state ship_zip ship_country
+                  payinfo paycvv paydate
+                  invoicing_list
+                  cust_pkg.pkgpart
+                  svc_acct.username svc_acct._password 
+                  customer_options
+                );
+ } elsif ( $format =~ /^svc_broadband/ ) {
+    @fields = qw( agent_custid refnum
+                  last first company address1 address2 city state zip country
+                  daytime night
+                  ship_last ship_first ship_company ship_address1 ship_address2
+                  ship_city ship_state ship_zip ship_country
+                  payinfo paycvv paydate
+                  invoicing_list
+                  cust_pkg.pkgpart cust_pkg.bill
+                );
+    push @fields, map "svc_broadband.$_", qw( ip_addr mac_addr sectornum );
  } elsif ( $format =~ /^svc_external/ ) {
     @fields = qw( agent_custid refnum
                   last first company address1 address2 city state zip country
@@ -169,7 +205,35 @@ sub batch_import {
                 );
     push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
       if $format eq 'svc_external_svc_phone';
-    $payby = 'BILL';
+  } elsif ( $format eq 'birthdates-acct_phone_hardware') {
+    @fields = qw( agent_custid refnum
+                  last first company address1 address2 city state zip country
+                  daytime night
+                  ship_last ship_first ship_company ship_address1 ship_address2
+                  ship_city ship_state ship_zip ship_country
+                  birthdate spouse_birthdate
+                  payinfo paycvv paydate
+                  invoicing_list
+                  cust_pkg.pkgpart cust_pkg.bill
+                  svc_acct.username svc_acct._password 
+                );
+    push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
+    push @fields, map "svc_hardware.$_", qw(typenum ip_addr hw_addr serial);
+
+  } elsif ( $format eq 'national_id-acct_phone') {
+    @fields = qw( agent_custid refnum
+                  last first company address1 address2 city state zip country
+                  daytime night
+                  ship_last ship_first ship_company ship_address1 ship_address2
+                  ship_city ship_state ship_zip ship_country
+                  national_id
+                  payinfo paycvv paydate
+                  invoicing_list
+                  cust_pkg.pkgpart cust_pkg.bill
+                  svc_acct.username svc_acct._password svc_acct.slipip
+                );
+    push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
+
   } else {
     die "unknown format $format";
   }
@@ -214,6 +278,10 @@ sub batch_import {
   my $oldAutoCommit = $FS::UID::AutoCommit;
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
+
+  #implies ignore_expired_card
+  local($FS::cust_main::import) = 1;
+  local($FS::cust_main::import) = 1;
   
   my $line;
   my $row = 0;
@@ -253,27 +321,32 @@ sub batch_import {
       custbatch => $custbatch,
       agentnum  => $agentnum,
       refnum    => $refnum,
-      country   => $conf->config('countrydefault') || 'US',
-      payby     => $payby, #default
       paydate   => '12/2037', #default
     );
     my $billtime = time;
     my %cust_pkg = ( pkgpart => $pkgpart );
     my %svc_x = ();
+    my %bill_location = ();
+    my %ship_location = ();
+    my $cust_payby = '';
     foreach my $field ( @fields ) {
 
       if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
 
-        #$cust_pkg{$1} = str2time( shift @$columns );
+        #$cust_pkg{$1} = parse_datetime( shift @$columns );
         if ( $1 eq 'pkgpart' ) {
           $cust_pkg{$1} = shift @columns;
         } elsif ( $1 eq 'setup' ) {
-          $billtime = str2time(shift @columns);
+          $billtime = parse_datetime(shift @columns);
         } else {
-          $cust_pkg{$1} = str2time( shift @columns );
+          $cust_pkg{$1} = parse_datetime( shift @columns );
         } 
 
-      } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
+      } elsif ( $field =~ /^svc_acct\.(username|_password|slipip)$/ ) {
+
+        $svc_x{$1} = shift @columns;
+
+      } elsif ( $field =~ /^svc_broadband\.(ip_addr|mac_addr|sectornum)$/ ) {
 
         $svc_x{$1} = shift @columns;
 
@@ -283,7 +356,19 @@ sub batch_import {
 
       } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
         $svc_x{$1} = shift @columns;
-       
+      
+      } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
+
+        $svc_x{$1} = shift @columns;
+
+      } elsif ( $is_location{$field} ) {
+
+        $bill_location{$field} = shift @columns;
+
+      } elsif ( $field =~ /^ship_(.*)$/ and $is_location{$1} ) {
+
+        $ship_location{$1} = shift @columns;
+      
       } else {
 
         #refnum interception
@@ -312,22 +397,77 @@ sub batch_import {
         my $value = shift @columns;
         $cust_main{$field} = $value if length($value);
       }
+    } # foreach my $field
+    # finished importing columns
+
+    $bill_location{'country'} ||= $conf->config('countrydefault') || 'US';
+    $cust_main{'bill_location'} = FS::cust_location->new(\%bill_location);
+    if ( grep $_, values(%ship_location) ) {
+      $ship_location{'country'} ||= $conf->config('countrydefault') || 'US';
+      $cust_main{'ship_location'} = FS::cust_location->new(\%ship_location);
+    } else {
+      $cust_main{'ship_location'} = $cust_main{'bill_location'};
+    }
+
+    if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
+
+      if ( $cust_main{'payinfo'} =~ /^\s*(\d+\@[\d\.]+)\s*$/ ) {
+
+        delete $cust_main{'payinfo'};
+
+        $cust_payby = new FS::cust_payby {
+          'payby'   => 'CHEK',
+          'payinfo' => $1,
+        };
+
+      } elsif ($cust_main{'payinfo'} =~ /^\s*([AD]?)(.*)\s*$/) {
+
+        delete $cust_main{'payinfo'};
+
+        ## validate credit card if requested
+        if ($validate_cc) {
+          validate($2)
+            or return "Invalid card($2) for customer ".$cust_main{'first'}." ".$cust_main{'last'};
+          return "Unknown card type for customer ".$cust_main{'first'}." ".$cust_main{'last'}
+            if cardtype($2) eq "Unknown";
+        }
+
+        $cust_payby = new FS::cust_payby {
+          'payby'   => ($1 eq 'D') ? 'DCRD' : 'CARD',
+          'payinfo' => $2,
+          'paycvv'  => delete $cust_main{'paycvv'},
+          'paydate' => delete $cust_main{'paydate'},
+          'payname' => $cust_main{'first'}. ' '. $cust_main{'last'},
+        };
+
+      }
+
     }
 
-    $cust_main{'payby'} = 'CARD'
-      if defined $cust_main{'payinfo'}
-      && length  $cust_main{'payinfo'};
+    $cust_main{$_} = parse_datetime($cust_main{$_})
+      foreach grep $cust_main{$_},
+        qw( birthdate spouse_birthdate anniversary_date );
 
     my $invoicing_list = $cust_main{'invoicing_list'}
                            ? [ delete $cust_main{'invoicing_list'} ]
                            : [];
 
+    my $customer_options = delete $cust_main{customer_options};
+    $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
+    push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
+
     my $cust_main = new FS::cust_main ( \%cust_main );
 
     use Tie::RefHash;
     tie my %hash, 'Tie::RefHash'; #this part is important
 
     if ( $cust_pkg{'pkgpart'} ) {
+
+      unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
+        $dbh->rollback if $oldAutoCommit;
+        return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
+      }
+
       my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
 
       my @svc_x = ();
@@ -336,17 +476,27 @@ sub batch_import {
         $svcdb = 'svc_acct';
       } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
         $svcdb = 'svc_external';
+      } elsif ( $svc_x{ip_addr} || $svc_x{mac_addr} ) {
+        $svcdb = 'svc_broadband';
       }
 
       my $svc_phone = '';
       if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
         $svc_phone = FS::svc_phone->new( {
           map { $_ => delete($svc_x{$_}) }
-              qw( countrycode phonenum sip_password pin)
+              qw( countrycode phonenum sip_password pin )
         } );
       }
 
-      if ( $svcdb || $svc_phone ) {
+      my $svc_hardware = '';
+      if ( $svc_x{'typenum'} ) {
+        $svc_hardware = FS::svc_hardware->new( {
+          map { $_ => delete($svc_x{$_}) }
+            qw( typenum ip_addr hw_addr serial )
+        } );
+      }
+
+      if ( $svcdb || $svc_phone || $svc_hardware ) {
         my $part_pkg = $cust_pkg->part_pkg;
        unless ( $part_pkg ) {
          $dbh->rollback if $oldAutoCommit;
@@ -361,12 +511,20 @@ sub batch_import {
           $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
           push @svc_x, $svc_phone;
         }
+        if ( $svc_hardware ) {
+          $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
+          push @svc_x, $svc_hardware;
+        }
+
       }
 
       $hash{$cust_pkg} = \@svc_x;
     }
 
-    my $error = $cust_main->insert( \%hash, $invoicing_list );
+    my %options = ('invoicing_list' => $invoicing_list);
+    $options{'cust_payby'} = [ $cust_payby ] if $cust_payby;
+
+    my $error = $cust_main->insert( \%hash, %options );
 
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;