service dependencies: part_pkg_restrict / part_pkg_restrict_soft, RT#33685
[freeside.git] / FS / FS / part_svc_link.pm
1 package FS::part_svc_link;
2 use base qw( FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearchs ); # qw( qsearch qsearchs );
6
7 =head1 NAME
8
9 FS::part_svc_link - Object methods for part_svc_link records
10
11 =head1 SYNOPSIS
12
13   use FS::part_svc_link;
14
15   $record = new FS::part_svc_link \%hash;
16   $record = new FS::part_svc_link { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::part_svc_link object represents an service definition dependency.
29 FS::part_svc_link inherits from FS::Record.  The following fields are currently
30 supported:
31
32 =over 4
33
34 =item svclinknum
35
36 primary key
37
38 =cut
39
40 #=item linkname
41 #
42 #Dependency name
43
44 =item agentnum
45
46 Empty for global dependencies, or agentnum (see L<FS::agent>) for
47 agent-specific dependencies
48
49 =item src_svcpart
50
51 Source service definition (see L<FS::part_svc>)
52
53 =item dst_svcpart
54
55 Destination service definition (see L<FS::part_svc>)
56
57 =item link_type
58
59 Link type:
60
61 =over 4
62
63 =cut
64
65 # XXX false laziness w/edit/part_svc_link.html
66
67 =item part_pkg_restrict
68
69 In package defintions, require the destination service definition when the
70 source service definition is included
71
72 =item part_pkg_restrict_soft
73
74 Soft order block: in package definitions,  suggest the destination service definition when the source service definition is included
75
76 =item cust_svc_provision_restrict
77
78 Require the destination service to be provisioned before the source service
79
80 =item cust_svc_unprovision_restrict
81
82 Require the destination service to be unprovisioned before the source service
83
84 =item cust_svc_unprovision_cascade
85
86 Automatically unprovision the destination service when the source service is
87 unprovisioned
88
89 =item cust_svc_suspend_cascade
90
91 Suspend the destination service before the source service
92
93 =back
94
95 =back
96
97 =head1 METHODS
98
99 =over 4
100
101 =item new HASHREF
102
103 Creates a new record.  To add the record to the database, see L<"insert">.
104
105 Note that this stores the hash reference, not a distinct copy of the hash it
106 points to.  You can ask the object for a copy with the I<hash> method.
107
108 =cut
109
110 # the new method can be inherited from FS::Record, if a table method is defined
111
112 sub table { 'part_svc_link'; }
113
114 =item insert
115
116 Adds this record to the database.  If there is an error, returns the error,
117 otherwise returns false.
118
119 =item delete
120
121 Delete this record from the database.
122
123 =item replace OLD_RECORD
124
125 Replaces the OLD_RECORD with this one in the database.  If there is an error,
126 returns the error, otherwise returns false.
127
128 =item check
129
130 Checks all fields to make sure this is a valid record.  If there is
131 an error, returns the error, otherwise returns false.  Called by the insert
132 and replace methods.
133
134 =cut
135
136 sub check {
137   my $self = shift;
138
139   my $error = 
140     $self->ut_numbern('svclinknum')
141     #|| $self->ut_textn('linkname')
142     || $self->ut_number('src_svcpart')
143     || $self->ut_number('dst_svcpart')
144     || $self->ut_text('link_type')
145     || $self->ut_enum('disabled', [ '', 'Y' ])
146   ;
147   return $error if $error;
148
149   $self->SUPER::check;
150 }
151
152 =item description
153
154 Returns an extended description of this dependency, including.  Exact wording
155 depends on I<link_type>.
156
157 =cut
158
159 sub description {
160   my $self = shift;
161
162   my $src = $self->src_part_svc->svc;
163   my $dst = $self->dst_part_svc->svc;
164
165   #maybe sub-classes with overrides at some point
166   #  (and hooks each place we have manual checks for the various rules)
167   # but this will do for now
168
169   $self->link_type eq 'part_pkg_restrict'
170    and return "In package definitions, $dst is required when $src is included";
171
172   $self->link_type eq 'part_pkg_restrict_soft'
173    and return "In package definitions, $dst is suggested when $src is included";
174
175   $self->link_type eq 'cust_svc_provision_restrict'
176    and return "Require $dst provisioning before $src";
177
178   $self->link_type eq 'cust_svc_unprovision_restrict'
179    and return "Require $dst unprovisioning before $src";
180
181   $self->link_type eq 'cust_svc_unprovision_cascade'
182    and return "Automatically unprovision $dst when $src is unprovisioned";
183
184   $self->link_type eq 'cust_svc_suspend_cascade'
185    and return "Suspend $dst before $src";
186
187   warn "WARNING: unknown part_svc_link.link_type ". $self->link_type. "\n";
188   return "$src (unknown link_type ". $self->link_type. ") $dst";
189
190 }
191
192 =item src_part_svc 
193
194 Returns the source service definition, as an FS::part_svc object (see
195 L<FS::part_svc>).
196
197 =cut
198
199 sub src_part_svc {
200   my $self = shift;
201   qsearchs('part_svc', { svcpart=>$self->src_svcpart } );
202 }
203
204 =item src_svc
205
206 Returns the source service definition name (part_svc.svc).
207
208 =cut
209
210 sub src_svc {
211   shift->src_part_svc->svc;
212 }
213
214 =item dst_part_svc
215
216 Returns the destination service definition, as an FS::part_svc object (see
217 L<FS::part_svc>).
218
219 =cut
220
221 sub dst_part_svc {
222   my $self = shift;
223   qsearchs('part_svc', { svcpart=>$self->dst_svcpart } );
224 }
225
226 =item dst_svc
227
228 Returns the destination service definition name (part_svc.svc).
229
230 =cut
231
232 sub dst_svc {
233   shift->dst_part_svc->svc;
234 }
235
236 =back
237
238 =head1 BUGS
239
240 =head1 SEE ALSO
241
242 L<FS::part_svc>, L<FS::Record>
243
244 =cut
245
246 1;
247