2 use base qw( FS::cust_pkg::Search FS::cust_pkg::API
3 FS::otaker_Mixin FS::cust_main_Mixin FS::Sales_Mixin
4 FS::contact_Mixin FS::location_Mixin
5 FS::m2m_Common FS::option_Common
10 use Scalar::Util qw( blessed );
11 use List::Util qw(min max);
13 use Time::Local qw( timelocal timelocal_nocheck );
15 use FS::UID qw( dbh driver_name );
16 use FS::Misc qw( send_email );
17 use FS::Record qw( qsearch qsearchs fields );
23 use FS::cust_location;
25 use FS::cust_bill_pkg;
26 use FS::cust_pkg_detail;
27 use FS::cust_pkg_usage;
28 use FS::cdr_cust_pkg_usage;
33 use FS::cust_pkg_reason;
35 use FS::cust_pkg_usageprice;
36 use FS::cust_pkg_discount;
42 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
44 # because they load configuration by setting FS::UID::callback (see TODO)
50 # for sending cancel emails in sub cancel
53 our ($disable_agentcheck, $DEBUG, $me, $import) = (0, 0, '[FS::cust_pkg]', 0);
55 our $upgrade = 0; #go away after setup+start dates cleaned up for old customers
59 my ( $hashref, $cache ) = @_;
60 #if ( $hashref->{'pkgpart'} ) {
61 if ( $hashref->{'pkg'} ) {
62 # #@{ $self->{'_pkgnum'} } = ();
63 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
64 # $self->{'_pkgpart'} = $subcache;
65 # #push @{ $self->{'_pkgnum'} },
66 # FS::part_pkg->new_or_cached($hashref, $subcache);
67 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
69 if ( exists $hashref->{'svcnum'} ) {
70 #@{ $self->{'_pkgnum'} } = ();
71 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
72 $self->{'_svcnum'} = $subcache;
73 #push @{ $self->{'_pkgnum'} },
74 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
80 FS::cust_pkg - Object methods for cust_pkg objects
86 $record = new FS::cust_pkg \%hash;
87 $record = new FS::cust_pkg { 'column' => 'value' };
89 $error = $record->insert;
91 $error = $new_record->replace($old_record);
93 $error = $record->delete;
95 $error = $record->check;
97 $error = $record->cancel;
99 $error = $record->suspend;
101 $error = $record->unsuspend;
103 $part_pkg = $record->part_pkg;
105 @labels = $record->labels;
107 $seconds = $record->seconds_since($timestamp);
109 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
110 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
114 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
115 inherits from FS::Record. The following fields are currently supported:
121 Primary key (assigned automatically for new billing items)
125 Customer (see L<FS::cust_main>)
129 Billing item definition (see L<FS::part_pkg>)
133 Optional link to package location (see L<FS::location>)
137 date package was ordered (also remains same on changes)
149 date (next bill date)
177 order taker (see L<FS::access_user>)
181 If this field is set to 1, disables the automatic
182 unsuspension of this package when using the B<unsuspendauto> config option.
186 If not set, defaults to 1
190 Date of change from previous package
200 =item change_locationnum
208 The pkgnum of the package that this package is supplemental to, if any.
212 The package link (L<FS::part_pkg_link>) that defines this supplemental
213 package, if it is one.
215 =item change_to_pkgnum
217 The pkgnum of the package this one will be "changed to" in the future
218 (on its expiration date).
222 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
223 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
224 L<Time::Local> and L<Date::Parse> for conversion functions.
232 Create a new billing item. To add the item to the database, see L<"insert">.
236 sub table { 'cust_pkg'; }
237 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
238 sub cust_unlinked_msg {
240 "WARNING: can't find cust_main.custnum ". $self->custnum.
241 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
244 =item set_initial_timers
246 If required by the package definition, sets any automatic expire, adjourn,
247 or contract_end timers to some number of months after the start date
248 (or setup date, if the package has already been setup). If the package has
249 a delayed setup fee after a period of "free days", will also set the
250 start date to the end of that period.
254 sub set_initial_timers {
256 my $part_pkg = $self->part_pkg;
257 foreach my $action ( qw(expire adjourn contract_end) ) {
258 my $months = $part_pkg->option("${action}_months",1);
259 if($months and !$self->get($action)) {
260 my $start = $self->start_date || $self->setup || time;
261 $self->set($action, $part_pkg->add_freq($start, $months) );
265 # if this package has "free days" and delayed setup fee, then
266 # set start date that many days in the future.
267 # (this should have been set in the UI, but enforce it here)
268 if ( $part_pkg->option('free_days',1)
269 && $part_pkg->option('delay_setup',1)
272 $self->start_date( $part_pkg->default_start_date );
277 =item insert [ OPTION => VALUE ... ]
279 Adds this billing item to the database ("Orders" the item). If there is an
280 error, returns the error, otherwise returns false.
282 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
283 will be used to look up the package definition and agent restrictions will be
286 If the additional field I<refnum> is defined, an FS::pkg_referral record will
287 be created and inserted. Multiple FS::pkg_referral records can be created by
288 setting I<refnum> to an array reference of refnums or a hash reference with
289 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
290 record will be created corresponding to cust_main.refnum.
292 If the additional field I<cust_pkg_usageprice> is defined, it will be treated
293 as an arrayref of FS::cust_pkg_usageprice objects, which will be inserted.
294 (Note that this field cannot be set with a usual ->cust_pkg_usageprice method.
295 It can be set as part of the hash when creating the object, or with the B<set>
298 The following options are available:
304 If set true, supresses actions that should only be taken for new package
305 orders. (Currently this includes: intro periods when delay_setup is on,
306 auto-adding a 1st start date, auto-adding expiration/adjourn/contract_end dates)
310 cust_pkg_option records will be created
314 a ticket will be added to this customer with this subject
318 an optional queue name for ticket additions
322 Don't check the legality of the package definition. This should be used
323 when performing a package change that doesn't change the pkgpart (i.e.
331 my( $self, %options ) = @_;
334 $error = $self->check_pkgpart unless $options{'allow_pkgpart'};
335 return $error if $error;
337 my $part_pkg = $self->part_pkg;
339 if ( ! $import && ! $options{'change'} ) {
341 # set order date to now
342 $self->order_date(time) unless ($import && $self->order_date);
344 # if the package def says to start only on the first of the month:
345 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
346 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
347 $mon += 1 unless $mday == 1;
348 until ( $mon < 12 ) { $mon -= 12; $year++; }
349 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
352 if ($self->susp eq 'now' or $part_pkg->start_on_hold) {
353 # if the package was ordered on hold:
355 # - don't set the start date (it will be started manually)
356 $self->set('susp', $self->order_date);
357 $self->set('start_date', '');
359 # set expire/adjourn/contract_end timers, and free days, if appropriate
360 $self->set_initial_timers;
362 } # else this is a package change, and shouldn't have "new package" behavior
364 my $oldAutoCommit = $FS::UID::AutoCommit;
365 local $FS::UID::AutoCommit = 0;
368 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
370 $dbh->rollback if $oldAutoCommit;
374 $self->refnum($self->cust_main->refnum) unless $self->refnum;
375 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
376 $self->process_m2m( 'link_table' => 'pkg_referral',
377 'target_table' => 'part_referral',
378 'params' => $self->refnum,
381 if ( $self->hashref->{cust_pkg_usageprice} ) {
382 for my $cust_pkg_usageprice ( @{ $self->hashref->{cust_pkg_usageprice} } ) {
383 $cust_pkg_usageprice->pkgnum( $self->pkgnum );
384 my $error = $cust_pkg_usageprice->insert;
386 $dbh->rollback if $oldAutoCommit;
392 if ( $self->discountnum ) {
393 my $error = $self->insert_discount();
395 $dbh->rollback if $oldAutoCommit;
400 my $conf = new FS::Conf;
402 if ( ! $import && $conf->config('ticket_system') && $options{ticket_subject} ) {
404 #this init stuff is still inefficient, but at least its limited to
405 # the small number (any?) folks using ticket emailing on pkg order
408 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
415 use FS::TicketSystem;
416 FS::TicketSystem->init();
418 my $q = new RT::Queue($RT::SystemUser);
419 $q->Load($options{ticket_queue}) if $options{ticket_queue};
420 my $t = new RT::Ticket($RT::SystemUser);
421 my $mime = new MIME::Entity;
422 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
423 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
424 Subject => $options{ticket_subject},
427 $t->AddLink( Type => 'MemberOf',
428 Target => 'freeside://freeside/cust_main/'. $self->custnum,
432 if (! $import && $conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
433 my $queue = new FS::queue {
434 'job' => 'FS::cust_main::queueable_print',
436 $error = $queue->insert(
437 'custnum' => $self->custnum,
438 'template' => 'welcome_letter',
442 warn "can't send welcome letter: $error";
447 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
454 This method now works but you probably shouldn't use it.
456 You don't want to delete packages, because there would then be no record
457 the customer ever purchased the package. Instead, see the cancel method and
458 hide cancelled packages.
465 my $oldAutoCommit = $FS::UID::AutoCommit;
466 local $FS::UID::AutoCommit = 0;
469 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
470 my $error = $cust_pkg_discount->delete;
472 $dbh->rollback if $oldAutoCommit;
476 #cust_bill_pkg_discount?
478 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
479 my $error = $cust_pkg_detail->delete;
481 $dbh->rollback if $oldAutoCommit;
486 foreach my $cust_pkg_reason (
488 'table' => 'cust_pkg_reason',
489 'hashref' => { 'pkgnum' => $self->pkgnum },
493 my $error = $cust_pkg_reason->delete;
495 $dbh->rollback if $oldAutoCommit;
502 my $error = $self->SUPER::delete(@_);
504 $dbh->rollback if $oldAutoCommit;
508 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
514 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
516 Replaces the OLD_RECORD with this one in the database. If there is an error,
517 returns the error, otherwise returns false.
519 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
521 Changing pkgpart may have disasterous effects. See the order subroutine.
523 setup and bill are normally updated by calling the bill method of a customer
524 object (see L<FS::cust_main>).
526 suspend is normally updated by the suspend and unsuspend methods.
528 cancel is normally updated by the cancel method (and also the order subroutine
531 Available options are:
537 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.
541 the access_user (see L<FS::access_user>) providing the reason
545 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
554 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
559 ( ref($_[0]) eq 'HASH' )
563 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
564 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
567 #return "Can't change setup once it exists!"
568 # if $old->getfield('setup') &&
569 # $old->getfield('setup') != $new->getfield('setup');
571 #some logic for bill, susp, cancel?
573 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
575 my $oldAutoCommit = $FS::UID::AutoCommit;
576 local $FS::UID::AutoCommit = 0;
579 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
580 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
581 my $error = $new->insert_reason(
582 'reason' => $options->{'reason'},
583 'date' => $new->$method,
585 'reason_otaker' => $options->{'reason_otaker'},
588 dbh->rollback if $oldAutoCommit;
589 return "Error inserting cust_pkg_reason: $error";
594 #save off and freeze RADIUS attributes for any associated svc_acct records
596 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
598 #also check for specific exports?
599 # to avoid spurious modify export events
600 @svc_acct = map { $_->svc_x }
601 grep { $_->part_svc->svcdb eq 'svc_acct' }
604 $_->snapshot foreach @svc_acct;
608 my $error = $new->export_pkg_change($old)
609 || $new->SUPER::replace( $old,
611 ? $options->{options}
615 $dbh->rollback if $oldAutoCommit;
619 #for prepaid packages,
620 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
621 foreach my $old_svc_acct ( @svc_acct ) {
622 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
624 $new_svc_acct->replace( $old_svc_acct,
625 'depend_jobnum' => $options->{depend_jobnum},
628 $dbh->rollback if $oldAutoCommit;
633 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
640 Checks all fields to make sure this is a valid billing item. If there is an
641 error, returns the error, otherwise returns false. Called by the insert and
649 if ( !$self->locationnum or $self->locationnum == -1 ) {
650 $self->set('locationnum', $self->cust_main->ship_locationnum);
654 $self->ut_numbern('pkgnum')
655 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
656 || $self->ut_numbern('pkgpart')
657 || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' )
658 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
659 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
660 || $self->ut_numbern('quantity')
661 || $self->ut_numbern('start_date')
662 || $self->ut_numbern('setup')
663 || $self->ut_numbern('bill')
664 || $self->ut_numbern('susp')
665 || $self->ut_numbern('cancel')
666 || $self->ut_numbern('adjourn')
667 || $self->ut_numbern('resume')
668 || $self->ut_numbern('expire')
669 || $self->ut_numbern('dundate')
670 || $self->ut_enum('no_auto', [ '', 'Y' ])
671 || $self->ut_enum('waive_setup', [ '', 'Y' ])
672 || $self->ut_textn('agent_pkgid')
673 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
674 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
675 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
676 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
677 || $self->ut_foreign_keyn('change_to_pkgnum', 'cust_pkg', 'pkgnum')
679 return $error if $error;
681 return "A package with both start date (future start) and setup date (already started) will never bill"
682 if $self->start_date && $self->setup && ! $upgrade;
684 return "A future unsuspend date can only be set for a package with a suspend date"
685 if $self->resume and !$self->susp and !$self->adjourn;
687 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
689 if ( $self->dbdef_table->column('manual_flag') ) {
690 $self->manual_flag('') if $self->manual_flag eq ' ';
691 $self->manual_flag =~ /^([01]?)$/
692 or return "Illegal manual_flag ". $self->manual_flag;
693 $self->manual_flag($1);
701 Check the pkgpart to make sure it's allowed with the reg_code and/or
702 promo_code of the package (if present) and with the customer's agent.
703 Called from C<insert>, unless we are doing a package change that doesn't
711 # my $error = $self->ut_numbern('pkgpart'); # already done
714 if ( $self->reg_code ) {
716 unless ( grep { $self->pkgpart == $_->pkgpart }
717 map { $_->reg_code_pkg }
718 qsearchs( 'reg_code', { 'code' => $self->reg_code,
719 'agentnum' => $self->cust_main->agentnum })
721 return "Unknown registration code";
724 } elsif ( $self->promo_code ) {
727 qsearchs('part_pkg', {
728 'pkgpart' => $self->pkgpart,
729 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
731 return 'Unknown promotional code' unless $promo_part_pkg;
735 unless ( $disable_agentcheck ) {
737 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
738 return "agent ". $agent->agentnum. ':'. $agent->agent.
739 " can't purchase pkgpart ". $self->pkgpart
740 unless $agent->pkgpart_hashref->{ $self->pkgpart }
741 || $agent->agentnum == $self->part_pkg->agentnum;
744 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
745 return $error if $error;
753 =item cancel [ OPTION => VALUE ... ]
755 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
756 in this package, then cancels the package itself (sets the cancel field to
759 Available options are:
763 =item quiet - can be set true to supress email cancellation notices.
765 =item time - can be set to cancel the package based on a specific future or
766 historical date. Using time ensures that the remaining amount is calculated
767 correctly. Note however that this is an immediate cancel and just changes
768 the date. You are PROBABLY looking to expire the account instead of using
771 =item reason - can be set to a cancellation reason (see L<FS:reason>),
772 either a reasonnum of an existing reason, or passing a hashref will create
773 a new reason. The hashref should have the following keys: typenum - Reason
774 type (see L<FS::reason_type>, reason - Text of the new reason.
776 =item date - can be set to a unix style timestamp to specify when to
779 =item nobill - can be set true to skip billing if it might otherwise be done.
781 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
782 not credit it. This must be set (by change()) when changing the package
783 to a different pkgpart or location, and probably shouldn't be in any other
784 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
789 If there is an error, returns the error, otherwise returns false.
794 my( $self, %options ) = @_;
797 # pass all suspend/cancel actions to the main package
798 # (unless the pkglinknum has been removed, then the link is defunct and
799 # this package can be canceled on its own)
800 if ( $self->main_pkgnum and $self->pkglinknum and !$options{'from_main'} ) {
801 return $self->main_pkg->cancel(%options);
804 my $conf = new FS::Conf;
806 warn "cust_pkg::cancel called with options".
807 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
810 my $oldAutoCommit = $FS::UID::AutoCommit;
811 local $FS::UID::AutoCommit = 0;
814 my $old = $self->select_for_update;
816 if ( $old->get('cancel') || $self->get('cancel') ) {
817 dbh->rollback if $oldAutoCommit;
818 return ""; # no error
821 # XXX possibly set cancel_time to the expire date?
822 my $cancel_time = $options{'time'} || time;
823 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
824 $date = '' if ($date && $date <= $cancel_time); # complain instead?
826 my $delay_cancel = undef;
827 if ( !$date && $self->part_pkg->option('delay_cancel',1)
828 && (($self->status eq 'active') || ($self->status eq 'suspended'))
830 my $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
831 my $expsecs = 60*60*24*$expdays;
832 my $suspfor = $self->susp ? $cancel_time - $self->susp : 0;
833 $expsecs = $expsecs - $suspfor if $suspfor;
834 unless ($expsecs <= 0) { #if it's already been suspended long enough, don't re-suspend
836 $date = $cancel_time + $expsecs;
840 #race condition: usage could be ongoing until unprovisioned
841 #resolved by performing a change package instead (which unprovisions) and
843 if ( !$options{nobill} && !$date ) {
844 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
845 my $copy = $self->new({$self->hash});
847 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
849 'time' => $cancel_time );
850 warn "Error billing during cancel, custnum ".
851 #$self->cust_main->custnum. ": $error"
856 if ( $options{'reason'} ) {
857 $error = $self->insert_reason( 'reason' => $options{'reason'},
858 'action' => $date ? 'expire' : 'cancel',
859 'date' => $date ? $date : $cancel_time,
860 'reason_otaker' => $options{'reason_otaker'},
863 dbh->rollback if $oldAutoCommit;
864 return "Error inserting cust_pkg_reason: $error";
868 my %svc_cancel_opt = ();
869 $svc_cancel_opt{'date'} = $date if $date;
870 foreach my $cust_svc (
873 sort { $a->[1] <=> $b->[1] }
874 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
875 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
877 my $part_svc = $cust_svc->part_svc;
878 next if ( defined($part_svc) and $part_svc->preserve );
879 my $error = $cust_svc->cancel( %svc_cancel_opt );
882 $dbh->rollback if $oldAutoCommit;
883 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
889 # credit remaining time if appropriate
891 if ( exists($options{'unused_credit'}) ) {
892 $do_credit = $options{'unused_credit'};
895 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
898 my $error = $self->credit_remaining('cancel', $cancel_time);
900 $dbh->rollback if $oldAutoCommit;
907 my %hash = $self->hash;
909 $hash{'expire'} = $date;
911 $hash{'susp'} = $cancel_time unless $self->susp;
912 $hash{'adjourn'} = undef;
913 $hash{'resume'} = undef;
916 $hash{'cancel'} = $cancel_time;
918 $hash{'change_custnum'} = $options{'change_custnum'};
920 # if this is a supplemental package that's lost its part_pkg_link, and it's
921 # being canceled for real, unlink it completely
922 if ( !$date and ! $self->pkglinknum ) {
923 $hash{main_pkgnum} = '';
926 my $new = new FS::cust_pkg ( \%hash );
927 $error = $new->replace( $self, options => { $self->options } );
928 if ( $self->change_to_pkgnum ) {
929 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
930 $error ||= $change_to->cancel || $change_to->delete;
933 $dbh->rollback if $oldAutoCommit;
937 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
938 $error = $supp_pkg->cancel(%options, 'from_main' => 1);
940 $dbh->rollback if $oldAutoCommit;
941 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
945 foreach my $usage ( $self->cust_pkg_usage ) {
946 $error = $usage->delete;
948 $dbh->rollback if $oldAutoCommit;
949 return "deleting usage pools: $error";
953 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
954 return '' if $date; #no errors
956 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
957 if ( !$options{'quiet'} &&
958 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
960 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
963 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
964 $error = $msg_template->send( 'cust_main' => $self->cust_main,
969 'from' => $conf->invoice_from_full( $self->cust_main->agentnum ),
970 'to' => \@invoicing_list,
971 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
972 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
973 'custnum' => $self->custnum,
974 'msgtype' => '', #admin?
977 #should this do something on errors?
984 =item cancel_if_expired [ NOW_TIMESTAMP ]
986 Cancels this package if its expire date has been reached.
990 sub cancel_if_expired {
992 my $time = shift || time;
993 return '' unless $self->expire && $self->expire <= $time;
994 my $error = $self->cancel;
996 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
997 $self->custnum. ": $error";
1004 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
1005 locationnum, (other fields?). Attempts to re-provision cancelled services
1006 using history information (errors at this stage are not fatal).
1008 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
1010 svc_fatal: service provisioning errors are fatal
1012 svc_errors: pass an array reference, will be filled in with any provisioning errors
1014 main_pkgnum: link the package as a supplemental package of this one. For
1020 my( $self, %options ) = @_;
1022 #in case you try do do $uncancel-date = $cust_pkg->uncacel
1023 return '' unless $self->get('cancel');
1025 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
1026 return $self->main_pkg->uncancel(%options);
1033 my $oldAutoCommit = $FS::UID::AutoCommit;
1034 local $FS::UID::AutoCommit = 0;
1038 # insert the new package
1041 my $cust_pkg = new FS::cust_pkg {
1042 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
1043 bill => ( $options{'bill'} || $self->get('bill') ),
1045 uncancel_pkgnum => $self->pkgnum,
1046 main_pkgnum => ($options{'main_pkgnum'} || ''),
1047 map { $_ => $self->get($_) } qw(
1048 custnum pkgpart locationnum
1050 susp adjourn resume expire start_date contract_end dundate
1051 change_date change_pkgpart change_locationnum
1052 manual_flag no_auto quantity agent_pkgid recur_show_zero setup_show_zero
1056 my $error = $cust_pkg->insert(
1057 'change' => 1, #supresses any referral credit to a referring customer
1058 'allow_pkgpart' => 1, # allow this even if the package def is disabled
1061 $dbh->rollback if $oldAutoCommit;
1069 #find historical services within this timeframe before the package cancel
1070 # (incompatible with "time" option to cust_pkg->cancel?)
1071 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
1072 # too little? (unprovisioing export delay?)
1073 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
1074 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1077 foreach my $h_cust_svc (@h_cust_svc) {
1078 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1079 #next unless $h_svc_x; #should this happen?
1080 (my $table = $h_svc_x->table) =~ s/^h_//;
1081 require "FS/$table.pm";
1082 my $class = "FS::$table";
1083 my $svc_x = $class->new( {
1084 'pkgnum' => $cust_pkg->pkgnum,
1085 'svcpart' => $h_cust_svc->svcpart,
1086 map { $_ => $h_svc_x->get($_) } fields($table)
1090 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1091 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1094 my $svc_error = $svc_x->insert;
1096 if ( $options{svc_fatal} ) {
1097 $dbh->rollback if $oldAutoCommit;
1100 # if we've failed to insert the svc_x object, svc_Common->insert
1101 # will have removed the cust_svc already. if not, then both records
1102 # were inserted but we failed for some other reason (export, most
1103 # likely). in that case, report the error and delete the records.
1104 push @svc_errors, $svc_error;
1105 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1107 # except if export_insert failed, export_delete probably won't be
1109 local $FS::svc_Common::noexport_hack = 1;
1110 my $cleanup_error = $svc_x->delete; # also deletes cust_svc
1111 if ( $cleanup_error ) { # and if THAT fails, then run away
1112 $dbh->rollback if $oldAutoCommit;
1113 return $cleanup_error;
1118 } #foreach $h_cust_svc
1120 #these are pretty rare, but should handle them
1121 # - dsl_device (mac addresses)
1122 # - phone_device (mac addresses)
1123 # - dsl_note (ikano notes)
1124 # - domain_record (i.e. restore DNS information w/domains)
1125 # - inventory_item(?) (inventory w/un-cancelling service?)
1126 # - nas (svc_broaband nas stuff)
1127 #this stuff is unused in the wild afaik
1128 # - mailinglistmember
1130 # - svc_domain.parent_svcnum?
1131 # - acct_snarf (ancient mail fetching config)
1132 # - cgp_rule (communigate)
1133 # - cust_svc_option (used by our Tron stuff)
1134 # - acct_rt_transaction (used by our time worked stuff)
1137 # also move over any services that didn't unprovision at cancellation
1140 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1141 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1142 my $error = $cust_svc->replace;
1144 $dbh->rollback if $oldAutoCommit;
1150 # Uncancel any supplemental packages, and make them supplemental to the
1154 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1156 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1158 $dbh->rollback if $oldAutoCommit;
1159 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1167 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1169 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1170 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1177 Cancels any pending expiration (sets the expire field to null).
1179 If there is an error, returns the error, otherwise returns false.
1184 my( $self, %options ) = @_;
1187 my $oldAutoCommit = $FS::UID::AutoCommit;
1188 local $FS::UID::AutoCommit = 0;
1191 my $old = $self->select_for_update;
1193 my $pkgnum = $old->pkgnum;
1194 if ( $old->get('cancel') || $self->get('cancel') ) {
1195 dbh->rollback if $oldAutoCommit;
1196 return "Can't unexpire cancelled package $pkgnum";
1197 # or at least it's pointless
1200 unless ( $old->get('expire') && $self->get('expire') ) {
1201 dbh->rollback if $oldAutoCommit;
1202 return ""; # no error
1205 my %hash = $self->hash;
1206 $hash{'expire'} = '';
1207 my $new = new FS::cust_pkg ( \%hash );
1208 $error = $new->replace( $self, options => { $self->options } );
1210 $dbh->rollback if $oldAutoCommit;
1214 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1220 =item suspend [ OPTION => VALUE ... ]
1222 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1223 package, then suspends the package itself (sets the susp field to now).
1225 Available options are:
1229 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1230 either a reasonnum of an existing reason, or passing a hashref will create
1231 a new reason. The hashref should have the following keys:
1232 - typenum - Reason type (see L<FS::reason_type>
1233 - reason - Text of the new reason.
1235 =item date - can be set to a unix style timestamp to specify when to
1238 =item time - can be set to override the current time, for calculation
1239 of final invoices or unused-time credits
1241 =item resume_date - can be set to a time when the package should be
1242 unsuspended. This may be more convenient than calling C<unsuspend()>
1245 =item from_main - allows a supplemental package to be suspended, rather
1246 than redirecting the method call to its main package. For internal use.
1250 If there is an error, returns the error, otherwise returns false.
1255 my( $self, %options ) = @_;
1258 # pass all suspend/cancel actions to the main package
1259 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1260 return $self->main_pkg->suspend(%options);
1263 my $oldAutoCommit = $FS::UID::AutoCommit;
1264 local $FS::UID::AutoCommit = 0;
1267 my $old = $self->select_for_update;
1269 my $pkgnum = $old->pkgnum;
1270 if ( $old->get('cancel') || $self->get('cancel') ) {
1271 dbh->rollback if $oldAutoCommit;
1272 return "Can't suspend cancelled package $pkgnum";
1275 if ( $old->get('susp') || $self->get('susp') ) {
1276 dbh->rollback if $oldAutoCommit;
1277 return ""; # no error # complain on adjourn?
1280 my $suspend_time = $options{'time'} || time;
1281 my $date = $options{date} if $options{date}; # adjourn/suspend later
1282 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1284 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1285 dbh->rollback if $oldAutoCommit;
1286 return "Package $pkgnum expires before it would be suspended.";
1289 # some false laziness with sub cancel
1290 if ( !$options{nobill} && !$date &&
1291 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1292 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1293 # make the entire cust_main->bill path recognize 'suspend' and
1294 # 'cancel' separately.
1295 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1296 my $copy = $self->new({$self->hash});
1298 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1300 'time' => $suspend_time );
1301 warn "Error billing during suspend, custnum ".
1302 #$self->cust_main->custnum. ": $error"
1307 if ( $options{'reason'} ) {
1308 $error = $self->insert_reason( 'reason' => $options{'reason'},
1309 'action' => $date ? 'adjourn' : 'suspend',
1310 'date' => $date ? $date : $suspend_time,
1311 'reason_otaker' => $options{'reason_otaker'},
1314 dbh->rollback if $oldAutoCommit;
1315 return "Error inserting cust_pkg_reason: $error";
1319 # if a reasonnum was passed, get the actual reason object so we can check
1321 # (passing a reason hashref is still allowed, but it can't be used with
1322 # the fancy behavioral options.)
1325 if ($options{'reason'} =~ /^\d+$/) {
1326 $reason = FS::reason->by_key($options{'reason'});
1329 my %hash = $self->hash;
1331 $hash{'adjourn'} = $date;
1333 $hash{'susp'} = $suspend_time;
1336 my $resume_date = $options{'resume_date'} || 0;
1337 if ( $resume_date > ($date || $suspend_time) ) {
1338 $hash{'resume'} = $resume_date;
1341 $options{options} ||= {};
1343 my $new = new FS::cust_pkg ( \%hash );
1344 $error = $new->replace( $self, options => { $self->options,
1345 %{ $options{options} },
1349 $dbh->rollback if $oldAutoCommit;
1353 unless ( $date ) { # then we are suspending now
1355 # credit remaining time if appropriate
1356 # (if required by the package def, or the suspend reason)
1357 my $unused_credit = $self->part_pkg->option('unused_credit_suspend',1)
1358 || ( defined($reason) && $reason->unused_credit );
1360 if ( $unused_credit ) {
1361 warn "crediting unused time on pkg#".$self->pkgnum."\n" if $DEBUG;
1362 my $error = $self->credit_remaining('suspend', $suspend_time);
1364 $dbh->rollback if $oldAutoCommit;
1371 foreach my $cust_svc (
1372 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
1374 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1376 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1377 $dbh->rollback if $oldAutoCommit;
1378 return "Illegal svcdb value in part_svc!";
1381 require "FS/$svcdb.pm";
1383 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1385 $error = $svc->suspend;
1387 $dbh->rollback if $oldAutoCommit;
1390 my( $label, $value ) = $cust_svc->label;
1391 push @labels, "$label: $value";
1395 my $conf = new FS::Conf;
1396 if ( $conf->config('suspend_email_admin') ) {
1398 my $error = send_email(
1399 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1400 #invoice_from ??? well as good as any
1401 'to' => $conf->config('suspend_email_admin'),
1402 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1404 "This is an automatic message from your Freeside installation\n",
1405 "informing you that the following customer package has been suspended:\n",
1407 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1408 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1409 ( map { "Service : $_\n" } @labels ),
1411 'custnum' => $self->custnum,
1412 'msgtype' => 'admin'
1416 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1424 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1425 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1427 $dbh->rollback if $oldAutoCommit;
1428 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1432 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1437 =item credit_remaining MODE TIME
1439 Generate a credit for this package for the time remaining in the current
1440 billing period. MODE is either "suspend" or "cancel" (determines the
1441 credit type). TIME is the time of suspension/cancellation. Both arguments
1446 # Implementation note:
1448 # If you pkgpart-change a package that has been billed, and it's set to give
1449 # credit on package change, then this method gets called and then the new
1450 # package will have no last_bill date. Therefore the customer will be credited
1451 # only once (per billing period) even if there are multiple package changes.
1453 # If you location-change a package that has been billed, this method will NOT
1454 # be called and the new package WILL have the last bill date of the old
1457 # If the new package is then canceled within the same billing cycle,
1458 # credit_remaining needs to run calc_remain on the OLD package to determine
1459 # the amount of unused time to credit.
1461 sub credit_remaining {
1462 # Add a credit for remaining service
1463 my ($self, $mode, $time) = @_;
1464 die 'credit_remaining requires suspend or cancel'
1465 unless $mode eq 'suspend' or $mode eq 'cancel';
1466 die 'no suspend/cancel time' unless $time > 0;
1468 my $conf = FS::Conf->new;
1469 my $reason_type = $conf->config($mode.'_credit_type');
1471 my $last_bill = $self->getfield('last_bill') || 0;
1472 my $next_bill = $self->getfield('bill') || 0;
1473 if ( $last_bill > 0 # the package has been billed
1474 and $next_bill > 0 # the package has a next bill date
1475 and $next_bill >= $time # which is in the future
1477 my @cust_credit_source_bill_pkg = ();
1478 my $remaining_value = 0;
1480 my $remain_pkg = $self;
1481 $remaining_value = $remain_pkg->calc_remain(
1483 'cust_credit_source_bill_pkg' => \@cust_credit_source_bill_pkg,
1486 # we may have to walk back past some package changes to get to the
1487 # one that actually has unused time
1488 while ( $remaining_value == 0 ) {
1489 if ( $remain_pkg->change_pkgnum ) {
1490 $remain_pkg = FS::cust_pkg->by_key($remain_pkg->change_pkgnum);
1492 # the package has really never been billed
1495 $remaining_value = $remain_pkg->calc_remain(
1497 'cust_credit_source_bill_pkg' => \@cust_credit_source_bill_pkg,
1501 if ( $remaining_value > 0 ) {
1502 warn "Crediting for $remaining_value on package ".$self->pkgnum."\n"
1504 my $error = $self->cust_main->credit(
1506 'Credit for unused time on '. $self->part_pkg->pkg,
1507 'reason_type' => $reason_type,
1508 'cust_credit_source_bill_pkg' => \@cust_credit_source_bill_pkg,
1510 return "Error crediting customer \$$remaining_value for unused time".
1511 " on ". $self->part_pkg->pkg. ": $error"
1513 } #if $remaining_value
1514 } #if $last_bill, etc.
1518 =item unsuspend [ OPTION => VALUE ... ]
1520 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1521 package, then unsuspends the package itself (clears the susp field and the
1522 adjourn field if it is in the past). If the suspend reason includes an
1523 unsuspension package, that package will be ordered.
1525 Available options are:
1531 Can be set to a date to unsuspend the package in the future (the 'resume'
1534 =item adjust_next_bill
1536 Can be set true to adjust the next bill date forward by
1537 the amount of time the account was inactive. This was set true by default
1538 in the past (from 1.4.2 and 1.5.0pre6 through 1.7.0), but now needs to be
1539 explicitly requested with this option or in the price plan.
1543 If there is an error, returns the error, otherwise returns false.
1548 my( $self, %opt ) = @_;
1551 # pass all suspend/cancel actions to the main package
1552 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1553 return $self->main_pkg->unsuspend(%opt);
1556 my $oldAutoCommit = $FS::UID::AutoCommit;
1557 local $FS::UID::AutoCommit = 0;
1560 my $old = $self->select_for_update;
1562 my $pkgnum = $old->pkgnum;
1563 if ( $old->get('cancel') || $self->get('cancel') ) {
1564 $dbh->rollback if $oldAutoCommit;
1565 return "Can't unsuspend cancelled package $pkgnum";
1568 unless ( $old->get('susp') && $self->get('susp') ) {
1569 $dbh->rollback if $oldAutoCommit;
1570 return ""; # no error # complain instead?
1573 # handle the case of setting a future unsuspend (resume) date
1574 # and do not continue to actually unsuspend the package
1575 my $date = $opt{'date'};
1576 if ( $date and $date > time ) { # return an error if $date <= time?
1578 if ( $old->get('expire') && $old->get('expire') < $date ) {
1579 $dbh->rollback if $oldAutoCommit;
1580 return "Package $pkgnum expires before it would be unsuspended.";
1583 my $new = new FS::cust_pkg { $self->hash };
1584 $new->set('resume', $date);
1585 $error = $new->replace($self, options => $self->options);
1588 $dbh->rollback if $oldAutoCommit;
1592 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1598 if (!$self->setup) {
1599 # then this package is being released from on-hold status
1600 $self->set_initial_timers;
1605 foreach my $cust_svc (
1606 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1608 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1610 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1611 $dbh->rollback if $oldAutoCommit;
1612 return "Illegal svcdb value in part_svc!";
1615 require "FS/$svcdb.pm";
1617 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1619 $error = $svc->unsuspend;
1621 $dbh->rollback if $oldAutoCommit;
1624 my( $label, $value ) = $cust_svc->label;
1625 push @labels, "$label: $value";
1630 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1631 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
1633 my %hash = $self->hash;
1634 my $inactive = time - $hash{'susp'};
1636 my $conf = new FS::Conf;
1638 #adjust the next bill date forward
1639 # increment next bill date if certain conditions are met:
1640 # - it was due to be billed at some point
1641 # - either the global or local config says to do this
1642 my $adjust_bill = 0;
1645 && ( $hash{'bill'} || $hash{'setup'} )
1646 && ( $opt{'adjust_next_bill'}
1647 || $conf->exists('unsuspend-always_adjust_next_bill_date')
1648 || $self->part_pkg->option('unsuspend_adjust_bill', 1)
1655 # - the package billed during suspension
1656 # - or it was ordered on hold
1657 # - or the customer was credited for the unused time
1659 if ( $self->option('suspend_bill',1)
1660 or ( $self->part_pkg->option('suspend_bill',1)
1661 and ! $self->option('no_suspend_bill',1)
1663 or $hash{'order_date'} == $hash{'susp'}
1664 or $self->part_pkg->option('unused_credit_suspend')
1665 or ( ref($reason) and $reason->unused_credit )
1670 # then add the length of time suspended to the bill date
1671 if ( $adjust_bill ) {
1672 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive
1676 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
1677 $hash{'resume'} = '' if !$hash{'adjourn'};
1678 my $new = new FS::cust_pkg ( \%hash );
1679 $error = $new->replace( $self, options => { $self->options } );
1681 $dbh->rollback if $oldAutoCommit;
1687 if ( $reason && $reason->unsuspend_pkgpart ) {
1688 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
1689 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
1691 my $start_date = $self->cust_main->next_bill_date
1692 if $reason->unsuspend_hold;
1695 $unsusp_pkg = FS::cust_pkg->new({
1696 'custnum' => $self->custnum,
1697 'pkgpart' => $reason->unsuspend_pkgpart,
1698 'start_date' => $start_date,
1699 'locationnum' => $self->locationnum,
1700 # discount? probably not...
1703 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
1707 $dbh->rollback if $oldAutoCommit;
1712 if ( $conf->config('unsuspend_email_admin') ) {
1714 my $error = send_email(
1715 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1716 #invoice_from ??? well as good as any
1717 'to' => $conf->config('unsuspend_email_admin'),
1718 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
1719 "This is an automatic message from your Freeside installation\n",
1720 "informing you that the following customer package has been unsuspended:\n",
1722 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1723 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1724 ( map { "Service : $_\n" } @labels ),
1726 "An unsuspension fee was charged: ".
1727 $unsusp_pkg->part_pkg->pkg_comment."\n"
1731 'custnum' => $self->custnum,
1732 'msgtype' => 'admin',
1736 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
1742 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1743 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
1745 $dbh->rollback if $oldAutoCommit;
1746 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1750 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1757 Cancels any pending suspension (sets the adjourn field to null).
1759 If there is an error, returns the error, otherwise returns false.
1764 my( $self, %options ) = @_;
1767 my $oldAutoCommit = $FS::UID::AutoCommit;
1768 local $FS::UID::AutoCommit = 0;
1771 my $old = $self->select_for_update;
1773 my $pkgnum = $old->pkgnum;
1774 if ( $old->get('cancel') || $self->get('cancel') ) {
1775 dbh->rollback if $oldAutoCommit;
1776 return "Can't unadjourn cancelled package $pkgnum";
1777 # or at least it's pointless
1780 if ( $old->get('susp') || $self->get('susp') ) {
1781 dbh->rollback if $oldAutoCommit;
1782 return "Can't unadjourn suspended package $pkgnum";
1783 # perhaps this is arbitrary
1786 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1787 dbh->rollback if $oldAutoCommit;
1788 return ""; # no error
1791 my %hash = $self->hash;
1792 $hash{'adjourn'} = '';
1793 $hash{'resume'} = '';
1794 my $new = new FS::cust_pkg ( \%hash );
1795 $error = $new->replace( $self, options => { $self->options } );
1797 $dbh->rollback if $oldAutoCommit;
1801 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1808 =item change HASHREF | OPTION => VALUE ...
1810 Changes this package: cancels it and creates a new one, with a different
1811 pkgpart or locationnum or both. All services are transferred to the new
1812 package (no change will be made if this is not possible).
1814 Options may be passed as a list of key/value pairs or as a hash reference.
1821 New locationnum, to change the location for this package.
1825 New FS::cust_location object, to create a new location and assign it
1830 New FS::cust_main object, to create a new customer and assign the new package
1835 New pkgpart (see L<FS::part_pkg>).
1839 New refnum (see L<FS::part_referral>).
1843 New quantity; if unspecified, the new package will have the same quantity
1848 "New" (existing) FS::cust_pkg object. The package's services and other
1849 attributes will be transferred to this package.
1853 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1854 susp, adjourn, cancel, expire, and contract_end) to the new package.
1856 =item unprotect_svcs
1858 Normally, change() will rollback and return an error if some services
1859 can't be transferred (also see the I<cust_pkg-change_svcpart> config option).
1860 If unprotect_svcs is true, this method will transfer as many services as
1861 it can and then unconditionally cancel the old package.
1865 At least one of locationnum, cust_location, pkgpart, refnum, cust_main, or
1866 cust_pkg must be specified (otherwise, what's the point?)
1868 Returns either the new FS::cust_pkg object or a scalar error.
1872 my $err_or_new_cust_pkg = $old_cust_pkg->change
1876 #some false laziness w/order
1879 my $opt = ref($_[0]) ? shift : { @_ };
1881 my $conf = new FS::Conf;
1883 # Transactionize this whole mess
1884 my $oldAutoCommit = $FS::UID::AutoCommit;
1885 local $FS::UID::AutoCommit = 0;
1894 $hash{'setup'} = $time if $self->setup;
1896 $hash{'change_date'} = $time;
1897 $hash{"change_$_"} = $self->$_()
1898 foreach qw( pkgnum pkgpart locationnum );
1900 if ( $opt->{'cust_location'} ) {
1901 $error = $opt->{'cust_location'}->find_or_insert;
1903 $dbh->rollback if $oldAutoCommit;
1904 return "creating location record: $error";
1906 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1909 if ( $opt->{'cust_pkg'} ) {
1910 # treat changing to a package with a different pkgpart as a
1911 # pkgpart change (because it is)
1912 $opt->{'pkgpart'} = $opt->{'cust_pkg'}->pkgpart;
1915 # whether to override pkgpart checking on the new package
1916 my $same_pkgpart = 1;
1917 if ( $opt->{'pkgpart'} and ( $opt->{'pkgpart'} != $self->pkgpart ) ) {
1921 my $unused_credit = 0;
1922 my $keep_dates = $opt->{'keep_dates'};
1923 # Special case. If the pkgpart is changing, and the customer is
1924 # going to be credited for remaining time, don't keep setup, bill,
1925 # or last_bill dates, and DO pass the flag to cancel() to credit
1927 if ( $opt->{'pkgpart'}
1928 and $opt->{'pkgpart'} != $self->pkgpart
1929 and $self->part_pkg->option('unused_credit_change', 1) ) {
1932 $hash{$_} = '' foreach qw(setup bill last_bill);
1935 if ( $keep_dates ) {
1936 foreach my $date ( qw(setup bill last_bill susp adjourn cancel expire
1937 resume start_date contract_end ) ) {
1938 $hash{$date} = $self->getfield($date);
1941 # always keep this date, regardless of anything
1942 # (the date of the package change is in a different field)
1943 $hash{'order_date'} = $self->getfield('order_date');
1945 # allow $opt->{'locationnum'} = '' to specifically set it to null
1946 # (i.e. customer default location)
1947 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
1949 # usually this doesn't matter. the two cases where it does are:
1950 # 1. unused_credit_change + pkgpart change + setup fee on the new package
1952 # 2. (more importantly) changing a package before it's billed
1953 $hash{'waive_setup'} = $self->waive_setup;
1955 my $custnum = $self->custnum;
1956 if ( $opt->{cust_main} ) {
1957 my $cust_main = $opt->{cust_main};
1958 unless ( $cust_main->custnum ) {
1959 my $error = $cust_main->insert( @{ $opt->{cust_main_insert_args}||[] } );
1961 $dbh->rollback if $oldAutoCommit;
1962 return "inserting customer record: $error";
1965 $custnum = $cust_main->custnum;
1968 $hash{'contactnum'} = $opt->{'contactnum'} if $opt->{'contactnum'};
1971 if ( $opt->{'cust_pkg'} ) {
1972 # The target package already exists; update it to show that it was
1973 # changed from this package.
1974 $cust_pkg = $opt->{'cust_pkg'};
1976 foreach ( qw( pkgnum pkgpart locationnum ) ) {
1977 $cust_pkg->set("change_$_", $self->get($_));
1979 $cust_pkg->set('change_date', $time);
1980 $error = $cust_pkg->replace;
1983 # Create the new package.
1984 $cust_pkg = new FS::cust_pkg {
1985 custnum => $custnum,
1986 locationnum => $opt->{'locationnum'},
1987 ( map { $_ => ( $opt->{$_} || $self->$_() ) }
1988 qw( pkgpart quantity refnum salesnum )
1992 $error = $cust_pkg->insert( 'change' => 1,
1993 'allow_pkgpart' => $same_pkgpart );
1996 $dbh->rollback if $oldAutoCommit;
1997 return "inserting new package: $error";
2000 # Transfer services and cancel old package.
2002 $error = $self->transfer($cust_pkg);
2003 if ($error and $error == 0) {
2004 # $old_pkg->transfer failed.
2005 $dbh->rollback if $oldAutoCommit;
2006 return "transferring $error";
2009 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
2010 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
2011 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
2012 if ($error and $error == 0) {
2013 # $old_pkg->transfer failed.
2014 $dbh->rollback if $oldAutoCommit;
2015 return "converting $error";
2019 # We set unprotect_svcs when executing a "future package change". It's
2020 # not a user-interactive operation, so returning an error means the
2021 # package change will just fail. Rather than have that happen, we'll
2022 # let leftover services be deleted.
2023 if ($error > 0 and !$opt->{'unprotect_svcs'}) {
2024 # Transfers were successful, but we still had services left on the old
2025 # package. We can't change the package under this circumstances, so abort.
2026 $dbh->rollback if $oldAutoCommit;
2027 return "unable to transfer all services";
2030 #reset usage if changing pkgpart
2031 # AND usage rollover is off (otherwise adds twice, now and at package bill)
2032 if ($self->pkgpart != $cust_pkg->pkgpart) {
2033 my $part_pkg = $cust_pkg->part_pkg;
2034 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
2038 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
2041 $dbh->rollback if $oldAutoCommit;
2042 return "setting usage values: $error";
2045 # if NOT changing pkgpart, transfer any usage pools over
2046 foreach my $usage ($self->cust_pkg_usage) {
2047 $usage->set('pkgnum', $cust_pkg->pkgnum);
2048 $error = $usage->replace;
2050 $dbh->rollback if $oldAutoCommit;
2051 return "transferring usage pools: $error";
2056 # transfer usage pricing add-ons, if we're not changing pkgpart
2057 if ( $same_pkgpart ) {
2058 foreach my $old_cust_pkg_usageprice ($self->cust_pkg_usageprice) {
2059 my $new_cust_pkg_usageprice = new FS::cust_pkg_usageprice {
2060 'pkgnum' => $cust_pkg->pkgnum,
2061 'usagepricepart' => $old_cust_pkg_usageprice->usagepricepart,
2062 'quantity' => $old_cust_pkg_usageprice->quantity,
2064 $error = $new_cust_pkg_usageprice->insert;
2066 $dbh->rollback if $oldAutoCommit;
2067 return "Error transferring usage pricing add-on: $error";
2072 # transfer discounts, if we're not changing pkgpart
2073 if ( $same_pkgpart ) {
2074 foreach my $old_discount ($self->cust_pkg_discount_active) {
2075 # don't remove the old discount, we may still need to bill that package.
2076 my $new_discount = new FS::cust_pkg_discount {
2077 'pkgnum' => $cust_pkg->pkgnum,
2078 'discountnum' => $old_discount->discountnum,
2079 'months_used' => $old_discount->months_used,
2081 $error = $new_discount->insert;
2083 $dbh->rollback if $oldAutoCommit;
2084 return "transferring discounts: $error";
2089 # transfer (copy) invoice details
2090 foreach my $detail ($self->cust_pkg_detail) {
2091 my $new_detail = FS::cust_pkg_detail->new({ $detail->hash });
2092 $new_detail->set('pkgdetailnum', '');
2093 $new_detail->set('pkgnum', $cust_pkg->pkgnum);
2094 $error = $new_detail->insert;
2096 $dbh->rollback if $oldAutoCommit;
2097 return "transferring package notes: $error";
2103 if ( !$opt->{'cust_pkg'} ) {
2104 # Order any supplemental packages.
2105 my $part_pkg = $cust_pkg->part_pkg;
2106 my @old_supp_pkgs = $self->supplemental_pkgs;
2107 foreach my $link ($part_pkg->supp_part_pkg_link) {
2109 foreach (@old_supp_pkgs) {
2110 if ($_->pkgpart == $link->dst_pkgpart) {
2112 $_->pkgpart(0); # so that it can't match more than once
2116 # false laziness with FS::cust_main::Packages::order_pkg
2117 my $new = FS::cust_pkg->new({
2118 pkgpart => $link->dst_pkgpart,
2119 pkglinknum => $link->pkglinknum,
2120 custnum => $custnum,
2121 main_pkgnum => $cust_pkg->pkgnum,
2122 locationnum => $cust_pkg->locationnum,
2123 start_date => $cust_pkg->start_date,
2124 order_date => $cust_pkg->order_date,
2125 expire => $cust_pkg->expire,
2126 adjourn => $cust_pkg->adjourn,
2127 contract_end => $cust_pkg->contract_end,
2128 refnum => $cust_pkg->refnum,
2129 discountnum => $cust_pkg->discountnum,
2130 waive_setup => $cust_pkg->waive_setup,
2132 if ( $old and $opt->{'keep_dates'} ) {
2133 foreach (qw(setup bill last_bill)) {
2134 $new->set($_, $old->get($_));
2137 $error = $new->insert( allow_pkgpart => $same_pkgpart );
2140 $error ||= $old->transfer($new);
2142 if ( $error and $error > 0 ) {
2143 # no reason why this should ever fail, but still...
2144 $error = "Unable to transfer all services from supplemental package ".
2148 $dbh->rollback if $oldAutoCommit;
2151 push @new_supp_pkgs, $new;
2153 } # if !$opt->{'cust_pkg'}
2154 # because if there is one, then supplemental packages would already
2155 # have been created for it.
2157 #Good to go, cancel old package. Notify 'cancel' of whether to credit
2159 #Don't allow billing the package (preceding period packages and/or
2160 #outstanding usage) if we are keeping dates (i.e. location changing),
2161 #because the new package will be billed for the same date range.
2162 #Supplemental packages are also canceled here.
2164 # during scheduled changes, avoid canceling the package we just
2166 $self->set('change_to_pkgnum' => '');
2168 $error = $self->cancel(
2170 unused_credit => $unused_credit,
2171 nobill => $keep_dates,
2172 change_custnum => ( $self->custnum != $custnum ? $custnum : '' ),
2175 $dbh->rollback if $oldAutoCommit;
2176 return "canceling old package: $error";
2179 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
2181 my $error = $cust_pkg->cust_main->bill(
2182 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
2185 $dbh->rollback if $oldAutoCommit;
2186 return "billing new package: $error";
2190 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2196 =item change_later OPTION => VALUE...
2198 Schedule a package change for a later date. This actually orders the new
2199 package immediately, but sets its start date for a future date, and sets
2200 the current package to expire on the same date.
2202 If the package is already scheduled for a change, this can be called with
2203 'start_date' to change the scheduled date, or with pkgpart and/or
2204 locationnum to modify the package change. To cancel the scheduled change
2205 entirely, see C<abort_change>.
2213 The date for the package change. Required, and must be in the future.
2221 The pkgpart. locationnum, and quantity of the new package, with the same
2222 meaning as in C<change>.
2230 my $opt = ref($_[0]) ? shift : { @_ };
2232 my $oldAutoCommit = $FS::UID::AutoCommit;
2233 local $FS::UID::AutoCommit = 0;
2236 my $cust_main = $self->cust_main;
2238 my $date = delete $opt->{'start_date'} or return 'start_date required';
2240 if ( $date <= time ) {
2241 $dbh->rollback if $oldAutoCommit;
2242 return "start_date $date is in the past";
2247 if ( $self->change_to_pkgnum ) {
2248 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
2249 my $new_pkgpart = $opt->{'pkgpart'}
2250 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $change_to->pkgpart;
2251 my $new_locationnum = $opt->{'locationnum'}
2252 if $opt->{'locationnum'} and $opt->{'locationnum'} != $change_to->locationnum;
2253 my $new_quantity = $opt->{'quantity'}
2254 if $opt->{'quantity'} and $opt->{'quantity'} != $change_to->quantity;
2255 if ( $new_pkgpart or $new_locationnum or $new_quantity ) {
2256 # it hasn't been billed yet, so in principle we could just edit
2257 # it in place (w/o a package change), but that's bad form.
2258 # So change the package according to the new options...
2259 my $err_or_pkg = $change_to->change(%$opt);
2260 if ( ref $err_or_pkg ) {
2261 # Then set that package up for a future start.
2262 $self->set('change_to_pkgnum', $err_or_pkg->pkgnum);
2263 $self->set('expire', $date); # in case it's different
2264 $err_or_pkg->set('start_date', $date);
2265 $err_or_pkg->set('change_date', '');
2266 $err_or_pkg->set('change_pkgnum', '');
2268 $error = $self->replace ||
2269 $err_or_pkg->replace ||
2270 $change_to->cancel ||
2273 $error = $err_or_pkg;
2275 } else { # change the start date only.
2276 $self->set('expire', $date);
2277 $change_to->set('start_date', $date);
2278 $error = $self->replace || $change_to->replace;
2281 $dbh->rollback if $oldAutoCommit;
2284 $dbh->commit if $oldAutoCommit;
2287 } # if $self->change_to_pkgnum
2289 my $new_pkgpart = $opt->{'pkgpart'}
2290 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart;
2291 my $new_locationnum = $opt->{'locationnum'}
2292 if $opt->{'locationnum'} and $opt->{'locationnum'} != $self->locationnum;
2293 my $new_quantity = $opt->{'quantity'}
2294 if $opt->{'quantity'} and $opt->{'quantity'} != $self->quantity;
2296 return '' unless $new_pkgpart or $new_locationnum or $new_quantity; # wouldn't do anything
2298 # allow $opt->{'locationnum'} = '' to specifically set it to null
2299 # (i.e. customer default location)
2300 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2302 my $new = FS::cust_pkg->new( {
2303 custnum => $self->custnum,
2304 locationnum => $opt->{'locationnum'},
2305 start_date => $date,
2306 map { $_ => ( $opt->{$_} || $self->$_() ) }
2307 qw( pkgpart quantity refnum salesnum )
2309 $error = $new->insert('change' => 1,
2310 'allow_pkgpart' => ($new_pkgpart ? 0 : 1));
2312 $self->set('change_to_pkgnum', $new->pkgnum);
2313 $self->set('expire', $date);
2314 $error = $self->replace;
2317 $dbh->rollback if $oldAutoCommit;
2319 $dbh->commit if $oldAutoCommit;
2327 Cancels a future package change scheduled by C<change_later>.
2333 my $pkgnum = $self->change_to_pkgnum;
2334 my $change_to = FS::cust_pkg->by_key($pkgnum) if $pkgnum;
2337 $error = $change_to->cancel || $change_to->delete;
2338 return $error if $error;
2340 $self->set('change_to_pkgnum', '');
2341 $self->set('expire', '');
2345 =item set_quantity QUANTITY
2347 Change the package's quantity field. This is one of the few package properties
2348 that can safely be changed without canceling and reordering the package
2349 (because it doesn't affect tax eligibility). Returns an error or an
2356 $self = $self->replace_old; # just to make sure
2357 $self->quantity(shift);
2361 =item set_salesnum SALESNUM
2363 Change the package's salesnum (sales person) field. This is one of the few
2364 package properties that can safely be changed without canceling and reordering
2365 the package (because it doesn't affect tax eligibility). Returns an error or
2372 $self = $self->replace_old; # just to make sure
2373 $self->salesnum(shift);
2375 # XXX this should probably reassign any credit that's already been given
2378 =item modify_charge OPTIONS
2380 Change the properties of a one-time charge. The following properties can
2381 be changed this way:
2382 - pkg: the package description
2383 - classnum: the package class
2384 - additional: arrayref of additional invoice details to add to this package
2386 and, I<if the charge has not yet been billed>:
2387 - start_date: the date when it will be billed
2388 - amount: the setup fee to be charged
2389 - quantity: the multiplier for the setup fee
2391 If you pass 'adjust_commission' => 1, and the classnum changes, and there are
2392 commission credits linked to this charge, they will be recalculated.
2399 my $part_pkg = $self->part_pkg;
2400 my $pkgnum = $self->pkgnum;
2403 my $oldAutoCommit = $FS::UID::AutoCommit;
2404 local $FS::UID::AutoCommit = 0;
2406 return "Can't use modify_charge except on one-time charges"
2407 unless $part_pkg->freq eq '0';
2409 if ( length($opt{'pkg'}) and $part_pkg->pkg ne $opt{'pkg'} ) {
2410 $part_pkg->set('pkg', $opt{'pkg'});
2413 my %pkg_opt = $part_pkg->options;
2414 my $pkg_opt_modified = 0;
2416 $opt{'additional'} ||= [];
2419 foreach (grep /^additional/, keys %pkg_opt) {
2420 ($i) = ($_ =~ /^additional_info(\d+)$/);
2421 $old_additional[$i] = $pkg_opt{$_} if $i;
2422 delete $pkg_opt{$_};
2425 for ( $i = 0; exists($opt{'additional'}->[$i]); $i++ ) {
2426 $pkg_opt{ "additional_info$i" } = $opt{'additional'}->[$i];
2427 if (!exists($old_additional[$i])
2428 or $old_additional[$i] ne $opt{'additional'}->[$i])
2430 $pkg_opt_modified = 1;
2433 $pkg_opt_modified = 1 if (scalar(@old_additional) - 1) != $i;
2434 $pkg_opt{'additional_count'} = $i if $i > 0;
2437 if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} )
2440 $old_classnum = $part_pkg->classnum;
2441 $part_pkg->set('classnum', $opt{'classnum'});
2444 if ( !$self->get('setup') ) {
2445 # not yet billed, so allow amount, setup_cost, quantity and start_date
2447 if ( exists($opt{'amount'})
2448 and $part_pkg->option('setup_fee') != $opt{'amount'}
2449 and $opt{'amount'} > 0 ) {
2451 $pkg_opt{'setup_fee'} = $opt{'amount'};
2452 $pkg_opt_modified = 1;
2455 if ( exists($opt{'setup_cost'})
2456 and $part_pkg->setup_cost != $opt{'setup_cost'}
2457 and $opt{'setup_cost'} > 0 ) {
2459 $part_pkg->set('setup_cost', $opt{'setup_cost'});
2462 if ( exists($opt{'quantity'})
2463 and $opt{'quantity'} != $self->quantity
2464 and $opt{'quantity'} > 0 ) {
2466 $self->set('quantity', $opt{'quantity'});
2469 if ( exists($opt{'start_date'})
2470 and $opt{'start_date'} != $self->start_date ) {
2472 $self->set('start_date', $opt{'start_date'});
2476 } # else simply ignore them; the UI shouldn't allow editing the fields
2479 if ( exists($opt{'taxclass'})
2480 and $part_pkg->taxclass ne $opt{'taxclass'}) {
2482 $part_pkg->set('taxclass', $opt{'taxclass'});
2486 if ( $part_pkg->modified or $pkg_opt_modified ) {
2487 # can we safely modify the package def?
2488 # Yes, if it's not available for purchase, and this is the only instance
2490 if ( $part_pkg->disabled
2491 and FS::cust_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 1
2492 and FS::quotation_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 0
2494 $error = $part_pkg->replace( options => \%pkg_opt );
2497 $part_pkg = $part_pkg->clone;
2498 $part_pkg->set('disabled' => 'Y');
2499 $error = $part_pkg->insert( options => \%pkg_opt );
2500 # and associate this as yet-unbilled package to the new package def
2501 $self->set('pkgpart' => $part_pkg->pkgpart);
2504 $dbh->rollback if $oldAutoCommit;
2509 if ($self->modified) { # for quantity or start_date change, or if we had
2510 # to clone the existing package def
2511 my $error = $self->replace;
2512 return $error if $error;
2514 if (defined $old_classnum) {
2515 # fix invoice grouping records
2516 my $old_catname = $old_classnum
2517 ? FS::pkg_class->by_key($old_classnum)->categoryname
2519 my $new_catname = $opt{'classnum'}
2520 ? $part_pkg->pkg_class->categoryname
2522 if ( $old_catname ne $new_catname ) {
2523 foreach my $cust_bill_pkg ($self->cust_bill_pkg) {
2524 # (there should only be one...)
2525 my @display = qsearch( 'cust_bill_pkg_display', {
2526 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2527 'section' => $old_catname,
2529 foreach (@display) {
2530 $_->set('section', $new_catname);
2531 $error = $_->replace;
2533 $dbh->rollback if $oldAutoCommit;
2537 } # foreach $cust_bill_pkg
2540 if ( $opt{'adjust_commission'} ) {
2541 # fix commission credits...tricky.
2542 foreach my $cust_event ($self->cust_event) {
2543 my $part_event = $cust_event->part_event;
2544 foreach my $table (qw(sales agent)) {
2546 "FS::part_event::Action::Mixin::credit_${table}_pkg_class";
2547 my $credit = qsearchs('cust_credit', {
2548 'eventnum' => $cust_event->eventnum,
2550 if ( $part_event->isa($class) ) {
2551 # Yes, this results in current commission rates being applied
2552 # retroactively to a one-time charge. For accounting purposes
2553 # there ought to be some kind of time limit on doing this.
2554 my $amount = $part_event->_calc_credit($self);
2555 if ( $credit and $credit->amount ne $amount ) {
2556 # Void the old credit.
2557 $error = $credit->void('Package class changed');
2559 $dbh->rollback if $oldAutoCommit;
2560 return "$error (adjusting commission credit)";
2563 # redo the event action to recreate the credit.
2565 eval { $part_event->do_action( $self, $cust_event ) };
2567 $dbh->rollback if $oldAutoCommit;
2570 } # if $part_event->isa($class)
2572 } # foreach $cust_event
2573 } # if $opt{'adjust_commission'}
2574 } # if defined $old_classnum
2576 $dbh->commit if $oldAutoCommit;
2583 sub process_bulk_cust_pkg {
2586 warn Dumper($param) if $DEBUG;
2588 my $old_part_pkg = qsearchs('part_pkg',
2589 { pkgpart => $param->{'old_pkgpart'} });
2590 my $new_part_pkg = qsearchs('part_pkg',
2591 { pkgpart => $param->{'new_pkgpart'} });
2592 die "Must select a new package type\n" unless $new_part_pkg;
2593 #my $keep_dates = $param->{'keep_dates'} || 0;
2594 my $keep_dates = 1; # there is no good reason to turn this off
2596 my $oldAutoCommit = $FS::UID::AutoCommit;
2597 local $FS::UID::AutoCommit = 0;
2600 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
2603 foreach my $old_cust_pkg ( @cust_pkgs ) {
2605 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
2606 if ( $old_cust_pkg->getfield('cancel') ) {
2607 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
2608 $old_cust_pkg->pkgnum."\n"
2612 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
2614 my $error = $old_cust_pkg->change(
2615 'pkgpart' => $param->{'new_pkgpart'},
2616 'keep_dates' => $keep_dates
2618 if ( !ref($error) ) { # change returns the cust_pkg on success
2620 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
2623 $dbh->commit if $oldAutoCommit;
2629 Returns the last bill date, or if there is no last bill date, the setup date.
2630 Useful for billing metered services.
2636 return $self->setfield('last_bill', $_[0]) if @_;
2637 return $self->getfield('last_bill') if $self->getfield('last_bill');
2638 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
2639 'edate' => $self->bill, } );
2640 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
2643 =item last_cust_pkg_reason ACTION
2645 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
2646 Returns false if there is no reason or the package is not currenly ACTION'd
2647 ACTION is one of adjourn, susp, cancel, or expire.
2651 sub last_cust_pkg_reason {
2652 my ( $self, $action ) = ( shift, shift );
2653 my $date = $self->get($action);
2655 'table' => 'cust_pkg_reason',
2656 'hashref' => { 'pkgnum' => $self->pkgnum,
2657 'action' => substr(uc($action), 0, 1),
2660 'order_by' => 'ORDER BY num DESC LIMIT 1',
2664 =item last_reason ACTION
2666 Returns the most recent ACTION FS::reason associated with the package.
2667 Returns false if there is no reason or the package is not currenly ACTION'd
2668 ACTION is one of adjourn, susp, cancel, or expire.
2673 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
2674 $cust_pkg_reason->reason
2675 if $cust_pkg_reason;
2680 Returns the definition for this billing item, as an FS::part_pkg object (see
2687 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
2688 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
2689 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
2694 Returns the cancelled package this package was changed from, if any.
2700 return '' unless $self->change_pkgnum;
2701 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
2704 =item change_cust_main
2706 Returns the customter this package was detached to, if any.
2710 sub change_cust_main {
2712 return '' unless $self->change_custnum;
2713 qsearchs('cust_main', { 'custnum' => $self->change_custnum } );
2718 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
2725 $self->part_pkg->calc_setup($self, @_);
2730 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
2737 $self->part_pkg->calc_recur($self, @_);
2742 Calls the I<base_setup> of the FS::part_pkg object associated with this billing
2749 $self->part_pkg->base_setup($self, @_);
2754 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
2761 $self->part_pkg->base_recur($self, @_);
2766 Calls the I<calc_remain> of the FS::part_pkg object associated with this
2773 $self->part_pkg->calc_remain($self, @_);
2778 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
2785 $self->part_pkg->calc_cancel($self, @_);
2790 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
2796 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
2799 =item cust_pkg_detail [ DETAILTYPE ]
2801 Returns any customer package details for this package (see
2802 L<FS::cust_pkg_detail>).
2804 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2808 sub cust_pkg_detail {
2810 my %hash = ( 'pkgnum' => $self->pkgnum );
2811 $hash{detailtype} = shift if @_;
2813 'table' => 'cust_pkg_detail',
2814 'hashref' => \%hash,
2815 'order_by' => 'ORDER BY weight, pkgdetailnum',
2819 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
2821 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
2823 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2825 If there is an error, returns the error, otherwise returns false.
2829 sub set_cust_pkg_detail {
2830 my( $self, $detailtype, @details ) = @_;
2832 my $oldAutoCommit = $FS::UID::AutoCommit;
2833 local $FS::UID::AutoCommit = 0;
2836 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
2837 my $error = $current->delete;
2839 $dbh->rollback if $oldAutoCommit;
2840 return "error removing old detail: $error";
2844 foreach my $detail ( @details ) {
2845 my $cust_pkg_detail = new FS::cust_pkg_detail {
2846 'pkgnum' => $self->pkgnum,
2847 'detailtype' => $detailtype,
2848 'detail' => $detail,
2850 my $error = $cust_pkg_detail->insert;
2852 $dbh->rollback if $oldAutoCommit;
2853 return "error adding new detail: $error";
2858 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2865 Returns the customer billing events (see L<FS::cust_event>) for this invoice.
2869 #false laziness w/cust_bill.pm
2873 'table' => 'cust_event',
2874 'addl_from' => 'JOIN part_event USING ( eventpart )',
2875 'hashref' => { 'tablenum' => $self->pkgnum },
2876 'extra_sql' => " AND eventtable = 'cust_pkg' ",
2880 =item num_cust_event
2882 Returns the number of customer billing events (see L<FS::cust_event>) for this package.
2886 #false laziness w/cust_bill.pm
2887 sub num_cust_event {
2889 my $sql = "SELECT COUNT(*) ". $self->_from_cust_event_where;
2890 $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref->[0];
2893 =item exists_cust_event
2895 Returns true if there are customer billing events (see L<FS::cust_event>) for this package. More efficient than using num_cust_event.
2899 sub exists_cust_event {
2901 my $sql = "SELECT 1 ". $self->_from_cust_event_where. " LIMIT 1";
2902 my $row = $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref;
2903 $row ? $row->[0] : '';
2906 sub _from_cust_event_where {
2908 " FROM cust_event JOIN part_event USING ( eventpart ) ".
2909 " WHERE tablenum = ? AND eventtable = 'cust_pkg' ";
2913 my( $self, $sql, @args ) = @_;
2914 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
2915 $sth->execute(@args) or die $sth->errstr. " executing $sql";
2919 =item part_pkg_currency_option OPTIONNAME
2921 Returns a two item list consisting of the currency of this customer, if any,
2922 and a value for the provided option. If the customer has a currency, the value
2923 is the option value the given name and the currency (see
2924 L<FS::part_pkg_currency>). Otherwise, if the customer has no currency, is the
2925 regular option value for the given name (see L<FS::part_pkg_option>).
2929 sub part_pkg_currency_option {
2930 my( $self, $optionname ) = @_;
2931 my $part_pkg = $self->part_pkg;
2932 if ( my $currency = $self->cust_main->currency ) {
2933 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
2935 ('', $part_pkg->option($optionname) );
2939 =item cust_svc [ SVCPART ] (old, deprecated usage)
2941 =item cust_svc [ OPTION => VALUE ... ] (current usage)
2943 =item cust_svc_unsorted [ OPTION => VALUE ... ]
2945 Returns the services for this package, as FS::cust_svc objects (see
2946 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
2947 spcififed, returns only the matching services.
2949 As an optimization, use the cust_svc_unsorted version if you are not displaying
2956 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
2957 $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref(@_) );
2960 sub cust_svc_unsorted {
2962 @{ $self->cust_svc_unsorted_arrayref(@_) };
2965 sub cust_svc_unsorted_arrayref {
2968 return [] unless $self->num_cust_svc(@_);
2971 if ( @_ && $_[0] =~ /^\d+/ ) {
2972 $opt{svcpart} = shift;
2973 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2980 'table' => 'cust_svc',
2981 'hashref' => { 'pkgnum' => $self->pkgnum },
2983 if ( $opt{svcpart} ) {
2984 $search{hashref}->{svcpart} = $opt{'svcpart'};
2986 if ( $opt{'svcdb'} ) {
2987 $search{addl_from} = ' LEFT JOIN part_svc USING ( svcpart ) ';
2988 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{'svcdb'} );
2991 [ qsearch(\%search) ];
2995 =item overlimit [ SVCPART ]
2997 Returns the services for this package which have exceeded their
2998 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
2999 is specified, return only the matching services.
3005 return () unless $self->num_cust_svc(@_);
3006 grep { $_->overlimit } $self->cust_svc(@_);
3009 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3011 Returns historical services for this package created before END TIMESTAMP and
3012 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
3013 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
3014 I<pkg_svc.hidden> flag will be omitted.
3020 warn "$me _h_cust_svc called on $self\n"
3023 my ($end, $start, $mode) = @_;
3025 local($FS::Record::qsearch_qualify_columns) = 0;
3027 my @cust_svc = $self->_sort_cust_svc(
3028 [ qsearch( 'h_cust_svc',
3029 { 'pkgnum' => $self->pkgnum, },
3030 FS::h_cust_svc->sql_h_search(@_),
3034 if ( defined($mode) && $mode eq 'I' ) {
3035 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
3036 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
3042 sub _sort_cust_svc {
3043 my( $self, $arrayref ) = @_;
3046 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
3048 my %pkg_svc = map { $_->svcpart => $_ }
3049 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
3054 my $pkg_svc = $pkg_svc{ $_->svcpart } || '';
3056 $pkg_svc ? $pkg_svc->primary_svc : '',
3057 $pkg_svc ? $pkg_svc->quantity : 0,
3064 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
3066 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
3068 Returns the number of services for this package. Available options are svcpart
3069 and svcdb. If either is spcififed, returns only the matching services.
3076 return $self->{'_num_cust_svc'}
3078 && exists($self->{'_num_cust_svc'})
3079 && $self->{'_num_cust_svc'} =~ /\d/;
3081 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
3085 if ( @_ && $_[0] =~ /^\d+/ ) {
3086 $opt{svcpart} = shift;
3087 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
3093 my $select = 'SELECT COUNT(*) FROM cust_svc ';
3094 my $where = ' WHERE pkgnum = ? ';
3095 my @param = ($self->pkgnum);
3097 if ( $opt{'svcpart'} ) {
3098 $where .= ' AND svcpart = ? ';
3099 push @param, $opt{'svcpart'};
3101 if ( $opt{'svcdb'} ) {
3102 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
3103 $where .= ' AND svcdb = ? ';
3104 push @param, $opt{'svcdb'};
3107 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
3108 $sth->execute(@param) or die $sth->errstr;
3109 $sth->fetchrow_arrayref->[0];
3112 =item available_part_svc
3114 Returns a list of FS::part_svc objects representing services included in this
3115 package but not yet provisioned. Each FS::part_svc object also has an extra
3116 field, I<num_avail>, which specifies the number of available services.
3120 sub available_part_svc {
3123 my $pkg_quantity = $self->quantity || 1;
3125 grep { $_->num_avail > 0 }
3127 my $part_svc = $_->part_svc;
3128 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
3129 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
3131 # more evil encapsulation breakage
3132 if($part_svc->{'Hash'}{'num_avail'} > 0) {
3133 my @exports = $part_svc->part_export_did;
3134 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
3139 $self->part_pkg->pkg_svc;
3142 =item part_svc [ OPTION => VALUE ... ]
3144 Returns a list of FS::part_svc objects representing provisioned and available
3145 services included in this package. Each FS::part_svc object also has the
3146 following extra fields:
3160 (services) - array reference containing the provisioned services, as cust_svc objects
3164 Accepts two options:
3168 =item summarize_size
3170 If true, will omit the extra cust_pkg_svc option for objects where num_cust_svc
3171 is this size or greater.
3173 =item hide_discontinued
3175 If true, will omit looking for services that are no longer avaialble in the
3183 #label -> ($cust_svc->label)[1]
3189 my $pkg_quantity = $self->quantity || 1;
3191 #XXX some sort of sort order besides numeric by svcpart...
3192 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
3194 my $part_svc = $pkg_svc->part_svc;
3195 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3196 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
3197 $part_svc->{'Hash'}{'num_avail'} =
3198 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
3199 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3200 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
3201 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
3202 && $num_cust_svc >= $opt{summarize_size};
3203 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
3205 } $self->part_pkg->pkg_svc;
3207 unless ( $opt{hide_discontinued} ) {
3209 push @part_svc, map {
3211 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3212 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
3213 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
3214 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3215 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
3217 } $self->extra_part_svc;
3224 =item extra_part_svc
3226 Returns a list of FS::part_svc objects corresponding to services in this
3227 package which are still provisioned but not (any longer) available in the
3232 sub extra_part_svc {
3235 my $pkgnum = $self->pkgnum;
3236 #my $pkgpart = $self->pkgpart;
3239 # 'table' => 'part_svc',
3242 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
3243 # WHERE pkg_svc.svcpart = part_svc.svcpart
3244 # AND pkg_svc.pkgpart = ?
3247 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
3248 # LEFT JOIN cust_pkg USING ( pkgnum )
3249 # WHERE cust_svc.svcpart = part_svc.svcpart
3252 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
3255 #seems to benchmark slightly faster... (or did?)
3257 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
3258 my $pkgparts = join(',', @pkgparts);
3261 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
3262 #MySQL doesn't grok DISINCT ON
3263 'select' => 'DISTINCT part_svc.*',
3264 'table' => 'part_svc',
3266 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
3267 AND pkg_svc.pkgpart IN ($pkgparts)
3270 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
3271 LEFT JOIN cust_pkg USING ( pkgnum )
3274 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
3275 'extra_param' => [ [$self->pkgnum=>'int'] ],
3281 Returns a short status string for this package, currently:
3287 =item not yet billed
3289 =item one-time charge
3304 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
3306 return 'cancelled' if $self->get('cancel');
3307 return 'on hold' if $self->susp && ! $self->setup;
3308 return 'suspended' if $self->susp;
3309 return 'not yet billed' unless $self->setup;
3310 return 'one-time charge' if $freq =~ /^(0|$)/;
3314 =item ucfirst_status
3316 Returns the status with the first character capitalized.
3320 sub ucfirst_status {
3321 ucfirst(shift->status);
3326 Class method that returns the list of possible status strings for packages
3327 (see L<the status method|/status>). For example:
3329 @statuses = FS::cust_pkg->statuses();
3333 tie my %statuscolor, 'Tie::IxHash',
3334 'on hold' => '7E0079', #purple!
3335 'not yet billed' => '009999', #teal? cyan?
3336 'one-time charge' => '0000CC', #blue #'000000',
3337 'active' => '00CC00',
3338 'suspended' => 'FF9900',
3339 'cancelled' => 'FF0000',
3343 my $self = shift; #could be class...
3344 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
3345 # # mayble split btw one-time vs. recur
3356 Returns a hex triplet color string for this package's status.
3362 $statuscolor{$self->status};
3365 =item is_status_delay_cancel
3367 Returns true if part_pkg has option delay_cancel,
3368 cust_pkg status is 'suspended' and expire is set
3369 to cancel package within the next day (or however
3370 many days are set in global config part_pkg-delay_cancel-days.
3372 This is not a real status, this only meant for hacking display
3373 values, because otherwise treating the package as suspended is
3374 really the whole point of the delay_cancel option.
3378 sub is_status_delay_cancel {
3380 return 0 unless $self->part_pkg->option('delay_cancel',1);
3381 return 0 unless $self->status eq 'suspended';
3382 return 0 unless $self->expire;
3383 my $conf = new FS::Conf;
3384 my $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
3385 my $expsecs = 60*60*24*$expdays;
3386 return 0 unless $self->expire < time + $expsecs;
3392 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
3393 "pkg - comment" depending on user preference).
3399 my $label = $self->part_pkg->pkg_comment( cust_pkg=>$self, nopkgpart=>1 );
3400 $label = $self->pkgnum. ": $label"
3401 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
3405 =item pkg_label_long
3407 Returns a long label for this package, adding the primary service's label to
3412 sub pkg_label_long {
3414 my $label = $self->pkg_label;
3415 my $cust_svc = $self->primary_cust_svc;
3416 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
3422 Returns a customer-localized label for this package.
3428 $self->part_pkg->pkg_locale( $self->cust_main->locale );
3431 =item primary_cust_svc
3433 Returns a primary service (as FS::cust_svc object) if one can be identified.
3437 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
3439 sub primary_cust_svc {
3442 my @cust_svc = $self->cust_svc;
3444 return '' unless @cust_svc; #no serivces - irrelevant then
3446 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
3448 # primary service as specified in the package definition
3449 # or exactly one service definition with quantity one
3450 my $svcpart = $self->part_pkg->svcpart;
3451 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
3452 return $cust_svc[0] if scalar(@cust_svc) == 1;
3454 #couldn't identify one thing..
3460 Returns a list of lists, calling the label method for all services
3461 (see L<FS::cust_svc>) of this billing item.
3467 map { [ $_->label ] } $self->cust_svc;
3470 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3472 Like the labels method, but returns historical information on services that
3473 were active as of END_TIMESTAMP and (optionally) not cancelled before
3474 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
3475 I<pkg_svc.hidden> flag will be omitted.
3477 Returns a list of lists, calling the label method for all (historical) services
3478 (see L<FS::h_cust_svc>) of this billing item.
3484 warn "$me _h_labels called on $self\n"
3486 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
3491 Like labels, except returns a simple flat list, and shortens long
3492 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3493 identical services to one line that lists the service label and the number of
3494 individual services rather than individual items.
3499 shift->_labels_short( 'labels', @_ );
3502 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
3504 Like h_labels, except returns a simple flat list, and shortens long
3505 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3506 identical services to one line that lists the service label and the number of
3507 individual services rather than individual items.
3511 sub h_labels_short {
3512 shift->_labels_short( 'h_labels', @_ );
3516 my( $self, $method ) = ( shift, shift );
3518 warn "$me _labels_short called on $self with $method method\n"
3521 my $conf = new FS::Conf;
3522 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
3524 warn "$me _labels_short populating \%labels\n"
3528 #tie %labels, 'Tie::IxHash';
3529 push @{ $labels{$_->[0]} }, $_->[1]
3530 foreach $self->$method(@_);
3532 warn "$me _labels_short populating \@labels\n"
3536 foreach my $label ( keys %labels ) {
3538 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
3539 my $num = scalar(@values);
3540 warn "$me _labels_short $num items for $label\n"
3543 if ( $num > $max_same_services ) {
3544 warn "$me _labels_short more than $max_same_services, so summarizing\n"
3546 push @labels, "$label ($num)";
3548 if ( $conf->exists('cust_bill-consolidate_services') ) {
3549 warn "$me _labels_short consolidating services\n"
3551 # push @labels, "$label: ". join(', ', @values);
3553 my $detail = "$label: ";
3554 $detail .= shift(@values). ', '
3556 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
3558 push @labels, $detail;
3560 warn "$me _labels_short done consolidating services\n"
3563 warn "$me _labels_short adding service data\n"
3565 push @labels, map { "$label: $_" } @values;
3576 Returns the parent customer object (see L<FS::cust_main>).
3580 Returns the balance for this specific package, when using
3581 experimental package balance.
3587 $self->cust_main->balance_pkgnum( $self->pkgnum );
3590 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
3594 Returns the location object, if any (see L<FS::cust_location>).
3596 =item cust_location_or_main
3598 If this package is associated with a location, returns the locaiton (see
3599 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
3601 =item location_label [ OPTION => VALUE ... ]
3603 Returns the label of the location object (see L<FS::cust_location>).
3607 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
3609 =item tax_locationnum
3611 Returns the foreign key to a L<FS::cust_location> object for calculating
3612 tax on this package, as determined by the C<tax-pkg_address> and
3613 C<tax-ship_address> configuration flags.
3617 sub tax_locationnum {
3619 my $conf = FS::Conf->new;
3620 if ( $conf->exists('tax-pkg_address') ) {
3621 return $self->locationnum;
3623 elsif ( $conf->exists('tax-ship_address') ) {
3624 return $self->cust_main->ship_locationnum;
3627 return $self->cust_main->bill_locationnum;
3633 Returns the L<FS::cust_location> object for tax_locationnum.
3639 my $conf = FS::Conf->new;
3640 if ( $conf->exists('tax-pkg_address') and $self->locationnum ) {
3641 return FS::cust_location->by_key($self->locationnum);
3643 elsif ( $conf->exists('tax-ship_address') ) {
3644 return $self->cust_main->ship_location;
3647 return $self->cust_main->bill_location;
3651 =item seconds_since TIMESTAMP
3653 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
3654 package have been online since TIMESTAMP, according to the session monitor.
3656 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
3657 L<Time::Local> and L<Date::Parse> for conversion functions.
3662 my($self, $since) = @_;
3665 foreach my $cust_svc (
3666 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
3668 $seconds += $cust_svc->seconds_since($since);
3675 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
3677 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
3678 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
3681 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3682 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3688 sub seconds_since_sqlradacct {
3689 my($self, $start, $end) = @_;
3693 foreach my $cust_svc (
3695 my $part_svc = $_->part_svc;
3696 $part_svc->svcdb eq 'svc_acct'
3697 && scalar($part_svc->part_export_usage);
3700 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
3707 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
3709 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
3710 in this package for sessions ending between TIMESTAMP_START (inclusive) and
3714 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3715 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3720 sub attribute_since_sqlradacct {
3721 my($self, $start, $end, $attrib) = @_;
3725 foreach my $cust_svc (
3727 my $part_svc = $_->part_svc;
3728 scalar($part_svc->part_export_usage);
3731 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
3743 my( $self, $value ) = @_;
3744 if ( defined($value) ) {
3745 $self->setfield('quantity', $value);
3747 $self->getfield('quantity') || 1;
3750 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
3752 Transfers as many services as possible from this package to another package.
3754 The destination package can be specified by pkgnum by passing an FS::cust_pkg
3755 object. The destination package must already exist.
3757 Services are moved only if the destination allows services with the correct
3758 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
3759 this option with caution! No provision is made for export differences
3760 between the old and new service definitions. Probably only should be used
3761 when your exports for all service definitions of a given svcdb are identical.
3762 (attempt a transfer without it first, to move all possible svcpart-matching
3765 Any services that can't be moved remain in the original package.
3767 Returns an error, if there is one; otherwise, returns the number of services
3768 that couldn't be moved.
3773 my ($self, $dest_pkgnum, %opt) = @_;
3779 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
3780 $dest = $dest_pkgnum;
3781 $dest_pkgnum = $dest->pkgnum;
3783 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
3786 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
3788 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
3789 $target{$pkg_svc->svcpart} = $pkg_svc->quantity * ( $dest->quantity || 1 );
3792 foreach my $cust_svc ($dest->cust_svc) {
3793 $target{$cust_svc->svcpart}--;
3796 my %svcpart2svcparts = ();
3797 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3798 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
3799 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
3800 next if exists $svcpart2svcparts{$svcpart};
3801 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
3802 $svcpart2svcparts{$svcpart} = [
3804 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
3806 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
3807 'svcpart' => $_ } );
3809 $pkg_svc ? $pkg_svc->primary_svc : '',
3810 $pkg_svc ? $pkg_svc->quantity : 0,
3814 grep { $_ != $svcpart }
3816 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
3818 warn "alternates for svcpart $svcpart: ".
3819 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
3825 foreach my $cust_svc ($self->cust_svc) {
3826 my $svcnum = $cust_svc->svcnum;
3827 if($target{$cust_svc->svcpart} > 0
3828 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
3829 $target{$cust_svc->svcpart}--;
3830 my $new = new FS::cust_svc { $cust_svc->hash };
3831 $new->pkgnum($dest_pkgnum);
3832 $error = $new->replace($cust_svc);
3833 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3835 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
3836 warn "alternates to consider: ".
3837 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
3839 my @alternate = grep {
3840 warn "considering alternate svcpart $_: ".
3841 "$target{$_} available in new package\n"
3844 } @{$svcpart2svcparts{$cust_svc->svcpart}};
3846 warn "alternate(s) found\n" if $DEBUG;
3847 my $change_svcpart = $alternate[0];
3848 $target{$change_svcpart}--;
3849 my $new = new FS::cust_svc { $cust_svc->hash };
3850 $new->svcpart($change_svcpart);
3851 $new->pkgnum($dest_pkgnum);
3852 $error = $new->replace($cust_svc);
3860 my @label = $cust_svc->label;
3861 return "$label[0] $label[1]: $error";
3867 =item grab_svcnums SVCNUM, SVCNUM ...
3869 Change the pkgnum for the provided services to this packages. If there is an
3870 error, returns the error, otherwise returns false.
3878 my $oldAutoCommit = $FS::UID::AutoCommit;
3879 local $FS::UID::AutoCommit = 0;
3882 foreach my $svcnum (@svcnum) {
3883 my $cust_svc = qsearchs('cust_svc', { svcnum=>$svcnum } ) or do {
3884 $dbh->rollback if $oldAutoCommit;
3885 return "unknown svcnum $svcnum";
3887 $cust_svc->pkgnum( $self->pkgnum );
3888 my $error = $cust_svc->replace;
3890 $dbh->rollback if $oldAutoCommit;
3895 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3902 This method is deprecated. See the I<depend_jobnum> option to the insert and
3903 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
3907 #looks like this is still used by the order_pkg and change_pkg methods in
3908 # ClientAPI/MyAccount, need to look into those before removing
3912 my $oldAutoCommit = $FS::UID::AutoCommit;
3913 local $FS::UID::AutoCommit = 0;
3916 foreach my $cust_svc ( $self->cust_svc ) {
3917 #false laziness w/svc_Common::insert
3918 my $svc_x = $cust_svc->svc_x;
3919 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
3920 my $error = $part_export->export_insert($svc_x);
3922 $dbh->rollback if $oldAutoCommit;
3928 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3933 =item export_pkg_change OLD_CUST_PKG
3935 Calls the "pkg_change" export action for all services attached to this package.
3939 sub export_pkg_change {
3940 my( $self, $old ) = ( shift, shift );
3942 my $oldAutoCommit = $FS::UID::AutoCommit;
3943 local $FS::UID::AutoCommit = 0;
3946 foreach my $svc_x ( map $_->svc_x, $self->cust_svc ) {
3947 my $error = $svc_x->export('pkg_change', $self, $old);
3949 $dbh->rollback if $oldAutoCommit;
3954 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3961 Associates this package with a (suspension or cancellation) reason (see
3962 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
3965 Available options are:
3971 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.
3975 the access_user (see L<FS::access_user>) providing the reason
3983 the action (cancel, susp, adjourn, expire) associated with the reason
3987 If there is an error, returns the error, otherwise returns false.
3992 my ($self, %options) = @_;
3994 my $otaker = $options{reason_otaker} ||
3995 $FS::CurrentUser::CurrentUser->username;
3998 if ( $options{'reason'} =~ /^(\d+)$/ ) {
4002 } elsif ( ref($options{'reason'}) ) {
4004 return 'Enter a new reason (or select an existing one)'
4005 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
4007 my $reason = new FS::reason({
4008 'reason_type' => $options{'reason'}->{'typenum'},
4009 'reason' => $options{'reason'}->{'reason'},
4011 my $error = $reason->insert;
4012 return $error if $error;
4014 $reasonnum = $reason->reasonnum;
4017 return "Unparseable reason: ". $options{'reason'};
4020 my $cust_pkg_reason =
4021 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
4022 'reasonnum' => $reasonnum,
4023 'otaker' => $otaker,
4024 'action' => substr(uc($options{'action'}),0,1),
4025 'date' => $options{'date'}
4030 $cust_pkg_reason->insert;
4033 =item insert_discount
4035 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
4036 inserting a new discount on the fly (see L<FS::discount>).
4038 Available options are:
4046 If there is an error, returns the error, otherwise returns false.
4050 sub insert_discount {
4051 #my ($self, %options) = @_;
4054 my $cust_pkg_discount = new FS::cust_pkg_discount {
4055 'pkgnum' => $self->pkgnum,
4056 'discountnum' => $self->discountnum,
4058 'end_date' => '', #XXX
4059 #for the create a new discount case
4060 '_type' => $self->discountnum__type,
4061 'amount' => $self->discountnum_amount,
4062 'percent' => $self->discountnum_percent,
4063 'months' => $self->discountnum_months,
4064 'setup' => $self->discountnum_setup,
4065 #'disabled' => $self->discountnum_disabled,
4068 $cust_pkg_discount->insert;
4071 =item set_usage USAGE_VALUE_HASHREF
4073 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4074 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4075 upbytes, downbytes, and totalbytes are appropriate keys.
4077 All svc_accts which are part of this package have their values reset.
4082 my ($self, $valueref, %opt) = @_;
4084 #only svc_acct can set_usage for now
4085 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4086 my $svc_x = $cust_svc->svc_x;
4087 $svc_x->set_usage($valueref, %opt)
4088 if $svc_x->can("set_usage");
4092 =item recharge USAGE_VALUE_HASHREF
4094 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4095 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4096 upbytes, downbytes, and totalbytes are appropriate keys.
4098 All svc_accts which are part of this package have their values incremented.
4103 my ($self, $valueref) = @_;
4105 #only svc_acct can set_usage for now
4106 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4107 my $svc_x = $cust_svc->svc_x;
4108 $svc_x->recharge($valueref)
4109 if $svc_x->can("recharge");
4113 =item apply_usageprice
4117 sub apply_usageprice {
4120 my $oldAutoCommit = $FS::UID::AutoCommit;
4121 local $FS::UID::AutoCommit = 0;
4126 foreach my $cust_pkg_usageprice ( $self->cust_pkg_usageprice ) {
4127 $error ||= $cust_pkg_usageprice->apply;
4131 $dbh->rollback if $oldAutoCommit;
4132 die "error applying part_pkg_usageprice add-ons, pkgnum ". $self->pkgnum.
4135 $dbh->commit if $oldAutoCommit;
4141 =item cust_pkg_discount
4143 =item cust_pkg_discount_active
4147 sub cust_pkg_discount_active {
4149 grep { $_->status eq 'active' } $self->cust_pkg_discount;
4152 =item cust_pkg_usage
4154 Returns a list of all voice usage counters attached to this package.
4156 =item apply_usage OPTIONS
4158 Takes the following options:
4159 - cdr: a call detail record (L<FS::cdr>)
4160 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
4161 - minutes: the maximum number of minutes to be charged
4163 Finds available usage minutes for a call of this class, and subtracts
4164 up to that many minutes from the usage pool. If the usage pool is empty,
4165 and the C<cdr-minutes_priority> global config option is set, minutes may
4166 be taken from other calls as well. Either way, an allocation record will
4167 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
4168 number of minutes of usage applied to the call.
4173 my ($self, %opt) = @_;
4174 my $cdr = $opt{cdr};
4175 my $rate_detail = $opt{rate_detail};
4176 my $minutes = $opt{minutes};
4177 my $classnum = $rate_detail->classnum;
4178 my $pkgnum = $self->pkgnum;
4179 my $custnum = $self->custnum;
4181 my $oldAutoCommit = $FS::UID::AutoCommit;
4182 local $FS::UID::AutoCommit = 0;
4185 my $order = FS::Conf->new->config('cdr-minutes_priority');
4189 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
4191 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
4193 my @usage_recs = qsearch({
4194 'table' => 'cust_pkg_usage',
4195 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
4196 ' JOIN cust_pkg USING (pkgnum)'.
4197 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
4198 'select' => 'cust_pkg_usage.*',
4199 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
4200 " ( cust_pkg.custnum = $custnum AND ".
4201 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4202 $is_classnum . ' AND '.
4203 " cust_pkg_usage.minutes > 0",
4204 'order_by' => " ORDER BY priority ASC",
4207 my $orig_minutes = $minutes;
4209 while (!$error and $minutes > 0 and @usage_recs) {
4210 my $cust_pkg_usage = shift @usage_recs;
4211 $cust_pkg_usage->select_for_update;
4212 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
4213 pkgusagenum => $cust_pkg_usage->pkgusagenum,
4214 acctid => $cdr->acctid,
4215 minutes => min($cust_pkg_usage->minutes, $minutes),
4217 $cust_pkg_usage->set('minutes',
4218 $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes
4220 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
4221 $minutes -= $cdr_cust_pkg_usage->minutes;
4223 if ( $order and $minutes > 0 and !$error ) {
4224 # then try to steal minutes from another call
4226 'table' => 'cdr_cust_pkg_usage',
4227 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
4228 ' JOIN part_pkg_usage USING (pkgusagepart)'.
4229 ' JOIN cust_pkg USING (pkgnum)'.
4230 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
4231 ' JOIN cdr USING (acctid)',
4232 'select' => 'cdr_cust_pkg_usage.*',
4233 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
4234 " ( cust_pkg.pkgnum = $pkgnum OR ".
4235 " ( cust_pkg.custnum = $custnum AND ".
4236 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4237 " part_pkg_usage_class.classnum = $classnum",
4238 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
4240 if ( $order eq 'time' ) {
4241 # find CDRs that are using minutes, but have a later startdate
4243 my $startdate = $cdr->startdate;
4244 if ($startdate !~ /^\d+$/) {
4245 die "bad cdr startdate '$startdate'";
4247 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
4248 # minimize needless reshuffling
4249 $search{'order_by'} .= ', cdr.startdate DESC';
4251 # XXX may not work correctly with rate_time schedules. Could
4252 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
4254 $search{'addl_from'} .=
4255 ' JOIN rate_detail'.
4256 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
4257 if ( $order eq 'rate_high' ) {
4258 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
4259 $rate_detail->min_charge;
4260 $search{'order_by'} .= ', rate_detail.min_charge ASC';
4261 } elsif ( $order eq 'rate_low' ) {
4262 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
4263 $rate_detail->min_charge;
4264 $search{'order_by'} .= ', rate_detail.min_charge DESC';
4266 # this should really never happen
4267 die "invalid cdr-minutes_priority value '$order'\n";
4270 my @cdr_usage_recs = qsearch(\%search);
4272 while (!$error and @cdr_usage_recs and $minutes > 0) {
4273 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
4274 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
4275 my $old_cdr = $cdr_cust_pkg_usage->cdr;
4276 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
4277 $cdr_cust_pkg_usage->select_for_update;
4278 $old_cdr->select_for_update;
4279 $cust_pkg_usage->select_for_update;
4280 # in case someone else stole the usage from this CDR
4281 # while waiting for the lock...
4282 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
4283 # steal the usage allocation and flag the old CDR for reprocessing
4284 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
4285 # if the allocation is more minutes than we need, adjust it...
4286 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
4288 $cdr_cust_pkg_usage->set('minutes', $minutes);
4289 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
4290 $error = $cust_pkg_usage->replace;
4292 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
4293 $error ||= $cdr_cust_pkg_usage->replace;
4294 # deduct the stolen minutes
4295 $minutes -= $cdr_cust_pkg_usage->minutes;
4297 # after all minute-stealing is done, reset the affected CDRs
4298 foreach (values %reproc_cdrs) {
4299 $error ||= $_->set_status('');
4300 # XXX or should we just call $cdr->rate right here?
4301 # it's not like we can create a loop this way, since the min_charge
4302 # or call time has to go monotonically in one direction.
4303 # we COULD get some very deep recursions going, though...
4305 } # if $order and $minutes
4308 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
4310 $dbh->commit if $oldAutoCommit;
4311 return $orig_minutes - $minutes;
4315 =item supplemental_pkgs
4317 Returns a list of all packages supplemental to this one.
4321 sub supplemental_pkgs {
4323 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
4328 Returns the package that this one is supplemental to, if any.
4334 if ( $self->main_pkgnum ) {
4335 return FS::cust_pkg->by_key($self->main_pkgnum);
4342 =head1 CLASS METHODS
4348 Returns an SQL expression identifying recurring packages.
4352 sub recurring_sql { "
4353 '0' != ( select freq from part_pkg
4354 where cust_pkg.pkgpart = part_pkg.pkgpart )
4359 Returns an SQL expression identifying one-time packages.
4364 '0' = ( select freq from part_pkg
4365 where cust_pkg.pkgpart = part_pkg.pkgpart )
4370 Returns an SQL expression identifying ordered packages (recurring packages not
4376 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
4381 Returns an SQL expression identifying active packages.
4386 $_[0]->recurring_sql. "
4387 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4388 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4389 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4392 =item not_yet_billed_sql
4394 Returns an SQL expression identifying packages which have not yet been billed.
4398 sub not_yet_billed_sql { "
4399 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4400 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4401 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4406 Returns an SQL expression identifying inactive packages (one-time packages
4407 that are otherwise unsuspended/uncancelled).
4411 sub inactive_sql { "
4412 ". $_[0]->onetime_sql(). "
4413 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4414 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4415 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4420 Returns an SQL expression identifying on-hold packages.
4425 #$_[0]->recurring_sql(). ' AND '.
4427 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4428 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4429 AND ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4436 Returns an SQL expression identifying suspended packages.
4440 sub suspended_sql { susp_sql(@_); }
4442 #$_[0]->recurring_sql(). ' AND '.
4444 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4445 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4446 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4453 Returns an SQL exprression identifying cancelled packages.
4457 sub cancelled_sql { cancel_sql(@_); }
4459 #$_[0]->recurring_sql(). ' AND '.
4460 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
4465 Returns an SQL expression to give the package status as a string.
4471 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
4472 WHEN ( cust_pkg.susp IS NOT NULL AND cust_pkg.setup IS NULL ) THEN 'on hold'
4473 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
4474 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
4475 WHEN ".onetime_sql()." THEN 'one-time charge'
4482 Returns a list of two package counts. The first is a count of packages
4483 based on the supplied criteria and the second is the count of residential
4484 packages with those same criteria. Criteria are specified as in the search
4490 my ($class, $params) = @_;
4492 my $sql_query = $class->search( $params );
4494 my $count_sql = delete($sql_query->{'count_query'});
4495 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
4496 or die "couldn't parse count_sql";
4498 my $count_sth = dbh->prepare($count_sql)
4499 or die "Error preparing $count_sql: ". dbh->errstr;
4501 or die "Error executing $count_sql: ". $count_sth->errstr;
4502 my $count_arrayref = $count_sth->fetchrow_arrayref;
4504 return ( @$count_arrayref );
4508 =item tax_locationnum_sql
4510 Returns an SQL expression for the tax location for a package, based
4511 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
4515 sub tax_locationnum_sql {
4516 my $conf = FS::Conf->new;
4517 if ( $conf->exists('tax-pkg_address') ) {
4518 'cust_pkg.locationnum';
4520 elsif ( $conf->exists('tax-ship_address') ) {
4521 'cust_main.ship_locationnum';
4524 'cust_main.bill_locationnum';
4530 Returns a list: the first item is an SQL fragment identifying matching
4531 packages/customers via location (taking into account shipping and package
4532 address taxation, if enabled), and subsequent items are the parameters to
4533 substitute for the placeholders in that fragment.
4538 my($class, %opt) = @_;
4539 my $ornull = $opt{'ornull'};
4541 my $conf = new FS::Conf;
4543 # '?' placeholders in _location_sql_where
4544 my $x = $ornull ? 3 : 2;
4555 if ( $conf->exists('tax-ship_address') ) {
4558 ( ( ship_last IS NULL OR ship_last = '' )
4559 AND ". _location_sql_where('cust_main', '', $ornull ). "
4561 OR ( ship_last IS NOT NULL AND ship_last != ''
4562 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
4565 # AND payby != 'COMP'
4567 @main_param = ( @bill_param, @bill_param );
4571 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
4572 @main_param = @bill_param;
4578 if ( $conf->exists('tax-pkg_address') ) {
4580 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
4583 ( cust_pkg.locationnum IS NULL AND $main_where )
4584 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
4587 @param = ( @main_param, @bill_param );
4591 $where = $main_where;
4592 @param = @main_param;
4600 #subroutine, helper for location_sql
4601 sub _location_sql_where {
4603 my $prefix = @_ ? shift : '';
4604 my $ornull = @_ ? shift : '';
4606 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
4608 $ornull = $ornull ? ' OR ? IS NULL ' : '';
4610 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
4611 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
4612 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
4614 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
4616 # ( $table.${prefix}city = ? $or_empty_city $ornull )
4618 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4619 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4620 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
4621 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
4622 AND $table.${prefix}country = ?
4627 my( $self, $what ) = @_;
4629 my $what_show_zero = $what. '_show_zero';
4630 length($self->$what_show_zero())
4631 ? ($self->$what_show_zero() eq 'Y')
4632 : $self->part_pkg->$what_show_zero();
4639 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
4641 CUSTNUM is a customer (see L<FS::cust_main>)
4643 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4644 L<FS::part_pkg>) to order for this customer. Duplicates are of course
4647 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
4648 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
4649 new billing items. An error is returned if this is not possible (see
4650 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
4653 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4654 newly-created cust_pkg objects.
4656 REFNUM, if specified, will specify the FS::pkg_referral record to be created
4657 and inserted. Multiple FS::pkg_referral records can be created by
4658 setting I<refnum> to an array reference of refnums or a hash reference with
4659 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
4660 record will be created corresponding to cust_main.refnum.
4665 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
4667 my $conf = new FS::Conf;
4669 # Transactionize this whole mess
4670 my $oldAutoCommit = $FS::UID::AutoCommit;
4671 local $FS::UID::AutoCommit = 0;
4675 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
4676 # return "Customer not found: $custnum" unless $cust_main;
4678 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
4681 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4684 my $change = scalar(@old_cust_pkg) != 0;
4687 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
4689 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
4690 " to pkgpart ". $pkgparts->[0]. "\n"
4693 my $err_or_cust_pkg =
4694 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
4695 'refnum' => $refnum,
4698 unless (ref($err_or_cust_pkg)) {
4699 $dbh->rollback if $oldAutoCommit;
4700 return $err_or_cust_pkg;
4703 push @$return_cust_pkg, $err_or_cust_pkg;
4704 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4709 # Create the new packages.
4710 foreach my $pkgpart (@$pkgparts) {
4712 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
4714 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
4715 pkgpart => $pkgpart,
4719 $error = $cust_pkg->insert( 'change' => $change );
4720 push @$return_cust_pkg, $cust_pkg;
4722 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
4723 my $supp_pkg = FS::cust_pkg->new({
4724 custnum => $custnum,
4725 pkgpart => $link->dst_pkgpart,
4727 main_pkgnum => $cust_pkg->pkgnum,
4730 $error ||= $supp_pkg->insert( 'change' => $change );
4731 push @$return_cust_pkg, $supp_pkg;
4735 $dbh->rollback if $oldAutoCommit;
4740 # $return_cust_pkg now contains refs to all of the newly
4743 # Transfer services and cancel old packages.
4744 foreach my $old_pkg (@old_cust_pkg) {
4746 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
4749 foreach my $new_pkg (@$return_cust_pkg) {
4750 $error = $old_pkg->transfer($new_pkg);
4751 if ($error and $error == 0) {
4752 # $old_pkg->transfer failed.
4753 $dbh->rollback if $oldAutoCommit;
4758 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
4759 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
4760 foreach my $new_pkg (@$return_cust_pkg) {
4761 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
4762 if ($error and $error == 0) {
4763 # $old_pkg->transfer failed.
4764 $dbh->rollback if $oldAutoCommit;
4771 # Transfers were successful, but we went through all of the
4772 # new packages and still had services left on the old package.
4773 # We can't cancel the package under the circumstances, so abort.
4774 $dbh->rollback if $oldAutoCommit;
4775 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
4777 $error = $old_pkg->cancel( quiet=>1 );
4783 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4787 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
4789 A bulk change method to change packages for multiple customers.
4791 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4792 L<FS::part_pkg>) to order for each customer. Duplicates are of course
4795 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
4796 replace. The services (see L<FS::cust_svc>) are moved to the
4797 new billing items. An error is returned if this is not possible (see
4800 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4801 newly-created cust_pkg objects.
4806 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
4808 # Transactionize this whole mess
4809 my $oldAutoCommit = $FS::UID::AutoCommit;
4810 local $FS::UID::AutoCommit = 0;
4814 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4817 while(scalar(@old_cust_pkg)) {
4819 my $custnum = $old_cust_pkg[0]->custnum;
4820 my (@remove) = map { $_->pkgnum }
4821 grep { $_->custnum == $custnum } @old_cust_pkg;
4822 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
4824 my $error = order $custnum, $pkgparts, \@remove, \@return;
4826 push @errors, $error
4828 push @$return_cust_pkg, @return;
4831 if (scalar(@errors)) {
4832 $dbh->rollback if $oldAutoCommit;
4833 return join(' / ', @errors);
4836 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4840 # Used by FS::Upgrade to migrate to a new database.
4841 sub _upgrade_data { # class method
4842 my ($class, %opts) = @_;
4843 $class->_upgrade_otaker(%opts);
4845 # RT#10139, bug resulting in contract_end being set when it shouldn't
4846 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
4847 # RT#10830, bad calculation of prorate date near end of year
4848 # the date range for bill is December 2009, and we move it forward
4849 # one year if it's before the previous bill date (which it should
4851 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
4852 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
4853 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
4854 # RT6628, add order_date to cust_pkg
4855 'update cust_pkg set order_date = (select history_date from h_cust_pkg
4856 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
4857 history_action = \'insert\') where order_date is null',
4859 foreach my $sql (@statements) {
4860 my $sth = dbh->prepare($sql);
4861 $sth->execute or die $sth->errstr;
4864 # RT31194: supplemental package links that are deleted don't clean up
4866 my @pkglinknums = qsearch({
4867 'select' => 'DISTINCT cust_pkg.pkglinknum',
4868 'table' => 'cust_pkg',
4869 'addl_from' => ' LEFT JOIN part_pkg_link USING (pkglinknum) ',
4870 'extra_sql' => ' WHERE cust_pkg.pkglinknum IS NOT NULL
4871 AND part_pkg_link.pkglinknum IS NULL',
4873 foreach (@pkglinknums) {
4874 my $pkglinknum = $_->pkglinknum;
4875 warn "cleaning part_pkg_link #$pkglinknum\n";
4876 my $part_pkg_link = FS::part_pkg_link->new({pkglinknum => $pkglinknum});
4877 my $error = $part_pkg_link->remove_linked;
4878 die $error if $error;
4886 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
4888 In sub order, the @pkgparts array (passed by reference) is clobbered.
4890 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
4891 method to pass dates to the recur_prog expression, it should do so.
4893 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
4894 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
4895 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
4896 configuration values. Probably need a subroutine which decides what to do
4897 based on whether or not we've fetched the user yet, rather than a hash. See
4898 FS::UID and the TODO.
4900 Now that things are transactional should the check in the insert method be
4905 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
4906 L<FS::pkg_svc>, schema.html from the base documentation