+ my $self = shift;
+
+ if ( qsearch( 'cust_bill', { 'custnum' => $self->custnum } ) ) {
+ return "Can't delete a customer with invoices";
+ }
+ if ( qsearch( 'cust_credit', { 'custnum' => $self->custnum } ) ) {
+ return "Can't delete a customer with credits";
+ }
+
+ 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 @cust_pkg = qsearch( 'cust_pkg', { 'custnum' => $self->custnum } );
+ if ( @cust_pkg ) {
+ my $new_custnum = shift;
+ return "Invalid new customer number: $new_custnum"
+ unless qsearchs( 'cust_main', { 'custnum' => $new_custnum } );
+ foreach my $cust_pkg ( @cust_pkg ) {
+ my %hash = $cust_pkg->hash;
+ $hash{'custnum'} = $new_custnum;
+ my $new_cust_pkg = new FS::cust_pkg ( \%hash );
+ my $error = $new_cust_pkg->replace($cust_pkg);
+ return $error if $error;
+ }
+ }
+ foreach my $cust_main_invoice (
+ qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
+ ) {
+ my $error = $cust_main_invoice->delete;
+ return $error if $error;
+ }
+
+ $self->SUPER::delete;