This commit was manufactured by cvs2svn to create branch 'freeside_import'.
[freeside.git] / site_perl / cust_svc.pm
1 package FS::cust_svc;
2
3 use strict;
4 use vars qw(@ISA);
5 use Exporter;
6 use FS::Record qw(fields qsearchs);
7
8 @ISA = qw(FS::Record Exporter);
9
10 =head1 NAME
11
12 FS::cust_svc - Object method for cust_svc objects
13
14 =head1 SYNOPSIS
15
16   use FS::cust_svc;
17
18   $record = create FS::cust_svc \%hash
19   $record = create FS::cust_svc { '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 =head1 DESCRIPTION
30
31 An FS::cust_svc represents a service.  FS::cust_svc inherits from FS::Record.
32 The following fields are currently supported:
33
34 =over 4
35
36 =item svcnum - primary key (assigned automatically for new services)
37
38 =item pkgnum - Package (see L<FS::cust_pkg>)
39
40 =item svcpart - Service definition (see L<FS::part_svc>)
41
42 =back
43
44 =head1 METHODS
45
46 =over 4
47
48 =item create HASHREF
49
50 Creates a new service.  To add the refund to the database, see L<"insert">.
51 Services are normally created by creating FS::svc_ objects (see
52 L<FS::svc_acct>, L<FS::svc_domain>, and L<FS::svc_acct_sm>, among others).
53
54 =cut
55
56 sub create {
57   my($proto,$hashref)=@_; 
58
59   #now in FS::Record::new
60   #my($field);
61   #foreach $field (fields('cust_svc')) {
62   #  $hashref->{$field}='' unless defined $hashref->{$field};
63   #}
64
65   $proto->new('cust_svc',$hashref);
66 }
67
68 =item insert
69
70 Adds this service to the database.  If there is an error, returns the error,
71 otherwise returns false.
72
73 =cut
74
75 sub insert {
76   my($self)=@_;
77
78   $self->check or
79   $self->add;
80 }
81
82 =item delete
83
84 Deletes this service from the database.  If there is an error, returns the
85 error, otherwise returns false.
86
87 Called by the cancel method of the package (see L<FS::cust_pkg>).
88
89 =cut
90
91 sub delete {
92   my($self)=@_;
93   # anything else here?
94   $self->del;
95 }
96
97 =item replace OLD_RECORD
98
99 Replaces the OLD_RECORD with this one in the database.  If there is an error,
100 returns the error, otherwise returns false.
101
102 =cut
103
104 sub replace {
105   my($new,$old)=@_;
106   return "(Old) Not a cust_svc record!" unless $old->table eq "cust_svc";
107   return "Can't change svcnum!"
108     unless $old->getfield('svcnum') eq $new->getfield('svcnum');
109   $new->check or
110   $new->rep($old);
111 }
112
113 =item check
114
115 Checks all fields to make sure this is a valid service.  If there is an error,
116 returns the error, otehrwise returns false.  Called by the insert and
117 replace methods.
118
119 =cut
120
121 sub check {
122   my($self)=@_;
123   return "Not a cust_svc record!" unless $self->table eq "cust_svc";
124   my($recref) = $self->hashref;
125
126   $recref->{svcnum} =~ /^(\d*)$/ or return "Illegal svcnum";
127   $recref->{svcnum}=$1;
128
129   $recref->{pkgnum} =~ /^(\d*)$/ or return "Illegal pkgnum";
130   $recref->{pkgnum}=$1;
131   return "Unknown pkgnum" unless
132     ! $recref->{pkgnum} ||
133     qsearchs('cust_pkg',{'pkgnum'=>$recref->{pkgnum}});
134
135   $recref->{svcpart} =~ /^(\d+)$/ or return "Illegal svcpart";
136   $recref->{svcpart}=$1;
137   return "Unknown svcpart" unless
138     qsearchs('part_svc',{'svcpart'=>$recref->{svcpart}});
139
140   ''; #no error
141 }
142
143 =back
144
145 =head1 BUGS
146
147 Behaviour of changing the svcpart of cust_svc records is undefined and should
148 possibly be prohibited, and pkg_svc records are not checked.
149
150 pkg_svc records are not checket in general (here).
151
152 =head1 SEE ALSO
153
154 L<FS::Record>, L<FS::cust_pkg>, L<FS::part_svc>, L<FS::pkg_svc>, 
155 schema.html from the base documentation
156
157 =head1 HISTORY
158
159 ivan@voicenet.com 97-jul-10,14
160
161 no TableUtil, no FS::Lock ivan@sisd.com 98-mar-7
162
163 pod ivan@sisd.com 98-sep-21
164
165 =cut
166
167 1;
168