X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_bill_pkg.pm;h=f0d710534c9ccc5bb39be760c9edd7dda934e3d5;hb=24bb5c61843c8b58bd627674368b56b0bf61875e;hp=983f62bed82f9013f740a5dd967b4e963bf91205;hpb=63c66b013cbb8429b4f0f3796dd8ea5fe221a2c1;p=freeside.git diff --git a/FS/FS/cust_bill_pkg.pm b/FS/FS/cust_bill_pkg.pm index 983f62bed..f0d710534 100644 --- a/FS/FS/cust_bill_pkg.pm +++ b/FS/FS/cust_bill_pkg.pm @@ -6,7 +6,7 @@ use vars qw( @ISA $DEBUG $me ); use Carp; use List::Util qw( sum min ); use Text::CSV_XS; -use FS::Record qw( qsearch qsearchs dbh ); +use FS::Record qw( qsearch qsearchs dbh fields ); use FS::cust_pkg; use FS::cust_bill_pkg_detail; use FS::cust_bill_pkg_display; @@ -365,8 +365,10 @@ sub void { return $error; } + #more efficiently than below, because there could be lots + $self->void_cust_bill_pkg_detail($reprocess_cdrs); + foreach my $table (qw( - cust_bill_pkg_detail cust_bill_pkg_display cust_bill_pkg_discount cust_bill_pkg_tax_location @@ -374,17 +376,13 @@ sub void { cust_tax_exempt_pkg cust_bill_pkg_fee )) { - my %delete_args = (); - $delete_args{'reprocess_cdrs'} = $reprocess_cdrs - if $table eq 'cust_bill_pkg_detail'; - foreach my $linked ( qsearch($table, { billpkgnum=>$self->billpkgnum }) ) { my $vclass = 'FS::'.$table.'_void'; my $void = $vclass->new( { map { $_ => $linked->get($_) } $linked->fields }); - my $error = $void->insert || $linked->delete(%delete_args); + my $error = $void->insert || $linked->delete; if ( $error ) { $dbh->rollback if $oldAutoCommit; return $error; @@ -394,7 +392,7 @@ sub void { } - $error = $self->delete; + $error = $self->delete( skip_update_cust_bill_charged=>1 ); if ( $error ) { $dbh->rollback if $oldAutoCommit; return $error; @@ -406,6 +404,42 @@ sub void { } +sub void_cust_bill_pkg_detail { + my( $self, $reprocess_cdrs ) = @_; + + my $from_cust_bill_pkg_detail = + 'FROM cust_bill_pkg_detail WHERE billpkgnum = ?'; + my $where_detailnum = + "WHERE detailnum IN ( SELECT detailnum $from_cust_bill_pkg_detail )"; + + if ( $reprocess_cdrs ) { + #well, technically this could have been on other invoices / termination + # partners... separate flag? + $self->scalar_sql( + "DELETE FROM cdr_termination + WHERE acctid IN ( SELECT acctid FROM cdr $where_detailnum ) + ", + $self->billpkgnum + ); + } + + my $setstatus = $reprocess_cdrs ? ', freesidestatus = NULL' : ''; + $self->scalar_sql( + "UPDATE cdr SET detailnum = NULL $setstatus $where_detailnum", + $self->billpkgnum + ); + + my $fields = join(', ', fields('cust_bill_pkg_detail_void') ); + + $self->scalar_sql("INSERT INTO cust_bill_pkg_detail_void ($fields) + SELECT $fields $from_cust_bill_pkg_detail", + $self->billpkgnum + ); + + $self->scalar_sql("DELETE $from_cust_bill_pkg_detail", $self->billpkgnum); + +} + =item delete Not recommended. @@ -414,6 +448,7 @@ Not recommended. sub delete { my $self = shift; + my %opt = @_; local $SIG{HUP} = 'IGNORE'; local $SIG{INT} = 'IGNORE'; @@ -459,6 +494,26 @@ sub delete { } } + unless ( $opt{skip_update_cust_bill_charged} ) { + + #fix the invoice amount + + my $cust_bill = $self->cust_bill; + my $charged = $cust_bill->charged - $self->setup - $self->recur; + $charged = sprintf('%.2f', $charged + 0.00000001 ); + $cust_bill->charged( $charged ); + + #not adding a cc surcharge, but this override lets us modify charged + $cust_bill->{'Hash'}{'cc_surcharge_replace_hack'} = 1; + + my $error = $cust_bill->replace; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + + } + my $error = $self->SUPER::delete(@_); if ( $error ) { $dbh->rollback if $oldAutoCommit; @@ -716,6 +771,7 @@ Returns the customer (L object) for this line item. =cut sub cust_main { + carp "->cust_main called" if $DEBUG; # required for cust_main_Mixin equivalence # and use cust_bill instead of cust_pkg because this might not have a # cust_pkg