4 use vars qw( @ISA %freq %plans $DEBUG );
5 use Carp qw(carp cluck confess);
8 use FS::Record qw( qsearch qsearchs dbh dbdef );
14 use FS::part_pkg_option;
16 @ISA = qw( FS::Record ); # FS::option_Common ); # this can use option_Common
17 # when all the plandata bs is
24 FS::part_pkg - Object methods for part_pkg objects
30 $record = new FS::part_pkg \%hash
31 $record = new FS::part_pkg { 'column' => 'value' };
33 $custom_record = $template_record->clone;
35 $error = $record->insert;
37 $error = $new_record->replace($old_record);
39 $error = $record->delete;
41 $error = $record->check;
43 @pkg_svc = $record->pkg_svc;
45 $svcnum = $record->svcpart;
46 $svcnum = $record->svcpart( 'svc_acct' );
50 An FS::part_pkg object represents a package definition. FS::part_pkg
51 inherits from FS::Record. The following fields are currently supported:
55 =item pkgpart - primary key (assigned automatically for new package definitions)
57 =item pkg - Text name of this package definition (customer-viewable)
59 =item comment - Text name of this package definition (non-customer-viewable)
61 =item promo_code - Promotional code
63 =item setup - Setup fee expression (deprecated)
65 =item freq - Frequency of recurring fee
67 =item recur - Recurring fee expression (deprecated)
69 =item setuptax - Setup fee tax exempt flag, empty or `Y'
71 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
73 =item taxclass - Tax class
75 =item plan - Price plan
77 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
79 =item disabled - Disabled flag, empty or `Y'
89 Creates a new package definition. To add the package definition to
90 the database, see L<"insert">.
94 sub table { 'part_pkg'; }
98 An alternate constructor. Creates a new package definition by duplicating
99 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
100 to the comment field. To add the package definition to the database, see
107 my $class = ref($self);
108 my %hash = $self->hash;
109 $hash{'pkgpart'} = '';
110 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
111 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
112 #new FS::part_pkg ( \%hash ); # ?
113 new $class ( \%hash ); # ?
116 =item insert [ , OPTION => VALUE ... ]
118 Adds this package definition to the database. If there is an error,
119 returns the error, otherwise returns false.
121 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
122 I<custnum_ref> and I<options>.
124 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
125 values, appropriate FS::pkg_svc records will be inserted.
127 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
128 FS::pkg_svc record will be updated.
130 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
131 record itself), the object will be updated to point to this package definition.
133 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
134 the scalar will be updated with the custnum value from the cust_pkg record.
136 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
137 records will be inserted.
144 warn "FS::part_pkg::insert called on $self with options ".
145 join(', ', map "$_=>$options{$_}", keys %options)
148 local $SIG{HUP} = 'IGNORE';
149 local $SIG{INT} = 'IGNORE';
150 local $SIG{QUIT} = 'IGNORE';
151 local $SIG{TERM} = 'IGNORE';
152 local $SIG{TSTP} = 'IGNORE';
153 local $SIG{PIPE} = 'IGNORE';
155 my $oldAutoCommit = $FS::UID::AutoCommit;
156 local $FS::UID::AutoCommit = 0;
159 warn " saving legacy plandata" if $DEBUG;
160 my $plandata = $self->get('plandata');
161 $self->set('plandata', '');
163 warn " inserting part_pkg record" if $DEBUG;
164 my $error = $self->SUPER::insert;
166 $dbh->rollback if $oldAutoCommit;
172 warn " inserting part_pkg_option records for plandata" if $DEBUG;
173 foreach my $part_pkg_option (
174 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
175 return "illegal plandata: $plandata";
177 new FS::part_pkg_option {
178 'pkgpart' => $self->pkgpart,
183 split("\n", $plandata)
185 my $error = $part_pkg_option->insert;
187 $dbh->rollback if $oldAutoCommit;
192 } elsif ( $options{'options'} ) {
194 warn " inserting part_pkg_option records for options hashref" if $DEBUG;
195 foreach my $optionname ( keys %{$options{'options'}} ) {
197 my $part_pkg_option =
198 new FS::part_pkg_option {
199 'pkgpart' => $self->pkgpart,
200 'optionname' => $optionname,
201 'optionvalue' => $options{'options'}->{$optionname},
204 my $error = $part_pkg_option->insert;
206 $dbh->rollback if $oldAutoCommit;
214 my $conf = new FS::Conf;
215 if ( $conf->exists('agent_defaultpkg') ) {
216 warn " agent_defaultpkg set; allowing all agents to purchase package"
218 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
219 my $type_pkgs = new FS::type_pkgs({
220 'typenum' => $agent_type->typenum,
221 'pkgpart' => $self->pkgpart,
223 my $error = $type_pkgs->insert;
225 $dbh->rollback if $oldAutoCommit;
231 warn " inserting pkg_svc records" if $DEBUG;
232 my $pkg_svc = $options{'pkg_svc'} || {};
233 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
234 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
236 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
240 my $pkg_svc = new FS::pkg_svc( {
241 'pkgpart' => $self->pkgpart,
242 'svcpart' => $part_svc->svcpart,
243 'quantity' => $quantity,
244 'primary_svc' => $primary_svc,
246 my $error = $pkg_svc->insert;
248 $dbh->rollback if $oldAutoCommit;
253 if ( $options{'cust_pkg'} ) {
254 warn " updating cust_pkg record " if $DEBUG;
256 ref($options{'cust_pkg'})
257 ? $options{'cust_pkg'}
258 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
259 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
260 if $options{'custnum_ref'};
261 my %hash = $old_cust_pkg->hash;
262 $hash{'pkgpart'} = $self->pkgpart,
263 my $new_cust_pkg = new FS::cust_pkg \%hash;
264 local($FS::cust_pkg::disable_agentcheck) = 1;
265 my $error = $new_cust_pkg->replace($old_cust_pkg);
267 $dbh->rollback if $oldAutoCommit;
268 return "Error modifying cust_pkg record: $error";
272 warn " commiting transaction" if $DEBUG;
273 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
280 Currently unimplemented.
285 return "Can't (yet?) delete package definitions.";
286 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
289 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
291 Replaces OLD_RECORD with this one in the database. If there is an error,
292 returns the error, otherwise returns false.
294 Currently available options are: I<pkg_svc> and I<primary_svc>
296 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
297 values, the appropriate FS::pkg_svc records will be replace.
299 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
300 FS::pkg_svc record will be updated.
305 my( $new, $old ) = ( shift, shift );
307 warn "FS::part_pkg::replace called on $new to replace $old ".
308 "with options %options"
311 local $SIG{HUP} = 'IGNORE';
312 local $SIG{INT} = 'IGNORE';
313 local $SIG{QUIT} = 'IGNORE';
314 local $SIG{TERM} = 'IGNORE';
315 local $SIG{TSTP} = 'IGNORE';
316 local $SIG{PIPE} = 'IGNORE';
318 my $oldAutoCommit = $FS::UID::AutoCommit;
319 local $FS::UID::AutoCommit = 0;
322 warn " saving legacy plandata" if $DEBUG;
323 my $plandata = $new->get('plandata');
324 $new->set('plandata', '');
326 warn " deleting old part_pkg_option records" if $DEBUG;
327 foreach my $part_pkg_option ( $old->part_pkg_option ) {
328 my $error = $part_pkg_option->delete;
330 $dbh->rollback if $oldAutoCommit;
335 warn " replacing part_pkg record" if $DEBUG;
336 my $error = $new->SUPER::replace($old);
338 $dbh->rollback if $oldAutoCommit;
342 warn " inserting part_pkg_option records for plandata" if $DEBUG;
343 foreach my $part_pkg_option (
344 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
345 return "illegal plandata: $plandata";
347 new FS::part_pkg_option {
348 'pkgpart' => $new->pkgpart,
353 split("\n", $plandata)
355 my $error = $part_pkg_option->insert;
357 $dbh->rollback if $oldAutoCommit;
362 warn " replacing pkg_svc records" if $DEBUG;
363 my $pkg_svc = $options{'pkg_svc'} || {};
364 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
365 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
366 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
368 my $old_pkg_svc = qsearchs('pkg_svc', {
369 'pkgpart' => $old->pkgpart,
370 'svcpart' => $part_svc->svcpart,
372 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
373 my $old_primary_svc =
374 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
375 ? $old_pkg_svc->primary_svc
377 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
379 my $new_pkg_svc = new FS::pkg_svc( {
380 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
381 'pkgpart' => $new->pkgpart,
382 'svcpart' => $part_svc->svcpart,
383 'quantity' => $quantity,
384 'primary_svc' => $primary_svc,
386 my $error = $old_pkg_svc
387 ? $new_pkg_svc->replace($old_pkg_svc)
388 : $new_pkg_svc->insert;
390 $dbh->rollback if $oldAutoCommit;
395 warn " commiting transaction" if $DEBUG;
396 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
402 Checks all fields to make sure this is a valid package definition. If
403 there is an error, returns the error, otherwise returns false. Called by the
404 insert and replace methods.
410 warn "FS::part_pkg::check called on $self" if $DEBUG;
412 for (qw(setup recur plandata)) {
413 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
414 return "Use of $_ field is deprecated; set a plan and options"
415 if length($self->get($_));
419 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
420 my $error = $self->ut_number('freq');
421 return $error if $error;
423 $self->freq =~ /^(\d+[hdw]?)$/
424 or return "Illegal or empty freq: ". $self->freq;
428 my $error = $self->ut_numbern('pkgpart')
429 || $self->ut_text('pkg')
430 || $self->ut_text('comment')
431 || $self->ut_textn('promo_code')
432 || $self->ut_alphan('plan')
433 || $self->ut_enum('setuptax', [ '', 'Y' ] )
434 || $self->ut_enum('recurtax', [ '', 'Y' ] )
435 || $self->ut_textn('taxclass')
436 || $self->ut_enum('disabled', [ '', 'Y' ] )
437 || $self->SUPER::check
439 return $error if $error;
441 return 'Unknown plan '. $self->plan
442 unless exists($plans{$self->plan});
449 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
450 definition (with non-zero quantity).
456 #sort { $b->primary cmp $a->primary }
457 grep { $_->quantity }
458 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
461 =item svcpart [ SVCDB ]
463 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
464 associated with this package definition (see L<FS::pkg_svc>). Returns
465 false if there not a primary service definition or exactly one service
466 definition with quantity 1, or if SVCDB is specified and does not match the
467 svcdb of the service definition,
473 my $svcdb = scalar(@_) ? shift : '';
475 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
477 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
478 if dbdef->table('pkg_svc')->column('primary_svc');
479 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
481 return '' if scalar(@pkg_svc) != 1;
482 $pkg_svc[0]->svcpart;
487 Returns a list of the acceptable payment types for this package. Eventually
488 this should come out of a database table and be editable, but currently has the
489 following logic instead:
491 If the package is free, the single item B<BILL> is
492 returned, otherwise, the single item B<CARD> is returned.
494 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
500 if ( $self->is_free ) {
509 Returns true if this package is free.
515 unless ( $self->plan ) {
516 $self->setup =~ /^\s*0+(\.0*)?\s*$/
517 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
518 } elsif ( $self->can('is_free_options') ) {
519 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
520 map { $self->option($_) }
521 $self->is_free_options;
523 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
524 "provides neither is_free_options nor is_free method; returning false";
531 Returns an english representation of the I<freq> field, such as "monthly",
532 "weekly", "semi-annually", etc.
536 tie %freq, 'Tie::IxHash',
537 '0' => '(no recurring fee)',
541 '2w' => 'biweekly (every 2 weeks)',
543 '2' => 'bimonthly (every 2 months)',
544 '3' => 'quarterly (every 3 months)',
545 '6' => 'semiannually (every 6 months)',
547 '24' => 'biannually (every 2 years)',
548 '36' => 'triannually (every 3 years)',
549 '48' => '(every 4 years)',
550 '60' => '(every 5 years)',
551 '120' => '(every 10 years)',
556 my $freq = $self->freq;
557 if ( exists($freq{$freq}) ) {
560 my $interval = 'month';
561 if ( $freq =~ /^(\d+)([hdw])$/ ) {
562 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
563 $interval = $interval{$2};
568 "every $freq ${interval}s";
575 For backwards compatibility, returns the plandata field as well as all options
576 from FS::part_pkg_option.
582 carp "plandata is deprecated";
584 $self->SUPER::plandata(@_);
586 my $plandata = $self->get('plandata');
587 my %options = $self->options;
588 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
593 =item part_pkg_option
595 Returns all options as FS::part_pkg_option objects (see
596 L<FS::part_pkg_option>).
600 sub part_pkg_option {
602 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
607 Returns a list of option names and values suitable for assigning to a hash.
613 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
616 =item option OPTIONNAME
618 Returns the option value for the given name, or the empty string.
623 my( $self, $opt, $ornull ) = @_;
624 my $part_pkg_option =
625 qsearchs('part_pkg_option', {
626 pkgpart => $self->pkgpart,
629 return $part_pkg_option->optionvalue if $part_pkg_option;
630 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
631 split("\n", $self->get('plandata') );
632 return $plandata{$opt} if exists $plandata{$opt};
633 cluck "Package definition option $opt not found in options or plandata!\n"
640 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
641 PLAN is the object's I<plan> field. There should be better docs
642 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
648 my $plan = $self->plan;
650 confess "no price plan found for pkgpart ". $self->pkgpart. "\n"
654 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
655 my $class = ref($self). "::$plan";
656 warn "reblessing $self into $class" if $DEBUG;
659 bless($self, $class) unless $@;
663 #fallbacks that eval the setup and recur fields, for backwards compat
667 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
668 $self->_calc_eval('setup', @_);
673 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
674 $self->_calc_eval('recur', @_);
677 use vars qw( $sdate @details );
679 #my( $self, $field, $cust_pkg ) = @_;
680 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
682 *details = $detailsref;
683 $self->$field() =~ /^(.*)$/
684 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
685 $self->$field(). "\n";
687 return 0 if $prog =~ /^\s*$/;
688 my $value = eval $prog;
693 #fallback that return 0 for old legacy packages with no plan
695 sub calc_remain { 0; }
696 sub calc_cancel { 0; }
709 foreach my $INC ( @INC ) {
710 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
711 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
712 warn "attempting to load plan info from $file\n" if $DEBUG;
713 $file =~ /\/(\w+)\.pm$/ or do {
714 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
718 my $info = eval "use FS::part_pkg::$mod; ".
719 "\\%FS::part_pkg::$mod\::info;";
721 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
724 unless ( keys %$info ) {
725 warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
726 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
729 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
730 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
731 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
738 tie %plans, 'Tie::IxHash',
739 map { $_ => $info{$_} }
740 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
749 =head1 NEW PLAN CLASSES
751 A module should be added in FS/FS/part_pkg/ (an example may be found in
756 The delete method is unimplemented.
758 setup and recur semantics are not yet defined (and are implemented in
759 FS::cust_bill. hmm.).
765 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
766 schema.html from the base documentation.