+=item join_sql
+
+=cut
+
+sub join_sql {
+ #my $class = shift;
+
+ "
+ JOIN part_event USING ( eventpart )
+ LEFT JOIN cust_bill ON ( eventtable = 'cust_bill' AND tablenum = invnum )
+ LEFT JOIN cust_pkg ON ( eventtable = 'cust_pkg' AND tablenum = pkgnum )
+ LEFT JOIN cust_pay ON ( eventtable = 'cust_pay' AND tablenum = paynum )
+ LEFT JOIN cust_svc ON ( eventtable = 'svc_acct' AND tablenum = svcnum )
+ LEFT JOIN cust_pkg AS cust_pkg_for_svc ON ( cust_svc.pkgnum = cust_pkg_for_svc.pkgnum )
+ LEFT JOIN cust_main ON (
+ ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
+ OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
+ OR ( eventtable = 'cust_pkg' AND cust_pkg.custnum = cust_main.custnum )
+ OR ( eventtable = 'cust_pay' AND cust_pay.custnum = cust_main.custnum )
+ OR ( eventtable = 'svc_acct' AND cust_pkg_for_svc.custnum = cust_main.custnum )
+ )
+ ";
+
+}
+
+=item search_sql_where HASHREF
+
+Class method which returns an SQL WHERE fragment to search for parameters
+specified in HASHREF. Valid parameters are
+
+=over 4
+
+=item agentnum
+
+=item custnum
+
+=item invnum
+
+=item pkgnum
+
+=item svcnum
+
+=item failed
+
+=item beginning
+
+=item ending
+
+=back
+
+=cut
+
+#Note: validates all passed-in data; i.e. safe to use with unchecked CGI params.
+#sub
+
+sub search_sql_where {
+ my($class, $param) = @_;
+ if ( $DEBUG ) {
+ warn "$me search_sql_where called with params: \n".
+ join("\n", map { " $_: ". $param->{$_} } keys %$param ). "\n";
+ }
+
+ my @search = $class->cust_search_sql($param);
+
+ #eventpart
+ my @eventpart = ref($param->{'eventpart'})
+ ? @{ $param->{'eventpart'} }
+ : split(',', $param->{'eventpart'});
+ @eventpart = grep /^(\d+)$/, @eventpart;
+ if ( @eventpart ) {
+ push @search, 'eventpart IN ('. join(',', @eventpart). ')';
+ }
+
+ if ( $param->{'beginning'} =~ /^(\d+)$/ ) {
+ push @search, "cust_event._date >= $1";
+ }
+ if ( $param->{'ending'} =~ /^(\d+)$/ ) {
+ push @search, "cust_event._date <= $1";
+ }
+
+ #if ( $param->{'failed'} ) {
+ # push @search, "statustext != ''",
+ # "statustext IS NOT NULL",
+ # "statustext != 'N/A'";
+ #}
+ # huh?
+
+ my @event_status = ref($param->{'event_status'})
+ ? @{ $param->{'event_status'} }
+ : split(',', $param->{'event_status'});
+ if ( @event_status ) {
+ my @status;
+
+ my ($done_Y, $done_N);
+ foreach (@event_status) {
+ if ($_ eq 'done_Y') {
+ $done_Y = 1;
+ } elsif ( $_ eq 'done_N' ) {
+ $done_N = 1;
+ } else {
+ push @status, $_;
+ }
+ }
+ if ( $done_Y or $done_N ) {
+ push @status, 'done';
+ }
+ if ( @status ) {
+ push @search, "cust_event.status IN(" .
+ join(',', map "'$_'", @status) .
+ ')';
+ }
+
+ if ( $done_Y and not $done_N ) {
+ push @search, "cust_event.no_action IS NULL";
+ } elsif ( $done_N and not $done_Y ) {
+ push @search, "cust_event.no_action = 'Y'";
+ } # else they're both true, so don't add a constraint, or both false,
+ # and it doesn't matter.
+
+ } # event_status
+
+ # always hide initialization
+ push @search, 'cust_event.status != \'initial\'';
+
+ if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
+ push @search, "cust_main.custnum = '$1'";
+ }
+
+ if ( $param->{'invnum'} =~ /^(\d+)$/ ) {
+ push @search, "part_event.eventtable = 'cust_bill'",
+ "tablenum = '$1'";
+ }
+
+ if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) {
+ push @search, "part_event.eventtable = 'cust_pkg'",
+ "tablenum = '$1'";
+ }
+
+ if ( $param->{'paynum'} =~ /^(\d+)$/ ) {
+ push @search, "part_event.eventtable = 'cust_pay'",
+ "tablenum = '$1'";
+ }
+
+ if ( $param->{'svcnum'} =~ /^(\d+)$/ ) {
+ push @search, "part_event.eventtable = 'svc_acct'",
+ "tablenum = '$1'";
+ }
+
+ my $where = 'WHERE '. join(' AND ', @search );
+
+ join(' AND ', @search );
+
+}
+