bc5b756404209656e1563be2ffb7e7d0375465f8
[freeside.git] / FS / FS / svc_Common.pm
1 package FS::svc_Common;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearchs fields dbh );
6 use FS::cust_svc;
7 use FS::part_svc;
8
9 @ISA = qw( FS::Record );
10
11 =head1 NAME
12
13 FS::svc_Common - Object method for all svc_ records
14
15 =head1 SYNOPSIS
16
17 use FS::svc_Common;
18
19 @ISA = qw( FS::svc_Common );
20
21 =head1 DESCRIPTION
22
23 FS::svc_Common is intended as a base class for table-specific classes to
24 inherit from, i.e. FS::svc_acct.  FS::svc_Common inherits from FS::Record.
25
26 =head1 METHODS
27
28 =over 4
29
30 =item insert
31
32 Adds this record to the database.  If there is an error, returns the error,
33 otherwise returns false.
34
35 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be 
36 defined.  An FS::cust_svc record will be created and inserted.
37
38 =cut
39
40 sub insert {
41   my $self = shift;
42   my $error;
43
44   local $SIG{HUP} = 'IGNORE';
45   local $SIG{INT} = 'IGNORE';
46   local $SIG{QUIT} = 'IGNORE';
47   local $SIG{TERM} = 'IGNORE';
48   local $SIG{TSTP} = 'IGNORE';
49   local $SIG{PIPE} = 'IGNORE';
50
51   my $oldAutoCommit = $FS::UID::AutoCommit;
52   local $FS::UID::AutoCommit = 0;
53   my $dbh = dbh;
54
55   $error = $self->check;
56   return $error if $error;
57
58   my $svcnum = $self->svcnum;
59   my $cust_svc;
60   unless ( $svcnum ) {
61     $cust_svc = new FS::cust_svc ( {
62       #hua?# 'svcnum'  => $svcnum,
63       'pkgnum'  => $self->pkgnum,
64       'svcpart' => $self->svcpart,
65     } );
66     $error = $cust_svc->insert;
67     if ( $error ) {
68       $dbh->rollback if $oldAutoCommit;
69       return $error;
70     }
71     $svcnum = $self->svcnum($cust_svc->svcnum);
72   }
73
74   $error = $self->SUPER::insert;
75   if ( $error ) {
76     $dbh->rollback if $oldAutoCommit;
77     return $error;
78   }
79
80   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
81
82   '';
83 }
84
85 =item delete
86
87 Deletes this account from the database.  If there is an error, returns the
88 error, otherwise returns false.
89
90 The corresponding FS::cust_svc record will be deleted as well.
91
92 =cut
93
94 sub delete {
95   my $self = shift;
96   my $error;
97
98   local $SIG{HUP} = 'IGNORE';
99   local $SIG{INT} = 'IGNORE';
100   local $SIG{QUIT} = 'IGNORE';
101   local $SIG{TERM} = 'IGNORE';
102   local $SIG{TSTP} = 'IGNORE';
103   local $SIG{PIPE} = 'IGNORE';
104
105   my $svcnum = $self->svcnum;
106
107   $error = $self->SUPER::delete;
108   return $error if $error;
109
110   my $cust_svc = qsearchs( 'cust_svc' , { 'svcnum' => $svcnum } );  
111   $error = $cust_svc->delete;
112   return $error if $error;
113
114   '';
115 }
116
117 =item setfixed
118
119 Sets any fixed fields for this service (see L<FS::part_svc>).  If there is an
120 error, returns the error, otherwise returns the FS::part_svc object (use ref()
121 to test the return).  Usually called by the check method.
122
123 =cut
124
125 sub setfixed {
126   my $self = shift;
127   $self->setx('F');
128 }
129
130 =item setdefault
131
132 Sets all fields to their defaults (see L<FS::part_svc>), overriding their
133 current values.  If there is an error, returns the error, otherwise returns
134 the FS::part_svc object (use ref() to test the return).
135
136 =cut
137
138 sub setdefault {
139   my $self = shift;
140   $self->setx('D');
141 }
142
143 sub setx {
144   my $self = shift;
145   my $x = shift;
146
147   my $error;
148
149   $error =
150     $self->ut_numbern('svcnum')
151   ;
152   return $error if $error;
153
154   #get part_svc
155   my $svcpart;
156   if ( $self->svcnum ) {
157     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
158     return "Unknown svcnum" unless $cust_svc; 
159     $svcpart = $cust_svc->svcpart;
160   } else {
161     $svcpart = $self->getfield('svcpart');
162   }
163   my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $svcpart } );
164   return "Unkonwn svcpart" unless $part_svc;
165
166   #set default/fixed/whatever fields from part_svc
167   foreach my $field ( fields('svc_acct') ) {
168     if ( $part_svc->getfield('svc_acct__'. $field. '_flag') eq $x ) {
169       $self->setfield( $field, $part_svc->getfield('svc_acct__'. $field) );
170     }
171   }
172
173  $part_svc;
174
175 }
176
177 =item suspend
178
179 =item unsuspend
180
181 =item cancel
182
183 Stubs - return false (no error) so derived classes don't need to define these
184 methods.  Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
185
186 =cut
187
188 sub suspend { ''; }
189 sub unsuspend { ''; }
190 sub cancel { ''; }
191
192 =back
193
194 =head1 VERSION
195
196 $Id: svc_Common.pm,v 1.4 2001-04-22 00:49:30 ivan Exp $
197
198 =head1 BUGS
199
200 The setfixed method return value.
201
202 The new method should set defaults from part_svc (like the check method
203 sets fixed values)?
204
205 =head1 SEE ALSO
206
207 L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, schema.html
208 from the base documentation.
209
210 =cut
211
212 1;
213