From 496842b7db855bd616844c7667e5d70de42535fd Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Sun, 27 Apr 2014 17:43:55 -0700 Subject: [PATCH] allow cust_http export to be triggered for location changes, RT#22830 --- FS/FS/Conf.pm | 27 ++++++++++++++++++ FS/FS/cust_location.pm | 65 ++++++++++++++++++++++++++++++++++++++++-- FS/FS/part_export/cust_http.pm | 18 ++++++------ FS/FS/part_export/http.pm | 8 ++---- 4 files changed, 100 insertions(+), 18 deletions(-) diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index 59ccd90ab..5663c1939 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -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', diff --git a/FS/FS/cust_location.pm b/FS/FS/cust_location.pm index 51fcba35b..70ccbfd7e 100644 --- a/FS/FS/cust_location.pm +++ b/FS/FS/cust_location.pm @@ -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; + ''; } diff --git a/FS/FS/part_export/cust_http.pm b/FS/FS/part_export/cust_http.pm index e834f93ea..5ccb4035a 100644 --- a/FS/FS/part_export/cust_http.pm +++ b/FS/FS/part_export/cust_http.pm @@ -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, diff --git a/FS/FS/part_export/http.pm b/FS/FS/part_export/http.pm index 0d62409fc..aeeb59bfa 100644 --- a/FS/FS/part_export/http.pm +++ b/FS/FS/part_export/http.pm @@ -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, -- 2.11.0