1 package FS::cust_event;
4 use base qw( FS::cust_main_Mixin FS::Record );
5 use vars qw( @ISA $DEBUG $me );
6 use Carp qw( croak confess );
7 use FS::Record qw( qsearch qsearchs dbdef );
17 $me = '[FS::cust_event]';
21 FS::cust_event - Object methods for cust_event records
27 $record = new FS::cust_event \%hash;
28 $record = new FS::cust_event { 'column' => 'value' };
30 $error = $record->insert;
32 $error = $new_record->replace($old_record);
34 $error = $record->delete;
36 $error = $record->check;
40 An FS::cust_event object represents an completed event. FS::cust_event
41 inherits from FS::Record. The following fields are currently supported:
45 =item eventnum - primary key
47 =item eventpart - event definition (see L<FS::part_event>)
49 =item tablenum - customer, package or invoice, depending on the value of part_event.eventtable (see L<FS::cust_main>, L<FS::cust_pkg>, and L<FS::cust_bill>)
51 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
52 L<Time::Local> and L<Date::Parse> for conversion functions.
54 =item status - event status: B<new>, B<locked>, B<done> or B<failed>. Note: B<done> indicates the event is complete and should not be retried (statustext may still be set to an optional message), while B<failed> indicates the event failed and should be retried.
56 =item statustext - additional status detail (i.e. error or progress message)
58 =item no_action - 'Y' if the event action wasn't performed. Some actions
59 contain an internal check to see if the action is going to be impossible (for
60 example, emailing a notice to a customer who has no email address), and if so,
61 won't attempt the action. It shouldn't be reported as a failure because
62 there's no need to retry it. However, the action should set no_action = 'Y'
63 so that there's a record.
73 Creates a new completed invoice event. To add the compelted invoice event to
74 the database, see L<"insert">.
76 Note that this stores the hash reference, not a distinct copy of the hash it
77 points to. You can ask the object for a copy with the I<hash> method.
81 # the new method can be inherited from FS::Record, if a table method is defined
83 sub table { 'cust_event'; }
85 sub cust_linked { $_[0]->cust_main_custnum; }
86 sub cust_unlinked_msg {
88 "WARNING: can't find cust_main.custnum ". $self->custnum;
89 #' (cust_bill.invnum '. $self->invnum. ')';
93 $self->cust_main_custnum(@_) || $self->SUPER::custnum(@_);
98 Adds this record to the database. If there is an error, returns the error,
99 otherwise returns false.
103 # the insert method can be inherited from FS::Record
107 Delete this record from the database.
111 # the delete method can be inherited from FS::Record
113 =item replace OLD_RECORD
115 Replaces the OLD_RECORD with this one in the database. If there is an error,
116 returns the error, otherwise returns false.
120 # the replace method can be inherited from FS::Record
124 Checks all fields to make sure this is a valid completed invoice event. If
125 there is an error, returns the error, otherwise returns false. Called by the
126 insert and replace methods.
130 # the check method should currently be supplied - FS::Record contains some
131 # data checking routines
136 my $error = $self->ut_numbern('eventnum')
137 || $self->ut_foreign_key('eventpart', 'part_event', 'eventpart')
139 return $error if $error;
141 my $eventtable = $self->part_event->eventtable;
142 my $dbdef_eventtable = dbdef->table( $eventtable );
145 $self->ut_foreign_key( 'tablenum',
147 $dbdef_eventtable->primary_key
149 || $self->ut_number('_date')
150 || $self->ut_enum('status', [qw( new locked done failed initial)])
151 || $self->ut_anything('statustext')
152 || $self->ut_flag('no_action')
154 return $error if $error;
161 Returns the event definition (see L<FS::part_event>) for this completed event.
167 qsearchs( 'part_event', { 'eventpart' => $self->eventpart } );
172 Returns the customer, package, invoice or batched payment (see
173 L<FS::cust_main>, L<FS::cust_pkg>, L<FS::cust_bill> or L<FS::cust_pay_batch>)
174 for this completed invoice event.
179 croak "FS::cust_event::cust_bill called";
184 my $eventtable = $self->part_event->eventtable;
185 my $dbdef_table = dbdef->table( $eventtable );
186 my $primary_key = $dbdef_table->primary_key;
187 qsearchs( $eventtable, { $primary_key => $self->tablenum } );
190 =item test_conditions [ OPTION => VALUE ... ]
192 Tests conditions for this event, returns true if all conditions are satisfied,
197 sub test_conditions {
198 my( $self, %opt ) = @_;
199 my $part_event = $self->part_event;
200 my $object = $self->cust_X;
201 my @conditions = $part_event->part_event_condition;
202 $opt{'cust_event'} = $self;
203 $opt{'time'} = $self->_date
204 or die "test_conditions called without cust_event._date\n";
205 # this MUST be set, or all hell breaks loose in event conditions.
206 # it MUST be in the same time as in the cust_event object, or
207 # future time-dependent events will trigger incorrectly.
209 #no unsatisfied conditions
210 #! grep ! $_->condition( $object, %opt ), @conditions;
211 my @unsatisfied = grep ! $_->condition( $object, %opt ), @conditions;
213 if ( $opt{'stats_hashref'} ) {
214 foreach my $unsat (@unsatisfied) {
215 $opt{'stats_hashref'}->{$unsat->conditionname}++;
224 Runs the event action.
230 my %opt = @_; # currently only 'time'
231 my $time = $opt{'time'} || time;
233 my $part_event = $self->part_event;
235 my $object = $self->cust_X;
236 my $obj_pkey = $object->primary_key;
237 my $for = "for ". $object->table. " ". $object->$obj_pkey();
238 warn "running cust_event ". $self->eventnum.
239 " (". $part_event->action. ") $for\n"
244 local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
245 $error = eval { $part_event->do_action($object, $self); };
253 $statustext = "Error running ". $part_event->action. " action: $@";
256 if ( $error eq 'N/A' ) {
257 # archaic way to indicate no-op completion of spool_csv (and maybe
259 $self->no_action('Y');
261 $statustext = $error;
267 #replace or add myself
269 $self->status($status);
270 $self->statustext($statustext);
272 $error = $self->eventnum ? $self->replace : $self->insert;
274 #this is why we need that locked state...
275 my $e = 'WARNING: Event run but database not updated - '.
276 'error replacing or inserting cust_event '. $self->eventnum.
288 Changes the status of this event from B<done> to B<failed>, allowing it to be
295 return '' unless $self->status eq 'done';
296 my $old = ref($self)->new( { $self->hash } );
297 $self->status('failed');
298 $self->replace($old);
303 #Changes the statustext of this event to B<retriable>, rendering it
304 #retriable (should retry be called).
309 confess "cust_event->retriable called";
311 return '' unless $self->status eq 'done';
312 my $old = ref($self)->new( { $self->hash } );
313 $self->statustext('retriable');
314 $self->replace($old);
325 JOIN part_event USING ( eventpart )
327 LEFT JOIN cust_bill ON ( eventtable = 'cust_bill' AND tablenum = invnum )
328 LEFT JOIN cust_pkg ON ( eventtable = 'cust_pkg' AND tablenum = pkgnum )
329 LEFT JOIN cust_pay ON ( eventtable = 'cust_pay' AND tablenum = paynum )
330 LEFT JOIN cust_pay_batch ON ( eventtable = 'cust_pay_batch' AND tablenum = paybatchnum )
331 LEFT JOIN cust_statement ON ( eventtable = 'cust_statement' AND tablenum = cust_statement.statementnum )
333 LEFT JOIN cust_svc ON ( eventtable = 'svc_acct' AND tablenum = svcnum )
334 LEFT JOIN cust_pkg AS cust_pkg_for_svc ON ( cust_svc.pkgnum = cust_pkg_for_svc.pkgnum )
336 LEFT JOIN cust_main ON (
337 ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
338 OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
339 OR ( eventtable = 'cust_pkg' AND cust_pkg.custnum = cust_main.custnum )
340 OR ( eventtable = 'cust_pay' AND cust_pay.custnum = cust_main.custnum )
341 OR ( eventtable = 'svc_acct' AND cust_pkg_for_svc.custnum = cust_main.custnum )
347 =item search_sql_where HASHREF
349 Class method which returns an SQL WHERE fragment to search for parameters
350 specified in HASHREF. Valid parameters are
378 #Note: validates all passed-in data; i.e. safe to use with unchecked CGI params.
381 sub search_sql_where {
382 my($class, $param) = @_;
384 warn "$me search_sql_where called with params: \n".
385 join("\n", map { " $_: ". $param->{$_} } keys %$param ). "\n";
388 my @search = $class->cust_search_sql($param);
391 my @eventpart = ref($param->{'eventpart'})
392 ? @{ $param->{'eventpart'} }
393 : split(',', $param->{'eventpart'});
394 @eventpart = grep /^(\d+)$/, @eventpart;
396 push @search, 'eventpart IN ('. join(',', @eventpart). ')';
399 if ( $param->{'beginning'} =~ /^(\d+)$/ ) {
400 push @search, "cust_event._date >= $1";
402 if ( $param->{'ending'} =~ /^(\d+)$/ ) {
403 push @search, "cust_event._date <= $1";
406 #if ( $param->{'failed'} ) {
407 # push @search, "statustext != ''",
408 # "statustext IS NOT NULL",
409 # "statustext != 'N/A'";
413 my @event_status = ref($param->{'event_status'})
414 ? @{ $param->{'event_status'} }
415 : split(',', $param->{'event_status'});
416 if ( @event_status ) {
419 my ($done_Y, $done_N, $done_S);
420 # done_Y: action was taken
421 # done_N: action was not taken
422 # done_S: status message returned
423 foreach (@event_status) {
424 if ($_ eq 'done_Y') {
426 } elsif ( $_ eq 'done_N' ) {
428 } elsif ( $_ eq 'done_S' ) {
434 if ( $done_Y or $done_N or $done_S ) {
435 push @status, 'done';
438 push @search, "cust_event.status IN(" .
439 join(',', map "'$_'", @status) .
443 # done_S status should include only those where statustext is not null,
444 # and done_Y should include only those where it is.
445 if ( $done_Y and $done_N and $done_S ) {
450 push @done_status, "(cust_event.no_action IS NULL AND cust_event.statustext IS NULL)";
453 push @done_status, "(cust_event.no_action = 'Y')";
456 push @done_status, "(cust_event.no_action IS NULL AND cust_event.statustext IS NOT NULL)";
458 push @search, join(' OR ', @done_status) if @done_status;
463 # always hide initialization
464 push @search, 'cust_event.status != \'initial\'';
466 if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
467 push @search, "cust_main.custnum = '$1'";
470 if ( $param->{'invnum'} =~ /^(\d+)$/ ) {
471 push @search, "part_event.eventtable = 'cust_bill'",
475 if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) {
476 push @search, "part_event.eventtable = 'cust_pkg'",
480 if ( $param->{'paynum'} =~ /^(\d+)$/ ) {
481 push @search, "part_event.eventtable = 'cust_pay'",
485 if ( $param->{'svcnum'} =~ /^(\d+)$/ ) {
486 push @search, "part_event.eventtable = 'svc_acct'",
490 my $where = 'WHERE '. join(' AND ', @search );
492 join(' AND ', @search );
506 sub process_reprint {
507 process_re_X('print', @_);
514 sub process_reemail {
515 process_re_X('email', @_);
523 process_re_X('fax', @_);
526 use Storable qw(thaw);
530 my( $method, $job ) = ( shift, shift );
532 my $param = thaw(decode_base64(shift));
533 warn Dumper($param) if $DEBUG;
544 my($method, $param, $job) = @_;
546 my $search_sql = FS::cust_event->search_sql_where($param);
548 #maybe not...? we do want the "re-" action to match the search more closely
549 # # yuck! hardcoded *AND* sequential scans!
550 #my $where = " WHERE action LIKE 'cust_bill_send%' ".
551 # ( $search_sql ? " AND $search_sql" : "" );
553 my $where = ( $search_sql ? " WHERE $search_sql" : "" );
555 my @cust_event = qsearch({
556 'table' => 'cust_event',
557 'addl_from' => FS::cust_event->join_sql(),
559 'extra_sql' => $where,
562 warn "$me re_X found ". scalar(@cust_event). " events\n"
565 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
566 foreach my $cust_event ( @cust_event ) {
568 my $cust_X = $cust_event->cust_X; # cust_bill
569 next unless $cust_X->can($method);
571 my $part_event = $cust_event->part_event;
572 my $template = $part_event->templatename
573 || $cust_X->agent_template;
574 my $modenum = $part_event->option('modenum') || '';
575 my $invoice_from = $part_event->option('agent_invoice_from') || '';
576 $cust_X->set('mode' => $modenum);
577 $cust_X->$method( { template => $template,
579 from => $invoice_from,
582 if ( $job ) { #progressbar foo
584 if ( time - $min_sec > $last ) {
585 my $error = $job->update_statustext(
586 int( 100 * $num / scalar(@cust_event) )
588 die $error if $error;
595 #this doesn't work, but it would be nice
596 #if ( $job ) { #progressbar foo
597 # my $error = $job->update_statustext(
598 # scalar(@cust_event). " invoices re-${method}ed"
600 # die $error if $error;
609 L<FS::part_event>, L<FS::cust_bill>, L<FS::Record>, schema.html from the