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