X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_pkg.pm;h=6d90e524ca944de8a3b6637e3cd46371c9c36e42;hb=18b6953b62a1b97f3121100e71bbbfc7d29e75a5;hp=dd4d11c5fb82d79b546e21fa3b659cc79b739b10;hpb=4600f6ce1fa697de8d015c83e49ff61b534ba09d;p=freeside.git diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index dd4d11c5f..6d90e524c 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -4,7 +4,6 @@ use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::Sales_Mixin FS::m2m_Common FS::option_Common ); use strict; -use vars qw( $disable_agentcheck $DEBUG $me $upgrade ); use Carp qw(cluck); use Scalar::Util qw( blessed ); use List::Util qw(min max); @@ -31,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; @@ -49,12 +49,9 @@ use FS::svc_forward; # for sending cancel emails in sub cancel use FS::Conf; -$DEBUG = 0; -$me = '[FS::cust_pkg]'; +our ($disable_agentcheck, $DEBUG, $me, $import) = (0, 0, '[FS::cust_pkg]', 0); -$disable_agentcheck = 0; - -$upgrade = 0; #go away after setup+start dates cleaned up for old customers +our $upgrade = 0; #go away after setup+start dates cleaned up for old customers sub _cache { my $self = shift; @@ -258,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 @@ -298,44 +301,40 @@ sub insert { my $part_pkg = $self->part_pkg; - # if the package def says to start only on the first of the month: - if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) { - my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5]; - $mon += 1 unless $mday == 1; - until ( $mon < 12 ) { $mon -= 12; $year++; } - $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) ); - } - - # set up any automatic expire/adjourn/contract_end timers - # based on the start date - foreach my $action ( qw(expire adjourn contract_end) ) { - my $months = $part_pkg->option("${action}_months",1); - if($months and !$self->$action) { - my $start = $self->start_date || $self->setup || time; - $self->$action( $part_pkg->add_freq($start, $months) ); + if (! $import) { + # if the package def says to start only on the first of the month: + if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) { + my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5]; + $mon += 1 unless $mday == 1; + until ( $mon < 12 ) { $mon -= 12; $year++; } + $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) ); } - } - # if this package has "free days" and delayed setup fee, tehn - # set start date that many days in the future. - # (this should have been set in the UI, but enforce it here) - if ( ! $options{'change'} - && ( my $free_days = $part_pkg->option('free_days',1) ) - && $part_pkg->option('delay_setup',1) - #&& ! $self->start_date - ) - { - $self->start_date( $part_pkg->default_start_date ); - } + # set up any automatic expire/adjourn/contract_end timers + # based on the start date + foreach my $action ( qw(expire adjourn contract_end) ) { + my $months = $part_pkg->option("${action}_months",1); + if($months and !$self->$action) { + my $start = $self->start_date || $self->setup || time; + $self->$action( $part_pkg->add_freq($start, $months) ); + } + } - $self->order_date(time); + # if this package has "free days" and delayed setup fee, tehn + # set start date that many days in the future. + # (this should have been set in the UI, but enforce it here) + if ( ! $options{'change'} + && ( my $free_days = $part_pkg->option('free_days',1) ) + && $part_pkg->option('delay_setup',1) + #&& ! $self->start_date + ) + { + $self->start_date( $part_pkg->default_start_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'; + # set order date unless it was specified as part of an import + $self->order_date(time) unless $import && $self->order_date; my $oldAutoCommit = $FS::UID::AutoCommit; local $FS::UID::AutoCommit = 0; @@ -354,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 ) { @@ -364,7 +374,7 @@ sub insert { my $conf = new FS::Conf; - if ( $conf->config('ticket_system') && $options{ticket_subject} ) { + if ( ! $import && $conf->config('ticket_system') && $options{ticket_subject} ) { #this init stuff is still inefficient, but at least its limited to # the small number (any?) folks using ticket emailing on pkg order @@ -394,7 +404,7 @@ sub insert { ); } - if ($conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) { + if (! $import && $conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) { my $queue = new FS::queue { 'job' => 'FS::cust_main::queueable_print', }; @@ -427,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; @@ -544,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; @@ -784,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; @@ -987,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; @@ -1148,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; @@ -1231,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; @@ -1474,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; @@ -1657,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; @@ -1781,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; @@ -1957,6 +1904,22 @@ sub change { } } + # 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"; + } + } + } + # transfer discounts, if we're not changing pkgpart if ( $same_pkgpart ) { foreach my $old_discount ($self->cust_pkg_discount_active) { @@ -2265,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. @@ -2305,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 @@ -2398,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; @@ -2641,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; @@ -2751,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 { @@ -3135,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; @@ -3314,13 +3304,6 @@ sub _labels_short { Returns the parent customer object (see L). -=cut - -sub cust_main { - my $self = shift; - qsearchs( 'cust_main', { 'custnum' => $self->custnum } ); -} - =item balance Returns the balance for this specific package, when using @@ -3608,13 +3591,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; @@ -3649,13 +3625,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; @@ -3686,13 +3655,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; @@ -3864,15 +3826,36 @@ sub recharge { } } -=item cust_pkg_discount +=item apply_usageprice =cut -sub cust_pkg_discount { +sub apply_usageprice { my $self = shift; - qsearch('cust_pkg_discount', { 'pkgnum' => $self->pkgnum } ); + + 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 =cut @@ -3886,13 +3869,6 @@ sub cust_pkg_discount_active { Returns a list of all voice usage counters attached to this package. -=cut - -sub cust_pkg_usage { - my $self = shift; - qsearch('cust_pkg_usage', { pkgnum => $self->pkgnum }); -} - =item apply_usage OPTIONS Takes the following options: @@ -3918,16 +3894,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; @@ -4909,13 +4879,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; @@ -5055,13 +5018,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;