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
110 # (is this still necessary with transactions?)
112 my $error = $self->ut_number('custnum');
113 return $error if $error;
115 return "Unknown customer ". $self->custnum
116 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
118 $self->SUPER::insert;
124 Currently unimplemented. You don't want to delete billing items, because there
125 would then be no record the customer ever purchased the item. Instead, see
131 return "Can't delete cust_pkg records!";
134 =item replace OLD_RECORD
136 Replaces the OLD_RECORD with this one in the database. If there is an error,
137 returns the error, otherwise returns false.
139 Currently, custnum, setup, bill, susp, expire, and cancel may be changed.
141 Changing pkgpart may have disasterous effects. See the order subroutine.
143 setup and bill are normally updated by calling the bill method of a customer
144 object (see L<FS::cust_main>).
146 suspend is normally updated by the suspend and unsuspend methods.
148 cancel is normally updated by the cancel method (and also the order subroutine
154 my( $new, $old ) = ( shift, shift );
156 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
157 return "Can't change otaker!" if $old->otaker ne $new->otaker;
158 return "Can't change setup once it exists!"
159 if $old->getfield('setup') &&
160 $old->getfield('setup') != $new->getfield('setup');
161 #some logic for bill, susp, cancel?
163 $new->SUPER::replace($old);
168 Checks all fields to make sure this is a valid billing item. If there is an
169 error, returns the error, otherwise returns false. Called by the insert and
178 $self->ut_numbern('pkgnum')
179 || $self->ut_numbern('custnum')
180 || $self->ut_number('pkgpart')
181 || $self->ut_numbern('setup')
182 || $self->ut_numbern('bill')
183 || $self->ut_numbern('susp')
184 || $self->ut_numbern('cancel')
186 return $error if $error;
188 if ( $self->custnum ) {
189 return "Unknown customer"
190 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
193 return "Unknown pkgpart"
194 unless qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
196 $self->otaker(getotaker) unless $self->otaker;
197 $self->otaker =~ /^(\w{0,16})$/ or return "Illegal otaker";
205 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
206 in this package, then cancels the package itself (sets the cancel field to
209 If there is an error, returns the error, otherwise returns false.
217 local $SIG{HUP} = 'IGNORE';
218 local $SIG{INT} = 'IGNORE';
219 local $SIG{QUIT} = 'IGNORE';
220 local $SIG{TERM} = 'IGNORE';
221 local $SIG{TSTP} = 'IGNORE';
222 local $SIG{PIPE} = 'IGNORE';
224 my $oldAutoCommit = $FS::UID::AutoCommit;
225 local $FS::UID::AutoCommit = 0;
228 foreach my $cust_svc (
229 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
231 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
233 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
234 $dbh->rollback if $oldAutoCommit;
235 return "Illegal svcdb value in part_svc!";
238 require "FS/$svcdb.pm";
240 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
242 $error = $svc->cancel;
244 $dbh->rollback if $oldAutoCommit;
245 return "Error cancelling service: $error"
247 $error = $svc->delete;
249 $dbh->rollback if $oldAutoCommit;
250 return "Error deleting service: $error";
254 $error = $cust_svc->delete;
256 $dbh->rollback if $oldAutoCommit;
257 return "Error deleting cust_svc: $error";
262 unless ( $self->getfield('cancel') ) {
263 my %hash = $self->hash;
264 $hash{'cancel'} = time;
265 my $new = new FS::cust_pkg ( \%hash );
266 $error = $new->replace($self);
268 $dbh->rollback if $oldAutoCommit;
273 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
280 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
281 package, then suspends the package itself (sets the susp field to now).
283 If there is an error, returns the error, otherwise returns false.
291 local $SIG{HUP} = 'IGNORE';
292 local $SIG{INT} = 'IGNORE';
293 local $SIG{QUIT} = 'IGNORE';
294 local $SIG{TERM} = 'IGNORE';
295 local $SIG{TSTP} = 'IGNORE';
296 local $SIG{PIPE} = 'IGNORE';
298 my $oldAutoCommit = $FS::UID::AutoCommit;
299 local $FS::UID::AutoCommit = 0;
302 foreach my $cust_svc (
303 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
305 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
307 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
308 $dbh->rollback if $oldAutoCommit;
309 return "Illegal svcdb value in part_svc!";
312 require "FS/$svcdb.pm";
314 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
316 $error = $svc->suspend;
318 $dbh->rollback if $oldAutoCommit;
325 unless ( $self->getfield('susp') ) {
326 my %hash = $self->hash;
327 $hash{'susp'} = time;
328 my $new = new FS::cust_pkg ( \%hash );
329 $error = $new->replace($self);
331 $dbh->rollback if $oldAutoCommit;
336 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
343 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
344 package, then unsuspends the package itself (clears the susp field).
346 If there is an error, returns the error, otherwise returns false.
354 local $SIG{HUP} = 'IGNORE';
355 local $SIG{INT} = 'IGNORE';
356 local $SIG{QUIT} = 'IGNORE';
357 local $SIG{TERM} = 'IGNORE';
358 local $SIG{TSTP} = 'IGNORE';
359 local $SIG{PIPE} = 'IGNORE';
361 my $oldAutoCommit = $FS::UID::AutoCommit;
362 local $FS::UID::AutoCommit = 0;
365 foreach my $cust_svc (
366 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
368 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
370 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
371 $dbh->rollback if $oldAutoCommit;
372 return "Illegal svcdb value in part_svc!";
375 require "FS/$svcdb.pm";
377 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
379 $error = $svc->unsuspend;
381 $dbh->rollback if $oldAutoCommit;
388 unless ( ! $self->getfield('susp') ) {
389 my %hash = $self->hash;
391 my $new = new FS::cust_pkg ( \%hash );
392 $error = $new->replace($self);
394 $dbh->rollback if $oldAutoCommit;
399 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
406 Returns the definition for this billing item, as an FS::part_pkg object (see
413 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
418 Returns a list of lists, calling the label method for all services
419 (see L<FS::cust_svc>) of this billing item.
425 map { [ $_->label ] } qsearch ( 'cust_svc', { 'pkgnum' => $self->pkgnum } );
434 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ] ]
436 CUSTNUM is a customer (see L<FS::cust_main>)
438 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
439 L<FS::part_pkg>) to order for this customer. Duplicates are of course
442 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
443 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
444 new billing items. An error is returned if this is not possible (see
445 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
448 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
449 newly-created cust_pkg objects.
454 my($custnum, $pkgparts, $remove_pkgnums, $return_cust_pkg) = @_;
455 $remove_pkgnums = [] unless defined($remove_pkgnums);
457 my $oldAutoCommit = $FS::UID::AutoCommit;
458 local $FS::UID::AutoCommit = 0;
462 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
464 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
465 my($agent)=qsearchs('agent',{'agentnum'=> $cust_main->agentnum });
466 my %part_pkg = %{ $agent->pkgpart_hashref };
470 # for those packages being removed:
471 #@{ $svcnum{$svcpart} } goes from a svcpart to a list of FS::Record
472 # objects (table eq 'cust_svc')
474 foreach $pkgnum ( @{$remove_pkgnums} ) {
476 foreach $cust_svc (qsearch('cust_svc',{'pkgnum'=>$pkgnum})) {
477 push @{ $svcnum{$cust_svc->getfield('svcpart')} }, $cust_svc;
483 # for those packages the customer is purchasing:
484 # @{$pkgparts} is a list of said packages, by pkgpart
485 # @cust_svc is a corresponding list of lists of FS::Record objects
487 foreach $pkgpart ( @{$pkgparts} ) {
488 unless ( $part_pkg{$pkgpart} ) {
489 $dbh->rollback if $oldAutoCommit;
490 return "Customer not permitted to purchase pkgpart $pkgpart!";
494 ( $svcnum{$_} && @{ $svcnum{$_} } ) ? shift @{ $svcnum{$_} } : ();
495 } map { $_->svcpart } qsearch('pkg_svc', { 'pkgpart' => $pkgpart })
499 #check for leftover services
500 foreach (keys %svcnum) {
501 next unless @{ $svcnum{$_} };
502 $dbh->rollback if $oldAutoCommit;
503 return "Leftover services, svcpart $_: svcnum ".
504 join(', ', map { $_->svcnum } @{ $svcnum{$_} } );
507 #no leftover services, let's make changes.
509 local $SIG{HUP} = 'IGNORE';
510 local $SIG{INT} = 'IGNORE';
511 local $SIG{QUIT} = 'IGNORE';
512 local $SIG{TERM} = 'IGNORE';
513 local $SIG{TSTP} = 'IGNORE';
514 local $SIG{PIPE} = 'IGNORE';
516 #first cancel old packages
518 foreach $pkgnum ( @{$remove_pkgnums} ) {
519 my($old) = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
521 $dbh->rollback if $oldAutoCommit;
522 return "Package $pkgnum not found to remove!";
524 my(%hash) = $old->hash;
525 $hash{'cancel'}=time;
526 my($new) = new FS::cust_pkg ( \%hash );
527 my($error)=$new->replace($old);
529 $dbh->rollback if $oldAutoCommit;
530 return "Couldn't update package $pkgnum: $error";
534 #now add new packages, changing cust_svc records if necessary
536 while ($pkgpart=shift @{$pkgparts} ) {
538 my $new = new FS::cust_pkg {
539 'custnum' => $custnum,
540 'pkgpart' => $pkgpart,
542 my $error = $new->insert;
544 $dbh->rollback if $oldAutoCommit;
545 return "Couldn't insert new cust_pkg record: $error";
547 push @{$return_cust_pkg}, $new if $return_cust_pkg;
548 my $pkgnum = $new->pkgnum;
550 foreach my $cust_svc ( @{ shift @cust_svc } ) {
551 my(%hash) = $cust_svc->hash;
552 $hash{'pkgnum'}=$pkgnum;
553 my($new) = new FS::cust_svc ( \%hash );
554 my($error)=$new->replace($cust_svc);
556 $dbh->rollback if $oldAutoCommit;
557 return "Couldn't link old service to new package: $error";
562 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
571 $Id: cust_pkg.pm,v 1.8 2001-10-09 03:11:50 ivan Exp $
575 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
577 In sub order, the @pkgparts array (passed by reference) is clobbered.
579 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
580 method to pass dates to the recur_prog expression, it should do so.
582 FS::svc_acct, FS::svc_acct_sm, and FS::svc_domain are loaded via 'use' at
583 compile time, rather than via 'require' in sub { setup, suspend, unsuspend,
584 cancel } because they use %FS::UID::callback to load configuration values.
585 Probably need a subroutine which decides what to do based on whether or not
586 we've fetched the user yet, rather than a hash. See FS::UID and the TODO.
588 Now that things are transactional should the check in the insert method be
593 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>
594 , L<FS::pkg_svc>, schema.html from the base documentation