delete fees, RT#81713
[freeside.git] / FS / FS / part_pkg_report_option.pm
1 package FS::part_pkg_report_option;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
6
7 =head1 NAME
8
9 FS::part_pkg_report_option - Object methods for part_pkg_report_option records
10
11 =head1 SYNOPSIS
12
13   use FS::part_pkg_report_option;
14
15   $record = new FS::part_pkg_report_option \%hash;
16   $record = new FS::part_pkg_report_option { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::part_pkg_report_option object represents a package definition optional
29 reporting classification.  FS::part_pkg_report_option inherits from
30 FS::Record.  The following fields are currently supported:
31
32 =over 4
33
34 =item num
35
36 primary key
37
38 =item name
39
40 name - The name associated with the reporting option
41
42 =item disabled
43
44 disabled - set to 'Y' to prevent addition to new packages
45
46
47 =back
48
49 =head1 METHODS
50
51 =over 4
52
53 =item new HASHREF
54
55 Creates a new report option.  To add the option to the database, see L<"insert">.
56
57 Note that this stores the hash reference, not a distinct copy of the hash it
58 points to.  You can ask the object for a copy with the I<hash> method.
59
60 =cut
61
62 sub table { 'part_pkg_report_option'; }
63
64 =item insert
65
66 Adds this record to the database.  If there is an error, returns the error,
67 otherwise returns false.
68
69 =cut
70
71 =item delete
72
73 Delete this record from the database.
74
75 =cut
76
77 sub delete {
78   return "Can't delete part_pkg_report_option records!";
79 }
80
81 =item replace OLD_RECORD
82
83 Replaces the OLD_RECORD with this one in the database.  If there is an error,
84 returns the error, otherwise returns false.
85
86 =cut
87
88 =item check
89
90 Checks all fields to make sure this is a valid example.  If there is
91 an error, returns the error, otherwise returns false.  Called by the insert
92 and replace methods.
93
94 =cut
95
96 # the check method should currently be supplied - FS::Record contains some
97 # data checking routines
98
99 sub check {
100   my $self = shift;
101
102   my $error = 
103     $self->ut_numbern('num')
104     || $self->ut_text('name')
105     || $self->ut_enum('disabled', [ '', 'Y' ])
106   ;
107   return $error if $error;
108
109   $self->SUPER::check;
110 }
111
112 =back
113
114 =head1 CLASS METHODS
115
116 =over 4
117
118 =item subsets OPTIONNUM, ...
119
120 Given a list of report_option numbers, determines all combinations of those
121 numbers that exist on actual package definitions.  For each such combination,
122 returns an arrayref of report_option numbers, followed by an arrayref of
123 corresponding report class names.  This is used for a search optimization.
124
125 =cut
126
127 # probably doesn't belong here, but there's not a better place for it
128 # and optimizations are, by nature, hackish
129
130 sub subsets {
131   my ($self, @nums) = @_;
132   my @optionnames = map { "'report_option_$_'" } @nums;
133   my $where = "WHERE optionname IN(".join(',',@optionnames).")"
134     if @nums;
135   my $subselect =
136     "SELECT pkgpart, replace(optionname, 'report_option_', '')::int AS num ".
137     "FROM part_pkg_option $where ".
138     "ORDER BY pkgpart, num";
139   my $select =
140     "SELECT DISTINCT array_to_string(array_agg(num), ','), ".
141     "array_to_string(array_agg(name), ',') ".
142     "FROM ($subselect) AS x JOIN part_pkg_report_option USING (num) ".
143     "GROUP BY pkgpart";
144   my $dbh = dbh;
145   my $sth = $dbh->prepare($select)
146     or die $dbh->errstr; # seriously, this should never happen
147   $sth->execute
148     or die $sth->errstr;
149   # return a pair of entries for the null set (conventionally we use zero
150   # for that)
151   ( [ 0 ], [ '(empty class)' ],
152   # followed by the first two columns: report class numbers and names
153     map { [ split(',',$_->[0]) ],
154           [ split(',',$_->[1]) ] } @{ $sth->fetchall_arrayref }
155   );
156 }
157
158 =back
159
160 =head1 BUGS
161
162 Overlaps somewhat with pkg_class and pkg_category
163
164 =head1 SEE ALSO
165
166 L<FS::Record>, schema.html from the base documentation.
167
168 =cut
169
170 1;
171