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 );
15 $me = '[FS::cust_event]';
19 FS::cust_event - Object methods for cust_event records
25 $record = new FS::cust_event \%hash;
26 $record = new FS::cust_event { 'column' => 'value' };
28 $error = $record->insert;
30 $error = $new_record->replace($old_record);
32 $error = $record->delete;
34 $error = $record->check;
38 An FS::cust_event object represents an completed event. FS::cust_event
39 inherits from FS::Record. The following fields are currently supported:
43 =item eventnum - primary key
45 =item eventpart - event definition (see L<FS::part_event>)
47 =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>)
49 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
50 L<Time::Local> and L<Date::Parse> for conversion functions.
52 =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.
54 =item statustext - additional status detail (i.e. error or progress message)
64 Creates a new completed invoice event. To add the compelted invoice event to
65 the database, see L<"insert">.
67 Note that this stores the hash reference, not a distinct copy of the hash it
68 points to. You can ask the object for a copy with the I<hash> method.
72 # the new method can be inherited from FS::Record, if a table method is defined
74 sub table { 'cust_event'; }
76 sub cust_linked { $_[0]->cust_main_custnum; }
77 sub cust_unlinked_msg {
79 "WARNING: can't find cust_main.custnum ". $self->custnum;
80 #' (cust_bill.invnum '. $self->invnum. ')';
84 $self->cust_main_custnum(@_) || $self->SUPER::custnum(@_);
89 Adds this record to the database. If there is an error, returns the error,
90 otherwise returns false.
94 # the insert method can be inherited from FS::Record
98 Delete this record from the database.
102 # the delete method can be inherited from FS::Record
104 =item replace OLD_RECORD
106 Replaces the OLD_RECORD with this one in the database. If there is an error,
107 returns the error, otherwise returns false.
111 # the replace method can be inherited from FS::Record
115 Checks all fields to make sure this is a valid completed invoice event. If
116 there is an error, returns the error, otherwise returns false. Called by the
117 insert and replace methods.
121 # the check method should currently be supplied - FS::Record contains some
122 # data checking routines
127 my $error = $self->ut_numbern('eventnum')
128 || $self->ut_foreign_key('eventpart', 'part_event', 'eventpart')
130 return $error if $error;
132 my $eventtable = $self->part_event->eventtable;
133 my $dbdef_eventtable = dbdef->table( $eventtable );
136 $self->ut_foreign_key( 'tablenum',
138 $dbdef_eventtable->primary_key
140 || $self->ut_number('_date')
141 || $self->ut_enum('status', [qw( new locked done failed )])
142 || $self->ut_anything('statustext')
144 return $error if $error;
151 Returns the event definition (see L<FS::part_event>) for this completed event.
157 qsearchs( 'part_event', { 'eventpart' => $self->eventpart } );
162 Returns the customer, package, invoice or batched payment (see
163 L<FS::cust_main>, L<FS::cust_pkg>, L<FS::cust_bill> or L<FS::cust_pay_batch>)
164 for this completed invoice event.
169 croak "FS::cust_event::cust_bill called";
174 my $eventtable = $self->part_event->eventtable;
175 my $dbdef_table = dbdef->table( $eventtable );
176 my $primary_key = $dbdef_table->primary_key;
177 qsearchs( $eventtable, { $primary_key => $self->tablenum } );
180 =item test_conditions [ OPTION => VALUE ... ]
182 Tests conditions for this event, returns true if all conditions are satisfied,
187 sub test_conditions {
188 my( $self, %opt ) = @_;
189 my $part_event = $self->part_event;
190 my $object = $self->cust_X;
191 my @conditions = $part_event->part_event_condition;
192 $opt{'cust_event'} = $self;
194 #no unsatisfied conditions
195 #! grep ! $_->condition( $object, %opt ), @conditions;
196 my @unsatisfied = grep ! $_->condition( $object, %opt ), @conditions;
198 if ( $opt{'stats_hashref'} ) {
199 foreach my $unsat (@unsatisfied) {
200 $opt{'stats_hashref'}->{$unsat->conditionname}++;
209 Runs the event action.
216 my $part_event = $self->part_event;
218 my $object = $self->cust_X;
219 my $obj_pkey = $object->primary_key;
220 my $for = "for ". $object->table. " ". $object->$obj_pkey();
221 warn "running cust_event ". $self->eventnum.
222 " (". $part_event->action. ") $for\n"
227 local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
228 $error = eval { $part_event->do_action($object, $self); };
236 $statustext = "Error running ". $part_event->action. " action: $@";
239 $statustext = $error;
244 #replace or add myself
246 $self->status($status);
247 $self->statustext($statustext);
249 $error = $self->eventnum ? $self->replace : $self->insert;
251 #this is why we need that locked state...
252 my $e = 'WARNING: Event run but database not updated - '.
253 'error replacing or inserting cust_event '. $self->eventnum.
265 Changes the status of this event from B<done> to B<failed>, allowing it to be
272 return '' unless $self->status eq 'done';
273 my $old = ref($self)->new( { $self->hash } );
274 $self->status('failed');
275 $self->replace($old);
280 #Changes the statustext of this event to B<retriable>, rendering it
281 #retriable (should retry be called).
286 confess "cust_event->retriable called";
288 return '' unless $self->status eq 'done';
289 my $old = ref($self)->new( { $self->hash } );
290 $self->statustext('retriable');
291 $self->replace($old);
302 JOIN part_event USING ( eventpart )
303 LEFT JOIN cust_bill ON ( eventtable = 'cust_bill' AND tablenum = invnum )
304 LEFT JOIN cust_pkg ON ( eventtable = 'cust_pkg' AND tablenum = pkgnum )
305 LEFT JOIN cust_main ON ( ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
306 OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
307 OR ( eventtable = 'cust_pkg' AND cust_pkg.custnum = cust_main.custnum )
313 =item search_sql_where HASHREF
315 Class method which returns an SQL WHERE fragment to search for parameters
316 specified in HASHREF. Valid parameters are
342 #Note: validates all passed-in data; i.e. safe to use with unchecked CGI params.
345 sub search_sql_where {
346 my($class, $param) = @_;
348 warn "$me search_sql_where called with params: \n".
349 join("\n", map { " $_: ". $param->{$_} } keys %$param ). "\n";
352 my @search = $class->cust_search_sql($param);
355 my @eventpart = ref($param->{'eventpart'})
356 ? @{ $param->{'eventpart'} }
357 : split(',', $param->{'eventpart'});
358 @eventpart = grep /^(\d+)$/, @eventpart;
360 push @search, 'eventpart IN ('. join(',', @eventpart). ')';
363 if ( $param->{'beginning'} =~ /^(\d+)$/ ) {
364 push @search, "cust_event._date >= $1";
366 if ( $param->{'ending'} =~ /^(\d+)$/ ) {
367 push @search, "cust_event._date <= $1";
370 if ( $param->{'failed'} ) {
371 push @search, "statustext != ''",
372 "statustext IS NOT NULL",
373 "statustext != 'N/A'";
376 if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
377 push @search, "cust_main.custnum = '$1'";
380 if ( $param->{'invnum'} =~ /^(\d+)$/ ) {
381 push @search, "part_event.eventtable = 'cust_bill'",
385 if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) {
386 push @search, "part_event.eventtable = 'cust_pkg'",
390 my $where = 'WHERE '. join(' AND ', @search );
392 join(' AND ', @search );
406 sub process_reprint {
407 process_re_X('print', @_);
414 sub process_reemail {
415 process_re_X('email', @_);
423 process_re_X('fax', @_);
426 use Storable qw(thaw);
430 my( $method, $job ) = ( shift, shift );
432 my $param = thaw(decode_base64(shift));
433 warn Dumper($param) if $DEBUG;
444 my($method, $param, $job) = @_;
446 my $search_sql = FS::cust_event->search_sql_where($param);
448 #maybe not...? we do want the "re-" action to match the search more closely
449 # # yuck! hardcoded *AND* sequential scans!
450 #my $where = " WHERE action LIKE 'cust_bill_send%' ".
451 # ( $search_sql ? " AND $search_sql" : "" );
453 my $where = ( $search_sql ? " WHERE $search_sql" : "" );
455 my @cust_event = qsearch({
456 'table' => 'cust_event',
457 'addl_from' => FS::cust_event->join_sql(),
459 'extra_sql' => $where,
462 warn "$me re_X found ". scalar(@cust_event). " events\n"
465 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
466 foreach my $cust_event ( @cust_event ) {
468 my $cust_X = $cust_event->cust_X; # cust_bill
469 next unless $cust_X->can($method);
471 $cust_X->$method( $cust_event->part_event->templatename
472 || $cust_X->agent_template
475 if ( $job ) { #progressbar foo
477 if ( time - $min_sec > $last ) {
478 my $error = $job->update_statustext(
479 int( 100 * $num / scalar(@cust_event) )
481 die $error if $error;
488 #this doesn't work, but it would be nice
489 #if ( $job ) { #progressbar foo
490 # my $error = $job->update_statustext(
491 # scalar(@cust_event). " invoices re-${method}ed"
493 # die $error if $error;
502 L<FS::part_event>, L<FS::cust_bill>, L<FS::Record>, schema.html from the