credit limit for CDR prerating, RT#27267
[freeside.git] / FS / FS / cust_main.pm
index 3e36c60..b37b0da 100644 (file)
@@ -1,18 +1,21 @@
 package FS::cust_main;
-
-require 5.006;
-use strict;
-use base qw( FS::cust_main::Packages FS::cust_main::Status
+use base qw( FS::cust_main::Packages
+             FS::cust_main::Status
              FS::cust_main::NationalID
-             FS::cust_main::Billing FS::cust_main::Billing_Realtime
+             FS::cust_main::Billing
+             FS::cust_main::Billing_Realtime
              FS::cust_main::Billing_Discount
              FS::cust_main::Billing_ThirdParty
              FS::cust_main::Location
+             FS::cust_main::Credit_Limit
              FS::otaker_Mixin FS::payinfo_Mixin FS::cust_main_Mixin
              FS::geocode_Mixin FS::Quotable_Mixin FS::Sales_Mixin
              FS::o2m_Common
              FS::Record
            );
+
+require 5.006;
+use strict;
 use vars qw( $DEBUG $me $conf
              @encrypted_fields
              $import
@@ -60,7 +63,6 @@ use FS::cust_class;
 use FS::cust_main_exemption;
 use FS::cust_tax_adjustment;
 use FS::cust_tax_location;
-use FS::agent;
 use FS::agent_currency;
 use FS::cust_main_invoice;
 use FS::cust_tag;
@@ -349,6 +351,9 @@ created and inserted.
 
 If I<prospectnum> is set, moves contacts and locations from that prospect.
 
+If I<contact> is set to an arrayref of FS::contact objects, inserts those
+new contacts with this new customer.
+
 =cut
 
 sub insert {
@@ -411,7 +416,9 @@ sub insert {
     my $loc = delete $self->hashref->{$l};
     # XXX if we're moving a prospect's locations, do that here
     if ( !$loc ) {
-      return "$l not set";
+      #return "$l not set";
+      #location-less customer records are now permitted
+      next;
     }
     
     if ( !$loc->locationnum ) {
@@ -461,7 +468,7 @@ sub insert {
   foreach my $l (qw(bill_location ship_location)) {
     warn "  setting $l.custnum\n"
       if $DEBUG > 1;
-    my $loc = $self->$l;
+    my $loc = $self->$l or next;
     unless ( $loc->custnum ) {
       $loc->set(custnum => $self->custnum);
       $error ||= $loc->replace;
@@ -535,6 +542,21 @@ sub insert {
 
   }
 
+  my $contact = delete $options{'contact'};
+  if ( $contact ) {
+
+    foreach my $c ( @$contact ) {
+      $c->custnum($self->custnum);
+      my $error = $c->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+
+    }
+
+  }
+
   warn "  setting cust_main_exemption\n"
     if $DEBUG > 1;
 
@@ -1662,13 +1684,26 @@ sub queue_fuzzyfiles_update {
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
-  my @locations = $self->bill_location;
-  push @locations, $self->ship_location if $self->has_ship_address;
+  foreach my $field ( 'first', 'last', 'company', 'ship_company' ) {
+    my $queue = new FS::queue { 
+      'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
+    };
+    my @args = "cust_main.$field", $self->get($field);
+    my $error = $queue->insert( @args );
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "queueing job (transaction rolled back): $error";
+    }
+  }
+
+  my @locations = ();
+  push @locations, $self->bill_location if $self->bill_locationnum;
+  push @locations, $self->ship_location if @locations && $self->has_ship_address;
   foreach my $location (@locations) {
     my $queue = new FS::queue { 
-      'job' => 'FS::cust_main::Search::append_fuzzyfiles'
+      'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
     };
-    my @args = map $location->get($_), @FS::cust_main::Search::fuzzyfields;
+    my @args = 'cust_location.address1', $location->address1;
     my $error = $queue->insert( @args );
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
@@ -1700,8 +1735,8 @@ sub check {
     || $self->ut_number('agentnum')
     || $self->ut_textn('agent_custid')
     || $self->ut_number('refnum')
-    || $self->ut_foreign_key('bill_locationnum', 'cust_location','locationnum')
-    || $self->ut_foreign_key('ship_locationnum', 'cust_location','locationnum')
+    || $self->ut_foreign_keyn('bill_locationnum', 'cust_location','locationnum')
+    || $self->ut_foreign_keyn('ship_locationnum', 'cust_location','locationnum')
     || $self->ut_foreign_keyn('classnum', 'cust_class', 'classnum')
     || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
     || $self->ut_textn('custbatch')
@@ -1712,6 +1747,7 @@ sub check {
     || $self->ut_snumbern('spouse_birthdate')
     || $self->ut_snumbern('anniversary_date')
     || $self->ut_textn('company')
+    || $self->ut_textn('ship_company')
     || $self->ut_anything('comments')
     || $self->ut_numbern('referral_custnum')
     || $self->ut_textn('stateid')
@@ -1729,11 +1765,13 @@ sub check {
     || $self->ut_currencyn('currency')
   ;
 
-  my $company = $self->company;
-  $company =~ s/^\s+//; 
-  $company =~ s/\s+$//; 
-  $company =~ s/\s+/ /g;
-  $self->company($company);
+  foreach (qw(company ship_company)) {
+    my $company = $self->get($_);
+    $company =~ s/^\s+//; 
+    $company =~ s/\s+$//; 
+    $company =~ s/\s+/ /g;
+    $self->set($_, $company);
+  }
 
   #barf.  need message catalogs.  i18n.  etc.
   $error .= "Please select an advertising source."
@@ -2011,9 +2049,17 @@ sub check {
   ) {
     $self->payname( $self->first. " ". $self->getfield('last') );
   } else {
-    $self->payname =~ /^([\w \,\.\-\'\&]*)$/
-      or return gettext('illegal_name'). " payname: ". $self->payname;
-    $self->payname($1);
+
+    if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
+      $self->payname =~ /^([\w \,\.\-\']*)$/
+        or return gettext('illegal_name'). " payname: ". $self->payname;
+      $self->payname($1);
+    } else {
+      $self->payname =~ /^([\w \,\.\-\'\&]*)$/
+        or return gettext('illegal_name'). " payname: ". $self->payname;
+      $self->payname($1);
+    }
+
   }
 
   ### end of stuff moved to cust_payby
@@ -2082,7 +2128,7 @@ Returns all locations (see L<FS::cust_location>) for this customer.
 
 sub cust_location {
   my $self = shift;
-  qsearch('cust_location', { 'custnum' => $self->custnum,
+  qsearch('cust_location', { 'custnum'     => $self->custnum,
                              'prospectnum' => '' } );
 }
 
@@ -2312,13 +2358,6 @@ sub notes {
 
 Returns the agent (see L<FS::agent>) for this customer.
 
-=cut
-
-sub agent {
-  my $self = shift;
-  qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
-}
-
 =item agent_name
 
 Returns the agent name (see L<FS::agent>) for this customer.
@@ -2335,13 +2374,6 @@ sub agent_name {
 Returns any tags associated with this customer, as FS::cust_tag objects,
 or an empty list if there are no tags.
 
-=cut
-
-sub cust_tag {
-  my $self = shift;
-  qsearch('cust_tag', { 'custnum' => $self->custnum } );
-}
-
 =item part_tag
 
 Returns any tags associated with this customer, as FS::part_tag objects,
@@ -2360,17 +2392,6 @@ sub part_tag {
 Returns the customer class, as an FS::cust_class object, or the empty string
 if there is no customer class.
 
-=cut
-
-sub cust_class {
-  my $self = shift;
-  if ( $self->classnum ) {
-    qsearchs('cust_class', { 'classnum' => $self->classnum } );
-  } else {
-    return '';
-  } 
-}
-
 =item categoryname 
 
 Returns the customer category name, or the empty string if there is no customer
@@ -3044,13 +3065,6 @@ sub tax_exemption {
 
 =item cust_main_exemption
 
-=cut
-
-sub cust_main_exemption {
-  my $self = shift;
-  qsearch( 'cust_main_exemption', { 'custnum' => $self->custnum } );
-}
-
 =item invoicing_list [ ARRAYREF ]
 
 If an arguement is given, sets these email addresses as invoice recipients
@@ -4585,121 +4599,6 @@ sub search {
 
 =over 4
 
-=item batch_charge
-
-=cut
-
-sub batch_charge {
-  my $param = shift;
-  #warn join('-',keys %$param);
-  my $fh = $param->{filehandle};
-  my $agentnum = $param->{agentnum};
-  my $format = $param->{format};
-
-  my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
-
-  my @fields;
-  if ( $format eq 'simple' ) {
-    @fields = qw( custnum agent_custid amount pkg );
-  } else {
-    die "unknown format $format";
-  }
-
-  eval "use Text::CSV_XS;";
-  die $@ if $@;
-
-  my $csv = new Text::CSV_XS;
-  #warn $csv;
-  #warn $fh;
-
-  my $imported = 0;
-  #my $columns;
-
-  local $SIG{HUP} = 'IGNORE';
-  local $SIG{INT} = 'IGNORE';
-  local $SIG{QUIT} = 'IGNORE';
-  local $SIG{TERM} = 'IGNORE';
-  local $SIG{TSTP} = 'IGNORE';
-  local $SIG{PIPE} = 'IGNORE';
-
-  my $oldAutoCommit = $FS::UID::AutoCommit;
-  local $FS::UID::AutoCommit = 0;
-  my $dbh = dbh;
-  
-  #while ( $columns = $csv->getline($fh) ) {
-  my $line;
-  while ( defined($line=<$fh>) ) {
-
-    $csv->parse($line) or do {
-      $dbh->rollback if $oldAutoCommit;
-      return "can't parse: ". $csv->error_input();
-    };
-
-    my @columns = $csv->fields();
-    #warn join('-',@columns);
-
-    my %row = ();
-    foreach my $field ( @fields ) {
-      $row{$field} = shift @columns;
-    }
-
-    if ( $row{custnum} && $row{agent_custid} ) {
-      dbh->rollback if $oldAutoCommit;
-      return "can't specify custnum with agent_custid $row{agent_custid}";
-    }
-
-    my %hash = ();
-    if ( $row{agent_custid} && $agentnum ) {
-      %hash = ( 'agent_custid' => $row{agent_custid},
-                'agentnum'     => $agentnum,
-              );
-    }
-
-    if ( $row{custnum} ) {
-      %hash = ( 'custnum' => $row{custnum} );
-    }
-
-    unless ( scalar(keys %hash) ) {
-      $dbh->rollback if $oldAutoCommit;
-      return "can't find customer without custnum or agent_custid and agentnum";
-    }
-
-    my $cust_main = qsearchs('cust_main', { %hash } );
-    unless ( $cust_main ) {
-      $dbh->rollback if $oldAutoCommit;
-      my $custnum = $row{custnum} || $row{agent_custid};
-      return "unknown custnum $custnum";
-    }
-
-    if ( $row{'amount'} > 0 ) {
-      my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
-      if ( $error ) {
-        $dbh->rollback if $oldAutoCommit;
-        return $error;
-      }
-      $imported++;
-    } elsif ( $row{'amount'} < 0 ) {
-      my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
-                                      $row{'pkg'}                         );
-      if ( $error ) {
-        $dbh->rollback if $oldAutoCommit;
-        return $error;
-      }
-      $imported++;
-    } else {
-      #hmm?
-    }
-
-  }
-
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
-
-  return "Empty file!" unless $imported;
-
-  ''; #no error
-
-}
-
 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
 
 Deprecated.  Use event notification and message templates 
@@ -5040,6 +4939,9 @@ sub queued_bill {
   my $cust_main = qsearchs( 'cust_main', { custnum => $args{'custnum'} } );
   warn 'bill_and_collect custnum#'. $cust_main->custnum. "\n";#log custnum w/pid
 
+  #without this errors don't get rolled back
+  $args{'fatal'} = 1; # runs from job queue, will be caught
+
   $cust_main->bill_and_collect( %args );
 }