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.
106 # custnum might not have have been defined in sub check (for one-shot new
107 # customers), so check it here instead
109 my $error = $self->ut_number('custnum');
110 return $error if $error
112 return "Unknown customer"
113 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
115 $self->SUPER::insert;
121 Currently unimplemented. You don't want to delete billing items, because there
122 would then be no record the customer ever purchased the item. Instead, see
128 return "Can't delete cust_pkg records!";
131 =item replace OLD_RECORD
133 Replaces the OLD_RECORD with this one in the database. If there is an error,
134 returns the error, otherwise returns false.
136 Currently, custnum, setup, bill, susp, expire, and cancel may be changed.
138 Changing pkgpart may have disasterous effects. See the order subroutine.
140 setup and bill are normally updated by calling the bill method of a customer
141 object (see L<FS::cust_main>).
143 suspend is normally updated by the suspend and unsuspend methods.
145 cancel is normally updated by the cancel method (and also the order subroutine
151 my( $new, $old ) = ( shift, shift );
153 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
154 return "Can't change otaker!" if $old->otaker ne $new->otaker;
155 return "Can't change setup once it exists!"
156 if $old->getfield('setup') &&
157 $old->getfield('setup') != $new->getfield('setup');
158 #some logic for bill, susp, cancel?
160 $new->SUPER::replace($old);
165 Checks all fields to make sure this is a valid billing item. If there is an
166 error, returns the error, otherwise returns false. Called by the insert and
175 $self->ut_numbern('pkgnum')
176 || $self->ut_numbern('custnum')
177 || $self->ut_number('pkgpart')
178 || $self->ut_numbern('setup')
179 || $self->ut_numbern('bill')
180 || $self->ut_numbern('susp')
181 || $self->ut_numbern('cancel')
183 return $error if $error;
185 if ( $self->custnum ) {
186 return "Unknown customer"
187 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
190 return "Unknown pkgpart"
191 unless qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
193 $self->otaker(getotaker) unless $self->otaker;
194 $self->otaker =~ /^(\w{0,16})$/ or return "Illegal otaker";
202 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
203 in this package, then cancels the package itself (sets the cancel field to
206 If there is an error, returns the error, otherwise returns false.
214 local $SIG{HUP} = 'IGNORE';
215 local $SIG{INT} = 'IGNORE';
216 local $SIG{QUIT} = 'IGNORE';
217 local $SIG{TERM} = 'IGNORE';
218 local $SIG{TSTP} = 'IGNORE';
219 local $SIG{PIPE} = 'IGNORE';
221 my $oldAutoCommit = $FS::UID::AutoCommit;
222 local $FS::UID::AutoCommit = 0;
225 foreach my $cust_svc (
226 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
228 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
230 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
231 $dbh->rollback if $oldAutoCommit;
232 return "Illegal svcdb value in part_svc!";
235 require "FS/$svcdb.pm";
237 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
239 $error = $svc->cancel;
241 $dbh->rollback if $oldAutoCommit;
242 return "Error cancelling service: $error"
244 $error = $svc->delete;
246 $dbh->rollback if $oldAutoCommit;
247 return "Error deleting service: $error";
251 $error = $cust_svc->delete;
253 $dbh->rollback if $oldAutoCommit;
254 return "Error deleting cust_svc: $error";
259 unless ( $self->getfield('cancel') ) {
260 my %hash = $self->hash;
261 $hash{'cancel'} = time;
262 my $new = new FS::cust_pkg ( \%hash );
263 $error = $new->replace($self);
265 $dbh->rollback if $oldAutoCommit;
270 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
277 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
278 package, then suspends the package itself (sets the susp field to now).
280 If there is an error, returns the error, otherwise returns false.
288 local $SIG{HUP} = 'IGNORE';
289 local $SIG{INT} = 'IGNORE';
290 local $SIG{QUIT} = 'IGNORE';
291 local $SIG{TERM} = 'IGNORE';
292 local $SIG{TSTP} = 'IGNORE';
293 local $SIG{PIPE} = 'IGNORE';
295 my $oldAutoCommit = $FS::UID::AutoCommit;
296 local $FS::UID::AutoCommit = 0;
299 foreach my $cust_svc (
300 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
302 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
304 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
305 $dbh->rollback if $oldAutoCommit;
306 return "Illegal svcdb value in part_svc!";
309 require "FS/$svcdb.pm";
311 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
313 $error = $svc->suspend;
315 $dbh->rollback if $oldAutoCommit;
322 unless ( $self->getfield('susp') ) {
323 my %hash = $self->hash;
324 $hash{'susp'} = time;
325 my $new = new FS::cust_pkg ( \%hash );
326 $error = $new->replace($self);
328 $dbh->rollback if $oldAutoCommit;
333 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
340 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
341 package, then unsuspends the package itself (clears the susp field).
343 If there is an error, returns the error, otherwise returns false.
351 local $SIG{HUP} = 'IGNORE';
352 local $SIG{INT} = 'IGNORE';
353 local $SIG{QUIT} = 'IGNORE';
354 local $SIG{TERM} = 'IGNORE';
355 local $SIG{TSTP} = 'IGNORE';
356 local $SIG{PIPE} = 'IGNORE';
358 my $oldAutoCommit = $FS::UID::AutoCommit;
359 local $FS::UID::AutoCommit = 0;
362 foreach my $cust_svc (
363 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
365 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
367 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
368 $dbh->rollback if $oldAutoCommit;
369 return "Illegal svcdb value in part_svc!";
372 require "FS/$svcdb.pm";
374 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
376 $error = $svc->unsuspend;
378 $dbh->rollback if $oldAutoCommit;
385 unless ( ! $self->getfield('susp') ) {
386 my %hash = $self->hash;
388 my $new = new FS::cust_pkg ( \%hash );
389 $error = $new->replace($self);
391 $dbh->rollback if $oldAutoCommit;
396 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
403 Returns the definition for this billing item, as an FS::part_pkg object (see
410 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
415 Returns a list of lists, calling the label method for all services
416 (see L<FS::cust_svc>) of this billing item.
422 map { [ $_->label ] } qsearch ( 'cust_svc', { 'pkgnum' => $self->pkgnum } );
431 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF ]
433 CUSTNUM is a customer (see L<FS::cust_main>)
435 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
436 L<FS::part_pkg>) to order for this customer. Duplicates are of course
439 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
440 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
441 new billing items. An error is returned if this is not possible (see
447 my($custnum,$pkgparts,$remove_pkgnums)=@_;
449 my $oldAutoCommit = $FS::UID::AutoCommit;
450 local $FS::UID::AutoCommit = 0;
454 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
456 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
457 my($agent)=qsearchs('agent',{'agentnum'=> $cust_main->agentnum });
458 my %part_pkg = %{ $agent->pkgpart_hashref };
462 # for those packages being removed:
463 #@{ $svcnum{$svcpart} } goes from a svcpart to a list of FS::Record
464 # objects (table eq 'cust_svc')
466 foreach $pkgnum ( @{$remove_pkgnums} ) {
468 foreach $cust_svc (qsearch('cust_svc',{'pkgnum'=>$pkgnum})) {
469 push @{ $svcnum{$cust_svc->getfield('svcpart')} }, $cust_svc;
475 # for those packages the customer is purchasing:
476 # @{$pkgparts} is a list of said packages, by pkgpart
477 # @cust_svc is a corresponding list of lists of FS::Record objects
479 foreach $pkgpart ( @{$pkgparts} ) {
480 unless ( $part_pkg{$pkgpart} ) {
481 $dbh->rollback if $oldAutoCommit;
482 return "Customer not permitted to purchase pkgpart $pkgpart!";
486 ( $svcnum{$_} && @{ $svcnum{$_} } ) ? shift @{ $svcnum{$_} } : ();
487 } map { $_->svcpart } qsearch('pkg_svc', { 'pkgpart' => $pkgpart })
491 #check for leftover services
492 foreach (keys %svcnum) {
493 next unless @{ $svcnum{$_} };
494 $dbh->rollback if $oldAutoCommit;
495 return "Leftover services, svcpart $_: svcnum ".
496 join(', ', map { $_->svcnum } @{ $svcnum{$_} } );
499 #no leftover services, let's make changes.
501 local $SIG{HUP} = 'IGNORE';
502 local $SIG{INT} = 'IGNORE';
503 local $SIG{QUIT} = 'IGNORE';
504 local $SIG{TERM} = 'IGNORE';
505 local $SIG{TSTP} = 'IGNORE';
506 local $SIG{PIPE} = 'IGNORE';
508 #first cancel old packages
510 foreach $pkgnum ( @{$remove_pkgnums} ) {
511 my($old) = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
513 $dbh->rollback if $oldAutoCommit;
514 die "Package $pkgnum not found to remove!";
516 my(%hash) = $old->hash;
517 $hash{'cancel'}=time;
518 my($new) = new FS::cust_pkg ( \%hash );
519 my($error)=$new->replace($old);
521 $dbh->rollback if $oldAutoCommit;
522 die "Couldn't update package $pkgnum: $error";
526 #now add new packages, changing cust_svc records if necessary
528 while ($pkgpart=shift @{$pkgparts} ) {
530 my($new) = new FS::cust_pkg ( {
531 'custnum' => $custnum,
532 'pkgpart' => $pkgpart,
534 my($error) = $new->insert;
536 $dbh->rollback if $oldAutoCommit;
537 die "Couldn't insert new cust_pkg record: $error";
539 my($pkgnum)=$new->getfield('pkgnum');
542 foreach $cust_svc ( @{ shift @cust_svc } ) {
543 my(%hash) = $cust_svc->hash;
544 $hash{'pkgnum'}=$pkgnum;
545 my($new) = new FS::cust_svc ( \%hash );
546 my($error)=$new->replace($cust_svc);
548 $dbh->rollback if $oldAutoCommit;
549 die "Couldn't link old service to new package: $error";
554 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
563 $Id: cust_pkg.pm,v 1.5 2001-04-09 23:05:15 ivan Exp $
567 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
569 In sub order, the @pkgparts array (passed by reference) is clobbered.
571 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
572 method to pass dates to the recur_prog expression, it should do so.
574 FS::svc_acct, FS::svc_acct_sm, and FS::svc_domain are loaded via 'use' at
575 compile time, rather than via 'require' in sub { setup, suspend, unsuspend,
576 cancel } because they use %FS::UID::callback to load configuration values.
577 Probably need a subroutine which decides what to do based on whether or not
578 we've fetched the user yet, rather than a hash. See FS::UID and the TODO.
582 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>
583 , L<FS::pkg_svc>, schema.html from the base documentation