stray closing /TABLE in the no-ticket case
[freeside.git] / FS / FS / part_pkg_taxclass.pm
1 package FS::part_pkg_taxclass;
2
3 use strict;
4 use vars qw( @ISA );
5 use Scalar::Util qw( blessed );
6 use FS::UID qw( dbh );
7 use FS::Record qw(qsearch); # qsearchs );
8 use FS::cust_main_county;
9
10 @ISA = qw(FS::Record);
11
12 =head1 NAME
13
14 FS::part_pkg_taxclass - Object methods for part_pkg_taxclass records
15
16 =head1 SYNOPSIS
17
18   use FS::part_pkg_taxclass;
19
20   $record = new FS::part_pkg_taxclass \%hash;
21   $record = new FS::part_pkg_taxclass { '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::part_pkg_taxclass object declares the existence of a taxable sales
34 class.  FS::part_pkg_taxclass inherits from FS::Record.  
35
36 FS::part_pkg_taxclass is not used in tax calculation.  It is only used to 
37 list a set of valid tax class names for use in the user interface.  When
38 using internal taxes, the actual matching of tax definitions to package
39 tax class is a string match between tax class names.  This is arguably
40 a bug.
41
42 The following fields are currently supported:
43
44 =over 4
45
46 =item taxclassnum
47
48 Primary key
49
50 =item taxclass
51
52 Tax class
53
54 =item disabled
55
56 Disabled flag, empty or 'Y'
57
58 =back
59
60 =head1 METHODS
61
62 =over 4
63
64 =item new HASHREF
65
66 Creates a new tax class.  To add the tax class to the database, see L<"insert">.
67
68 Note that this stores the hash reference, not a distinct copy of the hash it
69 points to.  You can ask the object for a copy with the I<hash> method.
70
71 =cut
72
73 # the new method can be inherited from FS::Record, if a table method is defined
74
75 sub table { 'part_pkg_taxclass'; }
76
77 =item insert
78
79 Adds this record to the database.  If there is an error, returns the error,
80 otherwise returns false.
81
82 =cut
83
84 sub insert {
85   my $self = shift;
86
87   local $SIG{HUP} = 'IGNORE';
88   local $SIG{INT} = 'IGNORE';
89   local $SIG{QUIT} = 'IGNORE';
90   local $SIG{TERM} = 'IGNORE';
91   local $SIG{TSTP} = 'IGNORE';
92   local $SIG{PIPE} = 'IGNORE';
93
94   my $oldAutoCommit = $FS::UID::AutoCommit;
95   local $FS::UID::AutoCommit = 0;
96   my $dbh = dbh;
97
98   my $error = $self->SUPER::insert;
99   if ( $error ) {
100     $dbh->rollback if $oldAutoCommit;
101     return $error;
102   }
103
104   my $sth = dbh->prepare("
105     SELECT country, state, county FROM cust_main_county
106       WHERE taxclass IS NOT NULL AND taxclass != ''
107       GROUP BY country, state, county
108   ") or die dbh->errstr;
109   $sth->execute or die $sth->errstr;
110
111   while ( my $row = $sth->fetchrow_hashref ) {
112     #warn "inserting for $row";
113     my $cust_main_county = new FS::cust_main_county {
114       'country'  => $row->{country},
115       'state'    => $row->{state},
116       'county'   => $row->{county},
117       'tax'      => 0,
118       'taxclass' => $self->taxclass,
119       #exempt_amount
120       #taxname
121       #setuptax
122       #recurtax
123     };
124     $error = $cust_main_county->insert;
125     #last if $error;
126     if ( $error ) {
127       $dbh->rollback if $oldAutoCommit;
128       return $error;
129     }
130   }
131
132   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
133   '';
134 }
135
136 =item delete
137
138 Delete this record from the database.
139
140 =cut
141
142 # the delete method can be inherited from FS::Record
143
144 =item replace OLD_RECORD
145
146 Replaces the OLD_RECORD with this one in the database.  If there is an error,
147 returns the error, otherwise returns false.
148
149 =cut
150
151 sub replace {
152   my $new = shift;
153
154   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
155               ? shift
156               : $new->replace_old;
157
158   return "Can't change tax class name (disable and create anew)"
159     if $old->taxclass ne $new->taxclass;
160
161   $new->SUPER::replace(@_);
162 }
163
164 =item check
165
166 Checks all fields to make sure this is a valid tax class.  If there is
167 an error, returns the error, otherwise returns false.  Called by the insert
168 and replace methods.
169
170 =cut
171
172 # the check method should currently be supplied - FS::Record contains some
173 # data checking routines
174
175 sub check {
176   my $self = shift;
177
178   my $error = 
179     $self->ut_numbern('taxclassnum')
180     || $self->ut_text('taxclass')
181     || $self->ut_enum('disabled', [ '', 'Y' ] )
182   ;
183   return $error if $error;
184
185   $self->SUPER::check;
186 }
187
188 =back
189
190 =cut
191
192 # _upgrade_data
193 #
194 # Used by FS::Upgrade to migrate to a new database.
195
196 sub _upgrade_data { # class method
197   my ($class, %opts) = @_;
198
199   my $sth = dbh->prepare('
200     SELECT DISTINCT taxclass
201       FROM cust_main_county
202         LEFT JOIN part_pkg_taxclass USING ( taxclass )
203       WHERE taxclassnum IS NULL
204         AND taxclass IS NOT NULL
205   ') or die dbh->errstr;
206   $sth->execute or die $sth->errstr;
207   my %taxclass = map { $_->[0] => 1 } @{$sth->fetchall_arrayref};
208   my @taxclass = grep $_, keys %taxclass;
209
210   foreach my $taxclass ( @taxclass ) {
211
212     my $part_pkg_taxclass = new FS::part_pkg_taxclass ( {
213       'taxclass' => $taxclass,
214     } );
215     my $error = $part_pkg_taxclass->insert;
216     die $error if $error;
217
218   }
219
220 }
221
222 =head1 CLASS METHODS
223
224 =over 4
225
226 =item taxclass_names
227
228 Returns a list of all the non-disabled tax classes. If tax classes aren't
229 enabled, returns a single empty string.
230
231 =cut
232
233 sub taxclass_names {
234   if ( FS::Conf->new->exists('enable_taxclasses') ) {
235     return map { $_->get('taxclass') }
236       qsearch('part_pkg_taxclass', { disabled => '' });
237   } else {
238     return ( '' );
239   }
240 }
241
242 =head1 BUGS
243
244 Other tables (cust_main_county, part_pkg, agent_payment_gateway) have a text
245 taxclass instead of a key to this table.
246
247 =head1 SEE ALSO
248
249 L<FS::Record>, schema.html from the base documentation.
250
251 =cut
252
253 1;
254