refactor transaction from process/qual.cgi to qual.pm insert, RT#7111
authorivan <ivan>
Sat, 12 Mar 2011 21:13:34 +0000 (21:13 +0000)
committerivan <ivan>
Sat, 12 Mar 2011 21:13:34 +0000 (21:13 +0000)
FS/FS/qual.pm
httemplate/edit/process/qual.cgi

index 8f58389..e748382 100644 (file)
@@ -2,7 +2,7 @@ package FS::qual;
 
 use strict;
 use base qw( FS::option_Common );
-use FS::Record qw( qsearch qsearchs );
+use FS::Record qw( qsearch qsearchs dbh );
 
 =head1 NAME
 
@@ -74,7 +74,55 @@ otherwise returns false.
 
 =cut
 
-# the insert method can be inherited from FS::Record
+sub insert {
+  my $self = shift;
+  my %options = @_;
+
+  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;
+
+  if ( $options{'cust_location'} ) {
+    my $cust_location = $options{'cust_location'};
+    my $error = $cust_location->insert;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+    $self->locationnum( $cust_location->locationnum );
+  }
+
+  my @qual_option = ();
+  if ( $self->exportnum ) {
+    my $export = qsearchs( 'part_export', { 'exportnum' => $self->exportnum } )
+      or die 'Invalid exportnum';
+
+    my $qres = $export->qual($self);
+    unless ( ref($qres) ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "Qualification error: $qres";
+    }
+
+    $self->$_($qres->{$_}) foreach grep $qres->{$_}, qw(status vendor_qual_id);
+    @qual_option = ( $qres->{'options'} ) if ref($qres->{'options'});
+  }
+
+  my $error = $self->SUPER::insert(@qual_option);
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  '';
+}
 
 =item delete
 
index bc8c5a5..de076ed 100644 (file)
@@ -1,14 +1,11 @@
 %if ($error) {
 %  $cgi->param('error', $error);
-%  $dbh->rollback if $oldAutoCommit;
 <% $cgi->redirect(popurl(3). 'misc/qual.html?'. $cgi->query_string ) %>
 %} else {
-%  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 <% header('Qualification entered') %>
   <SCRIPT TYPE="text/javascript">
-    window.top.location = '<% popurl(3). "view/qual.cgi?qualnum=$qualnum" %>';
+    window.top.location = '<% popurl(3).'view/qual.cgi?qualnum='. $qual->qualnum %>';
   </SCRIPT>
-
   </BODY></HTML>
 %}
 <%init>
@@ -46,83 +43,35 @@ $cgi->param('locationnum') =~ /^(\-?\d*)$/
   or die 'illegal locationnum '. $cgi->param('locationnum');
 my $locationnum = $1;
 
-my $oldAutoCommit = $FS::UID::AutoCommit;
-local $FS::UID::AutoCommit = 0;
-my $dbh = dbh;
 my $error = '';
-my $cust_location;
+my $cust_location = '';
 if ( $locationnum == -1 ) { # adding a new one
-  my %location_hash = map { $_ => scalar($cgi->param($_)) }
-       qw( address1 address2 city county state zip country geocode );
-  $location_hash{$cust_or_prospect."num"} = $custnum_or_prospectnum;
-  $location_hash{location_type} = $cgi->param('location_type') 
-    if $cgi->param('location_type');
-  $location_hash{location_number} = $cgi->param('location_number') 
-    if $cgi->param('location_number');
-  $location_hash{location_kind} = $cgi->param('location_kind') 
-    if $cgi->param('location_kind');
-  $cust_location = new FS::cust_location ( { %location_hash } );
-  $error = $cust_location->insert;
-  die "Unable to insert cust_location: $error" if $error;
-}
-elsif ( $locationnum eq '' ) { # default service location
-    if ( $custnum ) { 
-         $cust_location = new FS::cust_location ( {
-               $cust_main_or_prospect_main->location_hash,
-               custnum => $custnum,
-         } );
-    } elsif ( $prospectnum ) {
-       die "a location must be specified explicitly for prospects";
-    }
-}
-elsif ( $locationnum != -2 ) { # -2 = address not required for qual
-  $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum })
-    or die 'Invalid locationnum'; 
-}
-
-my $export;
-if ( $exportnum > 0 ) {
- $export = qsearchs( 'part_export', { 'exportnum' => $exportnum } )
-    or die 'Invalid exportnum';
-}
 
-die "Nothing to qualify - neither TN nor address specified" 
-    unless ( defined $cust_location || $phonenum ne '' );
+  $cust_location = new FS::cust_location {
+    $cust_or_prospect."num" => $custnum_or_prospectnum,
+    map { $_ => scalar($cgi->param($_)) }
+      qw( address1 address2 city county state zip country geocode ),
+      grep scalar($cgi->param($_)),
+        qw( location_type location_number location_kind )
+  };
 
-my $qual;
-if ( $locationnum != -2 && $cust_location->locationnum > 0 ) {
-    $qual = new FS::qual( { locationnum => $cust_location->locationnum } );
-}
-else { # a cust_main default service address *OR* address not required
-    $qual = new FS::qual( { $cust_or_prospect."num" => $custnum_or_prospectnum } );
-}
-$qual->phonenum($phonenum) if $phonenum ne '';
-$qual->status('N');
+          #locationnum '': default service location
+} elsif ( $locationnum eq '' && $cust_or_prospect eq 'prospect' ) {
+    die "a location must be specified explicitly for prospects";
 
-if ( $export ) {
-    $qual->exportnum($export->exportnum);
-    my $qres = $export->qual($qual);
-    $error = "Qualification error: $qres" unless ref($qres);
-    unless ( $error ) {
-       $qual->status($qres->{'status'}) if $qres->{'status'};
-       $qual->vendor_qual_id($qres->{'vendor_qual_id'}) 
-           if $qres->{'vendor_qual_id'};
-       $error = $qual->insert($qres->{'options'}) if ref($qres->{'options'});
-    }
+          #locationnum -2: address not required for qual
+} elsif ( $locationnum == -2 && $phonenum eq '' ) {
+  $error = "Nothing to qualify - neither phone number nor address specified";
 }
 
-unless ( $error || $qual->qualnum ) {
-    $error = $qual->insert;
-}
+my $qual = new FS::qual {
+  'status' => 'N',
+};
+$qual->phonenum($phonenum) if $phonenum ne '';
+$qual->set( $cust_or_prospect."num" => $custnum_or_prospectnum )
+  unless $locationnum == -1 || $locationnum > 0;
+$qual->exportnum($exportnum) if $exportnum > 0;
 
-my $qualnum;
-unless ( $error ) {
-    if($qual->qualnum) {
-       $qualnum = $qual->qualnum;
-    }
-    else {
-       $error = "Unable to save qualification";
-    }
-}
+$error ||= $qual->insert( 'cust_location' => $cust_location );
 
 </%init>