X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_pkg.pm;h=834d2efb8079c45925e8478915141ff8be94b157;hb=9e4bc4c0c9c77d75618196f4f0eeeeeb35e2ee08;hp=d413596345aa7ffe963ac7cbb7254e7e436e5155;hpb=769381b3d73135ae9aeb1c05da4c9bd75d8d2b08;p=freeside.git diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index d41359634..834d2efb8 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -2,6 +2,7 @@ package FS::cust_pkg; use strict; use vars qw(@ISA $disable_agentcheck $DEBUG); +use Scalar::Util qw( blessed ); use List::Util qw(max); use Tie::IxHash; use FS::UID qw( getotaker dbh ); @@ -301,12 +302,17 @@ Calls =cut sub replace { - my( $new, $old, %options ) = @_; + my $new = shift; + + my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') ) + ? shift + : $new->replace_old; + + my $options = + ( ref($_[0]) eq 'HASH' ) + ? shift + : { @_ }; - # We absolutely have to have an old vs. new record to make this work. - if (!defined($old)) { - $old = qsearchs( 'cust_pkg', { 'pkgnum' => $new->pkgnum } ); - } #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart; return "Can't change otaker!" if $old->otaker ne $new->otaker; @@ -331,8 +337,8 @@ sub replace { my $dbh = dbh; foreach my $method ( qw(adjourn expire) ) { # How many reasons? - if ($options{'reason'} && $new->$method && $old->$method ne $new->$method) { - my $error = $new->insert_reason( 'reason' => $options{'reason'}, + if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) { + my $error = $new->insert_reason( 'reason' => $options->{'reason'}, 'date' => $new->$method, ); if ( $error ) { @@ -357,7 +363,7 @@ sub replace { } my $error = $new->SUPER::replace($old, - $options{options} ? ${options{options}} : () + $options->{options} ? $options->{options} : () ); if ( $error ) { $dbh->rollback if $oldAutoCommit; @@ -766,6 +772,22 @@ sub last_bill { $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0; } +=item last_cust_pkg_reason + +Returns the most recent FS::reason associated with the package. + +=cut + +sub last_cust_pkg_reason { + my $self = shift; + qsearchs( { + 'table' => 'cust_pkg_reason', + 'hashref' => { 'pkgnum' => $self->pkgnum, }, + 'extra_sql'=> "AND date <= ". time, + 'order_by' => 'ORDER BY date DESC LIMIT 1', + } ); +} + =item last_reason Returns the most recent FS::reason associated with the package. @@ -773,13 +795,8 @@ Returns the most recent FS::reason associated with the package. =cut sub last_reason { - my $self = shift; - my $cust_pkg_reason = qsearchs( { - 'table' => 'cust_pkg_reason', - 'hashref' => { 'pkgnum' => $self->pkgnum, }, - 'extra_sql'=> 'ORDER BY date DESC LIMIT 1', - } ); - qsearchs ( 'reason', { 'reasonnum' => $cust_pkg_reason->reasonnum } ) + my $cust_pkg_reason = shift->last_cust_pkg_reason; + $cust_pkg_reason->reason if $cust_pkg_reason; }