2 use base qw( FS::cust_pkg::Search FS::cust_pkg::API
3 FS::otaker_Mixin FS::cust_main_Mixin FS::Sales_Mixin
4 FS::contact_Mixin FS::location_Mixin
5 FS::m2m_Common FS::option_Common
10 use Scalar::Util qw( blessed );
11 use List::Util qw(min max);
13 use Time::Local qw( timelocal timelocal_nocheck );
15 use FS::UID qw( dbh driver_name );
16 use FS::Misc qw( send_email );
17 use FS::Record qw( qsearch qsearchs fields );
23 use FS::cust_location;
25 use FS::cust_bill_pkg;
26 use FS::cust_pkg_detail;
27 use FS::cust_pkg_usage;
28 use FS::cdr_cust_pkg_usage;
33 use FS::cust_pkg_reason;
35 use FS::cust_pkg_usageprice;
36 use FS::cust_pkg_discount;
42 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
44 # because they load configuration by setting FS::UID::callback (see TODO)
50 # for sending cancel emails in sub cancel
53 our ($disable_agentcheck, $DEBUG, $me, $import) = (0, 0, '[FS::cust_pkg]', 0);
55 our $upgrade = 0; #go away after setup+start dates cleaned up for old customers
59 my ( $hashref, $cache ) = @_;
60 #if ( $hashref->{'pkgpart'} ) {
61 if ( $hashref->{'pkg'} ) {
62 # #@{ $self->{'_pkgnum'} } = ();
63 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
64 # $self->{'_pkgpart'} = $subcache;
65 # #push @{ $self->{'_pkgnum'} },
66 # FS::part_pkg->new_or_cached($hashref, $subcache);
67 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
69 if ( exists $hashref->{'svcnum'} ) {
70 #@{ $self->{'_pkgnum'} } = ();
71 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
72 $self->{'_svcnum'} = $subcache;
73 #push @{ $self->{'_pkgnum'} },
74 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
80 FS::cust_pkg - Object methods for cust_pkg objects
86 $record = new FS::cust_pkg \%hash;
87 $record = new FS::cust_pkg { 'column' => 'value' };
89 $error = $record->insert;
91 $error = $new_record->replace($old_record);
93 $error = $record->delete;
95 $error = $record->check;
97 $error = $record->cancel;
99 $error = $record->suspend;
101 $error = $record->unsuspend;
103 $part_pkg = $record->part_pkg;
105 @labels = $record->labels;
107 $seconds = $record->seconds_since($timestamp);
109 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
110 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
114 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
115 inherits from FS::Record. The following fields are currently supported:
121 Primary key (assigned automatically for new billing items)
125 Customer (see L<FS::cust_main>)
129 Billing item definition (see L<FS::part_pkg>)
133 Optional link to package location (see L<FS::location>)
137 date package was ordered (also remains same on changes)
149 date (next bill date)
177 order taker (see L<FS::access_user>)
181 If this field is set to 1, disables the automatic
182 unsuspension of this package when using the B<unsuspendauto> config option.
186 If not set, defaults to 1
190 Date of change from previous package
200 =item change_locationnum
208 The pkgnum of the package that this package is supplemental to, if any.
212 The package link (L<FS::part_pkg_link>) that defines this supplemental
213 package, if it is one.
215 =item change_to_pkgnum
217 The pkgnum of the package this one will be "changed to" in the future
218 (on its expiration date).
222 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
223 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
224 L<Time::Local> and L<Date::Parse> for conversion functions.
232 Create a new billing item. To add the item to the database, see L<"insert">.
236 sub table { 'cust_pkg'; }
237 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
238 sub cust_unlinked_msg {
240 "WARNING: can't find cust_main.custnum ". $self->custnum.
241 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
244 =item set_initial_timers
246 If required by the package definition, sets any automatic expire, adjourn,
247 or contract_end timers to some number of months after the start date
248 (or setup date, if the package has already been setup). If the package has
249 a delayed setup fee after a period of "free days", will also set the
250 start date to the end of that period.
254 sub set_initial_timers {
256 my $part_pkg = $self->part_pkg;
257 foreach my $action ( qw(expire adjourn contract_end) ) {
258 my $months = $part_pkg->option("${action}_months",1);
259 if($months and !$self->get($action)) {
260 my $start = $self->start_date || $self->setup || time;
261 $self->set($action, $part_pkg->add_freq($start, $months) );
265 # if this package has "free days" and delayed setup fee, then
266 # set start date that many days in the future.
267 # (this should have been set in the UI, but enforce it here)
268 if ( $part_pkg->option('free_days',1)
269 && $part_pkg->option('delay_setup',1)
272 $self->start_date( $part_pkg->default_start_date );
277 =item insert [ OPTION => VALUE ... ]
279 Adds this billing item to the database ("Orders" the item). If there is an
280 error, returns the error, otherwise returns false.
282 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
283 will be used to look up the package definition and agent restrictions will be
286 If the additional field I<refnum> is defined, an FS::pkg_referral record will
287 be created and inserted. Multiple FS::pkg_referral records can be created by
288 setting I<refnum> to an array reference of refnums or a hash reference with
289 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
290 record will be created corresponding to cust_main.refnum.
292 If the additional field I<cust_pkg_usageprice> is defined, it will be treated
293 as an arrayref of FS::cust_pkg_usageprice objects, which will be inserted.
294 (Note that this field cannot be set with a usual ->cust_pkg_usageprice method.
295 It can be set as part of the hash when creating the object, or with the B<set>
298 The following options are available:
304 If set true, supresses actions that should only be taken for new package
305 orders. (Currently this includes: intro periods when delay_setup is on,
306 auto-adding a 1st start date, auto-adding expiration/adjourn/contract_end dates)
310 cust_pkg_option records will be created
314 a ticket will be added to this customer with this subject
318 an optional queue name for ticket additions
322 Don't check the legality of the package definition. This should be used
323 when performing a package change that doesn't change the pkgpart (i.e.
331 my( $self, %options ) = @_;
334 $error = $self->check_pkgpart unless $options{'allow_pkgpart'};
335 return $error if $error;
337 my $part_pkg = $self->part_pkg;
339 if ( ! $import && ! $options{'change'} ) {
341 # set order date to now
342 $self->order_date(time) unless ($import && $self->order_date);
344 # if the package def says to start only on the first of the month:
345 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
346 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
347 $mon += 1 unless $mday == 1;
348 until ( $mon < 12 ) { $mon -= 12; $year++; }
349 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
352 if ($self->susp eq 'now' or $part_pkg->start_on_hold) {
353 # if the package was ordered on hold:
355 # - don't set the start date (it will be started manually)
356 $self->set('susp', $self->order_date);
357 $self->set('start_date', '');
359 # set expire/adjourn/contract_end timers, and free days, if appropriate
360 $self->set_initial_timers;
362 } # else this is a package change, and shouldn't have "new package" behavior
364 my $oldAutoCommit = $FS::UID::AutoCommit;
365 local $FS::UID::AutoCommit = 0;
368 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
370 $dbh->rollback if $oldAutoCommit;
374 $self->refnum($self->cust_main->refnum) unless $self->refnum;
375 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
376 $self->process_m2m( 'link_table' => 'pkg_referral',
377 'target_table' => 'part_referral',
378 'params' => $self->refnum,
381 if ( $self->hashref->{cust_pkg_usageprice} ) {
382 for my $cust_pkg_usageprice ( @{ $self->hashref->{cust_pkg_usageprice} } ) {
383 $cust_pkg_usageprice->pkgnum( $self->pkgnum );
384 my $error = $cust_pkg_usageprice->insert;
386 $dbh->rollback if $oldAutoCommit;
392 if ( $self->discountnum ) {
393 my $error = $self->insert_discount();
395 $dbh->rollback if $oldAutoCommit;
400 my $conf = new FS::Conf;
402 if ( ! $import && $conf->config('ticket_system') && $options{ticket_subject} ) {
404 #this init stuff is still inefficient, but at least its limited to
405 # the small number (any?) folks using ticket emailing on pkg order
408 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
415 use FS::TicketSystem;
416 FS::TicketSystem->init();
418 my $q = new RT::Queue($RT::SystemUser);
419 $q->Load($options{ticket_queue}) if $options{ticket_queue};
420 my $t = new RT::Ticket($RT::SystemUser);
421 my $mime = new MIME::Entity;
422 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
423 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
424 Subject => $options{ticket_subject},
427 $t->AddLink( Type => 'MemberOf',
428 Target => 'freeside://freeside/cust_main/'. $self->custnum,
432 if (! $import && $conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
433 my $queue = new FS::queue {
434 'job' => 'FS::cust_main::queueable_print',
436 $error = $queue->insert(
437 'custnum' => $self->custnum,
438 'template' => 'welcome_letter',
442 warn "can't send welcome letter: $error";
447 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
454 This method now works but you probably shouldn't use it.
456 You don't want to delete packages, because there would then be no record
457 the customer ever purchased the package. Instead, see the cancel method and
458 hide cancelled packages.
465 my $oldAutoCommit = $FS::UID::AutoCommit;
466 local $FS::UID::AutoCommit = 0;
469 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
470 my $error = $cust_pkg_discount->delete;
472 $dbh->rollback if $oldAutoCommit;
476 #cust_bill_pkg_discount?
478 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
479 my $error = $cust_pkg_detail->delete;
481 $dbh->rollback if $oldAutoCommit;
486 foreach my $cust_pkg_reason (
488 'table' => 'cust_pkg_reason',
489 'hashref' => { 'pkgnum' => $self->pkgnum },
493 my $error = $cust_pkg_reason->delete;
495 $dbh->rollback if $oldAutoCommit;
502 my $error = $self->SUPER::delete(@_);
504 $dbh->rollback if $oldAutoCommit;
508 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
514 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
516 Replaces the OLD_RECORD with this one in the database. If there is an error,
517 returns the error, otherwise returns false.
519 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
521 Changing pkgpart may have disasterous effects. See the order subroutine.
523 setup and bill are normally updated by calling the bill method of a customer
524 object (see L<FS::cust_main>).
526 suspend is normally updated by the suspend and unsuspend methods.
528 cancel is normally updated by the cancel method (and also the order subroutine
531 Available options are:
537 can be set to a cancellation reason (see L<FS:reason>), either a reasonnum of an existing reason, or passing a hashref will create a new reason. The hashref should have the following keys: typenum - Reason type (see L<FS::reason_type>, reason - Text of the new reason.
541 the access_user (see L<FS::access_user>) providing the reason
545 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
554 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
559 ( ref($_[0]) eq 'HASH' )
563 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
564 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
567 #return "Can't change setup once it exists!"
568 # if $old->getfield('setup') &&
569 # $old->getfield('setup') != $new->getfield('setup');
571 #some logic for bill, susp, cancel?
573 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
575 my $oldAutoCommit = $FS::UID::AutoCommit;
576 local $FS::UID::AutoCommit = 0;
579 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
580 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
581 my $error = $new->insert_reason(
582 'reason' => $options->{'reason'},
583 'date' => $new->$method,
585 'reason_otaker' => $options->{'reason_otaker'},
588 dbh->rollback if $oldAutoCommit;
589 return "Error inserting cust_pkg_reason: $error";
594 #save off and freeze RADIUS attributes for any associated svc_acct records
596 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
598 #also check for specific exports?
599 # to avoid spurious modify export events
600 @svc_acct = map { $_->svc_x }
601 grep { $_->part_svc->svcdb eq 'svc_acct' }
604 $_->snapshot foreach @svc_acct;
608 my $error = $new->export_pkg_change($old)
609 || $new->SUPER::replace( $old,
611 ? $options->{options}
615 $dbh->rollback if $oldAutoCommit;
619 #for prepaid packages,
620 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
621 foreach my $old_svc_acct ( @svc_acct ) {
622 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
624 $new_svc_acct->replace( $old_svc_acct,
625 'depend_jobnum' => $options->{depend_jobnum},
628 $dbh->rollback if $oldAutoCommit;
633 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
640 Checks all fields to make sure this is a valid billing item. If there is an
641 error, returns the error, otherwise returns false. Called by the insert and
649 if ( !$self->locationnum or $self->locationnum == -1 ) {
650 $self->set('locationnum', $self->cust_main->ship_locationnum);
654 $self->ut_numbern('pkgnum')
655 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
656 || $self->ut_numbern('pkgpart')
657 || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' )
658 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
659 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
660 || $self->ut_numbern('quantity')
661 || $self->ut_numbern('start_date')
662 || $self->ut_numbern('setup')
663 || $self->ut_numbern('bill')
664 || $self->ut_numbern('susp')
665 || $self->ut_numbern('cancel')
666 || $self->ut_numbern('adjourn')
667 || $self->ut_numbern('resume')
668 || $self->ut_numbern('expire')
669 || $self->ut_numbern('dundate')
670 || $self->ut_flag('no_auto', [ '', 'Y' ])
671 || $self->ut_flag('waive_setup', [ '', 'Y' ])
672 || $self->ut_flag('separate_bill')
673 || $self->ut_textn('agent_pkgid')
674 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
675 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
676 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
677 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
678 || $self->ut_foreign_keyn('change_to_pkgnum', 'cust_pkg', 'pkgnum')
680 return $error if $error;
682 return "A package with both start date (future start) and setup date (already started) will never bill"
683 if $self->start_date && $self->setup && ! $upgrade;
685 return "A future unsuspend date can only be set for a package with a suspend date"
686 if $self->resume and !$self->susp and !$self->adjourn;
688 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
690 if ( $self->dbdef_table->column('manual_flag') ) {
691 $self->manual_flag('') if $self->manual_flag eq ' ';
692 $self->manual_flag =~ /^([01]?)$/
693 or return "Illegal manual_flag ". $self->manual_flag;
694 $self->manual_flag($1);
702 Check the pkgpart to make sure it's allowed with the reg_code and/or
703 promo_code of the package (if present) and with the customer's agent.
704 Called from C<insert>, unless we are doing a package change that doesn't
712 # my $error = $self->ut_numbern('pkgpart'); # already done
715 if ( $self->reg_code ) {
717 unless ( grep { $self->pkgpart == $_->pkgpart }
718 map { $_->reg_code_pkg }
719 qsearchs( 'reg_code', { 'code' => $self->reg_code,
720 'agentnum' => $self->cust_main->agentnum })
722 return "Unknown registration code";
725 } elsif ( $self->promo_code ) {
728 qsearchs('part_pkg', {
729 'pkgpart' => $self->pkgpart,
730 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
732 return 'Unknown promotional code' unless $promo_part_pkg;
736 unless ( $disable_agentcheck ) {
738 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
739 return "agent ". $agent->agentnum. ':'. $agent->agent.
740 " can't purchase pkgpart ". $self->pkgpart
741 unless $agent->pkgpart_hashref->{ $self->pkgpart }
742 || $agent->agentnum == $self->part_pkg->agentnum;
745 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
746 return $error if $error;
754 =item cancel [ OPTION => VALUE ... ]
756 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
757 in this package, then cancels the package itself (sets the cancel field to
760 Available options are:
764 =item quiet - can be set true to supress email cancellation notices.
766 =item time - can be set to cancel the package based on a specific future or
767 historical date. Using time ensures that the remaining amount is calculated
768 correctly. Note however that this is an immediate cancel and just changes
769 the date. You are PROBABLY looking to expire the account instead of using
772 =item reason - can be set to a cancellation reason (see L<FS:reason>),
773 either a reasonnum of an existing reason, or passing a hashref will create
774 a new reason. The hashref should have the following keys: typenum - Reason
775 type (see L<FS::reason_type>, reason - Text of the new reason.
777 =item date - can be set to a unix style timestamp to specify when to
780 =item nobill - can be set true to skip billing if it might otherwise be done.
782 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
783 not credit it. This must be set (by change()) when changing the package
784 to a different pkgpart or location, and probably shouldn't be in any other
785 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
788 =item delay_cancel - for internal use, to allow proper handling of
789 supplemental packages when the main package is flagged to suspend
794 If there is an error, returns the error, otherwise returns false.
799 my( $self, %options ) = @_;
802 # pass all suspend/cancel actions to the main package
803 # (unless the pkglinknum has been removed, then the link is defunct and
804 # this package can be canceled on its own)
805 if ( $self->main_pkgnum and $self->pkglinknum and !$options{'from_main'} ) {
806 return $self->main_pkg->cancel(%options);
809 my $conf = new FS::Conf;
811 warn "cust_pkg::cancel called with options".
812 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
815 my $oldAutoCommit = $FS::UID::AutoCommit;
816 local $FS::UID::AutoCommit = 0;
819 my $old = $self->select_for_update;
821 if ( $old->get('cancel') || $self->get('cancel') ) {
822 dbh->rollback if $oldAutoCommit;
823 return ""; # no error
826 # XXX possibly set cancel_time to the expire date?
827 my $cancel_time = $options{'time'} || time;
828 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
829 $date = '' if ($date && $date <= $cancel_time); # complain instead?
831 my $delay_cancel = $options{'delay_cancel'};
832 if ( !$date && $self->part_pkg->option('delay_cancel',1)
833 && (($self->status eq 'active') || ($self->status eq 'suspended'))
835 my $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
836 my $expsecs = 60*60*24*$expdays;
837 my $suspfor = $self->susp ? $cancel_time - $self->susp : 0;
838 $expsecs = $expsecs - $suspfor if $suspfor;
839 unless ($expsecs <= 0) { #if it's already been suspended long enough, don't re-suspend
841 $date = $cancel_time + $expsecs;
845 #race condition: usage could be ongoing until unprovisioned
846 #resolved by performing a change package instead (which unprovisions) and
848 if ( !$options{nobill} && !$date ) {
849 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
850 my $copy = $self->new({$self->hash});
852 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
854 'time' => $cancel_time );
855 warn "Error billing during cancel, custnum ".
856 #$self->cust_main->custnum. ": $error"
861 if ( $options{'reason'} ) {
862 $error = $self->insert_reason( 'reason' => $options{'reason'},
863 'action' => $date ? 'expire' : 'cancel',
864 'date' => $date ? $date : $cancel_time,
865 'reason_otaker' => $options{'reason_otaker'},
868 dbh->rollback if $oldAutoCommit;
869 return "Error inserting cust_pkg_reason: $error";
873 my %svc_cancel_opt = ();
874 $svc_cancel_opt{'date'} = $date if $date;
875 foreach my $cust_svc (
878 sort { $a->[1] <=> $b->[1] }
879 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
880 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
882 my $part_svc = $cust_svc->part_svc;
883 next if ( defined($part_svc) and $part_svc->preserve );
884 my $error = $cust_svc->cancel( %svc_cancel_opt );
887 $dbh->rollback if $oldAutoCommit;
888 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
894 # credit remaining time if appropriate
896 if ( exists($options{'unused_credit'}) ) {
897 $do_credit = $options{'unused_credit'};
900 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
903 my $error = $self->credit_remaining('cancel', $cancel_time);
905 $dbh->rollback if $oldAutoCommit;
911 my %hash = $self->hash;
913 $hash{'expire'} = $date;
915 # just to be sure these are clear
916 $hash{'adjourn'} = undef;
917 $hash{'resume'} = undef;
920 $hash{'cancel'} = $cancel_time;
922 $hash{'change_custnum'} = $options{'change_custnum'};
924 # if this is a supplemental package that's lost its part_pkg_link, and it's
925 # being canceled for real, unlink it completely
926 if ( !$date and ! $self->pkglinknum ) {
927 $hash{main_pkgnum} = '';
930 my $new = new FS::cust_pkg ( \%hash );
931 $error = $new->replace( $self, options => { $self->options } );
932 if ( $self->change_to_pkgnum ) {
933 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
934 $error ||= $change_to->cancel || $change_to->delete;
937 $dbh->rollback if $oldAutoCommit;
941 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
942 $error = $supp_pkg->cancel(%options,
944 'date' => $date, #in case it got changed by delay_cancel
945 'delay_cancel' => $delay_cancel,
948 $dbh->rollback if $oldAutoCommit;
949 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
953 if ($delay_cancel && !$options{'from_main'}) {
954 $error = $new->suspend(
956 'time' => $cancel_time
961 foreach my $usage ( $self->cust_pkg_usage ) {
962 $error = $usage->delete;
964 $dbh->rollback if $oldAutoCommit;
965 return "deleting usage pools: $error";
970 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
971 return '' if $date; #no errors
973 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
974 if ( !$options{'quiet'} &&
975 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
977 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
980 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
981 $error = $msg_template->send( 'cust_main' => $self->cust_main,
986 'from' => $conf->invoice_from_full( $self->cust_main->agentnum ),
987 'to' => \@invoicing_list,
988 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
989 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
990 'custnum' => $self->custnum,
991 'msgtype' => '', #admin?
994 #should this do something on errors?
1001 =item cancel_if_expired [ NOW_TIMESTAMP ]
1003 Cancels this package if its expire date has been reached.
1007 sub cancel_if_expired {
1009 my $time = shift || time;
1010 return '' unless $self->expire && $self->expire <= $time;
1011 my $error = $self->cancel;
1013 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
1014 $self->custnum. ": $error";
1021 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
1022 locationnum, (other fields?). Attempts to re-provision cancelled services
1023 using history information (errors at this stage are not fatal).
1025 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
1027 svc_fatal: service provisioning errors are fatal
1029 svc_errors: pass an array reference, will be filled in with any provisioning errors
1031 main_pkgnum: link the package as a supplemental package of this one. For
1037 my( $self, %options ) = @_;
1039 #in case you try do do $uncancel-date = $cust_pkg->uncacel
1040 return '' unless $self->get('cancel');
1042 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
1043 return $self->main_pkg->uncancel(%options);
1050 my $oldAutoCommit = $FS::UID::AutoCommit;
1051 local $FS::UID::AutoCommit = 0;
1055 # insert the new package
1058 my $cust_pkg = new FS::cust_pkg {
1059 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
1060 bill => ( $options{'bill'} || $self->get('bill') ),
1062 uncancel_pkgnum => $self->pkgnum,
1063 main_pkgnum => ($options{'main_pkgnum'} || ''),
1064 map { $_ => $self->get($_) } qw(
1065 custnum pkgpart locationnum
1067 susp adjourn resume expire start_date contract_end dundate
1068 change_date change_pkgpart change_locationnum
1069 manual_flag no_auto separate_bill quantity agent_pkgid
1070 recur_show_zero setup_show_zero
1074 my $error = $cust_pkg->insert(
1075 'change' => 1, #supresses any referral credit to a referring customer
1076 'allow_pkgpart' => 1, # allow this even if the package def is disabled
1079 $dbh->rollback if $oldAutoCommit;
1087 #find historical services within this timeframe before the package cancel
1088 # (incompatible with "time" option to cust_pkg->cancel?)
1089 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
1090 # too little? (unprovisioing export delay?)
1091 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
1092 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1095 foreach my $h_cust_svc (@h_cust_svc) {
1096 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1097 #next unless $h_svc_x; #should this happen?
1098 (my $table = $h_svc_x->table) =~ s/^h_//;
1099 require "FS/$table.pm";
1100 my $class = "FS::$table";
1101 my $svc_x = $class->new( {
1102 'pkgnum' => $cust_pkg->pkgnum,
1103 'svcpart' => $h_cust_svc->svcpart,
1104 map { $_ => $h_svc_x->get($_) } fields($table)
1108 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1109 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1112 my $svc_error = $svc_x->insert;
1114 if ( $options{svc_fatal} ) {
1115 $dbh->rollback if $oldAutoCommit;
1118 # if we've failed to insert the svc_x object, svc_Common->insert
1119 # will have removed the cust_svc already. if not, then both records
1120 # were inserted but we failed for some other reason (export, most
1121 # likely). in that case, report the error and delete the records.
1122 push @svc_errors, $svc_error;
1123 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1125 # except if export_insert failed, export_delete probably won't be
1127 local $FS::svc_Common::noexport_hack = 1;
1128 my $cleanup_error = $svc_x->delete; # also deletes cust_svc
1129 if ( $cleanup_error ) { # and if THAT fails, then run away
1130 $dbh->rollback if $oldAutoCommit;
1131 return $cleanup_error;
1136 } #foreach $h_cust_svc
1138 #these are pretty rare, but should handle them
1139 # - dsl_device (mac addresses)
1140 # - phone_device (mac addresses)
1141 # - dsl_note (ikano notes)
1142 # - domain_record (i.e. restore DNS information w/domains)
1143 # - inventory_item(?) (inventory w/un-cancelling service?)
1144 # - nas (svc_broaband nas stuff)
1145 #this stuff is unused in the wild afaik
1146 # - mailinglistmember
1148 # - svc_domain.parent_svcnum?
1149 # - acct_snarf (ancient mail fetching config)
1150 # - cgp_rule (communigate)
1151 # - cust_svc_option (used by our Tron stuff)
1152 # - acct_rt_transaction (used by our time worked stuff)
1155 # also move over any services that didn't unprovision at cancellation
1158 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1159 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1160 my $error = $cust_svc->replace;
1162 $dbh->rollback if $oldAutoCommit;
1168 # Uncancel any supplemental packages, and make them supplemental to the
1172 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1174 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1176 $dbh->rollback if $oldAutoCommit;
1177 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1185 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1187 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1188 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1195 Cancels any pending expiration (sets the expire field to null).
1197 If there is an error, returns the error, otherwise returns false.
1202 my( $self, %options ) = @_;
1205 my $oldAutoCommit = $FS::UID::AutoCommit;
1206 local $FS::UID::AutoCommit = 0;
1209 my $old = $self->select_for_update;
1211 my $pkgnum = $old->pkgnum;
1212 if ( $old->get('cancel') || $self->get('cancel') ) {
1213 dbh->rollback if $oldAutoCommit;
1214 return "Can't unexpire cancelled package $pkgnum";
1215 # or at least it's pointless
1218 unless ( $old->get('expire') && $self->get('expire') ) {
1219 dbh->rollback if $oldAutoCommit;
1220 return ""; # no error
1223 my %hash = $self->hash;
1224 $hash{'expire'} = '';
1225 my $new = new FS::cust_pkg ( \%hash );
1226 $error = $new->replace( $self, options => { $self->options } );
1228 $dbh->rollback if $oldAutoCommit;
1232 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1238 =item suspend [ OPTION => VALUE ... ]
1240 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1241 package, then suspends the package itself (sets the susp field to now).
1243 Available options are:
1247 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1248 either a reasonnum of an existing reason, or passing a hashref will create
1249 a new reason. The hashref should have the following keys:
1250 - typenum - Reason type (see L<FS::reason_type>
1251 - reason - Text of the new reason.
1253 =item date - can be set to a unix style timestamp to specify when to
1256 =item time - can be set to override the current time, for calculation
1257 of final invoices or unused-time credits
1259 =item resume_date - can be set to a time when the package should be
1260 unsuspended. This may be more convenient than calling C<unsuspend()>
1263 =item from_main - allows a supplemental package to be suspended, rather
1264 than redirecting the method call to its main package. For internal use.
1266 =item from_cancel - used when suspending from the cancel method, forces
1267 this to skip everything besides basic suspension. For internal use.
1271 If there is an error, returns the error, otherwise returns false.
1276 my( $self, %options ) = @_;
1279 # pass all suspend/cancel actions to the main package
1280 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1281 return $self->main_pkg->suspend(%options);
1284 my $oldAutoCommit = $FS::UID::AutoCommit;
1285 local $FS::UID::AutoCommit = 0;
1288 my $old = $self->select_for_update;
1290 my $pkgnum = $old->pkgnum;
1291 if ( $old->get('cancel') || $self->get('cancel') ) {
1292 dbh->rollback if $oldAutoCommit;
1293 return "Can't suspend cancelled package $pkgnum";
1296 if ( $old->get('susp') || $self->get('susp') ) {
1297 dbh->rollback if $oldAutoCommit;
1298 return ""; # no error # complain on adjourn?
1301 my $suspend_time = $options{'time'} || time;
1302 my $date = $options{date} if $options{date}; # adjourn/suspend later
1303 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1305 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1306 dbh->rollback if $oldAutoCommit;
1307 return "Package $pkgnum expires before it would be suspended.";
1310 # some false laziness with sub cancel
1311 if ( !$options{nobill} && !$date && !$options{'from_cancel'} &&
1312 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1313 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1314 # make the entire cust_main->bill path recognize 'suspend' and
1315 # 'cancel' separately.
1316 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1317 my $copy = $self->new({$self->hash});
1319 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1321 'time' => $suspend_time );
1322 warn "Error billing during suspend, custnum ".
1323 #$self->cust_main->custnum. ": $error"
1328 if ( $options{'reason'} ) {
1329 $error = $self->insert_reason( 'reason' => $options{'reason'},
1330 'action' => $date ? 'adjourn' : 'suspend',
1331 'date' => $date ? $date : $suspend_time,
1332 'reason_otaker' => $options{'reason_otaker'},
1335 dbh->rollback if $oldAutoCommit;
1336 return "Error inserting cust_pkg_reason: $error";
1340 # if a reasonnum was passed, get the actual reason object so we can check
1342 # (passing a reason hashref is still allowed, but it can't be used with
1343 # the fancy behavioral options.)
1346 if ($options{'reason'} =~ /^\d+$/) {
1347 $reason = FS::reason->by_key($options{'reason'});
1350 my %hash = $self->hash;
1352 $hash{'adjourn'} = $date;
1354 $hash{'susp'} = $suspend_time;
1357 my $resume_date = $options{'resume_date'} || 0;
1358 if ( $resume_date > ($date || $suspend_time) ) {
1359 $hash{'resume'} = $resume_date;
1362 $options{options} ||= {};
1364 my $new = new FS::cust_pkg ( \%hash );
1365 $error = $new->replace( $self, options => { $self->options,
1366 %{ $options{options} },
1370 $dbh->rollback if $oldAutoCommit;
1374 unless ( $date ) { # then we are suspending now
1376 unless ($options{'from_cancel'}) {
1377 # credit remaining time if appropriate
1378 # (if required by the package def, or the suspend reason)
1379 my $unused_credit = $self->part_pkg->option('unused_credit_suspend',1)
1380 || ( defined($reason) && $reason->unused_credit );
1382 if ( $unused_credit ) {
1383 warn "crediting unused time on pkg#".$self->pkgnum."\n" if $DEBUG;
1384 my $error = $self->credit_remaining('suspend', $suspend_time);
1386 $dbh->rollback if $oldAutoCommit;
1394 foreach my $cust_svc (
1395 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
1397 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1399 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1400 $dbh->rollback if $oldAutoCommit;
1401 return "Illegal svcdb value in part_svc!";
1404 require "FS/$svcdb.pm";
1406 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1408 $error = $svc->suspend;
1410 $dbh->rollback if $oldAutoCommit;
1413 my( $label, $value ) = $cust_svc->label;
1414 push @labels, "$label: $value";
1418 my $conf = new FS::Conf;
1419 if ( $conf->config('suspend_email_admin') && !$options{'from_cancel'} ) {
1421 my $error = send_email(
1422 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1423 #invoice_from ??? well as good as any
1424 'to' => $conf->config('suspend_email_admin'),
1425 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1427 "This is an automatic message from your Freeside installation\n",
1428 "informing you that the following customer package has been suspended:\n",
1430 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1431 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1432 ( map { "Service : $_\n" } @labels ),
1434 'custnum' => $self->custnum,
1435 'msgtype' => 'admin'
1439 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1447 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1448 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1450 $dbh->rollback if $oldAutoCommit;
1451 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1455 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1460 =item credit_remaining MODE TIME
1462 Generate a credit for this package for the time remaining in the current
1463 billing period. MODE is either "suspend" or "cancel" (determines the
1464 credit type). TIME is the time of suspension/cancellation. Both arguments
1469 # Implementation note:
1471 # If you pkgpart-change a package that has been billed, and it's set to give
1472 # credit on package change, then this method gets called and then the new
1473 # package will have no last_bill date. Therefore the customer will be credited
1474 # only once (per billing period) even if there are multiple package changes.
1476 # If you location-change a package that has been billed, this method will NOT
1477 # be called and the new package WILL have the last bill date of the old
1480 # If the new package is then canceled within the same billing cycle,
1481 # credit_remaining needs to run calc_remain on the OLD package to determine
1482 # the amount of unused time to credit.
1484 sub credit_remaining {
1485 # Add a credit for remaining service
1486 my ($self, $mode, $time) = @_;
1487 die 'credit_remaining requires suspend or cancel'
1488 unless $mode eq 'suspend' or $mode eq 'cancel';
1489 die 'no suspend/cancel time' unless $time > 0;
1491 my $conf = FS::Conf->new;
1492 my $reason_type = $conf->config($mode.'_credit_type');
1494 my $last_bill = $self->getfield('last_bill') || 0;
1495 my $next_bill = $self->getfield('bill') || 0;
1496 if ( $last_bill > 0 # the package has been billed
1497 and $next_bill > 0 # the package has a next bill date
1498 and $next_bill >= $time # which is in the future
1500 my @cust_credit_source_bill_pkg = ();
1501 my $remaining_value = 0;
1503 my $remain_pkg = $self;
1504 $remaining_value = $remain_pkg->calc_remain(
1506 'cust_credit_source_bill_pkg' => \@cust_credit_source_bill_pkg,
1509 # we may have to walk back past some package changes to get to the
1510 # one that actually has unused time
1511 while ( $remaining_value == 0 ) {
1512 if ( $remain_pkg->change_pkgnum ) {
1513 $remain_pkg = FS::cust_pkg->by_key($remain_pkg->change_pkgnum);
1515 # the package has really never been billed
1518 $remaining_value = $remain_pkg->calc_remain(
1520 'cust_credit_source_bill_pkg' => \@cust_credit_source_bill_pkg,
1524 if ( $remaining_value > 0 ) {
1525 warn "Crediting for $remaining_value on package ".$self->pkgnum."\n"
1527 my $error = $self->cust_main->credit(
1529 'Credit for unused time on '. $self->part_pkg->pkg,
1530 'reason_type' => $reason_type,
1531 'cust_credit_source_bill_pkg' => \@cust_credit_source_bill_pkg,
1533 return "Error crediting customer \$$remaining_value for unused time".
1534 " on ". $self->part_pkg->pkg. ": $error"
1536 } #if $remaining_value
1537 } #if $last_bill, etc.
1541 =item unsuspend [ OPTION => VALUE ... ]
1543 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1544 package, then unsuspends the package itself (clears the susp field and the
1545 adjourn field if it is in the past). If the suspend reason includes an
1546 unsuspension package, that package will be ordered.
1548 Available options are:
1554 Can be set to a date to unsuspend the package in the future (the 'resume'
1557 =item adjust_next_bill
1559 Can be set true to adjust the next bill date forward by
1560 the amount of time the account was inactive. This was set true by default
1561 in the past (from 1.4.2 and 1.5.0pre6 through 1.7.0), but now needs to be
1562 explicitly requested with this option or in the price plan.
1566 If there is an error, returns the error, otherwise returns false.
1571 my( $self, %opt ) = @_;
1574 # pass all suspend/cancel actions to the main package
1575 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1576 return $self->main_pkg->unsuspend(%opt);
1579 my $oldAutoCommit = $FS::UID::AutoCommit;
1580 local $FS::UID::AutoCommit = 0;
1583 my $old = $self->select_for_update;
1585 my $pkgnum = $old->pkgnum;
1586 if ( $old->get('cancel') || $self->get('cancel') ) {
1587 $dbh->rollback if $oldAutoCommit;
1588 return "Can't unsuspend cancelled package $pkgnum";
1591 unless ( $old->get('susp') && $self->get('susp') ) {
1592 $dbh->rollback if $oldAutoCommit;
1593 return ""; # no error # complain instead?
1596 # handle the case of setting a future unsuspend (resume) date
1597 # and do not continue to actually unsuspend the package
1598 my $date = $opt{'date'};
1599 if ( $date and $date > time ) { # return an error if $date <= time?
1601 if ( $old->get('expire') && $old->get('expire') < $date ) {
1602 $dbh->rollback if $oldAutoCommit;
1603 return "Package $pkgnum expires before it would be unsuspended.";
1606 my $new = new FS::cust_pkg { $self->hash };
1607 $new->set('resume', $date);
1608 $error = $new->replace($self, options => $self->options);
1611 $dbh->rollback if $oldAutoCommit;
1615 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1621 if (!$self->setup) {
1622 # then this package is being released from on-hold status
1623 $self->set_initial_timers;
1628 foreach my $cust_svc (
1629 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1631 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1633 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1634 $dbh->rollback if $oldAutoCommit;
1635 return "Illegal svcdb value in part_svc!";
1638 require "FS/$svcdb.pm";
1640 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1642 $error = $svc->unsuspend;
1644 $dbh->rollback if $oldAutoCommit;
1647 my( $label, $value ) = $cust_svc->label;
1648 push @labels, "$label: $value";
1653 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1654 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
1656 my %hash = $self->hash;
1657 my $inactive = time - $hash{'susp'};
1659 my $conf = new FS::Conf;
1661 #adjust the next bill date forward
1662 # increment next bill date if certain conditions are met:
1663 # - it was due to be billed at some point
1664 # - either the global or local config says to do this
1665 my $adjust_bill = 0;
1668 && ( $hash{'bill'} || $hash{'setup'} )
1669 && ( $opt{'adjust_next_bill'}
1670 || $conf->exists('unsuspend-always_adjust_next_bill_date')
1671 || $self->part_pkg->option('unsuspend_adjust_bill', 1)
1678 # - the package billed during suspension
1679 # - or it was ordered on hold
1680 # - or the customer was credited for the unused time
1682 if ( $self->option('suspend_bill',1)
1683 or ( $self->part_pkg->option('suspend_bill',1)
1684 and ! $self->option('no_suspend_bill',1)
1686 or $hash{'order_date'} == $hash{'susp'}
1687 or $self->part_pkg->option('unused_credit_suspend')
1688 or ( ref($reason) and $reason->unused_credit )
1693 # then add the length of time suspended to the bill date
1694 if ( $adjust_bill ) {
1695 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive
1699 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
1700 $hash{'resume'} = '' if !$hash{'adjourn'};
1701 my $new = new FS::cust_pkg ( \%hash );
1702 $error = $new->replace( $self, options => { $self->options } );
1704 $dbh->rollback if $oldAutoCommit;
1710 if ( $reason && $reason->unsuspend_pkgpart ) {
1711 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
1712 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
1714 my $start_date = $self->cust_main->next_bill_date
1715 if $reason->unsuspend_hold;
1718 $unsusp_pkg = FS::cust_pkg->new({
1719 'custnum' => $self->custnum,
1720 'pkgpart' => $reason->unsuspend_pkgpart,
1721 'start_date' => $start_date,
1722 'locationnum' => $self->locationnum,
1723 # discount? probably not...
1726 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
1730 $dbh->rollback if $oldAutoCommit;
1735 if ( $conf->config('unsuspend_email_admin') ) {
1737 my $error = send_email(
1738 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1739 #invoice_from ??? well as good as any
1740 'to' => $conf->config('unsuspend_email_admin'),
1741 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
1742 "This is an automatic message from your Freeside installation\n",
1743 "informing you that the following customer package has been unsuspended:\n",
1745 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1746 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1747 ( map { "Service : $_\n" } @labels ),
1749 "An unsuspension fee was charged: ".
1750 $unsusp_pkg->part_pkg->pkg_comment."\n"
1754 'custnum' => $self->custnum,
1755 'msgtype' => 'admin',
1759 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
1765 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1766 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
1768 $dbh->rollback if $oldAutoCommit;
1769 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1773 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1780 Cancels any pending suspension (sets the adjourn field to null).
1782 If there is an error, returns the error, otherwise returns false.
1787 my( $self, %options ) = @_;
1790 my $oldAutoCommit = $FS::UID::AutoCommit;
1791 local $FS::UID::AutoCommit = 0;
1794 my $old = $self->select_for_update;
1796 my $pkgnum = $old->pkgnum;
1797 if ( $old->get('cancel') || $self->get('cancel') ) {
1798 dbh->rollback if $oldAutoCommit;
1799 return "Can't unadjourn cancelled package $pkgnum";
1800 # or at least it's pointless
1803 if ( $old->get('susp') || $self->get('susp') ) {
1804 dbh->rollback if $oldAutoCommit;
1805 return "Can't unadjourn suspended package $pkgnum";
1806 # perhaps this is arbitrary
1809 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1810 dbh->rollback if $oldAutoCommit;
1811 return ""; # no error
1814 my %hash = $self->hash;
1815 $hash{'adjourn'} = '';
1816 $hash{'resume'} = '';
1817 my $new = new FS::cust_pkg ( \%hash );
1818 $error = $new->replace( $self, options => { $self->options } );
1820 $dbh->rollback if $oldAutoCommit;
1824 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1831 =item change HASHREF | OPTION => VALUE ...
1833 Changes this package: cancels it and creates a new one, with a different
1834 pkgpart or locationnum or both. All services are transferred to the new
1835 package (no change will be made if this is not possible).
1837 Options may be passed as a list of key/value pairs or as a hash reference.
1844 New locationnum, to change the location for this package.
1848 New FS::cust_location object, to create a new location and assign it
1853 New FS::cust_main object, to create a new customer and assign the new package
1858 New pkgpart (see L<FS::part_pkg>).
1862 New refnum (see L<FS::part_referral>).
1866 New quantity; if unspecified, the new package will have the same quantity
1871 "New" (existing) FS::cust_pkg object. The package's services and other
1872 attributes will be transferred to this package.
1876 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1877 susp, adjourn, cancel, expire, and contract_end) to the new package.
1879 =item unprotect_svcs
1881 Normally, change() will rollback and return an error if some services
1882 can't be transferred (also see the I<cust_pkg-change_svcpart> config option).
1883 If unprotect_svcs is true, this method will transfer as many services as
1884 it can and then unconditionally cancel the old package.
1888 At least one of locationnum, cust_location, pkgpart, refnum, cust_main, or
1889 cust_pkg must be specified (otherwise, what's the point?)
1891 Returns either the new FS::cust_pkg object or a scalar error.
1895 my $err_or_new_cust_pkg = $old_cust_pkg->change
1899 #some false laziness w/order
1902 my $opt = ref($_[0]) ? shift : { @_ };
1904 my $conf = new FS::Conf;
1906 # Transactionize this whole mess
1907 my $oldAutoCommit = $FS::UID::AutoCommit;
1908 local $FS::UID::AutoCommit = 0;
1917 $hash{'setup'} = $time if $self->setup;
1919 $hash{'change_date'} = $time;
1920 $hash{"change_$_"} = $self->$_()
1921 foreach qw( pkgnum pkgpart locationnum );
1923 if ( $opt->{'cust_location'} ) {
1924 $error = $opt->{'cust_location'}->find_or_insert;
1926 $dbh->rollback if $oldAutoCommit;
1927 return "creating location record: $error";
1929 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1932 if ( $opt->{'cust_pkg'} ) {
1933 # treat changing to a package with a different pkgpart as a
1934 # pkgpart change (because it is)
1935 $opt->{'pkgpart'} = $opt->{'cust_pkg'}->pkgpart;
1938 # whether to override pkgpart checking on the new package
1939 my $same_pkgpart = 1;
1940 if ( $opt->{'pkgpart'} and ( $opt->{'pkgpart'} != $self->pkgpart ) ) {
1944 my $unused_credit = 0;
1945 my $keep_dates = $opt->{'keep_dates'};
1946 # Special case. If the pkgpart is changing, and the customer is
1947 # going to be credited for remaining time, don't keep setup, bill,
1948 # or last_bill dates, and DO pass the flag to cancel() to credit
1950 if ( $opt->{'pkgpart'}
1951 and $opt->{'pkgpart'} != $self->pkgpart
1952 and $self->part_pkg->option('unused_credit_change', 1) ) {
1955 $hash{$_} = '' foreach qw(setup bill last_bill);
1958 if ( $keep_dates ) {
1959 foreach my $date ( qw(setup bill last_bill susp adjourn cancel expire
1960 resume start_date contract_end ) ) {
1961 $hash{$date} = $self->getfield($date);
1964 # always keep this date, regardless of anything
1965 # (the date of the package change is in a different field)
1966 $hash{'order_date'} = $self->getfield('order_date');
1968 # allow $opt->{'locationnum'} = '' to specifically set it to null
1969 # (i.e. customer default location)
1970 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
1972 # usually this doesn't matter. the two cases where it does are:
1973 # 1. unused_credit_change + pkgpart change + setup fee on the new package
1975 # 2. (more importantly) changing a package before it's billed
1976 $hash{'waive_setup'} = $self->waive_setup;
1978 my $custnum = $self->custnum;
1979 if ( $opt->{cust_main} ) {
1980 my $cust_main = $opt->{cust_main};
1981 unless ( $cust_main->custnum ) {
1982 my $error = $cust_main->insert( @{ $opt->{cust_main_insert_args}||[] } );
1984 $dbh->rollback if $oldAutoCommit;
1985 return "inserting customer record: $error";
1988 $custnum = $cust_main->custnum;
1991 $hash{'contactnum'} = $opt->{'contactnum'} if $opt->{'contactnum'};
1994 if ( $opt->{'cust_pkg'} ) {
1995 # The target package already exists; update it to show that it was
1996 # changed from this package.
1997 $cust_pkg = $opt->{'cust_pkg'};
1999 foreach ( qw( pkgnum pkgpart locationnum ) ) {
2000 $cust_pkg->set("change_$_", $self->get($_));
2002 $cust_pkg->set('change_date', $time);
2003 $error = $cust_pkg->replace;
2006 # Create the new package.
2007 $cust_pkg = new FS::cust_pkg {
2008 custnum => $custnum,
2009 locationnum => $opt->{'locationnum'},
2010 ( map { $_ => ( $opt->{$_} || $self->$_() ) }
2011 qw( pkgpart quantity refnum salesnum )
2015 $error = $cust_pkg->insert( 'change' => 1,
2016 'allow_pkgpart' => $same_pkgpart );
2019 $dbh->rollback if $oldAutoCommit;
2020 return "inserting new package: $error";
2023 # Transfer services and cancel old package.
2025 $error = $self->transfer($cust_pkg);
2026 if ($error and $error == 0) {
2027 # $old_pkg->transfer failed.
2028 $dbh->rollback if $oldAutoCommit;
2029 return "transferring $error";
2032 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
2033 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
2034 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
2035 if ($error and $error == 0) {
2036 # $old_pkg->transfer failed.
2037 $dbh->rollback if $oldAutoCommit;
2038 return "converting $error";
2042 # We set unprotect_svcs when executing a "future package change". It's
2043 # not a user-interactive operation, so returning an error means the
2044 # package change will just fail. Rather than have that happen, we'll
2045 # let leftover services be deleted.
2046 if ($error > 0 and !$opt->{'unprotect_svcs'}) {
2047 # Transfers were successful, but we still had services left on the old
2048 # package. We can't change the package under this circumstances, so abort.
2049 $dbh->rollback if $oldAutoCommit;
2050 return "unable to transfer all services";
2053 #reset usage if changing pkgpart
2054 # AND usage rollover is off (otherwise adds twice, now and at package bill)
2055 if ($self->pkgpart != $cust_pkg->pkgpart) {
2056 my $part_pkg = $cust_pkg->part_pkg;
2057 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
2061 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
2064 $dbh->rollback if $oldAutoCommit;
2065 return "setting usage values: $error";
2068 # if NOT changing pkgpart, transfer any usage pools over
2069 foreach my $usage ($self->cust_pkg_usage) {
2070 $usage->set('pkgnum', $cust_pkg->pkgnum);
2071 $error = $usage->replace;
2073 $dbh->rollback if $oldAutoCommit;
2074 return "transferring usage pools: $error";
2079 # transfer usage pricing add-ons, if we're not changing pkgpart
2080 if ( $same_pkgpart ) {
2081 foreach my $old_cust_pkg_usageprice ($self->cust_pkg_usageprice) {
2082 my $new_cust_pkg_usageprice = new FS::cust_pkg_usageprice {
2083 'pkgnum' => $cust_pkg->pkgnum,
2084 'usagepricepart' => $old_cust_pkg_usageprice->usagepricepart,
2085 'quantity' => $old_cust_pkg_usageprice->quantity,
2087 $error = $new_cust_pkg_usageprice->insert;
2089 $dbh->rollback if $oldAutoCommit;
2090 return "Error transferring usage pricing add-on: $error";
2095 # transfer discounts, if we're not changing pkgpart
2096 if ( $same_pkgpart ) {
2097 foreach my $old_discount ($self->cust_pkg_discount_active) {
2098 # don't remove the old discount, we may still need to bill that package.
2099 my $new_discount = new FS::cust_pkg_discount {
2100 'pkgnum' => $cust_pkg->pkgnum,
2101 'discountnum' => $old_discount->discountnum,
2102 'months_used' => $old_discount->months_used,
2104 $error = $new_discount->insert;
2106 $dbh->rollback if $oldAutoCommit;
2107 return "transferring discounts: $error";
2112 # transfer (copy) invoice details
2113 foreach my $detail ($self->cust_pkg_detail) {
2114 my $new_detail = FS::cust_pkg_detail->new({ $detail->hash });
2115 $new_detail->set('pkgdetailnum', '');
2116 $new_detail->set('pkgnum', $cust_pkg->pkgnum);
2117 $error = $new_detail->insert;
2119 $dbh->rollback if $oldAutoCommit;
2120 return "transferring package notes: $error";
2126 if ( !$opt->{'cust_pkg'} ) {
2127 # Order any supplemental packages.
2128 my $part_pkg = $cust_pkg->part_pkg;
2129 my @old_supp_pkgs = $self->supplemental_pkgs;
2130 foreach my $link ($part_pkg->supp_part_pkg_link) {
2132 foreach (@old_supp_pkgs) {
2133 if ($_->pkgpart == $link->dst_pkgpart) {
2135 $_->pkgpart(0); # so that it can't match more than once
2139 # false laziness with FS::cust_main::Packages::order_pkg
2140 my $new = FS::cust_pkg->new({
2141 pkgpart => $link->dst_pkgpart,
2142 pkglinknum => $link->pkglinknum,
2143 custnum => $custnum,
2144 main_pkgnum => $cust_pkg->pkgnum,
2145 locationnum => $cust_pkg->locationnum,
2146 start_date => $cust_pkg->start_date,
2147 order_date => $cust_pkg->order_date,
2148 expire => $cust_pkg->expire,
2149 adjourn => $cust_pkg->adjourn,
2150 contract_end => $cust_pkg->contract_end,
2151 refnum => $cust_pkg->refnum,
2152 discountnum => $cust_pkg->discountnum,
2153 waive_setup => $cust_pkg->waive_setup,
2155 if ( $old and $opt->{'keep_dates'} ) {
2156 foreach (qw(setup bill last_bill)) {
2157 $new->set($_, $old->get($_));
2160 $error = $new->insert( allow_pkgpart => $same_pkgpart );
2163 $error ||= $old->transfer($new);
2165 if ( $error and $error > 0 ) {
2166 # no reason why this should ever fail, but still...
2167 $error = "Unable to transfer all services from supplemental package ".
2171 $dbh->rollback if $oldAutoCommit;
2174 push @new_supp_pkgs, $new;
2176 } # if !$opt->{'cust_pkg'}
2177 # because if there is one, then supplemental packages would already
2178 # have been created for it.
2180 #Good to go, cancel old package. Notify 'cancel' of whether to credit
2182 #Don't allow billing the package (preceding period packages and/or
2183 #outstanding usage) if we are keeping dates (i.e. location changing),
2184 #because the new package will be billed for the same date range.
2185 #Supplemental packages are also canceled here.
2187 # during scheduled changes, avoid canceling the package we just
2189 $self->set('change_to_pkgnum' => '');
2191 $error = $self->cancel(
2193 unused_credit => $unused_credit,
2194 nobill => $keep_dates,
2195 change_custnum => ( $self->custnum != $custnum ? $custnum : '' ),
2198 $dbh->rollback if $oldAutoCommit;
2199 return "canceling old package: $error";
2202 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
2204 my $error = $cust_pkg->cust_main->bill(
2205 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
2208 $dbh->rollback if $oldAutoCommit;
2209 return "billing new package: $error";
2213 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2219 =item change_later OPTION => VALUE...
2221 Schedule a package change for a later date. This actually orders the new
2222 package immediately, but sets its start date for a future date, and sets
2223 the current package to expire on the same date.
2225 If the package is already scheduled for a change, this can be called with
2226 'start_date' to change the scheduled date, or with pkgpart and/or
2227 locationnum to modify the package change. To cancel the scheduled change
2228 entirely, see C<abort_change>.
2236 The date for the package change. Required, and must be in the future.
2244 The pkgpart. locationnum, and quantity of the new package, with the same
2245 meaning as in C<change>.
2253 my $opt = ref($_[0]) ? shift : { @_ };
2255 my $oldAutoCommit = $FS::UID::AutoCommit;
2256 local $FS::UID::AutoCommit = 0;
2259 my $cust_main = $self->cust_main;
2261 my $date = delete $opt->{'start_date'} or return 'start_date required';
2263 if ( $date <= time ) {
2264 $dbh->rollback if $oldAutoCommit;
2265 return "start_date $date is in the past";
2270 if ( $self->change_to_pkgnum ) {
2271 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
2272 my $new_pkgpart = $opt->{'pkgpart'}
2273 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $change_to->pkgpart;
2274 my $new_locationnum = $opt->{'locationnum'}
2275 if $opt->{'locationnum'} and $opt->{'locationnum'} != $change_to->locationnum;
2276 my $new_quantity = $opt->{'quantity'}
2277 if $opt->{'quantity'} and $opt->{'quantity'} != $change_to->quantity;
2278 if ( $new_pkgpart or $new_locationnum or $new_quantity ) {
2279 # it hasn't been billed yet, so in principle we could just edit
2280 # it in place (w/o a package change), but that's bad form.
2281 # So change the package according to the new options...
2282 my $err_or_pkg = $change_to->change(%$opt);
2283 if ( ref $err_or_pkg ) {
2284 # Then set that package up for a future start.
2285 $self->set('change_to_pkgnum', $err_or_pkg->pkgnum);
2286 $self->set('expire', $date); # in case it's different
2287 $err_or_pkg->set('start_date', $date);
2288 $err_or_pkg->set('change_date', '');
2289 $err_or_pkg->set('change_pkgnum', '');
2291 $error = $self->replace ||
2292 $err_or_pkg->replace ||
2293 $change_to->cancel ||
2296 $error = $err_or_pkg;
2298 } else { # change the start date only.
2299 $self->set('expire', $date);
2300 $change_to->set('start_date', $date);
2301 $error = $self->replace || $change_to->replace;
2304 $dbh->rollback if $oldAutoCommit;
2307 $dbh->commit if $oldAutoCommit;
2310 } # if $self->change_to_pkgnum
2312 my $new_pkgpart = $opt->{'pkgpart'}
2313 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart;
2314 my $new_locationnum = $opt->{'locationnum'}
2315 if $opt->{'locationnum'} and $opt->{'locationnum'} != $self->locationnum;
2316 my $new_quantity = $opt->{'quantity'}
2317 if $opt->{'quantity'} and $opt->{'quantity'} != $self->quantity;
2319 return '' unless $new_pkgpart or $new_locationnum or $new_quantity; # wouldn't do anything
2321 # allow $opt->{'locationnum'} = '' to specifically set it to null
2322 # (i.e. customer default location)
2323 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2325 my $new = FS::cust_pkg->new( {
2326 custnum => $self->custnum,
2327 locationnum => $opt->{'locationnum'},
2328 start_date => $date,
2329 map { $_ => ( $opt->{$_} || $self->$_() ) }
2330 qw( pkgpart quantity refnum salesnum )
2332 $error = $new->insert('change' => 1,
2333 'allow_pkgpart' => ($new_pkgpart ? 0 : 1));
2335 $self->set('change_to_pkgnum', $new->pkgnum);
2336 $self->set('expire', $date);
2337 $error = $self->replace;
2340 $dbh->rollback if $oldAutoCommit;
2342 $dbh->commit if $oldAutoCommit;
2350 Cancels a future package change scheduled by C<change_later>.
2356 my $pkgnum = $self->change_to_pkgnum;
2357 my $change_to = FS::cust_pkg->by_key($pkgnum) if $pkgnum;
2360 $error = $change_to->cancel || $change_to->delete;
2361 return $error if $error;
2363 $self->set('change_to_pkgnum', '');
2364 $self->set('expire', '');
2368 =item set_quantity QUANTITY
2370 Change the package's quantity field. This is one of the few package properties
2371 that can safely be changed without canceling and reordering the package
2372 (because it doesn't affect tax eligibility). Returns an error or an
2379 $self = $self->replace_old; # just to make sure
2380 $self->quantity(shift);
2384 =item set_salesnum SALESNUM
2386 Change the package's salesnum (sales person) field. This is one of the few
2387 package properties that can safely be changed without canceling and reordering
2388 the package (because it doesn't affect tax eligibility). Returns an error or
2395 $self = $self->replace_old; # just to make sure
2396 $self->salesnum(shift);
2398 # XXX this should probably reassign any credit that's already been given
2401 =item modify_charge OPTIONS
2403 Change the properties of a one-time charge. The following properties can
2404 be changed this way:
2405 - pkg: the package description
2406 - classnum: the package class
2407 - additional: arrayref of additional invoice details to add to this package
2409 and, I<if the charge has not yet been billed>:
2410 - start_date: the date when it will be billed
2411 - amount: the setup fee to be charged
2412 - quantity: the multiplier for the setup fee
2413 - separate_bill: whether to put the charge on a separate invoice
2415 If you pass 'adjust_commission' => 1, and the classnum changes, and there are
2416 commission credits linked to this charge, they will be recalculated.
2423 my $part_pkg = $self->part_pkg;
2424 my $pkgnum = $self->pkgnum;
2427 my $oldAutoCommit = $FS::UID::AutoCommit;
2428 local $FS::UID::AutoCommit = 0;
2430 return "Can't use modify_charge except on one-time charges"
2431 unless $part_pkg->freq eq '0';
2433 if ( length($opt{'pkg'}) and $part_pkg->pkg ne $opt{'pkg'} ) {
2434 $part_pkg->set('pkg', $opt{'pkg'});
2437 my %pkg_opt = $part_pkg->options;
2438 my $pkg_opt_modified = 0;
2440 $opt{'additional'} ||= [];
2443 foreach (grep /^additional/, keys %pkg_opt) {
2444 ($i) = ($_ =~ /^additional_info(\d+)$/);
2445 $old_additional[$i] = $pkg_opt{$_} if $i;
2446 delete $pkg_opt{$_};
2449 for ( $i = 0; exists($opt{'additional'}->[$i]); $i++ ) {
2450 $pkg_opt{ "additional_info$i" } = $opt{'additional'}->[$i];
2451 if (!exists($old_additional[$i])
2452 or $old_additional[$i] ne $opt{'additional'}->[$i])
2454 $pkg_opt_modified = 1;
2457 $pkg_opt_modified = 1 if (scalar(@old_additional) - 1) != $i;
2458 $pkg_opt{'additional_count'} = $i if $i > 0;
2461 if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} )
2464 $old_classnum = $part_pkg->classnum;
2465 $part_pkg->set('classnum', $opt{'classnum'});
2468 if ( !$self->get('setup') ) {
2469 # not yet billed, so allow amount, setup_cost, quantity, start_date,
2472 if ( exists($opt{'amount'})
2473 and $part_pkg->option('setup_fee') != $opt{'amount'}
2474 and $opt{'amount'} > 0 ) {
2476 $pkg_opt{'setup_fee'} = $opt{'amount'};
2477 $pkg_opt_modified = 1;
2480 if ( exists($opt{'setup_cost'})
2481 and $part_pkg->setup_cost != $opt{'setup_cost'}
2482 and $opt{'setup_cost'} > 0 ) {
2484 $part_pkg->set('setup_cost', $opt{'setup_cost'});
2487 if ( exists($opt{'quantity'})
2488 and $opt{'quantity'} != $self->quantity
2489 and $opt{'quantity'} > 0 ) {
2491 $self->set('quantity', $opt{'quantity'});
2494 if ( exists($opt{'start_date'})
2495 and $opt{'start_date'} != $self->start_date ) {
2497 $self->set('start_date', $opt{'start_date'});
2500 if ( exists($opt{'separate_bill'})
2501 and $opt{'separate_bill'} ne $self->separate_bill ) {
2503 $self->set('separate_bill', $opt{'separate_bill'});
2507 } # else simply ignore them; the UI shouldn't allow editing the fields
2510 if ( exists($opt{'taxclass'})
2511 and $part_pkg->taxclass ne $opt{'taxclass'}) {
2513 $part_pkg->set('taxclass', $opt{'taxclass'});
2517 if ( $part_pkg->modified or $pkg_opt_modified ) {
2518 # can we safely modify the package def?
2519 # Yes, if it's not available for purchase, and this is the only instance
2521 if ( $part_pkg->disabled
2522 and FS::cust_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 1
2523 and FS::quotation_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 0
2525 $error = $part_pkg->replace( options => \%pkg_opt );
2528 $part_pkg = $part_pkg->clone;
2529 $part_pkg->set('disabled' => 'Y');
2530 $error = $part_pkg->insert( options => \%pkg_opt );
2531 # and associate this as yet-unbilled package to the new package def
2532 $self->set('pkgpart' => $part_pkg->pkgpart);
2535 $dbh->rollback if $oldAutoCommit;
2540 if ($self->modified) { # for quantity or start_date change, or if we had
2541 # to clone the existing package def
2542 my $error = $self->replace;
2543 return $error if $error;
2545 if (defined $old_classnum) {
2546 # fix invoice grouping records
2547 my $old_catname = $old_classnum
2548 ? FS::pkg_class->by_key($old_classnum)->categoryname
2550 my $new_catname = $opt{'classnum'}
2551 ? $part_pkg->pkg_class->categoryname
2553 if ( $old_catname ne $new_catname ) {
2554 foreach my $cust_bill_pkg ($self->cust_bill_pkg) {
2555 # (there should only be one...)
2556 my @display = qsearch( 'cust_bill_pkg_display', {
2557 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2558 'section' => $old_catname,
2560 foreach (@display) {
2561 $_->set('section', $new_catname);
2562 $error = $_->replace;
2564 $dbh->rollback if $oldAutoCommit;
2568 } # foreach $cust_bill_pkg
2571 if ( $opt{'adjust_commission'} ) {
2572 # fix commission credits...tricky.
2573 foreach my $cust_event ($self->cust_event) {
2574 my $part_event = $cust_event->part_event;
2575 foreach my $table (qw(sales agent)) {
2577 "FS::part_event::Action::Mixin::credit_${table}_pkg_class";
2578 my $credit = qsearchs('cust_credit', {
2579 'eventnum' => $cust_event->eventnum,
2581 if ( $part_event->isa($class) ) {
2582 # Yes, this results in current commission rates being applied
2583 # retroactively to a one-time charge. For accounting purposes
2584 # there ought to be some kind of time limit on doing this.
2585 my $amount = $part_event->_calc_credit($self);
2586 if ( $credit and $credit->amount ne $amount ) {
2587 # Void the old credit.
2588 $error = $credit->void('Package class changed');
2590 $dbh->rollback if $oldAutoCommit;
2591 return "$error (adjusting commission credit)";
2594 # redo the event action to recreate the credit.
2596 eval { $part_event->do_action( $self, $cust_event ) };
2598 $dbh->rollback if $oldAutoCommit;
2601 } # if $part_event->isa($class)
2603 } # foreach $cust_event
2604 } # if $opt{'adjust_commission'}
2605 } # if defined $old_classnum
2607 $dbh->commit if $oldAutoCommit;
2614 sub process_bulk_cust_pkg {
2617 warn Dumper($param) if $DEBUG;
2619 my $old_part_pkg = qsearchs('part_pkg',
2620 { pkgpart => $param->{'old_pkgpart'} });
2621 my $new_part_pkg = qsearchs('part_pkg',
2622 { pkgpart => $param->{'new_pkgpart'} });
2623 die "Must select a new package type\n" unless $new_part_pkg;
2624 #my $keep_dates = $param->{'keep_dates'} || 0;
2625 my $keep_dates = 1; # there is no good reason to turn this off
2627 my $oldAutoCommit = $FS::UID::AutoCommit;
2628 local $FS::UID::AutoCommit = 0;
2631 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
2634 foreach my $old_cust_pkg ( @cust_pkgs ) {
2636 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
2637 if ( $old_cust_pkg->getfield('cancel') ) {
2638 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
2639 $old_cust_pkg->pkgnum."\n"
2643 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
2645 my $error = $old_cust_pkg->change(
2646 'pkgpart' => $param->{'new_pkgpart'},
2647 'keep_dates' => $keep_dates
2649 if ( !ref($error) ) { # change returns the cust_pkg on success
2651 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
2654 $dbh->commit if $oldAutoCommit;
2660 Returns the last bill date, or if there is no last bill date, the setup date.
2661 Useful for billing metered services.
2667 return $self->setfield('last_bill', $_[0]) if @_;
2668 return $self->getfield('last_bill') if $self->getfield('last_bill');
2669 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
2670 'edate' => $self->bill, } );
2671 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
2674 =item last_cust_pkg_reason ACTION
2676 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
2677 Returns false if there is no reason or the package is not currenly ACTION'd
2678 ACTION is one of adjourn, susp, cancel, or expire.
2682 sub last_cust_pkg_reason {
2683 my ( $self, $action ) = ( shift, shift );
2684 my $date = $self->get($action);
2686 'table' => 'cust_pkg_reason',
2687 'hashref' => { 'pkgnum' => $self->pkgnum,
2688 'action' => substr(uc($action), 0, 1),
2691 'order_by' => 'ORDER BY num DESC LIMIT 1',
2695 =item last_reason ACTION
2697 Returns the most recent ACTION FS::reason associated with the package.
2698 Returns false if there is no reason or the package is not currenly ACTION'd
2699 ACTION is one of adjourn, susp, cancel, or expire.
2704 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
2705 $cust_pkg_reason->reason
2706 if $cust_pkg_reason;
2711 Returns the definition for this billing item, as an FS::part_pkg object (see
2718 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
2719 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
2720 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
2725 Returns the cancelled package this package was changed from, if any.
2731 return '' unless $self->change_pkgnum;
2732 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
2735 =item change_cust_main
2737 Returns the customter this package was detached to, if any.
2741 sub change_cust_main {
2743 return '' unless $self->change_custnum;
2744 qsearchs('cust_main', { 'custnum' => $self->change_custnum } );
2749 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
2756 $self->part_pkg->calc_setup($self, @_);
2761 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
2768 $self->part_pkg->calc_recur($self, @_);
2773 Calls the I<base_setup> of the FS::part_pkg object associated with this billing
2780 $self->part_pkg->base_setup($self, @_);
2785 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
2792 $self->part_pkg->base_recur($self, @_);
2797 Calls the I<calc_remain> of the FS::part_pkg object associated with this
2804 $self->part_pkg->calc_remain($self, @_);
2809 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
2816 $self->part_pkg->calc_cancel($self, @_);
2821 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
2827 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
2830 =item cust_pkg_detail [ DETAILTYPE ]
2832 Returns any customer package details for this package (see
2833 L<FS::cust_pkg_detail>).
2835 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2839 sub cust_pkg_detail {
2841 my %hash = ( 'pkgnum' => $self->pkgnum );
2842 $hash{detailtype} = shift if @_;
2844 'table' => 'cust_pkg_detail',
2845 'hashref' => \%hash,
2846 'order_by' => 'ORDER BY weight, pkgdetailnum',
2850 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
2852 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
2854 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2856 If there is an error, returns the error, otherwise returns false.
2860 sub set_cust_pkg_detail {
2861 my( $self, $detailtype, @details ) = @_;
2863 my $oldAutoCommit = $FS::UID::AutoCommit;
2864 local $FS::UID::AutoCommit = 0;
2867 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
2868 my $error = $current->delete;
2870 $dbh->rollback if $oldAutoCommit;
2871 return "error removing old detail: $error";
2875 foreach my $detail ( @details ) {
2876 my $cust_pkg_detail = new FS::cust_pkg_detail {
2877 'pkgnum' => $self->pkgnum,
2878 'detailtype' => $detailtype,
2879 'detail' => $detail,
2881 my $error = $cust_pkg_detail->insert;
2883 $dbh->rollback if $oldAutoCommit;
2884 return "error adding new detail: $error";
2889 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2896 Returns the customer billing events (see L<FS::cust_event>) for this invoice.
2900 #false laziness w/cust_bill.pm
2904 'table' => 'cust_event',
2905 'addl_from' => 'JOIN part_event USING ( eventpart )',
2906 'hashref' => { 'tablenum' => $self->pkgnum },
2907 'extra_sql' => " AND eventtable = 'cust_pkg' ",
2911 =item num_cust_event
2913 Returns the number of customer billing events (see L<FS::cust_event>) for this package.
2917 #false laziness w/cust_bill.pm
2918 sub num_cust_event {
2920 my $sql = "SELECT COUNT(*) ". $self->_from_cust_event_where;
2921 $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref->[0];
2924 =item exists_cust_event
2926 Returns true if there are customer billing events (see L<FS::cust_event>) for this package. More efficient than using num_cust_event.
2930 sub exists_cust_event {
2932 my $sql = "SELECT 1 ". $self->_from_cust_event_where. " LIMIT 1";
2933 my $row = $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref;
2934 $row ? $row->[0] : '';
2937 sub _from_cust_event_where {
2939 " FROM cust_event JOIN part_event USING ( eventpart ) ".
2940 " WHERE tablenum = ? AND eventtable = 'cust_pkg' ";
2944 my( $self, $sql, @args ) = @_;
2945 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
2946 $sth->execute(@args) or die $sth->errstr. " executing $sql";
2950 =item part_pkg_currency_option OPTIONNAME
2952 Returns a two item list consisting of the currency of this customer, if any,
2953 and a value for the provided option. If the customer has a currency, the value
2954 is the option value the given name and the currency (see
2955 L<FS::part_pkg_currency>). Otherwise, if the customer has no currency, is the
2956 regular option value for the given name (see L<FS::part_pkg_option>).
2960 sub part_pkg_currency_option {
2961 my( $self, $optionname ) = @_;
2962 my $part_pkg = $self->part_pkg;
2963 if ( my $currency = $self->cust_main->currency ) {
2964 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
2966 ('', $part_pkg->option($optionname) );
2970 =item cust_svc [ SVCPART ] (old, deprecated usage)
2972 =item cust_svc [ OPTION => VALUE ... ] (current usage)
2974 =item cust_svc_unsorted [ OPTION => VALUE ... ]
2976 Returns the services for this package, as FS::cust_svc objects (see
2977 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
2978 spcififed, returns only the matching services.
2980 As an optimization, use the cust_svc_unsorted version if you are not displaying
2987 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
2988 $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref(@_) );
2991 sub cust_svc_unsorted {
2993 @{ $self->cust_svc_unsorted_arrayref(@_) };
2996 sub cust_svc_unsorted_arrayref {
2999 return [] unless $self->num_cust_svc(@_);
3002 if ( @_ && $_[0] =~ /^\d+/ ) {
3003 $opt{svcpart} = shift;
3004 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
3011 'table' => 'cust_svc',
3012 'hashref' => { 'pkgnum' => $self->pkgnum },
3014 if ( $opt{svcpart} ) {
3015 $search{hashref}->{svcpart} = $opt{'svcpart'};
3017 if ( $opt{'svcdb'} ) {
3018 $search{addl_from} = ' LEFT JOIN part_svc USING ( svcpart ) ';
3019 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{'svcdb'} );
3022 [ qsearch(\%search) ];
3026 =item overlimit [ SVCPART ]
3028 Returns the services for this package which have exceeded their
3029 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
3030 is specified, return only the matching services.
3036 return () unless $self->num_cust_svc(@_);
3037 grep { $_->overlimit } $self->cust_svc(@_);
3040 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3042 Returns historical services for this package created before END TIMESTAMP and
3043 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
3044 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
3045 I<pkg_svc.hidden> flag will be omitted.
3051 warn "$me _h_cust_svc called on $self\n"
3054 my ($end, $start, $mode) = @_;
3056 local($FS::Record::qsearch_qualify_columns) = 0;
3058 my @cust_svc = $self->_sort_cust_svc(
3059 [ qsearch( 'h_cust_svc',
3060 { 'pkgnum' => $self->pkgnum, },
3061 FS::h_cust_svc->sql_h_search(@_),
3065 if ( defined($mode) && $mode eq 'I' ) {
3066 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
3067 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
3073 sub _sort_cust_svc {
3074 my( $self, $arrayref ) = @_;
3077 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
3079 my %pkg_svc = map { $_->svcpart => $_ }
3080 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
3085 my $pkg_svc = $pkg_svc{ $_->svcpart } || '';
3087 $pkg_svc ? $pkg_svc->primary_svc : '',
3088 $pkg_svc ? $pkg_svc->quantity : 0,
3095 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
3097 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
3099 Returns the number of services for this package. Available options are svcpart
3100 and svcdb. If either is spcififed, returns only the matching services.
3107 return $self->{'_num_cust_svc'}
3109 && exists($self->{'_num_cust_svc'})
3110 && $self->{'_num_cust_svc'} =~ /\d/;
3112 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
3116 if ( @_ && $_[0] =~ /^\d+/ ) {
3117 $opt{svcpart} = shift;
3118 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
3124 my $select = 'SELECT COUNT(*) FROM cust_svc ';
3125 my $where = ' WHERE pkgnum = ? ';
3126 my @param = ($self->pkgnum);
3128 if ( $opt{'svcpart'} ) {
3129 $where .= ' AND svcpart = ? ';
3130 push @param, $opt{'svcpart'};
3132 if ( $opt{'svcdb'} ) {
3133 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
3134 $where .= ' AND svcdb = ? ';
3135 push @param, $opt{'svcdb'};
3138 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
3139 $sth->execute(@param) or die $sth->errstr;
3140 $sth->fetchrow_arrayref->[0];
3143 =item available_part_svc
3145 Returns a list of FS::part_svc objects representing services included in this
3146 package but not yet provisioned. Each FS::part_svc object also has an extra
3147 field, I<num_avail>, which specifies the number of available services.
3151 sub available_part_svc {
3154 my $pkg_quantity = $self->quantity || 1;
3156 grep { $_->num_avail > 0 }
3158 my $part_svc = $_->part_svc;
3159 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
3160 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
3162 # more evil encapsulation breakage
3163 if($part_svc->{'Hash'}{'num_avail'} > 0) {
3164 my @exports = $part_svc->part_export_did;
3165 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
3170 $self->part_pkg->pkg_svc;
3173 =item part_svc [ OPTION => VALUE ... ]
3175 Returns a list of FS::part_svc objects representing provisioned and available
3176 services included in this package. Each FS::part_svc object also has the
3177 following extra fields:
3191 (services) - array reference containing the provisioned services, as cust_svc objects
3195 Accepts two options:
3199 =item summarize_size
3201 If true, will omit the extra cust_pkg_svc option for objects where num_cust_svc
3202 is this size or greater.
3204 =item hide_discontinued
3206 If true, will omit looking for services that are no longer avaialble in the
3214 #label -> ($cust_svc->label)[1]
3220 my $pkg_quantity = $self->quantity || 1;
3222 #XXX some sort of sort order besides numeric by svcpart...
3223 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
3225 my $part_svc = $pkg_svc->part_svc;
3226 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3227 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
3228 $part_svc->{'Hash'}{'num_avail'} =
3229 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
3230 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3231 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
3232 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
3233 && $num_cust_svc >= $opt{summarize_size};
3234 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
3236 } $self->part_pkg->pkg_svc;
3238 unless ( $opt{hide_discontinued} ) {
3240 push @part_svc, map {
3242 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3243 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
3244 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
3245 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3246 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
3248 } $self->extra_part_svc;
3255 =item extra_part_svc
3257 Returns a list of FS::part_svc objects corresponding to services in this
3258 package which are still provisioned but not (any longer) available in the
3263 sub extra_part_svc {
3266 my $pkgnum = $self->pkgnum;
3267 #my $pkgpart = $self->pkgpart;
3270 # 'table' => 'part_svc',
3273 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
3274 # WHERE pkg_svc.svcpart = part_svc.svcpart
3275 # AND pkg_svc.pkgpart = ?
3278 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
3279 # LEFT JOIN cust_pkg USING ( pkgnum )
3280 # WHERE cust_svc.svcpart = part_svc.svcpart
3283 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
3286 #seems to benchmark slightly faster... (or did?)
3288 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
3289 my $pkgparts = join(',', @pkgparts);
3292 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
3293 #MySQL doesn't grok DISINCT ON
3294 'select' => 'DISTINCT part_svc.*',
3295 'table' => 'part_svc',
3297 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
3298 AND pkg_svc.pkgpart IN ($pkgparts)
3301 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
3302 LEFT JOIN cust_pkg USING ( pkgnum )
3305 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
3306 'extra_param' => [ [$self->pkgnum=>'int'] ],
3312 Returns a short status string for this package, currently:
3318 =item not yet billed
3320 =item one-time charge
3335 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
3337 return 'cancelled' if $self->get('cancel');
3338 return 'on hold' if $self->susp && ! $self->setup;
3339 return 'suspended' if $self->susp;
3340 return 'not yet billed' unless $self->setup;
3341 return 'one-time charge' if $freq =~ /^(0|$)/;
3345 =item ucfirst_status
3347 Returns the status with the first character capitalized.
3351 sub ucfirst_status {
3352 ucfirst(shift->status);
3357 Class method that returns the list of possible status strings for packages
3358 (see L<the status method|/status>). For example:
3360 @statuses = FS::cust_pkg->statuses();
3364 tie my %statuscolor, 'Tie::IxHash',
3365 'on hold' => 'FF00F5', #brighter purple!
3366 'not yet billed' => '009999', #teal? cyan?
3367 'one-time charge' => '0000CC', #blue #'000000',
3368 'active' => '00CC00',
3369 'suspended' => 'FF9900',
3370 'cancelled' => 'FF0000',
3374 my $self = shift; #could be class...
3375 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
3376 # # mayble split btw one-time vs. recur
3387 Returns a hex triplet color string for this package's status.
3393 $statuscolor{$self->status};
3396 =item is_status_delay_cancel
3398 Returns true if part_pkg has option delay_cancel,
3399 cust_pkg status is 'suspended' and expire is set
3400 to cancel package within the next day (or however
3401 many days are set in global config part_pkg-delay_cancel-days.
3403 This is not a real status, this only meant for hacking display
3404 values, because otherwise treating the package as suspended is
3405 really the whole point of the delay_cancel option.
3409 sub is_status_delay_cancel {
3411 if ( $self->main_pkgnum and $self->pkglinknum ) {
3412 return $self->main_pkg->is_status_delay_cancel;
3414 return 0 unless $self->part_pkg->option('delay_cancel',1);
3415 return 0 unless $self->status eq 'suspended';
3416 return 0 unless $self->expire;
3417 my $conf = new FS::Conf;
3418 my $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
3419 my $expsecs = 60*60*24*$expdays;
3420 return 0 unless $self->expire < time + $expsecs;
3426 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
3427 "pkg - comment" depending on user preference).
3433 my $label = $self->part_pkg->pkg_comment( cust_pkg=>$self, nopkgpart=>1 );
3434 $label = $self->pkgnum. ": $label"
3435 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
3439 =item pkg_label_long
3441 Returns a long label for this package, adding the primary service's label to
3446 sub pkg_label_long {
3448 my $label = $self->pkg_label;
3449 my $cust_svc = $self->primary_cust_svc;
3450 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
3456 Returns a customer-localized label for this package.
3462 $self->part_pkg->pkg_locale( $self->cust_main->locale );
3465 =item primary_cust_svc
3467 Returns a primary service (as FS::cust_svc object) if one can be identified.
3471 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
3473 sub primary_cust_svc {
3476 my @cust_svc = $self->cust_svc;
3478 return '' unless @cust_svc; #no serivces - irrelevant then
3480 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
3482 # primary service as specified in the package definition
3483 # or exactly one service definition with quantity one
3484 my $svcpart = $self->part_pkg->svcpart;
3485 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
3486 return $cust_svc[0] if scalar(@cust_svc) == 1;
3488 #couldn't identify one thing..
3494 Returns a list of lists, calling the label method for all services
3495 (see L<FS::cust_svc>) of this billing item.
3501 map { [ $_->label ] } $self->cust_svc;
3504 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3506 Like the labels method, but returns historical information on services that
3507 were active as of END_TIMESTAMP and (optionally) not cancelled before
3508 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
3509 I<pkg_svc.hidden> flag will be omitted.
3511 Returns a list of lists, calling the label method for all (historical) services
3512 (see L<FS::h_cust_svc>) of this billing item.
3518 warn "$me _h_labels called on $self\n"
3520 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
3525 Like labels, except returns a simple flat list, and shortens long
3526 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3527 identical services to one line that lists the service label and the number of
3528 individual services rather than individual items.
3533 shift->_labels_short( 'labels', @_ );
3536 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
3538 Like h_labels, except returns a simple flat list, and shortens long
3539 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3540 identical services to one line that lists the service label and the number of
3541 individual services rather than individual items.
3545 sub h_labels_short {
3546 shift->_labels_short( 'h_labels', @_ );
3550 my( $self, $method ) = ( shift, shift );
3552 warn "$me _labels_short called on $self with $method method\n"
3555 my $conf = new FS::Conf;
3556 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
3558 warn "$me _labels_short populating \%labels\n"
3562 #tie %labels, 'Tie::IxHash';
3563 push @{ $labels{$_->[0]} }, $_->[1]
3564 foreach $self->$method(@_);
3566 warn "$me _labels_short populating \@labels\n"
3570 foreach my $label ( keys %labels ) {
3572 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
3573 my $num = scalar(@values);
3574 warn "$me _labels_short $num items for $label\n"
3577 if ( $num > $max_same_services ) {
3578 warn "$me _labels_short more than $max_same_services, so summarizing\n"
3580 push @labels, "$label ($num)";
3582 if ( $conf->exists('cust_bill-consolidate_services') ) {
3583 warn "$me _labels_short consolidating services\n"
3585 # push @labels, "$label: ". join(', ', @values);
3587 my $detail = "$label: ";
3588 $detail .= shift(@values). ', '
3590 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
3592 push @labels, $detail;
3594 warn "$me _labels_short done consolidating services\n"
3597 warn "$me _labels_short adding service data\n"
3599 push @labels, map { "$label: $_" } @values;
3610 Returns the parent customer object (see L<FS::cust_main>).
3614 Returns the balance for this specific package, when using
3615 experimental package balance.
3621 $self->cust_main->balance_pkgnum( $self->pkgnum );
3624 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
3628 Returns the location object, if any (see L<FS::cust_location>).
3630 =item cust_location_or_main
3632 If this package is associated with a location, returns the locaiton (see
3633 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
3635 =item location_label [ OPTION => VALUE ... ]
3637 Returns the label of the location object (see L<FS::cust_location>).
3641 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
3643 =item tax_locationnum
3645 Returns the foreign key to a L<FS::cust_location> object for calculating
3646 tax on this package, as determined by the C<tax-pkg_address> and
3647 C<tax-ship_address> configuration flags.
3651 sub tax_locationnum {
3653 my $conf = FS::Conf->new;
3654 if ( $conf->exists('tax-pkg_address') ) {
3655 return $self->locationnum;
3657 elsif ( $conf->exists('tax-ship_address') ) {
3658 return $self->cust_main->ship_locationnum;
3661 return $self->cust_main->bill_locationnum;
3667 Returns the L<FS::cust_location> object for tax_locationnum.
3673 my $conf = FS::Conf->new;
3674 if ( $conf->exists('tax-pkg_address') and $self->locationnum ) {
3675 return FS::cust_location->by_key($self->locationnum);
3677 elsif ( $conf->exists('tax-ship_address') ) {
3678 return $self->cust_main->ship_location;
3681 return $self->cust_main->bill_location;
3685 =item seconds_since TIMESTAMP
3687 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
3688 package have been online since TIMESTAMP, according to the session monitor.
3690 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
3691 L<Time::Local> and L<Date::Parse> for conversion functions.
3696 my($self, $since) = @_;
3699 foreach my $cust_svc (
3700 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
3702 $seconds += $cust_svc->seconds_since($since);
3709 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
3711 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
3712 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
3715 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3716 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3722 sub seconds_since_sqlradacct {
3723 my($self, $start, $end) = @_;
3727 foreach my $cust_svc (
3729 my $part_svc = $_->part_svc;
3730 $part_svc->svcdb eq 'svc_acct'
3731 && scalar($part_svc->part_export_usage);
3734 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
3741 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
3743 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
3744 in this package for sessions ending between TIMESTAMP_START (inclusive) and
3748 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3749 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3754 sub attribute_since_sqlradacct {
3755 my($self, $start, $end, $attrib) = @_;
3759 foreach my $cust_svc (
3761 my $part_svc = $_->part_svc;
3762 scalar($part_svc->part_export_usage);
3765 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
3777 my( $self, $value ) = @_;
3778 if ( defined($value) ) {
3779 $self->setfield('quantity', $value);
3781 $self->getfield('quantity') || 1;
3784 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
3786 Transfers as many services as possible from this package to another package.
3788 The destination package can be specified by pkgnum by passing an FS::cust_pkg
3789 object. The destination package must already exist.
3791 Services are moved only if the destination allows services with the correct
3792 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
3793 this option with caution! No provision is made for export differences
3794 between the old and new service definitions. Probably only should be used
3795 when your exports for all service definitions of a given svcdb are identical.
3796 (attempt a transfer without it first, to move all possible svcpart-matching
3799 Any services that can't be moved remain in the original package.
3801 Returns an error, if there is one; otherwise, returns the number of services
3802 that couldn't be moved.
3807 my ($self, $dest_pkgnum, %opt) = @_;
3813 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
3814 $dest = $dest_pkgnum;
3815 $dest_pkgnum = $dest->pkgnum;
3817 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
3820 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
3822 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
3823 $target{$pkg_svc->svcpart} = $pkg_svc->quantity * ( $dest->quantity || 1 );
3826 foreach my $cust_svc ($dest->cust_svc) {
3827 $target{$cust_svc->svcpart}--;
3830 my %svcpart2svcparts = ();
3831 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3832 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
3833 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
3834 next if exists $svcpart2svcparts{$svcpart};
3835 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
3836 $svcpart2svcparts{$svcpart} = [
3838 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
3840 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
3841 'svcpart' => $_ } );
3843 $pkg_svc ? $pkg_svc->primary_svc : '',
3844 $pkg_svc ? $pkg_svc->quantity : 0,
3848 grep { $_ != $svcpart }
3850 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
3852 warn "alternates for svcpart $svcpart: ".
3853 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
3859 foreach my $cust_svc ($self->cust_svc) {
3860 my $svcnum = $cust_svc->svcnum;
3861 if($target{$cust_svc->svcpart} > 0
3862 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
3863 $target{$cust_svc->svcpart}--;
3864 my $new = new FS::cust_svc { $cust_svc->hash };
3865 $new->pkgnum($dest_pkgnum);
3866 $error = $new->replace($cust_svc);
3867 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3869 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
3870 warn "alternates to consider: ".
3871 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
3873 my @alternate = grep {
3874 warn "considering alternate svcpart $_: ".
3875 "$target{$_} available in new package\n"
3878 } @{$svcpart2svcparts{$cust_svc->svcpart}};
3880 warn "alternate(s) found\n" if $DEBUG;
3881 my $change_svcpart = $alternate[0];
3882 $target{$change_svcpart}--;
3883 my $new = new FS::cust_svc { $cust_svc->hash };
3884 $new->svcpart($change_svcpart);
3885 $new->pkgnum($dest_pkgnum);
3886 $error = $new->replace($cust_svc);
3894 my @label = $cust_svc->label;
3895 return "$label[0] $label[1]: $error";
3901 =item grab_svcnums SVCNUM, SVCNUM ...
3903 Change the pkgnum for the provided services to this packages. If there is an
3904 error, returns the error, otherwise returns false.
3912 my $oldAutoCommit = $FS::UID::AutoCommit;
3913 local $FS::UID::AutoCommit = 0;
3916 foreach my $svcnum (@svcnum) {
3917 my $cust_svc = qsearchs('cust_svc', { svcnum=>$svcnum } ) or do {
3918 $dbh->rollback if $oldAutoCommit;
3919 return "unknown svcnum $svcnum";
3921 $cust_svc->pkgnum( $self->pkgnum );
3922 my $error = $cust_svc->replace;
3924 $dbh->rollback if $oldAutoCommit;
3929 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3936 This method is deprecated. See the I<depend_jobnum> option to the insert and
3937 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
3941 #looks like this is still used by the order_pkg and change_pkg methods in
3942 # ClientAPI/MyAccount, need to look into those before removing
3946 my $oldAutoCommit = $FS::UID::AutoCommit;
3947 local $FS::UID::AutoCommit = 0;
3950 foreach my $cust_svc ( $self->cust_svc ) {
3951 #false laziness w/svc_Common::insert
3952 my $svc_x = $cust_svc->svc_x;
3953 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
3954 my $error = $part_export->export_insert($svc_x);
3956 $dbh->rollback if $oldAutoCommit;
3962 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3967 =item export_pkg_change OLD_CUST_PKG
3969 Calls the "pkg_change" export action for all services attached to this package.
3973 sub export_pkg_change {
3974 my( $self, $old ) = ( shift, shift );
3976 my $oldAutoCommit = $FS::UID::AutoCommit;
3977 local $FS::UID::AutoCommit = 0;
3980 foreach my $svc_x ( map $_->svc_x, $self->cust_svc ) {
3981 my $error = $svc_x->export('pkg_change', $self, $old);
3983 $dbh->rollback if $oldAutoCommit;
3988 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3995 Associates this package with a (suspension or cancellation) reason (see
3996 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
3999 Available options are:
4005 can be set to a cancellation reason (see L<FS:reason>), either a reasonnum of an existing reason, or passing a hashref will create a new reason. The hashref should have the following keys: typenum - Reason type (see L<FS::reason_type>, reason - Text of the new reason.
4009 the access_user (see L<FS::access_user>) providing the reason
4017 the action (cancel, susp, adjourn, expire) associated with the reason
4021 If there is an error, returns the error, otherwise returns false.
4026 my ($self, %options) = @_;
4028 my $otaker = $options{reason_otaker} ||
4029 $FS::CurrentUser::CurrentUser->username;
4032 if ( $options{'reason'} =~ /^(\d+)$/ ) {
4036 } elsif ( ref($options{'reason'}) ) {
4038 return 'Enter a new reason (or select an existing one)'
4039 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
4041 my $reason = new FS::reason({
4042 'reason_type' => $options{'reason'}->{'typenum'},
4043 'reason' => $options{'reason'}->{'reason'},
4045 my $error = $reason->insert;
4046 return $error if $error;
4048 $reasonnum = $reason->reasonnum;
4051 return "Unparseable reason: ". $options{'reason'};
4054 my $cust_pkg_reason =
4055 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
4056 'reasonnum' => $reasonnum,
4057 'otaker' => $otaker,
4058 'action' => substr(uc($options{'action'}),0,1),
4059 'date' => $options{'date'}
4064 $cust_pkg_reason->insert;
4067 =item insert_discount
4069 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
4070 inserting a new discount on the fly (see L<FS::discount>).
4072 Available options are:
4080 If there is an error, returns the error, otherwise returns false.
4084 sub insert_discount {
4085 #my ($self, %options) = @_;
4088 my $cust_pkg_discount = new FS::cust_pkg_discount {
4089 'pkgnum' => $self->pkgnum,
4090 'discountnum' => $self->discountnum,
4092 'end_date' => '', #XXX
4093 #for the create a new discount case
4094 '_type' => $self->discountnum__type,
4095 'amount' => $self->discountnum_amount,
4096 'percent' => $self->discountnum_percent,
4097 'months' => $self->discountnum_months,
4098 'setup' => $self->discountnum_setup,
4099 #'disabled' => $self->discountnum_disabled,
4102 $cust_pkg_discount->insert;
4105 =item set_usage USAGE_VALUE_HASHREF
4107 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4108 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4109 upbytes, downbytes, and totalbytes are appropriate keys.
4111 All svc_accts which are part of this package have their values reset.
4116 my ($self, $valueref, %opt) = @_;
4118 #only svc_acct can set_usage for now
4119 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4120 my $svc_x = $cust_svc->svc_x;
4121 $svc_x->set_usage($valueref, %opt)
4122 if $svc_x->can("set_usage");
4126 =item recharge USAGE_VALUE_HASHREF
4128 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4129 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4130 upbytes, downbytes, and totalbytes are appropriate keys.
4132 All svc_accts which are part of this package have their values incremented.
4137 my ($self, $valueref) = @_;
4139 #only svc_acct can set_usage for now
4140 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4141 my $svc_x = $cust_svc->svc_x;
4142 $svc_x->recharge($valueref)
4143 if $svc_x->can("recharge");
4147 =item apply_usageprice
4151 sub apply_usageprice {
4154 my $oldAutoCommit = $FS::UID::AutoCommit;
4155 local $FS::UID::AutoCommit = 0;
4160 foreach my $cust_pkg_usageprice ( $self->cust_pkg_usageprice ) {
4161 $error ||= $cust_pkg_usageprice->apply;
4165 $dbh->rollback if $oldAutoCommit;
4166 die "error applying part_pkg_usageprice add-ons, pkgnum ". $self->pkgnum.
4169 $dbh->commit if $oldAutoCommit;
4175 =item cust_pkg_discount
4177 =item cust_pkg_discount_active
4181 sub cust_pkg_discount_active {
4183 grep { $_->status eq 'active' } $self->cust_pkg_discount;
4186 =item cust_pkg_usage
4188 Returns a list of all voice usage counters attached to this package.
4190 =item apply_usage OPTIONS
4192 Takes the following options:
4193 - cdr: a call detail record (L<FS::cdr>)
4194 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
4195 - minutes: the maximum number of minutes to be charged
4197 Finds available usage minutes for a call of this class, and subtracts
4198 up to that many minutes from the usage pool. If the usage pool is empty,
4199 and the C<cdr-minutes_priority> global config option is set, minutes may
4200 be taken from other calls as well. Either way, an allocation record will
4201 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
4202 number of minutes of usage applied to the call.
4207 my ($self, %opt) = @_;
4208 my $cdr = $opt{cdr};
4209 my $rate_detail = $opt{rate_detail};
4210 my $minutes = $opt{minutes};
4211 my $classnum = $rate_detail->classnum;
4212 my $pkgnum = $self->pkgnum;
4213 my $custnum = $self->custnum;
4215 my $oldAutoCommit = $FS::UID::AutoCommit;
4216 local $FS::UID::AutoCommit = 0;
4219 my $order = FS::Conf->new->config('cdr-minutes_priority');
4223 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
4225 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
4227 my @usage_recs = qsearch({
4228 'table' => 'cust_pkg_usage',
4229 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
4230 ' JOIN cust_pkg USING (pkgnum)'.
4231 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
4232 'select' => 'cust_pkg_usage.*',
4233 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
4234 " ( cust_pkg.custnum = $custnum AND ".
4235 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4236 $is_classnum . ' AND '.
4237 " cust_pkg_usage.minutes > 0",
4238 'order_by' => " ORDER BY priority ASC",
4241 my $orig_minutes = $minutes;
4243 while (!$error and $minutes > 0 and @usage_recs) {
4244 my $cust_pkg_usage = shift @usage_recs;
4245 $cust_pkg_usage->select_for_update;
4246 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
4247 pkgusagenum => $cust_pkg_usage->pkgusagenum,
4248 acctid => $cdr->acctid,
4249 minutes => min($cust_pkg_usage->minutes, $minutes),
4251 $cust_pkg_usage->set('minutes',
4252 $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes
4254 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
4255 $minutes -= $cdr_cust_pkg_usage->minutes;
4257 if ( $order and $minutes > 0 and !$error ) {
4258 # then try to steal minutes from another call
4260 'table' => 'cdr_cust_pkg_usage',
4261 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
4262 ' JOIN part_pkg_usage USING (pkgusagepart)'.
4263 ' JOIN cust_pkg USING (pkgnum)'.
4264 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
4265 ' JOIN cdr USING (acctid)',
4266 'select' => 'cdr_cust_pkg_usage.*',
4267 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
4268 " ( cust_pkg.pkgnum = $pkgnum OR ".
4269 " ( cust_pkg.custnum = $custnum AND ".
4270 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4271 " part_pkg_usage_class.classnum = $classnum",
4272 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
4274 if ( $order eq 'time' ) {
4275 # find CDRs that are using minutes, but have a later startdate
4277 my $startdate = $cdr->startdate;
4278 if ($startdate !~ /^\d+$/) {
4279 die "bad cdr startdate '$startdate'";
4281 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
4282 # minimize needless reshuffling
4283 $search{'order_by'} .= ', cdr.startdate DESC';
4285 # XXX may not work correctly with rate_time schedules. Could
4286 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
4288 $search{'addl_from'} .=
4289 ' JOIN rate_detail'.
4290 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
4291 if ( $order eq 'rate_high' ) {
4292 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
4293 $rate_detail->min_charge;
4294 $search{'order_by'} .= ', rate_detail.min_charge ASC';
4295 } elsif ( $order eq 'rate_low' ) {
4296 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
4297 $rate_detail->min_charge;
4298 $search{'order_by'} .= ', rate_detail.min_charge DESC';
4300 # this should really never happen
4301 die "invalid cdr-minutes_priority value '$order'\n";
4304 my @cdr_usage_recs = qsearch(\%search);
4306 while (!$error and @cdr_usage_recs and $minutes > 0) {
4307 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
4308 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
4309 my $old_cdr = $cdr_cust_pkg_usage->cdr;
4310 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
4311 $cdr_cust_pkg_usage->select_for_update;
4312 $old_cdr->select_for_update;
4313 $cust_pkg_usage->select_for_update;
4314 # in case someone else stole the usage from this CDR
4315 # while waiting for the lock...
4316 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
4317 # steal the usage allocation and flag the old CDR for reprocessing
4318 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
4319 # if the allocation is more minutes than we need, adjust it...
4320 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
4322 $cdr_cust_pkg_usage->set('minutes', $minutes);
4323 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
4324 $error = $cust_pkg_usage->replace;
4326 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
4327 $error ||= $cdr_cust_pkg_usage->replace;
4328 # deduct the stolen minutes
4329 $minutes -= $cdr_cust_pkg_usage->minutes;
4331 # after all minute-stealing is done, reset the affected CDRs
4332 foreach (values %reproc_cdrs) {
4333 $error ||= $_->set_status('');
4334 # XXX or should we just call $cdr->rate right here?
4335 # it's not like we can create a loop this way, since the min_charge
4336 # or call time has to go monotonically in one direction.
4337 # we COULD get some very deep recursions going, though...
4339 } # if $order and $minutes
4342 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
4344 $dbh->commit if $oldAutoCommit;
4345 return $orig_minutes - $minutes;
4349 =item supplemental_pkgs
4351 Returns a list of all packages supplemental to this one.
4355 sub supplemental_pkgs {
4357 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
4362 Returns the package that this one is supplemental to, if any.
4368 if ( $self->main_pkgnum ) {
4369 return FS::cust_pkg->by_key($self->main_pkgnum);
4376 =head1 CLASS METHODS
4382 Returns an SQL expression identifying recurring packages.
4386 sub recurring_sql { "
4387 '0' != ( select freq from part_pkg
4388 where cust_pkg.pkgpart = part_pkg.pkgpart )
4393 Returns an SQL expression identifying one-time packages.
4398 '0' = ( select freq from part_pkg
4399 where cust_pkg.pkgpart = part_pkg.pkgpart )
4404 Returns an SQL expression identifying ordered packages (recurring packages not
4410 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
4415 Returns an SQL expression identifying active packages.
4420 $_[0]->recurring_sql. "
4421 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4422 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4423 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4426 =item not_yet_billed_sql
4428 Returns an SQL expression identifying packages which have not yet been billed.
4432 sub not_yet_billed_sql { "
4433 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4434 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4435 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4440 Returns an SQL expression identifying inactive packages (one-time packages
4441 that are otherwise unsuspended/uncancelled).
4445 sub inactive_sql { "
4446 ". $_[0]->onetime_sql(). "
4447 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4448 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4449 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4454 Returns an SQL expression identifying on-hold packages.
4459 #$_[0]->recurring_sql(). ' AND '.
4461 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4462 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4463 AND ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4470 Returns an SQL expression identifying suspended packages.
4474 sub suspended_sql { susp_sql(@_); }
4476 #$_[0]->recurring_sql(). ' AND '.
4478 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4479 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4480 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4487 Returns an SQL exprression identifying cancelled packages.
4491 sub cancelled_sql { cancel_sql(@_); }
4493 #$_[0]->recurring_sql(). ' AND '.
4494 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
4499 Returns an SQL expression to give the package status as a string.
4505 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
4506 WHEN ( cust_pkg.susp IS NOT NULL AND cust_pkg.setup IS NULL ) THEN 'on hold'
4507 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
4508 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
4509 WHEN ".onetime_sql()." THEN 'one-time charge'
4516 Returns a list of two package counts. The first is a count of packages
4517 based on the supplied criteria and the second is the count of residential
4518 packages with those same criteria. Criteria are specified as in the search
4524 my ($class, $params) = @_;
4526 my $sql_query = $class->search( $params );
4528 my $count_sql = delete($sql_query->{'count_query'});
4529 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
4530 or die "couldn't parse count_sql";
4532 my $count_sth = dbh->prepare($count_sql)
4533 or die "Error preparing $count_sql: ". dbh->errstr;
4535 or die "Error executing $count_sql: ". $count_sth->errstr;
4536 my $count_arrayref = $count_sth->fetchrow_arrayref;
4538 return ( @$count_arrayref );
4542 =item tax_locationnum_sql
4544 Returns an SQL expression for the tax location for a package, based
4545 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
4549 sub tax_locationnum_sql {
4550 my $conf = FS::Conf->new;
4551 if ( $conf->exists('tax-pkg_address') ) {
4552 'cust_pkg.locationnum';
4554 elsif ( $conf->exists('tax-ship_address') ) {
4555 'cust_main.ship_locationnum';
4558 'cust_main.bill_locationnum';
4564 Returns a list: the first item is an SQL fragment identifying matching
4565 packages/customers via location (taking into account shipping and package
4566 address taxation, if enabled), and subsequent items are the parameters to
4567 substitute for the placeholders in that fragment.
4572 my($class, %opt) = @_;
4573 my $ornull = $opt{'ornull'};
4575 my $conf = new FS::Conf;
4577 # '?' placeholders in _location_sql_where
4578 my $x = $ornull ? 3 : 2;
4589 if ( $conf->exists('tax-ship_address') ) {
4592 ( ( ship_last IS NULL OR ship_last = '' )
4593 AND ". _location_sql_where('cust_main', '', $ornull ). "
4595 OR ( ship_last IS NOT NULL AND ship_last != ''
4596 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
4599 # AND payby != 'COMP'
4601 @main_param = ( @bill_param, @bill_param );
4605 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
4606 @main_param = @bill_param;
4612 if ( $conf->exists('tax-pkg_address') ) {
4614 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
4617 ( cust_pkg.locationnum IS NULL AND $main_where )
4618 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
4621 @param = ( @main_param, @bill_param );
4625 $where = $main_where;
4626 @param = @main_param;
4634 #subroutine, helper for location_sql
4635 sub _location_sql_where {
4637 my $prefix = @_ ? shift : '';
4638 my $ornull = @_ ? shift : '';
4640 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
4642 $ornull = $ornull ? ' OR ? IS NULL ' : '';
4644 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
4645 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
4646 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
4648 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
4650 # ( $table.${prefix}city = ? $or_empty_city $ornull )
4652 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4653 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4654 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
4655 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
4656 AND $table.${prefix}country = ?
4661 my( $self, $what ) = @_;
4663 my $what_show_zero = $what. '_show_zero';
4664 length($self->$what_show_zero())
4665 ? ($self->$what_show_zero() eq 'Y')
4666 : $self->part_pkg->$what_show_zero();
4673 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
4675 CUSTNUM is a customer (see L<FS::cust_main>)
4677 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4678 L<FS::part_pkg>) to order for this customer. Duplicates are of course
4681 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
4682 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
4683 new billing items. An error is returned if this is not possible (see
4684 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
4687 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4688 newly-created cust_pkg objects.
4690 REFNUM, if specified, will specify the FS::pkg_referral record to be created
4691 and inserted. Multiple FS::pkg_referral records can be created by
4692 setting I<refnum> to an array reference of refnums or a hash reference with
4693 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
4694 record will be created corresponding to cust_main.refnum.
4699 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
4701 my $conf = new FS::Conf;
4703 # Transactionize this whole mess
4704 my $oldAutoCommit = $FS::UID::AutoCommit;
4705 local $FS::UID::AutoCommit = 0;
4709 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
4710 # return "Customer not found: $custnum" unless $cust_main;
4712 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
4715 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4718 my $change = scalar(@old_cust_pkg) != 0;
4721 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
4723 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
4724 " to pkgpart ". $pkgparts->[0]. "\n"
4727 my $err_or_cust_pkg =
4728 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
4729 'refnum' => $refnum,
4732 unless (ref($err_or_cust_pkg)) {
4733 $dbh->rollback if $oldAutoCommit;
4734 return $err_or_cust_pkg;
4737 push @$return_cust_pkg, $err_or_cust_pkg;
4738 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4743 # Create the new packages.
4744 foreach my $pkgpart (@$pkgparts) {
4746 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
4748 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
4749 pkgpart => $pkgpart,
4753 $error = $cust_pkg->insert( 'change' => $change );
4754 push @$return_cust_pkg, $cust_pkg;
4756 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
4757 my $supp_pkg = FS::cust_pkg->new({
4758 custnum => $custnum,
4759 pkgpart => $link->dst_pkgpart,
4761 main_pkgnum => $cust_pkg->pkgnum,
4764 $error ||= $supp_pkg->insert( 'change' => $change );
4765 push @$return_cust_pkg, $supp_pkg;
4769 $dbh->rollback if $oldAutoCommit;
4774 # $return_cust_pkg now contains refs to all of the newly
4777 # Transfer services and cancel old packages.
4778 foreach my $old_pkg (@old_cust_pkg) {
4780 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
4783 foreach my $new_pkg (@$return_cust_pkg) {
4784 $error = $old_pkg->transfer($new_pkg);
4785 if ($error and $error == 0) {
4786 # $old_pkg->transfer failed.
4787 $dbh->rollback if $oldAutoCommit;
4792 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
4793 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
4794 foreach my $new_pkg (@$return_cust_pkg) {
4795 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
4796 if ($error and $error == 0) {
4797 # $old_pkg->transfer failed.
4798 $dbh->rollback if $oldAutoCommit;
4805 # Transfers were successful, but we went through all of the
4806 # new packages and still had services left on the old package.
4807 # We can't cancel the package under the circumstances, so abort.
4808 $dbh->rollback if $oldAutoCommit;
4809 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
4811 $error = $old_pkg->cancel( quiet=>1 );
4817 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4821 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
4823 A bulk change method to change packages for multiple customers.
4825 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4826 L<FS::part_pkg>) to order for each customer. Duplicates are of course
4829 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
4830 replace. The services (see L<FS::cust_svc>) are moved to the
4831 new billing items. An error is returned if this is not possible (see
4834 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4835 newly-created cust_pkg objects.
4840 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
4842 # Transactionize this whole mess
4843 my $oldAutoCommit = $FS::UID::AutoCommit;
4844 local $FS::UID::AutoCommit = 0;
4848 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4851 while(scalar(@old_cust_pkg)) {
4853 my $custnum = $old_cust_pkg[0]->custnum;
4854 my (@remove) = map { $_->pkgnum }
4855 grep { $_->custnum == $custnum } @old_cust_pkg;
4856 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
4858 my $error = order $custnum, $pkgparts, \@remove, \@return;
4860 push @errors, $error
4862 push @$return_cust_pkg, @return;
4865 if (scalar(@errors)) {
4866 $dbh->rollback if $oldAutoCommit;
4867 return join(' / ', @errors);
4870 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4874 # Used by FS::Upgrade to migrate to a new database.
4875 sub _upgrade_data { # class method
4876 my ($class, %opts) = @_;
4877 $class->_upgrade_otaker(%opts);
4879 # RT#10139, bug resulting in contract_end being set when it shouldn't
4880 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
4881 # RT#10830, bad calculation of prorate date near end of year
4882 # the date range for bill is December 2009, and we move it forward
4883 # one year if it's before the previous bill date (which it should
4885 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
4886 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
4887 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
4888 # RT6628, add order_date to cust_pkg
4889 'update cust_pkg set order_date = (select history_date from h_cust_pkg
4890 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
4891 history_action = \'insert\') where order_date is null',
4893 foreach my $sql (@statements) {
4894 my $sth = dbh->prepare($sql);
4895 $sth->execute or die $sth->errstr;
4898 # RT31194: supplemental package links that are deleted don't clean up
4900 my @pkglinknums = qsearch({
4901 'select' => 'DISTINCT cust_pkg.pkglinknum',
4902 'table' => 'cust_pkg',
4903 'addl_from' => ' LEFT JOIN part_pkg_link USING (pkglinknum) ',
4904 'extra_sql' => ' WHERE cust_pkg.pkglinknum IS NOT NULL
4905 AND part_pkg_link.pkglinknum IS NULL',
4907 foreach (@pkglinknums) {
4908 my $pkglinknum = $_->pkglinknum;
4909 warn "cleaning part_pkg_link #$pkglinknum\n";
4910 my $part_pkg_link = FS::part_pkg_link->new({pkglinknum => $pkglinknum});
4911 my $error = $part_pkg_link->remove_linked;
4912 die $error if $error;
4920 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
4922 In sub order, the @pkgparts array (passed by reference) is clobbered.
4924 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
4925 method to pass dates to the recur_prog expression, it should do so.
4927 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
4928 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
4929 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
4930 configuration values. Probably need a subroutine which decides what to do
4931 based on whether or not we've fetched the user yet, rather than a hash. See
4932 FS::UID and the TODO.
4934 Now that things are transactional should the check in the insert method be
4939 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
4940 L<FS::pkg_svc>, schema.html from the base documentation