4 use vars qw( @ISA %plans $DEBUG );
5 use Carp qw(carp cluck confess);
8 use FS::Record qw( qsearch qsearchs dbh dbdef );
14 use FS::part_pkg_option;
17 @ISA = qw( FS::Record ); # FS::option_Common ); # this can use option_Common
18 # when all the plandata bs is
25 FS::part_pkg - Object methods for part_pkg objects
31 $record = new FS::part_pkg \%hash
32 $record = new FS::part_pkg { 'column' => 'value' };
34 $custom_record = $template_record->clone;
36 $error = $record->insert;
38 $error = $new_record->replace($old_record);
40 $error = $record->delete;
42 $error = $record->check;
44 @pkg_svc = $record->pkg_svc;
46 $svcnum = $record->svcpart;
47 $svcnum = $record->svcpart( 'svc_acct' );
51 An FS::part_pkg object represents a package definition. FS::part_pkg
52 inherits from FS::Record. The following fields are currently supported:
56 =item pkgpart - primary key (assigned automatically for new package definitions)
58 =item pkg - Text name of this package definition (customer-viewable)
60 =item comment - Text name of this package definition (non-customer-viewable)
62 =item classnum - Optional package class (see L<FS::pkg_class>)
64 =item promo_code - Promotional code
66 =item setup - Setup fee expression (deprecated)
68 =item freq - Frequency of recurring fee
70 =item recur - Recurring fee expression (deprecated)
72 =item setuptax - Setup fee tax exempt flag, empty or `Y'
74 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
76 =item taxclass - Tax class
78 =item plan - Price plan
80 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
82 =item disabled - Disabled flag, empty or `Y'
92 Creates a new package definition. To add the package definition to
93 the database, see L<"insert">.
97 sub table { 'part_pkg'; }
101 An alternate constructor. Creates a new package definition by duplicating
102 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
103 to the comment field. To add the package definition to the database, see
110 my $class = ref($self);
111 my %hash = $self->hash;
112 $hash{'pkgpart'} = '';
113 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
114 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
115 #new FS::part_pkg ( \%hash ); # ?
116 new $class ( \%hash ); # ?
119 =item insert [ , OPTION => VALUE ... ]
121 Adds this package definition to the database. If there is an error,
122 returns the error, otherwise returns false.
124 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
125 I<custnum_ref> and I<options>.
127 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
128 values, appropriate FS::pkg_svc records will be inserted.
130 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
131 FS::pkg_svc record will be updated.
133 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
134 record itself), the object will be updated to point to this package definition.
136 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
137 the scalar will be updated with the custnum value from the cust_pkg record.
139 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
140 records will be inserted.
147 warn "FS::part_pkg::insert called on $self with options ".
148 join(', ', map "$_=>$options{$_}", keys %options)
151 local $SIG{HUP} = 'IGNORE';
152 local $SIG{INT} = 'IGNORE';
153 local $SIG{QUIT} = 'IGNORE';
154 local $SIG{TERM} = 'IGNORE';
155 local $SIG{TSTP} = 'IGNORE';
156 local $SIG{PIPE} = 'IGNORE';
158 my $oldAutoCommit = $FS::UID::AutoCommit;
159 local $FS::UID::AutoCommit = 0;
162 warn " saving legacy plandata" if $DEBUG;
163 my $plandata = $self->get('plandata');
164 $self->set('plandata', '');
166 warn " inserting part_pkg record" if $DEBUG;
167 my $error = $self->SUPER::insert;
169 $dbh->rollback if $oldAutoCommit;
175 warn " inserting part_pkg_option records for plandata" if $DEBUG;
176 foreach my $part_pkg_option (
177 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
178 return "illegal plandata: $plandata";
180 new FS::part_pkg_option {
181 'pkgpart' => $self->pkgpart,
186 split("\n", $plandata)
188 my $error = $part_pkg_option->insert;
190 $dbh->rollback if $oldAutoCommit;
195 } elsif ( $options{'options'} ) {
197 warn " inserting part_pkg_option records for options hashref" if $DEBUG;
198 foreach my $optionname ( keys %{$options{'options'}} ) {
200 my $part_pkg_option =
201 new FS::part_pkg_option {
202 'pkgpart' => $self->pkgpart,
203 'optionname' => $optionname,
204 'optionvalue' => $options{'options'}->{$optionname},
207 my $error = $part_pkg_option->insert;
209 $dbh->rollback if $oldAutoCommit;
217 my $conf = new FS::Conf;
218 if ( $conf->exists('agent_defaultpkg') ) {
219 warn " agent_defaultpkg set; allowing all agents to purchase package"
221 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
222 my $type_pkgs = new FS::type_pkgs({
223 'typenum' => $agent_type->typenum,
224 'pkgpart' => $self->pkgpart,
226 my $error = $type_pkgs->insert;
228 $dbh->rollback if $oldAutoCommit;
234 warn " inserting pkg_svc records" if $DEBUG;
235 my $pkg_svc = $options{'pkg_svc'} || {};
236 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
237 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
239 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
243 my $pkg_svc = new FS::pkg_svc( {
244 'pkgpart' => $self->pkgpart,
245 'svcpart' => $part_svc->svcpart,
246 'quantity' => $quantity,
247 'primary_svc' => $primary_svc,
249 my $error = $pkg_svc->insert;
251 $dbh->rollback if $oldAutoCommit;
256 if ( $options{'cust_pkg'} ) {
257 warn " updating cust_pkg record " if $DEBUG;
259 ref($options{'cust_pkg'})
260 ? $options{'cust_pkg'}
261 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
262 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
263 if $options{'custnum_ref'};
264 my %hash = $old_cust_pkg->hash;
265 $hash{'pkgpart'} = $self->pkgpart,
266 my $new_cust_pkg = new FS::cust_pkg \%hash;
267 local($FS::cust_pkg::disable_agentcheck) = 1;
268 my $error = $new_cust_pkg->replace($old_cust_pkg);
270 $dbh->rollback if $oldAutoCommit;
271 return "Error modifying cust_pkg record: $error";
275 warn " commiting transaction" if $DEBUG;
276 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
283 Currently unimplemented.
288 return "Can't (yet?) delete package definitions.";
289 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
292 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
294 Replaces OLD_RECORD with this one in the database. If there is an error,
295 returns the error, otherwise returns false.
297 Currently available options are: I<pkg_svc> and I<primary_svc>
299 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
300 values, the appropriate FS::pkg_svc records will be replace.
302 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
303 FS::pkg_svc record will be updated.
308 my( $new, $old ) = ( shift, shift );
310 warn "FS::part_pkg::replace called on $new to replace $old ".
311 "with options %options"
314 local $SIG{HUP} = 'IGNORE';
315 local $SIG{INT} = 'IGNORE';
316 local $SIG{QUIT} = 'IGNORE';
317 local $SIG{TERM} = 'IGNORE';
318 local $SIG{TSTP} = 'IGNORE';
319 local $SIG{PIPE} = 'IGNORE';
321 my $oldAutoCommit = $FS::UID::AutoCommit;
322 local $FS::UID::AutoCommit = 0;
325 warn " saving legacy plandata" if $DEBUG;
326 my $plandata = $new->get('plandata');
327 $new->set('plandata', '');
329 warn " deleting old part_pkg_option records" if $DEBUG;
330 foreach my $part_pkg_option ( $old->part_pkg_option ) {
331 my $error = $part_pkg_option->delete;
333 $dbh->rollback if $oldAutoCommit;
338 warn " replacing part_pkg record" if $DEBUG;
339 my $error = $new->SUPER::replace($old);
341 $dbh->rollback if $oldAutoCommit;
345 warn " inserting part_pkg_option records for plandata" if $DEBUG;
346 foreach my $part_pkg_option (
347 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
348 return "illegal plandata: $plandata";
350 new FS::part_pkg_option {
351 'pkgpart' => $new->pkgpart,
356 split("\n", $plandata)
358 my $error = $part_pkg_option->insert;
360 $dbh->rollback if $oldAutoCommit;
365 warn " replacing pkg_svc records" if $DEBUG;
366 my $pkg_svc = $options{'pkg_svc'} || {};
367 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
368 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
369 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
371 my $old_pkg_svc = qsearchs('pkg_svc', {
372 'pkgpart' => $old->pkgpart,
373 'svcpart' => $part_svc->svcpart,
375 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
376 my $old_primary_svc =
377 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
378 ? $old_pkg_svc->primary_svc
380 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
382 my $new_pkg_svc = new FS::pkg_svc( {
383 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
384 'pkgpart' => $new->pkgpart,
385 'svcpart' => $part_svc->svcpart,
386 'quantity' => $quantity,
387 'primary_svc' => $primary_svc,
389 my $error = $old_pkg_svc
390 ? $new_pkg_svc->replace($old_pkg_svc)
391 : $new_pkg_svc->insert;
393 $dbh->rollback if $oldAutoCommit;
398 warn " commiting transaction" if $DEBUG;
399 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
405 Checks all fields to make sure this is a valid package definition. If
406 there is an error, returns the error, otherwise returns false. Called by the
407 insert and replace methods.
413 warn "FS::part_pkg::check called on $self" if $DEBUG;
415 for (qw(setup recur plandata)) {
416 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
417 return "Use of $_ field is deprecated; set a plan and options"
418 if length($self->get($_));
422 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
423 my $error = $self->ut_number('freq');
424 return $error if $error;
426 $self->freq =~ /^(\d+[hdw]?)$/
427 or return "Illegal or empty freq: ". $self->freq;
431 my $error = $self->ut_numbern('pkgpart')
432 || $self->ut_text('pkg')
433 || $self->ut_text('comment')
434 || $self->ut_textn('promo_code')
435 || $self->ut_alphan('plan')
436 || $self->ut_enum('setuptax', [ '', 'Y' ] )
437 || $self->ut_enum('recurtax', [ '', 'Y' ] )
438 || $self->ut_textn('taxclass')
439 || $self->ut_enum('disabled', [ '', 'Y' ] )
440 || $self->SUPER::check
442 return $error if $error;
444 if ( $self->classnum !~ /^$/ ) {
445 my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum');
446 return $error if $error;
451 return 'Unknown plan '. $self->plan
452 unless exists($plans{$self->plan});
454 my $conf = new FS::Conf;
455 return 'Taxclass is required'
456 if ! $self->taxclass && $conf->exists('require_taxclasses');
463 Returns the package class, as an FS::pkg_class object, or the empty string
464 if there is no package class.
470 if ( $self->classnum ) {
471 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
479 Returns the package class name, or the empty string if there is no package
486 my $pkg_class = $self->pkg_class;
488 ? $pkg_class->classname
494 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
495 definition (with non-zero quantity).
501 #sort { $b->primary cmp $a->primary }
502 grep { $_->quantity }
503 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
506 =item svcpart [ SVCDB ]
508 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
509 associated with this package definition (see L<FS::pkg_svc>). Returns
510 false if there not a primary service definition or exactly one service
511 definition with quantity 1, or if SVCDB is specified and does not match the
512 svcdb of the service definition,
518 my $svcdb = scalar(@_) ? shift : '';
520 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
522 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
523 if dbdef->table('pkg_svc')->column('primary_svc');
524 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
526 return '' if scalar(@pkg_svc) != 1;
527 $pkg_svc[0]->svcpart;
532 Returns a list of the acceptable payment types for this package. Eventually
533 this should come out of a database table and be editable, but currently has the
534 following logic instead:
536 If the package is free, the single item B<BILL> is
537 returned, otherwise, the single item B<CARD> is returned.
539 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
545 if ( $self->is_free ) {
554 Returns true if this package is free.
560 unless ( $self->plan ) {
561 $self->setup =~ /^\s*0+(\.0*)?\s*$/
562 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
563 } elsif ( $self->can('is_free_options') ) {
564 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
565 map { $self->option($_) }
566 $self->is_free_options;
568 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
569 "provides neither is_free_options nor is_free method; returning false";
576 #method, class method or sub? #my $self = shift;
578 tie my %freq, 'Tie::IxHash',
579 '0' => '(no recurring fee)',
583 '2w' => 'biweekly (every 2 weeks)',
585 '2' => 'bimonthly (every 2 months)',
586 '3' => 'quarterly (every 3 months)',
587 '6' => 'semiannually (every 6 months)',
589 '24' => 'biannually (every 2 years)',
590 '36' => 'triannually (every 3 years)',
591 '48' => '(every 4 years)',
592 '60' => '(every 5 years)',
593 '120' => '(every 10 years)',
602 Returns an english representation of the I<freq> field, such as "monthly",
603 "weekly", "semi-annually", etc.
609 my $freq = $self->freq;
611 #my $freqs_href = $self->freqs_href;
612 my $freqs_href = freqs_href();
614 if ( exists($freqs_href->{$freq}) ) {
615 $freqs_href->{$freq};
617 my $interval = 'month';
618 if ( $freq =~ /^(\d+)([hdw])$/ ) {
619 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
620 $interval = $interval{$2};
625 "every $freq ${interval}s";
632 For backwards compatibility, returns the plandata field as well as all options
633 from FS::part_pkg_option.
639 carp "plandata is deprecated";
641 $self->SUPER::plandata(@_);
643 my $plandata = $self->get('plandata');
644 my %options = $self->options;
645 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
650 =item part_pkg_option
652 Returns all options as FS::part_pkg_option objects (see
653 L<FS::part_pkg_option>).
657 sub part_pkg_option {
659 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
664 Returns a list of option names and values suitable for assigning to a hash.
670 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
673 =item option OPTIONNAME
675 Returns the option value for the given name, or the empty string.
680 my( $self, $opt, $ornull ) = @_;
681 my $part_pkg_option =
682 qsearchs('part_pkg_option', {
683 pkgpart => $self->pkgpart,
686 return $part_pkg_option->optionvalue if $part_pkg_option;
687 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
688 split("\n", $self->get('plandata') );
689 return $plandata{$opt} if exists $plandata{$opt};
690 cluck "Package definition option $opt not found in options or plandata!\n"
697 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
698 PLAN is the object's I<plan> field. There should be better docs
699 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
705 my $plan = $self->plan;
707 confess "no price plan found for pkgpart ". $self->pkgpart. "\n"
711 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
712 my $class = ref($self). "::$plan";
713 warn "reblessing $self into $class" if $DEBUG;
716 bless($self, $class) unless $@;
720 #fallbacks that eval the setup and recur fields, for backwards compat
724 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
725 $self->_calc_eval('setup', @_);
730 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
731 $self->_calc_eval('recur', @_);
734 use vars qw( $sdate @details );
736 #my( $self, $field, $cust_pkg ) = @_;
737 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
739 *details = $detailsref;
740 $self->$field() =~ /^(.*)$/
741 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
742 $self->$field(). "\n";
744 return 0 if $prog =~ /^\s*$/;
745 my $value = eval $prog;
750 #fallback that return 0 for old legacy packages with no plan
752 sub calc_remain { 0; }
753 sub calc_cancel { 0; }
766 foreach my $INC ( @INC ) {
767 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
768 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
769 warn "attempting to load plan info from $file\n" if $DEBUG;
770 $file =~ /\/(\w+)\.pm$/ or do {
771 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
775 my $info = eval "use FS::part_pkg::$mod; ".
776 "\\%FS::part_pkg::$mod\::info;";
778 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
781 unless ( keys %$info ) {
782 warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
783 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
786 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
787 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
788 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
795 tie %plans, 'Tie::IxHash',
796 map { $_ => $info{$_} }
797 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
806 =head1 NEW PLAN CLASSES
808 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
809 found in eg/plan_template.pm. Until then, it is suggested that you use the
810 other modules in FS/FS/part_pkg/ as a guide.
814 The delete method is unimplemented.
816 setup and recur semantics are not yet defined (and are implemented in
817 FS::cust_bill. hmm.). now they're deprecated and need to go.
823 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
824 schema.html from the base documentation.