X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_event%2FCondition%2Fbalance_age.pm;fp=FS%2FFS%2Fpart_event%2FCondition%2Fbalance_age.pm;h=94f231ec14cbb0c4b9a0ee0d6d6c889ed610e79a;hb=eb4ff7f73c5d4bdf74a3472448b5a195598ff4cd;hp=0000000000000000000000000000000000000000;hpb=32b5d3a31f112a381f0a15ac5e3a2204242f3405;p=freeside.git diff --git a/FS/FS/part_event/Condition/balance_age.pm b/FS/FS/part_event/Condition/balance_age.pm new file mode 100644 index 000000000..94f231ec1 --- /dev/null +++ b/FS/FS/part_event/Condition/balance_age.pm @@ -0,0 +1,83 @@ +package FS::part_event::Condition::balance_age; + +require 5.006; +use strict; +use Time::Local qw(timelocal_nocheck); + +use base qw( FS::part_event::Condition ); + +sub description { 'Customer balance age'; } + +sub option_fields { + ( + 'balance' => { 'label' => 'Balance over', + 'type' => 'money', + 'value' => '0.00', #default + }, + 'age' => { 'label' => 'Age', + 'type' => 'freq', + }, + ); +} + +sub condition { + my($self, $object, %opt) = @_; + + my $cust_main = $self->cust_main($object); + + my $over = $self->option('balance'); + $over = 0 unless length($over); + + #false laziness w/cust_bill_age + my $time = $opt{'time'}; + my $age = $self->option('age'); + $age = '0m' unless length($age); + + my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5]; + if ( $age =~ /^(\d+)m$/i ) { + $mon -= $1; + until ( $mon >= 0 ) { $mon += 12; $year--; } + } elsif ( $age =~ /^(\d+)y$/i ) { + $year -= $1; + } elsif ( $age =~ /^(\d+)w$/i ) { + $mday -= $1 * 7; + } elsif ( $age =~ /^(\d+)d$/i ) { + $mday -= $1; + } elsif ( $age =~ /^(\d+)h$/i ) { + $hour -= $hour; + } else { + die "unparsable age: $age"; + } + my $age_date = timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year); + + $cust_main->balance_date($age_date) > $over; +} + +sub condition_sql { + my( $class, $table, %opt ) = @_; + + my $time = $opt{'time'}; + + my $over = $class->condition_sql_option('balance'); + my $age = $class->condition_sql_option('age'); + my $age_sql = + "$time - EXTRACT( EPOCH FROM REPLACE( $age, 'm', 'mon')::interval )"; + + my $balance_sql = FS::cust_main->balance_date_sql( $age_sql ); + + "$balance_sql > $over"; + +} + +sub order_sql { + my( $class ) = @_; + + my $age = $class->condition_sql_option('age'); + "EXTRACT( EPOCH FROM REPLACE( $age, 'm', 'mon')::interval )"; +} + +sub order_sql_weight { + 10; +} + +1;