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'; }
+=item check_options OPTIONS
+
+Validate options
+
+=cut
+
+my $duration_rx = qr/^(\d+)$/;
+my $unit_rx = qr/^[wmdh]$/;
+my $both_rx = qr/^(\d+)([wmdh])/;
+
+sub check_options {
+ my ($self, $options) = @_;
+
+ my $age = $options->{age};
+ my $age_units = $options->{age_units};
+
+ return "Invalid (age) must be defined: $age"
+ unless( defined $age );
+
+ # over-ride possibly inaccurate unit indicator
+ if( $age =~ /$both_rx/ ){
+ $age = $1;
+ $age_units = $2;
+ }
+
+ return "Invalid (age_units) must be defined: $age_units"
+ unless defined $age_units;
+
+ return "Invalid (age) must be integer: $age"
+ unless( $age =~ /$duration_rx/ );
+
+ return "Invalid (age) must be non-zero: $age"
+ if ( $age == 0 );
+
+ return( "Invalid (age_units) must be m/w/d/h: $age_units" )
+ unless( $age_units =~ /$unit_rx/i );
+
+ return '';
+}
+
sub option_fields {
(
'balance' => { 'label' => 'Balance over',
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);
+ my $age = $self->option_age_from('age', $opt{'time'} );
- $cust_main->balance_date($age_date) > $over;
+ $cust_main->balance_date($age) > $over;
}
sub condition_sql {
my $balance_sql = FS::cust_main->balance_date_sql( $age );
- "$balance_sql > $over";
+ "$balance_sql > CAST( $over AS DECIMAL(10,2) )";
}
sub order_sql {
shift->condition_sql_option_age('age');
}
-use FS::UID qw( driver_name );
-
sub order_sql_weight {
10;
}