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 sum);
13 use Time::Local qw( timelocal timelocal_nocheck );
15 use FS::UID qw( dbh driver_name );
16 use FS::Record qw( qsearch qsearchs fields );
22 use FS::cust_location;
24 use FS::cust_bill_pkg;
25 use FS::cust_pkg_detail;
26 use FS::cust_pkg_usage;
27 use FS::cdr_cust_pkg_usage;
32 use FS::cust_pkg_reason;
34 use FS::cust_pkg_usageprice;
35 use FS::cust_pkg_discount;
43 # temporary fix; remove this once (un)suspend admin notices are cleaned up
44 use FS::Misc qw(send_email);
46 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
48 # because they load configuration by setting FS::UID::callback (see TODO)
54 # for sending cancel emails in sub cancel
57 our ($disable_agentcheck, $DEBUG, $me, $import) = (0, 0, '[FS::cust_pkg]', 0);
59 our $upgrade = 0; #go away after setup+start dates cleaned up for old customers
61 our $cache_enabled = 0;
64 my( $self, $hashref ) = @_;
65 if ( $cache_enabled && $hashref->{'pkg'} && $hashref->{'plan'} ) {
66 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
72 my ( $hashref, $cache ) = @_;
73 # #if ( $hashref->{'pkgpart'} ) {
74 # if ( $hashref->{'pkg'} ) {
75 # # #@{ $self->{'_pkgnum'} } = ();
76 # # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
77 # # $self->{'_pkgpart'} = $subcache;
78 # # #push @{ $self->{'_pkgnum'} },
79 # # FS::part_pkg->new_or_cached($hashref, $subcache);
80 # $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
82 if ( exists $hashref->{'svcnum'} ) {
83 #@{ $self->{'_pkgnum'} } = ();
84 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
85 $self->{'_svcnum'} = $subcache;
86 #push @{ $self->{'_pkgnum'} },
87 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
93 FS::cust_pkg - Object methods for cust_pkg objects
99 $record = new FS::cust_pkg \%hash;
100 $record = new FS::cust_pkg { 'column' => 'value' };
102 $error = $record->insert;
104 $error = $new_record->replace($old_record);
106 $error = $record->delete;
108 $error = $record->check;
110 $error = $record->cancel;
112 $error = $record->suspend;
114 $error = $record->unsuspend;
116 $part_pkg = $record->part_pkg;
118 @labels = $record->labels;
120 $seconds = $record->seconds_since($timestamp);
122 #bulk cancel+order... perhaps slightly deprecated, only used by the bulk
123 # cancel+order in the web UI and nowhere else (edit/process/cust_pkg.cgi)
124 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
125 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
129 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
130 inherits from FS::Record. The following fields are currently supported:
136 Primary key (assigned automatically for new billing items)
140 Customer (see L<FS::cust_main>)
144 Billing item definition (see L<FS::part_pkg>)
148 Optional link to package location (see L<FS::location>)
152 date package was ordered (also remains same on changes)
164 date (next bill date)
192 order taker (see L<FS::access_user>)
196 If not set, defaults to 1
200 Date of change from previous package
210 =item change_locationnum
218 The pkgnum of the package that this package is supplemental to, if any.
222 The package link (L<FS::part_pkg_link>) that defines this supplemental
223 package, if it is one.
225 =item change_to_pkgnum
227 The pkgnum of the package this one will be "changed to" in the future
228 (on its expiration date).
232 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
233 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
234 L<Time::Local> and L<Date::Parse> for conversion functions.
242 Create a new billing item. To add the item to the database, see L<"insert">.
246 sub table { 'cust_pkg'; }
247 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
248 sub cust_unlinked_msg {
250 "WARNING: can't find cust_main.custnum ". $self->custnum.
251 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
254 =item set_initial_timers
256 If required by the package definition, sets any automatic expire, adjourn,
257 or contract_end timers to some number of months after the start date
258 (or setup date, if the package has already been setup). If the package has
259 a delayed setup fee after a period of "free days", will also set the
260 start date to the end of that period.
262 If the package has an automatic transfer rule (C<change_to_pkgnum>), then
263 this will also order the package and set its start date.
267 sub set_initial_timers {
269 my $part_pkg = $self->part_pkg;
270 my $start = $self->start_date || $self->setup || time;
272 foreach my $action ( qw(expire adjourn contract_end) ) {
273 my $months = $part_pkg->get("${action}_months");
274 if($months and !$self->get($action)) {
275 $self->set($action, $part_pkg->add_freq($start, $months) );
279 # if this package has an expire date and a change_to_pkgpart, set automatic
281 # (but don't call change_later, as that would call $self->replace, and we're
282 # probably in the middle of $self->insert right now)
283 if ( $part_pkg->expire_months and $part_pkg->change_to_pkgpart ) {
284 if ( $self->change_to_pkgnum ) {
285 # this can happen if a package is ordered on hold, scheduled for a
286 # future change _while on hold_, and then released from hold, causing
287 # the automatic transfer to schedule.
289 # what's correct behavior in that case? I think it's to disallow
290 # future-changing an on-hold package that has an automatic transfer.
291 # but if we DO get into this situation, let the manual package change
293 warn "pkgnum ".$self->pkgnum.": manual future package change blocks ".
294 "automatic transfer.\n";
296 my $change_to = FS::cust_pkg->new( {
297 start_date => $self->get('expire'),
298 pkgpart => $part_pkg->change_to_pkgpart,
299 map { $_ => $self->get($_) }
300 qw( custnum locationnum quantity refnum salesnum contract_end )
302 my $error = $change_to->insert;
304 return $error if $error;
305 $self->set('change_to_pkgnum', $change_to->pkgnum);
309 # if this package has "free days" and delayed setup fee, then
310 # set start date that many days in the future.
311 # (this should have been set in the UI, but enforce it here)
312 if ( $part_pkg->option('free_days',1)
313 && $part_pkg->option('delay_setup',1)
316 $self->start_date( $part_pkg->default_start_date );
322 =item insert [ OPTION => VALUE ... ]
324 Adds this billing item to the database ("Orders" the item). If there is an
325 error, returns the error, otherwise returns false.
327 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
328 will be used to look up the package definition and agent restrictions will be
331 If the additional field I<refnum> is defined, an FS::pkg_referral record will
332 be created and inserted. Multiple FS::pkg_referral records can be created by
333 setting I<refnum> to an array reference of refnums or a hash reference with
334 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
335 record will be created corresponding to cust_main.refnum.
337 If the additional field I<cust_pkg_usageprice> is defined, it will be treated
338 as an arrayref of FS::cust_pkg_usageprice objects, which will be inserted.
339 (Note that this field cannot be set with a usual ->cust_pkg_usageprice method.
340 It can be set as part of the hash when creating the object, or with the B<set>
343 The following options are available:
349 If set true, supresses actions that should only be taken for new package
350 orders. (Currently this includes: intro periods when delay_setup is on,
351 auto-adding a 1st start date, auto-adding expiration/adjourn/contract_end dates)
355 cust_pkg_option records will be created
359 a ticket will be added to this customer with this subject
363 an optional queue name for ticket additions
367 Don't check the legality of the package definition. This should be used
368 when performing a package change that doesn't change the pkgpart (i.e.
376 my( $self, %options ) = @_;
378 my $oldAutoCommit = $FS::UID::AutoCommit;
379 local $FS::UID::AutoCommit = 0;
383 $error = $self->check_pkgpart unless $options{'allow_pkgpart'};
385 my $part_pkg = $self->part_pkg;
387 if ( ! $import && ! $options{'change'} ) {
389 # set order date to now
390 $self->order_date(time) unless ($import && $self->order_date);
392 # if the package def says to start only on the first of the month:
393 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
394 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
395 $mon += 1 unless $mday == 1;
396 until ( $mon < 12 ) { $mon -= 12; $year++; }
397 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
400 if ($self->susp eq 'now' or $part_pkg->start_on_hold) {
401 # if the package was ordered on hold:
403 # - don't set the start date (it will be started manually)
404 $self->set('susp', $self->order_date);
405 $self->set('start_date', '');
407 # set expire/adjourn/contract_end timers, and free days, if appropriate
408 # and automatic package transfer, which can fail, so capture the result
409 $error = $self->set_initial_timers;
411 } # else this is a package change, and shouldn't have "new package" behavior
413 $error ||= $self->SUPER::insert($options{options} ? %{$options{options}} : ());
415 $dbh->rollback if $oldAutoCommit;
419 $self->refnum($self->cust_main->refnum) unless $self->refnum;
420 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
421 $self->process_m2m( 'link_table' => 'pkg_referral',
422 'target_table' => 'part_referral',
423 'params' => $self->refnum,
426 if ( $self->hashref->{cust_pkg_usageprice} ) {
427 for my $cust_pkg_usageprice ( @{ $self->hashref->{cust_pkg_usageprice} } ) {
428 $cust_pkg_usageprice->pkgnum( $self->pkgnum );
429 my $error = $cust_pkg_usageprice->insert;
431 $dbh->rollback if $oldAutoCommit;
437 if ( $self->setup_discountnum || $self->recur_discountnum ) {
438 my $error = $self->insert_discount();
440 $dbh->rollback if $oldAutoCommit;
445 my $conf = new FS::Conf;
447 if ($self->locationnum) {
449 map qsearch( 'part_export', {exportnum=>$_} ),
450 $conf->config('cust_location-exports'); #, $agentnum
452 foreach my $part_export ( @part_export ) {
453 my $error = $part_export->export_pkg_location($self); #, @$export_args);
455 $dbh->rollback if $oldAutoCommit;
456 return "exporting to ". $part_export->exporttype.
457 " (transaction rolled back): $error";
462 if ( ! $import && $conf->config('ticket_system') && $options{ticket_subject} ) {
464 #this init stuff is still inefficient, but at least its limited to
465 # the small number (any?) folks using ticket emailing on pkg order
468 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
475 use FS::TicketSystem;
476 FS::TicketSystem->init();
478 my $q = new RT::Queue($RT::SystemUser);
479 $q->Load($options{ticket_queue}) if $options{ticket_queue};
480 my $t = new RT::Ticket($RT::SystemUser);
481 my $mime = new MIME::Entity;
482 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
483 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
484 Subject => $options{ticket_subject},
487 $t->AddLink( Type => 'MemberOf',
488 Target => 'freeside://freeside/cust_main/'. $self->custnum,
492 if (! $import && $conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
493 my $queue = new FS::queue {
494 'job' => 'FS::cust_main::queueable_print',
496 $error = $queue->insert(
497 'custnum' => $self->custnum,
498 'template' => 'welcome_letter',
502 warn "can't send welcome letter: $error";
507 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
514 This method now works but you probably shouldn't use it.
516 You don't want to delete packages, because there would then be no record
517 the customer ever purchased the package. Instead, see the cancel method and
518 hide cancelled packages.
522 # this is still used internally to abort future package changes, so it
528 # The following foreign keys to cust_pkg are not cleaned up here, and will
529 # cause package deletion to fail:
531 # cust_credit.pkgnum and commission_pkgnum (and cust_credit_void)
532 # cust_credit_bill.pkgnum
533 # cust_pay_pending.pkgnum
534 # cust_pay.pkgnum (and cust_pay_void)
535 # cust_bill_pay.pkgnum (wtf, shouldn't reference pkgnum)
536 # cust_pkg_usage.pkgnum
537 # cust_pkg.uncancel_pkgnum, change_pkgnum, main_pkgnum, and change_to_pkgnum
538 # rt_field_charge.pkgnum
540 # cust_svc is handled by canceling the package before deleting it
541 # cust_pkg_option is handled via option_Common
543 my $oldAutoCommit = $FS::UID::AutoCommit;
544 local $FS::UID::AutoCommit = 0;
547 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
548 my $error = $cust_pkg_discount->delete;
550 $dbh->rollback if $oldAutoCommit;
554 #cust_bill_pkg_discount?
556 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
557 my $error = $cust_pkg_detail->delete;
559 $dbh->rollback if $oldAutoCommit;
564 foreach my $cust_pkg_reason (
566 'table' => 'cust_pkg_reason',
567 'hashref' => { 'pkgnum' => $self->pkgnum },
571 my $error = $cust_pkg_reason->delete;
573 $dbh->rollback if $oldAutoCommit;
578 foreach my $pkg_referral ( $self->pkg_referral ) {
579 my $error = $pkg_referral->delete;
581 $dbh->rollback if $oldAutoCommit;
586 my $error = $self->SUPER::delete(@_);
588 $dbh->rollback if $oldAutoCommit;
592 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
598 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
600 Replaces the OLD_RECORD with this one in the database. If there is an error,
601 returns the error, otherwise returns false.
603 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
605 Changing pkgpart may have disasterous effects. See the order subroutine.
607 setup and bill are normally updated by calling the bill method of a customer
608 object (see L<FS::cust_main>).
610 suspend is normally updated by the suspend and unsuspend methods.
612 cancel is normally updated by the cancel method (and also the order subroutine
615 Available options are:
621 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.
625 the access_user (see L<FS::access_user>) providing the reason
629 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
638 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
643 ( ref($_[0]) eq 'HASH' )
647 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
648 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
651 #return "Can't change setup once it exists!"
652 # if $old->getfield('setup') &&
653 # $old->getfield('setup') != $new->getfield('setup');
655 #some logic for bill, susp, cancel?
657 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
659 my $oldAutoCommit = $FS::UID::AutoCommit;
660 local $FS::UID::AutoCommit = 0;
663 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
664 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
665 my $error = $new->insert_reason(
666 'reason' => $options->{'reason'},
667 'date' => $new->$method,
669 'reason_otaker' => $options->{'reason_otaker'},
672 dbh->rollback if $oldAutoCommit;
673 return "Error inserting cust_pkg_reason: $error";
678 #save off and freeze RADIUS attributes for any associated svc_acct records
680 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
682 #also check for specific exports?
683 # to avoid spurious modify export events
684 @svc_acct = map { $_->svc_x }
685 grep { $_->part_svc->svcdb eq 'svc_acct' }
688 $_->snapshot foreach @svc_acct;
692 my $error = $new->export_pkg_change($old)
693 || $new->SUPER::replace( $old,
695 ? $options->{options}
699 $dbh->rollback if $oldAutoCommit;
703 #for prepaid packages,
704 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
705 foreach my $old_svc_acct ( @svc_acct ) {
706 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
708 $new_svc_acct->replace( $old_svc_acct,
709 'depend_jobnum' => $options->{depend_jobnum},
712 $dbh->rollback if $oldAutoCommit;
717 # also run exports if removing locationnum?
718 # doesn't seem to happen, and we don't export blank locationnum on insert...
719 if ($new->locationnum and ($new->locationnum != $old->locationnum)) {
720 my $conf = new FS::Conf;
722 map qsearch( 'part_export', {exportnum=>$_} ),
723 $conf->config('cust_location-exports'); #, $agentnum
725 foreach my $part_export ( @part_export ) {
726 my $error = $part_export->export_pkg_location($new); #, @$export_args);
728 $dbh->rollback if $oldAutoCommit;
729 return "exporting to ". $part_export->exporttype.
730 " (transaction rolled back): $error";
735 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
742 Checks all fields to make sure this is a valid billing item. If there is an
743 error, returns the error, otherwise returns false. Called by the insert and
751 if ( !$self->locationnum or $self->locationnum == -1 ) {
752 $self->set('locationnum', $self->cust_main->ship_locationnum);
756 $self->ut_numbern('pkgnum')
757 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
758 || $self->ut_numbern('pkgpart')
759 || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' )
760 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
761 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
762 || $self->ut_numbern('quantity')
763 || $self->ut_numbern('start_date')
764 || $self->ut_numbern('setup')
765 || $self->ut_numbern('bill')
766 || $self->ut_numbern('susp')
767 || $self->ut_numbern('cancel')
768 || $self->ut_numbern('adjourn')
769 || $self->ut_numbern('resume')
770 || $self->ut_numbern('expire')
771 || $self->ut_numbern('dundate')
772 || $self->ut_flag('no_auto', [ '', 'Y' ])
773 || $self->ut_flag('waive_setup', [ '', 'Y' ])
774 || $self->ut_flag('separate_bill')
775 || $self->ut_textn('agent_pkgid')
776 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
777 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
778 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
779 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
780 || $self->ut_foreign_keyn('change_to_pkgnum', 'cust_pkg', 'pkgnum')
782 return $error if $error;
784 return "A package with both start date (future start) and setup date (already started) will never bill"
785 if $self->start_date && $self->setup && ! $upgrade;
787 return "A future unsuspend date can only be set for a package with a suspend date"
788 if $self->resume and !$self->susp and !$self->adjourn;
790 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
797 Check the pkgpart to make sure it's allowed with the reg_code and/or
798 promo_code of the package (if present) and with the customer's agent.
799 Called from C<insert>, unless we are doing a package change that doesn't
807 # my $error = $self->ut_numbern('pkgpart'); # already done
810 if ( $self->reg_code ) {
812 unless ( grep { $self->pkgpart == $_->pkgpart }
813 map { $_->reg_code_pkg }
814 qsearchs( 'reg_code', { 'code' => $self->reg_code,
815 'agentnum' => $self->cust_main->agentnum })
817 return "Unknown registration code";
820 } elsif ( $self->promo_code ) {
823 qsearchs('part_pkg', {
824 'pkgpart' => $self->pkgpart,
825 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
827 return 'Unknown promotional code' unless $promo_part_pkg;
831 unless ( $disable_agentcheck ) {
833 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
834 return "agent ". $agent->agentnum. ':'. $agent->agent.
835 " can't purchase pkgpart ". $self->pkgpart
836 unless $agent->pkgpart_hashref->{ $self->pkgpart }
837 || $agent->agentnum == $self->part_pkg->agentnum;
840 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
841 return $error if $error;
849 =item cancel [ OPTION => VALUE ... ]
851 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
852 in this package, then cancels the package itself (sets the cancel field to
855 Available options are:
859 =item quiet - can be set true to supress email cancellation notices.
861 =item time - can be set to cancel the package based on a specific future or
862 historical date. Using time ensures that the remaining amount is calculated
863 correctly. Note however that this is an immediate cancel and just changes
864 the date. You are PROBABLY looking to expire the account instead of using
867 =item reason - can be set to a cancellation reason (see L<FS:reason>),
868 either a reasonnum of an existing reason, or passing a hashref will create
869 a new reason. The hashref should have the following keys: typenum - Reason
870 type (see L<FS::reason_type>, reason - Text of the new reason.
872 =item date - can be set to a unix style timestamp to specify when to
875 =item nobill - can be set true to skip billing if it might otherwise be done.
877 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
878 not credit it. This must be set (by change()) when changing the package
879 to a different pkgpart or location, and probably shouldn't be in any other
880 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
883 =item no_delay_cancel - prevents delay_cancel behavior
884 no matter what other options say, for use when changing packages (or any
885 other time you're really sure you want an immediate cancel)
889 If there is an error, returns the error, otherwise returns false.
893 #NOT DOCUMENTING - this should only be used when calling recursively
894 #=item delay_cancel - for internal use, to allow proper handling of
895 #supplemental packages when the main package is flagged to suspend
896 #before cancelling, probably shouldn't be used otherwise (set the
897 #corresponding package option instead)
900 my( $self, %options ) = @_;
903 # supplemental packages can now be separately canceled, though the UI
904 # shouldn't permit it
906 ## pass all suspend/cancel actions to the main package
907 ## (unless the pkglinknum has been removed, then the link is defunct and
908 ## this package can be canceled on its own)
909 #if ( $self->main_pkgnum and $self->pkglinknum and !$options{'from_main'} ) {
910 # return $self->main_pkg->cancel(%options);
913 my $conf = new FS::Conf;
915 warn "cust_pkg::cancel called with options".
916 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
919 my $oldAutoCommit = $FS::UID::AutoCommit;
920 local $FS::UID::AutoCommit = 0;
923 my $old = $self->select_for_update;
925 if ( $old->get('cancel') || $self->get('cancel') ) {
926 dbh->rollback if $oldAutoCommit;
927 return ""; # no error
930 # XXX possibly set cancel_time to the expire date?
931 my $cancel_time = $options{'time'} || time;
932 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
933 $date = '' if ($date && $date <= $cancel_time); # complain instead?
935 my $delay_cancel = $options{'no_delay_cancel'} ? 0 : $options{'delay_cancel'};
936 if ( !$date && $self->part_pkg->option('delay_cancel',1)
937 && (($self->status eq 'active') || ($self->status eq 'suspended'))
938 && !$options{'no_delay_cancel'}
940 my $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
941 my $expsecs = 60*60*24*$expdays;
942 my $suspfor = $self->susp ? $cancel_time - $self->susp : 0;
943 $expsecs = $expsecs - $suspfor if $suspfor;
944 unless ($expsecs <= 0) { #if it's already been suspended long enough, don't re-suspend
946 $date = $cancel_time + $expsecs;
950 #race condition: usage could be ongoing until unprovisioned
951 #resolved by performing a change package instead (which unprovisions) and
953 if ( !$options{nobill} && !$date ) {
954 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
955 my $copy = $self->new({$self->hash});
957 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
959 'time' => $cancel_time );
960 warn "Error billing during cancel, custnum ".
961 #$self->cust_main->custnum. ": $error"
966 if ( $options{'reason'} ) {
967 $error = $self->insert_reason( 'reason' => $options{'reason'},
968 'action' => $date ? 'expire' : 'cancel',
969 'date' => $date ? $date : $cancel_time,
970 'reason_otaker' => $options{'reason_otaker'},
973 dbh->rollback if $oldAutoCommit;
974 return "Error inserting cust_pkg_reason: $error";
978 my %svc_cancel_opt = ();
979 $svc_cancel_opt{'date'} = $date if $date;
980 foreach my $cust_svc (
983 sort { $a->[1] <=> $b->[1] }
984 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
985 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
987 my $part_svc = $cust_svc->part_svc;
988 next if ( defined($part_svc) and $part_svc->preserve );
989 my $error = $cust_svc->cancel( %svc_cancel_opt );
992 $dbh->rollback if $oldAutoCommit;
993 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
998 # if a reasonnum was passed, get the actual reason object so we can check
1002 if ($options{'reason'} =~ /^\d+$/) {
1003 $reason = FS::reason->by_key($options{'reason'});
1007 # credit remaining time if any of these are true:
1008 # - unused_credit => 1 was passed (this happens when canceling a package
1009 # for a package change when unused_credit_change is set)
1010 # - no unused_credit option, and there is a cancel reason, and the cancel
1011 # reason says to credit the package
1012 # - no unused_credit option, and the package definition says to credit the
1013 # package on cancellation
1015 if ( exists($options{'unused_credit'}) ) {
1016 $do_credit = $options{'unused_credit'};
1017 } elsif ( defined($reason) && $reason->unused_credit ) {
1020 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
1023 my $error = $self->credit_remaining('cancel', $cancel_time);
1025 $dbh->rollback if $oldAutoCommit;
1031 my %hash = $self->hash;
1033 $hash{'expire'} = $date;
1034 if ($delay_cancel) {
1035 # just to be sure these are clear
1036 $hash{'adjourn'} = undef;
1037 $hash{'resume'} = undef;
1040 $hash{'cancel'} = $cancel_time;
1042 $hash{'change_custnum'} = $options{'change_custnum'};
1044 # if this is a supplemental package that's lost its part_pkg_link, and it's
1045 # being canceled for real, unlink it completely
1046 if ( !$date and ! $self->pkglinknum ) {
1047 $hash{main_pkgnum} = '';
1050 # if there is a future package change scheduled, unlink from it (like
1051 # abort_change) first, then delete it.
1052 $hash{'change_to_pkgnum'} = '';
1054 # save the package state
1055 my $new = new FS::cust_pkg ( \%hash );
1056 $error = $new->replace( $self, options => { $self->options } );
1058 if ( $self->change_to_pkgnum ) {
1059 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
1060 $error ||= $change_to->cancel('no_delay_cancel' => 1) || $change_to->delete;
1063 $dbh->rollback if $oldAutoCommit;
1067 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1068 $error = $supp_pkg->cancel(%options,
1070 'date' => $date, #in case it got changed by delay_cancel
1071 'delay_cancel' => $delay_cancel,
1074 $dbh->rollback if $oldAutoCommit;
1075 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1079 if ($delay_cancel && !$options{'from_main'}) {
1080 $error = $new->suspend(
1082 'time' => $cancel_time
1087 foreach my $usage ( $self->cust_pkg_usage ) {
1088 $error = $usage->delete;
1090 $dbh->rollback if $oldAutoCommit;
1091 return "deleting usage pools: $error";
1096 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1097 return '' if $date; #no errors
1099 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
1100 if ( !$options{'quiet'} &&
1101 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
1103 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
1106 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
1107 $error = $msg_template->send( 'cust_main' => $self->cust_main,
1108 'object' => $self );
1110 #should this do something on errors?
1117 =item cancel_if_expired [ NOW_TIMESTAMP ]
1119 Cancels this package if its expire date has been reached.
1123 sub cancel_if_expired {
1125 my $time = shift || time;
1126 return '' unless $self->expire && $self->expire <= $time;
1127 my $error = $self->cancel;
1129 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
1130 $self->custnum. ": $error";
1135 =item uncancel_svc_x
1137 For cancelled cust_pkg, returns a list of new, uninserted FS::svc_X records
1138 for services that would be inserted by L</uncancel>. Returned objects also
1139 include the field _h_svc_x, which contains the service history object.
1141 Set pkgnum before inserting.
1143 Accepts the following options:
1145 only_svcnum - arrayref of svcnum, only returns objects for these svcnum
1146 (and only if they would otherwise be returned by this)
1150 sub uncancel_svc_x {
1151 my ($self, %opt) = @_;
1153 die 'uncancel_svc_x called on a non-cancelled cust_pkg' unless $self->get('cancel');
1155 #find historical services within this timeframe before the package cancel
1156 # (incompatible with "time" option to cust_pkg->cancel?)
1157 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
1158 # too little? (unprovisioing export delay?)
1159 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
1160 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1163 foreach my $h_cust_svc (@h_cust_svc) {
1164 next if $opt{'only_svcnum'} && !(grep { $_ == $h_cust_svc->svcnum } @{$opt{'only_svcnum'}});
1165 # filter out services that still exist on this package (ie preserved svcs)
1166 # but keep services that have since been provisioned on another package (for informational purposes)
1167 next if qsearchs('cust_svc',{ 'svcnum' => $h_cust_svc->svcnum, 'pkgnum' => $self->pkgnum });
1168 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1169 next unless $h_svc_x; # this probably doesn't happen, but just in case
1170 (my $table = $h_svc_x->table) =~ s/^h_//;
1171 require "FS/$table.pm";
1172 my $class = "FS::$table";
1173 my $svc_x = $class->new( {
1174 'svcpart' => $h_cust_svc->svcpart,
1175 '_h_svc_x' => $h_svc_x,
1176 map { $_ => $h_svc_x->get($_) } fields($table)
1180 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1181 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1184 #these are pretty rare, but should handle them
1185 # - dsl_device (mac addresses)
1186 # - phone_device (mac addresses)
1187 # - dsl_note (ikano notes)
1188 # - domain_record (i.e. restore DNS information w/domains)
1189 # - inventory_item(?) (inventory w/un-cancelling service?)
1190 # - nas (svc_broaband nas stuff)
1191 #this stuff is unused in the wild afaik
1192 # - mailinglistmember
1194 # - svc_domain.parent_svcnum?
1195 # - acct_snarf (ancient mail fetching config)
1196 # - cgp_rule (communigate)
1197 # - cust_svc_option (used by our Tron stuff)
1198 # - acct_rt_transaction (used by our time worked stuff)
1200 push @svc_x, $svc_x;
1205 =item uncancel_svc_summary
1207 Returns an array of hashrefs, one for each service that could
1208 potentially be reprovisioned by L</uncancel>, with the following keys:
1216 label - from history table if not currently calculable, undefined if it can't be loaded
1218 reprovisionable - 1 if test reprovision succeeded, otherwise 0
1220 num_cust_svc - number of svcs for this svcpart, only if summarizing (see below)
1222 Cannot be run from within a transaction. Performs inserts
1223 to test the results, and then rolls back the transaction.
1224 Does not perform exports, so does not catch if export would fail.
1226 Also accepts the following options:
1228 no_test_reprovision - skip the test inserts (reprovisionable field will not exist)
1230 summarize_size - if true, returns a single summary record for svcparts with at
1231 least this many svcs, will have key num_cust_svc but not uncancel_svcnum, label or reprovisionable
1235 sub uncancel_svc_summary {
1236 my ($self, %opt) = @_;
1238 die 'uncancel_svc_summary called on a non-cancelled cust_pkg' unless $self->get('cancel');
1239 die 'uncancel_svc_summary called from within a transaction' unless $FS::UID::AutoCommit;
1241 local $FS::svc_Common::noexport_hack = 1; # very important not to run exports!!!
1242 local $FS::UID::AutoCommit = 0;
1244 # sort by svcpart, to check summarize_size
1245 my $uncancel_svc_x = {};
1246 foreach my $svc_x (sort { $a->{'svcpart'} <=> $b->{'svcpart'} } $self->uncancel_svc_x) {
1247 $uncancel_svc_x->{$svc_x->svcpart} = [] unless $uncancel_svc_x->{$svc_x->svcpart};
1248 push @{$uncancel_svc_x->{$svc_x->svcpart}}, $svc_x;
1252 foreach my $svcpart (keys %$uncancel_svc_x) {
1253 my @svcpart_svc_x = @{$uncancel_svc_x->{$svcpart}};
1254 if ($opt{'summarize_size'} && (@svcpart_svc_x >= $opt{'summarize_size'})) {
1255 my $svc_x = $svcpart_svc_x[0]; #grab first one for access to $part_svc
1256 my $part_svc = $svc_x->part_svc;
1258 'svcpart' => $part_svc->svcpart,
1259 'svc' => $part_svc->svc,
1260 'num_cust_svc' => scalar(@svcpart_svc_x),
1263 foreach my $svc_x (@svcpart_svc_x) {
1264 my $part_svc = $svc_x->part_svc;
1266 'svcpart' => $part_svc->svcpart,
1267 'svc' => $part_svc->svc,
1268 'uncancel_svcnum' => $svc_x->get('_h_svc_x')->svcnum,
1270 $svc_x->pkgnum($self->pkgnum); # provisioning services on a canceled package, will be rolled back
1272 unless ($opt{'no_test_reprovision'}) {
1273 # avoid possibly fatal errors from missing linked records
1274 eval { $insert_error = $svc_x->insert };
1275 $insert_error ||= $@;
1277 if ($opt{'no_test_reprovision'} or $insert_error) {
1278 # avoid possibly fatal errors from missing linked records
1279 eval { $out->{'label'} = $svc_x->label };
1280 eval { $out->{'label'} = $svc_x->get('_h_svc_x')->label } unless defined($out->{'label'});
1281 $out->{'reprovisionable'} = 0 unless $opt{'no_test_reprovision'};
1283 $out->{'label'} = $svc_x->label;
1284 $out->{'reprovisionable'} = 1;
1297 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
1298 locationnum, (other fields?). Attempts to re-provision cancelled services
1299 using history information (errors at this stage are not fatal).
1301 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
1303 svc_fatal: service provisioning errors are fatal
1305 svc_errors: pass an array reference, will be filled in with any provisioning errors
1307 only_svcnum: arrayref, only attempt to re-provision these cancelled services
1309 main_pkgnum: link the package as a supplemental package of this one. For
1315 my( $self, %options ) = @_;
1317 #in case you try do do $uncancel-date = $cust_pkg->uncacel
1318 return '' unless $self->get('cancel');
1320 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
1321 return $self->main_pkg->uncancel(%options);
1328 my $oldAutoCommit = $FS::UID::AutoCommit;
1329 local $FS::UID::AutoCommit = 0;
1333 # insert the new package
1336 my $cust_pkg = new FS::cust_pkg {
1337 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
1338 bill => ( $options{'bill'} || $self->get('bill') ),
1340 uncancel_pkgnum => $self->pkgnum,
1341 main_pkgnum => ($options{'main_pkgnum'} || ''),
1342 map { $_ => $self->get($_) } qw(
1343 custnum pkgpart locationnum
1345 susp adjourn resume expire start_date contract_end dundate
1346 change_date change_pkgpart change_locationnum
1347 no_auto separate_bill quantity agent_pkgid
1348 recur_show_zero setup_show_zero
1352 my $error = $cust_pkg->insert(
1353 'change' => 1, #supresses any referral credit to a referring customer
1354 'allow_pkgpart' => 1, # allow this even if the package def is disabled
1357 $dbh->rollback if $oldAutoCommit;
1366 foreach my $svc_x ($self->uncancel_svc_x('only_svcnum' => $options{'only_svcnum'})) {
1368 $svc_x->pkgnum($cust_pkg->pkgnum);
1369 my $svc_error = $svc_x->insert;
1372 if ( $options{svc_fatal} ) {
1373 $dbh->rollback if $oldAutoCommit;
1376 # if we've failed to insert the svc_x object, svc_Common->insert
1377 # will have removed the cust_svc already. if not, then both records
1378 # were inserted but we failed for some other reason (export, most
1379 # likely). in that case, report the error and delete the records.
1380 push @svc_errors, $svc_error;
1381 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1383 # except if export_insert failed, export_delete probably won't be
1385 local $FS::svc_Common::noexport_hack = 1;
1386 my $cleanup_error = $svc_x->delete; # also deletes cust_svc
1387 if ( $cleanup_error ) { # and if THAT fails, then run away
1388 $dbh->rollback if $oldAutoCommit;
1389 return $cleanup_error;
1394 } #foreach uncancel_svc_x
1397 # also move over any services that didn't unprovision at cancellation
1400 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1401 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1402 my $error = $cust_svc->replace;
1404 $dbh->rollback if $oldAutoCommit;
1410 # Uncancel any supplemental packages, and make them supplemental to the
1414 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1416 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1418 $dbh->rollback if $oldAutoCommit;
1419 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1427 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1429 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1430 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1437 Cancels any pending expiration (sets the expire field to null)
1438 for this package and any supplemental packages.
1440 If there is an error, returns the error, otherwise returns false.
1448 my $oldAutoCommit = $FS::UID::AutoCommit;
1449 local $FS::UID::AutoCommit = 0;
1452 my $old = $self->select_for_update;
1454 my $pkgnum = $old->pkgnum;
1455 if ( $old->get('cancel') || $self->get('cancel') ) {
1456 dbh->rollback if $oldAutoCommit;
1457 return "Can't unexpire cancelled package $pkgnum";
1458 # or at least it's pointless
1461 unless ( $old->get('expire') && $self->get('expire') ) {
1462 dbh->rollback if $oldAutoCommit;
1463 return ""; # no error
1466 my %hash = $self->hash;
1467 $hash{'expire'} = '';
1468 my $new = new FS::cust_pkg ( \%hash );
1469 $error = $new->replace( $self, options => { $self->options } );
1471 $dbh->rollback if $oldAutoCommit;
1475 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1476 $error = $supp_pkg->unexpire;
1478 $dbh->rollback if $oldAutoCommit;
1479 return "unexpiring supplemental pkg#".$supp_pkg->pkgnum.": $error";
1483 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1489 =item suspend [ OPTION => VALUE ... ]
1491 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1492 package, then suspends the package itself (sets the susp field to now).
1494 Available options are:
1498 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1499 either a reasonnum of an existing reason, or passing a hashref will create
1500 a new reason. The hashref should have the following keys:
1501 - typenum - Reason type (see L<FS::reason_type>
1502 - reason - Text of the new reason.
1504 =item date - can be set to a unix style timestamp to specify when to
1507 =item time - can be set to override the current time, for calculation
1508 of final invoices or unused-time credits
1510 =item resume_date - can be set to a time when the package should be
1511 unsuspended. This may be more convenient than calling C<unsuspend()>
1514 =item from_main - allows a supplemental package to be suspended, rather
1515 than redirecting the method call to its main package. For internal use.
1517 =item from_cancel - used when suspending from the cancel method, forces
1518 this to skip everything besides basic suspension. For internal use.
1522 If there is an error, returns the error, otherwise returns false.
1527 my( $self, %options ) = @_;
1530 # supplemental packages still can't be separately suspended, but silently
1531 # exit instead of failing or passing the action to the main package (so
1532 # that the "Suspend customer" action doesn't trip over the supplemental
1535 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1539 my $oldAutoCommit = $FS::UID::AutoCommit;
1540 local $FS::UID::AutoCommit = 0;
1543 my $old = $self->select_for_update;
1545 my $pkgnum = $old->pkgnum;
1546 if ( $old->get('cancel') || $self->get('cancel') ) {
1547 dbh->rollback if $oldAutoCommit;
1548 return "Can't suspend cancelled package $pkgnum";
1551 if ( $old->get('susp') || $self->get('susp') ) {
1552 dbh->rollback if $oldAutoCommit;
1553 return ""; # no error # complain on adjourn?
1556 my $suspend_time = $options{'time'} || time;
1557 my $date = $options{date} if $options{date}; # adjourn/suspend later
1558 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1560 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1561 dbh->rollback if $oldAutoCommit;
1562 return "Package $pkgnum expires before it would be suspended.";
1565 # some false laziness with sub cancel
1566 if ( !$options{nobill} && !$date && !$options{'from_cancel'} &&
1567 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1568 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1569 # make the entire cust_main->bill path recognize 'suspend' and
1570 # 'cancel' separately.
1571 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1572 my $copy = $self->new({$self->hash});
1574 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1576 'time' => $suspend_time );
1577 warn "Error billing during suspend, custnum ".
1578 #$self->cust_main->custnum. ": $error"
1583 my $cust_pkg_reason;
1584 if ( $options{'reason'} ) {
1585 $error = $self->insert_reason( 'reason' => $options{'reason'},
1586 'action' => $date ? 'adjourn' : 'suspend',
1587 'date' => $date ? $date : $suspend_time,
1588 'reason_otaker' => $options{'reason_otaker'},
1591 dbh->rollback if $oldAutoCommit;
1592 return "Error inserting cust_pkg_reason: $error";
1594 $cust_pkg_reason = qsearchs('cust_pkg_reason', {
1595 'date' => $date ? $date : $suspend_time,
1596 'action' => $date ? 'A' : 'S',
1597 'pkgnum' => $self->pkgnum,
1601 # if a reasonnum was passed, get the actual reason object so we can check
1603 # (passing a reason hashref is still allowed, but it can't be used with
1604 # the fancy behavioral options.)
1607 if ($options{'reason'} =~ /^\d+$/) {
1608 $reason = FS::reason->by_key($options{'reason'});
1611 my %hash = $self->hash;
1613 $hash{'adjourn'} = $date;
1615 $hash{'susp'} = $suspend_time;
1618 my $resume_date = $options{'resume_date'} || 0;
1619 if ( $resume_date > ($date || $suspend_time) ) {
1620 $hash{'resume'} = $resume_date;
1623 $options{options} ||= {};
1625 my $new = new FS::cust_pkg ( \%hash );
1626 $error = $new->replace( $self, options => { $self->options,
1627 %{ $options{options} },
1631 $dbh->rollback if $oldAutoCommit;
1635 unless ( $date ) { # then we are suspending now
1637 unless ($options{'from_cancel'}) {
1638 # credit remaining time if appropriate
1639 # (if required by the package def, or the suspend reason)
1640 my $unused_credit = $self->part_pkg->option('unused_credit_suspend',1)
1641 || ( defined($reason) && $reason->unused_credit );
1643 if ( $unused_credit ) {
1644 warn "crediting unused time on pkg#".$self->pkgnum."\n" if $DEBUG;
1645 my $error = $self->credit_remaining('suspend', $suspend_time);
1647 $dbh->rollback if $oldAutoCommit;
1653 my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } );
1655 #attempt ordering ala cust_svc_suspend_cascade (without infinite-looping
1656 # on the circular dep case)
1657 # (this is too simple for multi-level deps, we need to use something
1658 # to resolve the DAG properly when possible)
1660 $svcpart{$_->svcpart} = 0 foreach @cust_svc;
1661 foreach my $svcpart ( keys %svcpart ) {
1662 foreach my $part_svc_link (
1663 FS::part_svc_link->by_agentnum($self->cust_main->agentnum,
1664 src_svcpart => $svcpart,
1665 link_type => 'cust_svc_suspend_cascade'
1668 $svcpart{$part_svc_link->dst_svcpart} = max(
1669 $svcpart{$part_svc_link->dst_svcpart},
1670 $svcpart{$part_svc_link->src_svcpart} + 1
1674 @cust_svc = sort { $svcpart{ $a->svcpart } <=> $svcpart{ $b->svcpart } }
1678 foreach my $cust_svc ( @cust_svc ) {
1679 $cust_svc->suspend( 'labels_arrayref' => \@labels );
1682 # suspension fees: if there is a feepart, and it's not an unsuspend fee,
1683 # and this is not a suspend-before-cancel
1684 if ( $cust_pkg_reason ) {
1685 my $reason_obj = $cust_pkg_reason->reason;
1686 if ( $reason_obj->feepart and
1687 ! $reason_obj->fee_on_unsuspend and
1688 ! $options{'from_cancel'} ) {
1690 # register the need to charge a fee, cust_main->bill will do the rest
1691 warn "registering suspend fee: pkgnum ".$self->pkgnum.", feepart ".$reason->feepart."\n"
1693 my $cust_pkg_reason_fee = FS::cust_pkg_reason_fee->new({
1694 'pkgreasonnum' => $cust_pkg_reason->num,
1695 'pkgnum' => $self->pkgnum,
1696 'feepart' => $reason->feepart,
1697 'nextbill' => $reason->fee_hold,
1699 $error ||= $cust_pkg_reason_fee->insert;
1703 my $conf = new FS::Conf;
1704 if ( $conf->config('suspend_email_admin') && !$options{'from_cancel'} ) {
1706 my $error = send_email(
1707 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1708 #invoice_from ??? well as good as any
1709 'to' => $conf->config('suspend_email_admin'),
1710 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1712 "This is an automatic message from your Freeside installation\n",
1713 "informing you that the following customer package has been suspended:\n",
1715 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1716 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1717 ( map { "Service : $_\n" } @labels ),
1719 'custnum' => $self->custnum,
1720 'msgtype' => 'admin'
1724 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1732 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1733 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1735 $dbh->rollback if $oldAutoCommit;
1736 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1740 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1745 =item credit_remaining MODE TIME
1747 Generate a credit for this package for the time remaining in the current
1748 billing period. MODE is either "suspend" or "cancel" (determines the
1749 credit type). TIME is the time of suspension/cancellation. Both arguments
1754 # Implementation note:
1756 # If you pkgpart-change a package that has been billed, and it's set to give
1757 # credit on package change, then this method gets called and then the new
1758 # package will have no last_bill date. Therefore the customer will be credited
1759 # only once (per billing period) even if there are multiple package changes.
1761 # If you location-change a package that has been billed, this method will NOT
1762 # be called and the new package WILL have the last bill date of the old
1765 # If the new package is then canceled within the same billing cycle,
1766 # credit_remaining needs to run calc_remain on the OLD package to determine
1767 # the amount of unused time to credit.
1769 sub credit_remaining {
1770 # Add a credit for remaining service
1771 my ($self, $mode, $time) = @_;
1772 die 'credit_remaining requires suspend or cancel'
1773 unless $mode eq 'suspend' or $mode eq 'cancel';
1774 die 'no suspend/cancel time' unless $time > 0;
1776 my $conf = FS::Conf->new;
1777 my $reason_type = $conf->config($mode.'_credit_type');
1781 my $remain_pkg = $self;
1782 my (@billpkgnums, @amounts, @setuprecurs);
1784 # we may have to walk back past some package changes to get to the
1785 # one that actually has unused time. loop until that happens, or we
1786 # reach the first package in the chain.
1788 my $last_bill = $remain_pkg->get('last_bill') || 0;
1789 my $next_bill = $remain_pkg->get('bill') || 0;
1790 if ( $last_bill > 0 # the package has been billed
1791 and $next_bill > 0 # the package has a next bill date
1792 and $next_bill >= $time # which is in the future
1795 # Find actual charges for the period ending on or after the cancel
1797 my @charges = qsearch('cust_bill_pkg', {
1798 pkgnum => $remain_pkg->pkgnum,
1799 edate => {op => '>=', value => $time},
1800 recur => {op => '>' , value => 0},
1803 foreach my $cust_bill_pkg (@charges) {
1804 # hack to deal with the weird behavior of edate on package
1806 my $edate = $cust_bill_pkg->edate;
1807 if ( $self->recur_temporality eq 'preceding' ) {
1808 $edate = $self->add_freq($cust_bill_pkg->sdate);
1811 # this will also get any package charges that are _entirely_ after
1812 # the cancellation date (can happen with advance billing). in that
1813 # case, use the entire recurring charge:
1814 my $amount = $cust_bill_pkg->recur - $cust_bill_pkg->usage;
1815 my $max_credit = $amount
1816 - $cust_bill_pkg->credited('', '', setuprecur => 'recur') || 0;
1818 # but if the cancellation happens during the interval, prorate it:
1819 # (XXX obey prorate_round_day here?)
1820 if ( $cust_bill_pkg->sdate < $time ) {
1822 ($edate - $time) / ($edate - $cust_bill_pkg->sdate);
1825 # if there are existing credits, don't let the sum of credits exceed
1826 # the recurring charge
1827 $amount = $max_credit if $amount > $max_credit;
1829 $amount = sprintf('%.2f', $amount);
1831 # if no time has been used and/or there are existing line item
1832 # credits, we may end up not needing to credit anything.
1833 if ( $amount > 0 ) {
1835 push @billpkgnums, $cust_bill_pkg->billpkgnum;
1836 push @amounts, $amount;
1837 push @setuprecurs, 'recur';
1839 warn "Crediting for $amount on package ".$remain_pkg->pkgnum."\n"
1848 if ( my $changed_from_pkgnum = $remain_pkg->change_pkgnum ) {
1849 $remain_pkg = FS::cust_pkg->by_key($changed_from_pkgnum);
1851 # the package has really never been billed
1856 # keep traditional behavior here.
1858 my $reason = FS::reason->new_or_existing(
1859 reason => 'Credit for unused time on '. $self->part_pkg->pkg,
1860 type => $reason_type,
1864 return "failed to set credit reason: $@";
1867 my $error = FS::cust_credit->credit_lineitems(
1868 'billpkgnums' => \@billpkgnums,
1869 'setuprecurs' => \@setuprecurs,
1870 'amounts' => \@amounts,
1871 'custnum' => $self->custnum,
1873 'reasonnum' => $reason->reasonnum,
1881 =item unsuspend [ OPTION => VALUE ... ]
1883 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1884 package, then unsuspends the package itself (clears the susp field and the
1885 adjourn field if it is in the past). If the suspend reason includes an
1886 unsuspension package, that package will be ordered.
1888 Available options are:
1894 Can be set to a date to unsuspend the package in the future (the 'resume'
1897 =item adjust_next_bill
1899 Can be set true to adjust the next bill date forward by
1900 the amount of time the account was inactive. This was set true by default
1901 in the past (from 1.4.2 and 1.5.0pre6 through 1.7.0), but now needs to be
1902 explicitly requested with this option or in the price plan.
1906 If there is an error, returns the error, otherwise returns false.
1911 my( $self, %opt ) = @_;
1914 # pass all suspend/cancel actions to the main package
1915 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1916 return $self->main_pkg->unsuspend(%opt);
1919 my $oldAutoCommit = $FS::UID::AutoCommit;
1920 local $FS::UID::AutoCommit = 0;
1923 my $old = $self->select_for_update;
1925 my $pkgnum = $old->pkgnum;
1926 if ( $old->get('cancel') || $self->get('cancel') ) {
1927 $dbh->rollback if $oldAutoCommit;
1928 return "Can't unsuspend cancelled package $pkgnum";
1931 unless ( $old->get('susp') && $self->get('susp') ) {
1932 $dbh->rollback if $oldAutoCommit;
1933 return ""; # no error # complain instead?
1936 # handle the case of setting a future unsuspend (resume) date
1937 # and do not continue to actually unsuspend the package
1938 my $date = $opt{'date'};
1939 if ( $date and $date > time ) { # return an error if $date <= time?
1941 if ( $old->get('expire') && $old->get('expire') < $date ) {
1942 $dbh->rollback if $oldAutoCommit;
1943 return "Package $pkgnum expires before it would be unsuspended.";
1946 my $new = new FS::cust_pkg { $self->hash };
1947 $new->set('resume', $date);
1948 $error = $new->replace($self, options => $self->options);
1951 $dbh->rollback if $oldAutoCommit;
1955 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1961 if (!$self->setup) {
1962 # then this package is being released from on-hold status
1963 $error = $self->set_initial_timers;
1965 $dbh->rollback if $oldAutoCommit;
1972 foreach my $cust_svc (
1973 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1975 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1977 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1978 $dbh->rollback if $oldAutoCommit;
1979 return "Illegal svcdb value in part_svc!";
1982 require "FS/$svcdb.pm";
1984 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1986 $error = $svc->unsuspend;
1988 $dbh->rollback if $oldAutoCommit;
1991 my( $label, $value ) = $cust_svc->label;
1992 push @labels, "$label: $value";
1997 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1998 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
2000 my %hash = $self->hash;
2001 my $inactive = time - $hash{'susp'};
2003 my $conf = new FS::Conf;
2005 #adjust the next bill date forward
2006 # increment next bill date if certain conditions are met:
2007 # - it was due to be billed at some point
2008 # - either the global or local config says to do this
2009 my $adjust_bill = 0;
2012 && ( $hash{'bill'} || $hash{'setup'} )
2013 && ( $opt{'adjust_next_bill'}
2014 || $conf->exists('unsuspend-always_adjust_next_bill_date')
2015 || $self->part_pkg->option('unsuspend_adjust_bill', 1)
2022 # - the package billed during suspension
2023 # - or it was ordered on hold
2024 # - or the customer was credited for the unused time
2026 if ( $self->option('suspend_bill',1)
2027 or ( $self->part_pkg->option('suspend_bill',1)
2028 and ! $self->option('no_suspend_bill',1)
2030 or $hash{'order_date'} == $hash{'susp'}
2035 if ( $adjust_bill ) {
2036 if ( $self->part_pkg->option('unused_credit_suspend')
2037 or ( ref($reason) and $reason->unused_credit ) ) {
2038 # then the customer was credited for the unused time before suspending,
2039 # so their next bill should be immediate
2040 $hash{'bill'} = time;
2042 # add the length of time suspended to the bill date
2043 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
2048 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
2049 $hash{'resume'} = '' if !$hash{'adjourn'};
2050 my $new = new FS::cust_pkg ( \%hash );
2051 $error = $new->replace( $self, options => { $self->options } );
2053 $dbh->rollback if $oldAutoCommit;
2060 if ( $reason->unsuspend_pkgpart ) {
2061 warn "Suspend reason '".$reason->reason."' uses deprecated unsuspend_pkgpart feature.\n";
2062 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
2063 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
2065 my $start_date = $self->cust_main->next_bill_date
2066 if $reason->unsuspend_hold;
2069 $unsusp_pkg = FS::cust_pkg->new({
2070 'custnum' => $self->custnum,
2071 'pkgpart' => $reason->unsuspend_pkgpart,
2072 'start_date' => $start_date,
2073 'locationnum' => $self->locationnum,
2074 # discount? probably not...
2077 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
2080 # new way, using fees
2081 if ( $reason->feepart and $reason->fee_on_unsuspend ) {
2082 # register the need to charge a fee, cust_main->bill will do the rest
2083 warn "registering unsuspend fee: pkgnum ".$self->pkgnum.", feepart ".$reason->feepart."\n"
2085 my $cust_pkg_reason_fee = FS::cust_pkg_reason_fee->new({
2086 'pkgreasonnum' => $cust_pkg_reason->num,
2087 'pkgnum' => $self->pkgnum,
2088 'feepart' => $reason->feepart,
2089 'nextbill' => $reason->fee_hold,
2091 $error ||= $cust_pkg_reason_fee->insert;
2095 $dbh->rollback if $oldAutoCommit;
2100 if ( $conf->config('unsuspend_email_admin') ) {
2102 my $error = send_email(
2103 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
2104 #invoice_from ??? well as good as any
2105 'to' => $conf->config('unsuspend_email_admin'),
2106 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
2107 "This is an automatic message from your Freeside installation\n",
2108 "informing you that the following customer package has been unsuspended:\n",
2110 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
2111 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
2112 ( map { "Service : $_\n" } @labels ),
2114 "An unsuspension fee was charged: ".
2115 $unsusp_pkg->part_pkg->pkg_comment."\n"
2119 'custnum' => $self->custnum,
2120 'msgtype' => 'admin',
2124 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
2130 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
2131 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
2133 $dbh->rollback if $oldAutoCommit;
2134 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
2138 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2145 Cancels any pending suspension (sets the adjourn field to null)
2146 for this package and any supplemental packages.
2148 If there is an error, returns the error, otherwise returns false.
2156 my $oldAutoCommit = $FS::UID::AutoCommit;
2157 local $FS::UID::AutoCommit = 0;
2160 my $old = $self->select_for_update;
2162 my $pkgnum = $old->pkgnum;
2163 if ( $old->get('cancel') || $self->get('cancel') ) {
2164 dbh->rollback if $oldAutoCommit;
2165 return "Can't unadjourn cancelled package $pkgnum";
2166 # or at least it's pointless
2169 if ( $old->get('susp') || $self->get('susp') ) {
2170 dbh->rollback if $oldAutoCommit;
2171 return "Can't unadjourn suspended package $pkgnum";
2172 # perhaps this is arbitrary
2175 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
2176 dbh->rollback if $oldAutoCommit;
2177 return ""; # no error
2180 my %hash = $self->hash;
2181 $hash{'adjourn'} = '';
2182 $hash{'resume'} = '';
2183 my $new = new FS::cust_pkg ( \%hash );
2184 $error = $new->replace( $self, options => { $self->options } );
2186 $dbh->rollback if $oldAutoCommit;
2190 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
2191 $error = $supp_pkg->unadjourn;
2193 $dbh->rollback if $oldAutoCommit;
2194 return "unadjourning supplemental pkg#".$supp_pkg->pkgnum.": $error";
2198 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2205 =item change HASHREF | OPTION => VALUE ...
2207 Changes this package: cancels it and creates a new one, with a different
2208 pkgpart or locationnum or both. All services are transferred to the new
2209 package (no change will be made if this is not possible).
2211 Options may be passed as a list of key/value pairs or as a hash reference.
2218 New locationnum, to change the location for this package.
2222 New FS::cust_location object, to create a new location and assign it
2227 New FS::cust_main object, to create a new customer and assign the new package
2232 New pkgpart (see L<FS::part_pkg>).
2236 New refnum (see L<FS::part_referral>).
2240 New quantity; if unspecified, the new package will have the same quantity
2245 "New" (existing) FS::cust_pkg object. The package's services and other
2246 attributes will be transferred to this package.
2250 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
2251 susp, adjourn, cancel, expire, and contract_end) to the new package.
2253 =item unprotect_svcs
2255 Normally, change() will rollback and return an error if some services
2256 can't be transferred (also see the I<cust_pkg-change_svcpart> config option).
2257 If unprotect_svcs is true, this method will transfer as many services as
2258 it can and then unconditionally cancel the old package.
2262 If specified, sets this value for the contract_end date on the new package
2263 (without regard for keep_dates or the usual date-preservation behavior.)
2264 Will throw an error if defined but false; the UI doesn't allow editing
2265 this unless it already exists, making removal impossible to undo.
2269 At least one of locationnum, cust_location, pkgpart, refnum, cust_main, or
2270 cust_pkg must be specified (otherwise, what's the point?)
2272 Returns either the new FS::cust_pkg object or a scalar error.
2276 my $err_or_new_cust_pkg = $old_cust_pkg->change
2280 #used by change and change_later
2281 #didn't put with documented check methods because it depends on change-specific opts
2282 #and it also possibly edits the value of opts
2286 if ( defined($opt->{'contract_end'}) ) {
2287 my $current_contract_end = $self->get('contract_end');
2288 unless ($opt->{'contract_end'}) {
2289 if ($current_contract_end) {
2290 return "Cannot remove contract end date when changing packages";
2292 #shouldn't even pass this option if there's not a current value
2293 #but can be handled gracefully if the option is empty
2294 warn "Contract end date passed unexpectedly";
2295 delete $opt->{'contract_end'};
2299 unless ($current_contract_end) {
2300 #option shouldn't be passed, throw error if it's non-empty
2301 return "Cannot add contract end date when changing packages " . $self->pkgnum;
2307 #some false laziness w/order
2310 my $opt = ref($_[0]) ? shift : { @_ };
2312 my $conf = new FS::Conf;
2314 # handle contract_end on cust_pkg same as passed option
2315 if ( $opt->{'cust_pkg'} ) {
2316 $opt->{'contract_end'} = $opt->{'cust_pkg'}->contract_end;
2317 delete $opt->{'contract_end'} unless $opt->{'contract_end'};
2320 # check contract_end, prevent adding/removing
2321 my $error = $self->_check_change($opt);
2322 return $error if $error;
2324 # Transactionize this whole mess
2325 my $oldAutoCommit = $FS::UID::AutoCommit;
2326 local $FS::UID::AutoCommit = 0;
2329 if ( $opt->{'cust_location'} ) {
2330 $error = $opt->{'cust_location'}->find_or_insert;
2332 $dbh->rollback if $oldAutoCommit;
2333 return "creating location record: $error";
2335 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
2338 # figure out if we're changing pkgpart
2339 if ( $opt->{'cust_pkg'} ) {
2340 $opt->{'pkgpart'} = $opt->{'cust_pkg'}->pkgpart;
2343 # whether to override pkgpart checking on the new package
2344 my $same_pkgpart = 1;
2345 if ( $opt->{'pkgpart'} and ( $opt->{'pkgpart'} != $self->pkgpart ) ) {
2349 # Before going any further here: if the package is still in the pre-setup
2350 # state, it's safe to modify it in place. No need to charge/credit for
2351 # partial period, transfer usage pools, copy invoice details, or change any
2352 # dates. We DO need to "transfer" services (from the package to itself) to
2353 # check their validity on the new pkgpart.
2354 if ( ! $self->setup and ! $opt->{cust_pkg} and ! $opt->{cust_main} ) {
2355 foreach ( qw( locationnum pkgpart quantity refnum salesnum ) ) {
2356 if ( length($opt->{$_}) ) {
2357 $self->set($_, $opt->{$_});
2360 # almost. if the new pkgpart specifies start/adjourn/expire timers,
2362 if ( !$same_pkgpart ) {
2363 $error ||= $self->set_initial_timers;
2365 # but if contract_end was explicitly specified, that overrides all else
2366 $self->set('contract_end', $opt->{'contract_end'})
2367 if $opt->{'contract_end'};
2369 $error ||= $self->replace;
2371 $dbh->rollback if $oldAutoCommit;
2372 return "modifying package: $error";
2375 # check/convert services (only on pkgpart change, to avoid surprises
2376 # when editing locations)
2377 # (maybe do this if changing quantity?)
2378 if ( !$same_pkgpart ) {
2380 $error = $self->transfer($self);
2382 if ( $error and $error == 0 ) {
2383 $error = "transferring $error";
2384 } elsif ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
2385 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
2386 $error = $self->transfer($self, 'change_svcpart'=>1 );
2387 if ($error and $error == 0) {
2388 $error = "converting $error";
2393 $error = "unable to transfer all services";
2397 $dbh->rollback if $oldAutoCommit;
2401 } # done transferring services
2403 $dbh->commit if $oldAutoCommit;
2412 $hash{'setup'} = $time if $self->get('setup');
2414 $hash{'change_date'} = $time;
2415 $hash{"change_$_"} = $self->$_()
2416 foreach qw( pkgnum pkgpart locationnum );
2418 my $unused_credit = 0;
2419 my $keep_dates = $opt->{'keep_dates'};
2421 # Special case. If the pkgpart is changing, and the customer is going to be
2422 # credited for remaining time, don't keep setup, bill, or last_bill dates,
2423 # and DO pass the flag to cancel() to credit the customer. If the old
2424 # package had a setup date, set the new package's setup to the package
2425 # change date so that it has the same status as before.
2426 if ( $opt->{'pkgpart'}
2427 and $opt->{'pkgpart'} != $self->pkgpart
2428 and $self->part_pkg->option('unused_credit_change', 1) ) {
2431 $hash{'last_bill'} = '';
2435 if ( $keep_dates ) {
2436 foreach my $date ( qw(setup bill last_bill) ) {
2437 $hash{$date} = $self->getfield($date);
2440 # always keep the following dates
2441 foreach my $date (qw(order_date susp adjourn cancel expire resume
2442 start_date contract_end)) {
2443 $hash{$date} = $self->getfield($date);
2445 # but if contract_end was explicitly specified, that overrides all else
2446 $hash{'contract_end'} = $opt->{'contract_end'}
2447 if $opt->{'contract_end'};
2449 # allow $opt->{'locationnum'} = '' to specifically set it to null
2450 # (i.e. customer default location)
2451 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2453 # usually this doesn't matter. the two cases where it does are:
2454 # 1. unused_credit_change + pkgpart change + setup fee on the new package
2456 # 2. (more importantly) changing a package before it's billed
2457 $hash{'waive_setup'} = $self->waive_setup;
2459 # if this package is scheduled for a future package change, preserve that
2460 $hash{'change_to_pkgnum'} = $self->change_to_pkgnum;
2462 my $custnum = $self->custnum;
2463 if ( $opt->{cust_main} ) {
2464 my $cust_main = $opt->{cust_main};
2465 unless ( $cust_main->custnum ) {
2466 my $error = $cust_main->insert( @{ $opt->{cust_main_insert_args}||[] } );
2468 $dbh->rollback if $oldAutoCommit;
2469 return "inserting customer record: $error";
2472 $custnum = $cust_main->custnum;
2475 $hash{'contactnum'} = $opt->{'contactnum'} if $opt->{'contactnum'};
2478 if ( $opt->{'cust_pkg'} ) {
2479 # The target package already exists; update it to show that it was
2480 # changed from this package.
2481 $cust_pkg = $opt->{'cust_pkg'};
2483 # follow all the above rules for date changes, etc.
2484 foreach (keys %hash) {
2485 $cust_pkg->set($_, $hash{$_});
2487 # except those that implement the future package change behavior
2488 foreach (qw(change_to_pkgnum start_date expire)) {
2489 $cust_pkg->set($_, '');
2492 $error = $cust_pkg->replace;
2495 # Create the new package.
2496 $cust_pkg = new FS::cust_pkg {
2497 custnum => $custnum,
2498 locationnum => $opt->{'locationnum'},
2499 ( map { $_ => ( $opt->{$_} || $self->$_() ) }
2500 qw( pkgpart quantity refnum salesnum )
2504 $error = $cust_pkg->insert( 'change' => 1,
2505 'allow_pkgpart' => $same_pkgpart );
2508 $dbh->rollback if $oldAutoCommit;
2509 return "inserting new package: $error";
2512 # Transfer services and cancel old package.
2513 # Enforce service limits only if this is a pkgpart change.
2514 local $FS::cust_svc::ignore_quantity;
2515 $FS::cust_svc::ignore_quantity = 1 if $same_pkgpart;
2516 $error = $self->transfer($cust_pkg);
2517 if ($error and $error == 0) {
2518 # $old_pkg->transfer failed.
2519 $dbh->rollback if $oldAutoCommit;
2520 return "transferring $error";
2523 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
2524 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
2525 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
2526 if ($error and $error == 0) {
2527 # $old_pkg->transfer failed.
2528 $dbh->rollback if $oldAutoCommit;
2529 return "converting $error";
2533 # We set unprotect_svcs when executing a "future package change". It's
2534 # not a user-interactive operation, so returning an error means the
2535 # package change will just fail. Rather than have that happen, we'll
2536 # let leftover services be deleted.
2537 if ($error > 0 and !$opt->{'unprotect_svcs'}) {
2538 # Transfers were successful, but we still had services left on the old
2539 # package. We can't change the package under this circumstances, so abort.
2540 $dbh->rollback if $oldAutoCommit;
2541 return "unable to transfer all services";
2544 #reset usage if changing pkgpart
2545 # AND usage rollover is off (otherwise adds twice, now and at package bill)
2546 if ($self->pkgpart != $cust_pkg->pkgpart) {
2547 my $part_pkg = $cust_pkg->part_pkg;
2548 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
2552 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
2555 $dbh->rollback if $oldAutoCommit;
2556 return "setting usage values: $error";
2559 # if NOT changing pkgpart, transfer any usage pools over
2560 foreach my $usage ($self->cust_pkg_usage) {
2561 $usage->set('pkgnum', $cust_pkg->pkgnum);
2562 $error = $usage->replace;
2564 $dbh->rollback if $oldAutoCommit;
2565 return "transferring usage pools: $error";
2570 # transfer usage pricing add-ons, if we're not changing pkgpart or if they were specified
2571 if ( $same_pkgpart || $opt->{'cust_pkg_usageprice'}) {
2572 my @old_cust_pkg_usageprice;
2573 if ($opt->{'cust_pkg_usageprice'}) {
2574 @old_cust_pkg_usageprice = @{ $opt->{'cust_pkg_usageprice'} };
2576 @old_cust_pkg_usageprice = $self->cust_pkg_usageprice;
2578 foreach my $old_cust_pkg_usageprice (@old_cust_pkg_usageprice) {
2579 my $new_cust_pkg_usageprice = new FS::cust_pkg_usageprice {
2580 'pkgnum' => $cust_pkg->pkgnum,
2581 'usagepricepart' => $old_cust_pkg_usageprice->usagepricepart,
2582 'quantity' => $old_cust_pkg_usageprice->quantity,
2584 $error = $new_cust_pkg_usageprice->insert;
2586 $dbh->rollback if $oldAutoCommit;
2587 return "Error transferring usage pricing add-on: $error";
2592 # transfer discounts, if we're not changing pkgpart
2593 if ( $same_pkgpart ) {
2594 foreach my $old_discount ($self->cust_pkg_discount_active) {
2595 # don't remove the old discount, we may still need to bill that package.
2596 my $new_discount = new FS::cust_pkg_discount {
2597 'pkgnum' => $cust_pkg->pkgnum,
2598 'discountnum' => $old_discount->discountnum,
2599 'months_used' => $old_discount->months_used,
2601 $error = $new_discount->insert;
2603 $dbh->rollback if $oldAutoCommit;
2604 return "transferring discounts: $error";
2609 # transfer (copy) invoice details
2610 foreach my $detail ($self->cust_pkg_detail) {
2611 my $new_detail = FS::cust_pkg_detail->new({ $detail->hash });
2612 $new_detail->set('pkgdetailnum', '');
2613 $new_detail->set('pkgnum', $cust_pkg->pkgnum);
2614 $error = $new_detail->insert;
2616 $dbh->rollback if $oldAutoCommit;
2617 return "transferring package notes: $error";
2621 # transfer scheduled expire/adjourn reasons
2622 foreach my $action ('expire', 'adjourn') {
2623 if ( $cust_pkg->get($action) ) {
2624 my $reason = $self->last_cust_pkg_reason($action);
2626 $reason->set('pkgnum', $cust_pkg->pkgnum);
2627 $error = $reason->replace;
2629 $dbh->rollback if $oldAutoCommit;
2630 return "transferring $action reason: $error";
2638 if ( !$opt->{'cust_pkg'} ) {
2639 # Order any supplemental packages.
2640 my $part_pkg = $cust_pkg->part_pkg;
2641 my @old_supp_pkgs = $self->supplemental_pkgs;
2642 foreach my $link ($part_pkg->supp_part_pkg_link) {
2644 foreach (@old_supp_pkgs) {
2645 if ($_->pkgpart == $link->dst_pkgpart) {
2647 $_->pkgpart(0); # so that it can't match more than once
2651 # false laziness with FS::cust_main::Packages::order_pkg
2652 my $new = FS::cust_pkg->new({
2653 pkgpart => $link->dst_pkgpart,
2654 pkglinknum => $link->pkglinknum,
2655 custnum => $custnum,
2656 main_pkgnum => $cust_pkg->pkgnum,
2657 locationnum => $cust_pkg->locationnum,
2658 start_date => $cust_pkg->start_date,
2659 order_date => $cust_pkg->order_date,
2660 expire => $cust_pkg->expire,
2661 adjourn => $cust_pkg->adjourn,
2662 contract_end => $cust_pkg->contract_end,
2663 refnum => $cust_pkg->refnum,
2664 discountnum => $cust_pkg->discountnum,
2665 waive_setup => $cust_pkg->waive_setup,
2667 if ( $old and $opt->{'keep_dates'} ) {
2668 foreach (qw(setup bill last_bill)) {
2669 $new->set($_, $old->get($_));
2672 $error = $new->insert( allow_pkgpart => $same_pkgpart );
2675 $error ||= $old->transfer($new);
2677 if ( $error and $error > 0 ) {
2678 # no reason why this should ever fail, but still...
2679 $error = "Unable to transfer all services from supplemental package ".
2683 $dbh->rollback if $oldAutoCommit;
2686 push @new_supp_pkgs, $new;
2688 } # if !$opt->{'cust_pkg'}
2689 # because if there is one, then supplemental packages would already
2690 # have been created for it.
2692 #Good to go, cancel old package. Notify 'cancel' of whether to credit
2694 #Don't allow billing the package (preceding period packages and/or
2695 #outstanding usage) if we are keeping dates (i.e. location changing),
2696 #because the new package will be billed for the same date range.
2697 #Supplemental packages are also canceled here.
2699 # during scheduled changes, avoid canceling the package we just
2701 $self->set('change_to_pkgnum' => '');
2703 $error = $self->cancel(
2705 unused_credit => $unused_credit,
2706 nobill => $keep_dates,
2707 change_custnum => ( $self->custnum != $custnum ? $custnum : '' ),
2708 no_delay_cancel => 1,
2711 $dbh->rollback if $oldAutoCommit;
2712 return "canceling old package: $error";
2715 # transfer rt_field_charge, if we're not changing pkgpart
2716 # after billing of old package, before billing of new package
2717 if ( $same_pkgpart ) {
2718 foreach my $rt_field_charge ($self->rt_field_charge) {
2719 $rt_field_charge->set('pkgnum', $cust_pkg->pkgnum);
2720 $error = $rt_field_charge->replace;
2722 $dbh->rollback if $oldAutoCommit;
2723 return "transferring rt_field_charge: $error";
2728 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
2730 my $error = $cust_pkg->cust_main->bill(
2731 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
2734 $dbh->rollback if $oldAutoCommit;
2735 return "billing new package: $error";
2739 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2745 =item change_later OPTION => VALUE...
2747 Schedule a package change for a later date. This actually orders the new
2748 package immediately, but sets its start date for a future date, and sets
2749 the current package to expire on the same date.
2751 If the package is already scheduled for a change, this can be called with
2752 'start_date' to change the scheduled date, or with pkgpart and/or
2753 locationnum to modify the package change. To cancel the scheduled change
2754 entirely, see C<abort_change>.
2762 The date for the package change. Required, and must be in the future.
2772 The pkgpart, locationnum, quantity and optional contract_end of the new
2773 package, with the same meaning as in C<change>.
2781 my $opt = ref($_[0]) ? shift : { @_ };
2783 # check contract_end, prevent adding/removing
2784 my $error = $self->_check_change($opt);
2785 return $error if $error;
2787 my $oldAutoCommit = $FS::UID::AutoCommit;
2788 local $FS::UID::AutoCommit = 0;
2791 my $cust_main = $self->cust_main;
2793 my $date = delete $opt->{'start_date'} or return 'start_date required';
2795 if ( $date <= time ) {
2796 $dbh->rollback if $oldAutoCommit;
2797 return "start_date $date is in the past";
2800 # If the user entered a new location, set it up now.
2801 if ( $opt->{'cust_location'} ) {
2802 $error = $opt->{'cust_location'}->find_or_insert;
2804 $dbh->rollback if $oldAutoCommit;
2805 return "creating location record: $error";
2807 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
2810 if ( $self->change_to_pkgnum ) {
2811 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
2812 my $new_pkgpart = $opt->{'pkgpart'}
2813 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $change_to->pkgpart;
2814 my $new_locationnum = $opt->{'locationnum'}
2815 if $opt->{'locationnum'} and $opt->{'locationnum'} != $change_to->locationnum;
2816 my $new_quantity = $opt->{'quantity'}
2817 if $opt->{'quantity'} and $opt->{'quantity'} != $change_to->quantity;
2818 my $new_contract_end = $opt->{'contract_end'}
2819 if $opt->{'contract_end'} and $opt->{'contract_end'} != $change_to->contract_end;
2820 if ( $new_pkgpart or $new_locationnum or $new_quantity or $new_contract_end ) {
2821 # it hasn't been billed yet, so in principle we could just edit
2822 # it in place (w/o a package change), but that's bad form.
2823 # So change the package according to the new options...
2824 my $err_or_pkg = $change_to->change(%$opt);
2825 if ( ref $err_or_pkg ) {
2826 # Then set that package up for a future start.
2827 $self->set('change_to_pkgnum', $err_or_pkg->pkgnum);
2828 $self->set('expire', $date); # in case it's different
2829 $err_or_pkg->set('start_date', $date);
2830 $err_or_pkg->set('change_date', '');
2831 $err_or_pkg->set('change_pkgnum', '');
2833 $error = $self->replace ||
2834 $err_or_pkg->replace ||
2835 #because change() might've edited existing scheduled change in place
2836 (($err_or_pkg->pkgnum == $change_to->pkgnum) ? '' :
2837 $change_to->cancel('no_delay_cancel' => 1) ||
2838 $change_to->delete);
2840 $error = $err_or_pkg;
2842 } else { # change the start date only.
2843 $self->set('expire', $date);
2844 $change_to->set('start_date', $date);
2845 $error = $self->replace || $change_to->replace;
2848 $dbh->rollback if $oldAutoCommit;
2851 $dbh->commit if $oldAutoCommit;
2854 } # if $self->change_to_pkgnum
2856 my $new_pkgpart = $opt->{'pkgpart'}
2857 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart;
2858 my $new_locationnum = $opt->{'locationnum'}
2859 if $opt->{'locationnum'} and $opt->{'locationnum'} != $self->locationnum;
2860 my $new_quantity = $opt->{'quantity'}
2861 if $opt->{'quantity'} and $opt->{'quantity'} != $self->quantity;
2862 my $new_contract_end = $opt->{'contract_end'}
2863 if $opt->{'contract_end'} and $opt->{'contract_end'} != $self->contract_end;
2865 return '' unless $new_pkgpart or $new_locationnum or $new_quantity or $new_contract_end; # wouldn't do anything
2867 # allow $opt->{'locationnum'} = '' to specifically set it to null
2868 # (i.e. customer default location)
2869 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2871 my $new = FS::cust_pkg->new( {
2872 custnum => $self->custnum,
2873 locationnum => $opt->{'locationnum'},
2874 start_date => $date,
2875 map { $_ => ( $opt->{$_} || $self->$_() ) }
2876 qw( pkgpart quantity refnum salesnum contract_end )
2878 $error = $new->insert('change' => 1,
2879 'allow_pkgpart' => ($new_pkgpart ? 0 : 1));
2881 $self->set('change_to_pkgnum', $new->pkgnum);
2882 $self->set('expire', $date);
2883 $error = $self->replace;
2886 $dbh->rollback if $oldAutoCommit;
2888 $dbh->commit if $oldAutoCommit;
2896 Cancels a future package change scheduled by C<change_later>.
2902 my $oldAutoCommit = $FS::UID::AutoCommit;
2903 local $FS::UID::AutoCommit = 0;
2905 my $pkgnum = $self->change_to_pkgnum;
2906 my $change_to = FS::cust_pkg->by_key($pkgnum) if $pkgnum;
2908 $self->set('change_to_pkgnum', '');
2909 $self->set('expire', '');
2910 $error = $self->replace;
2912 $error ||= $change_to->cancel || $change_to->delete;
2915 if ( $oldAutoCommit ) {
2926 =item set_quantity QUANTITY
2928 Change the package's quantity field. This is one of the few package properties
2929 that can safely be changed without canceling and reordering the package
2930 (because it doesn't affect tax eligibility). Returns an error or an
2937 $self = $self->replace_old; # just to make sure
2938 $self->quantity(shift);
2942 =item set_salesnum SALESNUM
2944 Change the package's salesnum (sales person) field. This is one of the few
2945 package properties that can safely be changed without canceling and reordering
2946 the package (because it doesn't affect tax eligibility). Returns an error or
2953 $self = $self->replace_old; # just to make sure
2954 $self->salesnum(shift);
2956 # XXX this should probably reassign any credit that's already been given
2959 =item modify_charge OPTIONS
2961 Change the properties of a one-time charge. The following properties can
2962 be changed this way:
2963 - pkg: the package description
2964 - classnum: the package class
2965 - additional: arrayref of additional invoice details to add to this package
2967 and, I<if the charge has not yet been billed>:
2968 - start_date: the date when it will be billed
2969 - amount: the setup fee to be charged
2970 - quantity: the multiplier for the setup fee
2971 - separate_bill: whether to put the charge on a separate invoice
2973 If you pass 'adjust_commission' => 1, and the classnum changes, and there are
2974 commission credits linked to this charge, they will be recalculated.
2981 my $part_pkg = $self->part_pkg;
2982 my $pkgnum = $self->pkgnum;
2985 my $oldAutoCommit = $FS::UID::AutoCommit;
2986 local $FS::UID::AutoCommit = 0;
2988 return "Can't use modify_charge except on one-time charges"
2989 unless $part_pkg->freq eq '0';
2991 if ( length($opt{'pkg'}) and $part_pkg->pkg ne $opt{'pkg'} ) {
2992 $part_pkg->set('pkg', $opt{'pkg'});
2995 my %pkg_opt = $part_pkg->options;
2996 my $pkg_opt_modified = 0;
2998 $opt{'additional'} ||= [];
3001 foreach (grep /^additional/, keys %pkg_opt) {
3002 ($i) = ($_ =~ /^additional_info(\d+)$/);
3003 $old_additional[$i] = $pkg_opt{$_} if $i;
3004 delete $pkg_opt{$_};
3007 for ( $i = 0; exists($opt{'additional'}->[$i]); $i++ ) {
3008 $pkg_opt{ "additional_info$i" } = $opt{'additional'}->[$i];
3009 if (!exists($old_additional[$i])
3010 or $old_additional[$i] ne $opt{'additional'}->[$i])
3012 $pkg_opt_modified = 1;
3015 $pkg_opt_modified = 1 if scalar(@old_additional) != $i;
3016 $pkg_opt{'additional_count'} = $i if $i > 0;
3019 if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} )
3022 $old_classnum = $part_pkg->classnum;
3023 $part_pkg->set('classnum', $opt{'classnum'});
3026 if ( !$self->get('setup') ) {
3027 # not yet billed, so allow amount, setup_cost, quantity, start_date,
3030 if ( exists($opt{'amount'})
3031 and $part_pkg->option('setup_fee') != $opt{'amount'}
3032 and $opt{'amount'} > 0 ) {
3034 $pkg_opt{'setup_fee'} = $opt{'amount'};
3035 $pkg_opt_modified = 1;
3038 if ( exists($opt{'setup_cost'})
3039 and $part_pkg->setup_cost != $opt{'setup_cost'}
3040 and $opt{'setup_cost'} > 0 ) {
3042 $part_pkg->set('setup_cost', $opt{'setup_cost'});
3045 if ( exists($opt{'quantity'})
3046 and $opt{'quantity'} != $self->quantity
3047 and $opt{'quantity'} > 0 ) {
3049 $self->set('quantity', $opt{'quantity'});
3052 if ( exists($opt{'start_date'})
3053 and $opt{'start_date'} != $self->start_date ) {
3055 $self->set('start_date', $opt{'start_date'});
3058 if ( exists($opt{'separate_bill'})
3059 and $opt{'separate_bill'} ne $self->separate_bill ) {
3061 $self->set('separate_bill', $opt{'separate_bill'});
3065 } # else simply ignore them; the UI shouldn't allow editing the fields
3068 if ( exists($opt{'taxclass'})
3069 and $part_pkg->taxclass ne $opt{'taxclass'}) {
3071 $part_pkg->set('taxclass', $opt{'taxclass'});
3075 if ( $part_pkg->modified or $pkg_opt_modified ) {
3076 # can we safely modify the package def?
3077 # Yes, if it's not available for purchase, and this is the only instance
3079 if ( $part_pkg->disabled
3080 and FS::cust_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 1
3081 and FS::quotation_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 0
3083 $error = $part_pkg->replace( options => \%pkg_opt );
3086 $part_pkg = $part_pkg->clone;
3087 $part_pkg->set('disabled' => 'Y');
3088 $error = $part_pkg->insert( options => \%pkg_opt );
3089 # and associate this as yet-unbilled package to the new package def
3090 $self->set('pkgpart' => $part_pkg->pkgpart);
3093 $dbh->rollback if $oldAutoCommit;
3098 if ($self->modified) { # for quantity or start_date change, or if we had
3099 # to clone the existing package def
3100 my $error = $self->replace;
3101 return $error if $error;
3103 if (defined $old_classnum) {
3104 # fix invoice grouping records
3105 my $old_catname = $old_classnum
3106 ? FS::pkg_class->by_key($old_classnum)->categoryname
3108 my $new_catname = $opt{'classnum'}
3109 ? $part_pkg->pkg_class->categoryname
3111 if ( $old_catname ne $new_catname ) {
3112 foreach my $cust_bill_pkg ($self->cust_bill_pkg) {
3113 # (there should only be one...)
3114 my @display = qsearch( 'cust_bill_pkg_display', {
3115 'billpkgnum' => $cust_bill_pkg->billpkgnum,
3116 'section' => $old_catname,
3118 foreach (@display) {
3119 $_->set('section', $new_catname);
3120 $error = $_->replace;
3122 $dbh->rollback if $oldAutoCommit;
3126 } # foreach $cust_bill_pkg
3129 if ( $opt{'adjust_commission'} ) {
3130 # fix commission credits...tricky.
3131 foreach my $cust_event ($self->cust_event) {
3132 my $part_event = $cust_event->part_event;
3133 foreach my $table (qw(sales agent)) {
3135 "FS::part_event::Action::Mixin::credit_${table}_pkg_class";
3136 my $credit = qsearchs('cust_credit', {
3137 'eventnum' => $cust_event->eventnum,
3139 if ( $part_event->isa($class) ) {
3140 # Yes, this results in current commission rates being applied
3141 # retroactively to a one-time charge. For accounting purposes
3142 # there ought to be some kind of time limit on doing this.
3143 my $amount = $part_event->_calc_credit($self);
3144 if ( $credit and $credit->amount ne $amount ) {
3145 # Void the old credit.
3146 $error = $credit->void('Package class changed');
3148 $dbh->rollback if $oldAutoCommit;
3149 return "$error (adjusting commission credit)";
3152 # redo the event action to recreate the credit.
3154 eval { $part_event->do_action( $self, $cust_event ) };
3156 $dbh->rollback if $oldAutoCommit;
3159 } # if $part_event->isa($class)
3161 } # foreach $cust_event
3162 } # if $opt{'adjust_commission'}
3163 } # if defined $old_classnum
3165 $dbh->commit if $oldAutoCommit;
3169 sub process_bulk_cust_pkg {
3172 warn Dumper($param) if $DEBUG;
3174 my $old_part_pkg = qsearchs('part_pkg',
3175 { pkgpart => $param->{'old_pkgpart'} });
3176 my $new_part_pkg = qsearchs('part_pkg',
3177 { pkgpart => $param->{'new_pkgpart'} });
3178 die "Must select a new package type\n" unless $new_part_pkg;
3179 #my $keep_dates = $param->{'keep_dates'} || 0;
3180 my $keep_dates = 1; # there is no good reason to turn this off
3182 my $oldAutoCommit = $FS::UID::AutoCommit;
3183 local $FS::UID::AutoCommit = 0;
3186 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
3189 foreach my $old_cust_pkg ( @cust_pkgs ) {
3191 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
3192 if ( $old_cust_pkg->getfield('cancel') ) {
3193 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
3194 $old_cust_pkg->pkgnum."\n"
3198 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
3200 my $error = $old_cust_pkg->change(
3201 'pkgpart' => $param->{'new_pkgpart'},
3202 'keep_dates' => $keep_dates
3204 if ( !ref($error) ) { # change returns the cust_pkg on success
3206 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
3209 $dbh->commit if $oldAutoCommit;
3215 Returns the last bill date, or if there is no last bill date, the setup date.
3216 Useful for billing metered services.
3222 return $self->setfield('last_bill', $_[0]) if @_;
3223 return $self->getfield('last_bill') if $self->getfield('last_bill');
3224 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
3225 'edate' => $self->bill, } );
3226 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
3229 =item last_cust_pkg_reason ACTION
3231 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
3232 Returns false if there is no reason or the package is not currenly ACTION'd
3233 ACTION is one of adjourn, susp, cancel, or expire.
3237 sub last_cust_pkg_reason {
3238 my ( $self, $action ) = ( shift, shift );
3239 my $date = $self->get($action);
3241 'table' => 'cust_pkg_reason',
3242 'hashref' => { 'pkgnum' => $self->pkgnum,
3243 'action' => substr(uc($action), 0, 1),
3246 'order_by' => 'ORDER BY num DESC LIMIT 1',
3250 =item last_reason ACTION
3252 Returns the most recent ACTION FS::reason associated with the package.
3253 Returns false if there is no reason or the package is not currenly ACTION'd
3254 ACTION is one of adjourn, susp, cancel, or expire.
3259 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
3260 $cust_pkg_reason->reason
3261 if $cust_pkg_reason;
3266 Returns the definition for this billing item, as an FS::part_pkg object (see
3273 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
3274 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
3275 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
3280 Returns the cancelled package this package was changed from, if any.
3286 return '' unless $self->change_pkgnum;
3287 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
3290 =item change_cust_main
3292 Returns the customter this package was detached to, if any.
3296 sub change_cust_main {
3298 return '' unless $self->change_custnum;
3299 qsearchs('cust_main', { 'custnum' => $self->change_custnum } );
3304 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
3311 $self->part_pkg->calc_setup($self, @_);
3316 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
3323 $self->part_pkg->calc_recur($self, @_);
3328 Calls the I<base_setup> of the FS::part_pkg object associated with this billing
3335 $self->part_pkg->base_setup($self, @_);
3340 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
3347 $self->part_pkg->base_recur($self, @_);
3352 Calls the I<calc_remain> of the FS::part_pkg object associated with this
3359 $self->part_pkg->calc_remain($self, @_);
3364 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
3371 $self->part_pkg->calc_cancel($self, @_);
3376 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
3382 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
3385 =item cust_pkg_detail [ DETAILTYPE ]
3387 Returns any customer package details for this package (see
3388 L<FS::cust_pkg_detail>).
3390 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
3394 sub cust_pkg_detail {
3396 my %hash = ( 'pkgnum' => $self->pkgnum );
3397 $hash{detailtype} = shift if @_;
3399 'table' => 'cust_pkg_detail',
3400 'hashref' => \%hash,
3401 'order_by' => 'ORDER BY weight, pkgdetailnum',
3405 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
3407 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
3409 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
3411 If there is an error, returns the error, otherwise returns false.
3415 sub set_cust_pkg_detail {
3416 my( $self, $detailtype, @details ) = @_;
3418 my $oldAutoCommit = $FS::UID::AutoCommit;
3419 local $FS::UID::AutoCommit = 0;
3422 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
3423 my $error = $current->delete;
3425 $dbh->rollback if $oldAutoCommit;
3426 return "error removing old detail: $error";
3430 foreach my $detail ( @details ) {
3431 my $cust_pkg_detail = new FS::cust_pkg_detail {
3432 'pkgnum' => $self->pkgnum,
3433 'detailtype' => $detailtype,
3434 'detail' => $detail,
3436 my $error = $cust_pkg_detail->insert;
3438 $dbh->rollback if $oldAutoCommit;
3439 return "error adding new detail: $error";
3444 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3451 Returns the customer billing events (see L<FS::cust_event>) for this invoice.
3455 #false laziness w/cust_bill.pm
3459 'table' => 'cust_event',
3460 'addl_from' => 'JOIN part_event USING ( eventpart )',
3461 'hashref' => { 'tablenum' => $self->pkgnum },
3462 'extra_sql' => " AND eventtable = 'cust_pkg' ",
3466 =item num_cust_event
3468 Returns the number of customer billing events (see L<FS::cust_event>) for this package.
3472 #false laziness w/cust_bill.pm
3473 sub num_cust_event {
3475 my $sql = "SELECT COUNT(*) ". $self->_from_cust_event_where;
3476 $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref->[0];
3479 =item exists_cust_event
3481 Returns true if there are customer billing events (see L<FS::cust_event>) for this package. More efficient than using num_cust_event.
3485 sub exists_cust_event {
3487 my $sql = "SELECT 1 ". $self->_from_cust_event_where. " LIMIT 1";
3488 my $row = $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref;
3489 $row ? $row->[0] : '';
3492 sub _from_cust_event_where {
3494 " FROM cust_event JOIN part_event USING ( eventpart ) ".
3495 " WHERE tablenum = ? AND eventtable = 'cust_pkg' ";
3499 my( $self, $sql, @args ) = @_;
3500 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
3501 $sth->execute(@args) or die $sth->errstr. " executing $sql";
3505 =item part_pkg_currency_option OPTIONNAME
3507 Returns a two item list consisting of the currency of this customer, if any,
3508 and a value for the provided option. If the customer has a currency, the value
3509 is the option value the given name and the currency (see
3510 L<FS::part_pkg_currency>). Otherwise, if the customer has no currency, is the
3511 regular option value for the given name (see L<FS::part_pkg_option>).
3515 sub part_pkg_currency_option {
3516 my( $self, $optionname ) = @_;
3517 my $part_pkg = $self->part_pkg;
3518 if ( my $currency = $self->cust_main->currency ) {
3519 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
3521 ('', $part_pkg->option($optionname) );
3525 =item cust_svc [ SVCPART ] (old, deprecated usage)
3527 =item cust_svc [ OPTION => VALUE ... ] (current usage)
3529 =item cust_svc_unsorted [ OPTION => VALUE ... ]
3531 Returns the services for this package, as FS::cust_svc objects (see
3532 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
3533 spcififed, returns only the matching services.
3535 As an optimization, use the cust_svc_unsorted version if you are not displaying
3542 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
3543 $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref(@_) );
3546 sub cust_svc_unsorted {
3548 @{ $self->cust_svc_unsorted_arrayref(@_) };
3551 sub cust_svc_unsorted_arrayref {
3554 return [] unless $self->num_cust_svc(@_);
3557 if ( @_ && $_[0] =~ /^\d+/ ) {
3558 $opt{svcpart} = shift;
3559 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
3566 'select' => 'cust_svc.*, part_svc.*',
3567 'table' => 'cust_svc',
3568 'hashref' => { 'pkgnum' => $self->pkgnum },
3569 'addl_from' => 'LEFT JOIN part_svc USING ( svcpart )',
3571 $search{hashref}->{svcpart} = $opt{svcpart}
3573 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{svcdb} )
3576 [ qsearch(\%search) ];
3580 =item overlimit [ SVCPART ]
3582 Returns the services for this package which have exceeded their
3583 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
3584 is specified, return only the matching services.
3590 return () unless $self->num_cust_svc(@_);
3591 grep { $_->overlimit } $self->cust_svc(@_);
3594 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3596 Returns historical services for this package created before END TIMESTAMP and
3597 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
3598 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
3599 I<pkg_svc.hidden> flag will be omitted.
3605 warn "$me _h_cust_svc called on $self\n"
3608 my ($end, $start, $mode) = @_;
3610 local($FS::Record::qsearch_qualify_columns) = 0;
3612 my @cust_svc = $self->_sort_cust_svc(
3613 [ qsearch( 'h_cust_svc',
3614 { 'pkgnum' => $self->pkgnum, },
3615 FS::h_cust_svc->sql_h_search(@_),
3619 if ( defined($mode) && $mode eq 'I' ) {
3620 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
3621 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
3627 sub _sort_cust_svc {
3628 my( $self, $arrayref ) = @_;
3631 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
3633 my %pkg_svc = map { $_->svcpart => $_ }
3634 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
3639 my $pkg_svc = $pkg_svc{ $_->svcpart } || '';
3641 $pkg_svc ? $pkg_svc->primary_svc : '',
3642 $pkg_svc ? $pkg_svc->quantity : 0,
3649 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
3651 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
3653 Returns the number of services for this package. Available options are svcpart
3654 and svcdb. If either is spcififed, returns only the matching services.
3661 return $self->{'_num_cust_svc'}
3663 && exists($self->{'_num_cust_svc'})
3664 && $self->{'_num_cust_svc'} =~ /\d/;
3666 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
3670 if ( @_ && $_[0] =~ /^\d+/ ) {
3671 $opt{svcpart} = shift;
3672 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
3678 my $select = 'SELECT COUNT(*) FROM cust_svc ';
3679 my $where = ' WHERE pkgnum = ? ';
3680 my @param = ($self->pkgnum);
3682 if ( $opt{'svcpart'} ) {
3683 $where .= ' AND svcpart = ? ';
3684 push @param, $opt{'svcpart'};
3686 if ( $opt{'svcdb'} ) {
3687 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
3688 $where .= ' AND svcdb = ? ';
3689 push @param, $opt{'svcdb'};
3692 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
3693 $sth->execute(@param) or die $sth->errstr;
3694 $sth->fetchrow_arrayref->[0];
3697 =item available_part_svc
3699 Returns a list of FS::part_svc objects representing services included in this
3700 package but not yet provisioned. Each FS::part_svc object also has an extra
3701 field, I<num_avail>, which specifies the number of available services.
3703 Accepts option I<provision_hold>; if true, only returns part_svc for which the
3704 associated pkg_svc has the provision_hold flag set.
3708 sub available_part_svc {
3712 my $pkg_quantity = $self->quantity || 1;
3714 grep { $_->num_avail > 0 }
3716 my $part_svc = $_->part_svc;
3717 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
3718 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
3720 # more evil encapsulation breakage
3721 if ($part_svc->{'Hash'}{'num_avail'} > 0) {
3722 my @exports = $part_svc->part_export_did;
3723 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
3728 grep { $opt{'provision_hold'} ? $_->provision_hold : 1 }
3729 $self->part_pkg->pkg_svc;
3732 =item part_svc [ OPTION => VALUE ... ]
3734 Returns a list of FS::part_svc objects representing provisioned and available
3735 services included in this package. Each FS::part_svc object also has the
3736 following extra fields:
3750 (services) - array reference containing the provisioned services, as cust_svc objects
3754 Accepts two options:
3758 =item summarize_size
3760 If true, will omit the extra cust_pkg_svc option for objects where num_cust_svc
3761 is this size or greater.
3763 =item hide_discontinued
3765 If true, will omit looking for services that are no longer avaialble in the
3773 #label -> ($cust_svc->label)[1]
3779 my $pkg_quantity = $self->quantity || 1;
3781 #XXX some sort of sort order besides numeric by svcpart...
3782 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
3784 my $part_svc = $pkg_svc->part_svc;
3785 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3786 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
3787 $part_svc->{'Hash'}{'num_avail'} =
3788 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
3789 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3790 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
3791 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
3792 && $num_cust_svc >= $opt{summarize_size};
3793 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
3795 } $self->part_pkg->pkg_svc;
3797 unless ( $opt{hide_discontinued} ) {
3799 push @part_svc, map {
3801 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3802 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
3803 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
3804 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3805 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
3807 } $self->extra_part_svc;
3814 =item extra_part_svc
3816 Returns a list of FS::part_svc objects corresponding to services in this
3817 package which are still provisioned but not (any longer) available in the
3822 sub extra_part_svc {
3825 my $pkgnum = $self->pkgnum;
3826 #my $pkgpart = $self->pkgpart;
3829 # 'table' => 'part_svc',
3832 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
3833 # WHERE pkg_svc.svcpart = part_svc.svcpart
3834 # AND pkg_svc.pkgpart = ?
3837 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
3838 # LEFT JOIN cust_pkg USING ( pkgnum )
3839 # WHERE cust_svc.svcpart = part_svc.svcpart
3842 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
3845 #seems to benchmark slightly faster... (or did?)
3847 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
3848 my $pkgparts = join(',', @pkgparts);
3851 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
3852 #MySQL doesn't grok DISINCT ON
3853 'select' => 'DISTINCT part_svc.*',
3854 'table' => 'part_svc',
3856 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
3857 AND pkg_svc.pkgpart IN ($pkgparts)
3860 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
3861 LEFT JOIN cust_pkg USING ( pkgnum )
3864 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
3865 'extra_param' => [ [$self->pkgnum=>'int'] ],
3871 Returns a short status string for this package, currently:
3877 =item not yet billed
3879 =item one-time charge
3894 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
3896 return 'cancelled' if $self->get('cancel');
3897 return 'on hold' if $self->susp && ! $self->setup;
3898 return 'suspended' if $self->susp;
3899 return 'not yet billed' unless $self->setup;
3900 return 'one-time charge' if $freq =~ /^(0|$)/;
3904 =item ucfirst_status
3906 Returns the status with the first character capitalized.
3910 sub ucfirst_status {
3911 ucfirst(shift->status);
3916 Class method that returns the list of possible status strings for packages
3917 (see L<the status method|/status>). For example:
3919 @statuses = FS::cust_pkg->statuses();
3923 tie my %statuscolor, 'Tie::IxHash',
3924 'on hold' => 'FF00F5', #brighter purple!
3925 'not yet billed' => '009999', #teal? cyan?
3926 'one-time charge' => '0000CC', #blue #'000000',
3927 'active' => '00CC00',
3928 'suspended' => 'FF9900',
3929 'cancelled' => 'FF0000',
3933 my $self = shift; #could be class...
3934 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
3935 # # mayble split btw one-time vs. recur
3946 Returns a hex triplet color string for this package's status.
3952 $statuscolor{$self->status};
3955 =item is_status_delay_cancel
3957 Returns true if part_pkg has option delay_cancel,
3958 cust_pkg status is 'suspended' and expire is set
3959 to cancel package within the next day (or however
3960 many days are set in global config part_pkg-delay_cancel-days.
3962 Accepts option I<part_pkg-delay_cancel-days> which should be
3963 the value of the config setting, to avoid looking it up again.
3965 This is not a real status, this only meant for hacking display
3966 values, because otherwise treating the package as suspended is
3967 really the whole point of the delay_cancel option.
3971 sub is_status_delay_cancel {
3972 my ($self,%opt) = @_;
3973 if ( $self->main_pkgnum and $self->pkglinknum ) {
3974 return $self->main_pkg->is_status_delay_cancel;
3976 return 0 unless $self->part_pkg->option('delay_cancel',1);
3977 return 0 unless $self->status eq 'suspended';
3978 return 0 unless $self->expire;
3979 my $expdays = $opt{'part_pkg-delay_cancel-days'};
3981 my $conf = new FS::Conf;
3982 $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
3984 my $expsecs = 60*60*24*$expdays;
3985 return 0 unless $self->expire < time + $expsecs;
3991 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
3992 "pkg - comment" depending on user preference).
3998 my $label = $self->part_pkg->pkg_comment( cust_pkg=>$self, nopkgpart=>1 );
3999 $label = $self->pkgnum. ": $label"
4000 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
4004 =item pkg_label_long
4006 Returns a long label for this package, adding the primary service's label to
4011 sub pkg_label_long {
4013 my $label = $self->pkg_label;
4014 my $cust_svc = $self->primary_cust_svc;
4015 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
4021 Returns a customer-localized label for this package.
4027 $self->part_pkg->pkg_locale( $self->cust_main->locale );
4030 =item primary_cust_svc
4032 Returns a primary service (as FS::cust_svc object) if one can be identified.
4036 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
4038 sub primary_cust_svc {
4041 my @cust_svc = $self->cust_svc;
4043 return '' unless @cust_svc; #no serivces - irrelevant then
4045 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
4047 # primary service as specified in the package definition
4048 # or exactly one service definition with quantity one
4049 my $svcpart = $self->part_pkg->svcpart;
4050 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
4051 return $cust_svc[0] if scalar(@cust_svc) == 1;
4053 #couldn't identify one thing..
4059 Returns a list of lists, calling the label method for all services
4060 (see L<FS::cust_svc>) of this billing item.
4066 map { [ $_->label ] } $self->cust_svc;
4069 =item h_labels END_TIMESTAMP [, START_TIMESTAMP [, MODE [, LOCALE ] ] ]
4071 Like the labels method, but returns historical information on services that
4072 were active as of END_TIMESTAMP and (optionally) not cancelled before
4073 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
4074 I<pkg_svc.hidden> flag will be omitted.
4076 If LOCALE is passed, service definition names will be localized.
4078 Returns a list of lists, calling the label method for all (historical)
4079 services (see L<FS::h_cust_svc>) of this billing item.
4085 my ($end, $start, $mode, $locale) = @_;
4086 warn "$me h_labels\n"
4088 map { [ $_->label($end, $start, $locale) ] }
4089 $self->h_cust_svc($end, $start, $mode);
4094 Like labels, except returns a simple flat list, and shortens long
4095 (currently >5 or the cust_bill-max_same_services configuration value) lists of
4096 identical services to one line that lists the service label and the number of
4097 individual services rather than individual items.
4102 shift->_labels_short( 'labels' ); # 'labels' takes no further arguments
4105 =item h_labels_short END_TIMESTAMP [, START_TIMESTAMP [, MODE [, LOCALE ] ] ]
4107 Like h_labels, except returns a simple flat list, and shortens long
4108 (currently >5 or the cust_bill-max_same_services configuration value) lists
4109 of identical services to one line that lists the service label and the
4110 number of individual services rather than individual items.
4114 sub h_labels_short {
4115 shift->_labels_short( 'h_labels', @_ );
4118 # takes a method name ('labels' or 'h_labels') and all its arguments;
4119 # maybe should be "shorten($self->h_labels( ... ) )"
4122 my( $self, $method ) = ( shift, shift );
4124 warn "$me _labels_short called on $self with $method method\n"
4127 my $conf = new FS::Conf;
4128 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
4130 warn "$me _labels_short populating \%labels\n"
4134 #tie %labels, 'Tie::IxHash';
4135 push @{ $labels{$_->[0]} }, $_->[1]
4136 foreach $self->$method(@_);
4138 warn "$me _labels_short populating \@labels\n"
4142 foreach my $label ( keys %labels ) {
4144 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
4145 my $num = scalar(@values);
4146 warn "$me _labels_short $num items for $label\n"
4149 if ( $num > $max_same_services ) {
4150 warn "$me _labels_short more than $max_same_services, so summarizing\n"
4152 push @labels, "$label ($num)";
4154 if ( $conf->exists('cust_bill-consolidate_services') ) {
4155 warn "$me _labels_short consolidating services\n"
4157 # push @labels, "$label: ". join(', ', @values);
4159 my $detail = "$label: ";
4160 $detail .= shift(@values). ', '
4162 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
4164 push @labels, $detail;
4166 warn "$me _labels_short done consolidating services\n"
4169 warn "$me _labels_short adding service data\n"
4171 push @labels, map { "$label: $_" } @values;
4182 Returns the parent customer object (see L<FS::cust_main>).
4186 Returns the balance for this specific package, when using
4187 experimental package balance.
4193 $self->cust_main->balance_pkgnum( $self->pkgnum );
4196 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
4200 Returns the location object, if any (see L<FS::cust_location>).
4202 =item cust_location_or_main
4204 If this package is associated with a location, returns the locaiton (see
4205 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
4207 =item location_label [ OPTION => VALUE ... ]
4209 Returns the label of the location object (see L<FS::cust_location>).
4213 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
4215 =item tax_locationnum
4217 Returns the foreign key to a L<FS::cust_location> object for calculating
4218 tax on this package, as determined by the C<tax-pkg_address> and
4219 C<tax-ship_address> configuration flags.
4223 sub tax_locationnum {
4225 my $conf = FS::Conf->new;
4226 if ( $conf->exists('tax-pkg_address') ) {
4227 return $self->locationnum;
4229 elsif ( $conf->exists('tax-ship_address') ) {
4230 return $self->cust_main->ship_locationnum;
4233 return $self->cust_main->bill_locationnum;
4239 Returns the L<FS::cust_location> object for tax_locationnum.
4245 my $conf = FS::Conf->new;
4246 if ( $conf->exists('tax-pkg_address') and $self->locationnum ) {
4247 return FS::cust_location->by_key($self->locationnum);
4249 elsif ( $conf->exists('tax-ship_address') ) {
4250 return $self->cust_main->ship_location;
4253 return $self->cust_main->bill_location;
4257 =item seconds_since TIMESTAMP
4259 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
4260 package have been online since TIMESTAMP, according to the session monitor.
4262 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
4263 L<Time::Local> and L<Date::Parse> for conversion functions.
4268 my($self, $since) = @_;
4271 foreach my $cust_svc (
4272 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
4274 $seconds += $cust_svc->seconds_since($since);
4281 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
4283 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
4284 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
4287 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
4288 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
4294 sub seconds_since_sqlradacct {
4295 my($self, $start, $end) = @_;
4299 foreach my $cust_svc (
4301 my $part_svc = $_->part_svc;
4302 $part_svc->svcdb eq 'svc_acct'
4303 && scalar($part_svc->part_export_usage);
4306 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
4313 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
4315 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
4316 in this package for sessions ending between TIMESTAMP_START (inclusive) and
4320 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
4321 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
4326 sub attribute_since_sqlradacct {
4327 my($self, $start, $end, $attrib) = @_;
4331 foreach my $cust_svc (
4333 my $part_svc = $_->part_svc;
4334 scalar($part_svc->part_export_usage);
4337 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
4349 my( $self, $value ) = @_;
4350 if ( defined($value) ) {
4351 $self->setfield('quantity', $value);
4353 $self->getfield('quantity') || 1;
4356 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
4358 Transfers as many services as possible from this package to another package.
4360 The destination package can be specified by pkgnum by passing an FS::cust_pkg
4361 object. The destination package must already exist.
4363 Services are moved only if the destination allows services with the correct
4364 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
4365 this option with caution! No provision is made for export differences
4366 between the old and new service definitions. Probably only should be used
4367 when your exports for all service definitions of a given svcdb are identical.
4368 (attempt a transfer without it first, to move all possible svcpart-matching
4371 Any services that can't be moved remain in the original package.
4373 Returns an error, if there is one; otherwise, returns the number of services
4374 that couldn't be moved.
4379 my ($self, $dest_pkgnum, %opt) = @_;
4385 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
4386 $dest = $dest_pkgnum;
4387 $dest_pkgnum = $dest->pkgnum;
4389 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
4392 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
4394 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
4395 $target{$pkg_svc->svcpart} = $pkg_svc->quantity * ( $dest->quantity || 1 );
4398 foreach my $cust_svc ($dest->cust_svc) {
4399 $target{$cust_svc->svcpart}--;
4402 my %svcpart2svcparts = ();
4403 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
4404 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
4405 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
4406 next if exists $svcpart2svcparts{$svcpart};
4407 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
4408 $svcpart2svcparts{$svcpart} = [
4410 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
4412 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
4413 'svcpart' => $_ } );
4415 $pkg_svc ? $pkg_svc->primary_svc : '',
4416 $pkg_svc ? $pkg_svc->quantity : 0,
4420 grep { $_ != $svcpart }
4422 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
4424 warn "alternates for svcpart $svcpart: ".
4425 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
4431 foreach my $cust_svc ($self->cust_svc) {
4432 my $svcnum = $cust_svc->svcnum;
4433 if($target{$cust_svc->svcpart} > 0
4434 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
4435 $target{$cust_svc->svcpart}--;
4436 my $new = new FS::cust_svc { $cust_svc->hash };
4437 $new->pkgnum($dest_pkgnum);
4438 $error = $new->replace($cust_svc);
4439 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
4441 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
4442 warn "alternates to consider: ".
4443 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
4445 my @alternate = grep {
4446 warn "considering alternate svcpart $_: ".
4447 "$target{$_} available in new package\n"
4450 } @{$svcpart2svcparts{$cust_svc->svcpart}};
4452 warn "alternate(s) found\n" if $DEBUG;
4453 my $change_svcpart = $alternate[0];
4454 $target{$change_svcpart}--;
4455 my $new = new FS::cust_svc { $cust_svc->hash };
4456 $new->svcpart($change_svcpart);
4457 $new->pkgnum($dest_pkgnum);
4458 $error = $new->replace($cust_svc);
4466 my @label = $cust_svc->label;
4467 return "$label[0] $label[1]: $error";
4473 =item grab_svcnums SVCNUM, SVCNUM ...
4475 Change the pkgnum for the provided services to this packages. If there is an
4476 error, returns the error, otherwise returns false.
4484 my $oldAutoCommit = $FS::UID::AutoCommit;
4485 local $FS::UID::AutoCommit = 0;
4488 foreach my $svcnum (@svcnum) {
4489 my $cust_svc = qsearchs('cust_svc', { svcnum=>$svcnum } ) or do {
4490 $dbh->rollback if $oldAutoCommit;
4491 return "unknown svcnum $svcnum";
4493 $cust_svc->pkgnum( $self->pkgnum );
4494 my $error = $cust_svc->replace;
4496 $dbh->rollback if $oldAutoCommit;
4501 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4508 This method is deprecated. See the I<depend_jobnum> option to the insert and
4509 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
4513 #looks like this is still used by the order_pkg and change_pkg methods in
4514 # ClientAPI/MyAccount, need to look into those before removing
4518 my $oldAutoCommit = $FS::UID::AutoCommit;
4519 local $FS::UID::AutoCommit = 0;
4522 foreach my $cust_svc ( $self->cust_svc ) {
4523 #false laziness w/svc_Common::insert
4524 my $svc_x = $cust_svc->svc_x;
4525 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
4526 my $error = $part_export->export_insert($svc_x);
4528 $dbh->rollback if $oldAutoCommit;
4534 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4539 =item export_pkg_change OLD_CUST_PKG
4541 Calls the "pkg_change" export action for all services attached to this package.
4545 sub export_pkg_change {
4546 my( $self, $old ) = ( shift, shift );
4548 my $oldAutoCommit = $FS::UID::AutoCommit;
4549 local $FS::UID::AutoCommit = 0;
4552 foreach my $svc_x ( map $_->svc_x, $self->cust_svc ) {
4553 my $error = $svc_x->export('pkg_change', $self, $old);
4555 $dbh->rollback if $oldAutoCommit;
4560 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4567 Associates this package with a (suspension or cancellation) reason (see
4568 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
4571 Available options are:
4577 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.
4581 the access_user (see L<FS::access_user>) providing the reason
4589 the action (cancel, susp, adjourn, expire) associated with the reason
4593 If there is an error, returns the error, otherwise returns false.
4598 my ($self, %options) = @_;
4600 my $otaker = $options{reason_otaker} ||
4601 $FS::CurrentUser::CurrentUser->username;
4604 if ( $options{'reason'} =~ /^(\d+)$/ ) {
4608 } elsif ( ref($options{'reason'}) ) {
4610 return 'Enter a new reason (or select an existing one)'
4611 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
4613 my $reason = new FS::reason({
4614 'reason_type' => $options{'reason'}->{'typenum'},
4615 'reason' => $options{'reason'}->{'reason'},
4617 my $error = $reason->insert;
4618 return $error if $error;
4620 $reasonnum = $reason->reasonnum;
4623 return "Unparseable reason: ". $options{'reason'};
4626 my $cust_pkg_reason =
4627 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
4628 'reasonnum' => $reasonnum,
4629 'otaker' => $otaker,
4630 'action' => substr(uc($options{'action'}),0,1),
4631 'date' => $options{'date'}
4636 $cust_pkg_reason->insert;
4639 =item insert_discount
4641 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
4642 inserting a new discount on the fly (see L<FS::discount>).
4644 This will look at the cust_pkg for a pseudo-field named "setup_discountnum",
4645 and if present, will create a setup discount. If the discountnum is -1,
4646 a new discount definition will be inserted using the value in
4647 "setup_discountnum_amount" or "setup_discountnum_percent". Likewise for recur.
4649 If there is an error, returns the error, otherwise returns false.
4653 sub insert_discount {
4654 #my ($self, %options) = @_;
4657 foreach my $x (qw(setup recur)) {
4658 if ( my $discountnum = $self->get("${x}_discountnum") ) {
4659 my $cust_pkg_discount = FS::cust_pkg_discount->new( {
4660 'pkgnum' => $self->pkgnum,
4661 'discountnum' => $discountnum,
4664 'end_date' => '', #XXX
4665 #for the create a new discount case
4666 'amount' => $self->get("${x}_discountnum_amount"),
4667 'percent' => $self->get("${x}_discountnum_percent"),
4668 'months' => $self->get("${x}_discountnum_months"),
4670 if ( $x eq 'setup' ) {
4671 $cust_pkg_discount->setup('Y');
4672 $cust_pkg_discount->months('');
4674 my $error = $cust_pkg_discount->insert;
4675 return $error if $error;
4682 =item set_usage USAGE_VALUE_HASHREF
4684 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4685 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4686 upbytes, downbytes, and totalbytes are appropriate keys.
4688 All svc_accts which are part of this package have their values reset.
4693 my ($self, $valueref, %opt) = @_;
4695 #only svc_acct can set_usage for now
4696 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4697 my $svc_x = $cust_svc->svc_x;
4698 $svc_x->set_usage($valueref, %opt)
4699 if $svc_x->can("set_usage");
4703 =item recharge USAGE_VALUE_HASHREF
4705 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4706 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4707 upbytes, downbytes, and totalbytes are appropriate keys.
4709 All svc_accts which are part of this package have their values incremented.
4714 my ($self, $valueref) = @_;
4716 #only svc_acct can set_usage for now
4717 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4718 my $svc_x = $cust_svc->svc_x;
4719 $svc_x->recharge($valueref)
4720 if $svc_x->can("recharge");
4724 =item apply_usageprice
4728 sub apply_usageprice {
4731 my $oldAutoCommit = $FS::UID::AutoCommit;
4732 local $FS::UID::AutoCommit = 0;
4737 foreach my $cust_pkg_usageprice ( $self->cust_pkg_usageprice ) {
4738 $error ||= $cust_pkg_usageprice->apply;
4742 $dbh->rollback if $oldAutoCommit;
4743 die "error applying part_pkg_usageprice add-ons, pkgnum ". $self->pkgnum.
4746 $dbh->commit if $oldAutoCommit;
4752 =item cust_pkg_discount
4754 =item cust_pkg_discount_active
4758 sub cust_pkg_discount_active {
4760 grep { $_->status eq 'active' } $self->cust_pkg_discount;
4763 =item cust_pkg_usage
4765 Returns a list of all voice usage counters attached to this package.
4767 =item apply_usage OPTIONS
4769 Takes the following options:
4770 - cdr: a call detail record (L<FS::cdr>)
4771 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
4772 - minutes: the maximum number of minutes to be charged
4774 Finds available usage minutes for a call of this class, and subtracts
4775 up to that many minutes from the usage pool. If the usage pool is empty,
4776 and the C<cdr-minutes_priority> global config option is set, minutes may
4777 be taken from other calls as well. Either way, an allocation record will
4778 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
4779 number of minutes of usage applied to the call.
4784 my ($self, %opt) = @_;
4785 my $cdr = $opt{cdr};
4786 my $rate_detail = $opt{rate_detail};
4787 my $minutes = $opt{minutes};
4788 my $classnum = $rate_detail->classnum;
4789 my $pkgnum = $self->pkgnum;
4790 my $custnum = $self->custnum;
4792 my $oldAutoCommit = $FS::UID::AutoCommit;
4793 local $FS::UID::AutoCommit = 0;
4796 my $order = FS::Conf->new->config('cdr-minutes_priority');
4800 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
4802 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
4804 my @usage_recs = qsearch({
4805 'table' => 'cust_pkg_usage',
4806 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
4807 ' JOIN cust_pkg USING (pkgnum)'.
4808 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
4809 'select' => 'cust_pkg_usage.*',
4810 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
4811 " ( cust_pkg.custnum = $custnum AND ".
4812 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4813 $is_classnum . ' AND '.
4814 " cust_pkg_usage.minutes > 0",
4815 'order_by' => " ORDER BY priority ASC",
4818 my $orig_minutes = $minutes;
4820 while (!$error and $minutes > 0 and @usage_recs) {
4821 my $cust_pkg_usage = shift @usage_recs;
4822 $cust_pkg_usage->select_for_update;
4823 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
4824 pkgusagenum => $cust_pkg_usage->pkgusagenum,
4825 acctid => $cdr->acctid,
4826 minutes => min($cust_pkg_usage->minutes, $minutes),
4828 $cust_pkg_usage->set('minutes',
4829 $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes
4831 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
4832 $minutes -= $cdr_cust_pkg_usage->minutes;
4834 if ( $order and $minutes > 0 and !$error ) {
4835 # then try to steal minutes from another call
4837 'table' => 'cdr_cust_pkg_usage',
4838 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
4839 ' JOIN part_pkg_usage USING (pkgusagepart)'.
4840 ' JOIN cust_pkg USING (pkgnum)'.
4841 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
4842 ' JOIN cdr USING (acctid)',
4843 'select' => 'cdr_cust_pkg_usage.*',
4844 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
4845 " ( cust_pkg.pkgnum = $pkgnum OR ".
4846 " ( cust_pkg.custnum = $custnum AND ".
4847 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4848 " part_pkg_usage_class.classnum = $classnum",
4849 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
4851 if ( $order eq 'time' ) {
4852 # find CDRs that are using minutes, but have a later startdate
4854 my $startdate = $cdr->startdate;
4855 if ($startdate !~ /^\d+$/) {
4856 die "bad cdr startdate '$startdate'";
4858 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
4859 # minimize needless reshuffling
4860 $search{'order_by'} .= ', cdr.startdate DESC';
4862 # XXX may not work correctly with rate_time schedules. Could
4863 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
4865 $search{'addl_from'} .=
4866 ' JOIN rate_detail'.
4867 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
4868 if ( $order eq 'rate_high' ) {
4869 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
4870 $rate_detail->min_charge;
4871 $search{'order_by'} .= ', rate_detail.min_charge ASC';
4872 } elsif ( $order eq 'rate_low' ) {
4873 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
4874 $rate_detail->min_charge;
4875 $search{'order_by'} .= ', rate_detail.min_charge DESC';
4877 # this should really never happen
4878 die "invalid cdr-minutes_priority value '$order'\n";
4881 my @cdr_usage_recs = qsearch(\%search);
4883 while (!$error and @cdr_usage_recs and $minutes > 0) {
4884 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
4885 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
4886 my $old_cdr = $cdr_cust_pkg_usage->cdr;
4887 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
4888 $cdr_cust_pkg_usage->select_for_update;
4889 $old_cdr->select_for_update;
4890 $cust_pkg_usage->select_for_update;
4891 # in case someone else stole the usage from this CDR
4892 # while waiting for the lock...
4893 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
4894 # steal the usage allocation and flag the old CDR for reprocessing
4895 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
4896 # if the allocation is more minutes than we need, adjust it...
4897 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
4899 $cdr_cust_pkg_usage->set('minutes', $minutes);
4900 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
4901 $error = $cust_pkg_usage->replace;
4903 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
4904 $error ||= $cdr_cust_pkg_usage->replace;
4905 # deduct the stolen minutes
4906 $minutes -= $cdr_cust_pkg_usage->minutes;
4908 # after all minute-stealing is done, reset the affected CDRs
4909 foreach (values %reproc_cdrs) {
4910 $error ||= $_->set_status('');
4911 # XXX or should we just call $cdr->rate right here?
4912 # it's not like we can create a loop this way, since the min_charge
4913 # or call time has to go monotonically in one direction.
4914 # we COULD get some very deep recursions going, though...
4916 } # if $order and $minutes
4919 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
4921 $dbh->commit if $oldAutoCommit;
4922 return $orig_minutes - $minutes;
4926 =item supplemental_pkgs
4928 Returns a list of all packages supplemental to this one.
4932 sub supplemental_pkgs {
4934 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
4939 Returns the package that this one is supplemental to, if any.
4945 if ( $self->main_pkgnum ) {
4946 return FS::cust_pkg->by_key($self->main_pkgnum);
4953 =head1 CLASS METHODS
4959 Returns an SQL expression identifying recurring packages.
4963 sub recurring_sql { "
4964 '0' != ( select freq from part_pkg
4965 where cust_pkg.pkgpart = part_pkg.pkgpart )
4970 Returns an SQL expression identifying one-time packages.
4975 '0' = ( select freq from part_pkg
4976 where cust_pkg.pkgpart = part_pkg.pkgpart )
4981 Returns an SQL expression identifying ordered packages (recurring packages not
4987 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
4992 Returns an SQL expression identifying active packages.
4997 $_[0]->recurring_sql. "
4998 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4999 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
5000 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
5003 =item not_yet_billed_sql
5005 Returns an SQL expression identifying packages which have not yet been billed.
5009 sub not_yet_billed_sql { "
5010 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
5011 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
5012 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
5017 Returns an SQL expression identifying inactive packages (one-time packages
5018 that are otherwise unsuspended/uncancelled).
5022 sub inactive_sql { "
5023 ". $_[0]->onetime_sql(). "
5024 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
5025 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
5026 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
5031 Returns an SQL expression identifying on-hold packages.
5036 #$_[0]->recurring_sql(). ' AND '.
5038 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
5039 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
5040 AND ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
5047 Returns an SQL expression identifying suspended packages.
5051 sub suspended_sql { susp_sql(@_); }
5053 #$_[0]->recurring_sql(). ' AND '.
5055 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
5056 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
5057 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
5064 Returns an SQL exprression identifying cancelled packages.
5068 sub cancelled_sql { cancel_sql(@_); }
5070 #$_[0]->recurring_sql(). ' AND '.
5071 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
5074 =item ncancelled_recurring_sql
5076 Returns an SQL expression identifying un-cancelled, recurring packages.
5080 sub ncancelled_recurring_sql {
5081 $_[0]->recurring_sql().
5082 " AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 ) ";
5087 Returns an SQL expression to give the package status as a string.
5093 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
5094 WHEN ( cust_pkg.susp IS NOT NULL AND cust_pkg.setup IS NULL ) THEN 'on hold'
5095 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
5096 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
5097 WHEN ".onetime_sql()." THEN 'one-time charge'
5104 Returns a list of two package counts. The first is a count of packages
5105 based on the supplied criteria and the second is the count of residential
5106 packages with those same criteria. Criteria are specified as in the search
5112 my ($class, $params) = @_;
5114 my $sql_query = $class->search( $params );
5116 my $count_sql = delete($sql_query->{'count_query'});
5117 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
5118 or die "couldn't parse count_sql";
5120 my $count_sth = dbh->prepare($count_sql)
5121 or die "Error preparing $count_sql: ". dbh->errstr;
5123 or die "Error executing $count_sql: ". $count_sth->errstr;
5124 my $count_arrayref = $count_sth->fetchrow_arrayref;
5126 return ( @$count_arrayref );
5130 =item tax_locationnum_sql
5132 Returns an SQL expression for the tax location for a package, based
5133 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
5137 sub tax_locationnum_sql {
5138 my $conf = FS::Conf->new;
5139 if ( $conf->exists('tax-pkg_address') ) {
5140 'cust_pkg.locationnum';
5142 elsif ( $conf->exists('tax-ship_address') ) {
5143 'cust_main.ship_locationnum';
5146 'cust_main.bill_locationnum';
5152 Returns a list: the first item is an SQL fragment identifying matching
5153 packages/customers via location (taking into account shipping and package
5154 address taxation, if enabled), and subsequent items are the parameters to
5155 substitute for the placeholders in that fragment.
5160 my($class, %opt) = @_;
5161 my $ornull = $opt{'ornull'};
5163 my $conf = new FS::Conf;
5165 # '?' placeholders in _location_sql_where
5166 my $x = $ornull ? 3 : 2;
5177 if ( $conf->exists('tax-ship_address') ) {
5180 ( ( ship_last IS NULL OR ship_last = '' )
5181 AND ". _location_sql_where('cust_main', '', $ornull ). "
5183 OR ( ship_last IS NOT NULL AND ship_last != ''
5184 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
5187 # AND payby != 'COMP'
5189 @main_param = ( @bill_param, @bill_param );
5193 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
5194 @main_param = @bill_param;
5200 if ( $conf->exists('tax-pkg_address') ) {
5202 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
5205 ( cust_pkg.locationnum IS NULL AND $main_where )
5206 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
5209 @param = ( @main_param, @bill_param );
5213 $where = $main_where;
5214 @param = @main_param;
5222 #subroutine, helper for location_sql
5223 sub _location_sql_where {
5225 my $prefix = @_ ? shift : '';
5226 my $ornull = @_ ? shift : '';
5228 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
5230 $ornull = $ornull ? ' OR ? IS NULL ' : '';
5232 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
5233 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
5234 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
5236 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
5238 # ( $table.${prefix}city = ? $or_empty_city $ornull )
5240 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
5241 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
5242 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
5243 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
5244 AND $table.${prefix}country = ?
5249 my( $self, $what ) = @_;
5251 my $what_show_zero = $what. '_show_zero';
5252 length($self->$what_show_zero())
5253 ? ($self->$what_show_zero() eq 'Y')
5254 : $self->part_pkg->$what_show_zero();
5261 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
5263 Bulk cancel + order subroutine. Perhaps slightly deprecated, only used by the
5264 bulk cancel+order in the web UI and nowhere else (edit/process/cust_pkg.cgi)
5266 CUSTNUM is a customer (see L<FS::cust_main>)
5268 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
5269 L<FS::part_pkg>) to order for this customer. Duplicates are of course
5272 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
5273 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
5274 new billing items. An error is returned if this is not possible (see
5275 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
5278 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
5279 newly-created cust_pkg objects.
5281 REFNUM, if specified, will specify the FS::pkg_referral record to be created
5282 and inserted. Multiple FS::pkg_referral records can be created by
5283 setting I<refnum> to an array reference of refnums or a hash reference with
5284 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
5285 record will be created corresponding to cust_main.refnum.
5290 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
5292 my $conf = new FS::Conf;
5294 # Transactionize this whole mess
5295 my $oldAutoCommit = $FS::UID::AutoCommit;
5296 local $FS::UID::AutoCommit = 0;
5300 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
5301 # return "Customer not found: $custnum" unless $cust_main;
5303 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
5306 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
5309 my $change = scalar(@old_cust_pkg) != 0;
5312 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
5314 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
5315 " to pkgpart ". $pkgparts->[0]. "\n"
5318 my $err_or_cust_pkg =
5319 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
5320 'refnum' => $refnum,
5323 unless (ref($err_or_cust_pkg)) {
5324 $dbh->rollback if $oldAutoCommit;
5325 return $err_or_cust_pkg;
5328 push @$return_cust_pkg, $err_or_cust_pkg;
5329 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5334 # Create the new packages.
5335 foreach my $pkgpart (@$pkgparts) {
5337 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
5339 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
5340 pkgpart => $pkgpart,
5344 $error = $cust_pkg->insert( 'change' => $change );
5345 push @$return_cust_pkg, $cust_pkg;
5347 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
5348 my $supp_pkg = FS::cust_pkg->new({
5349 custnum => $custnum,
5350 pkgpart => $link->dst_pkgpart,
5352 main_pkgnum => $cust_pkg->pkgnum,
5355 $error ||= $supp_pkg->insert( 'change' => $change );
5356 push @$return_cust_pkg, $supp_pkg;
5360 $dbh->rollback if $oldAutoCommit;
5365 # $return_cust_pkg now contains refs to all of the newly
5368 # Transfer services and cancel old packages.
5369 foreach my $old_pkg (@old_cust_pkg) {
5371 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
5374 foreach my $new_pkg (@$return_cust_pkg) {
5375 $error = $old_pkg->transfer($new_pkg);
5376 if ($error and $error == 0) {
5377 # $old_pkg->transfer failed.
5378 $dbh->rollback if $oldAutoCommit;
5383 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
5384 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
5385 foreach my $new_pkg (@$return_cust_pkg) {
5386 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
5387 if ($error and $error == 0) {
5388 # $old_pkg->transfer failed.
5389 $dbh->rollback if $oldAutoCommit;
5396 # Transfers were successful, but we went through all of the
5397 # new packages and still had services left on the old package.
5398 # We can't cancel the package under the circumstances, so abort.
5399 $dbh->rollback if $oldAutoCommit;
5400 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
5402 $error = $old_pkg->cancel( quiet=>1, 'no_delay_cancel'=>1 );
5408 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5412 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
5414 A bulk change method to change packages for multiple customers.
5416 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
5417 L<FS::part_pkg>) to order for each customer. Duplicates are of course
5420 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
5421 replace. The services (see L<FS::cust_svc>) are moved to the
5422 new billing items. An error is returned if this is not possible (see
5425 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
5426 newly-created cust_pkg objects.
5431 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
5433 # Transactionize this whole mess
5434 my $oldAutoCommit = $FS::UID::AutoCommit;
5435 local $FS::UID::AutoCommit = 0;
5439 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
5442 while(scalar(@old_cust_pkg)) {
5444 my $custnum = $old_cust_pkg[0]->custnum;
5445 my (@remove) = map { $_->pkgnum }
5446 grep { $_->custnum == $custnum } @old_cust_pkg;
5447 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
5449 my $error = order $custnum, $pkgparts, \@remove, \@return;
5451 push @errors, $error
5453 push @$return_cust_pkg, @return;
5456 if (scalar(@errors)) {
5457 $dbh->rollback if $oldAutoCommit;
5458 return join(' / ', @errors);
5461 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5465 =item forward_emails
5467 Returns a hash of svcnums and corresponding email addresses
5468 for svc_acct services that can be used as source or dest
5469 for svc_forward services provisioned in this package.
5471 Accepts options I<svc_forward> OR I<svcnum> for a svc_forward
5472 service; if included, will ensure the current values of the
5473 specified service are included in the list, even if for some
5474 other reason they wouldn't be. If called as a class method
5475 with a specified service, returns only these current values.
5477 Caution: does not actually check if svc_forward services are
5478 available to be provisioned on this package.
5482 sub forward_emails {
5486 #load optional service, thoroughly validated
5487 die "Use svcnum or svc_forward, not both"
5488 if $opt{'svcnum'} && $opt{'svc_forward'};
5489 my $svc_forward = $opt{'svc_forward'};
5490 $svc_forward ||= qsearchs('svc_forward',{ 'svcnum' => $opt{'svcnum'} })
5492 die "Specified service is not a forward service"
5493 if $svc_forward && (ref($svc_forward) ne 'FS::svc_forward');
5494 die "Specified service not found"
5495 if ($opt{'svcnum'} || $opt{'svc_forward'}) && !$svc_forward;
5499 ## everything below was basically copied from httemplate/edit/svc_forward.cgi
5500 ## with minimal refactoring, not sure why we can't just load all svc_accts for this custnum
5502 #add current values from specified service, if there was one
5504 foreach my $method (qw( srcsvc_acct dstsvc_acct )) {
5505 my $svc_acct = $svc_forward->$method();
5506 $email{$svc_acct->svcnum} = $svc_acct->email if $svc_acct;
5510 if (ref($self) eq 'FS::cust_pkg') {
5512 #and including the rest for this customer
5513 my($u_part_svc,@u_acct_svcparts);
5514 foreach $u_part_svc ( qsearch('part_svc',{'svcdb'=>'svc_acct'}) ) {
5515 push @u_acct_svcparts,$u_part_svc->getfield('svcpart');
5518 my $custnum = $self->getfield('custnum');
5519 foreach my $i_cust_pkg ( qsearch('cust_pkg',{'custnum'=>$custnum}) ) {
5520 my $cust_pkgnum = $i_cust_pkg->getfield('pkgnum');
5521 #now find the corresponding record(s) in cust_svc (for this pkgnum!)
5522 foreach my $acct_svcpart (@u_acct_svcparts) {
5523 foreach my $i_cust_svc (
5524 qsearch( 'cust_svc', { 'pkgnum' => $cust_pkgnum,
5525 'svcpart' => $acct_svcpart } )
5527 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $i_cust_svc->svcnum } );
5528 $email{$svc_acct->svcnum} = $svc_acct->email;
5537 # Used by FS::Upgrade to migrate to a new database.
5538 sub _upgrade_data { # class method
5539 my ($class, %opts) = @_;
5540 $class->_upgrade_otaker(%opts);
5542 # RT#10139, bug resulting in contract_end being set when it shouldn't
5543 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
5544 # RT#10830, bad calculation of prorate date near end of year
5545 # the date range for bill is December 2009, and we move it forward
5546 # one year if it's before the previous bill date (which it should
5548 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
5549 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
5550 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
5551 # RT6628, add order_date to cust_pkg
5552 'update cust_pkg set order_date = (select history_date from h_cust_pkg
5553 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
5554 history_action = \'insert\') where order_date is null',
5556 foreach my $sql (@statements) {
5557 my $sth = dbh->prepare($sql);
5558 $sth->execute or die $sth->errstr;
5561 # RT31194: supplemental package links that are deleted don't clean up
5563 my @pkglinknums = qsearch({
5564 'select' => 'DISTINCT cust_pkg.pkglinknum',
5565 'table' => 'cust_pkg',
5566 'addl_from' => ' LEFT JOIN part_pkg_link USING (pkglinknum) ',
5567 'extra_sql' => ' WHERE cust_pkg.pkglinknum IS NOT NULL
5568 AND part_pkg_link.pkglinknum IS NULL',
5570 foreach (@pkglinknums) {
5571 my $pkglinknum = $_->pkglinknum;
5572 warn "cleaning part_pkg_link #$pkglinknum\n";
5573 my $part_pkg_link = FS::part_pkg_link->new({pkglinknum => $pkglinknum});
5574 my $error = $part_pkg_link->remove_linked;
5575 die $error if $error;
5578 # RT#73607: canceling a package with billing addons sometimes changes its
5580 # Find records where the last replace_new record for the package before it
5581 # was canceled has a different pkgpart from the package itself.
5582 my @cust_pkg = qsearch({
5583 'table' => 'cust_pkg',
5584 'select' => 'cust_pkg.*, h_cust_pkg.pkgpart AS h_pkgpart',
5585 'addl_from' => ' JOIN (
5586 SELECT pkgnum, MAX(historynum) AS historynum FROM h_cust_pkg
5587 WHERE cancel IS NULL
5588 AND history_action = \'replace_new\'
5590 ) AS last_history USING (pkgnum)
5591 JOIN h_cust_pkg USING (historynum)',
5592 'extra_sql' => ' WHERE cust_pkg.cancel is not null
5593 AND cust_pkg.pkgpart != h_cust_pkg.pkgpart'
5595 foreach my $cust_pkg ( @cust_pkg ) {
5596 my $pkgnum = $cust_pkg->pkgnum;
5597 warn "fixing pkgpart on canceled pkg#$pkgnum\n";
5598 $cust_pkg->set('pkgpart', $cust_pkg->h_pkgpart);
5599 my $error = $cust_pkg->replace;
5600 die $error if $error;
5609 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
5611 In sub order, the @pkgparts array (passed by reference) is clobbered.
5613 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
5614 method to pass dates to the recur_prog expression, it should do so.
5616 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
5617 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
5618 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
5619 configuration values. Probably need a subroutine which decides what to do
5620 based on whether or not we've fetched the user yet, rather than a hash. See
5621 FS::UID and the TODO.
5623 Now that things are transactional should the check in the insert method be
5628 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
5629 L<FS::pkg_svc>, schema.html from the base documentation