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 );
22 FS::part_pkg - Object methods for part_pkg objects
28 $record = new FS::part_pkg \%hash
29 $record = new FS::part_pkg { 'column' => 'value' };
31 $custom_record = $template_record->clone;
33 $error = $record->insert;
35 $error = $new_record->replace($old_record);
37 $error = $record->delete;
39 $error = $record->check;
41 @pkg_svc = $record->pkg_svc;
43 $svcnum = $record->svcpart;
44 $svcnum = $record->svcpart( 'svc_acct' );
48 An FS::part_pkg object represents a package definition. FS::part_pkg
49 inherits from FS::Record. The following fields are currently supported:
53 =item pkgpart - primary key (assigned automatically for new package definitions)
55 =item pkg - Text name of this package definition (customer-viewable)
57 =item comment - Text name of this package definition (non-customer-viewable)
59 =item promo_code - Promotional code
61 =item setup - Setup fee expression (deprecated)
63 =item freq - Frequency of recurring fee
65 =item recur - Recurring fee expression (deprecated)
67 =item setuptax - Setup fee tax exempt flag, empty or `Y'
69 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
71 =item taxclass - Tax class
73 =item plan - Price plan
75 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
77 =item disabled - Disabled flag, empty or `Y'
87 Creates a new package definition. To add the package definition to
88 the database, see L<"insert">.
92 sub table { 'part_pkg'; }
96 An alternate constructor. Creates a new package definition by duplicating
97 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
98 to the comment field. To add the package definition to the database, see
105 my $class = ref($self);
106 my %hash = $self->hash;
107 $hash{'pkgpart'} = '';
108 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
109 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
110 #new FS::part_pkg ( \%hash ); # ?
111 new $class ( \%hash ); # ?
114 =item insert [ , OPTION => VALUE ... ]
116 Adds this package definition to the database. If there is an error,
117 returns the error, otherwise returns false.
119 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
120 I<custnum_ref> and I<options>.
122 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
123 values, appropriate FS::pkg_svc records will be inserted.
125 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
126 FS::pkg_svc record will be updated.
128 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
129 record itself), the object will be updated to point to this package definition.
131 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
132 the scalar will be updated with the custnum value from the cust_pkg record.
134 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
135 records will be inserted.
142 warn "FS::part_pkg::insert called on $self with options ".
143 join(', ', map "$_=>$options{$_}", keys %options)
146 local $SIG{HUP} = 'IGNORE';
147 local $SIG{INT} = 'IGNORE';
148 local $SIG{QUIT} = 'IGNORE';
149 local $SIG{TERM} = 'IGNORE';
150 local $SIG{TSTP} = 'IGNORE';
151 local $SIG{PIPE} = 'IGNORE';
153 my $oldAutoCommit = $FS::UID::AutoCommit;
154 local $FS::UID::AutoCommit = 0;
157 warn " saving legacy plandata" if $DEBUG;
158 my $plandata = $self->get('plandata');
159 $self->set('plandata', '');
161 warn " inserting part_pkg record" if $DEBUG;
162 my $error = $self->SUPER::insert;
164 $dbh->rollback if $oldAutoCommit;
170 warn " inserting part_pkg_option records for plandata" if $DEBUG;
171 foreach my $part_pkg_option (
172 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
173 return "illegal plandata: $plandata";
175 new FS::part_pkg_option {
176 'pkgpart' => $self->pkgpart,
181 split("\n", $plandata)
183 my $error = $part_pkg_option->insert;
185 $dbh->rollback if $oldAutoCommit;
190 } elsif ( $options{'options'} ) {
192 warn " inserting part_pkg_option records for options hashref" if $DEBUG;
193 foreach my $optionname ( keys %{$options{'options'}} ) {
195 my $part_pkg_option =
196 new FS::part_pkg_option {
197 'pkgpart' => $self->pkgpart,
198 'optionname' => $optionname,
199 'optionvalue' => $options{'options'}->{$optionname},
202 my $error = $part_pkg_option->insert;
204 $dbh->rollback if $oldAutoCommit;
212 my $conf = new FS::Conf;
213 if ( $conf->exists('agent_defaultpkg') ) {
214 warn " agent_defaultpkg set; allowing all agents to purchase package"
216 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
217 my $type_pkgs = new FS::type_pkgs({
218 'typenum' => $agent_type->typenum,
219 'pkgpart' => $self->pkgpart,
221 my $error = $type_pkgs->insert;
223 $dbh->rollback if $oldAutoCommit;
229 warn " inserting pkg_svc records" if $DEBUG;
230 my $pkg_svc = $options{'pkg_svc'} || {};
231 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
232 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
234 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
238 my $pkg_svc = new FS::pkg_svc( {
239 'pkgpart' => $self->pkgpart,
240 'svcpart' => $part_svc->svcpart,
241 'quantity' => $quantity,
242 'primary_svc' => $primary_svc,
244 my $error = $pkg_svc->insert;
246 $dbh->rollback if $oldAutoCommit;
251 if ( $options{'cust_pkg'} ) {
252 warn " updating cust_pkg record " if $DEBUG;
254 ref($options{'cust_pkg'})
255 ? $options{'cust_pkg'}
256 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
257 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
258 if $options{'custnum_ref'};
259 my %hash = $old_cust_pkg->hash;
260 $hash{'pkgpart'} = $self->pkgpart,
261 my $new_cust_pkg = new FS::cust_pkg \%hash;
262 local($FS::cust_pkg::disable_agentcheck) = 1;
263 my $error = $new_cust_pkg->replace($old_cust_pkg);
265 $dbh->rollback if $oldAutoCommit;
266 return "Error modifying cust_pkg record: $error";
270 warn " commiting transaction" if $DEBUG;
271 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
278 Currently unimplemented.
283 return "Can't (yet?) delete package definitions.";
284 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
287 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
289 Replaces OLD_RECORD with this one in the database. If there is an error,
290 returns the error, otherwise returns false.
292 Currently available options are: I<pkg_svc> and I<primary_svc>
294 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
295 values, the appropriate FS::pkg_svc records will be replace.
297 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
298 FS::pkg_svc record will be updated.
303 my( $new, $old ) = ( shift, shift );
305 warn "FS::part_pkg::replace called on $new to replace $old ".
306 "with options %options"
309 local $SIG{HUP} = 'IGNORE';
310 local $SIG{INT} = 'IGNORE';
311 local $SIG{QUIT} = 'IGNORE';
312 local $SIG{TERM} = 'IGNORE';
313 local $SIG{TSTP} = 'IGNORE';
314 local $SIG{PIPE} = 'IGNORE';
316 my $oldAutoCommit = $FS::UID::AutoCommit;
317 local $FS::UID::AutoCommit = 0;
320 warn " saving legacy plandata" if $DEBUG;
321 my $plandata = $new->get('plandata');
322 $new->set('plandata', '');
324 warn " deleting old part_pkg_option records" if $DEBUG;
325 foreach my $part_pkg_option ( $old->part_pkg_option ) {
326 my $error = $part_pkg_option->delete;
328 $dbh->rollback if $oldAutoCommit;
333 warn " replacing part_pkg record" if $DEBUG;
334 my $error = $new->SUPER::replace($old);
336 $dbh->rollback if $oldAutoCommit;
340 warn " inserting part_pkg_option records for plandata" if $DEBUG;
341 foreach my $part_pkg_option (
342 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
343 return "illegal plandata: $plandata";
345 new FS::part_pkg_option {
346 'pkgpart' => $new->pkgpart,
351 split("\n", $plandata)
353 my $error = $part_pkg_option->insert;
355 $dbh->rollback if $oldAutoCommit;
360 warn " replacing pkg_svc records" if $DEBUG;
361 my $pkg_svc = $options{'pkg_svc'} || {};
362 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
363 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
364 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
366 my $old_pkg_svc = qsearchs('pkg_svc', {
367 'pkgpart' => $old->pkgpart,
368 'svcpart' => $part_svc->svcpart,
370 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
371 my $old_primary_svc =
372 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
373 ? $old_pkg_svc->primary_svc
375 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
377 my $new_pkg_svc = new FS::pkg_svc( {
378 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
379 'pkgpart' => $new->pkgpart,
380 'svcpart' => $part_svc->svcpart,
381 'quantity' => $quantity,
382 'primary_svc' => $primary_svc,
384 my $error = $old_pkg_svc
385 ? $new_pkg_svc->replace($old_pkg_svc)
386 : $new_pkg_svc->insert;
388 $dbh->rollback if $oldAutoCommit;
393 warn " commiting transaction" if $DEBUG;
394 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
400 Checks all fields to make sure this is a valid package definition. If
401 there is an error, returns the error, otherwise returns false. Called by the
402 insert and replace methods.
408 warn "FS::part_pkg::check called on $self" if $DEBUG;
410 for (qw(setup recur plandata)) {
411 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
412 return "Use of $_ field is deprecated; set a plan and options"
413 if length($self->get($_));
417 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
418 my $error = $self->ut_number('freq');
419 return $error if $error;
421 $self->freq =~ /^(\d+[dw]?)$/
422 or return "Illegal or empty freq: ". $self->freq;
426 my $error = $self->ut_numbern('pkgpart')
427 || $self->ut_text('pkg')
428 || $self->ut_text('comment')
429 || $self->ut_textn('promo_code')
430 || $self->ut_alphan('plan')
431 || $self->ut_enum('setuptax', [ '', 'Y' ] )
432 || $self->ut_enum('recurtax', [ '', 'Y' ] )
433 || $self->ut_textn('taxclass')
434 || $self->ut_enum('disabled', [ '', 'Y' ] )
435 || $self->SUPER::check
437 return $error if $error;
439 return 'Unknown plan '. $self->plan
440 unless exists($plans{$self->plan});
447 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
448 definition (with non-zero quantity).
454 #sort { $b->primary cmp $a->primary }
455 grep { $_->quantity }
456 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
459 =item svcpart [ SVCDB ]
461 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
462 associated with this package definition (see L<FS::pkg_svc>). Returns
463 false if there not a primary service definition or exactly one service
464 definition with quantity 1, or if SVCDB is specified and does not match the
465 svcdb of the service definition,
471 my $svcdb = scalar(@_) ? shift : '';
473 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
475 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
476 if dbdef->table('pkg_svc')->column('primary_svc');
477 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
479 return '' if scalar(@pkg_svc) != 1;
480 $pkg_svc[0]->svcpart;
485 Returns a list of the acceptable payment types for this package. Eventually
486 this should come out of a database table and be editable, but currently has the
487 following logic instead:
489 If the package is free, the single item B<BILL> is
490 returned, otherwise, the single item B<CARD> is returned.
492 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
498 if ( $self->is_free ) {
507 Returns true if this package is free.
513 unless ( $self->plan ) {
514 $self->setup =~ /^\s*0+(\.0*)?\s*$/
515 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
516 } elsif ( $self->can('is_free_options') ) {
517 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
518 map { $self->option($_) }
519 $self->is_free_options;
521 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
522 "provides neither is_free_options nor is_free method; returning false";
529 Returns an english representation of the I<freq> field, such as "monthly",
530 "weekly", "semi-annually", etc.
534 tie %freq, 'Tie::IxHash',
535 '0' => '(no recurring fee)',
538 '2w' => 'biweekly (every 2 weeks)',
540 '2' => 'bimonthly (every 2 months)',
541 '3' => 'quarterly (every 3 months)',
542 '6' => 'semiannually (every 6 months)',
544 '24' => 'biannually (every 2 years)',
545 '36' => 'triannually (every 3 years)',
546 '48' => '(every 4 years)',
547 '60' => '(every 5 years)',
548 '120' => '(every 10 years)',
553 my $freq = $self->freq;
554 if ( exists($freq{$freq}) ) {
557 my $interval = 'month';
558 if ( $freq =~ /^(\d+)([dw])$/ ) {
559 my %interval = ( 'd'=>'day', 'w'=>'week' );
560 $interval = $interval{$2};
565 "every $freq ${interval}s";
572 For backwards compatibility, returns the plandata field as well as all options
573 from FS::part_pkg_option.
579 carp "plandata is deprecated";
581 $self->SUPER::plandata(@_);
583 my $plandata = $self->get('plandata');
584 my %options = $self->options;
585 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
590 =item part_pkg_option
592 Returns all options as FS::part_pkg_option objects (see
593 L<FS::part_pkg_option>).
597 sub part_pkg_option {
599 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
604 Returns a list of option names and values suitable for assigning to a hash.
610 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
613 =item option OPTIONNAME
615 Returns the option value for the given name, or the empty string.
620 my( $self, $opt, $ornull ) = @_;
621 my $part_pkg_option =
622 qsearchs('part_pkg_option', {
623 pkgpart => $self->pkgpart,
626 return $part_pkg_option->optionvalue if $part_pkg_option;
627 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
628 split("\n", $self->get('plandata') );
629 return $plandata{$opt} if exists $plandata{$opt};
630 cluck "Package definition option $opt not found in options or plandata!\n"
637 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
638 PLAN is the object's I<plan> field. There should be better docs
639 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
645 my $plan = $self->plan;
647 confess "no price plan found for pkgpart ". $self->pkgpart. "\n"
651 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
652 my $class = ref($self). "::$plan";
653 warn "reblessing $self into $class" if $DEBUG;
656 bless($self, $class) unless $@;
660 #fallbacks that eval the setup and recur fields, for backwards compat
664 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
665 $self->_calc_eval('setup', @_);
670 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
671 $self->_calc_eval('recur', @_);
674 use vars qw( $sdate @details );
676 #my( $self, $field, $cust_pkg ) = @_;
677 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
679 *details = $detailsref;
680 $self->$field() =~ /^(.*)$/
681 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
682 $self->$field(). "\n";
684 return 0 if $prog =~ /^\s*$/;
685 my $value = eval $prog;
690 #fallback that return 0 for old legacy packages with no plan
692 sub calc_remain { 0; }
693 sub calc_cancel { 0; }
706 foreach my $INC ( @INC ) {
707 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
708 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
709 warn "attempting to load plan info from $file\n" if $DEBUG;
710 $file =~ /\/(\w+)\.pm$/ or do {
711 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
715 my $info = eval "use FS::part_pkg::$mod; ".
716 "\\%FS::part_pkg::$mod\::info;";
718 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
721 unless ( keys %$info ) {
722 warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
723 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
726 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
727 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
728 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
735 tie %plans, 'Tie::IxHash',
736 map { $_ => $info{$_} }
737 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
746 =head1 NEW PLAN CLASSES
748 A module should be added in FS/FS/part_pkg/ (an example may be found in
753 The delete method is unimplemented.
755 setup and recur semantics are not yet defined (and are implemented in
756 FS::cust_bill. hmm.).
760 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
761 schema.html from the base documentation.