9c0c4e69fb63f38251d2eeb7addc14c5c517dee2
[freeside.git] / site_perl / svc_Common.pm
1 package FS::svc_Common;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearchs fields );
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   $error = $self->check;
52   return $error if $error;
53
54   my $svcnum = $self->svcnum;
55   my $cust_svc;
56   unless ( $svcnum ) {
57     $cust_svc = new FS::cust_svc ( {
58       'svcnum'  => $svcnum,
59       'pkgnum'  => $self->pkgnum,
60       'svcpart' => $self->svcpart,
61     } );
62     $error = $cust_svc->insert;
63     return $error if $error;
64     $svcnum = $self->svcnum($cust_svc->svcnum);
65   }
66
67   $error = $self->SUPER::insert;
68   if ( $error ) {
69     $cust_svc->delete if $cust_svc;
70     return $error;
71   }
72
73   '';
74 }
75
76 =item delete
77
78 Deletes this account from the database.  If there is an error, returns the
79 error, otherwise returns false.
80
81 The corresponding FS::cust_svc record will be deleted as well.
82
83 =cut
84
85 sub delete {
86   my $self = shift;
87   my $error;
88
89   local $SIG{HUP} = 'IGNORE';
90   local $SIG{INT} = 'IGNORE';
91   local $SIG{QUIT} = 'IGNORE';
92   local $SIG{TERM} = 'IGNORE';
93   local $SIG{TSTP} = 'IGNORE';
94   local $SIG{PIPE} = 'IGNORE';
95
96   my $svcnum = $self->svcnum;
97
98   $error = $self->SUPER::delete;
99   return $error if $error;
100
101   my $cust_svc = qsearchs( 'cust_svc' , { 'svcnum' => $svcnum } );  
102   $error = $cust_svc->delete;
103   return $error if $error;
104
105   '';
106 }
107
108 =item setfixed;
109
110 Sets any fixed fields for this service (see L<FS::part_svc>).  If there is an
111 error, returns the error, otherwise returns the FS::part_svc object (use ref()
112 to test the return).  Usually called by the check method.
113
114 =cut
115
116 sub setfixed {
117   my $self = shift;
118
119   my $error;
120
121   $error =
122     $self->ut_numbern('svcnum')
123   ;
124   return $error if $error;
125
126   #get part_svc
127   my $svcpart;
128   if ( $self->svcnum ) {
129     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
130     return "Unknown svcnum" unless $cust_svc; 
131     $svcpart = $cust_svc->svcpart;
132   } else {
133     $svcpart = $self->getfield('svcpart');
134   }
135   my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $svcpart } );
136   return "Unkonwn svcpart" unless $part_svc;
137
138   #set fixed fields from part_svc
139   foreach my $field ( fields('svc_acct') ) {
140     if ( $part_svc->getfield('svc_acct__'. $field. '_flag') eq 'F' ) {
141       $self->setfield( $field, $part_svc->getfield('svc_acct__'. $field) );
142     }
143   }
144
145  $part_svc;
146
147 }
148
149 =item suspend
150
151 =item unsuspend
152
153 =item cancel
154
155 Stubs - return false (no error) so derived classes don't need to define these
156 methods.  Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
157
158 =cut
159
160 sub suspend { ''; }
161 sub unsuspend { ''; }
162 sub cancel { ''; }
163
164 =back
165
166 =head1 VERSION
167
168 $Id: svc_Common.pm,v 1.2 1999-01-25 12:26:14 ivan Exp $
169
170 =head1 BUGS
171
172 The setfixed method return value.
173
174 The new method should set defaults from part_svc (like the check method
175 sets fixed values).
176
177 =head1 SEE ALSO
178
179 L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, schema.html
180 from the base documentation.
181
182 =head1 HISTORY
183
184 $Log: svc_Common.pm,v $
185 Revision 1.2  1999-01-25 12:26:14  ivan
186 yet more mod_perl stuff
187
188 Revision 1.1  1998/12/30 00:30:45  ivan
189 svc_ stuff is more properly OO - has a common superclass FS::svc_Common
190
191
192 =cut
193
194 1;
195