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;
18 @ISA = qw( FS::m2m_Common FS::Record ); # FS::option_Common ); # this can use option_Common
19 # when all the plandata bs is
26 FS::part_pkg - Object methods for part_pkg objects
32 $record = new FS::part_pkg \%hash
33 $record = new FS::part_pkg { 'column' => 'value' };
35 $custom_record = $template_record->clone;
37 $error = $record->insert;
39 $error = $new_record->replace($old_record);
41 $error = $record->delete;
43 $error = $record->check;
45 @pkg_svc = $record->pkg_svc;
47 $svcnum = $record->svcpart;
48 $svcnum = $record->svcpart( 'svc_acct' );
52 An FS::part_pkg object represents a package definition. FS::part_pkg
53 inherits from FS::Record. The following fields are currently supported:
57 =item pkgpart - primary key (assigned automatically for new package definitions)
59 =item pkg - Text name of this package definition (customer-viewable)
61 =item comment - Text name of this package definition (non-customer-viewable)
63 =item classnum - Optional package class (see L<FS::pkg_class>)
65 =item promo_code - Promotional code
67 =item setup - Setup fee expression (deprecated)
69 =item freq - Frequency of recurring fee
71 =item recur - Recurring fee expression (deprecated)
73 =item setuptax - Setup fee tax exempt flag, empty or `Y'
75 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
77 =item taxclass - Tax class
79 =item plan - Price plan
81 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
83 =item disabled - Disabled flag, empty or `Y'
85 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
87 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
89 =item agentnum - Optional agentnum (see L<FS::agent>)
99 Creates a new package definition. To add the package definition to
100 the database, see L<"insert">.
104 sub table { 'part_pkg'; }
108 An alternate constructor. Creates a new package definition by duplicating
109 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
110 to the comment field. To add the package definition to the database, see
117 my $class = ref($self);
118 my %hash = $self->hash;
119 $hash{'pkgpart'} = '';
120 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
121 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
122 #new FS::part_pkg ( \%hash ); # ?
123 new $class ( \%hash ); # ?
126 =item insert [ , OPTION => VALUE ... ]
128 Adds this package definition to the database. If there is an error,
129 returns the error, otherwise returns false.
131 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
132 I<custnum_ref> and I<options>.
134 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
135 values, appropriate FS::pkg_svc records will be inserted.
137 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
138 FS::pkg_svc record will be updated.
140 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
141 record itself), the object will be updated to point to this package definition.
143 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
144 the scalar will be updated with the custnum value from the cust_pkg record.
146 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
147 records will be inserted.
154 warn "FS::part_pkg::insert called on $self with options ".
155 join(', ', map "$_=>$options{$_}", keys %options)
158 local $SIG{HUP} = 'IGNORE';
159 local $SIG{INT} = 'IGNORE';
160 local $SIG{QUIT} = 'IGNORE';
161 local $SIG{TERM} = 'IGNORE';
162 local $SIG{TSTP} = 'IGNORE';
163 local $SIG{PIPE} = 'IGNORE';
165 my $oldAutoCommit = $FS::UID::AutoCommit;
166 local $FS::UID::AutoCommit = 0;
169 warn " saving legacy plandata" if $DEBUG;
170 my $plandata = $self->get('plandata');
171 $self->set('plandata', '');
173 warn " inserting part_pkg record" if $DEBUG;
174 my $error = $self->SUPER::insert;
176 $dbh->rollback if $oldAutoCommit;
182 warn " inserting part_pkg_option records for plandata" if $DEBUG;
183 foreach my $part_pkg_option (
184 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
185 return "illegal plandata: $plandata";
187 new FS::part_pkg_option {
188 'pkgpart' => $self->pkgpart,
193 split("\n", $plandata)
195 my $error = $part_pkg_option->insert;
197 $dbh->rollback if $oldAutoCommit;
202 } elsif ( $options{'options'} ) {
204 warn " inserting part_pkg_option records for options hashref" if $DEBUG;
205 foreach my $optionname ( keys %{$options{'options'}} ) {
207 my $part_pkg_option =
208 new FS::part_pkg_option {
209 'pkgpart' => $self->pkgpart,
210 'optionname' => $optionname,
211 'optionvalue' => $options{'options'}->{$optionname},
214 my $error = $part_pkg_option->insert;
216 $dbh->rollback if $oldAutoCommit;
224 my $conf = new FS::Conf;
225 if ( $conf->exists('agent_defaultpkg') ) {
226 warn " agent_defaultpkg set; allowing all agents to purchase package"
228 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
229 my $type_pkgs = new FS::type_pkgs({
230 'typenum' => $agent_type->typenum,
231 'pkgpart' => $self->pkgpart,
233 my $error = $type_pkgs->insert;
235 $dbh->rollback if $oldAutoCommit;
241 warn " inserting pkg_svc records" if $DEBUG;
242 my $pkg_svc = $options{'pkg_svc'} || {};
243 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
244 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
246 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
250 my $pkg_svc = new FS::pkg_svc( {
251 'pkgpart' => $self->pkgpart,
252 'svcpart' => $part_svc->svcpart,
253 'quantity' => $quantity,
254 'primary_svc' => $primary_svc,
256 my $error = $pkg_svc->insert;
258 $dbh->rollback if $oldAutoCommit;
263 if ( $options{'cust_pkg'} ) {
264 warn " updating cust_pkg record " if $DEBUG;
266 ref($options{'cust_pkg'})
267 ? $options{'cust_pkg'}
268 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
269 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
270 if $options{'custnum_ref'};
271 my %hash = $old_cust_pkg->hash;
272 $hash{'pkgpart'} = $self->pkgpart,
273 my $new_cust_pkg = new FS::cust_pkg \%hash;
274 local($FS::cust_pkg::disable_agentcheck) = 1;
275 my $error = $new_cust_pkg->replace($old_cust_pkg);
277 $dbh->rollback if $oldAutoCommit;
278 return "Error modifying cust_pkg record: $error";
282 warn " commiting transaction" if $DEBUG;
283 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
290 Currently unimplemented.
295 return "Can't (yet?) delete package definitions.";
296 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
299 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
301 Replaces OLD_RECORD with this one in the database. If there is an error,
302 returns the error, otherwise returns false.
304 Currently available options are: I<pkg_svc> and I<primary_svc>
306 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
307 values, the appropriate FS::pkg_svc records will be replace.
309 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
310 FS::pkg_svc record will be updated.
315 my( $new, $old ) = ( shift, shift );
318 # We absolutely have to have an old vs. new record to make this work.
319 if (!defined($old)) {
320 $old = qsearchs( 'part_pkg', { 'pkgpart' => $new->pkgpart } );
323 warn "FS::part_pkg::replace called on $new to replace $old ".
324 "with options %options"
327 local $SIG{HUP} = 'IGNORE';
328 local $SIG{INT} = 'IGNORE';
329 local $SIG{QUIT} = 'IGNORE';
330 local $SIG{TERM} = 'IGNORE';
331 local $SIG{TSTP} = 'IGNORE';
332 local $SIG{PIPE} = 'IGNORE';
334 my $oldAutoCommit = $FS::UID::AutoCommit;
335 local $FS::UID::AutoCommit = 0;
338 warn " saving legacy plandata" if $DEBUG;
339 my $plandata = $new->get('plandata');
340 $new->set('plandata', '');
342 warn " deleting old part_pkg_option records" if $DEBUG;
343 foreach my $part_pkg_option ( $old->part_pkg_option ) {
344 my $error = $part_pkg_option->delete;
346 $dbh->rollback if $oldAutoCommit;
351 warn " replacing part_pkg record" if $DEBUG;
352 my $error = $new->SUPER::replace($old);
354 $dbh->rollback if $oldAutoCommit;
358 warn " inserting part_pkg_option records for plandata" if $DEBUG;
359 foreach my $part_pkg_option (
360 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
361 return "illegal plandata: $plandata";
363 new FS::part_pkg_option {
364 'pkgpart' => $new->pkgpart,
369 split("\n", $plandata)
371 my $error = $part_pkg_option->insert;
373 $dbh->rollback if $oldAutoCommit;
378 warn " replacing pkg_svc records" if $DEBUG;
379 my $pkg_svc = $options{'pkg_svc'} || {};
380 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
381 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
382 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
384 my $old_pkg_svc = qsearchs('pkg_svc', {
385 'pkgpart' => $old->pkgpart,
386 'svcpart' => $part_svc->svcpart,
388 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
389 my $old_primary_svc =
390 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
391 ? $old_pkg_svc->primary_svc
393 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
395 my $new_pkg_svc = new FS::pkg_svc( {
396 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
397 'pkgpart' => $new->pkgpart,
398 'svcpart' => $part_svc->svcpart,
399 'quantity' => $quantity,
400 'primary_svc' => $primary_svc,
402 my $error = $old_pkg_svc
403 ? $new_pkg_svc->replace($old_pkg_svc)
404 : $new_pkg_svc->insert;
406 $dbh->rollback if $oldAutoCommit;
411 warn " commiting transaction" if $DEBUG;
412 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
418 Checks all fields to make sure this is a valid package definition. If
419 there is an error, returns the error, otherwise returns false. Called by the
420 insert and replace methods.
426 warn "FS::part_pkg::check called on $self" if $DEBUG;
428 for (qw(setup recur plandata)) {
429 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
430 return "Use of $_ field is deprecated; set a plan and options"
431 if length($self->get($_));
435 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
436 my $error = $self->ut_number('freq');
437 return $error if $error;
439 $self->freq =~ /^(\d+[hdw]?)$/
440 or return "Illegal or empty freq: ". $self->freq;
444 my $error = $self->ut_numbern('pkgpart')
445 || $self->ut_text('pkg')
446 || $self->ut_text('comment')
447 || $self->ut_textn('promo_code')
448 || $self->ut_alphan('plan')
449 || $self->ut_enum('setuptax', [ '', 'Y' ] )
450 || $self->ut_enum('recurtax', [ '', 'Y' ] )
451 || $self->ut_textn('taxclass')
452 || $self->ut_enum('disabled', [ '', 'Y' ] )
453 || $self->ut_floatn('pay_weight')
454 || $self->ut_floatn('credit_weight')
455 || $self->ut_agentnum_acl('agentnum', 'Edit global package definitions')
456 || $self->SUPER::check
458 return $error if $error;
460 if ( $self->classnum !~ /^$/ ) {
461 my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum');
462 return $error if $error;
467 return 'Unknown plan '. $self->plan
468 unless exists($plans{$self->plan});
470 my $conf = new FS::Conf;
471 return 'Taxclass is required'
472 if ! $self->taxclass && $conf->exists('require_taxclasses');
479 Returns the package class, as an FS::pkg_class object, or the empty string
480 if there is no package class.
486 if ( $self->classnum ) {
487 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
495 Returns the package class name, or the empty string if there is no package
502 my $pkg_class = $self->pkg_class;
504 ? $pkg_class->classname
510 Returns the associated agent for this event, if any, as an FS::agent object.
516 qsearchs('agent', { 'agentnum' => $self->agentnum } );
521 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
522 definition (with non-zero quantity).
528 #sort { $b->primary cmp $a->primary }
529 grep { $_->quantity }
530 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
533 =item svcpart [ SVCDB ]
535 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
536 associated with this package definition (see L<FS::pkg_svc>). Returns
537 false if there not a primary service definition or exactly one service
538 definition with quantity 1, or if SVCDB is specified and does not match the
539 svcdb of the service definition,
545 my $svcdb = scalar(@_) ? shift : '';
547 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
549 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
550 if dbdef->table('pkg_svc')->column('primary_svc');
551 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
553 return '' if scalar(@pkg_svc) != 1;
554 $pkg_svc[0]->svcpart;
559 Returns a list of the acceptable payment types for this package. Eventually
560 this should come out of a database table and be editable, but currently has the
561 following logic instead:
563 If the package is free, the single item B<BILL> is
564 returned, otherwise, the single item B<CARD> is returned.
566 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
572 if ( $self->is_free ) {
581 Returns true if this package is free.
587 unless ( $self->plan ) {
588 $self->setup =~ /^\s*0+(\.0*)?\s*$/
589 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
590 } elsif ( $self->can('is_free_options') ) {
591 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
592 map { $self->option($_) }
593 $self->is_free_options;
595 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
596 "provides neither is_free_options nor is_free method; returning false";
603 #method, class method or sub? #my $self = shift;
605 tie my %freq, 'Tie::IxHash',
606 '0' => '(no recurring fee)',
609 '2d' => 'every two days',
610 '3d' => 'every three days',
612 '2w' => 'biweekly (every 2 weeks)',
614 '45d' => 'every 45 days',
615 '2' => 'bimonthly (every 2 months)',
616 '3' => 'quarterly (every 3 months)',
617 '4' => 'every 4 months',
618 '137d' => 'every 4 1/2 months (137 days)',
619 '6' => 'semiannually (every 6 months)',
621 '13' => 'every 13 months (annually +1 month)',
622 '24' => 'biannually (every 2 years)',
623 '36' => 'triannually (every 3 years)',
624 '48' => '(every 4 years)',
625 '60' => '(every 5 years)',
626 '120' => '(every 10 years)',
635 Returns an english representation of the I<freq> field, such as "monthly",
636 "weekly", "semi-annually", etc.
642 my $freq = $self->freq;
644 #my $freqs_href = $self->freqs_href;
645 my $freqs_href = freqs_href();
647 if ( exists($freqs_href->{$freq}) ) {
648 $freqs_href->{$freq};
650 my $interval = 'month';
651 if ( $freq =~ /^(\d+)([hdw])$/ ) {
652 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
653 $interval = $interval{$2};
658 "every $freq ${interval}s";
665 For backwards compatibility, returns the plandata field as well as all options
666 from FS::part_pkg_option.
672 carp "plandata is deprecated";
674 $self->SUPER::plandata(@_);
676 my $plandata = $self->get('plandata');
677 my %options = $self->options;
678 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
683 =item part_pkg_option
685 Returns all options as FS::part_pkg_option objects (see
686 L<FS::part_pkg_option>).
690 sub part_pkg_option {
692 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
697 Returns a list of option names and values suitable for assigning to a hash.
703 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
706 =item option OPTIONNAME
708 Returns the option value for the given name, or the empty string.
713 my( $self, $opt, $ornull ) = @_;
714 my $part_pkg_option =
715 qsearchs('part_pkg_option', {
716 pkgpart => $self->pkgpart,
719 return $part_pkg_option->optionvalue if $part_pkg_option;
720 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
721 split("\n", $self->get('plandata') );
722 return $plandata{$opt} if exists $plandata{$opt};
723 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
724 "not found in options or plandata!\n"
731 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
732 PLAN is the object's I<plan> field. There should be better docs
733 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
739 my $plan = $self->plan;
741 confess "no price plan found for pkgpart ". $self->pkgpart. "\n"
745 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
746 my $class = ref($self). "::$plan";
747 warn "reblessing $self into $class" if $DEBUG;
750 bless($self, $class) unless $@;
754 #fallbacks that eval the setup and recur fields, for backwards compat
758 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
759 $self->_calc_eval('setup', @_);
764 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
765 $self->_calc_eval('recur', @_);
768 use vars qw( $sdate @details );
770 #my( $self, $field, $cust_pkg ) = @_;
771 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
773 *details = $detailsref;
774 $self->$field() =~ /^(.*)$/
775 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
776 $self->$field(). "\n";
778 return 0 if $prog =~ /^\s*$/;
779 my $value = eval $prog;
784 #fallback that return 0 for old legacy packages with no plan
786 sub calc_remain { 0; }
787 sub calc_cancel { 0; }
800 foreach my $INC ( @INC ) {
801 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
802 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
803 warn "attempting to load plan info from $file\n" if $DEBUG;
804 $file =~ /\/(\w+)\.pm$/ or do {
805 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
809 my $info = eval "use FS::part_pkg::$mod; ".
810 "\\%FS::part_pkg::$mod\::info;";
812 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
815 unless ( keys %$info ) {
816 warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
817 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
820 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
821 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
822 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
829 tie %plans, 'Tie::IxHash',
830 map { $_ => $info{$_} }
831 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
838 =item format OPTION DATA
840 Returns data formatted according to the function 'format' described
841 in the plan info. Returns DATA if no such function exists.
846 my ($self, $option, $data) = (shift, shift, shift);
847 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
848 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
854 =item parse OPTION DATA
856 Returns data parsed according to the function 'parse' described
857 in the plan info. Returns DATA if no such function exists.
862 my ($self, $option, $data) = (shift, shift, shift);
863 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
864 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
873 =head1 NEW PLAN CLASSES
875 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
876 found in eg/plan_template.pm. Until then, it is suggested that you use the
877 other modules in FS/FS/part_pkg/ as a guide.
881 The delete method is unimplemented.
883 setup and recur semantics are not yet defined (and are implemented in
884 FS::cust_bill. hmm.). now they're deprecated and need to go.
890 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
891 schema.html from the base documentation.