RT#18361 Delay package from billing until services are provisioned [v3 backport]
[freeside.git] / FS / FS / pkg_svc.pm
1 package FS::pkg_svc;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearchs );
6 use FS::part_pkg;
7 use FS::part_svc;
8
9 @ISA = qw( FS::Record );
10
11 =head1 NAME
12
13 FS::pkg_svc - Object methods for pkg_svc records
14
15 =head1 SYNOPSIS
16
17   use FS::pkg_svc;
18
19   $record = new FS::pkg_svc \%hash;
20   $record = new FS::pkg_svc { '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   $part_pkg = $record->part_pkg;
31
32   $part_svc = $record->part_svc;
33
34 =head1 DESCRIPTION
35
36 An FS::pkg_svc record links a billing item definition (see L<FS::part_pkg>) to
37 a service definition (see L<FS::part_svc>).  FS::pkg_svc inherits from
38 FS::Record.  The following fields are currently supported:
39
40 =over 4
41
42 =item pkgsvcnum - primary key
43
44 =item pkgpart - Billing item definition (see L<FS::part_pkg>)
45
46 =item svcpart - Service definition (see L<FS::part_svc>)
47
48 =item quantity - Quantity of this service definition that this billing item
49 definition includes
50
51 =item primary_svc - primary flag, empty or 'Y'
52
53 =item hidden - 'Y' to hide this service on invoices, null otherwise.
54
55 =item provision_hold - 'Y' to release package hold when all services marked with this are provisioned
56
57 =back
58
59 =head1 METHODS
60
61 =over 4
62
63 =item new HASHREF
64
65 Create a new record.  To add the record to the database, see L<"insert">.
66
67 =cut
68
69 sub table { 'pkg_svc'; }
70
71 =item insert
72
73 Adds this record to the database.  If there is an error, returns the error,
74 otherwise returns false.
75
76 =item delete
77
78 Deletes this record from the database.  If there is an error, returns the
79 error, otherwise returns false.
80
81 =item replace OLD_RECORD
82
83 Replaces OLD_RECORD with this one in the database.  If there is an error,
84 returns the error, otherwise returns false.
85
86 =cut
87
88 sub replace {
89   my( $new, $old ) = ( shift, shift );
90
91   $old = $new->replace_old unless defined($old);
92
93   return "Can't change pkgpart!" if $old->pkgpart != $new->pkgpart;
94   return "Can't change svcpart!" if $old->svcpart != $new->svcpart;
95
96   $new->SUPER::replace($old);
97 }
98
99 =item check
100
101 Checks all fields to make sure this is a valid record.  If there is an error,
102 returns the error, otherwise returns false.  Called by the insert and replace
103 methods.
104
105 =cut
106
107 sub check {
108   my $self = shift;
109
110   my $error;
111   $error =
112        $self->ut_numbern('pkgsvcnum')
113     || $self->ut_number('pkgpart')
114     || $self->ut_number('svcpart')
115     || $self->ut_number('quantity')
116     || $self->ut_enum('hidden', [ '', 'Y' ] )
117     || $self->ut_flag('provision_hold')
118   ;
119   return $error if $error;
120
121   return "Unknown pkgpart!" unless $self->part_pkg;
122   return "Unknown svcpart!" unless $self->part_svc;
123
124   if ( $self->dbdef_table->column('primary_svc') ) {
125     $error = $self->ut_enum('primary_svc', [ '', 'Y' ] );
126     return $error if $error;
127   }
128
129   $self->SUPER::check;
130 }
131
132 =item part_pkg
133
134 Returns the FS::part_pkg object (see L<FS::part_pkg>).
135
136 =cut
137
138 sub part_pkg {
139   my $self = shift;
140   qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
141 }
142
143 =item part_svc
144
145 Returns the FS::part_svc object (see L<FS::part_svc>).
146
147 =cut
148
149 sub part_svc {
150   my $self = shift;
151   qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
152 }
153
154 =back
155
156 =head1 BUGS
157
158 =head1 SEE ALSO
159
160 L<FS::Record>, L<FS::part_pkg>, L<FS::part_svc>, schema.html from the base
161 documentation.
162
163 =cut
164
165 1;
166