4 use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::location_Mixin
5 FS::m2m_Common FS::option_Common );
6 use vars qw($disable_agentcheck $DEBUG $me);
8 use Scalar::Util qw( blessed );
9 use List::Util qw(max);
11 use Time::Local qw( timelocal_nocheck );
13 use FS::UID qw( getotaker dbh );
14 use FS::Misc qw( send_email );
15 use FS::Record qw( qsearch qsearchs );
20 use FS::cust_location;
22 use FS::cust_bill_pkg;
23 use FS::cust_pkg_detail;
28 use FS::cust_pkg_reason;
30 use FS::cust_pkg_discount;
35 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
37 # because they load configuration by setting FS::UID::callback (see TODO)
43 # for sending cancel emails in sub cancel
47 $me = '[FS::cust_pkg]';
49 $disable_agentcheck = 0;
53 my ( $hashref, $cache ) = @_;
54 #if ( $hashref->{'pkgpart'} ) {
55 if ( $hashref->{'pkg'} ) {
56 # #@{ $self->{'_pkgnum'} } = ();
57 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
58 # $self->{'_pkgpart'} = $subcache;
59 # #push @{ $self->{'_pkgnum'} },
60 # FS::part_pkg->new_or_cached($hashref, $subcache);
61 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
63 if ( exists $hashref->{'svcnum'} ) {
64 #@{ $self->{'_pkgnum'} } = ();
65 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
66 $self->{'_svcnum'} = $subcache;
67 #push @{ $self->{'_pkgnum'} },
68 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
74 FS::cust_pkg - Object methods for cust_pkg objects
80 $record = new FS::cust_pkg \%hash;
81 $record = new FS::cust_pkg { 'column' => 'value' };
83 $error = $record->insert;
85 $error = $new_record->replace($old_record);
87 $error = $record->delete;
89 $error = $record->check;
91 $error = $record->cancel;
93 $error = $record->suspend;
95 $error = $record->unsuspend;
97 $part_pkg = $record->part_pkg;
99 @labels = $record->labels;
101 $seconds = $record->seconds_since($timestamp);
103 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
104 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
108 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
109 inherits from FS::Record. The following fields are currently supported:
115 Primary key (assigned automatically for new billing items)
119 Customer (see L<FS::cust_main>)
123 Billing item definition (see L<FS::part_pkg>)
127 Optional link to package location (see L<FS::location>)
131 date package was ordered (also remains same on changes)
143 date (next bill date)
171 order taker (see L<FS::access_user>)
175 If this field is set to 1, disables the automatic
176 unsuspension of this package when using the B<unsuspendauto> config option.
180 If not set, defaults to 1
184 Date of change from previous package
194 =item change_locationnum
202 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
203 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
204 L<Time::Local> and L<Date::Parse> for conversion functions.
212 Create a new billing item. To add the item to the database, see L<"insert">.
216 sub table { 'cust_pkg'; }
217 sub cust_linked { $_[0]->cust_main_custnum; }
218 sub cust_unlinked_msg {
220 "WARNING: can't find cust_main.custnum ". $self->custnum.
221 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
224 =item insert [ OPTION => VALUE ... ]
226 Adds this billing item to the database ("Orders" the item). If there is an
227 error, returns the error, otherwise returns false.
229 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
230 will be used to look up the package definition and agent restrictions will be
233 If the additional field I<refnum> is defined, an FS::pkg_referral record will
234 be created and inserted. Multiple FS::pkg_referral records can be created by
235 setting I<refnum> to an array reference of refnums or a hash reference with
236 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
237 record will be created corresponding to cust_main.refnum.
239 The following options are available:
245 If set true, supresses any referral credit to a referring customer.
249 cust_pkg_option records will be created
253 a ticket will be added to this customer with this subject
257 an optional queue name for ticket additions
264 my( $self, %options ) = @_;
266 my $error = $self->check_pkgpart;
267 return $error if $error;
269 if ( $self->part_pkg->option('start_1st', 1) && !$self->start_date ) {
270 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
271 $mon += 1 unless $mday == 1;
272 until ( $mon < 12 ) { $mon -= 12; $year++; }
273 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
276 foreach my $action ( qw(expire adjourn contract_end) ) {
277 my $months = $self->part_pkg->option("${action}_months",1);
278 if($months and !$self->$action) {
279 my $start = $self->start_date || $self->setup || time;
280 $self->$action( $self->part_pkg->add_freq($start, $months) );
284 $self->order_date(time);
286 local $SIG{HUP} = 'IGNORE';
287 local $SIG{INT} = 'IGNORE';
288 local $SIG{QUIT} = 'IGNORE';
289 local $SIG{TERM} = 'IGNORE';
290 local $SIG{TSTP} = 'IGNORE';
291 local $SIG{PIPE} = 'IGNORE';
293 my $oldAutoCommit = $FS::UID::AutoCommit;
294 local $FS::UID::AutoCommit = 0;
297 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
299 $dbh->rollback if $oldAutoCommit;
303 $self->refnum($self->cust_main->refnum) unless $self->refnum;
304 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
305 $self->process_m2m( 'link_table' => 'pkg_referral',
306 'target_table' => 'part_referral',
307 'params' => $self->refnum,
310 if ( $self->discountnum ) {
311 my $error = $self->insert_discount();
313 $dbh->rollback if $oldAutoCommit;
318 #if ( $self->reg_code ) {
319 # my $reg_code = qsearchs('reg_code', { 'code' => $self->reg_code } );
320 # $error = $reg_code->delete;
322 # $dbh->rollback if $oldAutoCommit;
327 my $conf = new FS::Conf;
329 if ( $conf->config('ticket_system') && $options{ticket_subject} ) {
332 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
339 use FS::TicketSystem;
340 FS::TicketSystem->init();
342 my $q = new RT::Queue($RT::SystemUser);
343 $q->Load($options{ticket_queue}) if $options{ticket_queue};
344 my $t = new RT::Ticket($RT::SystemUser);
345 my $mime = new MIME::Entity;
346 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
347 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
348 Subject => $options{ticket_subject},
351 $t->AddLink( Type => 'MemberOf',
352 Target => 'freeside://freeside/cust_main/'. $self->custnum,
356 if ($conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
357 my $queue = new FS::queue {
358 'job' => 'FS::cust_main::queueable_print',
360 $error = $queue->insert(
361 'custnum' => $self->custnum,
362 'template' => 'welcome_letter',
366 warn "can't send welcome letter: $error";
371 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
378 This method now works but you probably shouldn't use it.
380 You don't want to delete packages, because there would then be no record
381 the customer ever purchased the package. Instead, see the cancel method and
382 hide cancelled packages.
389 local $SIG{HUP} = 'IGNORE';
390 local $SIG{INT} = 'IGNORE';
391 local $SIG{QUIT} = 'IGNORE';
392 local $SIG{TERM} = 'IGNORE';
393 local $SIG{TSTP} = 'IGNORE';
394 local $SIG{PIPE} = 'IGNORE';
396 my $oldAutoCommit = $FS::UID::AutoCommit;
397 local $FS::UID::AutoCommit = 0;
400 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
401 my $error = $cust_pkg_discount->delete;
403 $dbh->rollback if $oldAutoCommit;
407 #cust_bill_pkg_discount?
409 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
410 my $error = $cust_pkg_detail->delete;
412 $dbh->rollback if $oldAutoCommit;
417 foreach my $cust_pkg_reason (
419 'table' => 'cust_pkg_reason',
420 'hashref' => { 'pkgnum' => $self->pkgnum },
424 my $error = $cust_pkg_reason->delete;
426 $dbh->rollback if $oldAutoCommit;
433 my $error = $self->SUPER::delete(@_);
435 $dbh->rollback if $oldAutoCommit;
439 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
445 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
447 Replaces the OLD_RECORD with this one in the database. If there is an error,
448 returns the error, otherwise returns false.
450 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
452 Changing pkgpart may have disasterous effects. See the order subroutine.
454 setup and bill are normally updated by calling the bill method of a customer
455 object (see L<FS::cust_main>).
457 suspend is normally updated by the suspend and unsuspend methods.
459 cancel is normally updated by the cancel method (and also the order subroutine
462 Available options are:
468 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.
472 the access_user (see L<FS::access_user>) providing the reason
476 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
485 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
490 ( ref($_[0]) eq 'HASH' )
494 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
495 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
498 #return "Can't change setup once it exists!"
499 # if $old->getfield('setup') &&
500 # $old->getfield('setup') != $new->getfield('setup');
502 #some logic for bill, susp, cancel?
504 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
506 local $SIG{HUP} = 'IGNORE';
507 local $SIG{INT} = 'IGNORE';
508 local $SIG{QUIT} = 'IGNORE';
509 local $SIG{TERM} = 'IGNORE';
510 local $SIG{TSTP} = 'IGNORE';
511 local $SIG{PIPE} = 'IGNORE';
513 my $oldAutoCommit = $FS::UID::AutoCommit;
514 local $FS::UID::AutoCommit = 0;
517 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
518 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
519 my $error = $new->insert_reason(
520 'reason' => $options->{'reason'},
521 'date' => $new->$method,
523 'reason_otaker' => $options->{'reason_otaker'},
526 dbh->rollback if $oldAutoCommit;
527 return "Error inserting cust_pkg_reason: $error";
532 #save off and freeze RADIUS attributes for any associated svc_acct records
534 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
536 #also check for specific exports?
537 # to avoid spurious modify export events
538 @svc_acct = map { $_->svc_x }
539 grep { $_->part_svc->svcdb eq 'svc_acct' }
542 $_->snapshot foreach @svc_acct;
546 my $error = $new->SUPER::replace($old,
547 $options->{options} ? $options->{options} : ()
550 $dbh->rollback if $oldAutoCommit;
554 #for prepaid packages,
555 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
556 foreach my $old_svc_acct ( @svc_acct ) {
557 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
559 $new_svc_acct->replace( $old_svc_acct,
560 'depend_jobnum' => $options->{depend_jobnum},
563 $dbh->rollback if $oldAutoCommit;
568 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
575 Checks all fields to make sure this is a valid billing item. If there is an
576 error, returns the error, otherwise returns false. Called by the insert and
584 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
587 $self->ut_numbern('pkgnum')
588 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
589 || $self->ut_numbern('pkgpart')
590 || $self->check_pkgpart
591 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
592 || $self->ut_numbern('start_date')
593 || $self->ut_numbern('setup')
594 || $self->ut_numbern('bill')
595 || $self->ut_numbern('susp')
596 || $self->ut_numbern('cancel')
597 || $self->ut_numbern('adjourn')
598 || $self->ut_numbern('expire')
599 || $self->ut_numbern('dundate')
600 || $self->ut_enum('no_auto', [ '', 'Y' ])
601 || $self->ut_enum('waive_setup', [ '', 'Y' ])
602 || $self->ut_numbern('agent_pkgid')
603 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
604 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
606 return $error if $error;
608 return "A package with both start date (future start) and setup date (already started) will never bill"
609 if $self->start_date && $self->setup;
611 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
613 if ( $self->dbdef_table->column('manual_flag') ) {
614 $self->manual_flag('') if $self->manual_flag eq ' ';
615 $self->manual_flag =~ /^([01]?)$/
616 or return "Illegal manual_flag ". $self->manual_flag;
617 $self->manual_flag($1);
630 my $error = $self->ut_numbern('pkgpart');
631 return $error if $error;
633 if ( $self->reg_code ) {
635 unless ( grep { $self->pkgpart == $_->pkgpart }
636 map { $_->reg_code_pkg }
637 qsearchs( 'reg_code', { 'code' => $self->reg_code,
638 'agentnum' => $self->cust_main->agentnum })
640 return "Unknown registration code";
643 } elsif ( $self->promo_code ) {
646 qsearchs('part_pkg', {
647 'pkgpart' => $self->pkgpart,
648 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
650 return 'Unknown promotional code' unless $promo_part_pkg;
654 unless ( $disable_agentcheck ) {
656 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
657 return "agent ". $agent->agentnum. ':'. $agent->agent.
658 " can't purchase pkgpart ". $self->pkgpart
659 unless $agent->pkgpart_hashref->{ $self->pkgpart }
660 || $agent->agentnum == $self->part_pkg->agentnum;
663 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
664 return $error if $error;
672 =item cancel [ OPTION => VALUE ... ]
674 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
675 in this package, then cancels the package itself (sets the cancel field to
678 Available options are:
682 =item quiet - can be set true to supress email cancellation notices.
684 =item time - can be set to cancel the package based on a specific future or historical date. Using time ensures that the remaining amount is calculated correctly. Note however that this is an immediate cancel and just changes the date. You are PROBABLY looking to expire the account instead of using this.
686 =item reason - 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.
688 =item date - can be set to a unix style timestamp to specify when to cancel (expire)
690 =item nobill - can be set true to skip billing if it might otherwise be done.
692 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
693 not credit it. This must be set (by change()) when changing the package
694 to a different pkgpart or location, and probably shouldn't be in any other
695 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
700 If there is an error, returns the error, otherwise returns false.
705 my( $self, %options ) = @_;
708 my $conf = new FS::Conf;
710 warn "cust_pkg::cancel called with options".
711 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
714 local $SIG{HUP} = 'IGNORE';
715 local $SIG{INT} = 'IGNORE';
716 local $SIG{QUIT} = 'IGNORE';
717 local $SIG{TERM} = 'IGNORE';
718 local $SIG{TSTP} = 'IGNORE';
719 local $SIG{PIPE} = 'IGNORE';
721 my $oldAutoCommit = $FS::UID::AutoCommit;
722 local $FS::UID::AutoCommit = 0;
725 my $old = $self->select_for_update;
727 if ( $old->get('cancel') || $self->get('cancel') ) {
728 dbh->rollback if $oldAutoCommit;
729 return ""; # no error
732 my $date = $options{date} if $options{date}; # expire/cancel later
733 $date = '' if ($date && $date <= time); # complain instead?
735 #race condition: usage could be ongoing until unprovisioned
736 #resolved by performing a change package instead (which unprovisions) and
738 if ( !$options{nobill} && !$date && $conf->exists('bill_usage_on_cancel') ) {
739 my $copy = $self->new({$self->hash});
741 $copy->cust_main->bill( pkg_list => [ $copy ], cancel => 1 );
742 warn "Error billing during cancel, custnum ".
743 #$self->cust_main->custnum. ": $error"
748 my $cancel_time = $options{'time'} || time;
750 if ( $options{'reason'} ) {
751 $error = $self->insert_reason( 'reason' => $options{'reason'},
752 'action' => $date ? 'expire' : 'cancel',
753 'date' => $date ? $date : $cancel_time,
754 'reason_otaker' => $options{'reason_otaker'},
757 dbh->rollback if $oldAutoCommit;
758 return "Error inserting cust_pkg_reason: $error";
762 my %svc_cancel_opt = ();
763 $svc_cancel_opt{'date'} = $date if $date;
764 foreach my $cust_svc (
767 sort { $a->[1] <=> $b->[1] }
768 map { [ $_, $_->svc_x->table_info->{'cancel_weight'} ]; }
769 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
771 my $part_svc = $cust_svc->part_svc;
772 next if ( defined($part_svc) and $part_svc->preserve );
773 my $error = $cust_svc->cancel( %svc_cancel_opt );
776 $dbh->rollback if $oldAutoCommit;
777 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
784 # Add a credit for remaining service
785 my $last_bill = $self->getfield('last_bill') || 0;
786 my $next_bill = $self->getfield('bill') || 0;
788 if ( exists($options{'unused_credit'}) ) {
789 $do_credit = $options{'unused_credit'};
792 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
795 and $last_bill > 0 # the package has been billed
796 and $next_bill > 0 # the package has a next bill date
797 and $next_bill >= $cancel_time # which is in the future
799 my $remaining_value = $self->calc_remain('time' => $cancel_time);
800 if ( $remaining_value > 0 ) {
801 my $error = $self->cust_main->credit(
803 'Credit for unused time on '. $self->part_pkg->pkg,
804 'reason_type' => $conf->config('cancel_credit_type'),
807 $dbh->rollback if $oldAutoCommit;
808 return "Error crediting customer \$$remaining_value for unused time".
809 " on ". $self->part_pkg->pkg. ": $error";
811 } #if $remaining_value
816 my %hash = $self->hash;
817 $date ? ($hash{'expire'} = $date) : ($hash{'cancel'} = $cancel_time);
818 my $new = new FS::cust_pkg ( \%hash );
819 $error = $new->replace( $self, options => { $self->options } );
821 $dbh->rollback if $oldAutoCommit;
825 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
826 return '' if $date; #no errors
828 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
829 if ( !$options{'quiet'} &&
830 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
832 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
835 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
836 $error = $msg_template->send( 'cust_main' => $self->cust_main,
841 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
842 'to' => \@invoicing_list,
843 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
844 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
847 #should this do something on errors?
854 =item cancel_if_expired [ NOW_TIMESTAMP ]
856 Cancels this package if its expire date has been reached.
860 sub cancel_if_expired {
862 my $time = shift || time;
863 return '' unless $self->expire && $self->expire <= $time;
864 my $error = $self->cancel;
866 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
867 $self->custnum. ": $error";
874 Cancels any pending expiration (sets the expire field to null).
876 If there is an error, returns the error, otherwise returns false.
881 my( $self, %options ) = @_;
884 local $SIG{HUP} = 'IGNORE';
885 local $SIG{INT} = 'IGNORE';
886 local $SIG{QUIT} = 'IGNORE';
887 local $SIG{TERM} = 'IGNORE';
888 local $SIG{TSTP} = 'IGNORE';
889 local $SIG{PIPE} = 'IGNORE';
891 my $oldAutoCommit = $FS::UID::AutoCommit;
892 local $FS::UID::AutoCommit = 0;
895 my $old = $self->select_for_update;
897 my $pkgnum = $old->pkgnum;
898 if ( $old->get('cancel') || $self->get('cancel') ) {
899 dbh->rollback if $oldAutoCommit;
900 return "Can't unexpire cancelled package $pkgnum";
901 # or at least it's pointless
904 unless ( $old->get('expire') && $self->get('expire') ) {
905 dbh->rollback if $oldAutoCommit;
906 return ""; # no error
909 my %hash = $self->hash;
910 $hash{'expire'} = '';
911 my $new = new FS::cust_pkg ( \%hash );
912 $error = $new->replace( $self, options => { $self->options } );
914 $dbh->rollback if $oldAutoCommit;
918 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
924 =item suspend [ OPTION => VALUE ... ]
926 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
927 package, then suspends the package itself (sets the susp field to now).
929 Available options are:
933 =item reason - 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.
935 =item date - can be set to a unix style timestamp to specify when to suspend (adjourn)
939 If there is an error, returns the error, otherwise returns false.
944 my( $self, %options ) = @_;
947 local $SIG{HUP} = 'IGNORE';
948 local $SIG{INT} = 'IGNORE';
949 local $SIG{QUIT} = 'IGNORE';
950 local $SIG{TERM} = 'IGNORE';
951 local $SIG{TSTP} = 'IGNORE';
952 local $SIG{PIPE} = 'IGNORE';
954 my $oldAutoCommit = $FS::UID::AutoCommit;
955 local $FS::UID::AutoCommit = 0;
958 my $old = $self->select_for_update;
960 my $pkgnum = $old->pkgnum;
961 if ( $old->get('cancel') || $self->get('cancel') ) {
962 dbh->rollback if $oldAutoCommit;
963 return "Can't suspend cancelled package $pkgnum";
966 if ( $old->get('susp') || $self->get('susp') ) {
967 dbh->rollback if $oldAutoCommit;
968 return ""; # no error # complain on adjourn?
971 my $date = $options{date} if $options{date}; # adjourn/suspend later
972 $date = '' if ($date && $date <= time); # complain instead?
974 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
975 dbh->rollback if $oldAutoCommit;
976 return "Package $pkgnum expires before it would be suspended.";
979 my $suspend_time = $options{'time'} || time;
981 if ( $options{'reason'} ) {
982 $error = $self->insert_reason( 'reason' => $options{'reason'},
983 'action' => $date ? 'adjourn' : 'suspend',
984 'date' => $date ? $date : $suspend_time,
985 'reason_otaker' => $options{'reason_otaker'},
988 dbh->rollback if $oldAutoCommit;
989 return "Error inserting cust_pkg_reason: $error";
997 foreach my $cust_svc (
998 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
1000 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1002 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1003 $dbh->rollback if $oldAutoCommit;
1004 return "Illegal svcdb value in part_svc!";
1007 require "FS/$svcdb.pm";
1009 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1011 $error = $svc->suspend;
1013 $dbh->rollback if $oldAutoCommit;
1016 my( $label, $value ) = $cust_svc->label;
1017 push @labels, "$label: $value";
1021 my $conf = new FS::Conf;
1022 if ( $conf->config('suspend_email_admin') ) {
1024 my $error = send_email(
1025 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1026 #invoice_from ??? well as good as any
1027 'to' => $conf->config('suspend_email_admin'),
1028 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1030 "This is an automatic message from your Freeside installation\n",
1031 "informing you that the following customer package has been suspended:\n",
1033 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1034 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1035 ( map { "Service : $_\n" } @labels ),
1040 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1048 my %hash = $self->hash;
1050 $hash{'adjourn'} = $date;
1052 $hash{'susp'} = $suspend_time;
1054 my $new = new FS::cust_pkg ( \%hash );
1055 $error = $new->replace( $self, options => { $self->options } );
1057 $dbh->rollback if $oldAutoCommit;
1061 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1066 =item unsuspend [ OPTION => VALUE ... ]
1068 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1069 package, then unsuspends the package itself (clears the susp field and the
1070 adjourn field if it is in the past).
1072 Available options are:
1076 =item adjust_next_bill
1078 Can be set true to adjust the next bill date forward by
1079 the amount of time the account was inactive. This was set true by default
1080 since 1.4.2 and 1.5.0pre6; however, starting with 1.7.0 this needs to be
1081 explicitly requested. Price plans for which this makes sense (anniversary-date
1082 based than prorate or subscription) could have an option to enable this
1087 If there is an error, returns the error, otherwise returns false.
1092 my( $self, %opt ) = @_;
1095 local $SIG{HUP} = 'IGNORE';
1096 local $SIG{INT} = 'IGNORE';
1097 local $SIG{QUIT} = 'IGNORE';
1098 local $SIG{TERM} = 'IGNORE';
1099 local $SIG{TSTP} = 'IGNORE';
1100 local $SIG{PIPE} = 'IGNORE';
1102 my $oldAutoCommit = $FS::UID::AutoCommit;
1103 local $FS::UID::AutoCommit = 0;
1106 my $old = $self->select_for_update;
1108 my $pkgnum = $old->pkgnum;
1109 if ( $old->get('cancel') || $self->get('cancel') ) {
1110 dbh->rollback if $oldAutoCommit;
1111 return "Can't unsuspend cancelled package $pkgnum";
1114 unless ( $old->get('susp') && $self->get('susp') ) {
1115 dbh->rollback if $oldAutoCommit;
1116 return ""; # no error # complain instead?
1119 foreach my $cust_svc (
1120 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1122 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1124 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1125 $dbh->rollback if $oldAutoCommit;
1126 return "Illegal svcdb value in part_svc!";
1129 require "FS/$svcdb.pm";
1131 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1133 $error = $svc->unsuspend;
1135 $dbh->rollback if $oldAutoCommit;
1142 my %hash = $self->hash;
1143 my $inactive = time - $hash{'susp'};
1145 my $conf = new FS::Conf;
1147 if ( $inactive > 0 &&
1148 ( $hash{'bill'} || $hash{'setup'} ) &&
1149 ( $opt{'adjust_next_bill'} ||
1150 $conf->exists('unsuspend-always_adjust_next_bill_date') ||
1151 $self->part_pkg->option('unsuspend_adjust_bill', 1) )
1154 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
1159 $hash{'adjourn'} = '' if $hash{'adjourn'} < time;
1160 my $new = new FS::cust_pkg ( \%hash );
1161 $error = $new->replace( $self, options => { $self->options } );
1163 $dbh->rollback if $oldAutoCommit;
1167 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1174 Cancels any pending suspension (sets the adjourn field to null).
1176 If there is an error, returns the error, otherwise returns false.
1181 my( $self, %options ) = @_;
1184 local $SIG{HUP} = 'IGNORE';
1185 local $SIG{INT} = 'IGNORE';
1186 local $SIG{QUIT} = 'IGNORE';
1187 local $SIG{TERM} = 'IGNORE';
1188 local $SIG{TSTP} = 'IGNORE';
1189 local $SIG{PIPE} = 'IGNORE';
1191 my $oldAutoCommit = $FS::UID::AutoCommit;
1192 local $FS::UID::AutoCommit = 0;
1195 my $old = $self->select_for_update;
1197 my $pkgnum = $old->pkgnum;
1198 if ( $old->get('cancel') || $self->get('cancel') ) {
1199 dbh->rollback if $oldAutoCommit;
1200 return "Can't unadjourn cancelled package $pkgnum";
1201 # or at least it's pointless
1204 if ( $old->get('susp') || $self->get('susp') ) {
1205 dbh->rollback if $oldAutoCommit;
1206 return "Can't unadjourn suspended package $pkgnum";
1207 # perhaps this is arbitrary
1210 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1211 dbh->rollback if $oldAutoCommit;
1212 return ""; # no error
1215 my %hash = $self->hash;
1216 $hash{'adjourn'} = '';
1217 my $new = new FS::cust_pkg ( \%hash );
1218 $error = $new->replace( $self, options => { $self->options } );
1220 $dbh->rollback if $oldAutoCommit;
1224 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1231 =item change HASHREF | OPTION => VALUE ...
1233 Changes this package: cancels it and creates a new one, with a different
1234 pkgpart or locationnum or both. All services are transferred to the new
1235 package (no change will be made if this is not possible).
1237 Options may be passed as a list of key/value pairs or as a hash reference.
1244 New locationnum, to change the location for this package.
1248 New FS::cust_location object, to create a new location and assign it
1253 New pkgpart (see L<FS::part_pkg>).
1257 New refnum (see L<FS::part_referral>).
1261 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1262 susp, adjourn, cancel, expire, and contract_end) to the new package.
1266 At least one of locationnum, cust_location, pkgpart, refnum must be specified
1267 (otherwise, what's the point?)
1269 Returns either the new FS::cust_pkg object or a scalar error.
1273 my $err_or_new_cust_pkg = $old_cust_pkg->change
1277 #some false laziness w/order
1280 my $opt = ref($_[0]) ? shift : { @_ };
1282 # my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
1285 my $conf = new FS::Conf;
1287 # Transactionize this whole mess
1288 local $SIG{HUP} = 'IGNORE';
1289 local $SIG{INT} = 'IGNORE';
1290 local $SIG{QUIT} = 'IGNORE';
1291 local $SIG{TERM} = 'IGNORE';
1292 local $SIG{TSTP} = 'IGNORE';
1293 local $SIG{PIPE} = 'IGNORE';
1295 my $oldAutoCommit = $FS::UID::AutoCommit;
1296 local $FS::UID::AutoCommit = 0;
1305 #$hash{$_} = $self->$_() foreach qw( last_bill bill );
1307 #$hash{$_} = $self->$_() foreach qw( setup );
1309 $hash{'setup'} = $time if $self->setup;
1311 $hash{'change_date'} = $time;
1312 $hash{"change_$_"} = $self->$_()
1313 foreach qw( pkgnum pkgpart locationnum );
1315 if ( $opt->{'cust_location'} &&
1316 ( ! $opt->{'locationnum'} || $opt->{'locationnum'} == -1 ) ) {
1317 $error = $opt->{'cust_location'}->insert;
1319 $dbh->rollback if $oldAutoCommit;
1320 return "inserting cust_location (transaction rolled back): $error";
1322 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1325 my $unused_credit = 0;
1326 if ( $opt->{'keep_dates'} ) {
1327 foreach my $date ( qw(setup bill last_bill susp adjourn cancel expire
1328 start_date contract_end ) ) {
1329 $hash{$date} = $self->getfield($date);
1332 # Special case. If the pkgpart is changing, and the customer is
1333 # going to be credited for remaining time, don't keep setup, bill,
1334 # or last_bill dates, and DO pass the flag to cancel() to credit
1336 if ( $opt->{'pkgpart'}
1337 and $opt->{'pkgpart'} != $self->pkgpart
1338 and $self->part_pkg->option('unused_credit_change', 1) ) {
1340 $hash{$_} = '' foreach qw(setup bill last_bill);
1343 # Create the new package.
1344 my $cust_pkg = new FS::cust_pkg {
1345 custnum => $self->custnum,
1346 pkgpart => ( $opt->{'pkgpart'} || $self->pkgpart ),
1347 refnum => ( $opt->{'refnum'} || $self->refnum ),
1348 locationnum => ( $opt->{'locationnum'} || $self->locationnum ),
1352 $error = $cust_pkg->insert( 'change' => 1 );
1354 $dbh->rollback if $oldAutoCommit;
1358 # Transfer services and cancel old package.
1360 $error = $self->transfer($cust_pkg);
1361 if ($error and $error == 0) {
1362 # $old_pkg->transfer failed.
1363 $dbh->rollback if $oldAutoCommit;
1367 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
1368 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
1369 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
1370 if ($error and $error == 0) {
1371 # $old_pkg->transfer failed.
1372 $dbh->rollback if $oldAutoCommit;
1378 # Transfers were successful, but we still had services left on the old
1379 # package. We can't change the package under this circumstances, so abort.
1380 $dbh->rollback if $oldAutoCommit;
1381 return "Unable to transfer all services from package ". $self->pkgnum;
1384 #reset usage if changing pkgpart
1385 # AND usage rollover is off (otherwise adds twice, now and at package bill)
1386 if ($self->pkgpart != $cust_pkg->pkgpart) {
1387 my $part_pkg = $cust_pkg->part_pkg;
1388 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
1392 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
1395 $dbh->rollback if $oldAutoCommit;
1396 return "Error setting usage values: $error";
1400 #Good to go, cancel old package. Notify 'cancel' of whether to credit
1402 $error = $self->cancel( quiet=>1, unused_credit => $unused_credit );
1404 $dbh->rollback if $oldAutoCommit;
1408 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
1410 my $error = $cust_pkg->cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1412 $dbh->rollback if $oldAutoCommit;
1417 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1423 use Storable 'thaw';
1425 sub process_bulk_cust_pkg {
1427 my $param = thaw(decode_base64(shift));
1428 warn Dumper($param) if $DEBUG;
1430 my $old_part_pkg = qsearchs('part_pkg',
1431 { pkgpart => $param->{'old_pkgpart'} });
1432 my $new_part_pkg = qsearchs('part_pkg',
1433 { pkgpart => $param->{'new_pkgpart'} });
1434 die "Must select a new package type\n" unless $new_part_pkg;
1435 #my $keep_dates = $param->{'keep_dates'} || 0;
1436 my $keep_dates = 1; # there is no good reason to turn this off
1438 local $SIG{HUP} = 'IGNORE';
1439 local $SIG{INT} = 'IGNORE';
1440 local $SIG{QUIT} = 'IGNORE';
1441 local $SIG{TERM} = 'IGNORE';
1442 local $SIG{TSTP} = 'IGNORE';
1443 local $SIG{PIPE} = 'IGNORE';
1445 my $oldAutoCommit = $FS::UID::AutoCommit;
1446 local $FS::UID::AutoCommit = 0;
1449 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
1452 foreach my $old_cust_pkg ( @cust_pkgs ) {
1454 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
1455 if ( $old_cust_pkg->getfield('cancel') ) {
1456 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
1457 $old_cust_pkg->pkgnum."\n"
1461 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
1463 my $error = $old_cust_pkg->change(
1464 'pkgpart' => $param->{'new_pkgpart'},
1465 'keep_dates' => $keep_dates
1467 if ( !ref($error) ) { # change returns the cust_pkg on success
1469 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
1472 $dbh->commit if $oldAutoCommit;
1478 Returns the last bill date, or if there is no last bill date, the setup date.
1479 Useful for billing metered services.
1485 return $self->setfield('last_bill', $_[0]) if @_;
1486 return $self->getfield('last_bill') if $self->getfield('last_bill');
1487 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
1488 'edate' => $self->bill, } );
1489 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
1492 =item last_cust_pkg_reason ACTION
1494 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
1495 Returns false if there is no reason or the package is not currenly ACTION'd
1496 ACTION is one of adjourn, susp, cancel, or expire.
1500 sub last_cust_pkg_reason {
1501 my ( $self, $action ) = ( shift, shift );
1502 my $date = $self->get($action);
1504 'table' => 'cust_pkg_reason',
1505 'hashref' => { 'pkgnum' => $self->pkgnum,
1506 'action' => substr(uc($action), 0, 1),
1509 'order_by' => 'ORDER BY num DESC LIMIT 1',
1513 =item last_reason ACTION
1515 Returns the most recent ACTION FS::reason associated with the package.
1516 Returns false if there is no reason or the package is not currenly ACTION'd
1517 ACTION is one of adjourn, susp, cancel, or expire.
1522 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
1523 $cust_pkg_reason->reason
1524 if $cust_pkg_reason;
1529 Returns the definition for this billing item, as an FS::part_pkg object (see
1536 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
1537 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
1538 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
1543 Returns the cancelled package this package was changed from, if any.
1549 return '' unless $self->change_pkgnum;
1550 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
1555 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
1562 $self->part_pkg->calc_setup($self, @_);
1567 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
1574 $self->part_pkg->calc_recur($self, @_);
1579 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
1586 $self->part_pkg->base_recur($self, @_);
1591 Calls the I<calc_remain> of the FS::part_pkg object associated with this
1598 $self->part_pkg->calc_remain($self, @_);
1603 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
1610 $self->part_pkg->calc_cancel($self, @_);
1615 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
1621 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
1624 =item cust_pkg_detail [ DETAILTYPE ]
1626 Returns any customer package details for this package (see
1627 L<FS::cust_pkg_detail>).
1629 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
1633 sub cust_pkg_detail {
1635 my %hash = ( 'pkgnum' => $self->pkgnum );
1636 $hash{detailtype} = shift if @_;
1638 'table' => 'cust_pkg_detail',
1639 'hashref' => \%hash,
1640 'order_by' => 'ORDER BY weight, pkgdetailnum',
1644 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
1646 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
1648 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
1650 If there is an error, returns the error, otherwise returns false.
1654 sub set_cust_pkg_detail {
1655 my( $self, $detailtype, @details ) = @_;
1657 local $SIG{HUP} = 'IGNORE';
1658 local $SIG{INT} = 'IGNORE';
1659 local $SIG{QUIT} = 'IGNORE';
1660 local $SIG{TERM} = 'IGNORE';
1661 local $SIG{TSTP} = 'IGNORE';
1662 local $SIG{PIPE} = 'IGNORE';
1664 my $oldAutoCommit = $FS::UID::AutoCommit;
1665 local $FS::UID::AutoCommit = 0;
1668 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
1669 my $error = $current->delete;
1671 $dbh->rollback if $oldAutoCommit;
1672 return "error removing old detail: $error";
1676 foreach my $detail ( @details ) {
1677 my $cust_pkg_detail = new FS::cust_pkg_detail {
1678 'pkgnum' => $self->pkgnum,
1679 'detailtype' => $detailtype,
1680 'detail' => $detail,
1682 my $error = $cust_pkg_detail->insert;
1684 $dbh->rollback if $oldAutoCommit;
1685 return "error adding new detail: $error";
1690 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1697 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
1701 #false laziness w/cust_bill.pm
1705 'table' => 'cust_event',
1706 'addl_from' => 'JOIN part_event USING ( eventpart )',
1707 'hashref' => { 'tablenum' => $self->pkgnum },
1708 'extra_sql' => " AND eventtable = 'cust_pkg' ",
1712 =item num_cust_event
1714 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
1718 #false laziness w/cust_bill.pm
1719 sub num_cust_event {
1722 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
1723 " WHERE tablenum = ? AND eventtable = 'cust_pkg'";
1724 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
1725 $sth->execute($self->pkgnum) or die $sth->errstr. " executing $sql";
1726 $sth->fetchrow_arrayref->[0];
1729 =item cust_svc [ SVCPART ]
1731 Returns the services for this package, as FS::cust_svc objects (see
1732 L<FS::cust_svc>). If a svcpart is specified, return only the matching
1740 return () unless $self->num_cust_svc(@_);
1743 return qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum,
1744 'svcpart' => shift, } );
1747 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
1749 #if ( $self->{'_svcnum'} ) {
1750 # values %{ $self->{'_svcnum'}->cache };
1752 $self->_sort_cust_svc(
1753 [ qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } ) ]
1759 =item overlimit [ SVCPART ]
1761 Returns the services for this package which have exceeded their
1762 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
1763 is specified, return only the matching services.
1769 return () unless $self->num_cust_svc(@_);
1770 grep { $_->overlimit } $self->cust_svc(@_);
1773 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
1775 Returns historical services for this package created before END TIMESTAMP and
1776 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
1777 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
1778 I<pkg_svc.hidden> flag will be omitted.
1784 warn "$me _h_cust_svc called on $self\n"
1787 my ($end, $start, $mode) = @_;
1788 my @cust_svc = $self->_sort_cust_svc(
1789 [ qsearch( 'h_cust_svc',
1790 { 'pkgnum' => $self->pkgnum, },
1791 FS::h_cust_svc->sql_h_search(@_),
1794 if ( $mode eq 'I' ) {
1795 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
1796 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
1802 sub _sort_cust_svc {
1803 my( $self, $arrayref ) = @_;
1806 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
1811 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $self->pkgpart,
1812 'svcpart' => $_->svcpart } );
1814 $pkg_svc ? $pkg_svc->primary_svc : '',
1815 $pkg_svc ? $pkg_svc->quantity : 0,
1822 =item num_cust_svc [ SVCPART ]
1824 Returns the number of provisioned services for this package. If a svcpart is
1825 specified, counts only the matching services.
1832 return $self->{'_num_cust_svc'}
1834 && exists($self->{'_num_cust_svc'})
1835 && $self->{'_num_cust_svc'} =~ /\d/;
1837 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
1840 my $sql = 'SELECT COUNT(*) FROM cust_svc WHERE pkgnum = ?';
1841 $sql .= ' AND svcpart = ?' if @_;
1843 my $sth = dbh->prepare($sql) or die dbh->errstr;
1844 $sth->execute($self->pkgnum, @_) or die $sth->errstr;
1845 $sth->fetchrow_arrayref->[0];
1848 =item available_part_svc
1850 Returns a list of FS::part_svc objects representing services included in this
1851 package but not yet provisioned. Each FS::part_svc object also has an extra
1852 field, I<num_avail>, which specifies the number of available services.
1856 sub available_part_svc {
1858 grep { $_->num_avail > 0 }
1860 my $part_svc = $_->part_svc;
1861 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
1862 $_->quantity - $self->num_cust_svc($_->svcpart);
1864 # more evil encapsulation breakage
1865 if($part_svc->{'Hash'}{'num_avail'} > 0) {
1866 my @exports = $part_svc->part_export_did;
1867 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
1872 $self->part_pkg->pkg_svc;
1877 Returns a list of FS::part_svc objects representing provisioned and available
1878 services included in this package. Each FS::part_svc object also has the
1879 following extra fields:
1883 =item num_cust_svc (count)
1885 =item num_avail (quantity - count)
1887 =item cust_pkg_svc (services) - array reference containing the provisioned services, as cust_svc objects
1890 label -> ($cust_svc->label)[1]
1899 #XXX some sort of sort order besides numeric by svcpart...
1900 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
1902 my $part_svc = $pkg_svc->part_svc;
1903 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
1904 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
1905 $part_svc->{'Hash'}{'num_avail'} =
1906 max( 0, $pkg_svc->quantity - $num_cust_svc );
1907 $part_svc->{'Hash'}{'cust_pkg_svc'} =
1908 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
1909 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
1911 } $self->part_pkg->pkg_svc;
1914 push @part_svc, map {
1916 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
1917 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
1918 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
1919 $part_svc->{'Hash'}{'cust_pkg_svc'} =
1920 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
1922 } $self->extra_part_svc;
1928 =item extra_part_svc
1930 Returns a list of FS::part_svc objects corresponding to services in this
1931 package which are still provisioned but not (any longer) available in the
1936 sub extra_part_svc {
1939 my $pkgnum = $self->pkgnum;
1940 my $pkgpart = $self->pkgpart;
1943 # 'table' => 'part_svc',
1946 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
1947 # WHERE pkg_svc.svcpart = part_svc.svcpart
1948 # AND pkg_svc.pkgpart = ?
1951 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
1952 # LEFT JOIN cust_pkg USING ( pkgnum )
1953 # WHERE cust_svc.svcpart = part_svc.svcpart
1956 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
1959 #seems to benchmark slightly faster...
1961 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
1962 #MySQL doesn't grok DISINCT ON
1963 'select' => 'DISTINCT part_svc.*',
1964 'table' => 'part_svc',
1966 'LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
1967 AND pkg_svc.pkgpart = ?
1970 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
1971 LEFT JOIN cust_pkg USING ( pkgnum )
1974 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
1975 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
1981 Returns a short status string for this package, currently:
1985 =item not yet billed
1987 =item one-time charge
2002 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
2004 return 'cancelled' if $self->get('cancel');
2005 return 'suspended' if $self->susp;
2006 return 'not yet billed' unless $self->setup;
2007 return 'one-time charge' if $freq =~ /^(0|$)/;
2011 =item ucfirst_status
2013 Returns the status with the first character capitalized.
2017 sub ucfirst_status {
2018 ucfirst(shift->status);
2023 Class method that returns the list of possible status strings for packages
2024 (see L<the status method|/status>). For example:
2026 @statuses = FS::cust_pkg->statuses();
2030 tie my %statuscolor, 'Tie::IxHash',
2031 'not yet billed' => '009999', #teal? cyan?
2032 'one-time charge' => '000000',
2033 'active' => '00CC00',
2034 'suspended' => 'FF9900',
2035 'cancelled' => 'FF0000',
2039 my $self = shift; #could be class...
2040 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
2041 # # mayble split btw one-time vs. recur
2047 Returns a hex triplet color string for this package's status.
2053 $statuscolor{$self->status};
2058 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
2059 "pkg-comment" depending on user preference).
2065 my $label = $self->part_pkg->pkg_comment( 'nopkgpart' => 1 );
2066 $label = $self->pkgnum. ": $label"
2067 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
2071 =item pkg_label_long
2073 Returns a long label for this package, adding the primary service's label to
2078 sub pkg_label_long {
2080 my $label = $self->pkg_label;
2081 my $cust_svc = $self->primary_cust_svc;
2082 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
2086 =item primary_cust_svc
2088 Returns a primary service (as FS::cust_svc object) if one can be identified.
2092 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
2094 sub primary_cust_svc {
2097 my @cust_svc = $self->cust_svc;
2099 return '' unless @cust_svc; #no serivces - irrelevant then
2101 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
2103 # primary service as specified in the package definition
2104 # or exactly one service definition with quantity one
2105 my $svcpart = $self->part_pkg->svcpart;
2106 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
2107 return $cust_svc[0] if scalar(@cust_svc) == 1;
2109 #couldn't identify one thing..
2115 Returns a list of lists, calling the label method for all services
2116 (see L<FS::cust_svc>) of this billing item.
2122 map { [ $_->label ] } $self->cust_svc;
2125 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
2127 Like the labels method, but returns historical information on services that
2128 were active as of END_TIMESTAMP and (optionally) not cancelled before
2129 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
2130 I<pkg_svc.hidden> flag will be omitted.
2132 Returns a list of lists, calling the label method for all (historical) services
2133 (see L<FS::h_cust_svc>) of this billing item.
2139 warn "$me _h_labels called on $self\n"
2141 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
2146 Like labels, except returns a simple flat list, and shortens long
2147 (currently >5 or the cust_bill-max_same_services configuration value) lists of
2148 identical services to one line that lists the service label and the number of
2149 individual services rather than individual items.
2154 shift->_labels_short( 'labels', @_ );
2157 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
2159 Like h_labels, except returns a simple flat list, and shortens long
2160 (currently >5 or the cust_bill-max_same_services configuration value) lists of
2161 identical services to one line that lists the service label and the number of
2162 individual services rather than individual items.
2166 sub h_labels_short {
2167 shift->_labels_short( 'h_labels', @_ );
2171 my( $self, $method ) = ( shift, shift );
2173 warn "$me _labels_short called on $self with $method method\n"
2176 my $conf = new FS::Conf;
2177 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
2179 warn "$me _labels_short populating \%labels\n"
2183 #tie %labels, 'Tie::IxHash';
2184 push @{ $labels{$_->[0]} }, $_->[1]
2185 foreach $self->$method(@_);
2187 warn "$me _labels_short populating \@labels\n"
2191 foreach my $label ( keys %labels ) {
2193 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
2194 my $num = scalar(@values);
2195 warn "$me _labels_short $num items for $label\n"
2198 if ( $num > $max_same_services ) {
2199 warn "$me _labels_short more than $max_same_services, so summarizing\n"
2201 push @labels, "$label ($num)";
2203 if ( $conf->exists('cust_bill-consolidate_services') ) {
2204 warn "$me _labels_short consolidating services\n"
2206 # push @labels, "$label: ". join(', ', @values);
2208 my $detail = "$label: ";
2209 $detail .= shift(@values). ', '
2211 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
2213 push @labels, $detail;
2215 warn "$me _labels_short done consolidating services\n"
2218 warn "$me _labels_short adding service data\n"
2220 push @labels, map { "$label: $_" } @values;
2231 Returns the parent customer object (see L<FS::cust_main>).
2237 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
2240 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
2244 Returns the location object, if any (see L<FS::cust_location>).
2246 =item cust_location_or_main
2248 If this package is associated with a location, returns the locaiton (see
2249 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
2251 =item location_label [ OPTION => VALUE ... ]
2253 Returns the label of the location object (see L<FS::cust_location>).
2257 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
2259 =item seconds_since TIMESTAMP
2261 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
2262 package have been online since TIMESTAMP, according to the session monitor.
2264 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
2265 L<Time::Local> and L<Date::Parse> for conversion functions.
2270 my($self, $since) = @_;
2273 foreach my $cust_svc (
2274 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
2276 $seconds += $cust_svc->seconds_since($since);
2283 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
2285 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
2286 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
2289 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2290 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2296 sub seconds_since_sqlradacct {
2297 my($self, $start, $end) = @_;
2301 foreach my $cust_svc (
2303 my $part_svc = $_->part_svc;
2304 $part_svc->svcdb eq 'svc_acct'
2305 && scalar($part_svc->part_export('sqlradius'));
2308 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
2315 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
2317 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
2318 in this package for sessions ending between TIMESTAMP_START (inclusive) and
2322 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2323 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2328 sub attribute_since_sqlradacct {
2329 my($self, $start, $end, $attrib) = @_;
2333 foreach my $cust_svc (
2335 my $part_svc = $_->part_svc;
2336 $part_svc->svcdb eq 'svc_acct'
2337 && scalar($part_svc->part_export('sqlradius'));
2340 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
2352 my( $self, $value ) = @_;
2353 if ( defined($value) ) {
2354 $self->setfield('quantity', $value);
2356 $self->getfield('quantity') || 1;
2359 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
2361 Transfers as many services as possible from this package to another package.
2363 The destination package can be specified by pkgnum by passing an FS::cust_pkg
2364 object. The destination package must already exist.
2366 Services are moved only if the destination allows services with the correct
2367 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
2368 this option with caution! No provision is made for export differences
2369 between the old and new service definitions. Probably only should be used
2370 when your exports for all service definitions of a given svcdb are identical.
2371 (attempt a transfer without it first, to move all possible svcpart-matching
2374 Any services that can't be moved remain in the original package.
2376 Returns an error, if there is one; otherwise, returns the number of services
2377 that couldn't be moved.
2382 my ($self, $dest_pkgnum, %opt) = @_;
2388 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
2389 $dest = $dest_pkgnum;
2390 $dest_pkgnum = $dest->pkgnum;
2392 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
2395 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
2397 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
2398 $target{$pkg_svc->svcpart} = $pkg_svc->quantity;
2401 foreach my $cust_svc ($dest->cust_svc) {
2402 $target{$cust_svc->svcpart}--;
2405 my %svcpart2svcparts = ();
2406 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
2407 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
2408 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
2409 next if exists $svcpart2svcparts{$svcpart};
2410 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2411 $svcpart2svcparts{$svcpart} = [
2413 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
2415 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
2416 'svcpart' => $_ } );
2418 $pkg_svc ? $pkg_svc->primary_svc : '',
2419 $pkg_svc ? $pkg_svc->quantity : 0,
2423 grep { $_ != $svcpart }
2425 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
2427 warn "alternates for svcpart $svcpart: ".
2428 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
2433 foreach my $cust_svc ($self->cust_svc) {
2434 if($target{$cust_svc->svcpart} > 0) {
2435 $target{$cust_svc->svcpart}--;
2436 my $new = new FS::cust_svc { $cust_svc->hash };
2437 $new->pkgnum($dest_pkgnum);
2438 my $error = $new->replace($cust_svc);
2439 return $error if $error;
2440 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
2442 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
2443 warn "alternates to consider: ".
2444 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
2446 my @alternate = grep {
2447 warn "considering alternate svcpart $_: ".
2448 "$target{$_} available in new package\n"
2451 } @{$svcpart2svcparts{$cust_svc->svcpart}};
2453 warn "alternate(s) found\n" if $DEBUG;
2454 my $change_svcpart = $alternate[0];
2455 $target{$change_svcpart}--;
2456 my $new = new FS::cust_svc { $cust_svc->hash };
2457 $new->svcpart($change_svcpart);
2458 $new->pkgnum($dest_pkgnum);
2459 my $error = $new->replace($cust_svc);
2460 return $error if $error;
2473 This method is deprecated. See the I<depend_jobnum> option to the insert and
2474 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
2481 local $SIG{HUP} = 'IGNORE';
2482 local $SIG{INT} = 'IGNORE';
2483 local $SIG{QUIT} = 'IGNORE';
2484 local $SIG{TERM} = 'IGNORE';
2485 local $SIG{TSTP} = 'IGNORE';
2486 local $SIG{PIPE} = 'IGNORE';
2488 my $oldAutoCommit = $FS::UID::AutoCommit;
2489 local $FS::UID::AutoCommit = 0;
2492 foreach my $cust_svc ( $self->cust_svc ) {
2493 #false laziness w/svc_Common::insert
2494 my $svc_x = $cust_svc->svc_x;
2495 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
2496 my $error = $part_export->export_insert($svc_x);
2498 $dbh->rollback if $oldAutoCommit;
2504 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2511 Associates this package with a (suspension or cancellation) reason (see
2512 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
2515 Available options are:
2521 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.
2525 the access_user (see L<FS::access_user>) providing the reason
2533 the action (cancel, susp, adjourn, expire) associated with the reason
2537 If there is an error, returns the error, otherwise returns false.
2542 my ($self, %options) = @_;
2544 my $otaker = $options{reason_otaker} ||
2545 $FS::CurrentUser::CurrentUser->username;
2548 if ( $options{'reason'} =~ /^(\d+)$/ ) {
2552 } elsif ( ref($options{'reason'}) ) {
2554 return 'Enter a new reason (or select an existing one)'
2555 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
2557 my $reason = new FS::reason({
2558 'reason_type' => $options{'reason'}->{'typenum'},
2559 'reason' => $options{'reason'}->{'reason'},
2561 my $error = $reason->insert;
2562 return $error if $error;
2564 $reasonnum = $reason->reasonnum;
2567 return "Unparsable reason: ". $options{'reason'};
2570 my $cust_pkg_reason =
2571 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
2572 'reasonnum' => $reasonnum,
2573 'otaker' => $otaker,
2574 'action' => substr(uc($options{'action'}),0,1),
2575 'date' => $options{'date'}
2580 $cust_pkg_reason->insert;
2583 =item insert_discount
2585 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
2586 inserting a new discount on the fly (see L<FS::discount>).
2588 Available options are:
2596 If there is an error, returns the error, otherwise returns false.
2600 sub insert_discount {
2601 #my ($self, %options) = @_;
2604 my $cust_pkg_discount = new FS::cust_pkg_discount {
2605 'pkgnum' => $self->pkgnum,
2606 'discountnum' => $self->discountnum,
2608 'end_date' => '', #XXX
2609 #for the create a new discount case
2610 '_type' => $self->discountnum__type,
2611 'amount' => $self->discountnum_amount,
2612 'percent' => $self->discountnum_percent,
2613 'months' => $self->discountnum_months,
2614 'setup' => $self->discountnum_setup,
2615 #'disabled' => $self->discountnum_disabled,
2618 $cust_pkg_discount->insert;
2621 =item set_usage USAGE_VALUE_HASHREF
2623 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
2624 to which they should be set (see L<FS::svc_acct>). Currently seconds,
2625 upbytes, downbytes, and totalbytes are appropriate keys.
2627 All svc_accts which are part of this package have their values reset.
2632 my ($self, $valueref, %opt) = @_;
2634 foreach my $cust_svc ($self->cust_svc){
2635 my $svc_x = $cust_svc->svc_x;
2636 $svc_x->set_usage($valueref, %opt)
2637 if $svc_x->can("set_usage");
2641 =item recharge USAGE_VALUE_HASHREF
2643 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
2644 to which they should be set (see L<FS::svc_acct>). Currently seconds,
2645 upbytes, downbytes, and totalbytes are appropriate keys.
2647 All svc_accts which are part of this package have their values incremented.
2652 my ($self, $valueref) = @_;
2654 foreach my $cust_svc ($self->cust_svc){
2655 my $svc_x = $cust_svc->svc_x;
2656 $svc_x->recharge($valueref)
2657 if $svc_x->can("recharge");
2661 =item cust_pkg_discount
2665 sub cust_pkg_discount {
2667 qsearch('cust_pkg_discount', { 'pkgnum' => $self->pkgnum } );
2670 =item cust_pkg_discount_active
2674 sub cust_pkg_discount_active {
2676 grep { $_->status eq 'active' } $self->cust_pkg_discount;
2681 =head1 CLASS METHODS
2687 Returns an SQL expression identifying recurring packages.
2691 sub recurring_sql { "
2692 '0' != ( select freq from part_pkg
2693 where cust_pkg.pkgpart = part_pkg.pkgpart )
2698 Returns an SQL expression identifying one-time packages.
2703 '0' = ( select freq from part_pkg
2704 where cust_pkg.pkgpart = part_pkg.pkgpart )
2709 Returns an SQL expression identifying ordered packages (recurring packages not
2715 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
2720 Returns an SQL expression identifying active packages.
2725 $_[0]->recurring_sql. "
2726 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
2727 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2728 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
2731 =item not_yet_billed_sql
2733 Returns an SQL expression identifying packages which have not yet been billed.
2737 sub not_yet_billed_sql { "
2738 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
2739 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2740 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
2745 Returns an SQL expression identifying inactive packages (one-time packages
2746 that are otherwise unsuspended/uncancelled).
2750 sub inactive_sql { "
2751 ". $_[0]->onetime_sql(). "
2752 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
2753 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2754 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
2760 Returns an SQL expression identifying suspended packages.
2764 sub suspended_sql { susp_sql(@_); }
2766 #$_[0]->recurring_sql(). ' AND '.
2768 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2769 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
2776 Returns an SQL exprression identifying cancelled packages.
2780 sub cancelled_sql { cancel_sql(@_); }
2782 #$_[0]->recurring_sql(). ' AND '.
2783 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
2788 Returns an SQL expression to give the package status as a string.
2794 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
2795 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
2796 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
2797 WHEN ".onetime_sql()." THEN 'one-time charge'
2802 =item search HASHREF
2806 Returns a qsearch hash expression to search for parameters specified in HASHREF.
2807 Valid parameters are
2815 active, inactive, suspended, cancel (or cancelled)
2819 active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
2823 boolean selects custom packages
2829 pkgpart or arrayref or hashref of pkgparts
2833 arrayref of beginning and ending epoch date
2837 arrayref of beginning and ending epoch date
2841 arrayref of beginning and ending epoch date
2845 arrayref of beginning and ending epoch date
2849 arrayref of beginning and ending epoch date
2853 arrayref of beginning and ending epoch date
2857 arrayref of beginning and ending epoch date
2861 pkgnum or APKG_pkgnum
2865 a value suited to passing to FS::UI::Web::cust_header
2869 specifies the user for agent virtualization
2873 boolean selects packages containing fcc form 477 telco lines
2880 my ($class, $params) = @_;
2887 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
2889 "cust_main.agentnum = $1";
2896 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
2898 "cust_pkg.custnum = $1";
2905 if ( $params->{'pkgbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
2907 "cust_pkg.pkgbatch = '$1'";
2914 if ( $params->{'magic'} eq 'active'
2915 || $params->{'status'} eq 'active' ) {
2917 push @where, FS::cust_pkg->active_sql();
2919 } elsif ( $params->{'magic'} =~ /^not[ _]yet[ _]billed$/
2920 || $params->{'status'} =~ /^not[ _]yet[ _]billed$/ ) {
2922 push @where, FS::cust_pkg->not_yet_billed_sql();
2924 } elsif ( $params->{'magic'} =~ /^(one-time charge|inactive)/
2925 || $params->{'status'} =~ /^(one-time charge|inactive)/ ) {
2927 push @where, FS::cust_pkg->inactive_sql();
2929 } elsif ( $params->{'magic'} eq 'suspended'
2930 || $params->{'status'} eq 'suspended' ) {
2932 push @where, FS::cust_pkg->suspended_sql();
2934 } elsif ( $params->{'magic'} =~ /^cancell?ed$/
2935 || $params->{'status'} =~ /^cancell?ed$/ ) {
2937 push @where, FS::cust_pkg->cancelled_sql();
2942 # parse package class
2945 #false lazinessish w/graph/cust_bill_pkg.cgi
2948 if ( exists($params->{'classnum'})
2949 && $params->{'classnum'} =~ /^(\d*)$/
2953 if ( $classnum ) { #a specific class
2954 push @where, "part_pkg.classnum = $classnum";
2956 #@pkg_class = ( qsearchs('pkg_class', { 'classnum' => $classnum } ) );
2957 #die "classnum $classnum not found!" unless $pkg_class[0];
2958 #$title .= $pkg_class[0]->classname.' ';
2960 } elsif ( $classnum eq '' ) { #the empty class
2962 push @where, "part_pkg.classnum IS NULL";
2963 #$title .= 'Empty class ';
2964 #@pkg_class = ( '(empty class)' );
2965 } elsif ( $classnum eq '0' ) {
2966 #@pkg_class = qsearch('pkg_class', {} ); # { 'disabled' => '' } );
2967 #push @pkg_class, '(empty class)';
2969 die "illegal classnum";
2975 # parse package report options
2978 my @report_option = ();
2979 if ( exists($params->{'report_option'})
2980 && $params->{'report_option'} =~ /^([,\d]*)$/
2983 @report_option = split(',', $1);
2986 if (@report_option) {
2987 # this will result in the empty set for the dangling comma case as it should
2989 map{ "0 < ( SELECT count(*) FROM part_pkg_option
2990 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
2991 AND optionname = 'report_option_$_'
2992 AND optionvalue = '1' )"
3002 push @where, "part_pkg.custom = 'Y'" if $params->{custom};
3008 push @where, "part_pkg.fcc_ds0s > 0" if $params->{fcc_line};
3014 if ( exists($params->{'censustract'}) ) {
3015 $params->{'censustract'} =~ /^([.\d]*)$/;
3016 my $censustract = "cust_main.censustract = '$1'";
3017 $censustract .= ' OR cust_main.censustract is NULL' unless $1;
3018 push @where, "( $censustract )";
3025 if ( ref($params->{'pkgpart'}) ) {
3028 if ( ref($params->{'pkgpart'}) eq 'HASH' ) {
3029 @pkgpart = grep $params->{'pkgpart'}{$_}, keys %{ $params->{'pkgpart'} };
3030 } elsif ( ref($params->{'pkgpart'}) eq 'ARRAY' ) {
3031 @pkgpart = @{ $params->{'pkgpart'} };
3033 die 'unhandled pkgpart ref '. $params->{'pkgpart'};
3036 @pkgpart = grep /^(\d+)$/, @pkgpart;
3038 push @where, 'pkgpart IN ('. join(',', @pkgpart). ')' if scalar(@pkgpart);
3040 } elsif ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
3041 push @where, "pkgpart = $1";
3050 #false laziness w/report_cust_pkg.html
3053 'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
3054 'active' => { 'susp'=>1, 'cancel'=>1 },
3055 'suspended' => { 'cancel' => 1 },
3060 if( exists($params->{'active'} ) ) {
3061 # This overrides all the other date-related fields
3062 my($beginning, $ending) = @{$params->{'active'}};
3064 "cust_pkg.setup IS NOT NULL",
3065 "cust_pkg.setup <= $ending",
3066 "(cust_pkg.cancel IS NULL OR cust_pkg.cancel >= $beginning )",
3067 "NOT (".FS::cust_pkg->onetime_sql . ")";
3070 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end cancel )) {
3072 next unless exists($params->{$field});
3074 my($beginning, $ending) = @{$params->{$field}};
3076 next if $beginning == 0 && $ending == 4294967295;
3079 "cust_pkg.$field IS NOT NULL",
3080 "cust_pkg.$field >= $beginning",
3081 "cust_pkg.$field <= $ending";
3083 $orderby ||= "ORDER BY cust_pkg.$field";
3088 $orderby ||= 'ORDER BY bill';
3091 # parse magic, legacy, etc.
3094 if ( $params->{'magic'} &&
3095 $params->{'magic'} =~ /^(active|inactive|suspended|cancell?ed)$/
3098 $orderby = 'ORDER BY pkgnum';
3100 if ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
3101 push @where, "pkgpart = $1";
3104 } elsif ( $params->{'query'} eq 'pkgnum' ) {
3106 $orderby = 'ORDER BY pkgnum';
3108 } elsif ( $params->{'query'} eq 'APKG_pkgnum' ) {
3110 $orderby = 'ORDER BY pkgnum';
3113 SELECT count(*) FROM pkg_svc
3114 WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
3115 AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
3116 WHERE cust_svc.pkgnum = cust_pkg.pkgnum
3117 AND cust_svc.svcpart = pkg_svc.svcpart
3124 # setup queries, links, subs, etc. for the search
3127 # here is the agent virtualization
3128 if ($params->{CurrentUser}) {
3130 qsearchs('access_user', { username => $params->{CurrentUser} });
3133 push @where, $access_user->agentnums_sql('table'=>'cust_main');
3138 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql('table'=>'cust_main');
3141 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
3143 my $addl_from = 'LEFT JOIN cust_main USING ( custnum ) '.
3144 'LEFT JOIN part_pkg USING ( pkgpart ) '.
3145 'LEFT JOIN pkg_class ON ( part_pkg.classnum = pkg_class.classnum ) ';
3147 my $count_query = "SELECT COUNT(*) FROM cust_pkg $addl_from $extra_sql";
3150 'table' => 'cust_pkg',
3152 'select' => join(', ',
3154 ( map "part_pkg.$_", qw( pkg freq ) ),
3155 'pkg_class.classname',
3156 'cust_main.custnum AS cust_main_custnum',
3157 FS::UI::Web::cust_sql_fields(
3158 $params->{'cust_fields'}
3161 'extra_sql' => "$extra_sql $orderby",
3162 'addl_from' => $addl_from,
3163 'count_query' => $count_query,
3170 Returns a list of two package counts. The first is a count of packages
3171 based on the supplied criteria and the second is the count of residential
3172 packages with those same criteria. Criteria are specified as in the search
3178 my ($class, $params) = @_;
3180 my $sql_query = $class->search( $params );
3182 my $count_sql = delete($sql_query->{'count_query'});
3183 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
3184 or die "couldn't parse count_sql";
3186 my $count_sth = dbh->prepare($count_sql)
3187 or die "Error preparing $count_sql: ". dbh->errstr;
3189 or die "Error executing $count_sql: ". $count_sth->errstr;
3190 my $count_arrayref = $count_sth->fetchrow_arrayref;
3192 return ( @$count_arrayref );
3199 Returns a list: the first item is an SQL fragment identifying matching
3200 packages/customers via location (taking into account shipping and package
3201 address taxation, if enabled), and subsequent items are the parameters to
3202 substitute for the placeholders in that fragment.
3207 my($class, %opt) = @_;
3208 my $ornull = $opt{'ornull'};
3210 my $conf = new FS::Conf;
3212 # '?' placeholders in _location_sql_where
3213 my $x = $ornull ? 3 : 2;
3214 my @bill_param = ( ('city')x3, ('county')x$x, ('state')x$x, 'country' );
3218 if ( $conf->exists('tax-ship_address') ) {
3221 ( ( ship_last IS NULL OR ship_last = '' )
3222 AND ". _location_sql_where('cust_main', '', $ornull ). "
3224 OR ( ship_last IS NOT NULL AND ship_last != ''
3225 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
3228 # AND payby != 'COMP'
3230 @main_param = ( @bill_param, @bill_param );
3234 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
3235 @main_param = @bill_param;
3241 if ( $conf->exists('tax-pkg_address') ) {
3243 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
3246 ( cust_pkg.locationnum IS NULL AND $main_where )
3247 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
3250 @param = ( @main_param, @bill_param );
3254 $where = $main_where;
3255 @param = @main_param;
3263 #subroutine, helper for location_sql
3264 sub _location_sql_where {
3266 my $prefix = @_ ? shift : '';
3267 my $ornull = @_ ? shift : '';
3269 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
3271 $ornull = $ornull ? ' OR ? IS NULL ' : '';
3273 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL ) ";
3274 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL ) ";
3275 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL ) ";
3277 # ( $table.${prefix}city = ? $or_empty_city $ornull )
3279 ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS text) IS NULL )
3280 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
3281 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
3282 AND $table.${prefix}country = ?
3287 my( $self, $what ) = @_;
3289 my $what_show_zero = $what. '_show_zero';
3290 length($self->$what_show_zero())
3291 ? ($self->$what_show_zero() eq 'Y')
3292 : $self->part_pkg->$what_show_zero();
3299 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
3301 CUSTNUM is a customer (see L<FS::cust_main>)
3303 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
3304 L<FS::part_pkg>) to order for this customer. Duplicates are of course
3307 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
3308 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
3309 new billing items. An error is returned if this is not possible (see
3310 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
3313 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
3314 newly-created cust_pkg objects.
3316 REFNUM, if specified, will specify the FS::pkg_referral record to be created
3317 and inserted. Multiple FS::pkg_referral records can be created by
3318 setting I<refnum> to an array reference of refnums or a hash reference with
3319 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
3320 record will be created corresponding to cust_main.refnum.
3325 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
3327 my $conf = new FS::Conf;
3329 # Transactionize this whole mess
3330 local $SIG{HUP} = 'IGNORE';
3331 local $SIG{INT} = 'IGNORE';
3332 local $SIG{QUIT} = 'IGNORE';
3333 local $SIG{TERM} = 'IGNORE';
3334 local $SIG{TSTP} = 'IGNORE';
3335 local $SIG{PIPE} = 'IGNORE';
3337 my $oldAutoCommit = $FS::UID::AutoCommit;
3338 local $FS::UID::AutoCommit = 0;
3342 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
3343 # return "Customer not found: $custnum" unless $cust_main;
3345 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
3348 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
3351 my $change = scalar(@old_cust_pkg) != 0;
3354 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
3356 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
3357 " to pkgpart ". $pkgparts->[0]. "\n"
3360 my $err_or_cust_pkg =
3361 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
3362 'refnum' => $refnum,
3365 unless (ref($err_or_cust_pkg)) {
3366 $dbh->rollback if $oldAutoCommit;
3367 return $err_or_cust_pkg;
3370 push @$return_cust_pkg, $err_or_cust_pkg;
3371 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3376 # Create the new packages.
3377 foreach my $pkgpart (@$pkgparts) {
3379 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
3381 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
3382 pkgpart => $pkgpart,
3386 $error = $cust_pkg->insert( 'change' => $change );
3388 $dbh->rollback if $oldAutoCommit;
3391 push @$return_cust_pkg, $cust_pkg;
3393 # $return_cust_pkg now contains refs to all of the newly
3396 # Transfer services and cancel old packages.
3397 foreach my $old_pkg (@old_cust_pkg) {
3399 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
3402 foreach my $new_pkg (@$return_cust_pkg) {
3403 $error = $old_pkg->transfer($new_pkg);
3404 if ($error and $error == 0) {
3405 # $old_pkg->transfer failed.
3406 $dbh->rollback if $oldAutoCommit;
3411 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
3412 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
3413 foreach my $new_pkg (@$return_cust_pkg) {
3414 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
3415 if ($error and $error == 0) {
3416 # $old_pkg->transfer failed.
3417 $dbh->rollback if $oldAutoCommit;
3424 # Transfers were successful, but we went through all of the
3425 # new packages and still had services left on the old package.
3426 # We can't cancel the package under the circumstances, so abort.
3427 $dbh->rollback if $oldAutoCommit;
3428 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
3430 $error = $old_pkg->cancel( quiet=>1 );
3436 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3440 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
3442 A bulk change method to change packages for multiple customers.
3444 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
3445 L<FS::part_pkg>) to order for each customer. Duplicates are of course
3448 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
3449 replace. The services (see L<FS::cust_svc>) are moved to the
3450 new billing items. An error is returned if this is not possible (see
3453 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
3454 newly-created cust_pkg objects.
3459 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
3461 # Transactionize this whole mess
3462 local $SIG{HUP} = 'IGNORE';
3463 local $SIG{INT} = 'IGNORE';
3464 local $SIG{QUIT} = 'IGNORE';
3465 local $SIG{TERM} = 'IGNORE';
3466 local $SIG{TSTP} = 'IGNORE';
3467 local $SIG{PIPE} = 'IGNORE';
3469 my $oldAutoCommit = $FS::UID::AutoCommit;
3470 local $FS::UID::AutoCommit = 0;
3474 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
3477 while(scalar(@old_cust_pkg)) {
3479 my $custnum = $old_cust_pkg[0]->custnum;
3480 my (@remove) = map { $_->pkgnum }
3481 grep { $_->custnum == $custnum } @old_cust_pkg;
3482 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
3484 my $error = order $custnum, $pkgparts, \@remove, \@return;
3486 push @errors, $error
3488 push @$return_cust_pkg, @return;
3491 if (scalar(@errors)) {
3492 $dbh->rollback if $oldAutoCommit;
3493 return join(' / ', @errors);
3496 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3500 # Used by FS::Upgrade to migrate to a new database.
3501 sub _upgrade_data { # class method
3502 my ($class, %opts) = @_;
3503 $class->_upgrade_otaker(%opts);
3505 # RT#10139, bug resulting in contract_end being set when it shouldn't
3506 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
3507 # RT#10830, bad calculation of prorate date near end of year
3508 # the date range for bill is December 2009, and we move it forward
3509 # one year if it's before the previous bill date (which it should
3511 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
3512 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
3513 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
3514 # RT6628, add order_date to cust_pkg
3515 'update cust_pkg set order_date = (select history_date from h_cust_pkg
3516 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
3517 history_action = \'insert\') where order_date is null',
3519 foreach my $sql (@statements) {
3520 my $sth = dbh->prepare($sql);
3521 $sth->execute or die $sth->errstr;
3529 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
3531 In sub order, the @pkgparts array (passed by reference) is clobbered.
3533 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
3534 method to pass dates to the recur_prog expression, it should do so.
3536 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
3537 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
3538 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
3539 configuration values. Probably need a subroutine which decides what to do
3540 based on whether or not we've fetched the user yet, rather than a hash. See
3541 FS::UID and the TODO.
3543 Now that things are transactional should the check in the insert method be
3548 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
3549 L<FS::pkg_svc>, schema.html from the base documentation