1 package FS::part_event;
4 use base qw( FS::m2name_Common FS::option_Common );
7 use FS::Record qw( dbh qsearch qsearchs );
10 use FS::part_event_option;
11 use FS::part_event_condition;
19 FS::part_event - Object methods for part_event records
25 $record = new FS::part_event \%hash;
26 $record = new FS::part_event { 'column' => 'value' };
28 $error = $record->insert( { 'option' => 'value' } );
29 $error = $record->insert( \%options );
31 $error = $new_record->replace($old_record);
33 $error = $record->delete;
35 $error = $record->check;
37 $error = $record->do_event( $direct_object );
41 An FS::part_event object represents an event definition - a billing, collection
42 or other callback which is triggered when certain customer, invoice, package or
43 other conditions are met. FS::part_event inherits from FS::Record. The
44 following fields are currently supported:
48 =item eventpart - primary key
50 =item agentnum - Optional agentnum (see L<FS::agent>)
52 =item event - event name
54 =item eventtable - table name against which this event is triggered: one of "cust_main", "cust_bill", "cust_statement", "cust_pkg", "svc_acct".
56 =item check_freq - how often events of this type are checked; currently "1d" (daily) and "1m" (monthly) are recognized. Note that the apprioriate freeside-daily and/or freeside-monthly cron job needs to be in place.
58 =item weight - ordering for events
60 =item action - event action (like part_bill_event.plan - eventcode plan)
62 =item disabled - Disabled flag, empty or `Y'
72 Creates a new invoice event definition. To add the invoice event definition 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 { 'part_event'; }
84 =item insert [ HASHREF ]
86 Adds this record to the database. If there is an error, returns the error,
87 otherwise returns false.
89 If a list or hash reference of options is supplied, part_export_option records
90 are created (see L<FS::part_event_option>).
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 [ HASHREF | OPTION => VALUE ... ]
106 Replaces the OLD_RECORD with this one in the database. If there is an error,
107 returns the error, otherwise returns false.
109 If a list or hash reference of options is supplied, part_event_option
110 records are created or modified (see L<FS::part_event_option>).
114 # the replace method can be inherited from FS::Record
118 Checks all fields to make sure this is a valid invoice event definition. If
119 there is an error, returns the error, otherwise returns false. Called by the
120 insert and replace methods.
124 # the check method should currently be supplied - FS::Record contains some
125 # data checking routines
130 $self->weight(0) unless $self->weight;
133 $self->ut_numbern('eventpart')
134 || $self->ut_text('event')
135 || $self->ut_enum('eventtable', [ $self->eventtables ] )
136 || $self->ut_enum('check_freq', [ '1d', '1m' ])
137 || $self->ut_number('weight')
138 || $self->ut_alpha('action')
139 || $self->ut_enum('disabled', [ '', 'Y' ] )
140 || $self->ut_agentnum_acl('agentnum', 'Edit global billing events')
142 return $error if $error;
144 #XXX check action to make sure a module exists?
145 # well it'll die in _rebless...
152 Reblesses the object into the FS::part_event::Action::ACTION class, where
153 ACTION is the object's I<action> field.
159 my $action = $self->action or return $self;
160 #my $class = ref($self). "::$action";
161 my $class = "FS::part_event::Action::$action";
164 bless($self, $class); # unless $@;
168 =item part_event_condition
170 Returns the conditions associated with this event, as FS::part_event_condition
171 objects (see L<FS::part_event_condition>)
175 sub part_event_condition {
177 qsearch( 'part_event_condition', { 'eventpart' => $self->eventpart } );
180 =item new_cust_event OBJECT, [ OPTION => VALUE ]
182 Creates a new customer event (see L<FS::cust_event>) for the provided object.
184 The only option allowed is 'time', to set the "current" time for the event.
189 my( $self, $object, %opt ) = @_;
191 confess "**** $object is not a ". $self->eventtable
192 if ref($object) ne "FS::". $self->eventtable;
194 my $pkey = $object->primary_key;
197 'eventpart' => $self->eventpart,
198 'tablenum' => $object->$pkey(),
199 #'_date' => time, #i think we always want the real "now" here.
200 '_date' => ($opt{'time'} || time),
205 #surely this doesn't work
206 sub reasontext { confess "part_event->reasontext deprecated"; }
209 #Returns the text of any reason associated with this event.
215 # my $r = qsearchs('reason', { 'reasonnum' => $self->reason });
225 Returns the associated agent for this event, if any, as an FS::agent object.
231 qsearchs('agent', { 'agentnum' => $self->agentnum } );
236 Returns the alternate invoice template name, if any, or false if there is
237 no alternate template for this event.
244 if ( $self->action =~ /^cust_bill_send_(alternate|agent)$/
245 && ( $self->option('agent_templatename')
246 || $self->option('templatename') )
249 $self->option('agent_templatename')
250 || $self->option('templatename');
257 =item targets OPTIONS
259 Returns all objects (of type C<FS::eventtable>, for this object's
260 C<eventtable>) eligible for processing under this event, as of right now.
261 The L<FS::cust_event> object used to test event conditions will be
262 included in each object as the 'cust_event' pseudo-field.
264 This is not used in normal event processing (which is done on a
265 per-customer basis to control timing of pre- and post-billing events)
266 but can be useful when configuring events.
270 sub targets { # may want to cursor this also
273 my $time = $opt{'time'} ||= time;
275 my $query = $self->_target_query(%opt);
276 my @objects = qsearch($query);
278 foreach my $object ( @objects ) {
279 my $cust_event = $self->new_cust_event($object, 'time' => $time);
280 next unless $cust_event->test_conditions;
282 $object->set('cust_event', $cust_event);
283 push @tested_objects, $object;
291 my $time = $opt{'time'};
293 my $eventpart = $self->eventpart;
294 $eventpart =~ /^\d+$/ or die "bad eventpart $eventpart";
295 my $eventtable = $self->eventtable;
297 # find all objects that meet the conditions for this part_event
299 # this is the 'object' side of the FROM clause
300 if ( $eventtable ne 'cust_main' ) {
302 ($self->eventtables_cust_join->{$eventtable} || '') .
303 ' LEFT JOIN cust_main USING (custnum) ';
306 # this is the 'event' side
307 my $join = FS::part_event_condition->join_conditions_sql( $eventtable,
310 my $where = FS::part_event_condition->where_conditions_sql( $eventtable,
314 " INNER JOIN part_event ON ( part_event.eventpart = $eventpart ) $join";
316 $where .= ' AND cust_main.agentnum = '.$self->agentnum
318 # don't enforce check_freq since this is a special, out-of-order check
319 # and don't enforce disabled because we want to be able to see targets
320 # for a disabled event
323 table => $eventtable,
326 extra_sql => "WHERE $where",
331 =item initialize PARAMS
333 Identify all objects eligible for this event and create L<FS::cust_event>
334 records for each of them, as of the present time, with status "initial". When
335 combined with conditions that prevent an event from running more than once
336 (at all or within some period), this will exclude any objects that met the
337 conditions before the event was created.
339 If an L<FS::part_event> object needs to be initialized, it should be created
340 in a disabled state to avoid running the event prematurely for any existing
341 objects. C<initialize> will enable it once all the cust_event records
344 This may take some time, so it should be run from the job queue.
354 local $FS::UID::AutoCommit = 1;
355 my $cursor = FS::Cursor->new( $self->_target_query('time' => $time) );
356 while (my $object = $cursor->fetch) {
358 my $cust_event = $self->new_cust_event($object, 'time' => $time);
359 next unless $cust_event->test_conditions;
361 $cust_event->status('initial');
362 $error = $cust_event->insert;
363 die $error if $error;
366 # on successful completion only, re-enable the event
367 if ( $self->disabled ) {
369 $error = $self->replace;
370 die $error if $error;
384 =item eventtable_labels
386 Returns a hash reference of labels for eventtable values,
387 i.e. 'cust_main'=>'Customer'
391 sub eventtable_labels {
394 tie my %hash, 'Tie::IxHash',
395 'cust_pkg' => 'Package',
396 'cust_bill' => 'Invoice',
397 'cust_main' => 'Customer',
398 'cust_pay' => 'Payment',
399 'cust_pay_batch' => 'Batch payment',
400 'cust_statement' => 'Statement', #too general a name here? "Invoice group"?
401 'svc_acct' => 'Account service (svc_acct)',
407 =item eventtable_pkey_sql
409 Returns a hash reference of full SQL primary key names for eventtable values,
410 i.e. 'cust_main'=>'cust_main.custnum'
414 sub eventtable_pkey_sql {
417 my $hashref = $class->eventtable_pkey;
419 my %hash = map { $_ => "$_.". $hashref->{$_} } keys %$hashref;
424 =item eventtable_pkey
426 Returns a hash reference of full SQL primary key names for eventtable values,
427 i.e. 'cust_main'=>'custnum'
431 sub eventtable_pkey {
435 'cust_main' => 'custnum',
436 'cust_bill' => 'invnum',
437 'cust_pkg' => 'pkgnum',
438 'cust_pay' => 'paynum',
439 'cust_pay_batch' => 'paybatchnum',
440 'cust_statement' => 'statementnum',
441 'svc_acct' => 'svcnum',
447 Returns a list of eventtable values (default ordering; suited for display).
453 my $eventtables = $class->eventtable_labels;
457 =item eventtables_runorder
459 Returns a list of eventtable values (run order).
463 sub eventtables_runorder {
464 shift->eventtables; #same for now
467 =item eventtables_cust_join
469 Returns a hash reference of SQL expressions to join each eventtable to
470 a table with a 'custnum' field.
474 sub eventtables_cust_join {
476 'svc_acct' => 'LEFT JOIN cust_svc USING (svcnum) LEFT JOIN cust_pkg USING (pkgnum)',
481 =item eventtables_custnum
483 Returns a hash reference of SQL expressions for the 'custnum' field when
484 I<eventtables_cust_join> is in effect. The default is "$eventtable.custnum".
488 sub eventtables_custnum {
490 map({ $_, "$_.custnum" } shift->eventtables),
491 'svc_acct' => 'cust_pkg.custnum'
497 =item check_freq_labels
499 Returns a hash reference of labels for check_freq values,
504 sub check_freq_labels {
514 =item actions [ EVENTTABLE ]
516 Return information about the available actions. If an eventtable is specified,
517 only return information about actions available for that eventtable.
519 Information is returned as key-value pairs. Keys are event names. Values are
520 hashrefs with the following keys:
526 =item eventtable_hashref
536 =head1 ADDING NEW EVENTTABLES
538 To add an eventtable, you must:
542 =item Add the table to "eventtable_labels" (with a label) and to
543 "eventtable_pkey" (with its primary key).
545 =item If the table doesn't have a "custnum" field of its own (such
546 as a svc_x table), add a suitable join expression to
547 eventtables_cust_join and an expression for the final custnum field
548 to eventtables_custnum.
550 =item Create a method named FS::cust_main->$eventtable(): a wrapper
551 around qsearch() to return all records in the new table belonging to
552 the cust_main object. This method must accept 'addl_from' and
553 'extra_sql' arguments in the way qsearch() does. For svc_ tables,
554 wrap the svc_x() method.
556 =item Add it to FS::cust_event->join_sql and search_sql_where so that
557 search/cust_event.html will find it.
559 =item Create a UI link/form to search for events linked to objects
560 in the new eventtable, using search/cust_event.html. Place this
561 somewhere appropriate to the eventtable.
565 See L<FS::part_event::Action> for more information.
569 #false laziness w/part_event_condition.pm
570 #some false laziness w/part_export & part_pkg
572 foreach my $INC ( @INC ) {
573 foreach my $file ( glob("$INC/FS/part_event/Action/*.pm") ) {
574 warn "attempting to load Action from $file\n" if $DEBUG;
575 $file =~ /\/(\w+)\.pm$/ or do {
576 warn "unrecognized file in $INC/FS/part_event/Action/: $file\n";
580 eval "use FS::part_event::Action::$mod;";
582 die "error using FS::part_event::Action::$mod (skipping): $@\n" if $@;
583 #warn "error using FS::part_event::Action::$mod (skipping): $@\n" if $@;
587 ( map { $_ => "FS::part_event::Action::$mod"->$_() }
588 qw( description eventtable_hashref default_weight deprecated )
589 #option_fields_hashref
591 'option_fields' => [ "FS::part_event::Action::$mod"->option_fields() ],
597 my( $class, $eventtable ) = @_;
599 map { $_ => $actions{$_} }
600 sort { $actions{$a}->{'default_weight'}<=>$actions{$b}->{'default_weight'} }
601 # || $actions{$a}->{'description'} cmp $actions{$b}->{'description'} }
602 $class->all_actions( $eventtable )
607 =item all_actions [ EVENTTABLE ]
609 Returns a list of just the action names
614 my ( $class, $eventtable ) = @_;
616 grep { !$eventtable || $actions{$_}->{'eventtable_hashref'}{$eventtable} }
620 =item process_initialize 'eventpart' => EVENTPART
622 Job queue wrapper for "initialize". EVENTPART identifies the
623 L<FS::part_event> object to initialize.
627 sub process_initialize {
630 qsearchs('part_event', { eventpart => $opt{'eventpart'}})
631 or die "eventpart '$opt{eventpart}' not found!\n";
632 $part_event->initialize;
639 L<FS::part_event_option>, L<FS::part_event_condition>, L<FS::cust_main>,
640 L<FS::cust_pkg>, L<FS::svc_acct>, L<FS::cust_bill>, L<FS::cust_bill_event>,
642 schema.html from the base documentation.