Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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 =back
49
50 =head1 METHODS
51
52 =over 4
53
54 =item new HASHREF
55
56 Creates a new event-fee link.  To add the record to the database, 
57 see L<"insert">.
58
59 =cut
60
61 sub table { 'cust_event_fee'; }
62
63 =item insert
64
65 Adds this record to the database.  If there is an error, returns the error,
66 otherwise returns false.
67
68 =item delete
69
70 Delete this record from the database.
71
72 =item replace OLD_RECORD
73
74 Replaces the OLD_RECORD with this one in the database.  If there is an error,
75 returns the error, otherwise returns false.
76
77 =item check
78
79 Checks all fields to make sure this is a valid example.  If there is
80 an error, returns the error, otherwise returns false.  Called by the insert
81 and replace methods.
82
83 =cut
84
85 # the check method should currently be supplied - FS::Record contains some
86 # data checking routines
87
88 sub check {
89   my $self = shift;
90
91   my $error = 
92     $self->ut_numbern('eventfeenum')
93     || $self->ut_foreign_key('eventnum', 'cust_event', 'eventnum')
94     || $self->ut_foreign_keyn('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
95     || $self->ut_foreign_key('feepart', 'part_fee', 'feepart')
96   ;
97   return $error if $error;
98
99   $self->SUPER::check;
100 }
101
102 =back
103
104 =head1 CLASS METHODS
105
106 =over 4
107
108 =item by_cust CUSTNUM[, PARAMS]
109
110 Finds all cust_event_fee records belonging to the customer CUSTNUM.  Currently
111 fee events can be cust_main or cust_bill events; this will return both.
112
113 PARAMS can be additional params to pass to qsearch; this really only works
114 for 'hashref' and 'order_by'.
115
116 =cut
117
118 sub by_cust {
119   my $class = shift;
120   my $custnum = shift or return;
121   my %params = @_;
122   $custnum =~ /^\d+$/ or die "bad custnum $custnum";
123
124   # silliness
125   my $where = ($params{hashref} && keys (%{ $params{hashref} }))
126               ? 'AND'
127               : 'WHERE';
128   qsearch({
129     table     => 'cust_event_fee',
130     addl_from => 'JOIN cust_event USING (eventnum) ' .
131                  'JOIN part_event USING (eventpart) ',
132     extra_sql => "$where eventtable = 'cust_main' ".
133                  "AND cust_event.tablenum = $custnum",
134     %params
135   }),
136   qsearch({
137     table     => 'cust_event_fee',
138     addl_from => 'JOIN cust_event USING (eventnum) ' .
139                  'JOIN part_event USING (eventpart) ' .
140                  'JOIN cust_bill ON (cust_event.tablenum = cust_bill.invnum)',
141     extra_sql => "$where eventtable = 'cust_bill' ".
142                  "AND cust_bill.custnum = $custnum",
143     %params
144   })
145 }
146
147                   
148
149 =head1 BUGS
150
151 =head1 SEE ALSO
152
153 L<FS::cust_event>, L<FS::part_fee>, L<FS::Record>
154
155 =cut
156
157 1;
158