default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / cust_bill_pkg_tax_rate_location.pm
1 package FS::cust_bill_pkg_tax_rate_location;
2 use base qw( FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearch qsearchs );
6 use FS::cust_pkg;
7 use FS::cust_bill_pay_pkg;
8 use FS::cust_credit_bill_pkg;
9 use FS::tax_rate;
10
11 =head1 NAME
12
13 FS::cust_bill_pkg_tax_rate_location - Object methods for cust_bill_pkg_tax_rate_location records
14
15 =head1 SYNOPSIS
16
17   use FS::cust_bill_pkg_tax_rate_location;
18
19   $record = new FS::cust_bill_pkg_tax_rate_location \%hash;
20   $record = new FS::cust_bill_pkg_tax_rate_location { '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::cust_bill_pkg_tax_rate_location object represents an record of taxation
33 based on package location.  FS::cust_bill_pkg_tax_rate_location inherits from
34 FS::Record.  The following fields are currently supported:
35
36 =over 4
37
38 =item billpkgtaxratelocationnum
39
40 billpkgtaxratelocationnum
41
42 =item billpkgnum
43
44 billpkgnum
45
46 =item taxnum
47
48 taxnum
49
50 =item taxtype
51
52 taxtype
53
54 =item locationtaxid
55
56 locationtaxid
57
58 =item taxratelocationnum
59
60 taxratelocationnum
61
62 =item amount
63
64 amount
65
66
67 =back
68
69 =head1 METHODS
70
71 =over 4
72
73 =item new HASHREF
74
75 Creates a new record.  To add the record to the database, see L<"insert">.
76
77 Note that this stores the hash reference, not a distinct copy of the hash it
78 points to.  You can ask the object for a copy with the I<hash> method.
79
80 =cut
81
82 sub table { 'cust_bill_pkg_tax_rate_location'; }
83
84 =item insert
85
86 Adds this record to the database.  If there is an error, returns the error,
87 otherwise returns false.
88
89 =item delete
90
91 Delete this record from the database.
92
93 =item replace OLD_RECORD
94
95 Replaces the OLD_RECORD with this one in the database.  If there is an error,
96 returns the error, otherwise returns false.
97
98 =item check
99
100 Checks all fields to make sure this is a valid record.  If there is
101 an error, returns the error, otherwise returns false.  Called by the insert
102 and replace methods.
103
104 =cut
105
106 # the check method should currently be supplied - FS::Record contains some
107 # data checking routines
108
109 sub check {
110   my $self = shift;
111
112   my $error = 
113     $self->ut_numbern('billpkgtaxratelocationnum')
114     || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum' )
115     || $self->ut_number('taxnum') #cust_bill_pkg/tax_rate key, based on taxtype
116     || $self->ut_enum('taxtype', [ qw( FS::cust_main_county FS::tax_rate ) ] )
117     || $self->ut_textn('locationtaxid')
118     || $self->ut_foreign_key('taxratelocationnum', 'tax_rate_location', 'taxratelocationnum' )
119     || $self->ut_money('amount')
120   ;
121   return $error if $error;
122
123   $self->SUPER::check;
124 }
125
126 =item cust_bill_pkg
127
128 Returns the associated cust_bill_pkg object
129
130 =item tax_rate_location
131
132 Returns the associated tax_rate_location object
133
134 =item taxname
135
136 Returns the tax name (the itemdesc).
137
138 =cut
139
140 sub taxname {
141   my $self = shift;
142   my $tax_rate = FS::tax_rate->by_key($self->taxnum)
143     or return '';
144   $tax_rate->taxname;
145 }
146
147 =item desc
148
149 Returns a description for this tax line item constituent.  Currently this
150 is the desc of the associated line item followed by the
151 state,county,city,locationtaxid for the location in parentheses.
152
153 =cut
154
155 sub desc {
156   my $self = shift;
157   my $tax_rate_location = $self->tax_rate_location;
158   my $location = join(', ', grep { $_ }
159                             map { $tax_rate_location->$_ }
160                             qw( state county city )
161   );
162   $location .= ( $location && $self->locationtaxid ) ? ', ' : '';
163   $location .= $self->locationtaxid;
164   my $cust_bill_pkg_desc = $self->billpkgnum
165                          ? $self->cust_bill_pkg->desc
166                          : $self->cust_bill_pkg_desc;
167   "$cust_bill_pkg_desc ($location)";
168
169 }
170
171
172 =item owed
173
174 Returns the amount owed (still outstanding) on this tax line item which is 
175 the amount of this record minus all payment applications and credit
176 applications.
177
178 =cut
179
180 sub owed {
181   my $self = shift;
182   my $balance = $self->amount;
183   $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg('setup') );
184   $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg('setup') );
185   $balance = sprintf( '%.2f', $balance );
186   $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
187   $balance;
188 }
189
190 sub cust_bill_pay_pkg {
191   my $self = shift;
192   qsearch( 'cust_bill_pay_pkg', { map { $_ => $self->$_ }
193                                   qw( billpkgtaxratelocationnum billpkgnum )
194                                 }
195          );
196 }
197
198 sub cust_credit_bill_pkg {
199   my $self = shift;
200   qsearch( 'cust_credit_bill_pkg', { map { $_ => $self->$_ }
201                                      qw( billpkgtaxratelocationnum billpkgnum )
202                                    }
203          );
204 }
205
206 =back
207
208 =head1 BUGS
209
210 =head1 SEE ALSO
211
212 L<FS::Record>, schema.html from the base documentation.
213
214 =cut
215
216 1;
217