merge NG auth, RT#21563
[freeside.git] / FS / FS / part_event / Condition / once_perinv.pm
1 package FS::part_event::Condition::once_perinv;
2
3 use strict;
4 use FS::Record qw( qsearch );
5 use FS::part_event;
6 use FS::cust_event;
7
8 use base qw( FS::part_event::Condition );
9
10 sub description { "Run only once for each time the package has been billed"; }
11
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.
14
15 sub option_fields {
16   (
17     'paid' => { 'label' => 'Only count paid bills',
18                 'type'  => 'checkbox',
19                 'value' => 'Y',
20               },
21   )
22 }
23
24 sub eventtable_hashref {
25     { 'cust_main' => 0,
26       'cust_bill' => 0,
27       'cust_pkg'  => 1,
28     };
29 }
30
31 sub condition {
32   my($self, $cust_pkg, %opt) = @_;
33
34   my @cust_bill_pkg = qsearch('cust_bill_pkg', { pkgnum=>$cust_pkg->pkgnum });
35
36   @cust_bill_pkg = grep { ($_->owed_setup + $_->owed_recur) == 0 }
37                      @cust_bill_pkg
38     if $self->option('paid');
39
40   my %invnum = ();
41   $invnum{$_->invnum} = 1 foreach @cust_bill_pkg;
42
43   my @events = qsearch( {
44       'table'     => 'cust_event', 
45       'hashref'   => { 'eventpart' => $self->eventpart,
46                        'status'    => { op=>'!=', value=>'failed' },
47                        'tablenum'  => $cust_pkg->pkgnum,
48                      },
49       'extra_sql' => ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/
50                        ? " AND eventnum != $1 " : '' ),
51   } );
52   scalar(@events) < scalar(keys %invnum);
53 }
54
55 sub condition_sql {
56   my( $self, $table ) = @_;
57
58   #paid flag not yet implemented here, but that's okay, a partial optimization
59   # is better than none
60
61   "( 
62     ( SELECT COUNT(distinct(invnum)) 
63       FROM cust_bill_pkg
64       WHERE cust_bill_pkg.pkgnum = cust_pkg.pkgnum )
65     >
66     ( SELECT COUNT(*)
67       FROM cust_event
68       WHERE cust_event.eventpart = part_event.eventpart
69         AND cust_event.tablenum = cust_pkg.pkgnum
70         AND status != 'failed' )
71   )"
72
73 }
74
75 1;