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