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;
34 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
36 # because they load configuration by setting FS::UID::callback (see TODO)
42 # for sending cancel emails in sub cancel
46 $me = '[FS::cust_pkg]';
48 $disable_agentcheck = 0;
52 my ( $hashref, $cache ) = @_;
53 #if ( $hashref->{'pkgpart'} ) {
54 if ( $hashref->{'pkg'} ) {
55 # #@{ $self->{'_pkgnum'} } = ();
56 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
57 # $self->{'_pkgpart'} = $subcache;
58 # #push @{ $self->{'_pkgnum'} },
59 # FS::part_pkg->new_or_cached($hashref, $subcache);
60 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
62 if ( exists $hashref->{'svcnum'} ) {
63 #@{ $self->{'_pkgnum'} } = ();
64 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
65 $self->{'_svcnum'} = $subcache;
66 #push @{ $self->{'_pkgnum'} },
67 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
73 FS::cust_pkg - Object methods for cust_pkg objects
79 $record = new FS::cust_pkg \%hash;
80 $record = new FS::cust_pkg { 'column' => 'value' };
82 $error = $record->insert;
84 $error = $new_record->replace($old_record);
86 $error = $record->delete;
88 $error = $record->check;
90 $error = $record->cancel;
92 $error = $record->suspend;
94 $error = $record->unsuspend;
96 $part_pkg = $record->part_pkg;
98 @labels = $record->labels;
100 $seconds = $record->seconds_since($timestamp);
102 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
103 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
107 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
108 inherits from FS::Record. The following fields are currently supported:
114 Primary key (assigned automatically for new billing items)
118 Customer (see L<FS::cust_main>)
122 Billing item definition (see L<FS::part_pkg>)
126 Optional link to package location (see L<FS::location>)
130 date package was ordered (also remains same on changes)
142 date (next bill date)
170 order taker (see L<FS::access_user>)
174 If this field is set to 1, disables the automatic
175 unsuspension of this package when using the B<unsuspendauto> config option.
179 If not set, defaults to 1
183 Date of change from previous package
193 =item change_locationnum
199 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
200 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
201 L<Time::Local> and L<Date::Parse> for conversion functions.
209 Create a new billing item. To add the item to the database, see L<"insert">.
213 sub table { 'cust_pkg'; }
214 sub cust_linked { $_[0]->cust_main_custnum; }
215 sub cust_unlinked_msg {
217 "WARNING: can't find cust_main.custnum ". $self->custnum.
218 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
221 =item insert [ OPTION => VALUE ... ]
223 Adds this billing item to the database ("Orders" the item). If there is an
224 error, returns the error, otherwise returns false.
226 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
227 will be used to look up the package definition and agent restrictions will be
230 If the additional field I<refnum> is defined, an FS::pkg_referral record will
231 be created and inserted. Multiple FS::pkg_referral records can be created by
232 setting I<refnum> to an array reference of refnums or a hash reference with
233 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
234 record will be created corresponding to cust_main.refnum.
236 The following options are available:
242 If set true, supresses any referral credit to a referring customer.
246 cust_pkg_option records will be created
250 a ticket will be added to this customer with this subject
254 an optional queue name for ticket additions
261 my( $self, %options ) = @_;
263 my $error = $self->check_pkgpart;
264 return $error if $error;
266 if ( $self->part_pkg->option('start_1st', 1) && !$self->start_date ) {
267 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
268 $mon += 1 unless $mday == 1;
269 until ( $mon < 12 ) { $mon -= 12; $year++; }
270 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
273 foreach my $action ( qw(expire adjourn contract_end) ) {
274 my $months = $self->part_pkg->option("${action}_months",1);
275 if($months and !$self->$action) {
276 my $start = $self->start_date || $self->setup || time;
277 $self->$action( $self->part_pkg->add_freq($start, $months) );
281 $self->order_date(time);
283 local $SIG{HUP} = 'IGNORE';
284 local $SIG{INT} = 'IGNORE';
285 local $SIG{QUIT} = 'IGNORE';
286 local $SIG{TERM} = 'IGNORE';
287 local $SIG{TSTP} = 'IGNORE';
288 local $SIG{PIPE} = 'IGNORE';
290 my $oldAutoCommit = $FS::UID::AutoCommit;
291 local $FS::UID::AutoCommit = 0;
294 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
296 $dbh->rollback if $oldAutoCommit;
300 $self->refnum($self->cust_main->refnum) unless $self->refnum;
301 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
302 $self->process_m2m( 'link_table' => 'pkg_referral',
303 'target_table' => 'part_referral',
304 'params' => $self->refnum,
307 if ( $self->discountnum ) {
308 my $error = $self->insert_discount();
310 $dbh->rollback if $oldAutoCommit;
315 #if ( $self->reg_code ) {
316 # my $reg_code = qsearchs('reg_code', { 'code' => $self->reg_code } );
317 # $error = $reg_code->delete;
319 # $dbh->rollback if $oldAutoCommit;
324 my $conf = new FS::Conf;
326 if ( $conf->config('ticket_system') && $options{ticket_subject} ) {
329 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
336 use FS::TicketSystem;
337 FS::TicketSystem->init();
339 my $q = new RT::Queue($RT::SystemUser);
340 $q->Load($options{ticket_queue}) if $options{ticket_queue};
341 my $t = new RT::Ticket($RT::SystemUser);
342 my $mime = new MIME::Entity;
343 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
344 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
345 Subject => $options{ticket_subject},
348 $t->AddLink( Type => 'MemberOf',
349 Target => 'freeside://freeside/cust_main/'. $self->custnum,
353 if ($conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
354 my $queue = new FS::queue {
355 'job' => 'FS::cust_main::queueable_print',
357 $error = $queue->insert(
358 'custnum' => $self->custnum,
359 'template' => 'welcome_letter',
363 warn "can't send welcome letter: $error";
368 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
375 This method now works but you probably shouldn't use it.
377 You don't want to delete packages, because there would then be no record
378 the customer ever purchased the package. Instead, see the cancel method and
379 hide cancelled packages.
386 local $SIG{HUP} = 'IGNORE';
387 local $SIG{INT} = 'IGNORE';
388 local $SIG{QUIT} = 'IGNORE';
389 local $SIG{TERM} = 'IGNORE';
390 local $SIG{TSTP} = 'IGNORE';
391 local $SIG{PIPE} = 'IGNORE';
393 my $oldAutoCommit = $FS::UID::AutoCommit;
394 local $FS::UID::AutoCommit = 0;
397 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
398 my $error = $cust_pkg_discount->delete;
400 $dbh->rollback if $oldAutoCommit;
404 #cust_bill_pkg_discount?
406 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
407 my $error = $cust_pkg_detail->delete;
409 $dbh->rollback if $oldAutoCommit;
414 foreach my $cust_pkg_reason (
416 'table' => 'cust_pkg_reason',
417 'hashref' => { 'pkgnum' => $self->pkgnum },
421 my $error = $cust_pkg_reason->delete;
423 $dbh->rollback if $oldAutoCommit;
430 my $error = $self->SUPER::delete(@_);
432 $dbh->rollback if $oldAutoCommit;
436 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
442 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
444 Replaces the OLD_RECORD with this one in the database. If there is an error,
445 returns the error, otherwise returns false.
447 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
449 Changing pkgpart may have disasterous effects. See the order subroutine.
451 setup and bill are normally updated by calling the bill method of a customer
452 object (see L<FS::cust_main>).
454 suspend is normally updated by the suspend and unsuspend methods.
456 cancel is normally updated by the cancel method (and also the order subroutine
459 Available options are:
465 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.
469 the access_user (see L<FS::access_user>) providing the reason
473 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
482 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
487 ( ref($_[0]) eq 'HASH' )
491 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
492 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
495 #return "Can't change setup once it exists!"
496 # if $old->getfield('setup') &&
497 # $old->getfield('setup') != $new->getfield('setup');
499 #some logic for bill, susp, cancel?
501 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
503 local $SIG{HUP} = 'IGNORE';
504 local $SIG{INT} = 'IGNORE';
505 local $SIG{QUIT} = 'IGNORE';
506 local $SIG{TERM} = 'IGNORE';
507 local $SIG{TSTP} = 'IGNORE';
508 local $SIG{PIPE} = 'IGNORE';
510 my $oldAutoCommit = $FS::UID::AutoCommit;
511 local $FS::UID::AutoCommit = 0;
514 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
515 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
516 my $error = $new->insert_reason(
517 'reason' => $options->{'reason'},
518 'date' => $new->$method,
520 'reason_otaker' => $options->{'reason_otaker'},
523 dbh->rollback if $oldAutoCommit;
524 return "Error inserting cust_pkg_reason: $error";
529 #save off and freeze RADIUS attributes for any associated svc_acct records
531 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
533 #also check for specific exports?
534 # to avoid spurious modify export events
535 @svc_acct = map { $_->svc_x }
536 grep { $_->part_svc->svcdb eq 'svc_acct' }
539 $_->snapshot foreach @svc_acct;
543 my $error = $new->SUPER::replace($old,
544 $options->{options} ? $options->{options} : ()
547 $dbh->rollback if $oldAutoCommit;
551 #for prepaid packages,
552 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
553 foreach my $old_svc_acct ( @svc_acct ) {
554 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
556 $new_svc_acct->replace( $old_svc_acct,
557 'depend_jobnum' => $options->{depend_jobnum},
560 $dbh->rollback if $oldAutoCommit;
565 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
572 Checks all fields to make sure this is a valid billing item. If there is an
573 error, returns the error, otherwise returns false. Called by the insert and
581 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
584 $self->ut_numbern('pkgnum')
585 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
586 || $self->ut_numbern('pkgpart')
587 || $self->check_pkgpart
588 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
589 || $self->ut_numbern('start_date')
590 || $self->ut_numbern('setup')
591 || $self->ut_numbern('bill')
592 || $self->ut_numbern('susp')
593 || $self->ut_numbern('cancel')
594 || $self->ut_numbern('adjourn')
595 || $self->ut_numbern('expire')
596 || $self->ut_enum('no_auto', [ '', 'Y' ])
598 return $error if $error;
600 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
602 if ( $self->dbdef_table->column('manual_flag') ) {
603 $self->manual_flag('') if $self->manual_flag eq ' ';
604 $self->manual_flag =~ /^([01]?)$/
605 or return "Illegal manual_flag ". $self->manual_flag;
606 $self->manual_flag($1);
619 my $error = $self->ut_numbern('pkgpart');
620 return $error if $error;
622 if ( $self->reg_code ) {
624 unless ( grep { $self->pkgpart == $_->pkgpart }
625 map { $_->reg_code_pkg }
626 qsearchs( 'reg_code', { 'code' => $self->reg_code,
627 'agentnum' => $self->cust_main->agentnum })
629 return "Unknown registration code";
632 } elsif ( $self->promo_code ) {
635 qsearchs('part_pkg', {
636 'pkgpart' => $self->pkgpart,
637 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
639 return 'Unknown promotional code' unless $promo_part_pkg;
643 unless ( $disable_agentcheck ) {
645 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
646 return "agent ". $agent->agentnum. ':'. $agent->agent.
647 " can't purchase pkgpart ". $self->pkgpart
648 unless $agent->pkgpart_hashref->{ $self->pkgpart }
649 || $agent->agentnum == $self->part_pkg->agentnum;
652 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
653 return $error if $error;
661 =item cancel [ OPTION => VALUE ... ]
663 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
664 in this package, then cancels the package itself (sets the cancel field to
667 Available options are:
671 =item quiet - can be set true to supress email cancellation notices.
673 =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.
675 =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.
677 =item date - can be set to a unix style timestamp to specify when to cancel (expire)
679 =item nobill - can be set true to skip billing if it might otherwise be done.
681 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
682 not credit it. This must be set (by change()) when changing the package
683 to a different pkgpart or location, and probably shouldn't be in any other
684 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
689 If there is an error, returns the error, otherwise returns false.
694 my( $self, %options ) = @_;
697 my $conf = new FS::Conf;
699 warn "cust_pkg::cancel called with options".
700 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
703 local $SIG{HUP} = 'IGNORE';
704 local $SIG{INT} = 'IGNORE';
705 local $SIG{QUIT} = 'IGNORE';
706 local $SIG{TERM} = 'IGNORE';
707 local $SIG{TSTP} = 'IGNORE';
708 local $SIG{PIPE} = 'IGNORE';
710 my $oldAutoCommit = $FS::UID::AutoCommit;
711 local $FS::UID::AutoCommit = 0;
714 my $old = $self->select_for_update;
716 if ( $old->get('cancel') || $self->get('cancel') ) {
717 dbh->rollback if $oldAutoCommit;
718 return ""; # no error
721 my $date = $options{date} if $options{date}; # expire/cancel later
722 $date = '' if ($date && $date <= time); # complain instead?
724 #race condition: usage could be ongoing until unprovisioned
725 #resolved by performing a change package instead (which unprovisions) and
727 if ( !$options{nobill} && !$date && $conf->exists('bill_usage_on_cancel') ) {
728 my $copy = $self->new({$self->hash});
730 $copy->cust_main->bill( pkg_list => [ $copy ], cancel => 1 );
731 warn "Error billing during cancel, custnum ".
732 #$self->cust_main->custnum. ": $error"
737 my $cancel_time = $options{'time'} || time;
739 if ( $options{'reason'} ) {
740 $error = $self->insert_reason( 'reason' => $options{'reason'},
741 'action' => $date ? 'expire' : 'cancel',
742 'date' => $date ? $date : $cancel_time,
743 'reason_otaker' => $options{'reason_otaker'},
746 dbh->rollback if $oldAutoCommit;
747 return "Error inserting cust_pkg_reason: $error";
751 my %svc_cancel_opt = ();
752 $svc_cancel_opt{'date'} = $date if $date;
753 foreach my $cust_svc (
756 sort { $a->[1] <=> $b->[1] }
757 map { [ $_, $_->svc_x->table_info->{'cancel_weight'} ]; }
758 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
760 my $error = $cust_svc->cancel( %svc_cancel_opt );
763 $dbh->rollback if $oldAutoCommit;
764 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
771 # Add a credit for remaining service
772 my $last_bill = $self->getfield('last_bill') || 0;
773 my $next_bill = $self->getfield('bill') || 0;
775 if ( exists($options{'unused_credit'}) ) {
776 $do_credit = $options{'unused_credit'};
779 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
782 and $last_bill > 0 # the package has been billed
783 and $next_bill > 0 # the package has a next bill date
784 and $next_bill >= $cancel_time # which is in the future
786 my $remaining_value = $self->calc_remain('time' => $cancel_time);
787 if ( $remaining_value > 0 ) {
788 my $error = $self->cust_main->credit(
790 'Credit for unused time on '. $self->part_pkg->pkg,
791 'reason_type' => $conf->config('cancel_credit_type'),
794 $dbh->rollback if $oldAutoCommit;
795 return "Error crediting customer \$$remaining_value for unused time".
796 " on ". $self->part_pkg->pkg. ": $error";
798 } #if $remaining_value
803 my %hash = $self->hash;
804 $date ? ($hash{'expire'} = $date) : ($hash{'cancel'} = $cancel_time);
805 my $new = new FS::cust_pkg ( \%hash );
806 $error = $new->replace( $self, options => { $self->options } );
808 $dbh->rollback if $oldAutoCommit;
812 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
813 return '' if $date; #no errors
815 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
816 if ( !$options{'quiet'} &&
817 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
819 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
822 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
823 $error = $msg_template->send( 'cust_main' => $self->cust_main,
828 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
829 'to' => \@invoicing_list,
830 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
831 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
834 #should this do something on errors?
841 =item cancel_if_expired [ NOW_TIMESTAMP ]
843 Cancels this package if its expire date has been reached.
847 sub cancel_if_expired {
849 my $time = shift || time;
850 return '' unless $self->expire && $self->expire <= $time;
851 my $error = $self->cancel;
853 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
854 $self->custnum. ": $error";
861 Cancels any pending expiration (sets the expire field to null).
863 If there is an error, returns the error, otherwise returns false.
868 my( $self, %options ) = @_;
871 local $SIG{HUP} = 'IGNORE';
872 local $SIG{INT} = 'IGNORE';
873 local $SIG{QUIT} = 'IGNORE';
874 local $SIG{TERM} = 'IGNORE';
875 local $SIG{TSTP} = 'IGNORE';
876 local $SIG{PIPE} = 'IGNORE';
878 my $oldAutoCommit = $FS::UID::AutoCommit;
879 local $FS::UID::AutoCommit = 0;
882 my $old = $self->select_for_update;
884 my $pkgnum = $old->pkgnum;
885 if ( $old->get('cancel') || $self->get('cancel') ) {
886 dbh->rollback if $oldAutoCommit;
887 return "Can't unexpire cancelled package $pkgnum";
888 # or at least it's pointless
891 unless ( $old->get('expire') && $self->get('expire') ) {
892 dbh->rollback if $oldAutoCommit;
893 return ""; # no error
896 my %hash = $self->hash;
897 $hash{'expire'} = '';
898 my $new = new FS::cust_pkg ( \%hash );
899 $error = $new->replace( $self, options => { $self->options } );
901 $dbh->rollback if $oldAutoCommit;
905 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
911 =item suspend [ OPTION => VALUE ... ]
913 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
914 package, then suspends the package itself (sets the susp field to now).
916 Available options are:
920 =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.
922 =item date - can be set to a unix style timestamp to specify when to suspend (adjourn)
926 If there is an error, returns the error, otherwise returns false.
931 my( $self, %options ) = @_;
934 local $SIG{HUP} = 'IGNORE';
935 local $SIG{INT} = 'IGNORE';
936 local $SIG{QUIT} = 'IGNORE';
937 local $SIG{TERM} = 'IGNORE';
938 local $SIG{TSTP} = 'IGNORE';
939 local $SIG{PIPE} = 'IGNORE';
941 my $oldAutoCommit = $FS::UID::AutoCommit;
942 local $FS::UID::AutoCommit = 0;
945 my $old = $self->select_for_update;
947 my $pkgnum = $old->pkgnum;
948 if ( $old->get('cancel') || $self->get('cancel') ) {
949 dbh->rollback if $oldAutoCommit;
950 return "Can't suspend cancelled package $pkgnum";
953 if ( $old->get('susp') || $self->get('susp') ) {
954 dbh->rollback if $oldAutoCommit;
955 return ""; # no error # complain on adjourn?
958 my $date = $options{date} if $options{date}; # adjourn/suspend later
959 $date = '' if ($date && $date <= time); # complain instead?
961 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
962 dbh->rollback if $oldAutoCommit;
963 return "Package $pkgnum expires before it would be suspended.";
966 my $suspend_time = $options{'time'} || time;
968 if ( $options{'reason'} ) {
969 $error = $self->insert_reason( 'reason' => $options{'reason'},
970 'action' => $date ? 'adjourn' : 'suspend',
971 'date' => $date ? $date : $suspend_time,
972 'reason_otaker' => $options{'reason_otaker'},
975 dbh->rollback if $oldAutoCommit;
976 return "Error inserting cust_pkg_reason: $error";
984 foreach my $cust_svc (
985 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
987 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
989 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
990 $dbh->rollback if $oldAutoCommit;
991 return "Illegal svcdb value in part_svc!";
994 require "FS/$svcdb.pm";
996 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
998 $error = $svc->suspend;
1000 $dbh->rollback if $oldAutoCommit;
1003 my( $label, $value ) = $cust_svc->label;
1004 push @labels, "$label: $value";
1008 my $conf = new FS::Conf;
1009 if ( $conf->config('suspend_email_admin') ) {
1011 my $error = send_email(
1012 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1013 #invoice_from ??? well as good as any
1014 'to' => $conf->config('suspend_email_admin'),
1015 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1017 "This is an automatic message from your Freeside installation\n",
1018 "informing you that the following customer package has been suspended:\n",
1020 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1021 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1022 ( map { "Service : $_\n" } @labels ),
1027 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1035 my %hash = $self->hash;
1037 $hash{'adjourn'} = $date;
1039 $hash{'susp'} = $suspend_time;
1041 my $new = new FS::cust_pkg ( \%hash );
1042 $error = $new->replace( $self, options => { $self->options } );
1044 $dbh->rollback if $oldAutoCommit;
1048 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1053 =item unsuspend [ OPTION => VALUE ... ]
1055 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1056 package, then unsuspends the package itself (clears the susp field and the
1057 adjourn field if it is in the past).
1059 Available options are:
1063 =item adjust_next_bill
1065 Can be set true to adjust the next bill date forward by
1066 the amount of time the account was inactive. This was set true by default
1067 since 1.4.2 and 1.5.0pre6; however, starting with 1.7.0 this needs to be
1068 explicitly requested. Price plans for which this makes sense (anniversary-date
1069 based than prorate or subscription) could have an option to enable this
1074 If there is an error, returns the error, otherwise returns false.
1079 my( $self, %opt ) = @_;
1082 local $SIG{HUP} = 'IGNORE';
1083 local $SIG{INT} = 'IGNORE';
1084 local $SIG{QUIT} = 'IGNORE';
1085 local $SIG{TERM} = 'IGNORE';
1086 local $SIG{TSTP} = 'IGNORE';
1087 local $SIG{PIPE} = 'IGNORE';
1089 my $oldAutoCommit = $FS::UID::AutoCommit;
1090 local $FS::UID::AutoCommit = 0;
1093 my $old = $self->select_for_update;
1095 my $pkgnum = $old->pkgnum;
1096 if ( $old->get('cancel') || $self->get('cancel') ) {
1097 dbh->rollback if $oldAutoCommit;
1098 return "Can't unsuspend cancelled package $pkgnum";
1101 unless ( $old->get('susp') && $self->get('susp') ) {
1102 dbh->rollback if $oldAutoCommit;
1103 return ""; # no error # complain instead?
1106 foreach my $cust_svc (
1107 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1109 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1111 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1112 $dbh->rollback if $oldAutoCommit;
1113 return "Illegal svcdb value in part_svc!";
1116 require "FS/$svcdb.pm";
1118 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1120 $error = $svc->unsuspend;
1122 $dbh->rollback if $oldAutoCommit;
1129 my %hash = $self->hash;
1130 my $inactive = time - $hash{'susp'};
1132 my $conf = new FS::Conf;
1134 if ( $inactive > 0 &&
1135 ( $hash{'bill'} || $hash{'setup'} ) &&
1136 ( $opt{'adjust_next_bill'} ||
1137 $conf->exists('unsuspend-always_adjust_next_bill_date') ||
1138 $self->part_pkg->option('unsuspend_adjust_bill', 1) )
1141 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
1146 $hash{'adjourn'} = '' if $hash{'adjourn'} < time;
1147 my $new = new FS::cust_pkg ( \%hash );
1148 $error = $new->replace( $self, options => { $self->options } );
1150 $dbh->rollback if $oldAutoCommit;
1154 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1161 Cancels any pending suspension (sets the adjourn field to null).
1163 If there is an error, returns the error, otherwise returns false.
1168 my( $self, %options ) = @_;
1171 local $SIG{HUP} = 'IGNORE';
1172 local $SIG{INT} = 'IGNORE';
1173 local $SIG{QUIT} = 'IGNORE';
1174 local $SIG{TERM} = 'IGNORE';
1175 local $SIG{TSTP} = 'IGNORE';
1176 local $SIG{PIPE} = 'IGNORE';
1178 my $oldAutoCommit = $FS::UID::AutoCommit;
1179 local $FS::UID::AutoCommit = 0;
1182 my $old = $self->select_for_update;
1184 my $pkgnum = $old->pkgnum;
1185 if ( $old->get('cancel') || $self->get('cancel') ) {
1186 dbh->rollback if $oldAutoCommit;
1187 return "Can't unadjourn cancelled package $pkgnum";
1188 # or at least it's pointless
1191 if ( $old->get('susp') || $self->get('susp') ) {
1192 dbh->rollback if $oldAutoCommit;
1193 return "Can't unadjourn suspended package $pkgnum";
1194 # perhaps this is arbitrary
1197 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1198 dbh->rollback if $oldAutoCommit;
1199 return ""; # no error
1202 my %hash = $self->hash;
1203 $hash{'adjourn'} = '';
1204 my $new = new FS::cust_pkg ( \%hash );
1205 $error = $new->replace( $self, options => { $self->options } );
1207 $dbh->rollback if $oldAutoCommit;
1211 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1218 =item change HASHREF | OPTION => VALUE ...
1220 Changes this package: cancels it and creates a new one, with a different
1221 pkgpart or locationnum or both. All services are transferred to the new
1222 package (no change will be made if this is not possible).
1224 Options may be passed as a list of key/value pairs or as a hash reference.
1231 New locationnum, to change the location for this package.
1235 New FS::cust_location object, to create a new location and assign it
1240 New pkgpart (see L<FS::part_pkg>).
1244 New refnum (see L<FS::part_referral>).
1248 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1249 susp, adjourn, cancel, expire, and contract_end) to the new package.
1253 At least one of locationnum, cust_location, pkgpart, refnum must be specified
1254 (otherwise, what's the point?)
1256 Returns either the new FS::cust_pkg object or a scalar error.
1260 my $err_or_new_cust_pkg = $old_cust_pkg->change
1264 #some false laziness w/order
1267 my $opt = ref($_[0]) ? shift : { @_ };
1269 # my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
1272 my $conf = new FS::Conf;
1274 # Transactionize this whole mess
1275 local $SIG{HUP} = 'IGNORE';
1276 local $SIG{INT} = 'IGNORE';
1277 local $SIG{QUIT} = 'IGNORE';
1278 local $SIG{TERM} = 'IGNORE';
1279 local $SIG{TSTP} = 'IGNORE';
1280 local $SIG{PIPE} = 'IGNORE';
1282 my $oldAutoCommit = $FS::UID::AutoCommit;
1283 local $FS::UID::AutoCommit = 0;
1292 #$hash{$_} = $self->$_() foreach qw( last_bill bill );
1294 #$hash{$_} = $self->$_() foreach qw( setup );
1296 $hash{'setup'} = $time if $self->setup;
1298 $hash{'change_date'} = $time;
1299 $hash{"change_$_"} = $self->$_()
1300 foreach qw( pkgnum pkgpart locationnum );
1302 if ( $opt->{'cust_location'} &&
1303 ( ! $opt->{'locationnum'} || $opt->{'locationnum'} == -1 ) ) {
1304 $error = $opt->{'cust_location'}->insert;
1306 $dbh->rollback if $oldAutoCommit;
1307 return "inserting cust_location (transaction rolled back): $error";
1309 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1312 my $unused_credit = 0;
1313 if ( $opt->{'keep_dates'} ) {
1314 foreach my $date ( qw(setup bill last_bill susp adjourn cancel expire
1315 start_date contract_end ) ) {
1316 $hash{$date} = $self->getfield($date);
1319 # Special case. If the pkgpart is changing, and the customer is
1320 # going to be credited for remaining time, don't keep setup, bill,
1321 # or last_bill dates, and DO pass the flag to cancel() to credit
1323 if ( $opt->{'pkgpart'}
1324 and $opt->{'pkgpart'} != $self->pkgpart
1325 and $self->part_pkg->option('unused_credit_change', 1) ) {
1327 $hash{$_} = '' foreach qw(setup bill last_bill);
1330 # Create the new package.
1331 my $cust_pkg = new FS::cust_pkg {
1332 custnum => $self->custnum,
1333 pkgpart => ( $opt->{'pkgpart'} || $self->pkgpart ),
1334 refnum => ( $opt->{'refnum'} || $self->refnum ),
1335 locationnum => ( $opt->{'locationnum'} || $self->locationnum ),
1339 $error = $cust_pkg->insert( 'change' => 1 );
1341 $dbh->rollback if $oldAutoCommit;
1345 # Transfer services and cancel old package.
1347 $error = $self->transfer($cust_pkg);
1348 if ($error and $error == 0) {
1349 # $old_pkg->transfer failed.
1350 $dbh->rollback if $oldAutoCommit;
1354 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
1355 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
1356 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
1357 if ($error and $error == 0) {
1358 # $old_pkg->transfer failed.
1359 $dbh->rollback if $oldAutoCommit;
1365 # Transfers were successful, but we still had services left on the old
1366 # package. We can't change the package under this circumstances, so abort.
1367 $dbh->rollback if $oldAutoCommit;
1368 return "Unable to transfer all services from package ". $self->pkgnum;
1371 #reset usage if changing pkgpart
1372 # AND usage rollover is off (otherwise adds twice, now and at package bill)
1373 if ($self->pkgpart != $cust_pkg->pkgpart) {
1374 my $part_pkg = $cust_pkg->part_pkg;
1375 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
1379 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
1382 $dbh->rollback if $oldAutoCommit;
1383 return "Error setting usage values: $error";
1387 #Good to go, cancel old package. Notify 'cancel' of whether to credit
1389 $error = $self->cancel( quiet=>1, unused_credit => $unused_credit );
1391 $dbh->rollback if $oldAutoCommit;
1395 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
1397 my $error = $cust_pkg->cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
1399 $dbh->rollback if $oldAutoCommit;
1404 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1411 use Storable 'thaw';
1413 sub process_bulk_cust_pkg {
1415 my $param = thaw(decode_base64(shift));
1416 warn Dumper($param) if $DEBUG;
1418 my $old_part_pkg = qsearchs('part_pkg',
1419 { pkgpart => $param->{'old_pkgpart'} });
1420 my $new_part_pkg = qsearchs('part_pkg',
1421 { pkgpart => $param->{'new_pkgpart'} });
1422 die "Must select a new package type\n" unless $new_part_pkg;
1423 #my $keep_dates = $param->{'keep_dates'} || 0;
1424 my $keep_dates = 1; # there is no good reason to turn this off
1426 local $SIG{HUP} = 'IGNORE';
1427 local $SIG{INT} = 'IGNORE';
1428 local $SIG{QUIT} = 'IGNORE';
1429 local $SIG{TERM} = 'IGNORE';
1430 local $SIG{TSTP} = 'IGNORE';
1431 local $SIG{PIPE} = 'IGNORE';
1433 my $oldAutoCommit = $FS::UID::AutoCommit;
1434 local $FS::UID::AutoCommit = 0;
1437 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
1440 foreach my $old_cust_pkg ( @cust_pkgs ) {
1442 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
1443 if ( $old_cust_pkg->getfield('cancel') ) {
1444 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
1445 $old_cust_pkg->pkgnum."\n"
1449 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
1451 my $error = $old_cust_pkg->change(
1452 'pkgpart' => $param->{'new_pkgpart'},
1453 'keep_dates' => $keep_dates
1455 if ( !ref($error) ) { # change returns the cust_pkg on success
1457 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
1460 $dbh->commit if $oldAutoCommit;
1466 Returns the last bill date, or if there is no last bill date, the setup date.
1467 Useful for billing metered services.
1473 return $self->setfield('last_bill', $_[0]) if @_;
1474 return $self->getfield('last_bill') if $self->getfield('last_bill');
1475 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
1476 'edate' => $self->bill, } );
1477 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
1480 =item last_cust_pkg_reason ACTION
1482 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
1483 Returns false if there is no reason or the package is not currenly ACTION'd
1484 ACTION is one of adjourn, susp, cancel, or expire.
1488 sub last_cust_pkg_reason {
1489 my ( $self, $action ) = ( shift, shift );
1490 my $date = $self->get($action);
1492 'table' => 'cust_pkg_reason',
1493 'hashref' => { 'pkgnum' => $self->pkgnum,
1494 'action' => substr(uc($action), 0, 1),
1497 'order_by' => 'ORDER BY num DESC LIMIT 1',
1501 =item last_reason ACTION
1503 Returns the most recent ACTION FS::reason associated with the package.
1504 Returns false if there is no reason or the package is not currenly ACTION'd
1505 ACTION is one of adjourn, susp, cancel, or expire.
1510 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
1511 $cust_pkg_reason->reason
1512 if $cust_pkg_reason;
1517 Returns the definition for this billing item, as an FS::part_pkg object (see
1524 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
1525 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
1526 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
1531 Returns the cancelled package this package was changed from, if any.
1537 return '' unless $self->change_pkgnum;
1538 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
1543 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
1550 $self->part_pkg->calc_setup($self, @_);
1555 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
1562 $self->part_pkg->calc_recur($self, @_);
1567 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
1574 $self->part_pkg->base_recur($self, @_);
1579 Calls the I<calc_remain> of the FS::part_pkg object associated with this
1586 $self->part_pkg->calc_remain($self, @_);
1591 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
1598 $self->part_pkg->calc_cancel($self, @_);
1603 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
1609 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
1612 =item cust_pkg_detail [ DETAILTYPE ]
1614 Returns any customer package details for this package (see
1615 L<FS::cust_pkg_detail>).
1617 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
1621 sub cust_pkg_detail {
1623 my %hash = ( 'pkgnum' => $self->pkgnum );
1624 $hash{detailtype} = shift if @_;
1626 'table' => 'cust_pkg_detail',
1627 'hashref' => \%hash,
1628 'order_by' => 'ORDER BY weight, pkgdetailnum',
1632 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
1634 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
1636 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
1638 If there is an error, returns the error, otherwise returns false.
1642 sub set_cust_pkg_detail {
1643 my( $self, $detailtype, @details ) = @_;
1645 local $SIG{HUP} = 'IGNORE';
1646 local $SIG{INT} = 'IGNORE';
1647 local $SIG{QUIT} = 'IGNORE';
1648 local $SIG{TERM} = 'IGNORE';
1649 local $SIG{TSTP} = 'IGNORE';
1650 local $SIG{PIPE} = 'IGNORE';
1652 my $oldAutoCommit = $FS::UID::AutoCommit;
1653 local $FS::UID::AutoCommit = 0;
1656 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
1657 my $error = $current->delete;
1659 $dbh->rollback if $oldAutoCommit;
1660 return "error removing old detail: $error";
1664 foreach my $detail ( @details ) {
1665 my $cust_pkg_detail = new FS::cust_pkg_detail {
1666 'pkgnum' => $self->pkgnum,
1667 'detailtype' => $detailtype,
1668 'detail' => $detail,
1670 my $error = $cust_pkg_detail->insert;
1672 $dbh->rollback if $oldAutoCommit;
1673 return "error adding new detail: $error";
1678 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1685 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
1689 #false laziness w/cust_bill.pm
1693 'table' => 'cust_event',
1694 'addl_from' => 'JOIN part_event USING ( eventpart )',
1695 'hashref' => { 'tablenum' => $self->pkgnum },
1696 'extra_sql' => " AND eventtable = 'cust_pkg' ",
1700 =item num_cust_event
1702 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
1706 #false laziness w/cust_bill.pm
1707 sub num_cust_event {
1710 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
1711 " WHERE tablenum = ? AND eventtable = 'cust_pkg'";
1712 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
1713 $sth->execute($self->pkgnum) or die $sth->errstr. " executing $sql";
1714 $sth->fetchrow_arrayref->[0];
1717 =item cust_svc [ SVCPART ]
1719 Returns the services for this package, as FS::cust_svc objects (see
1720 L<FS::cust_svc>). If a svcpart is specified, return only the matching
1728 return () unless $self->num_cust_svc(@_);
1731 return qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum,
1732 'svcpart' => shift, } );
1735 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
1737 #if ( $self->{'_svcnum'} ) {
1738 # values %{ $self->{'_svcnum'}->cache };
1740 $self->_sort_cust_svc(
1741 [ qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } ) ]
1747 =item overlimit [ SVCPART ]
1749 Returns the services for this package which have exceeded their
1750 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
1751 is specified, return only the matching services.
1757 return () unless $self->num_cust_svc(@_);
1758 grep { $_->overlimit } $self->cust_svc(@_);
1761 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
1763 Returns historical services for this package created before END TIMESTAMP and
1764 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
1765 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
1766 I<pkg_svc.hidden> flag will be omitted.
1772 warn "$me _h_cust_svc called on $self\n"
1775 my ($end, $start, $mode) = @_;
1776 my @cust_svc = $self->_sort_cust_svc(
1777 [ qsearch( 'h_cust_svc',
1778 { 'pkgnum' => $self->pkgnum, },
1779 FS::h_cust_svc->sql_h_search(@_),
1782 if ( $mode eq 'I' ) {
1783 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
1784 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
1790 sub _sort_cust_svc {
1791 my( $self, $arrayref ) = @_;
1794 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
1799 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $self->pkgpart,
1800 'svcpart' => $_->svcpart } );
1802 $pkg_svc ? $pkg_svc->primary_svc : '',
1803 $pkg_svc ? $pkg_svc->quantity : 0,
1810 =item num_cust_svc [ SVCPART ]
1812 Returns the number of provisioned services for this package. If a svcpart is
1813 specified, counts only the matching services.
1820 return $self->{'_num_cust_svc'}
1822 && exists($self->{'_num_cust_svc'})
1823 && $self->{'_num_cust_svc'} =~ /\d/;
1825 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
1828 my $sql = 'SELECT COUNT(*) FROM cust_svc WHERE pkgnum = ?';
1829 $sql .= ' AND svcpart = ?' if @_;
1831 my $sth = dbh->prepare($sql) or die dbh->errstr;
1832 $sth->execute($self->pkgnum, @_) or die $sth->errstr;
1833 $sth->fetchrow_arrayref->[0];
1836 =item available_part_svc
1838 Returns a list of FS::part_svc objects representing services included in this
1839 package but not yet provisioned. Each FS::part_svc object also has an extra
1840 field, I<num_avail>, which specifies the number of available services.
1844 sub available_part_svc {
1846 grep { $_->num_avail > 0 }
1848 my $part_svc = $_->part_svc;
1849 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
1850 $_->quantity - $self->num_cust_svc($_->svcpart);
1852 # more evil encapsulation breakage
1853 if($part_svc->{'Hash'}{'num_avail'} > 0) {
1854 my @exports = $part_svc->part_export_did;
1855 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
1860 $self->part_pkg->pkg_svc;
1865 Returns a list of FS::part_svc objects representing provisioned and available
1866 services included in this package. Each FS::part_svc object also has the
1867 following extra fields:
1871 =item num_cust_svc (count)
1873 =item num_avail (quantity - count)
1875 =item cust_pkg_svc (services) - array reference containing the provisioned services, as cust_svc objects
1878 label -> ($cust_svc->label)[1]
1887 #XXX some sort of sort order besides numeric by svcpart...
1888 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
1890 my $part_svc = $pkg_svc->part_svc;
1891 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
1892 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
1893 $part_svc->{'Hash'}{'num_avail'} =
1894 max( 0, $pkg_svc->quantity - $num_cust_svc );
1895 $part_svc->{'Hash'}{'cust_pkg_svc'} =
1896 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
1897 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
1899 } $self->part_pkg->pkg_svc;
1902 push @part_svc, map {
1904 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
1905 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
1906 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
1907 $part_svc->{'Hash'}{'cust_pkg_svc'} =
1908 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
1910 } $self->extra_part_svc;
1916 =item extra_part_svc
1918 Returns a list of FS::part_svc objects corresponding to services in this
1919 package which are still provisioned but not (any longer) available in the
1924 sub extra_part_svc {
1927 my $pkgnum = $self->pkgnum;
1928 my $pkgpart = $self->pkgpart;
1931 # 'table' => 'part_svc',
1934 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
1935 # WHERE pkg_svc.svcpart = part_svc.svcpart
1936 # AND pkg_svc.pkgpart = ?
1939 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
1940 # LEFT JOIN cust_pkg USING ( pkgnum )
1941 # WHERE cust_svc.svcpart = part_svc.svcpart
1944 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
1947 #seems to benchmark slightly faster...
1949 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
1950 #MySQL doesn't grok DISINCT ON
1951 'select' => 'DISTINCT part_svc.*',
1952 'table' => 'part_svc',
1954 'LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
1955 AND pkg_svc.pkgpart = ?
1958 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
1959 LEFT JOIN cust_pkg USING ( pkgnum )
1962 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
1963 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
1969 Returns a short status string for this package, currently:
1973 =item not yet billed
1975 =item one-time charge
1990 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
1992 return 'cancelled' if $self->get('cancel');
1993 return 'suspended' if $self->susp;
1994 return 'not yet billed' unless $self->setup;
1995 return 'one-time charge' if $freq =~ /^(0|$)/;
1999 =item ucfirst_status
2001 Returns the status with the first character capitalized.
2005 sub ucfirst_status {
2006 ucfirst(shift->status);
2011 Class method that returns the list of possible status strings for packages
2012 (see L<the status method|/status>). For example:
2014 @statuses = FS::cust_pkg->statuses();
2018 tie my %statuscolor, 'Tie::IxHash',
2019 'not yet billed' => '009999', #teal? cyan?
2020 'one-time charge' => '000000',
2021 'active' => '00CC00',
2022 'suspended' => 'FF9900',
2023 'cancelled' => 'FF0000',
2027 my $self = shift; #could be class...
2028 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
2029 # # mayble split btw one-time vs. recur
2035 Returns a hex triplet color string for this package's status.
2041 $statuscolor{$self->status};
2046 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
2047 "pkg-comment" depending on user preference).
2053 my $label = $self->part_pkg->pkg_comment( 'nopkgpart' => 1 );
2054 $label = $self->pkgnum. ": $label"
2055 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
2059 =item pkg_label_long
2061 Returns a long label for this package, adding the primary service's label to
2066 sub pkg_label_long {
2068 my $label = $self->pkg_label;
2069 my $cust_svc = $self->primary_cust_svc;
2070 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
2074 =item primary_cust_svc
2076 Returns a primary service (as FS::cust_svc object) if one can be identified.
2080 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
2082 sub primary_cust_svc {
2085 my @cust_svc = $self->cust_svc;
2087 return '' unless @cust_svc; #no serivces - irrelevant then
2089 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
2091 # primary service as specified in the package definition
2092 # or exactly one service definition with quantity one
2093 my $svcpart = $self->part_pkg->svcpart;
2094 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
2095 return $cust_svc[0] if scalar(@cust_svc) == 1;
2097 #couldn't identify one thing..
2103 Returns a list of lists, calling the label method for all services
2104 (see L<FS::cust_svc>) of this billing item.
2110 map { [ $_->label ] } $self->cust_svc;
2113 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
2115 Like the labels method, but returns historical information on services that
2116 were active as of END_TIMESTAMP and (optionally) not cancelled before
2117 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
2118 I<pkg_svc.hidden> flag will be omitted.
2120 Returns a list of lists, calling the label method for all (historical) services
2121 (see L<FS::h_cust_svc>) of this billing item.
2127 warn "$me _h_labels called on $self\n"
2129 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
2134 Like labels, except returns a simple flat list, and shortens long
2135 (currently >5 or the cust_bill-max_same_services configuration value) lists of
2136 identical services to one line that lists the service label and the number of
2137 individual services rather than individual items.
2142 shift->_labels_short( 'labels', @_ );
2145 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
2147 Like h_labels, except returns a simple flat list, and shortens long
2148 (currently >5 or the cust_bill-max_same_services configuration value) lists of
2149 identical services to one line that lists the service label and the number of
2150 individual services rather than individual items.
2154 sub h_labels_short {
2155 shift->_labels_short( 'h_labels', @_ );
2159 my( $self, $method ) = ( shift, shift );
2161 warn "$me _labels_short called on $self with $method method\n"
2164 my $conf = new FS::Conf;
2165 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
2167 warn "$me _labels_short populating \%labels\n"
2171 #tie %labels, 'Tie::IxHash';
2172 push @{ $labels{$_->[0]} }, $_->[1]
2173 foreach $self->$method(@_);
2175 warn "$me _labels_short populating \@labels\n"
2179 foreach my $label ( keys %labels ) {
2181 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
2182 my $num = scalar(@values);
2183 warn "$me _labels_short $num items for $label\n"
2186 if ( $num > $max_same_services ) {
2187 warn "$me _labels_short more than $max_same_services, so summarizing\n"
2189 push @labels, "$label ($num)";
2191 if ( $conf->exists('cust_bill-consolidate_services') ) {
2192 warn "$me _labels_short consolidating services\n"
2194 # push @labels, "$label: ". join(', ', @values);
2196 my $detail = "$label: ";
2197 $detail .= shift(@values). ', '
2198 while @values && length($detail.$values[0]) < 78;
2200 push @labels, $detail;
2202 warn "$me _labels_short done consolidating services\n"
2205 warn "$me _labels_short adding service data\n"
2207 push @labels, map { "$label: $_" } @values;
2218 Returns the parent customer object (see L<FS::cust_main>).
2224 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
2227 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
2231 Returns the location object, if any (see L<FS::cust_location>).
2233 =item cust_location_or_main
2235 If this package is associated with a location, returns the locaiton (see
2236 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
2238 =item location_label [ OPTION => VALUE ... ]
2240 Returns the label of the location object (see L<FS::cust_location>).
2244 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
2246 =item seconds_since TIMESTAMP
2248 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
2249 package have been online since TIMESTAMP, according to the session monitor.
2251 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
2252 L<Time::Local> and L<Date::Parse> for conversion functions.
2257 my($self, $since) = @_;
2260 foreach my $cust_svc (
2261 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
2263 $seconds += $cust_svc->seconds_since($since);
2270 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
2272 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
2273 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
2276 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2277 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2283 sub seconds_since_sqlradacct {
2284 my($self, $start, $end) = @_;
2288 foreach my $cust_svc (
2290 my $part_svc = $_->part_svc;
2291 $part_svc->svcdb eq 'svc_acct'
2292 && scalar($part_svc->part_export('sqlradius'));
2295 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
2302 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
2304 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
2305 in this package for sessions ending between TIMESTAMP_START (inclusive) and
2309 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2310 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2315 sub attribute_since_sqlradacct {
2316 my($self, $start, $end, $attrib) = @_;
2320 foreach my $cust_svc (
2322 my $part_svc = $_->part_svc;
2323 $part_svc->svcdb eq 'svc_acct'
2324 && scalar($part_svc->part_export('sqlradius'));
2327 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
2339 my( $self, $value ) = @_;
2340 if ( defined($value) ) {
2341 $self->setfield('quantity', $value);
2343 $self->getfield('quantity') || 1;
2346 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
2348 Transfers as many services as possible from this package to another package.
2350 The destination package can be specified by pkgnum by passing an FS::cust_pkg
2351 object. The destination package must already exist.
2353 Services are moved only if the destination allows services with the correct
2354 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
2355 this option with caution! No provision is made for export differences
2356 between the old and new service definitions. Probably only should be used
2357 when your exports for all service definitions of a given svcdb are identical.
2358 (attempt a transfer without it first, to move all possible svcpart-matching
2361 Any services that can't be moved remain in the original package.
2363 Returns an error, if there is one; otherwise, returns the number of services
2364 that couldn't be moved.
2369 my ($self, $dest_pkgnum, %opt) = @_;
2375 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
2376 $dest = $dest_pkgnum;
2377 $dest_pkgnum = $dest->pkgnum;
2379 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
2382 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
2384 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
2385 $target{$pkg_svc->svcpart} = $pkg_svc->quantity;
2388 foreach my $cust_svc ($dest->cust_svc) {
2389 $target{$cust_svc->svcpart}--;
2392 my %svcpart2svcparts = ();
2393 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
2394 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
2395 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
2396 next if exists $svcpart2svcparts{$svcpart};
2397 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
2398 $svcpart2svcparts{$svcpart} = [
2400 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
2402 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
2403 'svcpart' => $_ } );
2405 $pkg_svc ? $pkg_svc->primary_svc : '',
2406 $pkg_svc ? $pkg_svc->quantity : 0,
2410 grep { $_ != $svcpart }
2412 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
2414 warn "alternates for svcpart $svcpart: ".
2415 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
2420 foreach my $cust_svc ($self->cust_svc) {
2421 if($target{$cust_svc->svcpart} > 0) {
2422 $target{$cust_svc->svcpart}--;
2423 my $new = new FS::cust_svc { $cust_svc->hash };
2424 $new->pkgnum($dest_pkgnum);
2425 my $error = $new->replace($cust_svc);
2426 return $error if $error;
2427 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
2429 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
2430 warn "alternates to consider: ".
2431 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
2433 my @alternate = grep {
2434 warn "considering alternate svcpart $_: ".
2435 "$target{$_} available in new package\n"
2438 } @{$svcpart2svcparts{$cust_svc->svcpart}};
2440 warn "alternate(s) found\n" if $DEBUG;
2441 my $change_svcpart = $alternate[0];
2442 $target{$change_svcpart}--;
2443 my $new = new FS::cust_svc { $cust_svc->hash };
2444 $new->svcpart($change_svcpart);
2445 $new->pkgnum($dest_pkgnum);
2446 my $error = $new->replace($cust_svc);
2447 return $error if $error;
2460 This method is deprecated. See the I<depend_jobnum> option to the insert and
2461 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
2468 local $SIG{HUP} = 'IGNORE';
2469 local $SIG{INT} = 'IGNORE';
2470 local $SIG{QUIT} = 'IGNORE';
2471 local $SIG{TERM} = 'IGNORE';
2472 local $SIG{TSTP} = 'IGNORE';
2473 local $SIG{PIPE} = 'IGNORE';
2475 my $oldAutoCommit = $FS::UID::AutoCommit;
2476 local $FS::UID::AutoCommit = 0;
2479 foreach my $cust_svc ( $self->cust_svc ) {
2480 #false laziness w/svc_Common::insert
2481 my $svc_x = $cust_svc->svc_x;
2482 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
2483 my $error = $part_export->export_insert($svc_x);
2485 $dbh->rollback if $oldAutoCommit;
2491 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2498 Associates this package with a (suspension or cancellation) reason (see
2499 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
2502 Available options are:
2508 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.
2512 the access_user (see L<FS::access_user>) providing the reason
2520 the action (cancel, susp, adjourn, expire) associated with the reason
2524 If there is an error, returns the error, otherwise returns false.
2529 my ($self, %options) = @_;
2531 my $otaker = $options{reason_otaker} ||
2532 $FS::CurrentUser::CurrentUser->username;
2535 if ( $options{'reason'} =~ /^(\d+)$/ ) {
2539 } elsif ( ref($options{'reason'}) ) {
2541 return 'Enter a new reason (or select an existing one)'
2542 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
2544 my $reason = new FS::reason({
2545 'reason_type' => $options{'reason'}->{'typenum'},
2546 'reason' => $options{'reason'}->{'reason'},
2548 my $error = $reason->insert;
2549 return $error if $error;
2551 $reasonnum = $reason->reasonnum;
2554 return "Unparsable reason: ". $options{'reason'};
2557 my $cust_pkg_reason =
2558 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
2559 'reasonnum' => $reasonnum,
2560 'otaker' => $otaker,
2561 'action' => substr(uc($options{'action'}),0,1),
2562 'date' => $options{'date'}
2567 $cust_pkg_reason->insert;
2570 =item insert_discount
2572 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
2573 inserting a new discount on the fly (see L<FS::discount>).
2575 Available options are:
2583 If there is an error, returns the error, otherwise returns false.
2587 sub insert_discount {
2588 #my ($self, %options) = @_;
2591 my $cust_pkg_discount = new FS::cust_pkg_discount {
2592 'pkgnum' => $self->pkgnum,
2593 'discountnum' => $self->discountnum,
2595 'end_date' => '', #XXX
2596 #for the create a new discount case
2597 '_type' => $self->discountnum__type,
2598 'amount' => $self->discountnum_amount,
2599 'percent' => $self->discountnum_percent,
2600 'months' => $self->discountnum_months,
2601 #'disabled' => $self->discountnum_disabled,
2604 $cust_pkg_discount->insert;
2607 =item set_usage USAGE_VALUE_HASHREF
2609 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
2610 to which they should be set (see L<FS::svc_acct>). Currently seconds,
2611 upbytes, downbytes, and totalbytes are appropriate keys.
2613 All svc_accts which are part of this package have their values reset.
2618 my ($self, $valueref, %opt) = @_;
2620 foreach my $cust_svc ($self->cust_svc){
2621 my $svc_x = $cust_svc->svc_x;
2622 $svc_x->set_usage($valueref, %opt)
2623 if $svc_x->can("set_usage");
2627 =item recharge USAGE_VALUE_HASHREF
2629 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
2630 to which they should be set (see L<FS::svc_acct>). Currently seconds,
2631 upbytes, downbytes, and totalbytes are appropriate keys.
2633 All svc_accts which are part of this package have their values incremented.
2638 my ($self, $valueref) = @_;
2640 foreach my $cust_svc ($self->cust_svc){
2641 my $svc_x = $cust_svc->svc_x;
2642 $svc_x->recharge($valueref)
2643 if $svc_x->can("recharge");
2647 =item cust_pkg_discount
2651 sub cust_pkg_discount {
2653 qsearch('cust_pkg_discount', { 'pkgnum' => $self->pkgnum } );
2656 =item cust_pkg_discount_active
2660 sub cust_pkg_discount_active {
2662 grep { $_->status eq 'active' } $self->cust_pkg_discount;
2667 =head1 CLASS METHODS
2673 Returns an SQL expression identifying recurring packages.
2677 sub recurring_sql { "
2678 '0' != ( select freq from part_pkg
2679 where cust_pkg.pkgpart = part_pkg.pkgpart )
2684 Returns an SQL expression identifying one-time packages.
2689 '0' = ( select freq from part_pkg
2690 where cust_pkg.pkgpart = part_pkg.pkgpart )
2695 Returns an SQL expression identifying ordered packages (recurring packages not
2701 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
2706 Returns an SQL expression identifying active packages.
2711 $_[0]->recurring_sql. "
2712 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
2713 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2714 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
2717 =item not_yet_billed_sql
2719 Returns an SQL expression identifying packages which have not yet been billed.
2723 sub not_yet_billed_sql { "
2724 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
2725 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2726 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
2731 Returns an SQL expression identifying inactive packages (one-time packages
2732 that are otherwise unsuspended/uncancelled).
2736 sub inactive_sql { "
2737 ". $_[0]->onetime_sql(). "
2738 AND cust_pkg.setup IS NOT NULL AND 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 )
2746 Returns an SQL expression identifying suspended packages.
2750 sub suspended_sql { susp_sql(@_); }
2752 #$_[0]->recurring_sql(). ' AND '.
2754 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2755 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
2762 Returns an SQL exprression identifying cancelled packages.
2766 sub cancelled_sql { cancel_sql(@_); }
2768 #$_[0]->recurring_sql(). ' AND '.
2769 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
2774 Returns an SQL expression to give the package status as a string.
2780 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
2781 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
2782 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
2783 WHEN ".onetime_sql()." THEN 'one-time charge'
2788 =item search HASHREF
2792 Returns a qsearch hash expression to search for parameters specified in HASHREF.
2793 Valid parameters are
2801 active, inactive, suspended, cancel (or cancelled)
2805 active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
2809 boolean selects custom packages
2815 pkgpart or arrayref or hashref of pkgparts
2819 arrayref of beginning and ending epoch date
2823 arrayref of beginning and ending epoch date
2827 arrayref of beginning and ending epoch date
2831 arrayref of beginning and ending epoch date
2835 arrayref of beginning and ending epoch date
2839 arrayref of beginning and ending epoch date
2843 arrayref of beginning and ending epoch date
2847 pkgnum or APKG_pkgnum
2851 a value suited to passing to FS::UI::Web::cust_header
2855 specifies the user for agent virtualization
2859 boolean selects packages containing fcc form 477 telco lines
2866 my ($class, $params) = @_;
2873 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
2875 "cust_main.agentnum = $1";
2882 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
2884 "cust_pkg.custnum = $1";
2891 if ( $params->{'pkgbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
2893 "cust_pkg.pkgbatch = '$1'";
2900 if ( $params->{'magic'} eq 'active'
2901 || $params->{'status'} eq 'active' ) {
2903 push @where, FS::cust_pkg->active_sql();
2905 } elsif ( $params->{'magic'} =~ /^not[ _]yet[ _]billed$/
2906 || $params->{'status'} =~ /^not[ _]yet[ _]billed$/ ) {
2908 push @where, FS::cust_pkg->not_yet_billed_sql();
2910 } elsif ( $params->{'magic'} =~ /^(one-time charge|inactive)/
2911 || $params->{'status'} =~ /^(one-time charge|inactive)/ ) {
2913 push @where, FS::cust_pkg->inactive_sql();
2915 } elsif ( $params->{'magic'} eq 'suspended'
2916 || $params->{'status'} eq 'suspended' ) {
2918 push @where, FS::cust_pkg->suspended_sql();
2920 } elsif ( $params->{'magic'} =~ /^cancell?ed$/
2921 || $params->{'status'} =~ /^cancell?ed$/ ) {
2923 push @where, FS::cust_pkg->cancelled_sql();
2928 # parse package class
2931 #false lazinessish w/graph/cust_bill_pkg.cgi
2934 if ( exists($params->{'classnum'})
2935 && $params->{'classnum'} =~ /^(\d*)$/
2939 if ( $classnum ) { #a specific class
2940 push @where, "part_pkg.classnum = $classnum";
2942 #@pkg_class = ( qsearchs('pkg_class', { 'classnum' => $classnum } ) );
2943 #die "classnum $classnum not found!" unless $pkg_class[0];
2944 #$title .= $pkg_class[0]->classname.' ';
2946 } elsif ( $classnum eq '' ) { #the empty class
2948 push @where, "part_pkg.classnum IS NULL";
2949 #$title .= 'Empty class ';
2950 #@pkg_class = ( '(empty class)' );
2951 } elsif ( $classnum eq '0' ) {
2952 #@pkg_class = qsearch('pkg_class', {} ); # { 'disabled' => '' } );
2953 #push @pkg_class, '(empty class)';
2955 die "illegal classnum";
2961 # parse package report options
2964 my @report_option = ();
2965 if ( exists($params->{'report_option'})
2966 && $params->{'report_option'} =~ /^([,\d]*)$/
2969 @report_option = split(',', $1);
2972 if (@report_option) {
2973 # this will result in the empty set for the dangling comma case as it should
2975 map{ "0 < ( SELECT count(*) FROM part_pkg_option
2976 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
2977 AND optionname = 'report_option_$_'
2978 AND optionvalue = '1' )"
2988 push @where, "part_pkg.custom = 'Y'" if $params->{custom};
2994 push @where, "part_pkg.fcc_ds0s > 0" if $params->{fcc_line};
3000 if ( exists($params->{'censustract'}) ) {
3001 $params->{'censustract'} =~ /^([.\d]*)$/;
3002 my $censustract = "cust_main.censustract = '$1'";
3003 $censustract .= ' OR cust_main.censustract is NULL' unless $1;
3004 push @where, "( $censustract )";
3011 if ( ref($params->{'pkgpart'}) ) {
3014 if ( ref($params->{'pkgpart'}) eq 'HASH' ) {
3015 @pkgpart = grep $params->{'pkgpart'}{$_}, keys %{ $params->{'pkgpart'} };
3016 } elsif ( ref($params->{'pkgpart'}) eq 'ARRAY' ) {
3017 @pkgpart = @{ $params->{'pkgpart'} };
3019 die 'unhandled pkgpart ref '. $params->{'pkgpart'};
3022 @pkgpart = grep /^(\d+)$/, @pkgpart;
3024 push @where, 'pkgpart IN ('. join(',', @pkgpart). ')' if scalar(@pkgpart);
3026 } elsif ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
3027 push @where, "pkgpart = $1";
3036 #false laziness w/report_cust_pkg.html
3039 'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
3040 'active' => { 'susp'=>1, 'cancel'=>1 },
3041 'suspended' => { 'cancel' => 1 },
3046 if( exists($params->{'active'} ) ) {
3047 # This overrides all the other date-related fields
3048 my($beginning, $ending) = @{$params->{'active'}};
3050 "cust_pkg.setup IS NOT NULL",
3051 "cust_pkg.setup <= $ending",
3052 "(cust_pkg.cancel IS NULL OR cust_pkg.cancel >= $beginning )",
3053 "NOT (".FS::cust_pkg->onetime_sql . ")";
3056 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end cancel )) {
3058 next unless exists($params->{$field});
3060 my($beginning, $ending) = @{$params->{$field}};
3062 next if $beginning == 0 && $ending == 4294967295;
3065 "cust_pkg.$field IS NOT NULL",
3066 "cust_pkg.$field >= $beginning",
3067 "cust_pkg.$field <= $ending";
3069 $orderby ||= "ORDER BY cust_pkg.$field";
3074 $orderby ||= 'ORDER BY bill';
3077 # parse magic, legacy, etc.
3080 if ( $params->{'magic'} &&
3081 $params->{'magic'} =~ /^(active|inactive|suspended|cancell?ed)$/
3084 $orderby = 'ORDER BY pkgnum';
3086 if ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
3087 push @where, "pkgpart = $1";
3090 } elsif ( $params->{'query'} eq 'pkgnum' ) {
3092 $orderby = 'ORDER BY pkgnum';
3094 } elsif ( $params->{'query'} eq 'APKG_pkgnum' ) {
3096 $orderby = 'ORDER BY pkgnum';
3099 SELECT count(*) FROM pkg_svc
3100 WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
3101 AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
3102 WHERE cust_svc.pkgnum = cust_pkg.pkgnum
3103 AND cust_svc.svcpart = pkg_svc.svcpart
3110 # setup queries, links, subs, etc. for the search
3113 # here is the agent virtualization
3114 if ($params->{CurrentUser}) {
3116 qsearchs('access_user', { username => $params->{CurrentUser} });
3119 push @where, $access_user->agentnums_sql('table'=>'cust_main');
3124 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql('table'=>'cust_main');
3127 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
3129 my $addl_from = 'LEFT JOIN cust_main USING ( custnum ) '.
3130 'LEFT JOIN part_pkg USING ( pkgpart ) '.
3131 'LEFT JOIN pkg_class ON ( part_pkg.classnum = pkg_class.classnum ) ';
3133 my $count_query = "SELECT COUNT(*) FROM cust_pkg $addl_from $extra_sql";
3136 'table' => 'cust_pkg',
3138 'select' => join(', ',
3140 ( map "part_pkg.$_", qw( pkg freq ) ),
3141 'pkg_class.classname',
3142 'cust_main.custnum AS cust_main_custnum',
3143 FS::UI::Web::cust_sql_fields(
3144 $params->{'cust_fields'}
3147 'extra_sql' => "$extra_sql $orderby",
3148 'addl_from' => $addl_from,
3149 'count_query' => $count_query,
3156 Returns a list of two package counts. The first is a count of packages
3157 based on the supplied criteria and the second is the count of residential
3158 packages with those same criteria. Criteria are specified as in the search
3164 my ($class, $params) = @_;
3166 my $sql_query = $class->search( $params );
3168 my $count_sql = delete($sql_query->{'count_query'});
3169 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
3170 or die "couldn't parse count_sql";
3172 my $count_sth = dbh->prepare($count_sql)
3173 or die "Error preparing $count_sql: ". dbh->errstr;
3175 or die "Error executing $count_sql: ". $count_sth->errstr;
3176 my $count_arrayref = $count_sth->fetchrow_arrayref;
3178 return ( @$count_arrayref );
3185 Returns a list: the first item is an SQL fragment identifying matching
3186 packages/customers via location (taking into account shipping and package
3187 address taxation, if enabled), and subsequent items are the parameters to
3188 substitute for the placeholders in that fragment.
3193 my($class, %opt) = @_;
3194 my $ornull = $opt{'ornull'};
3196 my $conf = new FS::Conf;
3198 # '?' placeholders in _location_sql_where
3199 my $x = $ornull ? 3 : 2;
3200 my @bill_param = ( ('city')x3, ('county')x$x, ('state')x$x, 'country' );
3204 if ( $conf->exists('tax-ship_address') ) {
3207 ( ( ship_last IS NULL OR ship_last = '' )
3208 AND ". _location_sql_where('cust_main', '', $ornull ). "
3210 OR ( ship_last IS NOT NULL AND ship_last != ''
3211 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
3214 # AND payby != 'COMP'
3216 @main_param = ( @bill_param, @bill_param );
3220 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
3221 @main_param = @bill_param;
3227 if ( $conf->exists('tax-pkg_address') ) {
3229 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
3232 ( cust_pkg.locationnum IS NULL AND $main_where )
3233 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
3236 @param = ( @main_param, @bill_param );
3240 $where = $main_where;
3241 @param = @main_param;
3249 #subroutine, helper for location_sql
3250 sub _location_sql_where {
3252 my $prefix = @_ ? shift : '';
3253 my $ornull = @_ ? shift : '';
3255 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
3257 $ornull = $ornull ? ' OR ? IS NULL ' : '';
3259 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL ) ";
3260 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL ) ";
3261 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL ) ";
3263 # ( $table.${prefix}city = ? $or_empty_city $ornull )
3265 ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS text) IS NULL )
3266 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
3267 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
3268 AND $table.${prefix}country = ?
3276 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
3278 CUSTNUM is a customer (see L<FS::cust_main>)
3280 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
3281 L<FS::part_pkg>) to order for this customer. Duplicates are of course
3284 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
3285 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
3286 new billing items. An error is returned if this is not possible (see
3287 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
3290 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
3291 newly-created cust_pkg objects.
3293 REFNUM, if specified, will specify the FS::pkg_referral record to be created
3294 and inserted. Multiple FS::pkg_referral records can be created by
3295 setting I<refnum> to an array reference of refnums or a hash reference with
3296 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
3297 record will be created corresponding to cust_main.refnum.
3302 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
3304 my $conf = new FS::Conf;
3306 # Transactionize this whole mess
3307 local $SIG{HUP} = 'IGNORE';
3308 local $SIG{INT} = 'IGNORE';
3309 local $SIG{QUIT} = 'IGNORE';
3310 local $SIG{TERM} = 'IGNORE';
3311 local $SIG{TSTP} = 'IGNORE';
3312 local $SIG{PIPE} = 'IGNORE';
3314 my $oldAutoCommit = $FS::UID::AutoCommit;
3315 local $FS::UID::AutoCommit = 0;
3319 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
3320 # return "Customer not found: $custnum" unless $cust_main;
3322 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
3325 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
3328 my $change = scalar(@old_cust_pkg) != 0;
3331 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
3333 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
3334 " to pkgpart ". $pkgparts->[0]. "\n"
3337 my $err_or_cust_pkg =
3338 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
3339 'refnum' => $refnum,
3342 unless (ref($err_or_cust_pkg)) {
3343 $dbh->rollback if $oldAutoCommit;
3344 return $err_or_cust_pkg;
3347 push @$return_cust_pkg, $err_or_cust_pkg;
3348 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3353 # Create the new packages.
3354 foreach my $pkgpart (@$pkgparts) {
3356 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
3358 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
3359 pkgpart => $pkgpart,
3363 $error = $cust_pkg->insert( 'change' => $change );
3365 $dbh->rollback if $oldAutoCommit;
3368 push @$return_cust_pkg, $cust_pkg;
3370 # $return_cust_pkg now contains refs to all of the newly
3373 # Transfer services and cancel old packages.
3374 foreach my $old_pkg (@old_cust_pkg) {
3376 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
3379 foreach my $new_pkg (@$return_cust_pkg) {
3380 $error = $old_pkg->transfer($new_pkg);
3381 if ($error and $error == 0) {
3382 # $old_pkg->transfer failed.
3383 $dbh->rollback if $oldAutoCommit;
3388 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
3389 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
3390 foreach my $new_pkg (@$return_cust_pkg) {
3391 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
3392 if ($error and $error == 0) {
3393 # $old_pkg->transfer failed.
3394 $dbh->rollback if $oldAutoCommit;
3401 # Transfers were successful, but we went through all of the
3402 # new packages and still had services left on the old package.
3403 # We can't cancel the package under the circumstances, so abort.
3404 $dbh->rollback if $oldAutoCommit;
3405 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
3407 $error = $old_pkg->cancel( quiet=>1 );
3413 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3417 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
3419 A bulk change method to change packages for multiple customers.
3421 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
3422 L<FS::part_pkg>) to order for each customer. Duplicates are of course
3425 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
3426 replace. The services (see L<FS::cust_svc>) are moved to the
3427 new billing items. An error is returned if this is not possible (see
3430 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
3431 newly-created cust_pkg objects.
3436 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
3438 # Transactionize this whole mess
3439 local $SIG{HUP} = 'IGNORE';
3440 local $SIG{INT} = 'IGNORE';
3441 local $SIG{QUIT} = 'IGNORE';
3442 local $SIG{TERM} = 'IGNORE';
3443 local $SIG{TSTP} = 'IGNORE';
3444 local $SIG{PIPE} = 'IGNORE';
3446 my $oldAutoCommit = $FS::UID::AutoCommit;
3447 local $FS::UID::AutoCommit = 0;
3451 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
3454 while(scalar(@old_cust_pkg)) {
3456 my $custnum = $old_cust_pkg[0]->custnum;
3457 my (@remove) = map { $_->pkgnum }
3458 grep { $_->custnum == $custnum } @old_cust_pkg;
3459 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
3461 my $error = order $custnum, $pkgparts, \@remove, \@return;
3463 push @errors, $error
3465 push @$return_cust_pkg, @return;
3468 if (scalar(@errors)) {
3469 $dbh->rollback if $oldAutoCommit;
3470 return join(' / ', @errors);
3473 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3477 # Used by FS::Upgrade to migrate to a new database.
3478 sub _upgrade_data { # class method
3479 my ($class, %opts) = @_;
3480 $class->_upgrade_otaker(%opts);
3482 # RT#10139, bug resulting in contract_end being set when it shouldn't
3483 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
3484 # RT#10830, bad calculation of prorate date near end of year
3485 # the date range for bill is December 2009, and we move it forward
3486 # one year if it's before the previous bill date (which it should
3488 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
3489 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
3490 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
3491 # RT6628, add order_date to cust_pkg
3492 'update cust_pkg set order_date = (select history_date from h_cust_pkg
3493 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
3494 history_action = \'insert\') where order_date is null',
3496 foreach my $sql (@statements) {
3497 my $sth = dbh->prepare($sql);
3498 $sth->execute or die $sth->errstr;
3506 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
3508 In sub order, the @pkgparts array (passed by reference) is clobbered.
3510 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
3511 method to pass dates to the recur_prog expression, it should do so.
3513 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
3514 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
3515 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
3516 configuration values. Probably need a subroutine which decides what to do
3517 based on whether or not we've fetched the user yet, rather than a hash. See
3518 FS::UID and the TODO.
3520 Now that things are transactional should the check in the insert method be
3525 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
3526 L<FS::pkg_svc>, schema.html from the base documentation