1 package FS::cust_tax_exempt_pkg;
2 use base qw( FS::cust_main_Mixin FS::Record );
6 use FS::cust_main_county;
7 use FS::upgrade_journal;
9 # some kind of common ancestor with cust_bill_pkg_tax_location would make sense
13 FS::cust_tax_exempt_pkg - Object methods for cust_tax_exempt_pkg records
17 use FS::cust_tax_exempt_pkg;
19 $record = new FS::cust_tax_exempt_pkg \%hash;
20 $record = new FS::cust_tax_exempt_pkg { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::cust_tax_exempt_pkg object represents a record of a customer tax
33 exemption. Whenever a package would be taxed (based on its location and
34 taxclass), but some or all of it is exempt from taxation, an
35 FS::cust_tax_exempt_pkg record is created.
37 FS::cust_tax_exempt inherits from FS::Record. The following fields are
42 =item exemptpkgnum - primary key
44 =item billpkgnum - invoice line item (see L<FS::cust_bill_pkg>) that
45 was exempted from tax.
47 =item taxnum - tax rate (see L<FS::cust_main_county>)
49 =item year - the year in which the exemption occurred. NULL if this
50 is a customer or package exemption rather than a monthly exemption.
52 =item month - the month in which the exemption occurred. NULL if this
53 is a customer or package exemption.
55 =item amount - the amount of revenue exempted. For monthly exemptions
56 this may be anything up to the monthly exemption limit defined in
57 L<FS::cust_main_county> for this tax. For customer exemptions it is
58 always the full price of the line item. For package exemptions it
59 may be the setup fee, the recurring fee, or the sum of those.
61 =item exempt_cust - flag indicating that the customer is tax-exempt
62 (cust_main.tax = 'Y').
64 =item exempt_cust_taxname - flag indicating that the customer is exempt
65 from the tax with this name (see L<FS::cust_main_exemption).
67 =item exempt_setup, exempt_recur: flag indicating that the package's setup
68 or recurring fee is not taxable (part_pkg.setuptax and part_pkg.recurtax).
70 =item exempt_monthly: flag indicating that this is a monthly per-customer
71 exemption (Texas tax).
81 Creates a new exemption record. To add the examption record to the database,
84 Note that this stores the hash reference, not a distinct copy of the hash it
85 points to. You can ask the object for a copy with the I<hash> method.
89 # the new method can be inherited from FS::Record, if a table method is defined
91 sub table { 'cust_tax_exempt_pkg'; }
95 Adds this record to the database. If there is an error, returns the error,
96 otherwise returns false.
100 # the insert method can be inherited from FS::Record
104 Delete this record from the database.
108 # the delete method can be inherited from FS::Record
110 =item replace OLD_RECORD
112 Replaces the OLD_RECORD with this one in the database. If there is an error,
113 returns the error, otherwise returns false.
117 # the replace method can be inherited from FS::Record
121 Checks all fields to make sure this is a valid exemption record. If there is
122 an error, returns the error, otherwise returns false. Called by the insert
127 # the check method should currently be supplied - FS::Record contains some
128 # data checking routines
133 my $error = $self->ut_numbern('exemptnum')
134 || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
135 || $self->ut_foreign_key('taxnum', 'cust_main_county', 'taxnum')
136 || $self->ut_foreign_keyn('creditbillpkgnum',
137 'cust_credit_bill_pkg',
139 || $self->ut_numbern('year') #check better
140 || $self->ut_numbern('month') #check better
141 || $self->ut_money('amount')
142 || $self->ut_flag('exempt_cust')
143 || $self->ut_flag('exempt_setup')
144 || $self->ut_flag('exempt_recur')
145 || $self->ut_flag('exempt_cust_taxname')
146 || $self->SUPER::check
149 return $error if $error;
151 if ( $self->get('exempt_cust') ) {
152 $self->set($_ => '') for qw(
153 exempt_cust_taxname exempt_setup exempt_recur exempt_monthly month year
155 } elsif ( $self->get('exempt_cust_taxname') ) {
156 $self->set($_ => '') for qw(
157 exempt_setup exempt_recur exempt_monthly month year
159 } elsif ( $self->get('exempt_setup') || $self->get('exempt_recur') ) {
160 $self->set($_ => '') for qw(exempt_monthly month year);
161 } elsif ( $self->get('exempt_monthly') ) {
162 $self->year =~ /^\d{4}$/
163 or return "illegal exemption year: '".$self->year."'";
164 $self->month >= 1 && $self->month <= 12
165 or return "illegal exemption month: '".$self->month."'";
167 return "no exemption type selected";
173 =item cust_main_county
175 Returns the associated tax definition if it still exists in the database.
176 Otherwise returns false.
180 # do not remove; this can't be autogenerated
182 sub cust_main_county {
184 if ( $self->taxtype eq 'FS::cust_main_county' ) {
185 return FS::cust_main_county->by_key($self->taxnum);
193 my $journal = 'cust_tax_exempt_pkg_flags';
194 if ( !FS::upgrade_journal->is_done($journal) ) {
195 my $sql = "UPDATE cust_tax_exempt_pkg SET exempt_monthly = 'Y' ".
196 "WHERE month IS NOT NULL";
197 dbh->do($sql) or die dbh->errstr;
198 FS::upgrade_journal->set_done($journal);
206 Texas tax is still a royal pain in the ass.
210 L<FS::cust_main_county>, L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from
211 the base documentation.