1 package FS::part_event::Condition::once_perinv;
4 use FS::Record qw( qsearch );
8 use base qw( FS::part_event::Condition );
10 sub description { "Run only once for each time the package has been billed"; }
12 # Run the event, at most, a number of times equal to the number of
13 # distinct invoices that contain line items from this package.
17 'paid' => { 'label' => 'Only count paid bills',
21 'no_late' => { 'label' => "But don't consider paid bills which were late.",
28 sub eventtable_hashref {
36 my($self, $cust_pkg, %opt) = @_;
38 my @cust_bill_pkg = qsearch('cust_bill_pkg', { pkgnum=>$cust_pkg->pkgnum });
40 if ( $self->option('paid') ) {
41 @cust_bill_pkg = grep { ($_->owed_setup + $_->owed_recur) == 0 }
44 if ( $self->option('no_late') ) {
45 @cust_bill_pkg = grep {
46 my $cust_bill_pkg = $_;
48 my @cust_bill_pay_pkg = ();
49 push @cust_bill_pay_pkg, $cust_bill_pkg->cust_bill_pay_pkg($_)
50 for qw( setup recur );
51 return 1 unless @cust_bill_pay_pkg; #no payments? must be credits then
52 #not considering those "late"
54 my @cust_pay = sort { $a->_date <=> $b->_date }
55 map { $_->cust_bill_pay->cust_pay }
58 #most recent payment less than due date? okay, we were paid on time
59 $cust_pay[-1] <= $cust_bill_pkg->cust_bill->due_date;
67 $invnum{$_->invnum} = 1 foreach @cust_bill_pkg;
69 my @events = qsearch( {
70 'table' => 'cust_event',
71 'hashref' => { 'eventpart' => $self->eventpart,
72 'status' => { op=>'!=', value=>'failed' },
73 'tablenum' => $cust_pkg->pkgnum,
75 'extra_sql' => ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/
76 ? " AND eventnum != $1 " : '' ),
78 scalar(@events) < scalar(keys %invnum);
82 my( $self, $table ) = @_;
84 #paid flag not yet implemented here, but that's okay, a partial optimization
88 ( SELECT COUNT(distinct(invnum))
90 WHERE cust_bill_pkg.pkgnum = cust_pkg.pkgnum )
94 WHERE cust_event.eventpart = part_event.eventpart
95 AND cust_event.tablenum = cust_pkg.pkgnum
96 AND status != 'failed' )