summaryrefslogtreecommitdiff
path: root/FS/FS/device_Common.pm
blob: ac00b7669951be3620eaafeb979c60c19e8181a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package FS::device_Common;

use strict;
use NEXT;
use FS::Record qw( qsearch dbh ); # qsearchs );

=head1 NAME

FS::device_Common - Base class for svc_X classes which have associated X_devices

=head1 SYNOPSIS

  package FS::svc_newservice
  use base qw( FS::device_Common FS::svc_Common );

=head1 DESCRIPTION

=cut

sub _device_table {
  my $self = shift;
  ( my $device_table = $self->table ) =~ s/^svc_//;
  $device_table.'_device';
}

sub device_table {
  my $self = shift;
  my $device_table = $self->_device_table;
  eval "use FS::$device_table;";
  die $@ if $@;
  $device_table;
}

sub device_objects {
  my $self = shift;
  qsearch($self->device_table, { 'svcnum' => $self->svcnum } );
}

sub delete {
  my $self = shift;

  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;

  foreach my $device ( $self->device_objects ) {
    my $error = $device->delete;
    if ( $error ) {
      $dbh->rollback if $oldAutoCommit;
      return $error;
    }
  }

  my $error = $self->NEXT::delete;
  if ( $error ) {
    $dbh->rollback if $oldAutoCommit;
    return $error;
  }

  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
  '';

}

=head1 BUGS

=head1 SEE ALSO

=cut

1;