Initial revision
[freeside.git] / site_perl / part_svc.pm
1 package FS::part_svc;
2
3 use strict;
4 use vars qw(@ISA @EXPORT_OK);
5 use Exporter;
6 use FS::Record qw(fields hfields);
7
8 @ISA = qw(FS::Record Exporter);
9 @EXPORT_OK = qw(hfields fields);
10
11 =head1 NAME
12
13 FS::part_svc - Object methods for part_svc objects
14
15 =head1 SYNOPSIS
16
17   use FS::part_svc;
18
19   $record = create FS::part_referral \%hash
20   $record = create FS::part_referral { '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 =head1 DESCRIPTION
31
32 An FS::part_svc represents a service definition.  FS::part_svc inherits from
33 FS::Record.  The following fields are currently supported:
34
35 =over 4
36
37 =item svcpart - primary key (assigned automatically for new service definitions)
38
39 =item svc - text name of this service definition
40
41 =item svcdb - table used for this service.  See L<FS::svc_acct>,
42 L<FS::svc_domain>, and L<FS::svc_acct_sm>, among others.
43
44 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
45
46 =item I<svcdb>__I<field>_flag - defines I<svcdb>__I<field> action: null, `D' for default, or `F' for fixed
47
48 =back
49
50 =head1 METHODS
51
52 =over 4
53
54 =item create HASHREF
55
56 Creates a new service definition.  To add the service definition to the
57 database, see L<"insert">.
58
59 =cut
60
61 sub create {
62   my($proto,$hashref)=@_;
63
64   #now in FS::Record::new
65   #my($field);
66   #foreach $field (fields('part_svc')) {
67   #  $hashref->{$field}='' unless defined $hashref->{$field};
68   #}
69
70   $proto->new('part_svc',$hashref);
71 }
72
73 =item insert
74
75 Adds this service definition to the database.  If there is an error, returns
76 the error, otherwise returns false.
77
78 =cut
79
80 sub insert {
81   my($self)=@_;
82
83   $self->check or
84   $self->add;
85 }
86
87 =item delete
88
89 Currently unimplemented.
90
91 =cut
92
93 sub delete {
94   return "Can't (yet?) delete service definitions.";
95 # maybe check & make sure the svcpart isn't in cust_svc or (in any packages)?
96 #  my($self)=@_;
97 #
98 #  $self->del;
99 }
100
101 =item replace OLD_RECORD
102
103 Replaces OLD_RECORD with this one in the database.  If there is an error,
104 returns the error, otherwise returns false.
105
106 =cut
107
108 sub replace {
109   my($new,$old)=@_;
110   return "(Old) Not a part_svc record!" unless $old->table eq "part_svc";
111   return "Can't change svcpart!"
112     unless $old->getfield('svcpart') eq $new->getfield('svcpart');
113   return "Can't change svcdb!"
114     unless $old->getfield('svcdb') eq $new->getfield('svcdb');
115   $new->check or
116   $new->rep($old);
117 }
118
119 =item check
120
121 Checks all fields to make sure this is a valid service definition.  If there is
122 an error, returns the error, otherwise returns false.  Called by the insert
123 and replace methods.
124
125 =cut
126
127 sub check {
128   my($self)=@_;
129   return "Not a part_svc record!" unless $self->table eq "part_svc";
130   my($recref) = $self->hashref;
131
132   my($error);
133   return $error if $error=
134     $self->ut_numbern('svcpart')
135     || $self->ut_text('svc')
136     || $self->ut_alpha('svcdb')
137   ;
138
139   my(@fields) = eval { fields($recref->{svcdb}) }; #might die
140   return "Unknown svcdb!" unless @fields;
141
142   my($svcdb);
143   foreach $svcdb ( qw(
144     svc_acct svc_acct_sm svc_charge svc_domain svc_wo
145   ) ) {
146     my(@rows)=map { /^${svcdb}__(.*)$/; $1 }
147       grep ! /_flag$/,
148         grep /^${svcdb}__/,
149           fields('part_svc');
150     my($row);
151     foreach $row (@rows) {
152       unless ( $svcdb eq $recref->{svcdb} ) {
153         $recref->{$svcdb.'__'.$row}='';
154         $recref->{$svcdb.'__'.$row.'_flag'}='';
155         next;
156       }
157       $recref->{$svcdb.'__'.$row.'_flag'} =~ /^([DF]?)$/
158         or return "Illegal flag for $svcdb $row";
159       $recref->{$svcdb.'__'.$row.'_flag'} = $1;
160
161 #      $recref->{$svcdb.'__'.$row} =~ /^(.*)$/ #not restrictive enough?
162 #        or return "Illegal value for $svcdb $row";
163 #      $recref->{$svcdb.'__'.$row} = $1;
164       my($error);
165       return $error if $error=$self->ut_anything($svcdb.'__'.$row);
166
167     }
168   }
169
170   ''; #no error
171 }
172
173 =back
174
175 =head1 BUGS
176
177 It doesn't properly override FS::Record yet.
178
179 Delete is unimplemented.
180
181 =head1 SEE ALSO
182
183 L<FS::Record>, L<FS::part_pkg>, L<FS::pkg_svc>, L<FS::cust_svc>,
184 L<FS::svc_acct>, L<FS::svc_acct_sm>, L<FS::svc_domain>, schema.html from the
185 base documentation.
186
187 =head1 HISTORY
188
189 ivan@sisd.com 97-nov-14
190
191 data checking/untainting calls into FS::Record added
192 ivan@sisd.com 97-dec-6
193
194 pod ivan@sisd.com 98-sep-21
195
196 =cut
197
198 1;
199