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