allow cust_http export to be triggered for location changes, RT#22830
authorIvan Kohler <ivan@freeside.biz>
Mon, 28 Apr 2014 00:43:55 +0000 (17:43 -0700)
committerIvan Kohler <ivan@freeside.biz>
Mon, 28 Apr 2014 00:43:55 +0000 (17:43 -0700)
FS/FS/Conf.pm
FS/FS/cust_location.pm
FS/FS/part_export/cust_http.pm
FS/FS/part_export/http.pm

index 59ccd90..5663c19 100644 (file)
@@ -5215,6 +5215,33 @@ and customer address. Include units.',
     },
   },
 
+  #false laziness w/above options_sub and option_sub
+  {
+    'key'         => 'cust_location-exports',
+    'section'     => '',
+    'description' => 'Export(s) to call on cust_location insert, modification and deletion.',
+    'type'        => 'select-sub',
+    'multiple'    => 1,
+    'options_sub' => sub {
+      require FS::Record;
+      require FS::part_export;
+      my @part_export =
+        map { qsearch( 'part_export', {exporttype => $_ } ) }
+          keys %{FS::part_export::export_info('cust_location')};
+      map { $_->exportnum => $_->exporttype.' to '.$_->machine } @part_export;
+    },
+    'option_sub'  => sub {
+      require FS::Record;
+      require FS::part_export;
+      my $part_export = FS::Record::qsearchs(
+        'part_export', { 'exportnum' => shift }
+      );
+      $part_export
+        ? $part_export->exporttype.' to '.$part_export->machine
+        : '';
+    },
+  },
+
   {
     'key'         => 'cust_tag-location',
     'section'     => 'UI',
index 51fcba3..70ccbfd 100644 (file)
@@ -12,6 +12,7 @@ use FS::Conf;
 use FS::prospect_main;
 use FS::cust_main;
 use FS::cust_main_county;
+use FS::part_export;
 use FS::GeocodeCache;
 
 $import = 0;
@@ -205,19 +206,49 @@ sub insert {
     $self->set('censusyear' => $conf->config('census_year') || 2012);
   }
 
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
   my $error = $self->SUPER::insert(@_);
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
 
   #false laziness with cust_main, will go away eventually
-  if ( !$import and !$error and $conf->config('tax_district_method') ) {
+  if ( !$import and $conf->config('tax_district_method') ) {
 
     my $queue = new FS::queue {
       'job' => 'FS::geocode_Mixin::process_district_update'
     };
     $error = $queue->insert( ref($self), $self->locationnum );
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
 
   }
 
-  $error || '';
+  # cust_location exports
+  #my $export_args = $options{'export_args'} || [];
+
+  my @part_export =
+    map qsearch( 'part_export', {exportnum=>$_} ),
+      $conf->config('cust_location-exports'); #, $agentnum
+
+  foreach my $part_export ( @part_export ) {
+    my $error = $part_export->export_insert($self); #, @$export_args);
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "exporting to ". $part_export->exporttype.
+             " (transaction rolled back): $error";
+    }
+  }
+
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  '';
 }
 
 =item delete
@@ -242,7 +273,35 @@ sub replace {
     }
   }
 
-  $self->SUPER::replace($old);
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my $error = $self->SUPER::replace($old);
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  # cust_location exports
+  #my $export_args = $options{'export_args'} || [];
+
+  my @part_export =
+    map qsearch( 'part_export', {exportnum=>$_} ),
+      $conf->config('cust_location-exports'); #, $agentnum
+
+  foreach my $part_export ( @part_export ) {
+    my $error = $part_export->export_replace($self, $old); #, @$export_args);
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "exporting to ". $part_export->exporttype.
+             " (transaction rolled back): $error";
+    }
+  }
+
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  '';
 }
 
 
index e834f93..5ccb403 100644 (file)
@@ -19,10 +19,10 @@ tie %options, 'Tie::IxHash',
     default => join("\n",
       "action  'insert'",
       "custnum \$cust_main->custnum",
-      "first   \$cust_main->first",
-      "last    \$cust_main->get('last')",
-      ( map "$_ \$cust_main->$_", qw( company address1 address2 city county state zip country daytime night fax  last ) ),
-      "email   \$cust_main->invoicing_list_emailonly_scalar",
+#      "first   \$cust_main->first",
+#      "last    \$cust_main->get('last')",
+#      ( map "$_ \$cust_main->$_", qw( company address1 address2 city county state zip country daytime night fax ) ),
+#      "email   \$cust_main->invoicing_list_emailonly_scalar",
     ),
   },
   'delete_data' => {
@@ -39,10 +39,10 @@ tie %options, 'Tie::IxHash',
     default => join("\n",
       "action  'replace'",
       "custnum \$new_cust_main->custnum",
-      "first   \$new_cust_main->first",
-      "last    \$new_cust_main->get('last')",
-      ( map "$_ \$cust_main->$_", qw( company address1 address2 city county state zip country daytime night fax  last ) ),
-      "email   \$new_cust_main->invoicing_list_emailonly_scalar",
+#      "first   \$new_cust_main->first",
+#      "last    \$new_cust_main->get('last')",
+#      ( map "$_ \$cust_main->$_", qw( company address1 address2 city county state zip country daytime night fax ) ),
+#      "email   \$new_cust_main->invoicing_list_emailonly_scalar",
     ),
   },
   'success_regexp' => {
@@ -52,7 +52,7 @@ tie %options, 'Tie::IxHash',
 ;
 
 %info = (
-  'svc'     => 'cust_main',
+  'svc'     => [qw( cust_main cust_location )],
   'desc'    => 'Send an HTTP or HTTPS GET or POST request, for customers.',
   'options' => \%options,
   'no_machine' => 1,
index 0d62409..aeeb59b 100644 (file)
@@ -91,9 +91,7 @@ sub _export_command {
 
   return unless $self->option("${action}_data");
 
-  my $cust_main = $svc_x->table eq 'cust_main'
-                    ? $svc_x
-                    : $svc_x->cust_svc->cust_pkg->cust_main;
+  my $cust_main = $svc_x->cust_main or return;
 
   $self->http_queue( $svc_x->svcnum,
     $self->option('method'),
@@ -115,9 +113,7 @@ sub _export_replace {
 
   return unless $self->option('replace_data');
 
-  my $new_cust_main = $new->table eq 'cust_main'
-                        ? $new
-                        : $new->cust_svc->cust_pkg->cust_main;
+  my $new_cust_main = $new->cust_main or return;
   my $cust_main = $new_cust_main; #so folks can use $new_cust_main or $cust_main
 
   $self->http_queue( $new->svcnum,