package fees and usage-based fees, #27687, #25899
[freeside.git] / FS / FS / cust_event_fee.pm
1 package FS::cust_event_fee;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::cust_event;
7 use FS::part_fee;
8
9 =head1 NAME
10
11 FS::cust_event_fee - Object methods for cust_event_fee records
12
13 =head1 SYNOPSIS
14
15   use FS::cust_event_fee;
16
17   $record = new FS::cust_event_fee \%hash;
18   $record = new FS::cust_event_fee { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::cust_event_fee object links a billing event that charged a fee
31 (an L<FS::cust_event>) to the resulting invoice line item (an 
32 L<FS::cust_bill_pkg> object).  FS::cust_event_fee inherits from FS::Record.  
33 The following fields are currently supported:
34
35 =over 4
36
37 =item eventfeenum - primary key
38
39 =item eventnum - key of the cust_event record that required the fee to be 
40 created.  This is a unique column; there's no reason for a single event 
41 instance to create more than one fee.
42
43 =item billpkgnum - key of the cust_bill_pkg record representing the fee 
44 on an invoice.  This is also a unique column but can be NULL to indicate
45 a fee that hasn't been billed yet.  In that case it will be billed the next
46 time billing runs for the customer.
47
48 =item feepart - key of the fee definition (L<FS::part_fee>).
49
50 =item nextbill - 'Y' if the fee should be charged on the customer's next
51 bill, rather than causing a bill to be produced immediately.
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new event-fee link.  To add the record to the database, 
62 see L<"insert">.
63
64 =cut
65
66 sub table { 'cust_event_fee'; }
67
68 =item insert
69
70 Adds this record to the database.  If there is an error, returns the error,
71 otherwise returns false.
72
73 =item delete
74
75 Delete this record from the database.
76
77 =item replace OLD_RECORD
78
79 Replaces the OLD_RECORD with this one in the database.  If there is an error,
80 returns the error, otherwise returns false.
81
82 =item check
83
84 Checks all fields to make sure this is a valid example.  If there is
85 an error, returns the error, otherwise returns false.  Called by the insert
86 and replace methods.
87
88 =cut
89
90 # the check method should currently be supplied - FS::Record contains some
91 # data checking routines
92
93 sub check {
94   my $self = shift;
95
96   my $error = 
97     $self->ut_numbern('eventfeenum')
98     || $self->ut_foreign_key('eventnum', 'cust_event', 'eventnum')
99     || $self->ut_foreign_keyn('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
100     || $self->ut_foreign_key('feepart', 'part_fee', 'feepart')
101     || $self->ut_flag('nextbill')
102   ;
103   return $error if $error;
104
105   $self->SUPER::check;
106 }
107
108 =back
109
110 =head1 CLASS METHODS
111
112 =over 4
113
114 =item by_cust CUSTNUM[, PARAMS]
115
116 Finds all cust_event_fee records belonging to the customer CUSTNUM.  Currently
117 fee events can be cust_main, cust_pkg, or cust_bill events; this will return 
118 all of them.
119
120 PARAMS can be additional params to pass to qsearch; this really only works
121 for 'hashref' and 'order_by'.
122
123 =cut
124
125 sub by_cust {
126   my $class = shift;
127   my $custnum = shift or return;
128   my %params = @_;
129   $custnum =~ /^\d+$/ or die "bad custnum $custnum";
130
131   # silliness
132   my $where = ($params{hashref} && keys (%{ $params{hashref} }))
133               ? 'AND'
134               : 'WHERE';
135   qsearch({
136     table     => 'cust_event_fee',
137     addl_from => 'JOIN cust_event USING (eventnum) ' .
138                  'JOIN part_event USING (eventpart) ',
139     extra_sql => "$where eventtable = 'cust_main' ".
140                  "AND cust_event.tablenum = $custnum",
141     %params
142   }),
143   qsearch({
144     table     => 'cust_event_fee',
145     addl_from => 'JOIN cust_event USING (eventnum) ' .
146                  'JOIN part_event USING (eventpart) ' .
147                  'JOIN cust_bill ON (cust_event.tablenum = cust_bill.invnum)',
148     extra_sql => "$where eventtable = 'cust_bill' ".
149                  "AND cust_bill.custnum = $custnum",
150     %params
151   }),
152   qsearch({
153     table     => 'cust_event_fee',
154     addl_from => 'JOIN cust_event USING (eventnum) ' .
155                  'JOIN part_event USING (eventpart) ' .
156                  'JOIN cust_pkg ON (cust_event.tablenum = cust_pkg.pkgnum)',
157     extra_sql => "$where eventtable = 'cust_pkg' ".
158                  "AND cust_pkg.custnum = $custnum",
159     %params
160   })
161 }
162
163 # stubs
164
165 sub cust_event {
166   my $self = shift;
167   FS::cust_event->by_key($self->eventnum);
168 }
169
170 sub part_fee {
171   my $self = shift;
172   FS::part_fee->by_key($self->feepart);
173 }
174
175 =head1 BUGS
176
177 =head1 SEE ALSO
178
179 L<FS::cust_event>, L<FS::part_fee>, L<FS::Record>
180
181 =cut
182
183 1;
184