[freeside-commits] freeside/FS/FS msg_template.pm, 1.22, 1.23 part_event.pm, 1.4, 1.5 svc_Common.pm, 1.62, 1.63

Mark Wells mark at wavetail.420.am
Wed Aug 31 22:13:10 PDT 2011


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv1936/FS/FS

Modified Files:
	msg_template.pm part_event.pm svc_Common.pm 
Log Message:
svc_acct events for usage limits, #13202

Index: svc_Common.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/svc_Common.pm,v
retrieving revision 1.62
retrieving revision 1.63
diff -u -w -d -r1.62 -r1.63
--- svc_Common.pm	14 Feb 2011 21:43:01 -0000	1.62
+++ svc_Common.pm	1 Sep 2011 05:13:08 -0000	1.63
@@ -159,6 +159,16 @@
   $self->label(@_);
 }
 
+sub cust_main {
+  my $self = shift;
+  (($self->cust_svc || return)->cust_pkg || return)->cust_main || return
+}
+
+sub cust_linked {
+  my $self = shift;
+  defined($self->cust_main);
+}
+
 =item check
 
 Checks the validity of fields in this record.

Index: msg_template.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/msg_template.pm,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -w -d -r1.22 -r1.23
--- msg_template.pm	28 Jul 2011 22:01:46 -0000	1.22
+++ msg_template.pm	1 Sep 2011 05:13:08 -0000	1.23
@@ -430,6 +430,20 @@
 # helper sub for package dates
 my $ymd = sub { $_[0] ? time2str('%Y-%m-%d', $_[0]) : '' };
 
+# helper sub for usage-related messages
+my $usage_warning = sub {
+  my $svc = shift;
+  foreach my $col (qw(seconds upbytes downbytes totalbytes)) {
+    my $amount = $svc->$col; next if $amount eq '';
+    my $method = $col.'_threshold';
+    my $threshold = $svc->$method; next if $threshold eq '';
+    return [$col, $amount, $threshold] if $amount <= $threshold;
+    # this only returns the first one that's below threshold, if there are 
+    # several.
+  }
+  return ['', '', ''];
+};
+
 #my $conf = new FS::Conf;
 
 #return contexts and fill-in values
@@ -514,6 +528,9 @@
       domain
       ),
       [ password          => sub { shift->getfield('_password') } ],
+      [ column            => sub { &$usage_warning(shift)->[0] } ],
+      [ amount            => sub { &$usage_warning(shift)->[1] } ],
+      [ threshold         => sub { &$usage_warning(shift)->[2] } ],
     ],
     'svc_domain' => [qw(
       svcnum

Index: part_event.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_event.pm,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -w -d -r1.4 -r1.5
--- part_event.pm	20 Aug 2009 04:03:34 -0000	1.4
+++ part_event.pm	1 Sep 2011 05:13:08 -0000	1.5
@@ -52,7 +52,7 @@
 
 =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) (or "cust_statement")
+=item eventtable - table name against which this event is triggered: one of "cust_main", "cust_bill", "cust_statement", "cust_pkg", "svc_acct".
 
 =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.
 
@@ -178,14 +178,16 @@
   qsearch( 'part_event_condition', { 'eventpart' => $self->eventpart } );
 }
 
-=item new_cust_event OBJECT
+=item new_cust_event OBJECT, [ OPTION => VALUE ]
 
 Creates a new customer event (see L<FS::cust_event>) for the provided object.
 
+The only option allowed is 'time', to set the "current" time for the event.
+
 =cut
 
 sub new_cust_event {
-  my( $self, $object ) = @_;
+  my( $self, $object, %opt ) = @_;
 
   confess "**** $object is not a ". $self->eventtable
     if ref($object) ne "FS::". $self->eventtable;
@@ -195,7 +197,8 @@
   new FS::cust_event {
     'eventpart' => $self->eventpart,
     'tablenum'  => $object->$pkey(),
-    '_date'     => time, #i think we always want the real "now" here.
+    #'_date'     => time, #i think we always want the real "now" here.
+    '_date'     => ($opt{'time'} || time),
     'status'    => 'new',
   };
 }
