5 use FS::UID qw( getotaker dbh );
6 use FS::Record qw( qsearch qsearchs );
13 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
15 # because they load configuraion by setting FS::UID::callback (see TODO)
21 @ISA = qw( FS::Record );
25 FS::cust_pkg - Object methods for cust_pkg objects
31 $record = new FS::cust_pkg \%hash;
32 $record = new FS::cust_pkg { 'column' => 'value' };
34 $error = $record->insert;
36 $error = $new_record->replace($old_record);
38 $error = $record->delete;
40 $error = $record->check;
42 $error = $record->cancel;
44 $error = $record->suspend;
46 $error = $record->unsuspend;
48 $part_pkg = $record->part_pkg;
50 @labels = $record->labels;
52 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
53 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );
57 An FS::cust_pkg object represents a customer billing item. FS::cust_pkg
58 inherits from FS::Record. The following fields are currently supported:
62 =item pkgnum - primary key (assigned automatically for new billing items)
64 =item custnum - Customer (see L<FS::cust_main>)
66 =item pkgpart - Billing item definition (see L<FS::part_pkg>)
78 =item otaker - order taker (assigned automatically if null, see L<FS::UID>)
82 Note: setup, bill, susp, expire and cancel are specified as UNIX timestamps;
83 see L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for
92 Create a new billing item. To add the item to the database, see L<"insert">.
96 sub table { 'cust_pkg'; }
100 Adds this billing item to the database ("Orders" the item). If there is an
101 error, returns the error, otherwise returns false.
108 # custnum might not have have been defined in sub check (for one-shot new
109 # customers), so check it here instead
111 my $error = $self->ut_number('custnum');
112 return $error if $error
114 return "Unknown customer"
115 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
117 $self->SUPER::insert;
123 Currently unimplemented. You don't want to delete billing items, because there
124 would then be no record the customer ever purchased the item. Instead, see
130 return "Can't delete cust_pkg records!";
133 =item replace OLD_RECORD
135 Replaces the OLD_RECORD with this one in the database. If there is an error,
136 returns the error, otherwise returns false.
138 Currently, custnum, setup, bill, susp, expire, and cancel may be changed.
140 Changing pkgpart may have disasterous effects. See the order subroutine.
142 setup and bill are normally updated by calling the bill method of a customer
143 object (see L<FS::cust_main>).
145 suspend is normally updated by the suspend and unsuspend methods.
147 cancel is normally updated by the cancel method (and also the order subroutine
153 my( $new, $old ) = ( shift, shift );
155 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
156 return "Can't change otaker!" if $old->otaker ne $new->otaker;
157 return "Can't change setup once it exists!"
158 if $old->getfield('setup') &&
159 $old->getfield('setup') != $new->getfield('setup');
160 #some logic for bill, susp, cancel?
162 $new->SUPER::replace($old);
167 Checks all fields to make sure this is a valid billing item. If there is an
168 error, returns the error, otherwise returns false. Called by the insert and
177 $self->ut_numbern('pkgnum')
178 || $self->ut_numbern('custnum')
179 || $self->ut_number('pkgpart')
180 || $self->ut_numbern('setup')
181 || $self->ut_numbern('bill')
182 || $self->ut_numbern('susp')
183 || $self->ut_numbern('cancel')
185 return $error if $error;
187 if ( $self->custnum ) {
188 return "Unknown customer"
189 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
192 return "Unknown pkgpart"
193 unless qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
195 $self->otaker(getotaker) unless $self->otaker;
196 $self->otaker =~ /^(\w{0,16})$/ or return "Illegal otaker";
204 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
205 in this package, then cancels the package itself (sets the cancel field to
208 If there is an error, returns the error, otherwise returns false.
216 local $SIG{HUP} = 'IGNORE';
217 local $SIG{INT} = 'IGNORE';
218 local $SIG{QUIT} = 'IGNORE';
219 local $SIG{TERM} = 'IGNORE';
220 local $SIG{TSTP} = 'IGNORE';
221 local $SIG{PIPE} = 'IGNORE';
223 my $oldAutoCommit = $FS::UID::AutoCommit;
224 local $FS::UID::AutoCommit = 0;
227 foreach my $cust_svc (
228 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
230 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
232 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
233 $dbh->rollback if $oldAutoCommit;
234 return "Illegal svcdb value in part_svc!";
237 require "FS/$svcdb.pm";
239 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
241 $error = $svc->cancel;
243 $dbh->rollback if $oldAutoCommit;
244 return "Error cancelling service: $error"
246 $error = $svc->delete;
248 $dbh->rollback if $oldAutoCommit;
249 return "Error deleting service: $error";
253 $error = $cust_svc->delete;
255 $dbh->rollback if $oldAutoCommit;
256 return "Error deleting cust_svc: $error";
261 unless ( $self->getfield('cancel') ) {
262 my %hash = $self->hash;
263 $hash{'cancel'} = time;
264 my $new = new FS::cust_pkg ( \%hash );
265 $error = $new->replace($self);
267 $dbh->rollback if $oldAutoCommit;
272 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
279 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
280 package, then suspends the package itself (sets the susp field to now).
282 If there is an error, returns the error, otherwise returns false.
290 local $SIG{HUP} = 'IGNORE';
291 local $SIG{INT} = 'IGNORE';
292 local $SIG{QUIT} = 'IGNORE';
293 local $SIG{TERM} = 'IGNORE';
294 local $SIG{TSTP} = 'IGNORE';
295 local $SIG{PIPE} = 'IGNORE';
297 my $oldAutoCommit = $FS::UID::AutoCommit;
298 local $FS::UID::AutoCommit = 0;
301 foreach my $cust_svc (
302 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
304 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
306 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
307 $dbh->rollback if $oldAutoCommit;
308 return "Illegal svcdb value in part_svc!";
311 require "FS/$svcdb.pm";
313 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
315 $error = $svc->suspend;
317 $dbh->rollback if $oldAutoCommit;
324 unless ( $self->getfield('susp') ) {
325 my %hash = $self->hash;
326 $hash{'susp'} = time;
327 my $new = new FS::cust_pkg ( \%hash );
328 $error = $new->replace($self);
330 $dbh->rollback if $oldAutoCommit;
335 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
342 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
343 package, then unsuspends the package itself (clears the susp field).
345 If there is an error, returns the error, otherwise returns false.
353 local $SIG{HUP} = 'IGNORE';
354 local $SIG{INT} = 'IGNORE';
355 local $SIG{QUIT} = 'IGNORE';
356 local $SIG{TERM} = 'IGNORE';
357 local $SIG{TSTP} = 'IGNORE';
358 local $SIG{PIPE} = 'IGNORE';
360 my $oldAutoCommit = $FS::UID::AutoCommit;
361 local $FS::UID::AutoCommit = 0;
364 foreach my $cust_svc (
365 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
367 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
369 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
370 $dbh->rollback if $oldAutoCommit;
371 return "Illegal svcdb value in part_svc!";
374 require "FS/$svcdb.pm";
376 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
378 $error = $svc->unsuspend;
380 $dbh->rollback if $oldAutoCommit;
387 unless ( ! $self->getfield('susp') ) {
388 my %hash = $self->hash;
390 my $new = new FS::cust_pkg ( \%hash );
391 $error = $new->replace($self);
393 $dbh->rollback if $oldAutoCommit;
398 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
405 Returns the definition for this billing item, as an FS::part_pkg object (see
412 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
417 Returns a list of lists, calling the label method for all services
418 (see L<FS::cust_svc>) of this billing item.
424 map { [ $_->label ] } qsearch ( 'cust_svc', { 'pkgnum' => $self->pkgnum } );
433 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ] ]
435 CUSTNUM is a customer (see L<FS::cust_main>)
437 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
438 L<FS::part_pkg>) to order for this customer. Duplicates are of course
441 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
442 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
443 new billing items. An error is returned if this is not possible (see
444 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
447 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
448 newly-created cust_pkg objects.
453 my($custnum, $pkgparts, $remove_pkgnums, $return_cust_pkg) = @_;
454 $remove_pkgnums = [] unless defined($remove_pkgnums);
456 my $oldAutoCommit = $FS::UID::AutoCommit;
457 local $FS::UID::AutoCommit = 0;
461 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
463 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
464 my($agent)=qsearchs('agent',{'agentnum'=> $cust_main->agentnum });
465 my %part_pkg = %{ $agent->pkgpart_hashref };
469 # for those packages being removed:
470 #@{ $svcnum{$svcpart} } goes from a svcpart to a list of FS::Record
471 # objects (table eq 'cust_svc')
473 foreach $pkgnum ( @{$remove_pkgnums} ) {
475 foreach $cust_svc (qsearch('cust_svc',{'pkgnum'=>$pkgnum})) {
476 push @{ $svcnum{$cust_svc->getfield('svcpart')} }, $cust_svc;
482 # for those packages the customer is purchasing:
483 # @{$pkgparts} is a list of said packages, by pkgpart
484 # @cust_svc is a corresponding list of lists of FS::Record objects
486 foreach $pkgpart ( @{$pkgparts} ) {
487 unless ( $part_pkg{$pkgpart} ) {
488 $dbh->rollback if $oldAutoCommit;
489 return "Customer not permitted to purchase pkgpart $pkgpart!";
493 ( $svcnum{$_} && @{ $svcnum{$_} } ) ? shift @{ $svcnum{$_} } : ();
494 } map { $_->svcpart } qsearch('pkg_svc', { 'pkgpart' => $pkgpart })
498 #check for leftover services
499 foreach (keys %svcnum) {
500 next unless @{ $svcnum{$_} };
501 $dbh->rollback if $oldAutoCommit;
502 return "Leftover services, svcpart $_: svcnum ".
503 join(', ', map { $_->svcnum } @{ $svcnum{$_} } );
506 #no leftover services, let's make changes.
508 local $SIG{HUP} = 'IGNORE';
509 local $SIG{INT} = 'IGNORE';
510 local $SIG{QUIT} = 'IGNORE';
511 local $SIG{TERM} = 'IGNORE';
512 local $SIG{TSTP} = 'IGNORE';
513 local $SIG{PIPE} = 'IGNORE';
515 #first cancel old packages
517 foreach $pkgnum ( @{$remove_pkgnums} ) {
518 my($old) = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
520 $dbh->rollback if $oldAutoCommit;
521 return "Package $pkgnum not found to remove!";
523 my(%hash) = $old->hash;
524 $hash{'cancel'}=time;
525 my($new) = new FS::cust_pkg ( \%hash );
526 my($error)=$new->replace($old);
528 $dbh->rollback if $oldAutoCommit;
529 return "Couldn't update package $pkgnum: $error";
533 #now add new packages, changing cust_svc records if necessary
535 while ($pkgpart=shift @{$pkgparts} ) {
537 my $new = new FS::cust_pkg {
538 'custnum' => $custnum,
539 'pkgpart' => $pkgpart,
541 my $error = $new->insert;
543 $dbh->rollback if $oldAutoCommit;
544 return "Couldn't insert new cust_pkg record: $error";
546 push @{$return_cust_pkg}, $new if $return_cust_pkg;
547 my $pkgnum = $new->pkgnum;
549 foreach my $cust_svc ( @{ shift @cust_svc } ) {
550 my(%hash) = $cust_svc->hash;
551 $hash{'pkgnum'}=$pkgnum;
552 my($new) = new FS::cust_svc ( \%hash );
553 my($error)=$new->replace($cust_svc);
555 $dbh->rollback if $oldAutoCommit;
556 return "Couldn't link old service to new package: $error";
561 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
570 $Id: cust_pkg.pm,v 1.7 2001-10-01 10:31:08 ivan Exp $
574 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
576 In sub order, the @pkgparts array (passed by reference) is clobbered.
578 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
579 method to pass dates to the recur_prog expression, it should do so.
581 FS::svc_acct, FS::svc_acct_sm, and FS::svc_domain are loaded via 'use' at
582 compile time, rather than via 'require' in sub { setup, suspend, unsuspend,
583 cancel } because they use %FS::UID::callback to load configuration values.
584 Probably need a subroutine which decides what to do based on whether or not
585 we've fetched the user yet, rather than a hash. See FS::UID and the TODO.
589 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>
590 , L<FS::pkg_svc>, schema.html from the base documentation