herding elephants: add primary keys to *all* tables for slony
[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 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Create a new record.  To add the record to the database, see L<"insert">.
62
63 =cut
64
65 sub table { 'pkg_svc'; }
66
67 =item insert
68
69 Adds this record to the database.  If there is an error, returns the error,
70 otherwise returns false.
71
72 =item delete
73
74 Deletes this record from the database.  If there is an error, returns the
75 error, otherwise returns false.
76
77 =item replace OLD_RECORD
78
79 Replaces OLD_RECORD with this one in the database.  If there is an error,
80 returns the error, otherwise returns false.
81
82 =cut
83
84 sub replace {
85   my ( $new, $old ) = ( shift, shift );
86
87   return "Can't change pkgpart!" if $old->pkgpart != $new->pkgpart;
88   return "Can't change svcpart!" if $old->svcpart != $new->svcpart;
89
90   $new->SUPER::replace($old);
91 }
92
93 =item check
94
95 Checks all fields to make sure this is a valid record.  If there is an error,
96 returns the error, otherwise returns false.  Called by the insert and replace
97 methods.
98
99 =cut
100
101 sub check {
102   my $self = shift;
103
104   my $error;
105   $error =
106        $self->ut_numbern('pkgsvcnum')
107     || $self->ut_number('pkgpart')
108     || $self->ut_number('svcpart')
109     || $self->ut_number('quantity')
110   ;
111   return $error if $error;
112
113   return "Unknown pkgpart!" unless $self->part_pkg;
114   return "Unknown svcpart!" unless $self->part_svc;
115
116   if ( $self->dbdef_table->column('primary_svc') ) {
117     $error = $self->ut_enum('primary_svc', [ '', 'Y' ] );
118     return $error if $error;
119   }
120
121   $self->SUPER::check;
122 }
123
124 =item part_pkg
125
126 Returns the FS::part_pkg object (see L<FS::part_pkg>).
127
128 =cut
129
130 sub part_pkg {
131   my $self = shift;
132   qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
133 }
134
135 =item part_svc
136
137 Returns the FS::part_svc object (see L<FS::part_svc>).
138
139 =cut
140
141 sub part_svc {
142   my $self = shift;
143   qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
144 }
145
146 =back
147
148 =head1 BUGS
149
150 =head1 SEE ALSO
151
152 L<FS::Record>, L<FS::part_pkg>, L<FS::part_svc>, schema.html from the base
153 documentation.
154
155 =cut
156
157 1;
158