X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main.pm;h=6d879bcd406d3dc32628fa7ca82eddb2304570c7;hb=66ec38299e2f40271fa1e66c7a4db529a5b261d4;hp=c2107125b6e538be32c172bb5c0b193580639559;hpb=4e2b0b934d568e9b512bc45bf684bf5f23ae63b6;p=freeside.git diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index c2107125b..6d879bcd4 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -1715,12 +1715,7 @@ sub check { # If it is encrypted and the private key is not availaible then we can't # check the credit card. - - my $check_payinfo = 1; - - if ($self->is_encrypted($self->payinfo)) { - $check_payinfo = 0; - } + my $check_payinfo = ! $self->is_encrypted($self->payinfo); if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) { @@ -4796,7 +4791,7 @@ sub _realtime_bop_result { 'paid' => $cust_pay_pending->paid, '_date' => '', 'payby' => $cust_pay_pending->payby, - #'payinfo' => $payinfo, + 'payinfo' => $options{'payinfo'}, 'paybatch' => $paybatch, 'paydate' => $cust_pay_pending->paydate, 'pkgnum' => $cust_pay_pending->pkgnum, @@ -5200,7 +5195,7 @@ sub realtime_refund_bop { my $self = shift; my %options = (); - if (ref($_[0]) ne 'HASH') { + if (ref($_[0]) eq 'HASH') { %options = %{$_[0]}; } else { my $method = shift; @@ -5876,29 +5871,17 @@ sub total_owed_date { my $self = shift; my $time = shift; -# my $custnum = $self->custnum; -# -# my $owed_sql = FS::cust_bill->owed_sql; -# -# my $sql = " -# SELECT SUM($owed_sql) FROM cust_bill -# WHERE custnum = $custnum -# AND _date <= $time -# "; -# -# my $sth = dbh->prepare($sql) or die dbh->errstr; -# $sth->execute() or die $sth->errstr; -# -# return sprintf( '%.2f', $sth->fetchrow_arrayref->[0] ); + my $custnum = $self->custnum; - my $total_bill = 0; - foreach my $cust_bill ( - grep { $_->_date <= $time } - qsearch('cust_bill', { 'custnum' => $self->custnum, } ) - ) { - $total_bill += $cust_bill->owed; - } - sprintf( "%.2f", $total_bill ); + my $owed_sql = FS::cust_bill->owed_sql; + + my $sql = " + SELECT SUM($owed_sql) FROM cust_bill + WHERE custnum = $custnum + AND _date <= $time + "; + + sprintf( "%.2f", $self->scalar_sql($sql) ); } @@ -5968,9 +5951,18 @@ sub total_credited { sub total_unapplied_credits { my $self = shift; - my $total_credit = 0; - $total_credit += $_->credited foreach $self->cust_credit; - sprintf( "%.2f", $total_credit ); + + my $custnum = $self->custnum; + + my $unapplied_sql = FS::cust_credit->unapplied_sql; + + my $sql = " + SELECT SUM($unapplied_sql) FROM cust_credit + WHERE custnum = $custnum + "; + + sprintf( "%.2f", $self->scalar_sql($sql) ); + } =item total_unapplied_credits_pkgnum PKGNUM @@ -5997,9 +5989,18 @@ See L. sub total_unapplied_payments { my $self = shift; - my $total_unapplied = 0; - $total_unapplied += $_->unapplied foreach $self->cust_pay; - sprintf( "%.2f", $total_unapplied ); + + my $custnum = $self->custnum; + + my $unapplied_sql = FS::cust_pay->unapplied_sql; + + my $sql = " + SELECT SUM($unapplied_sql) FROM cust_pay + WHERE custnum = $custnum + "; + + sprintf( "%.2f", $self->scalar_sql($sql) ); + } =item total_unapplied_payments_pkgnum PKGNUM @@ -6027,9 +6028,17 @@ customer. See L. sub total_unapplied_refunds { my $self = shift; - my $total_unapplied = 0; - $total_unapplied += $_->unapplied foreach $self->cust_refund; - sprintf( "%.2f", $total_unapplied ); + my $custnum = $self->custnum; + + my $unapplied_sql = FS::cust_refund->unapplied_sql; + + my $sql = " + SELECT SUM($unapplied_sql) FROM cust_refund + WHERE custnum = $custnum + "; + + sprintf( "%.2f", $self->scalar_sql($sql) ); + } =item balance @@ -6041,12 +6050,7 @@ total_unapplied_credits minus total_unapplied_payments). sub balance { my $self = shift; - sprintf( "%.2f", - $self->total_owed - + $self->total_unapplied_refunds - - $self->total_unapplied_credits - - $self->total_unapplied_payments - ); + $self->balance_date_range; } =item balance_date TIME @@ -6061,19 +6065,13 @@ functions. sub balance_date { my $self = shift; - my $time = shift; - sprintf( "%.2f", - $self->total_owed_date($time) - + $self->total_unapplied_refunds - - $self->total_unapplied_credits - - $self->total_unapplied_payments - ); + $self->balance_date_range(shift); } -=item balance_date_range START_TIME [ END_TIME [ OPTION => VALUE ... ] ] +=item balance_date_range [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ] -Returns the balance for this customer, only considering invoices with date -earlier than START_TIME, and optionally not later than END_TIME +Returns the balance for this customer, optionally considering invoices with +date earlier than START_TIME, and not later than END_TIME (total_owed_date minus total_unapplied_credits minus total_unapplied_payments). Times are specified as SQL fragments or numeric @@ -7137,6 +7135,8 @@ Returns a status string for this customer, currently: =item prospect - No packages have ever been ordered +=item ordered - Recurring packages all are new (not yet billed). + =item active - One or more recurring packages is active =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled) @@ -7153,7 +7153,8 @@ sub status { shift->cust_status(@_); } sub cust_status { my $self = shift; - for my $status (qw( prospect active inactive suspended cancelled )) { + # prospect ordered active inactive suspended cancelled + for my $status ( FS::cust_main->statuses() ) { my $method = $status.'_sql'; my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g; my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr; @@ -7188,6 +7189,7 @@ use vars qw(%statuscolor); tie %statuscolor, 'Tie::IxHash', 'prospect' => '7e0079', #'000000', #black? naw, purple 'active' => '00CC00', #green + 'ordered' => '009999', #teal? cyan? 'inactive' => '0000CC', #blue 'suspended' => 'FF9900', #yellow 'cancelled' => 'FF0000', #red @@ -7297,9 +7299,20 @@ sub select_count_pkgs_sql { $select_count_pkgs; } -sub prospect_sql { " - 0 = ( $select_count_pkgs ) -"; } +sub prospect_sql { + " 0 = ( $select_count_pkgs ) "; +} + +=item ordered_sql + +Returns an SQL expression identifying ordered cust_main records (customers with +recurring packages not yet setup). + +=cut + +sub ordered_sql { + " 0 < ( $select_count_pkgs AND ". FS::cust_pkg->ordered_sql. " ) "; +} =item active_sql @@ -7308,10 +7321,9 @@ active recurring packages). =cut -sub active_sql { " - 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " - ) -"; } +sub active_sql { + " 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " ) "; +} =item inactive_sql @@ -7400,10 +7412,10 @@ sub balance_sql { " WHERE cust_refund.custnum = cust_main.custnum ) "; } -=item balance_date_sql START_TIME [ END_TIME [ OPTION => VALUE ... ] ] +=item balance_date_sql [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ] -Returns an SQL fragment to retreive the balance for this customer, only -considering invoices with date earlier than START_TIME, and optionally not +Returns an SQL fragment to retreive the balance for this customer, optionally +considering invoices with date earlier than START_TIME, and not later than END_TIME (total_owed_date minus total_unapplied_credits minus total_unapplied_payments). @@ -7601,7 +7613,7 @@ sub search { # parse status ## - #prospect active inactive suspended cancelled + #prospect ordered active inactive suspended cancelled if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) { my $method = $params->{'status'}. '_sql'; #push @where, $class->$method();