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>)
80 =item manual_flag - If this field is set to 1, disables the automatic
81 unsuspensiond of this package when using the B<unsuspendauto> config file.
85 Note: setup, bill, susp, expire and cancel are specified as UNIX timestamps;
86 see L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for
95 Create a new billing item. To add the item to the database, see L<"insert">.
99 sub table { 'cust_pkg'; }
103 Adds this billing item to the database ("Orders" the item). If there is an
104 error, returns the error, otherwise returns false.
111 # custnum might not have have been defined in sub check (for one-shot new
112 # customers), so check it here instead
113 # (is this still necessary with transactions?)
115 my $error = $self->ut_number('custnum');
116 return $error if $error;
118 return "Unknown customer ". $self->custnum unless $self->cust_main;
120 $self->SUPER::insert;
126 Currently unimplemented. You don't want to delete billing items, because there
127 would then be no record the customer ever purchased the item. Instead, see
133 return "Can't delete cust_pkg records!";
136 =item replace OLD_RECORD
138 Replaces the OLD_RECORD with this one in the database. If there is an error,
139 returns the error, otherwise returns false.
141 Currently, custnum, setup, bill, susp, expire, and cancel may be changed.
143 Changing pkgpart may have disasterous effects. See the order subroutine.
145 setup and bill are normally updated by calling the bill method of a customer
146 object (see L<FS::cust_main>).
148 suspend is normally updated by the suspend and unsuspend methods.
150 cancel is normally updated by the cancel method (and also the order subroutine
156 my( $new, $old ) = ( shift, shift );
158 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
159 return "Can't change otaker!" if $old->otaker ne $new->otaker;
162 #return "Can't change setup once it exists!"
163 # if $old->getfield('setup') &&
164 # $old->getfield('setup') != $new->getfield('setup');
166 #some logic for bill, susp, cancel?
168 $new->SUPER::replace($old);
173 Checks all fields to make sure this is a valid billing item. If there is an
174 error, returns the error, otherwise returns false. Called by the insert and
183 $self->ut_numbern('pkgnum')
184 || $self->ut_numbern('custnum')
185 || $self->ut_number('pkgpart')
186 || $self->ut_numbern('setup')
187 || $self->ut_numbern('bill')
188 || $self->ut_numbern('susp')
189 || $self->ut_numbern('cancel')
191 return $error if $error;
193 if ( $self->custnum ) {
194 return "Unknown customer ". $self->custnum unless $self->cust_main;
197 return "Unknown pkgpart"
198 unless qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
200 $self->otaker(getotaker) unless $self->otaker;
201 $self->otaker =~ /^(\w{0,16})$/ or return "Illegal otaker";
204 if ( $self->dbdef_table->column('manual_flag') ) {
205 $self->manual_flag =~ /^([01]?)$/ or return "Illegal manual_flag";
206 $self->manual_flag($1);
214 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
215 in this package, then cancels the package itself (sets the cancel field to
218 If there is an error, returns the error, otherwise returns false.
226 local $SIG{HUP} = 'IGNORE';
227 local $SIG{INT} = 'IGNORE';
228 local $SIG{QUIT} = 'IGNORE';
229 local $SIG{TERM} = 'IGNORE';
230 local $SIG{TSTP} = 'IGNORE';
231 local $SIG{PIPE} = 'IGNORE';
233 my $oldAutoCommit = $FS::UID::AutoCommit;
234 local $FS::UID::AutoCommit = 0;
237 foreach my $cust_svc (
238 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
240 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
242 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
243 $dbh->rollback if $oldAutoCommit;
244 return "Illegal svcdb value in part_svc!";
247 require "FS/$svcdb.pm";
249 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
251 $error = $svc->cancel;
253 $dbh->rollback if $oldAutoCommit;
254 return "Error cancelling service: $error"
256 $error = $svc->delete;
258 $dbh->rollback if $oldAutoCommit;
259 return "Error deleting service: $error";
263 $error = $cust_svc->delete;
265 $dbh->rollback if $oldAutoCommit;
266 return "Error deleting cust_svc: $error";
271 unless ( $self->getfield('cancel') ) {
272 my %hash = $self->hash;
273 $hash{'cancel'} = time;
274 my $new = new FS::cust_pkg ( \%hash );
275 $error = $new->replace($self);
277 $dbh->rollback if $oldAutoCommit;
282 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
289 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
290 package, then suspends the package itself (sets the susp field to now).
292 If there is an error, returns the error, otherwise returns false.
300 local $SIG{HUP} = 'IGNORE';
301 local $SIG{INT} = 'IGNORE';
302 local $SIG{QUIT} = 'IGNORE';
303 local $SIG{TERM} = 'IGNORE';
304 local $SIG{TSTP} = 'IGNORE';
305 local $SIG{PIPE} = 'IGNORE';
307 my $oldAutoCommit = $FS::UID::AutoCommit;
308 local $FS::UID::AutoCommit = 0;
311 foreach my $cust_svc (
312 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
314 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
316 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
317 $dbh->rollback if $oldAutoCommit;
318 return "Illegal svcdb value in part_svc!";
321 require "FS/$svcdb.pm";
323 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
325 $error = $svc->suspend;
327 $dbh->rollback if $oldAutoCommit;
334 unless ( $self->getfield('susp') ) {
335 my %hash = $self->hash;
336 $hash{'susp'} = time;
337 my $new = new FS::cust_pkg ( \%hash );
338 $error = $new->replace($self);
340 $dbh->rollback if $oldAutoCommit;
345 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
352 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
353 package, then unsuspends the package itself (clears the susp field).
355 If there is an error, returns the error, otherwise returns false.
363 local $SIG{HUP} = 'IGNORE';
364 local $SIG{INT} = 'IGNORE';
365 local $SIG{QUIT} = 'IGNORE';
366 local $SIG{TERM} = 'IGNORE';
367 local $SIG{TSTP} = 'IGNORE';
368 local $SIG{PIPE} = 'IGNORE';
370 my $oldAutoCommit = $FS::UID::AutoCommit;
371 local $FS::UID::AutoCommit = 0;
374 foreach my $cust_svc (
375 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
377 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
379 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
380 $dbh->rollback if $oldAutoCommit;
381 return "Illegal svcdb value in part_svc!";
384 require "FS/$svcdb.pm";
386 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
388 $error = $svc->unsuspend;
390 $dbh->rollback if $oldAutoCommit;
397 unless ( ! $self->getfield('susp') ) {
398 my %hash = $self->hash;
400 my $new = new FS::cust_pkg ( \%hash );
401 $error = $new->replace($self);
403 $dbh->rollback if $oldAutoCommit;
408 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
415 Returns the definition for this billing item, as an FS::part_pkg object (see
422 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
427 Returns a list of lists, calling the label method for all services
428 (see L<FS::cust_svc>) of this billing item.
434 map { [ $_->label ] } qsearch ( 'cust_svc', { 'pkgnum' => $self->pkgnum } );
439 Returns the parent customer object (see L<FS::cust_main>).
445 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
454 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ] ]
456 CUSTNUM is a customer (see L<FS::cust_main>)
458 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
459 L<FS::part_pkg>) to order for this customer. Duplicates are of course
462 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
463 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
464 new billing items. An error is returned if this is not possible (see
465 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
468 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
469 newly-created cust_pkg objects.
474 my($custnum, $pkgparts, $remove_pkgnums, $return_cust_pkg) = @_;
475 $remove_pkgnums = [] unless defined($remove_pkgnums);
477 my $oldAutoCommit = $FS::UID::AutoCommit;
478 local $FS::UID::AutoCommit = 0;
482 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
484 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
485 my($agent)=qsearchs('agent',{'agentnum'=> $cust_main->agentnum });
486 my %part_pkg = %{ $agent->pkgpart_hashref };
490 # for those packages being removed:
491 #@{ $svcnum{$svcpart} } goes from a svcpart to a list of FS::Record
492 # objects (table eq 'cust_svc')
494 foreach $pkgnum ( @{$remove_pkgnums} ) {
496 foreach $cust_svc (qsearch('cust_svc',{'pkgnum'=>$pkgnum})) {
497 push @{ $svcnum{$cust_svc->getfield('svcpart')} }, $cust_svc;
503 # for those packages the customer is purchasing:
504 # @{$pkgparts} is a list of said packages, by pkgpart
505 # @cust_svc is a corresponding list of lists of FS::Record objects
507 foreach $pkgpart ( @{$pkgparts} ) {
508 unless ( $part_pkg{$pkgpart} ) {
509 $dbh->rollback if $oldAutoCommit;
510 return "Customer not permitted to purchase pkgpart $pkgpart!";
514 ( $svcnum{$_} && @{ $svcnum{$_} } ) ? shift @{ $svcnum{$_} } : ();
515 } map { $_->svcpart } qsearch('pkg_svc', { 'pkgpart' => $pkgpart })
519 #check for leftover services
520 foreach (keys %svcnum) {
521 next unless @{ $svcnum{$_} };
522 $dbh->rollback if $oldAutoCommit;
523 return "Leftover services, svcpart $_: svcnum ".
524 join(', ', map { $_->svcnum } @{ $svcnum{$_} } );
527 #no leftover services, let's make changes.
529 local $SIG{HUP} = 'IGNORE';
530 local $SIG{INT} = 'IGNORE';
531 local $SIG{QUIT} = 'IGNORE';
532 local $SIG{TERM} = 'IGNORE';
533 local $SIG{TSTP} = 'IGNORE';
534 local $SIG{PIPE} = 'IGNORE';
536 #first cancel old packages
538 foreach $pkgnum ( @{$remove_pkgnums} ) {
539 my($old) = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
541 $dbh->rollback if $oldAutoCommit;
542 return "Package $pkgnum not found to remove!";
544 my(%hash) = $old->hash;
545 $hash{'cancel'}=time;
546 my($new) = new FS::cust_pkg ( \%hash );
547 my($error)=$new->replace($old);
549 $dbh->rollback if $oldAutoCommit;
550 return "Couldn't update package $pkgnum: $error";
554 #now add new packages, changing cust_svc records if necessary
556 while ($pkgpart=shift @{$pkgparts} ) {
558 my $new = new FS::cust_pkg {
559 'custnum' => $custnum,
560 'pkgpart' => $pkgpart,
562 my $error = $new->insert;
564 $dbh->rollback if $oldAutoCommit;
565 return "Couldn't insert new cust_pkg record: $error";
567 push @{$return_cust_pkg}, $new if $return_cust_pkg;
568 my $pkgnum = $new->pkgnum;
570 foreach my $cust_svc ( @{ shift @cust_svc } ) {
571 my(%hash) = $cust_svc->hash;
572 $hash{'pkgnum'}=$pkgnum;
573 my($new) = new FS::cust_svc ( \%hash );
574 my($error)=$new->replace($cust_svc);
576 $dbh->rollback if $oldAutoCommit;
577 return "Couldn't link old service to new package: $error";
582 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
591 $Id: cust_pkg.pm,v 1.11 2001-10-15 14:58:03 ivan Exp $
595 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
597 In sub order, the @pkgparts array (passed by reference) is clobbered.
599 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
600 method to pass dates to the recur_prog expression, it should do so.
602 FS::svc_acct, FS::svc_acct_sm, and FS::svc_domain are loaded via 'use' at
603 compile time, rather than via 'require' in sub { setup, suspend, unsuspend,
604 cancel } because they use %FS::UID::callback to load configuration values.
605 Probably need a subroutine which decides what to do based on whether or not
606 we've fetched the user yet, rather than a hash. See FS::UID and the TODO.
608 Now that things are transactional should the check in the insert method be
613 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>,
614 L<FS::pkg_svc>, schema.html from the base documentation