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