4 use vars qw(@ISA $disable_agentcheck @SVCDB_CANCEL_SEQ $DEBUG);
5 use FS::UID qw( getotaker dbh );
6 use FS::Misc qw( send_email );
7 use FS::Record qw( qsearch qsearchs );
8 use FS::cust_main_Mixin;
14 use FS::cust_bill_pkg;
18 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
20 # because they load configuraion by setting FS::UID::callback (see TODO)
26 # for sending cancel emails in sub cancel
29 @ISA = qw( FS::cust_main_Mixin FS::Record );
33 $disable_agentcheck = 0;
35 # The order in which to unprovision services.
36 @SVCDB_CANCEL_SEQ = qw( svc_external
45 my ( $hashref, $cache ) = @_;
46 #if ( $hashref->{'pkgpart'} ) {
47 if ( $hashref->{'pkg'} ) {
48 # #@{ $self->{'_pkgnum'} } = ();
49 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
50 # $self->{'_pkgpart'} = $subcache;
51 # #push @{ $self->{'_pkgnum'} },
52 # FS::part_pkg->new_or_cached($hashref, $subcache);
53 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
55 if ( exists $hashref->{'svcnum'} ) {
56 #@{ $self->{'_pkgnum'} } = ();
57 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
58 $self->{'_svcnum'} = $subcache;
59 #push @{ $self->{'_pkgnum'} },
60 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
66 FS::cust_pkg - Object methods for cust_pkg objects
72 $record = new FS::cust_pkg \%hash;
73 $record = new FS::cust_pkg { 'column' => 'value' };
75 $error = $record->insert;
77 $error = $new_record->replace($old_record);
79 $error = $record->delete;
81 $error = $record->check;
83 $error = $record->cancel;
85 $error = $record->suspend;
87 $error = $record->unsuspend;
89 $part_pkg = $record->part_pkg;
91 @labels = $record->labels;
93 $seconds = $record->seconds_since($timestamp);
95 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
96 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
100 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
101 inherits from FS::Record. The following fields are currently supported:
105 =item pkgnum - primary key (assigned automatically for new billing items)
107 =item custnum - Customer (see L<FS::cust_main>)
109 =item pkgpart - Billing item definition (see L<FS::part_pkg>)
113 =item bill - date (next bill date)
115 =item last_bill - last bill date
123 =item otaker - order taker (assigned automatically if null, see L<FS::UID>)
125 =item manual_flag - If this field is set to 1, disables the automatic
126 unsuspension of this package when using the B<unsuspendauto> config file.
130 Note: setup, bill, susp, expire and cancel are specified as UNIX timestamps;
131 see L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for
132 conversion functions.
140 Create a new billing item. To add the item to the database, see L<"insert">.
144 sub table { 'cust_pkg'; }
145 sub cust_linked { $_[0]->cust_main_custnum; }
146 sub cust_unlinked_msg {
148 "WARNING: can't find cust_main.custnum ". $self->custnum.
149 ' (cust_pkg.pkgnum '. $self->pkgnum. ')';
152 =item insert [ OPTION => VALUE ... ]
154 Adds this billing item to the database ("Orders" the item). If there is an
155 error, returns the error, otherwise returns false.
157 If the additional field I<promo_code> is defined instead of I<pkgpart>, it
158 will be used to look up the package definition and agent restrictions will be
161 The following options are available: I<change>
163 I<change>, if set true, supresses any referral credit to a referring customer.
168 my( $self, %options ) = @_;
170 local $SIG{HUP} = 'IGNORE';
171 local $SIG{INT} = 'IGNORE';
172 local $SIG{QUIT} = 'IGNORE';
173 local $SIG{TERM} = 'IGNORE';
174 local $SIG{TSTP} = 'IGNORE';
175 local $SIG{PIPE} = 'IGNORE';
177 my $oldAutoCommit = $FS::UID::AutoCommit;
178 local $FS::UID::AutoCommit = 0;
181 my $error = $self->SUPER::insert;
183 $dbh->rollback if $oldAutoCommit;
187 #if ( $self->reg_code ) {
188 # my $reg_code = qsearchs('reg_code', { 'code' => $self->reg_code } );
189 # $error = $reg_code->delete;
191 # $dbh->rollback if $oldAutoCommit;
196 my $conf = new FS::Conf;
197 my $cust_main = $self->cust_main;
198 my $part_pkg = $self->part_pkg;
199 if ( $conf->exists('referral_credit')
200 && $cust_main->referral_custnum
201 && ! $options{'change'}
202 && $part_pkg->freq !~ /^0\D?$/
205 my $referring_cust_main = $cust_main->referring_cust_main;
206 if ( $referring_cust_main->status ne 'cancelled' ) {
208 if ( $part_pkg->freq !~ /^\d+$/ ) {
209 warn 'WARNING: Not crediting customer '. $cust_main->referral_custnum.
210 ' for package '. $self->pkgnum.
211 ' ( customer '. $self->custnum. ')'.
212 ' - One-time referral credits not (yet) available for '.
213 ' packages with '. $part_pkg->freq_pretty. ' frequency';
216 my $amount = sprintf( "%.2f", $part_pkg->base_recur / $part_pkg->freq );
218 $referring_cust_main->credit( $amount,
219 'Referral credit for '. $cust_main->name
222 $dbh->rollback if $oldAutoCommit;
223 return "Error crediting customer ". $cust_main->referral_custnum.
224 " for referral: $error";
232 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
239 This method now works but you probably shouldn't use it.
241 You don't want to delete billing items, because there would then be no record
242 the customer ever purchased the item. Instead, see the cancel method.
247 # return "Can't delete cust_pkg records!";
250 =item replace OLD_RECORD
252 Replaces the OLD_RECORD with this one in the database. If there is an error,
253 returns the error, otherwise returns false.
255 Currently, custnum, setup, bill, susp, expire, and cancel may be changed.
257 Changing pkgpart may have disasterous effects. See the order subroutine.
259 setup and bill are normally updated by calling the bill method of a customer
260 object (see L<FS::cust_main>).
262 suspend is normally updated by the suspend and unsuspend methods.
264 cancel is normally updated by the cancel method (and also the order subroutine
270 my( $new, $old ) = ( shift, shift );
272 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
273 return "Can't change otaker!" if $old->otaker ne $new->otaker;
276 #return "Can't change setup once it exists!"
277 # if $old->getfield('setup') &&
278 # $old->getfield('setup') != $new->getfield('setup');
280 #some logic for bill, susp, cancel?
282 local($disable_agentcheck) = 1 if $old->pkgpart == $new->pkgpart;
284 $new->SUPER::replace($old);
289 Checks all fields to make sure this is a valid billing item. If there is an
290 error, returns the error, otherwise returns false. Called by the insert and
299 $self->ut_numbern('pkgnum')
300 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
301 || $self->ut_numbern('pkgpart')
302 || $self->ut_numbern('setup')
303 || $self->ut_numbern('bill')
304 || $self->ut_numbern('susp')
305 || $self->ut_numbern('cancel')
307 return $error if $error;
309 if ( $self->reg_code ) {
311 unless ( grep { $self->pkgpart == $_->pkgpart }
312 map { $_->reg_code_pkg }
313 qsearchs( 'reg_code', { 'code' => $self->reg_code,
314 'agentnum' => $self->cust_main->agentnum })
316 return "Unknown registraiton code";
319 } elsif ( $self->promo_code ) {
322 qsearchs('part_pkg', {
323 'pkgpart' => $self->pkgpart,
324 'promo_code' => { op=>'ILIKE', value=>$self->promo_code },
326 return 'Unknown promotional code' unless $promo_part_pkg;
330 unless ( $disable_agentcheck ) {
332 qsearchs( 'agent', { 'agentnum' => $self->cust_main->agentnum } );
333 my $pkgpart_href = $agent->pkgpart_hashref;
334 return "agent ". $agent->agentnum.
335 " can't purchase pkgpart ". $self->pkgpart
336 unless $pkgpart_href->{ $self->pkgpart };
339 $error = $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' );
340 return $error if $error;
344 $self->otaker(getotaker) unless $self->otaker;
345 $self->otaker =~ /^([\w\.\-]{0,16})$/ or return "Illegal otaker";
348 if ( $self->dbdef_table->column('manual_flag') ) {
349 $self->manual_flag('') if $self->manual_flag eq ' ';
350 $self->manual_flag =~ /^([01]?)$/
351 or return "Illegal manual_flag ". $self->manual_flag;
352 $self->manual_flag($1);
358 =item cancel [ OPTION => VALUE ... ]
360 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
361 in this package, then cancels the package itself (sets the cancel field to
364 Available options are: I<quiet>
366 I<quiet> can be set true to supress email cancellation notices.
368 If there is an error, returns the error, otherwise returns false.
373 my( $self, %options ) = @_;
376 local $SIG{HUP} = 'IGNORE';
377 local $SIG{INT} = 'IGNORE';
378 local $SIG{QUIT} = 'IGNORE';
379 local $SIG{TERM} = 'IGNORE';
380 local $SIG{TSTP} = 'IGNORE';
381 local $SIG{PIPE} = 'IGNORE';
383 my $oldAutoCommit = $FS::UID::AutoCommit;
384 local $FS::UID::AutoCommit = 0;
388 foreach my $cust_svc (
389 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
391 push @{ $svc{$cust_svc->part_svc->svcdb} }, $cust_svc;
394 foreach my $svcdb (@SVCDB_CANCEL_SEQ) {
395 foreach my $cust_svc (@{ $svc{$svcdb} }) {
396 my $error = $cust_svc->cancel;
399 $dbh->rollback if $oldAutoCommit;
400 return "Error cancelling cust_svc: $error";
405 # Add a credit for remaining service
406 my $remaining_value = $self->calc_remain();
407 if ( $remaining_value > 0 ) {
408 my $error = $self->cust_main->credit(
410 'Credit for unused time on'. $self->part_pkg->pkg,
413 $dbh->rollback if $oldAutoCommit;
414 return "Error crediting customer \$$remaining_value for unused time on".
415 $self->part_pkg->pkg. ": $error";
419 unless ( $self->getfield('cancel') ) {
420 my %hash = $self->hash;
421 $hash{'cancel'} = time;
422 my $new = new FS::cust_pkg ( \%hash );
423 $error = $new->replace($self);
425 $dbh->rollback if $oldAutoCommit;
430 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
432 my $conf = new FS::Conf;
433 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
434 if ( !$options{'quiet'} && $conf->exists('emailcancel') && @invoicing_list ) {
435 my $conf = new FS::Conf;
436 my $error = send_email(
437 'from' => $conf->config('invoice_from'),
438 'to' => \@invoicing_list,
439 'subject' => $conf->config('cancelsubject'),
440 'body' => [ map "$_\n", $conf->config('cancelmessage') ],
442 #should this do something on errors?
451 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
452 package, then suspends the package itself (sets the susp field to now).
454 If there is an error, returns the error, otherwise returns false.
462 local $SIG{HUP} = 'IGNORE';
463 local $SIG{INT} = 'IGNORE';
464 local $SIG{QUIT} = 'IGNORE';
465 local $SIG{TERM} = 'IGNORE';
466 local $SIG{TSTP} = 'IGNORE';
467 local $SIG{PIPE} = 'IGNORE';
469 my $oldAutoCommit = $FS::UID::AutoCommit;
470 local $FS::UID::AutoCommit = 0;
473 foreach my $cust_svc (
474 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
476 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
478 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
479 $dbh->rollback if $oldAutoCommit;
480 return "Illegal svcdb value in part_svc!";
483 require "FS/$svcdb.pm";
485 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
487 $error = $svc->suspend;
489 $dbh->rollback if $oldAutoCommit;
496 unless ( $self->getfield('susp') ) {
497 my %hash = $self->hash;
498 $hash{'susp'} = time;
499 my $new = new FS::cust_pkg ( \%hash );
500 $error = $new->replace($self);
502 $dbh->rollback if $oldAutoCommit;
507 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
514 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
515 package, then unsuspends the package itself (clears the susp field).
517 If there is an error, returns the error, otherwise returns false.
525 local $SIG{HUP} = 'IGNORE';
526 local $SIG{INT} = 'IGNORE';
527 local $SIG{QUIT} = 'IGNORE';
528 local $SIG{TERM} = 'IGNORE';
529 local $SIG{TSTP} = 'IGNORE';
530 local $SIG{PIPE} = 'IGNORE';
532 my $oldAutoCommit = $FS::UID::AutoCommit;
533 local $FS::UID::AutoCommit = 0;
536 foreach my $cust_svc (
537 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
539 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
541 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
542 $dbh->rollback if $oldAutoCommit;
543 return "Illegal svcdb value in part_svc!";
546 require "FS/$svcdb.pm";
548 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
550 $error = $svc->unsuspend;
552 $dbh->rollback if $oldAutoCommit;
559 unless ( ! $self->getfield('susp') ) {
560 my %hash = $self->hash;
561 my $inactive = time - $hash{'susp'};
563 $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive
564 if $inactive > 0 && ( $hash{'bill'} || $hash{'setup'} );
565 my $new = new FS::cust_pkg ( \%hash );
566 $error = $new->replace($self);
568 $dbh->rollback if $oldAutoCommit;
573 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
580 Returns the last bill date, or if there is no last bill date, the setup date.
581 Useful for billing metered services.
587 if ( $self->dbdef_table->column('last_bill') ) {
588 return $self->setfield('last_bill', $_[0]) if @_;
589 return $self->getfield('last_bill') if $self->getfield('last_bill');
591 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
592 'edate' => $self->bill, } );
593 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
598 Returns the definition for this billing item, as an FS::part_pkg object (see
605 #exists( $self->{'_pkgpart'} )
607 ? $self->{'_pkgpart'}
608 : qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
613 Calls the I<calc_setup> of the FS::part_pkg object associated with this billing
620 $self->part_pkg->calc_setup($self, @_);
625 Calls the I<calc_recur> of the FS::part_pkg object associated with this billing
632 $self->part_pkg->calc_recur($self, @_);
637 Calls the I<calc_remain> of the FS::part_pkg object associated with this
644 $self->part_pkg->calc_remain($self, @_);
649 Calls the I<calc_cancel> of the FS::part_pkg object associated with this
656 $self->part_pkg->calc_cancel($self, @_);
659 =item cust_svc [ SVCPART ]
661 Returns the services for this package, as FS::cust_svc objects (see
662 L<FS::cust_svc>). If a svcpart is specified, return only the matching
671 return qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum,
672 'svcpart' => shift, } );
675 #if ( $self->{'_svcnum'} ) {
676 # values %{ $self->{'_svcnum'}->cache };
678 $self->_sort_cust_svc(
679 [ qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } ) ]
685 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ]
687 Returns historical services for this package created before END TIMESTAMP and
688 (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects
689 (see L<FS::h_cust_svc>).
696 $self->_sort_cust_svc(
697 [ qsearch( 'h_cust_svc',
698 { 'pkgnum' => $self->pkgnum, },
699 FS::h_cust_svc->sql_h_search(@_),
706 my( $self, $arrayref ) = @_;
709 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
711 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $self->pkgpart,
712 'svcpart' => $_->svcpart } );
714 $pkg_svc ? $pkg_svc->primary_svc : '',
715 $pkg_svc ? $pkg_svc->quantity : 0,
722 =item num_cust_svc [ SVCPART ]
724 Returns the number of provisioned services for this package. If a svcpart is
725 specified, counts only the matching services.
731 my $sql = 'SELECT COUNT(*) FROM cust_svc WHERE pkgnum = ?';
732 $sql .= ' AND svcpart = ?' if @_;
733 my $sth = dbh->prepare($sql) or die dbh->errstr;
734 $sth->execute($self->pkgnum, @_) or die $sth->errstr;
735 $sth->fetchrow_arrayref->[0];
738 =item available_part_svc
740 Returns a list FS::part_svc objects representing services included in this
741 package but not yet provisioned. Each FS::part_svc object also has an extra
742 field, I<num_avail>, which specifies the number of available services.
746 sub available_part_svc {
748 grep { $_->num_avail > 0 }
750 my $part_svc = $_->part_svc;
751 $part_svc->{'Hash'}{'num_avail'} = #evil encapsulation-breaking
752 $_->quantity - $self->num_cust_svc($_->svcpart);
755 $self->part_pkg->pkg_svc;
760 Returns a short status string for this package, currently:
766 =item one-time charge
781 return 'cancelled' if $self->get('cancel');
782 return 'suspended' if $self->susp;
783 return 'not yet billed' unless $self->setup;
784 return 'one-time charge' if $self->part_pkg->freq =~ /^(0|$)/;
790 Returns a hex triplet color string for this package's status.
795 'not yet billed' => '000000',
796 'one-time charge' => '000000',
797 'active' => '00CC00',
798 'suspended' => 'FF9900',
799 'cancelled' => 'FF0000',
803 $statuscolor{$self->status};
808 Returns a list of lists, calling the label method for all services
809 (see L<FS::cust_svc>) of this billing item.
815 map { [ $_->label ] } $self->cust_svc;
818 =item h_labels END_TIMESTAMP [ START_TIMESTAMP ]
820 Like the labels method, but returns historical information on services that
821 were active as of END_TIMESTAMP and (optionally) not cancelled before
824 Returns a list of lists, calling the label method for all (historical) services
825 (see L<FS::h_cust_svc>) of this billing item.
831 map { [ $_->label(@_) ] } $self->h_cust_svc(@_);
834 =item h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
836 Like h_labels, except returns a simple flat list, and shortens long
837 (currently >5) lists of identical services to one line that lists the service
838 label and the number of individual services rather than individual items.
846 #tie %labels, 'Tie::IxHash';
847 push @{ $labels{$_->[0]} }, $_->[1]
848 foreach $self->h_labels(@_);
850 foreach my $label ( keys %labels ) {
851 my @values = @{ $labels{$label} };
852 my $num = scalar(@values);
854 push @labels, "$label ($num)";
856 push @labels, map { "$label: $_" } @values;
866 Returns the parent customer object (see L<FS::cust_main>).
872 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
875 =item seconds_since TIMESTAMP
877 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
878 package have been online since TIMESTAMP, according to the session monitor.
880 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
881 L<Time::Local> and L<Date::Parse> for conversion functions.
886 my($self, $since) = @_;
889 foreach my $cust_svc (
890 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
892 $seconds += $cust_svc->seconds_since($since);
899 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
901 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
902 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
905 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
906 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
912 sub seconds_since_sqlradacct {
913 my($self, $start, $end) = @_;
917 foreach my $cust_svc (
919 my $part_svc = $_->part_svc;
920 $part_svc->svcdb eq 'svc_acct'
921 && scalar($part_svc->part_export('sqlradius'));
924 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
931 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
933 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
934 in this package for sessions ending between TIMESTAMP_START (inclusive) and
938 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
939 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
944 sub attribute_since_sqlradacct {
945 my($self, $start, $end, $attrib) = @_;
949 foreach my $cust_svc (
951 my $part_svc = $_->part_svc;
952 $part_svc->svcdb eq 'svc_acct'
953 && scalar($part_svc->part_export('sqlradius'));
956 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
963 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
965 Transfers as many services as possible from this package to another package.
967 The destination package can be specified by pkgnum by passing an FS::cust_pkg
968 object. The destination package must already exist.
970 Services are moved only if the destination allows services with the correct
971 I<svcpart> (not svcdb), unless the B<change_svcpart> option is set true. Use
972 this option with caution! No provision is made for export differences
973 between the old and new service definitions. Probably only should be used
974 when your exports for all service definitions of a given svcdb are identical.
975 (attempt a transfer without it first, to move all possible svcpart-matching
978 Any services that can't be moved remain in the original package.
980 Returns an error, if there is one; otherwise, returns the number of services
981 that couldn't be moved.
986 my ($self, $dest_pkgnum, %opt) = @_;
992 if (ref ($dest_pkgnum) eq 'FS::cust_pkg') {
993 $dest = $dest_pkgnum;
994 $dest_pkgnum = $dest->pkgnum;
996 $dest = qsearchs('cust_pkg', { pkgnum => $dest_pkgnum });
999 return ('Package does not exist: '.$dest_pkgnum) unless $dest;
1001 foreach my $pkg_svc ( $dest->part_pkg->pkg_svc ) {
1002 $target{$pkg_svc->svcpart} = $pkg_svc->quantity;
1005 foreach my $cust_svc ($dest->cust_svc) {
1006 $target{$cust_svc->svcpart}--;
1009 my %svcpart2svcparts = ();
1010 if ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
1011 warn "change_svcpart option received, creating alternates list\n" if $DEBUG;
1012 foreach my $svcpart ( map { $_->svcpart } $self->cust_svc ) {
1013 next if exists $svcpart2svcparts{$svcpart};
1014 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
1015 $svcpart2svcparts{$svcpart} = [
1017 sort { $b->[1] cmp $a->[1] or $a->[2] <=> $b->[2] }
1019 my $pkg_svc = qsearchs( 'pkg_svc', { 'pkgpart' => $dest->pkgpart,
1020 'svcpart' => $_ } );
1022 $pkg_svc ? $pkg_svc->primary_svc : '',
1023 $pkg_svc ? $pkg_svc->quantity : 0,
1027 grep { $_ != $svcpart }
1029 qsearch('part_svc', { 'svcdb' => $part_svc->svcdb } )
1031 warn "alternates for svcpart $svcpart: ".
1032 join(', ', @{$svcpart2svcparts{$svcpart}}). "\n"
1037 foreach my $cust_svc ($self->cust_svc) {
1038 if($target{$cust_svc->svcpart} > 0) {
1039 $target{$cust_svc->svcpart}--;
1040 my $new = new FS::cust_svc {
1041 svcnum => $cust_svc->svcnum,
1042 svcpart => $cust_svc->svcpart,
1043 pkgnum => $dest_pkgnum,
1045 my $error = $new->replace($cust_svc);
1046 return $error if $error;
1047 } elsif ( exists $opt{'change_svcpart'} && $opt{'change_svcpart'} ) {
1049 warn "looking for alternates for svcpart ". $cust_svc->svcpart. "\n";
1050 warn "alternates to consider: ".
1051 join(', ', @{$svcpart2svcparts{$cust_svc->svcpart}}). "\n";
1053 my @alternate = grep {
1054 warn "considering alternate svcpart $_: ".
1055 "$target{$_} available in new package\n"
1058 } @{$svcpart2svcparts{$cust_svc->svcpart}};
1060 warn "alternate(s) found\n" if $DEBUG;
1061 my $change_svcpart = $alternate[0];
1062 $target{$change_svcpart}--;
1063 my $new = new FS::cust_svc {
1064 svcnum => $cust_svc->svcnum,
1065 svcpart => $change_svcpart,
1066 pkgnum => $dest_pkgnum,
1068 my $error = $new->replace($cust_svc);
1069 return $error if $error;
1082 This method is deprecated. See the I<depend_jobnum> option to the insert and
1083 order_pkgs methods in FS::cust_main for a better way to defer provisioning.
1090 local $SIG{HUP} = 'IGNORE';
1091 local $SIG{INT} = 'IGNORE';
1092 local $SIG{QUIT} = 'IGNORE';
1093 local $SIG{TERM} = 'IGNORE';
1094 local $SIG{TSTP} = 'IGNORE';
1095 local $SIG{PIPE} = 'IGNORE';
1097 my $oldAutoCommit = $FS::UID::AutoCommit;
1098 local $FS::UID::AutoCommit = 0;
1101 foreach my $cust_svc ( $self->cust_svc ) {
1102 #false laziness w/svc_Common::insert
1103 my $svc_x = $cust_svc->svc_x;
1104 foreach my $part_export ( $cust_svc->part_svc->part_export ) {
1105 my $error = $part_export->export_insert($svc_x);
1107 $dbh->rollback if $oldAutoCommit;
1113 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1126 Returns an SQL expression identifying recurring packages.
1130 sub recurring_sql { "
1131 '0' != ( select freq from part_pkg
1132 where cust_pkg.pkgpart = part_pkg.pkgpart )
1137 Returns an SQL expression identifying active packages.
1142 ". $_[0]->recurring_sql(). "
1143 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
1144 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
1150 Returns an SQL expression identifying suspended packages.
1154 sub suspended_sql { susp_sql(@_); }
1156 ". $_[0]->recurring_sql(). "
1157 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
1158 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0
1164 Returns an SQL exprression identifying cancelled packages.
1168 sub cancelled_sql { cancel_sql(@_); }
1170 ". $_[0]->recurring_sql(). "
1171 AND cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0
1178 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ] ]
1180 CUSTNUM is a customer (see L<FS::cust_main>)
1182 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
1183 L<FS::part_pkg>) to order for this customer. Duplicates are of course
1186 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
1187 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
1188 new billing items. An error is returned if this is not possible (see
1189 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
1192 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
1193 newly-created cust_pkg objects.
1198 my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
1200 my $conf = new FS::Conf;
1202 # Transactionize this whole mess
1203 local $SIG{HUP} = 'IGNORE';
1204 local $SIG{INT} = 'IGNORE';
1205 local $SIG{QUIT} = 'IGNORE';
1206 local $SIG{TERM} = 'IGNORE';
1207 local $SIG{TSTP} = 'IGNORE';
1208 local $SIG{PIPE} = 'IGNORE';
1210 my $oldAutoCommit = $FS::UID::AutoCommit;
1211 local $FS::UID::AutoCommit = 0;
1215 my $cust_main = qsearchs('cust_main', { custnum => $custnum });
1216 return "Customer not found: $custnum" unless $cust_main;
1218 my @old_cust_pkg = map { qsearchs('cust_pkg', { pkgnum => $_ }) }
1221 my $change = scalar(@old_cust_pkg) != 0;
1224 if ( scalar(@old_cust_pkg) == 1 ) {
1225 #$hash{$_} = $old_cust_pkg[0]->$_() foreach qw( last_bill bill );
1226 $hash{'setup'} = time;
1229 # Create the new packages.
1230 foreach my $pkgpart (@$pkgparts) {
1231 my $cust_pkg = new FS::cust_pkg { custnum => $custnum,
1232 pkgpart => $pkgpart,
1235 $error = $cust_pkg->insert( 'change' => $change );
1237 $dbh->rollback if $oldAutoCommit;
1240 push @$return_cust_pkg, $cust_pkg;
1242 # $return_cust_pkg now contains refs to all of the newly
1245 # Transfer services and cancel old packages.
1246 foreach my $old_pkg (@old_cust_pkg) {
1248 foreach my $new_pkg (@$return_cust_pkg) {
1249 $error = $old_pkg->transfer($new_pkg);
1250 if ($error and $error == 0) {
1251 # $old_pkg->transfer failed.
1252 $dbh->rollback if $oldAutoCommit;
1257 if ( $error > 0 && $conf->exists('cust_pkg-change_svcpart') ) {
1258 warn "trying transfer again with change_svcpart option\n" if $DEBUG;
1259 foreach my $new_pkg (@$return_cust_pkg) {
1260 $error = $old_pkg->transfer($new_pkg, 'change_svcpart'=>1 );
1261 if ($error and $error == 0) {
1262 # $old_pkg->transfer failed.
1263 $dbh->rollback if $oldAutoCommit;
1270 # Transfers were successful, but we went through all of the
1271 # new packages and still had services left on the old package.
1272 # We can't cancel the package under the circumstances, so abort.
1273 $dbh->rollback if $oldAutoCommit;
1274 return "Unable to transfer all services from package ".$old_pkg->pkgnum;
1276 $error = $old_pkg->cancel;
1282 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1290 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
1292 In sub order, the @pkgparts array (passed by reference) is clobbered.
1294 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
1295 method to pass dates to the recur_prog expression, it should do so.
1297 FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are
1298 loaded via 'use' at compile time, rather than via 'require' in sub { setup,
1299 suspend, unsuspend, cancel } because they use %FS::UID::callback to load
1300 configuration values. Probably need a subroutine which decides what to do
1301 based on whether or not we've fetched the user yet, rather than a hash. See
1302 FS::UID and the TODO.
1304 Now that things are transactional should the check in the insert method be
1309 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
1310 L<FS::pkg_svc>, schema.html from the base documentation