add customer fields option with agent, display_custnum, status and name, RT#73721
[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( 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 after 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 sub table { 'part_svc_link'; }
111
112 =item by_agentnum AGENTNUM, KEY => VALUE, ...
113
114 Alternate search consructor.  Given an agentnum then a list of keys and values,
115 searches for part_svc_link records with the given agentnum (or no agentnum).
116
117 Additional keys and values are searched for in the part_pkg_link table
118 (typically src_svcpart and link_type).
119
120 =cut
121
122 sub by_agentnum {
123   my( $class, $agentnum, %opt ) = @_;
124
125   qsearch({ 'table'     => 'part_svc_link', #$class->table,
126             'hashref'   => \%opt,
127             'extra_sql' =>
128               $agentnum
129                 ? "AND ( agentnum IS NULL OR agentnum = $agentnum )"
130                 : 'AND agentnum IS NULL',
131          });
132
133 }
134
135 =item insert
136
137 Adds this record to the database.  If there is an error, returns the error,
138 otherwise returns false.
139
140 =item delete
141
142 Delete this record from the database.
143
144 =item replace OLD_RECORD
145
146 Replaces the OLD_RECORD with this one in the database.  If there is an error,
147 returns the error, otherwise returns false.
148
149 =item check
150
151 Checks all fields to make sure this is a valid record.  If there is
152 an error, returns the error, otherwise returns false.  Called by the insert
153 and replace methods.
154
155 =cut
156
157 sub check {
158   my $self = shift;
159
160   my $error = 
161     $self->ut_numbern('svclinknum')
162     #|| $self->ut_textn('linkname')
163     || $self->ut_number('src_svcpart')
164     || $self->ut_number('dst_svcpart')
165     || $self->ut_text('link_type')
166     || $self->ut_enum('disabled', [ '', 'Y' ])
167   ;
168   return $error if $error;
169
170   $self->SUPER::check;
171 }
172
173 =item description
174
175 Returns an extended description of this dependency, including.  Exact wording
176 depends on I<link_type>.
177
178 =cut
179
180 sub description {
181   my $self = shift;
182
183   my $src = $self->src_part_svc->svc;
184   my $dst = $self->dst_part_svc->svc;
185
186   #maybe sub-classes with overrides at some point
187   #  (and hooks each place we have manual checks for the various rules)
188   # but this will do for now
189
190   my $l = $self->link_type;
191
192   $l eq 'part_pkg_restrict'
193    and return "In package definitions, $dst is required when $src is included";
194
195   $l eq 'part_pkg_restrict_soft'
196    and return "In package definitions, $dst is suggested when $src is included";
197
198   $l eq 'cust_svc_provision_restrict'
199    and return "Require $dst provisioning before $src";
200
201   $l eq 'cust_svc_unprovision_restrict'
202    and return "Require $dst unprovisioning before $src";
203
204   $l eq 'cust_svc_unprovision_cascade'
205    and return "Automatically unprovision $dst when $src is unprovisioned";
206
207   $l eq 'cust_svc_suspend_cascade'
208    and return "Suspend $dst after $src";
209
210   warn "WARNING: unknown part_svc_link.link_type $l\n";
211   return "$src (unknown link_type $l) $dst";
212
213 }
214
215 =item src_part_svc 
216
217 Returns the source service definition, as an FS::part_svc object (see
218 L<FS::part_svc>).
219
220 =cut
221
222 sub src_part_svc {
223   my $self = shift;
224   qsearchs('part_svc', { svcpart=>$self->src_svcpart } );
225 }
226
227 =item src_svc
228
229 Returns the source service definition name (part_svc.svc).
230
231 =cut
232
233 sub src_svc {
234   shift->src_part_svc->svc;
235 }
236
237 =item dst_part_svc
238
239 Returns the destination service definition, as an FS::part_svc object (see
240 L<FS::part_svc>).
241
242 =cut
243
244 sub dst_part_svc {
245   my $self = shift;
246   qsearchs('part_svc', { svcpart=>$self->dst_svcpart } );
247 }
248
249 =item dst_svc
250
251 Returns the destination service definition name (part_svc.svc).
252
253 =cut
254
255 sub dst_svc {
256   shift->dst_part_svc->svc;
257 }
258
259 =back
260
261 =head1 BUGS
262
263 =head1 SEE ALSO
264
265 L<FS::part_svc>, L<FS::Record>
266
267 =cut
268
269 1;
270