1 package FS::cust_event;
2 use base qw( FS::cust_main_Mixin FS::Record );
5 use vars qw( $DEBUG $me );
6 use Carp qw( croak confess );
7 use FS::Record qw( qsearch qsearchs dbdef );
16 $me = '[FS::cust_event]';
20 FS::cust_event - Object methods for cust_event records
26 $record = new FS::cust_event \%hash;
27 $record = new FS::cust_event { 'column' => 'value' };
29 $error = $record->insert;
31 $error = $new_record->replace($old_record);
33 $error = $record->delete;
35 $error = $record->check;
39 An FS::cust_event object represents an completed event. FS::cust_event
40 inherits from FS::Record. The following fields are currently supported:
44 =item eventnum - primary key
46 =item eventpart - event definition (see L<FS::part_event>)
48 =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>)
50 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
51 L<Time::Local> and L<Date::Parse> for conversion functions.
53 =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.
55 =item statustext - additional status detail (i.e. error or progress message)
57 =item no_action - 'Y' if the event action wasn't performed. Some actions
58 contain an internal check to see if the action is going to be impossible (for
59 example, emailing a notice to a customer who has no email address), and if so,
60 won't attempt the action. It shouldn't be reported as a failure because
61 there's no need to retry it. However, the action should set no_action = 'Y'
62 so that there's a record.
72 Creates a new completed invoice event. To add the compelted invoice event to
73 the database, see L<"insert">.
75 Note that this stores the hash reference, not a distinct copy of the hash it
76 points to. You can ask the object for a copy with the I<hash> method.
80 # the new method can be inherited from FS::Record, if a table method is defined
82 sub table { 'cust_event'; }
84 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
85 sub cust_unlinked_msg {
87 "WARNING: can't find cust_main.custnum ". $self->custnum;
88 #' (cust_bill.invnum '. $self->invnum. ')';
92 $self->cust_main_custnum(@_) || $self->SUPER::custnum(@_);
97 Adds this record to the database. If there is an error, returns the error,
98 otherwise returns false.
102 # the insert method can be inherited from FS::Record
106 Delete this record from the database.
110 # the delete method can be inherited from FS::Record
112 =item replace OLD_RECORD
114 Replaces the OLD_RECORD with this one in the database. If there is an error,
115 returns the error, otherwise returns false.
119 # the replace method can be inherited from FS::Record
123 Checks all fields to make sure this is a valid completed invoice event. If
124 there is an error, returns the error, otherwise returns false. Called by the
125 insert and replace methods.
129 # the check method should currently be supplied - FS::Record contains some
130 # data checking routines
135 my $error = $self->ut_numbern('eventnum')
136 || $self->ut_foreign_key('eventpart', 'part_event', 'eventpart')
138 return $error if $error;
140 my $eventtable = $self->part_event->eventtable;
141 my $dbdef_eventtable = dbdef->table( $eventtable );
144 $self->ut_foreign_key( 'tablenum',
146 $dbdef_eventtable->primary_key
148 || $self->ut_number('_date')
149 || $self->ut_enum('status', [qw( new locked done failed initial)])
150 || $self->ut_anything('statustext')
151 || $self->ut_flag('no_action')
153 return $error if $error;
160 Returns the event definition (see L<FS::part_event>) for this completed event.
164 Returns the customer, package, invoice or batched payment (see
165 L<FS::cust_main>, L<FS::cust_pkg>, L<FS::cust_bill> or L<FS::cust_pay_batch>)
166 for this completed invoice event.
171 croak "FS::cust_event::cust_bill called";
176 my $eventtable = $self->part_event->eventtable;
177 my $dbdef_table = dbdef->table( $eventtable );
178 my $primary_key = $dbdef_table->primary_key;
179 qsearchs( $eventtable, { $primary_key => $self->tablenum } );
182 =item test_conditions [ OPTION => VALUE ... ]
184 Tests conditions for this event, returns true if all conditions are satisfied,
189 sub test_conditions {
190 my( $self, %opt ) = @_;
191 my $part_event = $self->part_event;
192 my $object = $self->cust_X;
193 my @conditions = $part_event->part_event_condition;
194 $opt{'cust_event'} = $self;
195 $opt{'time'} = $self->_date
196 or die "test_conditions called without cust_event._date\n";
197 # this MUST be set, or all hell breaks loose in event conditions.
198 # it MUST be in the same time as in the cust_event object, or
199 # future time-dependent events will trigger incorrectly.
201 #no unsatisfied conditions
202 #! grep ! $_->condition( $object, %opt ), @conditions;
203 my @unsatisfied = grep ! $_->condition( $object, %opt ), @conditions;
205 if ( $opt{'stats_hashref'} ) {
206 foreach my $unsat (@unsatisfied) {
207 $opt{'stats_hashref'}->{$unsat->conditionname}++;
216 Runs the event action.
222 my %opt = @_; # currently only 'time'
223 my $time = $opt{'time'} || time;
225 my $part_event = $self->part_event;
227 my $object = $self->cust_X;
228 my $obj_pkey = $object->primary_key;
229 my $for = "for ". $object->table. " ". $object->$obj_pkey();
230 warn "running cust_event ". $self->eventnum.
231 " (". $part_event->action. ") $for\n"
236 local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
237 $error = eval { $part_event->do_action($object, $self); };
245 $statustext = "Error running ". $part_event->action. " action: $@";
248 if ( $error eq 'N/A' ) {
249 # archaic way to indicate no-op completion of spool_csv (and maybe
251 $self->no_action('Y');
253 $statustext = $error;
259 #replace or add myself
261 $self->status($status);
262 $self->statustext($statustext);
264 $error = $self->eventnum ? $self->replace : $self->insert;
266 #this is why we need that locked state...
267 my $e = 'WARNING: Event run but database not updated - '.
268 'error replacing or inserting cust_event '. $self->eventnum.
280 Changes the status of this event from B<done> to B<failed>, allowing it to be
287 return '' unless $self->status eq 'done';
288 my $old = ref($self)->new( { $self->hash } );
289 $self->status('failed');
290 $self->replace($old);
295 #Changes the statustext of this event to B<retriable>, rendering it
296 #retriable (should retry be called).
301 confess "cust_event->retriable called";
303 return '' unless $self->status eq 'done';
304 my $old = ref($self)->new( { $self->hash } );
305 $self->statustext('retriable');
306 $self->replace($old);
317 JOIN part_event USING ( eventpart )
318 LEFT JOIN cust_bill ON ( eventtable = 'cust_bill' AND tablenum = invnum )
319 LEFT JOIN cust_pkg ON ( eventtable = 'cust_pkg' AND tablenum = pkgnum )
320 LEFT JOIN cust_pay ON ( eventtable = 'cust_pay' AND tablenum = paynum )
321 LEFT JOIN cust_svc ON ( eventtable = 'svc_acct' AND tablenum = svcnum )
322 LEFT JOIN cust_pkg AS cust_pkg_for_svc ON ( cust_svc.pkgnum = cust_pkg_for_svc.pkgnum )
323 LEFT JOIN cust_main ON (
324 ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
325 OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
326 OR ( eventtable = 'cust_pkg' AND cust_pkg.custnum = cust_main.custnum )
327 OR ( eventtable = 'cust_pay' AND cust_pay.custnum = cust_main.custnum )
328 OR ( eventtable = 'svc_acct' AND cust_pkg_for_svc.custnum = cust_main.custnum )
334 =item search_sql_where HASHREF
336 Class method which returns an SQL WHERE fragment to search for parameters
337 specified in HASHREF. Valid parameters are
361 #Note: validates all passed-in data; i.e. safe to use with unchecked CGI params.
364 sub search_sql_where {
365 my($class, $param) = @_;
367 warn "$me search_sql_where called with params: \n".
368 join("\n", map { " $_: ". $param->{$_} } keys %$param ). "\n";
371 my @search = $class->cust_search_sql($param);
374 my @eventpart = ref($param->{'eventpart'})
375 ? @{ $param->{'eventpart'} }
376 : split(',', $param->{'eventpart'});
377 @eventpart = grep /^(\d+)$/, @eventpart;
379 push @search, 'eventpart IN ('. join(',', @eventpart). ')';
382 if ( $param->{'beginning'} =~ /^(\d+)$/ ) {
383 push @search, "cust_event._date >= $1";
385 if ( $param->{'ending'} =~ /^(\d+)$/ ) {
386 push @search, "cust_event._date <= $1";
389 #if ( $param->{'failed'} ) {
390 # push @search, "statustext != ''",
391 # "statustext IS NOT NULL",
392 # "statustext != 'N/A'";
396 my @event_status = ref($param->{'event_status'})
397 ? @{ $param->{'event_status'} }
398 : split(',', $param->{'event_status'});
399 if ( @event_status ) {
402 my ($done_Y, $done_N, $done_S);
403 # done_Y: action was taken
404 # done_N: action was not taken
405 # done_S: status message returned
406 foreach (@event_status) {
407 if ($_ eq 'done_Y') {
409 } elsif ( $_ eq 'done_N' ) {
411 } elsif ( $_ eq 'done_S' ) {
417 if ( $done_Y or $done_N or $done_S ) {
418 push @status, 'done';
421 push @search, "cust_event.status IN(" .
422 join(',', map "'$_'", @status) .
426 # done_S status should include only those where statustext is not null,
427 # and done_Y should include only those where it is.
428 if ( $done_Y and $done_N and $done_S ) {
433 push @done_status, "(cust_event.no_action IS NULL AND cust_event.statustext IS NULL)";
436 push @done_status, "(cust_event.no_action = 'Y')";
439 push @done_status, "(cust_event.no_action IS NULL AND cust_event.statustext IS NOT NULL)";
441 push @search, join(' OR ', @done_status) if @done_status;
446 # always hide initialization
447 push @search, 'cust_event.status != \'initial\'';
449 if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
450 push @search, "cust_main.custnum = '$1'";
453 if ( $param->{'invnum'} =~ /^(\d+)$/ ) {
454 push @search, "part_event.eventtable = 'cust_bill'",
458 if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) {
459 push @search, "part_event.eventtable = 'cust_pkg'",
463 if ( $param->{'paynum'} =~ /^(\d+)$/ ) {
464 push @search, "part_event.eventtable = 'cust_pay'",
468 if ( $param->{'svcnum'} =~ /^(\d+)$/ ) {
469 push @search, "part_event.eventtable = 'svc_acct'",
473 my $where = 'WHERE '. join(' AND ', @search );
475 join(' AND ', @search );
489 sub process_reprint {
490 process_re_X('print', @_);
497 sub process_reemail {
498 process_re_X('email', @_);
506 process_re_X('fax', @_);
511 my( $method, $job, $param ) = @_;
512 warn Dumper($param) if $DEBUG;
523 my($method, $param, $job) = @_;
525 my $search_sql = FS::cust_event->search_sql_where($param);
527 #maybe not...? we do want the "re-" action to match the search more closely
528 # # yuck! hardcoded *AND* sequential scans!
529 #my $where = " WHERE action LIKE 'cust_bill_send%' ".
530 # ( $search_sql ? " AND $search_sql" : "" );
532 my $where = ( $search_sql ? " WHERE $search_sql" : "" );
534 my @cust_event = qsearch({
535 'table' => 'cust_event',
536 'addl_from' => FS::cust_event->join_sql(),
538 'extra_sql' => $where,
541 warn "$me re_X found ". scalar(@cust_event). " events\n"
544 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
545 foreach my $cust_event ( @cust_event ) {
547 my $cust_X = $cust_event->cust_X; # cust_bill
548 next unless $cust_X->can($method);
550 my $part_event = $cust_event->part_event;
551 my $template = $part_event->templatename
552 || $cust_X->agent_template;
553 my $modenum = $part_event->option('modenum') || '';
554 my $invoice_from = $part_event->option('agent_invoice_from') || '';
555 $cust_X->set('mode' => $modenum);
556 $cust_X->$method( { template => $template,
558 from => $invoice_from,
561 if ( $job ) { #progressbar foo
563 if ( time - $min_sec > $last ) {
564 my $error = $job->update_statustext(
565 int( 100 * $num / scalar(@cust_event) )
567 die $error if $error;
574 #this doesn't work, but it would be nice
575 #if ( $job ) { #progressbar foo
576 # my $error = $job->update_statustext(
577 # scalar(@cust_event). " invoices re-${method}ed"
579 # die $error if $error;
588 L<FS::part_event>, L<FS::cust_bill>, L<FS::Record>, schema.html from the