4 use vars qw(@ISA $disable_agentcheck $DEBUG);
5 use Scalar::Util qw( blessed );
6 use List::Util qw(max);
8 use FS::UID qw( getotaker dbh );
9 use FS::Misc qw( send_email );
10 use FS::Record qw( qsearch qsearchs );
12 use FS::cust_main_Mixin;
16 use FS::cust_location;
18 use FS::cust_bill_pkg;
19 use FS::cust_pkg_detail;
24 use FS::cust_pkg_reason;
28 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
30 # because they load configuration by setting FS::UID::callback (see TODO)
36 # for sending cancel emails in sub cancel
39 @ISA = qw( FS::m2m_Common FS::cust_main_Mixin FS::option_Common FS::Record );
43 $disable_agentcheck = 0;
47 my ( $hashref, $cache ) = @_;
48 #if ( $hashref->{'pkgpart'} ) {
49 if ( $hashref->{'pkg'} ) {
50 # #@{ $self->{'_pkgnum'} } = ();
51 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
52 # $self->{'_pkgpart'} = $subcache;
53 # #push @{ $self->{'_pkgnum'} },
54 # FS::part_pkg->new_or_cached($hashref, $subcache);
55 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
57 if ( exists $hashref->{'svcnum'} ) {
58 #@{ $self->{'_pkgnum'} } = ();
59 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
60 $self->{'_svcnum'} = $subcache;
61 #push @{ $self->{'_pkgnum'} },
62 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
68 FS::cust_pkg - Object methods for cust_pkg objects
74 $record = new FS::cust_pkg \%hash;
75 $record = new FS::cust_pkg { 'column' => 'value' };
77 $error = $record->insert;
79 $error = $new_record->replace($old_record);
81 $error = $record->delete;
83 $error = $record->check;
85 $error = $record->cancel;
87 $error = $record->suspend;
89 $error = $record->unsuspend;
91 $part_pkg = $record->part_pkg;
93 @labels = $record->labels;
95 $seconds = $record->seconds_since($timestamp);
97 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
98 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
102 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
103 inherits from FS::Record. The following fields are currently supported:
109 Primary key (assigned automatically for new billing items)
113 Customer (see L<FS::cust_main>)
117 Billing item definition (see L<FS::part_pkg>)
121 Optional link to package location (see L<FS::location>)
129 date (next bill date)
153 order taker (assigned automatically if null, see L<FS::UID>)
157 If this field is set to 1, disables the automatic
158 unsuspension of this package when using the B<unsuspendauto> config option.
162 If not set, defaults to 1
166 Date of change from previous package
176 =item change_locationnum
182 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
183 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
184 L<Time::Local> and L<Date::Parse> for conversion functions.
192 Create a new billing item. To add the item to the database, see L<"insert">.
196 sub table { 'cust_pkg'; }
197 sub cust_linked { $_[0]->cust_main_custnum; }
198 sub cust_unlinked_msg {
200 "WARNING: can't find cust_main.custnum ". $self->custnum.
201 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
204 =item insert [ OPTION => VALUE ... ]
206 Adds this billing item to the database ("Orders" the item). If there is an
207 error, returns the error, otherwise returns false.
209 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
210 will be used to look up the package definition and agent restrictions will be
213 If the additional field I<refnum> is defined, an FS::pkg_referral record will
214 be created and inserted. Multiple FS::pkg_referral records can be created by
215 setting I<refnum> to an array reference of refnums or a hash reference with
216 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
217 record will be created corresponding to cust_main.refnum.
219 The following options are available:
225 If set true, supresses any referral credit to a referring customer.
229 cust_pkg_option records will be created
236 my( $self, %options ) = @_;
238 local $SIG{HUP} = 'IGNORE';
239 local $SIG{INT} = 'IGNORE';
240 local $SIG{QUIT} = 'IGNORE';
241 local $SIG{TERM} = 'IGNORE';
242 local $SIG{TSTP} = 'IGNORE';
243 local $SIG{PIPE} = 'IGNORE';
245 my $oldAutoCommit = $FS::UID::AutoCommit;
246 local $FS::UID::AutoCommit = 0;
249 my $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
251 $dbh->rollback if $oldAutoCommit;
255 $self->refnum($self->cust_main->refnum) unless $self->refnum;
256 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
257 $self->process_m2m( 'link_table' => 'pkg_referral',
258 'target_table' => 'part_referral',
259 'params' => $self->refnum,
262 #if ( $self->reg_code ) {
263 # my $reg_code = qsearchs('reg_code', { 'code' => $self->reg_code } );
264 # $error = $reg_code->delete;
266 # $dbh->rollback if $oldAutoCommit;
271 my $conf = new FS::Conf;
273 if ($conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
274 my $queue = new FS::queue {
275 'job' => 'FS::cust_main::queueable_print',
277 $error = $queue->insert(
278 'custnum' => $self->custnum,
279 'template' => 'welcome_letter',
283 warn "can't send welcome letter: $error";
288 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
295 This method now works but you probably shouldn't use it.
297 You don't want to delete billing items, because there would then be no record
298 the customer ever purchased the item. Instead, see the cancel method.
303 # return "Can't delete cust_pkg records!";
306 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
308 Replaces the OLD_RECORD with this one in the database. If there is an error,
309 returns the error, otherwise returns false.
311 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
313 Changing pkgpart may have disasterous effects. See the order subroutine.
315 setup and bill are normally updated by calling the bill method of a customer
316 object (see L<FS::cust_main>).
318 suspend is normally updated by the suspend and unsuspend methods.
320 cancel is normally updated by the cancel method (and also the order subroutine
323 Available options are:
329 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.
333 the access_user (see L<FS::access_user>) providing the reason
337 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
346 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
351 ( ref($_[0]) eq 'HASH' )
355 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
356 return "Can't change otaker!" if $old->otaker ne $new->otaker;
359 #return "Can't change setup once it exists!"
360 # if $old->getfield('setup') &&
361 # $old->getfield('setup') != $new->getfield('setup');
363 #some logic for bill, susp, cancel?
365 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
367 local $SIG{HUP} = 'IGNORE';
368 local $SIG{INT} = 'IGNORE';
369 local $SIG{QUIT} = 'IGNORE';
370 local $SIG{TERM} = 'IGNORE';
371 local $SIG{TSTP} = 'IGNORE';
372 local $SIG{PIPE} = 'IGNORE';
374 my $oldAutoCommit = $FS::UID::AutoCommit;
375 local $FS::UID::AutoCommit = 0;
378 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
379 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
380 my $error = $new->insert_reason(
381 'reason' => $options->{'reason'},
382 'date' => $new->$method,
384 'reason_otaker' => $options->{'reason_otaker'},
387 dbh->rollback if $oldAutoCommit;
388 return "Error inserting cust_pkg_reason: $error";
393 #save off and freeze RADIUS attributes for any associated svc_acct records
395 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
397 #also check for specific exports?
398 # to avoid spurious modify export events
399 @svc_acct = map { $_->svc_x }
400 grep { $_->part_svc->svcdb eq 'svc_acct' }
403 $_->snapshot foreach @svc_acct;
407 my $error = $new->SUPER::replace($old,
408 $options->{options} ? $options->{options} : ()
411 $dbh->rollback if $oldAutoCommit;
415 #for prepaid packages,
416 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
417 foreach my $old_svc_acct ( @svc_acct ) {
418 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
419 my $s_error = $new_svc_acct->replace($old_svc_acct);
421 $dbh->rollback if $oldAutoCommit;
426 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
433 Checks all fields to make sure this is a valid billing item. If there is an
434 error, returns the error, otherwise returns false. Called by the insert and
442 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
445 $self->ut_numbern('pkgnum')
446 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
447 || $self->ut_numbern('pkgpart')
448 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
449 || $self->ut_numbern('setup')
450 || $self->ut_numbern('bill')
451 || $self->ut_numbern('susp')
452 || $self->ut_numbern('cancel')
453 || $self->ut_numbern('adjourn')
454 || $self->ut_numbern('expire')
456 return $error if $error;
458 if ( $self->reg_code ) {
460 unless ( grep { $self->pkgpart == $_->pkgpart }
461 map { $_->reg_code_pkg }
462 qsearchs( 'reg_code', { 'code' => $self->reg_code,
463 'agentnum' => $self->cust_main->agentnum })
465 return "Unknown registration code";
468 } elsif ( $self->promo_code ) {
471 qsearchs('part_pkg', {
472 'pkgpart' => $self->pkgpart,
473 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
475 return 'Unknown promotional code' unless $promo_part_pkg;
479 unless ( $disable_agentcheck ) {
481 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
482 my $pkgpart_href = $agent->pkgpart_hashref;
483 return "agent ". $agent->agentnum.
484 " can't purchase pkgpart ". $self->pkgpart
485 unless $pkgpart_href->{ $self->pkgpart };
488 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
489 return $error if $error;
493 $self->otaker(getotaker) unless $self->otaker;
494 $self->otaker =~ /^(\w{1,32})$/ or return "Illegal otaker";
497 if ( $self->dbdef_table->column('manual_flag') ) {
498 $self->manual_flag('') if $self->manual_flag eq ' ';
499 $self->manual_flag =~ /^([01]?)$/
500 or return "Illegal manual_flag ". $self->manual_flag;
501 $self->manual_flag($1);
507 =item cancel [ OPTION => VALUE ... ]
509 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
510 in this package, then cancels the package itself (sets the cancel field to
513 Available options are:
517 =item quiet - can be set true to supress email cancellation notices.
519 =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.
521 =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.
523 =item date - can be set to a unix style timestamp to specify when to cancel (expire)
527 If there is an error, returns the error, otherwise returns false.
532 my( $self, %options ) = @_;
535 warn "cust_pkg::cancel called with options".
536 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
539 local $SIG{HUP} = 'IGNORE';
540 local $SIG{INT} = 'IGNORE';
541 local $SIG{QUIT} = 'IGNORE';
542 local $SIG{TERM} = 'IGNORE';
543 local $SIG{TSTP} = 'IGNORE';
544 local $SIG{PIPE} = 'IGNORE';
546 my $oldAutoCommit = $FS::UID::AutoCommit;
547 local $FS::UID::AutoCommit = 0;
550 my $old = $self->select_for_update;
552 if ( $old->get('cancel') || $self->get('cancel') ) {
553 dbh->rollback if $oldAutoCommit;
554 return ""; # no error
557 my $date = $options{date} if $options{date}; # expire/cancel later
558 $date = '' if ($date && $date <= time); # complain instead?
560 my $cancel_time = $options{'time'} || time;
562 if ( $options{'reason'} ) {
563 $error = $self->insert_reason( 'reason' => $options{'reason'},
564 'action' => $date ? 'expire' : 'cancel',
565 'date' => $date ? $date : $cancel_time,
566 'reason_otaker' => $options{'reason_otaker'},
569 dbh->rollback if $oldAutoCommit;
570 return "Error inserting cust_pkg_reason: $error";
576 foreach my $cust_svc (
579 sort { $a->[1] <=> $b->[1] }
580 map { [ $_, $_->svc_x->table_info->{'cancel_weight'} ]; }
581 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
584 my $error = $cust_svc->cancel;
587 $dbh->rollback if $oldAutoCommit;
588 return "Error cancelling cust_svc: $error";
592 # Add a credit for remaining service
593 my $remaining_value = $self->calc_remain(time=>$cancel_time);
594 if ( $remaining_value > 0 && !$options{'no_credit'} ) {
595 my $conf = new FS::Conf;
596 my $error = $self->cust_main->credit(
598 'Credit for unused time on '. $self->part_pkg->pkg,
599 'reason_type' => $conf->config('cancel_credit_type'),
602 $dbh->rollback if $oldAutoCommit;
603 return "Error crediting customer \$$remaining_value for unused time on".
604 $self->part_pkg->pkg. ": $error";
609 my %hash = $self->hash;
610 $date ? ($hash{'expire'} = $date) : ($hash{'cancel'} = $cancel_time);
611 my $new = new FS::cust_pkg ( \%hash );
612 $error = $new->replace( $self, options => { $self->options } );
614 $dbh->rollback if $oldAutoCommit;
618 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
619 return '' if $date; #no errors
621 my $conf = new FS::Conf;
622 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
623 if ( !$options{'quiet'} && $conf->exists('emailcancel') && @invoicing_list ) {
624 my $conf = new FS::Conf;
625 my $error = send_email(
626 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
627 'to' => \@invoicing_list,
628 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
629 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
631 #should this do something on errors?
638 =item cancel_if_expired [ NOW_TIMESTAMP ]
640 Cancels this package if its expire date has been reached.
644 sub cancel_if_expired {
646 my $time = shift || time;
647 return '' unless $self->expire && $self->expire <= $time;
648 my $error = $self->cancel;
650 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
651 $self->custnum. ": $error";
658 Cancels any pending expiration (sets the expire field to null).
660 If there is an error, returns the error, otherwise returns false.
665 my( $self, %options ) = @_;
668 local $SIG{HUP} = 'IGNORE';
669 local $SIG{INT} = 'IGNORE';
670 local $SIG{QUIT} = 'IGNORE';
671 local $SIG{TERM} = 'IGNORE';
672 local $SIG{TSTP} = 'IGNORE';
673 local $SIG{PIPE} = 'IGNORE';
675 my $oldAutoCommit = $FS::UID::AutoCommit;
676 local $FS::UID::AutoCommit = 0;
679 my $old = $self->select_for_update;
681 my $pkgnum = $old->pkgnum;
682 if ( $old->get('cancel') || $self->get('cancel') ) {
683 dbh->rollback if $oldAutoCommit;
684 return "Can't unexpire cancelled package $pkgnum";
685 # or at least it's pointless
688 unless ( $old->get('expire') && $self->get('expire') ) {
689 dbh->rollback if $oldAutoCommit;
690 return ""; # no error
693 my %hash = $self->hash;
694 $hash{'expire'} = '';
695 my $new = new FS::cust_pkg ( \%hash );
696 $error = $new->replace( $self, options => { $self->options } );
698 $dbh->rollback if $oldAutoCommit;
702 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
708 =item suspend [ OPTION => VALUE ... ]
710 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
711 package, then suspends the package itself (sets the susp field to now).
713 Available options are:
717 =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.
719 =item date - can be set to a unix style timestamp to specify when to suspend (adjourn)
723 If there is an error, returns the error, otherwise returns false.
728 my( $self, %options ) = @_;
731 local $SIG{HUP} = 'IGNORE';
732 local $SIG{INT} = 'IGNORE';
733 local $SIG{QUIT} = 'IGNORE';
734 local $SIG{TERM} = 'IGNORE';
735 local $SIG{TSTP} = 'IGNORE';
736 local $SIG{PIPE} = 'IGNORE';
738 my $oldAutoCommit = $FS::UID::AutoCommit;
739 local $FS::UID::AutoCommit = 0;
742 my $old = $self->select_for_update;
744 my $pkgnum = $old->pkgnum;
745 if ( $old->get('cancel') || $self->get('cancel') ) {
746 dbh->rollback if $oldAutoCommit;
747 return "Can't suspend cancelled package $pkgnum";
750 if ( $old->get('susp') || $self->get('susp') ) {
751 dbh->rollback if $oldAutoCommit;
752 return ""; # no error # complain on adjourn?
755 my $date = $options{date} if $options{date}; # adjourn/suspend later
756 $date = '' if ($date && $date <= time); # complain instead?
758 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
759 dbh->rollback if $oldAutoCommit;
760 return "Package $pkgnum expires before it would be suspended.";
763 my $suspend_time = $options{'time'} || time;
765 if ( $options{'reason'} ) {
766 $error = $self->insert_reason( 'reason' => $options{'reason'},
767 'action' => $date ? 'adjourn' : 'suspend',
768 'date' => $date ? $date : $suspend_time,
769 'reason_otaker' => $options{'reason_otaker'},
772 dbh->rollback if $oldAutoCommit;
773 return "Error inserting cust_pkg_reason: $error";
781 foreach my $cust_svc (
782 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
784 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
786 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
787 $dbh->rollback if $oldAutoCommit;
788 return "Illegal svcdb value in part_svc!";
791 require "FS/$svcdb.pm";
793 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
795 $error = $svc->suspend;
797 $dbh->rollback if $oldAutoCommit;
800 my( $label, $value ) = $cust_svc->label;
801 push @labels, "$label: $value";
805 my $conf = new FS::Conf;
806 if ( $conf->config('suspend_email_admin') ) {
808 my $error = send_email(
809 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
810 #invoice_from ??? well as good as any
811 'to' => $conf->config('suspend_email_admin'),
812 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
814 "This is an automatic message from your Freeside installation\n",
815 "informing you that the following customer package has been suspended:\n",
817 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
818 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
819 ( map { "Service : $_\n" } @labels ),
824 warn "WARNING: can't send suspension admin email (suspending anyway): ".
832 my %hash = $self->hash;
834 $hash{'adjourn'} = $date;
836 $hash{'susp'} = $suspend_time;
838 my $new = new FS::cust_pkg ( \%hash );
839 $error = $new->replace( $self, options => { $self->options } );
841 $dbh->rollback if $oldAutoCommit;
845 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
850 =item unsuspend [ OPTION => VALUE ... ]
852 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
853 package, then unsuspends the package itself (clears the susp field and the
854 adjourn field if it is in the past).
856 Available options are:
860 =item adjust_next_bill
862 Can be set true to adjust the next bill date forward by
863 the amount of time the account was inactive. This was set true by default
864 since 1.4.2 and 1.5.0pre6; however, starting with 1.7.0 this needs to be
865 explicitly requested. Price plans for which this makes sense (anniversary-date
866 based than prorate or subscription) could have an option to enable this
871 If there is an error, returns the error, otherwise returns false.
876 my( $self, %opt ) = @_;
879 local $SIG{HUP} = 'IGNORE';
880 local $SIG{INT} = 'IGNORE';
881 local $SIG{QUIT} = 'IGNORE';
882 local $SIG{TERM} = 'IGNORE';
883 local $SIG{TSTP} = 'IGNORE';
884 local $SIG{PIPE} = 'IGNORE';
886 my $oldAutoCommit = $FS::UID::AutoCommit;
887 local $FS::UID::AutoCommit = 0;
890 my $old = $self->select_for_update;
892 my $pkgnum = $old->pkgnum;
893 if ( $old->get('cancel') || $self->get('cancel') ) {
894 dbh->rollback if $oldAutoCommit;
895 return "Can't unsuspend cancelled package $pkgnum";
898 unless ( $old->get('susp') && $self->get('susp') ) {
899 dbh->rollback if $oldAutoCommit;
900 return ""; # no error # complain instead?
903 foreach my $cust_svc (
904 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
906 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
908 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
909 $dbh->rollback if $oldAutoCommit;
910 return "Illegal svcdb value in part_svc!";
913 require "FS/$svcdb.pm";
915 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
917 $error = $svc->unsuspend;
919 $dbh->rollback if $oldAutoCommit;
926 my %hash = $self->hash;
927 my $inactive = time - $hash{'susp'};
929 my $conf = new FS::Conf;
931 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive
932 if ( $opt{'adjust_next_bill'}
933 || $conf->exists('unsuspend-always_adjust_next_bill_date') )
934 && $inactive > 0 && ( $hash{'bill'} || $hash{'setup'} );
937 $hash{'adjourn'} = '' if $hash{'adjourn'} < time;
938 my $new = new FS::cust_pkg ( \%hash );
939 $error = $new->replace( $self, options => { $self->options } );
941 $dbh->rollback if $oldAutoCommit;
945 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
952 Cancels any pending suspension (sets the adjourn field to null).
954 If there is an error, returns the error, otherwise returns false.
959 my( $self, %options ) = @_;
962 local $SIG{HUP} = 'IGNORE';
963 local $SIG{INT} = 'IGNORE';
964 local $SIG{QUIT} = 'IGNORE';
965 local $SIG{TERM} = 'IGNORE';
966 local $SIG{TSTP} = 'IGNORE';
967 local $SIG{PIPE} = 'IGNORE';
969 my $oldAutoCommit = $FS::UID::AutoCommit;
970 local $FS::UID::AutoCommit = 0;
973 my $old = $self->select_for_update;
975 my $pkgnum = $old->pkgnum;
976 if ( $old->get('cancel') || $self->get('cancel') ) {
977 dbh->rollback if $oldAutoCommit;
978 return "Can't unadjourn cancelled package $pkgnum";
979 # or at least it's pointless
982 if ( $old->get('susp') || $self->get('susp') ) {
983 dbh->rollback if $oldAutoCommit;
984 return "Can't unadjourn suspended package $pkgnum";
985 # perhaps this is arbitrary
988 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
989 dbh->rollback if $oldAutoCommit;
990 return ""; # no error
993 my %hash = $self->hash;
994 $hash{'adjourn'} = '';
995 my $new = new FS::cust_pkg ( \%hash );
996 $error = $new->replace( $self, options => { $self->options } );
998 $dbh->rollback if $oldAutoCommit;
1002 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1009 =item change HASHREF | OPTION => VALUE ...
1011 Changes this package: cancels it and creates a new one, with a different
1012 pkgpart or locationnum or both. All services are transferred to the new
1013 package (no change will be made if this is not possible).
1015 Options may be passed as a list of key/value pairs or as a hash reference.
1022 New locationnum, to change the location for this package.
1026 New FS::cust_location object, to create a new location and assign it
1031 New pkgpart (see L<FS::part_pkg>).
1035 New refnum (see L<FS::part_referral>).
1039 At least one option must be specified (otherwise, what's the point?)
1041 Returns either the new FS::cust_pkg object or a scalar error.
1045 my $err_or_new_cust_pkg = $old_cust_pkg->change
1049 #some false laziness w/order
1052 my $opt = ref($_[0]) ? shift : { @_ };
1054 # my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
1057 my $conf = new FS::Conf;
1059 # Transactionize this whole mess
1060 local $SIG{HUP} = 'IGNORE';
1061 local $SIG{INT} = 'IGNORE';
1062 local $SIG{QUIT} = 'IGNORE';
1063 local $SIG{TERM} = 'IGNORE';
1064 local $SIG{TSTP} = 'IGNORE';
1065 local $SIG{PIPE} = 'IGNORE';
1067 my $oldAutoCommit = $FS::UID::AutoCommit;
1068 local $FS::UID::AutoCommit = 0;
1077 #$hash{$_} = $self->$_() foreach qw( last_bill bill );
1079 #$hash{$_} = $self->$_() foreach qw( setup );
1081 $hash{'setup'} = $time if $self->setup;
1083 $hash{'change_date'} = $time;
1084 $hash{"change_$_"} = $self->$_()
1085 foreach qw( pkgnum pkgpart locationnum );
1087 if ( $opt->{'cust_location'} &&
1088 ( ! $opt->{'locationnum'} || $opt->{'locationnum'} == -1 ) ) {
1089 $error = $opt->{'cust_location'}->insert;
1091 $dbh->rollback if $oldAutoCommit;
1092 return "inserting cust_location (transaction rolled back): $error";
1094 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1097 # Create the new package.
1098 my $cust_pkg = new FS::cust_pkg {
1099 custnum => $self->custnum,
1100 pkgpart => ( $opt->{'pkgpart'} || $self->pkgpart ),
1101 refnum => ( $opt->{'refnum'} || $self->refnum ),
1102 locationnum => ( $opt->{'locationnum'} || $self->locationnum ),
1106 $error = $cust_pkg->insert( 'change' => 1 );
1108 $dbh->rollback if $oldAutoCommit;
1112 # Transfer services and cancel old package.
1114 $error = $self->transfer($cust_pkg);
1115 if ($error and $error == 0) {
1116 # $old_pkg->transfer failed.
1117 $dbh->rollback if $oldAutoCommit;
1121 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
1122 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
1123 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
1124 if ($error and $error == 0) {
1125 # $old_pkg->transfer failed.
1126 $dbh->rollback if $oldAutoCommit;
1132 # Transfers were successful, but we still had services left on the old
1133 # package. We can't change the package under this circumstances, so abort.
1134 $dbh->rollback if $oldAutoCommit;
1135 return "Unable to transfer all services from package ". $self->pkgnum;
1138 #reset usage if changing pkgpart
1139 if ($self->pkgpart != $cust_pkg->pkgpart) {
1140 my $part_pkg = $cust_pkg->part_pkg;
1141 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
1145 if $part_pkg->can('reset_usage');
1148 $dbh->rollback if $oldAutoCommit;
1149 return "Error setting usage values: $error";
1153 #Good to go, cancel old package.
1154 $error = $self->cancel( quiet=>1 );
1160 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1167 Returns the last bill date, or if there is no last bill date, the setup date.
1168 Useful for billing metered services.
1174 return $self->setfield('last_bill', $_[0]) if @_;
1175 return $self->getfield('last_bill') if $self->getfield('last_bill');
1176 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
1177 'edate' => $self->bill, } );
1178 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
1181 =item last_cust_pkg_reason ACTION
1183 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
1184 Returns false if there is no reason or the package is not currenly ACTION'd
1185 ACTION is one of adjourn, susp, cancel, or expire.
1189 sub last_cust_pkg_reason {
1190 my ( $self, $action ) = ( shift, shift );
1191 my $date = $self->get($action);
1193 'table' => 'cust_pkg_reason',
1194 'hashref' => { 'pkgnum' => $self->pkgnum,
1195 'action' => substr(uc($action), 0, 1),
1198 'order_by' => 'ORDER BY num DESC LIMIT 1',
1202 =item last_reason ACTION
1204 Returns the most recent ACTION FS::reason associated with the package.
1205 Returns false if there is no reason or the package is not currenly ACTION'd
1206 ACTION is one of adjourn, susp, cancel, or expire.
1211 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
1212 $cust_pkg_reason->reason
1213 if $cust_pkg_reason;
1218 Returns the definition for this billing item, as an FS::part_pkg object (see
1225 #exists( $self->{'_pkgpart'} )
1227 ? $self->{'_pkgpart'}
1228 : qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
1233 Returns the cancelled package this package was changed from, if any.
1239 return '' unless $self->change_pkgnum;
1240 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
1245 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
1252 $self->part_pkg->calc_setup($self, @_);
1257 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
1264 $self->part_pkg->calc_recur($self, @_);
1269 Calls the I<calc_remain> of the FS::part_pkg object associated with this
1276 $self->part_pkg->calc_remain($self, @_);
1281 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
1288 $self->part_pkg->calc_cancel($self, @_);
1293 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
1299 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
1302 =item cust_pkg_detail [ DETAILTYPE ]
1304 Returns any customer package details for this package (see
1305 L<FS::cust_pkg_detail>).
1307 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
1311 sub cust_pkg_detail {
1313 my %hash = ( 'pkgnum' => $self->pkgnum );
1314 $hash{detailtype} = shift if @_;
1316 'table' => 'cust_pkg_detail',
1317 'hashref' => \%hash,
1318 'order_by' => 'ORDER BY weight, pkgdetailnum',
1322 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
1324 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
1326 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
1328 If there is an error, returns the error, otherwise returns false.
1332 sub set_cust_pkg_detail {
1333 my( $self, $detailtype, @details ) = @_;
1335 local $SIG{HUP} = 'IGNORE';
1336 local $SIG{INT} = 'IGNORE';
1337 local $SIG{QUIT} = 'IGNORE';
1338 local $SIG{TERM} = 'IGNORE';
1339 local $SIG{TSTP} = 'IGNORE';
1340 local $SIG{PIPE} = 'IGNORE';
1342 my $oldAutoCommit = $FS::UID::AutoCommit;
1343 local $FS::UID::AutoCommit = 0;
1346 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
1347 my $error = $current->delete;
1349 $dbh->rollback if $oldAutoCommit;
1350 return "error removing old detail: $error";
1354 foreach my $detail ( @details ) {
1355 my $cust_pkg_detail = new FS::cust_pkg_detail {
1356 'pkgnum' => $self->pkgnum,
1357 'detailtype' => $detailtype,
1358 'detail' => $detail,
1360 my $error = $cust_pkg_detail->insert;
1362 $dbh->rollback if $oldAutoCommit;
1363 return "error adding new detail: $error";
1368 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1375 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
1379 #false laziness w/cust_bill.pm
1383 'table' => 'cust_event',
1384 'addl_from' => 'JOIN part_event USING ( eventpart )',
1385 'hashref' => { 'tablenum' => $self->pkgnum },
1386 'extra_sql' => " AND eventtable = 'cust_pkg' ",
1390 =item num_cust_event
1392 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
1396 #false laziness w/cust_bill.pm
1397 sub num_cust_event {
1400 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
1401 " WHERE tablenum = ? AND eventtable = 'cust_pkg'";
1402 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
1403 $sth->execute($self->pkgnum) or die $sth->errstr. " executing $sql";
1404 $sth->fetchrow_arrayref->[0];
1407 =item cust_svc [ SVCPART ]
1409 Returns the services for this package, as FS::cust_svc objects (see
1410 L<FS::cust_svc>). If a svcpart is specified, return only the matching
1419 return qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum,
1420 'svcpart' => shift, } );
1423 #if ( $self->{'_svcnum'} ) {
1424 # values %{ $self->{'_svcnum'}->cache };
1426 $self->_sort_cust_svc(
1427 [ qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } ) ]
1433 =item overlimit [ SVCPART ]
1435 Returns the services for this package which have exceeded their
1436 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
1437 is specified, return only the matching services.
1443 grep { $_->overlimit } $self->cust_svc;
1446 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ]
1448 Returns historical services for this package created before END TIMESTAMP and
1449 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
1450 (see L<FS::h_cust_svc>).
1457 $self->_sort_cust_svc(
1458 [ qsearch( 'h_cust_svc',
1459 { 'pkgnum' => $self->pkgnum, },
1460 FS::h_cust_svc->sql_h_search(@_),
1466 sub _sort_cust_svc {
1467 my( $self, $arrayref ) = @_;
1470 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
1472 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $self->pkgpart,
1473 'svcpart' => $_->svcpart } );
1475 $pkg_svc ? $pkg_svc->primary_svc : '',
1476 $pkg_svc ? $pkg_svc->quantity : 0,
1483 =item num_cust_svc [ SVCPART ]
1485 Returns the number of provisioned services for this package. If a svcpart is
1486 specified, counts only the matching services.
1492 my $sql = 'SELECT COUNT(*) FROM cust_svc WHERE pkgnum = ?';
1493 $sql .= ' AND svcpart = ?' if @_;
1494 my $sth = dbh->prepare($sql) or die dbh->errstr;
1495 $sth->execute($self->pkgnum, @_) or die $sth->errstr;
1496 $sth->fetchrow_arrayref->[0];
1499 =item available_part_svc
1501 Returns a list of FS::part_svc objects representing services included in this
1502 package but not yet provisioned. Each FS::part_svc object also has an extra
1503 field, I<num_avail>, which specifies the number of available services.
1507 sub available_part_svc {
1509 grep { $_->num_avail > 0 }
1511 my $part_svc = $_->part_svc;
1512 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
1513 $_->quantity - $self->num_cust_svc($_->svcpart);
1516 $self->part_pkg->pkg_svc;
1521 Returns a list of FS::part_svc objects representing provisioned and available
1522 services included in this package. Each FS::part_svc object also has the
1523 following extra fields:
1527 =item num_cust_svc (count)
1529 =item num_avail (quantity - count)
1531 =item cust_pkg_svc (services) - array reference containing the provisioned services, as cust_svc objects
1534 label -> ($cust_svc->label)[1]
1543 #XXX some sort of sort order besides numeric by svcpart...
1544 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
1546 my $part_svc = $pkg_svc->part_svc;
1547 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
1548 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
1549 $part_svc->{'Hash'}{'num_avail'} =
1550 max( 0, $pkg_svc->quantity - $num_cust_svc );
1551 $part_svc->{'Hash'}{'cust_pkg_svc'} = [ $self->cust_svc($part_svc->svcpart) ];
1553 } $self->part_pkg->pkg_svc;
1556 push @part_svc, map {
1558 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
1559 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
1560 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
1561 $part_svc->{'Hash'}{'cust_pkg_svc'} = [ $self->cust_svc($part_svc->svcpart) ];
1563 } $self->extra_part_svc;
1569 =item extra_part_svc
1571 Returns a list of FS::part_svc objects corresponding to services in this
1572 package which are still provisioned but not (any longer) available in the
1577 sub extra_part_svc {
1580 my $pkgnum = $self->pkgnum;
1581 my $pkgpart = $self->pkgpart;
1584 # 'table' => 'part_svc',
1587 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
1588 # WHERE pkg_svc.svcpart = part_svc.svcpart
1589 # AND pkg_svc.pkgpart = ?
1592 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
1593 # LEFT JOIN cust_pkg USING ( pkgnum )
1594 # WHERE cust_svc.svcpart = part_svc.svcpart
1597 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
1600 #seems to benchmark slightly faster...
1602 'select' => 'DISTINCT ON (svcpart) part_svc.*',
1603 'table' => 'part_svc',
1605 'LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
1606 AND pkg_svc.pkgpart = ?
1609 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
1610 LEFT JOIN cust_pkg USING ( pkgnum )
1613 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
1614 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
1620 Returns a short status string for this package, currently:
1624 =item not yet billed
1626 =item one-time charge
1641 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
1643 return 'cancelled' if $self->get('cancel');
1644 return 'suspended' if $self->susp;
1645 return 'not yet billed' unless $self->setup;
1646 return 'one-time charge' if $freq =~ /^(0|$)/;
1652 Class method that returns the list of possible status strings for packages
1653 (see L<the status method|/status>). For example:
1655 @statuses = FS::cust_pkg->statuses();
1659 tie my %statuscolor, 'Tie::IxHash',
1660 'not yet billed' => '000000',
1661 'one-time charge' => '000000',
1662 'active' => '00CC00',
1663 'suspended' => 'FF9900',
1664 'cancelled' => 'FF0000',
1668 my $self = shift; #could be class...
1669 grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
1670 # mayble split btw one-time vs. recur
1676 Returns a hex triplet color string for this package's status.
1682 $statuscolor{$self->status};
1687 Returns a list of lists, calling the label method for all services
1688 (see L<FS::cust_svc>) of this billing item.
1694 map { [ $_->label ] } $self->cust_svc;
1697 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ]
1699 Like the labels method, but returns historical information on services that
1700 were active as of END_TIMESTAMP and (optionally) not cancelled before
1703 Returns a list of lists, calling the label method for all (historical) services
1704 (see L<FS::h_cust_svc>) of this billing item.
1710 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
1713 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
1715 Like h_labels, except returns a simple flat list, and shortens long
1716 (currently >5 or the cust_bill-max_same_services configuration value) lists of
1717 identical services to one line that lists the service label and the number of
1718 individual services rather than individual items.
1722 sub h_labels_short {
1725 my $conf = new FS::Conf;
1726 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
1729 #tie %labels, 'Tie::IxHash';
1730 push @{ $labels{$_->[0]} }, $_->[1]
1731 foreach $self->h_labels(@_);
1733 foreach my $label ( keys %labels ) {
1735 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
1736 my $num = scalar(@values);
1737 if ( $num > $max_same_services ) {
1738 push @labels, "$label ($num)";
1740 push @labels, map { "$label: $_" } @values;
1750 Returns the parent customer object (see L<FS::cust_main>).
1756 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
1761 Returns the location object, if any (see L<FS::cust_location>).
1767 return '' unless $self->locationnum;
1768 qsearchs( 'cust_location', { 'locationnum' => $self->locationnum } );
1771 =item cust_location_or_main
1773 If this package is associated with a location, returns the locaiton (see
1774 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
1778 sub cust_location_or_main {
1780 $self->cust_location || $self->cust_main;
1783 =item seconds_since TIMESTAMP
1785 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
1786 package have been online since TIMESTAMP, according to the session monitor.
1788 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1789 L<Time::Local> and L<Date::Parse> for conversion functions.
1794 my($self, $since) = @_;
1797 foreach my $cust_svc (
1798 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
1800 $seconds += $cust_svc->seconds_since($since);
1807 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
1809 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
1810 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
1813 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
1814 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
1820 sub seconds_since_sqlradacct {
1821 my($self, $start, $end) = @_;
1825 foreach my $cust_svc (
1827 my $part_svc = $_->part_svc;
1828 $part_svc->svcdb eq 'svc_acct'
1829 && scalar($part_svc->part_export('sqlradius'));
1832 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
1839 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
1841 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
1842 in this package for sessions ending between TIMESTAMP_START (inclusive) and
1846 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
1847 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
1852 sub attribute_since_sqlradacct {
1853 my($self, $start, $end, $attrib) = @_;
1857 foreach my $cust_svc (
1859 my $part_svc = $_->part_svc;
1860 $part_svc->svcdb eq 'svc_acct'
1861 && scalar($part_svc->part_export('sqlradius'));
1864 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
1876 my( $self, $value ) = @_;
1877 if ( defined($value) ) {
1878 $self->setfield('quantity', $value);
1880 $self->getfield('quantity') || 1;
1883 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
1885 Transfers as many services as possible from this package to another package.
1887 The destination package can be specified by pkgnum by passing an FS::cust_pkg
1888 object. The destination package must already exist.
1890 Services are moved only if the destination allows services with the correct
1891 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
1892 this option with caution! No provision is made for export differences
1893 between the old and new service definitions. Probably only should be used
1894 when your exports for all service definitions of a given svcdb are identical.
1895 (attempt a transfer without it first, to move all possible svcpart-matching
1898 Any services that can't be moved remain in the original package.
1900 Returns an error, if there is one; otherwise, returns the number of services
1901 that couldn't be moved.
1906 my ($self, $dest_pkgnum, %opt) = @_;
1912 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
1913 $dest = $dest_pkgnum;
1914 $dest_pkgnum = $dest->pkgnum;
1916 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
1919 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
1921 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
1922 $target{$pkg_svc->svcpart} = $pkg_svc->quantity;
1925 foreach my $cust_svc ($dest->cust_svc) {
1926 $target{$cust_svc->svcpart}--;
1929 my %svcpart2svcparts = ();
1930 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
1931 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
1932 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
1933 next if exists $svcpart2svcparts{$svcpart};
1934 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1935 $svcpart2svcparts{$svcpart} = [
1937 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
1939 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
1940 'svcpart' => $_ } );
1942 $pkg_svc ? $pkg_svc->primary_svc : '',
1943 $pkg_svc ? $pkg_svc->quantity : 0,
1947 grep { $_ != $svcpart }
1949 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
1951 warn "alternates for svcpart $svcpart: ".
1952 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
1957 foreach my $cust_svc ($self->cust_svc) {
1958 if($target{$cust_svc->svcpart} > 0) {
1959 $target{$cust_svc->svcpart}--;
1960 my $new = new FS::cust_svc { $cust_svc->hash };
1961 $new->pkgnum($dest_pkgnum);
1962 my $error = $new->replace($cust_svc);
1963 return $error if $error;
1964 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
1966 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
1967 warn "alternates to consider: ".
1968 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
1970 my @alternate = grep {
1971 warn "considering alternate svcpart $_: ".
1972 "$target{$_} available in new package\n"
1975 } @{$svcpart2svcparts{$cust_svc->svcpart}};
1977 warn "alternate(s) found\n" if $DEBUG;
1978 my $change_svcpart = $alternate[0];
1979 $target{$change_svcpart}--;
1980 my $new = new FS::cust_svc { $cust_svc->hash };
1981 $new->svcpart($change_svcpart);
1982 $new->pkgnum($dest_pkgnum);
1983 my $error = $new->replace($cust_svc);
1984 return $error if $error;
1997 This method is deprecated. See the I<depend_jobnum> option to the insert and
1998 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
2005 local $SIG{HUP} = 'IGNORE';
2006 local $SIG{INT} = 'IGNORE';
2007 local $SIG{QUIT} = 'IGNORE';
2008 local $SIG{TERM} = 'IGNORE';
2009 local $SIG{TSTP} = 'IGNORE';
2010 local $SIG{PIPE} = 'IGNORE';
2012 my $oldAutoCommit = $FS::UID::AutoCommit;
2013 local $FS::UID::AutoCommit = 0;
2016 foreach my $cust_svc ( $self->cust_svc ) {
2017 #false laziness w/svc_Common::insert
2018 my $svc_x = $cust_svc->svc_x;
2019 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
2020 my $error = $part_export->export_insert($svc_x);
2022 $dbh->rollback if $oldAutoCommit;
2028 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2035 =head1 CLASS METHODS
2041 Returns an SQL expression identifying recurring packages.
2045 sub recurring_sql { "
2046 '0' != ( select freq from part_pkg
2047 where cust_pkg.pkgpart = part_pkg.pkgpart )
2052 Returns an SQL expression identifying one-time packages.
2057 '0' = ( select freq from part_pkg
2058 where cust_pkg.pkgpart = part_pkg.pkgpart )
2063 Returns an SQL expression identifying active packages.
2068 ". $_[0]->recurring_sql(). "
2069 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2070 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
2075 Returns an SQL expression identifying inactive packages (one-time packages
2076 that are otherwise unsuspended/uncancelled).
2080 sub inactive_sql { "
2081 ". $_[0]->onetime_sql(). "
2082 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2083 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
2089 Returns an SQL expression identifying suspended packages.
2093 sub suspended_sql { susp_sql(@_); }
2095 #$_[0]->recurring_sql(). ' AND '.
2097 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2098 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
2105 Returns an SQL exprression identifying cancelled packages.
2109 sub cancelled_sql { cancel_sql(@_); }
2111 #$_[0]->recurring_sql(). ' AND '.
2112 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
2115 =item search_sql HASHREF
2119 Returns a qsearch hash expression to search for parameters specified in HASHREF.
2120 Valid parameters are
2128 active, inactive, suspended, cancel (or cancelled)
2132 active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
2142 arrayref of beginning and ending epoch date
2146 arrayref of beginning and ending epoch date
2150 arrayref of beginning and ending epoch date
2154 arrayref of beginning and ending epoch date
2158 arrayref of beginning and ending epoch date
2162 arrayref of beginning and ending epoch date
2166 arrayref of beginning and ending epoch date
2170 pkgnum or APKG_pkgnum
2174 a value suited to passing to FS::UI::Web::cust_header
2178 specifies the user for agent virtualization
2185 my ($class, $params) = @_;
2192 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
2194 "cust_main.agentnum = $1";
2201 if ( $params->{'magic'} eq 'active'
2202 || $params->{'status'} eq 'active' ) {
2204 push @where, FS::cust_pkg->active_sql();
2206 } elsif ( $params->{'magic'} eq 'inactive'
2207 || $params->{'status'} eq 'inactive' ) {
2209 push @where, FS::cust_pkg->inactive_sql();
2211 } elsif ( $params->{'magic'} eq 'suspended'
2212 || $params->{'status'} eq 'suspended' ) {
2214 push @where, FS::cust_pkg->suspended_sql();
2216 } elsif ( $params->{'magic'} =~ /^cancell?ed$/
2217 || $params->{'status'} =~ /^cancell?ed$/ ) {
2219 push @where, FS::cust_pkg->cancelled_sql();
2221 } elsif ( $params->{'status'} =~ /^(one-time charge|inactive)$/ ) {
2223 push @where, FS::cust_pkg->inactive_sql();
2228 # parse package class
2231 #false lazinessish w/graph/cust_bill_pkg.cgi
2234 if ( exists($params->{'classnum'})
2235 && $params->{'classnum'} =~ /^(\d*)$/
2239 if ( $classnum ) { #a specific class
2240 push @where, "classnum = $classnum";
2242 #@pkg_class = ( qsearchs('pkg_class', { 'classnum' => $classnum } ) );
2243 #die "classnum $classnum not found!" unless $pkg_class[0];
2244 #$title .= $pkg_class[0]->classname.' ';
2246 } elsif ( $classnum eq '' ) { #the empty class
2248 push @where, "classnum IS NULL";
2249 #$title .= 'Empty class ';
2250 #@pkg_class = ( '(empty class)' );
2251 } elsif ( $classnum eq '0' ) {
2252 #@pkg_class = qsearch('pkg_class', {} ); # { 'disabled' => '' } );
2253 #push @pkg_class, '(empty class)';
2255 die "illegal classnum";
2264 my $pkgpart = join (' OR pkgpart=',
2265 grep {$_} map { /^(\d+)$/; } ($params->{'pkgpart'}));
2266 push @where, '(pkgpart=' . $pkgpart . ')' if $pkgpart;
2274 #false laziness w/report_cust_pkg.html
2277 'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
2278 'active' => { 'susp'=>1, 'cancel'=>1 },
2279 'suspended' => { 'cancel' => 1 },
2284 foreach my $field (qw( setup last_bill bill adjourn susp expire cancel )) {
2286 next unless exists($params->{$field});
2288 my($beginning, $ending) = @{$params->{$field}};
2290 next if $beginning == 0 && $ending == 4294967295;
2293 "cust_pkg.$field IS NOT NULL",
2294 "cust_pkg.$field >= $beginning",
2295 "cust_pkg.$field <= $ending";
2297 $orderby ||= "ORDER BY cust_pkg.$field";
2301 $orderby ||= 'ORDER BY bill';
2304 # parse magic, legacy, etc.
2307 if ( $params->{'magic'} &&
2308 $params->{'magic'} =~ /^(active|inactive|suspended|cancell?ed)$/
2311 $orderby = 'ORDER BY pkgnum';
2313 if ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
2314 push @where, "pkgpart = $1";
2317 } elsif ( $params->{'query'} eq 'pkgnum' ) {
2319 $orderby = 'ORDER BY pkgnum';
2321 } elsif ( $params->{'query'} eq 'APKG_pkgnum' ) {
2323 $orderby = 'ORDER BY pkgnum';
2326 SELECT count(*) FROM pkg_svc
2327 WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
2328 AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
2329 WHERE cust_svc.pkgnum = cust_pkg.pkgnum
2330 AND cust_svc.svcpart = pkg_svc.svcpart
2337 # setup queries, links, subs, etc. for the search
2340 # here is the agent virtualization
2341 if ($params->{CurrentUser}) {
2343 qsearchs('access_user', { username => $params->{CurrentUser} });
2346 push @where, $access_user->agentnums_sql('table'=>'cust_main');
2351 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql('table'=>'cust_main');
2354 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
2356 my $addl_from = 'LEFT JOIN cust_main USING ( custnum ) '.
2357 'LEFT JOIN part_pkg USING ( pkgpart ) '.
2358 'LEFT JOIN pkg_class USING ( classnum ) ';
2360 my $count_query = "SELECT COUNT(*) FROM cust_pkg $addl_from $extra_sql";
2363 'table' => 'cust_pkg',
2365 'select' => join(', ',
2367 ( map "part_pkg.$_", qw( pkg freq ) ),
2368 'pkg_class.classname',
2369 'cust_main.custnum as cust_main_custnum',
2370 FS::UI::Web::cust_sql_fields(
2371 $params->{'cust_fields'}
2374 'extra_sql' => "$extra_sql $orderby",
2375 'addl_from' => $addl_from,
2376 'count_query' => $count_query,
2383 Returns a list: the first item is an SQL fragment identifying matching
2384 packages/customers via location (taking into account shipping and package
2385 address taxation, if enabled), and subsequent items are the parameters to
2386 substitute for the placeholders in that fragment.
2391 my($class, %opt) = @_;
2392 my $ornull = $opt{'ornull'};
2394 my $conf = new FS::Conf;
2396 # '?' placeholders in _location_sql_where
2399 @bill_param = qw( county county state state state country );
2401 @bill_param = qw( county state state country );
2403 unshift @bill_param, 'county'; # unless $nec;
2407 if ( $conf->exists('tax-ship_address') ) {
2410 ( ( ship_last IS NULL OR ship_last = '' )
2411 AND ". _location_sql_where('cust_main', '', $ornull ). "
2413 OR ( ship_last IS NOT NULL AND ship_last != ''
2414 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
2417 # AND payby != 'COMP'
2419 @main_param = ( @bill_param, @bill_param );
2423 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
2424 @main_param = @bill_param;
2430 if ( $conf->exists('tax-pkg_address') ) {
2432 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
2435 ( cust_pkg.locationnum IS NULL AND $main_where )
2436 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
2439 @param = ( @main_param, @bill_param );
2443 $where = $main_where;
2444 @param = @main_param;
2452 #subroutine, helper for location_sql
2453 sub _location_sql_where {
2455 my $prefix = @_ ? shift : '';
2456 my $ornull = @_ ? shift : '';
2458 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
2460 $ornull = $ornull ? ' OR ? IS NULL ' : '';
2462 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL ) ";
2463 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL ) ";
2466 ( $table.${prefix}county = ? $or_empty_county $ornull )
2467 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
2468 AND $table.${prefix}country = ?
2476 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
2478 CUSTNUM is a customer (see L<FS::cust_main>)
2480 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
2481 L<FS::part_pkg>) to order for this customer. Duplicates are of course
2484 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
2485 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
2486 new billing items. An error is returned if this is not possible (see
2487 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
2490 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
2491 newly-created cust_pkg objects.
2493 REFNUM, if specified, will specify the FS::pkg_referral record to be created
2494 and inserted. Multiple FS::pkg_referral records can be created by
2495 setting I<refnum> to an array reference of refnums or a hash reference with
2496 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
2497 record will be created corresponding to cust_main.refnum.
2502 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
2504 my $conf = new FS::Conf;
2506 # Transactionize this whole mess
2507 local $SIG{HUP} = 'IGNORE';
2508 local $SIG{INT} = 'IGNORE';
2509 local $SIG{QUIT} = 'IGNORE';
2510 local $SIG{TERM} = 'IGNORE';
2511 local $SIG{TSTP} = 'IGNORE';
2512 local $SIG{PIPE} = 'IGNORE';
2514 my $oldAutoCommit = $FS::UID::AutoCommit;
2515 local $FS::UID::AutoCommit = 0;
2519 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
2520 # return "Customer not found: $custnum" unless $cust_main;
2522 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
2525 my $change = scalar(@old_cust_pkg) != 0;
2528 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
2530 my $err_or_cust_pkg =
2531 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
2532 'refnum' => $refnum,
2535 unless (ref($err_or_cust_pkg)) {
2536 $dbh->rollback if $oldAutoCommit;
2537 return $err_or_cust_pkg;
2540 push @$return_cust_pkg, $err_or_cust_pkg;
2545 # Create the new packages.
2546 foreach my $pkgpart (@$pkgparts) {
2547 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
2548 pkgpart => $pkgpart,
2552 $error = $cust_pkg->insert( 'change' => $change );
2554 $dbh->rollback if $oldAutoCommit;
2557 push @$return_cust_pkg, $cust_pkg;
2559 # $return_cust_pkg now contains refs to all of the newly
2562 # Transfer services and cancel old packages.
2563 foreach my $old_pkg (@old_cust_pkg) {
2565 foreach my $new_pkg (@$return_cust_pkg) {
2566 $error = $old_pkg->transfer($new_pkg);
2567 if ($error and $error == 0) {
2568 # $old_pkg->transfer failed.
2569 $dbh->rollback if $oldAutoCommit;
2574 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
2575 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
2576 foreach my $new_pkg (@$return_cust_pkg) {
2577 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
2578 if ($error and $error == 0) {
2579 # $old_pkg->transfer failed.
2580 $dbh->rollback if $oldAutoCommit;
2587 # Transfers were successful, but we went through all of the
2588 # new packages and still had services left on the old package.
2589 # We can't cancel the package under the circumstances, so abort.
2590 $dbh->rollback if $oldAutoCommit;
2591 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
2593 $error = $old_pkg->cancel( quiet=>1 );
2599 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2603 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
2605 A bulk change method to change packages for multiple customers.
2607 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
2608 L<FS::part_pkg>) to order for each customer. Duplicates are of course
2611 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
2612 replace. The services (see L<FS::cust_svc>) are moved to the
2613 new billing items. An error is returned if this is not possible (see
2616 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
2617 newly-created cust_pkg objects.
2622 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
2624 # Transactionize this whole mess
2625 local $SIG{HUP} = 'IGNORE';
2626 local $SIG{INT} = 'IGNORE';
2627 local $SIG{QUIT} = 'IGNORE';
2628 local $SIG{TERM} = 'IGNORE';
2629 local $SIG{TSTP} = 'IGNORE';
2630 local $SIG{PIPE} = 'IGNORE';
2632 my $oldAutoCommit = $FS::UID::AutoCommit;
2633 local $FS::UID::AutoCommit = 0;
2637 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
2640 while(scalar(@old_cust_pkg)) {
2642 my $custnum = $old_cust_pkg[0]->custnum;
2643 my (@remove) = map { $_->pkgnum }
2644 grep { $_->custnum == $custnum } @old_cust_pkg;
2645 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
2647 my $error = order $custnum, $pkgparts, \@remove, \@return;
2649 push @errors, $error
2651 push @$return_cust_pkg, @return;
2654 if (scalar(@errors)) {
2655 $dbh->rollback if $oldAutoCommit;
2656 return join(' / ', @errors);
2659 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2665 Associates this package with a (suspension or cancellation) reason (see
2666 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
2669 Available options are:
2675 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.
2679 the access_user (see L<FS::access_user>) providing the reason
2687 the action (cancel, susp, adjourn, expire) associated with the reason
2691 If there is an error, returns the error, otherwise returns false.
2696 my ($self, %options) = @_;
2698 my $otaker = $options{reason_otaker} ||
2699 $FS::CurrentUser::CurrentUser->username;
2702 if ( $options{'reason'} =~ /^(\d+)$/ ) {
2706 } elsif ( ref($options{'reason'}) ) {
2708 return 'Enter a new reason (or select an existing one)'
2709 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
2711 my $reason = new FS::reason({
2712 'reason_type' => $options{'reason'}->{'typenum'},
2713 'reason' => $options{'reason'}->{'reason'},
2715 my $error = $reason->insert;
2716 return $error if $error;
2718 $reasonnum = $reason->reasonnum;
2721 return "Unparsable reason: ". $options{'reason'};
2724 my $cust_pkg_reason =
2725 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
2726 'reasonnum' => $reasonnum,
2727 'otaker' => $otaker,
2728 'action' => substr(uc($options{'action'}),0,1),
2729 'date' => $options{'date'}
2734 $cust_pkg_reason->insert;
2737 =item set_usage USAGE_VALUE_HASHREF
2739 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
2740 to which they should be set (see L<FS::svc_acct>). Currently seconds,
2741 upbytes, downbytes, and totalbytes are appropriate keys.
2743 All svc_accts which are part of this package have their values reset.
2748 my ($self, $valueref, %opt) = @_;
2750 foreach my $cust_svc ($self->cust_svc){
2751 my $svc_x = $cust_svc->svc_x;
2752 $svc_x->set_usage($valueref, %opt)
2753 if $svc_x->can("set_usage");
2757 =item recharge USAGE_VALUE_HASHREF
2759 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
2760 to which they should be set (see L<FS::svc_acct>). Currently seconds,
2761 upbytes, downbytes, and totalbytes are appropriate keys.
2763 All svc_accts which are part of this package have their values incremented.
2768 my ($self, $valueref) = @_;
2770 foreach my $cust_svc ($self->cust_svc){
2771 my $svc_x = $cust_svc->svc_x;
2772 $svc_x->recharge($valueref)
2773 if $svc_x->can("recharge");
2781 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
2783 In sub order, the @pkgparts array (passed by reference) is clobbered.
2785 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
2786 method to pass dates to the recur_prog expression, it should do so.
2788 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
2789 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
2790 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
2791 configuration values. Probably need a subroutine which decides what to do
2792 based on whether or not we've fetched the user yet, rather than a hash. See
2793 FS::UID and the TODO.
2795 Now that things are transactional should the check in the insert method be
2800 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
2801 L<FS::pkg_svc>, schema.html from the base documentation