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',
24 sub eventtable_hashref {
32 my($self, $cust_pkg, %opt) = @_;
34 my @cust_bill_pkg = qsearch('cust_bill_pkg', { pkgnum=>$cust_pkg->pkgnum });
36 @cust_bill_pkg = grep { ($_->owed_setup + $_->owed_recur) == 0 }
38 if $self->option('paid');
41 $invnum{$_->invnum} = 1 foreach @cust_bill_pkg;
43 my @events = qsearch( {
44 'table' => 'cust_event',
45 'hashref' => { 'eventpart' => $self->eventpart,
46 'status' => { op=>'!=', value=>'failed' },
47 'tablenum' => $cust_pkg->pkgnum,
49 'extra_sql' => ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/
50 ? " AND eventnum != $1 " : '' ),
52 scalar(@events) < scalar(keys %invnum);
56 my( $self, $table ) = @_;
58 #paid flag not yet implemented here, but that's okay, a partial optimization
62 ( SELECT COUNT(distinct(invnum))
64 WHERE cust_bill_pkg.pkgnum = cust_pkg.pkgnum )
68 WHERE cust_event.eventpart = part_event.eventpart
69 AND cust_event.tablenum = cust_pkg.pkgnum
70 AND status != 'failed' )