X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_pkg.pm;h=088eee9d314e468721ce0e702d5b3a29e3c57563;hb=d7b759d8d95114f5bc9acc3cf9666eaccbd4bfd8;hp=ce9869056862072007a21923a267dec23f1c0e68;hpb=32072dbf59a054529f5304574c0f56f9567d14d0;p=freeside.git diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index ce9869056..088eee9d3 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -30,6 +30,7 @@ use FS::reg_code; use FS::part_svc; use FS::cust_pkg_reason; use FS::reason; +use FS::cust_pkg_usageprice; use FS::cust_pkg_discount; use FS::discount; use FS::UI::Web; @@ -254,6 +255,12 @@ setting I to an array reference of refnums or a hash reference with refnums as keys. If no I is defined, a default FS::pkg_referral record will be created corresponding to cust_main.refnum. +If the additional field I is defined, it will be treated +as an arrayref of FS::cust_pkg_usageprice objects, which will be inserted. +(Note that this field cannot be set with a usual ->cust_pkg_usageprice method. +It can be set as part of the hash when creating the object, or with the B +method.) + The following options are available: =over 4 @@ -329,13 +336,6 @@ sub insert { # set order date unless it was specified as part of an import $self->order_date(time) unless $import && $self->order_date; - 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; @@ -353,6 +353,17 @@ sub insert { 'params' => $self->refnum, ); + if ( $self->hashref->{cust_pkg_usageprice} ) { + for my $cust_pkg_usageprice ( @{ $self->hashref->{cust_pkg_usageprice} } ) { + $cust_pkg_usageprice->pkgnum( $self->pkgnum ); + my $error = $cust_pkg_usageprice->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + } + } + if ( $self->discountnum ) { my $error = $self->insert_discount(); if ( $error ) { @@ -426,13 +437,6 @@ hide cancelled packages. sub delete { my $self = 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; @@ -543,13 +547,6 @@ sub replace { local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart; - 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; @@ -783,13 +780,6 @@ sub cancel { join(', ', map { "$_: $options{$_}" } keys %options ). "\n" if $DEBUG; - 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; @@ -986,13 +976,6 @@ sub uncancel { # Transaction-alize ## - 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; @@ -1147,13 +1130,6 @@ sub unexpire { my( $self, %options ) = @_; my $error; - 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; @@ -1230,13 +1206,6 @@ sub suspend { return $self->main_pkg->suspend(%options); } - 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; @@ -1473,13 +1442,6 @@ sub unsuspend { return $self->main_pkg->unsuspend(%opt); } - 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; @@ -1656,13 +1618,6 @@ sub unadjourn { my( $self, %options ) = @_; my $error; - 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; @@ -1780,13 +1735,6 @@ sub change { my $conf = new FS::Conf; # Transactionize this whole mess - 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; @@ -1807,7 +1755,7 @@ sub change { $error = $opt->{'cust_location'}->find_or_insert; if ( $error ) { $dbh->rollback if $oldAutoCommit; - return "inserting cust_location (transaction rolled back): $error"; + return "creating location record: $error"; } $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum; } @@ -1862,7 +1810,7 @@ sub change { my $error = $cust_main->insert( @{ $opt->{cust_main_insert_args}||[] } ); if ( $error ) { $dbh->rollback if $oldAutoCommit; - return "inserting cust_main (transaction rolled back): $error"; + return "inserting customer record: $error"; } } $custnum = $cust_main->custnum; @@ -1897,7 +1845,7 @@ sub change { } if ($error) { $dbh->rollback if $oldAutoCommit; - return $error; + return "inserting new package: $error"; } # Transfer services and cancel old package. @@ -1906,7 +1854,7 @@ sub change { if ($error and $error == 0) { # $old_pkg->transfer failed. $dbh->rollback if $oldAutoCommit; - return $error; + return "transferring $error"; } if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) { @@ -1915,7 +1863,7 @@ sub change { if ($error and $error == 0) { # $old_pkg->transfer failed. $dbh->rollback if $oldAutoCommit; - return $error; + return "converting $error"; } } @@ -1927,7 +1875,7 @@ sub change { # Transfers were successful, but we still had services left on the old # package. We can't change the package under this circumstances, so abort. $dbh->rollback if $oldAutoCommit; - return "Unable to transfer all services from package ". $self->pkgnum; + return "unable to transfer all services"; } #reset usage if changing pkgpart @@ -1942,7 +1890,7 @@ sub change { if ($error) { $dbh->rollback if $oldAutoCommit; - return "Error setting usage values: $error"; + return "setting usage values: $error"; } } else { # if NOT changing pkgpart, transfer any usage pools over @@ -1951,7 +1899,23 @@ sub change { $error = $usage->replace; if ( $error ) { $dbh->rollback if $oldAutoCommit; - return "Error transferring usage pools: $error"; + return "transferring usage pools: $error"; + } + } + } + + # transfer usage pricing add-ons, if we're not changing pkgpart + if ( $same_pkgpart ) { + foreach my $old_cust_pkg_usageprice ($self->cust_pkg_usageprice) { + my $new_cust_pkg_usageprice = new FS::cust_pkg_usageprice { + 'pkgnum' => $cust_pkg->pkgnum, + 'usagepricepart' => $old_cust_pkg_usageprice->usagepricepart, + 'quantity' => $old_cust_pkg_usageprice->quantity, + }; + $error = $new_cust_pkg_usageprice->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "Error transferring usage pricing add-on: $error"; } } } @@ -1968,7 +1932,7 @@ sub change { $error = $new_discount->insert; if ( $error ) { $dbh->rollback if $oldAutoCommit; - return "Error transferring discounts: $error"; + return "transferring discounts: $error"; } } } @@ -1981,7 +1945,7 @@ sub change { $error = $new_detail->insert; if ( $error ) { $dbh->rollback if $oldAutoCommit; - return "Error transferring package notes: $error"; + return "transferring package notes: $error"; } } @@ -2060,7 +2024,7 @@ sub change { ); if ($error) { $dbh->rollback if $oldAutoCommit; - return $error; + return "canceling old package: $error"; } if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) { @@ -2070,7 +2034,7 @@ sub change { ); if ( $error ) { $dbh->rollback if $oldAutoCommit; - return $error; + return "billing new package: $error"; } } @@ -2264,13 +2228,17 @@ sub set_salesnum { =item modify_charge OPTIONS -Change the properties of a one-time charge. Currently the only properties -that can be changed this way are those that have no impact on billing -calculations: +Change the properties of a one-time charge. The following properties can +be changed this way: - pkg: the package description - classnum: the package class - additional: arrayref of additional invoice details to add to this package +and, I: +- start_date: the date when it will be billed +- amount: the setup fee to be charged +- quantity: the multiplier for the setup fee + If you pass 'adjust_commission' => 1, and the classnum changes, and there are commission credits linked to this charge, they will be recalculated. @@ -2304,14 +2272,51 @@ sub modify_charge { } my $old_classnum; - if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} ) { + if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} ) + { # remember it $old_classnum = $part_pkg->classnum; $part_pkg->set('classnum', $opt{'classnum'}); } + if ( !$self->get('setup') ) { + # not yet billed, so allow amount and quantity + if ( exists($opt{'quantity'}) + and $opt{'quantity'} != $self->quantity + and $opt{'quantity'} > 0 ) { + + $self->set('quantity', $opt{'quantity'}); + } + if ( exists($opt{'start_date'}) + and $opt{'start_date'} != $self->start_date ) { + + $self->set('start_date', $opt{'start_date'}); + } + if ($self->modified) { # for quantity or start_date change + my $error = $self->replace; + return $error if $error; + } + + if ( exists($opt{'amount'}) + and $part_pkg->option('setup_fee') != $opt{'amount'} + and $opt{'amount'} > 0 ) { + + $pkg_opt{'setup_fee'} = $opt{'amount'}; + # standard for one-time charges is to set comment = (formatted) amount + # update it to avoid confusion + my $conf = FS::Conf->new; + $part_pkg->set('comment', + ($conf->config('money_char') || '$') . + sprintf('%.2f', $opt{'amount'}) + ); + } + } # else simply ignore them; the UI shouldn't allow editing the fields + my $error = $part_pkg->replace( options => \%pkg_opt ); - return $error if $error; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } if (defined $old_classnum) { # fix invoice grouping records @@ -2397,13 +2402,6 @@ sub process_bulk_cust_pkg { #my $keep_dates = $param->{'keep_dates'} || 0; my $keep_dates = 1; # there is no good reason to turn this off - 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; @@ -2640,13 +2638,6 @@ If there is an error, returns the error, otherwise returns false. sub set_cust_pkg_detail { my( $self, $detailtype, @details ) = @_; - 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; @@ -2750,12 +2741,12 @@ the results. sub cust_svc { my $self = shift; cluck "cust_pkg->cust_svc called" if $DEBUG > 2; - $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref ); + $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref(@_) ); } sub cust_svc_unsorted { my $self = shift; - @{ $self->cust_svc_unsorted_arrayref }; + @{ $self->cust_svc_unsorted_arrayref(@_) }; } sub cust_svc_unsorted_arrayref { @@ -3134,7 +3125,7 @@ Returns a label for this package. (Currently "pkgnum: pkg - comment" or sub pkg_label { my $self = shift; - my $label = $self->part_pkg->pkg_comment( 'nopkgpart' => 1 ); + my $label = $self->part_pkg->pkg_comment( cust_pkg=>$self, nopkgpart=>1 ); $label = $self->pkgnum. ": $label" if $FS::CurrentUser::CurrentUser->option('show_pkgnum'); $label; @@ -3551,13 +3542,14 @@ sub transfer { } foreach my $cust_svc ($self->cust_svc) { + my $svcnum = $cust_svc->svcnum; if($target{$cust_svc->svcpart} > 0 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option $target{$cust_svc->svcpart}--; my $new = new FS::cust_svc { $cust_svc->hash }; $new->pkgnum($dest_pkgnum); my $error = $new->replace($cust_svc); - return $error if $error; + return "svcnum $svcnum: $error" if $error; } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) { if ( $DEBUG ) { warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n"; @@ -3578,7 +3570,7 @@ sub transfer { $new->svcpart($change_svcpart); $new->pkgnum($dest_pkgnum); my $error = $new->replace($cust_svc); - return $error if $error; + return "svcnum $svcnum: $error" if $error; } else { $remaining++; } @@ -3600,13 +3592,6 @@ sub grab_svcnums { my $self = shift; my @svcnum = @_; - 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; @@ -3641,13 +3626,6 @@ order_pkgs methods in FS::cust_main for a better way to defer provisioning. sub reexport { my $self = 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; @@ -3678,13 +3656,6 @@ Calls the "pkg_change" export action for all services attached to this package. sub export_pkg_change { my( $self, $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; @@ -3856,6 +3827,34 @@ sub recharge { } } +=item apply_usageprice + +=cut + +sub apply_usageprice { + my $self = shift; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + my $error = ''; + + foreach my $cust_pkg_usageprice ( $self->cust_pkg_usageprice ) { + $error ||= $cust_pkg_usageprice->apply; + } + + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + die "error applying part_pkg_usageprice add-ons, pkgnum ". $self->pkgnum. + ": $error\n"; + } else { + $dbh->commit if $oldAutoCommit; + } + + +} + =item cust_pkg_discount =item cust_pkg_discount_active @@ -3896,16 +3895,10 @@ sub apply_usage { my $pkgnum = $self->pkgnum; my $custnum = $self->custnum; - 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 $order = FS::Conf->new->config('cdr-minutes_priority'); my $is_classnum; @@ -4887,13 +4880,6 @@ sub order { my $conf = new FS::Conf; # Transactionize this whole mess - 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; @@ -5033,13 +5019,6 @@ sub bulk_change { my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_; # Transactionize this whole mess - 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;