29d7c5c1fb92c91858dfd4db834e75d0af4979e5
[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 or cust_bill events; this will return both.
118
119 PARAMS can be additional params to pass to qsearch; this really only works
120 for 'hashref' and 'order_by'.
121
122 =cut
123
124 sub by_cust {
125   my $class = shift;
126   my $custnum = shift or return;
127   my %params = @_;
128   $custnum =~ /^\d+$/ or die "bad custnum $custnum";
129
130   # silliness
131   my $where = ($params{hashref} && keys (%{ $params{hashref} }))
132               ? 'AND'
133               : 'WHERE';
134   qsearch({
135     table     => 'cust_event_fee',
136     addl_from => 'JOIN cust_event USING (eventnum) ' .
137                  'JOIN part_event USING (eventpart) ',
138     extra_sql => "$where eventtable = 'cust_main' ".
139                  "AND cust_event.tablenum = $custnum",
140     %params
141   }),
142   qsearch({
143     table     => 'cust_event_fee',
144     addl_from => 'JOIN cust_event USING (eventnum) ' .
145                  'JOIN part_event USING (eventpart) ' .
146                  'JOIN cust_bill ON (cust_event.tablenum = cust_bill.invnum)',
147     extra_sql => "$where eventtable = 'cust_bill' ".
148                  "AND cust_bill.custnum = $custnum",
149     %params
150   })
151 }
152
153 # stubs
154
155 sub cust_event {
156   my $self = shift;
157   FS::cust_event->by_key($self->eventnum);
158 }
159
160 sub part_fee {
161   my $self = shift;
162   FS::part_fee->by_key($self->feepart);
163 }
164
165 =head1 BUGS
166
167 =head1 SEE ALSO
168
169 L<FS::cust_event>, L<FS::part_fee>, L<FS::Record>
170
171 =cut
172
173 1;
174