1 package FS::cust_event;
4 use vars qw( @ISA $DEBUG );
5 use Carp qw( croak confess );
6 use FS::Record qw( qsearch qsearchs dbdef );
7 use FS::cust_main_Mixin;
14 @ISA = qw(FS::cust_main_Mixin FS::Record);
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)
65 Creates a new completed invoice event. To add the compelted invoice event to
66 the database, see L<"insert">.
68 Note that this stores the hash reference, not a distinct copy of the hash it
69 points to. You can ask the object for a copy with the I<hash> method.
73 # the new method can be inherited from FS::Record, if a table method is defined
75 sub table { 'cust_event'; }
77 sub cust_linked { $_[0]->cust_main_custnum; }
78 sub cust_unlinked_msg {
80 "WARNING: can't find cust_main.custnum ". $self->custnum;
81 #' (cust_bill.invnum '. $self->invnum. ')';
85 $self->cust_main_custnum(@_) || $self->SUPER::custnum(@_);
90 Adds this record to the database. If there is an error, returns the error,
91 otherwise returns false.
95 # the insert method can be inherited from FS::Record
99 Delete this record from the database.
103 # the delete method can be inherited from FS::Record
105 =item replace OLD_RECORD
107 Replaces the OLD_RECORD with this one in the database. If there is an error,
108 returns the error, otherwise returns false.
112 # the replace method can be inherited from FS::Record
116 Checks all fields to make sure this is a valid completed invoice event. If
117 there is an error, returns the error, otherwise returns false. Called by the
118 insert and replace methods.
122 # the check method should currently be supplied - FS::Record contains some
123 # data checking routines
128 my $error = $self->ut_numbern('eventnum')
129 || $self->ut_foreign_key('eventpart', 'part_event', 'eventpart')
131 return $error if $error;
133 my $eventtable = $self->part_event->eventtable;
134 my $dbdef_eventtable = dbdef->table( $eventtable );
137 $self->ut_foreign_key( 'tablenum',
139 $dbdef_eventtable->primary_key
141 || $self->ut_number('_date')
142 || $self->ut_enum('status', [qw( new locked done failed )])
143 || $self->ut_anything('statustext')
145 return $error if $error;
152 Returns the event definition (see L<FS::part_event>) for this completed event.
158 qsearchs( 'part_event', { 'eventpart' => $self->eventpart } );
163 Returns the customer, package, invoice or batched payment (see
164 L<FS::cust_main>, L<FS::cust_pkg>, L<FS::cust_bill> or L<FS::cust_pay_batch>)
165 for this completed invoice event.
170 croak "FS::cust_event::cust_bill called";
175 my $eventtable = $self->part_event->eventtable;
176 my $dbdef_table = dbdef->table( $eventtable );
177 my $primary_key = $dbdef_table->primary_key;
178 qsearchs( $eventtable, { $primary_key => $self->tablenum } );
181 =item test_conditions [ OPTION => VALUE ... ]
183 Tests conditions for this event, returns true if all conditions are satisfied,
188 sub test_conditions {
189 my( $self, %opt ) = @_;
190 my $part_event = $self->part_event;
191 my $object = $self->cust_X;
192 my @conditions = $part_event->part_event_condition;
193 $opt{'cust_event'} = $self;
195 #no unsatisfied conditions
196 #! grep ! $_->condition( $object, %opt ), @conditions;
197 my @unsatisfied = grep ! $_->condition( $object, %opt ), @conditions;
199 if ( $opt{'stats_hashref'} ) {
200 foreach my $unsat (@unsatisfied) {
201 $opt{'stats_hashref'}->{$unsat->conditionname}++;
210 Runs the event action.
217 my $part_event = $self->part_event;
219 my $object = $self->cust_X;
220 my $obj_pkey = $object->primary_key;
221 my $for = "for ". $object->table. " ". $object->$obj_pkey();
222 warn "running cust_event ". $self->eventnum.
223 " (". $part_event->action. ") $for\n"
226 my $oldAutoCommit = $FS::UID::AutoCommit;
227 local $FS::UID::AutoCommit = 0;
231 local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
232 $error = eval { $part_event->do_action($object); };
240 $statustext = "Error running ". $part_event->action. " action: $@";
243 $statustext = $error;
248 #replace or add myself
250 $self->status($status);
251 $self->statustext($statustext);
253 $error = $self->eventnum ? $self->replace : $self->insert;
255 #this is why we need that locked state...
256 my $e = 'WARNING: Event run but database not updated - '.
257 'error replacing or inserting cust_event '. $self->eventnum.
269 Changes the status of this event from B<done> to B<failed>, allowing it to be
276 return '' unless $self->status eq 'done';
277 my $old = ref($self)->new( { $self->hash } );
278 $self->status('failed');
279 $self->replace($old);
284 #Changes the statustext of this event to B<retriable>, rendering it
285 #retriable (should retry be called).
290 confess "cust_event->retriable called";
292 return '' unless $self->status eq 'done';
293 my $old = ref($self)->new( { $self->hash } );
294 $self->statustext('retriable');
295 $self->replace($old);
308 sub process_reprint {
309 process_re_X('print', @_);
316 sub process_reemail {
317 process_re_X('email', @_);
325 process_re_X('fax', @_);
328 use Storable qw(thaw);
332 my( $method, $job ) = ( shift, shift );
334 my $param = thaw(decode_base64(shift));
335 warn Dumper($param) if $DEBUG;
339 $param->{'beginning'},
348 my($method, $beginning, $ending, $failed, $job) = @_;
350 my $from = 'LEFT JOIN part_event USING ( eventpart )';
352 # yuck! hardcoed *AND* sequential scans!
353 my $where = " WHERE action LIKE 'cust_bill_send%'".
354 " AND cust_event._date >= $beginning".
355 " AND cust_event._date <= $ending";
356 $where .= " AND statustext != '' AND statustext IS NOT NULL"
359 my @cust_event = qsearch({
360 'table' => 'cust_event',
361 'addl_from' => $from,
363 'extra_sql' => $where,
366 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
367 foreach my $cust_event ( @cust_event ) {
370 $cust_event->cust_bill->$method(
371 $cust_event->part_event->templatename
372 || $cust_event->cust_main->agent_template
375 if ( $job ) { #progressbar foo
377 if ( time - $min_sec > $last ) {
378 my $error = $job->update_statustext(
379 int( 100 * $num / scalar(@cust_event) )
381 die $error if $error;
388 #this doesn't work, but it would be nice
389 #if ( $job ) { #progressbar foo
390 # my $error = $job->update_statustext(
391 # scalar(@cust_event). " invoices re-${method}ed"
393 # die $error if $error;
402 L<FS::part_event>, L<FS::cust_bill>, L<FS::Record>, schema.html from the