1 package FS::part_event::Condition::pkg_age_Common;
2 use base qw( FS::part_event::Condition );
7 tie our %dates, 'Tie::IxHash',
8 'setup' => 'Setup date',
9 'last_bill' => 'Last bill date',
10 'bill' => 'Next bill date',
11 'adjourn' => 'Adjournment date',
12 'susp' => 'Suspension date',
13 'expire' => 'Expiration date',
14 'cancel' => 'Cancellation date',
15 'contract_end' => 'Contract end date',
16 'orig_setup' => 'Original setup date',
19 sub eventtable_hashref {
30 'age' => { 'label' => $class->pkg_age_label,
33 'field' => { 'label' => 'Compare date',
35 'options' => [ keys %dates ],
42 my( $self, $cust_pkg, %opt ) = @_;
44 my $age = $self->pkg_age_age( $cust_pkg, %opt );
46 my $field = $self->option('field');
47 if ( $field =~ /^orig_(\w+)$/ ) {
48 # then find the package's oldest ancestor and compare to that
50 while ($cust_pkg->change_pkgnum) {
51 $cust_pkg = $cust_pkg->old_cust_pkg;
55 my $pkg_date = $cust_pkg->get( $field );
57 $pkg_date && $self->pkg_age_compare( $pkg_date, $age );
62 my( $self, $cust_pkg, %opt ) = @_;
63 $self->option_age_from('age', $opt{'time'} );
66 #doesn't work if you override pkg_age_age,
67 # so if you do, override this with at least a stub that returns 'true'
69 my( $class, $table, %opt ) = @_;
70 my $age = $class->condition_sql_option_age_from('age', $opt{'time'});
71 my $field = $class->condition_sql_option('field');
72 my $op = $class->pkg_age_operator;
74 #amazingly, this is actually faster
76 foreach ( keys %dates ) {
77 $sql .= " WHEN $field = '$_' THEN ";
78 # don't even try to handle orig_setup in here. it's not worth it.
82 $sql .= " (cust_pkg.$_ IS NOT NULL AND cust_pkg.$_ $op $age)";