X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_bill.pm;h=b9c0b5fb4596f8c228563e639314879fb9683440;hb=cb90d32aa1db8564d890cca3231784340cd5d964;hp=82023f6b5010fb75c601efe2aa19af26ef13465b;hpb=9035034a53d60cb7a7687dfee899c1d0c775ea74;p=freeside.git diff --git a/FS/FS/cust_bill.pm b/FS/FS/cust_bill.pm index 82023f6b5..b9c0b5fb4 100644 --- a/FS/FS/cust_bill.pm +++ b/FS/FS/cust_bill.pm @@ -24,6 +24,7 @@ use FS::cust_credit_bill; use FS::pay_batch; use FS::cust_pay_batch; use FS::cust_bill_event; +use FS::cust_event; use FS::part_pkg; use FS::cust_bill_pay; use FS::cust_bill_pay_batch; @@ -271,8 +272,7 @@ sub open_cust_bill_pkg { =item cust_bill_event -Returns the completed invoice events (see L) for this -invoice. +Returns the completed invoice events (deprecated, old-style events - see L) for this invoice. =cut @@ -281,6 +281,54 @@ sub cust_bill_event { qsearch( 'cust_bill_event', { 'invnum' => $self->invnum } ); } +=item num_cust_bill_event + +Returns the number of completed invoice events (deprecated, old-style events - see L) for this invoice. + +=cut + +sub num_cust_bill_event { + my $self = shift; + my $sql = + "SELECT COUNT(*) FROM cust_bill_event WHERE invnum = ?"; + my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql"; + $sth->execute($self->invnum) or die $sth->errstr. " executing $sql"; + $sth->fetchrow_arrayref->[0]; +} + +=item cust_event + +Returns the new-style customer billing events (see L) for this invoice. + +=cut + +#false laziness w/cust_pkg.pm +sub cust_event { + my $self = shift; + qsearch({ + 'table' => 'cust_event', + 'addl_from' => 'JOIN part_event USING ( eventpart )', + 'hashref' => { 'tablenum' => $self->invnum }, + 'extra_sql' => " AND eventtable = 'cust_bill' ", + }); +} + +=item num_cust_event + +Returns the number of new-style customer billing events (see L) for this invoice. + +=cut + +#false laziness w/cust_pkg.pm +sub num_cust_event { + my $self = shift; + my $sql = + "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ". + " WHERE tablenum = ? AND eventtable = 'cust_bill'"; + my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql"; + $sth->execute($self->invnum) or die $sth->errstr. " executing $sql"; + $sth->fetchrow_arrayref->[0]; +} =item cust_main @@ -2577,6 +2625,8 @@ sub _items_payments { =back + + =head1 SUBROUTINES =over 4 @@ -2643,6 +2693,12 @@ sub re_X { if ( $param{'end'} =~ /^(\d+)$/ ) { push @where, "cust_bill._date < $1"; } + if ( $param{'invnum_min'} =~ /^(\d+)$/ ) { + push @where, "cust_bill.invnum >= $1"; + } + if ( $param{'invnum_max'} =~ /^(\d+)$/ ) { + push @where, "cust_bill.invnum <= $1"; + } if ( $param{'agentnum'} =~ /^(\d+)$/ ) { push @where, "cust_main.agentnum = $1"; } @@ -2666,7 +2722,6 @@ sub re_X { if ( $param{'newest_percust'} ) { $distinct = 'DISTINCT ON ( cust_bill.custnum )'; $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC'; - #$count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'"; } my @cust_bill = qsearch( 'cust_bill', @@ -2698,6 +2753,34 @@ sub re_X { =back +=head1 CLASS METHODS + +=over 4 + +=item owed_sql + +Returns an SQL fragment to retreived the amount owed. + +=cut + +sub owed_sql { + #my $class = shift; + + "charged + - COALESCE( + ( SELECT SUM(amount) FROM cust_bill_pay + WHERE cust_bill.invnum = cust_bill_pay.invnum ) + ,0 + ) + - COALESCE( + ( SELECT SUM(amount) FROM cust_credit_bill + WHERE cust_bill.invnum = cust_credit_bill.invnum ) + ,0 + ) + "; + +} + =head1 BUGS The delete method.