summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Kohler <ivan@freeside.biz>2013-07-23 12:19:37 -0700
committerIvan Kohler <ivan@freeside.biz>2013-07-23 12:19:37 -0700
commit095f0bf409cc1e30e6ce583b2f48e639ad79b8b2 (patch)
tree04cdf7d43969ed6a849e22d90b9306f2cb50b069
parentd24fa4ecb03899992e299638bf04345f6bec8e75 (diff)
add "Package Age Younger" condition, RT#23171
-rw-r--r--FS/FS/part_event/Condition/pkg_age_Common.pm68
-rw-r--r--FS/FS/part_event/Condition/pkg_age_before.pm18
-rw-r--r--FS/FS/part_event_condition.pm4
3 files changed, 90 insertions, 0 deletions
diff --git a/FS/FS/part_event/Condition/pkg_age_Common.pm b/FS/FS/part_event/Condition/pkg_age_Common.pm
new file mode 100644
index 000000000..0f3b9efb4
--- /dev/null
+++ b/FS/FS/part_event/Condition/pkg_age_Common.pm
@@ -0,0 +1,68 @@
+package FS::part_event::Condition::pkg_age_Common;
+use base qw( FS::part_event::Condition );
+
+use strict;
+use Tie::IxHash;
+
+tie our %dates, 'Tie::IxHash',
+ 'setup' => 'Setup date',
+ 'last_bill' => 'Last bill date',
+ 'bill' => 'Next bill date',
+ 'adjourn' => 'Adjournment date',
+ 'susp' => 'Suspension date',
+ 'expire' => 'Expiration date',
+ 'cancel' => 'Cancellation date',
+ 'contract_end' => 'Contract end date',
+;
+
+sub eventtable_hashref {
+ { 'cust_main' => 0,
+ 'cust_bill' => 0,
+ 'cust_pkg' => 1,
+ };
+}
+
+#something like this
+sub option_fields {
+ my $class = shift;
+ (
+ 'age' => { 'label' => $class->pkg_age_label,
+ 'type' => 'freq',
+ },
+ 'field' => { 'label' => 'Compare date',
+ 'type' => 'select',
+ 'options' => [ keys %dates ],
+ 'labels' => \%dates,
+ },
+ );
+}
+
+sub condition {
+ my( $self, $cust_pkg, %opt ) = @_;
+
+ my $age = $self->option_age_from('age', $opt{'time'} );
+
+ my $pkg_date = $cust_pkg->get( $self->option('field') );
+
+ $pkg_date && $self->pkg_age_compare( $pkg_date, $age );
+
+}
+
+sub condition_sql {
+ my( $class, $table, %opt ) = @_;
+ my $age = $class->condition_sql_option_age_from('age', $opt{'time'});
+ my $field = $class->condition_sql_option('field');
+ my $op = $class->pkg_age_operator;
+
+ #amazingly, this is actually faster
+ my $sql = '( CASE';
+ foreach ( keys %dates ) {
+ $sql .= " WHEN $field = '$_' THEN ".
+ " (cust_pkg.$_ IS NOT NULL AND cust_pkg.$_ $op $age)";
+ }
+ $sql .= ' END )';
+ return $sql;
+}
+
+1;
+
diff --git a/FS/FS/part_event/Condition/pkg_age_before.pm b/FS/FS/part_event/Condition/pkg_age_before.pm
new file mode 100644
index 000000000..c778265f2
--- /dev/null
+++ b/FS/FS/part_event/Condition/pkg_age_before.pm
@@ -0,0 +1,18 @@
+package FS::part_event::Condition::pkg_age_before;
+use base qw( FS::part_event::Condition::pkg_age_Common );
+
+use strict;
+
+sub description { 'Package Age Younger'; }
+
+sub pkg_age_operator { '>'; }
+
+sub pkg_age_label { 'Package date age younger than'; }
+
+sub pkg_age_compare {
+ my( $pkg_date, $age ) = @_;
+ $pkg_date > $age;
+}
+
+1;
+
diff --git a/FS/FS/part_event_condition.pm b/FS/FS/part_event_condition.pm
index 32f19a3ae..d550680f7 100644
--- a/FS/FS/part_event_condition.pm
+++ b/FS/FS/part_event_condition.pm
@@ -185,6 +185,10 @@ foreach my $INC ( @INC ) {
};
my $mod = $1;
my $fullmod = "FS::part_event::Condition::$mod";
+ if ( $fullmod =~ /_(Mixin|Common)$/ ) {
+ #warn "skipping $1 class $fullmod\n";
+ next;
+ }
eval "use $fullmod;";
if ( $@ ) {
die "error using $fullmod (skipping): $@\n" if $@;