@@ -252,6 +255,90 @@
   }
 }
 
+=item initialize PARAMS
+
+Identify all objects eligible for this event and create L<FS::cust_event>
+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<FS::part_event> object needs to be initialized, it should be created 
+in a disabled state to avoid running the event prematurely for any existing 
+objects.  C<initialize> 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
@@ -274,6 +361,7 @@
     'cust_main'      => 'Customer',
     'cust_pay_batch' => 'Batch payment',
     'cust_statement' => 'Statement',  #too general a name here? "Invoice group"?
+    'svc_acct'       => 'Login service',
   ;
 
   \%hash
@@ -312,6 +400,7 @@
     'cust_pkg'       => 'pkgnum',
     'cust_pay_batch' => 'paybatchnum',
     'cust_statement' => 'statementnum',
+    'svc_acct'       => 'svcnum',
   };
 }
 
@@ -337,6 +426,36 @@
   shift->eventtables; #same for now
 }
 
+=item eventtables_cust_join
+
+Returns a hash reference of SQL expressions to join each eventtable to 
+a table with a 'custnum' field.
+
+=cut
+
+sub eventtables_cust_join {
+  my %hash = (
+    'svc_acct' => 'LEFT JOIN cust_svc USING (svcnum) LEFT JOIN cust_pkg USING (pkgnum)',
+  );
+  \%hash;
+}
+
+=item eventtables_custnum
+
+Returns a hash reference of SQL expressions for the 'custnum' field when 
+I<eventtables_cust_join> is in effect.  The default is "$eventtable.custnum".
+
+=cut
+
+sub eventtables_custnum {
+  my %hash = (
+    map({ $_, "$_.custnum" } shift->eventtables),
+    'svc_acct' => 'cust_pkg.custnum'
+  );
+  \%hash;
+}
+
+
 =item check_freq_labels
 
 Returns a hash reference of labels for check_freq values,
@@ -376,6 +495,35 @@
 
 =back
 
+=head1 ADDING NEW EVENTTABLES
+
+To add an eventtable, you must:
+
+=over 4
+
+=item Add the table to "eventtable_labels" (with a label) and to 
+"eventtable_pkey" (with its primary key).
+
+=item If the table doesn't have a "custnum" field of its own (such 
+as a svc_x table), add a suitable join expression to 
+eventtables_cust_join and an expression for the final custnum field 
+to eventtables_custnum.
+
+=item Create a method named FS::cust_main->$eventtable(): a wrapper 
+around qsearch() to return all records in the new table belonging to 
+the cust_main object.  This method must accept 'addl_from' and 
+'extra_sql' arguments in the way qsearch() does.  For svc_ tables, 
+wrap the svc_x() method.
+
+=item Add it to FS::cust_event->join_sql and search_sql_where so that 
+search/cust_event.html will find it.
+
+=item Create a UI link/form to search for events linked to objects 
+in the new eventtable, using search/cust_event.html.  Place this 
+somewhere appropriate to the eventtable.
+
+=back
+
 See L<FS::part_event::Action> for more information.
 
 =cut
@@ -430,12 +578,28 @@
        keys %actions
 }
 
+=item process_initialize 'eventpart' => EVENTPART
+
+Job queue wrapper for "initialize".  EVENTPART identifies the 
+L<FS::part_event> 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";
+  $part_event->initialize;
+}
+
 =back
 
 =head1 SEE ALSO
 
 L<FS::part_event_option>, L<FS::part_event_condition>, L<FS::cust_main>,
-L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_bill_event>, L<FS::Record>,
+L<FS::cust_pkg>, L<FS::svc_acct>, L<FS::cust_bill>, L<FS::cust_bill_event>, 
+L<FS::Record>,
 schema.html from the base documentation.
 
 =cut



More information about the freeside-commits mailing list