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
119 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
121 $self->SUPER::insert;
127 Currently unimplemented. You don't want to delete billing items, because there
128 would then be no record the customer ever purchased the item. Instead, see
134 return "Can't delete cust_pkg records!";
137 =item replace OLD_RECORD
139 Replaces the OLD_RECORD with this one in the database. If there is an error,
140 returns the error, otherwise returns false.
142 Currently, custnum, setup, bill, susp, expire, and cancel may be changed.
144 Changing pkgpart may have disasterous effects. See the order subroutine.
146 setup and bill are normally updated by calling the bill method of a customer
147 object (see L<FS::cust_main>).
149 suspend is normally updated by the suspend and unsuspend methods.
151 cancel is normally updated by the cancel method (and also the order subroutine
157 my( $new, $old ) = ( shift, shift );
159 #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
160 return "Can't change otaker!" if $old->otaker ne $new->otaker;
161 return "Can't change setup once it exists!"
162 if $old->getfield('setup') &&
163 $old->getfield('setup') != $new->getfield('setup');
164 #some logic for bill, susp, cancel?
166 $new->SUPER::replace($old);
171 Checks all fields to make sure this is a valid billing item. If there is an
172 error, returns the error, otherwise returns false. Called by the insert and
181 $self->ut_numbern('pkgnum')
182 || $self->ut_numbern('custnum')
183 || $self->ut_number('pkgpart')
184 || $self->ut_numbern('setup')
185 || $self->ut_numbern('bill')
186 || $self->ut_numbern('susp')
187 || $self->ut_numbern('cancel')
189 return $error if $error;
191 if ( $self->custnum ) {
192 return "Unknown customer"
193 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
196 return "Unknown pkgpart"
197 unless qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
199 $self->otaker(getotaker) unless $self->otaker;
200 $self->otaker =~ /^(\w{0,16})$/ or return "Illegal otaker";
203 if ( $self->dbdef_table->column('manual_flag') ) {
204 $self->manual_flag =~ /^([01]?)$/ or return "Illegal manual_flag";
205 $self->manual_flag($1);
213 Cancels and removes all services (see L<FS::cust_svc> and L<FS::part_svc>)
214 in this package, then cancels the package itself (sets the cancel field to
217 If there is an error, returns the error, otherwise returns false.
225 local $SIG{HUP} = 'IGNORE';
226 local $SIG{INT} = 'IGNORE';
227 local $SIG{QUIT} = 'IGNORE';
228 local $SIG{TERM} = 'IGNORE';
229 local $SIG{TSTP} = 'IGNORE';
230 local $SIG{PIPE} = 'IGNORE';
232 my $oldAutoCommit = $FS::UID::AutoCommit;
233 local $FS::UID::AutoCommit = 0;
236 foreach my $cust_svc (
237 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
239 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
241 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
242 $dbh->rollback if $oldAutoCommit;
243 return "Illegal svcdb value in part_svc!";
246 require "FS/$svcdb.pm";
248 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
250 $error = $svc->cancel;
252 $dbh->rollback if $oldAutoCommit;
253 return "Error cancelling service: $error"
255 $error = $svc->delete;
257 $dbh->rollback if $oldAutoCommit;
258 return "Error deleting service: $error";
262 $error = $cust_svc->delete;
264 $dbh->rollback if $oldAutoCommit;
265 return "Error deleting cust_svc: $error";
270 unless ( $self->getfield('cancel') ) {
271 my %hash = $self->hash;
272 $hash{'cancel'} = time;
273 my $new = new FS::cust_pkg ( \%hash );
274 $error = $new->replace($self);
276 $dbh->rollback if $oldAutoCommit;
281 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
288 Suspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
289 package, then suspends the package itself (sets the susp field to now).
291 If there is an error, returns the error, otherwise returns false.
299 local $SIG{HUP} = 'IGNORE';
300 local $SIG{INT} = 'IGNORE';
301 local $SIG{QUIT} = 'IGNORE';
302 local $SIG{TERM} = 'IGNORE';
303 local $SIG{TSTP} = 'IGNORE';
304 local $SIG{PIPE} = 'IGNORE';
306 my $oldAutoCommit = $FS::UID::AutoCommit;
307 local $FS::UID::AutoCommit = 0;
310 foreach my $cust_svc (
311 qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
313 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
315 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
316 $dbh->rollback if $oldAutoCommit;
317 return "Illegal svcdb value in part_svc!";
320 require "FS/$svcdb.pm";
322 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
324 $error = $svc->suspend;
326 $dbh->rollback if $oldAutoCommit;
333 unless ( $self->getfield('susp') ) {
334 my %hash = $self->hash;
335 $hash{'susp'} = time;
336 my $new = new FS::cust_pkg ( \%hash );
337 $error = $new->replace($self);
339 $dbh->rollback if $oldAutoCommit;
344 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
351 Unsuspends all services (see L<FS::cust_svc> and L<FS::part_svc>) in this
352 package, then unsuspends the package itself (clears the susp field).
354 If there is an error, returns the error, otherwise returns false.
362 local $SIG{HUP} = 'IGNORE';
363 local $SIG{INT} = 'IGNORE';
364 local $SIG{QUIT} = 'IGNORE';
365 local $SIG{TERM} = 'IGNORE';
366 local $SIG{TSTP} = 'IGNORE';
367 local $SIG{PIPE} = 'IGNORE';
369 my $oldAutoCommit = $FS::UID::AutoCommit;
370 local $FS::UID::AutoCommit = 0;
373 foreach my $cust_svc (
374 qsearch('cust_svc',{'pkgnum'=> $self->pkgnum } )
376 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $cust_svc->svcpart } );
378 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
379 $dbh->rollback if $oldAutoCommit;
380 return "Illegal svcdb value in part_svc!";
383 require "FS/$svcdb.pm";
385 my $svc = qsearchs( $svcdb, { 'svcnum' => $cust_svc->svcnum } );
387 $error = $svc->unsuspend;
389 $dbh->rollback if $oldAutoCommit;
396 unless ( ! $self->getfield('susp') ) {
397 my %hash = $self->hash;
399 my $new = new FS::cust_pkg ( \%hash );
400 $error = $new->replace($self);
402 $dbh->rollback if $oldAutoCommit;
407 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
414 Returns the definition for this billing item, as an FS::part_pkg object (see
421 qsearchs( 'part_pkg', { 'pkgpart' => $self->pkgpart } );
426 Returns a list of lists, calling the label method for all services
427 (see L<FS::cust_svc>) of this billing item.
433 map { [ $_->label ] } qsearch ( 'cust_svc', { 'pkgnum' => $self->pkgnum } );
442 =item order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ] ]
444 CUSTNUM is a customer (see L<FS::cust_main>)
446 PKGPARTS is a list of pkgparts specifying the the billing item definitions (see
447 L<FS::part_pkg>) to order for this customer. Duplicates are of course
450 REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to
451 remove for this customer. The services (see L<FS::cust_svc>) are moved to the
452 new billing items. An error is returned if this is not possible (see
453 L<FS::pkg_svc>). An empty arrayref is equivalent to not specifying this
456 RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the
457 newly-created cust_pkg objects.
462 my($custnum, $pkgparts, $remove_pkgnums, $return_cust_pkg) = @_;
463 $remove_pkgnums = [] unless defined($remove_pkgnums);
465 my $oldAutoCommit = $FS::UID::AutoCommit;
466 local $FS::UID::AutoCommit = 0;
470 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
472 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
473 my($agent)=qsearchs('agent',{'agentnum'=> $cust_main->agentnum });
474 my %part_pkg = %{ $agent->pkgpart_hashref };
478 # for those packages being removed:
479 #@{ $svcnum{$svcpart} } goes from a svcpart to a list of FS::Record
480 # objects (table eq 'cust_svc')
482 foreach $pkgnum ( @{$remove_pkgnums} ) {
484 foreach $cust_svc (qsearch('cust_svc',{'pkgnum'=>$pkgnum})) {
485 push @{ $svcnum{$cust_svc->getfield('svcpart')} }, $cust_svc;
491 # for those packages the customer is purchasing:
492 # @{$pkgparts} is a list of said packages, by pkgpart
493 # @cust_svc is a corresponding list of lists of FS::Record objects
495 foreach $pkgpart ( @{$pkgparts} ) {
496 unless ( $part_pkg{$pkgpart} ) {
497 $dbh->rollback if $oldAutoCommit;
498 return "Customer not permitted to purchase pkgpart $pkgpart!";
502 ( $svcnum{$_} && @{ $svcnum{$_} } ) ? shift @{ $svcnum{$_} } : ();
503 } map { $_->svcpart } qsearch('pkg_svc', { 'pkgpart' => $pkgpart })
507 #check for leftover services
508 foreach (keys %svcnum) {
509 next unless @{ $svcnum{$_} };
510 $dbh->rollback if $oldAutoCommit;
511 return "Leftover services, svcpart $_: svcnum ".
512 join(', ', map { $_->svcnum } @{ $svcnum{$_} } );
515 #no leftover services, let's make changes.
517 local $SIG{HUP} = 'IGNORE';
518 local $SIG{INT} = 'IGNORE';
519 local $SIG{QUIT} = 'IGNORE';
520 local $SIG{TERM} = 'IGNORE';
521 local $SIG{TSTP} = 'IGNORE';
522 local $SIG{PIPE} = 'IGNORE';
524 #first cancel old packages
526 foreach $pkgnum ( @{$remove_pkgnums} ) {
527 my($old) = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
529 $dbh->rollback if $oldAutoCommit;
530 return "Package $pkgnum not found to remove!";
532 my(%hash) = $old->hash;
533 $hash{'cancel'}=time;
534 my($new) = new FS::cust_pkg ( \%hash );
535 my($error)=$new->replace($old);
537 $dbh->rollback if $oldAutoCommit;
538 return "Couldn't update package $pkgnum: $error";
542 #now add new packages, changing cust_svc records if necessary
544 while ($pkgpart=shift @{$pkgparts} ) {
546 my $new = new FS::cust_pkg {
547 'custnum' => $custnum,
548 'pkgpart' => $pkgpart,
550 my $error = $new->insert;
552 $dbh->rollback if $oldAutoCommit;
553 return "Couldn't insert new cust_pkg record: $error";
555 push @{$return_cust_pkg}, $new if $return_cust_pkg;
556 my $pkgnum = $new->pkgnum;
558 foreach my $cust_svc ( @{ shift @cust_svc } ) {
559 my(%hash) = $cust_svc->hash;
560 $hash{'pkgnum'}=$pkgnum;
561 my($new) = new FS::cust_svc ( \%hash );
562 my($error)=$new->replace($cust_svc);
564 $dbh->rollback if $oldAutoCommit;
565 return "Couldn't link old service to new package: $error";
570 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
579 $Id: cust_pkg.pm,v 1.9 2001-10-09 23:10:16 ivan Exp $
583 sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?
585 In sub order, the @pkgparts array (passed by reference) is clobbered.
587 Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard
588 method to pass dates to the recur_prog expression, it should do so.
590 FS::svc_acct, FS::svc_acct_sm, and FS::svc_domain are loaded via 'use' at
591 compile time, rather than via 'require' in sub { setup, suspend, unsuspend,
592 cancel } because they use %FS::UID::callback to load configuration values.
593 Probably need a subroutine which decides what to do based on whether or not
594 we've fetched the user yet, rather than a hash. See FS::UID and the TODO.
596 Now that things are transactional should the check in the insert method be
601 L<FS::Record>, L<FS::cust_main>, L<FS::part_pkg>, L<FS::cust_svc>
602 , L<FS::pkg_svc>, schema.html from the base documentation