delete fees, RT#81713
[freeside.git] / FS / FS / pkg_category.pm
1 package FS::pkg_category;
2 use base qw( FS::category_Common );
3
4 use strict;
5 use vars qw( @ISA $me $DEBUG );
6 use FS::Record qw( qsearch qsearchs );
7 use FS::pkg_class;
8 use FS::part_pkg;
9
10 $DEBUG = 0;
11 $me = '[FS::pkg_category]';
12
13 =head1 NAME
14
15 FS::pkg_category - Object methods for pkg_category records
16
17 =head1 SYNOPSIS
18
19   use FS::pkg_category;
20
21   $record = new FS::pkg_category \%hash;
22   $record = new FS::pkg_category { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::pkg_category object represents an package category.  Every package class
35 (see L<FS::pkg_class>) has, optionally, a package category. FS::pkg_category
36 inherits from FS::Record.  The following fields are currently supported:
37
38 =over 4
39
40 =item categorynum
41
42 primary key (assigned automatically for new package categoryes)
43
44 =item categoryname
45
46 Text name of this package category
47
48 =item weight
49
50 Weight
51
52 =item ticketing_queueid
53
54 Ticketing Queue
55
56 =item condense
57
58 Condense flag for invoice display, empty or 'Y'
59
60
61 =item disabled
62
63 Disabled flag, empty or 'Y'
64
65 =back
66
67 =head1 METHODS
68
69 =over 4
70
71 =item new HASHREF
72
73 Creates a new package category.  To add the package category to the database,
74 see L<"insert">.
75
76 =cut
77
78 sub table { 'pkg_category'; }
79
80 =item insert
81
82 Adds this package category to the database.  If there is an error, returns the
83 error, otherwise returns false.
84
85 =item delete
86
87 Deletes this package category from the database.  Only package categoryes with
88 no associated package definitions can be deleted.  If there is an error,
89 returns the error, otherwise returns false.
90
91 =item replace [ OLD_RECORD ]
92
93 Replaces OLD_RECORD with this one in the database.  If there is an error,
94 returns the error, otherwise returns false.
95
96 =item check
97
98 Checks all fields to make sure this is a valid package category.  If there is an
99 error, returns the error, otherwise returns false.  Called by the insert and
100 replace methods.
101
102 =cut
103
104 sub check {
105   my $self = shift;
106
107   $self->ut_enum('condense', [ '', 'Y' ])
108     || $self->ut_snumbern('ticketing_queueid')
109     || $self->SUPER::check;
110 }
111
112 =item ticketing_queue
113
114 Returns the queue name corresponding with the id from the I<ticketing_queueid>
115 field, or the empty string.
116
117 =cut
118
119 sub ticketing_queue {
120   my $self = shift;
121   return 'Agent-specific queue' if $self->ticketing_queueid == -1;
122   return '' unless $self->ticketing_queueid;
123   FS::TicketSystem->queue($self->ticketing_queueid);
124 }
125
126 # _ upgrade_data
127 #
128 # Used by FS::Upgrade to migrate to a new database.
129
130 sub _upgrade_data {
131   my ($class, %opts) = @_;
132
133   warn "$me upgrading $class\n" if $DEBUG;
134
135   my @pkg_category =
136     qsearch('pkg_category', { weight => { op => '!=', value => '' } } );
137
138   unless( scalar(@pkg_category) ) {
139     my @pkg_category = qsearch('pkg_category', {} );
140     my $weight = 0;
141     foreach ( sort { $a->description cmp $b->description } @pkg_category ) {
142       $_->weight($weight);
143       my $error = $_->replace;
144       die "error setting pkg_category weight: $error\n" if $error;
145       $weight += 10;
146     }
147   }
148
149   # create default category for package fees
150   my $tax_category_name = 'Taxes, Surcharges, and Fees';
151   my $tax_category = qsearchs('pkg_category', 
152     { categoryname => $tax_category_name }
153   );
154   if (!$tax_category) {
155     $tax_category = FS::pkg_category->new({
156         categoryname => $tax_category_name,
157         weight       => 1000, # doesn't really matter
158     });
159     my $error = $tax_category->insert;
160     die "error creating tax category: $error\n" if $error;
161   }
162
163   my $fee_class_name = 'Fees'; # does not appear on invoice
164   my $fee_class = qsearchs('pkg_class', { classname => $fee_class_name });
165   if (!$fee_class) {
166     $fee_class = FS::pkg_class->new({
167         classname   => $fee_class_name,
168         categorynum => $tax_category->categorynum,
169     });
170     my $error = $fee_class->insert;
171     die "error creating fee class: $error\n" if $error;
172   }
173
174   # assign it to all fee defs that don't otherwise have a class
175   foreach my $part_fee (qsearch('part_fee', { classnum => '' })) {
176     $part_fee->set('classnum', $fee_class->classnum);
177     my $error = $part_fee->replace;
178     die "error assigning default class to fee def#".$part_fee->feepart .
179       ":$error\n" if $error;
180   }
181
182   '';
183 }
184
185 =back
186
187 =head1 BUGS
188
189 =head1 SEE ALSO
190
191 L<FS::category_Common>, L<FS::Record>
192
193 =cut
194
195 1;
196