1 package FS::cust_event_fee;
2 use base qw( FS::cust_main_Mixin FS::Record FS::FeeOrigin_Mixin );
5 use FS::Record qw( qsearch dbh );
10 FS::cust_event_fee - Object methods for cust_event_fee records
14 use FS::cust_event_fee;
16 $record = new FS::cust_event_fee \%hash;
17 $record = new FS::cust_event_fee { 'column' => 'value' };
19 $error = $record->insert;
21 $error = $new_record->replace($old_record);
23 $error = $record->delete;
25 $error = $record->check;
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:
36 =item eventfeenum - primary key
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.
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.
47 =item feepart - key of the fee definition (L<FS::part_fee>).
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.
60 Creates a new event-fee link. To add the record to the database,
65 sub table { 'cust_event_fee'; }
69 Adds this record to the database. If there is an error, returns the error,
70 otherwise returns false.
74 Delete this record from the database.
81 my $oldAutoCommit = $FS::UID::AutoCommit;
82 local $FS::UID::AutoCommit = 0;
85 my $cust_bill_pkg = $self->cust_bill_pkg;
86 if ( $cust_bill_pkg ) {
87 my $error = $cust_bill_pkg->delete;
89 $dbh->rollback if $oldAutoCommit;
94 my $error = $self->SUPER::delete;
96 $dbh->rollback if $oldAutoCommit;
100 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
106 =item replace OLD_RECORD
108 Replaces the OLD_RECORD with this one in the database. If there is an error,
109 returns the error, otherwise returns false.
113 Checks all fields to make sure this is a valid example. If there is
114 an error, returns the error, otherwise returns false. Called by the insert
123 $self->ut_numbern('eventfeenum')
124 || $self->ut_foreign_key('eventnum', 'cust_event', 'eventnum')
125 || $self->ut_foreign_keyn('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
126 || $self->ut_foreign_key('feepart', 'part_fee', 'feepart')
127 || $self->ut_flag('nextbill')
129 return $error if $error;
140 =item _by_cust CUSTNUM[, PARAMS]
142 See L<FS::FeeOrigin_Mixin/by_cust>. This is the implementation for
143 event-triggered fees.
149 my $custnum = shift or return;
151 $custnum =~ /^\d+$/ or die "bad custnum $custnum";
154 my $where = ($params{hashref} && keys (%{ $params{hashref} }))
158 table => 'cust_event_fee',
159 addl_from => 'JOIN cust_event USING (eventnum) ' .
160 'JOIN part_event USING (eventpart) ',
161 extra_sql => "$where eventtable = 'cust_main' ".
162 "AND cust_event.tablenum = $custnum",
166 table => 'cust_event_fee',
167 addl_from => 'JOIN cust_event USING (eventnum) ' .
168 'JOIN part_event USING (eventpart) ' .
169 'JOIN cust_bill ON (cust_event.tablenum = cust_bill.invnum)',
170 extra_sql => "$where eventtable = 'cust_bill' ".
171 "AND cust_bill.custnum = $custnum",
175 table => 'cust_event_fee',
176 addl_from => 'JOIN cust_event USING (eventnum) ' .
177 'JOIN part_event USING (eventpart) ' .
178 'JOIN cust_pay_batch ON (cust_event.tablenum = cust_pay_batch.paybatchnum)',
179 extra_sql => "$where eventtable = 'cust_pay_batch' ".
180 "AND cust_pay_batch.custnum = $custnum",
184 table => 'cust_event_fee',
185 addl_from => 'JOIN cust_event USING (eventnum) ' .
186 'JOIN part_event USING (eventpart) ' .
187 'JOIN cust_pkg ON (cust_event.tablenum = cust_pkg.pkgnum)',
188 extra_sql => "$where eventtable = 'cust_pkg' ".
189 "AND cust_pkg.custnum = $custnum",
196 See L<FS::FeeOrigin_Mixin/cust_bill>. This version simply returns the event
197 object if the event is an invoice event.
203 my $object = $self->cust_event->cust_X;
204 if ( $object->isa('FS::cust_bill') ) {
213 See L<FS::FeeOrigin_Mixin/cust_bill>. This version simply returns the event
214 object if the event is a package event.
220 my $object = $self->cust_event->cust_X;
221 if ( $object->isa('FS::cust_pkg') ) {
228 =item search_sql_where
232 sub search_sql_where {
233 my($class, $param) = @_;
235 my $where = FS::cust_event->search_sql_where( $param );
237 if ( $param->{'billpkgnum'} eq 'NULL' ) {
238 $where .= ' AND billpkgnum IS NULL';
239 } elsif ( $param->{'billpkgnum'} eq 'NOT NULL' ) {
240 $where .= ' AND billpkgnum IS NOT NULL';
254 ' LEFT JOIN cust_event USING (eventnum)
255 LEFT JOIN cust_bill_pkg USING (billpkgnum)
256 LEFT JOIN cust_bill AS fee_cust_bill USING (invnum)
257 LEFT JOIN part_fee ON (cust_event_fee.feepart = part_fee.feepart )
258 '. FS::cust_event->join_sql();
273 my( $job, $param ) = @_;
275 my $search_sql = FS::cust_event_fee->search_sql_where($param);
276 my $where = $search_sql ? " WHERE $search_sql" : '';
278 my @cust_event_fee = qsearch({
279 'table' => 'cust_event_fee',
280 'addl_from' => FS::cust_event_fee->join_sql(),
282 'extra_sql' => $where,
285 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
286 foreach my $cust_event_fee ( @cust_event_fee ) {
288 my $error = $cust_event_fee->delete;
289 die $error if $error;
291 if ( $job ) { #progressbar foo
293 if ( time - $min_sec > $last ) {
294 my $error = $job->update_statustext(
295 int( 100 * $num / scalar(@cust_event_fee) )
297 die $error if $error;
306 sub _upgrade_schema {
307 my ($class, %opts) = @_;
310 DELETE FROM cust_event_Fee WHERE NOT EXISTS
311 ( SELECT 1 FROM cust_event WHERE cust_event.eventnum = cust_event_fee.eventnum )
314 my $sth = dbh->prepare($sql) or die dbh->errstr;
315 $sth->execute or die $sth->errstr;
324 L<FS::cust_event>, L<FS::FeeOrigin_Mixin>, L<FS::Record>