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::m2m_Common 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'
84 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
86 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
96 Creates a new package definition. To add the package definition to
97 the database, see L<"insert">.
101 sub table { 'part_pkg'; }
105 An alternate constructor. Creates a new package definition by duplicating
106 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
107 to the comment field. To add the package definition to the database, see
114 my $class = ref($self);
115 my %hash = $self->hash;
116 $hash{'pkgpart'} = '';
117 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
118 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
119 #new FS::part_pkg ( \%hash ); # ?
120 new $class ( \%hash ); # ?
123 =item insert [ , OPTION => VALUE ... ]
125 Adds this package definition to the database. If there is an error,
126 returns the error, otherwise returns false.
128 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
129 I<custnum_ref> and I<options>.
131 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
132 values, appropriate FS::pkg_svc records will be inserted.
134 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
135 FS::pkg_svc record will be updated.
137 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
138 record itself), the object will be updated to point to this package definition.
140 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
141 the scalar will be updated with the custnum value from the cust_pkg record.
143 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
144 records will be inserted.
151 warn "FS::part_pkg::insert called on $self with options ".
152 join(', ', map "$_=>$options{$_}", keys %options)
155 local $SIG{HUP} = 'IGNORE';
156 local $SIG{INT} = 'IGNORE';
157 local $SIG{QUIT} = 'IGNORE';
158 local $SIG{TERM} = 'IGNORE';
159 local $SIG{TSTP} = 'IGNORE';
160 local $SIG{PIPE} = 'IGNORE';
162 my $oldAutoCommit = $FS::UID::AutoCommit;
163 local $FS::UID::AutoCommit = 0;
166 warn " saving legacy plandata" if $DEBUG;
167 my $plandata = $self->get('plandata');
168 $self->set('plandata', '');
170 warn " inserting part_pkg record" if $DEBUG;
171 my $error = $self->SUPER::insert;
173 $dbh->rollback if $oldAutoCommit;
179 warn " inserting part_pkg_option records for plandata" if $DEBUG;
180 foreach my $part_pkg_option (
181 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
182 return "illegal plandata: $plandata";
184 new FS::part_pkg_option {
185 'pkgpart' => $self->pkgpart,
190 split("\n", $plandata)
192 my $error = $part_pkg_option->insert;
194 $dbh->rollback if $oldAutoCommit;
199 } elsif ( $options{'options'} ) {
201 warn " inserting part_pkg_option records for options hashref" if $DEBUG;
202 foreach my $optionname ( keys %{$options{'options'}} ) {
204 my $part_pkg_option =
205 new FS::part_pkg_option {
206 'pkgpart' => $self->pkgpart,
207 'optionname' => $optionname,
208 'optionvalue' => $options{'options'}->{$optionname},
211 my $error = $part_pkg_option->insert;
213 $dbh->rollback if $oldAutoCommit;
221 my $conf = new FS::Conf;
222 if ( $conf->exists('agent_defaultpkg') ) {
223 warn " agent_defaultpkg set; allowing all agents to purchase package"
225 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
226 my $type_pkgs = new FS::type_pkgs({
227 'typenum' => $agent_type->typenum,
228 'pkgpart' => $self->pkgpart,
230 my $error = $type_pkgs->insert;
232 $dbh->rollback if $oldAutoCommit;
238 warn " inserting pkg_svc records" if $DEBUG;
239 my $pkg_svc = $options{'pkg_svc'} || {};
240 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
241 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
243 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
247 my $pkg_svc = new FS::pkg_svc( {
248 'pkgpart' => $self->pkgpart,
249 'svcpart' => $part_svc->svcpart,
250 'quantity' => $quantity,
251 'primary_svc' => $primary_svc,
253 my $error = $pkg_svc->insert;
255 $dbh->rollback if $oldAutoCommit;
260 if ( $options{'cust_pkg'} ) {
261 warn " updating cust_pkg record " if $DEBUG;
263 ref($options{'cust_pkg'})
264 ? $options{'cust_pkg'}
265 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
266 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
267 if $options{'custnum_ref'};
268 my %hash = $old_cust_pkg->hash;
269 $hash{'pkgpart'} = $self->pkgpart,
270 my $new_cust_pkg = new FS::cust_pkg \%hash;
271 local($FS::cust_pkg::disable_agentcheck) = 1;
272 my $error = $new_cust_pkg->replace($old_cust_pkg);
274 $dbh->rollback if $oldAutoCommit;
275 return "Error modifying cust_pkg record: $error";
279 warn " commiting transaction" if $DEBUG;
280 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
287 Currently unimplemented.
292 return "Can't (yet?) delete package definitions.";
293 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
296 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
298 Replaces OLD_RECORD with this one in the database. If there is an error,
299 returns the error, otherwise returns false.
301 Currently available options are: I<pkg_svc> and I<primary_svc>
303 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
304 values, the appropriate FS::pkg_svc records will be replace.
306 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
307 FS::pkg_svc record will be updated.
312 my( $new, $old ) = ( shift, shift );
315 # We absolutely have to have an old vs. new record to make this work.
316 if (!defined($old)) {
317 $old = qsearchs( 'part_pkg', { 'pkgpart' => $new->pkgpart } );
320 warn "FS::part_pkg::replace called on $new to replace $old ".
321 "with options %options"
324 local $SIG{HUP} = 'IGNORE';
325 local $SIG{INT} = 'IGNORE';
326 local $SIG{QUIT} = 'IGNORE';
327 local $SIG{TERM} = 'IGNORE';
328 local $SIG{TSTP} = 'IGNORE';
329 local $SIG{PIPE} = 'IGNORE';
331 my $oldAutoCommit = $FS::UID::AutoCommit;
332 local $FS::UID::AutoCommit = 0;
335 warn " saving legacy plandata" if $DEBUG;
336 my $plandata = $new->get('plandata');
337 $new->set('plandata', '');
339 warn " deleting old part_pkg_option records" if $DEBUG;
340 foreach my $part_pkg_option ( $old->part_pkg_option ) {
341 my $error = $part_pkg_option->delete;
343 $dbh->rollback if $oldAutoCommit;
348 warn " replacing part_pkg record" if $DEBUG;
349 my $error = $new->SUPER::replace($old);
351 $dbh->rollback if $oldAutoCommit;
355 warn " inserting part_pkg_option records for plandata" if $DEBUG;
356 foreach my $part_pkg_option (
357 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
358 return "illegal plandata: $plandata";
360 new FS::part_pkg_option {
361 'pkgpart' => $new->pkgpart,
366 split("\n", $plandata)
368 my $error = $part_pkg_option->insert;
370 $dbh->rollback if $oldAutoCommit;
375 warn " replacing pkg_svc records" if $DEBUG;
376 my $pkg_svc = $options{'pkg_svc'} || {};
377 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
378 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
379 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
381 my $old_pkg_svc = qsearchs('pkg_svc', {
382 'pkgpart' => $old->pkgpart,
383 'svcpart' => $part_svc->svcpart,
385 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
386 my $old_primary_svc =
387 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
388 ? $old_pkg_svc->primary_svc
390 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
392 my $new_pkg_svc = new FS::pkg_svc( {
393 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
394 'pkgpart' => $new->pkgpart,
395 'svcpart' => $part_svc->svcpart,
396 'quantity' => $quantity,
397 'primary_svc' => $primary_svc,
399 my $error = $old_pkg_svc
400 ? $new_pkg_svc->replace($old_pkg_svc)
401 : $new_pkg_svc->insert;
403 $dbh->rollback if $oldAutoCommit;
408 warn " commiting transaction" if $DEBUG;
409 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
415 Checks all fields to make sure this is a valid package definition. If
416 there is an error, returns the error, otherwise returns false. Called by the
417 insert and replace methods.
423 warn "FS::part_pkg::check called on $self" if $DEBUG;
425 for (qw(setup recur plandata)) {
426 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
427 return "Use of $_ field is deprecated; set a plan and options"
428 if length($self->get($_));
432 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
433 my $error = $self->ut_number('freq');
434 return $error if $error;
436 $self->freq =~ /^(\d+[hdw]?)$/
437 or return "Illegal or empty freq: ". $self->freq;
441 my $error = $self->ut_numbern('pkgpart')
442 || $self->ut_text('pkg')
443 || $self->ut_text('comment')
444 || $self->ut_textn('promo_code')
445 || $self->ut_alphan('plan')
446 || $self->ut_enum('setuptax', [ '', 'Y' ] )
447 || $self->ut_enum('recurtax', [ '', 'Y' ] )
448 || $self->ut_textn('taxclass')
449 || $self->ut_enum('disabled', [ '', 'Y' ] )
450 || $self->ut_floatn('pay_weight')
451 || $self->ut_floatn('credit_weight')
452 || $self->SUPER::check
454 return $error if $error;
456 if ( $self->classnum !~ /^$/ ) {
457 my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum');
458 return $error if $error;
463 return 'Unknown plan '. $self->plan
464 unless exists($plans{$self->plan});
466 my $conf = new FS::Conf;
467 return 'Taxclass is required'
468 if ! $self->taxclass && $conf->exists('require_taxclasses');
475 Returns the package class, as an FS::pkg_class object, or the empty string
476 if there is no package class.
482 if ( $self->classnum ) {
483 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
491 Returns the package class name, or the empty string if there is no package
498 my $pkg_class = $self->pkg_class;
500 ? $pkg_class->classname
506 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
507 definition (with non-zero quantity).
513 #sort { $b->primary cmp $a->primary }
514 grep { $_->quantity }
515 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
518 =item svcpart [ SVCDB ]
520 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
521 associated with this package definition (see L<FS::pkg_svc>). Returns
522 false if there not a primary service definition or exactly one service
523 definition with quantity 1, or if SVCDB is specified and does not match the
524 svcdb of the service definition,
530 my $svcdb = scalar(@_) ? shift : '';
532 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
534 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
535 if dbdef->table('pkg_svc')->column('primary_svc');
536 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
538 return '' if scalar(@pkg_svc) != 1;
539 $pkg_svc[0]->svcpart;
544 Returns a list of the acceptable payment types for this package. Eventually
545 this should come out of a database table and be editable, but currently has the
546 following logic instead:
548 If the package is free, the single item B<BILL> is
549 returned, otherwise, the single item B<CARD> is returned.
551 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
557 if ( $self->is_free ) {
566 Returns true if this package is free.
572 unless ( $self->plan ) {
573 $self->setup =~ /^\s*0+(\.0*)?\s*$/
574 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
575 } elsif ( $self->can('is_free_options') ) {
576 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
577 map { $self->option($_) }
578 $self->is_free_options;
580 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
581 "provides neither is_free_options nor is_free method; returning false";
588 #method, class method or sub? #my $self = shift;
590 tie my %freq, 'Tie::IxHash',
591 '0' => '(no recurring fee)',
594 '2d' => 'every two days',
596 '2w' => 'biweekly (every 2 weeks)',
598 '45d' => 'every 45 days',
599 '2' => 'bimonthly (every 2 months)',
600 '3' => 'quarterly (every 3 months)',
601 '6' => 'semiannually (every 6 months)',
603 '13' => 'every 13 months (annually +1 month)',
604 '24' => 'biannually (every 2 years)',
605 '36' => 'triannually (every 3 years)',
606 '48' => '(every 4 years)',
607 '60' => '(every 5 years)',
608 '120' => '(every 10 years)',
617 Returns an english representation of the I<freq> field, such as "monthly",
618 "weekly", "semi-annually", etc.
624 my $freq = $self->freq;
626 #my $freqs_href = $self->freqs_href;
627 my $freqs_href = freqs_href();
629 if ( exists($freqs_href->{$freq}) ) {
630 $freqs_href->{$freq};
632 my $interval = 'month';
633 if ( $freq =~ /^(\d+)([hdw])$/ ) {
634 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
635 $interval = $interval{$2};
640 "every $freq ${interval}s";
647 For backwards compatibility, returns the plandata field as well as all options
648 from FS::part_pkg_option.
654 carp "plandata is deprecated";
656 $self->SUPER::plandata(@_);
658 my $plandata = $self->get('plandata');
659 my %options = $self->options;
660 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
665 =item part_pkg_option
667 Returns all options as FS::part_pkg_option objects (see
668 L<FS::part_pkg_option>).
672 sub part_pkg_option {
674 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
679 Returns a list of option names and values suitable for assigning to a hash.
685 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
688 =item option OPTIONNAME
690 Returns the option value for the given name, or the empty string.
695 my( $self, $opt, $ornull ) = @_;
696 my $part_pkg_option =
697 qsearchs('part_pkg_option', {
698 pkgpart => $self->pkgpart,
701 return $part_pkg_option->optionvalue if $part_pkg_option;
702 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
703 split("\n", $self->get('plandata') );
704 return $plandata{$opt} if exists $plandata{$opt};
705 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
706 "not found in options or plandata!\n"
713 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
714 PLAN is the object's I<plan> field. There should be better docs
715 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
721 my $plan = $self->plan;
723 confess "no price plan found for pkgpart ". $self->pkgpart. "\n"
727 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
728 my $class = ref($self). "::$plan";
729 warn "reblessing $self into $class" if $DEBUG;
732 bless($self, $class) unless $@;
736 #fallbacks that eval the setup and recur fields, for backwards compat
740 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
741 $self->_calc_eval('setup', @_);
746 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
747 $self->_calc_eval('recur', @_);
750 use vars qw( $sdate @details );
752 #my( $self, $field, $cust_pkg ) = @_;
753 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
755 *details = $detailsref;
756 $self->$field() =~ /^(.*)$/
757 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
758 $self->$field(). "\n";
760 return 0 if $prog =~ /^\s*$/;
761 my $value = eval $prog;
766 #fallback that return 0 for old legacy packages with no plan
768 sub calc_remain { 0; }
769 sub calc_cancel { 0; }
782 foreach my $INC ( @INC ) {
783 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
784 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
785 warn "attempting to load plan info from $file\n" if $DEBUG;
786 $file =~ /\/(\w+)\.pm$/ or do {
787 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
791 my $info = eval "use FS::part_pkg::$mod; ".
792 "\\%FS::part_pkg::$mod\::info;";
794 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
797 unless ( keys %$info ) {
798 warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
799 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
802 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
803 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
804 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
811 tie %plans, 'Tie::IxHash',
812 map { $_ => $info{$_} }
813 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
820 =item format OPTION DATA
822 Returns data formatted according to the function 'format' described
823 in the plan info. Returns DATA if no such function exists.
828 my ($self, $option, $data) = (shift, shift, shift);
829 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
830 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
836 =item parse OPTION DATA
838 Returns data parsed according to the function 'parse' described
839 in the plan info. Returns DATA if no such function exists.
844 my ($self, $option, $data) = (shift, shift, shift);
845 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
846 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
855 =head1 NEW PLAN CLASSES
857 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
858 found in eg/plan_template.pm. Until then, it is suggested that you use the
859 other modules in FS/FS/part_pkg/ as a guide.
863 The delete method is unimplemented.
865 setup and recur semantics are not yet defined (and are implemented in
866 FS::cust_bill. hmm.). now they're deprecated and need to go.
872 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
873 schema.html from the base documentation.