use File::Temp; #qw( tempfile );
use Business::CreditCard 0.28;
use List::Util qw(min);
+use Try::Tiny;
use FS::UID qw( dbh driver_name );
use FS::Record qw( qsearchs qsearch dbdef regexp_sql );
use FS::Cursor;
use FS::sales;
use FS::cust_payby;
use FS::contact;
+use FS::reason;
# 1 is mostly method/subroutine entry and options
# 2 traces progress of some operations
=item quiet - can be set true to supress email cancellation notices.
-=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 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 cust_pkg_reason - can be an arrayref of L<FS::cust_pkg_reason> objects
for the individual packages, parallel to the C<cust_pkg> argument. The
sub cancel_pkgs {
my( $self, %opt ) = @_;
- my $oldAutoCommit = $FS::UID::AutoCommit;
+ # we're going to cancel services, which is not reversible
+ die "cancel_pkgs cannot be run inside a transaction"
+ if $FS::UID::AutoCommit == 0;
+
local $FS::UID::AutoCommit = 0;
return ( 'access denied' )
my $ban = new FS::banned_pay $cust_payby->_new_banned_pay_hashref;
my $error = $ban->insert;
if ($error) {
- dbh->rollback if $oldAutoCommit;
+ dbh->rollback;
return ( $error );
}
'time' => $cancel_time );
if ($error) {
warn "Error billing during cancel, custnum ". $self->custnum. ": $error";
- dbh->rollback if $oldAutoCommit;
+ dbh->rollback;
return ( "Error billing during cancellation: $error" );
}
}
+ dbh->commit;
my @errors;
- # now cancel all services, the same way we would for individual packages
+ # try to cancel each service, the same way we would for individual packages,
+ # but in cancel weight order.
my @cust_svc = map { $_->cust_svc } @pkgs;
my @sorted_cust_svc =
map { $_->[0] }
foreach my $cust_svc (@sorted_cust_svc) {
my $part_svc = $cust_svc->part_svc;
next if ( defined($part_svc) and $part_svc->preserve );
- my $error = $cust_svc->cancel; # immediate cancel, no date option
- push @errors, $error if $error;
+ # immediate cancel, no date option
+ # transactionize individually
+ my $error = try { $cust_svc->cancel } catch { $_ };
+ if ( $error ) {
+ dbh->rollback;
+ push @errors, $error;
+ } else {
+ dbh->commit;
+ }
}
if (@errors) {
- dbh->rollback if $oldAutoCommit;
return @errors;
}
if ($opt{'cust_pkg_reason'}) {
@cprs = @{ delete $opt{'cust_pkg_reason'} };
}
+ my $null_reason;
foreach (@pkgs) {
my %lopt = %opt;
if (@cprs) {
my $cpr = shift @cprs;
- $lopt{'reason'} = $cpr->reasonnum;
- $lopt{'reason_otaker'} = $cpr->otaker;
+ if ( $cpr ) {
+ $lopt{'reason'} = $cpr->reasonnum;
+ $lopt{'reason_otaker'} = $cpr->otaker;
+ } else {
+ warn "no reason found when canceling package ".$_->pkgnum."\n";
+ # we're not actually required to pass a reason to cust_pkg::cancel,
+ # but if we're getting to this point, something has gone awry.
+ $null_reason ||= FS::reason->new_or_existing(
+ reason => 'unknown reason',
+ type => 'Cancel Reason',
+ class => 'C',
+ );
+ $lopt{'reason'} = $null_reason->reasonnum;
+ $lopt{'reason_otaker'} = $FS::CurrentUser::CurrentUser->username;
+ }
}
my $error = $_->cancel(%lopt);
- push @errors, 'pkgnum '.$_->pkgnum.': '.$error if $error;
- }
-
- if (@errors) {
- dbh->rollback if $oldAutoCommit;
- return @errors;
+ if ( $error ) {
+ dbh->rollback;
+ push @errors, 'pkgnum '.$_->pkgnum.': '.$error;
+ } else {
+ dbh->commit;
+ }
}
- return;
+ return @errors;
}
sub _banned_pay_hashref {
=cut
sub uncancelled_sql { uncancel_sql(@_); }
-sub uncancel_sql { "
- ( 0 < ( $select_count_pkgs
- AND ( cust_pkg.cancel IS NULL
- OR cust_pkg.cancel = 0
- )
- )
- OR 0 = ( $select_count_pkgs )
- )
-"; }
+sub uncancel_sql {
+ my $self = shift;
+ "( NOT (".$self->cancelled_sql.") )"; #sensitive to cust_main-status_module
+}
=item balance_sql