mostly properly OO, some work still to be done with svc_ stuff
[freeside.git] / site_perl / table_template-svc.pm
1 package FS::svc_table;
2
3 use strict;
4 use vars qw(@ISA);
5 use FS::Record qw(fields qsearch qsearchs);
6 use FS::cust_svc;
7
8 @ISA = qw(FS::Record);
9
10 =head1 NAME
11
12 FS::table_name - Object methods for table_name records
13
14 =head1 SYNOPSIS
15
16   use FS::table_name;
17
18   $record = new FS::table_name \%hash;
19   $record = new FS::table_name { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29   $error = $record->suspend;
30
31   $error = $record->unsuspend;
32
33   $error = $record->cancel;
34
35 =head1 DESCRIPTION
36
37 An FS::table_name object represents an example.  FS::table_name inherits from
38 FS::Record.  The following fields are currently supported:
39
40 =over 4
41
42 =item field - description
43
44 =back
45
46 =head1 METHODS
47
48 =over 4
49
50 =item new HASHREF
51
52 Creates a new example.  To add the example to the database, see L<"insert">.
53
54 Note that this stores the hash reference, not a distinct copy of the hash it
55 points to.  You can ask the object for a copy with the I<hash> method.
56
57 =cut
58
59 sub table { 'table_name'; }
60
61 =item insert
62
63 Adds this record to the database.  If there is an error, returns the error,
64 otherwise returns false.
65
66 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be 
67 defined.  An FS::cust_svc record will be created and inserted.
68
69 =cut
70
71 sub insert {
72   my($self)=@_;
73   my($error);
74
75   local $SIG{HUP} = 'IGNORE';
76   local $SIG{INT} = 'IGNORE';
77   local $SIG{QUIT} = 'IGNORE';
78   local $SIG{TERM} = 'IGNORE';
79   local $SIG{TSTP} = 'IGNORE';
80
81   $error=$self->check;
82   return $error if $error;
83
84   my($svcnum)=$self->svcnum;
85   my($cust_svc);
86   unless ( $svcnum ) {
87     $cust_svc=create FS::cust_svc ( {
88       'svcnum'  => $svcnum,
89       'pkgnum'  => $self->pkgnum,
90       'svcpart' => $self->svcpart,
91     } );
92     my($error) = $cust_svc->insert;
93     return $error if $error;
94     $svcnum = $self->svcnum($cust_svc->svcnum);
95   }
96
97   $error = $self->add;
98   if ($error) {
99     #$cust_svc->del if $cust_svc;
100     $cust_svc->delete if $cust_svc;
101     return $error;
102
103   ''; #no error
104 }
105
106 =item delete
107
108 Delete this record from the database.
109
110 =cut
111
112 sub delete {
113   my($self)=@_;
114   my($error);
115
116   $error = $self->del;
117   return $error if $error;
118
119 }
120
121
122 =item replace OLD_RECORD
123
124 Replaces the OLD_RECORD with this one in the database.  If there is an error,
125 returns the error, otherwise returns false.
126
127 =cut
128
129 sub replace {
130   my($new,$old)=@_;
131   my($error);
132
133   return "(Old) Not a svc_table record!" unless $old->table eq "svc_table";
134   return "Can't change svcnum!"
135     unless $old->getfield('svcnum') eq $new->getfield('svcnum');
136
137   $error=$new->check;
138   return $error if $error;
139
140   $error = $new->rep($old);
141   return $error if $error;
142
143   ''; #no error
144 }
145
146 =item suspend
147
148 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
149
150 =cut
151
152 sub suspend {
153   ''; #no error (stub)
154 }
155
156 =item unsuspend
157
158 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
159
160 =cut
161
162 sub unsuspend {
163   ''; #no error (stub)
164 }
165
166
167 =item cancel
168
169 Just returns false (no error) for now.
170
171 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
172
173 =cut
174
175 sub cancel {
176   ''; #no error (stub)
177 }
178
179 =item check
180
181 Checks all fields to make sure this is a valid example.  If there is
182 an error, returns the error, otherwise returns false.  Called by the insert
183 and repalce methods.
184
185 =cut
186
187 sub check {
188   my($self)=@_;
189   return "Not a svc_table record!" unless $self->table eq "svc_table";
190   my($recref) = $self->hashref;
191
192   $recref->{svcnum} =~ /^(\d+)$/ or return "Illegal svcnum";
193   $recref->{svcnum} = $1;
194
195   #get part_svc
196   my($svcpart);
197   my($svcnum)=$self->getfield('svcnum');
198   if ($svcnum) {
199     my($cust_svc)=qsearchs('cust_svc',{'svcnum'=>$svcnum});
200     return "Unknown svcnum" unless $cust_svc; 
201     $svcpart=$cust_svc->svcpart;
202   } else {
203     $svcpart=$self->getfield('svcpart');
204   }
205   my($part_svc)=qsearchs('part_svc',{'svcpart'=>$svcpart});
206   return "Unkonwn svcpart" unless $part_svc;
207
208   #set fixed fields from part_svc
209   my($field);
210   foreach $field ( fields('svc_acct') ) {
211     if ( $part_svc->getfield('svc_acct__'. $field. '_flag') eq 'F' ) {
212       $self->setfield($field,$part_svc->getfield('svc_acct__'. $field) );
213     }
214   }
215
216   ''; #no error
217 }
218
219 =back
220
221 =head1 VERSION
222
223 $Id: table_template-svc.pm,v 1.3 1998-12-29 11:59:56 ivan Exp $
224
225 =head1 BUGS
226
227 The author forgot to customize this manpage.
228
229 =head1 SEE ALSO
230
231 L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, schema.html
232 froom the base documentation.
233
234 =head1 HISTORY
235
236 ivan@voicenet.com 97-jul-21
237
238 $Log: table_template-svc.pm,v $
239 Revision 1.3  1998-12-29 11:59:56  ivan
240 mostly properly OO, some work still to be done with svc_ stuff
241
242 Revision 1.2  1998/11/15 04:33:01  ivan
243 updates for newest versoin
244
245
246 =cut
247
248 1;
249