1 package FS::svc_Common;
4 use vars qw( @ISA $noexport_hack );
5 use FS::Record qw( qsearch qsearchs fields dbh );
10 @ISA = qw( FS::Record );
14 FS::svc_Common - Object method for all svc_ records
20 @ISA = qw( FS::svc_Common );
24 FS::svc_Common is intended as a base class for table-specific classes to
25 inherit from, i.e. FS::svc_acct. FS::svc_Common inherits from FS::Record.
35 # This restricts the fields based on part_svc_column and the svcpart of
36 # the service. There are four possible cases:
37 # 1. svcpart passed as part of the svc_x hash.
38 # 2. svcpart fetched via cust_svc based on svcnum.
39 # 3. No svcnum or svcpart. In this case, return ALL the fields with
40 # dbtable eq $self->table.
41 # 4. Called via "fields('svc_acct')" or something similar. In this case
42 # there is no $self object.
46 my @vfields = $self->SUPER::virtual_fields;
48 return @vfields unless (ref $self); # Case 4
50 if ($self->svcpart) { # Case 1
51 $svcpart = $self->svcpart;
52 } elsif (my $cust_svc = $self->cust_svc) { # Case 2
53 $svcpart = $cust_svc->svcpart;
58 if ($svcpart) { #Cases 1 and 2
59 my %flags = map { $_->columnname, $_->columnflag } (
60 qsearch ('part_svc_column', { svcpart => $svcpart } )
62 return grep { not ($flags{$_} eq 'X') } @vfields;
71 Checks the validity of fields in this record.
73 At present, this does nothing but call FS::Record::check (which, in turn,
74 does nothing but run virtual field checks).
83 =item insert [ JOBNUM_ARRAYREF [ OBJECTS_ARRAYREF ] ]
85 Adds this record to the database. If there is an error, returns the error,
86 otherwise returns false.
88 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
89 defined. An FS::cust_svc record will be created and inserted.
91 If an arrayref is passed as parameter, the B<jobnum>s of any export jobs will
92 be added to the array.
94 If an arrayref of FS::tablename objects (for example, FS::acct_snarf objects)
95 is passed as the optional second parameter, they will have their svcnum fields
96 set and will be inserted after this record, but before any exports are run.
102 local $FS::queue::jobnums = shift if @_;
103 my $objects = scalar(@_) ? shift : [];
106 local $SIG{HUP} = 'IGNORE';
107 local $SIG{INT} = 'IGNORE';
108 local $SIG{QUIT} = 'IGNORE';
109 local $SIG{TERM} = 'IGNORE';
110 local $SIG{TSTP} = 'IGNORE';
111 local $SIG{PIPE} = 'IGNORE';
113 my $oldAutoCommit = $FS::UID::AutoCommit;
114 local $FS::UID::AutoCommit = 0;
117 $error = $self->check;
118 return $error if $error;
120 my $svcnum = $self->svcnum;
123 $cust_svc = new FS::cust_svc ( {
124 #hua?# 'svcnum' => $svcnum,
125 'pkgnum' => $self->pkgnum,
126 'svcpart' => $self->svcpart,
128 $error = $cust_svc->insert;
130 $dbh->rollback if $oldAutoCommit;
133 $svcnum = $self->svcnum($cust_svc->svcnum);
135 $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
136 unless ( $cust_svc ) {
137 $dbh->rollback if $oldAutoCommit;
138 return "no cust_svc record found for svcnum ". $self->svcnum;
140 $self->pkgnum($cust_svc->pkgnum);
141 $self->svcpart($cust_svc->svcpart);
144 $error = $self->SUPER::insert;
146 $dbh->rollback if $oldAutoCommit;
150 foreach my $object ( @$objects ) {
151 $object->svcnum($self->svcnum);
152 $error = $object->insert;
154 $dbh->rollback if $oldAutoCommit;
160 unless ( $noexport_hack ) {
161 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
162 my $error = $part_export->export_insert($self);
164 $dbh->rollback if $oldAutoCommit;
165 return "exporting to ". $part_export->exporttype.
166 " (transaction rolled back): $error";
171 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
178 Deletes this account from the database. If there is an error, returns the
179 error, otherwise returns false.
181 The corresponding FS::cust_svc record will be deleted as well.
189 local $SIG{HUP} = 'IGNORE';
190 local $SIG{INT} = 'IGNORE';
191 local $SIG{QUIT} = 'IGNORE';
192 local $SIG{TERM} = 'IGNORE';
193 local $SIG{TSTP} = 'IGNORE';
194 local $SIG{PIPE} = 'IGNORE';
196 my $svcnum = $self->svcnum;
198 my $oldAutoCommit = $FS::UID::AutoCommit;
199 local $FS::UID::AutoCommit = 0;
202 $error = $self->SUPER::delete;
203 return $error if $error;
206 unless ( $noexport_hack ) {
207 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
208 my $error = $part_export->export_delete($self);
210 $dbh->rollback if $oldAutoCommit;
211 return "exporting to ". $part_export->exporttype.
212 " (transaction rolled back): $error";
217 return $error if $error;
219 my $cust_svc = $self->cust_svc;
220 $error = $cust_svc->delete;
221 return $error if $error;
223 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
228 =item replace OLD_RECORD
230 Replaces OLD_RECORD with this one. If there is an error, returns the error,
231 otherwise returns false.
236 my ($new, $old) = (shift, shift);
238 local $SIG{HUP} = 'IGNORE';
239 local $SIG{INT} = 'IGNORE';
240 local $SIG{QUIT} = 'IGNORE';
241 local $SIG{TERM} = 'IGNORE';
242 local $SIG{TSTP} = 'IGNORE';
243 local $SIG{PIPE} = 'IGNORE';
245 my $oldAutoCommit = $FS::UID::AutoCommit;
246 local $FS::UID::AutoCommit = 0;
249 my $error = $new->SUPER::replace($old);
251 $dbh->rollback if $oldAutoCommit;
256 unless ( $noexport_hack ) {
257 foreach my $part_export ( $new->cust_svc->part_svc->part_export ) {
258 my $error = $part_export->export_replace($new,$old);
260 $dbh->rollback if $oldAutoCommit;
261 return "error exporting to ". $part_export->exporttype.
262 " (transaction rolled back): $error";
267 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
274 Sets any fixed fields for this service (see L<FS::part_svc>). If there is an
275 error, returns the error, otherwise returns the FS::part_svc object (use ref()
276 to test the return). Usually called by the check method.
287 Sets all fields to their defaults (see L<FS::part_svc>), overriding their
288 current values. If there is an error, returns the error, otherwise returns
289 the FS::part_svc object (use ref() to test the return).
305 $self->ut_numbern('svcnum')
307 return $error if $error;
311 if ( $self->svcnum ) {
312 my $cust_svc = $self->cust_svc;
313 return "Unknown svcnum" unless $cust_svc;
314 $svcpart = $cust_svc->svcpart;
316 $svcpart = $self->getfield('svcpart');
318 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $svcpart } );
319 return "Unkonwn svcpart" unless $part_svc;
321 #set default/fixed/whatever fields from part_svc
322 my $table = $self->table;
323 foreach my $field ( grep { $_ ne 'svcnum' } $self->fields ) {
324 my $part_svc_column = $part_svc->part_svc_column($field);
325 if ( $part_svc_column->columnflag eq $x ) {
326 $self->setfield( $field, $part_svc_column->columnvalue );
336 Returns the cust_svc record associated with this svc_ record, as a FS::cust_svc
337 object (see L<FS::cust_svc>).
343 qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
348 Runs export_suspend callbacks.
355 local $SIG{HUP} = 'IGNORE';
356 local $SIG{INT} = 'IGNORE';
357 local $SIG{QUIT} = 'IGNORE';
358 local $SIG{TERM} = 'IGNORE';
359 local $SIG{TSTP} = 'IGNORE';
360 local $SIG{PIPE} = 'IGNORE';
362 my $oldAutoCommit = $FS::UID::AutoCommit;
363 local $FS::UID::AutoCommit = 0;
367 unless ( $noexport_hack ) {
368 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
369 my $error = $part_export->export_suspend($self);
371 $dbh->rollback if $oldAutoCommit;
372 return "error exporting to ". $part_export->exporttype.
373 " (transaction rolled back): $error";
378 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
385 Runs export_unsuspend callbacks.
392 local $SIG{HUP} = 'IGNORE';
393 local $SIG{INT} = 'IGNORE';
394 local $SIG{QUIT} = 'IGNORE';
395 local $SIG{TERM} = 'IGNORE';
396 local $SIG{TSTP} = 'IGNORE';
397 local $SIG{PIPE} = 'IGNORE';
399 my $oldAutoCommit = $FS::UID::AutoCommit;
400 local $FS::UID::AutoCommit = 0;
404 unless ( $noexport_hack ) {
405 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
406 my $error = $part_export->export_unsuspend($self);
408 $dbh->rollback if $oldAutoCommit;
409 return "error exporting to ". $part_export->exporttype.
410 " (transaction rolled back): $error";
415 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
422 Stub - returns false (no error) so derived classes don't need to define these
423 methods. Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
433 $Id: svc_Common.pm,v 1.14 2003-10-25 02:05:44 ivan Exp $
437 The setfixed method return value.
441 L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, schema.html
442 from the base documentation.