ee190fb8d82eab253f1cae20c917944b39f8be0c
[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   } else {
73     $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
74     unless ( $cust_svc ) {
75       $dbh->rollback if $oldAutoCommit;
76       return "no cust_svc record found for svcnum ". $self->svcnum;
77     }
78     $self->pkgnum($cust_svc->pkgnum);
79     $self->svcpart($cust_svc->svcpart);
80   }
81
82   $error = $self->SUPER::insert;
83   if ( $error ) {
84     $dbh->rollback if $oldAutoCommit;
85     return $error;
86   }
87
88   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
89
90   '';
91 }
92
93 =item delete
94
95 Deletes this account from the database.  If there is an error, returns the
96 error, otherwise returns false.
97
98 The corresponding FS::cust_svc record will be deleted as well.
99
100 =cut
101
102 sub delete {
103   my $self = shift;
104   my $error;
105
106   local $SIG{HUP} = 'IGNORE';
107   local $SIG{INT} = 'IGNORE';
108   local $SIG{QUIT} = 'IGNORE';
109   local $SIG{TERM} = 'IGNORE';
110   local $SIG{TSTP} = 'IGNORE';
111   local $SIG{PIPE} = 'IGNORE';
112
113   my $svcnum = $self->svcnum;
114
115   $error = $self->SUPER::delete;
116   return $error if $error;
117
118   my $cust_svc = $self->cust_svc;
119   $error = $cust_svc->delete;
120   return $error if $error;
121
122   '';
123 }
124
125 =item setfixed
126
127 Sets any fixed fields for this service (see L<FS::part_svc>).  If there is an
128 error, returns the error, otherwise returns the FS::part_svc object (use ref()
129 to test the return).  Usually called by the check method.
130
131 =cut
132
133 sub setfixed {
134   my $self = shift;
135   $self->setx('F');
136 }
137
138 =item setdefault
139
140 Sets all fields to their defaults (see L<FS::part_svc>), overriding their
141 current values.  If there is an error, returns the error, otherwise returns
142 the FS::part_svc object (use ref() to test the return).
143
144 =cut
145
146 sub setdefault {
147   my $self = shift;
148   $self->setx('D');
149 }
150
151 sub setx {
152   my $self = shift;
153   my $x = shift;
154
155   my $error;
156
157   $error =
158     $self->ut_numbern('svcnum')
159   ;
160   return $error if $error;
161
162   #get part_svc
163   my $svcpart;
164   if ( $self->svcnum ) {
165     my $cust_svc = $self->cust_svc;
166     return "Unknown svcnum" unless $cust_svc; 
167     $svcpart = $cust_svc->svcpart;
168   } else {
169     $svcpart = $self->getfield('svcpart');
170   }
171   my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $svcpart } );
172   return "Unkonwn svcpart" unless $part_svc;
173
174   #set default/fixed/whatever fields from part_svc
175   my $table = $self->table;
176   foreach my $field ( grep { $_ ne 'svcnum' } fields($table) ) {
177     my $part_svc_column = $part_svc->part_svc_column($field);
178     if ( $part_svc_column->columnflag eq $x ) {
179       $self->setfield( $field, $part_svc_column->columnvalue );
180     }
181   }
182
183  $part_svc;
184
185 }
186
187 =item cust_svc
188
189 Returns the cust_svc record associated with this svc_ record, as a FS::cust_svc
190 object (see L<FS::cust_svc>).
191
192 =cut
193
194 sub cust_svc {
195   my $self = shift;
196   qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
197 }
198
199 =item suspend
200
201 =item unsuspend
202
203 =item cancel
204
205 Stubs - return false (no error) so derived classes don't need to define these
206 methods.  Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
207
208 =cut
209
210 sub suspend { ''; }
211 sub unsuspend { ''; }
212 sub cancel { ''; }
213
214 =back
215
216 =head1 VERSION
217
218 $Id: svc_Common.pm,v 1.8 2002-03-18 16:05:35 ivan Exp $
219
220 =head1 BUGS
221
222 The setfixed method return value.
223
224 =head1 SEE ALSO
225
226 L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, schema.html
227 from the base documentation.
228
229 =cut
230
231 1;
232