d64d10db2be6c7429957cf7c6d5519ee7dba25f1
[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 pkgpart - Billing item definition (see L<FS::part_pkg>)
43
44 =item svcpart - Service definition (see L<FS::part_svc>)
45
46 =item quantity - Quantity of this service definition that this billing item
47 definition includes
48
49 =back
50
51 =head1 METHODS
52
53 =over 4
54
55 =item new HASHREF
56
57 Create a new record.  To add the record to the database, see L<"insert">.
58
59 =cut
60
61 sub table { 'pkg_svc'; }
62
63 =item insert
64
65 Adds this record to the database.  If there is an error, returns the error,
66 otherwise returns false.
67
68 =item delete
69
70 Deletes this record from the database.  If this svcpart is also the default svcpart,
71 we set part_pkg.def_svcpart to NULL.  If there is an error, returns the error,
72 otherwise returns false.
73
74 sub delete {
75   my $self = shift;
76   my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $self->pkgpart } );
77
78   # Should this be wrapped in a transaction?
79   if ( $part_pkg->def_svcpart == $self->svcpart ) {
80     my $new = new FS::part_pkg $part_pkg->hash;
81     $new->def_svcpart = 0;
82     my $error = $new->replace($part_pkg);
83     return $error if $error;
84   }
85
86   $self->SUPER::delete;
87 }
88
89 =item replace OLD_RECORD
90
91 Replaces OLD_RECORD with this one in the database.  If there is an error,
92 returns the error, otherwise returns false.
93
94 =cut
95
96 sub replace {
97   my ( $new, $old ) = ( shift, shift );
98
99   return "Can't change pkgpart!" if $old->pkgpart != $new->pkgpart;
100   return "Can't change svcpart!" if $old->svcpart != $new->svcpart;
101
102   my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $new->pkgpart } );
103
104   # Should this be wrapped in a transaction?
105   if ( ($part_pkg->def_svcpart == $new->svcpart) && ($new->quantity == 0) ) {
106     my $new_part_pkg = new FS::part_pkg $part_pkg->hash;
107     $new_part_pkg->def_svcpart = 0;
108     my $error = $new_part_pkg->replace($part_pkg);
109     return $error if $error;
110   }
111
112   $new->SUPER::replace($old);
113 }
114
115 =item check
116
117 Checks all fields to make sure this is a valid record.  If there is an error,
118 returns the error, otherwise returns false.  Called by the insert and replace
119 methods.
120
121 =cut
122
123 sub check {
124   my $self = shift;
125
126   my $error;
127   $error =
128     $self->ut_number('pkgpart')
129     || $self->ut_number('svcpart')
130     || $self->ut_number('quantity')
131   ;
132   return $error if $error;
133
134   return "Unknown pkgpart!" unless $self->part_pkg;
135   return "Unknown svcpart!" unless $self->part_svc;
136
137   ''; #no error
138 }
139
140 =item part_pkg
141
142 Returns the FS::part_pkg object (see L<FS::part_pkg>).
143
144 =cut
145
146 sub part_pkg {
147   my $self = shift;
148   qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
149 }
150
151 =item part_svc
152
153 Returns the FS::part_svc object (see L<FS::part_svc>).
154
155 =cut
156
157 sub part_svc {
158   my $self = shift;
159   qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
160 }
161
162 =back
163
164 =head1 VERSION
165
166 $Id: pkg_svc.pm,v 1.2 2002-06-08 07:48:37 khoff Exp $
167
168 =head1 BUGS
169
170 =head1 SEE ALSO
171
172 L<FS::Record>, L<FS::part_pkg>, L<FS::part_svc>, schema.html from the base
173 documentation.
174
175 =cut
176
177 1;
178