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)
66 Creates a new completed invoice event. To add the compelted invoice event to
67 the database, see L<"insert">.
69 Note that this stores the hash reference, not a distinct copy of the hash it
70 points to. You can ask the object for a copy with the I<hash> method.
74 # the new method can be inherited from FS::Record, if a table method is defined
76 sub table { 'cust_event'; }
78 sub cust_linked { $_[0]->cust_main_custnum; }
79 sub cust_unlinked_msg {
81 "WARNING: can't find cust_main.custnum ". $self->custnum;
82 #' (cust_bill.invnum '. $self->invnum. ')';
86 $self->cust_main_custnum(@_) || $self->SUPER::custnum(@_);
91 Adds this record to the database. If there is an error, returns the error,
92 otherwise returns false.
96 # the insert method can be inherited from FS::Record
100 Delete this record from the database.
104 # the delete method can be inherited from FS::Record
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 # the replace method can be inherited from FS::Record
117 Checks all fields to make sure this is a valid completed invoice event. If
118 there is an error, returns the error, otherwise returns false. Called by the
119 insert and replace methods.
123 # the check method should currently be supplied - FS::Record contains some
124 # data checking routines
129 my $error = $self->ut_numbern('eventnum')
130 || $self->ut_foreign_key('eventpart', 'part_event', 'eventpart')
132 return $error if $error;
134 my $eventtable = $self->part_event->eventtable;
135 my $dbdef_eventtable = dbdef->table( $eventtable );
138 $self->ut_foreign_key( 'tablenum',
140 $dbdef_eventtable->primary_key
142 || $self->ut_number('_date')
143 || $self->ut_enum('status', [qw( new locked done failed initial)])
144 || $self->ut_anything('statustext')
146 return $error if $error;
153 Returns the event definition (see L<FS::part_event>) for this completed event.
159 qsearchs( 'part_event', { 'eventpart' => $self->eventpart } );
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 $statustext = $error;
253 #replace or add myself
255 $self->status($status);
256 $self->statustext($statustext);
258 $error = $self->eventnum ? $self->replace : $self->insert;
260 #this is why we need that locked state...
261 my $e = 'WARNING: Event run but database not updated - '.
262 'error replacing or inserting cust_event '. $self->eventnum.
274 Changes the status of this event from B<done> to B<failed>, allowing it to be
281 return '' unless $self->status eq 'done';
282 my $old = ref($self)->new( { $self->hash } );
283 $self->status('failed');
284 $self->replace($old);
289 #Changes the statustext of this event to B<retriable>, rendering it
290 #retriable (should retry be called).
295 confess "cust_event->retriable called";
297 return '' unless $self->status eq 'done';
298 my $old = ref($self)->new( { $self->hash } );
299 $self->statustext('retriable');
300 $self->replace($old);
311 JOIN part_event USING ( eventpart )
312 LEFT JOIN cust_bill ON ( eventtable = 'cust_bill' AND tablenum = invnum )
313 LEFT JOIN cust_pkg ON ( eventtable = 'cust_pkg' AND tablenum = pkgnum )
314 LEFT JOIN cust_pay ON ( eventtable = 'cust_pay' AND tablenum = paynum )
315 LEFT JOIN cust_svc ON ( eventtable = 'svc_acct' AND tablenum = svcnum )
316 LEFT JOIN cust_pkg AS cust_pkg_for_svc ON ( cust_svc.pkgnum = cust_pkg_for_svc.pkgnum )
317 LEFT JOIN cust_main ON (
318 ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
319 OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
320 OR ( eventtable = 'cust_pkg' AND cust_pkg.custnum = cust_main.custnum )
321 OR ( eventtable = 'cust_pay' AND cust_pay.custnum = cust_main.custnum )
322 OR ( eventtable = 'svc_acct' AND cust_pkg_for_svc.custnum = cust_main.custnum )
328 =item search_sql_where HASHREF
330 Class method which returns an SQL WHERE fragment to search for parameters
331 specified in HASHREF. Valid parameters are
359 #Note: validates all passed-in data; i.e. safe to use with unchecked CGI params.
362 sub search_sql_where {
363 my($class, $param) = @_;
365 warn "$me search_sql_where called with params: \n".
366 join("\n", map { " $_: ". $param->{$_} } keys %$param ). "\n";
369 my @search = $class->cust_search_sql($param);
372 my @eventpart = ref($param->{'eventpart'})
373 ? @{ $param->{'eventpart'} }
374 : split(',', $param->{'eventpart'});
375 @eventpart = grep /^(\d+)$/, @eventpart;
377 push @search, 'eventpart IN ('. join(',', @eventpart). ')';
380 if ( $param->{'beginning'} =~ /^(\d+)$/ ) {
381 push @search, "cust_event._date >= $1";
383 if ( $param->{'ending'} =~ /^(\d+)$/ ) {
384 push @search, "cust_event._date <= $1";
387 if ( $param->{'failed'} ) {
388 push @search, "statustext != ''",
389 "statustext IS NOT NULL",
390 "statustext != 'N/A'";
393 if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
394 push @search, "cust_main.custnum = '$1'";
397 if ( $param->{'invnum'} =~ /^(\d+)$/ ) {
398 push @search, "part_event.eventtable = 'cust_bill'",
402 if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) {
403 push @search, "part_event.eventtable = 'cust_pkg'",
407 if ( $param->{'paynum'} =~ /^(\d+)$/ ) {
408 push @search, "part_event.eventtable = 'cust_pay'",
412 if ( $param->{'svcnum'} =~ /^(\d+)$/ ) {
413 push @search, "part_event.eventtable = 'svc_acct'",
417 my $where = 'WHERE '. join(' AND ', @search );
419 join(' AND ', @search );
433 sub process_reprint {
434 process_re_X('print', @_);
441 sub process_reemail {
442 process_re_X('email', @_);
450 process_re_X('fax', @_);
453 use Storable qw(thaw);
457 my( $method, $job ) = ( shift, shift );
459 my $param = thaw(decode_base64(shift));
460 warn Dumper($param) if $DEBUG;
471 my($method, $param, $job) = @_;
473 my $search_sql = FS::cust_event->search_sql_where($param);
475 #maybe not...? we do want the "re-" action to match the search more closely
476 # # yuck! hardcoded *AND* sequential scans!
477 #my $where = " WHERE action LIKE 'cust_bill_send%' ".
478 # ( $search_sql ? " AND $search_sql" : "" );
480 my $where = ( $search_sql ? " WHERE $search_sql" : "" );
482 my @cust_event = qsearch({
483 'table' => 'cust_event',
484 'addl_from' => FS::cust_event->join_sql(),
486 'extra_sql' => $where,
489 warn "$me re_X found ". scalar(@cust_event). " events\n"
492 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
493 foreach my $cust_event ( @cust_event ) {
495 my $cust_X = $cust_event->cust_X; # cust_bill
496 next unless $cust_X->can($method);
498 my $part_event = $cust_event->part_event;
499 my $template = $part_event->templatename
500 || $cust_X->agent_template;
501 my $modenum = $part_event->option('modenum') || '';
502 my $invoice_from = $part_event->option('agent_invoice_from') || '';
503 $cust_X->set('mode' => $modenum);
504 $cust_X->$method( { template => $template,
506 from => $invoice_from,
509 if ( $job ) { #progressbar foo
511 if ( time - $min_sec > $last ) {
512 my $error = $job->update_statustext(
513 int( 100 * $num / scalar(@cust_event) )
515 die $error if $error;
522 #this doesn't work, but it would be nice
523 #if ( $job ) { #progressbar foo
524 # my $error = $job->update_statustext(
525 # scalar(@cust_event). " invoices re-${method}ed"
527 # die $error if $error;
536 L<FS::part_event>, L<FS::cust_bill>, L<FS::Record>, schema.html from the