delete fees, RT#81713
[freeside.git] / FS / FS / cust_class.pm
1 package FS::cust_class;
2 use base qw( FS::class_Common );
3
4 use strict;
5 use FS::cust_category;
6 use FS::Record qw( dbh );
7
8 =head1 NAME
9
10 FS::cust_class - Object methods for cust_class records
11
12 =head1 SYNOPSIS
13
14   use FS::cust_class;
15
16   $record = new FS::cust_class \%hash;
17   $record = new FS::cust_class { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
29 An FS::pkg_class object represents an customer class.  Every customer (see
30 L<FS::cust_main>) has, optionally, a customer class. FS::cust_class inherits
31 from FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item classnum
36
37 primary key
38
39 =item classname
40
41 Text name of this customer class
42
43 =item categorynum
44
45 Number of associated cust_category (see L<FS::cust_category>)
46
47 =item tax
48
49 Tax exempt flag, empty or 'Y'.  Used when the cust_class-tax_exempt
50 configuration setting is turned on.
51
52 =item disabled
53
54 Disabled flag, empty or 'Y'
55
56 =back
57
58 =head1 METHODS
59
60 =over 4
61
62 =item new HASHREF
63
64 Creates a new customer class.  To add the customer class to the database, see
65 L<"insert">.
66
67 =cut
68
69 sub table { 'cust_class'; }
70 sub _target_table { 'cust_main'; }
71
72 =item insert
73
74 Adds this customer class to the database.  If there is an error, returns the
75 error, otherwise returns false.
76
77 =item delete
78
79 Delete this customer class from the database.  Only customer classes with no
80 associated customers can be deleted.  If there is an error, returns
81 the error, otherwise returns false.
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 customer class.  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 sub check {
97   my $self = shift;
98
99      $self->ut_enum('tax', [ '', 'Y' ])
100   || $self->SUPER::check;
101
102 }
103
104 =item cust_category
105
106 Returns the cust_category record associated with this class, or false if there
107 is none.
108
109 =cut
110
111 sub cust_category {
112   my $self = shift;
113   $self->category;
114 }
115
116 =item categoryname
117
118 Returns the category name associated with this class, or false if there
119 is none.
120
121 =item num_prospect
122
123 =item num_ordered
124
125 =item num_active
126
127 =item num_inactive
128
129 =item num_suspended
130
131 =item num_cancelled
132
133 =cut
134
135 sub num_sql {
136   my( $self, $sql ) = @_;
137   my $statement = "SELECT COUNT(*) FROM cust_main WHERE classnum = ? AND $sql";
138   my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
139   $sth->execute($self->classnum) or die $sth->errstr. " executing $statement";
140   $sth->fetchrow_arrayref->[0];
141 }
142
143 sub num_prospect  { shift->num_sql(FS::cust_main->prospect_sql) }
144 sub num_ordered   { shift->num_sql(FS::cust_main->ordered_sql) }
145 sub num_active    { shift->num_sql(FS::cust_main->active_sql) }
146 sub num_inactive  { shift->num_sql(FS::cust_main->inactive_sql) }
147 sub num_suspended { shift->num_sql(FS::cust_main->susp_sql) }
148 sub num_cancelled { shift->num_sql(FS::cust_main->cancel_sql) }
149
150 =back
151
152 =head1 BUGS
153
154 =head1 SEE ALSO
155
156 L<FS::cust_main>, L<FS::Record>
157
158 =cut
159
160 1;