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 );
8 use Scalar::Util qw( blessed );
9 use List::Util qw(min max);
11 use Time::Local qw( timelocal timelocal_nocheck );
13 use FS::UID qw( dbh driver_name );
14 use FS::Misc qw( send_email );
15 use FS::Record qw( qsearch qsearchs fields );
21 use FS::cust_location;
23 use FS::cust_bill_pkg;
24 use FS::cust_pkg_detail;
25 use FS::cust_pkg_usage;
26 use FS::cdr_cust_pkg_usage;
31 use FS::cust_pkg_reason;
33 use FS::cust_pkg_discount;
40 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
42 # because they load configuration by setting FS::UID::callback (see TODO)
48 # for sending cancel emails in sub cancel
51 our ($disable_agentcheck, $DEBUG, $me, $import) = (0, 0, '[FS::cust_pkg]', 0);
53 our $upgrade = 0; #go away after setup+start dates cleaned up for old customers
57 my ( $hashref, $cache ) = @_;
58 #if ( $hashref->{'pkgpart'} ) {
59 if ( $hashref->{'pkg'} ) {
60 # #@{ $self->{'_pkgnum'} } = ();
61 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
62 # $self->{'_pkgpart'} = $subcache;
63 # #push @{ $self->{'_pkgnum'} },
64 # FS::part_pkg->new_or_cached($hashref, $subcache);
65 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
67 if ( exists $hashref->{'svcnum'} ) {
68 #@{ $self->{'_pkgnum'} } = ();
69 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
70 $self->{'_svcnum'} = $subcache;
71 #push @{ $self->{'_pkgnum'} },
72 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
78 FS::cust_pkg - Object methods for cust_pkg objects
84 $record = new FS::cust_pkg \%hash;
85 $record = new FS::cust_pkg { 'column' => 'value' };
87 $error = $record->insert;
89 $error = $new_record->replace($old_record);
91 $error = $record->delete;
93 $error = $record->check;
95 $error = $record->cancel;
97 $error = $record->suspend;
99 $error = $record->unsuspend;
101 $part_pkg = $record->part_pkg;
103 @labels = $record->labels;
105 $seconds = $record->seconds_since($timestamp);
107 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
108 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
112 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
113 inherits from FS::Record. The following fields are currently supported:
119 Primary key (assigned automatically for new billing items)
123 Customer (see L<FS::cust_main>)
127 Billing item definition (see L<FS::part_pkg>)
131 Optional link to package location (see L<FS::location>)
135 date package was ordered (also remains same on changes)
147 date (next bill date)
175 order taker (see L<FS::access_user>)
179 If this field is set to 1, disables the automatic
180 unsuspension of this package when using the B<unsuspendauto> config option.
184 If not set, defaults to 1
188 Date of change from previous package
198 =item change_locationnum
206 The pkgnum of the package that this package is supplemental to, if any.
210 The package link (L<FS::part_pkg_link>) that defines this supplemental
211 package, if it is one.
213 =item change_to_pkgnum
215 The pkgnum of the package this one will be "changed to" in the future
216 (on its expiration date).
220 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
221 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
222 L<Time::Local> and L<Date::Parse> for conversion functions.
230 Create a new billing item. To add the item to the database, see L<"insert">.
234 sub table { 'cust_pkg'; }
235 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
236 sub cust_unlinked_msg {
238 "WARNING: can't find cust_main.custnum ". $self->custnum.
239 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
242 =item insert [ OPTION => VALUE ... ]
244 Adds this billing item to the database ("Orders" the item). If there is an
245 error, returns the error, otherwise returns false.
247 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
248 will be used to look up the package definition and agent restrictions will be
251 If the additional field I<refnum> is defined, an FS::pkg_referral record will
252 be created and inserted. Multiple FS::pkg_referral records can be created by
253 setting I<refnum> to an array reference of refnums or a hash reference with
254 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
255 record will be created corresponding to cust_main.refnum.
257 The following options are available:
263 If set true, supresses actions that should only be taken for new package
264 orders. (Currently this includes: intro periods when delay_setup is on.)
268 cust_pkg_option records will be created
270 =item cust_pkg_usageprice
272 Array reference of cust_pkg_usageprice objects, will be inserted
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;
302 # if the package def says to start only on the first of the month:
303 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
304 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
305 $mon += 1 unless $mday == 1;
306 until ( $mon < 12 ) { $mon -= 12; $year++; }
307 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
310 # set up any automatic expire/adjourn/contract_end timers
311 # based on the start date
312 foreach my $action ( qw(expire adjourn contract_end) ) {
313 my $months = $part_pkg->option("${action}_months",1);
314 if($months and !$self->$action) {
315 my $start = $self->start_date || $self->setup || time;
316 $self->$action( $part_pkg->add_freq($start, $months) );
320 # if this package has "free days" and delayed setup fee, tehn
321 # set start date that many days in the future.
322 # (this should have been set in the UI, but enforce it here)
323 if ( ! $options{'change'}
324 && ( my $free_days = $part_pkg->option('free_days',1) )
325 && $part_pkg->option('delay_setup',1)
326 #&& ! $self->start_date
329 $self->start_date( $part_pkg->default_start_date );
333 # set order date unless it was specified as part of an import
334 $self->order_date(time) unless $import && $self->order_date;
336 local $SIG{HUP} = 'IGNORE';
337 local $SIG{INT} = 'IGNORE';
338 local $SIG{QUIT} = 'IGNORE';
339 local $SIG{TERM} = 'IGNORE';
340 local $SIG{TSTP} = 'IGNORE';
341 local $SIG{PIPE} = 'IGNORE';
343 my $oldAutoCommit = $FS::UID::AutoCommit;
344 local $FS::UID::AutoCommit = 0;
347 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
349 $dbh->rollback if $oldAutoCommit;
353 $self->refnum($self->cust_main->refnum) unless $self->refnum;
354 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
355 $self->process_m2m( 'link_table' => 'pkg_referral',
356 'target_table' => 'part_referral',
357 'params' => $self->refnum,
360 if ( $self->hashref->{cust_pkg_usageprice} ) {
361 for my $cust_pkg_usageprice ( @{ $self->hashref->{cust_pkg_usageprice} } ) {
362 $cust_pkg_usageprice->pkgnum( $self->pkgnum );
363 my $error = $cust_pkg_usageprice->insert;
365 $dbh->rollback if $oldAutoCommit;
371 if ( $self->discountnum ) {
372 my $error = $self->insert_discount();
374 $dbh->rollback if $oldAutoCommit;
379 my $conf = new FS::Conf;
381 if ( ! $import && $conf->config('ticket_system') && $options{ticket_subject} ) {
383 #this init stuff is still inefficient, but at least its limited to
384 # the small number (any?) folks using ticket emailing on pkg order
387 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
394 use FS::TicketSystem;
395 FS::TicketSystem->init();
397 my $q = new RT::Queue($RT::SystemUser);
398 $q->Load($options{ticket_queue}) if $options{ticket_queue};
399 my $t = new RT::Ticket($RT::SystemUser);
400 my $mime = new MIME::Entity;
401 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
402 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
403 Subject => $options{ticket_subject},
406 $t->AddLink( Type => 'MemberOf',
407 Target => 'freeside://freeside/cust_main/'. $self->custnum,
411 if (! $import && $conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
412 my $queue = new FS::queue {
413 'job' => 'FS::cust_main::queueable_print',
415 $error = $queue->insert(
416 'custnum' => $self->custnum,
417 'template' => 'welcome_letter',
421 warn "can't send welcome letter: $error";
426 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
433 This method now works but you probably shouldn't use it.
435 You don't want to delete packages, because there would then be no record
436 the customer ever purchased the package. Instead, see the cancel method and
437 hide cancelled packages.
444 local $SIG{HUP} = 'IGNORE';
445 local $SIG{INT} = 'IGNORE';
446 local $SIG{QUIT} = 'IGNORE';
447 local $SIG{TERM} = 'IGNORE';
448 local $SIG{TSTP} = 'IGNORE';
449 local $SIG{PIPE} = 'IGNORE';
451 my $oldAutoCommit = $FS::UID::AutoCommit;
452 local $FS::UID::AutoCommit = 0;
455 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
456 my $error = $cust_pkg_discount->delete;
458 $dbh->rollback if $oldAutoCommit;
462 #cust_bill_pkg_discount?
464 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
465 my $error = $cust_pkg_detail->delete;
467 $dbh->rollback if $oldAutoCommit;
472 foreach my $cust_pkg_reason (
474 'table' => 'cust_pkg_reason',
475 'hashref' => { 'pkgnum' => $self->pkgnum },
479 my $error = $cust_pkg_reason->delete;
481 $dbh->rollback if $oldAutoCommit;
488 my $error = $self->SUPER::delete(@_);
490 $dbh->rollback if $oldAutoCommit;
494 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
500 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
502 Replaces the OLD_RECORD with this one in the database. If there is an error,
503 returns the error, otherwise returns false.
505 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
507 Changing pkgpart may have disasterous effects. See the order subroutine.
509 setup and bill are normally updated by calling the bill method of a customer
510 object (see L<FS::cust_main>).
512 suspend is normally updated by the suspend and unsuspend methods.
514 cancel is normally updated by the cancel method (and also the order subroutine
517 Available options are:
523 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.
527 the access_user (see L<FS::access_user>) providing the reason
531 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
540 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
545 ( ref($_[0]) eq 'HASH' )
549 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
550 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
553 #return "Can't change setup once it exists!"
554 # if $old->getfield('setup') &&
555 # $old->getfield('setup') != $new->getfield('setup');
557 #some logic for bill, susp, cancel?
559 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
561 local $SIG{HUP} = 'IGNORE';
562 local $SIG{INT} = 'IGNORE';
563 local $SIG{QUIT} = 'IGNORE';
564 local $SIG{TERM} = 'IGNORE';
565 local $SIG{TSTP} = 'IGNORE';
566 local $SIG{PIPE} = 'IGNORE';
568 my $oldAutoCommit = $FS::UID::AutoCommit;
569 local $FS::UID::AutoCommit = 0;
572 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
573 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
574 my $error = $new->insert_reason(
575 'reason' => $options->{'reason'},
576 'date' => $new->$method,
578 'reason_otaker' => $options->{'reason_otaker'},
581 dbh->rollback if $oldAutoCommit;
582 return "Error inserting cust_pkg_reason: $error";
587 #save off and freeze RADIUS attributes for any associated svc_acct records
589 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
591 #also check for specific exports?
592 # to avoid spurious modify export events
593 @svc_acct = map { $_->svc_x }
594 grep { $_->part_svc->svcdb eq 'svc_acct' }
597 $_->snapshot foreach @svc_acct;
601 my $error = $new->export_pkg_change($old)
602 || $new->SUPER::replace( $old,
604 ? $options->{options}
608 $dbh->rollback if $oldAutoCommit;
612 #for prepaid packages,
613 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
614 foreach my $old_svc_acct ( @svc_acct ) {
615 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
617 $new_svc_acct->replace( $old_svc_acct,
618 'depend_jobnum' => $options->{depend_jobnum},
621 $dbh->rollback if $oldAutoCommit;
626 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
633 Checks all fields to make sure this is a valid billing item. If there is an
634 error, returns the error, otherwise returns false. Called by the insert and
642 if ( !$self->locationnum or $self->locationnum == -1 ) {
643 $self->set('locationnum', $self->cust_main->ship_locationnum);
647 $self->ut_numbern('pkgnum')
648 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
649 || $self->ut_numbern('pkgpart')
650 || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' )
651 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
652 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
653 || $self->ut_numbern('quantity')
654 || $self->ut_numbern('start_date')
655 || $self->ut_numbern('setup')
656 || $self->ut_numbern('bill')
657 || $self->ut_numbern('susp')
658 || $self->ut_numbern('cancel')
659 || $self->ut_numbern('adjourn')
660 || $self->ut_numbern('resume')
661 || $self->ut_numbern('expire')
662 || $self->ut_numbern('dundate')
663 || $self->ut_enum('no_auto', [ '', 'Y' ])
664 || $self->ut_enum('waive_setup', [ '', 'Y' ])
665 || $self->ut_numbern('agent_pkgid')
666 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
667 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
668 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
669 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
670 || $self->ut_foreign_keyn('change_to_pkgnum', 'cust_pkg', 'pkgnum')
672 return $error if $error;
674 return "A package with both start date (future start) and setup date (already started) will never bill"
675 if $self->start_date && $self->setup && ! $upgrade;
677 return "A future unsuspend date can only be set for a package with a suspend date"
678 if $self->resume and !$self->susp and !$self->adjourn;
680 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
682 if ( $self->dbdef_table->column('manual_flag') ) {
683 $self->manual_flag('') if $self->manual_flag eq ' ';
684 $self->manual_flag =~ /^([01]?)$/
685 or return "Illegal manual_flag ". $self->manual_flag;
686 $self->manual_flag($1);
694 Check the pkgpart to make sure it's allowed with the reg_code and/or
695 promo_code of the package (if present) and with the customer's agent.
696 Called from C<insert>, unless we are doing a package change that doesn't
704 # my $error = $self->ut_numbern('pkgpart'); # already done
707 if ( $self->reg_code ) {
709 unless ( grep { $self->pkgpart == $_->pkgpart }
710 map { $_->reg_code_pkg }
711 qsearchs( 'reg_code', { 'code' => $self->reg_code,
712 'agentnum' => $self->cust_main->agentnum })
714 return "Unknown registration code";
717 } elsif ( $self->promo_code ) {
720 qsearchs('part_pkg', {
721 'pkgpart' => $self->pkgpart,
722 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
724 return 'Unknown promotional code' unless $promo_part_pkg;
728 unless ( $disable_agentcheck ) {
730 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
731 return "agent ". $agent->agentnum. ':'. $agent->agent.
732 " can't purchase pkgpart ". $self->pkgpart
733 unless $agent->pkgpart_hashref->{ $self->pkgpart }
734 || $agent->agentnum == $self->part_pkg->agentnum;
737 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
738 return $error if $error;
746 =item cancel [ OPTION => VALUE ... ]
748 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
749 in this package, then cancels the package itself (sets the cancel field to
752 Available options are:
756 =item quiet - can be set true to supress email cancellation notices.
758 =item time - can be set to cancel the package based on a specific future or
759 historical date. Using time ensures that the remaining amount is calculated
760 correctly. Note however that this is an immediate cancel and just changes
761 the date. You are PROBABLY looking to expire the account instead of using
764 =item reason - can be set to a cancellation reason (see L<FS:reason>),
765 either a reasonnum of an existing reason, or passing a hashref will create
766 a new reason. The hashref should have the following keys: typenum - Reason
767 type (see L<FS::reason_type>, reason - Text of the new reason.
769 =item date - can be set to a unix style timestamp to specify when to
772 =item nobill - can be set true to skip billing if it might otherwise be done.
774 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
775 not credit it. This must be set (by change()) when changing the package
776 to a different pkgpart or location, and probably shouldn't be in any other
777 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
782 If there is an error, returns the error, otherwise returns false.
787 my( $self, %options ) = @_;
790 # pass all suspend/cancel actions to the main package
791 if ( $self->main_pkgnum and !$options{'from_main'} ) {
792 return $self->main_pkg->cancel(%options);
795 my $conf = new FS::Conf;
797 warn "cust_pkg::cancel called with options".
798 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
801 local $SIG{HUP} = 'IGNORE';
802 local $SIG{INT} = 'IGNORE';
803 local $SIG{QUIT} = 'IGNORE';
804 local $SIG{TERM} = 'IGNORE';
805 local $SIG{TSTP} = 'IGNORE';
806 local $SIG{PIPE} = 'IGNORE';
808 my $oldAutoCommit = $FS::UID::AutoCommit;
809 local $FS::UID::AutoCommit = 0;
812 my $old = $self->select_for_update;
814 if ( $old->get('cancel') || $self->get('cancel') ) {
815 dbh->rollback if $oldAutoCommit;
816 return ""; # no error
819 # XXX possibly set cancel_time to the expire date?
820 my $cancel_time = $options{'time'} || time;
821 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
822 $date = '' if ($date && $date <= $cancel_time); # complain instead?
824 #race condition: usage could be ongoing until unprovisioned
825 #resolved by performing a change package instead (which unprovisions) and
827 if ( !$options{nobill} && !$date ) {
828 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
829 my $copy = $self->new({$self->hash});
831 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
833 'time' => $cancel_time );
834 warn "Error billing during cancel, custnum ".
835 #$self->cust_main->custnum. ": $error"
840 if ( $options{'reason'} ) {
841 $error = $self->insert_reason( 'reason' => $options{'reason'},
842 'action' => $date ? 'expire' : 'cancel',
843 'date' => $date ? $date : $cancel_time,
844 'reason_otaker' => $options{'reason_otaker'},
847 dbh->rollback if $oldAutoCommit;
848 return "Error inserting cust_pkg_reason: $error";
852 my %svc_cancel_opt = ();
853 $svc_cancel_opt{'date'} = $date if $date;
854 foreach my $cust_svc (
857 sort { $a->[1] <=> $b->[1] }
858 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
859 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
861 my $part_svc = $cust_svc->part_svc;
862 next if ( defined($part_svc) and $part_svc->preserve );
863 my $error = $cust_svc->cancel( %svc_cancel_opt );
866 $dbh->rollback if $oldAutoCommit;
867 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
873 # credit remaining time if appropriate
875 if ( exists($options{'unused_credit'}) ) {
876 $do_credit = $options{'unused_credit'};
879 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
882 my $error = $self->credit_remaining('cancel', $cancel_time);
884 $dbh->rollback if $oldAutoCommit;
891 my %hash = $self->hash;
893 $hash{'expire'} = $date;
895 $hash{'cancel'} = $cancel_time;
897 $hash{'change_custnum'} = $options{'change_custnum'};
899 my $new = new FS::cust_pkg ( \%hash );
900 $error = $new->replace( $self, options => { $self->options } );
901 if ( $self->change_to_pkgnum ) {
902 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
903 $error ||= $change_to->cancel || $change_to->delete;
906 $dbh->rollback if $oldAutoCommit;
910 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
911 $error = $supp_pkg->cancel(%options, 'from_main' => 1);
913 $dbh->rollback if $oldAutoCommit;
914 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
918 foreach my $usage ( $self->cust_pkg_usage ) {
919 $error = $usage->delete;
921 $dbh->rollback if $oldAutoCommit;
922 return "deleting usage pools: $error";
926 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
927 return '' if $date; #no errors
929 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
930 if ( !$options{'quiet'} &&
931 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
933 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
936 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
937 $error = $msg_template->send( 'cust_main' => $self->cust_main,
942 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
943 'to' => \@invoicing_list,
944 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
945 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
948 #should this do something on errors?
955 =item cancel_if_expired [ NOW_TIMESTAMP ]
957 Cancels this package if its expire date has been reached.
961 sub cancel_if_expired {
963 my $time = shift || time;
964 return '' unless $self->expire && $self->expire <= $time;
965 my $error = $self->cancel;
967 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
968 $self->custnum. ": $error";
975 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
976 locationnum, (other fields?). Attempts to re-provision cancelled services
977 using history information (errors at this stage are not fatal).
979 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
981 svc_fatal: service provisioning errors are fatal
983 svc_errors: pass an array reference, will be filled in with any provisioning errors
985 main_pkgnum: link the package as a supplemental package of this one. For
991 my( $self, %options ) = @_;
993 #in case you try do do $uncancel-date = $cust_pkg->uncacel
994 return '' unless $self->get('cancel');
996 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
997 return $self->main_pkg->uncancel(%options);
1004 local $SIG{HUP} = 'IGNORE';
1005 local $SIG{INT} = 'IGNORE';
1006 local $SIG{QUIT} = 'IGNORE';
1007 local $SIG{TERM} = 'IGNORE';
1008 local $SIG{TSTP} = 'IGNORE';
1009 local $SIG{PIPE} = 'IGNORE';
1011 my $oldAutoCommit = $FS::UID::AutoCommit;
1012 local $FS::UID::AutoCommit = 0;
1016 # insert the new package
1019 my $cust_pkg = new FS::cust_pkg {
1020 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
1021 bill => ( $options{'bill'} || $self->get('bill') ),
1023 uncancel_pkgnum => $self->pkgnum,
1024 main_pkgnum => ($options{'main_pkgnum'} || ''),
1025 map { $_ => $self->get($_) } qw(
1026 custnum pkgpart locationnum
1028 susp adjourn resume expire start_date contract_end dundate
1029 change_date change_pkgpart change_locationnum
1030 manual_flag no_auto quantity agent_pkgid recur_show_zero setup_show_zero
1034 my $error = $cust_pkg->insert(
1035 'change' => 1, #supresses any referral credit to a referring customer
1036 'allow_pkgpart' => 1, # allow this even if the package def is disabled
1039 $dbh->rollback if $oldAutoCommit;
1047 #find historical services within this timeframe before the package cancel
1048 # (incompatible with "time" option to cust_pkg->cancel?)
1049 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
1050 # too little? (unprovisioing export delay?)
1051 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
1052 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1055 foreach my $h_cust_svc (@h_cust_svc) {
1056 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1057 #next unless $h_svc_x; #should this happen?
1058 (my $table = $h_svc_x->table) =~ s/^h_//;
1059 require "FS/$table.pm";
1060 my $class = "FS::$table";
1061 my $svc_x = $class->new( {
1062 'pkgnum' => $cust_pkg->pkgnum,
1063 'svcpart' => $h_cust_svc->svcpart,
1064 map { $_ => $h_svc_x->get($_) } fields($table)
1068 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1069 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1072 my $svc_error = $svc_x->insert;
1074 if ( $options{svc_fatal} ) {
1075 $dbh->rollback if $oldAutoCommit;
1078 # if we've failed to insert the svc_x object, svc_Common->insert
1079 # will have removed the cust_svc already. if not, then both records
1080 # were inserted but we failed for some other reason (export, most
1081 # likely). in that case, report the error and delete the records.
1082 push @svc_errors, $svc_error;
1083 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1085 # except if export_insert failed, export_delete probably won't be
1087 local $FS::svc_Common::noexport_hack = 1;
1088 my $cleanup_error = $svc_x->delete; # also deletes cust_svc
1089 if ( $cleanup_error ) { # and if THAT fails, then run away
1090 $dbh->rollback if $oldAutoCommit;
1091 return $cleanup_error;
1096 } #foreach $h_cust_svc
1098 #these are pretty rare, but should handle them
1099 # - dsl_device (mac addresses)
1100 # - phone_device (mac addresses)
1101 # - dsl_note (ikano notes)
1102 # - domain_record (i.e. restore DNS information w/domains)
1103 # - inventory_item(?) (inventory w/un-cancelling service?)
1104 # - nas (svc_broaband nas stuff)
1105 #this stuff is unused in the wild afaik
1106 # - mailinglistmember
1108 # - svc_domain.parent_svcnum?
1109 # - acct_snarf (ancient mail fetching config)
1110 # - cgp_rule (communigate)
1111 # - cust_svc_option (used by our Tron stuff)
1112 # - acct_rt_transaction (used by our time worked stuff)
1115 # also move over any services that didn't unprovision at cancellation
1118 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1119 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1120 my $error = $cust_svc->replace;
1122 $dbh->rollback if $oldAutoCommit;
1128 # Uncancel any supplemental packages, and make them supplemental to the
1132 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1134 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1136 $dbh->rollback if $oldAutoCommit;
1137 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1145 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1147 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1148 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1155 Cancels any pending expiration (sets the expire field to null).
1157 If there is an error, returns the error, otherwise returns false.
1162 my( $self, %options ) = @_;
1165 local $SIG{HUP} = 'IGNORE';
1166 local $SIG{INT} = 'IGNORE';
1167 local $SIG{QUIT} = 'IGNORE';
1168 local $SIG{TERM} = 'IGNORE';
1169 local $SIG{TSTP} = 'IGNORE';
1170 local $SIG{PIPE} = 'IGNORE';
1172 my $oldAutoCommit = $FS::UID::AutoCommit;
1173 local $FS::UID::AutoCommit = 0;
1176 my $old = $self->select_for_update;
1178 my $pkgnum = $old->pkgnum;
1179 if ( $old->get('cancel') || $self->get('cancel') ) {
1180 dbh->rollback if $oldAutoCommit;
1181 return "Can't unexpire cancelled package $pkgnum";
1182 # or at least it's pointless
1185 unless ( $old->get('expire') && $self->get('expire') ) {
1186 dbh->rollback if $oldAutoCommit;
1187 return ""; # no error
1190 my %hash = $self->hash;
1191 $hash{'expire'} = '';
1192 my $new = new FS::cust_pkg ( \%hash );
1193 $error = $new->replace( $self, options => { $self->options } );
1195 $dbh->rollback if $oldAutoCommit;
1199 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1205 =item suspend [ OPTION => VALUE ... ]
1207 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1208 package, then suspends the package itself (sets the susp field to now).
1210 Available options are:
1214 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1215 either a reasonnum of an existing reason, or passing a hashref will create
1216 a new reason. The hashref should have the following keys:
1217 - typenum - Reason type (see L<FS::reason_type>
1218 - reason - Text of the new reason.
1220 =item date - can be set to a unix style timestamp to specify when to
1223 =item time - can be set to override the current time, for calculation
1224 of final invoices or unused-time credits
1226 =item resume_date - can be set to a time when the package should be
1227 unsuspended. This may be more convenient than calling C<unsuspend()>
1230 =item from_main - allows a supplemental package to be suspended, rather
1231 than redirecting the method call to its main package. For internal use.
1235 If there is an error, returns the error, otherwise returns false.
1240 my( $self, %options ) = @_;
1243 # pass all suspend/cancel actions to the main package
1244 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1245 return $self->main_pkg->suspend(%options);
1248 local $SIG{HUP} = 'IGNORE';
1249 local $SIG{INT} = 'IGNORE';
1250 local $SIG{QUIT} = 'IGNORE';
1251 local $SIG{TERM} = 'IGNORE';
1252 local $SIG{TSTP} = 'IGNORE';
1253 local $SIG{PIPE} = 'IGNORE';
1255 my $oldAutoCommit = $FS::UID::AutoCommit;
1256 local $FS::UID::AutoCommit = 0;
1259 my $old = $self->select_for_update;
1261 my $pkgnum = $old->pkgnum;
1262 if ( $old->get('cancel') || $self->get('cancel') ) {
1263 dbh->rollback if $oldAutoCommit;
1264 return "Can't suspend cancelled package $pkgnum";
1267 if ( $old->get('susp') || $self->get('susp') ) {
1268 dbh->rollback if $oldAutoCommit;
1269 return ""; # no error # complain on adjourn?
1272 my $suspend_time = $options{'time'} || time;
1273 my $date = $options{date} if $options{date}; # adjourn/suspend later
1274 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1276 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1277 dbh->rollback if $oldAutoCommit;
1278 return "Package $pkgnum expires before it would be suspended.";
1281 # some false laziness with sub cancel
1282 if ( !$options{nobill} && !$date &&
1283 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1284 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1285 # make the entire cust_main->bill path recognize 'suspend' and
1286 # 'cancel' separately.
1287 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1288 my $copy = $self->new({$self->hash});
1290 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1292 'time' => $suspend_time );
1293 warn "Error billing during suspend, custnum ".
1294 #$self->cust_main->custnum. ": $error"
1299 if ( $options{'reason'} ) {
1300 $error = $self->insert_reason( 'reason' => $options{'reason'},
1301 'action' => $date ? 'adjourn' : 'suspend',
1302 'date' => $date ? $date : $suspend_time,
1303 'reason_otaker' => $options{'reason_otaker'},
1306 dbh->rollback if $oldAutoCommit;
1307 return "Error inserting cust_pkg_reason: $error";
1311 my %hash = $self->hash;
1313 $hash{'adjourn'} = $date;
1315 $hash{'susp'} = $suspend_time;
1318 my $resume_date = $options{'resume_date'} || 0;
1319 if ( $resume_date > ($date || $suspend_time) ) {
1320 $hash{'resume'} = $resume_date;
1323 $options{options} ||= {};
1325 my $new = new FS::cust_pkg ( \%hash );
1326 $error = $new->replace( $self, options => { $self->options,
1327 %{ $options{options} },
1331 $dbh->rollback if $oldAutoCommit;
1336 # credit remaining time if appropriate
1337 if ( $self->part_pkg->option('unused_credit_suspend', 1) ) {
1338 my $error = $self->credit_remaining('suspend', $suspend_time);
1340 $dbh->rollback if $oldAutoCommit;
1347 foreach my $cust_svc (
1348 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
1350 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1352 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1353 $dbh->rollback if $oldAutoCommit;
1354 return "Illegal svcdb value in part_svc!";
1357 require "FS/$svcdb.pm";
1359 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1361 $error = $svc->suspend;
1363 $dbh->rollback if $oldAutoCommit;
1366 my( $label, $value ) = $cust_svc->label;
1367 push @labels, "$label: $value";
1371 my $conf = new FS::Conf;
1372 if ( $conf->config('suspend_email_admin') ) {
1374 my $error = send_email(
1375 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1376 #invoice_from ??? well as good as any
1377 'to' => $conf->config('suspend_email_admin'),
1378 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1380 "This is an automatic message from your Freeside installation\n",
1381 "informing you that the following customer package has been suspended:\n",
1383 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1384 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1385 ( map { "Service : $_\n" } @labels ),
1390 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1398 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1399 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1401 $dbh->rollback if $oldAutoCommit;
1402 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1406 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1411 =item credit_remaining MODE TIME
1413 Generate a credit for this package for the time remaining in the current
1414 billing period. MODE is either "suspend" or "cancel" (determines the
1415 credit type). TIME is the time of suspension/cancellation. Both arguments
1420 sub credit_remaining {
1421 # Add a credit for remaining service
1422 my ($self, $mode, $time) = @_;
1423 die 'credit_remaining requires suspend or cancel'
1424 unless $mode eq 'suspend' or $mode eq 'cancel';
1425 die 'no suspend/cancel time' unless $time > 0;
1427 my $conf = FS::Conf->new;
1428 my $reason_type = $conf->config($mode.'_credit_type');
1430 my $last_bill = $self->getfield('last_bill') || 0;
1431 my $next_bill = $self->getfield('bill') || 0;
1432 if ( $last_bill > 0 # the package has been billed
1433 and $next_bill > 0 # the package has a next bill date
1434 and $next_bill >= $time # which is in the future
1436 my $remaining_value = $self->calc_remain('time' => $time);
1437 if ( $remaining_value > 0 ) {
1438 warn "Crediting for $remaining_value on package ".$self->pkgnum."\n"
1440 my $error = $self->cust_main->credit(
1442 'Credit for unused time on '. $self->part_pkg->pkg,
1443 'reason_type' => $reason_type,
1445 return "Error crediting customer \$$remaining_value for unused time".
1446 " on ". $self->part_pkg->pkg. ": $error"
1448 } #if $remaining_value
1449 } #if $last_bill, etc.
1453 =item unsuspend [ OPTION => VALUE ... ]
1455 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1456 package, then unsuspends the package itself (clears the susp field and the
1457 adjourn field if it is in the past). If the suspend reason includes an
1458 unsuspension package, that package will be ordered.
1460 Available options are:
1466 Can be set to a date to unsuspend the package in the future (the 'resume'
1469 =item adjust_next_bill
1471 Can be set true to adjust the next bill date forward by
1472 the amount of time the account was inactive. This was set true by default
1473 in the past (from 1.4.2 and 1.5.0pre6 through 1.7.0), but now needs to be
1474 explicitly requested with this option or in the price plan.
1478 If there is an error, returns the error, otherwise returns false.
1483 my( $self, %opt ) = @_;
1486 # pass all suspend/cancel actions to the main package
1487 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1488 return $self->main_pkg->unsuspend(%opt);
1491 local $SIG{HUP} = 'IGNORE';
1492 local $SIG{INT} = 'IGNORE';
1493 local $SIG{QUIT} = 'IGNORE';
1494 local $SIG{TERM} = 'IGNORE';
1495 local $SIG{TSTP} = 'IGNORE';
1496 local $SIG{PIPE} = 'IGNORE';
1498 my $oldAutoCommit = $FS::UID::AutoCommit;
1499 local $FS::UID::AutoCommit = 0;
1502 my $old = $self->select_for_update;
1504 my $pkgnum = $old->pkgnum;
1505 if ( $old->get('cancel') || $self->get('cancel') ) {
1506 $dbh->rollback if $oldAutoCommit;
1507 return "Can't unsuspend cancelled package $pkgnum";
1510 unless ( $old->get('susp') && $self->get('susp') ) {
1511 $dbh->rollback if $oldAutoCommit;
1512 return ""; # no error # complain instead?
1515 my $date = $opt{'date'};
1516 if ( $date and $date > time ) { # return an error if $date <= time?
1518 if ( $old->get('expire') && $old->get('expire') < $date ) {
1519 $dbh->rollback if $oldAutoCommit;
1520 return "Package $pkgnum expires before it would be unsuspended.";
1523 my $new = new FS::cust_pkg { $self->hash };
1524 $new->set('resume', $date);
1525 $error = $new->replace($self, options => $self->options);
1528 $dbh->rollback if $oldAutoCommit;
1532 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1540 foreach my $cust_svc (
1541 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1543 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1545 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1546 $dbh->rollback if $oldAutoCommit;
1547 return "Illegal svcdb value in part_svc!";
1550 require "FS/$svcdb.pm";
1552 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1554 $error = $svc->unsuspend;
1556 $dbh->rollback if $oldAutoCommit;
1559 my( $label, $value ) = $cust_svc->label;
1560 push @labels, "$label: $value";
1565 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1566 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
1568 my %hash = $self->hash;
1569 my $inactive = time - $hash{'susp'};
1571 my $conf = new FS::Conf;
1573 if ( $inactive > 0 &&
1574 ( $hash{'bill'} || $hash{'setup'} ) &&
1575 ( $opt{'adjust_next_bill'} ||
1576 $conf->exists('unsuspend-always_adjust_next_bill_date') ||
1577 $self->part_pkg->option('unsuspend_adjust_bill', 1) )
1580 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
1585 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
1586 $hash{'resume'} = '' if !$hash{'adjourn'};
1587 my $new = new FS::cust_pkg ( \%hash );
1588 $error = $new->replace( $self, options => { $self->options } );
1590 $dbh->rollback if $oldAutoCommit;
1596 if ( $reason && $reason->unsuspend_pkgpart ) {
1597 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
1598 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
1600 my $start_date = $self->cust_main->next_bill_date
1601 if $reason->unsuspend_hold;
1604 $unsusp_pkg = FS::cust_pkg->new({
1605 'custnum' => $self->custnum,
1606 'pkgpart' => $reason->unsuspend_pkgpart,
1607 'start_date' => $start_date,
1608 'locationnum' => $self->locationnum,
1609 # discount? probably not...
1612 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
1616 $dbh->rollback if $oldAutoCommit;
1621 if ( $conf->config('unsuspend_email_admin') ) {
1623 my $error = send_email(
1624 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1625 #invoice_from ??? well as good as any
1626 'to' => $conf->config('unsuspend_email_admin'),
1627 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
1628 "This is an automatic message from your Freeside installation\n",
1629 "informing you that the following customer package has been unsuspended:\n",
1631 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1632 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1633 ( map { "Service : $_\n" } @labels ),
1635 "An unsuspension fee was charged: ".
1636 $unsusp_pkg->part_pkg->pkg_comment."\n"
1643 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
1649 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1650 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
1652 $dbh->rollback if $oldAutoCommit;
1653 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1657 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1664 Cancels any pending suspension (sets the adjourn field to null).
1666 If there is an error, returns the error, otherwise returns false.
1671 my( $self, %options ) = @_;
1674 local $SIG{HUP} = 'IGNORE';
1675 local $SIG{INT} = 'IGNORE';
1676 local $SIG{QUIT} = 'IGNORE';
1677 local $SIG{TERM} = 'IGNORE';
1678 local $SIG{TSTP} = 'IGNORE';
1679 local $SIG{PIPE} = 'IGNORE';
1681 my $oldAutoCommit = $FS::UID::AutoCommit;
1682 local $FS::UID::AutoCommit = 0;
1685 my $old = $self->select_for_update;
1687 my $pkgnum = $old->pkgnum;
1688 if ( $old->get('cancel') || $self->get('cancel') ) {
1689 dbh->rollback if $oldAutoCommit;
1690 return "Can't unadjourn cancelled package $pkgnum";
1691 # or at least it's pointless
1694 if ( $old->get('susp') || $self->get('susp') ) {
1695 dbh->rollback if $oldAutoCommit;
1696 return "Can't unadjourn suspended package $pkgnum";
1697 # perhaps this is arbitrary
1700 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1701 dbh->rollback if $oldAutoCommit;
1702 return ""; # no error
1705 my %hash = $self->hash;
1706 $hash{'adjourn'} = '';
1707 $hash{'resume'} = '';
1708 my $new = new FS::cust_pkg ( \%hash );
1709 $error = $new->replace( $self, options => { $self->options } );
1711 $dbh->rollback if $oldAutoCommit;
1715 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1722 =item change HASHREF | OPTION => VALUE ...
1724 Changes this package: cancels it and creates a new one, with a different
1725 pkgpart or locationnum or both. All services are transferred to the new
1726 package (no change will be made if this is not possible).
1728 Options may be passed as a list of key/value pairs or as a hash reference.
1735 New locationnum, to change the location for this package.
1739 New FS::cust_location object, to create a new location and assign it
1744 New FS::cust_main object, to create a new customer and assign the new package
1749 New pkgpart (see L<FS::part_pkg>).
1753 New refnum (see L<FS::part_referral>).
1757 New quantity; if unspecified, the new package will have the same quantity
1762 "New" (existing) FS::cust_pkg object. The package's services and other
1763 attributes will be transferred to this package.
1767 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1768 susp, adjourn, cancel, expire, and contract_end) to the new package.
1770 =item unprotect_svcs
1772 Normally, change() will rollback and return an error if some services
1773 can't be transferred (also see the I<cust_pkg-change_svcpart> config option).
1774 If unprotect_svcs is true, this method will transfer as many services as
1775 it can and then unconditionally cancel the old package.
1779 At least one of locationnum, cust_location, pkgpart, refnum, cust_main, or
1780 cust_pkg must be specified (otherwise, what's the point?)
1782 Returns either the new FS::cust_pkg object or a scalar error.
1786 my $err_or_new_cust_pkg = $old_cust_pkg->change
1790 #some false laziness w/order
1793 my $opt = ref($_[0]) ? shift : { @_ };
1795 my $conf = new FS::Conf;
1797 # Transactionize this whole mess
1798 local $SIG{HUP} = 'IGNORE';
1799 local $SIG{INT} = 'IGNORE';
1800 local $SIG{QUIT} = 'IGNORE';
1801 local $SIG{TERM} = 'IGNORE';
1802 local $SIG{TSTP} = 'IGNORE';
1803 local $SIG{PIPE} = 'IGNORE';
1805 my $oldAutoCommit = $FS::UID::AutoCommit;
1806 local $FS::UID::AutoCommit = 0;
1815 $hash{'setup'} = $time if $self->setup;
1817 $hash{'change_date'} = $time;
1818 $hash{"change_$_"} = $self->$_()
1819 foreach qw( pkgnum pkgpart locationnum );
1821 if ( $opt->{'cust_location'} ) {
1822 $error = $opt->{'cust_location'}->find_or_insert;
1824 $dbh->rollback if $oldAutoCommit;
1825 return "inserting cust_location (transaction rolled back): $error";
1827 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1830 if ( $opt->{'cust_pkg'} ) {
1831 # treat changing to a package with a different pkgpart as a
1832 # pkgpart change (because it is)
1833 $opt->{'pkgpart'} = $opt->{'cust_pkg'}->pkgpart;
1836 # whether to override pkgpart checking on the new package
1837 my $same_pkgpart = 1;
1838 if ( $opt->{'pkgpart'} and ( $opt->{'pkgpart'} != $self->pkgpart ) ) {
1842 my $unused_credit = 0;
1843 my $keep_dates = $opt->{'keep_dates'};
1844 # Special case. If the pkgpart is changing, and the customer is
1845 # going to be credited for remaining time, don't keep setup, bill,
1846 # or last_bill dates, and DO pass the flag to cancel() to credit
1848 if ( $opt->{'pkgpart'}
1849 and $opt->{'pkgpart'} != $self->pkgpart
1850 and $self->part_pkg->option('unused_credit_change', 1) ) {
1853 $hash{$_} = '' foreach qw(setup bill last_bill);
1856 if ( $keep_dates ) {
1857 foreach my $date ( qw(setup bill last_bill susp adjourn cancel expire
1858 resume start_date contract_end ) ) {
1859 $hash{$date} = $self->getfield($date);
1863 # allow $opt->{'locationnum'} = '' to specifically set it to null
1864 # (i.e. customer default location)
1865 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
1867 # usually this doesn't matter. the two cases where it does are:
1868 # 1. unused_credit_change + pkgpart change + setup fee on the new package
1870 # 2. (more importantly) changing a package before it's billed
1871 $hash{'waive_setup'} = $self->waive_setup;
1873 my $custnum = $self->custnum;
1874 if ( $opt->{cust_main} ) {
1875 my $cust_main = $opt->{cust_main};
1876 unless ( $cust_main->custnum ) {
1877 my $error = $cust_main->insert( @{ $opt->{cust_main_insert_args}||[] } );
1879 $dbh->rollback if $oldAutoCommit;
1880 return "inserting cust_main (transaction rolled back): $error";
1883 $custnum = $cust_main->custnum;
1886 $hash{'contactnum'} = $opt->{'contactnum'} if $opt->{'contactnum'};
1889 if ( $opt->{'cust_pkg'} ) {
1890 # The target package already exists; update it to show that it was
1891 # changed from this package.
1892 $cust_pkg = $opt->{'cust_pkg'};
1894 foreach ( qw( pkgnum pkgpart locationnum ) ) {
1895 $cust_pkg->set("change_$_", $self->get($_));
1897 $cust_pkg->set('change_date', $time);
1898 $error = $cust_pkg->replace;
1901 # Create the new package.
1902 $cust_pkg = new FS::cust_pkg {
1903 custnum => $custnum,
1904 locationnum => $opt->{'locationnum'},
1905 ( map { $_ => ( $opt->{$_} || $self->$_() ) }
1906 qw( pkgpart quantity refnum salesnum )
1910 $error = $cust_pkg->insert( 'change' => 1,
1911 'allow_pkgpart' => $same_pkgpart );
1914 $dbh->rollback if $oldAutoCommit;
1918 # Transfer services and cancel old package.
1920 $error = $self->transfer($cust_pkg);
1921 if ($error and $error == 0) {
1922 # $old_pkg->transfer failed.
1923 $dbh->rollback if $oldAutoCommit;
1927 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
1928 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
1929 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
1930 if ($error and $error == 0) {
1931 # $old_pkg->transfer failed.
1932 $dbh->rollback if $oldAutoCommit;
1937 # We set unprotect_svcs when executing a "future package change". It's
1938 # not a user-interactive operation, so returning an error means the
1939 # package change will just fail. Rather than have that happen, we'll
1940 # let leftover services be deleted.
1941 if ($error > 0 and !$opt->{'unprotect_svcs'}) {
1942 # Transfers were successful, but we still had services left on the old
1943 # package. We can't change the package under this circumstances, so abort.
1944 $dbh->rollback if $oldAutoCommit;
1945 return "Unable to transfer all services from package ". $self->pkgnum;
1948 #reset usage if changing pkgpart
1949 # AND usage rollover is off (otherwise adds twice, now and at package bill)
1950 if ($self->pkgpart != $cust_pkg->pkgpart) {
1951 my $part_pkg = $cust_pkg->part_pkg;
1952 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
1956 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
1959 $dbh->rollback if $oldAutoCommit;
1960 return "Error setting usage values: $error";
1963 # if NOT changing pkgpart, transfer any usage pools over
1964 foreach my $usage ($self->cust_pkg_usage) {
1965 $usage->set('pkgnum', $cust_pkg->pkgnum);
1966 $error = $usage->replace;
1968 $dbh->rollback if $oldAutoCommit;
1969 return "Error transferring usage pools: $error";
1974 # transfer discounts, if we're not changing pkgpart
1975 if ( $same_pkgpart ) {
1976 foreach my $old_discount ($self->cust_pkg_discount_active) {
1977 # don't remove the old discount, we may still need to bill that package.
1978 my $new_discount = new FS::cust_pkg_discount {
1979 'pkgnum' => $cust_pkg->pkgnum,
1980 'discountnum' => $old_discount->discountnum,
1981 'months_used' => $old_discount->months_used,
1983 $error = $new_discount->insert;
1985 $dbh->rollback if $oldAutoCommit;
1986 return "Error transferring discounts: $error";
1991 # transfer (copy) invoice details
1992 foreach my $detail ($self->cust_pkg_detail) {
1993 my $new_detail = FS::cust_pkg_detail->new({ $detail->hash });
1994 $new_detail->set('pkgdetailnum', '');
1995 $new_detail->set('pkgnum', $cust_pkg->pkgnum);
1996 $error = $new_detail->insert;
1998 $dbh->rollback if $oldAutoCommit;
1999 return "Error transferring package notes: $error";
2005 if ( !$opt->{'cust_pkg'} ) {
2006 # Order any supplemental packages.
2007 my $part_pkg = $cust_pkg->part_pkg;
2008 my @old_supp_pkgs = $self->supplemental_pkgs;
2009 foreach my $link ($part_pkg->supp_part_pkg_link) {
2011 foreach (@old_supp_pkgs) {
2012 if ($_->pkgpart == $link->dst_pkgpart) {
2014 $_->pkgpart(0); # so that it can't match more than once
2018 # false laziness with FS::cust_main::Packages::order_pkg
2019 my $new = FS::cust_pkg->new({
2020 pkgpart => $link->dst_pkgpart,
2021 pkglinknum => $link->pkglinknum,
2022 custnum => $custnum,
2023 main_pkgnum => $cust_pkg->pkgnum,
2024 locationnum => $cust_pkg->locationnum,
2025 start_date => $cust_pkg->start_date,
2026 order_date => $cust_pkg->order_date,
2027 expire => $cust_pkg->expire,
2028 adjourn => $cust_pkg->adjourn,
2029 contract_end => $cust_pkg->contract_end,
2030 refnum => $cust_pkg->refnum,
2031 discountnum => $cust_pkg->discountnum,
2032 waive_setup => $cust_pkg->waive_setup,
2034 if ( $old and $opt->{'keep_dates'} ) {
2035 foreach (qw(setup bill last_bill)) {
2036 $new->set($_, $old->get($_));
2039 $error = $new->insert( allow_pkgpart => $same_pkgpart );
2042 $error ||= $old->transfer($new);
2044 if ( $error and $error > 0 ) {
2045 # no reason why this should ever fail, but still...
2046 $error = "Unable to transfer all services from supplemental package ".
2050 $dbh->rollback if $oldAutoCommit;
2053 push @new_supp_pkgs, $new;
2055 } # if !$opt->{'cust_pkg'}
2056 # because if there is one, then supplemental packages would already
2057 # have been created for it.
2059 #Good to go, cancel old package. Notify 'cancel' of whether to credit
2061 #Don't allow billing the package (preceding period packages and/or
2062 #outstanding usage) if we are keeping dates (i.e. location changing),
2063 #because the new package will be billed for the same date range.
2064 #Supplemental packages are also canceled here.
2066 # during scheduled changes, avoid canceling the package we just
2068 $self->set('change_to_pkgnum' => '');
2070 $error = $self->cancel(
2072 unused_credit => $unused_credit,
2073 nobill => $keep_dates,
2074 change_custnum => ( $self->custnum != $custnum ? $custnum : '' ),
2077 $dbh->rollback if $oldAutoCommit;
2081 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
2083 my $error = $cust_pkg->cust_main->bill(
2084 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
2087 $dbh->rollback if $oldAutoCommit;
2092 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2098 =item change_later OPTION => VALUE...
2100 Schedule a package change for a later date. This actually orders the new
2101 package immediately, but sets its start date for a future date, and sets
2102 the current package to expire on the same date.
2104 If the package is already scheduled for a change, this can be called with
2105 'start_date' to change the scheduled date, or with pkgpart and/or
2106 locationnum to modify the package change. To cancel the scheduled change
2107 entirely, see C<abort_change>.
2115 The date for the package change. Required, and must be in the future.
2123 The pkgpart. locationnum, and quantity of the new package, with the same
2124 meaning as in C<change>.
2132 my $opt = ref($_[0]) ? shift : { @_ };
2134 my $oldAutoCommit = $FS::UID::AutoCommit;
2135 local $FS::UID::AutoCommit = 0;
2138 my $cust_main = $self->cust_main;
2140 my $date = delete $opt->{'start_date'} or return 'start_date required';
2142 if ( $date <= time ) {
2143 $dbh->rollback if $oldAutoCommit;
2144 return "start_date $date is in the past";
2149 if ( $self->change_to_pkgnum ) {
2150 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
2151 my $new_pkgpart = $opt->{'pkgpart'}
2152 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $change_to->pkgpart;
2153 my $new_locationnum = $opt->{'locationnum'}
2154 if $opt->{'locationnum'} and $opt->{'locationnum'} != $change_to->locationnum;
2155 my $new_quantity = $opt->{'quantity'}
2156 if $opt->{'quantity'} and $opt->{'quantity'} != $change_to->quantity;
2157 if ( $new_pkgpart or $new_locationnum or $new_quantity ) {
2158 # it hasn't been billed yet, so in principle we could just edit
2159 # it in place (w/o a package change), but that's bad form.
2160 # So change the package according to the new options...
2161 my $err_or_pkg = $change_to->change(%$opt);
2162 if ( ref $err_or_pkg ) {
2163 # Then set that package up for a future start.
2164 $self->set('change_to_pkgnum', $err_or_pkg->pkgnum);
2165 $self->set('expire', $date); # in case it's different
2166 $err_or_pkg->set('start_date', $date);
2167 $err_or_pkg->set('change_date', '');
2168 $err_or_pkg->set('change_pkgnum', '');
2170 $error = $self->replace ||
2171 $err_or_pkg->replace ||
2172 $change_to->cancel ||
2175 $error = $err_or_pkg;
2177 } else { # change the start date only.
2178 $self->set('expire', $date);
2179 $change_to->set('start_date', $date);
2180 $error = $self->replace || $change_to->replace;
2183 $dbh->rollback if $oldAutoCommit;
2186 $dbh->commit if $oldAutoCommit;
2189 } # if $self->change_to_pkgnum
2191 my $new_pkgpart = $opt->{'pkgpart'}
2192 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart;
2193 my $new_locationnum = $opt->{'locationnum'}
2194 if $opt->{'locationnum'} and $opt->{'locationnum'} != $self->locationnum;
2195 my $new_quantity = $opt->{'quantity'}
2196 if $opt->{'quantity'} and $opt->{'quantity'} != $self->quantity;
2198 return '' unless $new_pkgpart or $new_locationnum or $new_quantity; # wouldn't do anything
2200 # allow $opt->{'locationnum'} = '' to specifically set it to null
2201 # (i.e. customer default location)
2202 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2204 my $new = FS::cust_pkg->new( {
2205 custnum => $self->custnum,
2206 locationnum => $opt->{'locationnum'},
2207 start_date => $date,
2208 map { $_ => ( $opt->{$_} || $self->$_() ) }
2209 qw( pkgpart quantity refnum salesnum )
2211 $error = $new->insert('change' => 1,
2212 'allow_pkgpart' => ($new_pkgpart ? 0 : 1));
2214 $self->set('change_to_pkgnum', $new->pkgnum);
2215 $self->set('expire', $date);
2216 $error = $self->replace;
2219 $dbh->rollback if $oldAutoCommit;
2221 $dbh->commit if $oldAutoCommit;
2229 Cancels a future package change scheduled by C<change_later>.
2235 my $pkgnum = $self->change_to_pkgnum;
2236 my $change_to = FS::cust_pkg->by_key($pkgnum) if $pkgnum;
2239 $error = $change_to->cancel || $change_to->delete;
2240 return $error if $error;
2242 $self->set('change_to_pkgnum', '');
2243 $self->set('expire', '');
2247 =item set_quantity QUANTITY
2249 Change the package's quantity field. This is one of the few package properties
2250 that can safely be changed without canceling and reordering the package
2251 (because it doesn't affect tax eligibility). Returns an error or an
2258 $self = $self->replace_old; # just to make sure
2259 $self->quantity(shift);
2263 =item set_salesnum SALESNUM
2265 Change the package's salesnum (sales person) field. This is one of the few
2266 package properties that can safely be changed without canceling and reordering
2267 the package (because it doesn't affect tax eligibility). Returns an error or
2274 $self = $self->replace_old; # just to make sure
2275 $self->salesnum(shift);
2277 # XXX this should probably reassign any credit that's already been given
2280 =item modify_charge OPTIONS
2282 Change the properties of a one-time charge. Currently the only properties
2283 that can be changed this way are those that have no impact on billing
2285 - pkg: the package description
2286 - classnum: the package class
2287 - additional: arrayref of additional invoice details to add to this package
2289 If you pass 'adjust_commission' => 1, and the classnum changes, and there are
2290 commission credits linked to this charge, they will be recalculated.
2297 my $part_pkg = $self->part_pkg;
2298 my $pkgnum = $self->pkgnum;
2301 my $oldAutoCommit = $FS::UID::AutoCommit;
2302 local $FS::UID::AutoCommit = 0;
2304 return "Can't use modify_charge except on one-time charges"
2305 unless $part_pkg->freq eq '0';
2307 if ( length($opt{'pkg'}) and $part_pkg->pkg ne $opt{'pkg'} ) {
2308 $part_pkg->set('pkg', $opt{'pkg'});
2311 my %pkg_opt = $part_pkg->options;
2312 if ( ref($opt{'additional'}) ) {
2313 delete $pkg_opt{$_} foreach grep /^additional/, keys %pkg_opt;
2315 for ( $i = 0; exists($opt{'additional'}->[$i]); $i++ ) {
2316 $pkg_opt{ "additional_info$i" } = $opt{'additional'}->[$i];
2318 $pkg_opt{'additional_count'} = $i if $i > 0;
2322 if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} ) {
2324 $old_classnum = $part_pkg->classnum;
2325 $part_pkg->set('classnum', $opt{'classnum'});
2328 my $error = $part_pkg->replace( options => \%pkg_opt );
2329 return $error if $error;
2331 if (defined $old_classnum) {
2332 # fix invoice grouping records
2333 my $old_catname = $old_classnum
2334 ? FS::pkg_class->by_key($old_classnum)->categoryname
2336 my $new_catname = $opt{'classnum'}
2337 ? $part_pkg->pkg_class->categoryname
2339 if ( $old_catname ne $new_catname ) {
2340 foreach my $cust_bill_pkg ($self->cust_bill_pkg) {
2341 # (there should only be one...)
2342 my @display = qsearch( 'cust_bill_pkg_display', {
2343 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2344 'section' => $old_catname,
2346 foreach (@display) {
2347 $_->set('section', $new_catname);
2348 $error = $_->replace;
2350 $dbh->rollback if $oldAutoCommit;
2354 } # foreach $cust_bill_pkg
2357 if ( $opt{'adjust_commission'} ) {
2358 # fix commission credits...tricky.
2359 foreach my $cust_event ($self->cust_event) {
2360 my $part_event = $cust_event->part_event;
2361 foreach my $table (qw(sales agent)) {
2363 "FS::part_event::Action::Mixin::credit_${table}_pkg_class";
2364 my $credit = qsearchs('cust_credit', {
2365 'eventnum' => $cust_event->eventnum,
2367 if ( $part_event->isa($class) ) {
2368 # Yes, this results in current commission rates being applied
2369 # retroactively to a one-time charge. For accounting purposes
2370 # there ought to be some kind of time limit on doing this.
2371 my $amount = $part_event->_calc_credit($self);
2372 if ( $credit and $credit->amount ne $amount ) {
2373 # Void the old credit.
2374 $error = $credit->void('Package class changed');
2376 $dbh->rollback if $oldAutoCommit;
2377 return "$error (adjusting commission credit)";
2380 # redo the event action to recreate the credit.
2382 eval { $part_event->do_action( $self, $cust_event ) };
2384 $dbh->rollback if $oldAutoCommit;
2387 } # if $part_event->isa($class)
2389 } # foreach $cust_event
2390 } # if $opt{'adjust_commission'}
2391 } # if defined $old_classnum
2393 $dbh->commit if $oldAutoCommit;
2399 use Storable 'thaw';
2402 sub process_bulk_cust_pkg {
2404 my $param = thaw(decode_base64(shift));
2405 warn Dumper($param) if $DEBUG;
2407 my $old_part_pkg = qsearchs('part_pkg',
2408 { pkgpart => $param->{'old_pkgpart'} });
2409 my $new_part_pkg = qsearchs('part_pkg',
2410 { pkgpart => $param->{'new_pkgpart'} });
2411 die "Must select a new package type\n" unless $new_part_pkg;
2412 #my $keep_dates = $param->{'keep_dates'} || 0;
2413 my $keep_dates = 1; # there is no good reason to turn this off
2415 local $SIG{HUP} = 'IGNORE';
2416 local $SIG{INT} = 'IGNORE';
2417 local $SIG{QUIT} = 'IGNORE';
2418 local $SIG{TERM} = 'IGNORE';
2419 local $SIG{TSTP} = 'IGNORE';
2420 local $SIG{PIPE} = 'IGNORE';
2422 my $oldAutoCommit = $FS::UID::AutoCommit;
2423 local $FS::UID::AutoCommit = 0;
2426 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
2429 foreach my $old_cust_pkg ( @cust_pkgs ) {
2431 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
2432 if ( $old_cust_pkg->getfield('cancel') ) {
2433 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
2434 $old_cust_pkg->pkgnum."\n"
2438 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
2440 my $error = $old_cust_pkg->change(
2441 'pkgpart' => $param->{'new_pkgpart'},
2442 'keep_dates' => $keep_dates
2444 if ( !ref($error) ) { # change returns the cust_pkg on success
2446 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
2449 $dbh->commit if $oldAutoCommit;
2455 Returns the last bill date, or if there is no last bill date, the setup date.
2456 Useful for billing metered services.
2462 return $self->setfield('last_bill', $_[0]) if @_;
2463 return $self->getfield('last_bill') if $self->getfield('last_bill');
2464 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
2465 'edate' => $self->bill, } );
2466 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
2469 =item last_cust_pkg_reason ACTION
2471 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
2472 Returns false if there is no reason or the package is not currenly ACTION'd
2473 ACTION is one of adjourn, susp, cancel, or expire.
2477 sub last_cust_pkg_reason {
2478 my ( $self, $action ) = ( shift, shift );
2479 my $date = $self->get($action);
2481 'table' => 'cust_pkg_reason',
2482 'hashref' => { 'pkgnum' => $self->pkgnum,
2483 'action' => substr(uc($action), 0, 1),
2486 'order_by' => 'ORDER BY num DESC LIMIT 1',
2490 =item last_reason ACTION
2492 Returns the most recent ACTION FS::reason associated with the package.
2493 Returns false if there is no reason or the package is not currenly ACTION'd
2494 ACTION is one of adjourn, susp, cancel, or expire.
2499 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
2500 $cust_pkg_reason->reason
2501 if $cust_pkg_reason;
2506 Returns the definition for this billing item, as an FS::part_pkg object (see
2513 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
2514 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
2515 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
2520 Returns the cancelled package this package was changed from, if any.
2526 return '' unless $self->change_pkgnum;
2527 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
2530 =item change_cust_main
2532 Returns the customter this package was detached to, if any.
2536 sub change_cust_main {
2538 return '' unless $self->change_custnum;
2539 qsearchs('cust_main', { 'custnum' => $self->change_custnum } );
2544 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
2551 $self->part_pkg->calc_setup($self, @_);
2556 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
2563 $self->part_pkg->calc_recur($self, @_);
2568 Calls the I<base_setup> of the FS::part_pkg object associated with this billing
2575 $self->part_pkg->base_setup($self, @_);
2580 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
2587 $self->part_pkg->base_recur($self, @_);
2592 Calls the I<calc_remain> of the FS::part_pkg object associated with this
2599 $self->part_pkg->calc_remain($self, @_);
2604 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
2611 $self->part_pkg->calc_cancel($self, @_);
2616 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
2622 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
2625 =item cust_pkg_detail [ DETAILTYPE ]
2627 Returns any customer package details for this package (see
2628 L<FS::cust_pkg_detail>).
2630 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2634 sub cust_pkg_detail {
2636 my %hash = ( 'pkgnum' => $self->pkgnum );
2637 $hash{detailtype} = shift if @_;
2639 'table' => 'cust_pkg_detail',
2640 'hashref' => \%hash,
2641 'order_by' => 'ORDER BY weight, pkgdetailnum',
2645 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
2647 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
2649 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2651 If there is an error, returns the error, otherwise returns false.
2655 sub set_cust_pkg_detail {
2656 my( $self, $detailtype, @details ) = @_;
2658 local $SIG{HUP} = 'IGNORE';
2659 local $SIG{INT} = 'IGNORE';
2660 local $SIG{QUIT} = 'IGNORE';
2661 local $SIG{TERM} = 'IGNORE';
2662 local $SIG{TSTP} = 'IGNORE';
2663 local $SIG{PIPE} = 'IGNORE';
2665 my $oldAutoCommit = $FS::UID::AutoCommit;
2666 local $FS::UID::AutoCommit = 0;
2669 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
2670 my $error = $current->delete;
2672 $dbh->rollback if $oldAutoCommit;
2673 return "error removing old detail: $error";
2677 foreach my $detail ( @details ) {
2678 my $cust_pkg_detail = new FS::cust_pkg_detail {
2679 'pkgnum' => $self->pkgnum,
2680 'detailtype' => $detailtype,
2681 'detail' => $detail,
2683 my $error = $cust_pkg_detail->insert;
2685 $dbh->rollback if $oldAutoCommit;
2686 return "error adding new detail: $error";
2691 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2698 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
2702 #false laziness w/cust_bill.pm
2706 'table' => 'cust_event',
2707 'addl_from' => 'JOIN part_event USING ( eventpart )',
2708 'hashref' => { 'tablenum' => $self->pkgnum },
2709 'extra_sql' => " AND eventtable = 'cust_pkg' ",
2713 =item num_cust_event
2715 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
2719 #false laziness w/cust_bill.pm
2720 sub num_cust_event {
2723 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
2724 " WHERE tablenum = ? AND eventtable = 'cust_pkg'";
2725 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
2726 $sth->execute($self->pkgnum) or die $sth->errstr. " executing $sql";
2727 $sth->fetchrow_arrayref->[0];
2730 =item part_pkg_currency_option OPTIONNAME
2732 Returns a two item list consisting of the currency of this customer, if any,
2733 and a value for the provided option. If the customer has a currency, the value
2734 is the option value the given name and the currency (see
2735 L<FS::part_pkg_currency>). Otherwise, if the customer has no currency, is the
2736 regular option value for the given name (see L<FS::part_pkg_option>).
2740 sub part_pkg_currency_option {
2741 my( $self, $optionname ) = @_;
2742 my $part_pkg = $self->part_pkg;
2743 if ( my $currency = $self->cust_main->currency ) {
2744 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
2746 ('', $part_pkg->option($optionname) );
2750 =item cust_svc [ SVCPART ] (old, deprecated usage)
2752 =item cust_svc [ OPTION => VALUE ... ] (current usage)
2754 =item cust_svc_unsorted [ OPTION => VALUE ... ]
2756 Returns the services for this package, as FS::cust_svc objects (see
2757 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
2758 spcififed, returns only the matching services.
2760 As an optimization, use the cust_svc_unsorted version if you are not displaying
2767 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
2768 $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref(@_) );
2771 sub cust_svc_unsorted {
2773 @{ $self->cust_svc_unsorted_arrayref(@_) };
2776 sub cust_svc_unsorted_arrayref {
2779 return () unless $self->num_cust_svc(@_);
2782 if ( @_ && $_[0] =~ /^\d+/ ) {
2783 $opt{svcpart} = shift;
2784 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2791 'table' => 'cust_svc',
2792 'hashref' => { 'pkgnum' => $self->pkgnum },
2794 if ( $opt{svcpart} ) {
2795 $search{hashref}->{svcpart} = $opt{'svcpart'};
2797 if ( $opt{'svcdb'} ) {
2798 $search{addl_from} = ' LEFT JOIN part_svc USING ( svcpart ) ';
2799 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{'svcdb'} );
2802 [ qsearch(\%search) ];
2806 =item overlimit [ SVCPART ]
2808 Returns the services for this package which have exceeded their
2809 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
2810 is specified, return only the matching services.
2816 return () unless $self->num_cust_svc(@_);
2817 grep { $_->overlimit } $self->cust_svc(@_);
2820 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
2822 Returns historical services for this package created before END TIMESTAMP and
2823 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
2824 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
2825 I<pkg_svc.hidden> flag will be omitted.
2831 warn "$me _h_cust_svc called on $self\n"
2834 my ($end, $start, $mode) = @_;
2835 my @cust_svc = $self->_sort_cust_svc(
2836 [ qsearch( 'h_cust_svc',
2837 { 'pkgnum' => $self->pkgnum, },
2838 FS::h_cust_svc->sql_h_search(@_),
2841 if ( defined($mode) && $mode eq 'I' ) {
2842 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
2843 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
2849 sub _sort_cust_svc {
2850 my( $self, $arrayref ) = @_;
2853 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
2855 my %pkg_svc = map { $_->svcpart => $_ }
2856 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
2861 my $pkg_svc = $pkg_svc{ $_->svcpart } || '';
2863 $pkg_svc ? $pkg_svc->primary_svc : '',
2864 $pkg_svc ? $pkg_svc->quantity : 0,
2871 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
2873 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
2875 Returns the number of services for this package. Available options are svcpart
2876 and svcdb. If either is spcififed, returns only the matching services.
2883 return $self->{'_num_cust_svc'}
2885 && exists($self->{'_num_cust_svc'})
2886 && $self->{'_num_cust_svc'} =~ /\d/;
2888 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
2892 if ( @_ && $_[0] =~ /^\d+/ ) {
2893 $opt{svcpart} = shift;
2894 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2900 my $select = 'SELECT COUNT(*) FROM cust_svc ';
2901 my $where = ' WHERE pkgnum = ? ';
2902 my @param = ($self->pkgnum);
2904 if ( $opt{'svcpart'} ) {
2905 $where .= ' AND svcpart = ? ';
2906 push @param, $opt{'svcpart'};
2908 if ( $opt{'svcdb'} ) {
2909 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
2910 $where .= ' AND svcdb = ? ';
2911 push @param, $opt{'svcdb'};
2914 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
2915 $sth->execute(@param) or die $sth->errstr;
2916 $sth->fetchrow_arrayref->[0];
2919 =item available_part_svc
2921 Returns a list of FS::part_svc objects representing services included in this
2922 package but not yet provisioned. Each FS::part_svc object also has an extra
2923 field, I<num_avail>, which specifies the number of available services.
2927 sub available_part_svc {
2930 my $pkg_quantity = $self->quantity || 1;
2932 grep { $_->num_avail > 0 }
2934 my $part_svc = $_->part_svc;
2935 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
2936 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
2938 # more evil encapsulation breakage
2939 if($part_svc->{'Hash'}{'num_avail'} > 0) {
2940 my @exports = $part_svc->part_export_did;
2941 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
2946 $self->part_pkg->pkg_svc;
2949 =item part_svc [ OPTION => VALUE ... ]
2951 Returns a list of FS::part_svc objects representing provisioned and available
2952 services included in this package. Each FS::part_svc object also has the
2953 following extra fields:
2957 =item num_cust_svc (count)
2959 =item num_avail (quantity - count)
2961 =item cust_pkg_svc (services) - array reference containing the provisioned services, as cust_svc objects
2965 Accepts one option: summarize_size. If specified and non-zero, will omit the
2966 extra cust_pkg_svc option for objects where num_cust_svc is this size or
2972 #label -> ($cust_svc->label)[1]
2978 my $pkg_quantity = $self->quantity || 1;
2980 #XXX some sort of sort order besides numeric by svcpart...
2981 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
2983 my $part_svc = $pkg_svc->part_svc;
2984 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
2985 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
2986 $part_svc->{'Hash'}{'num_avail'} =
2987 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
2988 $part_svc->{'Hash'}{'cust_pkg_svc'} =
2989 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
2990 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
2991 && $num_cust_svc >= $opt{summarize_size};
2992 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
2994 } $self->part_pkg->pkg_svc;
2997 push @part_svc, map {
2999 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3000 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
3001 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
3002 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3003 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
3005 } $self->extra_part_svc;
3011 =item extra_part_svc
3013 Returns a list of FS::part_svc objects corresponding to services in this
3014 package which are still provisioned but not (any longer) available in the
3019 sub extra_part_svc {
3022 my $pkgnum = $self->pkgnum;
3023 #my $pkgpart = $self->pkgpart;
3026 # 'table' => 'part_svc',
3029 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
3030 # WHERE pkg_svc.svcpart = part_svc.svcpart
3031 # AND pkg_svc.pkgpart = ?
3034 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
3035 # LEFT JOIN cust_pkg USING ( pkgnum )
3036 # WHERE cust_svc.svcpart = part_svc.svcpart
3039 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
3042 #seems to benchmark slightly faster... (or did?)
3044 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
3045 my $pkgparts = join(',', @pkgparts);
3048 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
3049 #MySQL doesn't grok DISINCT ON
3050 'select' => 'DISTINCT part_svc.*',
3051 'table' => 'part_svc',
3053 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
3054 AND pkg_svc.pkgpart IN ($pkgparts)
3057 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
3058 LEFT JOIN cust_pkg USING ( pkgnum )
3061 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
3062 'extra_param' => [ [$self->pkgnum=>'int'] ],
3068 Returns a short status string for this package, currently:
3072 =item not yet billed
3074 =item one-time charge
3089 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
3091 return 'cancelled' if $self->get('cancel');
3092 return 'suspended' if $self->susp;
3093 return 'not yet billed' unless $self->setup;
3094 return 'one-time charge' if $freq =~ /^(0|$)/;
3098 =item ucfirst_status
3100 Returns the status with the first character capitalized.
3104 sub ucfirst_status {
3105 ucfirst(shift->status);
3110 Class method that returns the list of possible status strings for packages
3111 (see L<the status method|/status>). For example:
3113 @statuses = FS::cust_pkg->statuses();
3117 tie my %statuscolor, 'Tie::IxHash',
3118 'not yet billed' => '009999', #teal? cyan?
3119 'one-time charge' => '000000',
3120 'active' => '00CC00',
3121 'suspended' => 'FF9900',
3122 'cancelled' => 'FF0000',
3126 my $self = shift; #could be class...
3127 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
3128 # # mayble split btw one-time vs. recur
3134 Returns a hex triplet color string for this package's status.
3140 $statuscolor{$self->status};
3145 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
3146 "pkg - comment" depending on user preference).
3152 my $label = $self->part_pkg->pkg_comment( 'nopkgpart' => 1 );
3153 $label = $self->pkgnum. ": $label"
3154 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
3158 =item pkg_label_long
3160 Returns a long label for this package, adding the primary service's label to
3165 sub pkg_label_long {
3167 my $label = $self->pkg_label;
3168 my $cust_svc = $self->primary_cust_svc;
3169 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
3175 Returns a customer-localized label for this package.
3181 $self->part_pkg->pkg_locale( $self->cust_main->locale );
3184 =item primary_cust_svc
3186 Returns a primary service (as FS::cust_svc object) if one can be identified.
3190 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
3192 sub primary_cust_svc {
3195 my @cust_svc = $self->cust_svc;
3197 return '' unless @cust_svc; #no serivces - irrelevant then
3199 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
3201 # primary service as specified in the package definition
3202 # or exactly one service definition with quantity one
3203 my $svcpart = $self->part_pkg->svcpart;
3204 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
3205 return $cust_svc[0] if scalar(@cust_svc) == 1;
3207 #couldn't identify one thing..
3213 Returns a list of lists, calling the label method for all services
3214 (see L<FS::cust_svc>) of this billing item.
3220 map { [ $_->label ] } $self->cust_svc;
3223 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3225 Like the labels method, but returns historical information on services that
3226 were active as of END_TIMESTAMP and (optionally) not cancelled before
3227 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
3228 I<pkg_svc.hidden> flag will be omitted.
3230 Returns a list of lists, calling the label method for all (historical) services
3231 (see L<FS::h_cust_svc>) of this billing item.
3237 warn "$me _h_labels called on $self\n"
3239 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
3244 Like labels, except returns a simple flat list, and shortens long
3245 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3246 identical services to one line that lists the service label and the number of
3247 individual services rather than individual items.
3252 shift->_labels_short( 'labels', @_ );
3255 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
3257 Like h_labels, except returns a simple flat list, and shortens long
3258 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3259 identical services to one line that lists the service label and the number of
3260 individual services rather than individual items.
3264 sub h_labels_short {
3265 shift->_labels_short( 'h_labels', @_ );
3269 my( $self, $method ) = ( shift, shift );
3271 warn "$me _labels_short called on $self with $method method\n"
3274 my $conf = new FS::Conf;
3275 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
3277 warn "$me _labels_short populating \%labels\n"
3281 #tie %labels, 'Tie::IxHash';
3282 push @{ $labels{$_->[0]} }, $_->[1]
3283 foreach $self->$method(@_);
3285 warn "$me _labels_short populating \@labels\n"
3289 foreach my $label ( keys %labels ) {
3291 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
3292 my $num = scalar(@values);
3293 warn "$me _labels_short $num items for $label\n"
3296 if ( $num > $max_same_services ) {
3297 warn "$me _labels_short more than $max_same_services, so summarizing\n"
3299 push @labels, "$label ($num)";
3301 if ( $conf->exists('cust_bill-consolidate_services') ) {
3302 warn "$me _labels_short consolidating services\n"
3304 # push @labels, "$label: ". join(', ', @values);
3306 my $detail = "$label: ";
3307 $detail .= shift(@values). ', '
3309 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
3311 push @labels, $detail;
3313 warn "$me _labels_short done consolidating services\n"
3316 warn "$me _labels_short adding service data\n"
3318 push @labels, map { "$label: $_" } @values;
3329 Returns the parent customer object (see L<FS::cust_main>).
3333 Returns the balance for this specific package, when using
3334 experimental package balance.
3340 $self->cust_main->balance_pkgnum( $self->pkgnum );
3343 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
3347 Returns the location object, if any (see L<FS::cust_location>).
3349 =item cust_location_or_main
3351 If this package is associated with a location, returns the locaiton (see
3352 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
3354 =item location_label [ OPTION => VALUE ... ]
3356 Returns the label of the location object (see L<FS::cust_location>).
3360 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
3362 =item tax_locationnum
3364 Returns the foreign key to a L<FS::cust_location> object for calculating
3365 tax on this package, as determined by the C<tax-pkg_address> and
3366 C<tax-ship_address> configuration flags.
3370 sub tax_locationnum {
3372 my $conf = FS::Conf->new;
3373 if ( $conf->exists('tax-pkg_address') ) {
3374 return $self->locationnum;
3376 elsif ( $conf->exists('tax-ship_address') ) {
3377 return $self->cust_main->ship_locationnum;
3380 return $self->cust_main->bill_locationnum;
3386 Returns the L<FS::cust_location> object for tax_locationnum.
3392 FS::cust_location->by_key( $self->tax_locationnum )
3395 =item seconds_since TIMESTAMP
3397 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
3398 package have been online since TIMESTAMP, according to the session monitor.
3400 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
3401 L<Time::Local> and L<Date::Parse> for conversion functions.
3406 my($self, $since) = @_;
3409 foreach my $cust_svc (
3410 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
3412 $seconds += $cust_svc->seconds_since($since);
3419 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
3421 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
3422 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
3425 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3426 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3432 sub seconds_since_sqlradacct {
3433 my($self, $start, $end) = @_;
3437 foreach my $cust_svc (
3439 my $part_svc = $_->part_svc;
3440 $part_svc->svcdb eq 'svc_acct'
3441 && scalar($part_svc->part_export_usage);
3444 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
3451 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
3453 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
3454 in this package for sessions ending between TIMESTAMP_START (inclusive) and
3458 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3459 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3464 sub attribute_since_sqlradacct {
3465 my($self, $start, $end, $attrib) = @_;
3469 foreach my $cust_svc (
3471 my $part_svc = $_->part_svc;
3472 scalar($part_svc->part_export_usage);
3475 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
3487 my( $self, $value ) = @_;
3488 if ( defined($value) ) {
3489 $self->setfield('quantity', $value);
3491 $self->getfield('quantity') || 1;
3494 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
3496 Transfers as many services as possible from this package to another package.
3498 The destination package can be specified by pkgnum by passing an FS::cust_pkg
3499 object. The destination package must already exist.
3501 Services are moved only if the destination allows services with the correct
3502 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
3503 this option with caution! No provision is made for export differences
3504 between the old and new service definitions. Probably only should be used
3505 when your exports for all service definitions of a given svcdb are identical.
3506 (attempt a transfer without it first, to move all possible svcpart-matching
3509 Any services that can't be moved remain in the original package.
3511 Returns an error, if there is one; otherwise, returns the number of services
3512 that couldn't be moved.
3517 my ($self, $dest_pkgnum, %opt) = @_;
3523 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
3524 $dest = $dest_pkgnum;
3525 $dest_pkgnum = $dest->pkgnum;
3527 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
3530 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
3532 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
3533 $target{$pkg_svc->svcpart} = $pkg_svc->quantity;
3536 foreach my $cust_svc ($dest->cust_svc) {
3537 $target{$cust_svc->svcpart}--;
3540 my %svcpart2svcparts = ();
3541 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3542 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
3543 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
3544 next if exists $svcpart2svcparts{$svcpart};
3545 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
3546 $svcpart2svcparts{$svcpart} = [
3548 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
3550 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
3551 'svcpart' => $_ } );
3553 $pkg_svc ? $pkg_svc->primary_svc : '',
3554 $pkg_svc ? $pkg_svc->quantity : 0,
3558 grep { $_ != $svcpart }
3560 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
3562 warn "alternates for svcpart $svcpart: ".
3563 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
3568 foreach my $cust_svc ($self->cust_svc) {
3569 if($target{$cust_svc->svcpart} > 0
3570 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
3571 $target{$cust_svc->svcpart}--;
3572 my $new = new FS::cust_svc { $cust_svc->hash };
3573 $new->pkgnum($dest_pkgnum);
3574 my $error = $new->replace($cust_svc);
3575 return $error if $error;
3576 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3578 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
3579 warn "alternates to consider: ".
3580 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
3582 my @alternate = grep {
3583 warn "considering alternate svcpart $_: ".
3584 "$target{$_} available in new package\n"
3587 } @{$svcpart2svcparts{$cust_svc->svcpart}};
3589 warn "alternate(s) found\n" if $DEBUG;
3590 my $change_svcpart = $alternate[0];
3591 $target{$change_svcpart}--;
3592 my $new = new FS::cust_svc { $cust_svc->hash };
3593 $new->svcpart($change_svcpart);
3594 $new->pkgnum($dest_pkgnum);
3595 my $error = $new->replace($cust_svc);
3596 return $error if $error;
3607 =item grab_svcnums SVCNUM, SVCNUM ...
3609 Change the pkgnum for the provided services to this packages. If there is an
3610 error, returns the error, otherwise returns false.
3618 local $SIG{HUP} = 'IGNORE';
3619 local $SIG{INT} = 'IGNORE';
3620 local $SIG{QUIT} = 'IGNORE';
3621 local $SIG{TERM} = 'IGNORE';
3622 local $SIG{TSTP} = 'IGNORE';
3623 local $SIG{PIPE} = 'IGNORE';
3625 my $oldAutoCommit = $FS::UID::AutoCommit;
3626 local $FS::UID::AutoCommit = 0;
3629 foreach my $svcnum (@svcnum) {
3630 my $cust_svc = qsearchs('cust_svc', { svcnum=>$svcnum } ) or do {
3631 $dbh->rollback if $oldAutoCommit;
3632 return "unknown svcnum $svcnum";
3634 $cust_svc->pkgnum( $self->pkgnum );
3635 my $error = $cust_svc->replace;
3637 $dbh->rollback if $oldAutoCommit;
3642 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3649 This method is deprecated. See the I<depend_jobnum> option to the insert and
3650 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
3654 #looks like this is still used by the order_pkg and change_pkg methods in
3655 # ClientAPI/MyAccount, need to look into those before removing
3659 local $SIG{HUP} = 'IGNORE';
3660 local $SIG{INT} = 'IGNORE';
3661 local $SIG{QUIT} = 'IGNORE';
3662 local $SIG{TERM} = 'IGNORE';
3663 local $SIG{TSTP} = 'IGNORE';
3664 local $SIG{PIPE} = 'IGNORE';
3666 my $oldAutoCommit = $FS::UID::AutoCommit;
3667 local $FS::UID::AutoCommit = 0;
3670 foreach my $cust_svc ( $self->cust_svc ) {
3671 #false laziness w/svc_Common::insert
3672 my $svc_x = $cust_svc->svc_x;
3673 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
3674 my $error = $part_export->export_insert($svc_x);
3676 $dbh->rollback if $oldAutoCommit;
3682 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3687 =item export_pkg_change OLD_CUST_PKG
3689 Calls the "pkg_change" export action for all services attached to this package.
3693 sub export_pkg_change {
3694 my( $self, $old ) = ( shift, shift );
3696 local $SIG{HUP} = 'IGNORE';
3697 local $SIG{INT} = 'IGNORE';
3698 local $SIG{QUIT} = 'IGNORE';
3699 local $SIG{TERM} = 'IGNORE';
3700 local $SIG{TSTP} = 'IGNORE';
3701 local $SIG{PIPE} = 'IGNORE';
3703 my $oldAutoCommit = $FS::UID::AutoCommit;
3704 local $FS::UID::AutoCommit = 0;
3707 foreach my $svc_x ( map $_->svc_x, $self->cust_svc ) {
3708 my $error = $svc_x->export('pkg_change', $self, $old);
3710 $dbh->rollback if $oldAutoCommit;
3715 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3722 Associates this package with a (suspension or cancellation) reason (see
3723 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
3726 Available options are:
3732 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.
3736 the access_user (see L<FS::access_user>) providing the reason
3744 the action (cancel, susp, adjourn, expire) associated with the reason
3748 If there is an error, returns the error, otherwise returns false.
3753 my ($self, %options) = @_;
3755 my $otaker = $options{reason_otaker} ||
3756 $FS::CurrentUser::CurrentUser->username;
3759 if ( $options{'reason'} =~ /^(\d+)$/ ) {
3763 } elsif ( ref($options{'reason'}) ) {
3765 return 'Enter a new reason (or select an existing one)'
3766 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
3768 my $reason = new FS::reason({
3769 'reason_type' => $options{'reason'}->{'typenum'},
3770 'reason' => $options{'reason'}->{'reason'},
3772 my $error = $reason->insert;
3773 return $error if $error;
3775 $reasonnum = $reason->reasonnum;
3778 return "Unparsable reason: ". $options{'reason'};
3781 my $cust_pkg_reason =
3782 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
3783 'reasonnum' => $reasonnum,
3784 'otaker' => $otaker,
3785 'action' => substr(uc($options{'action'}),0,1),
3786 'date' => $options{'date'}
3791 $cust_pkg_reason->insert;
3794 =item insert_discount
3796 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
3797 inserting a new discount on the fly (see L<FS::discount>).
3799 Available options are:
3807 If there is an error, returns the error, otherwise returns false.
3811 sub insert_discount {
3812 #my ($self, %options) = @_;
3815 my $cust_pkg_discount = new FS::cust_pkg_discount {
3816 'pkgnum' => $self->pkgnum,
3817 'discountnum' => $self->discountnum,
3819 'end_date' => '', #XXX
3820 #for the create a new discount case
3821 '_type' => $self->discountnum__type,
3822 'amount' => $self->discountnum_amount,
3823 'percent' => $self->discountnum_percent,
3824 'months' => $self->discountnum_months,
3825 'setup' => $self->discountnum_setup,
3826 #'disabled' => $self->discountnum_disabled,
3829 $cust_pkg_discount->insert;
3832 =item set_usage USAGE_VALUE_HASHREF
3834 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3835 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3836 upbytes, downbytes, and totalbytes are appropriate keys.
3838 All svc_accts which are part of this package have their values reset.
3843 my ($self, $valueref, %opt) = @_;
3845 #only svc_acct can set_usage for now
3846 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3847 my $svc_x = $cust_svc->svc_x;
3848 $svc_x->set_usage($valueref, %opt)
3849 if $svc_x->can("set_usage");
3853 =item recharge USAGE_VALUE_HASHREF
3855 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3856 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3857 upbytes, downbytes, and totalbytes are appropriate keys.
3859 All svc_accts which are part of this package have their values incremented.
3864 my ($self, $valueref) = @_;
3866 #only svc_acct can set_usage for now
3867 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3868 my $svc_x = $cust_svc->svc_x;
3869 $svc_x->recharge($valueref)
3870 if $svc_x->can("recharge");
3874 =item cust_pkg_discount
3876 =item cust_pkg_discount_active
3880 sub cust_pkg_discount_active {
3882 grep { $_->status eq 'active' } $self->cust_pkg_discount;
3885 =item cust_pkg_usage
3887 Returns a list of all voice usage counters attached to this package.
3889 =item apply_usage OPTIONS
3891 Takes the following options:
3892 - cdr: a call detail record (L<FS::cdr>)
3893 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
3894 - minutes: the maximum number of minutes to be charged
3896 Finds available usage minutes for a call of this class, and subtracts
3897 up to that many minutes from the usage pool. If the usage pool is empty,
3898 and the C<cdr-minutes_priority> global config option is set, minutes may
3899 be taken from other calls as well. Either way, an allocation record will
3900 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
3901 number of minutes of usage applied to the call.
3906 my ($self, %opt) = @_;
3907 my $cdr = $opt{cdr};
3908 my $rate_detail = $opt{rate_detail};
3909 my $minutes = $opt{minutes};
3910 my $classnum = $rate_detail->classnum;
3911 my $pkgnum = $self->pkgnum;
3912 my $custnum = $self->custnum;
3914 local $SIG{HUP} = 'IGNORE';
3915 local $SIG{INT} = 'IGNORE';
3916 local $SIG{QUIT} = 'IGNORE';
3917 local $SIG{TERM} = 'IGNORE';
3918 local $SIG{TSTP} = 'IGNORE';
3919 local $SIG{PIPE} = 'IGNORE';
3921 my $oldAutoCommit = $FS::UID::AutoCommit;
3922 local $FS::UID::AutoCommit = 0;
3924 my $order = FS::Conf->new->config('cdr-minutes_priority');
3928 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
3930 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
3932 my @usage_recs = qsearch({
3933 'table' => 'cust_pkg_usage',
3934 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
3935 ' JOIN cust_pkg USING (pkgnum)'.
3936 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
3937 'select' => 'cust_pkg_usage.*',
3938 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
3939 " ( cust_pkg.custnum = $custnum AND ".
3940 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3941 $is_classnum . ' AND '.
3942 " cust_pkg_usage.minutes > 0",
3943 'order_by' => " ORDER BY priority ASC",
3946 my $orig_minutes = $minutes;
3948 while (!$error and $minutes > 0 and @usage_recs) {
3949 my $cust_pkg_usage = shift @usage_recs;
3950 $cust_pkg_usage->select_for_update;
3951 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
3952 pkgusagenum => $cust_pkg_usage->pkgusagenum,
3953 acctid => $cdr->acctid,
3954 minutes => min($cust_pkg_usage->minutes, $minutes),
3956 $cust_pkg_usage->set('minutes',
3957 sprintf('%.0f', $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes)
3959 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
3960 $minutes -= $cdr_cust_pkg_usage->minutes;
3962 if ( $order and $minutes > 0 and !$error ) {
3963 # then try to steal minutes from another call
3965 'table' => 'cdr_cust_pkg_usage',
3966 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
3967 ' JOIN part_pkg_usage USING (pkgusagepart)'.
3968 ' JOIN cust_pkg USING (pkgnum)'.
3969 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
3970 ' JOIN cdr USING (acctid)',
3971 'select' => 'cdr_cust_pkg_usage.*',
3972 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
3973 " ( cust_pkg.pkgnum = $pkgnum OR ".
3974 " ( cust_pkg.custnum = $custnum AND ".
3975 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3976 " part_pkg_usage_class.classnum = $classnum",
3977 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
3979 if ( $order eq 'time' ) {
3980 # find CDRs that are using minutes, but have a later startdate
3982 my $startdate = $cdr->startdate;
3983 if ($startdate !~ /^\d+$/) {
3984 die "bad cdr startdate '$startdate'";
3986 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
3987 # minimize needless reshuffling
3988 $search{'order_by'} .= ', cdr.startdate DESC';
3990 # XXX may not work correctly with rate_time schedules. Could
3991 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
3993 $search{'addl_from'} .=
3994 ' JOIN rate_detail'.
3995 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
3996 if ( $order eq 'rate_high' ) {
3997 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
3998 $rate_detail->min_charge;
3999 $search{'order_by'} .= ', rate_detail.min_charge ASC';
4000 } elsif ( $order eq 'rate_low' ) {
4001 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
4002 $rate_detail->min_charge;
4003 $search{'order_by'} .= ', rate_detail.min_charge DESC';
4005 # this should really never happen
4006 die "invalid cdr-minutes_priority value '$order'\n";
4009 my @cdr_usage_recs = qsearch(\%search);
4011 while (!$error and @cdr_usage_recs and $minutes > 0) {
4012 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
4013 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
4014 my $old_cdr = $cdr_cust_pkg_usage->cdr;
4015 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
4016 $cdr_cust_pkg_usage->select_for_update;
4017 $old_cdr->select_for_update;
4018 $cust_pkg_usage->select_for_update;
4019 # in case someone else stole the usage from this CDR
4020 # while waiting for the lock...
4021 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
4022 # steal the usage allocation and flag the old CDR for reprocessing
4023 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
4024 # if the allocation is more minutes than we need, adjust it...
4025 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
4027 $cdr_cust_pkg_usage->set('minutes', $minutes);
4028 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
4029 $error = $cust_pkg_usage->replace;
4031 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
4032 $error ||= $cdr_cust_pkg_usage->replace;
4033 # deduct the stolen minutes
4034 $minutes -= $cdr_cust_pkg_usage->minutes;
4036 # after all minute-stealing is done, reset the affected CDRs
4037 foreach (values %reproc_cdrs) {
4038 $error ||= $_->set_status('');
4039 # XXX or should we just call $cdr->rate right here?
4040 # it's not like we can create a loop this way, since the min_charge
4041 # or call time has to go monotonically in one direction.
4042 # we COULD get some very deep recursions going, though...
4044 } # if $order and $minutes
4047 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
4049 $dbh->commit if $oldAutoCommit;
4050 return $orig_minutes - $minutes;
4054 =item supplemental_pkgs
4056 Returns a list of all packages supplemental to this one.
4060 sub supplemental_pkgs {
4062 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
4067 Returns the package that this one is supplemental to, if any.
4073 if ( $self->main_pkgnum ) {
4074 return FS::cust_pkg->by_key($self->main_pkgnum);
4081 =head1 CLASS METHODS
4087 Returns an SQL expression identifying recurring packages.
4091 sub recurring_sql { "
4092 '0' != ( select freq from part_pkg
4093 where cust_pkg.pkgpart = part_pkg.pkgpart )
4098 Returns an SQL expression identifying one-time packages.
4103 '0' = ( select freq from part_pkg
4104 where cust_pkg.pkgpart = part_pkg.pkgpart )
4109 Returns an SQL expression identifying ordered packages (recurring packages not
4115 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
4120 Returns an SQL expression identifying active packages.
4125 $_[0]->recurring_sql. "
4126 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4127 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4128 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4131 =item not_yet_billed_sql
4133 Returns an SQL expression identifying packages which have not yet been billed.
4137 sub not_yet_billed_sql { "
4138 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4139 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4140 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4145 Returns an SQL expression identifying inactive packages (one-time packages
4146 that are otherwise unsuspended/uncancelled).
4150 sub inactive_sql { "
4151 ". $_[0]->onetime_sql(). "
4152 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4153 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4154 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4160 Returns an SQL expression identifying suspended packages.
4164 sub suspended_sql { susp_sql(@_); }
4166 #$_[0]->recurring_sql(). ' AND '.
4168 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4169 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4176 Returns an SQL exprression identifying cancelled packages.
4180 sub cancelled_sql { cancel_sql(@_); }
4182 #$_[0]->recurring_sql(). ' AND '.
4183 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
4188 Returns an SQL expression to give the package status as a string.
4194 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
4195 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
4196 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
4197 WHEN ".onetime_sql()." THEN 'one-time charge'
4202 =item search HASHREF
4206 Returns a qsearch hash expression to search for parameters specified in HASHREF.
4207 Valid parameters are
4215 active, inactive, suspended, cancel (or cancelled)
4219 active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
4223 boolean selects custom packages
4229 pkgpart or arrayref or hashref of pkgparts
4233 arrayref of beginning and ending epoch date
4237 arrayref of beginning and ending epoch date
4241 arrayref of beginning and ending epoch date
4245 arrayref of beginning and ending epoch date
4249 arrayref of beginning and ending epoch date
4253 arrayref of beginning and ending epoch date
4257 arrayref of beginning and ending epoch date
4261 pkgnum or APKG_pkgnum
4265 a value suited to passing to FS::UI::Web::cust_header
4269 specifies the user for agent virtualization
4273 boolean; if true, returns only packages with more than 0 FCC phone lines.
4275 =item state, country
4277 Limit to packages with a service location in the specified state and country.
4278 For FCC 477 reporting, mostly.
4282 Limit to packages whose service locations are the same as the customer's
4283 default service location.
4285 =item location_nocust
4287 Limit to packages whose service locations are not the customer's default
4290 =item location_census
4292 Limit to packages whose service locations have census tracts.
4294 =item location_nocensus
4296 Limit to packages whose service locations do not have a census tract.
4298 =item location_geocode
4300 Limit to packages whose locations have geocodes.
4302 =item location_geocode
4304 Limit to packages whose locations do not have geocodes.
4311 my ($class, $params) = @_;
4318 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
4320 "cust_main.agentnum = $1";
4327 if ( $params->{'cust_status'} =~ /^([a-z]+)$/ ) {
4328 push @where, FS::cust_main->cust_status_sql . " = '$1' ";
4332 # parse customer sales person
4335 if ( $params->{'cust_main_salesnum'} =~ /^(\d+)$/ ) {
4336 push @where, ($1 > 0) ? "cust_main.salesnum = $1"
4337 : 'cust_main.salesnum IS NULL';
4342 # parse sales person
4345 if ( $params->{'salesnum'} =~ /^(\d+)$/ ) {
4346 push @where, ($1 > 0) ? "cust_pkg.salesnum = $1"
4347 : 'cust_pkg.salesnum IS NULL';
4354 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
4356 "cust_pkg.custnum = $1";
4363 if ( $params->{'pkgbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
4365 "cust_pkg.pkgbatch = '$1'";
4372 if ( $params->{'magic'} eq 'active'
4373 || $params->{'status'} eq 'active' ) {
4375 push @where, FS::cust_pkg->active_sql();
4377 } elsif ( $params->{'magic'} =~ /^not[ _]yet[ _]billed$/
4378 || $params->{'status'} =~ /^not[ _]yet[ _]billed$/ ) {
4380 push @where, FS::cust_pkg->not_yet_billed_sql();
4382 } elsif ( $params->{'magic'} =~ /^(one-time charge|inactive)/
4383 || $params->{'status'} =~ /^(one-time charge|inactive)/ ) {
4385 push @where, FS::cust_pkg->inactive_sql();
4387 } elsif ( $params->{'magic'} eq 'suspended'
4388 || $params->{'status'} eq 'suspended' ) {
4390 push @where, FS::cust_pkg->suspended_sql();
4392 } elsif ( $params->{'magic'} =~ /^cancell?ed$/
4393 || $params->{'status'} =~ /^cancell?ed$/ ) {
4395 push @where, FS::cust_pkg->cancelled_sql();
4400 # parse package class
4403 if ( exists($params->{'classnum'}) ) {
4406 if ( ref($params->{'classnum'}) ) {
4408 if ( ref($params->{'classnum'}) eq 'HASH' ) {
4409 @classnum = grep $params->{'classnum'}{$_}, keys %{ $params->{'classnum'} };
4410 } elsif ( ref($params->{'classnum'}) eq 'ARRAY' ) {
4411 @classnum = @{ $params->{'classnum'} };
4413 die 'unhandled classnum ref '. $params->{'classnum'};
4417 } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
4424 my @nums = grep $_, @classnum;
4425 push @c_where, 'part_pkg.classnum IN ('. join(',',@nums). ')' if @nums;
4426 my $null = scalar( grep { $_ eq '' } @classnum );
4427 push @c_where, 'part_pkg.classnum IS NULL' if $null;
4429 if ( scalar(@c_where) == 1 ) {
4430 push @where, @c_where;
4431 } elsif ( @c_where ) {
4432 push @where, ' ( '. join(' OR ', @c_where). ' ) ';
4441 # parse package report options
4444 my @report_option = ();
4445 if ( exists($params->{'report_option'}) ) {
4446 if ( ref($params->{'report_option'}) eq 'ARRAY' ) {
4447 @report_option = @{ $params->{'report_option'} };
4448 } elsif ( $params->{'report_option'} =~ /^([,\d]*)$/ ) {
4449 @report_option = split(',', $1);
4454 if (@report_option) {
4455 # this will result in the empty set for the dangling comma case as it should
4457 map{ "0 < ( SELECT count(*) FROM part_pkg_option
4458 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
4459 AND optionname = 'report_option_$_'
4460 AND optionvalue = '1' )"
4464 foreach my $any ( grep /^report_option_any/, keys %$params ) {
4466 my @report_option_any = ();
4467 if ( ref($params->{$any}) eq 'ARRAY' ) {
4468 @report_option_any = @{ $params->{$any} };
4469 } elsif ( $params->{$any} =~ /^([,\d]*)$/ ) {
4470 @report_option_any = split(',', $1);
4473 if (@report_option_any) {
4474 # this will result in the empty set for the dangling comma case as it should
4475 push @where, ' ( '. join(' OR ',
4476 map{ "0 < ( SELECT count(*) FROM part_pkg_option
4477 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
4478 AND optionname = 'report_option_$_'
4479 AND optionvalue = '1' )"
4480 } @report_option_any
4490 push @where, "part_pkg.custom = 'Y'" if $params->{custom};
4496 push @where, "(part_pkg.fcc_ds0s > 0 OR pkg_class.fcc_ds0s > 0)"
4497 if $params->{fcc_line};
4503 if ( exists($params->{'censustract'}) ) {
4504 $params->{'censustract'} =~ /^([.\d]*)$/;
4505 my $censustract = "cust_location.censustract = '$1'";
4506 $censustract .= ' OR cust_location.censustract is NULL' unless $1;
4507 push @where, "( $censustract )";
4511 # parse censustract2
4513 if ( exists($params->{'censustract2'})
4514 && $params->{'censustract2'} =~ /^(\d*)$/
4518 push @where, "cust_location.censustract LIKE '$1%'";
4521 "( cust_location.censustract = '' OR cust_location.censustract IS NULL )";
4526 # parse country/state
4528 for (qw(state country)) { # parsing rules are the same for these
4529 if ( exists($params->{$_})
4530 && uc($params->{$_}) =~ /^([A-Z]{2})$/ )
4532 # XXX post-2.3 only--before that, state/country may be in cust_main
4533 push @where, "cust_location.$_ = '$1'";
4540 if ( $params->{location_cust} xor $params->{location_nocust} ) {
4541 my $op = $params->{location_cust} ? '=' : '!=';
4542 push @where, "cust_location.locationnum $op cust_main.ship_locationnum";
4544 if ( $params->{location_census} xor $params->{location_nocensus} ) {
4545 my $op = $params->{location_census} ? "IS NOT NULL" : "IS NULL";
4546 push @where, "cust_location.censustract $op";
4548 if ( $params->{location_geocode} xor $params->{location_nogeocode} ) {
4549 my $op = $params->{location_geocode} ? "IS NOT NULL" : "IS NULL";
4550 push @where, "cust_location.geocode $op";
4557 if ( ref($params->{'pkgpart'}) ) {
4560 if ( ref($params->{'pkgpart'}) eq 'HASH' ) {
4561 @pkgpart = grep $params->{'pkgpart'}{$_}, keys %{ $params->{'pkgpart'} };
4562 } elsif ( ref($params->{'pkgpart'}) eq 'ARRAY' ) {
4563 @pkgpart = @{ $params->{'pkgpart'} };
4565 die 'unhandled pkgpart ref '. $params->{'pkgpart'};
4568 @pkgpart = grep /^(\d+)$/, @pkgpart;
4570 push @where, 'pkgpart IN ('. join(',', @pkgpart). ')' if scalar(@pkgpart);
4572 } elsif ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
4573 push @where, "pkgpart = $1";
4582 #false laziness w/report_cust_pkg.html
4585 'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
4586 'active' => { 'susp'=>1, 'cancel'=>1 },
4587 'suspended' => { 'cancel' => 1 },
4592 if( exists($params->{'active'} ) ) {
4593 # This overrides all the other date-related fields
4594 my($beginning, $ending) = @{$params->{'active'}};
4596 "cust_pkg.setup IS NOT NULL",
4597 "cust_pkg.setup <= $ending",
4598 "(cust_pkg.cancel IS NULL OR cust_pkg.cancel >= $beginning )",
4599 "NOT (".FS::cust_pkg->onetime_sql . ")";
4602 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel )) {
4604 next unless exists($params->{$field});
4606 my($beginning, $ending) = @{$params->{$field}};
4608 next if $beginning == 0 && $ending == 4294967295;
4611 "cust_pkg.$field IS NOT NULL",
4612 "cust_pkg.$field >= $beginning",
4613 "cust_pkg.$field <= $ending";
4615 $orderby ||= "ORDER BY cust_pkg.$field";
4620 $orderby ||= 'ORDER BY bill';
4623 # parse magic, legacy, etc.
4626 if ( $params->{'magic'} &&
4627 $params->{'magic'} =~ /^(active|inactive|suspended|cancell?ed)$/
4630 $orderby = 'ORDER BY pkgnum';
4632 if ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
4633 push @where, "pkgpart = $1";
4636 } elsif ( $params->{'query'} eq 'pkgnum' ) {
4638 $orderby = 'ORDER BY pkgnum';
4640 } elsif ( $params->{'query'} eq 'APKG_pkgnum' ) {
4642 $orderby = 'ORDER BY pkgnum';
4645 SELECT count(*) FROM pkg_svc
4646 WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
4647 AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
4648 WHERE cust_svc.pkgnum = cust_pkg.pkgnum
4649 AND cust_svc.svcpart = pkg_svc.svcpart
4656 # setup queries, links, subs, etc. for the search
4659 # here is the agent virtualization
4660 if ($params->{CurrentUser}) {
4662 qsearchs('access_user', { username => $params->{CurrentUser} });
4665 push @where, $access_user->agentnums_sql('table'=>'cust_main');
4670 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql('table'=>'cust_main');
4673 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
4675 my $addl_from = 'LEFT JOIN part_pkg USING ( pkgpart ) '.
4676 'LEFT JOIN pkg_class ON ( part_pkg.classnum = pkg_class.classnum ) '.
4677 'LEFT JOIN cust_location USING ( locationnum ) '.
4678 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
4682 if ( $params->{'select_zip5'} ) {
4683 my $zip = 'cust_location.zip';
4685 $select = "DISTINCT substr($zip,1,5) as zip";
4686 $orderby = "ORDER BY substr($zip,1,5)";
4687 $count_query = "SELECT COUNT( DISTINCT substr($zip,1,5) )";
4689 $select = join(', ',
4691 ( map "part_pkg.$_", qw( pkg freq ) ),
4692 'pkg_class.classname',
4693 'cust_main.custnum AS cust_main_custnum',
4694 FS::UI::Web::cust_sql_fields(
4695 $params->{'cust_fields'}
4698 $count_query = 'SELECT COUNT(*)';
4701 $count_query .= " FROM cust_pkg $addl_from $extra_sql";
4704 'table' => 'cust_pkg',
4706 'select' => $select,
4707 'extra_sql' => $extra_sql,
4708 'order_by' => $orderby,
4709 'addl_from' => $addl_from,
4710 'count_query' => $count_query,
4717 Returns a list of two package counts. The first is a count of packages
4718 based on the supplied criteria and the second is the count of residential
4719 packages with those same criteria. Criteria are specified as in the search
4725 my ($class, $params) = @_;
4727 my $sql_query = $class->search( $params );
4729 my $count_sql = delete($sql_query->{'count_query'});
4730 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
4731 or die "couldn't parse count_sql";
4733 my $count_sth = dbh->prepare($count_sql)
4734 or die "Error preparing $count_sql: ". dbh->errstr;
4736 or die "Error executing $count_sql: ". $count_sth->errstr;
4737 my $count_arrayref = $count_sth->fetchrow_arrayref;
4739 return ( @$count_arrayref );
4743 =item tax_locationnum_sql
4745 Returns an SQL expression for the tax location for a package, based
4746 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
4750 sub tax_locationnum_sql {
4751 my $conf = FS::Conf->new;
4752 if ( $conf->exists('tax-pkg_address') ) {
4753 'cust_pkg.locationnum';
4755 elsif ( $conf->exists('tax-ship_address') ) {
4756 'cust_main.ship_locationnum';
4759 'cust_main.bill_locationnum';
4765 Returns a list: the first item is an SQL fragment identifying matching
4766 packages/customers via location (taking into account shipping and package
4767 address taxation, if enabled), and subsequent items are the parameters to
4768 substitute for the placeholders in that fragment.
4773 my($class, %opt) = @_;
4774 my $ornull = $opt{'ornull'};
4776 my $conf = new FS::Conf;
4778 # '?' placeholders in _location_sql_where
4779 my $x = $ornull ? 3 : 2;
4790 if ( $conf->exists('tax-ship_address') ) {
4793 ( ( ship_last IS NULL OR ship_last = '' )
4794 AND ". _location_sql_where('cust_main', '', $ornull ). "
4796 OR ( ship_last IS NOT NULL AND ship_last != ''
4797 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
4800 # AND payby != 'COMP'
4802 @main_param = ( @bill_param, @bill_param );
4806 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
4807 @main_param = @bill_param;
4813 if ( $conf->exists('tax-pkg_address') ) {
4815 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
4818 ( cust_pkg.locationnum IS NULL AND $main_where )
4819 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
4822 @param = ( @main_param, @bill_param );
4826 $where = $main_where;
4827 @param = @main_param;
4835 #subroutine, helper for location_sql
4836 sub _location_sql_where {
4838 my $prefix = @_ ? shift : '';
4839 my $ornull = @_ ? shift : '';
4841 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
4843 $ornull = $ornull ? ' OR ? IS NULL ' : '';
4845 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
4846 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
4847 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
4849 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
4851 # ( $table.${prefix}city = ? $or_empty_city $ornull )
4853 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4854 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4855 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
4856 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
4857 AND $table.${prefix}country = ?
4862 my( $self, $what ) = @_;
4864 my $what_show_zero = $what. '_show_zero';
4865 length($self->$what_show_zero())
4866 ? ($self->$what_show_zero() eq 'Y')
4867 : $self->part_pkg->$what_show_zero();
4874 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
4876 CUSTNUM is a customer (see L<FS::cust_main>)
4878 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4879 L<FS::part_pkg>) to order for this customer. Duplicates are of course
4882 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
4883 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
4884 new billing items. An error is returned if this is not possible (see
4885 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
4888 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4889 newly-created cust_pkg objects.
4891 REFNUM, if specified, will specify the FS::pkg_referral record to be created
4892 and inserted. Multiple FS::pkg_referral records can be created by
4893 setting I<refnum> to an array reference of refnums or a hash reference with
4894 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
4895 record will be created corresponding to cust_main.refnum.
4900 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
4902 my $conf = new FS::Conf;
4904 # Transactionize this whole mess
4905 local $SIG{HUP} = 'IGNORE';
4906 local $SIG{INT} = 'IGNORE';
4907 local $SIG{QUIT} = 'IGNORE';
4908 local $SIG{TERM} = 'IGNORE';
4909 local $SIG{TSTP} = 'IGNORE';
4910 local $SIG{PIPE} = 'IGNORE';
4912 my $oldAutoCommit = $FS::UID::AutoCommit;
4913 local $FS::UID::AutoCommit = 0;
4917 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
4918 # return "Customer not found: $custnum" unless $cust_main;
4920 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
4923 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4926 my $change = scalar(@old_cust_pkg) != 0;
4929 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
4931 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
4932 " to pkgpart ". $pkgparts->[0]. "\n"
4935 my $err_or_cust_pkg =
4936 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
4937 'refnum' => $refnum,
4940 unless (ref($err_or_cust_pkg)) {
4941 $dbh->rollback if $oldAutoCommit;
4942 return $err_or_cust_pkg;
4945 push @$return_cust_pkg, $err_or_cust_pkg;
4946 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4951 # Create the new packages.
4952 foreach my $pkgpart (@$pkgparts) {
4954 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
4956 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
4957 pkgpart => $pkgpart,
4961 $error = $cust_pkg->insert( 'change' => $change );
4962 push @$return_cust_pkg, $cust_pkg;
4964 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
4965 my $supp_pkg = FS::cust_pkg->new({
4966 custnum => $custnum,
4967 pkgpart => $link->dst_pkgpart,
4969 main_pkgnum => $cust_pkg->pkgnum,
4972 $error ||= $supp_pkg->insert( 'change' => $change );
4973 push @$return_cust_pkg, $supp_pkg;
4977 $dbh->rollback if $oldAutoCommit;
4982 # $return_cust_pkg now contains refs to all of the newly
4985 # Transfer services and cancel old packages.
4986 foreach my $old_pkg (@old_cust_pkg) {
4988 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
4991 foreach my $new_pkg (@$return_cust_pkg) {
4992 $error = $old_pkg->transfer($new_pkg);
4993 if ($error and $error == 0) {
4994 # $old_pkg->transfer failed.
4995 $dbh->rollback if $oldAutoCommit;
5000 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
5001 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
5002 foreach my $new_pkg (@$return_cust_pkg) {
5003 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
5004 if ($error and $error == 0) {
5005 # $old_pkg->transfer failed.
5006 $dbh->rollback if $oldAutoCommit;
5013 # Transfers were successful, but we went through all of the
5014 # new packages and still had services left on the old package.
5015 # We can't cancel the package under the circumstances, so abort.
5016 $dbh->rollback if $oldAutoCommit;
5017 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
5019 $error = $old_pkg->cancel( quiet=>1 );
5025 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5029 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
5031 A bulk change method to change packages for multiple customers.
5033 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
5034 L<FS::part_pkg>) to order for each customer. Duplicates are of course
5037 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
5038 replace. The services (see L<FS::cust_svc>) are moved to the
5039 new billing items. An error is returned if this is not possible (see
5042 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
5043 newly-created cust_pkg objects.
5048 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
5050 # Transactionize this whole mess
5051 local $SIG{HUP} = 'IGNORE';
5052 local $SIG{INT} = 'IGNORE';
5053 local $SIG{QUIT} = 'IGNORE';
5054 local $SIG{TERM} = 'IGNORE';
5055 local $SIG{TSTP} = 'IGNORE';
5056 local $SIG{PIPE} = 'IGNORE';
5058 my $oldAutoCommit = $FS::UID::AutoCommit;
5059 local $FS::UID::AutoCommit = 0;
5063 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
5066 while(scalar(@old_cust_pkg)) {
5068 my $custnum = $old_cust_pkg[0]->custnum;
5069 my (@remove) = map { $_->pkgnum }
5070 grep { $_->custnum == $custnum } @old_cust_pkg;
5071 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
5073 my $error = order $custnum, $pkgparts, \@remove, \@return;
5075 push @errors, $error
5077 push @$return_cust_pkg, @return;
5080 if (scalar(@errors)) {
5081 $dbh->rollback if $oldAutoCommit;
5082 return join(' / ', @errors);
5085 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5089 # Used by FS::Upgrade to migrate to a new database.
5090 sub _upgrade_data { # class method
5091 my ($class, %opts) = @_;
5092 $class->_upgrade_otaker(%opts);
5094 # RT#10139, bug resulting in contract_end being set when it shouldn't
5095 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
5096 # RT#10830, bad calculation of prorate date near end of year
5097 # the date range for bill is December 2009, and we move it forward
5098 # one year if it's before the previous bill date (which it should
5100 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
5101 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
5102 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
5103 # RT6628, add order_date to cust_pkg
5104 'update cust_pkg set order_date = (select history_date from h_cust_pkg
5105 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
5106 history_action = \'insert\') where order_date is null',
5108 foreach my $sql (@statements) {
5109 my $sth = dbh->prepare($sql);
5110 $sth->execute or die $sth->errstr;
5118 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
5120 In sub order, the @pkgparts array (passed by reference) is clobbered.
5122 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
5123 method to pass dates to the recur_prog expression, it should do so.
5125 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
5126 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
5127 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
5128 configuration values. Probably need a subroutine which decides what to do
5129 based on whether or not we've fetched the user yet, rather than a hash. See
5130 FS::UID and the TODO.
5132 Now that things are transactional should the check in the insert method be
5137 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
5138 L<FS::pkg_svc>, schema.html from the base documentation