X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fpart_event.pm;h=1c2389989d2d7cf3ba16c7c4b1f68084ad329c61;hp=dfbb6a5d35355520a87aaa60905406a20000bb6e;hb=8282a324857d658d17061e3f0867d8c7d71b098a;hpb=2413a3d43808ea6567bf25215d810bf102c498fd diff --git a/FS/FS/part_event.pm b/FS/FS/part_event.pm index dfbb6a5d3..1c2389989 100644 --- a/FS/FS/part_event.pm +++ b/FS/FS/part_event.pm @@ -1,15 +1,15 @@ package FS::part_event; +use base qw( FS::m2name_Common FS::option_Common ); use strict; -use base qw( FS::m2name_Common FS::option_Common ); use vars qw( $DEBUG ); use Carp qw(confess); use FS::Record qw( dbh qsearch qsearchs ); use FS::Conf; +use FS::Cursor; use FS::part_event_option; use FS::part_event_condition; use FS::cust_event; -use FS::agent; $DEBUG = 0; @@ -169,13 +169,6 @@ sub _rebless { Returns the conditions associated with this event, as FS::part_event_condition objects (see L) -=cut - -sub part_event_condition { - my $self = shift; - qsearch( 'part_event_condition', { 'eventpart' => $self->eventpart } ); -} - =item new_cust_event OBJECT, [ OPTION => VALUE ] Creates a new customer event (see L) for the provided object. @@ -223,13 +216,6 @@ sub reasontext { confess "part_event->reasontext deprecated"; } Returns the associated agent for this event, if any, as an FS::agent object. -=cut - -sub agent { - my $self = shift; - qsearchs('agent', { 'agentnum' => $self->agentnum } ); -} - =item templatename Returns the alternate invoice template name, if any, or false if there is @@ -253,30 +239,41 @@ sub templatename { } } -=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. +=item targets OPTIONS -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. +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 may take some time, so it should be run from the job queue. +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 initialize { +sub targets { # may want to cursor this also my $self = shift; - my $time = time; # $opt{'time'}? + my %opt = @_; + my $time = $opt{'time'} ||= time; + + my $query = $self->_target_query(%opt); + my @objects = qsearch($query); + my @tested_objects; + foreach my $object ( @objects ) { + my $cust_event = $self->new_cust_event($object, 'time' => $time); + next unless $cust_event->test_conditions; - my $oldAutoCommit = $FS::UID::AutoCommit; - local $FS::UID::AutoCommit = 0; - my $dbh = dbh; + $object->set('cust_event', $cust_event); + push @tested_objects, $object; + } + @tested_objects; +} + +sub _target_query { + my $self = shift; + my %opt = @_; + my $time = $opt{'time'}; my $eventpart = $self->eventpart; $eventpart =~ /^\d+$/ or die "bad eventpart $eventpart"; @@ -286,51 +283,77 @@ sub initialize { 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) ' + $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 $join = FS::part_event_condition->join_conditions_sql( $eventtable, + 'time' => $time + ); my $where = FS::part_event_condition->where_conditions_sql( $eventtable, 'time' => $time ); - $join = $linkage . + $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({ + # 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 + + { 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 + }; +} + + +=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 $error; + + my $time = time; + + local $FS::UID::AutoCommit = 1; + my $cursor = FS::Cursor->new( $self->_target_query('time' => $time) ); + while (my $object = $cursor->fetch) { + 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; + die $error if $error; } - if ( !$error and $self->disabled ) { + + # on successful completion only, re-enable the event + if ( $self->disabled ) { $self->disabled(''); $error = $self->replace; + die $error if $error; } - if ( $error ) { - $dbh->rollback; - return $error; - } - $dbh->commit if $oldAutoCommit; return; } @@ -357,9 +380,10 @@ sub eventtable_labels { 'cust_pkg' => 'Package', 'cust_bill' => 'Invoice', 'cust_main' => 'Customer', + 'cust_pay' => 'Payment', 'cust_pay_batch' => 'Batch payment', 'cust_statement' => 'Statement', #too general a name here? "Invoice group"? - 'svc_acct' => 'Login service', + 'svc_acct' => 'Account service (svc_acct)', ; \%hash @@ -396,6 +420,7 @@ sub eventtable_pkey { 'cust_main' => 'custnum', 'cust_bill' => 'invnum', 'cust_pkg' => 'pkgnum', + 'cust_pay' => 'paynum', 'cust_pay_batch' => 'paybatchnum', 'cust_statement' => 'statementnum', 'svc_acct' => 'svcnum', @@ -558,6 +583,7 @@ sub actions { ( map { $_ => $actions{$_} } sort { $actions{$a}->{'default_weight'}<=>$actions{$b}->{'default_weight'} } + # || $actions{$a}->{'description'} cmp $actions{$b}->{'description'} } $class->all_actions( $eventtable ) );