X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_event%2FCondition%2Fonce_perinv.pm;h=b1ab90cafe9a18e56b15cdac9c6f1e7b3abd5ea8;hb=faa774764df03c6f3280177f6adfcd2214995a13;hp=f85a0566522580ad40889e54740498916d033050;hpb=b5c4237a34aef94976bc343c8d9e138664fc3984;p=freeside.git diff --git a/FS/FS/part_event/Condition/once_perinv.pm b/FS/FS/part_event/Condition/once_perinv.pm index f85a05665..b1ab90caf 100644 --- a/FS/FS/part_event/Condition/once_perinv.pm +++ b/FS/FS/part_event/Condition/once_perinv.pm @@ -12,6 +12,19 @@ sub description { "Run only once for each time the package has been billed"; } # Run the event, at most, a number of times equal to the number of # distinct invoices that contain line items from this package. +sub option_fields { + ( + 'paid' => { 'label' => 'Only count paid bills', + 'type' => 'checkbox', + 'value' => 'Y', + }, + 'no_late' => { 'label' => "But don't consider paid bills which were late.", + 'type' => 'checkbox', + 'value' => 'Y', + }, + ) +} + sub eventtable_hashref { { 'cust_main' => 0, 'cust_bill' => 0, @@ -22,9 +35,37 @@ sub eventtable_hashref { sub condition { my($self, $cust_pkg, %opt) = @_; - my %invnum; - $invnum{$_->invnum} = 1 - foreach ( qsearch('cust_bill_pkg', { 'pkgnum' => $cust_pkg->pkgnum }) ); + my @cust_bill_pkg = qsearch('cust_bill_pkg', { pkgnum=>$cust_pkg->pkgnum }); + + if ( $self->option('paid') ) { + @cust_bill_pkg = grep { ($_->owed_setup + $_->owed_recur) == 0 } + @cust_bill_pkg; + + if ( $self->option('no_late') ) { + @cust_bill_pkg = grep { + my $cust_bill_pkg = $_; + + my @cust_bill_pay_pkg = (); + push @cust_bill_pay_pkg, $cust_bill_pkg->cust_bill_pay_pkg($_) + for qw( setup recur ); + return 1 unless @cust_bill_pay_pkg; #no payments? must be credits then + #not considering those "late" + + my @cust_pay = sort { $a->_date <=> $b->_date } + map { $_->cust_bill_pay->cust_pay } + @cust_bill_pay_pkg; + + #most recent payment less than due date? okay, we were paid on time + $cust_pay[-1] <= $cust_bill_pkg->cust_bill->due_date; + + } @cust_bill_pkg; + } + + } + + my %invnum = (); + $invnum{$_->invnum} = 1 foreach @cust_bill_pkg; + my @events = qsearch( { 'table' => 'cust_event', 'hashref' => { 'eventpart' => $self->eventpart, @@ -40,6 +81,9 @@ sub condition { sub condition_sql { my( $self, $table ) = @_; + #paid flag not yet implemented here, but that's okay, a partial optimization + # is better than none + "( ( SELECT COUNT(distinct(invnum)) FROM cust_bill_pkg