backport reason handling
authorjeff <jeff>
Mon, 30 Jun 2008 21:49:14 +0000 (21:49 +0000)
committerjeff <jeff>
Mon, 30 Jun 2008 21:49:14 +0000 (21:49 +0000)
FS/FS/cust_pkg.pm
httemplate/misc/process/cancel_pkg.html

index 0747465..91a9b81 100644 (file)
@@ -1873,6 +1873,26 @@ sub order {
   '';
 }
 
+=item insert_reason
+
+Associates this package with a (suspension or cancellation) reason (see
+L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
+L<FS::reason>).
+
+Available options are:
+
+=over 4
+
+=item reason - can be set to a cancellation reason (see L<FS:reason>), either a reasonnum of an existing reason, or passing a hashref will create a new reason.  The hashref should have the following keys: typenum - Reason type (see L<FS::reason_type>, reason - Text of the new reason.
+
+=item date
+
+=back
+
+If there is an error, returns the error, otherwise returns false.
+
+=cut
+
 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
 
 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
@@ -1936,15 +1956,39 @@ sub insert_reason {
 
   my $otaker = $FS::CurrentUser::CurrentUser->username;
 
+  my $reasonnum;
+  if ( $options{'reason'} =~ /^(\d+)$/ ) {
+
+    $reasonnum = $1;
+
+  } elsif ( ref($options{'reason'}) ) {
+
+    return 'Enter a new reason (or select an existing one)'
+      unless $options{'reason'}->{'reason'} !~ /^\s*$/;
+
+    my $reason = new FS::reason({
+      'reason_type' => $options{'reason'}->{'typenum'},
+      'reason'      => $options{'reason'}->{'reason'},
+    });
+    my $error = $reason->insert;
+    return $error if $error;
+
+    $reasonnum = $reason->reasonnum;
+
+  } else {
+    return "Unparsable reason: ". $options{'reason'};
+  }
+
   my $cust_pkg_reason =
     new FS::cust_pkg_reason({ 'pkgnum'    => $self->pkgnum,
-                              'reasonnum' => $options{'reason'}
+                              'reasonnum' => $reasonnum
                              'otaker'    => $otaker,
                              'date'      => $options{'date'}
                                               ? $options{'date'}
                                               : time,
                            });
-  return $cust_pkg_reason->insert;
+
+  $cust_pkg_reason->insert;
 }
 
 =item set_usage USAGE_VALUE_HASHREF 
index 1a8d23b..d265c18 100755 (executable)
@@ -50,45 +50,30 @@ if ($method eq 'expire' || $method eq 'adjourn'){
 
 my $cust_pkg = qsearchs( 'cust_pkg', {'pkgnum'=>$pkgnum} );
 
-my $oldAutoCommit = $FS::UID::AutoCommit;
-local $FS::UID::AutoCommit = 0;
-my $dbh = dbh;
+#my $otaker = $FS::CurrentUser::CurrentUser->name;
+#$otaker = $FS::CurrentUser::CurrentUser->username
+#  if ($otaker eq "User, Legacy");
 
-my $otaker = $FS::CurrentUser::CurrentUser->name;
-$otaker = $FS::CurrentUser::CurrentUser->username
-  if ($otaker eq "User, Legacy");
-
-my $error = '';
 if ($reasonnum == -1) {
-
-  $error = 'Enter a new reason (or select an existing one)'
-    unless $cgi->param('newreasonnum') !~ /^\s*$/;
-
-  my $reason = new FS::reason({ 'reason_type' => $cgi->param('newreasonnumT'),
-                                'reason'      => $cgi->param('newreasonnum'),
-                              });
-  $error ||= $reason->insert;
-  $reasonnum = $reason->reasonnum
-    unless $error;
+  $reasonnum = {
+    'typenum' => scalar( $cgi->param('newreasonnumT') ),
+    'reason'  => scalar( $cgi->param('newreasonnum' ) ),
+  };
 }
 
-unless ($error) {
-  if ($method eq 'expire' || $method eq 'adjourn'){
-    my %hash = $cust_pkg->hash;
-    $hash{$method}=$date;
-    my $new = new FS::cust_pkg (\%hash);
-    $error = $new->replace($cust_pkg, 'reason' => $reasonnum);
-  }else{
-    $error = $cust_pkg->$method( 'reason' => $reasonnum );
-  }
+my $error;
+if ($method eq 'expire' || $method eq 'adjourn'){
+  my %hash = $cust_pkg->hash;
+  $hash{$method} = $date;
+  my $new = new FS::cust_pkg \%hash;
+  $error = $new->replace($cust_pkg, 'reason' => $reasonnum);
+} else {
+  $error = $cust_pkg->$method( 'reason' => $reasonnum );
 }
 
 if ($error) {
   $cgi->param('error', $error);
-  $dbh->rollback if $oldAutoCommit;
   print $cgi->redirect(popurl(2). "cancel_pkg.html?". $cgi->query_string );
 }
 
-$dbh->commit or die $dbh->errstr if $oldAutoCommit;
-
 </%init>