move invoice_latex templating to Text::Template, with special sauce^W^Wbackwards...
[freeside.git] / FS / FS / cust_main.pm
index 8246b93..c494591 100644 (file)
@@ -1,7 +1,8 @@
 package FS::cust_main;
 
 use strict;
-use vars qw( @ISA @EXPORT_OK $conf $DEBUG $import @encrypted_fields);
+use vars qw( @ISA @EXPORT_OK $DEBUG $conf @encrypted_fields
+             $import $skip_fuzzyfiles );
 use vars qw( $realtime_bop_decline_quiet ); #ugh
 use Safe;
 use Carp;
@@ -21,6 +22,7 @@ use FS::UID qw( getotaker dbh );
 use FS::Record qw( qsearchs qsearch dbdef );
 use FS::Misc qw( send_email );
 use FS::cust_pkg;
+use FS::cust_svc;
 use FS::cust_bill;
 use FS::cust_bill_pkg;
 use FS::cust_pay;
@@ -52,6 +54,7 @@ $DEBUG = 0;
 #$DEBUG = 1;
 
 $import = 0;
+$skip_fuzzyfiles = 0;
 
 @encrypted_fields = ('payinfo', 'paycvv');
 
@@ -419,10 +422,12 @@ sub insert {
     }
   }
 
-  $error = $self->queue_fuzzyfiles_update;
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return "updating fuzzy search cache: $error";
+  unless ( $import || $skip_fuzzyfiles ) {
+    $error = $self->queue_fuzzyfiles_update;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "updating fuzzy search cache: $error";
+    }
   }
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
@@ -445,6 +450,9 @@ be a better explanation of this, but until then, here's an example:
   );
   $cust_main->order_pkgs( \%hash, \'0', 'noexport'=>1 );
 
+Services can be new, in which case they are inserted, or existing unaudited
+services, in which case they are linked to the newly-created package.
+
 Currently available options are: I<depend_jobnum> and I<noexport>.
 
 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
@@ -493,12 +501,19 @@ sub order_pkgs {
       return "inserting cust_pkg (transaction rolled back): $error";
     }
     foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
-      $svc_something->pkgnum( $cust_pkg->pkgnum );
-      if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
-        $svc_something->seconds( $svc_something->seconds + $$seconds );
-        $$seconds = 0;
+      if ( $svc_something->svcnum ) {
+        my $old_cust_svc = $svc_something->cust_svc;
+        my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
+        $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
+        $error = $new_cust_svc->replace($old_cust_svc);
+      } else {
+        $svc_something->pkgnum( $cust_pkg->pkgnum );
+        if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
+          $svc_something->seconds( $svc_something->seconds + $$seconds );
+          $$seconds = 0;
+        }
+        $error = $svc_something->insert(%svc_options);
       }
-      $error = $svc_something->insert(%svc_options);
       if ( $error ) {
         $dbh->rollback if $oldAutoCommit;
         #return "inserting svc_ (transaction rolled back): $error";
@@ -724,10 +739,12 @@ sub replace {
     }
   }
 
-  $error = $self->queue_fuzzyfiles_update;
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return "updating fuzzy search cache: $error";
+  unless ( $import || $skip_fuzzyfiles ) {
+    $error = $self->queue_fuzzyfiles_update;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "updating fuzzy search cache: $error";
+    }
   }
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
@@ -1024,7 +1041,7 @@ sub check {
   ) {
     $self->payname( $self->first. " ". $self->getfield('last') );
   } else {
-    $self->payname =~ /^([\w \,\.\-\']+)$/
+    $self->payname =~ /^([\w \,\.\-\'\&]+)$/
       or return gettext('illegal_name'). " payname: ". $self->payname;
     $self->payname($1);
   }
@@ -1896,7 +1913,10 @@ sub realtime_bop {
        || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
     push @invoicing_list, $self->all_emails;
   }
-  my $email = $invoicing_list[0];
+
+  my $email = ($conf->exists('business-onlinepayment-email-override'))
+              ? $conf->config('business-onlinepayment-email-override')
+              : $invoicing_list[0];
 
   my $payinfo = exists($options{'payinfo'})
                   ? $options{'payinfo'}
@@ -2193,7 +2213,7 @@ sub realtime_refund_bop {
     $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
       or return "Unknown paynum $options{'paynum'}";
     $amount ||= $cust_pay->paid;
-    $cust_pay->paybatch =~ /^(\w+):(\w*)(:(\w+))?$/
+    $cust_pay->paybatch =~ /^(\w+):([\w-]*)(:(\w+))?$/
       or return "Can't parse paybatch for paynum $options{'paynum'}: ".
                 $cust_pay->paybatch;
     ( $pay_processor, $auth, $order_number ) = ( $1, $2, $4 );
@@ -2655,6 +2675,11 @@ is an error, returns the error, otherwise returns false.
 sub check_invoicing_list {
   my( $self, $arrayref ) = @_;
   foreach my $address ( @{$arrayref} ) {
+
+    if ($address eq 'FAX' and $self->getfield('fax') eq '') {
+      return 'Can\'t add FAX invoice destination with a blank FAX number.';
+    }
+
     my $cust_main_invoice = new FS::cust_main_invoice ( {
       'custnum' => $self->custnum,
       'dest'    => $address,