2 use base qw( FS::cust_pkg::Search FS::cust_pkg::API
3 FS::otaker_Mixin FS::cust_main_Mixin FS::Sales_Mixin
4 FS::contact_Mixin FS::location_Mixin
5 FS::m2m_Common FS::option_Common
10 use Scalar::Util qw( blessed );
11 use List::Util qw(min max);
13 use Time::Local qw( timelocal timelocal_nocheck );
15 use FS::UID qw( dbh driver_name );
16 use FS::Misc qw( send_email );
17 use FS::Record qw( qsearch qsearchs fields );
23 use FS::cust_location;
25 use FS::cust_bill_pkg;
26 use FS::cust_pkg_detail;
27 use FS::cust_pkg_usage;
28 use FS::cdr_cust_pkg_usage;
33 use FS::cust_pkg_reason;
35 use FS::cust_pkg_usageprice;
36 use FS::cust_pkg_discount;
42 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
44 # because they load configuration by setting FS::UID::callback (see TODO)
50 # for sending cancel emails in sub cancel
53 our ($disable_agentcheck, $DEBUG, $me, $import) = (0, 0, '[FS::cust_pkg]', 0);
55 our $upgrade = 0; #go away after setup+start dates cleaned up for old customers
59 my ( $hashref, $cache ) = @_;
60 #if ( $hashref->{'pkgpart'} ) {
61 if ( $hashref->{'pkg'} ) {
62 # #@{ $self->{'_pkgnum'} } = ();
63 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
64 # $self->{'_pkgpart'} = $subcache;
65 # #push @{ $self->{'_pkgnum'} },
66 # FS::part_pkg->new_or_cached($hashref, $subcache);
67 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
69 if ( exists $hashref->{'svcnum'} ) {
70 #@{ $self->{'_pkgnum'} } = ();
71 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
72 $self->{'_svcnum'} = $subcache;
73 #push @{ $self->{'_pkgnum'} },
74 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
80 FS::cust_pkg - Object methods for cust_pkg objects
86 $record = new FS::cust_pkg \%hash;
87 $record = new FS::cust_pkg { 'column' => 'value' };
89 $error = $record->insert;
91 $error = $new_record->replace($old_record);
93 $error = $record->delete;
95 $error = $record->check;
97 $error = $record->cancel;
99 $error = $record->suspend;
101 $error = $record->unsuspend;
103 $part_pkg = $record->part_pkg;
105 @labels = $record->labels;
107 $seconds = $record->seconds_since($timestamp);
109 #bulk cancel+order... perhaps slightly deprecated, only used by the bulk
110 # cancel+order in the web UI and nowhere else (edit/process/cust_pkg.cgi)
111 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
112 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
116 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
117 inherits from FS::Record. The following fields are currently supported:
123 Primary key (assigned automatically for new billing items)
127 Customer (see L<FS::cust_main>)
131 Billing item definition (see L<FS::part_pkg>)
135 Optional link to package location (see L<FS::location>)
139 date package was ordered (also remains same on changes)
151 date (next bill date)
179 order taker (see L<FS::access_user>)
183 If this field is set to 1, disables the automatic
184 unsuspension of this package when using the B<unsuspendauto> config option.
188 If not set, defaults to 1
192 Date of change from previous package
202 =item change_locationnum
210 The pkgnum of the package that this package is supplemental to, if any.
214 The package link (L<FS::part_pkg_link>) that defines this supplemental
215 package, if it is one.
217 =item change_to_pkgnum
219 The pkgnum of the package this one will be "changed to" in the future
220 (on its expiration date).
224 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
225 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
226 L<Time::Local> and L<Date::Parse> for conversion functions.
234 Create a new billing item. To add the item to the database, see L<"insert">.
238 sub table { 'cust_pkg'; }
239 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
240 sub cust_unlinked_msg {
242 "WARNING: can't find cust_main.custnum ". $self->custnum.
243 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
246 =item set_initial_timers
248 If required by the package definition, sets any automatic expire, adjourn,
249 or contract_end timers to some number of months after the start date
250 (or setup date, if the package has already been setup). If the package has
251 a delayed setup fee after a period of "free days", will also set the
252 start date to the end of that period.
254 If the package has an automatic transfer rule (C<change_to_pkgnum>), then
255 this will also order the package and set its start date.
259 sub set_initial_timers {
261 my $part_pkg = $self->part_pkg;
262 my $start = $self->start_date || $self->setup || time;
264 foreach my $action ( qw(expire adjourn contract_end) ) {
265 my $months = $part_pkg->get("${action}_months");
266 if($months and !$self->get($action)) {
267 $self->set($action, $part_pkg->add_freq($start, $months) );
271 # if this package has an expire date and a change_to_pkgpart, set automatic
273 # (but don't call change_later, as that would call $self->replace, and we're
274 # probably in the middle of $self->insert right now)
275 if ( $part_pkg->expire_months and $part_pkg->change_to_pkgpart ) {
276 if ( $self->change_to_pkgnum ) {
277 # this can happen if a package is ordered on hold, scheduled for a
278 # future change _while on hold_, and then released from hold, causing
279 # the automatic transfer to schedule.
281 # what's correct behavior in that case? I think it's to disallow
282 # future-changing an on-hold package that has an automatic transfer.
283 # but if we DO get into this situation, let the manual package change
285 warn "pkgnum ".$self->pkgnum.": manual future package change blocks ".
286 "automatic transfer.\n";
288 my $change_to = FS::cust_pkg->new( {
289 start_date => $self->get('expire'),
290 pkgpart => $part_pkg->change_to_pkgpart,
291 map { $_ => $self->get($_) }
292 qw( custnum locationnum quantity refnum salesnum contract_end )
294 my $error = $change_to->insert;
296 return $error if $error;
297 $self->set('change_to_pkgnum', $change_to->pkgnum);
301 # if this package has "free days" and delayed setup fee, then
302 # set start date that many days in the future.
303 # (this should have been set in the UI, but enforce it here)
304 if ( $part_pkg->option('free_days',1)
305 && $part_pkg->option('delay_setup',1)
308 $self->start_date( $part_pkg->default_start_date );
314 =item insert [ OPTION => VALUE ... ]
316 Adds this billing item to the database ("Orders" the item). If there is an
317 error, returns the error, otherwise returns false.
319 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
320 will be used to look up the package definition and agent restrictions will be
323 If the additional field I<refnum> is defined, an FS::pkg_referral record will
324 be created and inserted. Multiple FS::pkg_referral records can be created by
325 setting I<refnum> to an array reference of refnums or a hash reference with
326 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
327 record will be created corresponding to cust_main.refnum.
329 If the additional field I<cust_pkg_usageprice> is defined, it will be treated
330 as an arrayref of FS::cust_pkg_usageprice objects, which will be inserted.
331 (Note that this field cannot be set with a usual ->cust_pkg_usageprice method.
332 It can be set as part of the hash when creating the object, or with the B<set>
335 The following options are available:
341 If set true, supresses actions that should only be taken for new package
342 orders. (Currently this includes: intro periods when delay_setup is on,
343 auto-adding a 1st start date, auto-adding expiration/adjourn/contract_end dates)
347 cust_pkg_option records will be created
351 a ticket will be added to this customer with this subject
355 an optional queue name for ticket additions
359 Don't check the legality of the package definition. This should be used
360 when performing a package change that doesn't change the pkgpart (i.e.
368 my( $self, %options ) = @_;
370 my $oldAutoCommit = $FS::UID::AutoCommit;
371 local $FS::UID::AutoCommit = 0;
375 $error = $self->check_pkgpart unless $options{'allow_pkgpart'};
377 my $part_pkg = $self->part_pkg;
379 if ( ! $import && ! $options{'change'} ) {
381 # set order date to now
382 $self->order_date(time) unless ($import && $self->order_date);
384 # if the package def says to start only on the first of the month:
385 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
386 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
387 $mon += 1 unless $mday == 1;
388 until ( $mon < 12 ) { $mon -= 12; $year++; }
389 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
392 if ($self->susp eq 'now' or $part_pkg->start_on_hold) {
393 # if the package was ordered on hold:
395 # - don't set the start date (it will be started manually)
396 $self->set('susp', $self->order_date);
397 $self->set('start_date', '');
399 # set expire/adjourn/contract_end timers, and free days, if appropriate
400 # and automatic package transfer, which can fail, so capture the result
401 $error = $self->set_initial_timers;
403 } # else this is a package change, and shouldn't have "new package" behavior
405 $error ||= $self->SUPER::insert($options{options} ? %{$options{options}} : ());
407 $dbh->rollback if $oldAutoCommit;
411 $self->refnum($self->cust_main->refnum) unless $self->refnum;
412 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
413 $self->process_m2m( 'link_table' => 'pkg_referral',
414 'target_table' => 'part_referral',
415 'params' => $self->refnum,
418 if ( $self->hashref->{cust_pkg_usageprice} ) {
419 for my $cust_pkg_usageprice ( @{ $self->hashref->{cust_pkg_usageprice} } ) {
420 $cust_pkg_usageprice->pkgnum( $self->pkgnum );
421 my $error = $cust_pkg_usageprice->insert;
423 $dbh->rollback if $oldAutoCommit;
429 if ( $self->discountnum ) {
430 my $error = $self->insert_discount();
432 $dbh->rollback if $oldAutoCommit;
437 my $conf = new FS::Conf;
439 if ( ! $import && $conf->config('ticket_system') && $options{ticket_subject} ) {
441 #this init stuff is still inefficient, but at least its limited to
442 # the small number (any?) folks using ticket emailing on pkg order
445 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
452 use FS::TicketSystem;
453 FS::TicketSystem->init();
455 my $q = new RT::Queue($RT::SystemUser);
456 $q->Load($options{ticket_queue}) if $options{ticket_queue};
457 my $t = new RT::Ticket($RT::SystemUser);
458 my $mime = new MIME::Entity;
459 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
460 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
461 Subject => $options{ticket_subject},
464 $t->AddLink( Type => 'MemberOf',
465 Target => 'freeside://freeside/cust_main/'. $self->custnum,
469 if (! $import && $conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
470 my $queue = new FS::queue {
471 'job' => 'FS::cust_main::queueable_print',
473 $error = $queue->insert(
474 'custnum' => $self->custnum,
475 'template' => 'welcome_letter',
479 warn "can't send welcome letter: $error";
484 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
491 This method now works but you probably shouldn't use it.
493 You don't want to delete packages, because there would then be no record
494 the customer ever purchased the package. Instead, see the cancel method and
495 hide cancelled packages.
499 # this is still used internally to abort future package changes, so it
505 # The following foreign keys to cust_pkg are not cleaned up here, and will
506 # cause package deletion to fail:
508 # cust_credit.pkgnum and commission_pkgnum (and cust_credit_void)
509 # cust_credit_bill.pkgnum
510 # cust_pay_pending.pkgnum
511 # cust_pay.pkgnum (and cust_pay_void)
512 # cust_bill_pay.pkgnum (wtf, shouldn't reference pkgnum)
513 # cust_pkg_usage.pkgnum
514 # cust_pkg.uncancel_pkgnum, change_pkgnum, main_pkgnum, and change_to_pkgnum
516 # cust_svc is handled by canceling the package before deleting it
517 # cust_pkg_option is handled via option_Common
519 my $oldAutoCommit = $FS::UID::AutoCommit;
520 local $FS::UID::AutoCommit = 0;
523 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
524 my $error = $cust_pkg_discount->delete;
526 $dbh->rollback if $oldAutoCommit;
530 #cust_bill_pkg_discount?
532 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
533 my $error = $cust_pkg_detail->delete;
535 $dbh->rollback if $oldAutoCommit;
540 foreach my $cust_pkg_reason (
542 'table' => 'cust_pkg_reason',
543 'hashref' => { 'pkgnum' => $self->pkgnum },
547 my $error = $cust_pkg_reason->delete;
549 $dbh->rollback if $oldAutoCommit;
554 foreach my $pkg_referral ( $self->pkg_referral ) {
555 my $error = $pkg_referral->delete;
557 $dbh->rollback if $oldAutoCommit;
562 my $error = $self->SUPER::delete(@_);
564 $dbh->rollback if $oldAutoCommit;
568 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
574 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
576 Replaces the OLD_RECORD with this one in the database. If there is an error,
577 returns the error, otherwise returns false.
579 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
581 Changing pkgpart may have disasterous effects. See the order subroutine.
583 setup and bill are normally updated by calling the bill method of a customer
584 object (see L<FS::cust_main>).
586 suspend is normally updated by the suspend and unsuspend methods.
588 cancel is normally updated by the cancel method (and also the order subroutine
591 Available options are:
597 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.
601 the access_user (see L<FS::access_user>) providing the reason
605 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
614 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
619 ( ref($_[0]) eq 'HASH' )
623 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
624 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
627 #return "Can't change setup once it exists!"
628 # if $old->getfield('setup') &&
629 # $old->getfield('setup') != $new->getfield('setup');
631 #some logic for bill, susp, cancel?
633 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
635 my $oldAutoCommit = $FS::UID::AutoCommit;
636 local $FS::UID::AutoCommit = 0;
639 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
640 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
641 my $error = $new->insert_reason(
642 'reason' => $options->{'reason'},
643 'date' => $new->$method,
645 'reason_otaker' => $options->{'reason_otaker'},
648 dbh->rollback if $oldAutoCommit;
649 return "Error inserting cust_pkg_reason: $error";
654 #save off and freeze RADIUS attributes for any associated svc_acct records
656 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
658 #also check for specific exports?
659 # to avoid spurious modify export events
660 @svc_acct = map { $_->svc_x }
661 grep { $_->part_svc->svcdb eq 'svc_acct' }
664 $_->snapshot foreach @svc_acct;
668 my $error = $new->export_pkg_change($old)
669 || $new->SUPER::replace( $old,
671 ? $options->{options}
675 $dbh->rollback if $oldAutoCommit;
679 #for prepaid packages,
680 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
681 foreach my $old_svc_acct ( @svc_acct ) {
682 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
684 $new_svc_acct->replace( $old_svc_acct,
685 'depend_jobnum' => $options->{depend_jobnum},
688 $dbh->rollback if $oldAutoCommit;
693 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
700 Checks all fields to make sure this is a valid billing item. If there is an
701 error, returns the error, otherwise returns false. Called by the insert and
709 if ( !$self->locationnum or $self->locationnum == -1 ) {
710 $self->set('locationnum', $self->cust_main->ship_locationnum);
714 $self->ut_numbern('pkgnum')
715 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
716 || $self->ut_numbern('pkgpart')
717 || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' )
718 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
719 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
720 || $self->ut_numbern('quantity')
721 || $self->ut_numbern('start_date')
722 || $self->ut_numbern('setup')
723 || $self->ut_numbern('bill')
724 || $self->ut_numbern('susp')
725 || $self->ut_numbern('cancel')
726 || $self->ut_numbern('adjourn')
727 || $self->ut_numbern('resume')
728 || $self->ut_numbern('expire')
729 || $self->ut_numbern('dundate')
730 || $self->ut_flag('no_auto', [ '', 'Y' ])
731 || $self->ut_flag('waive_setup', [ '', 'Y' ])
732 || $self->ut_flag('separate_bill')
733 || $self->ut_textn('agent_pkgid')
734 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
735 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
736 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
737 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
738 || $self->ut_foreign_keyn('change_to_pkgnum', 'cust_pkg', 'pkgnum')
740 return $error if $error;
742 return "A package with both start date (future start) and setup date (already started) will never bill"
743 if $self->start_date && $self->setup && ! $upgrade;
745 return "A future unsuspend date can only be set for a package with a suspend date"
746 if $self->resume and !$self->susp and !$self->adjourn;
748 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
750 if ( $self->dbdef_table->column('manual_flag') ) {
751 $self->manual_flag('') if $self->manual_flag eq ' ';
752 $self->manual_flag =~ /^([01]?)$/
753 or return "Illegal manual_flag ". $self->manual_flag;
754 $self->manual_flag($1);
762 Check the pkgpart to make sure it's allowed with the reg_code and/or
763 promo_code of the package (if present) and with the customer's agent.
764 Called from C<insert>, unless we are doing a package change that doesn't
772 # my $error = $self->ut_numbern('pkgpart'); # already done
775 if ( $self->reg_code ) {
777 unless ( grep { $self->pkgpart == $_->pkgpart }
778 map { $_->reg_code_pkg }
779 qsearchs( 'reg_code', { 'code' => $self->reg_code,
780 'agentnum' => $self->cust_main->agentnum })
782 return "Unknown registration code";
785 } elsif ( $self->promo_code ) {
788 qsearchs('part_pkg', {
789 'pkgpart' => $self->pkgpart,
790 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
792 return 'Unknown promotional code' unless $promo_part_pkg;
796 unless ( $disable_agentcheck ) {
798 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
799 return "agent ". $agent->agentnum. ':'. $agent->agent.
800 " can't purchase pkgpart ". $self->pkgpart
801 unless $agent->pkgpart_hashref->{ $self->pkgpart }
802 || $agent->agentnum == $self->part_pkg->agentnum;
805 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
806 return $error if $error;
814 =item cancel [ OPTION => VALUE ... ]
816 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
817 in this package, then cancels the package itself (sets the cancel field to
820 Available options are:
824 =item quiet - can be set true to supress email cancellation notices.
826 =item time - can be set to cancel the package based on a specific future or
827 historical date. Using time ensures that the remaining amount is calculated
828 correctly. Note however that this is an immediate cancel and just changes
829 the date. You are PROBABLY looking to expire the account instead of using
832 =item reason - can be set to a cancellation reason (see L<FS:reason>),
833 either a reasonnum of an existing reason, or passing a hashref will create
834 a new reason. The hashref should have the following keys: typenum - Reason
835 type (see L<FS::reason_type>, reason - Text of the new reason.
837 =item date - can be set to a unix style timestamp to specify when to
840 =item nobill - can be set true to skip billing if it might otherwise be done.
842 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
843 not credit it. This must be set (by change()) when changing the package
844 to a different pkgpart or location, and probably shouldn't be in any other
845 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
848 =item no_delay_cancel - prevents delay_cancel behavior
849 no matter what other options say, for use when changing packages (or any
850 other time you're really sure you want an immediate cancel)
854 If there is an error, returns the error, otherwise returns false.
858 #NOT DOCUMENTING - this should only be used when calling recursively
859 #=item delay_cancel - for internal use, to allow proper handling of
860 #supplemental packages when the main package is flagged to suspend
861 #before cancelling, probably shouldn't be used otherwise (set the
862 #corresponding package option instead)
865 my( $self, %options ) = @_;
868 # supplemental packages can now be separately canceled, though the UI
869 # shouldn't permit it
871 ## pass all suspend/cancel actions to the main package
872 ## (unless the pkglinknum has been removed, then the link is defunct and
873 ## this package can be canceled on its own)
874 #if ( $self->main_pkgnum and $self->pkglinknum and !$options{'from_main'} ) {
875 # return $self->main_pkg->cancel(%options);
878 my $conf = new FS::Conf;
880 warn "cust_pkg::cancel called with options".
881 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
884 my $oldAutoCommit = $FS::UID::AutoCommit;
885 local $FS::UID::AutoCommit = 0;
888 my $old = $self->select_for_update;
890 if ( $old->get('cancel') || $self->get('cancel') ) {
891 dbh->rollback if $oldAutoCommit;
892 return ""; # no error
895 # XXX possibly set cancel_time to the expire date?
896 my $cancel_time = $options{'time'} || time;
897 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
898 $date = '' if ($date && $date <= $cancel_time); # complain instead?
900 my $delay_cancel = $options{'no_delay_cancel'} ? 0 : $options{'delay_cancel'};
901 if ( !$date && $self->part_pkg->option('delay_cancel',1)
902 && (($self->status eq 'active') || ($self->status eq 'suspended'))
903 && !$options{'no_delay_cancel'}
905 my $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
906 my $expsecs = 60*60*24*$expdays;
907 my $suspfor = $self->susp ? $cancel_time - $self->susp : 0;
908 $expsecs = $expsecs - $suspfor if $suspfor;
909 unless ($expsecs <= 0) { #if it's already been suspended long enough, don't re-suspend
911 $date = $cancel_time + $expsecs;
915 #race condition: usage could be ongoing until unprovisioned
916 #resolved by performing a change package instead (which unprovisions) and
918 if ( !$options{nobill} && !$date ) {
919 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
920 my $copy = $self->new({$self->hash});
922 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
924 'time' => $cancel_time );
925 warn "Error billing during cancel, custnum ".
926 #$self->cust_main->custnum. ": $error"
931 if ( $options{'reason'} ) {
932 $error = $self->insert_reason( 'reason' => $options{'reason'},
933 'action' => $date ? 'expire' : 'cancel',
934 'date' => $date ? $date : $cancel_time,
935 'reason_otaker' => $options{'reason_otaker'},
938 dbh->rollback if $oldAutoCommit;
939 return "Error inserting cust_pkg_reason: $error";
943 my %svc_cancel_opt = ();
944 $svc_cancel_opt{'date'} = $date if $date;
945 foreach my $cust_svc (
948 sort { $a->[1] <=> $b->[1] }
949 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
950 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
952 my $part_svc = $cust_svc->part_svc;
953 next if ( defined($part_svc) and $part_svc->preserve );
954 my $error = $cust_svc->cancel( %svc_cancel_opt );
957 $dbh->rollback if $oldAutoCommit;
958 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
964 # credit remaining time if appropriate
966 if ( exists($options{'unused_credit'}) ) {
967 $do_credit = $options{'unused_credit'};
970 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
973 my $error = $self->credit_remaining('cancel', $cancel_time);
975 $dbh->rollback if $oldAutoCommit;
981 my %hash = $self->hash;
983 $hash{'expire'} = $date;
985 # just to be sure these are clear
986 $hash{'adjourn'} = undef;
987 $hash{'resume'} = undef;
990 $hash{'cancel'} = $cancel_time;
992 $hash{'change_custnum'} = $options{'change_custnum'};
994 # if this is a supplemental package that's lost its part_pkg_link, and it's
995 # being canceled for real, unlink it completely
996 if ( !$date and ! $self->pkglinknum ) {
997 $hash{main_pkgnum} = '';
1000 # if there is a future package change scheduled, unlink from it (like
1001 # abort_change) first, then delete it.
1002 $hash{'change_to_pkgnum'} = '';
1004 # save the package state
1005 my $new = new FS::cust_pkg ( \%hash );
1006 $error = $new->replace( $self, options => { $self->options } );
1008 if ( $self->change_to_pkgnum ) {
1009 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
1010 $error ||= $change_to->cancel('no_delay_cancel' => 1) || $change_to->delete;
1013 $dbh->rollback if $oldAutoCommit;
1017 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1018 $error = $supp_pkg->cancel(%options,
1020 'date' => $date, #in case it got changed by delay_cancel
1021 'delay_cancel' => $delay_cancel,
1024 $dbh->rollback if $oldAutoCommit;
1025 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1029 if ($delay_cancel && !$options{'from_main'}) {
1030 $error = $new->suspend(
1032 'time' => $cancel_time
1037 foreach my $usage ( $self->cust_pkg_usage ) {
1038 $error = $usage->delete;
1040 $dbh->rollback if $oldAutoCommit;
1041 return "deleting usage pools: $error";
1046 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1047 return '' if $date; #no errors
1049 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
1050 if ( !$options{'quiet'} &&
1051 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
1053 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
1056 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
1057 $error = $msg_template->send( 'cust_main' => $self->cust_main,
1058 'object' => $self );
1061 $error = send_email(
1062 'from' => $conf->invoice_from_full( $self->cust_main->agentnum ),
1063 'to' => \@invoicing_list,
1064 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
1065 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
1066 'custnum' => $self->custnum,
1067 'msgtype' => '', #admin?
1070 #should this do something on errors?
1077 =item cancel_if_expired [ NOW_TIMESTAMP ]
1079 Cancels this package if its expire date has been reached.
1083 sub cancel_if_expired {
1085 my $time = shift || time;
1086 return '' unless $self->expire && $self->expire <= $time;
1087 my $error = $self->cancel;
1089 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
1090 $self->custnum. ": $error";
1097 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
1098 locationnum, (other fields?). Attempts to re-provision cancelled services
1099 using history information (errors at this stage are not fatal).
1101 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
1103 svc_fatal: service provisioning errors are fatal
1105 svc_errors: pass an array reference, will be filled in with any provisioning errors
1107 main_pkgnum: link the package as a supplemental package of this one. For
1113 my( $self, %options ) = @_;
1115 #in case you try do do $uncancel-date = $cust_pkg->uncacel
1116 return '' unless $self->get('cancel');
1118 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
1119 return $self->main_pkg->uncancel(%options);
1126 my $oldAutoCommit = $FS::UID::AutoCommit;
1127 local $FS::UID::AutoCommit = 0;
1131 # insert the new package
1134 my $cust_pkg = new FS::cust_pkg {
1135 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
1136 bill => ( $options{'bill'} || $self->get('bill') ),
1138 uncancel_pkgnum => $self->pkgnum,
1139 main_pkgnum => ($options{'main_pkgnum'} || ''),
1140 map { $_ => $self->get($_) } qw(
1141 custnum pkgpart locationnum
1143 susp adjourn resume expire start_date contract_end dundate
1144 change_date change_pkgpart change_locationnum
1145 manual_flag no_auto separate_bill quantity agent_pkgid
1146 recur_show_zero setup_show_zero
1150 my $error = $cust_pkg->insert(
1151 'change' => 1, #supresses any referral credit to a referring customer
1152 'allow_pkgpart' => 1, # allow this even if the package def is disabled
1155 $dbh->rollback if $oldAutoCommit;
1163 #find historical services within this timeframe before the package cancel
1164 # (incompatible with "time" option to cust_pkg->cancel?)
1165 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
1166 # too little? (unprovisioing export delay?)
1167 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
1168 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1171 foreach my $h_cust_svc (@h_cust_svc) {
1172 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1173 #next unless $h_svc_x; #should this happen?
1174 (my $table = $h_svc_x->table) =~ s/^h_//;
1175 require "FS/$table.pm";
1176 my $class = "FS::$table";
1177 my $svc_x = $class->new( {
1178 'pkgnum' => $cust_pkg->pkgnum,
1179 'svcpart' => $h_cust_svc->svcpart,
1180 map { $_ => $h_svc_x->get($_) } fields($table)
1184 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1185 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1188 my $svc_error = $svc_x->insert;
1190 if ( $options{svc_fatal} ) {
1191 $dbh->rollback if $oldAutoCommit;
1194 # if we've failed to insert the svc_x object, svc_Common->insert
1195 # will have removed the cust_svc already. if not, then both records
1196 # were inserted but we failed for some other reason (export, most
1197 # likely). in that case, report the error and delete the records.
1198 push @svc_errors, $svc_error;
1199 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1201 # except if export_insert failed, export_delete probably won't be
1203 local $FS::svc_Common::noexport_hack = 1;
1204 my $cleanup_error = $svc_x->delete; # also deletes cust_svc
1205 if ( $cleanup_error ) { # and if THAT fails, then run away
1206 $dbh->rollback if $oldAutoCommit;
1207 return $cleanup_error;
1212 } #foreach $h_cust_svc
1214 #these are pretty rare, but should handle them
1215 # - dsl_device (mac addresses)
1216 # - phone_device (mac addresses)
1217 # - dsl_note (ikano notes)
1218 # - domain_record (i.e. restore DNS information w/domains)
1219 # - inventory_item(?) (inventory w/un-cancelling service?)
1220 # - nas (svc_broaband nas stuff)
1221 #this stuff is unused in the wild afaik
1222 # - mailinglistmember
1224 # - svc_domain.parent_svcnum?
1225 # - acct_snarf (ancient mail fetching config)
1226 # - cgp_rule (communigate)
1227 # - cust_svc_option (used by our Tron stuff)
1228 # - acct_rt_transaction (used by our time worked stuff)
1231 # also move over any services that didn't unprovision at cancellation
1234 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1235 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1236 my $error = $cust_svc->replace;
1238 $dbh->rollback if $oldAutoCommit;
1244 # Uncancel any supplemental packages, and make them supplemental to the
1248 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1250 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1252 $dbh->rollback if $oldAutoCommit;
1253 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1261 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1263 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1264 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1271 Cancels any pending expiration (sets the expire field to null).
1273 If there is an error, returns the error, otherwise returns false.
1278 my( $self, %options ) = @_;
1281 my $oldAutoCommit = $FS::UID::AutoCommit;
1282 local $FS::UID::AutoCommit = 0;
1285 my $old = $self->select_for_update;
1287 my $pkgnum = $old->pkgnum;
1288 if ( $old->get('cancel') || $self->get('cancel') ) {
1289 dbh->rollback if $oldAutoCommit;
1290 return "Can't unexpire cancelled package $pkgnum";
1291 # or at least it's pointless
1294 unless ( $old->get('expire') && $self->get('expire') ) {
1295 dbh->rollback if $oldAutoCommit;
1296 return ""; # no error
1299 my %hash = $self->hash;
1300 $hash{'expire'} = '';
1301 my $new = new FS::cust_pkg ( \%hash );
1302 $error = $new->replace( $self, options => { $self->options } );
1304 $dbh->rollback if $oldAutoCommit;
1308 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1314 =item suspend [ OPTION => VALUE ... ]
1316 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1317 package, then suspends the package itself (sets the susp field to now).
1319 Available options are:
1323 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1324 either a reasonnum of an existing reason, or passing a hashref will create
1325 a new reason. The hashref should have the following keys:
1326 - typenum - Reason type (see L<FS::reason_type>
1327 - reason - Text of the new reason.
1329 =item date - can be set to a unix style timestamp to specify when to
1332 =item time - can be set to override the current time, for calculation
1333 of final invoices or unused-time credits
1335 =item resume_date - can be set to a time when the package should be
1336 unsuspended. This may be more convenient than calling C<unsuspend()>
1339 =item from_main - allows a supplemental package to be suspended, rather
1340 than redirecting the method call to its main package. For internal use.
1342 =item from_cancel - used when suspending from the cancel method, forces
1343 this to skip everything besides basic suspension. For internal use.
1347 If there is an error, returns the error, otherwise returns false.
1352 my( $self, %options ) = @_;
1355 # supplemental packages still can't be separately suspended, but silently
1356 # exit instead of failing or passing the action to the main package (so
1357 # that the "Suspend customer" action doesn't trip over the supplemental
1360 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1364 my $oldAutoCommit = $FS::UID::AutoCommit;
1365 local $FS::UID::AutoCommit = 0;
1368 my $old = $self->select_for_update;
1370 my $pkgnum = $old->pkgnum;
1371 if ( $old->get('cancel') || $self->get('cancel') ) {
1372 dbh->rollback if $oldAutoCommit;
1373 return "Can't suspend cancelled package $pkgnum";
1376 if ( $old->get('susp') || $self->get('susp') ) {
1377 dbh->rollback if $oldAutoCommit;
1378 return ""; # no error # complain on adjourn?
1381 my $suspend_time = $options{'time'} || time;
1382 my $date = $options{date} if $options{date}; # adjourn/suspend later
1383 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1385 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1386 dbh->rollback if $oldAutoCommit;
1387 return "Package $pkgnum expires before it would be suspended.";
1390 # some false laziness with sub cancel
1391 if ( !$options{nobill} && !$date && !$options{'from_cancel'} &&
1392 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1393 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1394 # make the entire cust_main->bill path recognize 'suspend' and
1395 # 'cancel' separately.
1396 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1397 my $copy = $self->new({$self->hash});
1399 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1401 'time' => $suspend_time );
1402 warn "Error billing during suspend, custnum ".
1403 #$self->cust_main->custnum. ": $error"
1408 my $cust_pkg_reason;
1409 if ( $options{'reason'} ) {
1410 $error = $self->insert_reason( 'reason' => $options{'reason'},
1411 'action' => $date ? 'adjourn' : 'suspend',
1412 'date' => $date ? $date : $suspend_time,
1413 'reason_otaker' => $options{'reason_otaker'},
1416 dbh->rollback if $oldAutoCommit;
1417 return "Error inserting cust_pkg_reason: $error";
1419 $cust_pkg_reason = qsearchs('cust_pkg_reason', {
1420 'date' => $date ? $date : $suspend_time,
1421 'action' => $date ? 'A' : 'S',
1422 'pkgnum' => $self->pkgnum,
1426 # if a reasonnum was passed, get the actual reason object so we can check
1428 # (passing a reason hashref is still allowed, but it can't be used with
1429 # the fancy behavioral options.)
1432 if ($options{'reason'} =~ /^\d+$/) {
1433 $reason = FS::reason->by_key($options{'reason'});
1436 my %hash = $self->hash;
1438 $hash{'adjourn'} = $date;
1440 $hash{'susp'} = $suspend_time;
1443 my $resume_date = $options{'resume_date'} || 0;
1444 if ( $resume_date > ($date || $suspend_time) ) {
1445 $hash{'resume'} = $resume_date;
1448 $options{options} ||= {};
1450 my $new = new FS::cust_pkg ( \%hash );
1451 $error = $new->replace( $self, options => { $self->options,
1452 %{ $options{options} },
1456 $dbh->rollback if $oldAutoCommit;
1460 unless ( $date ) { # then we are suspending now
1462 unless ($options{'from_cancel'}) {
1463 # credit remaining time if appropriate
1464 # (if required by the package def, or the suspend reason)
1465 my $unused_credit = $self->part_pkg->option('unused_credit_suspend',1)
1466 || ( defined($reason) && $reason->unused_credit );
1468 if ( $unused_credit ) {
1469 warn "crediting unused time on pkg#".$self->pkgnum."\n" if $DEBUG;
1470 my $error = $self->credit_remaining('suspend', $suspend_time);
1472 $dbh->rollback if $oldAutoCommit;
1478 my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } );
1480 #attempt ordering ala cust_svc_suspend_cascade (without infinite-looping
1481 # on the circular dep case)
1482 # (this is too simple for multi-level deps, we need to use something
1483 # to resolve the DAG properly when possible)
1485 $svcpart{$_->svcpart} = 0 foreach @cust_svc;
1486 foreach my $svcpart ( keys %svcpart ) {
1487 foreach my $part_svc_link (
1488 FS::part_svc_link->by_agentnum($self->cust_main->agentnum,
1489 src_svcpart => $svcpart,
1490 link_type => 'cust_svc_suspend_cascade'
1493 $svcpart{$part_svc_link->dst_svcpart} = max(
1494 $svcpart{$part_svc_link->dst_svcpart},
1495 $svcpart{$part_svc_link->src_svcpart} + 1
1499 @cust_svc = sort { $svcpart{ $a->svcpart } <=> $svcpart{ $b->svcpart } }
1503 foreach my $cust_svc ( @cust_svc ) {
1504 $cust_svc->suspend( 'labels_arrayref' => \@labels );
1507 # suspension fees: if there is a feepart, and it's not an unsuspend fee,
1508 # and this is not a suspend-before-cancel
1509 if ( $cust_pkg_reason ) {
1510 my $reason_obj = $cust_pkg_reason->reason;
1511 if ( $reason_obj->feepart and
1512 ! $reason_obj->fee_on_unsuspend and
1513 ! $options{'from_cancel'} ) {
1515 # register the need to charge a fee, cust_main->bill will do the rest
1516 warn "registering suspend fee: pkgnum ".$self->pkgnum.", feepart ".$reason->feepart."\n"
1518 my $cust_pkg_reason_fee = FS::cust_pkg_reason_fee->new({
1519 'pkgreasonnum' => $cust_pkg_reason->num,
1520 'pkgnum' => $self->pkgnum,
1521 'feepart' => $reason->feepart,
1522 'nextbill' => $reason->fee_hold,
1524 $error ||= $cust_pkg_reason_fee->insert;
1528 my $conf = new FS::Conf;
1529 if ( $conf->config('suspend_email_admin') && !$options{'from_cancel'} ) {
1531 my $error = send_email(
1532 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1533 #invoice_from ??? well as good as any
1534 'to' => $conf->config('suspend_email_admin'),
1535 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1537 "This is an automatic message from your Freeside installation\n",
1538 "informing you that the following customer package has been suspended:\n",
1540 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1541 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1542 ( map { "Service : $_\n" } @labels ),
1544 'custnum' => $self->custnum,
1545 'msgtype' => 'admin'
1549 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1557 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1558 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1560 $dbh->rollback if $oldAutoCommit;
1561 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1565 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1570 =item credit_remaining MODE TIME
1572 Generate a credit for this package for the time remaining in the current
1573 billing period. MODE is either "suspend" or "cancel" (determines the
1574 credit type). TIME is the time of suspension/cancellation. Both arguments
1579 # Implementation note:
1581 # If you pkgpart-change a package that has been billed, and it's set to give
1582 # credit on package change, then this method gets called and then the new
1583 # package will have no last_bill date. Therefore the customer will be credited
1584 # only once (per billing period) even if there are multiple package changes.
1586 # If you location-change a package that has been billed, this method will NOT
1587 # be called and the new package WILL have the last bill date of the old
1590 # If the new package is then canceled within the same billing cycle,
1591 # credit_remaining needs to run calc_remain on the OLD package to determine
1592 # the amount of unused time to credit.
1594 sub credit_remaining {
1595 # Add a credit for remaining service
1596 my ($self, $mode, $time) = @_;
1597 die 'credit_remaining requires suspend or cancel'
1598 unless $mode eq 'suspend' or $mode eq 'cancel';
1599 die 'no suspend/cancel time' unless $time > 0;
1601 my $conf = FS::Conf->new;
1602 my $reason_type = $conf->config($mode.'_credit_type');
1604 my $last_bill = $self->getfield('last_bill') || 0;
1605 my $next_bill = $self->getfield('bill') || 0;
1606 if ( $last_bill > 0 # the package has been billed
1607 and $next_bill > 0 # the package has a next bill date
1608 and $next_bill >= $time # which is in the future
1610 my @cust_credit_source_bill_pkg = ();
1611 my $remaining_value = 0;
1613 my $remain_pkg = $self;
1614 $remaining_value = $remain_pkg->calc_remain(
1616 'cust_credit_source_bill_pkg' => \@cust_credit_source_bill_pkg,
1619 # we may have to walk back past some package changes to get to the
1620 # one that actually has unused time
1621 while ( $remaining_value == 0 ) {
1622 if ( $remain_pkg->change_pkgnum ) {
1623 $remain_pkg = FS::cust_pkg->by_key($remain_pkg->change_pkgnum);
1625 # the package has really never been billed
1628 $remaining_value = $remain_pkg->calc_remain(
1630 'cust_credit_source_bill_pkg' => \@cust_credit_source_bill_pkg,
1634 if ( $remaining_value > 0 ) {
1635 warn "Crediting for $remaining_value on package ".$self->pkgnum."\n"
1637 my $error = $self->cust_main->credit(
1639 'Credit for unused time on '. $self->part_pkg->pkg,
1640 'reason_type' => $reason_type,
1641 'cust_credit_source_bill_pkg' => \@cust_credit_source_bill_pkg,
1643 return "Error crediting customer \$$remaining_value for unused time".
1644 " on ". $self->part_pkg->pkg. ": $error"
1646 } #if $remaining_value
1647 } #if $last_bill, etc.
1651 =item unsuspend [ OPTION => VALUE ... ]
1653 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1654 package, then unsuspends the package itself (clears the susp field and the
1655 adjourn field if it is in the past). If the suspend reason includes an
1656 unsuspension package, that package will be ordered.
1658 Available options are:
1664 Can be set to a date to unsuspend the package in the future (the 'resume'
1667 =item adjust_next_bill
1669 Can be set true to adjust the next bill date forward by
1670 the amount of time the account was inactive. This was set true by default
1671 in the past (from 1.4.2 and 1.5.0pre6 through 1.7.0), but now needs to be
1672 explicitly requested with this option or in the price plan.
1676 If there is an error, returns the error, otherwise returns false.
1681 my( $self, %opt ) = @_;
1684 # pass all suspend/cancel actions to the main package
1685 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1686 return $self->main_pkg->unsuspend(%opt);
1689 my $oldAutoCommit = $FS::UID::AutoCommit;
1690 local $FS::UID::AutoCommit = 0;
1693 my $old = $self->select_for_update;
1695 my $pkgnum = $old->pkgnum;
1696 if ( $old->get('cancel') || $self->get('cancel') ) {
1697 $dbh->rollback if $oldAutoCommit;
1698 return "Can't unsuspend cancelled package $pkgnum";
1701 unless ( $old->get('susp') && $self->get('susp') ) {
1702 $dbh->rollback if $oldAutoCommit;
1703 return ""; # no error # complain instead?
1706 # handle the case of setting a future unsuspend (resume) date
1707 # and do not continue to actually unsuspend the package
1708 my $date = $opt{'date'};
1709 if ( $date and $date > time ) { # return an error if $date <= time?
1711 if ( $old->get('expire') && $old->get('expire') < $date ) {
1712 $dbh->rollback if $oldAutoCommit;
1713 return "Package $pkgnum expires before it would be unsuspended.";
1716 my $new = new FS::cust_pkg { $self->hash };
1717 $new->set('resume', $date);
1718 $error = $new->replace($self, options => $self->options);
1721 $dbh->rollback if $oldAutoCommit;
1725 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1731 if (!$self->setup) {
1732 # then this package is being released from on-hold status
1733 $error = $self->set_initial_timers;
1735 $dbh->rollback if $oldAutoCommit;
1742 foreach my $cust_svc (
1743 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1745 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1747 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1748 $dbh->rollback if $oldAutoCommit;
1749 return "Illegal svcdb value in part_svc!";
1752 require "FS/$svcdb.pm";
1754 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1756 $error = $svc->unsuspend;
1758 $dbh->rollback if $oldAutoCommit;
1761 my( $label, $value ) = $cust_svc->label;
1762 push @labels, "$label: $value";
1767 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1768 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
1770 my %hash = $self->hash;
1771 my $inactive = time - $hash{'susp'};
1773 my $conf = new FS::Conf;
1775 #adjust the next bill date forward
1776 # increment next bill date if certain conditions are met:
1777 # - it was due to be billed at some point
1778 # - either the global or local config says to do this
1779 my $adjust_bill = 0;
1782 && ( $hash{'bill'} || $hash{'setup'} )
1783 && ( $opt{'adjust_next_bill'}
1784 || $conf->exists('unsuspend-always_adjust_next_bill_date')
1785 || $self->part_pkg->option('unsuspend_adjust_bill', 1)
1792 # - the package billed during suspension
1793 # - or it was ordered on hold
1794 # - or the customer was credited for the unused time
1796 if ( $self->option('suspend_bill',1)
1797 or ( $self->part_pkg->option('suspend_bill',1)
1798 and ! $self->option('no_suspend_bill',1)
1800 or $hash{'order_date'} == $hash{'susp'}
1805 if ( $adjust_bill ) {
1806 if ( $self->part_pkg->option('unused_credit_suspend')
1807 or ( ref($reason) and $reason->unused_credit ) ) {
1808 # then the customer was credited for the unused time before suspending,
1809 # so their next bill should be immediate
1810 $hash{'bill'} = time;
1812 # add the length of time suspended to the bill date
1813 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
1818 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
1819 $hash{'resume'} = '' if !$hash{'adjourn'};
1820 my $new = new FS::cust_pkg ( \%hash );
1821 $error = $new->replace( $self, options => { $self->options } );
1823 $dbh->rollback if $oldAutoCommit;
1830 if ( $reason->unsuspend_pkgpart ) {
1831 warn "Suspend reason '".$reason->reason."' uses deprecated unsuspend_pkgpart feature.\n";
1832 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
1833 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
1835 my $start_date = $self->cust_main->next_bill_date
1836 if $reason->unsuspend_hold;
1839 $unsusp_pkg = FS::cust_pkg->new({
1840 'custnum' => $self->custnum,
1841 'pkgpart' => $reason->unsuspend_pkgpart,
1842 'start_date' => $start_date,
1843 'locationnum' => $self->locationnum,
1844 # discount? probably not...
1847 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
1850 # new way, using fees
1851 if ( $reason->feepart and $reason->fee_on_unsuspend ) {
1852 # register the need to charge a fee, cust_main->bill will do the rest
1853 warn "registering unsuspend fee: pkgnum ".$self->pkgnum.", feepart ".$reason->feepart."\n"
1855 my $cust_pkg_reason_fee = FS::cust_pkg_reason_fee->new({
1856 'pkgreasonnum' => $cust_pkg_reason->num,
1857 'pkgnum' => $self->pkgnum,
1858 'feepart' => $reason->feepart,
1859 'nextbill' => $reason->fee_hold,
1861 $error ||= $cust_pkg_reason_fee->insert;
1865 $dbh->rollback if $oldAutoCommit;
1870 if ( $conf->config('unsuspend_email_admin') ) {
1872 my $error = send_email(
1873 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1874 #invoice_from ??? well as good as any
1875 'to' => $conf->config('unsuspend_email_admin'),
1876 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
1877 "This is an automatic message from your Freeside installation\n",
1878 "informing you that the following customer package has been unsuspended:\n",
1880 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1881 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1882 ( map { "Service : $_\n" } @labels ),
1884 "An unsuspension fee was charged: ".
1885 $unsusp_pkg->part_pkg->pkg_comment."\n"
1889 'custnum' => $self->custnum,
1890 'msgtype' => 'admin',
1894 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
1900 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1901 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
1903 $dbh->rollback if $oldAutoCommit;
1904 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1908 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1915 Cancels any pending suspension (sets the adjourn field to null).
1917 If there is an error, returns the error, otherwise returns false.
1922 my( $self, %options ) = @_;
1925 my $oldAutoCommit = $FS::UID::AutoCommit;
1926 local $FS::UID::AutoCommit = 0;
1929 my $old = $self->select_for_update;
1931 my $pkgnum = $old->pkgnum;
1932 if ( $old->get('cancel') || $self->get('cancel') ) {
1933 dbh->rollback if $oldAutoCommit;
1934 return "Can't unadjourn cancelled package $pkgnum";
1935 # or at least it's pointless
1938 if ( $old->get('susp') || $self->get('susp') ) {
1939 dbh->rollback if $oldAutoCommit;
1940 return "Can't unadjourn suspended package $pkgnum";
1941 # perhaps this is arbitrary
1944 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1945 dbh->rollback if $oldAutoCommit;
1946 return ""; # no error
1949 my %hash = $self->hash;
1950 $hash{'adjourn'} = '';
1951 $hash{'resume'} = '';
1952 my $new = new FS::cust_pkg ( \%hash );
1953 $error = $new->replace( $self, options => { $self->options } );
1955 $dbh->rollback if $oldAutoCommit;
1959 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1966 =item change HASHREF | OPTION => VALUE ...
1968 Changes this package: cancels it and creates a new one, with a different
1969 pkgpart or locationnum or both. All services are transferred to the new
1970 package (no change will be made if this is not possible).
1972 Options may be passed as a list of key/value pairs or as a hash reference.
1979 New locationnum, to change the location for this package.
1983 New FS::cust_location object, to create a new location and assign it
1988 New FS::cust_main object, to create a new customer and assign the new package
1993 New pkgpart (see L<FS::part_pkg>).
1997 New refnum (see L<FS::part_referral>).
2001 New quantity; if unspecified, the new package will have the same quantity
2006 "New" (existing) FS::cust_pkg object. The package's services and other
2007 attributes will be transferred to this package.
2011 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
2012 susp, adjourn, cancel, expire, and contract_end) to the new package.
2014 =item unprotect_svcs
2016 Normally, change() will rollback and return an error if some services
2017 can't be transferred (also see the I<cust_pkg-change_svcpart> config option).
2018 If unprotect_svcs is true, this method will transfer as many services as
2019 it can and then unconditionally cancel the old package.
2023 If specified, sets this value for the contract_end date on the new package
2024 (without regard for keep_dates or the usual date-preservation behavior.)
2025 Will throw an error if defined but false; the UI doesn't allow editing
2026 this unless it already exists, making removal impossible to undo.
2030 At least one of locationnum, cust_location, pkgpart, refnum, cust_main, or
2031 cust_pkg must be specified (otherwise, what's the point?)
2033 Returns either the new FS::cust_pkg object or a scalar error.
2037 my $err_or_new_cust_pkg = $old_cust_pkg->change
2041 #used by change and change_later
2042 #didn't put with documented check methods because it depends on change-specific opts
2043 #and it also possibly edits the value of opts
2047 if ( defined($opt->{'contract_end'}) ) {
2048 my $current_contract_end = $self->get('contract_end');
2049 unless ($opt->{'contract_end'}) {
2050 if ($current_contract_end) {
2051 return "Cannot remove contract end date when changing packages";
2053 #shouldn't even pass this option if there's not a current value
2054 #but can be handled gracefully if the option is empty
2055 warn "Contract end date passed unexpectedly";
2056 delete $opt->{'contract_end'};
2060 unless ($current_contract_end) {
2061 #option shouldn't be passed, throw error if it's non-empty
2062 return "Cannot add contract end date when changing packages " . $self->pkgnum;
2068 #some false laziness w/order
2071 my $opt = ref($_[0]) ? shift : { @_ };
2073 my $conf = new FS::Conf;
2075 # handle contract_end on cust_pkg same as passed option
2076 if ( $opt->{'cust_pkg'} ) {
2077 $opt->{'contract_end'} = $opt->{'cust_pkg'}->contract_end;
2078 delete $opt->{'contract_end'} unless $opt->{'contract_end'};
2081 # check contract_end, prevent adding/removing
2082 my $error = $self->_check_change($opt);
2083 return $error if $error;
2085 # Transactionize this whole mess
2086 my $oldAutoCommit = $FS::UID::AutoCommit;
2087 local $FS::UID::AutoCommit = 0;
2090 if ( $opt->{'cust_location'} ) {
2091 $error = $opt->{'cust_location'}->find_or_insert;
2093 $dbh->rollback if $oldAutoCommit;
2094 return "creating location record: $error";
2096 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
2099 # Before going any further here: if the package is still in the pre-setup
2100 # state, it's safe to modify it in place. No need to charge/credit for
2101 # partial period, transfer services, transfer usage pools, copy invoice
2102 # details, or change any dates.
2103 if ( ! $self->setup and ! $opt->{cust_pkg} and ! $opt->{cust_main} ) {
2104 foreach ( qw( locationnum pkgpart quantity refnum salesnum ) ) {
2105 if ( length($opt->{$_}) ) {
2106 $self->set($_, $opt->{$_});
2109 # almost. if the new pkgpart specifies start/adjourn/expire timers,
2111 if ( $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart ) {
2112 $error ||= $self->set_initial_timers;
2114 # but if contract_end was explicitly specified, that overrides all else
2115 $self->set('contract_end', $opt->{'contract_end'})
2116 if $opt->{'contract_end'};
2117 $error ||= $self->replace;
2119 $dbh->rollback if $oldAutoCommit;
2120 return "modifying package: $error";
2122 $dbh->commit if $oldAutoCommit;
2131 $hash{'setup'} = $time if $self->setup;
2133 $hash{'change_date'} = $time;
2134 $hash{"change_$_"} = $self->$_()
2135 foreach qw( pkgnum pkgpart locationnum );
2137 if ( $opt->{'cust_pkg'} ) {
2138 # treat changing to a package with a different pkgpart as a
2139 # pkgpart change (because it is)
2140 $opt->{'pkgpart'} = $opt->{'cust_pkg'}->pkgpart;
2143 # whether to override pkgpart checking on the new package
2144 my $same_pkgpart = 1;
2145 if ( $opt->{'pkgpart'} and ( $opt->{'pkgpart'} != $self->pkgpart ) ) {
2149 my $unused_credit = 0;
2150 my $keep_dates = $opt->{'keep_dates'};
2152 # Special case. If the pkgpart is changing, and the customer is
2153 # going to be credited for remaining time, don't keep setup, bill,
2154 # or last_bill dates, and DO pass the flag to cancel() to credit
2156 if ( $opt->{'pkgpart'}
2157 and $opt->{'pkgpart'} != $self->pkgpart
2158 and $self->part_pkg->option('unused_credit_change', 1) ) {
2161 $hash{$_} = '' foreach qw(setup bill last_bill);
2164 if ( $keep_dates ) {
2165 foreach my $date ( qw(setup bill last_bill) ) {
2166 $hash{$date} = $self->getfield($date);
2169 # always keep the following dates
2170 foreach my $date (qw(order_date susp adjourn cancel expire resume
2171 start_date contract_end)) {
2172 $hash{$date} = $self->getfield($date);
2174 # but if contract_end was explicitly specified, that overrides all else
2175 $hash{'contract_end'} = $opt->{'contract_end'}
2176 if $opt->{'contract_end'};
2178 # allow $opt->{'locationnum'} = '' to specifically set it to null
2179 # (i.e. customer default location)
2180 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2182 # usually this doesn't matter. the two cases where it does are:
2183 # 1. unused_credit_change + pkgpart change + setup fee on the new package
2185 # 2. (more importantly) changing a package before it's billed
2186 $hash{'waive_setup'} = $self->waive_setup;
2188 # if this package is scheduled for a future package change, preserve that
2189 $hash{'change_to_pkgnum'} = $self->change_to_pkgnum;
2191 my $custnum = $self->custnum;
2192 if ( $opt->{cust_main} ) {
2193 my $cust_main = $opt->{cust_main};
2194 unless ( $cust_main->custnum ) {
2195 my $error = $cust_main->insert( @{ $opt->{cust_main_insert_args}||[] } );
2197 $dbh->rollback if $oldAutoCommit;
2198 return "inserting customer record: $error";
2201 $custnum = $cust_main->custnum;
2204 $hash{'contactnum'} = $opt->{'contactnum'} if $opt->{'contactnum'};
2207 if ( $opt->{'cust_pkg'} ) {
2208 # The target package already exists; update it to show that it was
2209 # changed from this package.
2210 $cust_pkg = $opt->{'cust_pkg'};
2212 # follow all the above rules for date changes, etc.
2213 foreach (keys %hash) {
2214 $cust_pkg->set($_, $hash{$_});
2216 # except those that implement the future package change behavior
2217 foreach (qw(change_to_pkgnum start_date expire)) {
2218 $cust_pkg->set($_, '');
2221 $error = $cust_pkg->replace;
2224 # Create the new package.
2225 $cust_pkg = new FS::cust_pkg {
2226 custnum => $custnum,
2227 locationnum => $opt->{'locationnum'},
2228 ( map { $_ => ( $opt->{$_} || $self->$_() ) }
2229 qw( pkgpart quantity refnum salesnum )
2233 $error = $cust_pkg->insert( 'change' => 1,
2234 'allow_pkgpart' => $same_pkgpart );
2237 $dbh->rollback if $oldAutoCommit;
2238 return "inserting new package: $error";
2241 # Transfer services and cancel old package.
2242 # Enforce service limits only if this is a pkgpart change.
2243 local $FS::cust_svc::ignore_quantity;
2244 $FS::cust_svc::ignore_quantity = 1 if $same_pkgpart;
2245 $error = $self->transfer($cust_pkg);
2246 if ($error and $error == 0) {
2247 # $old_pkg->transfer failed.
2248 $dbh->rollback if $oldAutoCommit;
2249 return "transferring $error";
2252 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
2253 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
2254 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
2255 if ($error and $error == 0) {
2256 # $old_pkg->transfer failed.
2257 $dbh->rollback if $oldAutoCommit;
2258 return "converting $error";
2262 # We set unprotect_svcs when executing a "future package change". It's
2263 # not a user-interactive operation, so returning an error means the
2264 # package change will just fail. Rather than have that happen, we'll
2265 # let leftover services be deleted.
2266 if ($error > 0 and !$opt->{'unprotect_svcs'}) {
2267 # Transfers were successful, but we still had services left on the old
2268 # package. We can't change the package under this circumstances, so abort.
2269 $dbh->rollback if $oldAutoCommit;
2270 return "unable to transfer all services";
2273 #reset usage if changing pkgpart
2274 # AND usage rollover is off (otherwise adds twice, now and at package bill)
2275 if ($self->pkgpart != $cust_pkg->pkgpart) {
2276 my $part_pkg = $cust_pkg->part_pkg;
2277 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
2281 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
2284 $dbh->rollback if $oldAutoCommit;
2285 return "setting usage values: $error";
2288 # if NOT changing pkgpart, transfer any usage pools over
2289 foreach my $usage ($self->cust_pkg_usage) {
2290 $usage->set('pkgnum', $cust_pkg->pkgnum);
2291 $error = $usage->replace;
2293 $dbh->rollback if $oldAutoCommit;
2294 return "transferring usage pools: $error";
2299 # transfer usage pricing add-ons, if we're not changing pkgpart
2300 if ( $same_pkgpart ) {
2301 foreach my $old_cust_pkg_usageprice ($self->cust_pkg_usageprice) {
2302 my $new_cust_pkg_usageprice = new FS::cust_pkg_usageprice {
2303 'pkgnum' => $cust_pkg->pkgnum,
2304 'usagepricepart' => $old_cust_pkg_usageprice->usagepricepart,
2305 'quantity' => $old_cust_pkg_usageprice->quantity,
2307 $error = $new_cust_pkg_usageprice->insert;
2309 $dbh->rollback if $oldAutoCommit;
2310 return "Error transferring usage pricing add-on: $error";
2315 # transfer discounts, if we're not changing pkgpart
2316 if ( $same_pkgpart ) {
2317 foreach my $old_discount ($self->cust_pkg_discount_active) {
2318 # don't remove the old discount, we may still need to bill that package.
2319 my $new_discount = new FS::cust_pkg_discount {
2320 'pkgnum' => $cust_pkg->pkgnum,
2321 'discountnum' => $old_discount->discountnum,
2322 'months_used' => $old_discount->months_used,
2324 $error = $new_discount->insert;
2326 $dbh->rollback if $oldAutoCommit;
2327 return "transferring discounts: $error";
2332 # transfer (copy) invoice details
2333 foreach my $detail ($self->cust_pkg_detail) {
2334 my $new_detail = FS::cust_pkg_detail->new({ $detail->hash });
2335 $new_detail->set('pkgdetailnum', '');
2336 $new_detail->set('pkgnum', $cust_pkg->pkgnum);
2337 $error = $new_detail->insert;
2339 $dbh->rollback if $oldAutoCommit;
2340 return "transferring package notes: $error";
2346 if ( !$opt->{'cust_pkg'} ) {
2347 # Order any supplemental packages.
2348 my $part_pkg = $cust_pkg->part_pkg;
2349 my @old_supp_pkgs = $self->supplemental_pkgs;
2350 foreach my $link ($part_pkg->supp_part_pkg_link) {
2352 foreach (@old_supp_pkgs) {
2353 if ($_->pkgpart == $link->dst_pkgpart) {
2355 $_->pkgpart(0); # so that it can't match more than once
2359 # false laziness with FS::cust_main::Packages::order_pkg
2360 my $new = FS::cust_pkg->new({
2361 pkgpart => $link->dst_pkgpart,
2362 pkglinknum => $link->pkglinknum,
2363 custnum => $custnum,
2364 main_pkgnum => $cust_pkg->pkgnum,
2365 locationnum => $cust_pkg->locationnum,
2366 start_date => $cust_pkg->start_date,
2367 order_date => $cust_pkg->order_date,
2368 expire => $cust_pkg->expire,
2369 adjourn => $cust_pkg->adjourn,
2370 contract_end => $cust_pkg->contract_end,
2371 refnum => $cust_pkg->refnum,
2372 discountnum => $cust_pkg->discountnum,
2373 waive_setup => $cust_pkg->waive_setup,
2375 if ( $old and $opt->{'keep_dates'} ) {
2376 foreach (qw(setup bill last_bill)) {
2377 $new->set($_, $old->get($_));
2380 $error = $new->insert( allow_pkgpart => $same_pkgpart );
2383 $error ||= $old->transfer($new);
2385 if ( $error and $error > 0 ) {
2386 # no reason why this should ever fail, but still...
2387 $error = "Unable to transfer all services from supplemental package ".
2391 $dbh->rollback if $oldAutoCommit;
2394 push @new_supp_pkgs, $new;
2396 } # if !$opt->{'cust_pkg'}
2397 # because if there is one, then supplemental packages would already
2398 # have been created for it.
2400 #Good to go, cancel old package. Notify 'cancel' of whether to credit
2402 #Don't allow billing the package (preceding period packages and/or
2403 #outstanding usage) if we are keeping dates (i.e. location changing),
2404 #because the new package will be billed for the same date range.
2405 #Supplemental packages are also canceled here.
2407 # during scheduled changes, avoid canceling the package we just
2409 $self->set('change_to_pkgnum' => '');
2411 $error = $self->cancel(
2413 unused_credit => $unused_credit,
2414 nobill => $keep_dates,
2415 change_custnum => ( $self->custnum != $custnum ? $custnum : '' ),
2416 no_delay_cancel => 1,
2419 $dbh->rollback if $oldAutoCommit;
2420 return "canceling old package: $error";
2423 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
2425 my $error = $cust_pkg->cust_main->bill(
2426 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
2429 $dbh->rollback if $oldAutoCommit;
2430 return "billing new package: $error";
2434 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2440 =item change_later OPTION => VALUE...
2442 Schedule a package change for a later date. This actually orders the new
2443 package immediately, but sets its start date for a future date, and sets
2444 the current package to expire on the same date.
2446 If the package is already scheduled for a change, this can be called with
2447 'start_date' to change the scheduled date, or with pkgpart and/or
2448 locationnum to modify the package change. To cancel the scheduled change
2449 entirely, see C<abort_change>.
2457 The date for the package change. Required, and must be in the future.
2467 The pkgpart, locationnum, quantity and optional contract_end of the new
2468 package, with the same meaning as in C<change>.
2476 my $opt = ref($_[0]) ? shift : { @_ };
2478 # check contract_end, prevent adding/removing
2479 my $error = $self->_check_change($opt);
2480 return $error if $error;
2482 my $oldAutoCommit = $FS::UID::AutoCommit;
2483 local $FS::UID::AutoCommit = 0;
2486 my $cust_main = $self->cust_main;
2488 my $date = delete $opt->{'start_date'} or return 'start_date required';
2490 if ( $date <= time ) {
2491 $dbh->rollback if $oldAutoCommit;
2492 return "start_date $date is in the past";
2495 if ( $self->change_to_pkgnum ) {
2496 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
2497 my $new_pkgpart = $opt->{'pkgpart'}
2498 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $change_to->pkgpart;
2499 my $new_locationnum = $opt->{'locationnum'}
2500 if $opt->{'locationnum'} and $opt->{'locationnum'} != $change_to->locationnum;
2501 my $new_quantity = $opt->{'quantity'}
2502 if $opt->{'quantity'} and $opt->{'quantity'} != $change_to->quantity;
2503 my $new_contract_end = $opt->{'contract_end'}
2504 if $opt->{'contract_end'} and $opt->{'contract_end'} != $change_to->contract_end;
2505 if ( $new_pkgpart or $new_locationnum or $new_quantity or $new_contract_end ) {
2506 # it hasn't been billed yet, so in principle we could just edit
2507 # it in place (w/o a package change), but that's bad form.
2508 # So change the package according to the new options...
2509 my $err_or_pkg = $change_to->change(%$opt);
2510 if ( ref $err_or_pkg ) {
2511 # Then set that package up for a future start.
2512 $self->set('change_to_pkgnum', $err_or_pkg->pkgnum);
2513 $self->set('expire', $date); # in case it's different
2514 $err_or_pkg->set('start_date', $date);
2515 $err_or_pkg->set('change_date', '');
2516 $err_or_pkg->set('change_pkgnum', '');
2518 $error = $self->replace ||
2519 $err_or_pkg->replace ||
2520 #because change() might've edited existing scheduled change in place
2521 (($err_or_pkg->pkgnum == $change_to->pkgnum) ? '' :
2522 $change_to->cancel('no_delay_cancel' => 1) ||
2523 $change_to->delete);
2525 $error = $err_or_pkg;
2527 } else { # change the start date only.
2528 $self->set('expire', $date);
2529 $change_to->set('start_date', $date);
2530 $error = $self->replace || $change_to->replace;
2533 $dbh->rollback if $oldAutoCommit;
2536 $dbh->commit if $oldAutoCommit;
2539 } # if $self->change_to_pkgnum
2541 my $new_pkgpart = $opt->{'pkgpart'}
2542 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart;
2543 my $new_locationnum = $opt->{'locationnum'}
2544 if $opt->{'locationnum'} and $opt->{'locationnum'} != $self->locationnum;
2545 my $new_quantity = $opt->{'quantity'}
2546 if $opt->{'quantity'} and $opt->{'quantity'} != $self->quantity;
2547 my $new_contract_end = $opt->{'contract_end'}
2548 if $opt->{'contract_end'} and $opt->{'contract_end'} != $self->contract_end;
2550 return '' unless $new_pkgpart or $new_locationnum or $new_quantity or $new_contract_end; # wouldn't do anything
2552 # allow $opt->{'locationnum'} = '' to specifically set it to null
2553 # (i.e. customer default location)
2554 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2556 my $new = FS::cust_pkg->new( {
2557 custnum => $self->custnum,
2558 locationnum => $opt->{'locationnum'},
2559 start_date => $date,
2560 map { $_ => ( $opt->{$_} || $self->$_() ) }
2561 qw( pkgpart quantity refnum salesnum contract_end )
2563 $error = $new->insert('change' => 1,
2564 'allow_pkgpart' => ($new_pkgpart ? 0 : 1));
2566 $self->set('change_to_pkgnum', $new->pkgnum);
2567 $self->set('expire', $date);
2568 $error = $self->replace;
2571 $dbh->rollback if $oldAutoCommit;
2573 $dbh->commit if $oldAutoCommit;
2581 Cancels a future package change scheduled by C<change_later>.
2587 my $oldAutoCommit = $FS::UID::AutoCommit;
2588 local $FS::UID::AutoCommit = 0;
2590 my $pkgnum = $self->change_to_pkgnum;
2591 my $change_to = FS::cust_pkg->by_key($pkgnum) if $pkgnum;
2593 $self->set('change_to_pkgnum', '');
2594 $self->set('expire', '');
2595 $error = $self->replace;
2597 $error ||= $change_to->cancel || $change_to->delete;
2600 if ( $oldAutoCommit ) {
2611 =item set_quantity QUANTITY
2613 Change the package's quantity field. This is one of the few package properties
2614 that can safely be changed without canceling and reordering the package
2615 (because it doesn't affect tax eligibility). Returns an error or an
2622 $self = $self->replace_old; # just to make sure
2623 $self->quantity(shift);
2627 =item set_salesnum SALESNUM
2629 Change the package's salesnum (sales person) field. This is one of the few
2630 package properties that can safely be changed without canceling and reordering
2631 the package (because it doesn't affect tax eligibility). Returns an error or
2638 $self = $self->replace_old; # just to make sure
2639 $self->salesnum(shift);
2641 # XXX this should probably reassign any credit that's already been given
2644 =item modify_charge OPTIONS
2646 Change the properties of a one-time charge. The following properties can
2647 be changed this way:
2648 - pkg: the package description
2649 - classnum: the package class
2650 - additional: arrayref of additional invoice details to add to this package
2652 and, I<if the charge has not yet been billed>:
2653 - start_date: the date when it will be billed
2654 - amount: the setup fee to be charged
2655 - quantity: the multiplier for the setup fee
2656 - separate_bill: whether to put the charge on a separate invoice
2658 If you pass 'adjust_commission' => 1, and the classnum changes, and there are
2659 commission credits linked to this charge, they will be recalculated.
2666 my $part_pkg = $self->part_pkg;
2667 my $pkgnum = $self->pkgnum;
2670 my $oldAutoCommit = $FS::UID::AutoCommit;
2671 local $FS::UID::AutoCommit = 0;
2673 return "Can't use modify_charge except on one-time charges"
2674 unless $part_pkg->freq eq '0';
2676 if ( length($opt{'pkg'}) and $part_pkg->pkg ne $opt{'pkg'} ) {
2677 $part_pkg->set('pkg', $opt{'pkg'});
2680 my %pkg_opt = $part_pkg->options;
2681 my $pkg_opt_modified = 0;
2683 $opt{'additional'} ||= [];
2686 foreach (grep /^additional/, keys %pkg_opt) {
2687 ($i) = ($_ =~ /^additional_info(\d+)$/);
2688 $old_additional[$i] = $pkg_opt{$_} if $i;
2689 delete $pkg_opt{$_};
2692 for ( $i = 0; exists($opt{'additional'}->[$i]); $i++ ) {
2693 $pkg_opt{ "additional_info$i" } = $opt{'additional'}->[$i];
2694 if (!exists($old_additional[$i])
2695 or $old_additional[$i] ne $opt{'additional'}->[$i])
2697 $pkg_opt_modified = 1;
2700 $pkg_opt_modified = 1 if (scalar(@old_additional) - 1) != $i;
2701 $pkg_opt{'additional_count'} = $i if $i > 0;
2704 if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} )
2707 $old_classnum = $part_pkg->classnum;
2708 $part_pkg->set('classnum', $opt{'classnum'});
2711 if ( !$self->get('setup') ) {
2712 # not yet billed, so allow amount, setup_cost, quantity, start_date,
2715 if ( exists($opt{'amount'})
2716 and $part_pkg->option('setup_fee') != $opt{'amount'}
2717 and $opt{'amount'} > 0 ) {
2719 $pkg_opt{'setup_fee'} = $opt{'amount'};
2720 $pkg_opt_modified = 1;
2723 if ( exists($opt{'setup_cost'})
2724 and $part_pkg->setup_cost != $opt{'setup_cost'}
2725 and $opt{'setup_cost'} > 0 ) {
2727 $part_pkg->set('setup_cost', $opt{'setup_cost'});
2730 if ( exists($opt{'quantity'})
2731 and $opt{'quantity'} != $self->quantity
2732 and $opt{'quantity'} > 0 ) {
2734 $self->set('quantity', $opt{'quantity'});
2737 if ( exists($opt{'start_date'})
2738 and $opt{'start_date'} != $self->start_date ) {
2740 $self->set('start_date', $opt{'start_date'});
2743 if ( exists($opt{'separate_bill'})
2744 and $opt{'separate_bill'} ne $self->separate_bill ) {
2746 $self->set('separate_bill', $opt{'separate_bill'});
2750 } # else simply ignore them; the UI shouldn't allow editing the fields
2753 if ( exists($opt{'taxclass'})
2754 and $part_pkg->taxclass ne $opt{'taxclass'}) {
2756 $part_pkg->set('taxclass', $opt{'taxclass'});
2760 if ( $part_pkg->modified or $pkg_opt_modified ) {
2761 # can we safely modify the package def?
2762 # Yes, if it's not available for purchase, and this is the only instance
2764 if ( $part_pkg->disabled
2765 and FS::cust_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 1
2766 and FS::quotation_pkg->count('pkgpart = '.$part_pkg->pkgpart) == 0
2768 $error = $part_pkg->replace( options => \%pkg_opt );
2771 $part_pkg = $part_pkg->clone;
2772 $part_pkg->set('disabled' => 'Y');
2773 $error = $part_pkg->insert( options => \%pkg_opt );
2774 # and associate this as yet-unbilled package to the new package def
2775 $self->set('pkgpart' => $part_pkg->pkgpart);
2778 $dbh->rollback if $oldAutoCommit;
2783 if ($self->modified) { # for quantity or start_date change, or if we had
2784 # to clone the existing package def
2785 my $error = $self->replace;
2786 return $error if $error;
2788 if (defined $old_classnum) {
2789 # fix invoice grouping records
2790 my $old_catname = $old_classnum
2791 ? FS::pkg_class->by_key($old_classnum)->categoryname
2793 my $new_catname = $opt{'classnum'}
2794 ? $part_pkg->pkg_class->categoryname
2796 if ( $old_catname ne $new_catname ) {
2797 foreach my $cust_bill_pkg ($self->cust_bill_pkg) {
2798 # (there should only be one...)
2799 my @display = qsearch( 'cust_bill_pkg_display', {
2800 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2801 'section' => $old_catname,
2803 foreach (@display) {
2804 $_->set('section', $new_catname);
2805 $error = $_->replace;
2807 $dbh->rollback if $oldAutoCommit;
2811 } # foreach $cust_bill_pkg
2814 if ( $opt{'adjust_commission'} ) {
2815 # fix commission credits...tricky.
2816 foreach my $cust_event ($self->cust_event) {
2817 my $part_event = $cust_event->part_event;
2818 foreach my $table (qw(sales agent)) {
2820 "FS::part_event::Action::Mixin::credit_${table}_pkg_class";
2821 my $credit = qsearchs('cust_credit', {
2822 'eventnum' => $cust_event->eventnum,
2824 if ( $part_event->isa($class) ) {
2825 # Yes, this results in current commission rates being applied
2826 # retroactively to a one-time charge. For accounting purposes
2827 # there ought to be some kind of time limit on doing this.
2828 my $amount = $part_event->_calc_credit($self);
2829 if ( $credit and $credit->amount ne $amount ) {
2830 # Void the old credit.
2831 $error = $credit->void('Package class changed');
2833 $dbh->rollback if $oldAutoCommit;
2834 return "$error (adjusting commission credit)";
2837 # redo the event action to recreate the credit.
2839 eval { $part_event->do_action( $self, $cust_event ) };
2841 $dbh->rollback if $oldAutoCommit;
2844 } # if $part_event->isa($class)
2846 } # foreach $cust_event
2847 } # if $opt{'adjust_commission'}
2848 } # if defined $old_classnum
2850 $dbh->commit if $oldAutoCommit;
2857 sub process_bulk_cust_pkg {
2860 warn Dumper($param) if $DEBUG;
2862 my $old_part_pkg = qsearchs('part_pkg',
2863 { pkgpart => $param->{'old_pkgpart'} });
2864 my $new_part_pkg = qsearchs('part_pkg',
2865 { pkgpart => $param->{'new_pkgpart'} });
2866 die "Must select a new package type\n" unless $new_part_pkg;
2867 #my $keep_dates = $param->{'keep_dates'} || 0;
2868 my $keep_dates = 1; # there is no good reason to turn this off
2870 my $oldAutoCommit = $FS::UID::AutoCommit;
2871 local $FS::UID::AutoCommit = 0;
2874 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
2877 foreach my $old_cust_pkg ( @cust_pkgs ) {
2879 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
2880 if ( $old_cust_pkg->getfield('cancel') ) {
2881 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
2882 $old_cust_pkg->pkgnum."\n"
2886 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
2888 my $error = $old_cust_pkg->change(
2889 'pkgpart' => $param->{'new_pkgpart'},
2890 'keep_dates' => $keep_dates
2892 if ( !ref($error) ) { # change returns the cust_pkg on success
2894 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
2897 $dbh->commit if $oldAutoCommit;
2903 Returns the last bill date, or if there is no last bill date, the setup date.
2904 Useful for billing metered services.
2910 return $self->setfield('last_bill', $_[0]) if @_;
2911 return $self->getfield('last_bill') if $self->getfield('last_bill');
2912 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
2913 'edate' => $self->bill, } );
2914 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
2917 =item last_cust_pkg_reason ACTION
2919 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
2920 Returns false if there is no reason or the package is not currenly ACTION'd
2921 ACTION is one of adjourn, susp, cancel, or expire.
2925 sub last_cust_pkg_reason {
2926 my ( $self, $action ) = ( shift, shift );
2927 my $date = $self->get($action);
2929 'table' => 'cust_pkg_reason',
2930 'hashref' => { 'pkgnum' => $self->pkgnum,
2931 'action' => substr(uc($action), 0, 1),
2934 'order_by' => 'ORDER BY num DESC LIMIT 1',
2938 =item last_reason ACTION
2940 Returns the most recent ACTION FS::reason associated with the package.
2941 Returns false if there is no reason or the package is not currenly ACTION'd
2942 ACTION is one of adjourn, susp, cancel, or expire.
2947 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
2948 $cust_pkg_reason->reason
2949 if $cust_pkg_reason;
2954 Returns the definition for this billing item, as an FS::part_pkg object (see
2961 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
2962 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
2963 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
2968 Returns the cancelled package this package was changed from, if any.
2974 return '' unless $self->change_pkgnum;
2975 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
2978 =item change_cust_main
2980 Returns the customter this package was detached to, if any.
2984 sub change_cust_main {
2986 return '' unless $self->change_custnum;
2987 qsearchs('cust_main', { 'custnum' => $self->change_custnum } );
2992 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
2999 $self->part_pkg->calc_setup($self, @_);
3004 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
3011 $self->part_pkg->calc_recur($self, @_);
3016 Calls the I<base_setup> of the FS::part_pkg object associated with this billing
3023 $self->part_pkg->base_setup($self, @_);
3028 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
3035 $self->part_pkg->base_recur($self, @_);
3040 Calls the I<calc_remain> of the FS::part_pkg object associated with this
3047 $self->part_pkg->calc_remain($self, @_);
3052 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
3059 $self->part_pkg->calc_cancel($self, @_);
3064 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
3070 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
3073 =item cust_pkg_detail [ DETAILTYPE ]
3075 Returns any customer package details for this package (see
3076 L<FS::cust_pkg_detail>).
3078 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
3082 sub cust_pkg_detail {
3084 my %hash = ( 'pkgnum' => $self->pkgnum );
3085 $hash{detailtype} = shift if @_;
3087 'table' => 'cust_pkg_detail',
3088 'hashref' => \%hash,
3089 'order_by' => 'ORDER BY weight, pkgdetailnum',
3093 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
3095 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
3097 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
3099 If there is an error, returns the error, otherwise returns false.
3103 sub set_cust_pkg_detail {
3104 my( $self, $detailtype, @details ) = @_;
3106 my $oldAutoCommit = $FS::UID::AutoCommit;
3107 local $FS::UID::AutoCommit = 0;
3110 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
3111 my $error = $current->delete;
3113 $dbh->rollback if $oldAutoCommit;
3114 return "error removing old detail: $error";
3118 foreach my $detail ( @details ) {
3119 my $cust_pkg_detail = new FS::cust_pkg_detail {
3120 'pkgnum' => $self->pkgnum,
3121 'detailtype' => $detailtype,
3122 'detail' => $detail,
3124 my $error = $cust_pkg_detail->insert;
3126 $dbh->rollback if $oldAutoCommit;
3127 return "error adding new detail: $error";
3132 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3139 Returns the customer billing events (see L<FS::cust_event>) for this invoice.
3143 #false laziness w/cust_bill.pm
3147 'table' => 'cust_event',
3148 'addl_from' => 'JOIN part_event USING ( eventpart )',
3149 'hashref' => { 'tablenum' => $self->pkgnum },
3150 'extra_sql' => " AND eventtable = 'cust_pkg' ",
3154 =item num_cust_event
3156 Returns the number of customer billing events (see L<FS::cust_event>) for this package.
3160 #false laziness w/cust_bill.pm
3161 sub num_cust_event {
3163 my $sql = "SELECT COUNT(*) ". $self->_from_cust_event_where;
3164 $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref->[0];
3167 =item exists_cust_event
3169 Returns true if there are customer billing events (see L<FS::cust_event>) for this package. More efficient than using num_cust_event.
3173 sub exists_cust_event {
3175 my $sql = "SELECT 1 ". $self->_from_cust_event_where. " LIMIT 1";
3176 my $row = $self->_prep_ex($sql, $self->pkgnum)->fetchrow_arrayref;
3177 $row ? $row->[0] : '';
3180 sub _from_cust_event_where {
3182 " FROM cust_event JOIN part_event USING ( eventpart ) ".
3183 " WHERE tablenum = ? AND eventtable = 'cust_pkg' ";
3187 my( $self, $sql, @args ) = @_;
3188 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
3189 $sth->execute(@args) or die $sth->errstr. " executing $sql";
3193 =item part_pkg_currency_option OPTIONNAME
3195 Returns a two item list consisting of the currency of this customer, if any,
3196 and a value for the provided option. If the customer has a currency, the value
3197 is the option value the given name and the currency (see
3198 L<FS::part_pkg_currency>). Otherwise, if the customer has no currency, is the
3199 regular option value for the given name (see L<FS::part_pkg_option>).
3203 sub part_pkg_currency_option {
3204 my( $self, $optionname ) = @_;
3205 my $part_pkg = $self->part_pkg;
3206 if ( my $currency = $self->cust_main->currency ) {
3207 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
3209 ('', $part_pkg->option($optionname) );
3213 =item cust_svc [ SVCPART ] (old, deprecated usage)
3215 =item cust_svc [ OPTION => VALUE ... ] (current usage)
3217 =item cust_svc_unsorted [ OPTION => VALUE ... ]
3219 Returns the services for this package, as FS::cust_svc objects (see
3220 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
3221 spcififed, returns only the matching services.
3223 As an optimization, use the cust_svc_unsorted version if you are not displaying
3230 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
3231 $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref(@_) );
3234 sub cust_svc_unsorted {
3236 @{ $self->cust_svc_unsorted_arrayref(@_) };
3239 sub cust_svc_unsorted_arrayref {
3242 return [] unless $self->num_cust_svc(@_);
3245 if ( @_ && $_[0] =~ /^\d+/ ) {
3246 $opt{svcpart} = shift;
3247 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
3254 'table' => 'cust_svc',
3255 'hashref' => { 'pkgnum' => $self->pkgnum },
3257 if ( $opt{svcpart} ) {
3258 $search{hashref}->{svcpart} = $opt{'svcpart'};
3260 if ( $opt{'svcdb'} ) {
3261 $search{addl_from} = ' LEFT JOIN part_svc USING ( svcpart ) ';
3262 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{'svcdb'} );
3265 [ qsearch(\%search) ];
3269 =item overlimit [ SVCPART ]
3271 Returns the services for this package which have exceeded their
3272 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
3273 is specified, return only the matching services.
3279 return () unless $self->num_cust_svc(@_);
3280 grep { $_->overlimit } $self->cust_svc(@_);
3283 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3285 Returns historical services for this package created before END TIMESTAMP and
3286 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
3287 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
3288 I<pkg_svc.hidden> flag will be omitted.
3294 warn "$me _h_cust_svc called on $self\n"
3297 my ($end, $start, $mode) = @_;
3299 local($FS::Record::qsearch_qualify_columns) = 0;
3301 my @cust_svc = $self->_sort_cust_svc(
3302 [ qsearch( 'h_cust_svc',
3303 { 'pkgnum' => $self->pkgnum, },
3304 FS::h_cust_svc->sql_h_search(@_),
3308 if ( defined($mode) && $mode eq 'I' ) {
3309 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
3310 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
3316 sub _sort_cust_svc {
3317 my( $self, $arrayref ) = @_;
3320 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
3322 my %pkg_svc = map { $_->svcpart => $_ }
3323 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
3328 my $pkg_svc = $pkg_svc{ $_->svcpart } || '';
3330 $pkg_svc ? $pkg_svc->primary_svc : '',
3331 $pkg_svc ? $pkg_svc->quantity : 0,
3338 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
3340 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
3342 Returns the number of services for this package. Available options are svcpart
3343 and svcdb. If either is spcififed, returns only the matching services.
3350 return $self->{'_num_cust_svc'}
3352 && exists($self->{'_num_cust_svc'})
3353 && $self->{'_num_cust_svc'} =~ /\d/;
3355 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
3359 if ( @_ && $_[0] =~ /^\d+/ ) {
3360 $opt{svcpart} = shift;
3361 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
3367 my $select = 'SELECT COUNT(*) FROM cust_svc ';
3368 my $where = ' WHERE pkgnum = ? ';
3369 my @param = ($self->pkgnum);
3371 if ( $opt{'svcpart'} ) {
3372 $where .= ' AND svcpart = ? ';
3373 push @param, $opt{'svcpart'};
3375 if ( $opt{'svcdb'} ) {
3376 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
3377 $where .= ' AND svcdb = ? ';
3378 push @param, $opt{'svcdb'};
3381 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
3382 $sth->execute(@param) or die $sth->errstr;
3383 $sth->fetchrow_arrayref->[0];
3386 =item available_part_svc
3388 Returns a list of FS::part_svc objects representing services included in this
3389 package but not yet provisioned. Each FS::part_svc object also has an extra
3390 field, I<num_avail>, which specifies the number of available services.
3394 sub available_part_svc {
3397 my $pkg_quantity = $self->quantity || 1;
3399 grep { $_->num_avail > 0 }
3401 my $part_svc = $_->part_svc;
3402 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
3403 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
3405 # more evil encapsulation breakage
3406 if($part_svc->{'Hash'}{'num_avail'} > 0) {
3407 my @exports = $part_svc->part_export_did;
3408 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
3413 $self->part_pkg->pkg_svc;
3416 =item part_svc [ OPTION => VALUE ... ]
3418 Returns a list of FS::part_svc objects representing provisioned and available
3419 services included in this package. Each FS::part_svc object also has the
3420 following extra fields:
3434 (services) - array reference containing the provisioned services, as cust_svc objects
3438 Accepts two options:
3442 =item summarize_size
3444 If true, will omit the extra cust_pkg_svc option for objects where num_cust_svc
3445 is this size or greater.
3447 =item hide_discontinued
3449 If true, will omit looking for services that are no longer avaialble in the
3457 #label -> ($cust_svc->label)[1]
3463 my $pkg_quantity = $self->quantity || 1;
3465 #XXX some sort of sort order besides numeric by svcpart...
3466 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
3468 my $part_svc = $pkg_svc->part_svc;
3469 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3470 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
3471 $part_svc->{'Hash'}{'num_avail'} =
3472 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
3473 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3474 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
3475 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
3476 && $num_cust_svc >= $opt{summarize_size};
3477 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
3479 } $self->part_pkg->pkg_svc;
3481 unless ( $opt{hide_discontinued} ) {
3483 push @part_svc, map {
3485 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
3486 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
3487 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
3488 $part_svc->{'Hash'}{'cust_pkg_svc'} =
3489 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
3491 } $self->extra_part_svc;
3498 =item extra_part_svc
3500 Returns a list of FS::part_svc objects corresponding to services in this
3501 package which are still provisioned but not (any longer) available in the
3506 sub extra_part_svc {
3509 my $pkgnum = $self->pkgnum;
3510 #my $pkgpart = $self->pkgpart;
3513 # 'table' => 'part_svc',
3516 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
3517 # WHERE pkg_svc.svcpart = part_svc.svcpart
3518 # AND pkg_svc.pkgpart = ?
3521 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
3522 # LEFT JOIN cust_pkg USING ( pkgnum )
3523 # WHERE cust_svc.svcpart = part_svc.svcpart
3526 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
3529 #seems to benchmark slightly faster... (or did?)
3531 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
3532 my $pkgparts = join(',', @pkgparts);
3535 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
3536 #MySQL doesn't grok DISINCT ON
3537 'select' => 'DISTINCT part_svc.*',
3538 'table' => 'part_svc',
3540 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
3541 AND pkg_svc.pkgpart IN ($pkgparts)
3544 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
3545 LEFT JOIN cust_pkg USING ( pkgnum )
3548 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
3549 'extra_param' => [ [$self->pkgnum=>'int'] ],
3555 Returns a short status string for this package, currently:
3561 =item not yet billed
3563 =item one-time charge
3578 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
3580 return 'cancelled' if $self->get('cancel');
3581 return 'on hold' if $self->susp && ! $self->setup;
3582 return 'suspended' if $self->susp;
3583 return 'not yet billed' unless $self->setup;
3584 return 'one-time charge' if $freq =~ /^(0|$)/;
3588 =item ucfirst_status
3590 Returns the status with the first character capitalized.
3594 sub ucfirst_status {
3595 ucfirst(shift->status);
3600 Class method that returns the list of possible status strings for packages
3601 (see L<the status method|/status>). For example:
3603 @statuses = FS::cust_pkg->statuses();
3607 tie my %statuscolor, 'Tie::IxHash',
3608 'on hold' => 'FF00F5', #brighter purple!
3609 'not yet billed' => '009999', #teal? cyan?
3610 'one-time charge' => '0000CC', #blue #'000000',
3611 'active' => '00CC00',
3612 'suspended' => 'FF9900',
3613 'cancelled' => 'FF0000',
3617 my $self = shift; #could be class...
3618 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
3619 # # mayble split btw one-time vs. recur
3630 Returns a hex triplet color string for this package's status.
3636 $statuscolor{$self->status};
3639 =item is_status_delay_cancel
3641 Returns true if part_pkg has option delay_cancel,
3642 cust_pkg status is 'suspended' and expire is set
3643 to cancel package within the next day (or however
3644 many days are set in global config part_pkg-delay_cancel-days.
3646 Accepts option I<part_pkg-delay_cancel-days> which should be
3647 the value of the config setting, to avoid looking it up again.
3649 This is not a real status, this only meant for hacking display
3650 values, because otherwise treating the package as suspended is
3651 really the whole point of the delay_cancel option.
3655 sub is_status_delay_cancel {
3656 my ($self,%opt) = @_;
3657 if ( $self->main_pkgnum and $self->pkglinknum ) {
3658 return $self->main_pkg->is_status_delay_cancel;
3660 return 0 unless $self->part_pkg->option('delay_cancel',1);
3661 return 0 unless $self->status eq 'suspended';
3662 return 0 unless $self->expire;
3663 my $expdays = $opt{'part_pkg-delay_cancel-days'};
3665 my $conf = new FS::Conf;
3666 $expdays = $conf->config('part_pkg-delay_cancel-days') || 1;
3668 my $expsecs = 60*60*24*$expdays;
3669 return 0 unless $self->expire < time + $expsecs;
3675 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
3676 "pkg - comment" depending on user preference).
3682 my $label = $self->part_pkg->pkg_comment( cust_pkg=>$self, nopkgpart=>1 );
3683 $label = $self->pkgnum. ": $label"
3684 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
3688 =item pkg_label_long
3690 Returns a long label for this package, adding the primary service's label to
3695 sub pkg_label_long {
3697 my $label = $self->pkg_label;
3698 my $cust_svc = $self->primary_cust_svc;
3699 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
3705 Returns a customer-localized label for this package.
3711 $self->part_pkg->pkg_locale( $self->cust_main->locale );
3714 =item primary_cust_svc
3716 Returns a primary service (as FS::cust_svc object) if one can be identified.
3720 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
3722 sub primary_cust_svc {
3725 my @cust_svc = $self->cust_svc;
3727 return '' unless @cust_svc; #no serivces - irrelevant then
3729 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
3731 # primary service as specified in the package definition
3732 # or exactly one service definition with quantity one
3733 my $svcpart = $self->part_pkg->svcpart;
3734 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
3735 return $cust_svc[0] if scalar(@cust_svc) == 1;
3737 #couldn't identify one thing..
3743 Returns a list of lists, calling the label method for all services
3744 (see L<FS::cust_svc>) of this billing item.
3750 map { [ $_->label ] } $self->cust_svc;
3753 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3755 Like the labels method, but returns historical information on services that
3756 were active as of END_TIMESTAMP and (optionally) not cancelled before
3757 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
3758 I<pkg_svc.hidden> flag will be omitted.
3760 Returns a list of lists, calling the label method for all (historical) services
3761 (see L<FS::h_cust_svc>) of this billing item.
3767 warn "$me _h_labels called on $self\n"
3769 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
3774 Like labels, except returns a simple flat list, and shortens long
3775 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3776 identical services to one line that lists the service label and the number of
3777 individual services rather than individual items.
3782 shift->_labels_short( 'labels', @_ );
3785 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
3787 Like h_labels, except returns a simple flat list, and shortens long
3788 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3789 identical services to one line that lists the service label and the number of
3790 individual services rather than individual items.
3794 sub h_labels_short {
3795 shift->_labels_short( 'h_labels', @_ );
3799 my( $self, $method ) = ( shift, shift );
3801 warn "$me _labels_short called on $self with $method method\n"
3804 my $conf = new FS::Conf;
3805 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
3807 warn "$me _labels_short populating \%labels\n"
3811 #tie %labels, 'Tie::IxHash';
3812 push @{ $labels{$_->[0]} }, $_->[1]
3813 foreach $self->$method(@_);
3815 warn "$me _labels_short populating \@labels\n"
3819 foreach my $label ( keys %labels ) {
3821 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
3822 my $num = scalar(@values);
3823 warn "$me _labels_short $num items for $label\n"
3826 if ( $num > $max_same_services ) {
3827 warn "$me _labels_short more than $max_same_services, so summarizing\n"
3829 push @labels, "$label ($num)";
3831 if ( $conf->exists('cust_bill-consolidate_services') ) {
3832 warn "$me _labels_short consolidating services\n"
3834 # push @labels, "$label: ". join(', ', @values);
3836 my $detail = "$label: ";
3837 $detail .= shift(@values). ', '
3839 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
3841 push @labels, $detail;
3843 warn "$me _labels_short done consolidating services\n"
3846 warn "$me _labels_short adding service data\n"
3848 push @labels, map { "$label: $_" } @values;
3859 Returns the parent customer object (see L<FS::cust_main>).
3863 Returns the balance for this specific package, when using
3864 experimental package balance.
3870 $self->cust_main->balance_pkgnum( $self->pkgnum );
3873 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
3877 Returns the location object, if any (see L<FS::cust_location>).
3879 =item cust_location_or_main
3881 If this package is associated with a location, returns the locaiton (see
3882 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
3884 =item location_label [ OPTION => VALUE ... ]
3886 Returns the label of the location object (see L<FS::cust_location>).
3890 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
3892 =item tax_locationnum
3894 Returns the foreign key to a L<FS::cust_location> object for calculating
3895 tax on this package, as determined by the C<tax-pkg_address> and
3896 C<tax-ship_address> configuration flags.
3900 sub tax_locationnum {
3902 my $conf = FS::Conf->new;
3903 if ( $conf->exists('tax-pkg_address') ) {
3904 return $self->locationnum;
3906 elsif ( $conf->exists('tax-ship_address') ) {
3907 return $self->cust_main->ship_locationnum;
3910 return $self->cust_main->bill_locationnum;
3916 Returns the L<FS::cust_location> object for tax_locationnum.
3922 my $conf = FS::Conf->new;
3923 if ( $conf->exists('tax-pkg_address') and $self->locationnum ) {
3924 return FS::cust_location->by_key($self->locationnum);
3926 elsif ( $conf->exists('tax-ship_address') ) {
3927 return $self->cust_main->ship_location;
3930 return $self->cust_main->bill_location;
3934 =item seconds_since TIMESTAMP
3936 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
3937 package have been online since TIMESTAMP, according to the session monitor.
3939 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
3940 L<Time::Local> and L<Date::Parse> for conversion functions.
3945 my($self, $since) = @_;
3948 foreach my $cust_svc (
3949 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
3951 $seconds += $cust_svc->seconds_since($since);
3958 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
3960 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
3961 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
3964 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3965 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3971 sub seconds_since_sqlradacct {
3972 my($self, $start, $end) = @_;
3976 foreach my $cust_svc (
3978 my $part_svc = $_->part_svc;
3979 $part_svc->svcdb eq 'svc_acct'
3980 && scalar($part_svc->part_export_usage);
3983 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
3990 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
3992 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
3993 in this package for sessions ending between TIMESTAMP_START (inclusive) and
3997 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3998 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
4003 sub attribute_since_sqlradacct {
4004 my($self, $start, $end, $attrib) = @_;
4008 foreach my $cust_svc (
4010 my $part_svc = $_->part_svc;
4011 scalar($part_svc->part_export_usage);
4014 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
4026 my( $self, $value ) = @_;
4027 if ( defined($value) ) {
4028 $self->setfield('quantity', $value);
4030 $self->getfield('quantity') || 1;
4033 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
4035 Transfers as many services as possible from this package to another package.
4037 The destination package can be specified by pkgnum by passing an FS::cust_pkg
4038 object. The destination package must already exist.
4040 Services are moved only if the destination allows services with the correct
4041 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
4042 this option with caution! No provision is made for export differences
4043 between the old and new service definitions. Probably only should be used
4044 when your exports for all service definitions of a given svcdb are identical.
4045 (attempt a transfer without it first, to move all possible svcpart-matching
4048 Any services that can't be moved remain in the original package.
4050 Returns an error, if there is one; otherwise, returns the number of services
4051 that couldn't be moved.
4056 my ($self, $dest_pkgnum, %opt) = @_;
4062 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
4063 $dest = $dest_pkgnum;
4064 $dest_pkgnum = $dest->pkgnum;
4066 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
4069 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
4071 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
4072 $target{$pkg_svc->svcpart} = $pkg_svc->quantity * ( $dest->quantity || 1 );
4075 foreach my $cust_svc ($dest->cust_svc) {
4076 $target{$cust_svc->svcpart}--;
4079 my %svcpart2svcparts = ();
4080 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
4081 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
4082 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
4083 next if exists $svcpart2svcparts{$svcpart};
4084 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
4085 $svcpart2svcparts{$svcpart} = [
4087 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
4089 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
4090 'svcpart' => $_ } );
4092 $pkg_svc ? $pkg_svc->primary_svc : '',
4093 $pkg_svc ? $pkg_svc->quantity : 0,
4097 grep { $_ != $svcpart }
4099 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
4101 warn "alternates for svcpart $svcpart: ".
4102 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
4108 foreach my $cust_svc ($self->cust_svc) {
4109 my $svcnum = $cust_svc->svcnum;
4110 if($target{$cust_svc->svcpart} > 0
4111 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
4112 $target{$cust_svc->svcpart}--;
4113 my $new = new FS::cust_svc { $cust_svc->hash };
4114 $new->pkgnum($dest_pkgnum);
4115 $error = $new->replace($cust_svc);
4116 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
4118 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
4119 warn "alternates to consider: ".
4120 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
4122 my @alternate = grep {
4123 warn "considering alternate svcpart $_: ".
4124 "$target{$_} available in new package\n"
4127 } @{$svcpart2svcparts{$cust_svc->svcpart}};
4129 warn "alternate(s) found\n" if $DEBUG;
4130 my $change_svcpart = $alternate[0];
4131 $target{$change_svcpart}--;
4132 my $new = new FS::cust_svc { $cust_svc->hash };
4133 $new->svcpart($change_svcpart);
4134 $new->pkgnum($dest_pkgnum);
4135 $error = $new->replace($cust_svc);
4143 my @label = $cust_svc->label;
4144 return "$label[0] $label[1]: $error";
4150 =item grab_svcnums SVCNUM, SVCNUM ...
4152 Change the pkgnum for the provided services to this packages. If there is an
4153 error, returns the error, otherwise returns false.
4161 my $oldAutoCommit = $FS::UID::AutoCommit;
4162 local $FS::UID::AutoCommit = 0;
4165 foreach my $svcnum (@svcnum) {
4166 my $cust_svc = qsearchs('cust_svc', { svcnum=>$svcnum } ) or do {
4167 $dbh->rollback if $oldAutoCommit;
4168 return "unknown svcnum $svcnum";
4170 $cust_svc->pkgnum( $self->pkgnum );
4171 my $error = $cust_svc->replace;
4173 $dbh->rollback if $oldAutoCommit;
4178 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4185 This method is deprecated. See the I<depend_jobnum> option to the insert and
4186 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
4190 #looks like this is still used by the order_pkg and change_pkg methods in
4191 # ClientAPI/MyAccount, need to look into those before removing
4195 my $oldAutoCommit = $FS::UID::AutoCommit;
4196 local $FS::UID::AutoCommit = 0;
4199 foreach my $cust_svc ( $self->cust_svc ) {
4200 #false laziness w/svc_Common::insert
4201 my $svc_x = $cust_svc->svc_x;
4202 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
4203 my $error = $part_export->export_insert($svc_x);
4205 $dbh->rollback if $oldAutoCommit;
4211 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4216 =item export_pkg_change OLD_CUST_PKG
4218 Calls the "pkg_change" export action for all services attached to this package.
4222 sub export_pkg_change {
4223 my( $self, $old ) = ( shift, shift );
4225 my $oldAutoCommit = $FS::UID::AutoCommit;
4226 local $FS::UID::AutoCommit = 0;
4229 foreach my $svc_x ( map $_->svc_x, $self->cust_svc ) {
4230 my $error = $svc_x->export('pkg_change', $self, $old);
4232 $dbh->rollback if $oldAutoCommit;
4237 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4244 Associates this package with a (suspension or cancellation) reason (see
4245 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
4248 Available options are:
4254 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.
4258 the access_user (see L<FS::access_user>) providing the reason
4266 the action (cancel, susp, adjourn, expire) associated with the reason
4270 If there is an error, returns the error, otherwise returns false.
4275 my ($self, %options) = @_;
4277 my $otaker = $options{reason_otaker} ||
4278 $FS::CurrentUser::CurrentUser->username;
4281 if ( $options{'reason'} =~ /^(\d+)$/ ) {
4285 } elsif ( ref($options{'reason'}) ) {
4287 return 'Enter a new reason (or select an existing one)'
4288 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
4290 my $reason = new FS::reason({
4291 'reason_type' => $options{'reason'}->{'typenum'},
4292 'reason' => $options{'reason'}->{'reason'},
4294 my $error = $reason->insert;
4295 return $error if $error;
4297 $reasonnum = $reason->reasonnum;
4300 return "Unparseable reason: ". $options{'reason'};
4303 my $cust_pkg_reason =
4304 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
4305 'reasonnum' => $reasonnum,
4306 'otaker' => $otaker,
4307 'action' => substr(uc($options{'action'}),0,1),
4308 'date' => $options{'date'}
4313 $cust_pkg_reason->insert;
4316 =item insert_discount
4318 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
4319 inserting a new discount on the fly (see L<FS::discount>).
4321 Available options are:
4329 If there is an error, returns the error, otherwise returns false.
4333 sub insert_discount {
4334 #my ($self, %options) = @_;
4337 my $cust_pkg_discount = new FS::cust_pkg_discount {
4338 'pkgnum' => $self->pkgnum,
4339 'discountnum' => $self->discountnum,
4341 'end_date' => '', #XXX
4342 #for the create a new discount case
4343 '_type' => $self->discountnum__type,
4344 'amount' => $self->discountnum_amount,
4345 'percent' => $self->discountnum_percent,
4346 'months' => $self->discountnum_months,
4347 'setup' => $self->discountnum_setup,
4348 #'disabled' => $self->discountnum_disabled,
4351 $cust_pkg_discount->insert;
4354 =item set_usage USAGE_VALUE_HASHREF
4356 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4357 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4358 upbytes, downbytes, and totalbytes are appropriate keys.
4360 All svc_accts which are part of this package have their values reset.
4365 my ($self, $valueref, %opt) = @_;
4367 #only svc_acct can set_usage for now
4368 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4369 my $svc_x = $cust_svc->svc_x;
4370 $svc_x->set_usage($valueref, %opt)
4371 if $svc_x->can("set_usage");
4375 =item recharge USAGE_VALUE_HASHREF
4377 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
4378 to which they should be set (see L<FS::svc_acct>). Currently seconds,
4379 upbytes, downbytes, and totalbytes are appropriate keys.
4381 All svc_accts which are part of this package have their values incremented.
4386 my ($self, $valueref) = @_;
4388 #only svc_acct can set_usage for now
4389 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
4390 my $svc_x = $cust_svc->svc_x;
4391 $svc_x->recharge($valueref)
4392 if $svc_x->can("recharge");
4396 =item apply_usageprice
4400 sub apply_usageprice {
4403 my $oldAutoCommit = $FS::UID::AutoCommit;
4404 local $FS::UID::AutoCommit = 0;
4409 foreach my $cust_pkg_usageprice ( $self->cust_pkg_usageprice ) {
4410 $error ||= $cust_pkg_usageprice->apply;
4414 $dbh->rollback if $oldAutoCommit;
4415 die "error applying part_pkg_usageprice add-ons, pkgnum ". $self->pkgnum.
4418 $dbh->commit if $oldAutoCommit;
4424 =item cust_pkg_discount
4426 =item cust_pkg_discount_active
4430 sub cust_pkg_discount_active {
4432 grep { $_->status eq 'active' } $self->cust_pkg_discount;
4435 =item cust_pkg_usage
4437 Returns a list of all voice usage counters attached to this package.
4439 =item apply_usage OPTIONS
4441 Takes the following options:
4442 - cdr: a call detail record (L<FS::cdr>)
4443 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
4444 - minutes: the maximum number of minutes to be charged
4446 Finds available usage minutes for a call of this class, and subtracts
4447 up to that many minutes from the usage pool. If the usage pool is empty,
4448 and the C<cdr-minutes_priority> global config option is set, minutes may
4449 be taken from other calls as well. Either way, an allocation record will
4450 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
4451 number of minutes of usage applied to the call.
4456 my ($self, %opt) = @_;
4457 my $cdr = $opt{cdr};
4458 my $rate_detail = $opt{rate_detail};
4459 my $minutes = $opt{minutes};
4460 my $classnum = $rate_detail->classnum;
4461 my $pkgnum = $self->pkgnum;
4462 my $custnum = $self->custnum;
4464 my $oldAutoCommit = $FS::UID::AutoCommit;
4465 local $FS::UID::AutoCommit = 0;
4468 my $order = FS::Conf->new->config('cdr-minutes_priority');
4472 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
4474 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
4476 my @usage_recs = qsearch({
4477 'table' => 'cust_pkg_usage',
4478 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
4479 ' JOIN cust_pkg USING (pkgnum)'.
4480 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
4481 'select' => 'cust_pkg_usage.*',
4482 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
4483 " ( cust_pkg.custnum = $custnum AND ".
4484 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4485 $is_classnum . ' AND '.
4486 " cust_pkg_usage.minutes > 0",
4487 'order_by' => " ORDER BY priority ASC",
4490 my $orig_minutes = $minutes;
4492 while (!$error and $minutes > 0 and @usage_recs) {
4493 my $cust_pkg_usage = shift @usage_recs;
4494 $cust_pkg_usage->select_for_update;
4495 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
4496 pkgusagenum => $cust_pkg_usage->pkgusagenum,
4497 acctid => $cdr->acctid,
4498 minutes => min($cust_pkg_usage->minutes, $minutes),
4500 $cust_pkg_usage->set('minutes',
4501 $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes
4503 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
4504 $minutes -= $cdr_cust_pkg_usage->minutes;
4506 if ( $order and $minutes > 0 and !$error ) {
4507 # then try to steal minutes from another call
4509 'table' => 'cdr_cust_pkg_usage',
4510 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
4511 ' JOIN part_pkg_usage USING (pkgusagepart)'.
4512 ' JOIN cust_pkg USING (pkgnum)'.
4513 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
4514 ' JOIN cdr USING (acctid)',
4515 'select' => 'cdr_cust_pkg_usage.*',
4516 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
4517 " ( cust_pkg.pkgnum = $pkgnum OR ".
4518 " ( cust_pkg.custnum = $custnum AND ".
4519 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
4520 " part_pkg_usage_class.classnum = $classnum",
4521 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
4523 if ( $order eq 'time' ) {
4524 # find CDRs that are using minutes, but have a later startdate
4526 my $startdate = $cdr->startdate;
4527 if ($startdate !~ /^\d+$/) {
4528 die "bad cdr startdate '$startdate'";
4530 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
4531 # minimize needless reshuffling
4532 $search{'order_by'} .= ', cdr.startdate DESC';
4534 # XXX may not work correctly with rate_time schedules. Could
4535 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
4537 $search{'addl_from'} .=
4538 ' JOIN rate_detail'.
4539 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
4540 if ( $order eq 'rate_high' ) {
4541 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
4542 $rate_detail->min_charge;
4543 $search{'order_by'} .= ', rate_detail.min_charge ASC';
4544 } elsif ( $order eq 'rate_low' ) {
4545 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
4546 $rate_detail->min_charge;
4547 $search{'order_by'} .= ', rate_detail.min_charge DESC';
4549 # this should really never happen
4550 die "invalid cdr-minutes_priority value '$order'\n";
4553 my @cdr_usage_recs = qsearch(\%search);
4555 while (!$error and @cdr_usage_recs and $minutes > 0) {
4556 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
4557 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
4558 my $old_cdr = $cdr_cust_pkg_usage->cdr;
4559 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
4560 $cdr_cust_pkg_usage->select_for_update;
4561 $old_cdr->select_for_update;
4562 $cust_pkg_usage->select_for_update;
4563 # in case someone else stole the usage from this CDR
4564 # while waiting for the lock...
4565 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
4566 # steal the usage allocation and flag the old CDR for reprocessing
4567 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
4568 # if the allocation is more minutes than we need, adjust it...
4569 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
4571 $cdr_cust_pkg_usage->set('minutes', $minutes);
4572 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
4573 $error = $cust_pkg_usage->replace;
4575 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
4576 $error ||= $cdr_cust_pkg_usage->replace;
4577 # deduct the stolen minutes
4578 $minutes -= $cdr_cust_pkg_usage->minutes;
4580 # after all minute-stealing is done, reset the affected CDRs
4581 foreach (values %reproc_cdrs) {
4582 $error ||= $_->set_status('');
4583 # XXX or should we just call $cdr->rate right here?
4584 # it's not like we can create a loop this way, since the min_charge
4585 # or call time has to go monotonically in one direction.
4586 # we COULD get some very deep recursions going, though...
4588 } # if $order and $minutes
4591 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
4593 $dbh->commit if $oldAutoCommit;
4594 return $orig_minutes - $minutes;
4598 =item supplemental_pkgs
4600 Returns a list of all packages supplemental to this one.
4604 sub supplemental_pkgs {
4606 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
4611 Returns the package that this one is supplemental to, if any.
4617 if ( $self->main_pkgnum ) {
4618 return FS::cust_pkg->by_key($self->main_pkgnum);
4625 =head1 CLASS METHODS
4631 Returns an SQL expression identifying recurring packages.
4635 sub recurring_sql { "
4636 '0' != ( select freq from part_pkg
4637 where cust_pkg.pkgpart = part_pkg.pkgpart )
4642 Returns an SQL expression identifying one-time packages.
4647 '0' = ( select freq from part_pkg
4648 where cust_pkg.pkgpart = part_pkg.pkgpart )
4653 Returns an SQL expression identifying ordered packages (recurring packages not
4659 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
4664 Returns an SQL expression identifying active packages.
4669 $_[0]->recurring_sql. "
4670 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4671 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4672 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4675 =item not_yet_billed_sql
4677 Returns an SQL expression identifying packages which have not yet been billed.
4681 sub not_yet_billed_sql { "
4682 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4683 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4684 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4689 Returns an SQL expression identifying inactive packages (one-time packages
4690 that are otherwise unsuspended/uncancelled).
4694 sub inactive_sql { "
4695 ". $_[0]->onetime_sql(). "
4696 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4697 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4698 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4703 Returns an SQL expression identifying on-hold packages.
4708 #$_[0]->recurring_sql(). ' AND '.
4710 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4711 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4712 AND ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4719 Returns an SQL expression identifying suspended packages.
4723 sub suspended_sql { susp_sql(@_); }
4725 #$_[0]->recurring_sql(). ' AND '.
4727 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4728 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4729 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4736 Returns an SQL exprression identifying cancelled packages.
4740 sub cancelled_sql { cancel_sql(@_); }
4742 #$_[0]->recurring_sql(). ' AND '.
4743 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
4748 Returns an SQL expression to give the package status as a string.
4754 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
4755 WHEN ( cust_pkg.susp IS NOT NULL AND cust_pkg.setup IS NULL ) THEN 'on hold'
4756 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
4757 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
4758 WHEN ".onetime_sql()." THEN 'one-time charge'
4765 Returns a list of two package counts. The first is a count of packages
4766 based on the supplied criteria and the second is the count of residential
4767 packages with those same criteria. Criteria are specified as in the search
4773 my ($class, $params) = @_;
4775 my $sql_query = $class->search( $params );
4777 my $count_sql = delete($sql_query->{'count_query'});
4778 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
4779 or die "couldn't parse count_sql";
4781 my $count_sth = dbh->prepare($count_sql)
4782 or die "Error preparing $count_sql: ". dbh->errstr;
4784 or die "Error executing $count_sql: ". $count_sth->errstr;
4785 my $count_arrayref = $count_sth->fetchrow_arrayref;
4787 return ( @$count_arrayref );
4791 =item tax_locationnum_sql
4793 Returns an SQL expression for the tax location for a package, based
4794 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
4798 sub tax_locationnum_sql {
4799 my $conf = FS::Conf->new;
4800 if ( $conf->exists('tax-pkg_address') ) {
4801 'cust_pkg.locationnum';
4803 elsif ( $conf->exists('tax-ship_address') ) {
4804 'cust_main.ship_locationnum';
4807 'cust_main.bill_locationnum';
4813 Returns a list: the first item is an SQL fragment identifying matching
4814 packages/customers via location (taking into account shipping and package
4815 address taxation, if enabled), and subsequent items are the parameters to
4816 substitute for the placeholders in that fragment.
4821 my($class, %opt) = @_;
4822 my $ornull = $opt{'ornull'};
4824 my $conf = new FS::Conf;
4826 # '?' placeholders in _location_sql_where
4827 my $x = $ornull ? 3 : 2;
4838 if ( $conf->exists('tax-ship_address') ) {
4841 ( ( ship_last IS NULL OR ship_last = '' )
4842 AND ". _location_sql_where('cust_main', '', $ornull ). "
4844 OR ( ship_last IS NOT NULL AND ship_last != ''
4845 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
4848 # AND payby != 'COMP'
4850 @main_param = ( @bill_param, @bill_param );
4854 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
4855 @main_param = @bill_param;
4861 if ( $conf->exists('tax-pkg_address') ) {
4863 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
4866 ( cust_pkg.locationnum IS NULL AND $main_where )
4867 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
4870 @param = ( @main_param, @bill_param );
4874 $where = $main_where;
4875 @param = @main_param;
4883 #subroutine, helper for location_sql
4884 sub _location_sql_where {
4886 my $prefix = @_ ? shift : '';
4887 my $ornull = @_ ? shift : '';
4889 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
4891 $ornull = $ornull ? ' OR ? IS NULL ' : '';
4893 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
4894 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
4895 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
4897 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
4899 # ( $table.${prefix}city = ? $or_empty_city $ornull )
4901 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4902 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4903 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
4904 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
4905 AND $table.${prefix}country = ?
4910 my( $self, $what ) = @_;
4912 my $what_show_zero = $what. '_show_zero';
4913 length($self->$what_show_zero())
4914 ? ($self->$what_show_zero() eq 'Y')
4915 : $self->part_pkg->$what_show_zero();
4922 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
4924 Bulk cancel + order subroutine. Perhaps slightly deprecated, only used by the
4925 bulk cancel+order in the web UI and nowhere else (edit/process/cust_pkg.cgi)
4927 CUSTNUM is a customer (see L<FS::cust_main>)
4929 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4930 L<FS::part_pkg>) to order for this customer. Duplicates are of course
4933 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
4934 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
4935 new billing items. An error is returned if this is not possible (see
4936 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
4939 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4940 newly-created cust_pkg objects.
4942 REFNUM, if specified, will specify the FS::pkg_referral record to be created
4943 and inserted. Multiple FS::pkg_referral records can be created by
4944 setting I<refnum> to an array reference of refnums or a hash reference with
4945 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
4946 record will be created corresponding to cust_main.refnum.
4951 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
4953 my $conf = new FS::Conf;
4955 # Transactionize this whole mess
4956 my $oldAutoCommit = $FS::UID::AutoCommit;
4957 local $FS::UID::AutoCommit = 0;
4961 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
4962 # return "Customer not found: $custnum" unless $cust_main;
4964 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
4967 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4970 my $change = scalar(@old_cust_pkg) != 0;
4973 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
4975 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
4976 " to pkgpart ". $pkgparts->[0]. "\n"
4979 my $err_or_cust_pkg =
4980 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
4981 'refnum' => $refnum,
4984 unless (ref($err_or_cust_pkg)) {
4985 $dbh->rollback if $oldAutoCommit;
4986 return $err_or_cust_pkg;
4989 push @$return_cust_pkg, $err_or_cust_pkg;
4990 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4995 # Create the new packages.
4996 foreach my $pkgpart (@$pkgparts) {
4998 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
5000 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
5001 pkgpart => $pkgpart,
5005 $error = $cust_pkg->insert( 'change' => $change );
5006 push @$return_cust_pkg, $cust_pkg;
5008 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
5009 my $supp_pkg = FS::cust_pkg->new({
5010 custnum => $custnum,
5011 pkgpart => $link->dst_pkgpart,
5013 main_pkgnum => $cust_pkg->pkgnum,
5016 $error ||= $supp_pkg->insert( 'change' => $change );
5017 push @$return_cust_pkg, $supp_pkg;
5021 $dbh->rollback if $oldAutoCommit;
5026 # $return_cust_pkg now contains refs to all of the newly
5029 # Transfer services and cancel old packages.
5030 foreach my $old_pkg (@old_cust_pkg) {
5032 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
5035 foreach my $new_pkg (@$return_cust_pkg) {
5036 $error = $old_pkg->transfer($new_pkg);
5037 if ($error and $error == 0) {
5038 # $old_pkg->transfer failed.
5039 $dbh->rollback if $oldAutoCommit;
5044 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
5045 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
5046 foreach my $new_pkg (@$return_cust_pkg) {
5047 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
5048 if ($error and $error == 0) {
5049 # $old_pkg->transfer failed.
5050 $dbh->rollback if $oldAutoCommit;
5057 # Transfers were successful, but we went through all of the
5058 # new packages and still had services left on the old package.
5059 # We can't cancel the package under the circumstances, so abort.
5060 $dbh->rollback if $oldAutoCommit;
5061 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
5063 $error = $old_pkg->cancel( quiet=>1, 'no_delay_cancel'=>1 );
5069 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5073 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
5075 A bulk change method to change packages for multiple customers.
5077 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
5078 L<FS::part_pkg>) to order for each customer. Duplicates are of course
5081 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
5082 replace. The services (see L<FS::cust_svc>) are moved to the
5083 new billing items. An error is returned if this is not possible (see
5086 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
5087 newly-created cust_pkg objects.
5092 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
5094 # Transactionize this whole mess
5095 my $oldAutoCommit = $FS::UID::AutoCommit;
5096 local $FS::UID::AutoCommit = 0;
5100 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
5103 while(scalar(@old_cust_pkg)) {
5105 my $custnum = $old_cust_pkg[0]->custnum;
5106 my (@remove) = map { $_->pkgnum }
5107 grep { $_->custnum == $custnum } @old_cust_pkg;
5108 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
5110 my $error = order $custnum, $pkgparts, \@remove, \@return;
5112 push @errors, $error
5114 push @$return_cust_pkg, @return;
5117 if (scalar(@errors)) {
5118 $dbh->rollback if $oldAutoCommit;
5119 return join(' / ', @errors);
5122 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5126 =item forward_emails
5128 Returns a hash of svcnums and corresponding email addresses
5129 for svc_acct services that can be used as source or dest
5130 for svc_forward services provisioned in this package.
5132 Accepts options I<svc_forward> OR I<svcnum> for a svc_forward
5133 service; if included, will ensure the current values of the
5134 specified service are included in the list, even if for some
5135 other reason they wouldn't be. If called as a class method
5136 with a specified service, returns only these current values.
5138 Caution: does not actually check if svc_forward services are
5139 available to be provisioned on this package.
5143 sub forward_emails {
5147 #load optional service, thoroughly validated
5148 die "Use svcnum or svc_forward, not both"
5149 if $opt{'svcnum'} && $opt{'svc_forward'};
5150 my $svc_forward = $opt{'svc_forward'};
5151 $svc_forward ||= qsearchs('svc_forward',{ 'svcnum' => $opt{'svcnum'} })
5153 die "Specified service is not a forward service"
5154 if $svc_forward && (ref($svc_forward) ne 'FS::svc_forward');
5155 die "Specified service not found"
5156 if ($opt{'svcnum'} || $opt{'svc_forward'}) && !$svc_forward;
5160 ## everything below was basically copied from httemplate/edit/svc_forward.cgi
5161 ## with minimal refactoring, not sure why we can't just load all svc_accts for this custnum
5163 #add current values from specified service, if there was one
5165 foreach my $method (qw( srcsvc_acct dstsvc_acct )) {
5166 my $svc_acct = $svc_forward->$method();
5167 $email{$svc_acct->svcnum} = $svc_acct->email if $svc_acct;
5171 if (ref($self) eq 'FS::cust_pkg') {
5173 #and including the rest for this customer
5174 my($u_part_svc,@u_acct_svcparts);
5175 foreach $u_part_svc ( qsearch('part_svc',{'svcdb'=>'svc_acct'}) ) {
5176 push @u_acct_svcparts,$u_part_svc->getfield('svcpart');
5179 my $custnum = $self->getfield('custnum');
5180 foreach my $i_cust_pkg ( qsearch('cust_pkg',{'custnum'=>$custnum}) ) {
5181 my $cust_pkgnum = $i_cust_pkg->getfield('pkgnum');
5182 #now find the corresponding record(s) in cust_svc (for this pkgnum!)
5183 foreach my $acct_svcpart (@u_acct_svcparts) {
5184 foreach my $i_cust_svc (
5185 qsearch( 'cust_svc', { 'pkgnum' => $cust_pkgnum,
5186 'svcpart' => $acct_svcpart } )
5188 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $i_cust_svc->svcnum } );
5189 $email{$svc_acct->svcnum} = $svc_acct->email;
5198 # Used by FS::Upgrade to migrate to a new database.
5199 sub _upgrade_data { # class method
5200 my ($class, %opts) = @_;
5201 $class->_upgrade_otaker(%opts);
5203 # RT#10139, bug resulting in contract_end being set when it shouldn't
5204 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
5205 # RT#10830, bad calculation of prorate date near end of year
5206 # the date range for bill is December 2009, and we move it forward
5207 # one year if it's before the previous bill date (which it should
5209 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
5210 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
5211 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
5212 # RT6628, add order_date to cust_pkg
5213 'update cust_pkg set order_date = (select history_date from h_cust_pkg
5214 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
5215 history_action = \'insert\') where order_date is null',
5217 foreach my $sql (@statements) {
5218 my $sth = dbh->prepare($sql);
5219 $sth->execute or die $sth->errstr;
5222 # RT31194: supplemental package links that are deleted don't clean up
5224 my @pkglinknums = qsearch({
5225 'select' => 'DISTINCT cust_pkg.pkglinknum',
5226 'table' => 'cust_pkg',
5227 'addl_from' => ' LEFT JOIN part_pkg_link USING (pkglinknum) ',
5228 'extra_sql' => ' WHERE cust_pkg.pkglinknum IS NOT NULL
5229 AND part_pkg_link.pkglinknum IS NULL',
5231 foreach (@pkglinknums) {
5232 my $pkglinknum = $_->pkglinknum;
5233 warn "cleaning part_pkg_link #$pkglinknum\n";
5234 my $part_pkg_link = FS::part_pkg_link->new({pkglinknum => $pkglinknum});
5235 my $error = $part_pkg_link->remove_linked;
5236 die $error if $error;
5244 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
5246 In sub order, the @pkgparts array (passed by reference) is clobbered.
5248 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
5249 method to pass dates to the recur_prog expression, it should do so.
5251 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
5252 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
5253 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
5254 configuration values. Probably need a subroutine which decides what to do
5255 based on whether or not we've fetched the user yet, rather than a hash. See
5256 FS::UID and the TODO.
5258 Now that things are transactional should the check in the insert method be
5263 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
5264 L<FS::pkg_svc>, schema.html from the base documentation