update the tax class editor to enable taxclass adding, RT#2929
[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
8 @ISA = qw(FS::Record);
9
10 =head1 NAME
11
12 FS::part_pkg_taxclass - Object methods for part_pkg_taxclass records
13
14 =head1 SYNOPSIS
15
16   use FS::part_pkg_taxclass;
17
18   $record = new FS::part_pkg_taxclass \%hash;
19   $record = new FS::part_pkg_taxclass { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::part_pkg_taxclass object represents a tax class.  FS::part_pkg_taxclass
32 inherits from FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item taxclassnum
37
38 Primary key
39
40 =item taxclass
41
42 Tax class
43
44 =back
45
46 =head1 METHODS
47
48 =over 4
49
50 =item new HASHREF
51
52 Creates a new tax class.  To add the tax class to the database, see L<"insert">.
53
54 Note that this stores the hash reference, not a distinct copy of the hash it
55 points to.  You can ask the object for a copy with the I<hash> method.
56
57 =cut
58
59 # the new method can be inherited from FS::Record, if a table method is defined
60
61 sub table { 'part_pkg_taxclass'; }
62
63 =item insert
64
65 Adds this record to the database.  If there is an error, returns the error,
66 otherwise returns false.
67
68 =cut
69
70 # the insert method can be inherited from FS::Record
71
72 =item delete
73
74 Delete this record from the database.
75
76 =cut
77
78 # the delete method can be inherited from FS::Record
79
80 =item replace OLD_RECORD
81
82 Replaces the OLD_RECORD with this one in the database.  If there is an error,
83 returns the error, otherwise returns false.
84
85 =cut
86
87 # the replace method can be inherited from FS::Record
88
89 =item check
90
91 Checks all fields to make sure this is a valid tax class.  If there is
92 an error, returns the error, otherwise returns false.  Called by the insert
93 and replace methods.
94
95 =cut
96
97 # the check method should currently be supplied - FS::Record contains some
98 # data checking routines
99
100 sub check {
101   my $self = shift;
102
103   my $error = 
104     $self->ut_numbern('taxclassnum')
105     || $self->ut_text('taxclass')
106   ;
107   return $error if $error;
108
109   $self->SUPER::check;
110 }
111
112 =back
113
114 =cut
115
116 # _upgrade_data
117 #
118 # Used by FS::Upgrade to migrate to a new database.
119
120 sub _upgrade_data { # class method
121   my ($class, %opts) = @_;
122
123   my $sth = dbh->prepare('
124     SELECT DISTINCT taxclass
125       FROM cust_main_county
126         LEFT JOIN part_pkg_taxclass USING ( taxclass )
127       WHERE taxclassnum IS NULL
128         AND taxclass IS NOT NULL
129   ') or die dbh->errstr;
130   $sth->execute or die $sth->errstr;
131   my %taxclass = map { $_->[0] => 1 } @{$sth->fetchall_arrayref};
132   my @taxclass = grep $_, keys %taxclass;
133
134   foreach my $taxclass ( @taxclass ) {
135
136     my $part_pkg_taxclass = new FS::part_pkg_taxclass ( {
137       'taxclass' => $taxclass,
138     } );
139     my $error = $part_pkg_taxclass->insert;
140     die $error if $error;
141
142   }
143
144 }
145
146 =head1 BUGS
147
148 Other tables (cust_main_county, part_pkg, agent_payment_gateway) have a text
149 taxclass instead of a key to this table.
150
151 =head1 SEE ALSO
152
153 L<FS::Record>, schema.html from the base documentation.
154
155 =cut
156
157 1;
158