FS::Record::qsearch - more portable, doesn't depend on $sth->execute returning
[freeside.git] / FS / FS / part_svc.pm
1 package FS::part_svc;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( fields );
6
7 @ISA = qw(FS::Record);
8
9 =head1 NAME
10
11 FS::part_svc - Object methods for part_svc objects
12
13 =head1 SYNOPSIS
14
15   use FS::part_svc;
16
17   $record = new FS::part_referral \%hash
18   $record = new FS::part_referral { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::part_svc represents a service definition.  FS::part_svc inherits from
31 FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item svcpart - primary key (assigned automatically for new service definitions)
36
37 =item svc - text name of this service definition
38
39 =item svcdb - table used for this service.  See L<FS::svc_acct>,
40 L<FS::svc_domain>, and L<FS::svc_acct_sm>, among others.
41
42 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
43
44 =item I<svcdb>__I<field>_flag - defines I<svcdb>__I<field> action: null, `D' for default, or `F' for fixed
45
46 =back
47
48 =head1 METHODS
49
50 =over 4
51
52 =item new HASHREF
53
54 Creates a new service definition.  To add the service definition to the
55 database, see L<"insert">.
56
57 =cut
58
59 sub table { 'part_svc'; }
60
61 =item insert
62
63 Adds this service definition to the database.  If there is an error, returns
64 the error, otherwise returns false.
65
66 =item delete
67
68 Currently unimplemented.
69
70 =cut
71
72 sub delete {
73   return "Can't (yet?) delete service definitions.";
74 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
75 }
76
77 =item replace OLD_RECORD
78
79 Replaces OLD_RECORD with this one in the database.  If there is an error,
80 returns the error, otherwise returns false.
81
82 =cut
83
84 sub replace {
85   my ( $new, $old ) = ( shift, shift );
86
87   return "Can't change svcdb!"
88     unless $old->svcdb eq $new->svcdb;
89
90   $new->SUPER::replace( $old );
91 }
92
93 =item check
94
95 Checks all fields to make sure this is a valid service definition.  If there is
96 an error, returns the error, otherwise returns false.  Called by the insert
97 and replace methods.
98
99 =cut
100
101 sub check {
102   my $self = shift;
103   my $recref = $self->hashref;
104
105   my $error;
106   $error=
107     $self->ut_numbern('svcpart')
108     || $self->ut_text('svc')
109     || $self->ut_alpha('svcdb')
110   ;
111   return $error if $error;
112
113   my @fields = eval { fields( $recref->{svcdb} ) }; #might die
114   return "Unknown svcdb!" unless @fields;
115
116   my $svcdb;
117   foreach $svcdb ( qw(
118     svc_acct svc_acct_sm svc_domain
119   ) ) {
120     my @rows = map { /^${svcdb}__(.*)$/; $1 }
121       grep ! /_flag$/,
122         grep /^${svcdb}__/,
123           fields('part_svc');
124     foreach my $row (@rows) {
125       unless ( $svcdb eq $recref->{svcdb} ) {
126         $recref->{$svcdb.'__'.$row}='';
127         $recref->{$svcdb.'__'.$row.'_flag'}='';
128         next;
129       }
130       $recref->{$svcdb.'__'.$row.'_flag'} =~ /^([DF]?)$/
131         or return "Illegal flag for $svcdb $row";
132       $recref->{$svcdb.'__'.$row.'_flag'} = $1;
133
134       my $error = $self->ut_anything($svcdb.'__'.$row);
135       return $error if $error;
136
137     }
138   }
139
140   ''; #no error
141 }
142
143 =back
144
145 =head1 VERSION
146
147 $Id: part_svc.pm,v 1.1 1999-08-04 09:03:53 ivan Exp $
148
149 =head1 BUGS
150
151 Delete is unimplemented.
152
153 The list of svc_* tables is hardcoded.  When svc_acct_pop is renamed, this
154 should be fixed.
155
156 =head1 SEE ALSO
157
158 L<FS::Record>, L<FS::part_pkg>, L<FS::pkg_svc>, L<FS::cust_svc>,
159 L<FS::svc_acct>, L<FS::svc_acct_sm>, L<FS::svc_domain>, schema.html from the
160 base documentation.
161
162 =cut
163
164 1;
165