1 package FS::part_event::Condition::cust_bill_age_before_payby;
2 use base qw( FS::part_event::Condition );
5 use FS::Record qw( qsearchs );
8 sub description { 'Invoice is newer than last payment type change'; }
10 sub eventtable_hashref {
18 my( $self, $cust_bill, %opt ) = @_;
20 #my $cust_main = $cust_bill->cust_main;
23 my $newest = 2147483647; #2038 problem, because the field does
25 #this is pretty expensive, it would be way more efficient to check for
26 # changed payby in SQL
27 # (it would also help if a replace_new had a real FK ref to its replace_old)
28 while ( my $replace_new = qsearchs({
29 'table' => 'h_cust_main',
30 'hashref' => { 'custnum' => $cust_bill->custnum,
31 'history_action' => 'replace_new',
32 'history_date' => { op=>'<', value=>$newest },
34 'order_by' => 'ORDER BY history_date DESC LIMIT 1',
37 $newest = $replace_new->history_date;
38 my $replace_old = qsearchs({
39 'table' => 'h_cust_main',
40 'hashref' => { 'custnum' => $replace_new->custnum,
41 'history_action' => 'replace_old',
42 'history_date' => $replace_new->history_date, #fuzz?
44 }) or next; #no replace_old? ignore and continue on i guess
46 if ( $replace_new->payby ne $replace_old->payby ) {
47 $change_date = $replace_new->history_date;
53 ( $cust_bill->_date ) > $change_date;