From 81faa8d34d1287a61fd723d73ab02a022cf5d050 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 12 Apr 2002 15:14:58 +0000 Subject: [PATCH] fudge up FS::cust_pkg::order ("Order and cancel packages") to try to move services between svcparts as a last resort... --- FS/FS/cust_pkg.pm | 59 ++++++++++++++++++++++++++++++++++---------- FS/FS/cust_svc.pm | 39 ++++++++++++++++++++++++++++- httemplate/edit/cust_pkg.cgi | 13 ++++++++-- 3 files changed, 95 insertions(+), 16 deletions(-) diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index b241ecac2..f858a999d 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -556,23 +556,20 @@ sub order { my(%svcnum); # generate %svcnum # for those packages being removed: - #@{ $svcnum{$svcpart} } goes from a svcpart to a list of FS::Record - # objects (table eq 'cust_svc') + #@{ $svcnum{$svcpart} } goes from a svcpart to a list of FS::cust_svc objects my($pkgnum); foreach $pkgnum ( @{$remove_pkgnums} ) { - my($cust_svc); - foreach $cust_svc (qsearch('cust_svc',{'pkgnum'=>$pkgnum})) { + foreach my $cust_svc (qsearch('cust_svc',{'pkgnum'=>$pkgnum})) { push @{ $svcnum{$cust_svc->getfield('svcpart')} }, $cust_svc; } } - my(@cust_svc); + my @cust_svc; #generate @cust_svc # for those packages the customer is purchasing: # @{$pkgparts} is a list of said packages, by pkgpart # @cust_svc is a corresponding list of lists of FS::Record objects - my($pkgpart); - foreach $pkgpart ( @{$pkgparts} ) { + foreach my $pkgpart ( @{$pkgparts} ) { unless ( $part_pkg{$pkgpart} ) { $dbh->rollback if $oldAutoCommit; return "Customer not permitted to purchase pkgpart $pkgpart!"; @@ -584,6 +581,39 @@ sub order { ]; } + #special-case until this can be handled better + # move services to new svcparts - even if the svcparts don't match (svcdb + # needs to...) + # looks like they're moved in no particular order, ewwwwwwww + # and looks like just one of each svcpart can be moved... o well + + #start with still-leftover services + #foreach my $svcpart ( grep { scalar(@{ $svcnum{$_} }) } keys %svcnum ) { + foreach my $svcpart ( keys %svcnum ) { + next unless @{ $svcnum{$svcpart} }; + + my $svcdb = $svcnum{$svcpart}->[0]->part_svc->svcdb; + + #find an empty place to put one + my $i = 0; + foreach my $pkgpart ( @{$pkgparts} ) { + my @pkg_svc = qsearch('pkg_svc', { pkgpart=>$pkgpart } ); + if ( ! @{$cust_svc[$i]} #find an empty place to put them with + && grep { $svcdb eq $_->part_svc->svcdb } #with appropriate svcdb + @pkg_svc + ) { + my $new_svcpart = + ( grep { $svcdb eq $_->part_svc->svcdb } @pkg_svc )[0]->svcpart; + my $cust_svc = shift @{$svcnum{$svcpart}}; + $cust_svc->svcpart($new_svcpart); + #warn "changing from $svcpart to $new_svcpart!!!\n"; + $cust_svc[$i] = [ $cust_svc ]; + } + $i++; + } + + } + #check for leftover services foreach (keys %svcnum) { next unless @{ $svcnum{$_} }; @@ -602,8 +632,7 @@ sub order { local $SIG{PIPE} = 'IGNORE'; #first cancel old packages -# my($pkgnum); - foreach $pkgnum ( @{$remove_pkgnums} ) { + foreach my $pkgnum ( @{$remove_pkgnums} ) { my($old) = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum}); unless ( $old ) { $dbh->rollback if $oldAutoCommit; @@ -620,7 +649,7 @@ sub order { } #now add new packages, changing cust_svc records if necessary -# my($pkgpart); + my $pkgpart; while ($pkgpart=shift @{$pkgparts} ) { my $new = new FS::cust_pkg { @@ -638,8 +667,12 @@ sub order { foreach my $cust_svc ( @{ shift @cust_svc } ) { my(%hash) = $cust_svc->hash; $hash{'pkgnum'}=$pkgnum; - my($new) = new FS::cust_svc ( \%hash ); - my($error)=$new->replace($cust_svc); + my $new = new FS::cust_svc ( \%hash ); + + #avoid Record diffing missing changed svcpart field from above. + my $old = qsearchs('cust_svc', { 'svcnum' => $cust_svc->svcnum } ); + + my $error = $new->replace($old); if ( $error ) { $dbh->rollback if $oldAutoCommit; return "Couldn't link old service to new package: $error"; @@ -656,7 +689,7 @@ sub order { =head1 VERSION -$Id: cust_pkg.pm,v 1.16 2002-01-29 16:33:15 ivan Exp $ +$Id: cust_pkg.pm,v 1.17 2002-04-12 15:14:58 ivan Exp $ =head1 BUGS diff --git a/FS/FS/cust_svc.pm b/FS/FS/cust_svc.pm index 3e38be39e..23a3980ef 100644 --- a/FS/FS/cust_svc.pm +++ b/FS/FS/cust_svc.pm @@ -94,6 +94,43 @@ Called by the cancel method of the package (see L). Replaces the OLD_RECORD with this one in the database. If there is an error, returns the error, otherwise returns false. +=cut + +sub replace { + my ( $new, $old ) = ( shift, shift ); + + local $SIG{HUP} = 'IGNORE'; + local $SIG{INT} = 'IGNORE'; + local $SIG{QUIT} = 'IGNORE'; + local $SIG{TERM} = 'IGNORE'; + local $SIG{TSTP} = 'IGNORE'; + local $SIG{PIPE} = 'IGNORE'; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + my $error = $new->SUPER::replace($old); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error if $error; + } + + if ( $new->svcpart != $old->svcpart ) { + my $svc_x = $new->svc_x; + my $new_svc_x = ref($svc_x)->new({$svc_x->hash}); + my $error = $new_svc_x->replace($svc_x); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error if $error; + } + } + + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + ''; #no error + +} + =item check Checks all fields to make sure this is a valid service. If there is an error, @@ -245,7 +282,7 @@ sub seconds_since { =head1 VERSION -$Id: cust_svc.pm,v 1.12 2002-02-10 22:06:28 ivan Exp $ +$Id: cust_svc.pm,v 1.13 2002-04-12 15:14:58 ivan Exp $ =head1 BUGS diff --git a/httemplate/edit/cust_pkg.cgi b/httemplate/edit/cust_pkg.cgi index d546f7409..485d601eb 100755 --- a/httemplate/edit/cust_pkg.cgi +++ b/httemplate/edit/cust_pkg.cgi @@ -3,7 +3,16 @@ my %pkg = (); my %comment = (); -foreach (qsearch('part_pkg', { 'disabled' => '' })) { +my %all_pkg = (); +my %all_comment = (); +#foreach (qsearch('part_pkg', { 'disabled' => '' })) { +# $pkg{ $_ -> getfield('pkgpart') } = $_->getfield('pkg'); +# $comment{ $_ -> getfield('pkgpart') } = $_->getfield('comment'); +#} +foreach (qsearch('part_pkg', {} )) { + $all_pkg{ $_ -> getfield('pkgpart') } = $_->getfield('pkg'); + $all_comment{ $_ -> getfield('pkgpart') } = $_->getfield('comment'); + next if $_->disabled; $pkg{ $_ -> getfield('pkgpart') } = $_->getfield('pkg'); $comment{ $_ -> getfield('pkgpart') } = $_->getfield('comment'); } @@ -46,7 +55,7 @@ END my($pkgnum,$pkgpart)=( $_->getfield('pkgnum'), $_->getfield('pkgpart') ); print qq!$pkgnum: $pkg{$pkgpart} - $comment{$pkgpart}\n!; + print qq!>$pkgnum: $all_pkg{$pkgpart} - $all_comment{$pkgpart}\n!; $count ++ ; if ($count == 2) { -- 2.11.0