1896b52c912c96bf54577781bcbe737aff13d736
[freeside.git] / FS / FS / cust_event_fee.pm
1 package FS::cust_event_fee;
2 use base qw( FS::cust_main_Mixin FS::Record FS::FeeOrigin_Mixin );
3
4 use strict;
5 use FS::Record qw( qsearch dbh );
6 use FS::cust_event;
7
8 =head1 NAME
9
10 FS::cust_event_fee - Object methods for cust_event_fee records
11
12 =head1 SYNOPSIS
13
14   use FS::cust_event_fee;
15
16   $record = new FS::cust_event_fee \%hash;
17   $record = new FS::cust_event_fee { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
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:
33
34 =over 4
35
36 =item eventfeenum - primary key
37
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.
41
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.
46
47 =item feepart - key of the fee definition (L<FS::part_fee>).
48
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.
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new event-fee link.  To add the record to the database, 
61 see L<"insert">.
62
63 =cut
64
65 sub table { 'cust_event_fee'; }
66
67 =item insert
68
69 Adds this record to the database.  If there is an error, returns the error,
70 otherwise returns false.
71
72 =item delete
73
74 Delete this record from the database.
75
76 =cut
77
78 sub delete {
79   my $self = shift;
80
81   my $oldAutoCommit = $FS::UID::AutoCommit;
82   local $FS::UID::AutoCommit = 0;
83   my $dbh = dbh;
84
85   my $cust_bill_pkg = $self->cust_bill_pkg;
86   if ( $cust_bill_pkg ) {
87     my $error = $cust_bill_pkg->delete;
88     if ( $error ) {
89       $dbh->rollback if $oldAutoCommit;
90       return $error;
91     }
92   }
93
94   my $error = $self->SUPER::delete;
95   if ( $error ) {
96     $dbh->rollback if $oldAutoCommit;
97     return $error;
98   }
99
100   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
101
102   '';
103
104 }
105
106 =item replace OLD_RECORD
107
108 Replaces the OLD_RECORD with this one in the database.  If there is an error,
109 returns the error, otherwise returns false.
110
111 =item check
112
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
115 and replace methods.
116
117 =cut
118
119 sub check {
120   my $self = shift;
121
122   my $error = 
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')
128   ;
129   return $error if $error;
130
131   $self->SUPER::check;
132 }
133
134 =back
135
136 =head1 CLASS METHODS
137
138 =over 4
139
140 =item _by_cust CUSTNUM[, PARAMS]
141
142 See L<FS::FeeOrigin_Mixin/by_cust>. This is the implementation for 
143 event-triggered fees.
144
145 =cut
146
147 sub _by_cust {
148   my $class = shift;
149   my $custnum = shift or return;
150   my %params = @_;
151   $custnum =~ /^\d+$/ or die "bad custnum $custnum";
152
153   # silliness
154   my $where = ($params{hashref} && keys (%{ $params{hashref} }))
155               ? 'AND'
156               : 'WHERE';
157   qsearch({
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",
163     %params
164   }),
165   qsearch({
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",
172     %params
173   }),
174   qsearch({
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",
181     %params
182   }),
183   qsearch({
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",
190     %params
191   })
192 }
193
194 =item cust_bill
195
196 See L<FS::FeeOrigin_Mixin/cust_bill>. This version simply returns the event
197 object if the event is an invoice event.
198
199 =cut
200
201 sub cust_bill {
202   my $self = shift;
203   my $object = $self->cust_event->cust_X;
204   if ( $object->isa('FS::cust_bill') ) {
205     return $object;
206   } else {
207     return '';
208   }
209 }
210
211 =item cust_pkg
212
213 See L<FS::FeeOrigin_Mixin/cust_bill>. This version simply returns the event
214 object if the event is a package event.
215
216 =cut
217
218 sub cust_pkg {
219   my $self = shift;
220   my $object = $self->cust_event->cust_X;
221   if ( $object->isa('FS::cust_pkg') ) {
222     return $object;
223   } else {
224     return '';
225   }
226 }
227
228 =item search_sql_where
229
230 =cut
231
232 sub search_sql_where {
233   my($class, $param) = @_;
234
235   my $where = FS::cust_event->search_sql_where( $param );
236
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';
241   }
242
243   $where;
244
245 }
246
247 =item join_sql
248
249 =cut
250
251 sub join_sql {
252   #my $class = shift;
253
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();
259
260 }
261
262 =back
263
264 =head1 SUBROUTINES
265
266 =over 4
267
268 =item process_delete
269
270 =cut
271
272 sub process_delete {
273   my( $job, $param ) = @_;
274
275   my $search_sql = FS::cust_event_fee->search_sql_where($param);
276   my $where = $search_sql ? " WHERE $search_sql" : '';
277
278   my @cust_event_fee = qsearch({
279     'table'     => 'cust_event_fee',
280     'addl_from' => FS::cust_event_fee->join_sql(),
281     'hashref'   => {},
282     'extra_sql' => $where,
283   });
284
285   my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
286   foreach my $cust_event_fee ( @cust_event_fee ) {
287
288     my $error = $cust_event_fee->delete;
289     die $error if $error;
290
291     if ( $job ) { #progressbar foo
292       $num++;
293       if ( time - $min_sec > $last ) {
294         my $error = $job->update_statustext(
295           int( 100 * $num / scalar(@cust_event_fee) )
296         );
297         die $error if $error;
298         $last = time;
299       }
300     }
301
302   }
303
304 }
305
306 sub _upgrade_schema {
307   my ($class, %opts) = @_;
308
309   my $sql = '
310     DELETE FROM cust_event_Fee WHERE NOT EXISTS
311       ( SELECT 1 FROM cust_event WHERE cust_event.eventnum = cust_event_fee.eventnum )
312   ';
313
314   my $sth = dbh->prepare($sql) or die dbh->errstr;
315   $sth->execute or die $sth->errstr;
316   '';
317 }
318
319
320 =head1 BUGS
321
322 =head1 SEE ALSO
323
324 L<FS::cust_event>, L<FS::FeeOrigin_Mixin>, L<FS::Record>
325
326 =cut
327
328 1;
329