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(min max);
11 use Time::Local qw( timelocal timelocal_nocheck );
13 use FS::UID qw( getotaker dbh driver_name );
14 use FS::Misc qw( send_email );
15 use FS::Record qw( qsearch qsearchs fields );
20 use FS::cust_location;
22 use FS::cust_bill_pkg;
23 use FS::cust_pkg_detail;
24 use FS::cust_pkg_usage;
25 use FS::cdr_cust_pkg_usage;
30 use FS::cust_pkg_reason;
32 use FS::cust_pkg_discount;
37 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
39 # because they load configuration by setting FS::UID::callback (see TODO)
45 # for sending cancel emails in sub cancel
49 $me = '[FS::cust_pkg]';
51 $disable_agentcheck = 0;
55 my ( $hashref, $cache ) = @_;
56 #if ( $hashref->{'pkgpart'} ) {
57 if ( $hashref->{'pkg'} ) {
58 # #@{ $self->{'_pkgnum'} } = ();
59 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
60 # $self->{'_pkgpart'} = $subcache;
61 # #push @{ $self->{'_pkgnum'} },
62 # FS::part_pkg->new_or_cached($hashref, $subcache);
63 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
65 if ( exists $hashref->{'svcnum'} ) {
66 #@{ $self->{'_pkgnum'} } = ();
67 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
68 $self->{'_svcnum'} = $subcache;
69 #push @{ $self->{'_pkgnum'} },
70 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
76 FS::cust_pkg - Object methods for cust_pkg objects
82 $record = new FS::cust_pkg \%hash;
83 $record = new FS::cust_pkg { 'column' => 'value' };
85 $error = $record->insert;
87 $error = $new_record->replace($old_record);
89 $error = $record->delete;
91 $error = $record->check;
93 $error = $record->cancel;
95 $error = $record->suspend;
97 $error = $record->unsuspend;
99 $part_pkg = $record->part_pkg;
101 @labels = $record->labels;
103 $seconds = $record->seconds_since($timestamp);
105 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
106 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
110 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
111 inherits from FS::Record. The following fields are currently supported:
117 Primary key (assigned automatically for new billing items)
121 Customer (see L<FS::cust_main>)
125 Billing item definition (see L<FS::part_pkg>)
129 Optional link to package location (see L<FS::location>)
133 date package was ordered (also remains same on changes)
145 date (next bill date)
173 order taker (see L<FS::access_user>)
177 If this field is set to 1, disables the automatic
178 unsuspension of this package when using the B<unsuspendauto> config option.
182 If not set, defaults to 1
186 Date of change from previous package
196 =item change_locationnum
204 The pkgnum of the package that this package is supplemental to, if any.
208 The package link (L<FS::part_pkg_link>) that defines this supplemental
209 package, if it is one.
213 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
214 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
215 L<Time::Local> and L<Date::Parse> for conversion functions.
223 Create a new billing item. To add the item to the database, see L<"insert">.
227 sub table { 'cust_pkg'; }
228 sub cust_linked { $_[0]->cust_main_custnum; }
229 sub cust_unlinked_msg {
231 "WARNING: can't find cust_main.custnum ". $self->custnum.
232 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
235 =item insert [ OPTION => VALUE ... ]
237 Adds this billing item to the database ("Orders" the item). If there is an
238 error, returns the error, otherwise returns false.
240 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
241 will be used to look up the package definition and agent restrictions will be
244 If the additional field I<refnum> is defined, an FS::pkg_referral record will
245 be created and inserted. Multiple FS::pkg_referral records can be created by
246 setting I<refnum> to an array reference of refnums or a hash reference with
247 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
248 record will be created corresponding to cust_main.refnum.
250 The following options are available:
256 If set true, supresses any referral credit to a referring customer.
260 cust_pkg_option records will be created
264 a ticket will be added to this customer with this subject
268 an optional queue name for ticket additions
275 my( $self, %options ) = @_;
277 my $error = $self->check_pkgpart;
278 return $error if $error;
280 my $part_pkg = $self->part_pkg;
282 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
283 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
284 $mon += 1 unless $mday == 1;
285 until ( $mon < 12 ) { $mon -= 12; $year++; }
286 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
289 foreach my $action ( qw(expire adjourn contract_end) ) {
290 my $months = $part_pkg->option("${action}_months",1);
291 if($months and !$self->$action) {
292 my $start = $self->start_date || $self->setup || time;
293 $self->$action( $part_pkg->add_freq($start, $months) );
297 my $free_days = $part_pkg->option('free_days',1);
298 if ( $free_days && $part_pkg->option('delay_setup',1) ) { #&& !$self->start_date
299 my ($mday,$mon,$year) = (localtime(time) )[3,4,5];
300 #my $start_date = ($self->start_date || timelocal(0,0,0,$mday,$mon,$year)) + 86400 * $free_days;
301 my $start_date = timelocal(0,0,0,$mday,$mon,$year) + 86400 * $free_days;
302 $self->start_date($start_date);
305 $self->order_date(time);
307 local $SIG{HUP} = 'IGNORE';
308 local $SIG{INT} = 'IGNORE';
309 local $SIG{QUIT} = 'IGNORE';
310 local $SIG{TERM} = 'IGNORE';
311 local $SIG{TSTP} = 'IGNORE';
312 local $SIG{PIPE} = 'IGNORE';
314 my $oldAutoCommit = $FS::UID::AutoCommit;
315 local $FS::UID::AutoCommit = 0;
318 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
320 $dbh->rollback if $oldAutoCommit;
324 $self->refnum($self->cust_main->refnum) unless $self->refnum;
325 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
326 $self->process_m2m( 'link_table' => 'pkg_referral',
327 'target_table' => 'part_referral',
328 'params' => $self->refnum,
331 if ( $self->discountnum ) {
332 my $error = $self->insert_discount();
334 $dbh->rollback if $oldAutoCommit;
339 #if ( $self->reg_code ) {
340 # my $reg_code = qsearchs('reg_code', { 'code' => $self->reg_code } );
341 # $error = $reg_code->delete;
343 # $dbh->rollback if $oldAutoCommit;
348 my $conf = new FS::Conf;
350 if ( $conf->config('ticket_system') && $options{ticket_subject} ) {
352 #this init stuff is still inefficient, but at least its limited to
353 # the small number (any?) folks using ticket emailing on pkg order
356 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
363 use FS::TicketSystem;
364 FS::TicketSystem->init();
366 my $q = new RT::Queue($RT::SystemUser);
367 $q->Load($options{ticket_queue}) if $options{ticket_queue};
368 my $t = new RT::Ticket($RT::SystemUser);
369 my $mime = new MIME::Entity;
370 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
371 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
372 Subject => $options{ticket_subject},
375 $t->AddLink( Type => 'MemberOf',
376 Target => 'freeside://freeside/cust_main/'. $self->custnum,
380 if ($conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
381 my $queue = new FS::queue {
382 'job' => 'FS::cust_main::queueable_print',
384 $error = $queue->insert(
385 'custnum' => $self->custnum,
386 'template' => 'welcome_letter',
390 warn "can't send welcome letter: $error";
395 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
402 This method now works but you probably shouldn't use it.
404 You don't want to delete packages, because there would then be no record
405 the customer ever purchased the package. Instead, see the cancel method and
406 hide cancelled packages.
413 local $SIG{HUP} = 'IGNORE';
414 local $SIG{INT} = 'IGNORE';
415 local $SIG{QUIT} = 'IGNORE';
416 local $SIG{TERM} = 'IGNORE';
417 local $SIG{TSTP} = 'IGNORE';
418 local $SIG{PIPE} = 'IGNORE';
420 my $oldAutoCommit = $FS::UID::AutoCommit;
421 local $FS::UID::AutoCommit = 0;
424 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
425 my $error = $cust_pkg_discount->delete;
427 $dbh->rollback if $oldAutoCommit;
431 #cust_bill_pkg_discount?
433 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
434 my $error = $cust_pkg_detail->delete;
436 $dbh->rollback if $oldAutoCommit;
441 foreach my $cust_pkg_reason (
443 'table' => 'cust_pkg_reason',
444 'hashref' => { 'pkgnum' => $self->pkgnum },
448 my $error = $cust_pkg_reason->delete;
450 $dbh->rollback if $oldAutoCommit;
457 my $error = $self->SUPER::delete(@_);
459 $dbh->rollback if $oldAutoCommit;
463 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
469 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
471 Replaces the OLD_RECORD with this one in the database. If there is an error,
472 returns the error, otherwise returns false.
474 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
476 Changing pkgpart may have disasterous effects. See the order subroutine.
478 setup and bill are normally updated by calling the bill method of a customer
479 object (see L<FS::cust_main>).
481 suspend is normally updated by the suspend and unsuspend methods.
483 cancel is normally updated by the cancel method (and also the order subroutine
486 Available options are:
492 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.
496 the access_user (see L<FS::access_user>) providing the reason
500 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
509 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
514 ( ref($_[0]) eq 'HASH' )
518 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
519 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
522 #return "Can't change setup once it exists!"
523 # if $old->getfield('setup') &&
524 # $old->getfield('setup') != $new->getfield('setup');
526 #some logic for bill, susp, cancel?
528 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
530 local $SIG{HUP} = 'IGNORE';
531 local $SIG{INT} = 'IGNORE';
532 local $SIG{QUIT} = 'IGNORE';
533 local $SIG{TERM} = 'IGNORE';
534 local $SIG{TSTP} = 'IGNORE';
535 local $SIG{PIPE} = 'IGNORE';
537 my $oldAutoCommit = $FS::UID::AutoCommit;
538 local $FS::UID::AutoCommit = 0;
541 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
542 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
543 my $error = $new->insert_reason(
544 'reason' => $options->{'reason'},
545 'date' => $new->$method,
547 'reason_otaker' => $options->{'reason_otaker'},
550 dbh->rollback if $oldAutoCommit;
551 return "Error inserting cust_pkg_reason: $error";
556 #save off and freeze RADIUS attributes for any associated svc_acct records
558 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
560 #also check for specific exports?
561 # to avoid spurious modify export events
562 @svc_acct = map { $_->svc_x }
563 grep { $_->part_svc->svcdb eq 'svc_acct' }
566 $_->snapshot foreach @svc_acct;
570 my $error = $new->SUPER::replace($old,
571 $options->{options} ? $options->{options} : ()
574 $dbh->rollback if $oldAutoCommit;
578 #for prepaid packages,
579 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
580 foreach my $old_svc_acct ( @svc_acct ) {
581 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
583 $new_svc_acct->replace( $old_svc_acct,
584 'depend_jobnum' => $options->{depend_jobnum},
587 $dbh->rollback if $oldAutoCommit;
592 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
599 Checks all fields to make sure this is a valid billing item. If there is an
600 error, returns the error, otherwise returns false. Called by the insert and
608 if ( !$self->locationnum or $self->locationnum == -1 ) {
609 $self->set('locationnum', $self->cust_main->ship_locationnum);
613 $self->ut_numbern('pkgnum')
614 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
615 || $self->ut_numbern('pkgpart')
616 || $self->check_pkgpart
617 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
618 || $self->ut_numbern('start_date')
619 || $self->ut_numbern('setup')
620 || $self->ut_numbern('bill')
621 || $self->ut_numbern('susp')
622 || $self->ut_numbern('cancel')
623 || $self->ut_numbern('adjourn')
624 || $self->ut_numbern('resume')
625 || $self->ut_numbern('expire')
626 || $self->ut_numbern('dundate')
627 || $self->ut_enum('no_auto', [ '', 'Y' ])
628 || $self->ut_enum('waive_setup', [ '', 'Y' ])
629 || $self->ut_numbern('agent_pkgid')
630 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
631 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
632 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
633 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
635 return $error if $error;
637 return "A package with both start date (future start) and setup date (already started) will never bill"
638 if $self->start_date && $self->setup;
640 return "A future unsuspend date can only be set for a package with a suspend date"
641 if $self->resume and !$self->susp and !$self->adjourn;
643 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
645 if ( $self->dbdef_table->column('manual_flag') ) {
646 $self->manual_flag('') if $self->manual_flag eq ' ';
647 $self->manual_flag =~ /^([01]?)$/
648 or return "Illegal manual_flag ". $self->manual_flag;
649 $self->manual_flag($1);
662 my $error = $self->ut_numbern('pkgpart');
663 return $error if $error;
665 if ( $self->reg_code ) {
667 unless ( grep { $self->pkgpart == $_->pkgpart }
668 map { $_->reg_code_pkg }
669 qsearchs( 'reg_code', { 'code' => $self->reg_code,
670 'agentnum' => $self->cust_main->agentnum })
672 return "Unknown registration code";
675 } elsif ( $self->promo_code ) {
678 qsearchs('part_pkg', {
679 'pkgpart' => $self->pkgpart,
680 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
682 return 'Unknown promotional code' unless $promo_part_pkg;
686 unless ( $disable_agentcheck ) {
688 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
689 return "agent ". $agent->agentnum. ':'. $agent->agent.
690 " can't purchase pkgpart ". $self->pkgpart
691 unless $agent->pkgpart_hashref->{ $self->pkgpart }
692 || $agent->agentnum == $self->part_pkg->agentnum;
695 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
696 return $error if $error;
704 =item cancel [ OPTION => VALUE ... ]
706 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
707 in this package, then cancels the package itself (sets the cancel field to
710 Available options are:
714 =item quiet - can be set true to supress email cancellation notices.
716 =item time - can be set to cancel the package based on a specific future or
717 historical date. Using time ensures that the remaining amount is calculated
718 correctly. Note however that this is an immediate cancel and just changes
719 the date. You are PROBABLY looking to expire the account instead of using
722 =item reason - can be set to a cancellation reason (see L<FS:reason>),
723 either a reasonnum of an existing reason, or passing a hashref will create
724 a new reason. The hashref should have the following keys: typenum - Reason
725 type (see L<FS::reason_type>, reason - Text of the new reason.
727 =item date - can be set to a unix style timestamp to specify when to
730 =item nobill - can be set true to skip billing if it might otherwise be done.
732 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
733 not credit it. This must be set (by change()) when changing the package
734 to a different pkgpart or location, and probably shouldn't be in any other
735 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
740 If there is an error, returns the error, otherwise returns false.
745 my( $self, %options ) = @_;
748 # pass all suspend/cancel actions to the main package
749 if ( $self->main_pkgnum and !$options{'from_main'} ) {
750 return $self->main_pkg->cancel(%options);
753 my $conf = new FS::Conf;
755 warn "cust_pkg::cancel called with options".
756 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
759 local $SIG{HUP} = 'IGNORE';
760 local $SIG{INT} = 'IGNORE';
761 local $SIG{QUIT} = 'IGNORE';
762 local $SIG{TERM} = 'IGNORE';
763 local $SIG{TSTP} = 'IGNORE';
764 local $SIG{PIPE} = 'IGNORE';
766 my $oldAutoCommit = $FS::UID::AutoCommit;
767 local $FS::UID::AutoCommit = 0;
770 my $old = $self->select_for_update;
772 if ( $old->get('cancel') || $self->get('cancel') ) {
773 dbh->rollback if $oldAutoCommit;
774 return ""; # no error
777 # XXX possibly set cancel_time to the expire date?
778 my $cancel_time = $options{'time'} || time;
779 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
780 $date = '' if ($date && $date <= $cancel_time); # complain instead?
782 #race condition: usage could be ongoing until unprovisioned
783 #resolved by performing a change package instead (which unprovisions) and
785 if ( !$options{nobill} && !$date ) {
786 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
787 my $copy = $self->new({$self->hash});
789 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
791 'time' => $cancel_time );
792 warn "Error billing during cancel, custnum ".
793 #$self->cust_main->custnum. ": $error"
798 if ( $options{'reason'} ) {
799 $error = $self->insert_reason( 'reason' => $options{'reason'},
800 'action' => $date ? 'expire' : 'cancel',
801 'date' => $date ? $date : $cancel_time,
802 'reason_otaker' => $options{'reason_otaker'},
805 dbh->rollback if $oldAutoCommit;
806 return "Error inserting cust_pkg_reason: $error";
810 my %svc_cancel_opt = ();
811 $svc_cancel_opt{'date'} = $date if $date;
812 foreach my $cust_svc (
815 sort { $a->[1] <=> $b->[1] }
816 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
817 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
819 my $part_svc = $cust_svc->part_svc;
820 next if ( defined($part_svc) and $part_svc->preserve );
821 my $error = $cust_svc->cancel( %svc_cancel_opt );
824 $dbh->rollback if $oldAutoCommit;
825 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
831 # credit remaining time if appropriate
833 if ( exists($options{'unused_credit'}) ) {
834 $do_credit = $options{'unused_credit'};
837 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
840 my $error = $self->credit_remaining('cancel', $cancel_time);
842 $dbh->rollback if $oldAutoCommit;
849 my %hash = $self->hash;
850 $date ? ($hash{'expire'} = $date) : ($hash{'cancel'} = $cancel_time);
851 my $new = new FS::cust_pkg ( \%hash );
852 $error = $new->replace( $self, options => { $self->options } );
854 $dbh->rollback if $oldAutoCommit;
858 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
859 $error = $supp_pkg->cancel(%options, 'from_main' => 1);
861 $dbh->rollback if $oldAutoCommit;
862 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
866 foreach my $usage ( $self->cust_pkg_usage ) {
867 $error = $usage->delete;
869 $dbh->rollback if $oldAutoCommit;
870 return "deleting usage pools: $error";
874 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
875 return '' if $date; #no errors
877 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
878 if ( !$options{'quiet'} &&
879 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
881 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
884 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
885 $error = $msg_template->send( 'cust_main' => $self->cust_main,
890 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
891 'to' => \@invoicing_list,
892 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
893 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
896 #should this do something on errors?
903 =item cancel_if_expired [ NOW_TIMESTAMP ]
905 Cancels this package if its expire date has been reached.
909 sub cancel_if_expired {
911 my $time = shift || time;
912 return '' unless $self->expire && $self->expire <= $time;
913 my $error = $self->cancel;
915 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
916 $self->custnum. ": $error";
923 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
924 locationnum, (other fields?). Attempts to re-provision cancelled services
925 using history information (errors at this stage are not fatal).
927 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
929 svc_fatal: service provisioning errors are fatal
931 svc_errors: pass an array reference, will be filled in with any provisioning errors
933 main_pkgnum: link the package as a supplemental package of this one. For
939 my( $self, %options ) = @_;
941 #in case you try do do $uncancel-date = $cust_pkg->uncacel
942 return '' unless $self->get('cancel');
944 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
945 return $self->main_pkg->uncancel(%options);
952 local $SIG{HUP} = 'IGNORE';
953 local $SIG{INT} = 'IGNORE';
954 local $SIG{QUIT} = 'IGNORE';
955 local $SIG{TERM} = 'IGNORE';
956 local $SIG{TSTP} = 'IGNORE';
957 local $SIG{PIPE} = 'IGNORE';
959 my $oldAutoCommit = $FS::UID::AutoCommit;
960 local $FS::UID::AutoCommit = 0;
964 # insert the new package
967 my $cust_pkg = new FS::cust_pkg {
968 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
969 bill => ( $options{'bill'} || $self->get('bill') ),
971 uncancel_pkgnum => $self->pkgnum,
972 main_pkgnum => ($options{'main_pkgnum'} || ''),
973 map { $_ => $self->get($_) } qw(
974 custnum pkgpart locationnum
976 susp adjourn resume expire start_date contract_end dundate
977 change_date change_pkgpart change_locationnum
978 manual_flag no_auto quantity agent_pkgid recur_show_zero setup_show_zero
982 my $error = $cust_pkg->insert(
983 'change' => 1, #supresses any referral credit to a referring customer
986 $dbh->rollback if $oldAutoCommit;
994 #find historical services within this timeframe before the package cancel
995 # (incompatible with "time" option to cust_pkg->cancel?)
996 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
997 # too little? (unprovisioing export delay?)
998 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
999 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1002 foreach my $h_cust_svc (@h_cust_svc) {
1003 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1004 #next unless $h_svc_x; #should this happen?
1005 (my $table = $h_svc_x->table) =~ s/^h_//;
1006 require "FS/$table.pm";
1007 my $class = "FS::$table";
1008 my $svc_x = $class->new( {
1009 'pkgnum' => $cust_pkg->pkgnum,
1010 'svcpart' => $h_cust_svc->svcpart,
1011 map { $_ => $h_svc_x->get($_) } fields($table)
1015 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1016 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1019 my $svc_error = $svc_x->insert;
1021 if ( $options{svc_fatal} ) {
1022 $dbh->rollback if $oldAutoCommit;
1025 push @svc_errors, $svc_error;
1026 # is this necessary? svc_Common::insert already deletes the
1027 # cust_svc if inserting svc_x fails.
1028 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1030 my $cs_error = $cust_svc->delete;
1032 $dbh->rollback if $oldAutoCommit;
1038 } #foreach $h_cust_svc
1040 #these are pretty rare, but should handle them
1041 # - dsl_device (mac addresses)
1042 # - phone_device (mac addresses)
1043 # - dsl_note (ikano notes)
1044 # - domain_record (i.e. restore DNS information w/domains)
1045 # - inventory_item(?) (inventory w/un-cancelling service?)
1046 # - nas (svc_broaband nas stuff)
1047 #this stuff is unused in the wild afaik
1048 # - mailinglistmember
1050 # - svc_domain.parent_svcnum?
1051 # - acct_snarf (ancient mail fetching config)
1052 # - cgp_rule (communigate)
1053 # - cust_svc_option (used by our Tron stuff)
1054 # - acct_rt_transaction (used by our time worked stuff)
1057 # also move over any services that didn't unprovision at cancellation
1060 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1061 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1062 my $error = $cust_svc->replace;
1064 $dbh->rollback if $oldAutoCommit;
1070 # Uncancel any supplemental packages, and make them supplemental to the
1074 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1076 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1078 $dbh->rollback if $oldAutoCommit;
1079 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1087 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1089 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1090 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1097 Cancels any pending expiration (sets the expire field to null).
1099 If there is an error, returns the error, otherwise returns false.
1104 my( $self, %options ) = @_;
1107 local $SIG{HUP} = 'IGNORE';
1108 local $SIG{INT} = 'IGNORE';
1109 local $SIG{QUIT} = 'IGNORE';
1110 local $SIG{TERM} = 'IGNORE';
1111 local $SIG{TSTP} = 'IGNORE';
1112 local $SIG{PIPE} = 'IGNORE';
1114 my $oldAutoCommit = $FS::UID::AutoCommit;
1115 local $FS::UID::AutoCommit = 0;
1118 my $old = $self->select_for_update;
1120 my $pkgnum = $old->pkgnum;
1121 if ( $old->get('cancel') || $self->get('cancel') ) {
1122 dbh->rollback if $oldAutoCommit;
1123 return "Can't unexpire cancelled package $pkgnum";
1124 # or at least it's pointless
1127 unless ( $old->get('expire') && $self->get('expire') ) {
1128 dbh->rollback if $oldAutoCommit;
1129 return ""; # no error
1132 my %hash = $self->hash;
1133 $hash{'expire'} = '';
1134 my $new = new FS::cust_pkg ( \%hash );
1135 $error = $new->replace( $self, options => { $self->options } );
1137 $dbh->rollback if $oldAutoCommit;
1141 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1147 =item suspend [ OPTION => VALUE ... ]
1149 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1150 package, then suspends the package itself (sets the susp field to now).
1152 Available options are:
1156 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1157 either a reasonnum of an existing reason, or passing a hashref will create
1158 a new reason. The hashref should have the following keys:
1159 - typenum - Reason type (see L<FS::reason_type>
1160 - reason - Text of the new reason.
1162 =item date - can be set to a unix style timestamp to specify when to
1165 =item time - can be set to override the current time, for calculation
1166 of final invoices or unused-time credits
1168 =item resume_date - can be set to a time when the package should be
1169 unsuspended. This may be more convenient than calling C<unsuspend()>
1172 =item from_main - allows a supplemental package to be suspended, rather
1173 than redirecting the method call to its main package. For internal use.
1177 If there is an error, returns the error, otherwise returns false.
1182 my( $self, %options ) = @_;
1185 # pass all suspend/cancel actions to the main package
1186 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1187 return $self->main_pkg->suspend(%options);
1190 local $SIG{HUP} = 'IGNORE';
1191 local $SIG{INT} = 'IGNORE';
1192 local $SIG{QUIT} = 'IGNORE';
1193 local $SIG{TERM} = 'IGNORE';
1194 local $SIG{TSTP} = 'IGNORE';
1195 local $SIG{PIPE} = 'IGNORE';
1197 my $oldAutoCommit = $FS::UID::AutoCommit;
1198 local $FS::UID::AutoCommit = 0;
1201 my $old = $self->select_for_update;
1203 my $pkgnum = $old->pkgnum;
1204 if ( $old->get('cancel') || $self->get('cancel') ) {
1205 dbh->rollback if $oldAutoCommit;
1206 return "Can't suspend cancelled package $pkgnum";
1209 if ( $old->get('susp') || $self->get('susp') ) {
1210 dbh->rollback if $oldAutoCommit;
1211 return ""; # no error # complain on adjourn?
1214 my $suspend_time = $options{'time'} || time;
1215 my $date = $options{date} if $options{date}; # adjourn/suspend later
1216 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1218 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1219 dbh->rollback if $oldAutoCommit;
1220 return "Package $pkgnum expires before it would be suspended.";
1223 # some false laziness with sub cancel
1224 if ( !$options{nobill} && !$date &&
1225 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1226 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1227 # make the entire cust_main->bill path recognize 'suspend' and
1228 # 'cancel' separately.
1229 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1230 my $copy = $self->new({$self->hash});
1232 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1234 'time' => $suspend_time );
1235 warn "Error billing during suspend, custnum ".
1236 #$self->cust_main->custnum. ": $error"
1241 if ( $options{'reason'} ) {
1242 $error = $self->insert_reason( 'reason' => $options{'reason'},
1243 'action' => $date ? 'adjourn' : 'suspend',
1244 'date' => $date ? $date : $suspend_time,
1245 'reason_otaker' => $options{'reason_otaker'},
1248 dbh->rollback if $oldAutoCommit;
1249 return "Error inserting cust_pkg_reason: $error";
1253 my %hash = $self->hash;
1255 $hash{'adjourn'} = $date;
1257 $hash{'susp'} = $suspend_time;
1260 my $resume_date = $options{'resume_date'} || 0;
1261 if ( $resume_date > ($date || $suspend_time) ) {
1262 $hash{'resume'} = $resume_date;
1265 $options{options} ||= {};
1267 my $new = new FS::cust_pkg ( \%hash );
1268 $error = $new->replace( $self, options => { $self->options,
1269 %{ $options{options} },
1273 $dbh->rollback if $oldAutoCommit;
1278 # credit remaining time if appropriate
1279 if ( $self->part_pkg->option('unused_credit_suspend', 1) ) {
1280 my $error = $self->credit_remaining('suspend', $suspend_time);
1282 $dbh->rollback if $oldAutoCommit;
1289 foreach my $cust_svc (
1290 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
1292 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1294 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1295 $dbh->rollback if $oldAutoCommit;
1296 return "Illegal svcdb value in part_svc!";
1299 require "FS/$svcdb.pm";
1301 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1303 $error = $svc->suspend;
1305 $dbh->rollback if $oldAutoCommit;
1308 my( $label, $value ) = $cust_svc->label;
1309 push @labels, "$label: $value";
1313 my $conf = new FS::Conf;
1314 if ( $conf->config('suspend_email_admin') ) {
1316 my $error = send_email(
1317 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1318 #invoice_from ??? well as good as any
1319 'to' => $conf->config('suspend_email_admin'),
1320 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1322 "This is an automatic message from your Freeside installation\n",
1323 "informing you that the following customer package has been suspended:\n",
1325 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1326 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1327 ( map { "Service : $_\n" } @labels ),
1332 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1340 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1341 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1343 $dbh->rollback if $oldAutoCommit;
1344 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1348 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1353 =item credit_remaining MODE TIME
1355 Generate a credit for this package for the time remaining in the current
1356 billing period. MODE is either "suspend" or "cancel" (determines the
1357 credit type). TIME is the time of suspension/cancellation. Both arguments
1362 sub credit_remaining {
1363 # Add a credit for remaining service
1364 my ($self, $mode, $time) = @_;
1365 die 'credit_remaining requires suspend or cancel'
1366 unless $mode eq 'suspend' or $mode eq 'cancel';
1367 die 'no suspend/cancel time' unless $time > 0;
1369 my $conf = FS::Conf->new;
1370 my $reason_type = $conf->config($mode.'_credit_type');
1372 my $last_bill = $self->getfield('last_bill') || 0;
1373 my $next_bill = $self->getfield('bill') || 0;
1374 if ( $last_bill > 0 # the package has been billed
1375 and $next_bill > 0 # the package has a next bill date
1376 and $next_bill >= $time # which is in the future
1378 my $remaining_value = $self->calc_remain('time' => $time);
1379 if ( $remaining_value > 0 ) {
1380 warn "Crediting for $remaining_value on package ".$self->pkgnum."\n"
1382 my $error = $self->cust_main->credit(
1384 'Credit for unused time on '. $self->part_pkg->pkg,
1385 'reason_type' => $reason_type,
1387 return "Error crediting customer \$$remaining_value for unused time".
1388 " on ". $self->part_pkg->pkg. ": $error"
1390 } #if $remaining_value
1391 } #if $last_bill, etc.
1395 =item unsuspend [ OPTION => VALUE ... ]
1397 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1398 package, then unsuspends the package itself (clears the susp field and the
1399 adjourn field if it is in the past). If the suspend reason includes an
1400 unsuspension package, that package will be ordered.
1402 Available options are:
1408 Can be set to a date to unsuspend the package in the future (the 'resume'
1411 =item adjust_next_bill
1413 Can be set true to adjust the next bill date forward by
1414 the amount of time the account was inactive. This was set true by default
1415 since 1.4.2 and 1.5.0pre6; however, starting with 1.7.0 this needs to be
1416 explicitly requested. Price plans for which this makes sense (anniversary-date
1417 based than prorate or subscription) could have an option to enable this
1422 If there is an error, returns the error, otherwise returns false.
1427 my( $self, %opt ) = @_;
1430 # pass all suspend/cancel actions to the main package
1431 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1432 return $self->main_pkg->unsuspend(%opt);
1435 local $SIG{HUP} = 'IGNORE';
1436 local $SIG{INT} = 'IGNORE';
1437 local $SIG{QUIT} = 'IGNORE';
1438 local $SIG{TERM} = 'IGNORE';
1439 local $SIG{TSTP} = 'IGNORE';
1440 local $SIG{PIPE} = 'IGNORE';
1442 my $oldAutoCommit = $FS::UID::AutoCommit;
1443 local $FS::UID::AutoCommit = 0;
1446 my $old = $self->select_for_update;
1448 my $pkgnum = $old->pkgnum;
1449 if ( $old->get('cancel') || $self->get('cancel') ) {
1450 $dbh->rollback if $oldAutoCommit;
1451 return "Can't unsuspend cancelled package $pkgnum";
1454 unless ( $old->get('susp') && $self->get('susp') ) {
1455 $dbh->rollback if $oldAutoCommit;
1456 return ""; # no error # complain instead?
1459 my $date = $opt{'date'};
1460 if ( $date and $date > time ) { # return an error if $date <= time?
1462 if ( $old->get('expire') && $old->get('expire') < $date ) {
1463 $dbh->rollback if $oldAutoCommit;
1464 return "Package $pkgnum expires before it would be unsuspended.";
1467 my $new = new FS::cust_pkg { $self->hash };
1468 $new->set('resume', $date);
1469 $error = $new->replace($self, options => $self->options);
1472 $dbh->rollback if $oldAutoCommit;
1476 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1484 foreach my $cust_svc (
1485 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1487 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1489 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1490 $dbh->rollback if $oldAutoCommit;
1491 return "Illegal svcdb value in part_svc!";
1494 require "FS/$svcdb.pm";
1496 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1498 $error = $svc->unsuspend;
1500 $dbh->rollback if $oldAutoCommit;
1503 my( $label, $value ) = $cust_svc->label;
1504 push @labels, "$label: $value";
1509 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1510 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
1512 my %hash = $self->hash;
1513 my $inactive = time - $hash{'susp'};
1515 my $conf = new FS::Conf;
1517 if ( $inactive > 0 &&
1518 ( $hash{'bill'} || $hash{'setup'} ) &&
1519 ( $opt{'adjust_next_bill'} ||
1520 $conf->exists('unsuspend-always_adjust_next_bill_date') ||
1521 $self->part_pkg->option('unsuspend_adjust_bill', 1) )
1524 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
1529 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
1530 $hash{'resume'} = '' if !$hash{'adjourn'};
1531 my $new = new FS::cust_pkg ( \%hash );
1532 $error = $new->replace( $self, options => { $self->options } );
1534 $dbh->rollback if $oldAutoCommit;
1540 if ( $reason && $reason->unsuspend_pkgpart ) {
1541 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
1542 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
1544 my $start_date = $self->cust_main->next_bill_date
1545 if $reason->unsuspend_hold;
1548 $unsusp_pkg = FS::cust_pkg->new({
1549 'custnum' => $self->custnum,
1550 'pkgpart' => $reason->unsuspend_pkgpart,
1551 'start_date' => $start_date,
1552 'locationnum' => $self->locationnum,
1553 # discount? probably not...
1556 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
1560 $dbh->rollback if $oldAutoCommit;
1565 if ( $conf->config('unsuspend_email_admin') ) {
1567 my $error = send_email(
1568 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1569 #invoice_from ??? well as good as any
1570 'to' => $conf->config('unsuspend_email_admin'),
1571 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
1572 "This is an automatic message from your Freeside installation\n",
1573 "informing you that the following customer package has been unsuspended:\n",
1575 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1576 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1577 ( map { "Service : $_\n" } @labels ),
1579 "An unsuspension fee was charged: ".
1580 $unsusp_pkg->part_pkg->pkg_comment."\n"
1587 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
1593 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1594 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
1596 $dbh->rollback if $oldAutoCommit;
1597 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1601 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1608 Cancels any pending suspension (sets the adjourn field to null).
1610 If there is an error, returns the error, otherwise returns false.
1615 my( $self, %options ) = @_;
1618 local $SIG{HUP} = 'IGNORE';
1619 local $SIG{INT} = 'IGNORE';
1620 local $SIG{QUIT} = 'IGNORE';
1621 local $SIG{TERM} = 'IGNORE';
1622 local $SIG{TSTP} = 'IGNORE';
1623 local $SIG{PIPE} = 'IGNORE';
1625 my $oldAutoCommit = $FS::UID::AutoCommit;
1626 local $FS::UID::AutoCommit = 0;
1629 my $old = $self->select_for_update;
1631 my $pkgnum = $old->pkgnum;
1632 if ( $old->get('cancel') || $self->get('cancel') ) {
1633 dbh->rollback if $oldAutoCommit;
1634 return "Can't unadjourn cancelled package $pkgnum";
1635 # or at least it's pointless
1638 if ( $old->get('susp') || $self->get('susp') ) {
1639 dbh->rollback if $oldAutoCommit;
1640 return "Can't unadjourn suspended package $pkgnum";
1641 # perhaps this is arbitrary
1644 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1645 dbh->rollback if $oldAutoCommit;
1646 return ""; # no error
1649 my %hash = $self->hash;
1650 $hash{'adjourn'} = '';
1651 $hash{'resume'} = '';
1652 my $new = new FS::cust_pkg ( \%hash );
1653 $error = $new->replace( $self, options => { $self->options } );
1655 $dbh->rollback if $oldAutoCommit;
1659 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1666 =item change HASHREF | OPTION => VALUE ...
1668 Changes this package: cancels it and creates a new one, with a different
1669 pkgpart or locationnum or both. All services are transferred to the new
1670 package (no change will be made if this is not possible).
1672 Options may be passed as a list of key/value pairs or as a hash reference.
1679 New locationnum, to change the location for this package.
1683 New FS::cust_location object, to create a new location and assign it
1688 New pkgpart (see L<FS::part_pkg>).
1692 New refnum (see L<FS::part_referral>).
1696 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1697 susp, adjourn, cancel, expire, and contract_end) to the new package.
1701 At least one of locationnum, cust_location, pkgpart, refnum must be specified
1702 (otherwise, what's the point?)
1704 Returns either the new FS::cust_pkg object or a scalar error.
1708 my $err_or_new_cust_pkg = $old_cust_pkg->change
1712 #some false laziness w/order
1715 my $opt = ref($_[0]) ? shift : { @_ };
1717 # my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
1720 my $conf = new FS::Conf;
1722 # Transactionize this whole mess
1723 local $SIG{HUP} = 'IGNORE';
1724 local $SIG{INT} = 'IGNORE';
1725 local $SIG{QUIT} = 'IGNORE';
1726 local $SIG{TERM} = 'IGNORE';
1727 local $SIG{TSTP} = 'IGNORE';
1728 local $SIG{PIPE} = 'IGNORE';
1730 my $oldAutoCommit = $FS::UID::AutoCommit;
1731 local $FS::UID::AutoCommit = 0;
1740 #$hash{$_} = $self->$_() foreach qw( last_bill bill );
1742 #$hash{$_} = $self->$_() foreach qw( setup );
1744 $hash{'setup'} = $time if $self->setup;
1746 $hash{'change_date'} = $time;
1747 $hash{"change_$_"} = $self->$_()
1748 foreach qw( pkgnum pkgpart locationnum );
1750 if ( $opt->{'cust_location'} &&
1751 ( ! $opt->{'locationnum'} || $opt->{'locationnum'} == -1 ) ) {
1752 $error = $opt->{'cust_location'}->insert;
1754 $dbh->rollback if $oldAutoCommit;
1755 return "inserting cust_location (transaction rolled back): $error";
1757 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1760 my $unused_credit = 0;
1761 my $keep_dates = $opt->{'keep_dates'};
1762 # Special case. If the pkgpart is changing, and the customer is
1763 # going to be credited for remaining time, don't keep setup, bill,
1764 # or last_bill dates, and DO pass the flag to cancel() to credit
1766 if ( $opt->{'pkgpart'}
1767 and $opt->{'pkgpart'} != $self->pkgpart
1768 and $self->part_pkg->option('unused_credit_change', 1) ) {
1771 $hash{$_} = '' foreach qw(setup bill last_bill);
1774 if ( $keep_dates ) {
1775 foreach my $date ( qw(setup bill last_bill susp adjourn cancel expire
1776 resume start_date contract_end ) ) {
1777 $hash{$date} = $self->getfield($date);
1780 # allow $opt->{'locationnum'} = '' to specifically set it to null
1781 # (i.e. customer default location)
1782 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
1784 # Create the new package.
1785 my $cust_pkg = new FS::cust_pkg {
1786 custnum => $self->custnum,
1787 pkgpart => ( $opt->{'pkgpart'} || $self->pkgpart ),
1788 refnum => ( $opt->{'refnum'} || $self->refnum ),
1789 locationnum => ( $opt->{'locationnum'} ),
1792 $error = $cust_pkg->insert( 'change' => 1 );
1794 $dbh->rollback if $oldAutoCommit;
1798 # Transfer services and cancel old package.
1800 $error = $self->transfer($cust_pkg);
1801 if ($error and $error == 0) {
1802 # $old_pkg->transfer failed.
1803 $dbh->rollback if $oldAutoCommit;
1807 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
1808 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
1809 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
1810 if ($error and $error == 0) {
1811 # $old_pkg->transfer failed.
1812 $dbh->rollback if $oldAutoCommit;
1818 # Transfers were successful, but we still had services left on the old
1819 # package. We can't change the package under this circumstances, so abort.
1820 $dbh->rollback if $oldAutoCommit;
1821 return "Unable to transfer all services from package ". $self->pkgnum;
1824 #reset usage if changing pkgpart
1825 # AND usage rollover is off (otherwise adds twice, now and at package bill)
1826 if ($self->pkgpart != $cust_pkg->pkgpart) {
1827 my $part_pkg = $cust_pkg->part_pkg;
1828 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
1832 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
1835 $dbh->rollback if $oldAutoCommit;
1836 return "Error setting usage values: $error";
1839 # if NOT changing pkgpart, transfer any usage pools over
1840 foreach my $usage ($self->cust_pkg_usage) {
1841 $usage->set('pkgnum', $cust_pkg->pkgnum);
1842 $error = $usage->replace;
1844 $dbh->rollback if $oldAutoCommit;
1845 return "Error transferring usage pools: $error";
1850 # Order any supplemental packages.
1851 my $part_pkg = $cust_pkg->part_pkg;
1852 my @old_supp_pkgs = $self->supplemental_pkgs;
1854 foreach my $link ($part_pkg->supp_part_pkg_link) {
1856 foreach (@old_supp_pkgs) {
1857 if ($_->pkgpart == $link->dst_pkgpart) {
1859 $_->pkgpart(0); # so that it can't match more than once
1863 # false laziness with FS::cust_main::Packages::order_pkg
1864 my $new = FS::cust_pkg->new({
1865 pkgpart => $link->dst_pkgpart,
1866 pkglinknum => $link->pkglinknum,
1867 custnum => $self->custnum,
1868 main_pkgnum => $cust_pkg->pkgnum,
1869 locationnum => $cust_pkg->locationnum,
1870 start_date => $cust_pkg->start_date,
1871 order_date => $cust_pkg->order_date,
1872 expire => $cust_pkg->expire,
1873 adjourn => $cust_pkg->adjourn,
1874 contract_end => $cust_pkg->contract_end,
1875 refnum => $cust_pkg->refnum,
1876 discountnum => $cust_pkg->discountnum,
1877 waive_setup => $cust_pkg->waive_setup
1879 if ( $old and $opt->{'keep_dates'} ) {
1880 foreach (qw(setup bill last_bill)) {
1881 $new->set($_, $old->get($_));
1884 $error = $new->insert;
1887 $error ||= $old->transfer($new);
1889 if ( $error and $error > 0 ) {
1890 # no reason why this should ever fail, but still...
1891 $error = "Unable to transfer all services from supplemental package ".
1895 $dbh->rollback if $oldAutoCommit;
1898 push @new_supp_pkgs, $new;
1901 #Good to go, cancel old package. Notify 'cancel' of whether to credit
1903 #Don't allow billing the package (preceding period packages and/or
1904 #outstanding usage) if we are keeping dates (i.e. location changing),
1905 #because the new package will be billed for the same date range.
1906 #Supplemental packages are also canceled here.
1907 $error = $self->cancel(
1909 unused_credit => $unused_credit,
1910 nobill => $keep_dates
1913 $dbh->rollback if $oldAutoCommit;
1917 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
1919 my $error = $cust_pkg->cust_main->bill(
1920 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
1923 $dbh->rollback if $oldAutoCommit;
1928 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1934 =item set_quantity QUANTITY
1936 Change the package's quantity field. This is the one package property
1937 that can safely be changed without canceling and reordering the package
1938 (because it doesn't affect tax eligibility). Returns an error or an
1945 $self = $self->replace_old; # just to make sure
1947 ($qty =~ /^\d+$/ and $qty > 0) or return "bad package quantity $qty";
1948 $self->set('quantity' => $qty);
1952 use Storable 'thaw';
1954 sub process_bulk_cust_pkg {
1956 my $param = thaw(decode_base64(shift));
1957 warn Dumper($param) if $DEBUG;
1959 my $old_part_pkg = qsearchs('part_pkg',
1960 { pkgpart => $param->{'old_pkgpart'} });
1961 my $new_part_pkg = qsearchs('part_pkg',
1962 { pkgpart => $param->{'new_pkgpart'} });
1963 die "Must select a new package type\n" unless $new_part_pkg;
1964 #my $keep_dates = $param->{'keep_dates'} || 0;
1965 my $keep_dates = 1; # there is no good reason to turn this off
1967 local $SIG{HUP} = 'IGNORE';
1968 local $SIG{INT} = 'IGNORE';
1969 local $SIG{QUIT} = 'IGNORE';
1970 local $SIG{TERM} = 'IGNORE';
1971 local $SIG{TSTP} = 'IGNORE';
1972 local $SIG{PIPE} = 'IGNORE';
1974 my $oldAutoCommit = $FS::UID::AutoCommit;
1975 local $FS::UID::AutoCommit = 0;
1978 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
1981 foreach my $old_cust_pkg ( @cust_pkgs ) {
1983 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
1984 if ( $old_cust_pkg->getfield('cancel') ) {
1985 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
1986 $old_cust_pkg->pkgnum."\n"
1990 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
1992 my $error = $old_cust_pkg->change(
1993 'pkgpart' => $param->{'new_pkgpart'},
1994 'keep_dates' => $keep_dates
1996 if ( !ref($error) ) { # change returns the cust_pkg on success
1998 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
2001 $dbh->commit if $oldAutoCommit;
2007 Returns the last bill date, or if there is no last bill date, the setup date.
2008 Useful for billing metered services.
2014 return $self->setfield('last_bill', $_[0]) if @_;
2015 return $self->getfield('last_bill') if $self->getfield('last_bill');
2016 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
2017 'edate' => $self->bill, } );
2018 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
2021 =item last_cust_pkg_reason ACTION
2023 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
2024 Returns false if there is no reason or the package is not currenly ACTION'd
2025 ACTION is one of adjourn, susp, cancel, or expire.
2029 sub last_cust_pkg_reason {
2030 my ( $self, $action ) = ( shift, shift );
2031 my $date = $self->get($action);
2033 'table' => 'cust_pkg_reason',
2034 'hashref' => { 'pkgnum' => $self->pkgnum,
2035 'action' => substr(uc($action), 0, 1),
2038 'order_by' => 'ORDER BY num DESC LIMIT 1',
2042 =item last_reason ACTION
2044 Returns the most recent ACTION FS::reason associated with the package.
2045 Returns false if there is no reason or the package is not currenly ACTION'd
2046 ACTION is one of adjourn, susp, cancel, or expire.
2051 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
2052 $cust_pkg_reason->reason
2053 if $cust_pkg_reason;
2058 Returns the definition for this billing item, as an FS::part_pkg object (see
2065 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
2066 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
2067 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
2072 Returns the cancelled package this package was changed from, if any.
2078 return '' unless $self->change_pkgnum;
2079 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
2084 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
2091 $self->part_pkg->calc_setup($self, @_);
2096 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
2103 $self->part_pkg->calc_recur($self, @_);
2108 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
2115 $self->part_pkg->base_recur($self, @_);
2120 Calls the I<calc_remain> of the FS::part_pkg object associated with this
2127 $self->part_pkg->calc_remain($self, @_);
2132 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
2139 $self->part_pkg->calc_cancel($self, @_);
2144 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
2150 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
2153 =item cust_pkg_detail [ DETAILTYPE ]
2155 Returns any customer package details for this package (see
2156 L<FS::cust_pkg_detail>).
2158 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2162 sub cust_pkg_detail {
2164 my %hash = ( 'pkgnum' => $self->pkgnum );
2165 $hash{detailtype} = shift if @_;
2167 'table' => 'cust_pkg_detail',
2168 'hashref' => \%hash,
2169 'order_by' => 'ORDER BY weight, pkgdetailnum',
2173 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
2175 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
2177 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2179 If there is an error, returns the error, otherwise returns false.
2183 sub set_cust_pkg_detail {
2184 my( $self, $detailtype, @details ) = @_;
2186 local $SIG{HUP} = 'IGNORE';
2187 local $SIG{INT} = 'IGNORE';
2188 local $SIG{QUIT} = 'IGNORE';
2189 local $SIG{TERM} = 'IGNORE';
2190 local $SIG{TSTP} = 'IGNORE';
2191 local $SIG{PIPE} = 'IGNORE';
2193 my $oldAutoCommit = $FS::UID::AutoCommit;
2194 local $FS::UID::AutoCommit = 0;
2197 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
2198 my $error = $current->delete;
2200 $dbh->rollback if $oldAutoCommit;
2201 return "error removing old detail: $error";
2205 foreach my $detail ( @details ) {
2206 my $cust_pkg_detail = new FS::cust_pkg_detail {
2207 'pkgnum' => $self->pkgnum,
2208 'detailtype' => $detailtype,
2209 'detail' => $detail,
2211 my $error = $cust_pkg_detail->insert;
2213 $dbh->rollback if $oldAutoCommit;
2214 return "error adding new detail: $error";
2219 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2226 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
2230 #false laziness w/cust_bill.pm
2234 'table' => 'cust_event',
2235 'addl_from' => 'JOIN part_event USING ( eventpart )',
2236 'hashref' => { 'tablenum' => $self->pkgnum },
2237 'extra_sql' => " AND eventtable = 'cust_pkg' ",
2241 =item num_cust_event
2243 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
2247 #false laziness w/cust_bill.pm
2248 sub num_cust_event {
2251 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
2252 " WHERE tablenum = ? AND eventtable = 'cust_pkg'";
2253 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
2254 $sth->execute($self->pkgnum) or die $sth->errstr. " executing $sql";
2255 $sth->fetchrow_arrayref->[0];
2258 =item cust_svc [ SVCPART ] (old, deprecated usage)
2260 =item cust_svc [ OPTION => VALUE ... ] (current usage)
2262 Returns the services for this package, as FS::cust_svc objects (see
2263 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
2264 spcififed, returns only the matching services.
2271 return () unless $self->num_cust_svc(@_);
2274 if ( @_ && $_[0] =~ /^\d+/ ) {
2275 $opt{svcpart} = shift;
2276 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2283 'table' => 'cust_svc',
2284 'hashref' => { 'pkgnum' => $self->pkgnum },
2286 if ( $opt{svcpart} ) {
2287 $search{hashref}->{svcpart} = $opt{'svcpart'};
2289 if ( $opt{'svcdb'} ) {
2290 $search{addl_from} = ' LEFT JOIN part_svc USING ( svcpart ) ';
2291 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{'svcdb'} );
2294 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
2296 #if ( $self->{'_svcnum'} ) {
2297 # values %{ $self->{'_svcnum'}->cache };
2299 $self->_sort_cust_svc( [ qsearch(\%search) ] );
2304 =item overlimit [ SVCPART ]
2306 Returns the services for this package which have exceeded their
2307 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
2308 is specified, return only the matching services.
2314 return () unless $self->num_cust_svc(@_);
2315 grep { $_->overlimit } $self->cust_svc(@_);
2318 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
2320 Returns historical services for this package created before END TIMESTAMP and
2321 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
2322 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
2323 I<pkg_svc.hidden> flag will be omitted.
2329 warn "$me _h_cust_svc called on $self\n"
2332 my ($end, $start, $mode) = @_;
2333 my @cust_svc = $self->_sort_cust_svc(
2334 [ qsearch( 'h_cust_svc',
2335 { 'pkgnum' => $self->pkgnum, },
2336 FS::h_cust_svc->sql_h_search(@_),
2339 if ( defined($mode) && $mode eq 'I' ) {
2340 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
2341 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
2347 sub _sort_cust_svc {
2348 my( $self, $arrayref ) = @_;
2351 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
2356 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $self->pkgpart,
2357 'svcpart' => $_->svcpart } );
2359 $pkg_svc ? $pkg_svc->primary_svc : '',
2360 $pkg_svc ? $pkg_svc->quantity : 0,
2367 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
2369 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
2371 Returns the number of services for this package. Available options are svcpart
2372 and svcdb. If either is spcififed, returns only the matching services.
2379 return $self->{'_num_cust_svc'}
2381 && exists($self->{'_num_cust_svc'})
2382 && $self->{'_num_cust_svc'} =~ /\d/;
2384 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
2388 if ( @_ && $_[0] =~ /^\d+/ ) {
2389 $opt{svcpart} = shift;
2390 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2396 my $select = 'SELECT COUNT(*) FROM cust_svc ';
2397 my $where = ' WHERE pkgnum = ? ';
2398 my @param = ($self->pkgnum);
2400 if ( $opt{'svcpart'} ) {
2401 $where .= ' AND svcpart = ? ';
2402 push @param, $opt{'svcpart'};
2404 if ( $opt{'svcdb'} ) {
2405 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
2406 $where .= ' AND svcdb = ? ';
2407 push @param, $opt{'svcdb'};
2410 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
2411 $sth->execute(@param) or die $sth->errstr;
2412 $sth->fetchrow_arrayref->[0];
2415 =item available_part_svc
2417 Returns a list of FS::part_svc objects representing services included in this
2418 package but not yet provisioned. Each FS::part_svc object also has an extra
2419 field, I<num_avail>, which specifies the number of available services.
2423 sub available_part_svc {
2426 my $pkg_quantity = $self->quantity || 1;
2428 grep { $_->num_avail > 0 }
2430 my $part_svc = $_->part_svc;
2431 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
2432 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
2434 # more evil encapsulation breakage
2435 if($part_svc->{'Hash'}{'num_avail'} > 0) {
2436 my @exports = $part_svc->part_export_did;
2437 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
2442 $self->part_pkg->pkg_svc;
2445 =item part_svc [ OPTION => VALUE ... ]
2447 Returns a list of FS::part_svc objects representing provisioned and available
2448 services included in this package. Each FS::part_svc object also has the
2449 following extra fields:
2453 =item num_cust_svc (count)
2455 =item num_avail (quantity - count)
2457 =item cust_pkg_svc (services) - array reference containing the provisioned services, as cust_svc objects
2461 Accepts one option: summarize_size. If specified and non-zero, will omit the
2462 extra cust_pkg_svc option for objects where num_cust_svc is this size or
2468 #label -> ($cust_svc->label)[1]
2474 my $pkg_quantity = $self->quantity || 1;
2476 #XXX some sort of sort order besides numeric by svcpart...
2477 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
2479 my $part_svc = $pkg_svc->part_svc;
2480 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
2481 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
2482 $part_svc->{'Hash'}{'num_avail'} =
2483 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
2484 $part_svc->{'Hash'}{'cust_pkg_svc'} =
2485 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
2486 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
2487 && $num_cust_svc >= $opt{summarize_size};
2488 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
2490 } $self->part_pkg->pkg_svc;
2493 push @part_svc, map {
2495 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
2496 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
2497 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
2498 $part_svc->{'Hash'}{'cust_pkg_svc'} =
2499 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
2501 } $self->extra_part_svc;
2507 =item extra_part_svc
2509 Returns a list of FS::part_svc objects corresponding to services in this
2510 package which are still provisioned but not (any longer) available in the
2515 sub extra_part_svc {
2518 my $pkgnum = $self->pkgnum;
2519 #my $pkgpart = $self->pkgpart;
2522 # 'table' => 'part_svc',
2525 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
2526 # WHERE pkg_svc.svcpart = part_svc.svcpart
2527 # AND pkg_svc.pkgpart = ?
2530 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
2531 # LEFT JOIN cust_pkg USING ( pkgnum )
2532 # WHERE cust_svc.svcpart = part_svc.svcpart
2535 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
2538 #seems to benchmark slightly faster... (or did?)
2540 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
2541 my $pkgparts = join(',', @pkgparts);
2544 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
2545 #MySQL doesn't grok DISINCT ON
2546 'select' => 'DISTINCT part_svc.*',
2547 'table' => 'part_svc',
2549 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
2550 AND pkg_svc.pkgpart IN ($pkgparts)
2553 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
2554 LEFT JOIN cust_pkg USING ( pkgnum )
2557 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
2558 'extra_param' => [ [$self->pkgnum=>'int'] ],
2564 Returns a short status string for this package, currently:
2568 =item not yet billed
2570 =item one-time charge
2585 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
2587 return 'cancelled' if $self->get('cancel');
2588 return 'suspended' if $self->susp;
2589 return 'not yet billed' unless $self->setup;
2590 return 'one-time charge' if $freq =~ /^(0|$)/;
2594 =item ucfirst_status
2596 Returns the status with the first character capitalized.
2600 sub ucfirst_status {
2601 ucfirst(shift->status);
2606 Class method that returns the list of possible status strings for packages
2607 (see L<the status method|/status>). For example:
2609 @statuses = FS::cust_pkg->statuses();
2613 tie my %statuscolor, 'Tie::IxHash',
2614 'not yet billed' => '009999', #teal? cyan?
2615 'one-time charge' => '000000',
2616 'active' => '00CC00',
2617 'suspended' => 'FF9900',
2618 'cancelled' => 'FF0000',
2622 my $self = shift; #could be class...
2623 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
2624 # # mayble split btw one-time vs. recur
2630 Returns a hex triplet color string for this package's status.
2636 $statuscolor{$self->status};
2641 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
2642 "pkg-comment" depending on user preference).
2648 my $label = $self->part_pkg->pkg_comment( 'nopkgpart' => 1 );
2649 $label = $self->pkgnum. ": $label"
2650 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
2654 =item pkg_label_long
2656 Returns a long label for this package, adding the primary service's label to
2661 sub pkg_label_long {
2663 my $label = $self->pkg_label;
2664 my $cust_svc = $self->primary_cust_svc;
2665 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
2669 =item primary_cust_svc
2671 Returns a primary service (as FS::cust_svc object) if one can be identified.
2675 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
2677 sub primary_cust_svc {
2680 my @cust_svc = $self->cust_svc;
2682 return '' unless @cust_svc; #no serivces - irrelevant then
2684 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
2686 # primary service as specified in the package definition
2687 # or exactly one service definition with quantity one
2688 my $svcpart = $self->part_pkg->svcpart;
2689 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
2690 return $cust_svc[0] if scalar(@cust_svc) == 1;
2692 #couldn't identify one thing..
2698 Returns a list of lists, calling the label method for all services
2699 (see L<FS::cust_svc>) of this billing item.
2705 map { [ $_->label ] } $self->cust_svc;
2708 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
2710 Like the labels method, but returns historical information on services that
2711 were active as of END_TIMESTAMP and (optionally) not cancelled before
2712 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
2713 I<pkg_svc.hidden> flag will be omitted.
2715 Returns a list of lists, calling the label method for all (historical) services
2716 (see L<FS::h_cust_svc>) of this billing item.
2722 warn "$me _h_labels called on $self\n"
2724 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
2729 Like labels, except returns a simple flat list, and shortens long
2730 (currently >5 or the cust_bill-max_same_services configuration value) lists of
2731 identical services to one line that lists the service label and the number of
2732 individual services rather than individual items.
2737 shift->_labels_short( 'labels', @_ );
2740 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
2742 Like h_labels, except returns a simple flat list, and shortens long
2743 (currently >5 or the cust_bill-max_same_services configuration value) lists of
2744 identical services to one line that lists the service label and the number of
2745 individual services rather than individual items.
2749 sub h_labels_short {
2750 shift->_labels_short( 'h_labels', @_ );
2754 my( $self, $method ) = ( shift, shift );
2756 warn "$me _labels_short called on $self with $method method\n"
2759 my $conf = new FS::Conf;
2760 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
2762 warn "$me _labels_short populating \%labels\n"
2766 #tie %labels, 'Tie::IxHash';
2767 push @{ $labels{$_->[0]} }, $_->[1]
2768 foreach $self->$method(@_);
2770 warn "$me _labels_short populating \@labels\n"
2774 foreach my $label ( keys %labels ) {
2776 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
2777 my $num = scalar(@values);
2778 warn "$me _labels_short $num items for $label\n"
2781 if ( $num > $max_same_services ) {
2782 warn "$me _labels_short more than $max_same_services, so summarizing\n"
2784 push @labels, "$label ($num)";
2786 if ( $conf->exists('cust_bill-consolidate_services') ) {
2787 warn "$me _labels_short consolidating services\n"
2789 # push @labels, "$label: ". join(', ', @values);
2791 my $detail = "$label: ";
2792 $detail .= shift(@values). ', '
2794 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
2796 push @labels, $detail;
2798 warn "$me _labels_short done consolidating services\n"
2801 warn "$me _labels_short adding service data\n"
2803 push @labels, map { "$label: $_" } @values;
2814 Returns the parent customer object (see L<FS::cust_main>).
2820 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
2825 Returns the balance for this specific package, when using
2826 experimental package balance.
2832 $self->cust_main->balance_pkgnum( $self->pkgnum );
2835 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
2839 Returns the location object, if any (see L<FS::cust_location>).
2841 =item cust_location_or_main
2843 If this package is associated with a location, returns the locaiton (see
2844 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
2846 =item location_label [ OPTION => VALUE ... ]
2848 Returns the label of the location object (see L<FS::cust_location>).
2852 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
2854 =item tax_locationnum
2856 Returns the foreign key to a L<FS::cust_location> object for calculating
2857 tax on this package, as determined by the C<tax-pkg_address> and
2858 C<tax-ship_address> configuration flags.
2862 sub tax_locationnum {
2864 my $conf = FS::Conf->new;
2865 if ( $conf->exists('tax-pkg_address') ) {
2866 return $self->locationnum;
2868 elsif ( $conf->exists('tax-ship_address') ) {
2869 return $self->cust_main->ship_locationnum;
2872 return $self->cust_main->bill_locationnum;
2878 Returns the L<FS::cust_location> object for tax_locationnum.
2884 FS::cust_location->by_key( $self->tax_locationnum )
2887 =item seconds_since TIMESTAMP
2889 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
2890 package have been online since TIMESTAMP, according to the session monitor.
2892 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
2893 L<Time::Local> and L<Date::Parse> for conversion functions.
2898 my($self, $since) = @_;
2901 foreach my $cust_svc (
2902 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
2904 $seconds += $cust_svc->seconds_since($since);
2911 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
2913 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
2914 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
2917 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2918 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2924 sub seconds_since_sqlradacct {
2925 my($self, $start, $end) = @_;
2929 foreach my $cust_svc (
2931 my $part_svc = $_->part_svc;
2932 $part_svc->svcdb eq 'svc_acct'
2933 && scalar($part_svc->part_export_usage);
2936 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
2943 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
2945 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
2946 in this package for sessions ending between TIMESTAMP_START (inclusive) and
2950 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2951 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2956 sub attribute_since_sqlradacct {
2957 my($self, $start, $end, $attrib) = @_;
2961 foreach my $cust_svc (
2963 my $part_svc = $_->part_svc;
2964 $part_svc->svcdb eq 'svc_acct'
2965 && scalar($part_svc->part_export_usage);
2968 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
2980 my( $self, $value ) = @_;
2981 if ( defined($value) ) {
2982 $self->setfield('quantity', $value);
2984 $self->getfield('quantity') || 1;
2987 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
2989 Transfers as many services as possible from this package to another package.
2991 The destination package can be specified by pkgnum by passing an FS::cust_pkg
2992 object. The destination package must already exist.
2994 Services are moved only if the destination allows services with the correct
2995 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
2996 this option with caution! No provision is made for export differences
2997 between the old and new service definitions. Probably only should be used
2998 when your exports for all service definitions of a given svcdb are identical.
2999 (attempt a transfer without it first, to move all possible svcpart-matching
3002 Any services that can't be moved remain in the original package.
3004 Returns an error, if there is one; otherwise, returns the number of services
3005 that couldn't be moved.
3010 my ($self, $dest_pkgnum, %opt) = @_;
3016 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
3017 $dest = $dest_pkgnum;
3018 $dest_pkgnum = $dest->pkgnum;
3020 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
3023 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
3025 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
3026 $target{$pkg_svc->svcpart} = $pkg_svc->quantity;
3029 foreach my $cust_svc ($dest->cust_svc) {
3030 $target{$cust_svc->svcpart}--;
3033 my %svcpart2svcparts = ();
3034 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3035 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
3036 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
3037 next if exists $svcpart2svcparts{$svcpart};
3038 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
3039 $svcpart2svcparts{$svcpart} = [
3041 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
3043 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
3044 'svcpart' => $_ } );
3046 $pkg_svc ? $pkg_svc->primary_svc : '',
3047 $pkg_svc ? $pkg_svc->quantity : 0,
3051 grep { $_ != $svcpart }
3053 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
3055 warn "alternates for svcpart $svcpart: ".
3056 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
3061 foreach my $cust_svc ($self->cust_svc) {
3062 if($target{$cust_svc->svcpart} > 0
3063 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
3064 $target{$cust_svc->svcpart}--;
3065 my $new = new FS::cust_svc { $cust_svc->hash };
3066 $new->pkgnum($dest_pkgnum);
3067 my $error = $new->replace($cust_svc);
3068 return $error if $error;
3069 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3071 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
3072 warn "alternates to consider: ".
3073 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
3075 my @alternate = grep {
3076 warn "considering alternate svcpart $_: ".
3077 "$target{$_} available in new package\n"
3080 } @{$svcpart2svcparts{$cust_svc->svcpart}};
3082 warn "alternate(s) found\n" if $DEBUG;
3083 my $change_svcpart = $alternate[0];
3084 $target{$change_svcpart}--;
3085 my $new = new FS::cust_svc { $cust_svc->hash };
3086 $new->svcpart($change_svcpart);
3087 $new->pkgnum($dest_pkgnum);
3088 my $error = $new->replace($cust_svc);
3089 return $error if $error;
3102 This method is deprecated. See the I<depend_jobnum> option to the insert and
3103 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
3110 local $SIG{HUP} = 'IGNORE';
3111 local $SIG{INT} = 'IGNORE';
3112 local $SIG{QUIT} = 'IGNORE';
3113 local $SIG{TERM} = 'IGNORE';
3114 local $SIG{TSTP} = 'IGNORE';
3115 local $SIG{PIPE} = 'IGNORE';
3117 my $oldAutoCommit = $FS::UID::AutoCommit;
3118 local $FS::UID::AutoCommit = 0;
3121 foreach my $cust_svc ( $self->cust_svc ) {
3122 #false laziness w/svc_Common::insert
3123 my $svc_x = $cust_svc->svc_x;
3124 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
3125 my $error = $part_export->export_insert($svc_x);
3127 $dbh->rollback if $oldAutoCommit;
3133 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3140 Associates this package with a (suspension or cancellation) reason (see
3141 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
3144 Available options are:
3150 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.
3154 the access_user (see L<FS::access_user>) providing the reason
3162 the action (cancel, susp, adjourn, expire) associated with the reason
3166 If there is an error, returns the error, otherwise returns false.
3171 my ($self, %options) = @_;
3173 my $otaker = $options{reason_otaker} ||
3174 $FS::CurrentUser::CurrentUser->username;
3177 if ( $options{'reason'} =~ /^(\d+)$/ ) {
3181 } elsif ( ref($options{'reason'}) ) {
3183 return 'Enter a new reason (or select an existing one)'
3184 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
3186 my $reason = new FS::reason({
3187 'reason_type' => $options{'reason'}->{'typenum'},
3188 'reason' => $options{'reason'}->{'reason'},
3190 my $error = $reason->insert;
3191 return $error if $error;
3193 $reasonnum = $reason->reasonnum;
3196 return "Unparsable reason: ". $options{'reason'};
3199 my $cust_pkg_reason =
3200 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
3201 'reasonnum' => $reasonnum,
3202 'otaker' => $otaker,
3203 'action' => substr(uc($options{'action'}),0,1),
3204 'date' => $options{'date'}
3209 $cust_pkg_reason->insert;
3212 =item insert_discount
3214 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
3215 inserting a new discount on the fly (see L<FS::discount>).
3217 Available options are:
3225 If there is an error, returns the error, otherwise returns false.
3229 sub insert_discount {
3230 #my ($self, %options) = @_;
3233 my $cust_pkg_discount = new FS::cust_pkg_discount {
3234 'pkgnum' => $self->pkgnum,
3235 'discountnum' => $self->discountnum,
3237 'end_date' => '', #XXX
3238 #for the create a new discount case
3239 '_type' => $self->discountnum__type,
3240 'amount' => $self->discountnum_amount,
3241 'percent' => $self->discountnum_percent,
3242 'months' => $self->discountnum_months,
3243 'setup' => $self->discountnum_setup,
3244 #'disabled' => $self->discountnum_disabled,
3247 $cust_pkg_discount->insert;
3250 =item set_usage USAGE_VALUE_HASHREF
3252 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3253 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3254 upbytes, downbytes, and totalbytes are appropriate keys.
3256 All svc_accts which are part of this package have their values reset.
3261 my ($self, $valueref, %opt) = @_;
3263 #only svc_acct can set_usage for now
3264 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3265 my $svc_x = $cust_svc->svc_x;
3266 $svc_x->set_usage($valueref, %opt)
3267 if $svc_x->can("set_usage");
3271 =item recharge USAGE_VALUE_HASHREF
3273 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3274 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3275 upbytes, downbytes, and totalbytes are appropriate keys.
3277 All svc_accts which are part of this package have their values incremented.
3282 my ($self, $valueref) = @_;
3284 #only svc_acct can set_usage for now
3285 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3286 my $svc_x = $cust_svc->svc_x;
3287 $svc_x->recharge($valueref)
3288 if $svc_x->can("recharge");
3292 =item cust_pkg_discount
3296 sub cust_pkg_discount {
3298 qsearch('cust_pkg_discount', { 'pkgnum' => $self->pkgnum } );
3301 =item cust_pkg_discount_active
3305 sub cust_pkg_discount_active {
3307 grep { $_->status eq 'active' } $self->cust_pkg_discount;
3310 =item cust_pkg_usage
3312 Returns a list of all voice usage counters attached to this package.
3316 sub cust_pkg_usage {
3318 qsearch('cust_pkg_usage', { pkgnum => $self->pkgnum });
3321 =item apply_usage OPTIONS
3323 Takes the following options:
3324 - cdr: a call detail record (L<FS::cdr>)
3325 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
3326 - minutes: the maximum number of minutes to be charged
3328 Finds available usage minutes for a call of this class, and subtracts
3329 up to that many minutes from the usage pool. If the usage pool is empty,
3330 and the C<cdr-minutes_priority> global config option is set, minutes may
3331 be taken from other calls as well. Either way, an allocation record will
3332 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
3333 number of minutes of usage applied to the call.
3338 my ($self, %opt) = @_;
3339 my $cdr = $opt{cdr};
3340 my $rate_detail = $opt{rate_detail};
3341 my $minutes = $opt{minutes};
3342 my $classnum = $rate_detail->classnum;
3343 my $pkgnum = $self->pkgnum;
3344 my $custnum = $self->custnum;
3346 local $SIG{HUP} = 'IGNORE';
3347 local $SIG{INT} = 'IGNORE';
3348 local $SIG{QUIT} = 'IGNORE';
3349 local $SIG{TERM} = 'IGNORE';
3350 local $SIG{TSTP} = 'IGNORE';
3351 local $SIG{PIPE} = 'IGNORE';
3353 my $oldAutoCommit = $FS::UID::AutoCommit;
3354 local $FS::UID::AutoCommit = 0;
3356 my $order = FS::Conf->new->config('cdr-minutes_priority');
3360 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
3362 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
3364 my @usage_recs = qsearch({
3365 'table' => 'cust_pkg_usage',
3366 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
3367 ' JOIN cust_pkg USING (pkgnum)'.
3368 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
3369 'select' => 'cust_pkg_usage.*',
3370 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
3371 " ( cust_pkg.custnum = $custnum AND ".
3372 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3373 $is_classnum . ' AND '.
3374 " cust_pkg_usage.minutes > 0",
3375 'order_by' => " ORDER BY priority ASC",
3378 my $orig_minutes = $minutes;
3380 while (!$error and $minutes > 0 and @usage_recs) {
3381 my $cust_pkg_usage = shift @usage_recs;
3382 $cust_pkg_usage->select_for_update;
3383 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
3384 pkgusagenum => $cust_pkg_usage->pkgusagenum,
3385 acctid => $cdr->acctid,
3386 minutes => min($cust_pkg_usage->minutes, $minutes),
3388 $cust_pkg_usage->set('minutes',
3389 sprintf('%.0f', $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes)
3391 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
3392 $minutes -= $cdr_cust_pkg_usage->minutes;
3394 if ( $order and $minutes > 0 and !$error ) {
3395 # then try to steal minutes from another call
3397 'table' => 'cdr_cust_pkg_usage',
3398 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
3399 ' JOIN part_pkg_usage USING (pkgusagepart)'.
3400 ' JOIN cust_pkg USING (pkgnum)'.
3401 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
3402 ' JOIN cdr USING (acctid)',
3403 'select' => 'cdr_cust_pkg_usage.*',
3404 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
3405 " ( cust_pkg.pkgnum = $pkgnum OR ".
3406 " ( cust_pkg.custnum = $custnum AND ".
3407 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3408 " part_pkg_usage_class.classnum = $classnum",
3409 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
3411 if ( $order eq 'time' ) {
3412 # find CDRs that are using minutes, but have a later startdate
3414 my $startdate = $cdr->startdate;
3415 if ($startdate !~ /^\d+$/) {
3416 die "bad cdr startdate '$startdate'";
3418 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
3419 # minimize needless reshuffling
3420 $search{'order_by'} .= ', cdr.startdate DESC';
3422 # XXX may not work correctly with rate_time schedules. Could
3423 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
3425 $search{'addl_from'} .=
3426 ' JOIN rate_detail'.
3427 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
3428 if ( $order eq 'rate_high' ) {
3429 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
3430 $rate_detail->min_charge;
3431 $search{'order_by'} .= ', rate_detail.min_charge ASC';
3432 } elsif ( $order eq 'rate_low' ) {
3433 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
3434 $rate_detail->min_charge;
3435 $search{'order_by'} .= ', rate_detail.min_charge DESC';
3437 # this should really never happen
3438 die "invalid cdr-minutes_priority value '$order'\n";
3441 my @cdr_usage_recs = qsearch(\%search);
3443 while (!$error and @cdr_usage_recs and $minutes > 0) {
3444 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
3445 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
3446 my $old_cdr = $cdr_cust_pkg_usage->cdr;
3447 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
3448 $cdr_cust_pkg_usage->select_for_update;
3449 $old_cdr->select_for_update;
3450 $cust_pkg_usage->select_for_update;
3451 # in case someone else stole the usage from this CDR
3452 # while waiting for the lock...
3453 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
3454 # steal the usage allocation and flag the old CDR for reprocessing
3455 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
3456 # if the allocation is more minutes than we need, adjust it...
3457 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
3459 $cdr_cust_pkg_usage->set('minutes', $minutes);
3460 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
3461 $error = $cust_pkg_usage->replace;
3463 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
3464 $error ||= $cdr_cust_pkg_usage->replace;
3465 # deduct the stolen minutes
3466 $minutes -= $cdr_cust_pkg_usage->minutes;
3468 # after all minute-stealing is done, reset the affected CDRs
3469 foreach (values %reproc_cdrs) {
3470 $error ||= $_->set_status('');
3471 # XXX or should we just call $cdr->rate right here?
3472 # it's not like we can create a loop this way, since the min_charge
3473 # or call time has to go monotonically in one direction.
3474 # we COULD get some very deep recursions going, though...
3476 } # if $order and $minutes
3479 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
3481 $dbh->commit if $oldAutoCommit;
3482 return $orig_minutes - $minutes;
3486 =item supplemental_pkgs
3488 Returns a list of all packages supplemental to this one.
3492 sub supplemental_pkgs {
3494 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
3499 Returns the package that this one is supplemental to, if any.
3505 if ( $self->main_pkgnum ) {
3506 return FS::cust_pkg->by_key($self->main_pkgnum);
3513 =head1 CLASS METHODS
3519 Returns an SQL expression identifying recurring packages.
3523 sub recurring_sql { "
3524 '0' != ( select freq from part_pkg
3525 where cust_pkg.pkgpart = part_pkg.pkgpart )
3530 Returns an SQL expression identifying one-time packages.
3535 '0' = ( select freq from part_pkg
3536 where cust_pkg.pkgpart = part_pkg.pkgpart )
3541 Returns an SQL expression identifying ordered packages (recurring packages not
3547 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
3552 Returns an SQL expression identifying active packages.
3557 $_[0]->recurring_sql. "
3558 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
3559 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
3560 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
3563 =item not_yet_billed_sql
3565 Returns an SQL expression identifying packages which have not yet been billed.
3569 sub not_yet_billed_sql { "
3570 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
3571 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
3572 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
3577 Returns an SQL expression identifying inactive packages (one-time packages
3578 that are otherwise unsuspended/uncancelled).
3582 sub inactive_sql { "
3583 ". $_[0]->onetime_sql(). "
3584 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
3585 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
3586 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
3592 Returns an SQL expression identifying suspended packages.
3596 sub suspended_sql { susp_sql(@_); }
3598 #$_[0]->recurring_sql(). ' AND '.
3600 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
3601 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
3608 Returns an SQL exprression identifying cancelled packages.
3612 sub cancelled_sql { cancel_sql(@_); }
3614 #$_[0]->recurring_sql(). ' AND '.
3615 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
3620 Returns an SQL expression to give the package status as a string.
3626 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
3627 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
3628 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
3629 WHEN ".onetime_sql()." THEN 'one-time charge'
3634 =item search HASHREF
3638 Returns a qsearch hash expression to search for parameters specified in HASHREF.
3639 Valid parameters are
3647 active, inactive, suspended, cancel (or cancelled)
3651 active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
3655 boolean selects custom packages
3661 pkgpart or arrayref or hashref of pkgparts
3665 arrayref of beginning and ending epoch date
3669 arrayref of beginning and ending epoch date
3673 arrayref of beginning and ending epoch date
3677 arrayref of beginning and ending epoch date
3681 arrayref of beginning and ending epoch date
3685 arrayref of beginning and ending epoch date
3689 arrayref of beginning and ending epoch date
3693 pkgnum or APKG_pkgnum
3697 a value suited to passing to FS::UI::Web::cust_header
3701 specifies the user for agent virtualization
3705 boolean; if true, returns only packages with more than 0 FCC phone lines.
3707 =item state, country
3709 Limit to packages with a service location in the specified state and country.
3710 For FCC 477 reporting, mostly.
3717 my ($class, $params) = @_;
3724 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
3726 "cust_main.agentnum = $1";
3733 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
3735 "cust_pkg.custnum = $1";
3742 if ( $params->{'pkgbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
3744 "cust_pkg.pkgbatch = '$1'";
3751 if ( $params->{'magic'} eq 'active'
3752 || $params->{'status'} eq 'active' ) {
3754 push @where, FS::cust_pkg->active_sql();
3756 } elsif ( $params->{'magic'} =~ /^not[ _]yet[ _]billed$/
3757 || $params->{'status'} =~ /^not[ _]yet[ _]billed$/ ) {
3759 push @where, FS::cust_pkg->not_yet_billed_sql();
3761 } elsif ( $params->{'magic'} =~ /^(one-time charge|inactive)/
3762 || $params->{'status'} =~ /^(one-time charge|inactive)/ ) {
3764 push @where, FS::cust_pkg->inactive_sql();
3766 } elsif ( $params->{'magic'} eq 'suspended'
3767 || $params->{'status'} eq 'suspended' ) {
3769 push @where, FS::cust_pkg->suspended_sql();
3771 } elsif ( $params->{'magic'} =~ /^cancell?ed$/
3772 || $params->{'status'} =~ /^cancell?ed$/ ) {
3774 push @where, FS::cust_pkg->cancelled_sql();
3779 # parse package class
3782 if ( exists($params->{'classnum'}) ) {
3785 if ( ref($params->{'classnum'}) ) {
3787 if ( ref($params->{'classnum'}) eq 'HASH' ) {
3788 @classnum = grep $params->{'classnum'}{$_}, keys %{ $params->{'classnum'} };
3789 } elsif ( ref($params->{'classnum'}) eq 'ARRAY' ) {
3790 @classnum = @{ $params->{'classnum'} };
3792 die 'unhandled classnum ref '. $params->{'classnum'};
3796 } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
3803 my @nums = grep $_, @classnum;
3804 push @c_where, 'part_pkg.classnum IN ('. join(',',@nums). ')' if @nums;
3805 my $null = scalar( grep { $_ eq '' } @classnum );
3806 push @c_where, 'part_pkg.classnum IS NULL' if $null;
3808 if ( scalar(@c_where) == 1 ) {
3809 push @where, @c_where;
3810 } elsif ( @c_where ) {
3811 push @where, ' ( '. join(' OR ', @c_where). ' ) ';
3820 # parse package report options
3823 my @report_option = ();
3824 if ( exists($params->{'report_option'}) ) {
3825 if ( ref($params->{'report_option'}) eq 'ARRAY' ) {
3826 @report_option = @{ $params->{'report_option'} };
3827 } elsif ( $params->{'report_option'} =~ /^([,\d]*)$/ ) {
3828 @report_option = split(',', $1);
3833 if (@report_option) {
3834 # this will result in the empty set for the dangling comma case as it should
3836 map{ "0 < ( SELECT count(*) FROM part_pkg_option
3837 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
3838 AND optionname = 'report_option_$_'
3839 AND optionvalue = '1' )"
3843 foreach my $any ( grep /^report_option_any/, keys %$params ) {
3845 my @report_option_any = ();
3846 if ( ref($params->{$any}) eq 'ARRAY' ) {
3847 @report_option_any = @{ $params->{$any} };
3848 } elsif ( $params->{$any} =~ /^([,\d]*)$/ ) {
3849 @report_option_any = split(',', $1);
3852 if (@report_option_any) {
3853 # this will result in the empty set for the dangling comma case as it should
3854 push @where, ' ( '. join(' OR ',
3855 map{ "0 < ( SELECT count(*) FROM part_pkg_option
3856 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
3857 AND optionname = 'report_option_$_'
3858 AND optionvalue = '1' )"
3859 } @report_option_any
3869 push @where, "part_pkg.custom = 'Y'" if $params->{custom};
3875 push @where, "(part_pkg.fcc_ds0s > 0 OR pkg_class.fcc_ds0s > 0)"
3876 if $params->{fcc_line};
3882 if ( exists($params->{'censustract'}) ) {
3883 $params->{'censustract'} =~ /^([.\d]*)$/;
3884 my $censustract = "cust_location.censustract = '$1'";
3885 $censustract .= ' OR cust_location.censustract is NULL' unless $1;
3886 push @where, "( $censustract )";
3890 # parse censustract2
3892 if ( exists($params->{'censustract2'})
3893 && $params->{'censustract2'} =~ /^(\d*)$/
3897 push @where, "cust_location.censustract LIKE '$1%'";
3900 "( cust_location.censustract = '' OR cust_location.censustract IS NULL )";
3905 # parse country/state
3907 for (qw(state country)) { # parsing rules are the same for these
3908 if ( exists($params->{$_})
3909 && uc($params->{$_}) =~ /^([A-Z]{2})$/ )
3911 # XXX post-2.3 only--before that, state/country may be in cust_main
3912 push @where, "cust_location.$_ = '$1'";
3920 if ( ref($params->{'pkgpart'}) ) {
3923 if ( ref($params->{'pkgpart'}) eq 'HASH' ) {
3924 @pkgpart = grep $params->{'pkgpart'}{$_}, keys %{ $params->{'pkgpart'} };
3925 } elsif ( ref($params->{'pkgpart'}) eq 'ARRAY' ) {
3926 @pkgpart = @{ $params->{'pkgpart'} };
3928 die 'unhandled pkgpart ref '. $params->{'pkgpart'};
3931 @pkgpart = grep /^(\d+)$/, @pkgpart;
3933 push @where, 'pkgpart IN ('. join(',', @pkgpart). ')' if scalar(@pkgpart);
3935 } elsif ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
3936 push @where, "pkgpart = $1";
3945 #false laziness w/report_cust_pkg.html
3948 'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
3949 'active' => { 'susp'=>1, 'cancel'=>1 },
3950 'suspended' => { 'cancel' => 1 },
3955 if( exists($params->{'active'} ) ) {
3956 # This overrides all the other date-related fields
3957 my($beginning, $ending) = @{$params->{'active'}};
3959 "cust_pkg.setup IS NOT NULL",
3960 "cust_pkg.setup <= $ending",
3961 "(cust_pkg.cancel IS NULL OR cust_pkg.cancel >= $beginning )",
3962 "NOT (".FS::cust_pkg->onetime_sql . ")";
3965 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel )) {
3967 next unless exists($params->{$field});
3969 my($beginning, $ending) = @{$params->{$field}};
3971 next if $beginning == 0 && $ending == 4294967295;
3974 "cust_pkg.$field IS NOT NULL",
3975 "cust_pkg.$field >= $beginning",
3976 "cust_pkg.$field <= $ending";
3978 $orderby ||= "ORDER BY cust_pkg.$field";
3983 $orderby ||= 'ORDER BY bill';
3986 # parse magic, legacy, etc.
3989 if ( $params->{'magic'} &&
3990 $params->{'magic'} =~ /^(active|inactive|suspended|cancell?ed)$/
3993 $orderby = 'ORDER BY pkgnum';
3995 if ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
3996 push @where, "pkgpart = $1";
3999 } elsif ( $params->{'query'} eq 'pkgnum' ) {
4001 $orderby = 'ORDER BY pkgnum';
4003 } elsif ( $params->{'query'} eq 'APKG_pkgnum' ) {
4005 $orderby = 'ORDER BY pkgnum';
4008 SELECT count(*) FROM pkg_svc
4009 WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
4010 AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
4011 WHERE cust_svc.pkgnum = cust_pkg.pkgnum
4012 AND cust_svc.svcpart = pkg_svc.svcpart
4019 # setup queries, links, subs, etc. for the search
4022 # here is the agent virtualization
4023 if ($params->{CurrentUser}) {
4025 qsearchs('access_user', { username => $params->{CurrentUser} });
4028 push @where, $access_user->agentnums_sql('table'=>'cust_main');
4033 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql('table'=>'cust_main');
4036 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
4038 my $addl_from = 'LEFT JOIN part_pkg USING ( pkgpart ) '.
4039 'LEFT JOIN pkg_class ON ( part_pkg.classnum = pkg_class.classnum ) '.
4040 'LEFT JOIN cust_location USING ( locationnum ) '.
4041 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
4045 if ( $params->{'select_zip5'} ) {
4046 my $zip = 'cust_location.zip';
4048 $select = "DISTINCT substr($zip,1,5) as zip";
4049 $orderby = "ORDER BY substr($zip,1,5)";
4050 $count_query = "SELECT COUNT( DISTINCT substr($zip,1,5) )";
4052 $select = join(', ',
4054 ( map "part_pkg.$_", qw( pkg freq ) ),
4055 'pkg_class.classname',
4056 'cust_main.custnum AS cust_main_custnum',
4057 FS::UI::Web::cust_sql_fields(
4058 $params->{'cust_fields'}
4061 $count_query = 'SELECT COUNT(*)';
4064 $count_query .= " FROM cust_pkg $addl_from $extra_sql";
4067 'table' => 'cust_pkg',
4069 'select' => $select,
4070 'extra_sql' => $extra_sql,
4071 'order_by' => $orderby,
4072 'addl_from' => $addl_from,
4073 'count_query' => $count_query,
4080 Returns a list of two package counts. The first is a count of packages
4081 based on the supplied criteria and the second is the count of residential
4082 packages with those same criteria. Criteria are specified as in the search
4088 my ($class, $params) = @_;
4090 my $sql_query = $class->search( $params );
4092 my $count_sql = delete($sql_query->{'count_query'});
4093 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
4094 or die "couldn't parse count_sql";
4096 my $count_sth = dbh->prepare($count_sql)
4097 or die "Error preparing $count_sql: ". dbh->errstr;
4099 or die "Error executing $count_sql: ". $count_sth->errstr;
4100 my $count_arrayref = $count_sth->fetchrow_arrayref;
4102 return ( @$count_arrayref );
4106 =item tax_locationnum_sql
4108 Returns an SQL expression for the tax location for a package, based
4109 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
4113 sub tax_locationnum_sql {
4114 my $conf = FS::Conf->new;
4115 if ( $conf->exists('tax-pkg_address') ) {
4116 'cust_pkg.locationnum';
4118 elsif ( $conf->exists('tax-ship_address') ) {
4119 'cust_main.ship_locationnum';
4122 'cust_main.bill_locationnum';
4128 Returns a list: the first item is an SQL fragment identifying matching
4129 packages/customers via location (taking into account shipping and package
4130 address taxation, if enabled), and subsequent items are the parameters to
4131 substitute for the placeholders in that fragment.
4136 my($class, %opt) = @_;
4137 my $ornull = $opt{'ornull'};
4139 my $conf = new FS::Conf;
4141 # '?' placeholders in _location_sql_where
4142 my $x = $ornull ? 3 : 2;
4153 if ( $conf->exists('tax-ship_address') ) {
4156 ( ( ship_last IS NULL OR ship_last = '' )
4157 AND ". _location_sql_where('cust_main', '', $ornull ). "
4159 OR ( ship_last IS NOT NULL AND ship_last != ''
4160 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
4163 # AND payby != 'COMP'
4165 @main_param = ( @bill_param, @bill_param );
4169 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
4170 @main_param = @bill_param;
4176 if ( $conf->exists('tax-pkg_address') ) {
4178 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
4181 ( cust_pkg.locationnum IS NULL AND $main_where )
4182 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
4185 @param = ( @main_param, @bill_param );
4189 $where = $main_where;
4190 @param = @main_param;
4198 #subroutine, helper for location_sql
4199 sub _location_sql_where {
4201 my $prefix = @_ ? shift : '';
4202 my $ornull = @_ ? shift : '';
4204 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
4206 $ornull = $ornull ? ' OR ? IS NULL ' : '';
4208 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
4209 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
4210 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
4212 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
4214 # ( $table.${prefix}city = ? $or_empty_city $ornull )
4216 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4217 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4218 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
4219 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
4220 AND $table.${prefix}country = ?
4225 my( $self, $what ) = @_;
4227 my $what_show_zero = $what. '_show_zero';
4228 length($self->$what_show_zero())
4229 ? ($self->$what_show_zero() eq 'Y')
4230 : $self->part_pkg->$what_show_zero();
4237 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
4239 CUSTNUM is a customer (see L<FS::cust_main>)
4241 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4242 L<FS::part_pkg>) to order for this customer. Duplicates are of course
4245 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
4246 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
4247 new billing items. An error is returned if this is not possible (see
4248 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
4251 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4252 newly-created cust_pkg objects.
4254 REFNUM, if specified, will specify the FS::pkg_referral record to be created
4255 and inserted. Multiple FS::pkg_referral records can be created by
4256 setting I<refnum> to an array reference of refnums or a hash reference with
4257 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
4258 record will be created corresponding to cust_main.refnum.
4263 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
4265 my $conf = new FS::Conf;
4267 # Transactionize this whole mess
4268 local $SIG{HUP} = 'IGNORE';
4269 local $SIG{INT} = 'IGNORE';
4270 local $SIG{QUIT} = 'IGNORE';
4271 local $SIG{TERM} = 'IGNORE';
4272 local $SIG{TSTP} = 'IGNORE';
4273 local $SIG{PIPE} = 'IGNORE';
4275 my $oldAutoCommit = $FS::UID::AutoCommit;
4276 local $FS::UID::AutoCommit = 0;
4280 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
4281 # return "Customer not found: $custnum" unless $cust_main;
4283 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
4286 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4289 my $change = scalar(@old_cust_pkg) != 0;
4292 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
4294 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
4295 " to pkgpart ". $pkgparts->[0]. "\n"
4298 my $err_or_cust_pkg =
4299 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
4300 'refnum' => $refnum,
4303 unless (ref($err_or_cust_pkg)) {
4304 $dbh->rollback if $oldAutoCommit;
4305 return $err_or_cust_pkg;
4308 push @$return_cust_pkg, $err_or_cust_pkg;
4309 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4314 # Create the new packages.
4315 foreach my $pkgpart (@$pkgparts) {
4317 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
4319 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
4320 pkgpart => $pkgpart,
4324 $error = $cust_pkg->insert( 'change' => $change );
4325 push @$return_cust_pkg, $cust_pkg;
4327 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
4328 my $supp_pkg = FS::cust_pkg->new({
4329 custnum => $custnum,
4330 pkgpart => $link->dst_pkgpart,
4332 main_pkgnum => $cust_pkg->pkgnum,
4335 $error ||= $supp_pkg->insert( 'change' => $change );
4336 push @$return_cust_pkg, $supp_pkg;
4340 $dbh->rollback if $oldAutoCommit;
4345 # $return_cust_pkg now contains refs to all of the newly
4348 # Transfer services and cancel old packages.
4349 foreach my $old_pkg (@old_cust_pkg) {
4351 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
4354 foreach my $new_pkg (@$return_cust_pkg) {
4355 $error = $old_pkg->transfer($new_pkg);
4356 if ($error and $error == 0) {
4357 # $old_pkg->transfer failed.
4358 $dbh->rollback if $oldAutoCommit;
4363 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
4364 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
4365 foreach my $new_pkg (@$return_cust_pkg) {
4366 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
4367 if ($error and $error == 0) {
4368 # $old_pkg->transfer failed.
4369 $dbh->rollback if $oldAutoCommit;
4376 # Transfers were successful, but we went through all of the
4377 # new packages and still had services left on the old package.
4378 # We can't cancel the package under the circumstances, so abort.
4379 $dbh->rollback if $oldAutoCommit;
4380 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
4382 $error = $old_pkg->cancel( quiet=>1 );
4388 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4392 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
4394 A bulk change method to change packages for multiple customers.
4396 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4397 L<FS::part_pkg>) to order for each customer. Duplicates are of course
4400 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
4401 replace. The services (see L<FS::cust_svc>) are moved to the
4402 new billing items. An error is returned if this is not possible (see
4405 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4406 newly-created cust_pkg objects.
4411 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
4413 # Transactionize this whole mess
4414 local $SIG{HUP} = 'IGNORE';
4415 local $SIG{INT} = 'IGNORE';
4416 local $SIG{QUIT} = 'IGNORE';
4417 local $SIG{TERM} = 'IGNORE';
4418 local $SIG{TSTP} = 'IGNORE';
4419 local $SIG{PIPE} = 'IGNORE';
4421 my $oldAutoCommit = $FS::UID::AutoCommit;
4422 local $FS::UID::AutoCommit = 0;
4426 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4429 while(scalar(@old_cust_pkg)) {
4431 my $custnum = $old_cust_pkg[0]->custnum;
4432 my (@remove) = map { $_->pkgnum }
4433 grep { $_->custnum == $custnum } @old_cust_pkg;
4434 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
4436 my $error = order $custnum, $pkgparts, \@remove, \@return;
4438 push @errors, $error
4440 push @$return_cust_pkg, @return;
4443 if (scalar(@errors)) {
4444 $dbh->rollback if $oldAutoCommit;
4445 return join(' / ', @errors);
4448 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4452 # Used by FS::Upgrade to migrate to a new database.
4453 sub _upgrade_data { # class method
4454 my ($class, %opts) = @_;
4455 $class->_upgrade_otaker(%opts);
4457 # RT#10139, bug resulting in contract_end being set when it shouldn't
4458 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
4459 # RT#10830, bad calculation of prorate date near end of year
4460 # the date range for bill is December 2009, and we move it forward
4461 # one year if it's before the previous bill date (which it should
4463 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
4464 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
4465 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
4466 # RT6628, add order_date to cust_pkg
4467 'update cust_pkg set order_date = (select history_date from h_cust_pkg
4468 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
4469 history_action = \'insert\') where order_date is null',
4471 foreach my $sql (@statements) {
4472 my $sth = dbh->prepare($sql);
4473 $sth->execute or die $sth->errstr;
4481 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
4483 In sub order, the @pkgparts array (passed by reference) is clobbered.
4485 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
4486 method to pass dates to the recur_prog expression, it should do so.
4488 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
4489 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
4490 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
4491 configuration values. Probably need a subroutine which decides what to do
4492 based on whether or not we've fetched the user yet, rather than a hash. See
4493 FS::UID and the TODO.
4495 Now that things are transactional should the check in the insert method be
4500 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
4501 L<FS::pkg_svc>, schema.html from the base documentation