This commit was generated by cvs2svn to compensate for changes in r6255,
[freeside.git] / FS / FS / part_event / Condition / cust_bill_age.pm
1 package FS::part_event::Condition::cust_bill_age;
2
3 require 5.006;
4 use strict;
5 use Time::Local qw(timelocal_nocheck);
6
7 use base qw( FS::part_event::Condition );
8
9 sub description {
10   'Invoice age';
11 }
12
13 sub eventtable_hashref {
14     { 'cust_main' => 0,
15       'cust_bill' => 1,
16       'cust_pkg'  => 0,
17     };
18 }
19
20 #something like this
21 sub option_fields {
22   (
23     #'days' => { label=>'Days', size=>3, },
24     'age' => { label=>'Age', type=>'freq', },
25   );
26 }
27
28 sub condition {
29   my( $self, $cust_bill, %opt ) = @_;
30
31   #false laziness w/balance_age
32   my $time = $opt{'time'};
33   my $age = $self->option('age');
34   $age = '0m' unless length($age);
35
36   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
37   if ( $age =~ /^(\d+)m$/i ) {
38     $mon -= $1;
39     until ( $mon >= 0 ) { $mon += 12; $year--; }
40   } elsif ( $age =~ /^(\d+)y$/i ) {
41     $year -= $1;
42   } elsif ( $age =~ /^(\d+)w$/i ) {
43     $mday -= $1 * 7;
44   } elsif ( $age =~ /^(\d+)d$/i ) {
45     $mday -= $1;
46   } elsif ( $age =~ /^(\d+)h$/i ) {
47     $hour -= $hour;
48   } else {
49     die "unparsable age: $age";
50   }
51   my $age_date = timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
52
53   $cust_bill->_date <= $age_date;
54
55 }
56
57 #                            and seconds <= $time - cust_bill._date
58
59 sub condition_sql {
60   my( $class, $table, %opt ) = @_;
61
62   my $age  = $class->condition_sql_option_age_from('age', $opt{'time'} );
63
64   "cust_bill._date <= $age";
65 }
66
67 sub order_sql {
68   shift->condition_sql_option_age('age');
69 }
70
71 sub order_sql_weight {
72   0;
73 }
74
75 1;