better reporting for tax credits, RT#4729
[freeside.git] / FS / FS / cust_credit_bill_pkg.pm
1 package FS::cust_credit_bill_pkg;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearchs ); # qsearch );
6 use FS::cust_main_Mixin;
7 use FS::cust_credit_bill;
8 use FS::cust_bill_pkg;
9 use FS::cust_bill_pkg_tax_location;
10 use FS::cust_bill_pkg_tax_rate_location;
11
12 @ISA = qw( FS::cust_main_Mixin FS::Record );
13
14 =head1 NAME
15
16 FS::cust_credit_bill_pkg - Object methods for cust_credit_bill_pkg records
17
18 =head1 SYNOPSIS
19
20   use FS::cust_credit_bill_pkg;
21
22   $record = new FS::cust_credit_bill_pkg \%hash;
23   $record = new FS::cust_credit_bill_pkg { 'column' => 'value' };
24
25   $error = $record->insert;
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33 =head1 DESCRIPTION
34
35 An FS::cust_credit_bill_pkg object represents application of a credit (see 
36 L<FS::cust_credit_bill>) to a specific line item within an invoice
37 (see L<FS::cust_bill_pkg>).  FS::cust_credit_bill_pkg inherits from FS::Record.
38 The following fields are currently supported:
39
40 =over 4
41
42 =item creditbillpkgnum -  primary key
43
44 =item creditbillnum - Credit application to the overall invoice (see L<FS::cust_credit::bill>)
45
46 =item billpkgnum - Line item to which credit is applied (see L<FS::cust_bill_pkg>)
47
48 =item amount - Amount of the credit applied to this line item.
49
50 =item setuprecur - 'setup' or 'recur', designates whether the payment was applied to the setup or recurring portion of the line item.
51
52 =item sdate - starting date of recurring fee
53
54 =item edate - ending date of recurring fee
55
56 =back
57
58 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">.  Also
59 see L<Time::Local> and L<Date::Parse> for conversion functions.
60
61 =head1 METHODS
62
63 =over 4
64
65 =item new HASHREF
66
67 Creates a new example.  To add the example to the database, see L<"insert">.
68
69 Note that this stores the hash reference, not a distinct copy of the hash it
70 points to.  You can ask the object for a copy with the I<hash> method.
71
72 =cut
73
74 # the new method can be inherited from FS::Record, if a table method is defined
75
76 sub table { 'cust_credit_bill_pkg'; }
77
78 =item insert
79
80 Adds this record to the database.  If there is an error, returns the error,
81 otherwise returns false.
82
83 =cut
84
85 # the insert method can be inherited from FS::Record
86
87 =item delete
88
89 Delete this record from the database.
90
91 =cut
92
93 # the delete method can be inherited from FS::Record
94
95 =item replace OLD_RECORD
96
97 Replaces the OLD_RECORD with this one in the database.  If there is an error,
98 returns the error, otherwise returns false.
99
100 =cut
101
102 # the replace method can be inherited from FS::Record
103
104 =item check
105
106 Checks all fields to make sure this is a valid credit applicaiton.  If there is
107 an error, returns the error, otherwise returns false.  Called by the insert
108 and replace methods.
109
110 =cut
111
112 # the check method should currently be supplied - FS::Record contains some
113 # data checking routines
114
115 sub check {
116   my $self = shift;
117
118   my $error = 
119     $self->ut_numbern('creditbillpkgnum')
120     || $self->ut_foreign_key('creditbillnum', 'cust_credit_bill', 'creditbillnum')
121     || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum' )
122     || $self->ut_foreign_keyn('billpkgtaxlocationnum',
123                               'cust_bill_pkg_tax_location',
124                               'billpkgtaxlocationnum')
125     || $self->ut_foreign_keyn('billpkgtaxratelocationnum',
126                               'cust_bill_pkg_tax_rate_location',
127                               'billpkgtaxratelocationnum')
128     || $self->ut_money('amount')
129     || $self->ut_enum('setuprecur', [ 'setup', 'recur' ] )
130     || $self->ut_numbern('sdate')
131     || $self->ut_numbern('edate')
132   ;
133   return $error if $error;
134
135   $self->SUPER::check;
136 }
137
138 sub cust_credit_bill {
139   my $self = shift;
140   qsearchs('cust_credit_bill', { 'creditbillnum' => $self->creditbillnum } );
141 }
142
143 =back
144
145 =head1 BUGS
146
147 B<setuprecur> field is a kludge to compensate for cust_bill_pkg having separate
148 setup and recur fields.  It should be removed once that's fixed.
149
150 =head1 SEE ALSO
151
152 L<FS::Record>, schema.html from the base documentation.
153
154 =cut
155
156 1;
157