2 use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::Sales_Mixin
3 FS::contact_Mixin FS::location_Mixin
4 FS::m2m_Common FS::option_Common );
8 use Scalar::Util qw( blessed );
9 use List::Util qw(min max);
11 use Time::Local qw( timelocal timelocal_nocheck );
13 use FS::UID qw( dbh driver_name );
14 use FS::Misc qw( send_email );
15 use FS::Record qw( qsearch qsearchs fields );
21 use FS::cust_location;
23 use FS::cust_bill_pkg;
24 use FS::cust_pkg_detail;
25 use FS::cust_pkg_usage;
26 use FS::cdr_cust_pkg_usage;
31 use FS::cust_pkg_reason;
33 use FS::cust_pkg_usageprice;
34 use FS::cust_pkg_discount;
41 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
43 # because they load configuration by setting FS::UID::callback (see TODO)
49 # for sending cancel emails in sub cancel
52 our ($disable_agentcheck, $DEBUG, $me, $import) = (0, 0, '[FS::cust_pkg]', 0);
54 our $upgrade = 0; #go away after setup+start dates cleaned up for old customers
58 my ( $hashref, $cache ) = @_;
59 #if ( $hashref->{'pkgpart'} ) {
60 if ( $hashref->{'pkg'} ) {
61 # #@{ $self->{'_pkgnum'} } = ();
62 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
63 # $self->{'_pkgpart'} = $subcache;
64 # #push @{ $self->{'_pkgnum'} },
65 # FS::part_pkg->new_or_cached($hashref, $subcache);
66 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
68 if ( exists $hashref->{'svcnum'} ) {
69 #@{ $self->{'_pkgnum'} } = ();
70 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
71 $self->{'_svcnum'} = $subcache;
72 #push @{ $self->{'_pkgnum'} },
73 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
79 FS::cust_pkg - Object methods for cust_pkg objects
85 $record = new FS::cust_pkg \%hash;
86 $record = new FS::cust_pkg { 'column' => 'value' };
88 $error = $record->insert;
90 $error = $new_record->replace($old_record);
92 $error = $record->delete;
94 $error = $record->check;
96 $error = $record->cancel;
98 $error = $record->suspend;
100 $error = $record->unsuspend;
102 $part_pkg = $record->part_pkg;
104 @labels = $record->labels;
106 $seconds = $record->seconds_since($timestamp);
108 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
109 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
113 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
114 inherits from FS::Record. The following fields are currently supported:
120 Primary key (assigned automatically for new billing items)
124 Customer (see L<FS::cust_main>)
128 Billing item definition (see L<FS::part_pkg>)
132 Optional link to package location (see L<FS::location>)
136 date package was ordered (also remains same on changes)
148 date (next bill date)
176 order taker (see L<FS::access_user>)
180 If this field is set to 1, disables the automatic
181 unsuspension of this package when using the B<unsuspendauto> config option.
185 If not set, defaults to 1
189 Date of change from previous package
199 =item change_locationnum
207 The pkgnum of the package that this package is supplemental to, if any.
211 The package link (L<FS::part_pkg_link>) that defines this supplemental
212 package, if it is one.
214 =item change_to_pkgnum
216 The pkgnum of the package this one will be "changed to" in the future
217 (on its expiration date).
221 Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date
222 are specified as UNIX timestamps; see L<perlfunc/"time">. Also see
223 L<Time::Local> and L<Date::Parse> for conversion functions.
231 Create a new billing item. To add the item to the database, see L<"insert">.
235 sub table { 'cust_pkg'; }
236 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
237 sub cust_unlinked_msg {
239 "WARNING: can't find cust_main.custnum ". $self->custnum.
240 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
243 =item insert [ OPTION => VALUE ... ]
245 Adds this billing item to the database ("Orders" the item). If there is an
246 error, returns the error, otherwise returns false.
248 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
249 will be used to look up the package definition and agent restrictions will be
252 If the additional field I<refnum> is defined, an FS::pkg_referral record will
253 be created and inserted. Multiple FS::pkg_referral records can be created by
254 setting I<refnum> to an array reference of refnums or a hash reference with
255 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
256 record will be created corresponding to cust_main.refnum.
258 If the additional field I<cust_pkg_usageprice> is defined, it will be treated
259 as an arrayref of FS::cust_pkg_usageprice objects, which will be inserted.
260 (Note that this field cannot be set with a usual ->cust_pkg_usageprice method.
261 It can be set as part of the hash when creating the object, or with the B<set>
264 The following options are available:
270 If set true, supresses actions that should only be taken for new package
271 orders. (Currently this includes: intro periods when delay_setup is on.)
275 cust_pkg_option records will be created
279 a ticket will be added to this customer with this subject
283 an optional queue name for ticket additions
287 Don't check the legality of the package definition. This should be used
288 when performing a package change that doesn't change the pkgpart (i.e.
296 my( $self, %options ) = @_;
299 $error = $self->check_pkgpart unless $options{'allow_pkgpart'};
300 return $error if $error;
302 my $part_pkg = $self->part_pkg;
305 # if the package def says to start only on the first of the month:
306 if ( $part_pkg->option('start_1st', 1) && !$self->start_date ) {
307 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time) )[0,1,2,3,4,5];
308 $mon += 1 unless $mday == 1;
309 until ( $mon < 12 ) { $mon -= 12; $year++; }
310 $self->start_date( timelocal_nocheck(0,0,0,1,$mon,$year) );
313 # set up any automatic expire/adjourn/contract_end timers
314 # based on the start date
315 foreach my $action ( qw(expire adjourn contract_end) ) {
316 my $months = $part_pkg->option("${action}_months",1);
317 if($months and !$self->$action) {
318 my $start = $self->start_date || $self->setup || time;
319 $self->$action( $part_pkg->add_freq($start, $months) );
323 # if this package has "free days" and delayed setup fee, tehn
324 # set start date that many days in the future.
325 # (this should have been set in the UI, but enforce it here)
326 if ( ! $options{'change'}
327 && ( my $free_days = $part_pkg->option('free_days',1) )
328 && $part_pkg->option('delay_setup',1)
329 #&& ! $self->start_date
332 $self->start_date( $part_pkg->default_start_date );
336 # set order date unless it was specified as part of an import
337 $self->order_date(time) unless $import && $self->order_date;
339 my $oldAutoCommit = $FS::UID::AutoCommit;
340 local $FS::UID::AutoCommit = 0;
343 $error = $self->SUPER::insert($options{options} ? %{$options{options}} : ());
345 $dbh->rollback if $oldAutoCommit;
349 $self->refnum($self->cust_main->refnum) unless $self->refnum;
350 $self->refnum( [ $self->refnum ] ) unless ref($self->refnum);
351 $self->process_m2m( 'link_table' => 'pkg_referral',
352 'target_table' => 'part_referral',
353 'params' => $self->refnum,
356 if ( $self->hashref->{cust_pkg_usageprice} ) {
357 for my $cust_pkg_usageprice ( @{ $self->hashref->{cust_pkg_usageprice} } ) {
358 $cust_pkg_usageprice->pkgnum( $self->pkgnum );
359 my $error = $cust_pkg_usageprice->insert;
361 $dbh->rollback if $oldAutoCommit;
367 if ( $self->discountnum ) {
368 my $error = $self->insert_discount();
370 $dbh->rollback if $oldAutoCommit;
375 my $conf = new FS::Conf;
377 if ( ! $import && $conf->config('ticket_system') && $options{ticket_subject} ) {
379 #this init stuff is still inefficient, but at least its limited to
380 # the small number (any?) folks using ticket emailing on pkg order
383 # use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
390 use FS::TicketSystem;
391 FS::TicketSystem->init();
393 my $q = new RT::Queue($RT::SystemUser);
394 $q->Load($options{ticket_queue}) if $options{ticket_queue};
395 my $t = new RT::Ticket($RT::SystemUser);
396 my $mime = new MIME::Entity;
397 $mime->build( Type => 'text/plain', Data => $options{ticket_subject} );
398 $t->Create( $options{ticket_queue} ? (Queue => $q) : (),
399 Subject => $options{ticket_subject},
402 $t->AddLink( Type => 'MemberOf',
403 Target => 'freeside://freeside/cust_main/'. $self->custnum,
407 if (! $import && $conf->config('welcome_letter') && $self->cust_main->num_pkgs == 1) {
408 my $queue = new FS::queue {
409 'job' => 'FS::cust_main::queueable_print',
411 $error = $queue->insert(
412 'custnum' => $self->custnum,
413 'template' => 'welcome_letter',
417 warn "can't send welcome letter: $error";
422 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
429 This method now works but you probably shouldn't use it.
431 You don't want to delete packages, because there would then be no record
432 the customer ever purchased the package. Instead, see the cancel method and
433 hide cancelled packages.
440 my $oldAutoCommit = $FS::UID::AutoCommit;
441 local $FS::UID::AutoCommit = 0;
444 foreach my $cust_pkg_discount ($self->cust_pkg_discount) {
445 my $error = $cust_pkg_discount->delete;
447 $dbh->rollback if $oldAutoCommit;
451 #cust_bill_pkg_discount?
453 foreach my $cust_pkg_detail ($self->cust_pkg_detail) {
454 my $error = $cust_pkg_detail->delete;
456 $dbh->rollback if $oldAutoCommit;
461 foreach my $cust_pkg_reason (
463 'table' => 'cust_pkg_reason',
464 'hashref' => { 'pkgnum' => $self->pkgnum },
468 my $error = $cust_pkg_reason->delete;
470 $dbh->rollback if $oldAutoCommit;
477 my $error = $self->SUPER::delete(@_);
479 $dbh->rollback if $oldAutoCommit;
483 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
489 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
491 Replaces the OLD_RECORD with this one in the database. If there is an error,
492 returns the error, otherwise returns false.
494 Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
496 Changing pkgpart may have disasterous effects. See the order subroutine.
498 setup and bill are normally updated by calling the bill method of a customer
499 object (see L<FS::cust_main>).
501 suspend is normally updated by the suspend and unsuspend methods.
503 cancel is normally updated by the cancel method (and also the order subroutine
506 Available options are:
512 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.
516 the access_user (see L<FS::access_user>) providing the reason
520 hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
529 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
534 ( ref($_[0]) eq 'HASH' )
538 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
539 #return "Can't change otaker!" if $old->otaker ne $new->otaker;
542 #return "Can't change setup once it exists!"
543 # if $old->getfield('setup') &&
544 # $old->getfield('setup') != $new->getfield('setup');
546 #some logic for bill, susp, cancel?
548 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
550 my $oldAutoCommit = $FS::UID::AutoCommit;
551 local $FS::UID::AutoCommit = 0;
554 foreach my $method ( qw(adjourn expire) ) { # How many reasons?
555 if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
556 my $error = $new->insert_reason(
557 'reason' => $options->{'reason'},
558 'date' => $new->$method,
560 'reason_otaker' => $options->{'reason_otaker'},
563 dbh->rollback if $oldAutoCommit;
564 return "Error inserting cust_pkg_reason: $error";
569 #save off and freeze RADIUS attributes for any associated svc_acct records
571 if ( $old->part_pkg->is_prepaid || $new->part_pkg->is_prepaid ) {
573 #also check for specific exports?
574 # to avoid spurious modify export events
575 @svc_acct = map { $_->svc_x }
576 grep { $_->part_svc->svcdb eq 'svc_acct' }
579 $_->snapshot foreach @svc_acct;
583 my $error = $new->export_pkg_change($old)
584 || $new->SUPER::replace( $old,
586 ? $options->{options}
590 $dbh->rollback if $oldAutoCommit;
594 #for prepaid packages,
595 #trigger export of new RADIUS Expiration attribute when cust_pkg.bill changes
596 foreach my $old_svc_acct ( @svc_acct ) {
597 my $new_svc_acct = new FS::svc_acct { $old_svc_acct->hash };
599 $new_svc_acct->replace( $old_svc_acct,
600 'depend_jobnum' => $options->{depend_jobnum},
603 $dbh->rollback if $oldAutoCommit;
608 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
615 Checks all fields to make sure this is a valid billing item. If there is an
616 error, returns the error, otherwise returns false. Called by the insert and
624 if ( !$self->locationnum or $self->locationnum == -1 ) {
625 $self->set('locationnum', $self->cust_main->ship_locationnum);
629 $self->ut_numbern('pkgnum')
630 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
631 || $self->ut_numbern('pkgpart')
632 || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' )
633 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
634 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
635 || $self->ut_numbern('quantity')
636 || $self->ut_numbern('start_date')
637 || $self->ut_numbern('setup')
638 || $self->ut_numbern('bill')
639 || $self->ut_numbern('susp')
640 || $self->ut_numbern('cancel')
641 || $self->ut_numbern('adjourn')
642 || $self->ut_numbern('resume')
643 || $self->ut_numbern('expire')
644 || $self->ut_numbern('dundate')
645 || $self->ut_enum('no_auto', [ '', 'Y' ])
646 || $self->ut_enum('waive_setup', [ '', 'Y' ])
647 || $self->ut_numbern('agent_pkgid')
648 || $self->ut_enum('recur_show_zero', [ '', 'Y', 'N', ])
649 || $self->ut_enum('setup_show_zero', [ '', 'Y', 'N', ])
650 || $self->ut_foreign_keyn('main_pkgnum', 'cust_pkg', 'pkgnum')
651 || $self->ut_foreign_keyn('pkglinknum', 'part_pkg_link', 'pkglinknum')
652 || $self->ut_foreign_keyn('change_to_pkgnum', 'cust_pkg', 'pkgnum')
654 return $error if $error;
656 return "A package with both start date (future start) and setup date (already started) will never bill"
657 if $self->start_date && $self->setup && ! $upgrade;
659 return "A future unsuspend date can only be set for a package with a suspend date"
660 if $self->resume and !$self->susp and !$self->adjourn;
662 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
664 if ( $self->dbdef_table->column('manual_flag') ) {
665 $self->manual_flag('') if $self->manual_flag eq ' ';
666 $self->manual_flag =~ /^([01]?)$/
667 or return "Illegal manual_flag ". $self->manual_flag;
668 $self->manual_flag($1);
676 Check the pkgpart to make sure it's allowed with the reg_code and/or
677 promo_code of the package (if present) and with the customer's agent.
678 Called from C<insert>, unless we are doing a package change that doesn't
686 # my $error = $self->ut_numbern('pkgpart'); # already done
689 if ( $self->reg_code ) {
691 unless ( grep { $self->pkgpart == $_->pkgpart }
692 map { $_->reg_code_pkg }
693 qsearchs( 'reg_code', { 'code' => $self->reg_code,
694 'agentnum' => $self->cust_main->agentnum })
696 return "Unknown registration code";
699 } elsif ( $self->promo_code ) {
702 qsearchs('part_pkg', {
703 'pkgpart' => $self->pkgpart,
704 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
706 return 'Unknown promotional code' unless $promo_part_pkg;
710 unless ( $disable_agentcheck ) {
712 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
713 return "agent ". $agent->agentnum. ':'. $agent->agent.
714 " can't purchase pkgpart ". $self->pkgpart
715 unless $agent->pkgpart_hashref->{ $self->pkgpart }
716 || $agent->agentnum == $self->part_pkg->agentnum;
719 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
720 return $error if $error;
728 =item cancel [ OPTION => VALUE ... ]
730 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
731 in this package, then cancels the package itself (sets the cancel field to
734 Available options are:
738 =item quiet - can be set true to supress email cancellation notices.
740 =item time - can be set to cancel the package based on a specific future or
741 historical date. Using time ensures that the remaining amount is calculated
742 correctly. Note however that this is an immediate cancel and just changes
743 the date. You are PROBABLY looking to expire the account instead of using
746 =item reason - can be set to a cancellation reason (see L<FS:reason>),
747 either a reasonnum of an existing reason, or passing a hashref will create
748 a new reason. The hashref should have the following keys: typenum - Reason
749 type (see L<FS::reason_type>, reason - Text of the new reason.
751 =item date - can be set to a unix style timestamp to specify when to
754 =item nobill - can be set true to skip billing if it might otherwise be done.
756 =item unused_credit - can be set to 1 to credit the remaining time, or 0 to
757 not credit it. This must be set (by change()) when changing the package
758 to a different pkgpart or location, and probably shouldn't be in any other
759 case. If it's not set, the 'unused_credit_cancel' part_pkg option will
764 If there is an error, returns the error, otherwise returns false.
769 my( $self, %options ) = @_;
772 # pass all suspend/cancel actions to the main package
773 if ( $self->main_pkgnum and !$options{'from_main'} ) {
774 return $self->main_pkg->cancel(%options);
777 my $conf = new FS::Conf;
779 warn "cust_pkg::cancel called with options".
780 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
783 my $oldAutoCommit = $FS::UID::AutoCommit;
784 local $FS::UID::AutoCommit = 0;
787 my $old = $self->select_for_update;
789 if ( $old->get('cancel') || $self->get('cancel') ) {
790 dbh->rollback if $oldAutoCommit;
791 return ""; # no error
794 # XXX possibly set cancel_time to the expire date?
795 my $cancel_time = $options{'time'} || time;
796 my $date = $options{'date'} if $options{'date'}; # expire/cancel later
797 $date = '' if ($date && $date <= $cancel_time); # complain instead?
799 #race condition: usage could be ongoing until unprovisioned
800 #resolved by performing a change package instead (which unprovisions) and
802 if ( !$options{nobill} && !$date ) {
803 # && $conf->exists('bill_usage_on_cancel') ) { #calc_cancel checks this
804 my $copy = $self->new({$self->hash});
806 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
808 'time' => $cancel_time );
809 warn "Error billing during cancel, custnum ".
810 #$self->cust_main->custnum. ": $error"
815 if ( $options{'reason'} ) {
816 $error = $self->insert_reason( 'reason' => $options{'reason'},
817 'action' => $date ? 'expire' : 'cancel',
818 'date' => $date ? $date : $cancel_time,
819 'reason_otaker' => $options{'reason_otaker'},
822 dbh->rollback if $oldAutoCommit;
823 return "Error inserting cust_pkg_reason: $error";
827 my %svc_cancel_opt = ();
828 $svc_cancel_opt{'date'} = $date if $date;
829 foreach my $cust_svc (
832 sort { $a->[1] <=> $b->[1] }
833 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; }
834 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
836 my $part_svc = $cust_svc->part_svc;
837 next if ( defined($part_svc) and $part_svc->preserve );
838 my $error = $cust_svc->cancel( %svc_cancel_opt );
841 $dbh->rollback if $oldAutoCommit;
842 return 'Error '. ($svc_cancel_opt{'date'} ? 'expiring' : 'canceling' ).
848 # credit remaining time if appropriate
850 if ( exists($options{'unused_credit'}) ) {
851 $do_credit = $options{'unused_credit'};
854 $do_credit = $self->part_pkg->option('unused_credit_cancel', 1);
857 my $error = $self->credit_remaining('cancel', $cancel_time);
859 $dbh->rollback if $oldAutoCommit;
866 my %hash = $self->hash;
868 $hash{'expire'} = $date;
870 $hash{'cancel'} = $cancel_time;
872 $hash{'change_custnum'} = $options{'change_custnum'};
874 my $new = new FS::cust_pkg ( \%hash );
875 $error = $new->replace( $self, options => { $self->options } );
876 if ( $self->change_to_pkgnum ) {
877 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
878 $error ||= $change_to->cancel || $change_to->delete;
881 $dbh->rollback if $oldAutoCommit;
885 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
886 $error = $supp_pkg->cancel(%options, 'from_main' => 1);
888 $dbh->rollback if $oldAutoCommit;
889 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
893 foreach my $usage ( $self->cust_pkg_usage ) {
894 $error = $usage->delete;
896 $dbh->rollback if $oldAutoCommit;
897 return "deleting usage pools: $error";
901 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
902 return '' if $date; #no errors
904 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
905 if ( !$options{'quiet'} &&
906 $conf->exists('emailcancel', $self->cust_main->agentnum) &&
908 my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
911 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
912 $error = $msg_template->send( 'cust_main' => $self->cust_main,
917 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
918 'to' => \@invoicing_list,
919 'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
920 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
923 #should this do something on errors?
930 =item cancel_if_expired [ NOW_TIMESTAMP ]
932 Cancels this package if its expire date has been reached.
936 sub cancel_if_expired {
938 my $time = shift || time;
939 return '' unless $self->expire && $self->expire <= $time;
940 my $error = $self->cancel;
942 return "Error cancelling expired pkg ". $self->pkgnum. " for custnum ".
943 $self->custnum. ": $error";
950 "Un-cancels" this package: Orders a new package with the same custnum, pkgpart,
951 locationnum, (other fields?). Attempts to re-provision cancelled services
952 using history information (errors at this stage are not fatal).
954 cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
956 svc_fatal: service provisioning errors are fatal
958 svc_errors: pass an array reference, will be filled in with any provisioning errors
960 main_pkgnum: link the package as a supplemental package of this one. For
966 my( $self, %options ) = @_;
968 #in case you try do do $uncancel-date = $cust_pkg->uncacel
969 return '' unless $self->get('cancel');
971 if ( $self->main_pkgnum and !$options{'main_pkgnum'} ) {
972 return $self->main_pkg->uncancel(%options);
979 my $oldAutoCommit = $FS::UID::AutoCommit;
980 local $FS::UID::AutoCommit = 0;
984 # insert the new package
987 my $cust_pkg = new FS::cust_pkg {
988 last_bill => ( $options{'last_bill'} || $self->get('last_bill') ),
989 bill => ( $options{'bill'} || $self->get('bill') ),
991 uncancel_pkgnum => $self->pkgnum,
992 main_pkgnum => ($options{'main_pkgnum'} || ''),
993 map { $_ => $self->get($_) } qw(
994 custnum pkgpart locationnum
996 susp adjourn resume expire start_date contract_end dundate
997 change_date change_pkgpart change_locationnum
998 manual_flag no_auto quantity agent_pkgid recur_show_zero setup_show_zero
1002 my $error = $cust_pkg->insert(
1003 'change' => 1, #supresses any referral credit to a referring customer
1004 'allow_pkgpart' => 1, # allow this even if the package def is disabled
1007 $dbh->rollback if $oldAutoCommit;
1015 #find historical services within this timeframe before the package cancel
1016 # (incompatible with "time" option to cust_pkg->cancel?)
1017 my $fuzz = 2 * 60; #2 minutes? too much? (might catch separate unprovision)
1018 # too little? (unprovisioing export delay?)
1019 my($end, $start) = ( $self->get('cancel'), $self->get('cancel') - $fuzz );
1020 my @h_cust_svc = $self->h_cust_svc( $end, $start );
1023 foreach my $h_cust_svc (@h_cust_svc) {
1024 my $h_svc_x = $h_cust_svc->h_svc_x( $end, $start );
1025 #next unless $h_svc_x; #should this happen?
1026 (my $table = $h_svc_x->table) =~ s/^h_//;
1027 require "FS/$table.pm";
1028 my $class = "FS::$table";
1029 my $svc_x = $class->new( {
1030 'pkgnum' => $cust_pkg->pkgnum,
1031 'svcpart' => $h_cust_svc->svcpart,
1032 map { $_ => $h_svc_x->get($_) } fields($table)
1036 if ( $h_svc_x->isa('FS::h_svc_Radius_Mixin') ) {
1037 $svc_x->usergroup( [ $h_svc_x->h_usergroup($end, $start) ] );
1040 my $svc_error = $svc_x->insert;
1042 if ( $options{svc_fatal} ) {
1043 $dbh->rollback if $oldAutoCommit;
1046 # if we've failed to insert the svc_x object, svc_Common->insert
1047 # will have removed the cust_svc already. if not, then both records
1048 # were inserted but we failed for some other reason (export, most
1049 # likely). in that case, report the error and delete the records.
1050 push @svc_errors, $svc_error;
1051 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_x->svcnum });
1053 # except if export_insert failed, export_delete probably won't be
1055 local $FS::svc_Common::noexport_hack = 1;
1056 my $cleanup_error = $svc_x->delete; # also deletes cust_svc
1057 if ( $cleanup_error ) { # and if THAT fails, then run away
1058 $dbh->rollback if $oldAutoCommit;
1059 return $cleanup_error;
1064 } #foreach $h_cust_svc
1066 #these are pretty rare, but should handle them
1067 # - dsl_device (mac addresses)
1068 # - phone_device (mac addresses)
1069 # - dsl_note (ikano notes)
1070 # - domain_record (i.e. restore DNS information w/domains)
1071 # - inventory_item(?) (inventory w/un-cancelling service?)
1072 # - nas (svc_broaband nas stuff)
1073 #this stuff is unused in the wild afaik
1074 # - mailinglistmember
1076 # - svc_domain.parent_svcnum?
1077 # - acct_snarf (ancient mail fetching config)
1078 # - cgp_rule (communigate)
1079 # - cust_svc_option (used by our Tron stuff)
1080 # - acct_rt_transaction (used by our time worked stuff)
1083 # also move over any services that didn't unprovision at cancellation
1086 foreach my $cust_svc ( qsearch('cust_svc', { pkgnum => $self->pkgnum } ) ) {
1087 $cust_svc->pkgnum( $cust_pkg->pkgnum );
1088 my $error = $cust_svc->replace;
1090 $dbh->rollback if $oldAutoCommit;
1096 # Uncancel any supplemental packages, and make them supplemental to the
1100 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1102 $error = $supp_pkg->uncancel(%options, 'main_pkgnum' => $cust_pkg->pkgnum);
1104 $dbh->rollback if $oldAutoCommit;
1105 return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
1113 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1115 ${ $options{cust_pkg} } = $cust_pkg if ref($options{cust_pkg});
1116 @{ $options{svc_errors} } = @svc_errors if ref($options{svc_errors});
1123 Cancels any pending expiration (sets the expire field to null).
1125 If there is an error, returns the error, otherwise returns false.
1130 my( $self, %options ) = @_;
1133 my $oldAutoCommit = $FS::UID::AutoCommit;
1134 local $FS::UID::AutoCommit = 0;
1137 my $old = $self->select_for_update;
1139 my $pkgnum = $old->pkgnum;
1140 if ( $old->get('cancel') || $self->get('cancel') ) {
1141 dbh->rollback if $oldAutoCommit;
1142 return "Can't unexpire cancelled package $pkgnum";
1143 # or at least it's pointless
1146 unless ( $old->get('expire') && $self->get('expire') ) {
1147 dbh->rollback if $oldAutoCommit;
1148 return ""; # no error
1151 my %hash = $self->hash;
1152 $hash{'expire'} = '';
1153 my $new = new FS::cust_pkg ( \%hash );
1154 $error = $new->replace( $self, options => { $self->options } );
1156 $dbh->rollback if $oldAutoCommit;
1160 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1166 =item suspend [ OPTION => VALUE ... ]
1168 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1169 package, then suspends the package itself (sets the susp field to now).
1171 Available options are:
1175 =item reason - can be set to a cancellation reason (see L<FS:reason>),
1176 either a reasonnum of an existing reason, or passing a hashref will create
1177 a new reason. The hashref should have the following keys:
1178 - typenum - Reason type (see L<FS::reason_type>
1179 - reason - Text of the new reason.
1181 =item date - can be set to a unix style timestamp to specify when to
1184 =item time - can be set to override the current time, for calculation
1185 of final invoices or unused-time credits
1187 =item resume_date - can be set to a time when the package should be
1188 unsuspended. This may be more convenient than calling C<unsuspend()>
1191 =item from_main - allows a supplemental package to be suspended, rather
1192 than redirecting the method call to its main package. For internal use.
1196 If there is an error, returns the error, otherwise returns false.
1201 my( $self, %options ) = @_;
1204 # pass all suspend/cancel actions to the main package
1205 if ( $self->main_pkgnum and !$options{'from_main'} ) {
1206 return $self->main_pkg->suspend(%options);
1209 my $oldAutoCommit = $FS::UID::AutoCommit;
1210 local $FS::UID::AutoCommit = 0;
1213 my $old = $self->select_for_update;
1215 my $pkgnum = $old->pkgnum;
1216 if ( $old->get('cancel') || $self->get('cancel') ) {
1217 dbh->rollback if $oldAutoCommit;
1218 return "Can't suspend cancelled package $pkgnum";
1221 if ( $old->get('susp') || $self->get('susp') ) {
1222 dbh->rollback if $oldAutoCommit;
1223 return ""; # no error # complain on adjourn?
1226 my $suspend_time = $options{'time'} || time;
1227 my $date = $options{date} if $options{date}; # adjourn/suspend later
1228 $date = '' if ($date && $date <= $suspend_time); # complain instead?
1230 if ( $date && $old->get('expire') && $old->get('expire') < $date ) {
1231 dbh->rollback if $oldAutoCommit;
1232 return "Package $pkgnum expires before it would be suspended.";
1235 # some false laziness with sub cancel
1236 if ( !$options{nobill} && !$date &&
1237 $self->part_pkg->option('bill_suspend_as_cancel',1) ) {
1238 # kind of a kludge--'bill_suspend_as_cancel' to avoid having to
1239 # make the entire cust_main->bill path recognize 'suspend' and
1240 # 'cancel' separately.
1241 warn "Billing $pkgnum on suspension (at $suspend_time)\n" if $DEBUG;
1242 my $copy = $self->new({$self->hash});
1244 $copy->cust_main->bill( 'pkg_list' => [ $copy ],
1246 'time' => $suspend_time );
1247 warn "Error billing during suspend, custnum ".
1248 #$self->cust_main->custnum. ": $error"
1253 if ( $options{'reason'} ) {
1254 $error = $self->insert_reason( 'reason' => $options{'reason'},
1255 'action' => $date ? 'adjourn' : 'suspend',
1256 'date' => $date ? $date : $suspend_time,
1257 'reason_otaker' => $options{'reason_otaker'},
1260 dbh->rollback if $oldAutoCommit;
1261 return "Error inserting cust_pkg_reason: $error";
1265 my %hash = $self->hash;
1267 $hash{'adjourn'} = $date;
1269 $hash{'susp'} = $suspend_time;
1272 my $resume_date = $options{'resume_date'} || 0;
1273 if ( $resume_date > ($date || $suspend_time) ) {
1274 $hash{'resume'} = $resume_date;
1277 $options{options} ||= {};
1279 my $new = new FS::cust_pkg ( \%hash );
1280 $error = $new->replace( $self, options => { $self->options,
1281 %{ $options{options} },
1285 $dbh->rollback if $oldAutoCommit;
1290 # credit remaining time if appropriate
1291 if ( $self->part_pkg->option('unused_credit_suspend', 1) ) {
1292 my $error = $self->credit_remaining('suspend', $suspend_time);
1294 $dbh->rollback if $oldAutoCommit;
1301 foreach my $cust_svc (
1302 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
1304 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1306 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1307 $dbh->rollback if $oldAutoCommit;
1308 return "Illegal svcdb value in part_svc!";
1311 require "FS/$svcdb.pm";
1313 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1315 $error = $svc->suspend;
1317 $dbh->rollback if $oldAutoCommit;
1320 my( $label, $value ) = $cust_svc->label;
1321 push @labels, "$label: $value";
1325 my $conf = new FS::Conf;
1326 if ( $conf->config('suspend_email_admin') ) {
1328 my $error = send_email(
1329 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1330 #invoice_from ??? well as good as any
1331 'to' => $conf->config('suspend_email_admin'),
1332 'subject' => 'FREESIDE NOTIFICATION: Customer package suspended',
1334 "This is an automatic message from your Freeside installation\n",
1335 "informing you that the following customer package has been suspended:\n",
1337 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1338 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1339 ( map { "Service : $_\n" } @labels ),
1344 warn "WARNING: can't send suspension admin email (suspending anyway): ".
1352 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1353 $error = $supp_pkg->suspend(%options, 'from_main' => 1);
1355 $dbh->rollback if $oldAutoCommit;
1356 return "suspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1360 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1365 =item credit_remaining MODE TIME
1367 Generate a credit for this package for the time remaining in the current
1368 billing period. MODE is either "suspend" or "cancel" (determines the
1369 credit type). TIME is the time of suspension/cancellation. Both arguments
1374 sub credit_remaining {
1375 # Add a credit for remaining service
1376 my ($self, $mode, $time) = @_;
1377 die 'credit_remaining requires suspend or cancel'
1378 unless $mode eq 'suspend' or $mode eq 'cancel';
1379 die 'no suspend/cancel time' unless $time > 0;
1381 my $conf = FS::Conf->new;
1382 my $reason_type = $conf->config($mode.'_credit_type');
1384 my $last_bill = $self->getfield('last_bill') || 0;
1385 my $next_bill = $self->getfield('bill') || 0;
1386 if ( $last_bill > 0 # the package has been billed
1387 and $next_bill > 0 # the package has a next bill date
1388 and $next_bill >= $time # which is in the future
1390 my $remaining_value = $self->calc_remain('time' => $time);
1391 if ( $remaining_value > 0 ) {
1392 warn "Crediting for $remaining_value on package ".$self->pkgnum."\n"
1394 my $error = $self->cust_main->credit(
1396 'Credit for unused time on '. $self->part_pkg->pkg,
1397 'reason_type' => $reason_type,
1399 return "Error crediting customer \$$remaining_value for unused time".
1400 " on ". $self->part_pkg->pkg. ": $error"
1402 } #if $remaining_value
1403 } #if $last_bill, etc.
1407 =item unsuspend [ OPTION => VALUE ... ]
1409 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
1410 package, then unsuspends the package itself (clears the susp field and the
1411 adjourn field if it is in the past). If the suspend reason includes an
1412 unsuspension package, that package will be ordered.
1414 Available options are:
1420 Can be set to a date to unsuspend the package in the future (the 'resume'
1423 =item adjust_next_bill
1425 Can be set true to adjust the next bill date forward by
1426 the amount of time the account was inactive. This was set true by default
1427 in the past (from 1.4.2 and 1.5.0pre6 through 1.7.0), but now needs to be
1428 explicitly requested with this option or in the price plan.
1432 If there is an error, returns the error, otherwise returns false.
1437 my( $self, %opt ) = @_;
1440 # pass all suspend/cancel actions to the main package
1441 if ( $self->main_pkgnum and !$opt{'from_main'} ) {
1442 return $self->main_pkg->unsuspend(%opt);
1445 my $oldAutoCommit = $FS::UID::AutoCommit;
1446 local $FS::UID::AutoCommit = 0;
1449 my $old = $self->select_for_update;
1451 my $pkgnum = $old->pkgnum;
1452 if ( $old->get('cancel') || $self->get('cancel') ) {
1453 $dbh->rollback if $oldAutoCommit;
1454 return "Can't unsuspend cancelled package $pkgnum";
1457 unless ( $old->get('susp') && $self->get('susp') ) {
1458 $dbh->rollback if $oldAutoCommit;
1459 return ""; # no error # complain instead?
1462 my $date = $opt{'date'};
1463 if ( $date and $date > time ) { # return an error if $date <= time?
1465 if ( $old->get('expire') && $old->get('expire') < $date ) {
1466 $dbh->rollback if $oldAutoCommit;
1467 return "Package $pkgnum expires before it would be unsuspended.";
1470 my $new = new FS::cust_pkg { $self->hash };
1471 $new->set('resume', $date);
1472 $error = $new->replace($self, options => $self->options);
1475 $dbh->rollback if $oldAutoCommit;
1479 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1487 foreach my $cust_svc (
1488 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
1490 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
1492 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
1493 $dbh->rollback if $oldAutoCommit;
1494 return "Illegal svcdb value in part_svc!";
1497 require "FS/$svcdb.pm";
1499 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
1501 $error = $svc->unsuspend;
1503 $dbh->rollback if $oldAutoCommit;
1506 my( $label, $value ) = $cust_svc->label;
1507 push @labels, "$label: $value";
1512 my $cust_pkg_reason = $self->last_cust_pkg_reason('susp');
1513 my $reason = $cust_pkg_reason ? $cust_pkg_reason->reason : '';
1515 my %hash = $self->hash;
1516 my $inactive = time - $hash{'susp'};
1518 my $conf = new FS::Conf;
1520 if ( $inactive > 0 &&
1521 ( $hash{'bill'} || $hash{'setup'} ) &&
1522 ( $opt{'adjust_next_bill'} ||
1523 $conf->exists('unsuspend-always_adjust_next_bill_date') ||
1524 $self->part_pkg->option('unsuspend_adjust_bill', 1) )
1527 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive;
1532 $hash{'adjourn'} = '' if $hash{'adjourn'} and $hash{'adjourn'} < time;
1533 $hash{'resume'} = '' if !$hash{'adjourn'};
1534 my $new = new FS::cust_pkg ( \%hash );
1535 $error = $new->replace( $self, options => { $self->options } );
1537 $dbh->rollback if $oldAutoCommit;
1543 if ( $reason && $reason->unsuspend_pkgpart ) {
1544 my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart)
1545 or $error = "Unsuspend package definition ".$reason->unsuspend_pkgpart.
1547 my $start_date = $self->cust_main->next_bill_date
1548 if $reason->unsuspend_hold;
1551 $unsusp_pkg = FS::cust_pkg->new({
1552 'custnum' => $self->custnum,
1553 'pkgpart' => $reason->unsuspend_pkgpart,
1554 'start_date' => $start_date,
1555 'locationnum' => $self->locationnum,
1556 # discount? probably not...
1559 $error ||= $self->cust_main->order_pkg( 'cust_pkg' => $unsusp_pkg );
1563 $dbh->rollback if $oldAutoCommit;
1568 if ( $conf->config('unsuspend_email_admin') ) {
1570 my $error = send_email(
1571 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
1572 #invoice_from ??? well as good as any
1573 'to' => $conf->config('unsuspend_email_admin'),
1574 'subject' => 'FREESIDE NOTIFICATION: Customer package unsuspended', 'body' => [
1575 "This is an automatic message from your Freeside installation\n",
1576 "informing you that the following customer package has been unsuspended:\n",
1578 'Customer: #'. $self->custnum. ' '. $self->cust_main->name. "\n",
1579 'Package : #'. $self->pkgnum. " (". $self->part_pkg->pkg_comment. ")\n",
1580 ( map { "Service : $_\n" } @labels ),
1582 "An unsuspension fee was charged: ".
1583 $unsusp_pkg->part_pkg->pkg_comment."\n"
1590 warn "WARNING: can't send unsuspension admin email (unsuspending anyway): ".
1596 foreach my $supp_pkg ( $self->supplemental_pkgs ) {
1597 $error = $supp_pkg->unsuspend(%opt, 'from_main' => 1);
1599 $dbh->rollback if $oldAutoCommit;
1600 return "unsuspending supplemental pkg#".$supp_pkg->pkgnum.": $error";
1604 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1611 Cancels any pending suspension (sets the adjourn field to null).
1613 If there is an error, returns the error, otherwise returns false.
1618 my( $self, %options ) = @_;
1621 my $oldAutoCommit = $FS::UID::AutoCommit;
1622 local $FS::UID::AutoCommit = 0;
1625 my $old = $self->select_for_update;
1627 my $pkgnum = $old->pkgnum;
1628 if ( $old->get('cancel') || $self->get('cancel') ) {
1629 dbh->rollback if $oldAutoCommit;
1630 return "Can't unadjourn cancelled package $pkgnum";
1631 # or at least it's pointless
1634 if ( $old->get('susp') || $self->get('susp') ) {
1635 dbh->rollback if $oldAutoCommit;
1636 return "Can't unadjourn suspended package $pkgnum";
1637 # perhaps this is arbitrary
1640 unless ( $old->get('adjourn') && $self->get('adjourn') ) {
1641 dbh->rollback if $oldAutoCommit;
1642 return ""; # no error
1645 my %hash = $self->hash;
1646 $hash{'adjourn'} = '';
1647 $hash{'resume'} = '';
1648 my $new = new FS::cust_pkg ( \%hash );
1649 $error = $new->replace( $self, options => { $self->options } );
1651 $dbh->rollback if $oldAutoCommit;
1655 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1662 =item change HASHREF | OPTION => VALUE ...
1664 Changes this package: cancels it and creates a new one, with a different
1665 pkgpart or locationnum or both. All services are transferred to the new
1666 package (no change will be made if this is not possible).
1668 Options may be passed as a list of key/value pairs or as a hash reference.
1675 New locationnum, to change the location for this package.
1679 New FS::cust_location object, to create a new location and assign it
1684 New FS::cust_main object, to create a new customer and assign the new package
1689 New pkgpart (see L<FS::part_pkg>).
1693 New refnum (see L<FS::part_referral>).
1697 New quantity; if unspecified, the new package will have the same quantity
1702 "New" (existing) FS::cust_pkg object. The package's services and other
1703 attributes will be transferred to this package.
1707 Set to true to transfer billing dates (start_date, setup, last_bill, bill,
1708 susp, adjourn, cancel, expire, and contract_end) to the new package.
1710 =item unprotect_svcs
1712 Normally, change() will rollback and return an error if some services
1713 can't be transferred (also see the I<cust_pkg-change_svcpart> config option).
1714 If unprotect_svcs is true, this method will transfer as many services as
1715 it can and then unconditionally cancel the old package.
1719 At least one of locationnum, cust_location, pkgpart, refnum, cust_main, or
1720 cust_pkg must be specified (otherwise, what's the point?)
1722 Returns either the new FS::cust_pkg object or a scalar error.
1726 my $err_or_new_cust_pkg = $old_cust_pkg->change
1730 #some false laziness w/order
1733 my $opt = ref($_[0]) ? shift : { @_ };
1735 my $conf = new FS::Conf;
1737 # Transactionize this whole mess
1738 my $oldAutoCommit = $FS::UID::AutoCommit;
1739 local $FS::UID::AutoCommit = 0;
1748 $hash{'setup'} = $time if $self->setup;
1750 $hash{'change_date'} = $time;
1751 $hash{"change_$_"} = $self->$_()
1752 foreach qw( pkgnum pkgpart locationnum );
1754 if ( $opt->{'cust_location'} ) {
1755 $error = $opt->{'cust_location'}->find_or_insert;
1757 $dbh->rollback if $oldAutoCommit;
1758 return "creating location record: $error";
1760 $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum;
1763 if ( $opt->{'cust_pkg'} ) {
1764 # treat changing to a package with a different pkgpart as a
1765 # pkgpart change (because it is)
1766 $opt->{'pkgpart'} = $opt->{'cust_pkg'}->pkgpart;
1769 # whether to override pkgpart checking on the new package
1770 my $same_pkgpart = 1;
1771 if ( $opt->{'pkgpart'} and ( $opt->{'pkgpart'} != $self->pkgpart ) ) {
1775 my $unused_credit = 0;
1776 my $keep_dates = $opt->{'keep_dates'};
1777 # Special case. If the pkgpart is changing, and the customer is
1778 # going to be credited for remaining time, don't keep setup, bill,
1779 # or last_bill dates, and DO pass the flag to cancel() to credit
1781 if ( $opt->{'pkgpart'}
1782 and $opt->{'pkgpart'} != $self->pkgpart
1783 and $self->part_pkg->option('unused_credit_change', 1) ) {
1786 $hash{$_} = '' foreach qw(setup bill last_bill);
1789 if ( $keep_dates ) {
1790 foreach my $date ( qw(setup bill last_bill susp adjourn cancel expire
1791 resume start_date contract_end ) ) {
1792 $hash{$date} = $self->getfield($date);
1796 # allow $opt->{'locationnum'} = '' to specifically set it to null
1797 # (i.e. customer default location)
1798 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
1800 # usually this doesn't matter. the two cases where it does are:
1801 # 1. unused_credit_change + pkgpart change + setup fee on the new package
1803 # 2. (more importantly) changing a package before it's billed
1804 $hash{'waive_setup'} = $self->waive_setup;
1806 my $custnum = $self->custnum;
1807 if ( $opt->{cust_main} ) {
1808 my $cust_main = $opt->{cust_main};
1809 unless ( $cust_main->custnum ) {
1810 my $error = $cust_main->insert( @{ $opt->{cust_main_insert_args}||[] } );
1812 $dbh->rollback if $oldAutoCommit;
1813 return "inserting customer record: $error";
1816 $custnum = $cust_main->custnum;
1819 $hash{'contactnum'} = $opt->{'contactnum'} if $opt->{'contactnum'};
1822 if ( $opt->{'cust_pkg'} ) {
1823 # The target package already exists; update it to show that it was
1824 # changed from this package.
1825 $cust_pkg = $opt->{'cust_pkg'};
1827 foreach ( qw( pkgnum pkgpart locationnum ) ) {
1828 $cust_pkg->set("change_$_", $self->get($_));
1830 $cust_pkg->set('change_date', $time);
1831 $error = $cust_pkg->replace;
1834 # Create the new package.
1835 $cust_pkg = new FS::cust_pkg {
1836 custnum => $custnum,
1837 locationnum => $opt->{'locationnum'},
1838 ( map { $_ => ( $opt->{$_} || $self->$_() ) }
1839 qw( pkgpart quantity refnum salesnum )
1843 $error = $cust_pkg->insert( 'change' => 1,
1844 'allow_pkgpart' => $same_pkgpart );
1847 $dbh->rollback if $oldAutoCommit;
1848 return "inserting new package: $error";
1851 # Transfer services and cancel old package.
1853 $error = $self->transfer($cust_pkg);
1854 if ($error and $error == 0) {
1855 # $old_pkg->transfer failed.
1856 $dbh->rollback if $oldAutoCommit;
1857 return "transferring $error";
1860 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
1861 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
1862 $error = $self->transfer($cust_pkg, 'change_svcpart'=>1 );
1863 if ($error and $error == 0) {
1864 # $old_pkg->transfer failed.
1865 $dbh->rollback if $oldAutoCommit;
1866 return "converting $error";
1870 # We set unprotect_svcs when executing a "future package change". It's
1871 # not a user-interactive operation, so returning an error means the
1872 # package change will just fail. Rather than have that happen, we'll
1873 # let leftover services be deleted.
1874 if ($error > 0 and !$opt->{'unprotect_svcs'}) {
1875 # Transfers were successful, but we still had services left on the old
1876 # package. We can't change the package under this circumstances, so abort.
1877 $dbh->rollback if $oldAutoCommit;
1878 return "unable to transfer all services";
1881 #reset usage if changing pkgpart
1882 # AND usage rollover is off (otherwise adds twice, now and at package bill)
1883 if ($self->pkgpart != $cust_pkg->pkgpart) {
1884 my $part_pkg = $cust_pkg->part_pkg;
1885 $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
1889 if $part_pkg->can('reset_usage') && ! $part_pkg->option('usage_rollover',1);
1892 $dbh->rollback if $oldAutoCommit;
1893 return "setting usage values: $error";
1896 # if NOT changing pkgpart, transfer any usage pools over
1897 foreach my $usage ($self->cust_pkg_usage) {
1898 $usage->set('pkgnum', $cust_pkg->pkgnum);
1899 $error = $usage->replace;
1901 $dbh->rollback if $oldAutoCommit;
1902 return "transferring usage pools: $error";
1907 # transfer usage pricing add-ons, if we're not changing pkgpart
1908 if ( $same_pkgpart ) {
1909 foreach my $old_cust_pkg_usageprice ($self->cust_pkg_usageprice) {
1910 my $new_cust_pkg_usageprice = new FS::cust_pkg_usageprice {
1911 'pkgnum' => $cust_pkg->pkgnum,
1912 'usagepricepart' => $old_cust_pkg_usageprice->usagepricepart,
1913 'quantity' => $old_cust_pkg_usageprice->quantity,
1915 $error = $new_cust_pkg_usageprice->insert;
1917 $dbh->rollback if $oldAutoCommit;
1918 return "Error transferring usage pricing add-on: $error";
1923 # transfer discounts, if we're not changing pkgpart
1924 if ( $same_pkgpart ) {
1925 foreach my $old_discount ($self->cust_pkg_discount_active) {
1926 # don't remove the old discount, we may still need to bill that package.
1927 my $new_discount = new FS::cust_pkg_discount {
1928 'pkgnum' => $cust_pkg->pkgnum,
1929 'discountnum' => $old_discount->discountnum,
1930 'months_used' => $old_discount->months_used,
1932 $error = $new_discount->insert;
1934 $dbh->rollback if $oldAutoCommit;
1935 return "transferring discounts: $error";
1940 # transfer (copy) invoice details
1941 foreach my $detail ($self->cust_pkg_detail) {
1942 my $new_detail = FS::cust_pkg_detail->new({ $detail->hash });
1943 $new_detail->set('pkgdetailnum', '');
1944 $new_detail->set('pkgnum', $cust_pkg->pkgnum);
1945 $error = $new_detail->insert;
1947 $dbh->rollback if $oldAutoCommit;
1948 return "transferring package notes: $error";
1954 if ( !$opt->{'cust_pkg'} ) {
1955 # Order any supplemental packages.
1956 my $part_pkg = $cust_pkg->part_pkg;
1957 my @old_supp_pkgs = $self->supplemental_pkgs;
1958 foreach my $link ($part_pkg->supp_part_pkg_link) {
1960 foreach (@old_supp_pkgs) {
1961 if ($_->pkgpart == $link->dst_pkgpart) {
1963 $_->pkgpart(0); # so that it can't match more than once
1967 # false laziness with FS::cust_main::Packages::order_pkg
1968 my $new = FS::cust_pkg->new({
1969 pkgpart => $link->dst_pkgpart,
1970 pkglinknum => $link->pkglinknum,
1971 custnum => $custnum,
1972 main_pkgnum => $cust_pkg->pkgnum,
1973 locationnum => $cust_pkg->locationnum,
1974 start_date => $cust_pkg->start_date,
1975 order_date => $cust_pkg->order_date,
1976 expire => $cust_pkg->expire,
1977 adjourn => $cust_pkg->adjourn,
1978 contract_end => $cust_pkg->contract_end,
1979 refnum => $cust_pkg->refnum,
1980 discountnum => $cust_pkg->discountnum,
1981 waive_setup => $cust_pkg->waive_setup,
1983 if ( $old and $opt->{'keep_dates'} ) {
1984 foreach (qw(setup bill last_bill)) {
1985 $new->set($_, $old->get($_));
1988 $error = $new->insert( allow_pkgpart => $same_pkgpart );
1991 $error ||= $old->transfer($new);
1993 if ( $error and $error > 0 ) {
1994 # no reason why this should ever fail, but still...
1995 $error = "Unable to transfer all services from supplemental package ".
1999 $dbh->rollback if $oldAutoCommit;
2002 push @new_supp_pkgs, $new;
2004 } # if !$opt->{'cust_pkg'}
2005 # because if there is one, then supplemental packages would already
2006 # have been created for it.
2008 #Good to go, cancel old package. Notify 'cancel' of whether to credit
2010 #Don't allow billing the package (preceding period packages and/or
2011 #outstanding usage) if we are keeping dates (i.e. location changing),
2012 #because the new package will be billed for the same date range.
2013 #Supplemental packages are also canceled here.
2015 # during scheduled changes, avoid canceling the package we just
2017 $self->set('change_to_pkgnum' => '');
2019 $error = $self->cancel(
2021 unused_credit => $unused_credit,
2022 nobill => $keep_dates,
2023 change_custnum => ( $self->custnum != $custnum ? $custnum : '' ),
2026 $dbh->rollback if $oldAutoCommit;
2027 return "canceling old package: $error";
2030 if ( $conf->exists('cust_pkg-change_pkgpart-bill_now') ) {
2032 my $error = $cust_pkg->cust_main->bill(
2033 'pkg_list' => [ $cust_pkg, @new_supp_pkgs ]
2036 $dbh->rollback if $oldAutoCommit;
2037 return "billing new package: $error";
2041 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2047 =item change_later OPTION => VALUE...
2049 Schedule a package change for a later date. This actually orders the new
2050 package immediately, but sets its start date for a future date, and sets
2051 the current package to expire on the same date.
2053 If the package is already scheduled for a change, this can be called with
2054 'start_date' to change the scheduled date, or with pkgpart and/or
2055 locationnum to modify the package change. To cancel the scheduled change
2056 entirely, see C<abort_change>.
2064 The date for the package change. Required, and must be in the future.
2072 The pkgpart. locationnum, and quantity of the new package, with the same
2073 meaning as in C<change>.
2081 my $opt = ref($_[0]) ? shift : { @_ };
2083 my $oldAutoCommit = $FS::UID::AutoCommit;
2084 local $FS::UID::AutoCommit = 0;
2087 my $cust_main = $self->cust_main;
2089 my $date = delete $opt->{'start_date'} or return 'start_date required';
2091 if ( $date <= time ) {
2092 $dbh->rollback if $oldAutoCommit;
2093 return "start_date $date is in the past";
2098 if ( $self->change_to_pkgnum ) {
2099 my $change_to = FS::cust_pkg->by_key($self->change_to_pkgnum);
2100 my $new_pkgpart = $opt->{'pkgpart'}
2101 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $change_to->pkgpart;
2102 my $new_locationnum = $opt->{'locationnum'}
2103 if $opt->{'locationnum'} and $opt->{'locationnum'} != $change_to->locationnum;
2104 my $new_quantity = $opt->{'quantity'}
2105 if $opt->{'quantity'} and $opt->{'quantity'} != $change_to->quantity;
2106 if ( $new_pkgpart or $new_locationnum or $new_quantity ) {
2107 # it hasn't been billed yet, so in principle we could just edit
2108 # it in place (w/o a package change), but that's bad form.
2109 # So change the package according to the new options...
2110 my $err_or_pkg = $change_to->change(%$opt);
2111 if ( ref $err_or_pkg ) {
2112 # Then set that package up for a future start.
2113 $self->set('change_to_pkgnum', $err_or_pkg->pkgnum);
2114 $self->set('expire', $date); # in case it's different
2115 $err_or_pkg->set('start_date', $date);
2116 $err_or_pkg->set('change_date', '');
2117 $err_or_pkg->set('change_pkgnum', '');
2119 $error = $self->replace ||
2120 $err_or_pkg->replace ||
2121 $change_to->cancel ||
2124 $error = $err_or_pkg;
2126 } else { # change the start date only.
2127 $self->set('expire', $date);
2128 $change_to->set('start_date', $date);
2129 $error = $self->replace || $change_to->replace;
2132 $dbh->rollback if $oldAutoCommit;
2135 $dbh->commit if $oldAutoCommit;
2138 } # if $self->change_to_pkgnum
2140 my $new_pkgpart = $opt->{'pkgpart'}
2141 if $opt->{'pkgpart'} and $opt->{'pkgpart'} != $self->pkgpart;
2142 my $new_locationnum = $opt->{'locationnum'}
2143 if $opt->{'locationnum'} and $opt->{'locationnum'} != $self->locationnum;
2144 my $new_quantity = $opt->{'quantity'}
2145 if $opt->{'quantity'} and $opt->{'quantity'} != $self->quantity;
2147 return '' unless $new_pkgpart or $new_locationnum or $new_quantity; # wouldn't do anything
2149 # allow $opt->{'locationnum'} = '' to specifically set it to null
2150 # (i.e. customer default location)
2151 $opt->{'locationnum'} = $self->locationnum if !exists($opt->{'locationnum'});
2153 my $new = FS::cust_pkg->new( {
2154 custnum => $self->custnum,
2155 locationnum => $opt->{'locationnum'},
2156 start_date => $date,
2157 map { $_ => ( $opt->{$_} || $self->$_() ) }
2158 qw( pkgpart quantity refnum salesnum )
2160 $error = $new->insert('change' => 1,
2161 'allow_pkgpart' => ($new_pkgpart ? 0 : 1));
2163 $self->set('change_to_pkgnum', $new->pkgnum);
2164 $self->set('expire', $date);
2165 $error = $self->replace;
2168 $dbh->rollback if $oldAutoCommit;
2170 $dbh->commit if $oldAutoCommit;
2178 Cancels a future package change scheduled by C<change_later>.
2184 my $pkgnum = $self->change_to_pkgnum;
2185 my $change_to = FS::cust_pkg->by_key($pkgnum) if $pkgnum;
2188 $error = $change_to->cancel || $change_to->delete;
2189 return $error if $error;
2191 $self->set('change_to_pkgnum', '');
2192 $self->set('expire', '');
2196 =item set_quantity QUANTITY
2198 Change the package's quantity field. This is one of the few package properties
2199 that can safely be changed without canceling and reordering the package
2200 (because it doesn't affect tax eligibility). Returns an error or an
2207 $self = $self->replace_old; # just to make sure
2208 $self->quantity(shift);
2212 =item set_salesnum SALESNUM
2214 Change the package's salesnum (sales person) field. This is one of the few
2215 package properties that can safely be changed without canceling and reordering
2216 the package (because it doesn't affect tax eligibility). Returns an error or
2223 $self = $self->replace_old; # just to make sure
2224 $self->salesnum(shift);
2226 # XXX this should probably reassign any credit that's already been given
2229 =item modify_charge OPTIONS
2231 Change the properties of a one-time charge. The following properties can
2232 be changed this way:
2233 - pkg: the package description
2234 - classnum: the package class
2235 - additional: arrayref of additional invoice details to add to this package
2237 and, I<if the charge has not yet been billed>:
2238 - start_date: the date when it will be billed
2239 - amount: the setup fee to be charged
2240 - quantity: the multiplier for the setup fee
2242 If you pass 'adjust_commission' => 1, and the classnum changes, and there are
2243 commission credits linked to this charge, they will be recalculated.
2250 my $part_pkg = $self->part_pkg;
2251 my $pkgnum = $self->pkgnum;
2254 my $oldAutoCommit = $FS::UID::AutoCommit;
2255 local $FS::UID::AutoCommit = 0;
2257 return "Can't use modify_charge except on one-time charges"
2258 unless $part_pkg->freq eq '0';
2260 if ( length($opt{'pkg'}) and $part_pkg->pkg ne $opt{'pkg'} ) {
2261 $part_pkg->set('pkg', $opt{'pkg'});
2264 my %pkg_opt = $part_pkg->options;
2265 if ( ref($opt{'additional'}) ) {
2266 delete $pkg_opt{$_} foreach grep /^additional/, keys %pkg_opt;
2268 for ( $i = 0; exists($opt{'additional'}->[$i]); $i++ ) {
2269 $pkg_opt{ "additional_info$i" } = $opt{'additional'}->[$i];
2271 $pkg_opt{'additional_count'} = $i if $i > 0;
2275 if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} )
2278 $old_classnum = $part_pkg->classnum;
2279 $part_pkg->set('classnum', $opt{'classnum'});
2282 if ( !$self->get('setup') ) {
2283 # not yet billed, so allow amount and quantity
2284 if ( exists($opt{'quantity'})
2285 and $opt{'quantity'} != $self->quantity
2286 and $opt{'quantity'} > 0 ) {
2288 $self->set('quantity', $opt{'quantity'});
2290 if ( exists($opt{'start_date'})
2291 and $opt{'start_date'} != $self->start_date ) {
2293 $self->set('start_date', $opt{'start_date'});
2295 if ($self->modified) { # for quantity or start_date change
2296 my $error = $self->replace;
2297 return $error if $error;
2300 if ( exists($opt{'amount'})
2301 and $part_pkg->option('setup_fee') != $opt{'amount'}
2302 and $opt{'amount'} > 0 ) {
2304 $pkg_opt{'setup_fee'} = $opt{'amount'};
2305 # standard for one-time charges is to set comment = (formatted) amount
2306 # update it to avoid confusion
2307 my $conf = FS::Conf->new;
2308 $part_pkg->set('comment',
2309 ($conf->config('money_char') || '$') .
2310 sprintf('%.2f', $opt{'amount'})
2313 } # else simply ignore them; the UI shouldn't allow editing the fields
2315 my $error = $part_pkg->replace( options => \%pkg_opt );
2317 $dbh->rollback if $oldAutoCommit;
2321 if (defined $old_classnum) {
2322 # fix invoice grouping records
2323 my $old_catname = $old_classnum
2324 ? FS::pkg_class->by_key($old_classnum)->categoryname
2326 my $new_catname = $opt{'classnum'}
2327 ? $part_pkg->pkg_class->categoryname
2329 if ( $old_catname ne $new_catname ) {
2330 foreach my $cust_bill_pkg ($self->cust_bill_pkg) {
2331 # (there should only be one...)
2332 my @display = qsearch( 'cust_bill_pkg_display', {
2333 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2334 'section' => $old_catname,
2336 foreach (@display) {
2337 $_->set('section', $new_catname);
2338 $error = $_->replace;
2340 $dbh->rollback if $oldAutoCommit;
2344 } # foreach $cust_bill_pkg
2347 if ( $opt{'adjust_commission'} ) {
2348 # fix commission credits...tricky.
2349 foreach my $cust_event ($self->cust_event) {
2350 my $part_event = $cust_event->part_event;
2351 foreach my $table (qw(sales agent)) {
2353 "FS::part_event::Action::Mixin::credit_${table}_pkg_class";
2354 my $credit = qsearchs('cust_credit', {
2355 'eventnum' => $cust_event->eventnum,
2357 if ( $part_event->isa($class) ) {
2358 # Yes, this results in current commission rates being applied
2359 # retroactively to a one-time charge. For accounting purposes
2360 # there ought to be some kind of time limit on doing this.
2361 my $amount = $part_event->_calc_credit($self);
2362 if ( $credit and $credit->amount ne $amount ) {
2363 # Void the old credit.
2364 $error = $credit->void('Package class changed');
2366 $dbh->rollback if $oldAutoCommit;
2367 return "$error (adjusting commission credit)";
2370 # redo the event action to recreate the credit.
2372 eval { $part_event->do_action( $self, $cust_event ) };
2374 $dbh->rollback if $oldAutoCommit;
2377 } # if $part_event->isa($class)
2379 } # foreach $cust_event
2380 } # if $opt{'adjust_commission'}
2381 } # if defined $old_classnum
2383 $dbh->commit if $oldAutoCommit;
2389 use Storable 'thaw';
2392 sub process_bulk_cust_pkg {
2394 my $param = thaw(decode_base64(shift));
2395 warn Dumper($param) if $DEBUG;
2397 my $old_part_pkg = qsearchs('part_pkg',
2398 { pkgpart => $param->{'old_pkgpart'} });
2399 my $new_part_pkg = qsearchs('part_pkg',
2400 { pkgpart => $param->{'new_pkgpart'} });
2401 die "Must select a new package type\n" unless $new_part_pkg;
2402 #my $keep_dates = $param->{'keep_dates'} || 0;
2403 my $keep_dates = 1; # there is no good reason to turn this off
2405 my $oldAutoCommit = $FS::UID::AutoCommit;
2406 local $FS::UID::AutoCommit = 0;
2409 my @cust_pkgs = qsearch('cust_pkg', { 'pkgpart' => $param->{'old_pkgpart'} } );
2412 foreach my $old_cust_pkg ( @cust_pkgs ) {
2414 $job->update_statustext(int(100*$i/(scalar @cust_pkgs)));
2415 if ( $old_cust_pkg->getfield('cancel') ) {
2416 warn '[process_bulk_cust_pkg ] skipping canceled pkgnum '.
2417 $old_cust_pkg->pkgnum."\n"
2421 warn '[process_bulk_cust_pkg] changing pkgnum '.$old_cust_pkg->pkgnum."\n"
2423 my $error = $old_cust_pkg->change(
2424 'pkgpart' => $param->{'new_pkgpart'},
2425 'keep_dates' => $keep_dates
2427 if ( !ref($error) ) { # change returns the cust_pkg on success
2429 die "Error changing pkgnum ".$old_cust_pkg->pkgnum.": '$error'\n";
2432 $dbh->commit if $oldAutoCommit;
2438 Returns the last bill date, or if there is no last bill date, the setup date.
2439 Useful for billing metered services.
2445 return $self->setfield('last_bill', $_[0]) if @_;
2446 return $self->getfield('last_bill') if $self->getfield('last_bill');
2447 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
2448 'edate' => $self->bill, } );
2449 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
2452 =item last_cust_pkg_reason ACTION
2454 Returns the most recent ACTION FS::cust_pkg_reason associated with the package.
2455 Returns false if there is no reason or the package is not currenly ACTION'd
2456 ACTION is one of adjourn, susp, cancel, or expire.
2460 sub last_cust_pkg_reason {
2461 my ( $self, $action ) = ( shift, shift );
2462 my $date = $self->get($action);
2464 'table' => 'cust_pkg_reason',
2465 'hashref' => { 'pkgnum' => $self->pkgnum,
2466 'action' => substr(uc($action), 0, 1),
2469 'order_by' => 'ORDER BY num DESC LIMIT 1',
2473 =item last_reason ACTION
2475 Returns the most recent ACTION FS::reason associated with the package.
2476 Returns false if there is no reason or the package is not currenly ACTION'd
2477 ACTION is one of adjourn, susp, cancel, or expire.
2482 my $cust_pkg_reason = shift->last_cust_pkg_reason(@_);
2483 $cust_pkg_reason->reason
2484 if $cust_pkg_reason;
2489 Returns the definition for this billing item, as an FS::part_pkg object (see
2496 return $self->{'_pkgpart'} if $self->{'_pkgpart'};
2497 cluck "cust_pkg->part_pkg called" if $DEBUG > 1;
2498 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
2503 Returns the cancelled package this package was changed from, if any.
2509 return '' unless $self->change_pkgnum;
2510 qsearchs('cust_pkg', { 'pkgnum' => $self->change_pkgnum } );
2513 =item change_cust_main
2515 Returns the customter this package was detached to, if any.
2519 sub change_cust_main {
2521 return '' unless $self->change_custnum;
2522 qsearchs('cust_main', { 'custnum' => $self->change_custnum } );
2527 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
2534 $self->part_pkg->calc_setup($self, @_);
2539 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
2546 $self->part_pkg->calc_recur($self, @_);
2551 Calls the I<base_setup> of the FS::part_pkg object associated with this billing
2558 $self->part_pkg->base_setup($self, @_);
2563 Calls the I<base_recur> of the FS::part_pkg object associated with this billing
2570 $self->part_pkg->base_recur($self, @_);
2575 Calls the I<calc_remain> of the FS::part_pkg object associated with this
2582 $self->part_pkg->calc_remain($self, @_);
2587 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
2594 $self->part_pkg->calc_cancel($self, @_);
2599 Returns any invoice line items for this package (see L<FS::cust_bill_pkg>).
2605 qsearch( 'cust_bill_pkg', { 'pkgnum' => $self->pkgnum } );
2608 =item cust_pkg_detail [ DETAILTYPE ]
2610 Returns any customer package details for this package (see
2611 L<FS::cust_pkg_detail>).
2613 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2617 sub cust_pkg_detail {
2619 my %hash = ( 'pkgnum' => $self->pkgnum );
2620 $hash{detailtype} = shift if @_;
2622 'table' => 'cust_pkg_detail',
2623 'hashref' => \%hash,
2624 'order_by' => 'ORDER BY weight, pkgdetailnum',
2628 =item set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
2630 Sets customer package details for this package (see L<FS::cust_pkg_detail>).
2632 DETAILTYPE can be set to "I" for invoice details or "C" for comments.
2634 If there is an error, returns the error, otherwise returns false.
2638 sub set_cust_pkg_detail {
2639 my( $self, $detailtype, @details ) = @_;
2641 my $oldAutoCommit = $FS::UID::AutoCommit;
2642 local $FS::UID::AutoCommit = 0;
2645 foreach my $current ( $self->cust_pkg_detail($detailtype) ) {
2646 my $error = $current->delete;
2648 $dbh->rollback if $oldAutoCommit;
2649 return "error removing old detail: $error";
2653 foreach my $detail ( @details ) {
2654 my $cust_pkg_detail = new FS::cust_pkg_detail {
2655 'pkgnum' => $self->pkgnum,
2656 'detailtype' => $detailtype,
2657 'detail' => $detail,
2659 my $error = $cust_pkg_detail->insert;
2661 $dbh->rollback if $oldAutoCommit;
2662 return "error adding new detail: $error";
2667 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2674 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
2678 #false laziness w/cust_bill.pm
2682 'table' => 'cust_event',
2683 'addl_from' => 'JOIN part_event USING ( eventpart )',
2684 'hashref' => { 'tablenum' => $self->pkgnum },
2685 'extra_sql' => " AND eventtable = 'cust_pkg' ",
2689 =item num_cust_event
2691 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
2695 #false laziness w/cust_bill.pm
2696 sub num_cust_event {
2699 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
2700 " WHERE tablenum = ? AND eventtable = 'cust_pkg'";
2701 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
2702 $sth->execute($self->pkgnum) or die $sth->errstr. " executing $sql";
2703 $sth->fetchrow_arrayref->[0];
2706 =item part_pkg_currency_option OPTIONNAME
2708 Returns a two item list consisting of the currency of this customer, if any,
2709 and a value for the provided option. If the customer has a currency, the value
2710 is the option value the given name and the currency (see
2711 L<FS::part_pkg_currency>). Otherwise, if the customer has no currency, is the
2712 regular option value for the given name (see L<FS::part_pkg_option>).
2716 sub part_pkg_currency_option {
2717 my( $self, $optionname ) = @_;
2718 my $part_pkg = $self->part_pkg;
2719 if ( my $currency = $self->cust_main->currency ) {
2720 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
2722 ('', $part_pkg->option($optionname) );
2726 =item cust_svc [ SVCPART ] (old, deprecated usage)
2728 =item cust_svc [ OPTION => VALUE ... ] (current usage)
2730 =item cust_svc_unsorted [ OPTION => VALUE ... ]
2732 Returns the services for this package, as FS::cust_svc objects (see
2733 L<FS::cust_svc>). Available options are svcpart and svcdb. If either is
2734 spcififed, returns only the matching services.
2736 As an optimization, use the cust_svc_unsorted version if you are not displaying
2743 cluck "cust_pkg->cust_svc called" if $DEBUG > 2;
2744 $self->_sort_cust_svc( $self->cust_svc_unsorted_arrayref(@_) );
2747 sub cust_svc_unsorted {
2749 @{ $self->cust_svc_unsorted_arrayref(@_) };
2752 sub cust_svc_unsorted_arrayref {
2755 return () unless $self->num_cust_svc(@_);
2758 if ( @_ && $_[0] =~ /^\d+/ ) {
2759 $opt{svcpart} = shift;
2760 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2767 'table' => 'cust_svc',
2768 'hashref' => { 'pkgnum' => $self->pkgnum },
2770 if ( $opt{svcpart} ) {
2771 $search{hashref}->{svcpart} = $opt{'svcpart'};
2773 if ( $opt{'svcdb'} ) {
2774 $search{addl_from} = ' LEFT JOIN part_svc USING ( svcpart ) ';
2775 $search{extra_sql} = ' AND svcdb = '. dbh->quote( $opt{'svcdb'} );
2778 [ qsearch(\%search) ];
2782 =item overlimit [ SVCPART ]
2784 Returns the services for this package which have exceeded their
2785 usage limit as FS::cust_svc objects (see L<FS::cust_svc>). If a svcpart
2786 is specified, return only the matching services.
2792 return () unless $self->num_cust_svc(@_);
2793 grep { $_->overlimit } $self->cust_svc(@_);
2796 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
2798 Returns historical services for this package created before END TIMESTAMP and
2799 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
2800 (see L<FS::h_cust_svc>). If MODE is 'I' (for 'invoice'), services with the
2801 I<pkg_svc.hidden> flag will be omitted.
2807 warn "$me _h_cust_svc called on $self\n"
2810 my ($end, $start, $mode) = @_;
2811 my @cust_svc = $self->_sort_cust_svc(
2812 [ qsearch( 'h_cust_svc',
2813 { 'pkgnum' => $self->pkgnum, },
2814 FS::h_cust_svc->sql_h_search(@_),
2817 if ( defined($mode) && $mode eq 'I' ) {
2818 my %hidden_svcpart = map { $_->svcpart => $_->hidden } $self->part_svc;
2819 return grep { !$hidden_svcpart{$_->svcpart} } @cust_svc;
2825 sub _sort_cust_svc {
2826 my( $self, $arrayref ) = @_;
2829 sub ($$) { my ($a, $b) = @_; $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] };
2831 my %pkg_svc = map { $_->svcpart => $_ }
2832 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
2837 my $pkg_svc = $pkg_svc{ $_->svcpart } || '';
2839 $pkg_svc ? $pkg_svc->primary_svc : '',
2840 $pkg_svc ? $pkg_svc->quantity : 0,
2847 =item num_cust_svc [ SVCPART ] (old, deprecated usage)
2849 =item num_cust_svc [ OPTION => VALUE ... ] (current usage)
2851 Returns the number of services for this package. Available options are svcpart
2852 and svcdb. If either is spcififed, returns only the matching services.
2859 return $self->{'_num_cust_svc'}
2861 && exists($self->{'_num_cust_svc'})
2862 && $self->{'_num_cust_svc'} =~ /\d/;
2864 cluck "cust_pkg->num_cust_svc called, _num_cust_svc:".$self->{'_num_cust_svc'}
2868 if ( @_ && $_[0] =~ /^\d+/ ) {
2869 $opt{svcpart} = shift;
2870 } elsif ( @_ && ref($_[0]) eq 'HASH' ) {
2876 my $select = 'SELECT COUNT(*) FROM cust_svc ';
2877 my $where = ' WHERE pkgnum = ? ';
2878 my @param = ($self->pkgnum);
2880 if ( $opt{'svcpart'} ) {
2881 $where .= ' AND svcpart = ? ';
2882 push @param, $opt{'svcpart'};
2884 if ( $opt{'svcdb'} ) {
2885 $select .= ' LEFT JOIN part_svc USING ( svcpart ) ';
2886 $where .= ' AND svcdb = ? ';
2887 push @param, $opt{'svcdb'};
2890 my $sth = dbh->prepare("$select $where") or die dbh->errstr;
2891 $sth->execute(@param) or die $sth->errstr;
2892 $sth->fetchrow_arrayref->[0];
2895 =item available_part_svc
2897 Returns a list of FS::part_svc objects representing services included in this
2898 package but not yet provisioned. Each FS::part_svc object also has an extra
2899 field, I<num_avail>, which specifies the number of available services.
2903 sub available_part_svc {
2906 my $pkg_quantity = $self->quantity || 1;
2908 grep { $_->num_avail > 0 }
2910 my $part_svc = $_->part_svc;
2911 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
2912 $pkg_quantity * $_->quantity - $self->num_cust_svc($_->svcpart);
2914 # more evil encapsulation breakage
2915 if($part_svc->{'Hash'}{'num_avail'} > 0) {
2916 my @exports = $part_svc->part_export_did;
2917 $part_svc->{'Hash'}{'can_get_dids'} = scalar(@exports);
2922 $self->part_pkg->pkg_svc;
2925 =item part_svc [ OPTION => VALUE ... ]
2927 Returns a list of FS::part_svc objects representing provisioned and available
2928 services included in this package. Each FS::part_svc object also has the
2929 following extra fields:
2933 =item num_cust_svc (count)
2935 =item num_avail (quantity - count)
2937 =item cust_pkg_svc (services) - array reference containing the provisioned services, as cust_svc objects
2941 Accepts one option: summarize_size. If specified and non-zero, will omit the
2942 extra cust_pkg_svc option for objects where num_cust_svc is this size or
2948 #label -> ($cust_svc->label)[1]
2954 my $pkg_quantity = $self->quantity || 1;
2956 #XXX some sort of sort order besides numeric by svcpart...
2957 my @part_svc = sort { $a->svcpart <=> $b->svcpart } map {
2959 my $part_svc = $pkg_svc->part_svc;
2960 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
2961 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #more evil
2962 $part_svc->{'Hash'}{'num_avail'} =
2963 max( 0, $pkg_quantity * $pkg_svc->quantity - $num_cust_svc );
2964 $part_svc->{'Hash'}{'cust_pkg_svc'} =
2965 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : []
2966 unless exists($opt{summarize_size}) && $opt{summarize_size} > 0
2967 && $num_cust_svc >= $opt{summarize_size};
2968 $part_svc->{'Hash'}{'hidden'} = $pkg_svc->hidden;
2970 } $self->part_pkg->pkg_svc;
2973 push @part_svc, map {
2975 my $num_cust_svc = $self->num_cust_svc($part_svc->svcpart);
2976 $part_svc->{'Hash'}{'num_cust_svc'} = $num_cust_svc; #speak no evail
2977 $part_svc->{'Hash'}{'num_avail'} = 0; #0-$num_cust_svc ?
2978 $part_svc->{'Hash'}{'cust_pkg_svc'} =
2979 $num_cust_svc ? [ $self->cust_svc($part_svc->svcpart) ] : [];
2981 } $self->extra_part_svc;
2987 =item extra_part_svc
2989 Returns a list of FS::part_svc objects corresponding to services in this
2990 package which are still provisioned but not (any longer) available in the
2995 sub extra_part_svc {
2998 my $pkgnum = $self->pkgnum;
2999 #my $pkgpart = $self->pkgpart;
3002 # 'table' => 'part_svc',
3005 # "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
3006 # WHERE pkg_svc.svcpart = part_svc.svcpart
3007 # AND pkg_svc.pkgpart = ?
3010 # AND 0 < ( SELECT COUNT(*) FROM cust_svc
3011 # LEFT JOIN cust_pkg USING ( pkgnum )
3012 # WHERE cust_svc.svcpart = part_svc.svcpart
3015 # 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
3018 #seems to benchmark slightly faster... (or did?)
3020 my @pkgparts = map $_->pkgpart, $self->part_pkg->self_and_svc_linked;
3021 my $pkgparts = join(',', @pkgparts);
3024 #'select' => 'DISTINCT ON (svcpart) part_svc.*',
3025 #MySQL doesn't grok DISINCT ON
3026 'select' => 'DISTINCT part_svc.*',
3027 'table' => 'part_svc',
3029 "LEFT JOIN pkg_svc ON ( pkg_svc.svcpart = part_svc.svcpart
3030 AND pkg_svc.pkgpart IN ($pkgparts)
3033 LEFT JOIN cust_svc ON ( cust_svc.svcpart = part_svc.svcpart )
3034 LEFT JOIN cust_pkg USING ( pkgnum )
3037 'extra_sql' => "WHERE pkgsvcnum IS NULL AND cust_pkg.pkgnum = ? ",
3038 'extra_param' => [ [$self->pkgnum=>'int'] ],
3044 Returns a short status string for this package, currently:
3048 =item not yet billed
3050 =item one-time charge
3065 my $freq = length($self->freq) ? $self->freq : $self->part_pkg->freq;
3067 return 'cancelled' if $self->get('cancel');
3068 return 'suspended' if $self->susp;
3069 return 'not yet billed' unless $self->setup;
3070 return 'one-time charge' if $freq =~ /^(0|$)/;
3074 =item ucfirst_status
3076 Returns the status with the first character capitalized.
3080 sub ucfirst_status {
3081 ucfirst(shift->status);
3086 Class method that returns the list of possible status strings for packages
3087 (see L<the status method|/status>). For example:
3089 @statuses = FS::cust_pkg->statuses();
3093 tie my %statuscolor, 'Tie::IxHash',
3094 'not yet billed' => '009999', #teal? cyan?
3095 'one-time charge' => '000000',
3096 'active' => '00CC00',
3097 'suspended' => 'FF9900',
3098 'cancelled' => 'FF0000',
3102 my $self = shift; #could be class...
3103 #grep { $_ !~ /^(not yet billed)$/ } #this is a dumb status anyway
3104 # # mayble split btw one-time vs. recur
3110 Returns a hex triplet color string for this package's status.
3116 $statuscolor{$self->status};
3121 Returns a label for this package. (Currently "pkgnum: pkg - comment" or
3122 "pkg - comment" depending on user preference).
3128 my $label = $self->part_pkg->pkg_comment( cust_pkg=>$self, nopkgpart=>1 );
3129 $label = $self->pkgnum. ": $label"
3130 if $FS::CurrentUser::CurrentUser->option('show_pkgnum');
3134 =item pkg_label_long
3136 Returns a long label for this package, adding the primary service's label to
3141 sub pkg_label_long {
3143 my $label = $self->pkg_label;
3144 my $cust_svc = $self->primary_cust_svc;
3145 $label .= ' ('. ($cust_svc->label)[1]. ')' if $cust_svc;
3151 Returns a customer-localized label for this package.
3157 $self->part_pkg->pkg_locale( $self->cust_main->locale );
3160 =item primary_cust_svc
3162 Returns a primary service (as FS::cust_svc object) if one can be identified.
3166 #for labeling purposes - might not 100% match up with part_pkg->svcpart's idea
3168 sub primary_cust_svc {
3171 my @cust_svc = $self->cust_svc;
3173 return '' unless @cust_svc; #no serivces - irrelevant then
3175 return $cust_svc[0] if scalar(@cust_svc) == 1; #always return a single service
3177 # primary service as specified in the package definition
3178 # or exactly one service definition with quantity one
3179 my $svcpart = $self->part_pkg->svcpart;
3180 @cust_svc = grep { $_->svcpart == $svcpart } @cust_svc;
3181 return $cust_svc[0] if scalar(@cust_svc) == 1;
3183 #couldn't identify one thing..
3189 Returns a list of lists, calling the label method for all services
3190 (see L<FS::cust_svc>) of this billing item.
3196 map { [ $_->label ] } $self->cust_svc;
3199 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
3201 Like the labels method, but returns historical information on services that
3202 were active as of END_TIMESTAMP and (optionally) not cancelled before
3203 START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the
3204 I<pkg_svc.hidden> flag will be omitted.
3206 Returns a list of lists, calling the label method for all (historical) services
3207 (see L<FS::h_cust_svc>) of this billing item.
3213 warn "$me _h_labels called on $self\n"
3215 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
3220 Like labels, except returns a simple flat list, and shortens long
3221 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3222 identical services to one line that lists the service label and the number of
3223 individual services rather than individual items.
3228 shift->_labels_short( 'labels', @_ );
3231 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
3233 Like h_labels, except returns a simple flat list, and shortens long
3234 (currently >5 or the cust_bill-max_same_services configuration value) lists of
3235 identical services to one line that lists the service label and the number of
3236 individual services rather than individual items.
3240 sub h_labels_short {
3241 shift->_labels_short( 'h_labels', @_ );
3245 my( $self, $method ) = ( shift, shift );
3247 warn "$me _labels_short called on $self with $method method\n"
3250 my $conf = new FS::Conf;
3251 my $max_same_services = $conf->config('cust_bill-max_same_services') || 5;
3253 warn "$me _labels_short populating \%labels\n"
3257 #tie %labels, 'Tie::IxHash';
3258 push @{ $labels{$_->[0]} }, $_->[1]
3259 foreach $self->$method(@_);
3261 warn "$me _labels_short populating \@labels\n"
3265 foreach my $label ( keys %labels ) {
3267 my @values = grep { ! $seen{$_}++ } @{ $labels{$label} };
3268 my $num = scalar(@values);
3269 warn "$me _labels_short $num items for $label\n"
3272 if ( $num > $max_same_services ) {
3273 warn "$me _labels_short more than $max_same_services, so summarizing\n"
3275 push @labels, "$label ($num)";
3277 if ( $conf->exists('cust_bill-consolidate_services') ) {
3278 warn "$me _labels_short consolidating services\n"
3280 # push @labels, "$label: ". join(', ', @values);
3282 my $detail = "$label: ";
3283 $detail .= shift(@values). ', '
3285 && ( length($detail.$values[0]) < 78 || $detail eq "$label: " );
3287 push @labels, $detail;
3289 warn "$me _labels_short done consolidating services\n"
3292 warn "$me _labels_short adding service data\n"
3294 push @labels, map { "$label: $_" } @values;
3305 Returns the parent customer object (see L<FS::cust_main>).
3309 Returns the balance for this specific package, when using
3310 experimental package balance.
3316 $self->cust_main->balance_pkgnum( $self->pkgnum );
3319 #these subs are in location_Mixin.pm now... unfortunately the POD doesn't mixin
3323 Returns the location object, if any (see L<FS::cust_location>).
3325 =item cust_location_or_main
3327 If this package is associated with a location, returns the locaiton (see
3328 L<FS::cust_location>), otherwise returns the customer (see L<FS::cust_main>).
3330 =item location_label [ OPTION => VALUE ... ]
3332 Returns the label of the location object (see L<FS::cust_location>).
3336 #end of subs in location_Mixin.pm now... unfortunately the POD doesn't mixin
3338 =item tax_locationnum
3340 Returns the foreign key to a L<FS::cust_location> object for calculating
3341 tax on this package, as determined by the C<tax-pkg_address> and
3342 C<tax-ship_address> configuration flags.
3346 sub tax_locationnum {
3348 my $conf = FS::Conf->new;
3349 if ( $conf->exists('tax-pkg_address') ) {
3350 return $self->locationnum;
3352 elsif ( $conf->exists('tax-ship_address') ) {
3353 return $self->cust_main->ship_locationnum;
3356 return $self->cust_main->bill_locationnum;
3362 Returns the L<FS::cust_location> object for tax_locationnum.
3368 FS::cust_location->by_key( $self->tax_locationnum )
3371 =item seconds_since TIMESTAMP
3373 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
3374 package have been online since TIMESTAMP, according to the session monitor.
3376 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
3377 L<Time::Local> and L<Date::Parse> for conversion functions.
3382 my($self, $since) = @_;
3385 foreach my $cust_svc (
3386 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
3388 $seconds += $cust_svc->seconds_since($since);
3395 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
3397 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
3398 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
3401 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3402 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3408 sub seconds_since_sqlradacct {
3409 my($self, $start, $end) = @_;
3413 foreach my $cust_svc (
3415 my $part_svc = $_->part_svc;
3416 $part_svc->svcdb eq 'svc_acct'
3417 && scalar($part_svc->part_export_usage);
3420 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
3427 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
3429 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
3430 in this package for sessions ending between TIMESTAMP_START (inclusive) and
3434 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
3435 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
3440 sub attribute_since_sqlradacct {
3441 my($self, $start, $end, $attrib) = @_;
3445 foreach my $cust_svc (
3447 my $part_svc = $_->part_svc;
3448 scalar($part_svc->part_export_usage);
3451 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
3463 my( $self, $value ) = @_;
3464 if ( defined($value) ) {
3465 $self->setfield('quantity', $value);
3467 $self->getfield('quantity') || 1;
3470 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
3472 Transfers as many services as possible from this package to another package.
3474 The destination package can be specified by pkgnum by passing an FS::cust_pkg
3475 object. The destination package must already exist.
3477 Services are moved only if the destination allows services with the correct
3478 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
3479 this option with caution! No provision is made for export differences
3480 between the old and new service definitions. Probably only should be used
3481 when your exports for all service definitions of a given svcdb are identical.
3482 (attempt a transfer without it first, to move all possible svcpart-matching
3485 Any services that can't be moved remain in the original package.
3487 Returns an error, if there is one; otherwise, returns the number of services
3488 that couldn't be moved.
3493 my ($self, $dest_pkgnum, %opt) = @_;
3499 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
3500 $dest = $dest_pkgnum;
3501 $dest_pkgnum = $dest->pkgnum;
3503 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
3506 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
3508 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
3509 $target{$pkg_svc->svcpart} = $pkg_svc->quantity;
3512 foreach my $cust_svc ($dest->cust_svc) {
3513 $target{$cust_svc->svcpart}--;
3516 my %svcpart2svcparts = ();
3517 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3518 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
3519 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
3520 next if exists $svcpart2svcparts{$svcpart};
3521 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
3522 $svcpart2svcparts{$svcpart} = [
3524 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
3526 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
3527 'svcpart' => $_ } );
3529 $pkg_svc ? $pkg_svc->primary_svc : '',
3530 $pkg_svc ? $pkg_svc->quantity : 0,
3534 grep { $_ != $svcpart }
3536 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
3538 warn "alternates for svcpart $svcpart: ".
3539 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
3544 foreach my $cust_svc ($self->cust_svc) {
3545 my $svcnum = $cust_svc->svcnum;
3546 if($target{$cust_svc->svcpart} > 0
3547 or $FS::cust_svc::ignore_quantity) { # maybe should be a 'force' option
3548 $target{$cust_svc->svcpart}--;
3549 my $new = new FS::cust_svc { $cust_svc->hash };
3550 $new->pkgnum($dest_pkgnum);
3551 my $error = $new->replace($cust_svc);
3552 return "svcnum $svcnum: $error" if $error;
3553 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
3555 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
3556 warn "alternates to consider: ".
3557 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
3559 my @alternate = grep {
3560 warn "considering alternate svcpart $_: ".
3561 "$target{$_} available in new package\n"
3564 } @{$svcpart2svcparts{$cust_svc->svcpart}};
3566 warn "alternate(s) found\n" if $DEBUG;
3567 my $change_svcpart = $alternate[0];
3568 $target{$change_svcpart}--;
3569 my $new = new FS::cust_svc { $cust_svc->hash };
3570 $new->svcpart($change_svcpart);
3571 $new->pkgnum($dest_pkgnum);
3572 my $error = $new->replace($cust_svc);
3573 return "svcnum $svcnum: $error" if $error;
3584 =item grab_svcnums SVCNUM, SVCNUM ...
3586 Change the pkgnum for the provided services to this packages. If there is an
3587 error, returns the error, otherwise returns false.
3595 my $oldAutoCommit = $FS::UID::AutoCommit;
3596 local $FS::UID::AutoCommit = 0;
3599 foreach my $svcnum (@svcnum) {
3600 my $cust_svc = qsearchs('cust_svc', { svcnum=>$svcnum } ) or do {
3601 $dbh->rollback if $oldAutoCommit;
3602 return "unknown svcnum $svcnum";
3604 $cust_svc->pkgnum( $self->pkgnum );
3605 my $error = $cust_svc->replace;
3607 $dbh->rollback if $oldAutoCommit;
3612 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3619 This method is deprecated. See the I<depend_jobnum> option to the insert and
3620 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
3624 #looks like this is still used by the order_pkg and change_pkg methods in
3625 # ClientAPI/MyAccount, need to look into those before removing
3629 my $oldAutoCommit = $FS::UID::AutoCommit;
3630 local $FS::UID::AutoCommit = 0;
3633 foreach my $cust_svc ( $self->cust_svc ) {
3634 #false laziness w/svc_Common::insert
3635 my $svc_x = $cust_svc->svc_x;
3636 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
3637 my $error = $part_export->export_insert($svc_x);
3639 $dbh->rollback if $oldAutoCommit;
3645 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3650 =item export_pkg_change OLD_CUST_PKG
3652 Calls the "pkg_change" export action for all services attached to this package.
3656 sub export_pkg_change {
3657 my( $self, $old ) = ( shift, shift );
3659 my $oldAutoCommit = $FS::UID::AutoCommit;
3660 local $FS::UID::AutoCommit = 0;
3663 foreach my $svc_x ( map $_->svc_x, $self->cust_svc ) {
3664 my $error = $svc_x->export('pkg_change', $self, $old);
3666 $dbh->rollback if $oldAutoCommit;
3671 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3678 Associates this package with a (suspension or cancellation) reason (see
3679 L<FS::cust_pkg_reason>, possibly inserting a new reason on the fly (see
3682 Available options are:
3688 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.
3692 the access_user (see L<FS::access_user>) providing the reason
3700 the action (cancel, susp, adjourn, expire) associated with the reason
3704 If there is an error, returns the error, otherwise returns false.
3709 my ($self, %options) = @_;
3711 my $otaker = $options{reason_otaker} ||
3712 $FS::CurrentUser::CurrentUser->username;
3715 if ( $options{'reason'} =~ /^(\d+)$/ ) {
3719 } elsif ( ref($options{'reason'}) ) {
3721 return 'Enter a new reason (or select an existing one)'
3722 unless $options{'reason'}->{'reason'} !~ /^\s*$/;
3724 my $reason = new FS::reason({
3725 'reason_type' => $options{'reason'}->{'typenum'},
3726 'reason' => $options{'reason'}->{'reason'},
3728 my $error = $reason->insert;
3729 return $error if $error;
3731 $reasonnum = $reason->reasonnum;
3734 return "Unparsable reason: ". $options{'reason'};
3737 my $cust_pkg_reason =
3738 new FS::cust_pkg_reason({ 'pkgnum' => $self->pkgnum,
3739 'reasonnum' => $reasonnum,
3740 'otaker' => $otaker,
3741 'action' => substr(uc($options{'action'}),0,1),
3742 'date' => $options{'date'}
3747 $cust_pkg_reason->insert;
3750 =item insert_discount
3752 Associates this package with a discount (see L<FS::cust_pkg_discount>, possibly
3753 inserting a new discount on the fly (see L<FS::discount>).
3755 Available options are:
3763 If there is an error, returns the error, otherwise returns false.
3767 sub insert_discount {
3768 #my ($self, %options) = @_;
3771 my $cust_pkg_discount = new FS::cust_pkg_discount {
3772 'pkgnum' => $self->pkgnum,
3773 'discountnum' => $self->discountnum,
3775 'end_date' => '', #XXX
3776 #for the create a new discount case
3777 '_type' => $self->discountnum__type,
3778 'amount' => $self->discountnum_amount,
3779 'percent' => $self->discountnum_percent,
3780 'months' => $self->discountnum_months,
3781 'setup' => $self->discountnum_setup,
3782 #'disabled' => $self->discountnum_disabled,
3785 $cust_pkg_discount->insert;
3788 =item set_usage USAGE_VALUE_HASHREF
3790 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3791 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3792 upbytes, downbytes, and totalbytes are appropriate keys.
3794 All svc_accts which are part of this package have their values reset.
3799 my ($self, $valueref, %opt) = @_;
3801 #only svc_acct can set_usage for now
3802 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3803 my $svc_x = $cust_svc->svc_x;
3804 $svc_x->set_usage($valueref, %opt)
3805 if $svc_x->can("set_usage");
3809 =item recharge USAGE_VALUE_HASHREF
3811 USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts
3812 to which they should be set (see L<FS::svc_acct>). Currently seconds,
3813 upbytes, downbytes, and totalbytes are appropriate keys.
3815 All svc_accts which are part of this package have their values incremented.
3820 my ($self, $valueref) = @_;
3822 #only svc_acct can set_usage for now
3823 foreach my $cust_svc ( $self->cust_svc( 'svcdb'=>'svc_acct' ) ) {
3824 my $svc_x = $cust_svc->svc_x;
3825 $svc_x->recharge($valueref)
3826 if $svc_x->can("recharge");
3830 =item apply_usageprice
3834 sub apply_usageprice {
3837 my $oldAutoCommit = $FS::UID::AutoCommit;
3838 local $FS::UID::AutoCommit = 0;
3843 foreach my $cust_pkg_usageprice ( $self->cust_pkg_usageprice ) {
3844 $error ||= $cust_pkg_usageprice->apply;
3848 $dbh->rollback if $oldAutoCommit;
3849 die "error applying part_pkg_usageprice add-ons, pkgnum ". $self->pkgnum.
3852 $dbh->commit if $oldAutoCommit;
3858 =item cust_pkg_discount
3860 =item cust_pkg_discount_active
3864 sub cust_pkg_discount_active {
3866 grep { $_->status eq 'active' } $self->cust_pkg_discount;
3869 =item cust_pkg_usage
3871 Returns a list of all voice usage counters attached to this package.
3873 =item apply_usage OPTIONS
3875 Takes the following options:
3876 - cdr: a call detail record (L<FS::cdr>)
3877 - rate_detail: the rate determined for this call (L<FS::rate_detail>)
3878 - minutes: the maximum number of minutes to be charged
3880 Finds available usage minutes for a call of this class, and subtracts
3881 up to that many minutes from the usage pool. If the usage pool is empty,
3882 and the C<cdr-minutes_priority> global config option is set, minutes may
3883 be taken from other calls as well. Either way, an allocation record will
3884 be created (L<FS::cdr_cust_pkg_usage>) and this method will return the
3885 number of minutes of usage applied to the call.
3890 my ($self, %opt) = @_;
3891 my $cdr = $opt{cdr};
3892 my $rate_detail = $opt{rate_detail};
3893 my $minutes = $opt{minutes};
3894 my $classnum = $rate_detail->classnum;
3895 my $pkgnum = $self->pkgnum;
3896 my $custnum = $self->custnum;
3898 my $oldAutoCommit = $FS::UID::AutoCommit;
3899 local $FS::UID::AutoCommit = 0;
3902 my $order = FS::Conf->new->config('cdr-minutes_priority');
3906 $is_classnum = ' part_pkg_usage_class.classnum = '.$classnum;
3908 $is_classnum = ' part_pkg_usage_class.classnum IS NULL';
3910 my @usage_recs = qsearch({
3911 'table' => 'cust_pkg_usage',
3912 'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)'.
3913 ' JOIN cust_pkg USING (pkgnum)'.
3914 ' JOIN part_pkg_usage_class USING (pkgusagepart)',
3915 'select' => 'cust_pkg_usage.*',
3916 'extra_sql' => " WHERE ( cust_pkg.pkgnum = $pkgnum OR ".
3917 " ( cust_pkg.custnum = $custnum AND ".
3918 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3919 $is_classnum . ' AND '.
3920 " cust_pkg_usage.minutes > 0",
3921 'order_by' => " ORDER BY priority ASC",
3924 my $orig_minutes = $minutes;
3926 while (!$error and $minutes > 0 and @usage_recs) {
3927 my $cust_pkg_usage = shift @usage_recs;
3928 $cust_pkg_usage->select_for_update;
3929 my $cdr_cust_pkg_usage = FS::cdr_cust_pkg_usage->new({
3930 pkgusagenum => $cust_pkg_usage->pkgusagenum,
3931 acctid => $cdr->acctid,
3932 minutes => min($cust_pkg_usage->minutes, $minutes),
3934 $cust_pkg_usage->set('minutes',
3935 sprintf('%.0f', $cust_pkg_usage->minutes - $cdr_cust_pkg_usage->minutes)
3937 $error = $cust_pkg_usage->replace || $cdr_cust_pkg_usage->insert;
3938 $minutes -= $cdr_cust_pkg_usage->minutes;
3940 if ( $order and $minutes > 0 and !$error ) {
3941 # then try to steal minutes from another call
3943 'table' => 'cdr_cust_pkg_usage',
3944 'addl_from' => ' JOIN cust_pkg_usage USING (pkgusagenum)'.
3945 ' JOIN part_pkg_usage USING (pkgusagepart)'.
3946 ' JOIN cust_pkg USING (pkgnum)'.
3947 ' JOIN part_pkg_usage_class USING (pkgusagepart)'.
3948 ' JOIN cdr USING (acctid)',
3949 'select' => 'cdr_cust_pkg_usage.*',
3950 'extra_sql' => " WHERE cdr.freesidestatus = 'rated' AND ".
3951 " ( cust_pkg.pkgnum = $pkgnum OR ".
3952 " ( cust_pkg.custnum = $custnum AND ".
3953 " part_pkg_usage.shared IS NOT NULL ) ) AND ".
3954 " part_pkg_usage_class.classnum = $classnum",
3955 'order_by' => ' ORDER BY part_pkg_usage.priority ASC',
3957 if ( $order eq 'time' ) {
3958 # find CDRs that are using minutes, but have a later startdate
3960 my $startdate = $cdr->startdate;
3961 if ($startdate !~ /^\d+$/) {
3962 die "bad cdr startdate '$startdate'";
3964 $search{'extra_sql'} .= " AND cdr.startdate > $startdate";
3965 # minimize needless reshuffling
3966 $search{'order_by'} .= ', cdr.startdate DESC';
3968 # XXX may not work correctly with rate_time schedules. Could
3969 # fix this by storing ratedetailnum in cdr_cust_pkg_usage, I
3971 $search{'addl_from'} .=
3972 ' JOIN rate_detail'.
3973 ' ON (cdr.rated_ratedetailnum = rate_detail.ratedetailnum)';
3974 if ( $order eq 'rate_high' ) {
3975 $search{'extra_sql'} .= ' AND rate_detail.min_charge < '.
3976 $rate_detail->min_charge;
3977 $search{'order_by'} .= ', rate_detail.min_charge ASC';
3978 } elsif ( $order eq 'rate_low' ) {
3979 $search{'extra_sql'} .= ' AND rate_detail.min_charge > '.
3980 $rate_detail->min_charge;
3981 $search{'order_by'} .= ', rate_detail.min_charge DESC';
3983 # this should really never happen
3984 die "invalid cdr-minutes_priority value '$order'\n";
3987 my @cdr_usage_recs = qsearch(\%search);
3989 while (!$error and @cdr_usage_recs and $minutes > 0) {
3990 my $cdr_cust_pkg_usage = shift @cdr_usage_recs;
3991 my $cust_pkg_usage = $cdr_cust_pkg_usage->cust_pkg_usage;
3992 my $old_cdr = $cdr_cust_pkg_usage->cdr;
3993 $reproc_cdrs{$old_cdr->acctid} = $old_cdr;
3994 $cdr_cust_pkg_usage->select_for_update;
3995 $old_cdr->select_for_update;
3996 $cust_pkg_usage->select_for_update;
3997 # in case someone else stole the usage from this CDR
3998 # while waiting for the lock...
3999 next if $old_cdr->acctid != $cdr_cust_pkg_usage->acctid;
4000 # steal the usage allocation and flag the old CDR for reprocessing
4001 $cdr_cust_pkg_usage->set('acctid', $cdr->acctid);
4002 # if the allocation is more minutes than we need, adjust it...
4003 my $delta = $cdr_cust_pkg_usage->minutes - $minutes;
4005 $cdr_cust_pkg_usage->set('minutes', $minutes);
4006 $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $delta);
4007 $error = $cust_pkg_usage->replace;
4009 #warn 'CDR '.$cdr->acctid . ' stealing allocation '.$cdr_cust_pkg_usage->cdrusagenum.' from CDR '.$old_cdr->acctid."\n";
4010 $error ||= $cdr_cust_pkg_usage->replace;
4011 # deduct the stolen minutes
4012 $minutes -= $cdr_cust_pkg_usage->minutes;
4014 # after all minute-stealing is done, reset the affected CDRs
4015 foreach (values %reproc_cdrs) {
4016 $error ||= $_->set_status('');
4017 # XXX or should we just call $cdr->rate right here?
4018 # it's not like we can create a loop this way, since the min_charge
4019 # or call time has to go monotonically in one direction.
4020 # we COULD get some very deep recursions going, though...
4022 } # if $order and $minutes
4025 die "error applying included minutes\npkgnum ".$self->pkgnum.", class $classnum, acctid ".$cdr->acctid."\n$error\n"
4027 $dbh->commit if $oldAutoCommit;
4028 return $orig_minutes - $minutes;
4032 =item supplemental_pkgs
4034 Returns a list of all packages supplemental to this one.
4038 sub supplemental_pkgs {
4040 qsearch('cust_pkg', { 'main_pkgnum' => $self->pkgnum });
4045 Returns the package that this one is supplemental to, if any.
4051 if ( $self->main_pkgnum ) {
4052 return FS::cust_pkg->by_key($self->main_pkgnum);
4059 =head1 CLASS METHODS
4065 Returns an SQL expression identifying recurring packages.
4069 sub recurring_sql { "
4070 '0' != ( select freq from part_pkg
4071 where cust_pkg.pkgpart = part_pkg.pkgpart )
4076 Returns an SQL expression identifying one-time packages.
4081 '0' = ( select freq from part_pkg
4082 where cust_pkg.pkgpart = part_pkg.pkgpart )
4087 Returns an SQL expression identifying ordered packages (recurring packages not
4093 $_[0]->recurring_sql. " AND ". $_[0]->not_yet_billed_sql;
4098 Returns an SQL expression identifying active packages.
4103 $_[0]->recurring_sql. "
4104 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4105 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4106 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4109 =item not_yet_billed_sql
4111 Returns an SQL expression identifying packages which have not yet been billed.
4115 sub not_yet_billed_sql { "
4116 ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
4117 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4118 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4123 Returns an SQL expression identifying inactive packages (one-time packages
4124 that are otherwise unsuspended/uncancelled).
4128 sub inactive_sql { "
4129 ". $_[0]->onetime_sql(). "
4130 AND cust_pkg.setup IS NOT NULL AND cust_pkg.setup != 0
4131 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4132 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
4138 Returns an SQL expression identifying suspended packages.
4142 sub suspended_sql { susp_sql(@_); }
4144 #$_[0]->recurring_sql(). ' AND '.
4146 ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4147 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
4154 Returns an SQL exprression identifying cancelled packages.
4158 sub cancelled_sql { cancel_sql(@_); }
4160 #$_[0]->recurring_sql(). ' AND '.
4161 "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
4166 Returns an SQL expression to give the package status as a string.
4172 WHEN cust_pkg.cancel IS NOT NULL THEN 'cancelled'
4173 WHEN cust_pkg.susp IS NOT NULL THEN 'suspended'
4174 WHEN cust_pkg.setup IS NULL THEN 'not yet billed'
4175 WHEN ".onetime_sql()." THEN 'one-time charge'
4180 =item search HASHREF
4184 Returns a qsearch hash expression to search for parameters specified in HASHREF.
4185 Valid parameters are
4193 active, inactive, suspended, cancel (or cancelled)
4197 active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
4201 boolean selects custom packages
4207 pkgpart or arrayref or hashref of pkgparts
4211 arrayref of beginning and ending epoch date
4215 arrayref of beginning and ending epoch date
4219 arrayref of beginning and ending epoch date
4223 arrayref of beginning and ending epoch date
4227 arrayref of beginning and ending epoch date
4231 arrayref of beginning and ending epoch date
4235 arrayref of beginning and ending epoch date
4239 pkgnum or APKG_pkgnum
4243 a value suited to passing to FS::UI::Web::cust_header
4247 specifies the user for agent virtualization
4251 boolean; if true, returns only packages with more than 0 FCC phone lines.
4253 =item state, country
4255 Limit to packages with a service location in the specified state and country.
4256 For FCC 477 reporting, mostly.
4260 Limit to packages whose service locations are the same as the customer's
4261 default service location.
4263 =item location_nocust
4265 Limit to packages whose service locations are not the customer's default
4268 =item location_census
4270 Limit to packages whose service locations have census tracts.
4272 =item location_nocensus
4274 Limit to packages whose service locations do not have a census tract.
4276 =item location_geocode
4278 Limit to packages whose locations have geocodes.
4280 =item location_geocode
4282 Limit to packages whose locations do not have geocodes.
4289 my ($class, $params) = @_;
4296 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
4298 "cust_main.agentnum = $1";
4305 if ( $params->{'cust_status'} =~ /^([a-z]+)$/ ) {
4306 push @where, FS::cust_main->cust_status_sql . " = '$1' ";
4310 # parse customer sales person
4313 if ( $params->{'cust_main_salesnum'} =~ /^(\d+)$/ ) {
4314 push @where, ($1 > 0) ? "cust_main.salesnum = $1"
4315 : 'cust_main.salesnum IS NULL';
4320 # parse sales person
4323 if ( $params->{'salesnum'} =~ /^(\d+)$/ ) {
4324 push @where, ($1 > 0) ? "cust_pkg.salesnum = $1"
4325 : 'cust_pkg.salesnum IS NULL';
4332 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
4334 "cust_pkg.custnum = $1";
4341 if ( $params->{'pkgbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
4343 "cust_pkg.pkgbatch = '$1'";
4350 if ( $params->{'magic'} eq 'active'
4351 || $params->{'status'} eq 'active' ) {
4353 push @where, FS::cust_pkg->active_sql();
4355 } elsif ( $params->{'magic'} =~ /^not[ _]yet[ _]billed$/
4356 || $params->{'status'} =~ /^not[ _]yet[ _]billed$/ ) {
4358 push @where, FS::cust_pkg->not_yet_billed_sql();
4360 } elsif ( $params->{'magic'} =~ /^(one-time charge|inactive)/
4361 || $params->{'status'} =~ /^(one-time charge|inactive)/ ) {
4363 push @where, FS::cust_pkg->inactive_sql();
4365 } elsif ( $params->{'magic'} eq 'suspended'
4366 || $params->{'status'} eq 'suspended' ) {
4368 push @where, FS::cust_pkg->suspended_sql();
4370 } elsif ( $params->{'magic'} =~ /^cancell?ed$/
4371 || $params->{'status'} =~ /^cancell?ed$/ ) {
4373 push @where, FS::cust_pkg->cancelled_sql();
4378 # parse package class
4381 if ( exists($params->{'classnum'}) ) {
4384 if ( ref($params->{'classnum'}) ) {
4386 if ( ref($params->{'classnum'}) eq 'HASH' ) {
4387 @classnum = grep $params->{'classnum'}{$_}, keys %{ $params->{'classnum'} };
4388 } elsif ( ref($params->{'classnum'}) eq 'ARRAY' ) {
4389 @classnum = @{ $params->{'classnum'} };
4391 die 'unhandled classnum ref '. $params->{'classnum'};
4395 } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
4402 my @nums = grep $_, @classnum;
4403 push @c_where, 'part_pkg.classnum IN ('. join(',',@nums). ')' if @nums;
4404 my $null = scalar( grep { $_ eq '' } @classnum );
4405 push @c_where, 'part_pkg.classnum IS NULL' if $null;
4407 if ( scalar(@c_where) == 1 ) {
4408 push @where, @c_where;
4409 } elsif ( @c_where ) {
4410 push @where, ' ( '. join(' OR ', @c_where). ' ) ';
4419 # parse package report options
4422 my @report_option = ();
4423 if ( exists($params->{'report_option'}) ) {
4424 if ( ref($params->{'report_option'}) eq 'ARRAY' ) {
4425 @report_option = @{ $params->{'report_option'} };
4426 } elsif ( $params->{'report_option'} =~ /^([,\d]*)$/ ) {
4427 @report_option = split(',', $1);
4432 if (@report_option) {
4433 # this will result in the empty set for the dangling comma case as it should
4435 map{ "0 < ( SELECT count(*) FROM part_pkg_option
4436 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
4437 AND optionname = 'report_option_$_'
4438 AND optionvalue = '1' )"
4442 foreach my $any ( grep /^report_option_any/, keys %$params ) {
4444 my @report_option_any = ();
4445 if ( ref($params->{$any}) eq 'ARRAY' ) {
4446 @report_option_any = @{ $params->{$any} };
4447 } elsif ( $params->{$any} =~ /^([,\d]*)$/ ) {
4448 @report_option_any = split(',', $1);
4451 if (@report_option_any) {
4452 # this will result in the empty set for the dangling comma case as it should
4453 push @where, ' ( '. join(' OR ',
4454 map{ "0 < ( SELECT count(*) FROM part_pkg_option
4455 WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
4456 AND optionname = 'report_option_$_'
4457 AND optionvalue = '1' )"
4458 } @report_option_any
4468 push @where, "part_pkg.custom = 'Y'" if $params->{custom};
4474 push @where, "(part_pkg.fcc_ds0s > 0 OR pkg_class.fcc_ds0s > 0)"
4475 if $params->{fcc_line};
4481 if ( exists($params->{'censustract'}) ) {
4482 $params->{'censustract'} =~ /^([.\d]*)$/;
4483 my $censustract = "cust_location.censustract = '$1'";
4484 $censustract .= ' OR cust_location.censustract is NULL' unless $1;
4485 push @where, "( $censustract )";
4489 # parse censustract2
4491 if ( exists($params->{'censustract2'})
4492 && $params->{'censustract2'} =~ /^(\d*)$/
4496 push @where, "cust_location.censustract LIKE '$1%'";
4499 "( cust_location.censustract = '' OR cust_location.censustract IS NULL )";
4504 # parse country/state
4506 for (qw(state country)) { # parsing rules are the same for these
4507 if ( exists($params->{$_})
4508 && uc($params->{$_}) =~ /^([A-Z]{2})$/ )
4510 # XXX post-2.3 only--before that, state/country may be in cust_main
4511 push @where, "cust_location.$_ = '$1'";
4518 if ( $params->{location_cust} xor $params->{location_nocust} ) {
4519 my $op = $params->{location_cust} ? '=' : '!=';
4520 push @where, "cust_location.locationnum $op cust_main.ship_locationnum";
4522 if ( $params->{location_census} xor $params->{location_nocensus} ) {
4523 my $op = $params->{location_census} ? "IS NOT NULL" : "IS NULL";
4524 push @where, "cust_location.censustract $op";
4526 if ( $params->{location_geocode} xor $params->{location_nogeocode} ) {
4527 my $op = $params->{location_geocode} ? "IS NOT NULL" : "IS NULL";
4528 push @where, "cust_location.geocode $op";
4535 if ( ref($params->{'pkgpart'}) ) {
4538 if ( ref($params->{'pkgpart'}) eq 'HASH' ) {
4539 @pkgpart = grep $params->{'pkgpart'}{$_}, keys %{ $params->{'pkgpart'} };
4540 } elsif ( ref($params->{'pkgpart'}) eq 'ARRAY' ) {
4541 @pkgpart = @{ $params->{'pkgpart'} };
4543 die 'unhandled pkgpart ref '. $params->{'pkgpart'};
4546 @pkgpart = grep /^(\d+)$/, @pkgpart;
4548 push @where, 'pkgpart IN ('. join(',', @pkgpart). ')' if scalar(@pkgpart);
4550 } elsif ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
4551 push @where, "pkgpart = $1";
4560 #false laziness w/report_cust_pkg.html
4563 'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
4564 'active' => { 'susp'=>1, 'cancel'=>1 },
4565 'suspended' => { 'cancel' => 1 },
4570 if( exists($params->{'active'} ) ) {
4571 # This overrides all the other date-related fields
4572 my($beginning, $ending) = @{$params->{'active'}};
4574 "cust_pkg.setup IS NOT NULL",
4575 "cust_pkg.setup <= $ending",
4576 "(cust_pkg.cancel IS NULL OR cust_pkg.cancel >= $beginning )",
4577 "NOT (".FS::cust_pkg->onetime_sql . ")";
4580 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel )) {
4582 next unless exists($params->{$field});
4584 my($beginning, $ending) = @{$params->{$field}};
4586 next if $beginning == 0 && $ending == 4294967295;
4589 "cust_pkg.$field IS NOT NULL",
4590 "cust_pkg.$field >= $beginning",
4591 "cust_pkg.$field <= $ending";
4593 $orderby ||= "ORDER BY cust_pkg.$field";
4598 $orderby ||= 'ORDER BY bill';
4601 # parse magic, legacy, etc.
4604 if ( $params->{'magic'} &&
4605 $params->{'magic'} =~ /^(active|inactive|suspended|cancell?ed)$/
4608 $orderby = 'ORDER BY pkgnum';
4610 if ( $params->{'pkgpart'} =~ /^(\d+)$/ ) {
4611 push @where, "pkgpart = $1";
4614 } elsif ( $params->{'query'} eq 'pkgnum' ) {
4616 $orderby = 'ORDER BY pkgnum';
4618 } elsif ( $params->{'query'} eq 'APKG_pkgnum' ) {
4620 $orderby = 'ORDER BY pkgnum';
4623 SELECT count(*) FROM pkg_svc
4624 WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
4625 AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
4626 WHERE cust_svc.pkgnum = cust_pkg.pkgnum
4627 AND cust_svc.svcpart = pkg_svc.svcpart
4634 # setup queries, links, subs, etc. for the search
4637 # here is the agent virtualization
4638 if ($params->{CurrentUser}) {
4640 qsearchs('access_user', { username => $params->{CurrentUser} });
4643 push @where, $access_user->agentnums_sql('table'=>'cust_main');
4648 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql('table'=>'cust_main');
4651 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
4653 my $addl_from = 'LEFT JOIN part_pkg USING ( pkgpart ) '.
4654 'LEFT JOIN pkg_class ON ( part_pkg.classnum = pkg_class.classnum ) '.
4655 'LEFT JOIN cust_location USING ( locationnum ) '.
4656 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
4660 if ( $params->{'select_zip5'} ) {
4661 my $zip = 'cust_location.zip';
4663 $select = "DISTINCT substr($zip,1,5) as zip";
4664 $orderby = "ORDER BY substr($zip,1,5)";
4665 $count_query = "SELECT COUNT( DISTINCT substr($zip,1,5) )";
4667 $select = join(', ',
4669 ( map "part_pkg.$_", qw( pkg freq ) ),
4670 'pkg_class.classname',
4671 'cust_main.custnum AS cust_main_custnum',
4672 FS::UI::Web::cust_sql_fields(
4673 $params->{'cust_fields'}
4676 $count_query = 'SELECT COUNT(*)';
4679 $count_query .= " FROM cust_pkg $addl_from $extra_sql";
4682 'table' => 'cust_pkg',
4684 'select' => $select,
4685 'extra_sql' => $extra_sql,
4686 'order_by' => $orderby,
4687 'addl_from' => $addl_from,
4688 'count_query' => $count_query,
4695 Returns a list of two package counts. The first is a count of packages
4696 based on the supplied criteria and the second is the count of residential
4697 packages with those same criteria. Criteria are specified as in the search
4703 my ($class, $params) = @_;
4705 my $sql_query = $class->search( $params );
4707 my $count_sql = delete($sql_query->{'count_query'});
4708 $count_sql =~ s/ FROM/,count(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN 1 END) FROM/
4709 or die "couldn't parse count_sql";
4711 my $count_sth = dbh->prepare($count_sql)
4712 or die "Error preparing $count_sql: ". dbh->errstr;
4714 or die "Error executing $count_sql: ". $count_sth->errstr;
4715 my $count_arrayref = $count_sth->fetchrow_arrayref;
4717 return ( @$count_arrayref );
4721 =item tax_locationnum_sql
4723 Returns an SQL expression for the tax location for a package, based
4724 on the settings of 'tax-pkg_address' and 'tax-ship_address'.
4728 sub tax_locationnum_sql {
4729 my $conf = FS::Conf->new;
4730 if ( $conf->exists('tax-pkg_address') ) {
4731 'cust_pkg.locationnum';
4733 elsif ( $conf->exists('tax-ship_address') ) {
4734 'cust_main.ship_locationnum';
4737 'cust_main.bill_locationnum';
4743 Returns a list: the first item is an SQL fragment identifying matching
4744 packages/customers via location (taking into account shipping and package
4745 address taxation, if enabled), and subsequent items are the parameters to
4746 substitute for the placeholders in that fragment.
4751 my($class, %opt) = @_;
4752 my $ornull = $opt{'ornull'};
4754 my $conf = new FS::Conf;
4756 # '?' placeholders in _location_sql_where
4757 my $x = $ornull ? 3 : 2;
4768 if ( $conf->exists('tax-ship_address') ) {
4771 ( ( ship_last IS NULL OR ship_last = '' )
4772 AND ". _location_sql_where('cust_main', '', $ornull ). "
4774 OR ( ship_last IS NOT NULL AND ship_last != ''
4775 AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
4778 # AND payby != 'COMP'
4780 @main_param = ( @bill_param, @bill_param );
4784 $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP'
4785 @main_param = @bill_param;
4791 if ( $conf->exists('tax-pkg_address') ) {
4793 my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
4796 ( cust_pkg.locationnum IS NULL AND $main_where )
4797 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where )
4800 @param = ( @main_param, @bill_param );
4804 $where = $main_where;
4805 @param = @main_param;
4813 #subroutine, helper for location_sql
4814 sub _location_sql_where {
4816 my $prefix = @_ ? shift : '';
4817 my $ornull = @_ ? shift : '';
4819 # $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
4821 $ornull = $ornull ? ' OR ? IS NULL ' : '';
4823 my $or_empty_city = " OR ( ? = '' AND $table.${prefix}city IS NULL )";
4824 my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL )";
4825 my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL )";
4827 my $text = (driver_name =~ /^mysql/i) ? 'char' : 'text';
4829 # ( $table.${prefix}city = ? $or_empty_city $ornull )
4831 ( $table.district = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4832 AND ( $table.${prefix}city = ? OR ? = '' OR CAST(? AS $text) IS NULL )
4833 AND ( $table.${prefix}county = ? $or_empty_county $ornull )
4834 AND ( $table.${prefix}state = ? $or_empty_state $ornull )
4835 AND $table.${prefix}country = ?
4840 my( $self, $what ) = @_;
4842 my $what_show_zero = $what. '_show_zero';
4843 length($self->$what_show_zero())
4844 ? ($self->$what_show_zero() eq 'Y')
4845 : $self->part_pkg->$what_show_zero();
4852 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
4854 CUSTNUM is a customer (see L<FS::cust_main>)
4856 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
4857 L<FS::part_pkg>) to order for this customer. Duplicates are of course
4860 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
4861 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
4862 new billing items. An error is returned if this is not possible (see
4863 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
4866 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
4867 newly-created cust_pkg objects.
4869 REFNUM, if specified, will specify the FS::pkg_referral record to be created
4870 and inserted. Multiple FS::pkg_referral records can be created by
4871 setting I<refnum> to an array reference of refnums or a hash reference with
4872 refnums as keys. If no I<refnum> is defined, a default FS::pkg_referral
4873 record will be created corresponding to cust_main.refnum.
4878 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg, $refnum) = @_;
4880 my $conf = new FS::Conf;
4882 # Transactionize this whole mess
4883 my $oldAutoCommit = $FS::UID::AutoCommit;
4884 local $FS::UID::AutoCommit = 0;
4888 # my $cust_main = qsearchs('cust_main', { custnum => $custnum });
4889 # return "Customer not found: $custnum" unless $cust_main;
4891 warn "$me order: pkgnums to remove: ". join(',', @$remove_pkgnum). "\n"
4894 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
4897 my $change = scalar(@old_cust_pkg) != 0;
4900 if ( scalar(@old_cust_pkg) == 1 && scalar(@$pkgparts) == 1 ) {
4902 warn "$me order: changing pkgnum ". $old_cust_pkg[0]->pkgnum.
4903 " to pkgpart ". $pkgparts->[0]. "\n"
4906 my $err_or_cust_pkg =
4907 $old_cust_pkg[0]->change( 'pkgpart' => $pkgparts->[0],
4908 'refnum' => $refnum,
4911 unless (ref($err_or_cust_pkg)) {
4912 $dbh->rollback if $oldAutoCommit;
4913 return $err_or_cust_pkg;
4916 push @$return_cust_pkg, $err_or_cust_pkg;
4917 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4922 # Create the new packages.
4923 foreach my $pkgpart (@$pkgparts) {
4925 warn "$me order: inserting pkgpart $pkgpart\n" if $DEBUG;
4927 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
4928 pkgpart => $pkgpart,
4932 $error = $cust_pkg->insert( 'change' => $change );
4933 push @$return_cust_pkg, $cust_pkg;
4935 foreach my $link ($cust_pkg->part_pkg->supp_part_pkg_link) {
4936 my $supp_pkg = FS::cust_pkg->new({
4937 custnum => $custnum,
4938 pkgpart => $link->dst_pkgpart,
4940 main_pkgnum => $cust_pkg->pkgnum,
4943 $error ||= $supp_pkg->insert( 'change' => $change );
4944 push @$return_cust_pkg, $supp_pkg;
4948 $dbh->rollback if $oldAutoCommit;
4953 # $return_cust_pkg now contains refs to all of the newly
4956 # Transfer services and cancel old packages.
4957 foreach my $old_pkg (@old_cust_pkg) {
4959 warn "$me order: transferring services from pkgnum ". $old_pkg->pkgnum. "\n"
4962 foreach my $new_pkg (@$return_cust_pkg) {
4963 $error = $old_pkg->transfer($new_pkg);
4964 if ($error and $error == 0) {
4965 # $old_pkg->transfer failed.
4966 $dbh->rollback if $oldAutoCommit;
4971 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
4972 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
4973 foreach my $new_pkg (@$return_cust_pkg) {
4974 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
4975 if ($error and $error == 0) {
4976 # $old_pkg->transfer failed.
4977 $dbh->rollback if $oldAutoCommit;
4984 # Transfers were successful, but we went through all of the
4985 # new packages and still had services left on the old package.
4986 # We can't cancel the package under the circumstances, so abort.
4987 $dbh->rollback if $oldAutoCommit;
4988 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
4990 $error = $old_pkg->cancel( quiet=>1 );
4996 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5000 =item bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
5002 A bulk change method to change packages for multiple customers.
5004 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
5005 L<FS::part_pkg>) to order for each customer. Duplicates are of course
5008 REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to
5009 replace. The services (see L<FS::cust_svc>) are moved to the
5010 new billing items. An error is returned if this is not possible (see
5013 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
5014 newly-created cust_pkg objects.
5019 my ($pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
5021 # Transactionize this whole mess
5022 my $oldAutoCommit = $FS::UID::AutoCommit;
5023 local $FS::UID::AutoCommit = 0;
5027 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
5030 while(scalar(@old_cust_pkg)) {
5032 my $custnum = $old_cust_pkg[0]->custnum;
5033 my (@remove) = map { $_->pkgnum }
5034 grep { $_->custnum == $custnum } @old_cust_pkg;
5035 @old_cust_pkg = grep { $_->custnum != $custnum } @old_cust_pkg;
5037 my $error = order $custnum, $pkgparts, \@remove, \@return;
5039 push @errors, $error
5041 push @$return_cust_pkg, @return;
5044 if (scalar(@errors)) {
5045 $dbh->rollback if $oldAutoCommit;
5046 return join(' / ', @errors);
5049 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5053 # Used by FS::Upgrade to migrate to a new database.
5054 sub _upgrade_data { # class method
5055 my ($class, %opts) = @_;
5056 $class->_upgrade_otaker(%opts);
5058 # RT#10139, bug resulting in contract_end being set when it shouldn't
5059 'UPDATE cust_pkg SET contract_end = NULL WHERE contract_end = -1',
5060 # RT#10830, bad calculation of prorate date near end of year
5061 # the date range for bill is December 2009, and we move it forward
5062 # one year if it's before the previous bill date (which it should
5064 'UPDATE cust_pkg SET bill = bill + (365*24*60*60) WHERE bill < last_bill
5065 AND bill > 1259654400 AND bill < 1262332800 AND (SELECT plan FROM part_pkg
5066 WHERE part_pkg.pkgpart = cust_pkg.pkgpart) = \'prorate\'',
5067 # RT6628, add order_date to cust_pkg
5068 'update cust_pkg set order_date = (select history_date from h_cust_pkg
5069 where h_cust_pkg.pkgnum = cust_pkg.pkgnum and
5070 history_action = \'insert\') where order_date is null',
5072 foreach my $sql (@statements) {
5073 my $sth = dbh->prepare($sql);
5074 $sth->execute or die $sth->errstr;
5082 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
5084 In sub order, the @pkgparts array (passed by reference) is clobbered.
5086 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
5087 method to pass dates to the recur_prog expression, it should do so.
5089 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
5090 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
5091 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
5092 configuration values. Probably need a subroutine which decides what to do
5093 based on whether or not we've fetched the user yet, rather than a hash. See
5094 FS::UID and the TODO.
5096 Now that things are transactional should the check in the insert method be
5101 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
5102 L<FS::pkg_svc>, schema.html from the base documentation