From c632a91db61eb4ed86143fefd1ec6ebccc0ad526 Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 24 Mar 2009 04:36:16 +0000 Subject: [PATCH] more DTRT with usage on service transfer between packages and recharges RT #2884, #5040 --- FS/FS/cust_pkg.pm | 20 +++++++++++++-- FS/FS/part_pkg/flat.pm | 14 ++++++++--- FS/FS/svc_acct.pm | 42 ++++++++++++++++++++++++++++--- httemplate/edit/part_svc.cgi | 9 ++++++- httemplate/misc/process/recharge_svc.html | 2 +- 5 files changed, 75 insertions(+), 12 deletions(-) diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index 198e31bdf..6c38900f9 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -2153,6 +2153,22 @@ sub order { $dbh->rollback if $oldAutoCommit; return "Unable to transfer all services from package ".$old_pkg->pkgnum; } + + #reset usage if changing pkgpart + if ($old_pkg->pkgpart != $new_pkg->pkgpart) { + my $part_pkg = $new_pkg->part_pkg; + $error = $part_pkg->reset_usage($new_pkg, $part_pkg->is_prepaid + ? () + : ( 'null' => 1 ) + ) + if $part_pkg->can('reset_usage'); + + if ($error) { + $dbh->rollback if $oldAutoCommit; + return "Error setting usage values: $error"; + } + } + $error = $old_pkg->cancel( quiet=>1 ); if ($error) { $dbh->rollback; @@ -2298,11 +2314,11 @@ All svc_accts which are part of this package have their values reset. =cut sub set_usage { - my ($self, $valueref) = @_; + my ($self, $valueref, %opt) = @_; foreach my $cust_svc ($self->cust_svc){ my $svc_x = $cust_svc->svc_x; - $svc_x->set_usage($valueref) + $svc_x->set_usage($valueref, %opt) if $svc_x->can("set_usage"); } } diff --git a/FS/FS/part_pkg/flat.pm b/FS/FS/part_pkg/flat.pm index 01ea5d439..ca398c698 100644 --- a/FS/FS/part_pkg/flat.pm +++ b/FS/FS/part_pkg/flat.pm @@ -186,15 +186,21 @@ sub is_prepaid { 0; #no, we're postpaid } -sub reset_usage { - my($self, $cust_pkg) = @_; - my %values = map { $_, $self->option($_) } +sub usage_valuehash { + my $self = shift; + map { $_, $self->option($_) } grep { $self->option($_, 'hush') } qw(seconds upbytes downbytes totalbytes); +} + +sub reset_usage { + my($self, $cust_pkg, %opt) = @_; + warn " resetting usage counters" if $opt{debug} > 1; + my %values = $self->usage_valuehash; if ($self->option('usage_rollover', 1)) { $cust_pkg->recharge(\%values); }else{ - $cust_pkg->set_usage(\%values); + $cust_pkg->set_usage(\%values, %opt); } } diff --git a/FS/FS/svc_acct.pm b/FS/FS/svc_acct.pm index 1a9e8f2ad..4250db433 100644 --- a/FS/FS/svc_acct.pm +++ b/FS/FS/svc_acct.pm @@ -26,6 +26,7 @@ use FS::Conf; use FS::Record qw( qsearch qsearchs fields dbh dbdef ); use FS::Msgcat qw(gettext); use FS::UI::bytecount; +use FS::part_pkg; use FS::svc_Common; use FS::cust_svc; use FS::part_svc; @@ -290,6 +291,7 @@ sub table_info { type => 'text', disable_inventory => 1, disable_select => 1, + disable_part_svc_column => 1, }, 'upbytes' => { label => 'Upload', type => 'text', @@ -297,6 +299,7 @@ sub table_info { disable_select => 1, 'format' => \&FS::UI::bytecount::display_bytecount, 'parse' => \&FS::UI::bytecount::parse_bytecount, + disable_part_svc_column => 1, }, 'downbytes' => { label => 'Download', type => 'text', @@ -304,6 +307,7 @@ sub table_info { disable_select => 1, 'format' => \&FS::UI::bytecount::display_bytecount, 'parse' => \&FS::UI::bytecount::parse_bytecount, + disable_part_svc_column => 1, }, 'totalbytes'=> { label => 'Total up and download', type => 'text', @@ -311,11 +315,13 @@ sub table_info { disable_select => 1, 'format' => \&FS::UI::bytecount::display_bytecount, 'parse' => \&FS::UI::bytecount::parse_bytecount, + disable_part_svc_column => 1, }, 'seconds_threshold' => { label => 'Seconds threshold', type => 'text', disable_inventory => 1, disable_select => 1, + disable_part_svc_column => 1, }, 'upbytes_threshold' => { label => 'Upload threshold', type => 'text', @@ -323,6 +329,7 @@ sub table_info { disable_select => 1, 'format' => \&FS::UI::bytecount::display_bytecount, 'parse' => \&FS::UI::bytecount::parse_bytecount, + disable_part_svc_column => 1, }, 'downbytes_threshold' => { label => 'Download threshold', type => 'text', @@ -330,6 +337,7 @@ sub table_info { disable_select => 1, 'format' => \&FS::UI::bytecount::display_bytecount, 'parse' => \&FS::UI::bytecount::parse_bytecount, + disable_part_svc_column => 1, }, 'totalbytes_threshold'=> { label => 'Total up and download threshold', type => 'text', @@ -337,6 +345,7 @@ sub table_info { disable_select => 1, 'format' => \&FS::UI::bytecount::display_bytecount, 'parse' => \&FS::UI::bytecount::parse_bytecount, + disable_part_svc_column => 1, }, 'last_login'=> { label => 'Last login', @@ -535,6 +544,26 @@ sub insert { return $error; } + # set usage fields and thresholds if unset but set in a package def + if ( $self->pkgnum ) { + my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } ); + my $part_pkg = $cust_pkg->part_pkg if $cust_pkg; + if ( $part_pkg && $part_pkg->can('usage_valuehash') ) { + + my %values = $part_pkg->usage_valuehash; + my $multiplier = $conf->exists('svc_acct-usage_threshold') + ? 1 - $conf->config('svc_acct-usage_threshold')/100 + : 0.20; + + foreach ( keys %values ) { + next if $self->getfield($_); + $self->setfield( $_, $values{$_} ); + $self->setfield( $_. '_threshold', int( $values{$_} * $multiplier ) ); + } + + } + } + my @jobnums; $error = $self->SUPER::insert( 'jobnums' => \@jobnums, @@ -1720,7 +1749,7 @@ sub _op_usage { } sub set_usage { - my( $self, $valueref ) = @_; + my( $self, $valueref, %options ) = @_; warn "$me set_usage called for svcnum ". $self->svcnum. ' ('. $self->email. "): ". @@ -1741,6 +1770,11 @@ sub set_usage { my $reset = 0; my %handyhash = (); + if ( $options{null} ) { + %handyhash = ( map { ( $_ => 'NULL', $_."_threshold" => 'NULL' ) } + qw( seconds upbytes downbytes totalbytes ) + ); + } foreach my $field (keys %$valueref){ $reset = 1 if $valueref->{$field}; $self->setfield($field, $valueref->{$field}); @@ -1759,8 +1793,8 @@ sub set_usage { #die $error if $error; #services not explicity changed via the UI my $sql = "UPDATE svc_acct SET " . - join (',', map { "$_ = ?" } (keys %handyhash) ). - " WHERE svcnum = ?"; + join (',', map { "$_ = $handyhash{$_}" } (keys %handyhash) ). + " WHERE svcnum = ". $self->svcnum; warn "$me $sql\n" if $DEBUG; @@ -1768,7 +1802,7 @@ sub set_usage { if (scalar(keys %handyhash)) { my $sth = $dbh->prepare( $sql ) or die "Error preparing $sql: ". $dbh->errstr; - my $rv = $sth->execute((values %handyhash), $self->svcnum); + my $rv = $sth->execute(); die "Error executing $sql: ". $sth->errstr unless defined($rv); die "Can't update usage for svcnum ". $self->svcnum diff --git a/httemplate/edit/part_svc.cgi b/httemplate/edit/part_svc.cgi index e0fb615b1..deb3c80f9 100755 --- a/httemplate/edit/part_svc.cgi +++ b/httemplate/edit/part_svc.cgi @@ -131,7 +131,14 @@ that field. % % #yucky kludge % my @fields = defined( dbdef->table($layer) ) -% ? grep { $_ ne 'svcnum' } fields($layer) +% ? grep { +% $_ ne 'svcnum' && +% ( !FS::part_svc->svc_table_fields($layer) +% ->{$_}->{disable_part_svc_column} || +% $part_svc->part_svc_column($_)->columnflag +% ) +% } +% fields($layer) % : (); % push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge % $part_svc->svcpart($clone) if $clone; #haha, undone below diff --git a/httemplate/misc/process/recharge_svc.html b/httemplate/misc/process/recharge_svc.html index 147b9533a..4b7155f5d 100755 --- a/httemplate/misc/process/recharge_svc.html +++ b/httemplate/misc/process/recharge_svc.html @@ -27,7 +27,7 @@ % $description, $part_pkg->taxclass); % % if ($part_pkg->option('recharge_reset', 1)) { -% $error ||= $svc_acct->set_usage(\%rhash); +% $error ||= $svc_acct->set_usage(\%rhash, 'null' => 1); % }else{ % $error ||= $svc_acct->recharge(\%rhash); % } -- 2.11.0