"day-of-month of customer signup" condition, #16827
authorMark Wells <mark@freeside.biz>
Tue, 13 Mar 2012 03:24:22 +0000 (20:24 -0700)
committerMark Wells <mark@freeside.biz>
Tue, 13 Mar 2012 03:24:22 +0000 (20:24 -0700)
FS/FS/part_event.pm
FS/FS/part_event/Condition/signupdate_day.pm [new file with mode: 0644]
httemplate/browse/part_event.html
httemplate/view/part_event-targets.html

index 31d2afd..62f16fa 100644 (file)
@@ -253,7 +253,7 @@ sub templatename {
   }
 }
 
-=item targets
+=item targets OPTIONS
 
 Returns all objects (of type C<FS::eventtable>, for this object's 
 C<eventtable>) eligible for processing under this event, as of right now.
@@ -268,7 +268,8 @@ but can be useful when configuring events.
 
 sub targets {
   my $self = shift;
-  my $time = time; # $opt{'time'}?
+  my %opt = @_;
+  my $time = $opt{'time'} || time;
 
   my $eventpart = $self->eventpart;
   $eventpart =~ /^\d+$/ or die "bad eventpart $eventpart";
@@ -305,8 +306,8 @@ sub targets {
   });
   my @tested_objects;
   foreach my $object ( @objects ) {
-    my $cust_event = $self->new_cust_event($object, 'time' => $time);
-    next unless $cust_event->test_conditions;
+    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;
diff --git a/FS/FS/part_event/Condition/signupdate_day.pm b/FS/FS/part_event/Condition/signupdate_day.pm
new file mode 100644 (file)
index 0000000..dbe9e60
--- /dev/null
@@ -0,0 +1,54 @@
+package FS::part_event::Condition::signupdate_day;
+
+use strict;
+use Tie::IxHash;
+
+use base qw( FS::part_event::Condition );
+
+sub description {
+  "Customer signed up on the same day of month as today";
+}
+
+sub option_fields {
+  (
+    'delay' => { label  => 'Delay additional days',
+                 type   => 'text',
+                 value  => '0',
+               },
+  );
+}
+
+sub condition {
+  my( $self, $object, %opt ) = @_;
+
+  my $cust_main = $self->cust_main($object);
+
+  my ($today) = (localtime($opt{'time'}))[3];
+
+  my $delay = $self->option('delay') || 0;
+  my $signupday = ((localtime($cust_main->signupdate + $delay * 86400))[3] - 1)
+                   % 28 + 1;
+  
+  $today == $signupday;
+}
+
+sub condition_sql {
+  my( $class, $table, %opt ) = @_;
+  my $mday;
+  if ( $opt{'driver_name'} eq 'Pg' ) {
+    $mday = sub{ "EXTRACT( DAY FROM TO_TIMESTAMP($_[0]) )::INTEGER" };
+  }
+  elsif ( $opt{'driver_name'} eq 'mysql' ) {
+    $mday = sub{ "DAY( FROM_UNIXTIME($_[0]) )" };
+  }
+  else {
+    return 'true';
+  }
+
+  my $delay = $class->condition_sql_option_integer('delay', 
+    $opt{'driver_name'}); # returns 0 for null
+  $mday->($opt{'time'}) . ' = '.
+    '(' . $mday->("cust_main.signupdate + $delay * 86400") . ' - 1) % 28 + 1';
+}
+
+1;
index 6be2860..0399643 100644 (file)
@@ -45,7 +45,8 @@ my $link = [ $p.'edit/part_event.html?', 'eventpart' ];
 my $event_sub = sub {
   my $part_event = shift;
   my $onclick = include('/elements/popup_link_onclick.html',
-    action      => $p.'view/part_event-targets.html?'.$part_event->eventpart,
+    action      => $p.'view/part_event-targets.html?eventpart='.
+                    $part_event->eventpart,
     actionlabel => 'Event query - '.$part_event->event,
     width       => 650,
     height      => 420,
index c5faccf..2029fd4 100644 (file)
@@ -3,6 +3,16 @@
        'title'   => 'Event query - '.$part_event->event,
      }
 &>
+<FORM STYLE="display:inline" ACTION=<%$cgi->url%> METHOD="GET">
+When event is run on <& /elements/input-date-field.html, {
+  'name'    => 'date',
+  'value'   => $time,
+  'format'  => FS::Conf->new->config('date_format') || '%m/%d/%Y',
+} &>
+<INPUT TYPE="hidden" NAME="eventpart" VALUE="<%$eventpart%>">
+<INPUT TYPE="submit" VALUE="Refresh">
+</FORM>
+<BR><BR>
 % if ( $objects > 0 ) {
   <% emt("[quant,_1,$label]", $objects) %>
 %   if ( $part_event->eventtable ne 'cust_main' ) {
@@ -18,8 +28,8 @@
 
 %   my @rowcolors = ('ffffff','eeeeee');
 %   my $row = 0;
-  <TR style="background-color:#<% $rowcolors[$row++ % 2] %>">
 %   foreach my $object (@targets) {
+  <TR style="background-color:#<% $rowcolors[$row++ % 2] %>">
 %     # now works for all eventtables, including cust_pkg
 %     my $link = $p . 'view/' . $part_event->eventtable . '.cgi?' .
 %        $object->$pkey;
@@ -65,12 +75,14 @@ die "access denied"
   unless $curuser->access_right('Edit billing events')
         || $curuser->access_right('Edit global billing events');
 
-my ($eventpart) = $cgi->keywords;
+my ($eventpart) = $cgi->param('eventpart');
 $eventpart =~ /^\d+$/ or die 'illegal eventpart';
 
+my $time = parse_datetime($cgi->param('date')) || time;
+
 my $part_event = FS::part_event->by_key($eventpart)
   or die "Event definition $eventpart not found.\n";
-my @targets = $part_event->targets;
+my @targets = $part_event->targets('time' => $time);
 my $total = @targets;
 
 # in imitation of search/elements/search-html.html