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