fix for lack of input validation - RT#15405
[freeside.git] / FS / FS / part_event / Condition / balance_age.pm
1 package FS::part_event::Condition::balance_age;
2
3 use strict;
4 use base qw( FS::part_event::Condition );
5
6 sub description { 'Customer balance age'; }
7
8 =item check_options OPTIONS
9
10 Validate options
11
12 =cut
13
14 my $duration_rx = qr/^(\d+)$/;
15 my $unit_rx = qr/^[wmdh]$/;
16 my $both_rx = qr/^(\d+)([wmdh])/;
17
18 sub check_options {
19   my ($self, $options) = @_;
20
21   my $age       = $options->{age};
22   my $age_units = $options->{age_units};
23
24   return "Invalid (age) must be defined: $age"
25     unless( defined $age );
26
27   # over-ride possibly inaccurate unit indicator
28   if( $age =~ /$both_rx/ ){
29     $age = $1;
30     $age_units = $2;
31   }
32
33   return "Invalid (age_units) must be defined: $age_units"
34     unless defined $age_units;
35
36   return "Invalid (age) must be integer: $age"
37     unless( $age =~ /$duration_rx/ );
38
39   return "Invalid (age) must be non-zero: $age"
40     if ( $age == 0 );
41
42   return( "Invalid (age_units) must be m/w/d/h: $age_units" )
43     unless( $age_units =~ /$unit_rx/i );
44
45   return '';
46 }
47
48 sub option_fields {
49   (
50     'balance' => { 'label'      => 'Balance over',
51                    'type'       => 'money',
52                    'value'      => '0.00', #default
53                  },
54     'age'     => { 'label'      => 'Age',
55                    'type'       => 'freq',
56                  },
57   );
58 }
59
60 sub condition {
61   my($self, $object, %opt) = @_;
62
63   my $cust_main = $self->cust_main($object);
64
65   my $over = $self->option('balance');
66   $over = 0 unless length($over);
67
68   my $age = $self->option_age_from('age', $opt{'time'} );
69
70   $cust_main->balance_date($age) > $over;
71 }
72
73 sub condition_sql {
74   my( $class, $table, %opt ) = @_;
75
76   my $over    = $class->condition_sql_option('balance');
77   my $age     = $class->condition_sql_option_age_from('age', $opt{'time'});
78
79   my $balance_sql = FS::cust_main->balance_date_sql( $age );
80
81   "$balance_sql > CAST( $over AS DECIMAL(10,2) )";
82 }
83
84 sub order_sql {
85   shift->condition_sql_option_age('age');
86 }
87
88 sub order_sql_weight {
89   10;
90 }
91
92 1;