4 use base qw( FS::otaker_Mixin FS::cust_main_Mixin
5 FS::contact_Mixin FS::location_Mixin
6 FS::m2m_Common FS::option_Common );
7 use vars qw($disable_agentcheck $DEBUG $me);
9 use Scalar::Util qw( blessed );
10 use List::Util qw(min max);
12 use Time::Local qw( timelocal timelocal_nocheck );
14 use FS::UID qw( dbh driver_name );
15 use FS::Misc qw( send_email );
16 use FS::Record qw( qsearch qsearchs fields );
22 use FS::cust_location;
24 use FS::cust_bill_pkg;
25 use FS::cust_pkg_detail;
26 use FS::cust_pkg_usage;
27 use FS::cdr_cust_pkg_usage;
32 use FS::cust_pkg_reason;
34 use FS::cust_pkg_discount;
39 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
41 # because they load configuration by setting FS::UID::callback (see TODO)
47 # for sending cancel emails in sub cancel
51 $me = '[FS::cust_pkg]';
53 $disable_agentcheck = 0;
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
272 a ticket will be added to this customer with this subject
276 an optional queue name for ticket additions
280 Don't check the legality of the package definition. This should be used
281 when performing a package change that doesn't change the pkgpart (i.e.
289 my( $self, %options ) = @_;
292 $error = $self->check_pkgpart unless $options{'allow_pkgpart'};
293 return $error if $error;
295 my $part_pkg = $self->part_pkg;
297 # if the package def says to start only on the first of the month:
298 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
299 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
300 $mon += 1 unless $mday == 1;
301 until ( $mon < 12 ) { $mon -= 12; $year++; }
302 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
305 # set up any automatic expire/adjourn/contract_end timers
306 # based on the start date
307 foreach my $action ( qw(expire adjourn contract_end) ) {
308 my $months = $part_pkg->option("${action}_months",1);
309 if($months and !$self->$action) {
310 my $start = $self->start_date || $self->setup || time;
311 $self->$action( $part_pkg->add_freq($start, $months) );
315 # if this package has "free days" and delayed setup fee, tehn
316 # set start date that many days in the future.
317 # (this should have been set in the UI, but enforce it here)
318 if ( ! $options{'change'}
319 && ( my $free_days = $part_pkg->option('free_days',1) )
320 && $part_pkg->option('delay_setup',1)
321 #&& ! $self->start_date
324 $self->start_date( $part_pkg->default_start_date );
327 $self->order_date(time);
329 local $SIG{HUP} = 'IGNORE';
330 local $SIG{INT} = 'IGNORE';
331 local $SIG{QUIT} = 'IGNORE';
332 local $SIG{TERM} = 'IGNORE';
333 local $SIG{TSTP} = 'IGNORE';
334 local $SIG{PIPE} = 'IGNORE';
336 my $oldAutoCommit = $FS::UID::AutoCommit;
337 local $FS::UID::AutoCommit = 0;
340 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
342 $dbh->rollback if $oldAutoCommit;
346 $self->refnum($self->cust_main->refnum) unless $self->refnum;
347 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
348 $self->process_m2m( 'link_table' => 'pkg_referral',
349 'target_table' => 'part_referral',
350 'params' => $self->refnum,
353 if ( $self->discountnum ) {
354 my $error = $self->insert_discount();
356 $dbh->rollback if $oldAutoCommit;
361 my $conf = new FS::Conf;
363 if ( $conf->config('ticket_system') && $options{ticket_subject} ) {
365 #this init stuff is still inefficient, but at least its limited to
366 # the small number (any?) folks using ticket emailing on pkg order
369 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
376 use FS::TicketSystem;
377 FS::TicketSystem->init();
379 my $q = new RT::Queue($RT::SystemUser);
380 $q->Load($options{ticket_queue}) if $options{ticket_queue};
381 my $t = new RT::Ticket($RT::SystemUser);
382 my $mime = new MIME::Entity;
383 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
384 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
385 Subject => $options{ticket_subject},
388 $t->AddLink( Type => 'MemberOf',
389 Target => 'freeside://freeside/cust_main/'. $self->custnum,
393 if ($conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
394 my $queue = new FS::queue {
395 'job' => 'FS::cust_main::queueable_print',
397 $error = $queue->insert(
398 'custnum' => $self->custnum,
399 'template' => 'welcome_letter',
403 warn "can't send welcome letter: $error";
408 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
415 This method now works but you probably shouldn't use it.
417 You don't want to delete packages, because there would then be no record
418 the customer ever purchased the package. Instead, see the cancel method and
419 hide cancelled packages.
426 local $SIG{HUP} = 'IGNORE';
427 local $SIG{INT} = 'IGNORE';
428 local $SIG{QUIT} = 'IGNORE';
429 local $SIG{TERM} = 'IGNORE';
430 local $SIG{TSTP} = 'IGNORE';
431 local $SIG{PIPE} = 'IGNORE';
433 my $oldAutoCommit = $FS::UID::AutoCommit;
434 local $FS::UID::AutoCommit = 0;
437 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
438 my $error = $cust_pkg_discount->delete;
440 $dbh->rollback if $oldAutoCommit;
444 #cust_bill_pkg_discount?
446 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
447 my $error = $cust_pkg_detail->delete;
449 $dbh->rollback if $oldAutoCommit;
454 foreach my $cust_pkg_reason (
456 'table' => 'cust_pkg_reason',
457 'hashref' => { 'pkgnum' => $self->pkgnum },
461 my $error = $cust_pkg_reason->delete;
463 $dbh->rollback if $oldAutoCommit;
470 my $error = $self->SUPER::delete(@_);
472 $dbh->rollback if $oldAutoCommit;
476 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
482 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
484 Replaces the OLD_RECORD with this one in the database. If there is an error,
485 returns the error, otherwise returns false.
487 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
489 Changing pkgpart may have disasterous effects. See the order subroutine.
491 setup and bill are normally updated by calling the bill method of a customer
492 object (see L<FS::cust_main>).
494 suspend is normally updated by the suspend and unsuspend methods.
496 cancel is normally updated by the cancel method (and also the order subroutine
499 Available options are:
505 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.
509 the access_user (see L<FS::access_user>) providing the reason
513 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
522 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
527 ( ref($_[0]) eq 'HASH' )
531 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
532 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
535 #return "Can't change setup once it exists!"
536 # if $old->getfield('setup') &&
537 # $old->getfield('setup') != $new->getfield('setup');
539 #some logic for bill, susp, cancel?
541 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
543 local $SIG{HUP} = 'IGNORE';
544 local $SIG{INT} = 'IGNORE';
545 local $SIG{QUIT} = 'IGNORE';
546 local $SIG{TERM} = 'IGNORE';
547 local $SIG{TSTP} = 'IGNORE';
548 local $SIG{PIPE} = 'IGNORE';
550 my $oldAutoCommit = $FS::UID::AutoCommit;
551 local $FS::UID::AutoCommit = 0;
554 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
555 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
556 my $error = $new->insert_reason(
557 'reason' => $options->{'reason'},
558 'date' => $new->$method,
560 'reason_otaker' => $options->{'reason_otaker'},
563 dbh->rollback if $oldAutoCommit;
564 return "Error inserting cust_pkg_reason: $error";
569 #save off and freeze RADIUS attributes for any associated svc_acct records
571 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
573 #also check for specific exports?
574 # to avoid spurious modify export events
575 @svc_acct = map { $_->svc_x }
576 grep { $_->part_svc->svcdb eq 'svc_acct' }
579 $_->snapshot foreach @svc_acct;
583 my $error = $new->export_pkg_change($old)
584 || $new->SUPER::replace( $old,
586 ? $options->{options}
590 $dbh->rollback if $oldAutoCommit;
594 #for prepaid packages,
595 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
596 foreach my $old_svc_acct ( @svc_acct ) {
597 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
599 $new_svc_acct->replace( $old_svc_acct,
600 'depend_jobnum' => $options->{depend_jobnum},
603 $dbh->rollback if $oldAutoCommit;
608 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
615 Checks all fields to make sure this is a valid billing item. If there is an
616 error, returns the error, otherwise returns false. Called by the insert and
624 if ( !$self->locationnum or $self->locationnum == -1 ) {
625 $self->set('locationnum', $self->cust_main->ship_locationnum);
629 $self->ut_numbern('pkgnum')
630 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
631 || $self->ut_numbern('pkgpart')
632 || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' )
633 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
634 || $self->ut_numbern('start_date')
635 || $self->ut_numbern('setup')
636 || $self->ut_numbern('bill')
637 || $self->ut_numbern('susp')
638 || $self->ut_numbern('cancel')
639 || $self->ut_numbern('adjourn')
640 || $self->ut_numbern('resume')
641 || $self->ut_numbern('expire')
642 || $self->ut_numbern('dundate')
643 || $self->ut_enum('no_auto', [ '', 'Y' ])
644 || $self->ut_enum('waive_setup', [ '', 'Y' ])
645 || $self->ut_numbern('agent_pkgid')
646 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
647 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
648 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
649 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
650 || $self->ut_foreign_keyn('change_to_pkgnum', 'cust_pkg', 'pkgnum')
652 return $error if $error;
654 return "A package with both start date (future start) and setup date (already started) will never bill"
655 if $self->start_date && $self->setup;
657 return "A future unsuspend date can only be set for a package with a suspend date"
658 if $self->resume and !$self->susp and !$self->adjourn;
660 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
662 if ( $self->dbdef_table->column('manual_flag') ) {
663 $self->manual_flag('') if $self->manual_flag eq ' ';
664 $self->manual_flag =~ /^([01]?)$/
665 or return "Illegal manual_flag ". $self->manual_flag;
666 $self->manual_flag($1);
674 Check the pkgpart to make sure it's allowed with the reg_code and/or
675 promo_code of the package (if present) and with the customer's agent.
676 Called from C<insert>, unless we are doing a package change that doesn't
684 # my $error = $self->ut_numbern('pkgpart'); # already done
687 if ( $self->reg_code ) {
689 unless ( grep { $self->pkgpart == $_->pkgpart }
690 map { $_->reg_code_pkg }
691 qsearchs( 'reg_code', { 'code' => $self->reg_code,
692 'agentnum' => $self->cust_main->agentnum })
694 return "Unknown registration code";
697 } elsif ( $self->promo_code ) {
700 qsearchs('part_pkg', {
701 'pkgpart' => $self->pkgpart,
702 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
704 return 'Unknown promotional code' unless $promo_part_pkg;
708 unless ( $disable_agentcheck ) {
710 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
711 return "agent ". $agent->agentnum. ':'. $agent->agent.
712 " can't purchase pkgpart ". $self->pkgpart
713 unless $agent->pkgpart_hashref->{ $self->pkgpart }
714 || $agent->agentnum == $self->part_pkg->agentnum;
717 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
718 return $error if $error;
726 =item cancel [ OPTION => VALUE ... ]
728 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
729 in this package, then cancels the package itself (sets the cancel field to
732 Available options are:
736 =item quiet - can be set true to supress email cancellation notices.
738 =item time - can be set to cancel the package based on a specific future or
739 historical date. Using time ensures that the remaining amount is calculated
740 correctly. Note however that this is an immediate cancel and just changes
741 the date. You are PROBABLY looking to expire the account instead of using
744 =item reason - can be set to a cancellation reason (see L<FS:reason>),
745 either a reasonnum of an existing reason, or passing a hashref will create
746 a new reason. The hashref should have the following keys: typenum - Reason
747 type (see L<FS::reason_type>, reason - Text of the new reason.
749 =item date - can be set to a unix style timestamp to specify when to
752 =item nobill - can be set true to skip billing if it might otherwise be done.
754 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
755 not credit it. This must be set (by change()) when changing the package
756 to a different pkgpart or location, and probably shouldn't be in any other
757 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
762 If there is an error, returns the error, otherwise returns false.
767 my( $self, %options ) = @_;
770 # pass all suspend/cancel actions to the main package
771 if ( $self->main_pkgnum and !$options{'from_main'} ) {
772 return $self->main_pkg->cancel(%options);
775 my $conf = new FS::Conf;
777 warn "cust_pkg::cancel called with options".
778 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
781 local $SIG{HUP} = 'IGNORE';
782 local $SIG{INT} = 'IGNORE';
783 local $SIG{QUIT} = 'IGNORE';
784 local $SIG{TERM} = 'IGNORE';
785 local $SIG{TSTP} = 'IGNORE';
786 local $SIG{PIPE} = 'IGNORE';
788 my $oldAutoCommit = $FS::UID::AutoCommit;
789 local $FS::UID::AutoCommit = 0;
792 my $old = $self->select_for_update;
794 if ( $old->get('cancel') || $self->get('cancel') ) {
795 dbh->rollback if $oldAutoCommit;
796 return ""; # no error
799 # XXX possibly set cancel_time to the expire date?
800 my $cancel_time = $options{'time'} || time;
801 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
802 $date = '' if ($date && $date <= $cancel_time); # complain instead?
804 #race condition: usage could be ongoing until unprovisioned
805 #resolved by performing a change package instead (which unprovisions) and
807 if ( !$options{nobill} && !$date ) {
808 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
809 my $copy = $self->new({$self->hash});
811 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
813 'time' => $cancel_time );
814 warn "Error billing during cancel, custnum ".
815 #$self->cust_main->custnum. ": $error"
820 if ( $options{'reason'} ) {
821 $error = $self->insert_reason( 'reason' => $options{'reason'},
822 'action' => $date ? 'expire' : 'cancel',
823 'date' => $date ? $date : $cancel_time,
824 'reason_otaker' => $options{'reason_otaker'},
827 dbh->rollback if $oldAutoCommit;
828 return "Error inserting cust_pkg_reason: $error";
832 my %svc_cancel_opt = ();
833 $svc_cancel_opt{'date'} = $date if $date;
834 foreach my $cust_svc (
837 sort { $a->[1] <=> $b->[1] }
838 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
839 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
841 my $part_svc = $cust_svc->part_svc;
842 next if ( defined($part_svc) and $part_svc->preserve );
843 my $error = $cust_svc->cancel( %svc_cancel_opt );
846 $dbh->rollback if $oldAutoCommit;
847 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
853 # credit remaining time if appropriate
855 if ( exists($options{'unused_credit'}) ) {
856 $do_credit = $options{'unused_credit'};
859 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
862 my $error = $self->credit_remaining('cancel', $cancel_time);
864 $dbh->rollback if $oldAutoCommit;
871 my %hash = $self->hash;
873 $hash{'expire'} = $date;
875 $hash{'cancel'} = $cancel_time;
877 $hash{'change_custnum'} = $options{'change_custnum'};
879 my $new = new FS::cust_pkg ( \%hash );
880 $error = $new->replace( $self, options => { $self->options } );
881 if ( $self->change_to_pkgnum ) {
882 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
883 $error ||= $change_to->cancel || $change_to->delete;
886 $dbh->rollback if $oldAutoCommit;
890 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
891 $error = $supp_pkg->cancel(%options, 'from_main' => 1);
893 $dbh->rollback if $oldAutoCommit;
894 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
898 foreach my $usage ( $self->cust_pkg_usage ) {
899 $error = $usage->delete;
901 $dbh->rollback if $oldAutoCommit;
902 return "deleting usage pools: $error";
906 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
907 return '' if $date; #no errors
909 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
910 if ( !$options{'quiet'} &&
911 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
913 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
916 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
917 $error = $msg_template->send( 'cust_main' => $self->cust_main,
922 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
923 'to' => \@invoicing_list,
924 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
925 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
928 #should this do something on errors?
935 =item cancel_if_expired [ NOW_TIMESTAMP ]
937 Cancels this package if its expire date has been reached.
941 sub cancel_if_expired {
943 my $time = shift || time;
944 return '' unless $self->expire && $self->expire <= $time;
945 my $error = $self->cancel;
947 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
948 $self->custnum. ": $error";
955 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
956 locationnum, (other fields?). Attempts to re-provision cancelled services
957 using history information (errors at this stage are not fatal).
959 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
961 svc_fatal: service provisioning errors are fatal
963 svc_errors: pass an array reference, will be filled in with any provisioning errors
965 main_pkgnum: link the package as a supplemental package of this one. For
971 my( $self, %options ) = @_;
973 #in case you try do do $uncancel-date = $cust_pkg->uncacel
974 return '' unless $self->get('cancel');
976 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
977 return $self->main_pkg->uncancel(%options);
984 local $SIG{HUP} = 'IGNORE';
985 local $SIG{INT} = 'IGNORE';
986 local $SIG{QUIT} = 'IGNORE';
987 local $SIG{TERM} = 'IGNORE';
988 local $SIG{TSTP} = 'IGNORE';
989 local $SIG{PIPE} = 'IGNORE';
991 my $oldAutoCommit = $FS::UID::AutoCommit;
992 local $FS::UID::AutoCommit = 0;
996 # insert the new package
999 my $cust_pkg = new FS::cust_pkg {
1000 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
1001 bill => ( $options{'bill'} || $self->get('bill') ),
1003 uncancel_pkgnum => $self->pkgnum,
1004 main_pkgnum => ($options{'main_pkgnum'} || ''),
1005 map { $_ => $self->get($_) } qw(
1006 custnum pkgpart locationnum
1008 susp adjourn resume expire start_date contract_end dundate
1009 change_date change_pkgpart change_locationnum
1010 manual_flag no_auto quantity agent_pkgid recur_show_zero setup_show_zero
1014 my $error = $cust_pkg->insert(
1015 'change' => 1, #supresses any referral credit to a referring customer
1016 'allow_pkgpart' => 1, # allow this even if the package def is disabled
1019 $dbh->rollback if $oldAutoCommit;
1027 #find historical services within this timeframe before the package cancel
1028 # (incompatible with "time" option to cust_pkg->cancel?)
1029 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
1030 # too little? (unprovisioing export delay?)
1031 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
1032 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1035 foreach my $h_cust_svc (@h_cust_svc) {
1036 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1037 #next unless $h_svc_x; #should this happen?
1038 (my $table = $h_svc_x->table) =~ s/^h_//;
1039 require "FS/$table.pm";
1040 my $class = "FS::$table";
1041 my $svc_x = $class->new( {
1042 'pkgnum' => $cust_pkg->pkgnum,
1043 'svcpart' => $h_cust_svc->svcpart,
1044 map { $_ => $h_svc_x->get($_) } fields($table)
1048 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1049 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1052 my $svc_error = $svc_x->insert;
1054 if ( $options{svc_fatal} ) {
1055 $dbh->rollback if $oldAutoCommit;
1058 # if we've failed to insert the svc_x object, svc_Common->insert
1059 # will have removed the cust_svc already. if not, then both records
1060 # were inserted but we failed for some other reason (export, most
1061 # likely). in that case, report the error and delete the records.
1062 push @svc_errors, $svc_error;
1063 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1065 # except if export_insert failed, export_delete probably won't be
1067 local $FS::svc_Common::noexport_hack = 1;
1068 my $cleanup_error = $svc_x->delete; # also deletes cust_svc
1069 if ( $cleanup_error ) { # and if THAT fails, then run away
1070 $dbh->rollback if $oldAutoCommit;
1071 return $cleanup_error;
1076 } #foreach $h_cust_svc
1078 #these are pretty rare, but should handle them
1079 # - dsl_device (mac addresses)
1080 # - phone_device (mac addresses)
1081 # - dsl_note (ikano notes)
1082 # - domain_record (i.e. restore DNS information w/domains)
1083 # - inventory_item(?) (inventory w/un-cancelling service?)
1084 # - nas (svc_broaband nas stuff)
1085 #this stuff is unused in the wild afaik
1086 # - mailinglistmember
1088 # - svc_domain.parent_svcnum?
1089 # - acct_snarf (ancient mail fetching config)
1090 # - cgp_rule (communigate)
1091 # - cust_svc_option (used by our Tron stuff)
1092 # - acct_rt_transaction (used by our time worked stuff)
1095 # also move over any services that didn't unprovision at cancellation
1098 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1099 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1100 my $error = $cust_svc->replace;
1102 $dbh->rollback if $oldAutoCommit;
1108 # Uncancel any supplemental packages, and make them supplemental to the
1112 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1114 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1116 $dbh->rollback if $oldAutoCommit;
1117 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1125 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1127 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1128 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1135 Cancels any pending expiration (sets the expire field to null).
1137 If there is an error, returns the error, otherwise returns false.
1142 my( $self, %options ) = @_;
1145 local $SIG{HUP} = 'IGNORE';
1146 local $SIG{INT} = 'IGNORE';
1147 local $SIG{QUIT} = 'IGNORE';
1148 local $SIG{TERM} = 'IGNORE';
1149 local $SIG{TSTP} = 'IGNORE';
1150 local $SIG{PIPE} = 'IGNORE';
1152 my $oldAutoCommit = $FS::UID::AutoCommit;
1153 local $FS::UID::AutoCommit = 0;
1156 my $old = $self->select_for_update;
1158 my $pkgnum = $old->pkgnum;
1159 if ( $old->get('cancel') || $self->get('cancel') ) {
1160 dbh->rollback if $oldAutoCommit;
1161 return "Can't unexpire cancelled package $pkgnum";
1162 # or at least it's pointless
1165 unless ( $old->get('expire') && $self->get('expire') ) {
1166 dbh->rollback if $oldAutoCommit;
1167 return ""; # no error
1170 my %hash = $self->hash;
1171 $hash{'expire'} = '';
1172 my $new = new FS::cust_pkg ( \%hash );
1173 $error = $new->replace( $self, options => { $self->options } );
1175 $dbh->rollback if $oldAutoCommit;
1179 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1185 =item suspend [ OPTION => VALUE ... ]
1187 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1188 package, then suspends the package itself (sets the susp field to now).
1190 Available options are:
1194 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1195 either a reasonnum of an existing reason, or passing a hashref will create
1196 a new reason. The hashref should have the following keys:
1197 - typenum - Reason type (see L<FS::reason_type>
1198 - reason - Text of the new reason.
1200 =item date - can be set to a unix style timestamp to specify when to
1203 =item time - can be set to override the current time, for calculation
1204 of final invoices or unused-time credits
1206 =item resume_date - can be set to a time when the package should be
1207 unsuspended. This may be more convenient than calling C<unsuspend()>
1210 =item from_main - allows a supplemental package to be suspended, rather
1211 than redirecting the method call to its main package. For internal use.
1215 If there is an error, returns the error, otherwise returns false.
1220 my( $self, %options ) = @_;
1223 # pass all suspend/cancel actions to the main package
1224 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1225 return $self->main_pkg->suspend(%options);
1228 local $SIG{HUP} = 'IGNORE';
1229 local $SIG{INT} = 'IGNORE';
1230 local $SIG{QUIT} = 'IGNORE';
1231 local $SIG{TERM} = 'IGNORE';
1232 local $SIG{TSTP} = 'IGNORE';
1233 local $SIG{PIPE} = 'IGNORE';
1235 my $oldAutoCommit = $FS::UID::AutoCommit;
1236 local $FS::UID::AutoCommit = 0;
1239 my $old = $self->select_for_update;
1241 my $pkgnum = $old->pkgnum;
1242 if ( $old->get('cancel') || $self->get('cancel') ) {
1243 dbh->rollback if $oldAutoCommit;
1244 return "Can't suspend cancelled package $pkgnum";
1247 if ( $old->get('susp') || $self->get('susp') ) {
1248 dbh->rollback if $oldAutoCommit;
1249 return ""; # no error # complain on adjourn?
1252 my $suspend_time = $options{'time'} || time;
1253 my $date = $options{date} if $options{date}; # adjourn/suspend later
1254 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1256 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1257 dbh->rollback if $oldAutoCommit;
1258 return "Package $pkgnum expires before it would be suspended.";
1261 # some false laziness with sub cancel
1262 if ( !$options{nobill} && !$date &&
1263 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1264 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1265 # make the entire cust_main->bill path recognize 'suspend' and
1266 # 'cancel' separately.
1267 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1268 my $copy = $self->new({$self->hash});
1270 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1272 'time' => $suspend_time );
1273 warn "Error billing during suspend, custnum ".
1274 #$self->cust_main->custnum. ": $error"
1279 if ( $options{'reason'} ) {
1280 $error = $self->insert_reason( 'reason' => $options{'reason'},
1281 'action' => $date ? 'adjourn' : 'suspend',
1282 'date' => $date ? $date : $suspend_time,
1283 'reason_otaker' => $options{'reason_otaker'},
1286 dbh->rollback if $oldAutoCommit;
1287 return "Error inserting cust_pkg_reason: $error";
1291 my %hash = $self->hash;
1293 $hash{'adjourn'} = $date;
1295 $hash{'susp'} = $suspend_time;
1298 my $resume_date = $options{'resume_date'} || 0;
1299 if ( $resume_date > ($date || $suspend_time) ) {
1300 $hash{'resume'} = $resume_date;
1303 $options{options} ||= {};
1305 my $new = new FS::cust_pkg ( \%hash );
1306 $error = $new->replace( $self, options => { $self->options,
1307 %{ $options{options} },
1311 $dbh->rollback if $oldAutoCommit;
1316 # credit remaining time if appropriate
1317 if ( $self->part_pkg->option('unused_credit_suspend', 1) ) {
1318 my $error = $self->credit_remaining('suspend', $suspend_time);
1320 $dbh->rollback if $oldAutoCommit;
1327 foreach my $cust_svc (
1328 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
1330 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1332 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1333 $dbh->rollback if $oldAutoCommit;
1334 return "Illegal svcdb value in part_svc!";
1337 require "FS/$svcdb.pm";
1339 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1341 $error = $svc->suspend;
1343 $dbh->rollback if $oldAutoCommit;
1346 my( $label, $value ) = $cust_svc->label;
1347 push @labels, "$label: $value";
1351 my $conf = new FS::Conf;
1352 if ( $conf->config('suspend_email_admin') ) {
1354 my $error = send_email(
1355 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1356 #invoice_from ??? well as good as any
1357 'to' => $conf->config('suspend_email_admin'),
1358 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1360 "This is an automatic message from your Freeside installation\n",
1361 "informing you that the following customer package has been suspended:\n",
1363 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1364 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1365 ( map { "Service : $_\n" } @labels ),
1370 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1378 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1379 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1381 $dbh->rollback if $oldAutoCommit;
1382 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1386 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1391 =item credit_remaining MODE TIME
1393 Generate a credit for this package for the time remaining in the current
1394 billing period. MODE is either "suspend" or "cancel" (determines the
1395 credit type). TIME is the time of suspension/cancellation. Both arguments
1400 sub credit_remaining {
1401 # Add a credit for remaining service
1402 my ($self, $mode, $time) = @_;
1403 die 'credit_remaining requires suspend or cancel'
1404 unless $mode eq 'suspend' or $mode eq 'cancel';
1405 die 'no suspend/cancel time' unless $time > 0;
1407 my $conf = FS::Conf->new;
1408 my $reason_type = $conf->config($mode.'_credit_type');
1410 my $last_bill = $self->getfield('last_bill') || 0;
1411 my $next_bill = $self->getfield('bill') || 0;
1412 if ( $last_bill > 0 # the package has been billed
1413 and $next_bill > 0 # the package has a next bill date
1414 and $next_bill >= $time # which is in the future
1416 my $remaining_value = $self->calc_remain('time' => $time);
1417 if ( $remaining_value > 0 ) {
1418 warn "Crediting for $remaining_value on package ".$self->pkgnum."\n"
1420 my $error = $self->cust_main->credit(
1422 'Credit for unused time on '. $self->part_pkg->pkg,
1423 'reason_type' => $reason_type,
1425 return "Error crediting customer \$$remaining_value for unused time".
1426 " on ". $self->part_pkg->pkg. ": $error"
1428 } #if $remaining_value
1429 } #if $last_bill, etc.
1433 =item unsuspend [ OPTION => VALUE ... ]
1435 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1436 package, then unsuspends the package itself (clears the susp field and the
1437 adjourn field if it is in the past). If the suspend reason includes an
1438 unsuspension package, that package will be ordered.
1440 Available options are:
1446 Can be set to a date to unsuspend the package in the future (the 'resume'
1449 =item adjust_next_bill
1451 Can be set true to adjust the next bill date forward by
1452 the amount of time the account was inactive. This was set true by default
1453 since 1.4.2 and 1.5.0pre6; however, starting with 1.7.0 this needs to be
1454 explicitly requested. Price plans for which this makes sense (anniversary-date
1455 based than prorate or subscription) could have an option to enable this
1460 If there is an error, returns the error, otherwise returns false.
1465 my( $self, %opt ) = @_;
1468 # pass all suspend/cancel actions to the main package
1469 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1470 return $self->main_pkg->unsuspend(%opt);
1473 local $SIG{HUP} = 'IGNORE';
1474 local $SIG{INT} = 'IGNORE';
1475 local $SIG{QUIT} = 'IGNORE';
1476 local $SIG{TERM} = 'IGNORE';
1477 local $SIG{TSTP} = 'IGNORE';
1478 local $SIG{PIPE} = 'IGNORE';
1480 my $oldAutoCommit = $FS::UID::AutoCommit;
1481 local $FS::UID::AutoCommit = 0;
1484 my $old = $self->select_for_update;
1486 my $pkgnum = $old->pkgnum;
1487 if ( $old->get('cancel') || $self->get('cancel') ) {
1488 $dbh->rollback if $oldAutoCommit;
1489 return "Can't unsuspend cancelled package $pkgnum";
1492 unless ( $old->get('susp') && $self->get('susp') ) {
1493 $dbh->rollback if $oldAutoCommit;
1494 return ""; # no error # complain instead?
1497 my $date = $opt{'date'};
1498 if ( $date and $date > time ) { # return an error if $date <= time?
1500 if ( $old->get('expire') && $old->get('expire') < $date ) {
1501 $dbh->rollback if $oldAutoCommit;
1502 return "Package $pkgnum expires before it would be unsuspended.";
1505 my $new = new FS::cust_pkg { $self->hash };
1506 $new->set('resume', $date);
1507 $error = $new->replace($self, options => $self->options);
1510 $dbh->rollback if $oldAutoCommit;
1514 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1522 foreach my $cust_svc (
1523 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1525 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1527 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1528 $dbh->rollback if $oldAutoCommit;
1529 return "Illegal svcdb value in part_svc!";
1532 require "FS/$svcdb.pm";
1534 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1536 $error = $svc->unsuspend;
1538 $dbh->rollback if $oldAutoCommit;
1541 my( $label, $value ) = $cust_svc->label;
1542 push @labels, "$label: $value";
1547 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1548 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
1550 my %hash = $self->hash;
1551 my $inactive = time - $hash{'susp'};
1553 my $conf = new FS::Conf;
1555 if ( $inactive > 0 &&
1556 ( $hash{'bill'} || $hash{'setup'} ) &&
1557 ( $opt{'adjust_next_bill'} ||
1558 $conf->exists('unsuspend-always_adjust_next_bill_date') ||
1559 $self->part_pkg->option('unsuspend_adjust_bill', 1) )
1562 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
1567 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
1568 $hash{'resume'} = '' if !$hash{'adjourn'};
1569 my $new = new FS::cust_pkg ( \%hash );
1570 $error = $new->replace( $self, options => { $self->options } );
1572 $dbh->rollback if $oldAutoCommit;
1578 if ( $reason && $reason->unsuspend_pkgpart ) {
1579 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
1580 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
1582 my $start_date = $self->cust_main->next_bill_date
1583 if $reason->unsuspend_hold;
1586 $unsusp_pkg = FS::cust_pkg->new({
1587 'custnum' => $self->custnum,
1588 'pkgpart' => $reason->unsuspend_pkgpart,
1589 'start_date' => $start_date,
1590 'locationnum' => $self->locationnum,
1591 # discount? probably not...
1594 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
1598 $dbh->rollback if $oldAutoCommit;
1603 if ( $conf->config('unsuspend_email_admin') ) {
1605 my $error = send_email(
1606 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1607 #invoice_from ??? well as good as any
1608 'to' => $conf->config('unsuspend_email_admin'),
1609 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
1610 "This is an automatic message from your Freeside installation\n",
1611 "informing you that the following customer package has been unsuspended:\n",
1613 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1614 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1615 ( map { "Service : $_\n" } @labels ),
1617 "An unsuspension fee was charged: ".
1618 $unsusp_pkg->part_pkg->pkg_comment."\n"
1625 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
1631 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1632 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
1634 $dbh->rollback if $oldAutoCommit;
1635 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1639 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1646 Cancels any pending suspension (sets the adjourn field to null).
1648 If there is an error, returns the error, otherwise returns false.
1653 my( $self, %options ) = @_;
1656 local $SIG{HUP} = 'IGNORE';
1657 local $SIG{INT} = 'IGNORE';
1658 local $SIG{QUIT} = 'IGNORE';
1659 local $SIG{TERM} = 'IGNORE';
1660 local $SIG{TSTP} = 'IGNORE';
1661 local $SIG{PIPE} = 'IGNORE';
1663 my $oldAutoCommit = $FS::UID::AutoCommit;
1664 local $FS::UID::AutoCommit = 0;
1667 my $old = $self->select_for_update;
1669 my $pkgnum = $old->pkgnum;
1670 if ( $old->get('cancel') || $self->get('cancel') ) {
1671 dbh->rollback if $oldAutoCommit;
1672 return "Can't unadjourn cancelled package $pkgnum";
1673 # or at least it's pointless
1676 if ( $old->get('susp') || $self->get('susp') ) {
1677 dbh->rollback if $oldAutoCommit;
1678 return "Can't unadjourn suspended package $pkgnum";
1679 # perhaps this is arbitrary
1682 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1683 dbh->rollback if $oldAutoCommit;
1684 return ""; # no error
1687 my %hash = $self->hash;
1688 $hash{'adjourn'} = '';
1689 $hash{'resume'} = '';
1690 my $new = new FS::cust_pkg ( \%hash );
1691 $error = $new->replace( $self, options => { $self->options } );
1693 $dbh->rollback if $oldAutoCommit;
1697 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1704 =item change HASHREF | OPTION => VALUE ...
1706 Changes this package: cancels it and creates a new one, with a different
1707 pkgpart or locationnum or both. All services are transferred to the new
1708 package (no change will be made if this is not possible).
1710 Options may be passed as a list of key/value pairs or as a hash reference.
1717 New locationnum, to change the location for this package.
1721 New FS::cust_location object, to create a new location and assign it
1726 New FS::cust_main object, to create a new customer and assign the new package
1731 New pkgpart (see L<FS::part_pkg>).
1735 New refnum (see L<FS::part_referral>).
1739 "New" (existing) FS::cust_pkg object. The package's services and other
1740 attributes will be transferred to this package.
1744 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1745 susp, adjourn, cancel, expire, and contract_end) to the new package.
1747 =item unprotect_svcs
1749 Normally, change() will rollback and return an error if some services
1750 can't be transferred (also see the I<cust_pkg-change_svcpart> config option).
1751 If unprotect_svcs is true, this method will transfer as many services as
1752 it can and then unconditionally cancel the old package.
1756 At least one of locationnum, cust_location, pkgpart, refnum, cust_main, or
1757 cust_pkg must be specified (otherwise, what's the point?)
1759 Returns either the new FS::cust_pkg object or a scalar error.
1763 my $err_or_new_cust_pkg = $old_cust_pkg->change
1767 #some false laziness w/order
1770 my $opt = ref($_[0]) ? shift : { @_ };
1772 # my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
1775 my $conf = new FS::Conf;
1777 # Transactionize this whole mess
1778 local $SIG{HUP} = 'IGNORE';
1779 local $SIG{INT} = 'IGNORE';
1780 local $SIG{QUIT} = 'IGNORE';
1781 local $SIG{TERM} = 'IGNORE';
1782 local $SIG{TSTP} = 'IGNORE';
1783 local $SIG{PIPE} = 'IGNORE';
1785 my $oldAutoCommit = $FS::UID::AutoCommit;
1786 local $FS::UID::AutoCommit = 0;
1795 #$hash{$_} = $self->$_() foreach qw( last_bill bill );
1797 #$hash{$_} = $self->$_() foreach qw( setup );
1799 $hash{'setup'} = $time if $self->setup;
1801 $hash{'change_date'} = $time;
1802 $hash{"change_$_"} = $self->$_()
1803 foreach qw( pkgnum pkgpart locationnum );
1805 if ( $opt->{'cust_location'} ) {
1806 $error = $opt->{'cust_location'}->find_or_insert;
1808 $dbh->rollback if $oldAutoCommit;
1809 return "inserting cust_location (transaction rolled back): $error";
1811 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1814 if ( $opt->{'cust_pkg'} ) {
1815 # treat changing to a package with a different pkgpart as a
1816 # pkgpart change (because it is)
1817 $opt->{'pkgpart'} = $opt->{'cust_pkg'}->pkgpart;
1820 # whether to override pkgpart checking on the new package
1821 my $same_pkgpart = 1;
1822 if ( $opt->{'pkgpart'} and ( $opt->{'pkgpart'} != $self->pkgpart ) ) {
1826 my $unused_credit = 0;
1827 my $keep_dates = $opt->{'keep_dates'};
1828 # Special case. If the pkgpart is changing, and the customer is
1829 # going to be credited for remaining time, don't keep setup, bill,
1830 # or last_bill dates, and DO pass the flag to cancel() to credit
1832 if ( $opt->{'pkgpart'}
1833 and $opt->{'pkgpart'} != $self->pkgpart
1834 and $self->part_pkg->option('unused_credit_change', 1) ) {
1837 $hash{$_} = '' foreach qw(setup bill last_bill);
1840 if ( $keep_dates ) {
1841 foreach my $date ( qw(setup bill last_bill susp adjourn cancel expire
1842 resume start_date contract_end ) ) {
1843 $hash{$date} = $self->getfield($date);
1846 # allow $opt->{'locationnum'} = '' to specifically set it to null
1847 # (i.e. customer default location)
1848 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
1850 # usually this doesn't matter. the two cases where it does are:
1851 # 1. unused_credit_change + pkgpart change + setup fee on the new package
1853 # 2. (more importantly) changing a package before it's billed
1854 $hash{'waive_setup'} = $self->waive_setup;
1856 my $custnum = $self->custnum;
1857 if ( $opt->{cust_main} ) {
1858 my $cust_main = $opt->{cust_main};
1859 unless ( $cust_main->custnum ) {
1860 my $error = $cust_main->insert;
1862 $dbh->rollback if $oldAutoCommit;
1863 return "inserting cust_main (transaction rolled back): $error";
1866 $custnum = $cust_main->custnum;
1869 $hash{'contactnum'} = $opt->{'contactnum'} if $opt->{'contactnum'};
1872 if ( $opt->{'cust_pkg'} ) {
1873 # The target package already exists; update it to show that it was
1874 # changed from this package.
1875 $cust_pkg = $opt->{'cust_pkg'};
1877 foreach ( qw( pkgnum pkgpart locationnum ) ) {
1878 $cust_pkg->set("change_$_", $self->get($_));
1880 $cust_pkg->set('change_date', $time);
1881 $error = $cust_pkg->replace;
1884 # Create the new package.
1885 $cust_pkg = new FS::cust_pkg {
1886 custnum => $custnum,
1887 pkgpart => ( $opt->{'pkgpart'} || $self->pkgpart ),
1888 refnum => ( $opt->{'refnum'} || $self->refnum ),
1889 locationnum => ( $opt->{'locationnum'} ),
1892 $error = $cust_pkg->insert( 'change' => 1,
1893 'allow_pkgpart' => $same_pkgpart );
1896 $dbh->rollback if $oldAutoCommit;
1900 # Transfer services and cancel old package.
1902 $error = $self->transfer($cust_pkg);
1903 if ($error and $error == 0) {
1904 # $old_pkg->transfer failed.
1905 $dbh->rollback if $oldAutoCommit;
1909 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
1910 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
1911 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
1912 if ($error and $error == 0) {
1913 # $old_pkg->transfer failed.
1914 $dbh->rollback if $oldAutoCommit;
1919 # We set unprotect_svcs when executing a "future package change". It's
1920 # not a user-interactive operation, so returning an error means the
1921 # package change will just fail. Rather than have that happen, we'll
1922 # let leftover services be deleted.
1923 if ($error > 0 and !$opt->{'unprotect_svcs'}) {
1924 # Transfers were successful, but we still had services left on the old
1925 # package. We can't change the package under this circumstances, so abort.
1926 $dbh->rollback if $oldAutoCommit;
1927 return "Unable to transfer all services from package ". $self->pkgnum;
1930 #reset usage if changing pkgpart
1931 # AND usage rollover is off (otherwise adds twice, now and at package bill)
1932 if ($self->pkgpart != $cust_pkg->pkgpart) {
1933 my $part_pkg = $cust_pkg->part_pkg;
1934 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
1938 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
1941 $dbh->rollback if $oldAutoCommit;
1942 return "Error setting usage values: $error";
1945 # if NOT changing pkgpart, transfer any usage pools over
1946 foreach my $usage ($self->cust_pkg_usage) {
1947 $usage->set('pkgnum', $cust_pkg->pkgnum);
1948 $error = $usage->replace;
1950 $dbh->rollback if $oldAutoCommit;
1951 return "Error transferring usage pools: $error";
1956 # transfer discounts, if we're not changing pkgpart
1957 if ( $same_pkgpart ) {
1958 foreach my $old_discount ($self->cust_pkg_discount_active) {
1959 # don't remove the old discount, we may still need to bill that package.
1960 my $new_discount = new FS::cust_pkg_discount {
1961 'pkgnum' => $cust_pkg->pkgnum,
1962 'discountnum' => $old_discount->discountnum,
1963 'months_used' => $old_discount->months_used,
1965 $error = $new_discount->insert;
1967 $dbh->rollback if $oldAutoCommit;
1968 return "Error transferring discounts: $error";
1973 # transfer (copy) invoice details
1974 foreach my $detail ($self->cust_pkg_detail) {
1975 my $new_detail = FS::cust_pkg_detail->new({ $detail->hash });
1976 $new_detail->set('pkgdetailnum', '');
1977 $new_detail->set('pkgnum', $cust_pkg->pkgnum);
1978 $error = $new_detail->insert;
1980 $dbh->rollback if $oldAutoCommit;
1981 return "Error transferring package notes: $error";
1987 if ( !$opt->{'cust_pkg'} ) {
1988 # Order any supplemental packages.
1989 my $part_pkg = $cust_pkg->part_pkg;
1990 my @old_supp_pkgs = $self->supplemental_pkgs;
1991 foreach my $link ($part_pkg->supp_part_pkg_link) {
1993 foreach (@old_supp_pkgs) {
1994 if ($_->pkgpart == $link->dst_pkgpart) {
1996 $_->pkgpart(0); # so that it can't match more than once
2000 # false laziness with FS::cust_main::Packages::order_pkg
2001 my $new = FS::cust_pkg->new({
2002 pkgpart => $link->dst_pkgpart,
2003 pkglinknum => $link->pkglinknum,
2004 custnum => $custnum,
2005 main_pkgnum => $cust_pkg->pkgnum,
2006 locationnum => $cust_pkg->locationnum,
2007 start_date => $cust_pkg->start_date,
2008 order_date => $cust_pkg->order_date,
2009 expire => $cust_pkg->expire,
2010 adjourn => $cust_pkg->adjourn,
2011 contract_end => $cust_pkg->contract_end,
2012 refnum => $cust_pkg->refnum,
2013 discountnum => $cust_pkg->discountnum,
2014 waive_setup => $cust_pkg->waive_setup,
2016 if ( $old and $opt->{'keep_dates'} ) {
2017 foreach (qw(setup bill last_bill)) {
2018 $new->set($_, $old->get($_));
2021 $error = $new->insert( allow_pkgpart => $same_pkgpart );
2024 $error ||= $old->transfer($new);
2026 if ( $error and $error > 0 ) {
2027 # no reason why this should ever fail, but still...
2028 $error = "Unable to transfer all services from supplemental package ".
2032 $dbh->rollback if $oldAutoCommit;
2035 push @new_supp_pkgs, $new;
2037 } # if !$opt->{'cust_pkg'}
2038 # because if there is one, then supplemental packages would already
2039 # have been created for it.
2041 #Good to go, cancel old package. Notify 'cancel' of whether to credit
2043 #Don't allow billing the package (preceding period packages and/or
2044 #outstanding usage) if we are keeping dates (i.e. location changing),
2045 #because the new package will be billed for the same date range.
2046 #Supplemental packages are also canceled here.
2048 # during scheduled changes, avoid canceling the package we just
2050 $self->set('change_to_pkgnum' => '');
2052 $error = $self->cancel(
2054 unused_credit => $unused_credit,
2055 nobill => $keep_dates,
2056 change_custnum => ( $self->custnum != $custnum ? $custnum : '' ),
2059 $dbh->rollback if $oldAutoCommit;
2063 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
2065 my $error = $cust_pkg->cust_main->bill(
2066 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
2069 $dbh->rollback if $oldAutoCommit;
2074 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2080 =item change_later OPTION => VALUE...
2082 Schedule a package change for a later date. This actually orders the new
2083 package immediately, but sets its start date for a future date, and sets
2084 the current package to expire on the same date.
2086 If the package is already scheduled for a change, this can be called with
2087 'start_date' to change the scheduled date, or with pkgpart and/or
2088 locationnum to modify the package change. To cancel the scheduled change
2089 entirely, see C<abort_change>.
2097 The date for the package change. Required, and must be in the future.
2103 The pkgpart and locationnum of the new package, with the same
2104 meaning as in C<change>.
2112 my $opt = ref($_[0]) ? shift : { @_ };
2114 my $oldAutoCommit = $FS::UID::AutoCommit;
2115 local $FS::UID::AutoCommit = 0;
2118 my $cust_main = $self->cust_main;
2120 my $date = delete $opt->{'start_date'} or return 'start_date required';
2122 if ( $date <= time ) {
2123 $dbh->rollback if $oldAutoCommit;
2124 return "start_date $date is in the past";
2129 if ( $self->change_to_pkgnum ) {
2130 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
2131 my $new_pkgpart = $opt->{'pkgpart'}
2132 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $change_to->pkgpart;
2133 my $new_locationnum = $opt->{'locationnum'}
2134 if $opt->{'locationnum'} and $opt->{'locationnum'} != $change_to->locationnum;
2135 if ( $new_pkgpart or $new_locationnum ) {
2136 # it hasn't been billed yet, so in principle we could just edit
2137 # it in place (w/o a package change), but that's bad form.
2138 # So change the package according to the new options...
2139 my $err_or_pkg = $change_to->change(%$opt);
2140 if ( ref $err_or_pkg ) {
2141 # Then set that package up for a future start.
2142 $self->set('change_to_pkgnum', $err_or_pkg->pkgnum);
2143 $self->set('expire', $date); # in case it's different
2144 $err_or_pkg->set('start_date', $date);
2145 $err_or_pkg->set('change_date', '');
2146 $err_or_pkg->set('change_pkgnum', '');
2148 $error = $self->replace ||
2149 $err_or_pkg->replace ||
2150 $change_to->cancel ||
2153 $error = $err_or_pkg;
2155 } else { # change the start date only.
2156 $self->set('expire', $date);
2157 $change_to->set('start_date', $date);
2158 $error = $self->replace || $change_to->replace;
2161 $dbh->rollback if $oldAutoCommit;
2164 $dbh->commit if $oldAutoCommit;
2167 } # if $self->change_to_pkgnum
2169 my $new_pkgpart = $opt->{'pkgpart'}
2170 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart;
2171 my $new_locationnum = $opt->{'locationnum'}
2172 if $opt->{'locationnum'} and $opt->{'locationnum'} != $self->locationnum;
2173 return '' unless $new_pkgpart or $new_locationnum; # wouldn't do anything
2176 'custnum' => $self->custnum,
2177 'pkgpart' => ($opt->{'pkgpart'} || $self->pkgpart),
2178 'locationnum' => ($opt->{'locationnum'} || $self->locationnum),
2179 'start_date' => $date,
2181 my $new = FS::cust_pkg->new(\%hash);
2182 $error = $new->insert('change' => 1,
2183 'allow_pkgpart' => ($new_pkgpart ? 0 : 1));
2185 $self->set('change_to_pkgnum', $new->pkgnum);
2186 $self->set('expire', $date);
2187 $error = $self->replace;
2190 $dbh->rollback if $oldAutoCommit;
2192 $dbh->commit if $oldAutoCommit;
2200 Cancels a future package change scheduled by C<change_later>.
2206 my $pkgnum = $self->change_to_pkgnum;
2207 my $change_to = FS::cust_pkg->by_key($pkgnum) if $pkgnum;
2210 $error = $change_to->cancel || $change_to->delete;
2211 return $error if $error;
2213 $self->set('change_to_pkgnum', '');
2214 $self->set('expire', '');
2218 =item set_quantity QUANTITY
2220 Change the package's quantity field. This is the one package property
2221 that can safely be changed without canceling and reordering the package
2222 (because it doesn't affect tax eligibility). Returns an error or an
2229 $self = $self->replace_old; # just to make sure
2231 ($qty =~ /^\d+$/ and $qty > 0) or return "bad package quantity $qty";
2232 $self->set('quantity' => $qty);
2236 use Storable 'thaw';
2238 sub process_bulk_cust_pkg {
2240 my $param = thaw(decode_base64(shift));
2241 warn Dumper($param) if $DEBUG;
2243 my $old_part_pkg = qsearchs('part_pkg',
2244 { pkgpart => $param->{'old_pkgpart'} });
2245 my $new_part_pkg = qsearchs('part_pkg',
2246 { pkgpart => $param->{'new_pkgpart'} });
2247 die "Must select a new package type\n" unless $new_part_pkg;
2248 #my $keep_dates = $param->{'keep_dates'} || 0;
2249 my $keep_dates = 1; # there is no good reason to turn this off
2251 local $SIG{HUP} = 'IGNORE';
2252 local $SIG{INT} = 'IGNORE';
2253 local $SIG{QUIT} = 'IGNORE';
2254 local $SIG{TERM} = 'IGNORE';
2255 local $SIG{TSTP} = 'IGNORE';
2256 local $SIG{PIPE} = 'IGNORE';
2258 my $oldAutoCommit = $FS::UID::AutoCommit;
2259 local $FS::UID::AutoCommit = 0;
2262 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
2265 foreach my $old_cust_pkg ( @cust_pkgs ) {
2267 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
2268 if ( $old_cust_pkg->getfield('cancel') ) {
2269 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
2270 $old_cust_pkg->pkgnum."\n"
2274 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
2276 my $error = $old_cust_pkg->change(
2277 'pkgpart' => $param->{'new_pkgpart'},
2278 'keep_dates' => $keep_dates
2280 if ( !ref($error) ) { # change returns the cust_pkg on success
2282 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
2285 $dbh->commit if $oldAutoCommit;
2291 Returns the last bill date, or if there is no last bill date, the setup date.
2292 Useful for billing metered services.
2298 return $self->setfield('last_bill', $_[0]) if @_;
2299 return $self->getfield('last_bill') if $self->getfield('last_bill');
2300 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
2301 'edate' => $self->bill, } );
2302 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
2305 =item last_cust_pkg_reason ACTION
2307 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
2308 Returns false if there is no reason or the package is not currenly ACTION'd
2309 ACTION is one of adjourn, susp, cancel, or expire.
2313 sub last_cust_pkg_reason {
2314 my ( $self, $action ) = ( shift, shift );
2315 my $date = $self->get($action);
2317 'table' => 'cust_pkg_reason',
2318 'hashref' => { 'pkgnum' => $self->pkgnum,
2319 'action' => substr(uc($action), 0, 1),
2322 'order_by' => 'ORDER BY num DESC LIMIT 1',
2326 =item last_reason ACTION
2328 Returns the most recent ACTION FS::reason associated with the package.
2329 Returns false if there is no reason or the package is not currenly ACTION'd
2330 ACTION is one of adjourn, susp, cancel, or expire.
2335 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
2336 $cust_pkg_reason->reason
2337 if $cust_pkg_reason;
2342 Returns the definition for this billing item, as an FS::part_pkg object (see
2349 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
2350 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
2351 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
2356 Returns the cancelled package this package was changed from, if any.
2362 return '' unless $self->change_pkgnum;
2363 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
2366 =item change_cust_main
2368 Returns the customter this package was detached to, if any.
2372 sub change_cust_main {
2374 return '' unless $self->change_custnum;
2375 qsearchs('cust_main', { 'custnum' => $self->change_custnum } );
2380 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
2387 $self->part_pkg->calc_setup($self, @_);
2392 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
2399 $self->part_pkg->calc_recur($self, @_);
2404 Calls the I<base_setup> of the FS::part_pkg object associated with this billing
2411 $self->part_pkg->base_setup($self, @_);
2416 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
2423 $self->part_pkg->base_recur($self, @_);
2428 Calls the I<calc_remain> of the FS::part_pkg object associated with this
2435 $self->part_pkg->calc_remain($self, @_);
2440 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
2447 $self->part_pkg->calc_cancel($self, @_);
2452 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
2458 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
2461 =item cust_pkg_detail [ DETAILTYPE ]
2463 Returns any customer package details for this package (see
2464 L<FS::cust_pkg_detail>).
2466 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2470 sub cust_pkg_detail {
2472 my %hash = ( 'pkgnum' => $self->pkgnum );
2473 $hash{detailtype} = shift if @_;
2475 'table' => 'cust_pkg_detail',
2476 'hashref' => \%hash,
2477 'order_by' => 'ORDER BY weight, pkgdetailnum',
2481 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
2483 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
2485 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2487 If there is an error, returns the error, otherwise returns false.
2491 sub set_cust_pkg_detail {
2492 my( $self, $detailtype, @details ) = @_;
2494 local $SIG{HUP} = 'IGNORE';
2495 local $SIG{INT} = 'IGNORE';
2496 local $SIG{QUIT} = 'IGNORE';
2497 local $SIG{TERM} = 'IGNORE';
2498 local $SIG{TSTP} = 'IGNORE';
2499 local $SIG{PIPE} = 'IGNORE';
2501 my $oldAutoCommit = $FS::UID::AutoCommit;
2502 local $FS::UID::AutoCommit = 0;
2505 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
2506 my $error = $current->delete;
2508 $dbh->rollback if $oldAutoCommit;
2509 return "error removing old detail: $error";
2513 foreach my $detail ( @details ) {
2514 my $cust_pkg_detail = new FS::cust_pkg_detail {
2515 'pkgnum' => $self->pkgnum,
2516 'detailtype' => $detailtype,
2517 'detail' => $detail,
2519 my $error = $cust_pkg_detail->insert;
2521 $dbh->rollback if $oldAutoCommit;
2522 return "error adding new detail: $error";
2527 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2534 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
2538 #false laziness w/cust_bill.pm
2542 'table' => 'cust_event',
2543 'addl_from' => 'JOIN part_event USING ( eventpart )',
2544 'hashref' => { 'tablenum' => $self->pkgnum },
2545 'extra_sql' => " AND eventtable = 'cust_pkg' ",
2549 =item num_cust_event
2551 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
2555 #false laziness w/cust_bill.pm
2556 sub num_cust_event {
2559 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
2560 " WHERE tablenum = ? AND eventtable = 'cust_pkg'";
2561 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
2562 $sth->execute($self->pkgnum) or die $sth->errstr. " executing $sql";
2563 $sth->fetchrow_arrayref->[0];
2566 =item part_pkg_currency_option OPTIONNAME
2568 Returns a two item list consisting of the currency of this customer, if any,
2569 and a value for the provided option. If the customer has a currency, the value
2570 is the option value the given name and the currency (see
2571 L<FS::part_pkg_currency>). Otherwise, if the customer has no currency, is the
2572 regular option value for the given name (see L<FS::part_pkg_option>).
2576 sub part_pkg_currency_option {
2577 my( $self, $optionname ) = @_;
2578 my $part_pkg = $self->part_pkg;
2579 if ( my $currency = $self->cust_main->currency ) {
2580 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
2582 ('', $part_pkg->option($optionname) );
2586 =item cust_svc [ SVCPART ] (old, deprecated usage)
2588 =item cust_svc [ OPTION => VALUE ... ] (current usage)
2590 Returns the services for this package, as FS::cust_svc objects (see
2591 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
2592 spcififed, returns only the matching services.
2599 return () unless $self->num_cust_svc(@_);
2602 if ( @_ && $_[0] =~ /^\d+/ ) {
2603 $opt{svcpart} = shift;
2604 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2611 'table' => 'cust_svc',
2612 'hashref' => { 'pkgnum' => $self->pkgnum },
2614 if ( $opt{svcpart} ) {
2615 $search{hashref}->{svcpart} = $opt{'svcpart'};
2617 if ( $opt{'svcdb'} ) {
2618 $search{addl_from} = ' LEFT JOIN part_svc USING ( svcpart ) ';
2619 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{'svcdb'} );
2622 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
2624 #if ( $self->{'_svcnum'} ) {
2625 # values %{ $self->{'_svcnum'}->cache };
2627 $self->_sort_cust_svc( [ qsearch(\%search) ] );
2632 =item overlimit [ SVCPART ]
2634 Returns the services for this package which have exceeded their
2635 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
2636 is specified, return only the matching services.
2642 return () unless $self->num_cust_svc(@_);
2643 grep { $_->overlimit } $self->cust_svc(@_);
2646 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
2648 Returns historical services for this package created before END TIMESTAMP and
2649 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
2650 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
2651 I<pkg_svc.hidden> flag will be omitted.
2657 warn "$me _h_cust_svc called on $self\n"
2660 my ($end, $start, $mode) = @_;
2661 my @cust_svc = $self->_sort_cust_svc(
2662 [ qsearch( 'h_cust_svc',
2663 { 'pkgnum' => $self->pkgnum, },
2664 FS::h_cust_svc->sql_h_search(@_),
2667 if ( defined($mode) && $mode eq 'I' ) {
2668 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
2669 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
2675 sub _sort_cust_svc {
2676 my( $self, $arrayref ) = @_;
2679 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
2681 my %pkg_svc = map { $_->svcpart => $_ }
2682 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
2687 my $pkg_svc = $pkg_svc{ $_->svcpart } || '';
2689 $pkg_svc ? $pkg_svc->primary_svc : '',
2690 $pkg_svc ? $pkg_svc->quantity : 0,
2697 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
2699 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
2701 Returns the number of services for this package. Available options are svcpart
2702 and svcdb. If either is spcififed, returns only the matching services.
2709 return $self->{'_num_cust_svc'}
2711 && exists($self->{'_num_cust_svc'})
2712 && $self->{'_num_cust_svc'} =~ /\d/;
2714 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
2718 if ( @_ && $_[0] =~ /^\d+/ ) {
2719 $opt{svcpart} = shift;
2720 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2726 my $select = 'SELECT COUNT(*) FROM cust_svc ';
2727 my $where = ' WHERE pkgnum = ? ';
2728 my @param = ($self->pkgnum);
2730 if ( $opt{'svcpart'} ) {
2731 $where .= ' AND svcpart = ? ';
2732 push @param, $opt{'svcpart'};
2734 if ( $opt{'svcdb'} ) {
2735 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
2736 $where .= ' AND svcdb = ? ';
2737 push @param, $opt{'svcdb'};
2740 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
2741 $sth->execute(@param) or die $sth->errstr;
2742 $sth->fetchrow_arrayref->[0];
2745 =item available_part_svc
2747 Returns a list of FS::part_svc objects representing services included in this
2748 package but not yet provisioned. Each FS::part_svc object also has an extra
2749 field, I<num_avail>, which specifies the number of available services.
2753 sub available_part_svc {
2756 my $pkg_quantity = $self->quantity || 1;
2758 grep { $_->num_avail > 0 }
2760 my $part_svc = $_->part_svc;
2761 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
2762 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
2764 # more evil encapsulation breakage
2765 if($part_svc->{'Hash'}{'num_avail'} > 0) {
2766 my @exports = $part_svc->part_export_did;
2767 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
2772 $self->part_pkg->pkg_svc;
2775 =item part_svc [ OPTION => VALUE ... ]
2777 Returns a list of FS::part_svc objects representing provisioned and available
2778 services included in this package. Each FS::part_svc object also has the
2779 following extra fields:
2783 =item num_cust_svc (count)
2785 =item num_avail (quantity - count)
2787 =item cust_pkg_svc (services) - array reference containing the provisioned services, as cust_svc objects
2791 Accepts one option: summarize_size. If specified and non-zero, will omit the
2792 extra cust_pkg_svc option for objects where num_cust_svc is this size or
2798 #label -> ($cust_svc->label)[1]
2804 my $pkg_quantity = $self->quantity || 1;
2806 #XXX some sort of sort order besides numeric by svcpart...
2807 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
2809 my $part_svc = $pkg_svc->part_svc;
2810 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
2811 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
2812 $part_svc->{'Hash'}{'num_avail'} =
2813 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
2814 $part_svc->{'Hash'}{'cust_pkg_svc'} =
2815 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
2816 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
2817 && $num_cust_svc >= $opt{summarize_size};
2818 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
2820 } $self->part_pkg->pkg_svc;
2823 push @part_svc, map {
2825 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
2826 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
2827 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
2828 $part_svc->{'Hash'}{'cust_pkg_svc'} =
2829 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
2831 } $self->extra_part_svc;
2837 =item extra_part_svc
2839 Returns a list of FS::part_svc objects corresponding to services in this
2840 package which are still provisioned but not (any longer) available in the
2845 sub extra_part_svc {
2848 my $pkgnum = $self->pkgnum;
2849 #my $pkgpart = $self->pkgpart;
2852 # 'table' => 'part_svc',
2855 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
2856 # WHERE pkg_svc.svcpart = part_svc.svcpart
2857 # AND pkg_svc.pkgpart = ?
2860 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
2861 # LEFT JOIN cust_pkg USING ( pkgnum )
2862 # WHERE cust_svc.svcpart = part_svc.svcpart
2865 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
2868 #seems to benchmark slightly faster... (or did?)
2870 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
2871 my $pkgparts = join(',', @pkgparts);
2874 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
2875 #MySQL doesn't grok DISINCT ON
2876 'select' => 'DISTINCT part_svc.*',
2877 'table' => 'part_svc',
2879 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
2880 AND pkg_svc.pkgpart IN ($pkgparts)
2883 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
2884 LEFT JOIN cust_pkg USING ( pkgnum )
2887 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
2888 'extra_param' => [ [$self->pkgnum=>'int'] ],
2894 Returns a short status string for this package, currently:
2898 =item not yet billed
2900 =item one-time charge
2915 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
2917 return 'cancelled' if $self->get('cancel');
2918 return 'suspended' if $self->susp;
2919 return 'not yet billed' unless $self->setup;
2920 return 'one-time charge' if $freq =~ /^(0|$)/;
2924 =item ucfirst_status
2926 Returns the status with the first character capitalized.
2930 sub ucfirst_status {
2931 ucfirst(shift->status);
2936 Class method that returns the list of possible status strings for packages
2937 (see L<the status method|/status>). For example:
2939 @statuses = FS::cust_pkg->statuses();
2943 tie my %statuscolor, 'Tie::IxHash',
2944 'not yet billed' => '009999', #teal? cyan?
2945 'one-time charge' => '000000',
2946 'active' => '00CC00',
2947 'suspended' => 'FF9900',
2948 'cancelled' => 'FF0000',
2952 my $self = shift; #could be class...
2953 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
2954 # # mayble split btw one-time vs. recur
2960 Returns a hex triplet color string for this package's status.
2966 $statuscolor{$self->status};
2971 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
2972 "pkg - comment" depending on user preference).
2978 my $label = $self->part_pkg->pkg_comment( 'nopkgpart' => 1 );
2979 $label = $self->pkgnum. ": $label"
2980 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
2984 =item pkg_label_long
2986 Returns a long label for this package, adding the primary service's label to
2991 sub pkg_label_long {
2993 my $label = $self->pkg_label;
2994 my $cust_svc = $self->primary_cust_svc;
2995 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
3001 Returns a customer-localized label for this package.
3007 $self->part_pkg->pkg_locale( $self->cust_main->locale );
3010 =item primary_cust_svc
3012 Returns a primary service (as FS::cust_svc object) if one can be identified.
3016 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
3018 sub primary_cust_svc {
3021 my @cust_svc = $self->cust_svc;
3023 return '' unless @cust_svc; #no serivces - irrelevant then
3025 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
3027 # primary service as specified in the package definition
3028 # or exactly one service definition with quantity one
3029 my $svcpart = $self->part_pkg->svcpart;
3030 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
3031 return $cust_svc[0] if scalar(@cust_svc) == 1;
3033 #couldn't identify one thing..
3039 Returns a list of lists, calling the label method for all services
3040 (see L<FS::cust_svc>) of this billing item.
3046 map { [ $_->label ] } $self->cust_svc;
3049 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3051 Like the labels method, but returns historical information on services that
3052 were active as of END_TIMESTAMP and (optionally) not cancelled before
3053 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
3054 I<pkg_svc.hidden> flag will be omitted.
3056 Returns a list of lists, calling the label method for all (historical) services
3057 (see L<FS::h_cust_svc>) of this billing item.
3063 warn "$me _h_labels called on $self\n"
3065 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
3070 Like labels, except returns a simple flat list, and shortens long
3071 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3072 identical services to one line that lists the service label and the number of
3073 individual services rather than individual items.
3078 shift->_labels_short( 'labels', @_ );
3081 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
3083 Like h_labels, except returns a simple flat list, and shortens long
3084 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3085 identical services to one line that lists the service label and the number of
3086 individual services rather than individual items.
3090 sub h_labels_short {
3091 shift->_labels_short( 'h_labels', @_ );
3095 my( $self, $method ) = ( shift, shift );
3097 warn "$me _labels_short called on $self with $method method\n"
3100 my $conf = new FS::Conf;
3101 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
3103 warn "$me _labels_short populating \%labels\n"
3107 #tie %labels, 'Tie::IxHash';
3108 push @{ $labels{$_->[0]} }, $_->[1]
3109 foreach $self->$method(@_);
3111 warn "$me _labels_short populating \@labels\n"
3115 foreach my $label ( keys %labels ) {
3117 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
3118 my $num = scalar(@values);
3119 warn "$me _labels_short $num items for $label\n"
3122 if ( $num > $max_same_services ) {
3123 warn "$me _labels_short more than $max_same_services, so summarizing\n"
3125 push @labels, "$label ($num)";
3127 if ( $conf->exists('cust_bill-consolidate_services') ) {
3128 warn "$me _labels_short consolidating services\n"
3130 # push @labels, "$label: ". join(', ', @values);
3132 my $detail = "$label: ";
3133 $detail .= shift(@values). ', '
3135 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
3137 push @labels, $detail;
3139 warn "$me _labels_short done consolidating services\n"
3142 warn "$me _labels_short adding service data\n"
3144 push @labels, map { "$label: $_" } @values;
3155 Returns the parent customer object (see L<FS::cust_main>).
3161 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
3166 Returns the balance for this specific package, when using
3167 experimental package balance.
3173 $self->cust_main->balance_pkgnum( $self->pkgnum );
3176 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
3180 Returns the location object, if any (see L<FS::cust_location>).
3182 =item cust_location_or_main
3184 If this package is associated with a location, returns the locaiton (see
3185 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
3187 =item location_label [ OPTION => VALUE ... ]
3189 Returns the label of the location object (see L<FS::cust_location>).
3193 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
3195 =item tax_locationnum
3197 Returns the foreign key to a L<FS::cust_location> object for calculating
3198 tax on this package, as determined by the C<tax-pkg_address> and
3199 C<tax-ship_address> configuration flags.
3203 sub tax_locationnum {
3205 my $conf = FS::Conf->new;
3206 if ( $conf->exists('tax-pkg_address') ) {
3207 return $self->locationnum;
3209 elsif ( $conf->exists('tax-ship_address') ) {
3210 return $self->cust_main->ship_locationnum;
3213 return $self->cust_main->bill_locationnum;
3219 Returns the L<FS::cust_location> object for tax_locationnum.
3225 FS::cust_location->by_key( $self->tax_locationnum )
3228 =item seconds_since TIMESTAMP
3230 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
3231 package have been online since TIMESTAMP, according to the session monitor.
3233 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
3234 L<Time::Local> and L<Date::Parse> for conversion functions.
3239 my($self, $since) = @_;
3242 foreach my $cust_svc (
3243 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
3245 $seconds += $cust_svc->seconds_since($since);
3252 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
3254 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
3255 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
3258 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3259 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3265 sub seconds_since_sqlradacct {
3266 my($self, $start, $end) = @_;
3270 foreach my $cust_svc (
3272 my $part_svc = $_->part_svc;
3273 $part_svc->svcdb eq 'svc_acct'
3274 && scalar($part_svc->part_export_usage);
3277 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
3284 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
3286 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
3287 in this package for sessions ending between TIMESTAMP_START (inclusive) and
3291 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3292 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3297 sub attribute_since_sqlradacct {
3298 my($self, $start, $end, $attrib) = @_;
3302 foreach my $cust_svc (
3304 my $part_svc = $_->part_svc;
3305 $part_svc->svcdb eq 'svc_acct'
3306 && scalar($part_svc->part_export_usage);
3309 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
3321 my( $self, $value ) = @_;
3322 if ( defined($value) ) {
3323 $self->setfield('quantity', $value);
3325 $self->getfield('quantity') || 1;
3328 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
3330 Transfers as many services as possible from this package to another package.
3332 The destination package can be specified by pkgnum by passing an FS::cust_pkg
3333 object. The destination package must already exist.
3335 Services are moved only if the destination allows services with the correct
3336 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
3337 this option with caution! No provision is made for export differences
3338 between the old and new service definitions. Probably only should be used
3339 when your exports for all service definitions of a given svcdb are identical.
3340 (attempt a transfer without it first, to move all possible svcpart-matching
3343 Any services that can't be moved remain in the original package.
3345 Returns an error, if there is one; otherwise, returns the number of services
3346 that couldn't be moved.
3351 my ($self, $dest_pkgnum, %opt) = @_;
3357 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
3358 $dest = $dest_pkgnum;
3359 $dest_pkgnum = $dest->pkgnum;
3361 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
3364 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
3366 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
3367 $target{$pkg_svc->svcpart} = $pkg_svc->quantity;
3370 foreach my $cust_svc ($dest->cust_svc) {
3371 $target{$cust_svc->svcpart}--;
3374 my %svcpart2svcparts = ();
3375 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3376 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
3377 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
3378 next if exists $svcpart2svcparts{$svcpart};
3379 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
3380 $svcpart2svcparts{$svcpart} = [
3382 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
3384 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
3385 'svcpart' => $_ } );
3387 $pkg_svc ? $pkg_svc->primary_svc : '',
3388 $pkg_svc ? $pkg_svc->quantity : 0,
3392 grep { $_ != $svcpart }
3394 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
3396 warn "alternates for svcpart $svcpart: ".
3397 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
3402 foreach my $cust_svc ($self->cust_svc) {
3403 if($target{$cust_svc->svcpart} > 0
3404 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
3405 $target{$cust_svc->svcpart}--;
3406 my $new = new FS::cust_svc { $cust_svc->hash };
3407 $new->pkgnum($dest_pkgnum);
3408 my $error = $new->replace($cust_svc);
3409 return $error if $error;
3410 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3412 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
3413 warn "alternates to consider: ".
3414 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
3416 my @alternate = grep {
3417 warn "considering alternate svcpart $_: ".
3418 "$target{$_} available in new package\n"
3421 } @{$svcpart2svcparts{$cust_svc->svcpart}};
3423 warn "alternate(s) found\n" if $DEBUG;
3424 my $change_svcpart = $alternate[0];
3425 $target{$change_svcpart}--;
3426 my $new = new FS::cust_svc { $cust_svc->hash };
3427 $new->svcpart($change_svcpart);
3428 $new->pkgnum($dest_pkgnum);
3429 my $error = $new->replace($cust_svc);
3430 return $error if $error;
3441 =item grab_svcnums SVCNUM, SVCNUM ...
3443 Change the pkgnum for the provided services to this packages. If there is an
3444 error, returns the error, otherwise returns false.
3452 local $SIG{HUP} = 'IGNORE';
3453 local $SIG{INT} = 'IGNORE';
3454 local $SIG{QUIT} = 'IGNORE';
3455 local $SIG{TERM} = 'IGNORE';
3456 local $SIG{TSTP} = 'IGNORE';
3457 local $SIG{PIPE} = 'IGNORE';
3459 my $oldAutoCommit = $FS::UID::AutoCommit;
3460 local $FS::UID::AutoCommit = 0;
3463 foreach my $svcnum (@svcnum) {
3464 my $cust_svc = qsearchs('cust_svc', { svcnum=>$svcnum } ) or do {
3465 $dbh->rollback if $oldAutoCommit;
3466 return "unknown svcnum $svcnum";
3468 $cust_svc->pkgnum( $self->pkgnum );
3469 my $error = $cust_svc->replace;
3471 $dbh->rollback if $oldAutoCommit;
3476 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3483 This method is deprecated. See the I<depend_jobnum> option to the insert and
3484 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
3488 #looks like this is still used by the order_pkg and change_pkg methods in
3489 # ClientAPI/MyAccount, need to look into those before removing
3493 local $SIG{HUP} = 'IGNORE';
3494 local $SIG{INT} = 'IGNORE';
3495 local $SIG{QUIT} = 'IGNORE';
3496 local $SIG{TERM} = 'IGNORE';
3497 local $SIG{TSTP} = 'IGNORE';
3498 local $SIG{PIPE} = 'IGNORE';
3500 my $oldAutoCommit = $FS::UID::AutoCommit;
3501 local $FS::UID::AutoCommit = 0;
3504 foreach my $cust_svc ( $self->cust_svc ) {
3505 #false laziness w/svc_Common::insert
3506 my $svc_x = $cust_svc->svc_x;
3507 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
3508 my $error = $part_export->export_insert($svc_x);
3510 $dbh->rollback if $oldAutoCommit;
3516 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3521 =item export_pkg_change OLD_CUST_PKG
3523 Calls the "pkg_change" export action for all services attached to this package.
3527 sub export_pkg_change {
3528 my( $self, $old ) = ( shift, shift );
3530 local $SIG{HUP} = 'IGNORE';
3531 local $SIG{INT} = 'IGNORE';
3532 local $SIG{QUIT} = 'IGNORE';
3533 local $SIG{TERM} = 'IGNORE';
3534 local $SIG{TSTP} = 'IGNORE';
3535 local $SIG{PIPE} = 'IGNORE';
3537 my $oldAutoCommit = $FS::UID::AutoCommit;
3538 local $FS::UID::AutoCommit = 0;
3541 foreach my $svc_x ( map $_->svc_x, $self->cust_svc ) {
3542 my $error = $svc_x->export('pkg_change', $self, $old);
3544 $dbh->rollback if $oldAutoCommit;
3549 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3556 Associates this package with a (suspension or cancellation) reason (see
3557 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
3560 Available options are:
3566 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.
3570 the access_user (see L<FS::access_user>) providing the reason
3578 the action (cancel, susp, adjourn, expire) associated with the reason
3582 If there is an error, returns the error, otherwise returns false.
3587 my ($self, %options) = @_;
3589 my $otaker = $options{reason_otaker} ||
3590 $FS::CurrentUser::CurrentUser->username;
3593 if ( $options{'reason'} =~ /^(\d+)$/ ) {
3597 } elsif ( ref($options{'reason'}) ) {
3599 return 'Enter a new reason (or select an existing one)'
3600 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
3602 my $reason = new FS::reason({
3603 'reason_type' => $options{'reason'}->{'typenum'},
3604 'reason' => $options{'reason'}->{'reason'},
3606 my $error = $reason->insert;
3607 return $error if $error;
3609 $reasonnum = $reason->reasonnum;
3612 return "Unparsable reason: ". $options{'reason'};
3615 my $cust_pkg_reason =
3616 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
3617 'reasonnum' => $reasonnum,
3618 'otaker' => $otaker,
3619 'action' => substr(uc($options{'action'}),0,1),
3620 'date' => $options{'date'}
3625 $cust_pkg_reason->insert;
3628 =item insert_discount
3630 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
3631 inserting a new discount on the fly (see L<FS::discount>).
3633 Available options are:
3641 If there is an error, returns the error, otherwise returns false.
3645 sub insert_discount {
3646 #my ($self, %options) = @_;
3649 my $cust_pkg_discount = new FS::cust_pkg_discount {
3650 'pkgnum' => $self->pkgnum,
3651 'discountnum' => $self->discountnum,
3653 'end_date' => '', #XXX
3654 #for the create a new discount case
3655 '_type' => $self->discountnum__type,
3656 'amount' => $self->discountnum_amount,
3657 'percent' => $self->discountnum_percent,
3658 'months' => $self->discountnum_months,
3659 'setup' => $self->discountnum_setup,
3660 #'disabled' => $self->discountnum_disabled,
3663 $cust_pkg_discount->insert;
3666 =item set_usage USAGE_VALUE_HASHREF
3668 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3669 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3670 upbytes, downbytes, and totalbytes are appropriate keys.
3672 All svc_accts which are part of this package have their values reset.
3677 my ($self, $valueref, %opt) = @_;
3679 #only svc_acct can set_usage for now
3680 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3681 my $svc_x = $cust_svc->svc_x;
3682 $svc_x->set_usage($valueref, %opt)
3683 if $svc_x->can("set_usage");
3687 =item recharge USAGE_VALUE_HASHREF
3689 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3690 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3691 upbytes, downbytes, and totalbytes are appropriate keys.
3693 All svc_accts which are part of this package have their values incremented.
3698 my ($self, $valueref) = @_;
3700 #only svc_acct can set_usage for now
3701 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3702 my $svc_x = $cust_svc->svc_x;
3703 $svc_x->recharge($valueref)
3704 if $svc_x->can("recharge");
3708 =item cust_pkg_discount
3712 sub cust_pkg_discount {
3714 qsearch('cust_pkg_discount', { 'pkgnum' => $self->pkgnum } );
3717 =item cust_pkg_discount_active
3721 sub cust_pkg_discount_active {
3723 grep { $_->status eq 'active' } $self->cust_pkg_discount;
3726 =item cust_pkg_usage
3728 Returns a list of all voice usage counters attached to this package.
3732 sub cust_pkg_usage {
3734 qsearch('cust_pkg_usage', { pkgnum => $self->pkgnum });
3737 =item apply_usage OPTIONS
3739 Takes the following options:
3740 - cdr: a call detail record (L<FS::cdr>)
3741 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
3742 - minutes: the maximum number of minutes to be charged
3744 Finds available usage minutes for a call of this class, and subtracts
3745 up to that many minutes from the usage pool. If the usage pool is empty,
3746 and the C<cdr-minutes_priority> global config option is set, minutes may
3747 be taken from other calls as well. Either way, an allocation record will
3748 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
3749 number of minutes of usage applied to the call.
3754 my ($self, %opt) = @_;
3755 my $cdr = $opt{cdr};
3756 my $rate_detail = $opt{rate_detail};
3757 my $minutes = $opt{minutes};
3758 my $classnum = $rate_detail->classnum;
3759 my $pkgnum = $self->pkgnum;
3760 my $custnum = $self->custnum;
3762 local $SIG{HUP} = 'IGNORE';
3763 local $SIG{INT} = 'IGNORE';
3764 local $SIG{QUIT} = 'IGNORE';
3765 local $SIG{TERM} = 'IGNORE';
3766 local $SIG{TSTP} = 'IGNORE';
3767 local $SIG{PIPE} = 'IGNORE';
3769 my $oldAutoCommit = $FS::UID::AutoCommit;
3770 local $FS::UID::AutoCommit = 0;
3772 my $order = FS::Conf->new->config('cdr-minutes_priority');
3776 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
3778 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
3780 my @usage_recs = qsearch({
3781 'table' => 'cust_pkg_usage',
3782 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
3783 ' JOIN cust_pkg USING (pkgnum)'.
3784 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
3785 'select' => 'cust_pkg_usage.*',
3786 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
3787 " ( cust_pkg.custnum = $custnum AND ".
3788 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3789 $is_classnum . ' AND '.
3790 " cust_pkg_usage.minutes > 0",
3791 'order_by' => " ORDER BY priority ASC",
3794 my $orig_minutes = $minutes;
3796 while (!$error and $minutes > 0 and @usage_recs) {
3797 my $cust_pkg_usage = shift @usage_recs;
3798 $cust_pkg_usage->select_for_update;
3799 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
3800 pkgusagenum => $cust_pkg_usage->pkgusagenum,
3801 acctid => $cdr->acctid,
3802 minutes => min($cust_pkg_usage->minutes, $minutes),
3804 $cust_pkg_usage->set('minutes',
3805 sprintf('%.0f', $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes)
3807 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
3808 $minutes -= $cdr_cust_pkg_usage->minutes;
3810 if ( $order and $minutes > 0 and !$error ) {
3811 # then try to steal minutes from another call
3813 'table' => 'cdr_cust_pkg_usage',
3814 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
3815 ' JOIN part_pkg_usage USING (pkgusagepart)'.
3816 ' JOIN cust_pkg USING (pkgnum)'.
3817 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
3818 ' JOIN cdr USING (acctid)',
3819 'select' => 'cdr_cust_pkg_usage.*',
3820 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
3821 " ( cust_pkg.pkgnum = $pkgnum OR ".
3822 " ( cust_pkg.custnum = $custnum AND ".
3823 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3824 " part_pkg_usage_class.classnum = $classnum",
3825 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
3827 if ( $order eq 'time' ) {
3828 # find CDRs that are using minutes, but have a later startdate
3830 my $startdate = $cdr->startdate;
3831 if ($startdate !~ /^\d+$/) {
3832 die "bad cdr startdate '$startdate'";
3834 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
3835 # minimize needless reshuffling
3836 $search{'order_by'} .= ', cdr.startdate DESC';
3838 # XXX may not work correctly with rate_time schedules. Could
3839 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
3841 $search{'addl_from'} .=
3842 ' JOIN rate_detail'.
3843 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
3844 if ( $order eq 'rate_high' ) {
3845 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
3846 $rate_detail->min_charge;
3847 $search{'order_by'} .= ', rate_detail.min_charge ASC';
3848 } elsif ( $order eq 'rate_low' ) {
3849 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
3850 $rate_detail->min_charge;
3851 $search{'order_by'} .= ', rate_detail.min_charge DESC';
3853 # this should really never happen
3854 die "invalid cdr-minutes_priority value '$order'\n";
3857 my @cdr_usage_recs = qsearch(\%search);
3859 while (!$error and @cdr_usage_recs and $minutes > 0) {
3860 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
3861 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
3862 my $old_cdr = $cdr_cust_pkg_usage->cdr;
3863 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
3864 $cdr_cust_pkg_usage->select_for_update;
3865 $old_cdr->select_for_update;
3866 $cust_pkg_usage->select_for_update;
3867 # in case someone else stole the usage from this CDR
3868 # while waiting for the lock...
3869 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
3870 # steal the usage allocation and flag the old CDR for reprocessing
3871 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
3872 # if the allocation is more minutes than we need, adjust it...
3873 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
3875 $cdr_cust_pkg_usage->set('minutes', $minutes);
3876 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
3877 $error = $cust_pkg_usage->replace;
3879 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
3880 $error ||= $cdr_cust_pkg_usage->replace;
3881 # deduct the stolen minutes
3882 $minutes -= $cdr_cust_pkg_usage->minutes;
3884 # after all minute-stealing is done, reset the affected CDRs
3885 foreach (values %reproc_cdrs) {
3886 $error ||= $_->set_status('');
3887 # XXX or should we just call $cdr->rate right here?
3888 # it's not like we can create a loop this way, since the min_charge
3889 # or call time has to go monotonically in one direction.
3890 # we COULD get some very deep recursions going, though...
3892 } # if $order and $minutes
3895 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
3897 $dbh->commit if $oldAutoCommit;
3898 return $orig_minutes - $minutes;
3902 =item supplemental_pkgs
3904 Returns a list of all packages supplemental to this one.
3908 sub supplemental_pkgs {
3910 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
3915 Returns the package that this one is supplemental to, if any.
3921 if ( $self->main_pkgnum ) {
3922 return FS::cust_pkg->by_key($self->main_pkgnum);
3929 =head1 CLASS METHODS
3935 Returns an SQL expression identifying recurring packages.
3939 sub recurring_sql { "
3940 '0' != ( select freq from part_pkg
3941 where cust_pkg.pkgpart = part_pkg.pkgpart )
3946 Returns an SQL expression identifying one-time packages.
3951 '0' = ( select freq from part_pkg
3952 where cust_pkg.pkgpart = part_pkg.pkgpart )
3957 Returns an SQL expression identifying ordered packages (recurring packages not
3963 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
3968 Returns an SQL expression identifying active packages.
3973 $_[0]->recurring_sql. "
3974 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
3975 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
3976 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
3979 =item not_yet_billed_sql
3981 Returns an SQL expression identifying packages which have not yet been billed.
3985 sub not_yet_billed_sql { "
3986 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
3987 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
3988 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
3993 Returns an SQL expression identifying inactive packages (one-time packages
3994 that are otherwise unsuspended/uncancelled).
3998 sub inactive_sql { "
3999 ". $_[0]->onetime_sql(). "
4000 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4001 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4002 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4008 Returns an SQL expression identifying suspended packages.
4012 sub suspended_sql { susp_sql(@_); }
4014 #$_[0]->recurring_sql(). ' AND '.
4016 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4017 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4024 Returns an SQL exprression identifying cancelled packages.
4028 sub cancelled_sql { cancel_sql(@_); }
4030 #$_[0]->recurring_sql(). ' AND '.
4031 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
4036 Returns an SQL expression to give the package status as a string.
4042 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
4043 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
4044 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
4045 WHEN ".onetime_sql()." THEN 'one-time charge'
4050 =item search HASHREF
4054 Returns a qsearch hash expression to search for parameters specified in HASHREF.
4055 Valid parameters are
4063 active, inactive, suspended, cancel (or cancelled)
4067 active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
4071 boolean selects custom packages
4077 pkgpart or arrayref or hashref of pkgparts
4081 arrayref of beginning and ending epoch date
4085 arrayref of beginning and ending epoch date
4089 arrayref of beginning and ending epoch date
4093 arrayref of beginning and ending epoch date
4097 arrayref of beginning and ending epoch date
4101 arrayref of beginning and ending epoch date
4105 arrayref of beginning and ending epoch date
4109 pkgnum or APKG_pkgnum
4113 a value suited to passing to FS::UI::Web::cust_header
4117 specifies the user for agent virtualization
4121 boolean; if true, returns only packages with more than 0 FCC phone lines.
4123 =item state, country
4125 Limit to packages with a service location in the specified state and country.
4126 For FCC 477 reporting, mostly.
4133 my ($class, $params) = @_;
4140 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
4142 "cust_main.agentnum = $1";
4149 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
4151 "cust_pkg.custnum = $1";
4158 if ( $params->{'pkgbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
4160 "cust_pkg.pkgbatch = '$1'";
4167 if ( $params->{'magic'} eq 'active'
4168 || $params->{'status'} eq 'active' ) {
4170 push @where, FS::cust_pkg->active_sql();
4172 } elsif ( $params->{'magic'} =~ /^not[ _]yet[ _]billed$/
4173 || $params->{'status'} =~ /^not[ _]yet[ _]billed$/ ) {
4175 push @where, FS::cust_pkg->not_yet_billed_sql();
4177 } elsif ( $params->{'magic'} =~ /^(one-time charge|inactive)/
4178 || $params->{'status'} =~ /^(one-time charge|inactive)/ ) {
4180 push @where, FS::cust_pkg->inactive_sql();
4182 } elsif ( $params->{'magic'} eq 'suspended'
4183 || $params->{'status'} eq 'suspended' ) {
4185 push @where, FS::cust_pkg->suspended_sql();
4187 } elsif ( $params->{'magic'} =~ /^cancell?ed$/
4188 || $params->{'status'} =~ /^cancell?ed$/ ) {
4190 push @where, FS::cust_pkg->cancelled_sql();
4195 # parse package class
4198 if ( exists($params->{'classnum'}) ) {
4201 if ( ref($params->{'classnum'}) ) {
4203 if ( ref($params->{'classnum'}) eq 'HASH' ) {
4204 @classnum = grep $params->{'classnum'}{$_}, keys %{ $params->{'classnum'} };
4205 } elsif ( ref($params->{'classnum'}) eq 'ARRAY' ) {
4206 @classnum = @{ $params->{'classnum'} };
4208 die 'unhandled classnum ref '. $params->{'classnum'};
4212 } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
4219 my @nums = grep $_, @classnum;
4220 push @c_where, 'part_pkg.classnum IN ('. join(',',@nums). ')' if @nums;
4221 my $null = scalar( grep { $_ eq '' } @classnum );
4222 push @c_where, 'part_pkg.classnum IS NULL' if $null;
4224 if ( scalar(@c_where) == 1 ) {
4225 push @where, @c_where;
4226 } elsif ( @c_where ) {
4227 push @where, ' ( '. join(' OR ', @c_where). ' ) ';
4236 # parse package report options
4239 my @report_option = ();
4240 if ( exists($params->{'report_option'}) ) {
4241 if ( ref($params->{'report_option'}) eq 'ARRAY' ) {
4242 @report_option = @{ $params->{'report_option'} };
4243 } elsif ( $params->{'report_option'} =~ /^([,\d]*)$/ ) {
4244 @report_option = split(',', $1);
4249 if (@report_option) {
4250 # this will result in the empty set for the dangling comma case as it should
4252 map{ "0 < ( SELECT count(*) FROM part_pkg_option
4253 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
4254 AND optionname = 'report_option_$_'
4255 AND optionvalue = '1' )"
4259 foreach my $any ( grep /^report_option_any/, keys %$params ) {
4261 my @report_option_any = ();
4262 if ( ref($params->{$any}) eq 'ARRAY' ) {
4263 @report_option_any = @{ $params->{$any} };
4264 } elsif ( $params->{$any} =~ /^([,\d]*)$/ ) {
4265 @report_option_any = split(',', $1);
4268 if (@report_option_any) {
4269 # this will result in the empty set for the dangling comma case as it should
4270 push @where, ' ( '. join(' OR ',
4271 map{ "0 < ( SELECT count(*) FROM part_pkg_option
4272 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
4273 AND optionname = 'report_option_$_'
4274 AND optionvalue = '1' )"
4275 } @report_option_any
4285 push @where, "part_pkg.custom = 'Y'" if $params->{custom};
4291 push @where, "(part_pkg.fcc_ds0s > 0 OR pkg_class.fcc_ds0s > 0)"
4292 if $params->{fcc_line};
4298 if ( exists($params->{'censustract'}) ) {
4299 $params->{'censustract'} =~ /^([.\d]*)$/;
4300 my $censustract = "cust_location.censustract = '$1'";
4301 $censustract .= ' OR cust_location.censustract is NULL' unless $1;
4302 push @where, "( $censustract )";
4306 # parse censustract2
4308 if ( exists($params->{'censustract2'})
4309 && $params->{'censustract2'} =~ /^(\d*)$/
4313 push @where, "cust_location.censustract LIKE '$1%'";
4316 "( cust_location.censustract = '' OR cust_location.censustract IS NULL )";
4321 # parse country/state
4323 for (qw(state country)) { # parsing rules are the same for these
4324 if ( exists($params->{$_})
4325 && uc($params->{$_}) =~ /^([A-Z]{2})$/ )
4327 # XXX post-2.3 only--before that, state/country may be in cust_main
4328 push @where, "cust_location.$_ = '$1'";
4336 if ( ref($params->{'pkgpart'}) ) {
4339 if ( ref($params->{'pkgpart'}) eq 'HASH' ) {
4340 @pkgpart = grep $params->{'pkgpart'}{$_}, keys %{ $params->{'pkgpart'} };
4341 } elsif ( ref($params->{'pkgpart'}) eq 'ARRAY' ) {
4342 @pkgpart = @{ $params->{'pkgpart'} };
4344 die 'unhandled pkgpart ref '. $params->{'pkgpart'};
4347 @pkgpart = grep /^(\d+)$/, @pkgpart;
4349 push @where, 'pkgpart IN ('. join(',', @pkgpart). ')' if scalar(@pkgpart);
4351 } elsif ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
4352 push @where, "pkgpart = $1";
4361 #false laziness w/report_cust_pkg.html
4364 'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
4365 'active' => { 'susp'=>1, 'cancel'=>1 },
4366 'suspended' => { 'cancel' => 1 },
4371 if( exists($params->{'active'} ) ) {
4372 # This overrides all the other date-related fields
4373 my($beginning, $ending) = @{$params->{'active'}};
4375 "cust_pkg.setup IS NOT NULL",
4376 "cust_pkg.setup <= $ending",
4377 "(cust_pkg.cancel IS NULL OR cust_pkg.cancel >= $beginning )",
4378 "NOT (".FS::cust_pkg->onetime_sql . ")";
4381 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel )) {
4383 next unless exists($params->{$field});
4385 my($beginning, $ending) = @{$params->{$field}};
4387 next if $beginning == 0 && $ending == 4294967295;
4390 "cust_pkg.$field IS NOT NULL",
4391 "cust_pkg.$field >= $beginning",
4392 "cust_pkg.$field <= $ending";
4394 $orderby ||= "ORDER BY cust_pkg.$field";
4399 $orderby ||= 'ORDER BY bill';
4402 # parse magic, legacy, etc.
4405 if ( $params->{'magic'} &&
4406 $params->{'magic'} =~ /^(active|inactive|suspended|cancell?ed)$/
4409 $orderby = 'ORDER BY pkgnum';
4411 if ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
4412 push @where, "pkgpart = $1";
4415 } elsif ( $params->{'query'} eq 'pkgnum' ) {
4417 $orderby = 'ORDER BY pkgnum';
4419 } elsif ( $params->{'query'} eq 'APKG_pkgnum' ) {
4421 $orderby = 'ORDER BY pkgnum';
4424 SELECT count(*) FROM pkg_svc
4425 WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
4426 AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
4427 WHERE cust_svc.pkgnum = cust_pkg.pkgnum
4428 AND cust_svc.svcpart = pkg_svc.svcpart
4435 # setup queries, links, subs, etc. for the search
4438 # here is the agent virtualization
4439 if ($params->{CurrentUser}) {
4441 qsearchs('access_user', { username => $params->{CurrentUser} });
4444 push @where, $access_user->agentnums_sql('table'=>'cust_main');
4449 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql('table'=>'cust_main');
4452 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
4454 my $addl_from = 'LEFT JOIN part_pkg USING ( pkgpart ) '.
4455 'LEFT JOIN pkg_class ON ( part_pkg.classnum = pkg_class.classnum ) '.
4456 'LEFT JOIN cust_location USING ( locationnum ) '.
4457 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
4461 if ( $params->{'select_zip5'} ) {
4462 my $zip = 'cust_location.zip';
4464 $select = "DISTINCT substr($zip,1,5) as zip";
4465 $orderby = "ORDER BY substr($zip,1,5)";
4466 $count_query = "SELECT COUNT( DISTINCT substr($zip,1,5) )";
4468 $select = join(', ',
4470 ( map "part_pkg.$_", qw( pkg freq ) ),
4471 'pkg_class.classname',
4472 'cust_main.custnum AS cust_main_custnum',
4473 FS::UI::Web::cust_sql_fields(
4474 $params->{'cust_fields'}
4477 $count_query = 'SELECT COUNT(*)';
4480 $count_query .= " FROM cust_pkg $addl_from $extra_sql";
4483 'table' => 'cust_pkg',
4485 'select' => $select,
4486 'extra_sql' => $extra_sql,
4487 'order_by' => $orderby,
4488 'addl_from' => $addl_from,
4489 'count_query' => $count_query,
4496 Returns a list of two package counts. The first is a count of packages
4497 based on the supplied criteria and the second is the count of residential
4498 packages with those same criteria. Criteria are specified as in the search
4504 my ($class, $params) = @_;
4506 my $sql_query = $class->search( $params );
4508 my $count_sql = delete($sql_query->{'count_query'});
4509 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
4510 or die "couldn't parse count_sql";
4512 my $count_sth = dbh->prepare($count_sql)
4513 or die "Error preparing $count_sql: ". dbh->errstr;
4515 or die "Error executing $count_sql: ". $count_sth->errstr;
4516 my $count_arrayref = $count_sth->fetchrow_arrayref;
4518 return ( @$count_arrayref );
4522 =item tax_locationnum_sql
4524 Returns an SQL expression for the tax location for a package, based
4525 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
4529 sub tax_locationnum_sql {
4530 my $conf = FS::Conf->new;
4531 if ( $conf->exists('tax-pkg_address') ) {
4532 'cust_pkg.locationnum';
4534 elsif ( $conf->exists('tax-ship_address') ) {
4535 'cust_main.ship_locationnum';
4538 'cust_main.bill_locationnum';
4544 Returns a list: the first item is an SQL fragment identifying matching
4545 packages/customers via location (taking into account shipping and package
4546 address taxation, if enabled), and subsequent items are the parameters to
4547 substitute for the placeholders in that fragment.
4552 my($class, %opt) = @_;
4553 my $ornull = $opt{'ornull'};
4555 my $conf = new FS::Conf;
4557 # '?' placeholders in _location_sql_where
4558 my $x = $ornull ? 3 : 2;
4569 if ( $conf->exists('tax-ship_address') ) {
4572 ( ( ship_last IS NULL OR ship_last = '' )
4573 AND ". _location_sql_where('cust_main', '', $ornull ). "
4575 OR ( ship_last IS NOT NULL AND ship_last != ''
4576 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
4579 # AND payby != 'COMP'
4581 @main_param = ( @bill_param, @bill_param );
4585 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
4586 @main_param = @bill_param;
4592 if ( $conf->exists('tax-pkg_address') ) {
4594 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
4597 ( cust_pkg.locationnum IS NULL AND $main_where )
4598 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
4601 @param = ( @main_param, @bill_param );
4605 $where = $main_where;
4606 @param = @main_param;
4614 #subroutine, helper for location_sql
4615 sub _location_sql_where {
4617 my $prefix = @_ ? shift : '';
4618 my $ornull = @_ ? shift : '';
4620 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
4622 $ornull = $ornull ? ' OR ? IS NULL ' : '';
4624 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
4625 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
4626 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
4628 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
4630 # ( $table.${prefix}city = ? $or_empty_city $ornull )
4632 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4633 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4634 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
4635 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
4636 AND $table.${prefix}country = ?
4641 my( $self, $what ) = @_;
4643 my $what_show_zero = $what. '_show_zero';
4644 length($self->$what_show_zero())
4645 ? ($self->$what_show_zero() eq 'Y')
4646 : $self->part_pkg->$what_show_zero();
4653 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
4655 CUSTNUM is a customer (see L<FS::cust_main>)
4657 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4658 L<FS::part_pkg>) to order for this customer. Duplicates are of course
4661 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
4662 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
4663 new billing items. An error is returned if this is not possible (see
4664 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
4667 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4668 newly-created cust_pkg objects.
4670 REFNUM, if specified, will specify the FS::pkg_referral record to be created
4671 and inserted. Multiple FS::pkg_referral records can be created by
4672 setting I<refnum> to an array reference of refnums or a hash reference with
4673 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
4674 record will be created corresponding to cust_main.refnum.
4679 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
4681 my $conf = new FS::Conf;
4683 # Transactionize this whole mess
4684 local $SIG{HUP} = 'IGNORE';
4685 local $SIG{INT} = 'IGNORE';
4686 local $SIG{QUIT} = 'IGNORE';
4687 local $SIG{TERM} = 'IGNORE';
4688 local $SIG{TSTP} = 'IGNORE';
4689 local $SIG{PIPE} = 'IGNORE';
4691 my $oldAutoCommit = $FS::UID::AutoCommit;
4692 local $FS::UID::AutoCommit = 0;
4696 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
4697 # return "Customer not found: $custnum" unless $cust_main;
4699 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
4702 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4705 my $change = scalar(@old_cust_pkg) != 0;
4708 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
4710 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
4711 " to pkgpart ". $pkgparts->[0]. "\n"
4714 my $err_or_cust_pkg =
4715 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
4716 'refnum' => $refnum,
4719 unless (ref($err_or_cust_pkg)) {
4720 $dbh->rollback if $oldAutoCommit;
4721 return $err_or_cust_pkg;
4724 push @$return_cust_pkg, $err_or_cust_pkg;
4725 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4730 # Create the new packages.
4731 foreach my $pkgpart (@$pkgparts) {
4733 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
4735 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
4736 pkgpart => $pkgpart,
4740 $error = $cust_pkg->insert( 'change' => $change );
4741 push @$return_cust_pkg, $cust_pkg;
4743 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
4744 my $supp_pkg = FS::cust_pkg->new({
4745 custnum => $custnum,
4746 pkgpart => $link->dst_pkgpart,
4748 main_pkgnum => $cust_pkg->pkgnum,
4751 $error ||= $supp_pkg->insert( 'change' => $change );
4752 push @$return_cust_pkg, $supp_pkg;
4756 $dbh->rollback if $oldAutoCommit;
4761 # $return_cust_pkg now contains refs to all of the newly
4764 # Transfer services and cancel old packages.
4765 foreach my $old_pkg (@old_cust_pkg) {
4767 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
4770 foreach my $new_pkg (@$return_cust_pkg) {
4771 $error = $old_pkg->transfer($new_pkg);
4772 if ($error and $error == 0) {
4773 # $old_pkg->transfer failed.
4774 $dbh->rollback if $oldAutoCommit;
4779 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
4780 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
4781 foreach my $new_pkg (@$return_cust_pkg) {
4782 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
4783 if ($error and $error == 0) {
4784 # $old_pkg->transfer failed.
4785 $dbh->rollback if $oldAutoCommit;
4792 # Transfers were successful, but we went through all of the
4793 # new packages and still had services left on the old package.
4794 # We can't cancel the package under the circumstances, so abort.
4795 $dbh->rollback if $oldAutoCommit;
4796 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
4798 $error = $old_pkg->cancel( quiet=>1 );
4804 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4808 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
4810 A bulk change method to change packages for multiple customers.
4812 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4813 L<FS::part_pkg>) to order for each customer. Duplicates are of course
4816 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
4817 replace. The services (see L<FS::cust_svc>) are moved to the
4818 new billing items. An error is returned if this is not possible (see
4821 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4822 newly-created cust_pkg objects.
4827 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
4829 # Transactionize this whole mess
4830 local $SIG{HUP} = 'IGNORE';
4831 local $SIG{INT} = 'IGNORE';
4832 local $SIG{QUIT} = 'IGNORE';
4833 local $SIG{TERM} = 'IGNORE';
4834 local $SIG{TSTP} = 'IGNORE';
4835 local $SIG{PIPE} = 'IGNORE';
4837 my $oldAutoCommit = $FS::UID::AutoCommit;
4838 local $FS::UID::AutoCommit = 0;
4842 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4845 while(scalar(@old_cust_pkg)) {
4847 my $custnum = $old_cust_pkg[0]->custnum;
4848 my (@remove) = map { $_->pkgnum }
4849 grep { $_->custnum == $custnum } @old_cust_pkg;
4850 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
4852 my $error = order $custnum, $pkgparts, \@remove, \@return;
4854 push @errors, $error
4856 push @$return_cust_pkg, @return;
4859 if (scalar(@errors)) {
4860 $dbh->rollback if $oldAutoCommit;
4861 return join(' / ', @errors);
4864 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4868 # Used by FS::Upgrade to migrate to a new database.
4869 sub _upgrade_data { # class method
4870 my ($class, %opts) = @_;
4871 $class->_upgrade_otaker(%opts);
4873 # RT#10139, bug resulting in contract_end being set when it shouldn't
4874 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
4875 # RT#10830, bad calculation of prorate date near end of year
4876 # the date range for bill is December 2009, and we move it forward
4877 # one year if it's before the previous bill date (which it should
4879 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
4880 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
4881 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
4882 # RT6628, add order_date to cust_pkg
4883 'update cust_pkg set order_date = (select history_date from h_cust_pkg
4884 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
4885 history_action = \'insert\') where order_date is null',
4887 foreach my $sql (@statements) {
4888 my $sth = dbh->prepare($sql);
4889 $sth->execute or die $sth->errstr;
4897 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
4899 In sub order, the @pkgparts array (passed by reference) is clobbered.
4901 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
4902 method to pass dates to the recur_prog expression, it should do so.
4904 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
4905 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
4906 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
4907 configuration values. Probably need a subroutine which decides what to do
4908 based on whether or not we've fetched the user yet, rather than a hash. See
4909 FS::UID and the TODO.
4911 Now that things are transactional should the check in the insert method be
4916 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
4917 L<FS::pkg_svc>, schema.html from the base documentation