*** empty log message ***
[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   $self->setx('F');
119 }
120
121 =item setdefault
122
123 Sets all fields to their defaults (see L<FS::part_svc>), overriding their
124 current values.  If there is an error, returns the error, otherwise returns
125 the FS::part_svc object (use ref() to test the return).
126
127 =cut
128
129 sub setdefault {
130   my $self = shift;
131   $self->setx('D');
132 }
133
134 sub setx {
135   my $self = shift;
136   my $x = shift;
137
138   my $error;
139
140   $error =
141     $self->ut_numbern('svcnum')
142   ;
143   return $error if $error;
144
145   #get part_svc
146   my $svcpart;
147   if ( $self->svcnum ) {
148     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
149     return "Unknown svcnum" unless $cust_svc; 
150     $svcpart = $cust_svc->svcpart;
151   } else {
152     $svcpart = $self->getfield('svcpart');
153   }
154   my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $svcpart } );
155   return "Unkonwn svcpart" unless $part_svc;
156
157   #set default/fixed/whatever fields from part_svc
158   foreach my $field ( fields('svc_acct') ) {
159     if ( $part_svc->getfield('svc_acct__'. $field. '_flag') eq $x ) {
160       $self->setfield( $field, $part_svc->getfield('svc_acct__'. $field) );
161     }
162   }
163
164  $part_svc;
165
166 }
167
168 =item suspend
169
170 =item unsuspend
171
172 =item cancel
173
174 Stubs - return false (no error) so derived classes don't need to define these
175 methods.  Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
176
177 =cut
178
179 sub suspend { ''; }
180 sub unsuspend { ''; }
181 sub cancel { ''; }
182
183 =back
184
185 =head1 VERSION
186
187 $Id: svc_Common.pm,v 1.3 1999-03-25 13:31:29 ivan Exp $
188
189 =head1 BUGS
190
191 The setfixed method return value.
192
193 The new method should set defaults from part_svc (like the check method
194 sets fixed values)?
195
196 =head1 SEE ALSO
197
198 L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, schema.html
199 from the base documentation.
200
201 =head1 HISTORY
202
203 $Log: svc_Common.pm,v $
204 Revision 1.3  1999-03-25 13:31:29  ivan
205 added setdefault method (generalized setfixed method to setx method)
206
207 Revision 1.2  1999/01/25 12:26:14  ivan
208 yet more mod_perl stuff
209
210 Revision 1.1  1998/12/30 00:30:45  ivan
211 svc_ stuff is more properly OO - has a common superclass FS::svc_Common
212
213
214 =cut
215
216 1;
217