package categories (meta package classes) and grouping invoices by them
[freeside.git] / FS / FS / pkg_class.pm
1 package FS::pkg_class;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearchs qsearch );
6 use FS::part_pkg;
7 use FS::pkg_category;
8
9 @ISA = qw( FS::Record );
10
11 =head1 NAME
12
13 FS::pkg_class - Object methods for pkg_class records
14
15 =head1 SYNOPSIS
16
17   use FS::pkg_class;
18
19   $record = new FS::pkg_class \%hash;
20   $record = new FS::pkg_class { '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 =head1 DESCRIPTION
31
32 An FS::pkg_class object represents an package class.  Every package definition
33 (see L<FS::part_pkg>) has, optionally, a package class. FS::pkg_class inherits
34 from FS::Record.  The following fields are currently supported:
35
36 =over 4
37
38 =item classnum - primary key (assigned automatically for new package classes)
39
40 =item classname - Text name of this package class
41
42 =item categorynum - Number of associated pkg_category (see L<FS::pkg_category>)
43
44 =item disabled - Disabled flag, empty or 'Y'
45
46 =back
47
48 =head1 METHODS
49
50 =over 4
51
52 =item new HASHREF
53
54 Creates a new package class.  To add the package class to the database, see
55 L<"insert">.
56
57 =cut
58
59 sub table { 'pkg_class'; }
60
61 =item insert
62
63 Adds this package class to the database.  If there is an error, returns the
64 error, otherwise returns false.
65
66 =item delete
67
68 Deletes this package class from the database.  Only package classes with no
69 associated package definitions can be deleted.  If there is an error, returns
70 the error, otherwise returns false.
71
72 =cut
73
74 sub delete {
75   my $self = shift;
76
77   return "Can't delete an pkg_class with part_pkg records!"
78     if qsearch( 'part_pkg', { 'classnum' => $self->classnum } );
79
80   $self->SUPER::delete;
81 }
82
83 =item replace OLD_RECORD
84
85 Replaces OLD_RECORD with this one in the database.  If there is an error,
86 returns the error, otherwise returns false.
87
88 =item check
89
90 Checks all fields to make sure this is a valid package class.  If there is an
91 error, returns the error, otherwise returns false.  Called by the insert and
92 replace methods.
93
94 =cut
95
96 sub check {
97   my $self = shift;
98
99   $self->ut_numbern('classnum')
100   or $self->ut_text('classname')
101   or $self->ut_foreign_keyn('categorynum', 'pkg_category', 'categorynum')
102   or $self->SUPER::check;
103
104 }
105
106 =item pkg_category
107
108 Returns the pkg_category record associated with this class, or false if there
109 is none.
110
111 =cut
112
113 sub pkg_category {
114   my $self = shift;
115   qsearchs('pkg_category', { 'categorynum' => $self->categorynum } );
116 }
117
118 =item categoryname
119
120 Returns the category name associated with this class, or false if there
121 is none.
122
123 =cut
124
125 sub categoryname {
126   my $pkg_category = shift->pkg_category;
127   $pkg_category->categoryname if $pkg_category;
128 }
129
130 =back
131
132 =head1 BUGS
133
134 =head1 SEE ALSO
135
136 L<FS::Record>, L<FS::part_pkg>, schema.html from the base documentation.
137
138 =cut
139
140 1;
141