summaryrefslogtreecommitdiff
path: root/FS
diff options
context:
space:
mode:
authorIvan Kohler <ivan@freeside.biz>2014-12-28 15:03:21 -0800
committerIvan Kohler <ivan@freeside.biz>2014-12-28 15:03:21 -0800
commit357211ff7f54864dbc04876db6e84580e02e807c (patch)
treeec757e6f3d933847e118d7f64c59ea157c5cd880 /FS
parent1401eef8b37ea9ffc6a44819a0778697c775dac3 (diff)
add condition on "Invoice is newer than last payment type change", RT#31185
Diffstat (limited to 'FS')
-rw-r--r--FS/FS/part_event/Condition/cust_bill_age_before_payby.pm57
1 files changed, 57 insertions, 0 deletions
diff --git a/FS/FS/part_event/Condition/cust_bill_age_before_payby.pm b/FS/FS/part_event/Condition/cust_bill_age_before_payby.pm
new file mode 100644
index 000000000..273f508d9
--- /dev/null
+++ b/FS/FS/part_event/Condition/cust_bill_age_before_payby.pm
@@ -0,0 +1,57 @@
+package FS::part_event::Condition::cust_bill_age_before_payby;
+use base qw( FS::part_event::Condition );
+
+use strict;
+use FS::Record qw( qsearchs );
+use FS::h_cust_main;
+
+sub description { 'Invoice is newer than last payment type change'; }
+
+sub eventtable_hashref {
+ { 'cust_main' => 0,
+ 'cust_bill' => 1,
+ 'cust_pkg' => 0,
+ };
+}
+
+sub condition {
+ my( $self, $cust_bill, %opt ) = @_;
+
+ #my $cust_main = $cust_bill->cust_main;
+
+ my $change_date = 0;
+ my $newest = 4294967295; #2^32-1
+
+ #this is pretty expensive, it would be way more efficient to check for
+ # changed payby in SQL
+ # (it would also help if a replace_new had a real FK ref to its replace_old)
+ while ( my $replace_new = qsearchs({
+ 'table' => 'h_cust_main',
+ 'hashref' => { 'custnum' => $cust_bill->custnum,
+ 'history_action' => 'replace_new',
+ 'history_date' => { op=>'<', value=>$newest },
+ },
+ 'order_by' => 'ORDER BY history_date DESC LIMIT 1',
+ }))
+ {
+ my $newest = $replace_new->history_date;
+ my $replace_old = qsearchs({
+ 'table' => 'h_cust_main',
+ 'hashref' => { 'custnum' => $replace_new->custnum,
+ 'history_action' => 'replace_old',
+ 'history_date' => $replace_new->history_date,
+ }
+ }) or next; #no replace_old? ignore and continue on i guess
+
+ if ( $replace_new->payby ne $replace_old->payby ) {
+ $change_date = $replace_new->history_date;
+ last;
+ }
+
+ }
+
+ ( $cust_bill->_date ) > $change_date;
+
+}
+
+1;