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