Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / tax_status.pm
1 package FS::tax_status;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6
7 our %initial_data;
8
9 =head1 NAME
10
11 FS::tax_status - Object methods for tax_status records
12
13 =head1 SYNOPSIS
14
15   use FS::tax_status;
16
17   $record = new FS::tax_status \%hash;
18   $record = new FS::tax_status { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::tax_status object represents a customer tax status for use with
31 an external tax table.  FS::tax_status inherits from FS::Record.  The 
32 following fields are currently supported:
33
34 =over 4
35
36 =item taxstatusnum
37
38 primary key
39
40 =item data_vendor
41
42 Data vendor name (corresponds to the value of the C<taxproduct> config 
43 variable.)
44
45 =item taxstatus
46
47 The data vendor's name or code for the tax status.
48
49 =item description
50
51 Description for use in the Freeside UI.
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new tax status.  To add the record to the database, see L<"insert">.
62
63 =cut
64
65 sub table { 'tax_status'; }
66
67 =item insert
68
69 Adds this record to the database.  If there is an error, returns the error,
70 otherwise returns false.
71
72 =item delete
73
74 Delete this record from the database.
75
76 =item replace OLD_RECORD
77
78 Replaces the OLD_RECORD with this one in the database.  If there is an error,
79 returns the error, otherwise returns false.
80
81 =item check
82
83 Checks all fields to make sure this is a valid example.  If there is
84 an error, returns the error, otherwise returns false.  Called by the insert
85 and replace methods.
86
87 =cut
88
89 sub check {
90   my $self = shift;
91
92   my $error = 
93     $self->ut_numbern('taxstatusnum')
94     || $self->ut_textn('data_vendor')
95     || $self->ut_text('taxstatus')
96     || $self->ut_text('description')
97   ;
98   return $error if $error;
99
100   $self->SUPER::check;
101 }
102
103 sub _upgrade_data {
104   my $self = shift;
105   my $error;
106   foreach my $data_vendor ( keys %initial_data ) {
107     my $status_hash = $initial_data{$data_vendor};
108     foreach my $taxstatus (sort keys %$status_hash) {
109       my $description = $status_hash->{$taxstatus};
110       my $tax_status;
111       if ($tax_status = qsearchs('tax_status', {
112             data_vendor => $data_vendor,
113             taxstatus   => $taxstatus
114         }))
115       {
116         if ($tax_status->description ne $description) {
117           $tax_status->set(description => $description);
118           $error = $tax_status->replace;
119         }
120         # else it's already correct
121       } else {
122         $tax_status = FS::tax_status->new({
123             data_vendor => $data_vendor,
124             taxstatus   => $taxstatus,
125             description => $description
126         });
127         $error = $tax_status->insert;
128       }
129       die $error if $error;
130     }
131   }
132 }
133
134 %initial_data = (
135   'avalara' => {
136     'A' => 'Federal Government',
137     'B' => 'State/Local Government',
138     'C' => 'Tribal Government',
139     'D' => 'Foreign Diplomat',
140     'E' => 'Charitable Organization',
141     'F' => 'Religious/Education',
142     'G' => 'Resale',
143     'H' => 'Agricultural Production',
144     'I' => 'Industrial Production',
145     'J' => 'Direct Pay Permit',
146     'K' => 'Direct Mail',
147     'L' => 'Other',
148     'M' => 'Local Government',
149     # P, Q, R: Canada, not yet supported
150     # MED1/MED2: totally irrelevant to our users
151   },
152   suretax => {
153     'R' => 'Residential',
154     'B' => 'Business',
155     'I' => 'Industrial',
156     'L' => 'Lifeline',
157   },
158   billsoft => {
159     'Residential' => 'Residential',
160     'Business'    => 'Business',
161     'Industrial'  => 'Industrial',
162     'Senior Citizen' => 'Senior Citizen',
163   },
164 );
165
166 =back
167
168 =head1 SEE ALSO
169
170 L<FS::Record>, schema.html from the base documentation.
171
172 =cut
173
174 1;
175