4 use vars qw(@ISA $quiet $disable_agentcheck);
5 use FS::UID qw( getotaker dbh );
6 use FS::Record qw( qsearch qsearchs );
12 use FS::cust_bill_pkg;
14 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
16 # because they load configuraion by setting FS::UID::callback (see TODO)
23 # need all this for sending cancel emails in sub cancel
27 use Mail::Internet 1.44;
30 @ISA = qw( FS::Record );
32 $disable_agentcheck = 0;
36 my ( $hashref, $cache ) = @_;
37 #if ( $hashref->{'pkgpart'} ) {
38 if ( $hashref->{'pkg'} ) {
39 # #@{ $self->{'_pkgnum'} } = ();
40 # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
41 # $self->{'_pkgpart'} = $subcache;
42 # #push @{ $self->{'_pkgnum'} },
43 # FS::part_pkg->new_or_cached($hashref, $subcache);
44 $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
46 if ( exists $hashref->{'svcnum'} ) {
47 #@{ $self->{'_pkgnum'} } = ();
48 my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
49 $self->{'_svcnum'} = $subcache;
50 #push @{ $self->{'_pkgnum'} },
51 FS::cust_svc->new_or_cached($hashref, $subcache) if $hashref->{svcnum};
57 FS::cust_pkg - Object methods for cust_pkg objects
63 $record = new FS::cust_pkg \%hash;
64 $record = new FS::cust_pkg { 'column' => 'value' };
66 $error = $record->insert;
68 $error = $new_record->replace($old_record);
70 $error = $record->delete;
72 $error = $record->check;
74 $error = $record->cancel;
76 $error = $record->suspend;
78 $error = $record->unsuspend;
80 $part_pkg = $record->part_pkg;
82 @labels = $record->labels;
84 $seconds = $record->seconds_since($timestamp);
86 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
87 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
91 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
92 inherits from FS::Record. The following fields are currently supported:
96 =item pkgnum - primary key (assigned automatically for new billing items)
98 =item custnum - Customer (see L<FS::cust_main>)
100 =item pkgpart - Billing item definition (see L<FS::part_pkg>)
104 =item bill - date (next bill date)
112 =item otaker - order taker (assigned automatically if null, see L<FS::UID>)
114 =item manual_flag - If this field is set to 1, disables the automatic
115 unsuspension of this package when using the B<unsuspendauto> config file.
119 Note: setup, bill, susp, expire and cancel are specified as UNIX timestamps;
120 see L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for
121 conversion functions.
129 Create a new billing item. To add the item to the database, see L<"insert">.
133 sub table { 'cust_pkg'; }
137 Adds this billing item to the database ("Orders" the item). If there is an
138 error, returns the error, otherwise returns false.
145 # custnum might not have have been defined in sub check (for one-shot new
146 # customers), so check it here instead
147 # (is this still necessary with transactions?)
149 my $error = $self->ut_number('custnum');
150 return $error if $error;
152 my $cust_main = $self->cust_main;
153 return "Unknown customer ". $self->custnum unless $cust_main;
155 unless ( $disable_agentcheck ) {
156 my $agent = qsearchs( 'agent', { 'agentnum' => $cust_main->agentnum } );
157 my $pkgpart_href = $agent->pkgpart_hashref;
158 return "agent ". $agent->agentnum.
159 " can't purchase pkgpart ". $self->pkgpart
160 unless $pkgpart_href->{ $self->pkgpart };
163 $self->SUPER::insert;
169 This method now works but you probably shouldn't use it.
171 You don't want to delete billing items, because there would then be no record
172 the customer ever purchased the item. Instead, see the cancel method.
177 # return "Can't delete cust_pkg records!";
180 =item replace OLD_RECORD
182 Replaces the OLD_RECORD with this one in the database. If there is an error,
183 returns the error, otherwise returns false.
185 Currently, custnum, setup, bill, susp, expire, and cancel may be changed.
187 Changing pkgpart may have disasterous effects. See the order subroutine.
189 setup and bill are normally updated by calling the bill method of a customer
190 object (see L<FS::cust_main>).
192 suspend is normally updated by the suspend and unsuspend methods.
194 cancel is normally updated by the cancel method (and also the order subroutine
200 my( $new, $old ) = ( shift, shift );
202 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
203 return "Can't change otaker!" if $old->otaker ne $new->otaker;
206 #return "Can't change setup once it exists!"
207 # if $old->getfield('setup') &&
208 # $old->getfield('setup') != $new->getfield('setup');
210 #some logic for bill, susp, cancel?
212 $new->SUPER::replace($old);
217 Checks all fields to make sure this is a valid billing item. If there is an
218 error, returns the error, otherwise returns false. Called by the insert and
227 $self->ut_numbern('pkgnum')
228 || $self->ut_numbern('custnum')
229 || $self->ut_number('pkgpart')
230 || $self->ut_numbern('setup')
231 || $self->ut_numbern('bill')
232 || $self->ut_numbern('susp')
233 || $self->ut_numbern('cancel')
235 return $error if $error;
237 if ( $self->custnum ) {
238 return "Unknown customer ". $self->custnum unless $self->cust_main;
241 return "Unknown pkgpart: ". $self->pkgpart
242 unless qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
244 $self->otaker(getotaker) unless $self->otaker;
245 $self->otaker =~ /^([\w\.\-]{0,16})$/ or return "Illegal otaker";
248 if ( $self->dbdef_table->column('manual_flag') ) {
249 $self->manual_flag =~ /^([01]?)$/ or return "Illegal manual_flag";
250 $self->manual_flag($1);
258 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
259 in this package, then cancels the package itself (sets the cancel field to
262 If there is an error, returns the error, otherwise returns false.
270 local $SIG{HUP} = 'IGNORE';
271 local $SIG{INT} = 'IGNORE';
272 local $SIG{QUIT} = 'IGNORE';
273 local $SIG{TERM} = 'IGNORE';
274 local $SIG{TSTP} = 'IGNORE';
275 local $SIG{PIPE} = 'IGNORE';
277 my $oldAutoCommit = $FS::UID::AutoCommit;
278 local $FS::UID::AutoCommit = 0;
281 foreach my $cust_svc (
282 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
284 my $error = $cust_svc->cancel;
287 $dbh->rollback if $oldAutoCommit;
288 return "Error cancelling cust_svc: $error";
293 unless ( $self->getfield('cancel') ) {
294 my %hash = $self->hash;
295 $hash{'cancel'} = time;
296 my $new = new FS::cust_pkg ( \%hash );
297 $error = $new->replace($self);
299 $dbh->rollback if $oldAutoCommit;
304 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
306 my $conf = new FS::Conf;
308 if ( !$quiet && $conf->exists('emailcancel')
309 && grep { $_ ne 'POST' } $self->cust_main->invoicing_list) {
311 my @invoicing_list = $self->cust_main->invoicing_list;
313 my $invoice_from = $conf->config('invoice_from');
314 my @print_text = map "$_\n", $conf->config('cancelmessage');
315 my $subject = $conf->config('cancelsubject');
316 my $smtpmachine = $conf->config('smtpmachine');
318 if ( grep { $_ ne 'POST' } @invoicing_list ) { #email invoice
319 #false laziness w/FS::cust_pay::delete & fs_signup_server && ::realtime_card
320 #$ENV{SMTPHOSTS} = $smtpmachine;
321 $ENV{MAILADDRESS} = $invoice_from;
322 my $header = new Mail::Header ( [
323 "From: $invoice_from",
324 "To: ". join(', ', grep { $_ ne 'POST' } @invoicing_list ),
325 "Sender: $invoice_from",
326 "Reply-To: $invoice_from",
327 "Date: ". time2str("%a, %d %b %Y %X %z", time),
330 my $message = new Mail::Internet (
332 'Body' => [ @print_text ],
335 $message->smtpsend( Host => $smtpmachine )
336 or $message->smtpsend( Host => $smtpmachine, Debug => 1 );
337 #should this return an error?
347 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
348 package, then suspends the package itself (sets the susp field to now).
350 If there is an error, returns the error, otherwise returns false.
358 local $SIG{HUP} = 'IGNORE';
359 local $SIG{INT} = 'IGNORE';
360 local $SIG{QUIT} = 'IGNORE';
361 local $SIG{TERM} = 'IGNORE';
362 local $SIG{TSTP} = 'IGNORE';
363 local $SIG{PIPE} = 'IGNORE';
365 my $oldAutoCommit = $FS::UID::AutoCommit;
366 local $FS::UID::AutoCommit = 0;
369 foreach my $cust_svc (
370 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
372 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
374 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
375 $dbh->rollback if $oldAutoCommit;
376 return "Illegal svcdb value in part_svc!";
379 require "FS/$svcdb.pm";
381 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
383 $error = $svc->suspend;
385 $dbh->rollback if $oldAutoCommit;
392 unless ( $self->getfield('susp') ) {
393 my %hash = $self->hash;
394 $hash{'susp'} = time;
395 my $new = new FS::cust_pkg ( \%hash );
396 $error = $new->replace($self);
398 $dbh->rollback if $oldAutoCommit;
403 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
410 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
411 package, then unsuspends the package itself (clears the susp field).
413 If there is an error, returns the error, otherwise returns false.
421 local $SIG{HUP} = 'IGNORE';
422 local $SIG{INT} = 'IGNORE';
423 local $SIG{QUIT} = 'IGNORE';
424 local $SIG{TERM} = 'IGNORE';
425 local $SIG{TSTP} = 'IGNORE';
426 local $SIG{PIPE} = 'IGNORE';
428 my $oldAutoCommit = $FS::UID::AutoCommit;
429 local $FS::UID::AutoCommit = 0;
432 foreach my $cust_svc (
433 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
435 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
437 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
438 $dbh->rollback if $oldAutoCommit;
439 return "Illegal svcdb value in part_svc!";
442 require "FS/$svcdb.pm";
444 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
446 $error = $svc->unsuspend;
448 $dbh->rollback if $oldAutoCommit;
455 unless ( ! $self->getfield('susp') ) {
456 my %hash = $self->hash;
458 my $new = new FS::cust_pkg ( \%hash );
459 $error = $new->replace($self);
461 $dbh->rollback if $oldAutoCommit;
466 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
473 Returns the last bill date, or if there is no last bill date, the setup date.
474 Useful for billing metered services.
480 my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
481 'edate' => $self->bill, } );
482 $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
487 Returns the definition for this billing item, as an FS::part_pkg object (see
494 #exists( $self->{'_pkgpart'} )
496 ? $self->{'_pkgpart'}
497 : qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
502 Returns the services for this package, as FS::cust_svc objects (see
509 if ( $self->{'_svcnum'} ) {
510 values %{ $self->{'_svcnum'}->cache };
512 qsearch ( 'cust_svc', { 'pkgnum' => $self->pkgnum } );
518 Returns a list of lists, calling the label method for all services
519 (see L<FS::cust_svc>) of this billing item.
525 map { [ $_->label ] } $self->cust_svc;
530 Returns the parent customer object (see L<FS::cust_main>).
536 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
539 =item seconds_since TIMESTAMP
541 Returns the number of seconds all accounts (see L<FS::svc_acct>) in this
542 package have been online since TIMESTAMP, according to the session monitor.
544 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
545 L<Time::Local> and L<Date::Parse> for conversion functions.
550 my($self, $since) = @_;
553 foreach my $cust_svc (
554 grep { $_->part_svc->svcdb eq 'svc_acct' } $self->cust_svc
556 $seconds += $cust_svc->seconds_since($since);
563 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
565 Returns the numbers of seconds all accounts (see L<FS::svc_acct>) in this
566 package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END
569 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
570 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
576 sub seconds_since_sqlradacct {
577 my($self, $start, $end) = @_;
581 foreach my $cust_svc (
583 my $part_svc = $_->part_svc;
584 $part_svc->svcdb eq 'svc_acct'
585 && scalar($part_svc->part_export('sqlradius'));
588 $seconds += $cust_svc->seconds_since_sqlradacct($start, $end);
595 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
597 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
598 in this package for sessions ending between TIMESTAMP_START (inclusive) and
599 TIMESTAMP_END (exclusive).
601 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
602 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
607 sub attribute_since_sqlradacct {
608 my($self, $start, $end, $attrib) = @_;
612 foreach my $cust_svc (
614 my $part_svc = $_->part_svc;
615 $part_svc->svcdb eq 'svc_acct'
616 && scalar($part_svc->part_export('sqlradius'));
619 $sum += $cust_svc->attribute_since_sqlradacct($start, $end, $attrib);
632 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ] ]
634 CUSTNUM is a customer (see L<FS::cust_main>)
636 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
637 L<FS::part_pkg>) to order for this customer. Duplicates are of course
640 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
641 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
642 new billing items. An error is returned if this is not possible (see
643 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
646 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
647 newly-created cust_pkg objects.
652 my($custnum, $pkgparts, $remove_pkgnums, $return_cust_pkg) = @_;
653 $remove_pkgnums = [] unless defined($remove_pkgnums);
655 my $oldAutoCommit = $FS::UID::AutoCommit;
656 local $FS::UID::AutoCommit = 0;
660 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
662 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
663 my($agent)=qsearchs('agent',{'agentnum'=> $cust_main->agentnum });
664 my %part_pkg = %{ $agent->pkgpart_hashref };
668 # for those packages being removed:
669 #@{ $svcnum{$svcpart} } goes from a svcpart to a list of FS::cust_svc objects
671 foreach $pkgnum ( @{$remove_pkgnums} ) {
672 foreach my $cust_svc (qsearch('cust_svc',{'pkgnum'=>$pkgnum})) {
673 push @{ $svcnum{$cust_svc->getfield('svcpart')} }, $cust_svc;
679 # for those packages the customer is purchasing:
680 # @{$pkgparts} is a list of said packages, by pkgpart
681 # @cust_svc is a corresponding list of lists of FS::Record objects
682 foreach my $pkgpart ( @{$pkgparts} ) {
683 unless ( $part_pkg{$pkgpart} ) {
684 $dbh->rollback if $oldAutoCommit;
685 return "Customer not permitted to purchase pkgpart $pkgpart!";
689 ( $svcnum{$_} && @{ $svcnum{$_} } ) ? shift @{ $svcnum{$_} } : ();
690 } map { $_->svcpart }
691 qsearch('pkg_svc', { pkgpart => $pkgpart,
692 quantity => { op=>'>', value=>'0', } } )
696 #special-case until this can be handled better
697 # move services to new svcparts - even if the svcparts don't match (svcdb
699 # looks like they're moved in no particular order, ewwwwwwww
700 # and looks like just one of each svcpart can be moved... o well
702 #start with still-leftover services
703 #foreach my $svcpart ( grep { scalar(@{ $svcnum{$_} }) } keys %svcnum ) {
704 foreach my $svcpart ( keys %svcnum ) {
705 next unless @{ $svcnum{$svcpart} };
707 my $svcdb = $svcnum{$svcpart}->[0]->part_svc->svcdb;
709 #find an empty place to put one
711 foreach my $pkgpart ( @{$pkgparts} ) {
713 qsearch('pkg_svc', { pkgpart => $pkgpart,
714 quantity => { op=>'>', value=>'0', } } );
716 # grep { $_->quantity > 0 } qsearch('pkg_svc', { pkgpart=>$pkgpart } );
717 if ( ! @{$cust_svc[$i]} #find an empty place to put them with
718 && grep { $svcdb eq $_->part_svc->svcdb } #with appropriate svcdb
722 ( grep { $svcdb eq $_->part_svc->svcdb } @pkg_svc )[0]->svcpart;
723 my $cust_svc = shift @{$svcnum{$svcpart}};
724 $cust_svc->svcpart($new_svcpart);
725 #warn "changing from $svcpart to $new_svcpart!!!\n";
726 $cust_svc[$i] = [ $cust_svc ];
733 #check for leftover services
734 foreach (keys %svcnum) {
735 next unless @{ $svcnum{$_} };
736 $dbh->rollback if $oldAutoCommit;
737 return "Leftover services, svcpart $_: svcnum ".
738 join(', ', map { $_->svcnum } @{ $svcnum{$_} } );
741 #no leftover services, let's make changes.
743 local $SIG{HUP} = 'IGNORE';
744 local $SIG{INT} = 'IGNORE';
745 local $SIG{QUIT} = 'IGNORE';
746 local $SIG{TERM} = 'IGNORE';
747 local $SIG{TSTP} = 'IGNORE';
748 local $SIG{PIPE} = 'IGNORE';
750 #first cancel old packages
751 foreach my $pkgnum ( @{$remove_pkgnums} ) {
752 my($old) = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
754 $dbh->rollback if $oldAutoCommit;
755 return "Package $pkgnum not found to remove!";
757 my(%hash) = $old->hash;
758 $hash{'cancel'}=time;
759 my($new) = new FS::cust_pkg ( \%hash );
760 my($error)=$new->replace($old);
762 $dbh->rollback if $oldAutoCommit;
763 return "Couldn't update package $pkgnum: $error";
767 #now add new packages, changing cust_svc records if necessary
769 while ($pkgpart=shift @{$pkgparts} ) {
771 my $new = new FS::cust_pkg {
772 'custnum' => $custnum,
773 'pkgpart' => $pkgpart,
775 my $error = $new->insert;
777 $dbh->rollback if $oldAutoCommit;
778 return "Couldn't insert new cust_pkg record: $error";
780 push @{$return_cust_pkg}, $new if $return_cust_pkg;
781 my $pkgnum = $new->pkgnum;
783 foreach my $cust_svc ( @{ shift @cust_svc } ) {
784 my(%hash) = $cust_svc->hash;
785 $hash{'pkgnum'}=$pkgnum;
786 my $new = new FS::cust_svc ( \%hash );
788 #avoid Record diffing missing changed svcpart field from above.
789 my $old = qsearchs('cust_svc', { 'svcnum' => $cust_svc->svcnum } );
791 my $error = $new->replace($old);
793 $dbh->rollback if $oldAutoCommit;
794 return "Couldn't link old service to new package: $error";
799 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
808 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
810 In sub order, the @pkgparts array (passed by reference) is clobbered.
812 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
813 method to pass dates to the recur_prog expression, it should do so.
815 FS::svc_acct, FS::svc_acct_sm, and FS::svc_domain are loaded via 'use' at
816 compile time, rather than via 'require' in sub { setup, suspend, unsuspend,
817 cancel } because they use %FS::UID::callback to load configuration values.
818 Probably need a subroutine which decides what to do based on whether or not
819 we've fetched the user yet, rather than a hash. See FS::UID and the TODO.
821 Now that things are transactional should the check in the insert method be
826 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
827 L<FS::pkg_svc>, schema.html from the base documentation