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