X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_event.pm;h=efe09bd4c99e182b687ad9fca4320cd64abd1319;hb=280522d98836e3bd1ce8e658cf0aa8432fc8efba;hp=d0ab65e3f0f42315d05a33544143b6d49b01e14c;hpb=5e05724a635a22776f1b973f5d7e77989da4e048;p=freeside.git diff --git a/FS/FS/part_event.pm b/FS/FS/part_event.pm index d0ab65e3f..efe09bd4c 100644 --- a/FS/FS/part_event.pm +++ b/FS/FS/part_event.pm @@ -52,7 +52,7 @@ following fields are currently supported: =item event - event name -=item eventtable - table name against which this event is triggered; currently "cust_bill" (the traditional invoice events), "cust_main" (customer events) or "cust_pkg (package events) +=item eventtable - table name against which this event is triggered; currently "cust_bill" (the traditional invoice events), "cust_main" (customer events) or "cust_pkg (package events) (or "cust_statement") =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. @@ -133,7 +133,7 @@ sub check { my $error = $self->ut_numbern('eventpart') || $self->ut_text('event') - || $self->ut_enum('eventtable', [ 'cust_bill', 'cust_main', 'cust_pkg' ] ) + || $self->ut_enum('eventtable', [ $self->eventtables ] ) || $self->ut_enum('check_freq', [ '1d', '1m' ]) || $self->ut_number('weight') || $self->ut_alpha('action') @@ -252,6 +252,152 @@ sub templatename { } } +=item targets OPTIONS + +Returns all objects (of type C, for this object's +C) eligible for processing under this event, as of right now. +The L object used to test event conditions will be +included in each object as the 'cust_event' pseudo-field. + +This is not used in normal event processing (which is done on a +per-customer basis to control timing of pre- and post-billing events) +but can be useful when configuring events. + +=cut + +sub targets { + my $self = shift; + my %opt = @_; + my $time = $opt{'time'} || time; + + my $eventpart = $self->eventpart; + $eventpart =~ /^\d+$/ or die "bad eventpart $eventpart"; + my $eventtable = $self->eventtable; + + # find all objects that meet the conditions for this part_event + my $linkage = ''; + # this is the 'object' side of the FROM clause + if ( $eventtable ne 'cust_main' ) { + $linkage = + #($self->eventtables_cust_join->{$eventtable} || '') . #2.3 + ' LEFT JOIN cust_main USING (custnum) '; + } + + # this is the 'event' side + my $join = FS::part_event_condition->join_conditions_sql( $eventtable ); + my $where = FS::part_event_condition->where_conditions_sql( $eventtable, + 'time' => $time + ); + $join = $linkage . + " INNER JOIN part_event ON ( part_event.eventpart = $eventpart ) $join"; + + $where .= ' AND cust_main.agentnum = '.$self->agentnum + if $self->agentnum; + # don't enforce check_freq since this is a special, out-of-order check + # and don't enforce disabled because we want to be able to see targets + # for a disabled event + + my @objects = qsearch({ + table => $eventtable, + hashref => {}, + addl_from => $join, + extra_sql => "WHERE $where", + }); + my @tested_objects; + foreach my $object ( @objects ) { + my $cust_event = $self->new_cust_event($object); + next unless $cust_event->test_conditions('time' => $time); + + $object->set('cust_event', $cust_event); + push @tested_objects, $object; + } + @tested_objects; +} + +=item initialize PARAMS + +Identify all objects eligible for this event and create L +records for each of them, as of the present time, with status "initial". When +combined with conditions that prevent an event from running more than once +(at all or within some period), this will exclude any objects that met the +conditions before the event was created. + +If an L object needs to be initialized, it should be created +in a disabled state to avoid running the event prematurely for any existing +objects. C will enable it once all the cust_event records +have been created. + +This may take some time, so it should be run from the job queue. + +=cut + +sub initialize { + my $self = shift; + my $time = time; # $opt{'time'}? + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + my $eventpart = $self->eventpart; + $eventpart =~ /^\d+$/ or die "bad eventpart $eventpart"; + my $eventtable = $self->eventtable; + + # find all objects that meet the conditions for this part_event + my $linkage = ''; + # this is the 'object' side of the FROM clause + if ( $eventtable ne 'cust_main' ) { + $linkage = #($self->eventtables_cust_join->{$eventtable} || '') . + ' LEFT JOIN cust_main USING (custnum) ' + } + + # this is the 'event' side + my $join = FS::part_event_condition->join_conditions_sql( $eventtable ); + my $where = FS::part_event_condition->where_conditions_sql( $eventtable, + 'time' => $time + ); + $join = $linkage . + " INNER JOIN part_event ON ( part_event.eventpart = $eventpart ) $join"; + + $where .= ' AND cust_main.agentnum = '.$self->agentnum + if $self->agentnum; + # don't enforce check_freq since this is a special, out-of-order check, + # and don't enforce disabled because we want to do this with the part_event + # disabled. + my @objects = qsearch({ + table => $eventtable, + hashref => {}, + addl_from => $join, + extra_sql => "WHERE $where", + debug => 1, + }); + warn "initialize: ".(scalar @objects) ." $eventtable objects found\n" + if $DEBUG; + my $error = ''; + foreach my $object ( @objects ) { + # test conditions + my $cust_event = $self->new_cust_event($object, 'time' => $time); + next unless $cust_event->test_conditions; + + $cust_event->status('initial'); + $error = $cust_event->insert; + last if $error; + } + if ( !$error and $self->disabled ) { + $self->disabled(''); + $error = $self->replace; + } + if ( $error ) { + $dbh->rollback; + return $error; + } + $dbh->commit if $oldAutoCommit; + return; +} + +=cut + + =back =head1 CLASS METHODS @@ -273,6 +419,7 @@ sub eventtable_labels { 'cust_bill' => 'Invoice', 'cust_main' => 'Customer', 'cust_pay_batch' => 'Batch payment', + 'cust_statement' => 'Statement', #too general a name here? "Invoice group"? ; \%hash @@ -286,18 +433,33 @@ i.e. 'cust_main'=>'cust_main.custnum' =cut sub eventtable_pkey_sql { - #my $class = shift; + my $class = shift; - my %hash = ( - 'cust_main' => 'cust_main.custnum', - 'cust_bill' => 'cust_bill.invnum', - 'cust_pkg' => 'cust_pkg.pkgnum', - 'cust_pay_batch' => 'cust_pay_batch.paybatchnum', - ); + my $hashref = $class->eventtable_pkey; + + my %hash = map { $_ => "$_.". $hashref->{$_} } keys %$hashref; \%hash; } +=item eventtable_pkey + +Returns a hash reference of full SQL primary key names for eventtable values, +i.e. 'cust_main'=>'custnum' + +=cut + +sub eventtable_pkey { + #my $class = shift; + + { + 'cust_main' => 'custnum', + 'cust_bill' => 'invnum', + 'cust_pkg' => 'pkgnum', + 'cust_pay_batch' => 'paybatchnum', + 'cust_statement' => 'statementnum', + }; +} =item eventtables @@ -414,12 +576,30 @@ sub all_actions { keys %actions } +=item process_initialize 'eventpart' => EVENTPART + +Job queue wrapper for "initialize". EVENTPART identifies the +L object to initialize. + +=cut + +sub process_initialize { + my %opt = @_; + my $part_event = + qsearchs('part_event', { eventpart => $opt{'eventpart'}}) + or die "eventpart '$opt{eventpart}' not found!\n"; + my $error = $part_event->initialize; + die $error if $error; + ''; +} + =back =head1 SEE ALSO L, L, L, -L, L, L, L, +L, L, L, L, +L, schema.html from the base documentation. =cut