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
61 my ( $hashref, $cache ) = @_;
62 #if ( $hashref->{'pkgpart'} ) {
63 if ( $hashref->{'pkg'} ) {
64 # #@{ $self->{'_pkgnum'} } = ();
65 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
66 # $self->{'_pkgpart'} = $subcache;
67 # #push @{ $self->{'_pkgnum'} },
68 # FS::part_pkg->new_or_cached($hashref, $subcache);
69 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
71 if ( exists $hashref->{'svcnum'} ) {
72 #@{ $self->{'_pkgnum'} } = ();
73 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
74 $self->{'_svcnum'} = $subcache;
75 #push @{ $self->{'_pkgnum'} },
76 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
82 FS::cust_pkg - Object methods for cust_pkg objects
88 $record = new FS::cust_pkg \%hash;
89 $record = new FS::cust_pkg { 'column' => 'value' };
91 $error = $record->insert;
93 $error = $new_record->replace($old_record);
95 $error = $record->delete;
97 $error = $record->check;
99 $error = $record->cancel;
101 $error = $record->suspend;
103 $error = $record->unsuspend;
105 $part_pkg = $record->part_pkg;
107 @labels = $record->labels;
109 $seconds = $record->seconds_since($timestamp);
111 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
112 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
116 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
117 inherits from FS::Record. The following fields are currently supported:
123 Primary key (assigned automatically for new billing items)
127 Customer (see L<FS::cust_main>)
131 Billing item definition (see L<FS::part_pkg>)
135 Optional link to package location (see L<FS::location>)
139 date package was ordered (also remains same on changes)
151 date (next bill date)
179 order taker (see L<FS::access_user>)
183 If this field is set to 1, disables the automatic
184 unsuspension of this package when using the B<unsuspendauto> config option.
188 If not set, defaults to 1
192 Date of change from previous package
202 =item change_locationnum
210 The pkgnum of the package that this package is supplemental to, if any.
214 The package link (L<FS::part_pkg_link>) that defines this supplemental
215 package, if it is one.
217 =item change_to_pkgnum
219 The pkgnum of the package this one will be "changed to" in the future
220 (on its expiration date).
224 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
225 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
226 L<Time::Local> and L<Date::Parse> for conversion functions.
234 Create a new billing item. To add the item to the database, see L<"insert">.
238 sub table { 'cust_pkg'; }
239 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
240 sub cust_unlinked_msg {
242 "WARNING: can't find cust_main.custnum ". $self->custnum.
243 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
246 =item insert [ OPTION => VALUE ... ]
248 Adds this billing item to the database ("Orders" the item). If there is an
249 error, returns the error, otherwise returns false.
251 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
252 will be used to look up the package definition and agent restrictions will be
255 If the additional field I<refnum> is defined, an FS::pkg_referral record will
256 be created and inserted. Multiple FS::pkg_referral records can be created by
257 setting I<refnum> to an array reference of refnums or a hash reference with
258 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
259 record will be created corresponding to cust_main.refnum.
261 The following options are available:
267 If set true, supresses actions that should only be taken for new package
268 orders. (Currently this includes: intro periods when delay_setup is on.)
272 cust_pkg_option records will be created
276 a ticket will be added to this customer with this subject
280 an optional queue name for ticket additions
284 Don't check the legality of the package definition. This should be used
285 when performing a package change that doesn't change the pkgpart (i.e.
293 my( $self, %options ) = @_;
296 $error = $self->check_pkgpart unless $options{'allow_pkgpart'};
297 return $error if $error;
299 my $part_pkg = $self->part_pkg;
301 # if the package def says to start only on the first of the month:
302 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
303 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
304 $mon += 1 unless $mday == 1;
305 until ( $mon < 12 ) { $mon -= 12; $year++; }
306 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
309 # set up any automatic expire/adjourn/contract_end timers
310 # based on the start date
311 foreach my $action ( qw(expire adjourn contract_end) ) {
312 my $months = $part_pkg->option("${action}_months",1);
313 if($months and !$self->$action) {
314 my $start = $self->start_date || $self->setup || time;
315 $self->$action( $part_pkg->add_freq($start, $months) );
319 # if this package has "free days" and delayed setup fee, tehn
320 # set start date that many days in the future.
321 # (this should have been set in the UI, but enforce it here)
322 if ( ! $options{'change'}
323 && ( my $free_days = $part_pkg->option('free_days',1) )
324 && $part_pkg->option('delay_setup',1)
325 #&& ! $self->start_date
328 $self->start_date( $part_pkg->default_start_date );
331 $self->order_date(time);
333 local $SIG{HUP} = 'IGNORE';
334 local $SIG{INT} = 'IGNORE';
335 local $SIG{QUIT} = 'IGNORE';
336 local $SIG{TERM} = 'IGNORE';
337 local $SIG{TSTP} = 'IGNORE';
338 local $SIG{PIPE} = 'IGNORE';
340 my $oldAutoCommit = $FS::UID::AutoCommit;
341 local $FS::UID::AutoCommit = 0;
344 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
346 $dbh->rollback if $oldAutoCommit;
350 $self->refnum($self->cust_main->refnum) unless $self->refnum;
351 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
352 $self->process_m2m( 'link_table' => 'pkg_referral',
353 'target_table' => 'part_referral',
354 'params' => $self->refnum,
357 if ( $self->discountnum ) {
358 my $error = $self->insert_discount();
360 $dbh->rollback if $oldAutoCommit;
365 my $conf = new FS::Conf;
367 if ( $conf->config('ticket_system') && $options{ticket_subject} ) {
369 #this init stuff is still inefficient, but at least its limited to
370 # the small number (any?) folks using ticket emailing on pkg order
373 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
380 use FS::TicketSystem;
381 FS::TicketSystem->init();
383 my $q = new RT::Queue($RT::SystemUser);
384 $q->Load($options{ticket_queue}) if $options{ticket_queue};
385 my $t = new RT::Ticket($RT::SystemUser);
386 my $mime = new MIME::Entity;
387 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
388 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
389 Subject => $options{ticket_subject},
392 $t->AddLink( Type => 'MemberOf',
393 Target => 'freeside://freeside/cust_main/'. $self->custnum,
397 if ($conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
398 my $queue = new FS::queue {
399 'job' => 'FS::cust_main::queueable_print',
401 $error = $queue->insert(
402 'custnum' => $self->custnum,
403 'template' => 'welcome_letter',
407 warn "can't send welcome letter: $error";
412 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
419 This method now works but you probably shouldn't use it.
421 You don't want to delete packages, because there would then be no record
422 the customer ever purchased the package. Instead, see the cancel method and
423 hide cancelled packages.
430 local $SIG{HUP} = 'IGNORE';
431 local $SIG{INT} = 'IGNORE';
432 local $SIG{QUIT} = 'IGNORE';
433 local $SIG{TERM} = 'IGNORE';
434 local $SIG{TSTP} = 'IGNORE';
435 local $SIG{PIPE} = 'IGNORE';
437 my $oldAutoCommit = $FS::UID::AutoCommit;
438 local $FS::UID::AutoCommit = 0;
441 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
442 my $error = $cust_pkg_discount->delete;
444 $dbh->rollback if $oldAutoCommit;
448 #cust_bill_pkg_discount?
450 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
451 my $error = $cust_pkg_detail->delete;
453 $dbh->rollback if $oldAutoCommit;
458 foreach my $cust_pkg_reason (
460 'table' => 'cust_pkg_reason',
461 'hashref' => { 'pkgnum' => $self->pkgnum },
465 my $error = $cust_pkg_reason->delete;
467 $dbh->rollback if $oldAutoCommit;
474 my $error = $self->SUPER::delete(@_);
476 $dbh->rollback if $oldAutoCommit;
480 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
486 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
488 Replaces the OLD_RECORD with this one in the database. If there is an error,
489 returns the error, otherwise returns false.
491 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
493 Changing pkgpart may have disasterous effects. See the order subroutine.
495 setup and bill are normally updated by calling the bill method of a customer
496 object (see L<FS::cust_main>).
498 suspend is normally updated by the suspend and unsuspend methods.
500 cancel is normally updated by the cancel method (and also the order subroutine
503 Available options are:
509 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.
513 the access_user (see L<FS::access_user>) providing the reason
517 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
526 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
531 ( ref($_[0]) eq 'HASH' )
535 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
536 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
539 #return "Can't change setup once it exists!"
540 # if $old->getfield('setup') &&
541 # $old->getfield('setup') != $new->getfield('setup');
543 #some logic for bill, susp, cancel?
545 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
547 local $SIG{HUP} = 'IGNORE';
548 local $SIG{INT} = 'IGNORE';
549 local $SIG{QUIT} = 'IGNORE';
550 local $SIG{TERM} = 'IGNORE';
551 local $SIG{TSTP} = 'IGNORE';
552 local $SIG{PIPE} = 'IGNORE';
554 my $oldAutoCommit = $FS::UID::AutoCommit;
555 local $FS::UID::AutoCommit = 0;
558 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
559 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
560 my $error = $new->insert_reason(
561 'reason' => $options->{'reason'},
562 'date' => $new->$method,
564 'reason_otaker' => $options->{'reason_otaker'},
567 dbh->rollback if $oldAutoCommit;
568 return "Error inserting cust_pkg_reason: $error";
573 #save off and freeze RADIUS attributes for any associated svc_acct records
575 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
577 #also check for specific exports?
578 # to avoid spurious modify export events
579 @svc_acct = map { $_->svc_x }
580 grep { $_->part_svc->svcdb eq 'svc_acct' }
583 $_->snapshot foreach @svc_acct;
587 my $error = $new->export_pkg_change($old)
588 || $new->SUPER::replace( $old,
590 ? $options->{options}
594 $dbh->rollback if $oldAutoCommit;
598 #for prepaid packages,
599 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
600 foreach my $old_svc_acct ( @svc_acct ) {
601 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
603 $new_svc_acct->replace( $old_svc_acct,
604 'depend_jobnum' => $options->{depend_jobnum},
607 $dbh->rollback if $oldAutoCommit;
612 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
619 Checks all fields to make sure this is a valid billing item. If there is an
620 error, returns the error, otherwise returns false. Called by the insert and
628 if ( !$self->locationnum or $self->locationnum == -1 ) {
629 $self->set('locationnum', $self->cust_main->ship_locationnum);
633 $self->ut_numbern('pkgnum')
634 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
635 || $self->ut_numbern('pkgpart')
636 || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' )
637 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
638 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
639 || $self->ut_numbern('quantity')
640 || $self->ut_numbern('start_date')
641 || $self->ut_numbern('setup')
642 || $self->ut_numbern('bill')
643 || $self->ut_numbern('susp')
644 || $self->ut_numbern('cancel')
645 || $self->ut_numbern('adjourn')
646 || $self->ut_numbern('resume')
647 || $self->ut_numbern('expire')
648 || $self->ut_numbern('dundate')
649 || $self->ut_enum('no_auto', [ '', 'Y' ])
650 || $self->ut_enum('waive_setup', [ '', 'Y' ])
651 || $self->ut_numbern('agent_pkgid')
652 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
653 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
654 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
655 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
656 || $self->ut_foreign_keyn('change_to_pkgnum', 'cust_pkg', 'pkgnum')
658 return $error if $error;
660 return "A package with both start date (future start) and setup date (already started) will never bill"
661 if $self->start_date && $self->setup && ! $upgrade;
663 return "A future unsuspend date can only be set for a package with a suspend date"
664 if $self->resume and !$self->susp and !$self->adjourn;
666 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
668 if ( $self->dbdef_table->column('manual_flag') ) {
669 $self->manual_flag('') if $self->manual_flag eq ' ';
670 $self->manual_flag =~ /^([01]?)$/
671 or return "Illegal manual_flag ". $self->manual_flag;
672 $self->manual_flag($1);
680 Check the pkgpart to make sure it's allowed with the reg_code and/or
681 promo_code of the package (if present) and with the customer's agent.
682 Called from C<insert>, unless we are doing a package change that doesn't
690 # my $error = $self->ut_numbern('pkgpart'); # already done
693 if ( $self->reg_code ) {
695 unless ( grep { $self->pkgpart == $_->pkgpart }
696 map { $_->reg_code_pkg }
697 qsearchs( 'reg_code', { 'code' => $self->reg_code,
698 'agentnum' => $self->cust_main->agentnum })
700 return "Unknown registration code";
703 } elsif ( $self->promo_code ) {
706 qsearchs('part_pkg', {
707 'pkgpart' => $self->pkgpart,
708 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
710 return 'Unknown promotional code' unless $promo_part_pkg;
714 unless ( $disable_agentcheck ) {
716 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
717 return "agent ". $agent->agentnum. ':'. $agent->agent.
718 " can't purchase pkgpart ". $self->pkgpart
719 unless $agent->pkgpart_hashref->{ $self->pkgpart }
720 || $agent->agentnum == $self->part_pkg->agentnum;
723 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
724 return $error if $error;
732 =item cancel [ OPTION => VALUE ... ]
734 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
735 in this package, then cancels the package itself (sets the cancel field to
738 Available options are:
742 =item quiet - can be set true to supress email cancellation notices.
744 =item time - can be set to cancel the package based on a specific future or
745 historical date. Using time ensures that the remaining amount is calculated
746 correctly. Note however that this is an immediate cancel and just changes
747 the date. You are PROBABLY looking to expire the account instead of using
750 =item reason - can be set to a cancellation reason (see L<FS:reason>),
751 either a reasonnum of an existing reason, or passing a hashref will create
752 a new reason. The hashref should have the following keys: typenum - Reason
753 type (see L<FS::reason_type>, reason - Text of the new reason.
755 =item date - can be set to a unix style timestamp to specify when to
758 =item nobill - can be set true to skip billing if it might otherwise be done.
760 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
761 not credit it. This must be set (by change()) when changing the package
762 to a different pkgpart or location, and probably shouldn't be in any other
763 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
768 If there is an error, returns the error, otherwise returns false.
773 my( $self, %options ) = @_;
776 # pass all suspend/cancel actions to the main package
777 if ( $self->main_pkgnum and !$options{'from_main'} ) {
778 return $self->main_pkg->cancel(%options);
781 my $conf = new FS::Conf;
783 warn "cust_pkg::cancel called with options".
784 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
787 local $SIG{HUP} = 'IGNORE';
788 local $SIG{INT} = 'IGNORE';
789 local $SIG{QUIT} = 'IGNORE';
790 local $SIG{TERM} = 'IGNORE';
791 local $SIG{TSTP} = 'IGNORE';
792 local $SIG{PIPE} = 'IGNORE';
794 my $oldAutoCommit = $FS::UID::AutoCommit;
795 local $FS::UID::AutoCommit = 0;
798 my $old = $self->select_for_update;
800 if ( $old->get('cancel') || $self->get('cancel') ) {
801 dbh->rollback if $oldAutoCommit;
802 return ""; # no error
805 # XXX possibly set cancel_time to the expire date?
806 my $cancel_time = $options{'time'} || time;
807 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
808 $date = '' if ($date && $date <= $cancel_time); # complain instead?
810 #race condition: usage could be ongoing until unprovisioned
811 #resolved by performing a change package instead (which unprovisions) and
813 if ( !$options{nobill} && !$date ) {
814 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
815 my $copy = $self->new({$self->hash});
817 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
819 'time' => $cancel_time );
820 warn "Error billing during cancel, custnum ".
821 #$self->cust_main->custnum. ": $error"
826 if ( $options{'reason'} ) {
827 $error = $self->insert_reason( 'reason' => $options{'reason'},
828 'action' => $date ? 'expire' : 'cancel',
829 'date' => $date ? $date : $cancel_time,
830 'reason_otaker' => $options{'reason_otaker'},
833 dbh->rollback if $oldAutoCommit;
834 return "Error inserting cust_pkg_reason: $error";
838 my %svc_cancel_opt = ();
839 $svc_cancel_opt{'date'} = $date if $date;
840 foreach my $cust_svc (
843 sort { $a->[1] <=> $b->[1] }
844 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
845 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
847 my $part_svc = $cust_svc->part_svc;
848 next if ( defined($part_svc) and $part_svc->preserve );
849 my $error = $cust_svc->cancel( %svc_cancel_opt );
852 $dbh->rollback if $oldAutoCommit;
853 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
859 # credit remaining time if appropriate
861 if ( exists($options{'unused_credit'}) ) {
862 $do_credit = $options{'unused_credit'};
865 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
868 my $error = $self->credit_remaining('cancel', $cancel_time);
870 $dbh->rollback if $oldAutoCommit;
877 my %hash = $self->hash;
879 $hash{'expire'} = $date;
881 $hash{'cancel'} = $cancel_time;
883 $hash{'change_custnum'} = $options{'change_custnum'};
885 my $new = new FS::cust_pkg ( \%hash );
886 $error = $new->replace( $self, options => { $self->options } );
887 if ( $self->change_to_pkgnum ) {
888 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
889 $error ||= $change_to->cancel || $change_to->delete;
892 $dbh->rollback if $oldAutoCommit;
896 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
897 $error = $supp_pkg->cancel(%options, 'from_main' => 1);
899 $dbh->rollback if $oldAutoCommit;
900 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
904 foreach my $usage ( $self->cust_pkg_usage ) {
905 $error = $usage->delete;
907 $dbh->rollback if $oldAutoCommit;
908 return "deleting usage pools: $error";
912 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
913 return '' if $date; #no errors
915 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
916 if ( !$options{'quiet'} &&
917 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
919 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
922 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
923 $error = $msg_template->send( 'cust_main' => $self->cust_main,
928 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
929 'to' => \@invoicing_list,
930 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
931 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
934 #should this do something on errors?
941 =item cancel_if_expired [ NOW_TIMESTAMP ]
943 Cancels this package if its expire date has been reached.
947 sub cancel_if_expired {
949 my $time = shift || time;
950 return '' unless $self->expire && $self->expire <= $time;
951 my $error = $self->cancel;
953 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
954 $self->custnum. ": $error";
961 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
962 locationnum, (other fields?). Attempts to re-provision cancelled services
963 using history information (errors at this stage are not fatal).
965 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
967 svc_fatal: service provisioning errors are fatal
969 svc_errors: pass an array reference, will be filled in with any provisioning errors
971 main_pkgnum: link the package as a supplemental package of this one. For
977 my( $self, %options ) = @_;
979 #in case you try do do $uncancel-date = $cust_pkg->uncacel
980 return '' unless $self->get('cancel');
982 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
983 return $self->main_pkg->uncancel(%options);
990 local $SIG{HUP} = 'IGNORE';
991 local $SIG{INT} = 'IGNORE';
992 local $SIG{QUIT} = 'IGNORE';
993 local $SIG{TERM} = 'IGNORE';
994 local $SIG{TSTP} = 'IGNORE';
995 local $SIG{PIPE} = 'IGNORE';
997 my $oldAutoCommit = $FS::UID::AutoCommit;
998 local $FS::UID::AutoCommit = 0;
1002 # insert the new package
1005 my $cust_pkg = new FS::cust_pkg {
1006 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
1007 bill => ( $options{'bill'} || $self->get('bill') ),
1009 uncancel_pkgnum => $self->pkgnum,
1010 main_pkgnum => ($options{'main_pkgnum'} || ''),
1011 map { $_ => $self->get($_) } qw(
1012 custnum pkgpart locationnum
1014 susp adjourn resume expire start_date contract_end dundate
1015 change_date change_pkgpart change_locationnum
1016 manual_flag no_auto quantity agent_pkgid recur_show_zero setup_show_zero
1020 my $error = $cust_pkg->insert(
1021 'change' => 1, #supresses any referral credit to a referring customer
1022 'allow_pkgpart' => 1, # allow this even if the package def is disabled
1025 $dbh->rollback if $oldAutoCommit;
1033 #find historical services within this timeframe before the package cancel
1034 # (incompatible with "time" option to cust_pkg->cancel?)
1035 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
1036 # too little? (unprovisioing export delay?)
1037 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
1038 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1041 foreach my $h_cust_svc (@h_cust_svc) {
1042 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1043 #next unless $h_svc_x; #should this happen?
1044 (my $table = $h_svc_x->table) =~ s/^h_//;
1045 require "FS/$table.pm";
1046 my $class = "FS::$table";
1047 my $svc_x = $class->new( {
1048 'pkgnum' => $cust_pkg->pkgnum,
1049 'svcpart' => $h_cust_svc->svcpart,
1050 map { $_ => $h_svc_x->get($_) } fields($table)
1054 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1055 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1058 my $svc_error = $svc_x->insert;
1060 if ( $options{svc_fatal} ) {
1061 $dbh->rollback if $oldAutoCommit;
1064 # if we've failed to insert the svc_x object, svc_Common->insert
1065 # will have removed the cust_svc already. if not, then both records
1066 # were inserted but we failed for some other reason (export, most
1067 # likely). in that case, report the error and delete the records.
1068 push @svc_errors, $svc_error;
1069 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1071 # except if export_insert failed, export_delete probably won't be
1073 local $FS::svc_Common::noexport_hack = 1;
1074 my $cleanup_error = $svc_x->delete; # also deletes cust_svc
1075 if ( $cleanup_error ) { # and if THAT fails, then run away
1076 $dbh->rollback if $oldAutoCommit;
1077 return $cleanup_error;
1082 } #foreach $h_cust_svc
1084 #these are pretty rare, but should handle them
1085 # - dsl_device (mac addresses)
1086 # - phone_device (mac addresses)
1087 # - dsl_note (ikano notes)
1088 # - domain_record (i.e. restore DNS information w/domains)
1089 # - inventory_item(?) (inventory w/un-cancelling service?)
1090 # - nas (svc_broaband nas stuff)
1091 #this stuff is unused in the wild afaik
1092 # - mailinglistmember
1094 # - svc_domain.parent_svcnum?
1095 # - acct_snarf (ancient mail fetching config)
1096 # - cgp_rule (communigate)
1097 # - cust_svc_option (used by our Tron stuff)
1098 # - acct_rt_transaction (used by our time worked stuff)
1101 # also move over any services that didn't unprovision at cancellation
1104 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1105 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1106 my $error = $cust_svc->replace;
1108 $dbh->rollback if $oldAutoCommit;
1114 # Uncancel any supplemental packages, and make them supplemental to the
1118 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1120 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1122 $dbh->rollback if $oldAutoCommit;
1123 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1131 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1133 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1134 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1141 Cancels any pending expiration (sets the expire field to null).
1143 If there is an error, returns the error, otherwise returns false.
1148 my( $self, %options ) = @_;
1151 local $SIG{HUP} = 'IGNORE';
1152 local $SIG{INT} = 'IGNORE';
1153 local $SIG{QUIT} = 'IGNORE';
1154 local $SIG{TERM} = 'IGNORE';
1155 local $SIG{TSTP} = 'IGNORE';
1156 local $SIG{PIPE} = 'IGNORE';
1158 my $oldAutoCommit = $FS::UID::AutoCommit;
1159 local $FS::UID::AutoCommit = 0;
1162 my $old = $self->select_for_update;
1164 my $pkgnum = $old->pkgnum;
1165 if ( $old->get('cancel') || $self->get('cancel') ) {
1166 dbh->rollback if $oldAutoCommit;
1167 return "Can't unexpire cancelled package $pkgnum";
1168 # or at least it's pointless
1171 unless ( $old->get('expire') && $self->get('expire') ) {
1172 dbh->rollback if $oldAutoCommit;
1173 return ""; # no error
1176 my %hash = $self->hash;
1177 $hash{'expire'} = '';
1178 my $new = new FS::cust_pkg ( \%hash );
1179 $error = $new->replace( $self, options => { $self->options } );
1181 $dbh->rollback if $oldAutoCommit;
1185 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1191 =item suspend [ OPTION => VALUE ... ]
1193 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1194 package, then suspends the package itself (sets the susp field to now).
1196 Available options are:
1200 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1201 either a reasonnum of an existing reason, or passing a hashref will create
1202 a new reason. The hashref should have the following keys:
1203 - typenum - Reason type (see L<FS::reason_type>
1204 - reason - Text of the new reason.
1206 =item date - can be set to a unix style timestamp to specify when to
1209 =item time - can be set to override the current time, for calculation
1210 of final invoices or unused-time credits
1212 =item resume_date - can be set to a time when the package should be
1213 unsuspended. This may be more convenient than calling C<unsuspend()>
1216 =item from_main - allows a supplemental package to be suspended, rather
1217 than redirecting the method call to its main package. For internal use.
1221 If there is an error, returns the error, otherwise returns false.
1226 my( $self, %options ) = @_;
1229 # pass all suspend/cancel actions to the main package
1230 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1231 return $self->main_pkg->suspend(%options);
1234 local $SIG{HUP} = 'IGNORE';
1235 local $SIG{INT} = 'IGNORE';
1236 local $SIG{QUIT} = 'IGNORE';
1237 local $SIG{TERM} = 'IGNORE';
1238 local $SIG{TSTP} = 'IGNORE';
1239 local $SIG{PIPE} = 'IGNORE';
1241 my $oldAutoCommit = $FS::UID::AutoCommit;
1242 local $FS::UID::AutoCommit = 0;
1245 my $old = $self->select_for_update;
1247 my $pkgnum = $old->pkgnum;
1248 if ( $old->get('cancel') || $self->get('cancel') ) {
1249 dbh->rollback if $oldAutoCommit;
1250 return "Can't suspend cancelled package $pkgnum";
1253 if ( $old->get('susp') || $self->get('susp') ) {
1254 dbh->rollback if $oldAutoCommit;
1255 return ""; # no error # complain on adjourn?
1258 my $suspend_time = $options{'time'} || time;
1259 my $date = $options{date} if $options{date}; # adjourn/suspend later
1260 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1262 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1263 dbh->rollback if $oldAutoCommit;
1264 return "Package $pkgnum expires before it would be suspended.";
1267 # some false laziness with sub cancel
1268 if ( !$options{nobill} && !$date &&
1269 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1270 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1271 # make the entire cust_main->bill path recognize 'suspend' and
1272 # 'cancel' separately.
1273 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1274 my $copy = $self->new({$self->hash});
1276 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1278 'time' => $suspend_time );
1279 warn "Error billing during suspend, custnum ".
1280 #$self->cust_main->custnum. ": $error"
1285 if ( $options{'reason'} ) {
1286 $error = $self->insert_reason( 'reason' => $options{'reason'},
1287 'action' => $date ? 'adjourn' : 'suspend',
1288 'date' => $date ? $date : $suspend_time,
1289 'reason_otaker' => $options{'reason_otaker'},
1292 dbh->rollback if $oldAutoCommit;
1293 return "Error inserting cust_pkg_reason: $error";
1297 my %hash = $self->hash;
1299 $hash{'adjourn'} = $date;
1301 $hash{'susp'} = $suspend_time;
1304 my $resume_date = $options{'resume_date'} || 0;
1305 if ( $resume_date > ($date || $suspend_time) ) {
1306 $hash{'resume'} = $resume_date;
1309 $options{options} ||= {};
1311 my $new = new FS::cust_pkg ( \%hash );
1312 $error = $new->replace( $self, options => { $self->options,
1313 %{ $options{options} },
1317 $dbh->rollback if $oldAutoCommit;
1322 # credit remaining time if appropriate
1323 if ( $self->part_pkg->option('unused_credit_suspend', 1) ) {
1324 my $error = $self->credit_remaining('suspend', $suspend_time);
1326 $dbh->rollback if $oldAutoCommit;
1333 foreach my $cust_svc (
1334 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
1336 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1338 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1339 $dbh->rollback if $oldAutoCommit;
1340 return "Illegal svcdb value in part_svc!";
1343 require "FS/$svcdb.pm";
1345 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1347 $error = $svc->suspend;
1349 $dbh->rollback if $oldAutoCommit;
1352 my( $label, $value ) = $cust_svc->label;
1353 push @labels, "$label: $value";
1357 my $conf = new FS::Conf;
1358 if ( $conf->config('suspend_email_admin') ) {
1360 my $error = send_email(
1361 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1362 #invoice_from ??? well as good as any
1363 'to' => $conf->config('suspend_email_admin'),
1364 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1366 "This is an automatic message from your Freeside installation\n",
1367 "informing you that the following customer package has been suspended:\n",
1369 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1370 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1371 ( map { "Service : $_\n" } @labels ),
1376 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1384 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1385 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1387 $dbh->rollback if $oldAutoCommit;
1388 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1392 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1397 =item credit_remaining MODE TIME
1399 Generate a credit for this package for the time remaining in the current
1400 billing period. MODE is either "suspend" or "cancel" (determines the
1401 credit type). TIME is the time of suspension/cancellation. Both arguments
1406 sub credit_remaining {
1407 # Add a credit for remaining service
1408 my ($self, $mode, $time) = @_;
1409 die 'credit_remaining requires suspend or cancel'
1410 unless $mode eq 'suspend' or $mode eq 'cancel';
1411 die 'no suspend/cancel time' unless $time > 0;
1413 my $conf = FS::Conf->new;
1414 my $reason_type = $conf->config($mode.'_credit_type');
1416 my $last_bill = $self->getfield('last_bill') || 0;
1417 my $next_bill = $self->getfield('bill') || 0;
1418 if ( $last_bill > 0 # the package has been billed
1419 and $next_bill > 0 # the package has a next bill date
1420 and $next_bill >= $time # which is in the future
1422 my $remaining_value = $self->calc_remain('time' => $time);
1423 if ( $remaining_value > 0 ) {
1424 warn "Crediting for $remaining_value on package ".$self->pkgnum."\n"
1426 my $error = $self->cust_main->credit(
1428 'Credit for unused time on '. $self->part_pkg->pkg,
1429 'reason_type' => $reason_type,
1431 return "Error crediting customer \$$remaining_value for unused time".
1432 " on ". $self->part_pkg->pkg. ": $error"
1434 } #if $remaining_value
1435 } #if $last_bill, etc.
1439 =item unsuspend [ OPTION => VALUE ... ]
1441 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1442 package, then unsuspends the package itself (clears the susp field and the
1443 adjourn field if it is in the past). If the suspend reason includes an
1444 unsuspension package, that package will be ordered.
1446 Available options are:
1452 Can be set to a date to unsuspend the package in the future (the 'resume'
1455 =item adjust_next_bill
1457 Can be set true to adjust the next bill date forward by
1458 the amount of time the account was inactive. This was set true by default
1459 in the past (from 1.4.2 and 1.5.0pre6 through 1.7.0), but now needs to be
1460 explicitly requested with this option or in the price plan.
1464 If there is an error, returns the error, otherwise returns false.
1469 my( $self, %opt ) = @_;
1472 # pass all suspend/cancel actions to the main package
1473 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1474 return $self->main_pkg->unsuspend(%opt);
1477 local $SIG{HUP} = 'IGNORE';
1478 local $SIG{INT} = 'IGNORE';
1479 local $SIG{QUIT} = 'IGNORE';
1480 local $SIG{TERM} = 'IGNORE';
1481 local $SIG{TSTP} = 'IGNORE';
1482 local $SIG{PIPE} = 'IGNORE';
1484 my $oldAutoCommit = $FS::UID::AutoCommit;
1485 local $FS::UID::AutoCommit = 0;
1488 my $old = $self->select_for_update;
1490 my $pkgnum = $old->pkgnum;
1491 if ( $old->get('cancel') || $self->get('cancel') ) {
1492 $dbh->rollback if $oldAutoCommit;
1493 return "Can't unsuspend cancelled package $pkgnum";
1496 unless ( $old->get('susp') && $self->get('susp') ) {
1497 $dbh->rollback if $oldAutoCommit;
1498 return ""; # no error # complain instead?
1501 my $date = $opt{'date'};
1502 if ( $date and $date > time ) { # return an error if $date <= time?
1504 if ( $old->get('expire') && $old->get('expire') < $date ) {
1505 $dbh->rollback if $oldAutoCommit;
1506 return "Package $pkgnum expires before it would be unsuspended.";
1509 my $new = new FS::cust_pkg { $self->hash };
1510 $new->set('resume', $date);
1511 $error = $new->replace($self, options => $self->options);
1514 $dbh->rollback if $oldAutoCommit;
1518 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1526 foreach my $cust_svc (
1527 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1529 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1531 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1532 $dbh->rollback if $oldAutoCommit;
1533 return "Illegal svcdb value in part_svc!";
1536 require "FS/$svcdb.pm";
1538 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1540 $error = $svc->unsuspend;
1542 $dbh->rollback if $oldAutoCommit;
1545 my( $label, $value ) = $cust_svc->label;
1546 push @labels, "$label: $value";
1551 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1552 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
1554 my %hash = $self->hash;
1555 my $inactive = time - $hash{'susp'};
1557 my $conf = new FS::Conf;
1559 if ( $inactive > 0 &&
1560 ( $hash{'bill'} || $hash{'setup'} ) &&
1561 ( $opt{'adjust_next_bill'} ||
1562 $conf->exists('unsuspend-always_adjust_next_bill_date') ||
1563 $self->part_pkg->option('unsuspend_adjust_bill', 1) )
1566 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
1571 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
1572 $hash{'resume'} = '' if !$hash{'adjourn'};
1573 my $new = new FS::cust_pkg ( \%hash );
1574 $error = $new->replace( $self, options => { $self->options } );
1576 $dbh->rollback if $oldAutoCommit;
1582 if ( $reason && $reason->unsuspend_pkgpart ) {
1583 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
1584 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
1586 my $start_date = $self->cust_main->next_bill_date
1587 if $reason->unsuspend_hold;
1590 $unsusp_pkg = FS::cust_pkg->new({
1591 'custnum' => $self->custnum,
1592 'pkgpart' => $reason->unsuspend_pkgpart,
1593 'start_date' => $start_date,
1594 'locationnum' => $self->locationnum,
1595 # discount? probably not...
1598 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
1602 $dbh->rollback if $oldAutoCommit;
1607 if ( $conf->config('unsuspend_email_admin') ) {
1609 my $error = send_email(
1610 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1611 #invoice_from ??? well as good as any
1612 'to' => $conf->config('unsuspend_email_admin'),
1613 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
1614 "This is an automatic message from your Freeside installation\n",
1615 "informing you that the following customer package has been unsuspended:\n",
1617 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1618 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1619 ( map { "Service : $_\n" } @labels ),
1621 "An unsuspension fee was charged: ".
1622 $unsusp_pkg->part_pkg->pkg_comment."\n"
1629 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
1635 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1636 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
1638 $dbh->rollback if $oldAutoCommit;
1639 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1643 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1650 Cancels any pending suspension (sets the adjourn field to null).
1652 If there is an error, returns the error, otherwise returns false.
1657 my( $self, %options ) = @_;
1660 local $SIG{HUP} = 'IGNORE';
1661 local $SIG{INT} = 'IGNORE';
1662 local $SIG{QUIT} = 'IGNORE';
1663 local $SIG{TERM} = 'IGNORE';
1664 local $SIG{TSTP} = 'IGNORE';
1665 local $SIG{PIPE} = 'IGNORE';
1667 my $oldAutoCommit = $FS::UID::AutoCommit;
1668 local $FS::UID::AutoCommit = 0;
1671 my $old = $self->select_for_update;
1673 my $pkgnum = $old->pkgnum;
1674 if ( $old->get('cancel') || $self->get('cancel') ) {
1675 dbh->rollback if $oldAutoCommit;
1676 return "Can't unadjourn cancelled package $pkgnum";
1677 # or at least it's pointless
1680 if ( $old->get('susp') || $self->get('susp') ) {
1681 dbh->rollback if $oldAutoCommit;
1682 return "Can't unadjourn suspended package $pkgnum";
1683 # perhaps this is arbitrary
1686 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1687 dbh->rollback if $oldAutoCommit;
1688 return ""; # no error
1691 my %hash = $self->hash;
1692 $hash{'adjourn'} = '';
1693 $hash{'resume'} = '';
1694 my $new = new FS::cust_pkg ( \%hash );
1695 $error = $new->replace( $self, options => { $self->options } );
1697 $dbh->rollback if $oldAutoCommit;
1701 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1708 =item change HASHREF | OPTION => VALUE ...
1710 Changes this package: cancels it and creates a new one, with a different
1711 pkgpart or locationnum or both. All services are transferred to the new
1712 package (no change will be made if this is not possible).
1714 Options may be passed as a list of key/value pairs or as a hash reference.
1721 New locationnum, to change the location for this package.
1725 New FS::cust_location object, to create a new location and assign it
1730 New FS::cust_main object, to create a new customer and assign the new package
1735 New pkgpart (see L<FS::part_pkg>).
1739 New refnum (see L<FS::part_referral>).
1743 New quantity; if unspecified, the new package will have the same quantity
1748 "New" (existing) FS::cust_pkg object. The package's services and other
1749 attributes will be transferred to this package.
1753 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1754 susp, adjourn, cancel, expire, and contract_end) to the new package.
1756 =item unprotect_svcs
1758 Normally, change() will rollback and return an error if some services
1759 can't be transferred (also see the I<cust_pkg-change_svcpart> config option).
1760 If unprotect_svcs is true, this method will transfer as many services as
1761 it can and then unconditionally cancel the old package.
1765 At least one of locationnum, cust_location, pkgpart, refnum, cust_main, or
1766 cust_pkg must be specified (otherwise, what's the point?)
1768 Returns either the new FS::cust_pkg object or a scalar error.
1772 my $err_or_new_cust_pkg = $old_cust_pkg->change
1776 #some false laziness w/order
1779 my $opt = ref($_[0]) ? shift : { @_ };
1781 my $conf = new FS::Conf;
1783 # Transactionize this whole mess
1784 local $SIG{HUP} = 'IGNORE';
1785 local $SIG{INT} = 'IGNORE';
1786 local $SIG{QUIT} = 'IGNORE';
1787 local $SIG{TERM} = 'IGNORE';
1788 local $SIG{TSTP} = 'IGNORE';
1789 local $SIG{PIPE} = 'IGNORE';
1791 my $oldAutoCommit = $FS::UID::AutoCommit;
1792 local $FS::UID::AutoCommit = 0;
1801 $hash{'setup'} = $time if $self->setup;
1803 $hash{'change_date'} = $time;
1804 $hash{"change_$_"} = $self->$_()
1805 foreach qw( pkgnum pkgpart locationnum );
1807 if ( $opt->{'cust_location'} ) {
1808 $error = $opt->{'cust_location'}->find_or_insert;
1810 $dbh->rollback if $oldAutoCommit;
1811 return "creating location record: $error";
1813 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1816 if ( $opt->{'cust_pkg'} ) {
1817 # treat changing to a package with a different pkgpart as a
1818 # pkgpart change (because it is)
1819 $opt->{'pkgpart'} = $opt->{'cust_pkg'}->pkgpart;
1822 # whether to override pkgpart checking on the new package
1823 my $same_pkgpart = 1;
1824 if ( $opt->{'pkgpart'} and ( $opt->{'pkgpart'} != $self->pkgpart ) ) {
1828 my $unused_credit = 0;
1829 my $keep_dates = $opt->{'keep_dates'};
1830 # Special case. If the pkgpart is changing, and the customer is
1831 # going to be credited for remaining time, don't keep setup, bill,
1832 # or last_bill dates, and DO pass the flag to cancel() to credit
1834 if ( $opt->{'pkgpart'}
1835 and $opt->{'pkgpart'} != $self->pkgpart
1836 and $self->part_pkg->option('unused_credit_change', 1) ) {
1839 $hash{$_} = '' foreach qw(setup bill last_bill);
1842 if ( $keep_dates ) {
1843 foreach my $date ( qw(setup bill last_bill susp adjourn cancel expire
1844 resume start_date contract_end ) ) {
1845 $hash{$date} = $self->getfield($date);
1849 # allow $opt->{'locationnum'} = '' to specifically set it to null
1850 # (i.e. customer default location)
1851 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
1853 # usually this doesn't matter. the two cases where it does are:
1854 # 1. unused_credit_change + pkgpart change + setup fee on the new package
1856 # 2. (more importantly) changing a package before it's billed
1857 $hash{'waive_setup'} = $self->waive_setup;
1859 my $custnum = $self->custnum;
1860 if ( $opt->{cust_main} ) {
1861 my $cust_main = $opt->{cust_main};
1862 unless ( $cust_main->custnum ) {
1863 my $error = $cust_main->insert( @{ $opt->{cust_main_insert_args}||[] } );
1865 $dbh->rollback if $oldAutoCommit;
1866 return "inserting customer record: $error";
1869 $custnum = $cust_main->custnum;
1872 $hash{'contactnum'} = $opt->{'contactnum'} if $opt->{'contactnum'};
1875 if ( $opt->{'cust_pkg'} ) {
1876 # The target package already exists; update it to show that it was
1877 # changed from this package.
1878 $cust_pkg = $opt->{'cust_pkg'};
1880 foreach ( qw( pkgnum pkgpart locationnum ) ) {
1881 $cust_pkg->set("change_$_", $self->get($_));
1883 $cust_pkg->set('change_date', $time);
1884 $error = $cust_pkg->replace;
1887 # Create the new package.
1888 $cust_pkg = new FS::cust_pkg {
1889 custnum => $custnum,
1890 locationnum => $opt->{'locationnum'},
1891 ( map { $_ => ( $opt->{$_} || $self->$_() ) }
1892 qw( pkgpart quantity refnum salesnum )
1896 $error = $cust_pkg->insert( 'change' => 1,
1897 'allow_pkgpart' => $same_pkgpart );
1900 $dbh->rollback if $oldAutoCommit;
1901 return "inserting new package: $error";
1904 # Transfer services and cancel old package.
1906 $error = $self->transfer($cust_pkg);
1907 if ($error and $error == 0) {
1908 # $old_pkg->transfer failed.
1909 $dbh->rollback if $oldAutoCommit;
1910 return "transferring $error";
1913 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
1914 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
1915 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
1916 if ($error and $error == 0) {
1917 # $old_pkg->transfer failed.
1918 $dbh->rollback if $oldAutoCommit;
1919 return "converting $error";
1923 # We set unprotect_svcs when executing a "future package change". It's
1924 # not a user-interactive operation, so returning an error means the
1925 # package change will just fail. Rather than have that happen, we'll
1926 # let leftover services be deleted.
1927 if ($error > 0 and !$opt->{'unprotect_svcs'}) {
1928 # Transfers were successful, but we still had services left on the old
1929 # package. We can't change the package under this circumstances, so abort.
1930 $dbh->rollback if $oldAutoCommit;
1931 return "unable to transfer all services";
1934 #reset usage if changing pkgpart
1935 # AND usage rollover is off (otherwise adds twice, now and at package bill)
1936 if ($self->pkgpart != $cust_pkg->pkgpart) {
1937 my $part_pkg = $cust_pkg->part_pkg;
1938 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
1942 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
1945 $dbh->rollback if $oldAutoCommit;
1946 return "setting usage values: $error";
1949 # if NOT changing pkgpart, transfer any usage pools over
1950 foreach my $usage ($self->cust_pkg_usage) {
1951 $usage->set('pkgnum', $cust_pkg->pkgnum);
1952 $error = $usage->replace;
1954 $dbh->rollback if $oldAutoCommit;
1955 return "transferring usage pools: $error";
1960 # transfer discounts, if we're not changing pkgpart
1961 if ( $same_pkgpart ) {
1962 foreach my $old_discount ($self->cust_pkg_discount_active) {
1963 # don't remove the old discount, we may still need to bill that package.
1964 my $new_discount = new FS::cust_pkg_discount {
1965 'pkgnum' => $cust_pkg->pkgnum,
1966 'discountnum' => $old_discount->discountnum,
1967 'months_used' => $old_discount->months_used,
1969 $error = $new_discount->insert;
1971 $dbh->rollback if $oldAutoCommit;
1972 return "transferring discounts: $error";
1977 # transfer (copy) invoice details
1978 foreach my $detail ($self->cust_pkg_detail) {
1979 my $new_detail = FS::cust_pkg_detail->new({ $detail->hash });
1980 $new_detail->set('pkgdetailnum', '');
1981 $new_detail->set('pkgnum', $cust_pkg->pkgnum);
1982 $error = $new_detail->insert;
1984 $dbh->rollback if $oldAutoCommit;
1985 return "transferring package notes: $error";
1991 if ( !$opt->{'cust_pkg'} ) {
1992 # Order any supplemental packages.
1993 my $part_pkg = $cust_pkg->part_pkg;
1994 my @old_supp_pkgs = $self->supplemental_pkgs;
1995 foreach my $link ($part_pkg->supp_part_pkg_link) {
1997 foreach (@old_supp_pkgs) {
1998 if ($_->pkgpart == $link->dst_pkgpart) {
2000 $_->pkgpart(0); # so that it can't match more than once
2004 # false laziness with FS::cust_main::Packages::order_pkg
2005 my $new = FS::cust_pkg->new({
2006 pkgpart => $link->dst_pkgpart,
2007 pkglinknum => $link->pkglinknum,
2008 custnum => $custnum,
2009 main_pkgnum => $cust_pkg->pkgnum,
2010 locationnum => $cust_pkg->locationnum,
2011 start_date => $cust_pkg->start_date,
2012 order_date => $cust_pkg->order_date,
2013 expire => $cust_pkg->expire,
2014 adjourn => $cust_pkg->adjourn,
2015 contract_end => $cust_pkg->contract_end,
2016 refnum => $cust_pkg->refnum,
2017 discountnum => $cust_pkg->discountnum,
2018 waive_setup => $cust_pkg->waive_setup,
2020 if ( $old and $opt->{'keep_dates'} ) {
2021 foreach (qw(setup bill last_bill)) {
2022 $new->set($_, $old->get($_));
2025 $error = $new->insert( allow_pkgpart => $same_pkgpart );
2028 $error ||= $old->transfer($new);
2030 if ( $error and $error > 0 ) {
2031 # no reason why this should ever fail, but still...
2032 $error = "Unable to transfer all services from supplemental package ".
2036 $dbh->rollback if $oldAutoCommit;
2039 push @new_supp_pkgs, $new;
2041 } # if !$opt->{'cust_pkg'}
2042 # because if there is one, then supplemental packages would already
2043 # have been created for it.
2045 #Good to go, cancel old package. Notify 'cancel' of whether to credit
2047 #Don't allow billing the package (preceding period packages and/or
2048 #outstanding usage) if we are keeping dates (i.e. location changing),
2049 #because the new package will be billed for the same date range.
2050 #Supplemental packages are also canceled here.
2052 # during scheduled changes, avoid canceling the package we just
2054 $self->set('change_to_pkgnum' => '');
2056 $error = $self->cancel(
2058 unused_credit => $unused_credit,
2059 nobill => $keep_dates,
2060 change_custnum => ( $self->custnum != $custnum ? $custnum : '' ),
2063 $dbh->rollback if $oldAutoCommit;
2064 return "canceling old package: $error";
2067 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
2069 my $error = $cust_pkg->cust_main->bill(
2070 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
2073 $dbh->rollback if $oldAutoCommit;
2074 return "billing new package: $error";
2078 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2084 =item change_later OPTION => VALUE...
2086 Schedule a package change for a later date. This actually orders the new
2087 package immediately, but sets its start date for a future date, and sets
2088 the current package to expire on the same date.
2090 If the package is already scheduled for a change, this can be called with
2091 'start_date' to change the scheduled date, or with pkgpart and/or
2092 locationnum to modify the package change. To cancel the scheduled change
2093 entirely, see C<abort_change>.
2101 The date for the package change. Required, and must be in the future.
2109 The pkgpart. locationnum, and quantity of the new package, with the same
2110 meaning as in C<change>.
2118 my $opt = ref($_[0]) ? shift : { @_ };
2120 my $oldAutoCommit = $FS::UID::AutoCommit;
2121 local $FS::UID::AutoCommit = 0;
2124 my $cust_main = $self->cust_main;
2126 my $date = delete $opt->{'start_date'} or return 'start_date required';
2128 if ( $date <= time ) {
2129 $dbh->rollback if $oldAutoCommit;
2130 return "start_date $date is in the past";
2135 if ( $self->change_to_pkgnum ) {
2136 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
2137 my $new_pkgpart = $opt->{'pkgpart'}
2138 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $change_to->pkgpart;
2139 my $new_locationnum = $opt->{'locationnum'}
2140 if $opt->{'locationnum'} and $opt->{'locationnum'} != $change_to->locationnum;
2141 my $new_quantity = $opt->{'quantity'}
2142 if $opt->{'quantity'} and $opt->{'quantity'} != $change_to->quantity;
2143 if ( $new_pkgpart or $new_locationnum or $new_quantity ) {
2144 # it hasn't been billed yet, so in principle we could just edit
2145 # it in place (w/o a package change), but that's bad form.
2146 # So change the package according to the new options...
2147 my $err_or_pkg = $change_to->change(%$opt);
2148 if ( ref $err_or_pkg ) {
2149 # Then set that package up for a future start.
2150 $self->set('change_to_pkgnum', $err_or_pkg->pkgnum);
2151 $self->set('expire', $date); # in case it's different
2152 $err_or_pkg->set('start_date', $date);
2153 $err_or_pkg->set('change_date', '');
2154 $err_or_pkg->set('change_pkgnum', '');
2156 $error = $self->replace ||
2157 $err_or_pkg->replace ||
2158 $change_to->cancel ||
2161 $error = $err_or_pkg;
2163 } else { # change the start date only.
2164 $self->set('expire', $date);
2165 $change_to->set('start_date', $date);
2166 $error = $self->replace || $change_to->replace;
2169 $dbh->rollback if $oldAutoCommit;
2172 $dbh->commit if $oldAutoCommit;
2175 } # if $self->change_to_pkgnum
2177 my $new_pkgpart = $opt->{'pkgpart'}
2178 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart;
2179 my $new_locationnum = $opt->{'locationnum'}
2180 if $opt->{'locationnum'} and $opt->{'locationnum'} != $self->locationnum;
2181 my $new_quantity = $opt->{'quantity'}
2182 if $opt->{'quantity'} and $opt->{'quantity'} != $self->quantity;
2184 return '' unless $new_pkgpart or $new_locationnum or $new_quantity; # wouldn't do anything
2186 # allow $opt->{'locationnum'} = '' to specifically set it to null
2187 # (i.e. customer default location)
2188 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2190 my $new = FS::cust_pkg->new( {
2191 custnum => $self->custnum,
2192 locationnum => $opt->{'locationnum'},
2193 start_date => $date,
2194 map { $_ => ( $opt->{$_} || $self->$_() ) }
2195 qw( pkgpart quantity refnum salesnum )
2197 $error = $new->insert('change' => 1,
2198 'allow_pkgpart' => ($new_pkgpart ? 0 : 1));
2200 $self->set('change_to_pkgnum', $new->pkgnum);
2201 $self->set('expire', $date);
2202 $error = $self->replace;
2205 $dbh->rollback if $oldAutoCommit;
2207 $dbh->commit if $oldAutoCommit;
2215 Cancels a future package change scheduled by C<change_later>.
2221 my $pkgnum = $self->change_to_pkgnum;
2222 my $change_to = FS::cust_pkg->by_key($pkgnum) if $pkgnum;
2225 $error = $change_to->cancel || $change_to->delete;
2226 return $error if $error;
2228 $self->set('change_to_pkgnum', '');
2229 $self->set('expire', '');
2233 =item set_quantity QUANTITY
2235 Change the package's quantity field. This is one of the few package properties
2236 that can safely be changed without canceling and reordering the package
2237 (because it doesn't affect tax eligibility). Returns an error or an
2244 $self = $self->replace_old; # just to make sure
2245 $self->quantity(shift);
2249 =item set_salesnum SALESNUM
2251 Change the package's salesnum (sales person) field. This is one of the few
2252 package properties that can safely be changed without canceling and reordering
2253 the package (because it doesn't affect tax eligibility). Returns an error or
2260 $self = $self->replace_old; # just to make sure
2261 $self->salesnum(shift);
2263 # XXX this should probably reassign any credit that's already been given
2266 =item modify_charge OPTIONS
2268 Change the properties of a one-time charge. The following properties can
2269 be changed this way:
2270 - pkg: the package description
2271 - classnum: the package class
2272 - additional: arrayref of additional invoice details to add to this package
2274 and, I<if the charge has not yet been billed>:
2275 - start_date: the date when it will be billed
2276 - amount: the setup fee to be charged
2277 - quantity: the multiplier for the setup fee
2279 If you pass 'adjust_commission' => 1, and the classnum changes, and there are
2280 commission credits linked to this charge, they will be recalculated.
2287 my $part_pkg = $self->part_pkg;
2288 my $pkgnum = $self->pkgnum;
2291 my $oldAutoCommit = $FS::UID::AutoCommit;
2292 local $FS::UID::AutoCommit = 0;
2294 return "Can't use modify_charge except on one-time charges"
2295 unless $part_pkg->freq eq '0';
2297 if ( length($opt{'pkg'}) and $part_pkg->pkg ne $opt{'pkg'} ) {
2298 $part_pkg->set('pkg', $opt{'pkg'});
2301 my %pkg_opt = $part_pkg->options;
2302 if ( ref($opt{'additional'}) ) {
2303 delete $pkg_opt{$_} foreach grep /^additional/, keys %pkg_opt;
2305 for ( $i = 0; exists($opt{'additional'}->[$i]); $i++ ) {
2306 $pkg_opt{ "additional_info$i" } = $opt{'additional'}->[$i];
2308 $pkg_opt{'additional_count'} = $i if $i > 0;
2312 if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} )
2315 $old_classnum = $part_pkg->classnum;
2316 $part_pkg->set('classnum', $opt{'classnum'});
2319 if ( !$self->get('setup') ) {
2320 # not yet billed, so allow amount and quantity
2321 if ( exists($opt{'quantity'})
2322 and $opt{'quantity'} != $self->quantity
2323 and $opt{'quantity'} > 0 ) {
2325 $self->set('quantity', $opt{'quantity'});
2327 if ( exists($opt{'start_date'})
2328 and $opt{'start_date'} != $self->start_date ) {
2330 $self->set('start_date', $opt{'start_date'});
2332 if ($self->modified) { # for quantity or start_date change
2333 my $error = $self->replace;
2334 return $error if $error;
2337 if ( exists($opt{'amount'})
2338 and $part_pkg->option('setup_fee') != $opt{'amount'}
2339 and $opt{'amount'} > 0 ) {
2341 $pkg_opt{'setup_fee'} = $opt{'amount'};
2342 # standard for one-time charges is to set comment = (formatted) amount
2343 # update it to avoid confusion
2344 my $conf = FS::Conf->new;
2345 $part_pkg->set('comment',
2346 ($conf->config('money_char') || '$') .
2347 sprintf('%.2f', $opt{'amount'})
2350 } # else simply ignore them; the UI shouldn't allow editing the fields
2352 my $error = $part_pkg->replace( options => \%pkg_opt );
2354 $dbh->rollback if $oldAutoCommit;
2358 if (defined $old_classnum) {
2359 # fix invoice grouping records
2360 my $old_catname = $old_classnum
2361 ? FS::pkg_class->by_key($old_classnum)->categoryname
2363 my $new_catname = $opt{'classnum'}
2364 ? $part_pkg->pkg_class->categoryname
2366 if ( $old_catname ne $new_catname ) {
2367 foreach my $cust_bill_pkg ($self->cust_bill_pkg) {
2368 # (there should only be one...)
2369 my @display = qsearch( 'cust_bill_pkg_display', {
2370 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2371 'section' => $old_catname,
2373 foreach (@display) {
2374 $_->set('section', $new_catname);
2375 $error = $_->replace;
2377 $dbh->rollback if $oldAutoCommit;
2381 } # foreach $cust_bill_pkg
2384 if ( $opt{'adjust_commission'} ) {
2385 # fix commission credits...tricky.
2386 foreach my $cust_event ($self->cust_event) {
2387 my $part_event = $cust_event->part_event;
2388 foreach my $table (qw(sales agent)) {
2390 "FS::part_event::Action::Mixin::credit_${table}_pkg_class";
2391 my $credit = qsearchs('cust_credit', {
2392 'eventnum' => $cust_event->eventnum,
2394 if ( $part_event->isa($class) ) {
2395 # Yes, this results in current commission rates being applied
2396 # retroactively to a one-time charge. For accounting purposes
2397 # there ought to be some kind of time limit on doing this.
2398 my $amount = $part_event->_calc_credit($self);
2399 if ( $credit and $credit->amount ne $amount ) {
2400 # Void the old credit.
2401 $error = $credit->void('Package class changed');
2403 $dbh->rollback if $oldAutoCommit;
2404 return "$error (adjusting commission credit)";
2407 # redo the event action to recreate the credit.
2409 eval { $part_event->do_action( $self, $cust_event ) };
2411 $dbh->rollback if $oldAutoCommit;
2414 } # if $part_event->isa($class)
2416 } # foreach $cust_event
2417 } # if $opt{'adjust_commission'}
2418 } # if defined $old_classnum
2420 $dbh->commit if $oldAutoCommit;
2426 use Storable 'thaw';
2429 sub process_bulk_cust_pkg {
2431 my $param = thaw(decode_base64(shift));
2432 warn Dumper($param) if $DEBUG;
2434 my $old_part_pkg = qsearchs('part_pkg',
2435 { pkgpart => $param->{'old_pkgpart'} });
2436 my $new_part_pkg = qsearchs('part_pkg',
2437 { pkgpart => $param->{'new_pkgpart'} });
2438 die "Must select a new package type\n" unless $new_part_pkg;
2439 #my $keep_dates = $param->{'keep_dates'} || 0;
2440 my $keep_dates = 1; # there is no good reason to turn this off
2442 local $SIG{HUP} = 'IGNORE';
2443 local $SIG{INT} = 'IGNORE';
2444 local $SIG{QUIT} = 'IGNORE';
2445 local $SIG{TERM} = 'IGNORE';
2446 local $SIG{TSTP} = 'IGNORE';
2447 local $SIG{PIPE} = 'IGNORE';
2449 my $oldAutoCommit = $FS::UID::AutoCommit;
2450 local $FS::UID::AutoCommit = 0;
2453 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
2456 foreach my $old_cust_pkg ( @cust_pkgs ) {
2458 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
2459 if ( $old_cust_pkg->getfield('cancel') ) {
2460 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
2461 $old_cust_pkg->pkgnum."\n"
2465 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
2467 my $error = $old_cust_pkg->change(
2468 'pkgpart' => $param->{'new_pkgpart'},
2469 'keep_dates' => $keep_dates
2471 if ( !ref($error) ) { # change returns the cust_pkg on success
2473 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
2476 $dbh->commit if $oldAutoCommit;
2482 Returns the last bill date, or if there is no last bill date, the setup date.
2483 Useful for billing metered services.
2489 return $self->setfield('last_bill', $_[0]) if @_;
2490 return $self->getfield('last_bill') if $self->getfield('last_bill');
2491 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
2492 'edate' => $self->bill, } );
2493 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
2496 =item last_cust_pkg_reason ACTION
2498 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
2499 Returns false if there is no reason or the package is not currenly ACTION'd
2500 ACTION is one of adjourn, susp, cancel, or expire.
2504 sub last_cust_pkg_reason {
2505 my ( $self, $action ) = ( shift, shift );
2506 my $date = $self->get($action);
2508 'table' => 'cust_pkg_reason',
2509 'hashref' => { 'pkgnum' => $self->pkgnum,
2510 'action' => substr(uc($action), 0, 1),
2513 'order_by' => 'ORDER BY num DESC LIMIT 1',
2517 =item last_reason ACTION
2519 Returns the most recent ACTION FS::reason associated with the package.
2520 Returns false if there is no reason or the package is not currenly ACTION'd
2521 ACTION is one of adjourn, susp, cancel, or expire.
2526 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
2527 $cust_pkg_reason->reason
2528 if $cust_pkg_reason;
2533 Returns the definition for this billing item, as an FS::part_pkg object (see
2540 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
2541 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
2542 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
2547 Returns the cancelled package this package was changed from, if any.
2553 return '' unless $self->change_pkgnum;
2554 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
2557 =item change_cust_main
2559 Returns the customter this package was detached to, if any.
2563 sub change_cust_main {
2565 return '' unless $self->change_custnum;
2566 qsearchs('cust_main', { 'custnum' => $self->change_custnum } );
2571 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
2578 $self->part_pkg->calc_setup($self, @_);
2583 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
2590 $self->part_pkg->calc_recur($self, @_);
2595 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
2602 $self->part_pkg->base_recur($self, @_);
2607 Calls the I<calc_remain> of the FS::part_pkg object associated with this
2614 $self->part_pkg->calc_remain($self, @_);
2619 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
2626 $self->part_pkg->calc_cancel($self, @_);
2631 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
2637 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
2640 =item cust_pkg_detail [ DETAILTYPE ]
2642 Returns any customer package details for this package (see
2643 L<FS::cust_pkg_detail>).
2645 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2649 sub cust_pkg_detail {
2651 my %hash = ( 'pkgnum' => $self->pkgnum );
2652 $hash{detailtype} = shift if @_;
2654 'table' => 'cust_pkg_detail',
2655 'hashref' => \%hash,
2656 'order_by' => 'ORDER BY weight, pkgdetailnum',
2660 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
2662 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
2664 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2666 If there is an error, returns the error, otherwise returns false.
2670 sub set_cust_pkg_detail {
2671 my( $self, $detailtype, @details ) = @_;
2673 local $SIG{HUP} = 'IGNORE';
2674 local $SIG{INT} = 'IGNORE';
2675 local $SIG{QUIT} = 'IGNORE';
2676 local $SIG{TERM} = 'IGNORE';
2677 local $SIG{TSTP} = 'IGNORE';
2678 local $SIG{PIPE} = 'IGNORE';
2680 my $oldAutoCommit = $FS::UID::AutoCommit;
2681 local $FS::UID::AutoCommit = 0;
2684 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
2685 my $error = $current->delete;
2687 $dbh->rollback if $oldAutoCommit;
2688 return "error removing old detail: $error";
2692 foreach my $detail ( @details ) {
2693 my $cust_pkg_detail = new FS::cust_pkg_detail {
2694 'pkgnum' => $self->pkgnum,
2695 'detailtype' => $detailtype,
2696 'detail' => $detail,
2698 my $error = $cust_pkg_detail->insert;
2700 $dbh->rollback if $oldAutoCommit;
2701 return "error adding new detail: $error";
2706 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2713 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
2717 #false laziness w/cust_bill.pm
2721 'table' => 'cust_event',
2722 'addl_from' => 'JOIN part_event USING ( eventpart )',
2723 'hashref' => { 'tablenum' => $self->pkgnum },
2724 'extra_sql' => " AND eventtable = 'cust_pkg' ",
2728 =item num_cust_event
2730 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
2734 #false laziness w/cust_bill.pm
2735 sub num_cust_event {
2738 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
2739 " WHERE tablenum = ? AND eventtable = 'cust_pkg'";
2740 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
2741 $sth->execute($self->pkgnum) or die $sth->errstr. " executing $sql";
2742 $sth->fetchrow_arrayref->[0];
2745 =item cust_svc [ SVCPART ] (old, deprecated usage)
2747 =item cust_svc [ OPTION => VALUE ... ] (current usage)
2749 =item cust_svc_unsorted [ OPTION => VALUE ... ]
2751 Returns the services for this package, as FS::cust_svc objects (see
2752 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
2753 spcififed, returns only the matching services.
2755 As an optimization, use the cust_svc_unsorted version if you are not displaying
2762 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
2763 $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref(@_) );
2766 sub cust_svc_unsorted {
2768 @{ $self->cust_svc_unsorted_arrayref(@_) };
2771 sub cust_svc_unsorted_arrayref {
2774 return () unless $self->num_cust_svc(@_);
2777 if ( @_ && $_[0] =~ /^\d+/ ) {
2778 $opt{svcpart} = shift;
2779 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2786 'table' => 'cust_svc',
2787 'hashref' => { 'pkgnum' => $self->pkgnum },
2789 if ( $opt{svcpart} ) {
2790 $search{hashref}->{svcpart} = $opt{'svcpart'};
2792 if ( $opt{'svcdb'} ) {
2793 $search{addl_from} = ' LEFT JOIN part_svc USING ( svcpart ) ';
2794 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{'svcdb'} );
2797 [ qsearch(\%search) ];
2801 =item overlimit [ SVCPART ]
2803 Returns the services for this package which have exceeded their
2804 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
2805 is specified, return only the matching services.
2811 return () unless $self->num_cust_svc(@_);
2812 grep { $_->overlimit } $self->cust_svc(@_);
2815 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
2817 Returns historical services for this package created before END TIMESTAMP and
2818 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
2819 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
2820 I<pkg_svc.hidden> flag will be omitted.
2826 warn "$me _h_cust_svc called on $self\n"
2829 my ($end, $start, $mode) = @_;
2830 my @cust_svc = $self->_sort_cust_svc(
2831 [ qsearch( 'h_cust_svc',
2832 { 'pkgnum' => $self->pkgnum, },
2833 FS::h_cust_svc->sql_h_search(@_),
2836 if ( defined($mode) && $mode eq 'I' ) {
2837 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
2838 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
2844 sub _sort_cust_svc {
2845 my( $self, $arrayref ) = @_;
2848 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
2850 my %pkg_svc = map { $_->svcpart => $_ }
2851 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
2856 my $pkg_svc = $pkg_svc{ $_->svcpart } || '';
2858 $pkg_svc ? $pkg_svc->primary_svc : '',
2859 $pkg_svc ? $pkg_svc->quantity : 0,
2866 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
2868 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
2870 Returns the number of services for this package. Available options are svcpart
2871 and svcdb. If either is spcififed, returns only the matching services.
2878 return $self->{'_num_cust_svc'}
2880 && exists($self->{'_num_cust_svc'})
2881 && $self->{'_num_cust_svc'} =~ /\d/;
2883 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
2887 if ( @_ && $_[0] =~ /^\d+/ ) {
2888 $opt{svcpart} = shift;
2889 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2895 my $select = 'SELECT COUNT(*) FROM cust_svc ';
2896 my $where = ' WHERE pkgnum = ? ';
2897 my @param = ($self->pkgnum);
2899 if ( $opt{'svcpart'} ) {
2900 $where .= ' AND svcpart = ? ';
2901 push @param, $opt{'svcpart'};
2903 if ( $opt{'svcdb'} ) {
2904 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
2905 $where .= ' AND svcdb = ? ';
2906 push @param, $opt{'svcdb'};
2909 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
2910 $sth->execute(@param) or die $sth->errstr;
2911 $sth->fetchrow_arrayref->[0];
2914 =item available_part_svc
2916 Returns a list of FS::part_svc objects representing services included in this
2917 package but not yet provisioned. Each FS::part_svc object also has an extra
2918 field, I<num_avail>, which specifies the number of available services.
2922 sub available_part_svc {
2925 my $pkg_quantity = $self->quantity || 1;
2927 grep { $_->num_avail > 0 }
2929 my $part_svc = $_->part_svc;
2930 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
2931 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
2933 # more evil encapsulation breakage
2934 if($part_svc->{'Hash'}{'num_avail'} > 0) {
2935 my @exports = $part_svc->part_export_did;
2936 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
2941 $self->part_pkg->pkg_svc;
2944 =item part_svc [ OPTION => VALUE ... ]
2946 Returns a list of FS::part_svc objects representing provisioned and available
2947 services included in this package. Each FS::part_svc object also has the
2948 following extra fields:
2952 =item num_cust_svc (count)
2954 =item num_avail (quantity - count)
2956 =item cust_pkg_svc (services) - array reference containing the provisioned services, as cust_svc objects
2960 Accepts one option: summarize_size. If specified and non-zero, will omit the
2961 extra cust_pkg_svc option for objects where num_cust_svc is this size or
2967 #label -> ($cust_svc->label)[1]
2973 my $pkg_quantity = $self->quantity || 1;
2975 #XXX some sort of sort order besides numeric by svcpart...
2976 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
2978 my $part_svc = $pkg_svc->part_svc;
2979 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
2980 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
2981 $part_svc->{'Hash'}{'num_avail'} =
2982 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
2983 $part_svc->{'Hash'}{'cust_pkg_svc'} =
2984 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
2985 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
2986 && $num_cust_svc >= $opt{summarize_size};
2987 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
2989 } $self->part_pkg->pkg_svc;
2992 push @part_svc, map {
2994 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
2995 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
2996 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
2997 $part_svc->{'Hash'}{'cust_pkg_svc'} =
2998 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
3000 } $self->extra_part_svc;
3006 =item extra_part_svc
3008 Returns a list of FS::part_svc objects corresponding to services in this
3009 package which are still provisioned but not (any longer) available in the
3014 sub extra_part_svc {
3017 my $pkgnum = $self->pkgnum;
3018 #my $pkgpart = $self->pkgpart;
3021 # 'table' => 'part_svc',
3024 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
3025 # WHERE pkg_svc.svcpart = part_svc.svcpart
3026 # AND pkg_svc.pkgpart = ?
3029 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
3030 # LEFT JOIN cust_pkg USING ( pkgnum )
3031 # WHERE cust_svc.svcpart = part_svc.svcpart
3034 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
3037 #seems to benchmark slightly faster... (or did?)
3039 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
3040 my $pkgparts = join(',', @pkgparts);
3043 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
3044 #MySQL doesn't grok DISINCT ON
3045 'select' => 'DISTINCT part_svc.*',
3046 'table' => 'part_svc',
3048 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
3049 AND pkg_svc.pkgpart IN ($pkgparts)
3052 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
3053 LEFT JOIN cust_pkg USING ( pkgnum )
3056 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
3057 'extra_param' => [ [$self->pkgnum=>'int'] ],
3063 Returns a short status string for this package, currently:
3067 =item not yet billed
3069 =item one-time charge
3084 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
3086 return 'cancelled' if $self->get('cancel');
3087 return 'suspended' if $self->susp;
3088 return 'not yet billed' unless $self->setup;
3089 return 'one-time charge' if $freq =~ /^(0|$)/;
3093 =item ucfirst_status
3095 Returns the status with the first character capitalized.
3099 sub ucfirst_status {
3100 ucfirst(shift->status);
3105 Class method that returns the list of possible status strings for packages
3106 (see L<the status method|/status>). For example:
3108 @statuses = FS::cust_pkg->statuses();
3112 tie my %statuscolor, 'Tie::IxHash',
3113 'not yet billed' => '009999', #teal? cyan?
3114 'one-time charge' => '000000',
3115 'active' => '00CC00',
3116 'suspended' => 'FF9900',
3117 'cancelled' => 'FF0000',
3121 my $self = shift; #could be class...
3122 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
3123 # # mayble split btw one-time vs. recur
3129 Returns a hex triplet color string for this package's status.
3135 $statuscolor{$self->status};
3140 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
3141 "pkg - comment" depending on user preference).
3147 my $label = $self->part_pkg->pkg_comment( 'nopkgpart' => 1 );
3148 $label = $self->pkgnum. ": $label"
3149 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
3153 =item pkg_label_long
3155 Returns a long label for this package, adding the primary service's label to
3160 sub pkg_label_long {
3162 my $label = $self->pkg_label;
3163 my $cust_svc = $self->primary_cust_svc;
3164 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
3170 Returns a customer-localized label for this package.
3176 $self->part_pkg->pkg_locale( $self->cust_main->locale );
3179 =item primary_cust_svc
3181 Returns a primary service (as FS::cust_svc object) if one can be identified.
3185 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
3187 sub primary_cust_svc {
3190 my @cust_svc = $self->cust_svc;
3192 return '' unless @cust_svc; #no serivces - irrelevant then
3194 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
3196 # primary service as specified in the package definition
3197 # or exactly one service definition with quantity one
3198 my $svcpart = $self->part_pkg->svcpart;
3199 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
3200 return $cust_svc[0] if scalar(@cust_svc) == 1;
3202 #couldn't identify one thing..
3208 Returns a list of lists, calling the label method for all services
3209 (see L<FS::cust_svc>) of this billing item.
3215 map { [ $_->label ] } $self->cust_svc;
3218 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3220 Like the labels method, but returns historical information on services that
3221 were active as of END_TIMESTAMP and (optionally) not cancelled before
3222 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
3223 I<pkg_svc.hidden> flag will be omitted.
3225 Returns a list of lists, calling the label method for all (historical) services
3226 (see L<FS::h_cust_svc>) of this billing item.
3232 warn "$me _h_labels called on $self\n"
3234 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
3239 Like labels, except returns a simple flat list, and shortens long
3240 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3241 identical services to one line that lists the service label and the number of
3242 individual services rather than individual items.
3247 shift->_labels_short( 'labels', @_ );
3250 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
3252 Like h_labels, except returns a simple flat list, and shortens long
3253 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3254 identical services to one line that lists the service label and the number of
3255 individual services rather than individual items.
3259 sub h_labels_short {
3260 shift->_labels_short( 'h_labels', @_ );
3264 my( $self, $method ) = ( shift, shift );
3266 warn "$me _labels_short called on $self with $method method\n"
3269 my $conf = new FS::Conf;
3270 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
3272 warn "$me _labels_short populating \%labels\n"
3276 #tie %labels, 'Tie::IxHash';
3277 push @{ $labels{$_->[0]} }, $_->[1]
3278 foreach $self->$method(@_);
3280 warn "$me _labels_short populating \@labels\n"
3284 foreach my $label ( keys %labels ) {
3286 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
3287 my $num = scalar(@values);
3288 warn "$me _labels_short $num items for $label\n"
3291 if ( $num > $max_same_services ) {
3292 warn "$me _labels_short more than $max_same_services, so summarizing\n"
3294 push @labels, "$label ($num)";
3296 if ( $conf->exists('cust_bill-consolidate_services') ) {
3297 warn "$me _labels_short consolidating services\n"
3299 # push @labels, "$label: ". join(', ', @values);
3301 my $detail = "$label: ";
3302 $detail .= shift(@values). ', '
3304 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
3306 push @labels, $detail;
3308 warn "$me _labels_short done consolidating services\n"
3311 warn "$me _labels_short adding service data\n"
3313 push @labels, map { "$label: $_" } @values;
3324 Returns the parent customer object (see L<FS::cust_main>).
3330 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
3335 Returns the balance for this specific package, when using
3336 experimental package balance.
3342 $self->cust_main->balance_pkgnum( $self->pkgnum );
3345 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
3349 Returns the location object, if any (see L<FS::cust_location>).
3351 =item cust_location_or_main
3353 If this package is associated with a location, returns the locaiton (see
3354 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
3356 =item location_label [ OPTION => VALUE ... ]
3358 Returns the label of the location object (see L<FS::cust_location>).
3362 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
3364 =item tax_locationnum
3366 Returns the foreign key to a L<FS::cust_location> object for calculating
3367 tax on this package, as determined by the C<tax-pkg_address> and
3368 C<tax-ship_address> configuration flags.
3372 sub tax_locationnum {
3374 my $conf = FS::Conf->new;
3375 if ( $conf->exists('tax-pkg_address') ) {
3376 return $self->locationnum;
3378 elsif ( $conf->exists('tax-ship_address') ) {
3379 return $self->cust_main->ship_locationnum;
3382 return $self->cust_main->bill_locationnum;
3388 Returns the L<FS::cust_location> object for tax_locationnum.
3394 FS::cust_location->by_key( $self->tax_locationnum )
3397 =item seconds_since TIMESTAMP
3399 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
3400 package have been online since TIMESTAMP, according to the session monitor.
3402 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
3403 L<Time::Local> and L<Date::Parse> for conversion functions.
3408 my($self, $since) = @_;
3411 foreach my $cust_svc (
3412 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
3414 $seconds += $cust_svc->seconds_since($since);
3421 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
3423 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
3424 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
3427 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3428 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3434 sub seconds_since_sqlradacct {
3435 my($self, $start, $end) = @_;
3439 foreach my $cust_svc (
3441 my $part_svc = $_->part_svc;
3442 $part_svc->svcdb eq 'svc_acct'
3443 && scalar($part_svc->part_export_usage);
3446 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
3453 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
3455 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
3456 in this package for sessions ending between TIMESTAMP_START (inclusive) and
3460 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3461 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3466 sub attribute_since_sqlradacct {
3467 my($self, $start, $end, $attrib) = @_;
3471 foreach my $cust_svc (
3473 my $part_svc = $_->part_svc;
3474 scalar($part_svc->part_export_usage);
3477 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
3489 my( $self, $value ) = @_;
3490 if ( defined($value) ) {
3491 $self->setfield('quantity', $value);
3493 $self->getfield('quantity') || 1;
3496 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
3498 Transfers as many services as possible from this package to another package.
3500 The destination package can be specified by pkgnum by passing an FS::cust_pkg
3501 object. The destination package must already exist.
3503 Services are moved only if the destination allows services with the correct
3504 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
3505 this option with caution! No provision is made for export differences
3506 between the old and new service definitions. Probably only should be used
3507 when your exports for all service definitions of a given svcdb are identical.
3508 (attempt a transfer without it first, to move all possible svcpart-matching
3511 Any services that can't be moved remain in the original package.
3513 Returns an error, if there is one; otherwise, returns the number of services
3514 that couldn't be moved.
3519 my ($self, $dest_pkgnum, %opt) = @_;
3525 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
3526 $dest = $dest_pkgnum;
3527 $dest_pkgnum = $dest->pkgnum;
3529 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
3532 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
3534 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
3535 $target{$pkg_svc->svcpart} = $pkg_svc->quantity;
3538 foreach my $cust_svc ($dest->cust_svc) {
3539 $target{$cust_svc->svcpart}--;
3542 my %svcpart2svcparts = ();
3543 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3544 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
3545 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
3546 next if exists $svcpart2svcparts{$svcpart};
3547 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
3548 $svcpart2svcparts{$svcpart} = [
3550 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
3552 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
3553 'svcpart' => $_ } );
3555 $pkg_svc ? $pkg_svc->primary_svc : '',
3556 $pkg_svc ? $pkg_svc->quantity : 0,
3560 grep { $_ != $svcpart }
3562 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
3564 warn "alternates for svcpart $svcpart: ".
3565 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
3571 foreach my $cust_svc ($self->cust_svc) {
3572 my $svcnum = $cust_svc->svcnum;
3573 if($target{$cust_svc->svcpart} > 0
3574 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
3575 $target{$cust_svc->svcpart}--;
3576 my $new = new FS::cust_svc { $cust_svc->hash };
3577 $new->pkgnum($dest_pkgnum);
3578 $error = $new->replace($cust_svc);
3579 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3581 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
3582 warn "alternates to consider: ".
3583 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
3585 my @alternate = grep {
3586 warn "considering alternate svcpart $_: ".
3587 "$target{$_} available in new package\n"
3590 } @{$svcpart2svcparts{$cust_svc->svcpart}};
3592 warn "alternate(s) found\n" if $DEBUG;
3593 my $change_svcpart = $alternate[0];
3594 $target{$change_svcpart}--;
3595 my $new = new FS::cust_svc { $cust_svc->hash };
3596 $new->svcpart($change_svcpart);
3597 $new->pkgnum($dest_pkgnum);
3598 $error = $new->replace($cust_svc);
3606 my @label = $cust_svc->label;
3607 return "service $label[1]: $error";
3613 =item grab_svcnums SVCNUM, SVCNUM ...
3615 Change the pkgnum for the provided services to this packages. If there is an
3616 error, returns the error, otherwise returns false.
3624 local $SIG{HUP} = 'IGNORE';
3625 local $SIG{INT} = 'IGNORE';
3626 local $SIG{QUIT} = 'IGNORE';
3627 local $SIG{TERM} = 'IGNORE';
3628 local $SIG{TSTP} = 'IGNORE';
3629 local $SIG{PIPE} = 'IGNORE';
3631 my $oldAutoCommit = $FS::UID::AutoCommit;
3632 local $FS::UID::AutoCommit = 0;
3635 foreach my $svcnum (@svcnum) {
3636 my $cust_svc = qsearchs('cust_svc', { svcnum=>$svcnum } ) or do {
3637 $dbh->rollback if $oldAutoCommit;
3638 return "unknown svcnum $svcnum";
3640 $cust_svc->pkgnum( $self->pkgnum );
3641 my $error = $cust_svc->replace;
3643 $dbh->rollback if $oldAutoCommit;
3648 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3655 This method is deprecated. See the I<depend_jobnum> option to the insert and
3656 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
3663 local $SIG{HUP} = 'IGNORE';
3664 local $SIG{INT} = 'IGNORE';
3665 local $SIG{QUIT} = 'IGNORE';
3666 local $SIG{TERM} = 'IGNORE';
3667 local $SIG{TSTP} = 'IGNORE';
3668 local $SIG{PIPE} = 'IGNORE';
3670 my $oldAutoCommit = $FS::UID::AutoCommit;
3671 local $FS::UID::AutoCommit = 0;
3674 foreach my $cust_svc ( $self->cust_svc ) {
3675 #false laziness w/svc_Common::insert
3676 my $svc_x = $cust_svc->svc_x;
3677 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
3678 my $error = $part_export->export_insert($svc_x);
3680 $dbh->rollback if $oldAutoCommit;
3686 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3691 =item export_pkg_change OLD_CUST_PKG
3693 Calls the "pkg_change" export action for all services attached to this package.
3697 sub export_pkg_change {
3698 my( $self, $old ) = ( shift, shift );
3700 local $SIG{HUP} = 'IGNORE';
3701 local $SIG{INT} = 'IGNORE';
3702 local $SIG{QUIT} = 'IGNORE';
3703 local $SIG{TERM} = 'IGNORE';
3704 local $SIG{TSTP} = 'IGNORE';
3705 local $SIG{PIPE} = 'IGNORE';
3707 my $oldAutoCommit = $FS::UID::AutoCommit;
3708 local $FS::UID::AutoCommit = 0;
3711 foreach my $svc_x ( map $_->svc_x, $self->cust_svc ) {
3712 my $error = $svc_x->export('pkg_change', $self, $old);
3714 $dbh->rollback if $oldAutoCommit;
3719 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3726 Associates this package with a (suspension or cancellation) reason (see
3727 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
3730 Available options are:
3736 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.
3740 the access_user (see L<FS::access_user>) providing the reason
3748 the action (cancel, susp, adjourn, expire) associated with the reason
3752 If there is an error, returns the error, otherwise returns false.
3757 my ($self, %options) = @_;
3759 my $otaker = $options{reason_otaker} ||
3760 $FS::CurrentUser::CurrentUser->username;
3763 if ( $options{'reason'} =~ /^(\d+)$/ ) {
3767 } elsif ( ref($options{'reason'}) ) {
3769 return 'Enter a new reason (or select an existing one)'
3770 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
3772 my $reason = new FS::reason({
3773 'reason_type' => $options{'reason'}->{'typenum'},
3774 'reason' => $options{'reason'}->{'reason'},
3776 my $error = $reason->insert;
3777 return $error if $error;
3779 $reasonnum = $reason->reasonnum;
3782 return "Unparsable reason: ". $options{'reason'};
3785 my $cust_pkg_reason =
3786 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
3787 'reasonnum' => $reasonnum,
3788 'otaker' => $otaker,
3789 'action' => substr(uc($options{'action'}),0,1),
3790 'date' => $options{'date'}
3795 $cust_pkg_reason->insert;
3798 =item insert_discount
3800 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
3801 inserting a new discount on the fly (see L<FS::discount>).
3803 Available options are:
3811 If there is an error, returns the error, otherwise returns false.
3815 sub insert_discount {
3816 #my ($self, %options) = @_;
3819 my $cust_pkg_discount = new FS::cust_pkg_discount {
3820 'pkgnum' => $self->pkgnum,
3821 'discountnum' => $self->discountnum,
3823 'end_date' => '', #XXX
3824 #for the create a new discount case
3825 '_type' => $self->discountnum__type,
3826 'amount' => $self->discountnum_amount,
3827 'percent' => $self->discountnum_percent,
3828 'months' => $self->discountnum_months,
3829 'setup' => $self->discountnum_setup,
3830 #'disabled' => $self->discountnum_disabled,
3833 $cust_pkg_discount->insert;
3836 =item set_usage USAGE_VALUE_HASHREF
3838 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3839 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3840 upbytes, downbytes, and totalbytes are appropriate keys.
3842 All svc_accts which are part of this package have their values reset.
3847 my ($self, $valueref, %opt) = @_;
3849 #only svc_acct can set_usage for now
3850 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3851 my $svc_x = $cust_svc->svc_x;
3852 $svc_x->set_usage($valueref, %opt)
3853 if $svc_x->can("set_usage");
3857 =item recharge USAGE_VALUE_HASHREF
3859 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3860 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3861 upbytes, downbytes, and totalbytes are appropriate keys.
3863 All svc_accts which are part of this package have their values incremented.
3868 my ($self, $valueref) = @_;
3870 #only svc_acct can set_usage for now
3871 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3872 my $svc_x = $cust_svc->svc_x;
3873 $svc_x->recharge($valueref)
3874 if $svc_x->can("recharge");
3878 =item cust_pkg_discount
3882 sub cust_pkg_discount {
3884 qsearch('cust_pkg_discount', { 'pkgnum' => $self->pkgnum } );
3887 =item cust_pkg_discount_active
3891 sub cust_pkg_discount_active {
3893 grep { $_->status eq 'active' } $self->cust_pkg_discount;
3896 =item cust_pkg_usage
3898 Returns a list of all voice usage counters attached to this package.
3902 sub cust_pkg_usage {
3904 qsearch('cust_pkg_usage', { pkgnum => $self->pkgnum });
3907 =item apply_usage OPTIONS
3909 Takes the following options:
3910 - cdr: a call detail record (L<FS::cdr>)
3911 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
3912 - minutes: the maximum number of minutes to be charged
3914 Finds available usage minutes for a call of this class, and subtracts
3915 up to that many minutes from the usage pool. If the usage pool is empty,
3916 and the C<cdr-minutes_priority> global config option is set, minutes may
3917 be taken from other calls as well. Either way, an allocation record will
3918 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
3919 number of minutes of usage applied to the call.
3924 my ($self, %opt) = @_;
3925 my $cdr = $opt{cdr};
3926 my $rate_detail = $opt{rate_detail};
3927 my $minutes = $opt{minutes};
3928 my $classnum = $rate_detail->classnum;
3929 my $pkgnum = $self->pkgnum;
3930 my $custnum = $self->custnum;
3932 local $SIG{HUP} = 'IGNORE';
3933 local $SIG{INT} = 'IGNORE';
3934 local $SIG{QUIT} = 'IGNORE';
3935 local $SIG{TERM} = 'IGNORE';
3936 local $SIG{TSTP} = 'IGNORE';
3937 local $SIG{PIPE} = 'IGNORE';
3939 my $oldAutoCommit = $FS::UID::AutoCommit;
3940 local $FS::UID::AutoCommit = 0;
3942 my $order = FS::Conf->new->config('cdr-minutes_priority');
3946 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
3948 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
3950 my @usage_recs = qsearch({
3951 'table' => 'cust_pkg_usage',
3952 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
3953 ' JOIN cust_pkg USING (pkgnum)'.
3954 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
3955 'select' => 'cust_pkg_usage.*',
3956 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
3957 " ( cust_pkg.custnum = $custnum AND ".
3958 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3959 $is_classnum . ' AND '.
3960 " cust_pkg_usage.minutes > 0",
3961 'order_by' => " ORDER BY priority ASC",
3964 my $orig_minutes = $minutes;
3966 while (!$error and $minutes > 0 and @usage_recs) {
3967 my $cust_pkg_usage = shift @usage_recs;
3968 $cust_pkg_usage->select_for_update;
3969 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
3970 pkgusagenum => $cust_pkg_usage->pkgusagenum,
3971 acctid => $cdr->acctid,
3972 minutes => min($cust_pkg_usage->minutes, $minutes),
3974 $cust_pkg_usage->set('minutes',
3975 sprintf('%.0f', $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes)
3977 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
3978 $minutes -= $cdr_cust_pkg_usage->minutes;
3980 if ( $order and $minutes > 0 and !$error ) {
3981 # then try to steal minutes from another call
3983 'table' => 'cdr_cust_pkg_usage',
3984 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
3985 ' JOIN part_pkg_usage USING (pkgusagepart)'.
3986 ' JOIN cust_pkg USING (pkgnum)'.
3987 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
3988 ' JOIN cdr USING (acctid)',
3989 'select' => 'cdr_cust_pkg_usage.*',
3990 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
3991 " ( cust_pkg.pkgnum = $pkgnum OR ".
3992 " ( cust_pkg.custnum = $custnum AND ".
3993 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3994 " part_pkg_usage_class.classnum = $classnum",
3995 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
3997 if ( $order eq 'time' ) {
3998 # find CDRs that are using minutes, but have a later startdate
4000 my $startdate = $cdr->startdate;
4001 if ($startdate !~ /^\d+$/) {
4002 die "bad cdr startdate '$startdate'";
4004 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
4005 # minimize needless reshuffling
4006 $search{'order_by'} .= ', cdr.startdate DESC';
4008 # XXX may not work correctly with rate_time schedules. Could
4009 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
4011 $search{'addl_from'} .=
4012 ' JOIN rate_detail'.
4013 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
4014 if ( $order eq 'rate_high' ) {
4015 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
4016 $rate_detail->min_charge;
4017 $search{'order_by'} .= ', rate_detail.min_charge ASC';
4018 } elsif ( $order eq 'rate_low' ) {
4019 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
4020 $rate_detail->min_charge;
4021 $search{'order_by'} .= ', rate_detail.min_charge DESC';
4023 # this should really never happen
4024 die "invalid cdr-minutes_priority value '$order'\n";
4027 my @cdr_usage_recs = qsearch(\%search);
4029 while (!$error and @cdr_usage_recs and $minutes > 0) {
4030 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
4031 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
4032 my $old_cdr = $cdr_cust_pkg_usage->cdr;
4033 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
4034 $cdr_cust_pkg_usage->select_for_update;
4035 $old_cdr->select_for_update;
4036 $cust_pkg_usage->select_for_update;
4037 # in case someone else stole the usage from this CDR
4038 # while waiting for the lock...
4039 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
4040 # steal the usage allocation and flag the old CDR for reprocessing
4041 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
4042 # if the allocation is more minutes than we need, adjust it...
4043 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
4045 $cdr_cust_pkg_usage->set('minutes', $minutes);
4046 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
4047 $error = $cust_pkg_usage->replace;
4049 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
4050 $error ||= $cdr_cust_pkg_usage->replace;
4051 # deduct the stolen minutes
4052 $minutes -= $cdr_cust_pkg_usage->minutes;
4054 # after all minute-stealing is done, reset the affected CDRs
4055 foreach (values %reproc_cdrs) {
4056 $error ||= $_->set_status('');
4057 # XXX or should we just call $cdr->rate right here?
4058 # it's not like we can create a loop this way, since the min_charge
4059 # or call time has to go monotonically in one direction.
4060 # we COULD get some very deep recursions going, though...
4062 } # if $order and $minutes
4065 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
4067 $dbh->commit if $oldAutoCommit;
4068 return $orig_minutes - $minutes;
4072 =item supplemental_pkgs
4074 Returns a list of all packages supplemental to this one.
4078 sub supplemental_pkgs {
4080 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
4085 Returns the package that this one is supplemental to, if any.
4091 if ( $self->main_pkgnum ) {
4092 return FS::cust_pkg->by_key($self->main_pkgnum);
4099 =head1 CLASS METHODS
4105 Returns an SQL expression identifying recurring packages.
4109 sub recurring_sql { "
4110 '0' != ( select freq from part_pkg
4111 where cust_pkg.pkgpart = part_pkg.pkgpart )
4116 Returns an SQL expression identifying one-time packages.
4121 '0' = ( select freq from part_pkg
4122 where cust_pkg.pkgpart = part_pkg.pkgpart )
4127 Returns an SQL expression identifying ordered packages (recurring packages not
4133 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
4138 Returns an SQL expression identifying active packages.
4143 $_[0]->recurring_sql. "
4144 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4145 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4146 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4149 =item not_yet_billed_sql
4151 Returns an SQL expression identifying packages which have not yet been billed.
4155 sub not_yet_billed_sql { "
4156 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4157 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4158 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4163 Returns an SQL expression identifying inactive packages (one-time packages
4164 that are otherwise unsuspended/uncancelled).
4168 sub inactive_sql { "
4169 ". $_[0]->onetime_sql(). "
4170 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4171 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4172 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4178 Returns an SQL expression identifying suspended packages.
4182 sub suspended_sql { susp_sql(@_); }
4184 #$_[0]->recurring_sql(). ' AND '.
4186 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4187 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4194 Returns an SQL exprression identifying cancelled packages.
4198 sub cancelled_sql { cancel_sql(@_); }
4200 #$_[0]->recurring_sql(). ' AND '.
4201 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
4206 Returns an SQL expression to give the package status as a string.
4212 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
4213 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
4214 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
4215 WHEN ".onetime_sql()." THEN 'one-time charge'
4220 =item search HASHREF
4224 Returns a qsearch hash expression to search for parameters specified in HASHREF.
4225 Valid parameters are
4233 active, inactive, suspended, cancel (or cancelled)
4237 active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
4241 boolean selects custom packages
4247 pkgpart or arrayref or hashref of pkgparts
4251 arrayref of beginning and ending epoch date
4255 arrayref of beginning and ending epoch date
4259 arrayref of beginning and ending epoch date
4263 arrayref of beginning and ending epoch date
4267 arrayref of beginning and ending epoch date
4271 arrayref of beginning and ending epoch date
4275 arrayref of beginning and ending epoch date
4279 pkgnum or APKG_pkgnum
4283 a value suited to passing to FS::UI::Web::cust_header
4287 specifies the user for agent virtualization
4291 boolean; if true, returns only packages with more than 0 FCC phone lines.
4293 =item state, country
4295 Limit to packages with a service location in the specified state and country.
4296 For FCC 477 reporting, mostly.
4300 Limit to packages whose service locations are the same as the customer's
4301 default service location.
4303 =item location_nocust
4305 Limit to packages whose service locations are not the customer's default
4308 =item location_census
4310 Limit to packages whose service locations have census tracts.
4312 =item location_nocensus
4314 Limit to packages whose service locations do not have a census tract.
4316 =item location_geocode
4318 Limit to packages whose locations have geocodes.
4320 =item location_geocode
4322 Limit to packages whose locations do not have geocodes.
4329 my ($class, $params) = @_;
4336 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
4338 "cust_main.agentnum = $1";
4345 if ( $params->{'cust_status'} =~ /^([a-z]+)$/ ) {
4346 push @where, FS::cust_main->cust_status_sql . " = '$1' ";
4350 # parse customer sales person
4353 if ( $params->{'cust_main_salesnum'} =~ /^(\d+)$/ ) {
4354 push @where, ($1 > 0) ? "cust_main.salesnum = $1"
4355 : 'cust_main.salesnum IS NULL';
4360 # parse sales person
4363 if ( $params->{'salesnum'} =~ /^(\d+)$/ ) {
4364 push @where, ($1 > 0) ? "cust_pkg.salesnum = $1"
4365 : 'cust_pkg.salesnum IS NULL';
4372 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
4374 "cust_pkg.custnum = $1";
4381 if ( $params->{'pkgbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
4383 "cust_pkg.pkgbatch = '$1'";
4390 if ( $params->{'magic'} eq 'active'
4391 || $params->{'status'} eq 'active' ) {
4393 push @where, FS::cust_pkg->active_sql();
4395 } elsif ( $params->{'magic'} =~ /^not[ _]yet[ _]billed$/
4396 || $params->{'status'} =~ /^not[ _]yet[ _]billed$/ ) {
4398 push @where, FS::cust_pkg->not_yet_billed_sql();
4400 } elsif ( $params->{'magic'} =~ /^(one-time charge|inactive)/
4401 || $params->{'status'} =~ /^(one-time charge|inactive)/ ) {
4403 push @where, FS::cust_pkg->inactive_sql();
4405 } elsif ( $params->{'magic'} eq 'suspended'
4406 || $params->{'status'} eq 'suspended' ) {
4408 push @where, FS::cust_pkg->suspended_sql();
4410 } elsif ( $params->{'magic'} =~ /^cancell?ed$/
4411 || $params->{'status'} =~ /^cancell?ed$/ ) {
4413 push @where, FS::cust_pkg->cancelled_sql();
4418 # parse package class
4421 if ( exists($params->{'classnum'}) ) {
4424 if ( ref($params->{'classnum'}) ) {
4426 if ( ref($params->{'classnum'}) eq 'HASH' ) {
4427 @classnum = grep $params->{'classnum'}{$_}, keys %{ $params->{'classnum'} };
4428 } elsif ( ref($params->{'classnum'}) eq 'ARRAY' ) {
4429 @classnum = @{ $params->{'classnum'} };
4431 die 'unhandled classnum ref '. $params->{'classnum'};
4435 } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
4442 my @nums = grep $_, @classnum;
4443 push @c_where, 'part_pkg.classnum IN ('. join(',',@nums). ')' if @nums;
4444 my $null = scalar( grep { $_ eq '' } @classnum );
4445 push @c_where, 'part_pkg.classnum IS NULL' if $null;
4447 if ( scalar(@c_where) == 1 ) {
4448 push @where, @c_where;
4449 } elsif ( @c_where ) {
4450 push @where, ' ( '. join(' OR ', @c_where). ' ) ';
4459 # parse package report options
4462 my @report_option = ();
4463 if ( exists($params->{'report_option'}) ) {
4464 if ( ref($params->{'report_option'}) eq 'ARRAY' ) {
4465 @report_option = @{ $params->{'report_option'} };
4466 } elsif ( $params->{'report_option'} =~ /^([,\d]*)$/ ) {
4467 @report_option = split(',', $1);
4472 if (@report_option) {
4473 # this will result in the empty set for the dangling comma case as it should
4475 map{ "0 < ( SELECT count(*) FROM part_pkg_option
4476 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
4477 AND optionname = 'report_option_$_'
4478 AND optionvalue = '1' )"
4482 foreach my $any ( grep /^report_option_any/, keys %$params ) {
4484 my @report_option_any = ();
4485 if ( ref($params->{$any}) eq 'ARRAY' ) {
4486 @report_option_any = @{ $params->{$any} };
4487 } elsif ( $params->{$any} =~ /^([,\d]*)$/ ) {
4488 @report_option_any = split(',', $1);
4491 if (@report_option_any) {
4492 # this will result in the empty set for the dangling comma case as it should
4493 push @where, ' ( '. join(' OR ',
4494 map{ "0 < ( SELECT count(*) FROM part_pkg_option
4495 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
4496 AND optionname = 'report_option_$_'
4497 AND optionvalue = '1' )"
4498 } @report_option_any
4508 push @where, "part_pkg.custom = 'Y'" if $params->{custom};
4514 push @where, "(part_pkg.fcc_ds0s > 0 OR pkg_class.fcc_ds0s > 0)"
4515 if $params->{fcc_line};
4521 if ( exists($params->{'censustract'}) ) {
4522 $params->{'censustract'} =~ /^([.\d]*)$/;
4523 my $censustract = "cust_location.censustract = '$1'";
4524 $censustract .= ' OR cust_location.censustract is NULL' unless $1;
4525 push @where, "( $censustract )";
4529 # parse censustract2
4531 if ( exists($params->{'censustract2'})
4532 && $params->{'censustract2'} =~ /^(\d*)$/
4536 push @where, "cust_location.censustract LIKE '$1%'";
4539 "( cust_location.censustract = '' OR cust_location.censustract IS NULL )";
4544 # parse country/state
4546 for (qw(state country)) { # parsing rules are the same for these
4547 if ( exists($params->{$_})
4548 && uc($params->{$_}) =~ /^([A-Z]{2})$/ )
4550 # XXX post-2.3 only--before that, state/country may be in cust_main
4551 push @where, "cust_location.$_ = '$1'";
4558 if ( $params->{location_cust} xor $params->{location_nocust} ) {
4559 my $op = $params->{location_cust} ? '=' : '!=';
4560 push @where, "cust_location.locationnum $op cust_main.ship_locationnum";
4562 if ( $params->{location_census} xor $params->{location_nocensus} ) {
4563 my $op = $params->{location_census} ? "IS NOT NULL" : "IS NULL";
4564 push @where, "cust_location.censustract $op";
4566 if ( $params->{location_geocode} xor $params->{location_nogeocode} ) {
4567 my $op = $params->{location_geocode} ? "IS NOT NULL" : "IS NULL";
4568 push @where, "cust_location.geocode $op";
4575 if ( ref($params->{'pkgpart'}) ) {
4578 if ( ref($params->{'pkgpart'}) eq 'HASH' ) {
4579 @pkgpart = grep $params->{'pkgpart'}{$_}, keys %{ $params->{'pkgpart'} };
4580 } elsif ( ref($params->{'pkgpart'}) eq 'ARRAY' ) {
4581 @pkgpart = @{ $params->{'pkgpart'} };
4583 die 'unhandled pkgpart ref '. $params->{'pkgpart'};
4586 @pkgpart = grep /^(\d+)$/, @pkgpart;
4588 push @where, 'pkgpart IN ('. join(',', @pkgpart). ')' if scalar(@pkgpart);
4590 } elsif ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
4591 push @where, "pkgpart = $1";
4600 #false laziness w/report_cust_pkg.html
4603 'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
4604 'active' => { 'susp'=>1, 'cancel'=>1 },
4605 'suspended' => { 'cancel' => 1 },
4610 if( exists($params->{'active'} ) ) {
4611 # This overrides all the other date-related fields
4612 my($beginning, $ending) = @{$params->{'active'}};
4614 "cust_pkg.setup IS NOT NULL",
4615 "cust_pkg.setup <= $ending",
4616 "(cust_pkg.cancel IS NULL OR cust_pkg.cancel >= $beginning )",
4617 "NOT (".FS::cust_pkg->onetime_sql . ")";
4620 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel )) {
4622 next unless exists($params->{$field});
4624 my($beginning, $ending) = @{$params->{$field}};
4626 next if $beginning == 0 && $ending == 4294967295;
4629 "cust_pkg.$field IS NOT NULL",
4630 "cust_pkg.$field >= $beginning",
4631 "cust_pkg.$field <= $ending";
4633 $orderby ||= "ORDER BY cust_pkg.$field";
4638 $orderby ||= 'ORDER BY bill';
4641 # parse magic, legacy, etc.
4644 if ( $params->{'magic'} &&
4645 $params->{'magic'} =~ /^(active|inactive|suspended|cancell?ed)$/
4648 $orderby = 'ORDER BY pkgnum';
4650 if ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
4651 push @where, "pkgpart = $1";
4654 } elsif ( $params->{'query'} eq 'pkgnum' ) {
4656 $orderby = 'ORDER BY pkgnum';
4658 } elsif ( $params->{'query'} eq 'APKG_pkgnum' ) {
4660 $orderby = 'ORDER BY pkgnum';
4663 SELECT count(*) FROM pkg_svc
4664 WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
4665 AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
4666 WHERE cust_svc.pkgnum = cust_pkg.pkgnum
4667 AND cust_svc.svcpart = pkg_svc.svcpart
4674 # setup queries, links, subs, etc. for the search
4677 # here is the agent virtualization
4678 if ($params->{CurrentUser}) {
4680 qsearchs('access_user', { username => $params->{CurrentUser} });
4683 push @where, $access_user->agentnums_sql('table'=>'cust_main');
4688 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql('table'=>'cust_main');
4691 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
4693 my $addl_from = 'LEFT JOIN part_pkg USING ( pkgpart ) '.
4694 'LEFT JOIN pkg_class ON ( part_pkg.classnum = pkg_class.classnum ) '.
4695 'LEFT JOIN cust_location USING ( locationnum ) '.
4696 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
4700 if ( $params->{'select_zip5'} ) {
4701 my $zip = 'cust_location.zip';
4703 $select = "DISTINCT substr($zip,1,5) as zip";
4704 $orderby = "ORDER BY substr($zip,1,5)";
4705 $count_query = "SELECT COUNT( DISTINCT substr($zip,1,5) )";
4707 $select = join(', ',
4709 ( map "part_pkg.$_", qw( pkg freq ) ),
4710 'pkg_class.classname',
4711 'cust_main.custnum AS cust_main_custnum',
4712 FS::UI::Web::cust_sql_fields(
4713 $params->{'cust_fields'}
4716 $count_query = 'SELECT COUNT(*)';
4719 $count_query .= " FROM cust_pkg $addl_from $extra_sql";
4722 'table' => 'cust_pkg',
4724 'select' => $select,
4725 'extra_sql' => $extra_sql,
4726 'order_by' => $orderby,
4727 'addl_from' => $addl_from,
4728 'count_query' => $count_query,
4735 Returns a list of two package counts. The first is a count of packages
4736 based on the supplied criteria and the second is the count of residential
4737 packages with those same criteria. Criteria are specified as in the search
4743 my ($class, $params) = @_;
4745 my $sql_query = $class->search( $params );
4747 my $count_sql = delete($sql_query->{'count_query'});
4748 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
4749 or die "couldn't parse count_sql";
4751 my $count_sth = dbh->prepare($count_sql)
4752 or die "Error preparing $count_sql: ". dbh->errstr;
4754 or die "Error executing $count_sql: ". $count_sth->errstr;
4755 my $count_arrayref = $count_sth->fetchrow_arrayref;
4757 return ( @$count_arrayref );
4761 =item tax_locationnum_sql
4763 Returns an SQL expression for the tax location for a package, based
4764 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
4768 sub tax_locationnum_sql {
4769 my $conf = FS::Conf->new;
4770 if ( $conf->exists('tax-pkg_address') ) {
4771 'cust_pkg.locationnum';
4773 elsif ( $conf->exists('tax-ship_address') ) {
4774 'cust_main.ship_locationnum';
4777 'cust_main.bill_locationnum';
4783 Returns a list: the first item is an SQL fragment identifying matching
4784 packages/customers via location (taking into account shipping and package
4785 address taxation, if enabled), and subsequent items are the parameters to
4786 substitute for the placeholders in that fragment.
4791 my($class, %opt) = @_;
4792 my $ornull = $opt{'ornull'};
4794 my $conf = new FS::Conf;
4796 # '?' placeholders in _location_sql_where
4797 my $x = $ornull ? 3 : 2;
4808 if ( $conf->exists('tax-ship_address') ) {
4811 ( ( ship_last IS NULL OR ship_last = '' )
4812 AND ". _location_sql_where('cust_main', '', $ornull ). "
4814 OR ( ship_last IS NOT NULL AND ship_last != ''
4815 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
4818 # AND payby != 'COMP'
4820 @main_param = ( @bill_param, @bill_param );
4824 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
4825 @main_param = @bill_param;
4831 if ( $conf->exists('tax-pkg_address') ) {
4833 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
4836 ( cust_pkg.locationnum IS NULL AND $main_where )
4837 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
4840 @param = ( @main_param, @bill_param );
4844 $where = $main_where;
4845 @param = @main_param;
4853 #subroutine, helper for location_sql
4854 sub _location_sql_where {
4856 my $prefix = @_ ? shift : '';
4857 my $ornull = @_ ? shift : '';
4859 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
4861 $ornull = $ornull ? ' OR ? IS NULL ' : '';
4863 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
4864 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
4865 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
4867 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
4869 # ( $table.${prefix}city = ? $or_empty_city $ornull )
4871 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4872 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4873 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
4874 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
4875 AND $table.${prefix}country = ?
4880 my( $self, $what ) = @_;
4882 my $what_show_zero = $what. '_show_zero';
4883 length($self->$what_show_zero())
4884 ? ($self->$what_show_zero() eq 'Y')
4885 : $self->part_pkg->$what_show_zero();
4892 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
4894 CUSTNUM is a customer (see L<FS::cust_main>)
4896 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4897 L<FS::part_pkg>) to order for this customer. Duplicates are of course
4900 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
4901 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
4902 new billing items. An error is returned if this is not possible (see
4903 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
4906 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4907 newly-created cust_pkg objects.
4909 REFNUM, if specified, will specify the FS::pkg_referral record to be created
4910 and inserted. Multiple FS::pkg_referral records can be created by
4911 setting I<refnum> to an array reference of refnums or a hash reference with
4912 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
4913 record will be created corresponding to cust_main.refnum.
4918 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
4920 my $conf = new FS::Conf;
4922 # Transactionize this whole mess
4923 local $SIG{HUP} = 'IGNORE';
4924 local $SIG{INT} = 'IGNORE';
4925 local $SIG{QUIT} = 'IGNORE';
4926 local $SIG{TERM} = 'IGNORE';
4927 local $SIG{TSTP} = 'IGNORE';
4928 local $SIG{PIPE} = 'IGNORE';
4930 my $oldAutoCommit = $FS::UID::AutoCommit;
4931 local $FS::UID::AutoCommit = 0;
4935 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
4936 # return "Customer not found: $custnum" unless $cust_main;
4938 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
4941 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4944 my $change = scalar(@old_cust_pkg) != 0;
4947 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
4949 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
4950 " to pkgpart ". $pkgparts->[0]. "\n"
4953 my $err_or_cust_pkg =
4954 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
4955 'refnum' => $refnum,
4958 unless (ref($err_or_cust_pkg)) {
4959 $dbh->rollback if $oldAutoCommit;
4960 return $err_or_cust_pkg;
4963 push @$return_cust_pkg, $err_or_cust_pkg;
4964 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4969 # Create the new packages.
4970 foreach my $pkgpart (@$pkgparts) {
4972 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
4974 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
4975 pkgpart => $pkgpart,
4979 $error = $cust_pkg->insert( 'change' => $change );
4980 push @$return_cust_pkg, $cust_pkg;
4982 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
4983 my $supp_pkg = FS::cust_pkg->new({
4984 custnum => $custnum,
4985 pkgpart => $link->dst_pkgpart,
4987 main_pkgnum => $cust_pkg->pkgnum,
4990 $error ||= $supp_pkg->insert( 'change' => $change );
4991 push @$return_cust_pkg, $supp_pkg;
4995 $dbh->rollback if $oldAutoCommit;
5000 # $return_cust_pkg now contains refs to all of the newly
5003 # Transfer services and cancel old packages.
5004 foreach my $old_pkg (@old_cust_pkg) {
5006 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
5009 foreach my $new_pkg (@$return_cust_pkg) {
5010 $error = $old_pkg->transfer($new_pkg);
5011 if ($error and $error == 0) {
5012 # $old_pkg->transfer failed.
5013 $dbh->rollback if $oldAutoCommit;
5018 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
5019 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
5020 foreach my $new_pkg (@$return_cust_pkg) {
5021 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
5022 if ($error and $error == 0) {
5023 # $old_pkg->transfer failed.
5024 $dbh->rollback if $oldAutoCommit;
5031 # Transfers were successful, but we went through all of the
5032 # new packages and still had services left on the old package.
5033 # We can't cancel the package under the circumstances, so abort.
5034 $dbh->rollback if $oldAutoCommit;
5035 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
5037 $error = $old_pkg->cancel( quiet=>1 );
5043 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5047 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
5049 A bulk change method to change packages for multiple customers.
5051 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
5052 L<FS::part_pkg>) to order for each customer. Duplicates are of course
5055 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
5056 replace. The services (see L<FS::cust_svc>) are moved to the
5057 new billing items. An error is returned if this is not possible (see
5060 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
5061 newly-created cust_pkg objects.
5066 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
5068 # Transactionize this whole mess
5069 local $SIG{HUP} = 'IGNORE';
5070 local $SIG{INT} = 'IGNORE';
5071 local $SIG{QUIT} = 'IGNORE';
5072 local $SIG{TERM} = 'IGNORE';
5073 local $SIG{TSTP} = 'IGNORE';
5074 local $SIG{PIPE} = 'IGNORE';
5076 my $oldAutoCommit = $FS::UID::AutoCommit;
5077 local $FS::UID::AutoCommit = 0;
5081 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
5084 while(scalar(@old_cust_pkg)) {
5086 my $custnum = $old_cust_pkg[0]->custnum;
5087 my (@remove) = map { $_->pkgnum }
5088 grep { $_->custnum == $custnum } @old_cust_pkg;
5089 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
5091 my $error = order $custnum, $pkgparts, \@remove, \@return;
5093 push @errors, $error
5095 push @$return_cust_pkg, @return;
5098 if (scalar(@errors)) {
5099 $dbh->rollback if $oldAutoCommit;
5100 return join(' / ', @errors);
5103 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5107 # Used by FS::Upgrade to migrate to a new database.
5108 sub _upgrade_data { # class method
5109 my ($class, %opts) = @_;
5110 $class->_upgrade_otaker(%opts);
5112 # RT#10139, bug resulting in contract_end being set when it shouldn't
5113 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
5114 # RT#10830, bad calculation of prorate date near end of year
5115 # the date range for bill is December 2009, and we move it forward
5116 # one year if it's before the previous bill date (which it should
5118 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
5119 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
5120 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
5121 # RT6628, add order_date to cust_pkg
5122 'update cust_pkg set order_date = (select history_date from h_cust_pkg
5123 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
5124 history_action = \'insert\') where order_date is null',
5126 foreach my $sql (@statements) {
5127 my $sth = dbh->prepare($sql);
5128 $sth->execute or die $sth->errstr;
5136 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
5138 In sub order, the @pkgparts array (passed by reference) is clobbered.
5140 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
5141 method to pass dates to the recur_prog expression, it should do so.
5143 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
5144 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
5145 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
5146 configuration values. Probably need a subroutine which decides what to do
5147 based on whether or not we've fetched the user yet, rather than a hash. See
5148 FS::UID and the TODO.
5150 Now that things are transactional should the check in the insert method be
5155 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
5156 L<FS::pkg_svc>, schema.html from the base documentation