1 package FS::cust_event_fee;
2 use base qw( FS::Record FS::FeeOrigin_Mixin );
5 use FS::Record qw( qsearch dbh );
9 FS::cust_event_fee - Object methods for cust_event_fee records
13 use FS::cust_event_fee;
15 $record = new FS::cust_event_fee \%hash;
16 $record = new FS::cust_event_fee { 'column' => 'value' };
18 $error = $record->insert;
20 $error = $new_record->replace($old_record);
22 $error = $record->delete;
24 $error = $record->check;
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 and FS::FeeOrigin_Mixin. The following fields are currently supported:
35 =item eventfeenum - primary key
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.
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.
46 =item feepart - key of the fee definition (L<FS::part_fee>).
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.
59 Creates a new event-fee link. To add the record to the database,
64 sub table { 'cust_event_fee'; }
68 Adds this record to the database. If there is an error, returns the error,
69 otherwise returns false.
73 Delete this record from the database.
75 =item replace OLD_RECORD
77 Replaces the OLD_RECORD with this one in the database. If there is an error,
78 returns the error, otherwise returns false.
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
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 || $self->ut_flag('nextbill')
98 return $error if $error;
109 =item _by_cust CUSTNUM[, PARAMS]
111 See L<FS::FeeOrigin_Mixin/by_cust>. This is the implementation for
112 event-triggered fees.
118 my $custnum = shift or return;
120 $custnum =~ /^\d+$/ or die "bad custnum $custnum";
123 my $where = ($params{hashref} && keys (%{ $params{hashref} }))
127 table => 'cust_event_fee',
128 addl_from => 'JOIN cust_event USING (eventnum) ' .
129 'JOIN part_event USING (eventpart) ',
130 extra_sql => "$where eventtable = 'cust_main' ".
131 "AND cust_event.tablenum = $custnum",
135 table => 'cust_event_fee',
136 addl_from => 'JOIN cust_event USING (eventnum) ' .
137 'JOIN part_event USING (eventpart) ' .
138 'JOIN cust_bill ON (cust_event.tablenum = cust_bill.invnum)',
139 extra_sql => "$where eventtable = 'cust_bill' ".
140 "AND cust_bill.custnum = $custnum",
144 table => 'cust_event_fee',
145 addl_from => 'JOIN cust_event USING (eventnum) ' .
146 'JOIN part_event USING (eventpart) ' .
147 'JOIN cust_pay_batch ON (cust_event.tablenum = cust_pay_batch.paybatchnum)',
148 extra_sql => "$where eventtable = 'cust_pay_batch' ".
149 "AND cust_pay_batch.custnum = $custnum",
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",
165 See L<FS::FeeOrigin_Mixin/cust_bill>. This version simply returns the event
166 object if the event is an invoice event.
172 my $object = $self->cust_event->cust_X;
173 if ( $object->isa('FS::cust_bill') ) {
182 See L<FS::FeeOrigin_Mixin/cust_bill>. This version simply returns the event
183 object if the event is a package event.
189 my $object = $self->cust_event->cust_X;
190 if ( $object->isa('FS::cust_pkg') ) {
201 sub _upgrade_schema {
202 my ($class, %opts) = @_;
205 DELETE FROM cust_event_Fee WHERE NOT EXISTS
206 ( SELECT 1 FROM cust_event WHERE cust_event.eventnum = cust_event_fee.eventnum )
209 my $sth = dbh->prepare($sql) or die dbh->errstr;
210 $sth->execute or die $sth->errstr;
219 L<FS::cust_event>, L<FS::FeeOrigin_Mixin>, L<FS::Record>