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