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