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