2 use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::Sales_Mixin
3 FS::contact_Mixin FS::location_Mixin
4 FS::m2m_Common FS::option_Common );
7 use vars qw( $disable_agentcheck $DEBUG $me $upgrade );
9 use Scalar::Util qw( blessed );
10 use List::Util qw(min max);
12 use Time::Local qw( timelocal timelocal_nocheck );
14 use FS::UID qw( getotaker dbh driver_name );
15 use FS::Misc qw( send_email );
16 use FS::Record qw( qsearch qsearchs fields );
22 use FS::cust_location;
24 use FS::cust_bill_pkg;
25 use FS::cust_pkg_detail;
26 use FS::cust_pkg_usage;
27 use FS::cdr_cust_pkg_usage;
32 use FS::cust_pkg_reason;
34 use FS::cust_pkg_discount;
41 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
43 # because they load configuration by setting FS::UID::callback (see TODO)
49 # for sending cancel emails in sub cancel
53 $me = '[FS::cust_pkg]';
55 $disable_agentcheck = 0;
57 $upgrade = 0; #go away after setup+start dates cleaned up for old customers
60 my( $self, $hashref ) = @_;
61 if ( $hashref->{'pkg'} ) {
62 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
68 my ( $hashref, $cache ) = @_;
69 # #if ( $hashref->{'pkgpart'} ) {
70 # if ( $hashref->{'pkg'} ) {
71 # # #@{ $self->{'_pkgnum'} } = ();
72 # # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
73 # # $self->{'_pkgpart'} = $subcache;
74 # # #push @{ $self->{'_pkgnum'} },
75 # # FS::part_pkg->new_or_cached($hashref, $subcache);
76 # $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
78 if ( exists $hashref->{'svcnum'} ) {
79 #@{ $self->{'_pkgnum'} } = ();
80 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
81 $self->{'_svcnum'} = $subcache;
82 #push @{ $self->{'_pkgnum'} },
83 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
89 FS::cust_pkg - Object methods for cust_pkg objects
95 $record = new FS::cust_pkg \%hash;
96 $record = new FS::cust_pkg { 'column' => 'value' };
98 $error = $record->insert;
100 $error = $new_record->replace($old_record);
102 $error = $record->delete;
104 $error = $record->check;
106 $error = $record->cancel;
108 $error = $record->suspend;
110 $error = $record->unsuspend;
112 $part_pkg = $record->part_pkg;
114 @labels = $record->labels;
116 $seconds = $record->seconds_since($timestamp);
118 #bulk cancel+order... perhaps slightly deprecated, only used by the bulk
119 # cancel+order in the web UI and nowhere else (edit/process/cust_pkg.cgi)
120 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
121 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
125 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
126 inherits from FS::Record. The following fields are currently supported:
132 Primary key (assigned automatically for new billing items)
136 Customer (see L<FS::cust_main>)
140 Billing item definition (see L<FS::part_pkg>)
144 Optional link to package location (see L<FS::location>)
148 date package was ordered (also remains same on changes)
160 date (next bill date)
188 order taker (see L<FS::access_user>)
192 If this field is set to 1, disables the automatic
193 unsuspension of this package when using the B<unsuspendauto> config option.
197 If not set, defaults to 1
201 Date of change from previous package
211 =item change_locationnum
219 The pkgnum of the package that this package is supplemental to, if any.
223 The package link (L<FS::part_pkg_link>) that defines this supplemental
224 package, if it is one.
226 =item change_to_pkgnum
228 The pkgnum of the package this one will be "changed to" in the future
229 (on its expiration date).
233 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
234 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
235 L<Time::Local> and L<Date::Parse> for conversion functions.
243 Create a new billing item. To add the item to the database, see L<"insert">.
247 sub table { 'cust_pkg'; }
248 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
249 sub cust_unlinked_msg {
251 "WARNING: can't find cust_main.custnum ". $self->custnum.
252 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
255 =item set_initial_timers
257 If required by the package definition, sets any automatic expire, adjourn,
258 or contract_end timers to some number of months after the start date
259 (or setup date, if the package has already been setup). If the package has
260 a delayed setup fee after a period of "free days", will also set the
261 start date to the end of that period.
265 sub set_initial_timers {
267 my $part_pkg = $self->part_pkg;
268 foreach my $action ( qw(expire adjourn contract_end) ) {
269 my $months = $part_pkg->option("${action}_months",1);
270 if($months and !$self->get($action)) {
271 my $start = $self->start_date || $self->setup || time;
272 $self->set($action, $part_pkg->add_freq($start, $months) );
276 # if this package has "free days" and delayed setup fee, then
277 # set start date that many days in the future.
278 # (this should have been set in the UI, but enforce it here)
279 if ( $part_pkg->option('free_days',1)
280 && $part_pkg->option('delay_setup',1)
283 $self->start_date( $part_pkg->default_start_date );
288 =item insert [ OPTION => VALUE ... ]
290 Adds this billing item to the database ("Orders" the item). If there is an
291 error, returns the error, otherwise returns false.
293 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
294 will be used to look up the package definition and agent restrictions will be
297 If the additional field I<refnum> is defined, an FS::pkg_referral record will
298 be created and inserted. Multiple FS::pkg_referral records can be created by
299 setting I<refnum> to an array reference of refnums or a hash reference with
300 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
301 record will be created corresponding to cust_main.refnum.
303 The following options are available:
309 If set true, supresses actions that should only be taken for new package
310 orders. (Currently this includes: intro periods when delay_setup is on,
311 auto-adding a 1st start date, auto-adding expiration/adjourn/contract_end dates)
315 cust_pkg_option records will be created
319 a ticket will be added to this customer with this subject
323 an optional queue name for ticket additions
327 Don't check the legality of the package definition. This should be used
328 when performing a package change that doesn't change the pkgpart (i.e.
336 my( $self, %options ) = @_;
339 $error = $self->check_pkgpart unless $options{'allow_pkgpart'};
340 return $error if $error;
342 my $part_pkg = $self->part_pkg;
344 if ( ! $options{'change'} ) {
346 # set order date to now
347 $self->order_date(time);
349 # if the package def says to start only on the first of the month:
350 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
351 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
352 $mon += 1 unless $mday == 1;
353 until ( $mon < 12 ) { $mon -= 12; $year++; }
354 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
357 if ($self->susp eq 'now' or $part_pkg->start_on_hold) {
358 # if the package was ordered on hold:
360 # - don't set the start date (it will be started manually)
361 $self->set('susp', $self->order_date);
362 $self->set('start_date', '');
364 # set expire/adjourn/contract_end timers, and free days, if appropriate
365 $self->set_initial_timers;
367 } # else this is a package change, and shouldn't have "new package" behavior
369 local $SIG{HUP} = 'IGNORE';
370 local $SIG{INT} = 'IGNORE';
371 local $SIG{QUIT} = 'IGNORE';
372 local $SIG{TERM} = 'IGNORE';
373 local $SIG{TSTP} = 'IGNORE';
374 local $SIG{PIPE} = 'IGNORE';
376 my $oldAutoCommit = $FS::UID::AutoCommit;
377 local $FS::UID::AutoCommit = 0;
380 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
382 $dbh->rollback if $oldAutoCommit;
386 $self->refnum($self->cust_main->refnum) unless $self->refnum;
387 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
388 $self->process_m2m( 'link_table' => 'pkg_referral',
389 'target_table' => 'part_referral',
390 'params' => $self->refnum,
393 if ( $self->discountnum ) {
394 my $error = $self->insert_discount();
396 $dbh->rollback if $oldAutoCommit;
401 my $conf = new FS::Conf;
403 if ( $conf->config('ticket_system') && $options{ticket_subject} ) {
405 #this init stuff is still inefficient, but at least its limited to
406 # the small number (any?) folks using ticket emailing on pkg order
409 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
416 use FS::TicketSystem;
417 FS::TicketSystem->init();
419 my $q = new RT::Queue($RT::SystemUser);
420 $q->Load($options{ticket_queue}) if $options{ticket_queue};
421 my $t = new RT::Ticket($RT::SystemUser);
422 my $mime = new MIME::Entity;
423 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
424 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
425 Subject => $options{ticket_subject},
428 $t->AddLink( Type => 'MemberOf',
429 Target => 'freeside://freeside/cust_main/'. $self->custnum,
433 if ($conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
434 my $queue = new FS::queue {
435 'job' => 'FS::cust_main::queueable_print',
437 $error = $queue->insert(
438 'custnum' => $self->custnum,
439 'template' => 'welcome_letter',
443 warn "can't send welcome letter: $error";
448 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
455 This method now works but you probably shouldn't use it.
457 You don't want to delete packages, because there would then be no record
458 the customer ever purchased the package. Instead, see the cancel method and
459 hide cancelled packages.
466 local $SIG{HUP} = 'IGNORE';
467 local $SIG{INT} = 'IGNORE';
468 local $SIG{QUIT} = 'IGNORE';
469 local $SIG{TERM} = 'IGNORE';
470 local $SIG{TSTP} = 'IGNORE';
471 local $SIG{PIPE} = 'IGNORE';
473 my $oldAutoCommit = $FS::UID::AutoCommit;
474 local $FS::UID::AutoCommit = 0;
477 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
478 my $error = $cust_pkg_discount->delete;
480 $dbh->rollback if $oldAutoCommit;
484 #cust_bill_pkg_discount?
486 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
487 my $error = $cust_pkg_detail->delete;
489 $dbh->rollback if $oldAutoCommit;
494 foreach my $cust_pkg_reason (
496 'table' => 'cust_pkg_reason',
497 'hashref' => { 'pkgnum' => $self->pkgnum },
501 my $error = $cust_pkg_reason->delete;
503 $dbh->rollback if $oldAutoCommit;
510 my $error = $self->SUPER::delete(@_);
512 $dbh->rollback if $oldAutoCommit;
516 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
522 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
524 Replaces the OLD_RECORD with this one in the database. If there is an error,
525 returns the error, otherwise returns false.
527 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
529 Changing pkgpart may have disasterous effects. See the order subroutine.
531 setup and bill are normally updated by calling the bill method of a customer
532 object (see L<FS::cust_main>).
534 suspend is normally updated by the suspend and unsuspend methods.
536 cancel is normally updated by the cancel method (and also the order subroutine
539 Available options are:
545 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.
549 the access_user (see L<FS::access_user>) providing the reason
553 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
562 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
567 ( ref($_[0]) eq 'HASH' )
571 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
572 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
575 #return "Can't change setup once it exists!"
576 # if $old->getfield('setup') &&
577 # $old->getfield('setup') != $new->getfield('setup');
579 #some logic for bill, susp, cancel?
581 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
583 local $SIG{HUP} = 'IGNORE';
584 local $SIG{INT} = 'IGNORE';
585 local $SIG{QUIT} = 'IGNORE';
586 local $SIG{TERM} = 'IGNORE';
587 local $SIG{TSTP} = 'IGNORE';
588 local $SIG{PIPE} = 'IGNORE';
590 my $oldAutoCommit = $FS::UID::AutoCommit;
591 local $FS::UID::AutoCommit = 0;
594 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
595 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
596 my $error = $new->insert_reason(
597 'reason' => $options->{'reason'},
598 'date' => $new->$method,
600 'reason_otaker' => $options->{'reason_otaker'},
603 dbh->rollback if $oldAutoCommit;
604 return "Error inserting cust_pkg_reason: $error";
609 #save off and freeze RADIUS attributes for any associated svc_acct records
611 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
613 #also check for specific exports?
614 # to avoid spurious modify export events
615 @svc_acct = map { $_->svc_x }
616 grep { $_->part_svc->svcdb eq 'svc_acct' }
619 $_->snapshot foreach @svc_acct;
623 my $error = $new->export_pkg_change($old)
624 || $new->SUPER::replace( $old,
626 ? $options->{options}
630 $dbh->rollback if $oldAutoCommit;
634 #for prepaid packages,
635 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
636 foreach my $old_svc_acct ( @svc_acct ) {
637 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
639 $new_svc_acct->replace( $old_svc_acct,
640 'depend_jobnum' => $options->{depend_jobnum},
643 $dbh->rollback if $oldAutoCommit;
648 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
655 Checks all fields to make sure this is a valid billing item. If there is an
656 error, returns the error, otherwise returns false. Called by the insert and
664 if ( !$self->locationnum or $self->locationnum == -1 ) {
665 $self->set('locationnum', $self->cust_main->ship_locationnum);
669 $self->ut_numbern('pkgnum')
670 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
671 || $self->ut_numbern('pkgpart')
672 || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' )
673 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
674 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
675 || $self->ut_numbern('quantity')
676 || $self->ut_numbern('start_date')
677 || $self->ut_numbern('setup')
678 || $self->ut_numbern('bill')
679 || $self->ut_numbern('susp')
680 || $self->ut_numbern('cancel')
681 || $self->ut_numbern('adjourn')
682 || $self->ut_numbern('resume')
683 || $self->ut_numbern('expire')
684 || $self->ut_numbern('dundate')
685 || $self->ut_flag('no_auto', [ '', 'Y' ])
686 || $self->ut_flag('waive_setup', [ '', 'Y' ])
687 || $self->ut_flag('separate_bill')
688 || $self->ut_textn('agent_pkgid')
689 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
690 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
691 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
692 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
693 || $self->ut_foreign_keyn('change_to_pkgnum', 'cust_pkg', 'pkgnum')
695 return $error if $error;
697 return "A package with both start date (future start) and setup date (already started) will never bill"
698 if $self->start_date && $self->setup && ! $upgrade;
700 return "A future unsuspend date can only be set for a package with a suspend date"
701 if $self->resume and !$self->susp and !$self->adjourn;
703 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
705 if ( $self->dbdef_table->column('manual_flag') ) {
706 $self->manual_flag('') if $self->manual_flag eq ' ';
707 $self->manual_flag =~ /^([01]?)$/
708 or return "Illegal manual_flag ". $self->manual_flag;
709 $self->manual_flag($1);
717 Check the pkgpart to make sure it's allowed with the reg_code and/or
718 promo_code of the package (if present) and with the customer's agent.
719 Called from C<insert>, unless we are doing a package change that doesn't
727 # my $error = $self->ut_numbern('pkgpart'); # already done
730 if ( $self->reg_code ) {
732 unless ( grep { $self->pkgpart == $_->pkgpart }
733 map { $_->reg_code_pkg }
734 qsearchs( 'reg_code', { 'code' => $self->reg_code,
735 'agentnum' => $self->cust_main->agentnum })
737 return "Unknown registration code";
740 } elsif ( $self->promo_code ) {
743 qsearchs('part_pkg', {
744 'pkgpart' => $self->pkgpart,
745 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
747 return 'Unknown promotional code' unless $promo_part_pkg;
751 unless ( $disable_agentcheck ) {
753 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
754 return "agent ". $agent->agentnum. ':'. $agent->agent.
755 " can't purchase pkgpart ". $self->pkgpart
756 unless $agent->pkgpart_hashref->{ $self->pkgpart }
757 || $agent->agentnum == $self->part_pkg->agentnum;
760 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
761 return $error if $error;
769 =item cancel [ OPTION => VALUE ... ]
771 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
772 in this package, then cancels the package itself (sets the cancel field to
775 Available options are:
779 =item quiet - can be set true to supress email cancellation notices.
781 =item time - can be set to cancel the package based on a specific future or
782 historical date. Using time ensures that the remaining amount is calculated
783 correctly. Note however that this is an immediate cancel and just changes
784 the date. You are PROBABLY looking to expire the account instead of using
787 =item reason - can be set to a cancellation reason (see L<FS:reason>),
788 either a reasonnum of an existing reason, or passing a hashref will create
789 a new reason. The hashref should have the following keys: typenum - Reason
790 type (see L<FS::reason_type>, reason - Text of the new reason.
792 =item date - can be set to a unix style timestamp to specify when to
795 =item nobill - can be set true to skip billing if it might otherwise be done.
797 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
798 not credit it. This must be set (by change()) when changing the package
799 to a different pkgpart or location, and probably shouldn't be in any other
800 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
803 =item no_delay_cancel - prevents delay_cancel behavior
804 no matter what other options say, for use when changing packages (or any
805 other time you're really sure you want an immediate cancel)
809 If there is an error, returns the error, otherwise returns false.
813 #NOT DOCUMENTING - this should only be used when calling recursively
814 #=item delay_cancel - for internal use, to allow proper handling of
815 #supplemental packages when the main package is flagged to suspend
816 #before cancelling, probably shouldn't be used otherwise (set the
817 #corresponding package option instead)
820 my( $self, %options ) = @_;
823 # pass all suspend/cancel actions to the main package
824 # (unless the pkglinknum has been removed, then the link is defunct and
825 # this package can be canceled on its own)
826 if ( $self->main_pkgnum and $self->pkglinknum and !$options{'from_main'} ) {
827 return $self->main_pkg->cancel(%options);
830 my $conf = new FS::Conf;
832 warn "cust_pkg::cancel called with options".
833 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
836 local $SIG{HUP} = 'IGNORE';
837 local $SIG{INT} = 'IGNORE';
838 local $SIG{QUIT} = 'IGNORE';
839 local $SIG{TERM} = 'IGNORE';
840 local $SIG{TSTP} = 'IGNORE';
841 local $SIG{PIPE} = 'IGNORE';
843 my $oldAutoCommit = $FS::UID::AutoCommit;
844 local $FS::UID::AutoCommit = 0;
847 my $old = $self->select_for_update;
849 if ( $old->get('cancel') || $self->get('cancel') ) {
850 dbh->rollback if $oldAutoCommit;
851 return ""; # no error
854 # XXX possibly set cancel_time to the expire date?
855 my $cancel_time = $options{'time'} || time;
856 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
857 $date = '' if ($date && $date <= $cancel_time); # complain instead?
859 my $delay_cancel = $options{'no_delay_cancel'} ? 0 : $options{'delay_cancel'};
860 if ( !$date && $self->part_pkg->option('delay_cancel',1)
861 && (($self->status eq 'active') || ($self->status eq 'suspended'))
862 && !$options{'no_delay_cancel'}
864 my $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
865 my $expsecs = 60*60*24*$expdays;
866 my $suspfor = $self->susp ? $cancel_time - $self->susp : 0;
867 $expsecs = $expsecs - $suspfor if $suspfor;
868 unless ($expsecs <= 0) { #if it's already been suspended long enough, don't re-suspend
870 $date = $cancel_time + $expsecs;
874 #race condition: usage could be ongoing until unprovisioned
875 #resolved by performing a change package instead (which unprovisions) and
877 if ( !$options{nobill} && !$date ) {
878 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
879 my $copy = $self->new({$self->hash});
881 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
883 'time' => $cancel_time );
884 warn "Error billing during cancel, custnum ".
885 #$self->cust_main->custnum. ": $error"
890 if ( $options{'reason'} ) {
891 $error = $self->insert_reason( 'reason' => $options{'reason'},
892 'action' => $date ? 'expire' : 'cancel',
893 'date' => $date ? $date : $cancel_time,
894 'reason_otaker' => $options{'reason_otaker'},
897 dbh->rollback if $oldAutoCommit;
898 return "Error inserting cust_pkg_reason: $error";
902 my %svc_cancel_opt = ();
903 $svc_cancel_opt{'date'} = $date if $date;
904 foreach my $cust_svc (
907 sort { $a->[1] <=> $b->[1] }
908 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
909 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
911 my $part_svc = $cust_svc->part_svc;
912 next if ( defined($part_svc) and $part_svc->preserve );
913 my $error = $cust_svc->cancel( %svc_cancel_opt );
916 $dbh->rollback if $oldAutoCommit;
917 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
922 # if a reasonnum was passed, get the actual reason object so we can check
926 if ($options{'reason'} =~ /^\d+$/) {
927 $reason = FS::reason->by_key($options{'reason'});
931 # credit remaining time if any of these are true:
932 # - unused_credit => 1 was passed (this happens when canceling a package
933 # for a package change when unused_credit_change is set)
934 # - no unused_credit option, and there is a cancel reason, and the cancel
935 # reason says to credit the package
936 # - no unused_credit option, and the package definition says to credit the
937 # package on cancellation
939 if ( exists($options{'unused_credit'}) ) {
940 $do_credit = $options{'unused_credit'};
941 } elsif ( defined($reason) && $reason->unused_credit ) {
944 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
947 my $error = $self->credit_remaining('cancel', $cancel_time);
949 $dbh->rollback if $oldAutoCommit;
955 my %hash = $self->hash;
957 $hash{'expire'} = $date;
959 # just to be sure these are clear
960 $hash{'adjourn'} = undef;
961 $hash{'resume'} = undef;
964 $hash{'cancel'} = $cancel_time;
966 $hash{'change_custnum'} = $options{'change_custnum'};
968 # if this is a supplemental package that's lost its part_pkg_link, and it's
969 # being canceled for real, unlink it completely
970 if ( !$date and ! $self->pkglinknum ) {
971 $hash{main_pkgnum} = '';
974 my $new = new FS::cust_pkg ( \%hash );
975 $error = $new->replace( $self, options => { $self->options } );
976 if ( $self->change_to_pkgnum ) {
977 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
978 $error ||= $change_to->cancel('no_delay_cancel' => 1) || $change_to->delete;
981 $dbh->rollback if $oldAutoCommit;
985 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
986 $error = $supp_pkg->cancel(%options,
988 'date' => $date, #in case it got changed by delay_cancel
989 'delay_cancel' => $delay_cancel,
992 $dbh->rollback if $oldAutoCommit;
993 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
997 if ($delay_cancel && !$options{'from_main'}) {
998 $error = $new->suspend(
1000 'time' => $cancel_time
1005 foreach my $usage ( $self->cust_pkg_usage ) {
1006 $error = $usage->delete;
1008 $dbh->rollback if $oldAutoCommit;
1009 return "deleting usage pools: $error";
1014 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1015 return '' if $date; #no errors
1017 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
1018 if ( !$options{'quiet'} &&
1019 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
1021 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
1024 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
1025 $error = $msg_template->send( 'cust_main' => $self->cust_main,
1026 'object' => $self );
1029 $error = send_email(
1030 'from' => $conf->invoice_from_full( $self->cust_main->agentnum ),
1031 'to' => \@invoicing_list,
1032 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
1033 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
1034 'custnum' => $self->custnum,
1035 'msgtype' => '', #admin?
1038 #should this do something on errors?
1045 =item cancel_if_expired [ NOW_TIMESTAMP ]
1047 Cancels this package if its expire date has been reached.
1051 sub cancel_if_expired {
1053 my $time = shift || time;
1054 return '' unless $self->expire && $self->expire <= $time;
1055 my $error = $self->cancel;
1057 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
1058 $self->custnum. ": $error";
1065 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
1066 locationnum, (other fields?). Attempts to re-provision cancelled services
1067 using history information (errors at this stage are not fatal).
1069 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
1071 svc_fatal: service provisioning errors are fatal
1073 svc_errors: pass an array reference, will be filled in with any provisioning errors
1075 main_pkgnum: link the package as a supplemental package of this one. For
1081 my( $self, %options ) = @_;
1083 #in case you try do do $uncancel-date = $cust_pkg->uncacel
1084 return '' unless $self->get('cancel');
1086 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
1087 return $self->main_pkg->uncancel(%options);
1094 local $SIG{HUP} = 'IGNORE';
1095 local $SIG{INT} = 'IGNORE';
1096 local $SIG{QUIT} = 'IGNORE';
1097 local $SIG{TERM} = 'IGNORE';
1098 local $SIG{TSTP} = 'IGNORE';
1099 local $SIG{PIPE} = 'IGNORE';
1101 my $oldAutoCommit = $FS::UID::AutoCommit;
1102 local $FS::UID::AutoCommit = 0;
1106 # insert the new package
1109 my $cust_pkg = new FS::cust_pkg {
1110 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
1111 bill => ( $options{'bill'} || $self->get('bill') ),
1113 uncancel_pkgnum => $self->pkgnum,
1114 main_pkgnum => ($options{'main_pkgnum'} || ''),
1115 map { $_ => $self->get($_) } qw(
1116 custnum pkgpart locationnum
1118 susp adjourn resume expire start_date contract_end dundate
1119 change_date change_pkgpart change_locationnum
1120 manual_flag no_auto separate_bill quantity agent_pkgid
1121 recur_show_zero setup_show_zero
1125 my $error = $cust_pkg->insert(
1126 'change' => 1, #supresses any referral credit to a referring customer
1127 'allow_pkgpart' => 1, # allow this even if the package def is disabled
1130 $dbh->rollback if $oldAutoCommit;
1138 #find historical services within this timeframe before the package cancel
1139 # (incompatible with "time" option to cust_pkg->cancel?)
1140 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
1141 # too little? (unprovisioing export delay?)
1142 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
1143 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1146 foreach my $h_cust_svc (@h_cust_svc) {
1147 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1148 #next unless $h_svc_x; #should this happen?
1149 (my $table = $h_svc_x->table) =~ s/^h_//;
1150 require "FS/$table.pm";
1151 my $class = "FS::$table";
1152 my $svc_x = $class->new( {
1153 'pkgnum' => $cust_pkg->pkgnum,
1154 'svcpart' => $h_cust_svc->svcpart,
1155 map { $_ => $h_svc_x->get($_) } fields($table)
1159 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1160 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1163 my $svc_error = $svc_x->insert;
1165 if ( $options{svc_fatal} ) {
1166 $dbh->rollback if $oldAutoCommit;
1169 # if we've failed to insert the svc_x object, svc_Common->insert
1170 # will have removed the cust_svc already. if not, then both records
1171 # were inserted but we failed for some other reason (export, most
1172 # likely). in that case, report the error and delete the records.
1173 push @svc_errors, $svc_error;
1174 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1176 # except if export_insert failed, export_delete probably won't be
1178 local $FS::svc_Common::noexport_hack = 1;
1179 my $cleanup_error = $svc_x->delete; # also deletes cust_svc
1180 if ( $cleanup_error ) { # and if THAT fails, then run away
1181 $dbh->rollback if $oldAutoCommit;
1182 return $cleanup_error;
1187 } #foreach $h_cust_svc
1189 #these are pretty rare, but should handle them
1190 # - dsl_device (mac addresses)
1191 # - phone_device (mac addresses)
1192 # - dsl_note (ikano notes)
1193 # - domain_record (i.e. restore DNS information w/domains)
1194 # - inventory_item(?) (inventory w/un-cancelling service?)
1195 # - nas (svc_broaband nas stuff)
1196 #this stuff is unused in the wild afaik
1197 # - mailinglistmember
1199 # - svc_domain.parent_svcnum?
1200 # - acct_snarf (ancient mail fetching config)
1201 # - cgp_rule (communigate)
1202 # - cust_svc_option (used by our Tron stuff)
1203 # - acct_rt_transaction (used by our time worked stuff)
1206 # also move over any services that didn't unprovision at cancellation
1209 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1210 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1211 my $error = $cust_svc->replace;
1213 $dbh->rollback if $oldAutoCommit;
1219 # Uncancel any supplemental packages, and make them supplemental to the
1223 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1225 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1227 $dbh->rollback if $oldAutoCommit;
1228 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1236 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1238 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1239 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1246 Cancels any pending expiration (sets the expire field to null).
1248 If there is an error, returns the error, otherwise returns false.
1253 my( $self, %options ) = @_;
1256 local $SIG{HUP} = 'IGNORE';
1257 local $SIG{INT} = 'IGNORE';
1258 local $SIG{QUIT} = 'IGNORE';
1259 local $SIG{TERM} = 'IGNORE';
1260 local $SIG{TSTP} = 'IGNORE';
1261 local $SIG{PIPE} = 'IGNORE';
1263 my $oldAutoCommit = $FS::UID::AutoCommit;
1264 local $FS::UID::AutoCommit = 0;
1267 my $old = $self->select_for_update;
1269 my $pkgnum = $old->pkgnum;
1270 if ( $old->get('cancel') || $self->get('cancel') ) {
1271 dbh->rollback if $oldAutoCommit;
1272 return "Can't unexpire cancelled package $pkgnum";
1273 # or at least it's pointless
1276 unless ( $old->get('expire') && $self->get('expire') ) {
1277 dbh->rollback if $oldAutoCommit;
1278 return ""; # no error
1281 my %hash = $self->hash;
1282 $hash{'expire'} = '';
1283 my $new = new FS::cust_pkg ( \%hash );
1284 $error = $new->replace( $self, options => { $self->options } );
1286 $dbh->rollback if $oldAutoCommit;
1290 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1296 =item suspend [ OPTION => VALUE ... ]
1298 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1299 package, then suspends the package itself (sets the susp field to now).
1301 Available options are:
1305 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1306 either a reasonnum of an existing reason, or passing a hashref will create
1307 a new reason. The hashref should have the following keys:
1308 - typenum - Reason type (see L<FS::reason_type>
1309 - reason - Text of the new reason.
1311 =item date - can be set to a unix style timestamp to specify when to
1314 =item time - can be set to override the current time, for calculation
1315 of final invoices or unused-time credits
1317 =item resume_date - can be set to a time when the package should be
1318 unsuspended. This may be more convenient than calling C<unsuspend()>
1321 =item from_main - allows a supplemental package to be suspended, rather
1322 than redirecting the method call to its main package. For internal use.
1324 =item from_cancel - used when suspending from the cancel method, forces
1325 this to skip everything besides basic suspension. For internal use.
1329 If there is an error, returns the error, otherwise returns false.
1334 my( $self, %options ) = @_;
1337 # pass all suspend/cancel actions to the main package
1338 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1339 return $self->main_pkg->suspend(%options);
1342 local $SIG{HUP} = 'IGNORE';
1343 local $SIG{INT} = 'IGNORE';
1344 local $SIG{QUIT} = 'IGNORE';
1345 local $SIG{TERM} = 'IGNORE';
1346 local $SIG{TSTP} = 'IGNORE';
1347 local $SIG{PIPE} = 'IGNORE';
1349 my $oldAutoCommit = $FS::UID::AutoCommit;
1350 local $FS::UID::AutoCommit = 0;
1353 my $old = $self->select_for_update;
1355 my $pkgnum = $old->pkgnum;
1356 if ( $old->get('cancel') || $self->get('cancel') ) {
1357 dbh->rollback if $oldAutoCommit;
1358 return "Can't suspend cancelled package $pkgnum";
1361 if ( $old->get('susp') || $self->get('susp') ) {
1362 dbh->rollback if $oldAutoCommit;
1363 return ""; # no error # complain on adjourn?
1366 my $suspend_time = $options{'time'} || time;
1367 my $date = $options{date} if $options{date}; # adjourn/suspend later
1368 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1370 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1371 dbh->rollback if $oldAutoCommit;
1372 return "Package $pkgnum expires before it would be suspended.";
1375 # some false laziness with sub cancel
1376 if ( !$options{nobill} && !$date && !$options{'from_cancel'} &&
1377 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1378 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1379 # make the entire cust_main->bill path recognize 'suspend' and
1380 # 'cancel' separately.
1381 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1382 my $copy = $self->new({$self->hash});
1384 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1386 'time' => $suspend_time );
1387 warn "Error billing during suspend, custnum ".
1388 #$self->cust_main->custnum. ": $error"
1393 my $cust_pkg_reason;
1394 if ( $options{'reason'} ) {
1395 $error = $self->insert_reason( 'reason' => $options{'reason'},
1396 'action' => $date ? 'adjourn' : 'suspend',
1397 'date' => $date ? $date : $suspend_time,
1398 'reason_otaker' => $options{'reason_otaker'},
1401 dbh->rollback if $oldAutoCommit;
1402 return "Error inserting cust_pkg_reason: $error";
1404 $cust_pkg_reason = qsearchs('cust_pkg_reason', {
1405 'date' => $date ? $date : $suspend_time,
1406 'action' => $date ? 'A' : 'S',
1407 'pkgnum' => $self->pkgnum,
1411 # if a reasonnum was passed, get the actual reason object so we can check
1413 # (passing a reason hashref is still allowed, but it can't be used with
1414 # the fancy behavioral options.)
1417 if ($options{'reason'} =~ /^\d+$/) {
1418 $reason = FS::reason->by_key($options{'reason'});
1421 my %hash = $self->hash;
1423 $hash{'adjourn'} = $date;
1425 $hash{'susp'} = $suspend_time;
1428 my $resume_date = $options{'resume_date'} || 0;
1429 if ( $resume_date > ($date || $suspend_time) ) {
1430 $hash{'resume'} = $resume_date;
1433 $options{options} ||= {};
1435 my $new = new FS::cust_pkg ( \%hash );
1436 $error = $new->replace( $self, options => { $self->options,
1437 %{ $options{options} },
1441 $dbh->rollback if $oldAutoCommit;
1445 unless ( $date ) { # then we are suspending now
1447 unless ($options{'from_cancel'}) {
1448 # credit remaining time if appropriate
1449 # (if required by the package def, or the suspend reason)
1450 my $unused_credit = $self->part_pkg->option('unused_credit_suspend',1)
1451 || ( defined($reason) && $reason->unused_credit );
1453 if ( $unused_credit ) {
1454 warn "crediting unused time on pkg#".$self->pkgnum."\n" if $DEBUG;
1455 my $error = $self->credit_remaining('suspend', $suspend_time);
1457 $dbh->rollback if $oldAutoCommit;
1465 foreach my $cust_svc (
1466 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
1468 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1470 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1471 $dbh->rollback if $oldAutoCommit;
1472 return "Illegal svcdb value in part_svc!";
1475 require "FS/$svcdb.pm";
1477 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1479 $error = $svc->suspend;
1481 $dbh->rollback if $oldAutoCommit;
1484 my( $label, $value ) = $cust_svc->label;
1485 push @labels, "$label: $value";
1489 # suspension fees: if there is a feepart, and it's not an unsuspend fee,
1490 # and this is not a suspend-before-cancel
1491 if ( $cust_pkg_reason ) {
1492 my $reason_obj = $cust_pkg_reason->reason;
1493 if ( $reason_obj->feepart and
1494 ! $reason_obj->fee_on_unsuspend and
1495 ! $options{'from_cancel'} ) {
1497 # register the need to charge a fee, cust_main->bill will do the rest
1498 warn "registering suspend fee: pkgnum ".$self->pkgnum.", feepart ".$reason->feepart."\n"
1500 my $cust_pkg_reason_fee = FS::cust_pkg_reason_fee->new({
1501 'pkgreasonnum' => $cust_pkg_reason->num,
1502 'pkgnum' => $self->pkgnum,
1503 'feepart' => $reason->feepart,
1504 'nextbill' => $reason->fee_hold,
1506 $error ||= $cust_pkg_reason_fee->insert;
1510 my $conf = new FS::Conf;
1511 if ( $conf->config('suspend_email_admin') && !$options{'from_cancel'} ) {
1513 my $error = send_email(
1514 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1515 #invoice_from ??? well as good as any
1516 'to' => $conf->config('suspend_email_admin'),
1517 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1519 "This is an automatic message from your Freeside installation\n",
1520 "informing you that the following customer package has been suspended:\n",
1522 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1523 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1524 ( map { "Service : $_\n" } @labels ),
1526 'custnum' => $self->custnum,
1527 'msgtype' => 'admin'
1531 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1539 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1540 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1542 $dbh->rollback if $oldAutoCommit;
1543 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1547 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1552 =item credit_remaining MODE TIME
1554 Generate a credit for this package for the time remaining in the current
1555 billing period. MODE is either "suspend" or "cancel" (determines the
1556 credit type). TIME is the time of suspension/cancellation. Both arguments
1561 # Implementation note:
1563 # If you pkgpart-change a package that has been billed, and it's set to give
1564 # credit on package change, then this method gets called and then the new
1565 # package will have no last_bill date. Therefore the customer will be credited
1566 # only once (per billing period) even if there are multiple package changes.
1568 # If you location-change a package that has been billed, this method will NOT
1569 # be called and the new package WILL have the last bill date of the old
1572 # If the new package is then canceled within the same billing cycle,
1573 # credit_remaining needs to run calc_remain on the OLD package to determine
1574 # the amount of unused time to credit.
1576 sub credit_remaining {
1577 # Add a credit for remaining service
1578 my ($self, $mode, $time) = @_;
1579 die 'credit_remaining requires suspend or cancel'
1580 unless $mode eq 'suspend' or $mode eq 'cancel';
1581 die 'no suspend/cancel time' unless $time > 0;
1583 my $conf = FS::Conf->new;
1584 my $reason_type = $conf->config($mode.'_credit_type');
1586 my $last_bill = $self->getfield('last_bill') || 0;
1587 my $next_bill = $self->getfield('bill') || 0;
1588 if ( $last_bill > 0 # the package has been billed
1589 and $next_bill > 0 # the package has a next bill date
1590 and $next_bill >= $time # which is in the future
1592 my $remaining_value = 0;
1594 my $remain_pkg = $self;
1595 $remaining_value = $remain_pkg->calc_remain('time' => $time);
1597 # we may have to walk back past some package changes to get to the
1598 # one that actually has unused time
1599 while ( $remaining_value == 0 ) {
1600 if ( $remain_pkg->change_pkgnum ) {
1601 $remain_pkg = FS::cust_pkg->by_key($remain_pkg->change_pkgnum);
1603 # the package has really never been billed
1606 $remaining_value = $remain_pkg->calc_remain('time' => $time);
1609 if ( $remaining_value > 0 ) {
1610 warn "Crediting for $remaining_value on package ".$self->pkgnum."\n"
1612 my $error = $self->cust_main->credit(
1614 'Credit for unused time on '. $self->part_pkg->pkg,
1615 'reason_type' => $reason_type,
1617 return "Error crediting customer \$$remaining_value for unused time".
1618 " on ". $self->part_pkg->pkg. ": $error"
1620 } #if $remaining_value
1621 } #if $last_bill, etc.
1625 =item unsuspend [ OPTION => VALUE ... ]
1627 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1628 package, then unsuspends the package itself (clears the susp field and the
1629 adjourn field if it is in the past). If the suspend reason includes an
1630 unsuspension package, that package will be ordered.
1632 Available options are:
1638 Can be set to a date to unsuspend the package in the future (the 'resume'
1641 =item adjust_next_bill
1643 Can be set true to adjust the next bill date forward by
1644 the amount of time the account was inactive. This was set true by default
1645 in the past (from 1.4.2 and 1.5.0pre6 through 1.7.0), but now needs to be
1646 explicitly requested with this option or in the price plan.
1650 If there is an error, returns the error, otherwise returns false.
1655 my( $self, %opt ) = @_;
1658 # pass all suspend/cancel actions to the main package
1659 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1660 return $self->main_pkg->unsuspend(%opt);
1663 local $SIG{HUP} = 'IGNORE';
1664 local $SIG{INT} = 'IGNORE';
1665 local $SIG{QUIT} = 'IGNORE';
1666 local $SIG{TERM} = 'IGNORE';
1667 local $SIG{TSTP} = 'IGNORE';
1668 local $SIG{PIPE} = 'IGNORE';
1670 my $oldAutoCommit = $FS::UID::AutoCommit;
1671 local $FS::UID::AutoCommit = 0;
1674 my $old = $self->select_for_update;
1676 my $pkgnum = $old->pkgnum;
1677 if ( $old->get('cancel') || $self->get('cancel') ) {
1678 $dbh->rollback if $oldAutoCommit;
1679 return "Can't unsuspend cancelled package $pkgnum";
1682 unless ( $old->get('susp') && $self->get('susp') ) {
1683 $dbh->rollback if $oldAutoCommit;
1684 return ""; # no error # complain instead?
1687 # handle the case of setting a future unsuspend (resume) date
1688 # and do not continue to actually unsuspend the package
1689 my $date = $opt{'date'};
1690 if ( $date and $date > time ) { # return an error if $date <= time?
1692 if ( $old->get('expire') && $old->get('expire') < $date ) {
1693 $dbh->rollback if $oldAutoCommit;
1694 return "Package $pkgnum expires before it would be unsuspended.";
1697 my $new = new FS::cust_pkg { $self->hash };
1698 $new->set('resume', $date);
1699 $error = $new->replace($self, options => $self->options);
1702 $dbh->rollback if $oldAutoCommit;
1706 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1712 if (!$self->setup) {
1713 # then this package is being released from on-hold status
1714 $self->set_initial_timers;
1719 foreach my $cust_svc (
1720 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1722 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1724 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1725 $dbh->rollback if $oldAutoCommit;
1726 return "Illegal svcdb value in part_svc!";
1729 require "FS/$svcdb.pm";
1731 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1733 $error = $svc->unsuspend;
1735 $dbh->rollback if $oldAutoCommit;
1738 my( $label, $value ) = $cust_svc->label;
1739 push @labels, "$label: $value";
1744 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1745 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
1747 my %hash = $self->hash;
1748 my $inactive = time - $hash{'susp'};
1750 my $conf = new FS::Conf;
1752 # increment next bill date if certain conditions are met:
1753 # - it was due to be billed at some point
1754 # - either the global or local config says to do this
1755 my $adjust_bill = 0;
1758 && ( $hash{'bill'} || $hash{'setup'} )
1759 && ( $opt{'adjust_next_bill'}
1760 || $conf->exists('unsuspend-always_adjust_next_bill_date')
1761 || $self->part_pkg->option('unsuspend_adjust_bill', 1)
1768 # - the package billed during suspension
1769 # - or it was ordered on hold
1770 # - or the customer was credited for the unused time
1772 if ( $self->option('suspend_bill',1)
1773 or ( $self->part_pkg->option('suspend_bill',1)
1774 and ! $self->option('no_suspend_bill',1)
1776 or $hash{'order_date'} == $hash{'susp'}
1781 if ( $adjust_bill ) {
1782 if ( $self->part_pkg->option('unused_credit_suspend')
1783 or ( $reason and $reason->unused_credit ) ) {
1784 # then the customer was credited for the unused time before suspending,
1785 # so their next bill should be immediate.
1786 $hash{'bill'} = time;
1788 # add the length of time suspended to the bill date
1789 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
1794 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
1795 $hash{'resume'} = '' if !$hash{'adjourn'};
1796 my $new = new FS::cust_pkg ( \%hash );
1797 $error = $new->replace( $self, options => { $self->options } );
1799 $dbh->rollback if $oldAutoCommit;
1806 if ( $reason->unsuspend_pkgpart ) {
1807 #warn "Suspend reason '".$reason->reason."' uses deprecated unsuspend_pkgpart feature.\n"; # in 4.x
1808 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
1809 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
1811 my $start_date = $self->cust_main->next_bill_date
1812 if $reason->unsuspend_hold;
1815 $unsusp_pkg = FS::cust_pkg->new({
1816 'custnum' => $self->custnum,
1817 'pkgpart' => $reason->unsuspend_pkgpart,
1818 'start_date' => $start_date,
1819 'locationnum' => $self->locationnum,
1820 # discount? probably not...
1823 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
1826 # new way, using fees
1827 if ( $reason->feepart and $reason->fee_on_unsuspend ) {
1828 # register the need to charge a fee, cust_main->bill will do the rest
1829 warn "registering unsuspend fee: pkgnum ".$self->pkgnum.", feepart ".$reason->feepart."\n"
1831 my $cust_pkg_reason_fee = FS::cust_pkg_reason_fee->new({
1832 'pkgreasonnum' => $cust_pkg_reason->num,
1833 'pkgnum' => $self->pkgnum,
1834 'feepart' => $reason->feepart,
1835 'nextbill' => $reason->fee_hold,
1837 $error ||= $cust_pkg_reason_fee->insert;
1841 $dbh->rollback if $oldAutoCommit;
1846 if ( $conf->config('unsuspend_email_admin') ) {
1848 my $error = send_email(
1849 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1850 #invoice_from ??? well as good as any
1851 'to' => $conf->config('unsuspend_email_admin'),
1852 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
1853 "This is an automatic message from your Freeside installation\n",
1854 "informing you that the following customer package has been unsuspended:\n",
1856 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1857 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1858 ( map { "Service : $_\n" } @labels ),
1860 "An unsuspension fee was charged: ".
1861 $unsusp_pkg->part_pkg->pkg_comment."\n"
1865 'custnum' => $self->custnum,
1866 'msgtype' => 'admin',
1870 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
1876 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1877 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
1879 $dbh->rollback if $oldAutoCommit;
1880 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1884 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1891 Cancels any pending suspension (sets the adjourn field to null).
1893 If there is an error, returns the error, otherwise returns false.
1898 my( $self, %options ) = @_;
1901 local $SIG{HUP} = 'IGNORE';
1902 local $SIG{INT} = 'IGNORE';
1903 local $SIG{QUIT} = 'IGNORE';
1904 local $SIG{TERM} = 'IGNORE';
1905 local $SIG{TSTP} = 'IGNORE';
1906 local $SIG{PIPE} = 'IGNORE';
1908 my $oldAutoCommit = $FS::UID::AutoCommit;
1909 local $FS::UID::AutoCommit = 0;
1912 my $old = $self->select_for_update;
1914 my $pkgnum = $old->pkgnum;
1915 if ( $old->get('cancel') || $self->get('cancel') ) {
1916 dbh->rollback if $oldAutoCommit;
1917 return "Can't unadjourn cancelled package $pkgnum";
1918 # or at least it's pointless
1921 if ( $old->get('susp') || $self->get('susp') ) {
1922 dbh->rollback if $oldAutoCommit;
1923 return "Can't unadjourn suspended package $pkgnum";
1924 # perhaps this is arbitrary
1927 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1928 dbh->rollback if $oldAutoCommit;
1929 return ""; # no error
1932 my %hash = $self->hash;
1933 $hash{'adjourn'} = '';
1934 $hash{'resume'} = '';
1935 my $new = new FS::cust_pkg ( \%hash );
1936 $error = $new->replace( $self, options => { $self->options } );
1938 $dbh->rollback if $oldAutoCommit;
1942 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1949 =item change HASHREF | OPTION => VALUE ...
1951 Changes this package: cancels it and creates a new one, with a different
1952 pkgpart or locationnum or both. All services are transferred to the new
1953 package (no change will be made if this is not possible).
1955 Options may be passed as a list of key/value pairs or as a hash reference.
1962 New locationnum, to change the location for this package.
1966 New FS::cust_location object, to create a new location and assign it
1971 New FS::cust_main object, to create a new customer and assign the new package
1976 New pkgpart (see L<FS::part_pkg>).
1980 New refnum (see L<FS::part_referral>).
1984 New quantity; if unspecified, the new package will have the same quantity
1989 "New" (existing) FS::cust_pkg object. The package's services and other
1990 attributes will be transferred to this package.
1994 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1995 susp, adjourn, cancel, expire, and contract_end) to the new package.
1997 =item unprotect_svcs
1999 Normally, change() will rollback and return an error if some services
2000 can't be transferred (also see the I<cust_pkg-change_svcpart> config option).
2001 If unprotect_svcs is true, this method will transfer as many services as
2002 it can and then unconditionally cancel the old package.
2006 If specified, sets this value for the contract_end date on the new package
2007 (without regard for keep_dates or the usual date-preservation behavior.)
2008 Will throw an error if defined but false; the UI doesn't allow editing
2009 this unless it already exists, making removal impossible to undo.
2013 At least one of locationnum, cust_location, pkgpart, refnum, cust_main, or
2014 cust_pkg must be specified (otherwise, what's the point?)
2016 Returns either the new FS::cust_pkg object or a scalar error.
2020 my $err_or_new_cust_pkg = $old_cust_pkg->change
2024 #used by change and change_later
2025 #didn't put with documented check methods because it depends on change-specific opts
2026 #and it also possibly edits the value of opts
2030 if ( defined($opt->{'contract_end'}) ) {
2031 my $current_contract_end = $self->get('contract_end');
2032 unless ($opt->{'contract_end'}) {
2033 if ($current_contract_end) {
2034 return "Cannot remove contract end date when changing packages";
2036 #shouldn't even pass this option if there's not a current value
2037 #but can be handled gracefully if the option is empty
2038 warn "Contract end date passed unexpectedly";
2039 delete $opt->{'contract_end'};
2043 unless ($current_contract_end) {
2044 #option shouldn't be passed, throw error if it's non-empty
2045 return "Cannot add contract end date when changing packages " . $self->pkgnum;
2051 #some false laziness w/order
2054 my $opt = ref($_[0]) ? shift : { @_ };
2056 my $conf = new FS::Conf;
2058 # handle contract_end on cust_pkg same as passed option
2059 if ( $opt->{'cust_pkg'} ) {
2060 $opt->{'contract_end'} = $opt->{'cust_pkg'}->contract_end;
2061 delete $opt->{'contract_end'} unless $opt->{'contract_end'};
2064 # check contract_end, prevent adding/removing
2065 my $error = $self->_check_change($opt);
2066 return $error if $error;
2068 # Transactionize this whole mess
2069 local $SIG{HUP} = 'IGNORE';
2070 local $SIG{INT} = 'IGNORE';
2071 local $SIG{QUIT} = 'IGNORE';
2072 local $SIG{TERM} = 'IGNORE';
2073 local $SIG{TSTP} = 'IGNORE';
2074 local $SIG{PIPE} = 'IGNORE';
2076 my $oldAutoCommit = $FS::UID::AutoCommit;
2077 local $FS::UID::AutoCommit = 0;
2080 if ( $opt->{'cust_location'} ) {
2081 $error = $opt->{'cust_location'}->find_or_insert;
2083 $dbh->rollback if $oldAutoCommit;
2084 return "creating location record: $error";
2086 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
2089 # Before going any further here: if the package is still in the pre-setup
2090 # state, it's safe to modify it in place. No need to charge/credit for
2091 # partial period, transfer services, transfer usage pools, copy invoice
2092 # details, or change any dates.
2093 if ( ! $self->setup and ! $opt->{cust_pkg} and ! $opt->{cust_main} ) {
2094 foreach ( qw( locationnum pkgpart quantity refnum salesnum ) ) {
2095 if ( length($opt->{$_}) ) {
2096 $self->set($_, $opt->{$_});
2099 # almost. if the new pkgpart specifies start/adjourn/expire timers,
2101 if ( $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart ) {
2102 $self->set_initial_timers;
2104 # but if contract_end was explicitly specified, that overrides all else
2105 $self->set('contract_end', $opt->{'contract_end'})
2106 if $opt->{'contract_end'};
2107 $error = $self->replace;
2109 $dbh->rollback if $oldAutoCommit;
2110 return "modifying package: $error";
2112 $dbh->commit if $oldAutoCommit;
2121 $hash{'setup'} = $time if $self->setup;
2123 $hash{'change_date'} = $time;
2124 $hash{"change_$_"} = $self->$_()
2125 foreach qw( pkgnum pkgpart locationnum );
2127 if ( $opt->{'cust_pkg'} ) {
2128 # treat changing to a package with a different pkgpart as a
2129 # pkgpart change (because it is)
2130 $opt->{'pkgpart'} = $opt->{'cust_pkg'}->pkgpart;
2133 # whether to override pkgpart checking on the new package
2134 my $same_pkgpart = 1;
2135 if ( $opt->{'pkgpart'} and ( $opt->{'pkgpart'} != $self->pkgpart ) ) {
2139 my $unused_credit = 0;
2140 my $keep_dates = $opt->{'keep_dates'};
2142 # Special case. If the pkgpart is changing, and the customer is
2143 # going to be credited for remaining time, don't keep setup, bill,
2144 # or last_bill dates, and DO pass the flag to cancel() to credit
2146 if ( $opt->{'pkgpart'}
2147 and $opt->{'pkgpart'} != $self->pkgpart
2148 and $self->part_pkg->option('unused_credit_change', 1) ) {
2151 $hash{$_} = '' foreach qw(setup bill last_bill);
2154 if ( $keep_dates ) {
2155 foreach my $date ( qw(setup bill last_bill) ) {
2156 $hash{$date} = $self->getfield($date);
2159 # always keep the following dates
2160 foreach my $date (qw(order_date susp adjourn cancel expire resume
2161 start_date contract_end)) {
2162 $hash{$date} = $self->getfield($date);
2164 # but if contract_end was explicitly specified, that overrides all else
2165 $hash{'contract_end'} = $opt->{'contract_end'}
2166 if $opt->{'contract_end'};
2168 # allow $opt->{'locationnum'} = '' to specifically set it to null
2169 # (i.e. customer default location)
2170 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2172 # usually this doesn't matter. the two cases where it does are:
2173 # 1. unused_credit_change + pkgpart change + setup fee on the new package
2175 # 2. (more importantly) changing a package before it's billed
2176 $hash{'waive_setup'} = $self->waive_setup;
2178 # if this package is scheduled for a future package change, preserve that
2179 $hash{'change_to_pkgnum'} = $self->change_to_pkgnum;
2181 my $custnum = $self->custnum;
2182 if ( $opt->{cust_main} ) {
2183 my $cust_main = $opt->{cust_main};
2184 unless ( $cust_main->custnum ) {
2185 my $error = $cust_main->insert( @{ $opt->{cust_main_insert_args}||[] } );
2187 $dbh->rollback if $oldAutoCommit;
2188 return "inserting customer record: $error";
2191 $custnum = $cust_main->custnum;
2194 $hash{'contactnum'} = $opt->{'contactnum'} if $opt->{'contactnum'};
2197 if ( $opt->{'cust_pkg'} ) {
2198 # The target package already exists; update it to show that it was
2199 # changed from this package.
2200 $cust_pkg = $opt->{'cust_pkg'};
2202 # follow all the above rules for date changes, etc.
2203 foreach (keys %hash) {
2204 $cust_pkg->set($_, $hash{$_});
2206 # except those that implement the future package change behavior
2207 foreach (qw(change_to_pkgnum start_date expire)) {
2208 $cust_pkg->set($_, '');
2211 $error = $cust_pkg->replace;
2214 # Create the new package.
2215 $cust_pkg = new FS::cust_pkg {
2216 custnum => $custnum,
2217 locationnum => $opt->{'locationnum'},
2218 ( map { $_ => ( $opt->{$_} || $self->$_() ) }
2219 qw( pkgpart quantity refnum salesnum )
2223 $error = $cust_pkg->insert( 'change' => 1,
2224 'allow_pkgpart' => $same_pkgpart );
2227 $dbh->rollback if $oldAutoCommit;
2228 return "inserting new package: $error";
2231 # Transfer services and cancel old package.
2232 # Enforce service limits only if this is a pkgpart change.
2233 local $FS::cust_svc::ignore_quantity;
2234 $FS::cust_svc::ignore_quantity = 1 if $same_pkgpart;
2235 $error = $self->transfer($cust_pkg);
2236 if ($error and $error == 0) {
2237 # $old_pkg->transfer failed.
2238 $dbh->rollback if $oldAutoCommit;
2239 return "transferring $error";
2242 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
2243 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
2244 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
2245 if ($error and $error == 0) {
2246 # $old_pkg->transfer failed.
2247 $dbh->rollback if $oldAutoCommit;
2248 return "converting $error";
2252 # We set unprotect_svcs when executing a "future package change". It's
2253 # not a user-interactive operation, so returning an error means the
2254 # package change will just fail. Rather than have that happen, we'll
2255 # let leftover services be deleted.
2256 if ($error > 0 and !$opt->{'unprotect_svcs'}) {
2257 # Transfers were successful, but we still had services left on the old
2258 # package. We can't change the package under this circumstances, so abort.
2259 $dbh->rollback if $oldAutoCommit;
2260 return "unable to transfer all services";
2263 #reset usage if changing pkgpart
2264 # AND usage rollover is off (otherwise adds twice, now and at package bill)
2265 if ($self->pkgpart != $cust_pkg->pkgpart) {
2266 my $part_pkg = $cust_pkg->part_pkg;
2267 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
2271 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
2274 $dbh->rollback if $oldAutoCommit;
2275 return "setting usage values: $error";
2278 # if NOT changing pkgpart, transfer any usage pools over
2279 foreach my $usage ($self->cust_pkg_usage) {
2280 $usage->set('pkgnum', $cust_pkg->pkgnum);
2281 $error = $usage->replace;
2283 $dbh->rollback if $oldAutoCommit;
2284 return "transferring usage pools: $error";
2289 # transfer discounts, if we're not changing pkgpart
2290 if ( $same_pkgpart ) {
2291 foreach my $old_discount ($self->cust_pkg_discount_active) {
2292 # don't remove the old discount, we may still need to bill that package.
2293 my $new_discount = new FS::cust_pkg_discount {
2294 'pkgnum' => $cust_pkg->pkgnum,
2295 'discountnum' => $old_discount->discountnum,
2296 'months_used' => $old_discount->months_used,
2298 $error = $new_discount->insert;
2300 $dbh->rollback if $oldAutoCommit;
2301 return "transferring discounts: $error";
2306 # transfer (copy) invoice details
2307 foreach my $detail ($self->cust_pkg_detail) {
2308 my $new_detail = FS::cust_pkg_detail->new({ $detail->hash });
2309 $new_detail->set('pkgdetailnum', '');
2310 $new_detail->set('pkgnum', $cust_pkg->pkgnum);
2311 $error = $new_detail->insert;
2313 $dbh->rollback if $oldAutoCommit;
2314 return "transferring package notes: $error";
2320 if ( !$opt->{'cust_pkg'} ) {
2321 # Order any supplemental packages.
2322 my $part_pkg = $cust_pkg->part_pkg;
2323 my @old_supp_pkgs = $self->supplemental_pkgs;
2324 foreach my $link ($part_pkg->supp_part_pkg_link) {
2326 foreach (@old_supp_pkgs) {
2327 if ($_->pkgpart == $link->dst_pkgpart) {
2329 $_->pkgpart(0); # so that it can't match more than once
2333 # false laziness with FS::cust_main::Packages::order_pkg
2334 my $new = FS::cust_pkg->new({
2335 pkgpart => $link->dst_pkgpart,
2336 pkglinknum => $link->pkglinknum,
2337 custnum => $custnum,
2338 main_pkgnum => $cust_pkg->pkgnum,
2339 locationnum => $cust_pkg->locationnum,
2340 start_date => $cust_pkg->start_date,
2341 order_date => $cust_pkg->order_date,
2342 expire => $cust_pkg->expire,
2343 adjourn => $cust_pkg->adjourn,
2344 contract_end => $cust_pkg->contract_end,
2345 refnum => $cust_pkg->refnum,
2346 discountnum => $cust_pkg->discountnum,
2347 waive_setup => $cust_pkg->waive_setup,
2349 if ( $old and $opt->{'keep_dates'} ) {
2350 foreach (qw(setup bill last_bill)) {
2351 $new->set($_, $old->get($_));
2354 $error = $new->insert( allow_pkgpart => $same_pkgpart );
2357 $error ||= $old->transfer($new);
2359 if ( $error and $error > 0 ) {
2360 # no reason why this should ever fail, but still...
2361 $error = "Unable to transfer all services from supplemental package ".
2365 $dbh->rollback if $oldAutoCommit;
2368 push @new_supp_pkgs, $new;
2370 } # if !$opt->{'cust_pkg'}
2371 # because if there is one, then supplemental packages would already
2372 # have been created for it.
2374 #Good to go, cancel old package. Notify 'cancel' of whether to credit
2376 #Don't allow billing the package (preceding period packages and/or
2377 #outstanding usage) if we are keeping dates (i.e. location changing),
2378 #because the new package will be billed for the same date range.
2379 #Supplemental packages are also canceled here.
2381 # during scheduled changes, avoid canceling the package we just
2383 $self->set('change_to_pkgnum' => '');
2385 $error = $self->cancel(
2387 unused_credit => $unused_credit,
2388 nobill => $keep_dates,
2389 change_custnum => ( $self->custnum != $custnum ? $custnum : '' ),
2390 no_delay_cancel => 1,
2393 $dbh->rollback if $oldAutoCommit;
2394 return "canceling old package: $error";
2397 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
2399 my $error = $cust_pkg->cust_main->bill(
2400 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
2403 $dbh->rollback if $oldAutoCommit;
2404 return "billing new package: $error";
2408 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2414 =item change_later OPTION => VALUE...
2416 Schedule a package change for a later date. This actually orders the new
2417 package immediately, but sets its start date for a future date, and sets
2418 the current package to expire on the same date.
2420 If the package is already scheduled for a change, this can be called with
2421 'start_date' to change the scheduled date, or with pkgpart and/or
2422 locationnum to modify the package change. To cancel the scheduled change
2423 entirely, see C<abort_change>.
2431 The date for the package change. Required, and must be in the future.
2441 The pkgpart, locationnum, quantity and optional contract_end of the new
2442 package, with the same meaning as in C<change>.
2450 my $opt = ref($_[0]) ? shift : { @_ };
2452 # check contract_end, prevent adding/removing
2453 my $error = $self->_check_change($opt);
2454 return $error if $error;
2456 my $oldAutoCommit = $FS::UID::AutoCommit;
2457 local $FS::UID::AutoCommit = 0;
2460 my $cust_main = $self->cust_main;
2462 my $date = delete $opt->{'start_date'} or return 'start_date required';
2464 if ( $date <= time ) {
2465 $dbh->rollback if $oldAutoCommit;
2466 return "start_date $date is in the past";
2469 if ( $self->change_to_pkgnum ) {
2470 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
2471 my $new_pkgpart = $opt->{'pkgpart'}
2472 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $change_to->pkgpart;
2473 my $new_locationnum = $opt->{'locationnum'}
2474 if $opt->{'locationnum'} and $opt->{'locationnum'} != $change_to->locationnum;
2475 my $new_quantity = $opt->{'quantity'}
2476 if $opt->{'quantity'} and $opt->{'quantity'} != $change_to->quantity;
2477 my $new_contract_end = $opt->{'contract_end'}
2478 if $opt->{'contract_end'} and $opt->{'contract_end'} != $change_to->contract_end;
2479 if ( $new_pkgpart or $new_locationnum or $new_quantity or $new_contract_end ) {
2480 # it hasn't been billed yet, so in principle we could just edit
2481 # it in place (w/o a package change), but that's bad form.
2482 # So change the package according to the new options...
2483 my $err_or_pkg = $change_to->change(%$opt);
2484 if ( ref $err_or_pkg ) {
2485 # Then set that package up for a future start.
2486 $self->set('change_to_pkgnum', $err_or_pkg->pkgnum);
2487 $self->set('expire', $date); # in case it's different
2488 $err_or_pkg->set('start_date', $date);
2489 $err_or_pkg->set('change_date', '');
2490 $err_or_pkg->set('change_pkgnum', '');
2492 $error = $self->replace ||
2493 $err_or_pkg->replace ||
2494 #because change() might've edited existing scheduled change in place
2495 (($err_or_pkg->pkgnum == $change_to->pkgnum) ? '' :
2496 $change_to->cancel('no_delay_cancel' => 1) ||
2497 $change_to->delete);
2499 $error = $err_or_pkg;
2501 } else { # change the start date only.
2502 $self->set('expire', $date);
2503 $change_to->set('start_date', $date);
2504 $error = $self->replace || $change_to->replace;
2507 $dbh->rollback if $oldAutoCommit;
2510 $dbh->commit if $oldAutoCommit;
2513 } # if $self->change_to_pkgnum
2515 my $new_pkgpart = $opt->{'pkgpart'}
2516 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart;
2517 my $new_locationnum = $opt->{'locationnum'}
2518 if $opt->{'locationnum'} and $opt->{'locationnum'} != $self->locationnum;
2519 my $new_quantity = $opt->{'quantity'}
2520 if $opt->{'quantity'} and $opt->{'quantity'} != $self->quantity;
2521 my $new_contract_end = $opt->{'contract_end'}
2522 if $opt->{'contract_end'} and $opt->{'contract_end'} != $self->contract_end;
2524 return '' unless $new_pkgpart or $new_locationnum or $new_quantity or $new_contract_end; # wouldn't do anything
2526 # allow $opt->{'locationnum'} = '' to specifically set it to null
2527 # (i.e. customer default location)
2528 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2530 my $new = FS::cust_pkg->new( {
2531 custnum => $self->custnum,
2532 locationnum => $opt->{'locationnum'},
2533 start_date => $date,
2534 map { $_ => ( $opt->{$_} || $self->$_() ) }
2535 qw( pkgpart quantity refnum salesnum contract_end )
2537 $error = $new->insert('change' => 1,
2538 'allow_pkgpart' => ($new_pkgpart ? 0 : 1));
2540 $self->set('change_to_pkgnum', $new->pkgnum);
2541 $self->set('expire', $date);
2542 $error = $self->replace;
2545 $dbh->rollback if $oldAutoCommit;
2547 $dbh->commit if $oldAutoCommit;
2555 Cancels a future package change scheduled by C<change_later>.
2561 my $pkgnum = $self->change_to_pkgnum;
2562 my $change_to = FS::cust_pkg->by_key($pkgnum) if $pkgnum;
2565 $error = $change_to->cancel || $change_to->delete;
2566 return $error if $error;
2568 $self->set('change_to_pkgnum', '');
2569 $self->set('expire', '');
2573 =item set_quantity QUANTITY
2575 Change the package's quantity field. This is one of the few package properties
2576 that can safely be changed without canceling and reordering the package
2577 (because it doesn't affect tax eligibility). Returns an error or an
2584 $self = $self->replace_old; # just to make sure
2585 $self->quantity(shift);
2589 =item set_salesnum SALESNUM
2591 Change the package's salesnum (sales person) field. This is one of the few
2592 package properties that can safely be changed without canceling and reordering
2593 the package (because it doesn't affect tax eligibility). Returns an error or
2600 $self = $self->replace_old; # just to make sure
2601 $self->salesnum(shift);
2603 # XXX this should probably reassign any credit that's already been given
2606 =item modify_charge OPTIONS
2608 Change the properties of a one-time charge. The following properties can
2609 be changed this way:
2610 - pkg: the package description
2611 - classnum: the package class
2612 - additional: arrayref of additional invoice details to add to this package
2614 and, I<if the charge has not yet been billed>:
2615 - start_date: the date when it will be billed
2616 - amount: the setup fee to be charged
2617 - quantity: the multiplier for the setup fee
2618 - separate_bill: whether to put the charge on a separate invoice
2620 If you pass 'adjust_commission' => 1, and the classnum changes, and there are
2621 commission credits linked to this charge, they will be recalculated.
2628 my $part_pkg = $self->part_pkg;
2629 my $pkgnum = $self->pkgnum;
2632 my $oldAutoCommit = $FS::UID::AutoCommit;
2633 local $FS::UID::AutoCommit = 0;
2635 return "Can't use modify_charge except on one-time charges"
2636 unless $part_pkg->freq eq '0';
2638 if ( length($opt{'pkg'}) and $part_pkg->pkg ne $opt{'pkg'} ) {
2639 $part_pkg->set('pkg', $opt{'pkg'});
2642 my %pkg_opt = $part_pkg->options;
2643 my $pkg_opt_modified = 0;
2645 $opt{'additional'} ||= [];
2648 foreach (grep /^additional/, keys %pkg_opt) {
2649 ($i) = ($_ =~ /^additional_info(\d+)$/);
2650 $old_additional[$i] = $pkg_opt{$_} if $i;
2651 delete $pkg_opt{$_};
2654 for ( $i = 0; exists($opt{'additional'}->[$i]); $i++ ) {
2655 $pkg_opt{ "additional_info$i" } = $opt{'additional'}->[$i];
2656 if (!exists($old_additional[$i])
2657 or $old_additional[$i] ne $opt{'additional'}->[$i])
2659 $pkg_opt_modified = 1;
2662 $pkg_opt_modified = 1 if (scalar(@old_additional) - 1) != $i;
2663 $pkg_opt{'additional_count'} = $i if $i > 0;
2666 if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} )
2669 $old_classnum = $part_pkg->classnum;
2670 $part_pkg->set('classnum', $opt{'classnum'});
2673 if ( !$self->get('setup') ) {
2674 # not yet billed, so allow amount, setup_cost, quantity, start_date,
2677 if ( exists($opt{'amount'})
2678 and $part_pkg->option('setup_fee') != $opt{'amount'}
2679 and $opt{'amount'} > 0 ) {
2681 $pkg_opt{'setup_fee'} = $opt{'amount'};
2682 $pkg_opt_modified = 1;
2685 if ( exists($opt{'setup_cost'})
2686 and $part_pkg->setup_cost != $opt{'setup_cost'}
2687 and $opt{'setup_cost'} > 0 ) {
2689 $part_pkg->set('setup_cost', $opt{'setup_cost'});
2692 if ( exists($opt{'quantity'})
2693 and $opt{'quantity'} != $self->quantity
2694 and $opt{'quantity'} > 0 ) {
2696 $self->set('quantity', $opt{'quantity'});
2699 if ( exists($opt{'start_date'})
2700 and $opt{'start_date'} != $self->start_date ) {
2702 $self->set('start_date', $opt{'start_date'});
2705 if ( exists($opt{'separate_bill'})
2706 and $opt{'separate_bill'} ne $self->separate_bill ) {
2708 $self->set('separate_bill', $opt{'separate_bill'});
2712 } # else simply ignore them; the UI shouldn't allow editing the fields
2714 if ( exists($opt{'taxclass'})
2715 and $part_pkg->taxclass ne $opt{'taxclass'}) {
2717 $part_pkg->set('taxclass', $opt{'taxclass'});
2721 if ( $part_pkg->modified or $pkg_opt_modified ) {
2722 # can we safely modify the package def?
2723 # Yes, if it's not available for purchase, and this is the only instance
2725 if ( $part_pkg->disabled
2726 and FS::cust_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 1
2727 and FS::quotation_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 0
2729 $error = $part_pkg->replace( options => \%pkg_opt );
2732 $part_pkg = $part_pkg->clone;
2733 $part_pkg->set('disabled' => 'Y');
2734 $error = $part_pkg->insert( options => \%pkg_opt );
2735 # and associate this as yet-unbilled package to the new package def
2736 $self->set('pkgpart' => $part_pkg->pkgpart);
2739 $dbh->rollback if $oldAutoCommit;
2744 if ($self->modified) { # for quantity or start_date change, or if we had
2745 # to clone the existing package def
2746 my $error = $self->replace;
2747 return $error if $error;
2749 if (defined $old_classnum) {
2750 # fix invoice grouping records
2751 my $old_catname = $old_classnum
2752 ? FS::pkg_class->by_key($old_classnum)->categoryname
2754 my $new_catname = $opt{'classnum'}
2755 ? $part_pkg->pkg_class->categoryname
2757 if ( $old_catname ne $new_catname ) {
2758 foreach my $cust_bill_pkg ($self->cust_bill_pkg) {
2759 # (there should only be one...)
2760 my @display = qsearch( 'cust_bill_pkg_display', {
2761 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2762 'section' => $old_catname,
2764 foreach (@display) {
2765 $_->set('section', $new_catname);
2766 $error = $_->replace;
2768 $dbh->rollback if $oldAutoCommit;
2772 } # foreach $cust_bill_pkg
2775 if ( $opt{'adjust_commission'} ) {
2776 # fix commission credits...tricky.
2777 foreach my $cust_event ($self->cust_event) {
2778 my $part_event = $cust_event->part_event;
2779 foreach my $table (qw(sales agent)) {
2781 "FS::part_event::Action::Mixin::credit_${table}_pkg_class";
2782 my $credit = qsearchs('cust_credit', {
2783 'eventnum' => $cust_event->eventnum,
2785 if ( $part_event->isa($class) ) {
2786 # Yes, this results in current commission rates being applied
2787 # retroactively to a one-time charge. For accounting purposes
2788 # there ought to be some kind of time limit on doing this.
2789 my $amount = $part_event->_calc_credit($self);
2790 if ( $credit and $credit->amount ne $amount ) {
2791 # Void the old credit.
2792 $error = $credit->void('Package class changed');
2794 $dbh->rollback if $oldAutoCommit;
2795 return "$error (adjusting commission credit)";
2798 # redo the event action to recreate the credit.
2800 eval { $part_event->do_action( $self, $cust_event ) };
2802 $dbh->rollback if $oldAutoCommit;
2805 } # if $part_event->isa($class)
2807 } # foreach $cust_event
2808 } # if $opt{'adjust_commission'}
2809 } # if defined $old_classnum
2811 $dbh->commit if $oldAutoCommit;
2817 use Storable 'thaw';
2820 sub process_bulk_cust_pkg {
2822 my $param = thaw(decode_base64(shift));
2823 warn Dumper($param) if $DEBUG;
2825 my $old_part_pkg = qsearchs('part_pkg',
2826 { pkgpart => $param->{'old_pkgpart'} });
2827 my $new_part_pkg = qsearchs('part_pkg',
2828 { pkgpart => $param->{'new_pkgpart'} });
2829 die "Must select a new package type\n" unless $new_part_pkg;
2830 #my $keep_dates = $param->{'keep_dates'} || 0;
2831 my $keep_dates = 1; # there is no good reason to turn this off
2833 local $SIG{HUP} = 'IGNORE';
2834 local $SIG{INT} = 'IGNORE';
2835 local $SIG{QUIT} = 'IGNORE';
2836 local $SIG{TERM} = 'IGNORE';
2837 local $SIG{TSTP} = 'IGNORE';
2838 local $SIG{PIPE} = 'IGNORE';
2840 my $oldAutoCommit = $FS::UID::AutoCommit;
2841 local $FS::UID::AutoCommit = 0;
2844 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
2847 foreach my $old_cust_pkg ( @cust_pkgs ) {
2849 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
2850 if ( $old_cust_pkg->getfield('cancel') ) {
2851 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
2852 $old_cust_pkg->pkgnum."\n"
2856 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
2858 my $error = $old_cust_pkg->change(
2859 'pkgpart' => $param->{'new_pkgpart'},
2860 'keep_dates' => $keep_dates
2862 if ( !ref($error) ) { # change returns the cust_pkg on success
2864 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
2867 $dbh->commit if $oldAutoCommit;
2873 Returns the last bill date, or if there is no last bill date, the setup date.
2874 Useful for billing metered services.
2880 return $self->setfield('last_bill', $_[0]) if @_;
2881 return $self->getfield('last_bill') if $self->getfield('last_bill');
2882 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
2883 'edate' => $self->bill, } );
2884 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
2887 =item last_cust_pkg_reason ACTION
2889 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
2890 Returns false if there is no reason or the package is not currenly ACTION'd
2891 ACTION is one of adjourn, susp, cancel, or expire.
2895 sub last_cust_pkg_reason {
2896 my ( $self, $action ) = ( shift, shift );
2897 my $date = $self->get($action);
2899 'table' => 'cust_pkg_reason',
2900 'hashref' => { 'pkgnum' => $self->pkgnum,
2901 'action' => substr(uc($action), 0, 1),
2904 'order_by' => 'ORDER BY num DESC LIMIT 1',
2908 =item last_reason ACTION
2910 Returns the most recent ACTION FS::reason associated with the package.
2911 Returns false if there is no reason or the package is not currenly ACTION'd
2912 ACTION is one of adjourn, susp, cancel, or expire.
2917 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
2918 $cust_pkg_reason->reason
2919 if $cust_pkg_reason;
2924 Returns the definition for this billing item, as an FS::part_pkg object (see
2931 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
2932 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
2933 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
2938 Returns the cancelled package this package was changed from, if any.
2944 return '' unless $self->change_pkgnum;
2945 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
2948 =item change_cust_main
2950 Returns the customter this package was detached to, if any.
2954 sub change_cust_main {
2956 return '' unless $self->change_custnum;
2957 qsearchs('cust_main', { 'custnum' => $self->change_custnum } );
2962 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
2969 $self->part_pkg->calc_setup($self, @_);
2974 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
2981 $self->part_pkg->calc_recur($self, @_);
2986 Returns the base setup fee (per unit) of this package, from the package
2991 # minimal version for 3.x; in 4.x this can invoke currency conversion
2995 $self->part_pkg->unit_setup($self);
3000 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
3007 $self->part_pkg->base_recur($self, @_);
3012 Calls the I<calc_remain> of the FS::part_pkg object associated with this
3019 $self->part_pkg->calc_remain($self, @_);
3024 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
3031 $self->part_pkg->calc_cancel($self, @_);
3036 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
3042 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
3045 =item cust_pkg_detail [ DETAILTYPE ]
3047 Returns any customer package details for this package (see
3048 L<FS::cust_pkg_detail>).
3050 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
3054 sub cust_pkg_detail {
3056 my %hash = ( 'pkgnum' => $self->pkgnum );
3057 $hash{detailtype} = shift if @_;
3059 'table' => 'cust_pkg_detail',
3060 'hashref' => \%hash,
3061 'order_by' => 'ORDER BY weight, pkgdetailnum',
3065 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
3067 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
3069 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
3071 If there is an error, returns the error, otherwise returns false.
3075 sub set_cust_pkg_detail {
3076 my( $self, $detailtype, @details ) = @_;
3078 local $SIG{HUP} = 'IGNORE';
3079 local $SIG{INT} = 'IGNORE';
3080 local $SIG{QUIT} = 'IGNORE';
3081 local $SIG{TERM} = 'IGNORE';
3082 local $SIG{TSTP} = 'IGNORE';
3083 local $SIG{PIPE} = 'IGNORE';
3085 my $oldAutoCommit = $FS::UID::AutoCommit;
3086 local $FS::UID::AutoCommit = 0;
3089 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
3090 my $error = $current->delete;
3092 $dbh->rollback if $oldAutoCommit;
3093 return "error removing old detail: $error";
3097 foreach my $detail ( @details ) {
3098 my $cust_pkg_detail = new FS::cust_pkg_detail {
3099 'pkgnum' => $self->pkgnum,
3100 'detailtype' => $detailtype,
3101 'detail' => $detail,
3103 my $error = $cust_pkg_detail->insert;
3105 $dbh->rollback if $oldAutoCommit;
3106 return "error adding new detail: $error";
3111 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3118 Returns the customer billing events (see L<FS::cust_event>) for this invoice.
3122 #false laziness w/cust_bill.pm
3126 'table' => 'cust_event',
3127 'addl_from' => 'JOIN part_event USING ( eventpart )',
3128 'hashref' => { 'tablenum' => $self->pkgnum },
3129 'extra_sql' => " AND eventtable = 'cust_pkg' ",
3133 =item num_cust_event
3135 Returns the number of customer billing events (see L<FS::cust_event>) for this package.
3139 #false laziness w/cust_bill.pm
3140 sub num_cust_event {
3142 my $sql = "SELECT COUNT(*) ". $self->_from_cust_event_where;
3143 $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref->[0];
3146 =item exists_cust_event
3148 Returns true if there are customer billing events (see L<FS::cust_event>) for this package. More efficient than using num_cust_event.
3152 sub exists_cust_event {
3154 my $sql = "SELECT 1 ". $self->_from_cust_event_where. " LIMIT 1";
3155 my $row = $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref;
3156 $row ? $row->[0] : '';
3159 sub _from_cust_event_where {
3161 " FROM cust_event JOIN part_event USING ( eventpart ) ".
3162 " WHERE tablenum = ? AND eventtable = 'cust_pkg' ";
3166 my( $self, $sql, @args ) = @_;
3167 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
3168 $sth->execute(@args) or die $sth->errstr. " executing $sql";
3172 =item cust_svc [ SVCPART ] (old, deprecated usage)
3174 =item cust_svc [ OPTION => VALUE ... ] (current usage)
3176 =item cust_svc_unsorted [ OPTION => VALUE ... ]
3178 Returns the services for this package, as FS::cust_svc objects (see
3179 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
3180 spcififed, returns only the matching services.
3182 As an optimization, use the cust_svc_unsorted version if you are not displaying
3189 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
3190 $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref(@_) );
3193 sub cust_svc_unsorted {
3195 @{ $self->cust_svc_unsorted_arrayref(@_) };
3198 sub cust_svc_unsorted_arrayref {
3201 return [] unless $self->num_cust_svc(@_);
3204 if ( @_ && $_[0] =~ /^\d+/ ) {
3205 $opt{svcpart} = shift;
3206 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
3213 'table' => 'cust_svc',
3214 'hashref' => { 'pkgnum' => $self->pkgnum },
3216 if ( $opt{svcpart} ) {
3217 $search{hashref}->{svcpart} = $opt{'svcpart'};
3219 if ( $opt{'svcdb'} ) {
3220 $search{addl_from} = ' LEFT JOIN part_svc USING ( svcpart ) ';
3221 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{'svcdb'} );
3224 [ qsearch(\%search) ];
3228 =item overlimit [ SVCPART ]
3230 Returns the services for this package which have exceeded their
3231 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
3232 is specified, return only the matching services.
3238 return () unless $self->num_cust_svc(@_);
3239 grep { $_->overlimit } $self->cust_svc(@_);
3242 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3244 Returns historical services for this package created before END TIMESTAMP and
3245 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
3246 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
3247 I<pkg_svc.hidden> flag will be omitted.
3253 warn "$me _h_cust_svc called on $self\n"
3256 my ($end, $start, $mode) = @_;
3257 my @cust_svc = $self->_sort_cust_svc(
3258 [ qsearch( 'h_cust_svc',
3259 { 'pkgnum' => $self->pkgnum, },
3260 FS::h_cust_svc->sql_h_search(@_),
3263 if ( defined($mode) && $mode eq 'I' ) {
3264 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
3265 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
3271 sub _sort_cust_svc {
3272 my( $self, $arrayref ) = @_;
3275 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
3277 my %pkg_svc = map { $_->svcpart => $_ }
3278 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
3283 my $pkg_svc = $pkg_svc{ $_->svcpart } || '';
3285 $pkg_svc ? $pkg_svc->primary_svc : '',
3286 $pkg_svc ? $pkg_svc->quantity : 0,
3293 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
3295 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
3297 Returns the number of services for this package. Available options are svcpart
3298 and svcdb. If either is spcififed, returns only the matching services.
3305 return $self->{'_num_cust_svc'}
3307 && exists($self->{'_num_cust_svc'})
3308 && $self->{'_num_cust_svc'} =~ /\d/;
3310 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
3314 if ( @_ && $_[0] =~ /^\d+/ ) {
3315 $opt{svcpart} = shift;
3316 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
3322 my $select = 'SELECT COUNT(*) FROM cust_svc ';
3323 my $where = ' WHERE pkgnum = ? ';
3324 my @param = ($self->pkgnum);
3326 if ( $opt{'svcpart'} ) {
3327 $where .= ' AND svcpart = ? ';
3328 push @param, $opt{'svcpart'};
3330 if ( $opt{'svcdb'} ) {
3331 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
3332 $where .= ' AND svcdb = ? ';
3333 push @param, $opt{'svcdb'};
3336 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
3337 $sth->execute(@param) or die $sth->errstr;
3338 $sth->fetchrow_arrayref->[0];
3341 =item available_part_svc
3343 Returns a list of FS::part_svc objects representing services included in this
3344 package but not yet provisioned. Each FS::part_svc object also has an extra
3345 field, I<num_avail>, which specifies the number of available services.
3347 Accepts option I<provision_hold>; if true, only returns part_svc for which the
3348 associated pkg_svc has the provision_hold flag set.
3352 sub available_part_svc {
3356 my $pkg_quantity = $self->quantity || 1;
3358 grep { $_->num_avail > 0 }
3360 my $part_svc = $_->part_svc;
3361 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
3362 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
3364 # more evil encapsulation breakage
3365 if ($part_svc->{'Hash'}{'num_avail'} > 0) {
3366 my @exports = $part_svc->part_export_did;
3367 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
3372 grep { $opt{'provision_hold'} ? $_->provision_hold : 1 }
3373 $self->part_pkg->pkg_svc;
3376 =item part_svc [ OPTION => VALUE ... ]
3378 Returns a list of FS::part_svc objects representing provisioned and available
3379 services included in this package. Each FS::part_svc object also has the
3380 following extra fields:
3394 (services) - array reference containing the provisioned services, as cust_svc objects
3398 Accepts two options:
3402 =item summarize_size
3404 If true, will omit the extra cust_pkg_svc option for objects where num_cust_svc
3405 is this size or greater.
3407 =item hide_discontinued
3409 If true, will omit looking for services that are no longer avaialble in the
3417 #label -> ($cust_svc->label)[1]
3423 my $pkg_quantity = $self->quantity || 1;
3425 #XXX some sort of sort order besides numeric by svcpart...
3426 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
3428 my $part_svc = $pkg_svc->part_svc;
3429 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3430 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
3431 $part_svc->{'Hash'}{'num_avail'} =
3432 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
3433 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3434 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
3435 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
3436 && $num_cust_svc >= $opt{summarize_size};
3437 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
3439 } $self->part_pkg->pkg_svc;
3441 unless ( $opt{hide_discontinued} ) {
3443 push @part_svc, map {
3445 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3446 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
3447 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
3448 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3449 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
3451 } $self->extra_part_svc;
3458 =item extra_part_svc
3460 Returns a list of FS::part_svc objects corresponding to services in this
3461 package which are still provisioned but not (any longer) available in the
3466 sub extra_part_svc {
3469 my $pkgnum = $self->pkgnum;
3470 #my $pkgpart = $self->pkgpart;
3473 # 'table' => 'part_svc',
3476 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
3477 # WHERE pkg_svc.svcpart = part_svc.svcpart
3478 # AND pkg_svc.pkgpart = ?
3481 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
3482 # LEFT JOIN cust_pkg USING ( pkgnum )
3483 # WHERE cust_svc.svcpart = part_svc.svcpart
3486 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
3489 #seems to benchmark slightly faster... (or did?)
3491 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
3492 my $pkgparts = join(',', @pkgparts);
3495 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
3496 #MySQL doesn't grok DISINCT ON
3497 'select' => 'DISTINCT part_svc.*',
3498 'table' => 'part_svc',
3500 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
3501 AND pkg_svc.pkgpart IN ($pkgparts)
3504 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
3505 LEFT JOIN cust_pkg USING ( pkgnum )
3508 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
3509 'extra_param' => [ [$self->pkgnum=>'int'] ],
3515 Returns a short status string for this package, currently:
3521 =item not yet billed
3523 =item one-time charge
3538 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
3540 return 'cancelled' if $self->get('cancel');
3541 return 'on hold' if $self->susp && ! $self->setup;
3542 return 'suspended' if $self->susp;
3543 return 'not yet billed' unless $self->setup;
3544 return 'one-time charge' if $freq =~ /^(0|$)/;
3548 =item ucfirst_status
3550 Returns the status with the first character capitalized.
3554 sub ucfirst_status {
3555 ucfirst(shift->status);
3560 Class method that returns the list of possible status strings for packages
3561 (see L<the status method|/status>). For example:
3563 @statuses = FS::cust_pkg->statuses();
3567 tie my %statuscolor, 'Tie::IxHash',
3568 'on hold' => 'FF00F5', #brighter purple!
3569 'not yet billed' => '009999', #teal? cyan?
3570 'one-time charge' => '0000CC', #blue #'000000',
3571 'active' => '00CC00',
3572 'suspended' => 'FF9900',
3573 'cancelled' => 'FF0000',
3577 my $self = shift; #could be class...
3578 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
3579 # # mayble split btw one-time vs. recur
3590 Returns a hex triplet color string for this package's status.
3596 $statuscolor{$self->status};
3599 =item is_status_delay_cancel
3601 Returns true if part_pkg has option delay_cancel,
3602 cust_pkg status is 'suspended' and expire is set
3603 to cancel package within the next day (or however
3604 many days are set in global config part_pkg-delay_cancel-days.
3606 Accepts option I<part_pkg-delay_cancel-days> which should be
3607 the value of the config setting, to avoid looking it up again.
3609 This is not a real status, this only meant for hacking display
3610 values, because otherwise treating the package as suspended is
3611 really the whole point of the delay_cancel option.
3615 sub is_status_delay_cancel {
3616 my ($self,%opt) = @_;
3617 if ( $self->main_pkgnum and $self->pkglinknum ) {
3618 return $self->main_pkg->is_status_delay_cancel;
3620 return 0 unless $self->part_pkg->option('delay_cancel',1);
3621 return 0 unless $self->status eq 'suspended';
3622 return 0 unless $self->expire;
3623 my $expdays = $opt{'part_pkg-delay_cancel-days'};
3625 my $conf = new FS::Conf;
3626 $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
3628 my $expsecs = 60*60*24*$expdays;
3629 return 0 unless $self->expire < time + $expsecs;
3635 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
3636 "pkg - comment" depending on user preference).
3642 my $label = $self->part_pkg->pkg_comment( 'nopkgpart' => 1 );
3643 $label = $self->pkgnum. ": $label"
3644 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
3648 =item pkg_label_long
3650 Returns a long label for this package, adding the primary service's label to
3655 sub pkg_label_long {
3657 my $label = $self->pkg_label;
3658 my $cust_svc = $self->primary_cust_svc;
3659 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
3665 Returns a customer-localized label for this package.
3671 $self->part_pkg->pkg_locale( $self->cust_main->locale );
3674 =item primary_cust_svc
3676 Returns a primary service (as FS::cust_svc object) if one can be identified.
3680 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
3682 sub primary_cust_svc {
3685 my @cust_svc = $self->cust_svc;
3687 return '' unless @cust_svc; #no serivces - irrelevant then
3689 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
3691 # primary service as specified in the package definition
3692 # or exactly one service definition with quantity one
3693 my $svcpart = $self->part_pkg->svcpart;
3694 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
3695 return $cust_svc[0] if scalar(@cust_svc) == 1;
3697 #couldn't identify one thing..
3703 Returns a list of lists, calling the label method for all services
3704 (see L<FS::cust_svc>) of this billing item.
3710 map { [ $_->label ] } $self->cust_svc;
3713 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3715 Like the labels method, but returns historical information on services that
3716 were active as of END_TIMESTAMP and (optionally) not cancelled before
3717 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
3718 I<pkg_svc.hidden> flag will be omitted.
3720 Returns a list of lists, calling the label method for all (historical) services
3721 (see L<FS::h_cust_svc>) of this billing item.
3727 warn "$me _h_labels called on $self\n"
3729 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
3734 Like labels, except returns a simple flat list, and shortens long
3735 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3736 identical services to one line that lists the service label and the number of
3737 individual services rather than individual items.
3742 shift->_labels_short( 'labels', @_ );
3745 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
3747 Like h_labels, except returns a simple flat list, and shortens long
3748 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3749 identical services to one line that lists the service label and the number of
3750 individual services rather than individual items.
3754 sub h_labels_short {
3755 shift->_labels_short( 'h_labels', @_ );
3759 my( $self, $method ) = ( shift, shift );
3761 warn "$me _labels_short called on $self with $method method\n"
3764 my $conf = new FS::Conf;
3765 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
3767 warn "$me _labels_short populating \%labels\n"
3771 #tie %labels, 'Tie::IxHash';
3772 push @{ $labels{$_->[0]} }, $_->[1]
3773 foreach $self->$method(@_);
3775 warn "$me _labels_short populating \@labels\n"
3779 foreach my $label ( keys %labels ) {
3781 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
3782 my $num = scalar(@values);
3783 warn "$me _labels_short $num items for $label\n"
3786 if ( $num > $max_same_services ) {
3787 warn "$me _labels_short more than $max_same_services, so summarizing\n"
3789 push @labels, "$label ($num)";
3791 if ( $conf->exists('cust_bill-consolidate_services') ) {
3792 warn "$me _labels_short consolidating services\n"
3794 # push @labels, "$label: ". join(', ', @values);
3796 my $detail = "$label: ";
3797 $detail .= shift(@values). ', '
3799 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
3801 push @labels, $detail;
3803 warn "$me _labels_short done consolidating services\n"
3806 warn "$me _labels_short adding service data\n"
3808 push @labels, map { "$label: $_" } @values;
3819 Returns the parent customer object (see L<FS::cust_main>).
3825 cluck 'cust_pkg->cust_main called' if $DEBUG;
3826 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
3831 Returns the balance for this specific package, when using
3832 experimental package balance.
3838 $self->cust_main->balance_pkgnum( $self->pkgnum );
3841 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
3845 Returns the location object, if any (see L<FS::cust_location>).
3847 =item cust_location_or_main
3849 If this package is associated with a location, returns the locaiton (see
3850 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
3852 =item location_label [ OPTION => VALUE ... ]
3854 Returns the label of the location object (see L<FS::cust_location>).
3858 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
3860 =item tax_locationnum
3862 Returns the foreign key to a L<FS::cust_location> object for calculating
3863 tax on this package, as determined by the C<tax-pkg_address> and
3864 C<tax-ship_address> configuration flags.
3868 sub tax_locationnum {
3870 my $conf = FS::Conf->new;
3871 if ( $conf->exists('tax-pkg_address') ) {
3872 return $self->locationnum;
3874 elsif ( $conf->exists('tax-ship_address') ) {
3875 return $self->cust_main->ship_locationnum;
3878 return $self->cust_main->bill_locationnum;
3884 Returns the L<FS::cust_location> object for tax_locationnum.
3890 my $conf = FS::Conf->new;
3891 if ( $conf->exists('tax-pkg_address') and $self->locationnum ) {
3892 return FS::cust_location->by_key($self->locationnum);
3894 elsif ( $conf->exists('tax-ship_address') ) {
3895 return $self->cust_main->ship_location;
3898 return $self->cust_main->bill_location;
3902 =item seconds_since TIMESTAMP
3904 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
3905 package have been online since TIMESTAMP, according to the session monitor.
3907 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
3908 L<Time::Local> and L<Date::Parse> for conversion functions.
3913 my($self, $since) = @_;
3916 foreach my $cust_svc (
3917 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
3919 $seconds += $cust_svc->seconds_since($since);
3926 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
3928 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
3929 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
3932 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3933 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3939 sub seconds_since_sqlradacct {
3940 my($self, $start, $end) = @_;
3944 foreach my $cust_svc (
3946 my $part_svc = $_->part_svc;
3947 $part_svc->svcdb eq 'svc_acct'
3948 && scalar($part_svc->part_export_usage);
3951 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
3958 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
3960 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
3961 in this package for sessions ending between TIMESTAMP_START (inclusive) and
3965 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3966 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3971 sub attribute_since_sqlradacct {
3972 my($self, $start, $end, $attrib) = @_;
3976 foreach my $cust_svc (
3978 my $part_svc = $_->part_svc;
3979 scalar($part_svc->part_export_usage);
3982 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
3994 my( $self, $value ) = @_;
3995 if ( defined($value) ) {
3996 $self->setfield('quantity', $value);
3998 $self->getfield('quantity') || 1;
4001 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
4003 Transfers as many services as possible from this package to another package.
4005 The destination package can be specified by pkgnum by passing an FS::cust_pkg
4006 object. The destination package must already exist.
4008 Services are moved only if the destination allows services with the correct
4009 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
4010 this option with caution! No provision is made for export differences
4011 between the old and new service definitions. Probably only should be used
4012 when your exports for all service definitions of a given svcdb are identical.
4013 (attempt a transfer without it first, to move all possible svcpart-matching
4016 Any services that can't be moved remain in the original package.
4018 Returns an error, if there is one; otherwise, returns the number of services
4019 that couldn't be moved.
4024 my ($self, $dest_pkgnum, %opt) = @_;
4030 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
4031 $dest = $dest_pkgnum;
4032 $dest_pkgnum = $dest->pkgnum;
4034 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
4037 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
4039 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
4040 $target{$pkg_svc->svcpart} = $pkg_svc->quantity * ( $dest->quantity || 1 );
4043 foreach my $cust_svc ($dest->cust_svc) {
4044 $target{$cust_svc->svcpart}--;
4047 my %svcpart2svcparts = ();
4048 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
4049 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
4050 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
4051 next if exists $svcpart2svcparts{$svcpart};
4052 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
4053 $svcpart2svcparts{$svcpart} = [
4055 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
4057 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
4058 'svcpart' => $_ } );
4060 $pkg_svc ? $pkg_svc->primary_svc : '',
4061 $pkg_svc ? $pkg_svc->quantity : 0,
4065 grep { $_ != $svcpart }
4067 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
4069 warn "alternates for svcpart $svcpart: ".
4070 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
4076 foreach my $cust_svc ($self->cust_svc) {
4077 my $svcnum = $cust_svc->svcnum;
4078 if($target{$cust_svc->svcpart} > 0
4079 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
4080 $target{$cust_svc->svcpart}--;
4081 my $new = new FS::cust_svc { $cust_svc->hash };
4082 $new->pkgnum($dest_pkgnum);
4083 $error = $new->replace($cust_svc);
4084 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
4086 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
4087 warn "alternates to consider: ".
4088 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
4090 my @alternate = grep {
4091 warn "considering alternate svcpart $_: ".
4092 "$target{$_} available in new package\n"
4095 } @{$svcpart2svcparts{$cust_svc->svcpart}};
4097 warn "alternate(s) found\n" if $DEBUG;
4098 my $change_svcpart = $alternate[0];
4099 $target{$change_svcpart}--;
4100 my $new = new FS::cust_svc { $cust_svc->hash };
4101 $new->svcpart($change_svcpart);
4102 $new->pkgnum($dest_pkgnum);
4103 $error = $new->replace($cust_svc);
4111 my @label = $cust_svc->label;
4112 return "service $label[1]: $error";
4118 =item grab_svcnums SVCNUM, SVCNUM ...
4120 Change the pkgnum for the provided services to this packages. If there is an
4121 error, returns the error, otherwise returns false.
4129 local $SIG{HUP} = 'IGNORE';
4130 local $SIG{INT} = 'IGNORE';
4131 local $SIG{QUIT} = 'IGNORE';
4132 local $SIG{TERM} = 'IGNORE';
4133 local $SIG{TSTP} = 'IGNORE';
4134 local $SIG{PIPE} = 'IGNORE';
4136 my $oldAutoCommit = $FS::UID::AutoCommit;
4137 local $FS::UID::AutoCommit = 0;
4140 foreach my $svcnum (@svcnum) {
4141 my $cust_svc = qsearchs('cust_svc', { svcnum=>$svcnum } ) or do {
4142 $dbh->rollback if $oldAutoCommit;
4143 return "unknown svcnum $svcnum";
4145 $cust_svc->pkgnum( $self->pkgnum );
4146 my $error = $cust_svc->replace;
4148 $dbh->rollback if $oldAutoCommit;
4153 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4160 This method is deprecated. See the I<depend_jobnum> option to the insert and
4161 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
4168 local $SIG{HUP} = 'IGNORE';
4169 local $SIG{INT} = 'IGNORE';
4170 local $SIG{QUIT} = 'IGNORE';
4171 local $SIG{TERM} = 'IGNORE';
4172 local $SIG{TSTP} = 'IGNORE';
4173 local $SIG{PIPE} = 'IGNORE';
4175 my $oldAutoCommit = $FS::UID::AutoCommit;
4176 local $FS::UID::AutoCommit = 0;
4179 foreach my $cust_svc ( $self->cust_svc ) {
4180 #false laziness w/svc_Common::insert
4181 my $svc_x = $cust_svc->svc_x;
4182 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
4183 my $error = $part_export->export_insert($svc_x);
4185 $dbh->rollback if $oldAutoCommit;
4191 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4196 =item export_pkg_change OLD_CUST_PKG
4198 Calls the "pkg_change" export action for all services attached to this package.
4202 sub export_pkg_change {
4203 my( $self, $old ) = ( shift, shift );
4205 local $SIG{HUP} = 'IGNORE';
4206 local $SIG{INT} = 'IGNORE';
4207 local $SIG{QUIT} = 'IGNORE';
4208 local $SIG{TERM} = 'IGNORE';
4209 local $SIG{TSTP} = 'IGNORE';
4210 local $SIG{PIPE} = 'IGNORE';
4212 my $oldAutoCommit = $FS::UID::AutoCommit;
4213 local $FS::UID::AutoCommit = 0;
4216 foreach my $svc_x ( map $_->svc_x, $self->cust_svc ) {
4217 my $error = $svc_x->export('pkg_change', $self, $old);
4219 $dbh->rollback if $oldAutoCommit;
4224 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4231 Associates this package with a (suspension or cancellation) reason (see
4232 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
4235 Available options are:
4241 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.
4245 the access_user (see L<FS::access_user>) providing the reason
4253 the action (cancel, susp, adjourn, expire) associated with the reason
4257 If there is an error, returns the error, otherwise returns false.
4262 my ($self, %options) = @_;
4264 my $otaker = $options{reason_otaker} ||
4265 $FS::CurrentUser::CurrentUser->username;
4268 if ( $options{'reason'} =~ /^(\d+)$/ ) {
4272 } elsif ( ref($options{'reason'}) ) {
4274 return 'Enter a new reason (or select an existing one)'
4275 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
4277 my $reason = new FS::reason({
4278 'reason_type' => $options{'reason'}->{'typenum'},
4279 'reason' => $options{'reason'}->{'reason'},
4281 my $error = $reason->insert;
4282 return $error if $error;
4284 $reasonnum = $reason->reasonnum;
4287 return "Unparseable reason: ". $options{'reason'};
4290 my $cust_pkg_reason =
4291 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
4292 'reasonnum' => $reasonnum,
4293 'otaker' => $otaker,
4294 'action' => substr(uc($options{'action'}),0,1),
4295 'date' => $options{'date'}
4300 $cust_pkg_reason->insert;
4303 =item insert_discount
4305 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
4306 inserting a new discount on the fly (see L<FS::discount>).
4308 Available options are:
4316 If there is an error, returns the error, otherwise returns false.
4320 sub insert_discount {
4321 #my ($self, %options) = @_;
4324 my $cust_pkg_discount = new FS::cust_pkg_discount {
4325 'pkgnum' => $self->pkgnum,
4326 'discountnum' => $self->discountnum,
4328 'end_date' => '', #XXX
4329 #for the create a new discount case
4330 '_type' => $self->discountnum__type,
4331 'amount' => $self->discountnum_amount,
4332 'percent' => $self->discountnum_percent,
4333 'months' => $self->discountnum_months,
4334 'setup' => $self->discountnum_setup,
4335 #'disabled' => $self->discountnum_disabled,
4338 $cust_pkg_discount->insert;
4341 =item set_usage USAGE_VALUE_HASHREF
4343 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4344 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4345 upbytes, downbytes, and totalbytes are appropriate keys.
4347 All svc_accts which are part of this package have their values reset.
4352 my ($self, $valueref, %opt) = @_;
4354 #only svc_acct can set_usage for now
4355 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4356 my $svc_x = $cust_svc->svc_x;
4357 $svc_x->set_usage($valueref, %opt)
4358 if $svc_x->can("set_usage");
4362 =item recharge USAGE_VALUE_HASHREF
4364 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4365 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4366 upbytes, downbytes, and totalbytes are appropriate keys.
4368 All svc_accts which are part of this package have their values incremented.
4373 my ($self, $valueref) = @_;
4375 #only svc_acct can set_usage for now
4376 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4377 my $svc_x = $cust_svc->svc_x;
4378 $svc_x->recharge($valueref)
4379 if $svc_x->can("recharge");
4383 =item cust_pkg_discount
4387 sub cust_pkg_discount {
4389 qsearch('cust_pkg_discount', { 'pkgnum' => $self->pkgnum } );
4392 =item cust_pkg_discount_active
4396 sub cust_pkg_discount_active {
4398 grep { $_->status eq 'active' } $self->cust_pkg_discount;
4401 =item cust_pkg_usage
4403 Returns a list of all voice usage counters attached to this package.
4407 sub cust_pkg_usage {
4409 qsearch('cust_pkg_usage', { pkgnum => $self->pkgnum });
4412 =item apply_usage OPTIONS
4414 Takes the following options:
4415 - cdr: a call detail record (L<FS::cdr>)
4416 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
4417 - minutes: the maximum number of minutes to be charged
4419 Finds available usage minutes for a call of this class, and subtracts
4420 up to that many minutes from the usage pool. If the usage pool is empty,
4421 and the C<cdr-minutes_priority> global config option is set, minutes may
4422 be taken from other calls as well. Either way, an allocation record will
4423 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
4424 number of minutes of usage applied to the call.
4429 my ($self, %opt) = @_;
4430 my $cdr = $opt{cdr};
4431 my $rate_detail = $opt{rate_detail};
4432 my $minutes = $opt{minutes};
4433 my $classnum = $rate_detail->classnum;
4434 my $pkgnum = $self->pkgnum;
4435 my $custnum = $self->custnum;
4437 local $SIG{HUP} = 'IGNORE';
4438 local $SIG{INT} = 'IGNORE';
4439 local $SIG{QUIT} = 'IGNORE';
4440 local $SIG{TERM} = 'IGNORE';
4441 local $SIG{TSTP} = 'IGNORE';
4442 local $SIG{PIPE} = 'IGNORE';
4444 my $oldAutoCommit = $FS::UID::AutoCommit;
4445 local $FS::UID::AutoCommit = 0;
4447 my $order = FS::Conf->new->config('cdr-minutes_priority');
4451 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
4453 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
4455 my @usage_recs = qsearch({
4456 'table' => 'cust_pkg_usage',
4457 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
4458 ' JOIN cust_pkg USING (pkgnum)'.
4459 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
4460 'select' => 'cust_pkg_usage.*',
4461 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
4462 " ( cust_pkg.custnum = $custnum AND ".
4463 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4464 $is_classnum . ' AND '.
4465 " cust_pkg_usage.minutes > 0",
4466 'order_by' => " ORDER BY priority ASC",
4469 my $orig_minutes = $minutes;
4471 while (!$error and $minutes > 0 and @usage_recs) {
4472 my $cust_pkg_usage = shift @usage_recs;
4473 $cust_pkg_usage->select_for_update;
4474 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
4475 pkgusagenum => $cust_pkg_usage->pkgusagenum,
4476 acctid => $cdr->acctid,
4477 minutes => min($cust_pkg_usage->minutes, $minutes),
4479 $cust_pkg_usage->set('minutes',
4480 $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes
4482 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
4483 $minutes -= $cdr_cust_pkg_usage->minutes;
4485 if ( $order and $minutes > 0 and !$error ) {
4486 # then try to steal minutes from another call
4488 'table' => 'cdr_cust_pkg_usage',
4489 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
4490 ' JOIN part_pkg_usage USING (pkgusagepart)'.
4491 ' JOIN cust_pkg USING (pkgnum)'.
4492 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
4493 ' JOIN cdr USING (acctid)',
4494 'select' => 'cdr_cust_pkg_usage.*',
4495 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
4496 " ( cust_pkg.pkgnum = $pkgnum OR ".
4497 " ( cust_pkg.custnum = $custnum AND ".
4498 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4499 " part_pkg_usage_class.classnum = $classnum",
4500 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
4502 if ( $order eq 'time' ) {
4503 # find CDRs that are using minutes, but have a later startdate
4505 my $startdate = $cdr->startdate;
4506 if ($startdate !~ /^\d+$/) {
4507 die "bad cdr startdate '$startdate'";
4509 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
4510 # minimize needless reshuffling
4511 $search{'order_by'} .= ', cdr.startdate DESC';
4513 # XXX may not work correctly with rate_time schedules. Could
4514 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
4516 $search{'addl_from'} .=
4517 ' JOIN rate_detail'.
4518 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
4519 if ( $order eq 'rate_high' ) {
4520 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
4521 $rate_detail->min_charge;
4522 $search{'order_by'} .= ', rate_detail.min_charge ASC';
4523 } elsif ( $order eq 'rate_low' ) {
4524 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
4525 $rate_detail->min_charge;
4526 $search{'order_by'} .= ', rate_detail.min_charge DESC';
4528 # this should really never happen
4529 die "invalid cdr-minutes_priority value '$order'\n";
4532 my @cdr_usage_recs = qsearch(\%search);
4534 while (!$error and @cdr_usage_recs and $minutes > 0) {
4535 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
4536 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
4537 my $old_cdr = $cdr_cust_pkg_usage->cdr;
4538 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
4539 $cdr_cust_pkg_usage->select_for_update;
4540 $old_cdr->select_for_update;
4541 $cust_pkg_usage->select_for_update;
4542 # in case someone else stole the usage from this CDR
4543 # while waiting for the lock...
4544 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
4545 # steal the usage allocation and flag the old CDR for reprocessing
4546 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
4547 # if the allocation is more minutes than we need, adjust it...
4548 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
4550 $cdr_cust_pkg_usage->set('minutes', $minutes);
4551 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
4552 $error = $cust_pkg_usage->replace;
4554 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
4555 $error ||= $cdr_cust_pkg_usage->replace;
4556 # deduct the stolen minutes
4557 $minutes -= $cdr_cust_pkg_usage->minutes;
4559 # after all minute-stealing is done, reset the affected CDRs
4560 foreach (values %reproc_cdrs) {
4561 $error ||= $_->set_status('');
4562 # XXX or should we just call $cdr->rate right here?
4563 # it's not like we can create a loop this way, since the min_charge
4564 # or call time has to go monotonically in one direction.
4565 # we COULD get some very deep recursions going, though...
4567 } # if $order and $minutes
4570 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
4572 $dbh->commit if $oldAutoCommit;
4573 return $orig_minutes - $minutes;
4577 =item supplemental_pkgs
4579 Returns a list of all packages supplemental to this one.
4583 sub supplemental_pkgs {
4585 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
4590 Returns the package that this one is supplemental to, if any.
4596 if ( $self->main_pkgnum ) {
4597 return FS::cust_pkg->by_key($self->main_pkgnum);
4604 =head1 CLASS METHODS
4610 Returns an SQL expression identifying recurring packages.
4614 sub recurring_sql { "
4615 '0' != ( select freq from part_pkg
4616 where cust_pkg.pkgpart = part_pkg.pkgpart )
4621 Returns an SQL expression identifying one-time packages.
4626 '0' = ( select freq from part_pkg
4627 where cust_pkg.pkgpart = part_pkg.pkgpart )
4632 Returns an SQL expression identifying ordered packages (recurring packages not
4638 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
4643 Returns an SQL expression identifying active packages.
4648 $_[0]->recurring_sql. "
4649 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4650 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4651 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4654 =item not_yet_billed_sql
4656 Returns an SQL expression identifying packages which have not yet been billed.
4660 sub not_yet_billed_sql { "
4661 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4662 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4663 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4668 Returns an SQL expression identifying inactive packages (one-time packages
4669 that are otherwise unsuspended/uncancelled).
4673 sub inactive_sql { "
4674 ". $_[0]->onetime_sql(). "
4675 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4676 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4677 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4682 Returns an SQL expression identifying on-hold packages.
4687 #$_[0]->recurring_sql(). ' AND '.
4689 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4690 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4691 AND ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4698 Returns an SQL expression identifying suspended packages.
4702 sub suspended_sql { susp_sql(@_); }
4704 #$_[0]->recurring_sql(). ' AND '.
4706 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4707 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4708 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4715 Returns an SQL exprression identifying cancelled packages.
4719 sub cancelled_sql { cancel_sql(@_); }
4721 #$_[0]->recurring_sql(). ' AND '.
4722 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
4727 Returns an SQL expression to give the package status as a string.
4733 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
4734 WHEN ( cust_pkg.susp IS NOT NULL AND cust_pkg.setup IS NULL ) THEN 'on hold'
4735 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
4736 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
4737 WHEN ".onetime_sql()." THEN 'one-time charge'
4742 =item search HASHREF
4746 Returns a qsearch hash expression to search for parameters specified in HASHREF.
4747 Valid parameters are
4755 on hold, active, inactive (or one-time charge), suspended, canceled (or cancelled)
4759 Equivalent to "status", except that "canceled"/"cancelled" will exclude
4760 packages that were changed into a new package with the same pkgpart (i.e.
4761 location or quantity changes).
4765 boolean selects custom packages
4771 pkgpart or arrayref or hashref of pkgparts
4775 arrayref of beginning and ending epoch date
4779 arrayref of beginning and ending epoch date
4783 arrayref of beginning and ending epoch date
4787 arrayref of beginning and ending epoch date
4791 arrayref of beginning and ending epoch date
4795 arrayref of beginning and ending epoch date
4799 arrayref of beginning and ending epoch date
4803 pkgnum or APKG_pkgnum
4807 a value suited to passing to FS::UI::Web::cust_header
4811 specifies the user for agent virtualization
4815 boolean; if true, returns only packages with more than 0 FCC phone lines.
4817 =item state, country
4819 Limit to packages with a service location in the specified state and country.
4820 For FCC 477 reporting, mostly.
4824 Limit to packages whose service locations are the same as the customer's
4825 default service location.
4827 =item location_nocust
4829 Limit to packages whose service locations are not the customer's default
4832 =item location_census
4834 Limit to packages whose service locations have census tracts.
4836 =item location_nocensus
4838 Limit to packages whose service locations do not have a census tract.
4840 =item location_geocode
4842 Limit to packages whose locations have geocodes.
4844 =item location_geocode
4846 Limit to packages whose locations do not have geocodes.
4850 Limit to packages associated with a svc_broadband, associated with a sector,
4851 associated with this towernum (or any of these, if it's an arrayref) (or NO
4852 towernum, if it's zero). This is an extreme niche case.
4854 =item 477part, 477rownum, date
4856 Limit to packages included in a specific row of one of the FCC 477 reports.
4857 '477part' is the section name (see L<FS::Report::FCC_477> methods), 'date'
4858 is the report as-of date (completely unrelated to the package setup/bill/
4859 other date fields), and '477rownum' is the row number of the report starting
4860 with zero. Row numbers have no inherent meaning, so this is useful only
4861 for explaining a 477 report you've already run.
4868 my ($class, $params) = @_;
4875 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
4877 "cust_main.agentnum = $1";
4884 if ( $params->{'cust_status'} =~ /^([a-z]+)$/ ) {
4885 push @where, FS::cust_main->cust_status_sql . " = '$1' ";
4889 # parse customer sales person
4892 if ( $params->{'cust_main_salesnum'} =~ /^(\d+)$/ ) {
4893 push @where, ($1 > 0) ? "cust_main.salesnum = $1"
4894 : 'cust_main.salesnum IS NULL';
4899 # parse sales person
4902 if ( $params->{'salesnum'} =~ /^(\d+)$/ ) {
4903 push @where, ($1 > 0) ? "cust_pkg.salesnum = $1"
4904 : 'cust_pkg.salesnum IS NULL';
4911 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
4913 "cust_pkg.custnum = $1";
4920 if ( $params->{'pkgbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
4922 "cust_pkg.pkgbatch = '$1'";
4929 if ( $params->{'magic'} eq 'active'
4930 || $params->{'status'} eq 'active' ) {
4932 push @where, FS::cust_pkg->active_sql();
4934 } elsif ( $params->{'magic'} =~ /^not[ _]yet[ _]billed$/
4935 || $params->{'status'} =~ /^not[ _]yet[ _]billed$/ ) {
4937 push @where, FS::cust_pkg->not_yet_billed_sql();
4939 } elsif ( $params->{'magic'} =~ /^(one-time charge|inactive)/
4940 || $params->{'status'} =~ /^(one-time charge|inactive)/ ) {
4942 push @where, FS::cust_pkg->inactive_sql();
4944 } elsif ( $params->{'magic'} =~ /^on[ _]hold$/
4945 || $params->{'status'} =~ /^on[ _]hold$/ ) {
4947 push @where, FS::cust_pkg->on_hold_sql();
4950 } elsif ( $params->{'magic'} eq 'suspended'
4951 || $params->{'status'} eq 'suspended' ) {
4953 push @where, FS::cust_pkg->suspended_sql();
4955 } elsif ( $params->{'magic'} =~ /^cancell?ed$/
4956 || $params->{'status'} =~ /^cancell?ed$/ ) {
4958 push @where, FS::cust_pkg->cancelled_sql();
4962 ### special case: "magic" is used in detail links from browse/part_pkg,
4963 # where "cancelled" has the restriction "and not replaced with a package
4964 # of the same pkgpart". Be consistent with that.
4967 if ( $params->{'magic'} =~ /^cancell?ed$/ ) {
4968 my $new_pkgpart = "SELECT pkgpart FROM cust_pkg AS cust_pkg_next ".
4969 "WHERE cust_pkg_next.change_pkgnum = cust_pkg.pkgnum";
4970 # ...may not exist, if this was just canceled and not changed; in that
4971 # case give it a "new pkgpart" that never equals the old pkgpart
4972 push @where, "COALESCE(($new_pkgpart), 0) != cust_pkg.pkgpart";
4976 # parse package class
4979 if ( exists($params->{'classnum'}) ) {
4982 if ( ref($params->{'classnum'}) ) {
4984 if ( ref($params->{'classnum'}) eq 'HASH' ) {
4985 @classnum = grep $params->{'classnum'}{$_}, keys %{ $params->{'classnum'} };
4986 } elsif ( ref($params->{'classnum'}) eq 'ARRAY' ) {
4987 @classnum = @{ $params->{'classnum'} };
4989 die 'unhandled classnum ref '. $params->{'classnum'};
4993 } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
5000 my @nums = grep $_, @classnum;
5001 push @c_where, 'part_pkg.classnum IN ('. join(',',@nums). ')' if @nums;
5002 my $null = scalar( grep { $_ eq '' } @classnum );
5003 push @c_where, 'part_pkg.classnum IS NULL' if $null;
5005 if ( scalar(@c_where) == 1 ) {
5006 push @where, @c_where;
5007 } elsif ( @c_where ) {
5008 push @where, ' ( '. join(' OR ', @c_where). ' ) ';
5017 # parse refnum (advertising source)
5020 if ( exists($params->{'refnum'}) ) {
5022 if (ref $params->{'refnum'}) {
5023 @refnum = @{ $params->{'refnum'} };
5025 @refnum = ( $params->{'refnum'} );
5027 my $in = join(',', grep /^\d+$/, @refnum);
5028 push @where, "refnum IN($in)" if length $in;
5032 # parse package report options
5035 my @report_option = ();
5036 if ( exists($params->{'report_option'}) ) {
5037 if ( ref($params->{'report_option'}) eq 'ARRAY' ) {
5038 @report_option = @{ $params->{'report_option'} };
5039 } elsif ( $params->{'report_option'} =~ /^([,\d]*)$/ ) {
5040 @report_option = split(',', $1);
5045 if (@report_option) {
5046 # this will result in the empty set for the dangling comma case as it should
5048 map{ "0 < ( SELECT count(*) FROM part_pkg_option
5049 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
5050 AND optionname = 'report_option_$_'
5051 AND optionvalue = '1' )"
5055 foreach my $any ( grep /^report_option_any/, keys %$params ) {
5057 my @report_option_any = ();
5058 if ( ref($params->{$any}) eq 'ARRAY' ) {
5059 @report_option_any = @{ $params->{$any} };
5060 } elsif ( $params->{$any} =~ /^([,\d]*)$/ ) {
5061 @report_option_any = split(',', $1);
5064 if (@report_option_any) {
5065 # this will result in the empty set for the dangling comma case as it should
5066 push @where, ' ( '. join(' OR ',
5067 map{ "0 < ( SELECT count(*) FROM part_pkg_option
5068 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
5069 AND optionname = 'report_option_$_'
5070 AND optionvalue = '1' )"
5071 } @report_option_any
5081 push @where, "part_pkg.custom = 'Y'" if $params->{custom};
5087 push @where, "(part_pkg.fcc_ds0s > 0 OR pkg_class.fcc_ds0s > 0)"
5088 if $params->{fcc_line};
5094 if ( exists($params->{'censustract'}) ) {
5095 $params->{'censustract'} =~ /^([.\d]*)$/;
5096 my $censustract = "cust_location.censustract = '$1'";
5097 $censustract .= ' OR cust_location.censustract is NULL' unless $1;
5098 push @where, "( $censustract )";
5102 # parse censustract2
5104 if ( exists($params->{'censustract2'})
5105 && $params->{'censustract2'} =~ /^(\d*)$/
5109 push @where, "cust_location.censustract LIKE '$1%'";
5112 "( cust_location.censustract = '' OR cust_location.censustract IS NULL )";
5117 # parse country/state/zip
5119 for (qw(state country)) { # parsing rules are the same for these
5120 if ( exists($params->{$_})
5121 && uc($params->{$_}) =~ /^([A-Z]{2})$/ )
5123 # XXX post-2.3 only--before that, state/country may be in cust_main
5124 push @where, "cust_location.$_ = '$1'";
5127 if ( exists($params->{zip}) ) {
5128 push @where, "cust_location.zip = " . dbh->quote($params->{zip});
5134 if ( $params->{location_cust} xor $params->{location_nocust} ) {
5135 my $op = $params->{location_cust} ? '=' : '!=';
5136 push @where, "cust_location.locationnum $op cust_main.ship_locationnum";
5138 if ( $params->{location_census} xor $params->{location_nocensus} ) {
5139 my $op = $params->{location_census} ? "IS NOT NULL" : "IS NULL";
5140 push @where, "cust_location.censustract $op";
5142 if ( $params->{location_geocode} xor $params->{location_nogeocode} ) {
5143 my $op = $params->{location_geocode} ? "IS NOT NULL" : "IS NULL";
5144 push @where, "cust_location.geocode $op";
5151 if ( ref($params->{'pkgpart'}) ) {
5154 if ( ref($params->{'pkgpart'}) eq 'HASH' ) {
5155 @pkgpart = grep $params->{'pkgpart'}{$_}, keys %{ $params->{'pkgpart'} };
5156 } elsif ( ref($params->{'pkgpart'}) eq 'ARRAY' ) {
5157 @pkgpart = @{ $params->{'pkgpart'} };
5159 die 'unhandled pkgpart ref '. $params->{'pkgpart'};
5162 @pkgpart = grep /^(\d+)$/, @pkgpart;
5164 push @where, 'pkgpart IN ('. join(',', @pkgpart). ')' if scalar(@pkgpart);
5166 } elsif ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
5167 push @where, "pkgpart = $1";
5176 #false laziness w/report_cust_pkg.html
5179 'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
5180 'active' => { 'susp'=>1, 'cancel'=>1 },
5181 'suspended' => { 'cancel' => 1 },
5186 if( exists($params->{'active'} ) ) {
5187 # This overrides all the other date-related fields, and includes packages
5188 # that were active at some time during the interval. It excludes:
5189 # - packages that were set up after the end of the interval
5190 # - packages that were canceled before the start of the interval
5191 # - packages that were suspended before the start of the interval
5192 # and are still suspended now
5193 my($beginning, $ending) = @{$params->{'active'}};
5195 "cust_pkg.setup IS NOT NULL",
5196 "cust_pkg.setup <= $ending",
5197 "(cust_pkg.cancel IS NULL OR cust_pkg.cancel >= $beginning )",
5198 "(cust_pkg.susp IS NULL OR cust_pkg.susp >= $beginning )",
5199 "NOT (".FS::cust_pkg->onetime_sql . ")";
5202 my $exclude_change_from = 0;
5203 my $exclude_change_to = 0;
5205 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel )) {
5207 next unless exists($params->{$field});
5209 my($beginning, $ending) = @{$params->{$field}};
5211 next if $beginning == 0 && $ending == 4294967295;
5214 "cust_pkg.$field IS NOT NULL",
5215 "cust_pkg.$field >= $beginning",
5216 "cust_pkg.$field <= $ending";
5218 $orderby ||= "ORDER BY cust_pkg.$field";
5220 if ( $field eq 'setup' ) {
5221 $exclude_change_from = 1;
5222 } elsif ( $field eq 'cancel' ) {
5223 $exclude_change_to = 1;
5224 } elsif ( $field eq 'change_date' ) {
5225 # if we are given setup and change_date ranges, and the setup date
5226 # falls in _both_ ranges, then include the package whether it was
5228 $exclude_change_from = 0;
5232 if ($exclude_change_from) {
5233 push @where, "change_pkgnum IS NULL";
5235 if ($exclude_change_to) {
5236 # a join might be more efficient here
5237 push @where, "NOT EXISTS(
5238 SELECT 1 FROM cust_pkg AS changed_to_pkg
5239 WHERE cust_pkg.pkgnum = changed_to_pkg.change_pkgnum
5244 $orderby ||= 'ORDER BY bill';
5247 # parse magic, legacy, etc.
5250 if ( $params->{'magic'} &&
5251 $params->{'magic'} =~ /^(active|inactive|suspended|cancell?ed)$/
5254 $orderby = 'ORDER BY pkgnum';
5256 if ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
5257 push @where, "pkgpart = $1";
5260 } elsif ( $params->{'query'} eq 'pkgnum' ) {
5262 $orderby = 'ORDER BY pkgnum';
5264 } elsif ( $params->{'query'} eq 'APKG_pkgnum' ) {
5266 $orderby = 'ORDER BY pkgnum';
5269 SELECT count(*) FROM pkg_svc
5270 WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
5271 AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
5272 WHERE cust_svc.pkgnum = cust_pkg.pkgnum
5273 AND cust_svc.svcpart = pkg_svc.svcpart
5280 # parse the extremely weird 'towernum' param
5283 if ($params->{towernum}) {
5284 my $towernum = $params->{towernum};
5285 $towernum = [ $towernum ] if !ref($towernum);
5286 my $in = join(',', grep /^\d+$/, @$towernum);
5288 # inefficient, but this is an obscure feature
5289 eval "use FS::Report::Table";
5290 FS::Report::Table->_init_tower_pkg_cache; # probably does nothing
5291 push @where, "EXISTS(
5292 SELECT 1 FROM tower_pkg_cache
5293 WHERE tower_pkg_cache.pkgnum = cust_pkg.pkgnum
5294 AND tower_pkg_cache.towernum IN ($in)
5300 # parse the 477 report drill-down options
5303 if ($params->{'477part'} =~ /^([a-z]+)$/) {
5305 my ($date, $rownum, $agentnum);
5306 if ($params->{'date'} =~ /^(\d+)$/) {
5309 if ($params->{'477rownum'} =~ /^(\d+)$/) {
5312 if ($params->{'agentnum'} =~ /^(\d+)$/) {
5315 if ($date and defined($rownum)) {
5316 my $report = FS::Report::FCC_477->report($section,
5318 'agentnum' => $agentnum,
5321 my $pkgnums = $report->{detail}->[$rownum]
5322 or die "row $rownum is past the end of the report";
5323 # '0' so that if there are no pkgnums (empty string) it will create
5324 # a valid query that returns nothing
5325 warn "PKGNUMS:\n$pkgnums\n\n"; # XXX debug
5327 # and this overrides everything
5328 @where = ( "cust_pkg.pkgnum IN($pkgnums)" );
5329 } # else we're missing some params, ignore the whole business
5333 # setup queries, links, subs, etc. for the search
5336 # here is the agent virtualization
5337 if ($params->{CurrentUser}) {
5339 qsearchs('access_user', { username => $params->{CurrentUser} });
5342 push @where, $access_user->agentnums_sql('table'=>'cust_main');
5347 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql('table'=>'cust_main');
5350 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
5352 my $addl_from = 'LEFT JOIN part_pkg USING ( pkgpart ) '.
5353 'LEFT JOIN pkg_class ON ( part_pkg.classnum = pkg_class.classnum ) '.
5354 'LEFT JOIN cust_location USING ( locationnum ) '.
5355 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
5359 if ( $params->{'select_zip5'} ) {
5360 my $zip = 'cust_location.zip';
5362 $select = "DISTINCT substr($zip,1,5) as zip";
5363 $orderby = "ORDER BY substr($zip,1,5)";
5364 $count_query = "SELECT COUNT( DISTINCT substr($zip,1,5) )";
5366 $select = join(', ',
5368 ( map "part_pkg.$_", qw( pkg freq ) ),
5369 'pkg_class.classname',
5370 'cust_main.custnum AS cust_main_custnum',
5371 FS::UI::Web::cust_sql_fields(
5372 $params->{'cust_fields'}
5375 $count_query = 'SELECT COUNT(*)';
5378 $count_query .= " FROM cust_pkg $addl_from $extra_sql";
5381 'table' => 'cust_pkg',
5383 'select' => $select,
5384 'extra_sql' => $extra_sql,
5385 'order_by' => $orderby,
5386 'addl_from' => $addl_from,
5387 'count_query' => $count_query,
5394 Returns a list of two package counts. The first is a count of packages
5395 based on the supplied criteria and the second is the count of residential
5396 packages with those same criteria. Criteria are specified as in the search
5402 my ($class, $params) = @_;
5404 my $sql_query = $class->search( $params );
5406 my $count_sql = delete($sql_query->{'count_query'});
5407 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
5408 or die "couldn't parse count_sql";
5410 my $count_sth = dbh->prepare($count_sql)
5411 or die "Error preparing $count_sql: ". dbh->errstr;
5413 or die "Error executing $count_sql: ". $count_sth->errstr;
5414 my $count_arrayref = $count_sth->fetchrow_arrayref;
5416 return ( @$count_arrayref );
5420 =item tax_locationnum_sql
5422 Returns an SQL expression for the tax location for a package, based
5423 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
5427 sub tax_locationnum_sql {
5428 my $conf = FS::Conf->new;
5429 if ( $conf->exists('tax-pkg_address') ) {
5430 'cust_pkg.locationnum';
5432 elsif ( $conf->exists('tax-ship_address') ) {
5433 'cust_main.ship_locationnum';
5436 'cust_main.bill_locationnum';
5442 Returns a list: the first item is an SQL fragment identifying matching
5443 packages/customers via location (taking into account shipping and package
5444 address taxation, if enabled), and subsequent items are the parameters to
5445 substitute for the placeholders in that fragment.
5450 my($class, %opt) = @_;
5451 my $ornull = $opt{'ornull'};
5453 my $conf = new FS::Conf;
5455 # '?' placeholders in _location_sql_where
5456 my $x = $ornull ? 3 : 2;
5467 if ( $conf->exists('tax-ship_address') ) {
5470 ( ( ship_last IS NULL OR ship_last = '' )
5471 AND ". _location_sql_where('cust_main', '', $ornull ). "
5473 OR ( ship_last IS NOT NULL AND ship_last != ''
5474 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
5477 # AND payby != 'COMP'
5479 @main_param = ( @bill_param, @bill_param );
5483 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
5484 @main_param = @bill_param;
5490 if ( $conf->exists('tax-pkg_address') ) {
5492 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
5495 ( cust_pkg.locationnum IS NULL AND $main_where )
5496 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
5499 @param = ( @main_param, @bill_param );
5503 $where = $main_where;
5504 @param = @main_param;
5512 #subroutine, helper for location_sql
5513 sub _location_sql_where {
5515 my $prefix = @_ ? shift : '';
5516 my $ornull = @_ ? shift : '';
5518 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
5520 $ornull = $ornull ? ' OR ? IS NULL ' : '';
5522 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
5523 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
5524 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
5526 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
5528 # ( $table.${prefix}city = ? $or_empty_city $ornull )
5530 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
5531 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
5532 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
5533 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
5534 AND $table.${prefix}country = ?
5539 my( $self, $what ) = @_;
5541 my $what_show_zero = $what. '_show_zero';
5542 length($self->$what_show_zero())
5543 ? ($self->$what_show_zero() eq 'Y')
5544 : $self->part_pkg->$what_show_zero();
5551 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
5553 Bulk cancel + order subroutine. Perhaps slightly deprecated, only used by the
5554 bulk cancel+order in the web UI and nowhere else (edit/process/cust_pkg.cgi)
5556 CUSTNUM is a customer (see L<FS::cust_main>)
5558 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
5559 L<FS::part_pkg>) to order for this customer. Duplicates are of course
5562 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
5563 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
5564 new billing items. An error is returned if this is not possible (see
5565 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
5568 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
5569 newly-created cust_pkg objects.
5571 REFNUM, if specified, will specify the FS::pkg_referral record to be created
5572 and inserted. Multiple FS::pkg_referral records can be created by
5573 setting I<refnum> to an array reference of refnums or a hash reference with
5574 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
5575 record will be created corresponding to cust_main.refnum.
5580 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
5582 my $conf = new FS::Conf;
5584 # Transactionize this whole mess
5585 local $SIG{HUP} = 'IGNORE';
5586 local $SIG{INT} = 'IGNORE';
5587 local $SIG{QUIT} = 'IGNORE';
5588 local $SIG{TERM} = 'IGNORE';
5589 local $SIG{TSTP} = 'IGNORE';
5590 local $SIG{PIPE} = 'IGNORE';
5592 my $oldAutoCommit = $FS::UID::AutoCommit;
5593 local $FS::UID::AutoCommit = 0;
5597 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
5598 # return "Customer not found: $custnum" unless $cust_main;
5600 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
5603 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
5606 my $change = scalar(@old_cust_pkg) != 0;
5609 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
5611 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
5612 " to pkgpart ". $pkgparts->[0]. "\n"
5615 my $err_or_cust_pkg =
5616 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
5617 'refnum' => $refnum,
5620 unless (ref($err_or_cust_pkg)) {
5621 $dbh->rollback if $oldAutoCommit;
5622 return $err_or_cust_pkg;
5625 push @$return_cust_pkg, $err_or_cust_pkg;
5626 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5631 # Create the new packages.
5632 foreach my $pkgpart (@$pkgparts) {
5634 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
5636 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
5637 pkgpart => $pkgpart,
5641 $error = $cust_pkg->insert( 'change' => $change );
5642 push @$return_cust_pkg, $cust_pkg;
5644 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
5645 my $supp_pkg = FS::cust_pkg->new({
5646 custnum => $custnum,
5647 pkgpart => $link->dst_pkgpart,
5649 main_pkgnum => $cust_pkg->pkgnum,
5652 $error ||= $supp_pkg->insert( 'change' => $change );
5653 push @$return_cust_pkg, $supp_pkg;
5657 $dbh->rollback if $oldAutoCommit;
5662 # $return_cust_pkg now contains refs to all of the newly
5665 # Transfer services and cancel old packages.
5666 foreach my $old_pkg (@old_cust_pkg) {
5668 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
5671 foreach my $new_pkg (@$return_cust_pkg) {
5672 $error = $old_pkg->transfer($new_pkg);
5673 if ($error and $error == 0) {
5674 # $old_pkg->transfer failed.
5675 $dbh->rollback if $oldAutoCommit;
5680 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
5681 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
5682 foreach my $new_pkg (@$return_cust_pkg) {
5683 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
5684 if ($error and $error == 0) {
5685 # $old_pkg->transfer failed.
5686 $dbh->rollback if $oldAutoCommit;
5693 # Transfers were successful, but we went through all of the
5694 # new packages and still had services left on the old package.
5695 # We can't cancel the package under the circumstances, so abort.
5696 $dbh->rollback if $oldAutoCommit;
5697 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
5699 $error = $old_pkg->cancel( quiet=>1, 'no_delay_cancel'=>1 );
5705 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5709 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
5711 A bulk change method to change packages for multiple customers.
5713 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
5714 L<FS::part_pkg>) to order for each customer. Duplicates are of course
5717 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
5718 replace. The services (see L<FS::cust_svc>) are moved to the
5719 new billing items. An error is returned if this is not possible (see
5722 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
5723 newly-created cust_pkg objects.
5728 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
5730 # Transactionize this whole mess
5731 local $SIG{HUP} = 'IGNORE';
5732 local $SIG{INT} = 'IGNORE';
5733 local $SIG{QUIT} = 'IGNORE';
5734 local $SIG{TERM} = 'IGNORE';
5735 local $SIG{TSTP} = 'IGNORE';
5736 local $SIG{PIPE} = 'IGNORE';
5738 my $oldAutoCommit = $FS::UID::AutoCommit;
5739 local $FS::UID::AutoCommit = 0;
5743 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
5746 while(scalar(@old_cust_pkg)) {
5748 my $custnum = $old_cust_pkg[0]->custnum;
5749 my (@remove) = map { $_->pkgnum }
5750 grep { $_->custnum == $custnum } @old_cust_pkg;
5751 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
5753 my $error = order $custnum, $pkgparts, \@remove, \@return;
5755 push @errors, $error
5757 push @$return_cust_pkg, @return;
5760 if (scalar(@errors)) {
5761 $dbh->rollback if $oldAutoCommit;
5762 return join(' / ', @errors);
5765 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5769 =item forward_emails
5771 Returns a hash of svcnums and corresponding email addresses
5772 for svc_acct services that can be used as source or dest
5773 for svc_forward services provisioned in this package.
5775 Accepts options I<svc_forward> OR I<svcnum> for a svc_forward
5776 service; if included, will ensure the current values of the
5777 specified service are included in the list, even if for some
5778 other reason they wouldn't be. If called as a class method
5779 with a specified service, returns only these current values.
5781 Caution: does not actually check if svc_forward services are
5782 available to be provisioned on this package.
5786 sub forward_emails {
5790 #load optional service, thoroughly validated
5791 die "Use svcnum or svc_forward, not both"
5792 if $opt{'svcnum'} && $opt{'svc_forward'};
5793 my $svc_forward = $opt{'svc_forward'};
5794 $svc_forward ||= qsearchs('svc_forward',{ 'svcnum' => $opt{'svcnum'} })
5796 die "Specified service is not a forward service"
5797 if $svc_forward && (ref($svc_forward) ne 'FS::svc_forward');
5798 die "Specified service not found"
5799 if ($opt{'svcnum'} || $opt{'svc_forward'}) && !$svc_forward;
5803 ## everything below was basically copied from httemplate/edit/svc_forward.cgi
5804 ## with minimal refactoring, not sure why we can't just load all svc_accts for this custnum
5806 #add current values from specified service, if there was one
5808 foreach my $method (qw( srcsvc_acct dstsvc_acct )) {
5809 my $svc_acct = $svc_forward->$method();
5810 $email{$svc_acct->svcnum} = $svc_acct->email if $svc_acct;
5814 if (ref($self) eq 'FS::cust_pkg') {
5816 #and including the rest for this customer
5817 my($u_part_svc,@u_acct_svcparts);
5818 foreach $u_part_svc ( qsearch('part_svc',{'svcdb'=>'svc_acct'}) ) {
5819 push @u_acct_svcparts,$u_part_svc->getfield('svcpart');
5822 my $custnum = $self->getfield('custnum');
5823 foreach my $i_cust_pkg ( qsearch('cust_pkg',{'custnum'=>$custnum}) ) {
5824 my $cust_pkgnum = $i_cust_pkg->getfield('pkgnum');
5825 #now find the corresponding record(s) in cust_svc (for this pkgnum!)
5826 foreach my $acct_svcpart (@u_acct_svcparts) {
5827 foreach my $i_cust_svc (
5828 qsearch( 'cust_svc', { 'pkgnum' => $cust_pkgnum,
5829 'svcpart' => $acct_svcpart } )
5831 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $i_cust_svc->svcnum } );
5832 $email{$svc_acct->svcnum} = $svc_acct->email;
5841 # Used by FS::Upgrade to migrate to a new database.
5842 sub _upgrade_data { # class method
5843 my ($class, %opts) = @_;
5844 $class->_upgrade_otaker(%opts);
5846 # RT#10139, bug resulting in contract_end being set when it shouldn't
5847 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
5848 # RT#10830, bad calculation of prorate date near end of year
5849 # the date range for bill is December 2009, and we move it forward
5850 # one year if it's before the previous bill date (which it should
5852 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
5853 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
5854 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
5855 # RT6628, add order_date to cust_pkg
5856 'update cust_pkg set order_date = (select history_date from h_cust_pkg
5857 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
5858 history_action = \'insert\') where order_date is null',
5860 foreach my $sql (@statements) {
5861 my $sth = dbh->prepare($sql);
5862 $sth->execute or die $sth->errstr;
5865 # RT31194: supplemental package links that are deleted don't clean up
5867 my @pkglinknums = qsearch({
5868 'select' => 'DISTINCT cust_pkg.pkglinknum',
5869 'table' => 'cust_pkg',
5870 'addl_from' => ' LEFT JOIN part_pkg_link USING (pkglinknum) ',
5871 'extra_sql' => ' WHERE cust_pkg.pkglinknum IS NOT NULL
5872 AND part_pkg_link.pkglinknum IS NULL',
5874 foreach (@pkglinknums) {
5875 my $pkglinknum = $_->pkglinknum;
5876 warn "cleaning part_pkg_link #$pkglinknum\n";
5877 my $part_pkg_link = FS::part_pkg_link->new({pkglinknum => $pkglinknum});
5878 my $error = $part_pkg_link->remove_linked;
5879 die $error if $error;
5887 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
5889 In sub order, the @pkgparts array (passed by reference) is clobbered.
5891 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
5892 method to pass dates to the recur_prog expression, it should do so.
5894 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
5895 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
5896 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
5897 configuration values. Probably need a subroutine which decides what to do
5898 based on whether or not we've fetched the user yet, rather than a hash. See
5899 FS::UID and the TODO.
5901 Now that things are transactional should the check in the insert method be
5906 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
5907 L<FS::pkg_svc>, schema.html from the base documentation