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 use FS::part_pkg_taxoverride;
18 use FS::part_pkg_taxproduct;
20 @ISA = qw( FS::m2m_Common FS::Record ); # FS::option_Common ); # this can use option_Common
21 # when all the plandata bs is
28 FS::part_pkg - Object methods for part_pkg objects
34 $record = new FS::part_pkg \%hash
35 $record = new FS::part_pkg { 'column' => 'value' };
37 $custom_record = $template_record->clone;
39 $error = $record->insert;
41 $error = $new_record->replace($old_record);
43 $error = $record->delete;
45 $error = $record->check;
47 @pkg_svc = $record->pkg_svc;
49 $svcnum = $record->svcpart;
50 $svcnum = $record->svcpart( 'svc_acct' );
54 An FS::part_pkg object represents a package definition. FS::part_pkg
55 inherits from FS::Record. The following fields are currently supported:
59 =item pkgpart - primary key (assigned automatically for new package definitions)
61 =item pkg - Text name of this package definition (customer-viewable)
63 =item comment - Text name of this package definition (non-customer-viewable)
65 =item classnum - Optional package class (see L<FS::pkg_class>)
67 =item promo_code - Promotional code
69 =item setup - Setup fee expression (deprecated)
71 =item freq - Frequency of recurring fee
73 =item recur - Recurring fee expression (deprecated)
75 =item setuptax - Setup fee tax exempt flag, empty or `Y'
77 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
79 =item taxclass - Tax class
81 =item plan - Price plan
83 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
85 =item disabled - Disabled flag, empty or `Y'
87 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
89 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
91 =item agentnum - Optional agentnum (see L<FS::agent>)
101 Creates a new package definition. To add the package definition to
102 the database, see L<"insert">.
106 sub table { 'part_pkg'; }
110 An alternate constructor. Creates a new package definition by duplicating
111 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
112 to the comment field. To add the package definition to the database, see
119 my $class = ref($self);
120 my %hash = $self->hash;
121 $hash{'pkgpart'} = '';
122 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
123 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
124 #new FS::part_pkg ( \%hash ); # ?
125 new $class ( \%hash ); # ?
128 =item insert [ , OPTION => VALUE ... ]
130 Adds this package definition to the database. If there is an error,
131 returns the error, otherwise returns false.
133 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
134 I<custnum_ref> and I<options>.
136 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
137 values, appropriate FS::pkg_svc records will be inserted.
139 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
140 FS::pkg_svc record will be updated.
142 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
143 record itself), the object will be updated to point to this package definition.
145 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
146 the scalar will be updated with the custnum value from the cust_pkg record.
148 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
149 records will be inserted.
156 warn "FS::part_pkg::insert called on $self with options ".
157 join(', ', map "$_=>$options{$_}", keys %options)
160 local $SIG{HUP} = 'IGNORE';
161 local $SIG{INT} = 'IGNORE';
162 local $SIG{QUIT} = 'IGNORE';
163 local $SIG{TERM} = 'IGNORE';
164 local $SIG{TSTP} = 'IGNORE';
165 local $SIG{PIPE} = 'IGNORE';
167 my $oldAutoCommit = $FS::UID::AutoCommit;
168 local $FS::UID::AutoCommit = 0;
171 warn " saving legacy plandata" if $DEBUG;
172 my $plandata = $self->get('plandata');
173 $self->set('plandata', '');
175 warn " inserting part_pkg record" if $DEBUG;
176 my $error = $self->SUPER::insert;
178 $dbh->rollback if $oldAutoCommit;
184 warn " inserting part_pkg_option records for plandata" if $DEBUG;
185 foreach my $part_pkg_option (
186 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
187 return "illegal plandata: $plandata";
189 new FS::part_pkg_option {
190 'pkgpart' => $self->pkgpart,
195 split("\n", $plandata)
197 my $error = $part_pkg_option->insert;
199 $dbh->rollback if $oldAutoCommit;
204 } elsif ( $options{'options'} ) {
206 warn " inserting part_pkg_option records for options hashref" if $DEBUG;
207 foreach my $optionname ( keys %{$options{'options'}} ) {
209 my $part_pkg_option =
210 new FS::part_pkg_option {
211 'pkgpart' => $self->pkgpart,
212 'optionname' => $optionname,
213 'optionvalue' => $options{'options'}->{$optionname},
216 my $error = $part_pkg_option->insert;
218 $dbh->rollback if $oldAutoCommit;
226 my $conf = new FS::Conf;
227 if ( $conf->exists('agent_defaultpkg') ) {
228 warn " agent_defaultpkg set; allowing all agents to purchase package"
230 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
231 my $type_pkgs = new FS::type_pkgs({
232 'typenum' => $agent_type->typenum,
233 'pkgpart' => $self->pkgpart,
235 my $error = $type_pkgs->insert;
237 $dbh->rollback if $oldAutoCommit;
243 warn " inserting pkg_svc records" if $DEBUG;
244 my $pkg_svc = $options{'pkg_svc'} || {};
245 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
246 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
248 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
252 my $pkg_svc = new FS::pkg_svc( {
253 'pkgpart' => $self->pkgpart,
254 'svcpart' => $part_svc->svcpart,
255 'quantity' => $quantity,
256 'primary_svc' => $primary_svc,
258 my $error = $pkg_svc->insert;
260 $dbh->rollback if $oldAutoCommit;
265 if ( $options{'cust_pkg'} ) {
266 warn " updating cust_pkg record " if $DEBUG;
268 ref($options{'cust_pkg'})
269 ? $options{'cust_pkg'}
270 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
271 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
272 if $options{'custnum_ref'};
273 my %hash = $old_cust_pkg->hash;
274 $hash{'pkgpart'} = $self->pkgpart,
275 my $new_cust_pkg = new FS::cust_pkg \%hash;
276 local($FS::cust_pkg::disable_agentcheck) = 1;
277 my $error = $new_cust_pkg->replace($old_cust_pkg);
279 $dbh->rollback if $oldAutoCommit;
280 return "Error modifying cust_pkg record: $error";
284 warn " commiting transaction" if $DEBUG;
285 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
292 Currently unimplemented.
297 return "Can't (yet?) delete package definitions.";
298 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
301 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
303 Replaces OLD_RECORD with this one in the database. If there is an error,
304 returns the error, otherwise returns false.
306 Currently available options are: I<pkg_svc> and I<primary_svc>
308 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
309 values, the appropriate FS::pkg_svc records will be replace.
311 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
312 FS::pkg_svc record will be updated.
317 my( $new, $old ) = ( shift, shift );
320 # We absolutely have to have an old vs. new record to make this work.
321 if (!defined($old)) {
322 $old = qsearchs( 'part_pkg', { 'pkgpart' => $new->pkgpart } );
325 warn "FS::part_pkg::replace called on $new to replace $old ".
326 "with options %options"
329 local $SIG{HUP} = 'IGNORE';
330 local $SIG{INT} = 'IGNORE';
331 local $SIG{QUIT} = 'IGNORE';
332 local $SIG{TERM} = 'IGNORE';
333 local $SIG{TSTP} = 'IGNORE';
334 local $SIG{PIPE} = 'IGNORE';
336 my $oldAutoCommit = $FS::UID::AutoCommit;
337 local $FS::UID::AutoCommit = 0;
340 warn " saving legacy plandata" if $DEBUG;
341 my $plandata = $new->get('plandata');
342 $new->set('plandata', '');
344 warn " deleting old part_pkg_option records" if $DEBUG;
345 foreach my $part_pkg_option ( $old->part_pkg_option ) {
346 my $error = $part_pkg_option->delete;
348 $dbh->rollback if $oldAutoCommit;
353 warn " replacing part_pkg record" if $DEBUG;
354 my $error = $new->SUPER::replace($old);
356 $dbh->rollback if $oldAutoCommit;
360 warn " inserting part_pkg_option records for plandata" if $DEBUG;
361 foreach my $part_pkg_option (
362 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
363 return "illegal plandata: $plandata";
365 new FS::part_pkg_option {
366 'pkgpart' => $new->pkgpart,
371 split("\n", $plandata)
373 my $error = $part_pkg_option->insert;
375 $dbh->rollback if $oldAutoCommit;
380 warn " replacing pkg_svc records" if $DEBUG;
381 my $pkg_svc = $options{'pkg_svc'} || {};
382 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
383 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
384 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
386 my $old_pkg_svc = qsearchs('pkg_svc', {
387 'pkgpart' => $old->pkgpart,
388 'svcpart' => $part_svc->svcpart,
390 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
391 my $old_primary_svc =
392 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
393 ? $old_pkg_svc->primary_svc
395 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
397 my $new_pkg_svc = new FS::pkg_svc( {
398 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
399 'pkgpart' => $new->pkgpart,
400 'svcpart' => $part_svc->svcpart,
401 'quantity' => $quantity,
402 'primary_svc' => $primary_svc,
404 my $error = $old_pkg_svc
405 ? $new_pkg_svc->replace($old_pkg_svc)
406 : $new_pkg_svc->insert;
408 $dbh->rollback if $oldAutoCommit;
413 warn " commiting transaction" if $DEBUG;
414 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
420 Checks all fields to make sure this is a valid package definition. If
421 there is an error, returns the error, otherwise returns false. Called by the
422 insert and replace methods.
428 warn "FS::part_pkg::check called on $self" if $DEBUG;
430 for (qw(setup recur plandata)) {
431 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
432 return "Use of $_ field is deprecated; set a plan and options"
433 if length($self->get($_));
437 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
438 my $error = $self->ut_number('freq');
439 return $error if $error;
441 $self->freq =~ /^(\d+[hdw]?)$/
442 or return "Illegal or empty freq: ". $self->freq;
446 my $error = $self->ut_numbern('pkgpart')
447 || $self->ut_text('pkg')
448 || $self->ut_text('comment')
449 || $self->ut_textn('promo_code')
450 || $self->ut_alphan('plan')
451 || $self->ut_enum('setuptax', [ '', 'Y' ] )
452 || $self->ut_enum('recurtax', [ '', 'Y' ] )
453 || $self->ut_textn('taxclass')
454 || $self->ut_enum('disabled', [ '', 'Y' ] )
455 || $self->ut_floatn('pay_weight')
456 || $self->ut_floatn('credit_weight')
457 || $self->ut_agentnum_acl('agentnum', 'Edit global package definitions')
458 || $self->SUPER::check
460 return $error if $error;
462 if ( $self->classnum !~ /^$/ ) {
463 my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum');
464 return $error if $error;
469 return 'Unknown plan '. $self->plan
470 unless exists($plans{$self->plan});
472 my $conf = new FS::Conf;
473 return 'Taxclass is required'
474 if ! $self->taxclass && $conf->exists('require_taxclasses');
481 Returns the package class, as an FS::pkg_class object, or the empty string
482 if there is no package class.
488 if ( $self->classnum ) {
489 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
497 Returns the package class name, or the empty string if there is no package
504 my $pkg_class = $self->pkg_class;
506 ? $pkg_class->classname
512 Returns the associated agent for this event, if any, as an FS::agent object.
518 qsearchs('agent', { 'agentnum' => $self->agentnum } );
523 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
524 definition (with non-zero quantity).
530 #sort { $b->primary cmp $a->primary }
531 grep { $_->quantity }
532 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
535 =item svcpart [ SVCDB ]
537 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
538 associated with this package definition (see L<FS::pkg_svc>). Returns
539 false if there not a primary service definition or exactly one service
540 definition with quantity 1, or if SVCDB is specified and does not match the
541 svcdb of the service definition,
547 my $svcdb = scalar(@_) ? shift : '';
549 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
551 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
552 if dbdef->table('pkg_svc')->column('primary_svc');
553 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
555 return '' if scalar(@pkg_svc) != 1;
556 $pkg_svc[0]->svcpart;
561 Returns a list of the acceptable payment types for this package. Eventually
562 this should come out of a database table and be editable, but currently has the
563 following logic instead:
565 If the package is free, the single item B<BILL> is
566 returned, otherwise, the single item B<CARD> is returned.
568 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
574 if ( $self->is_free ) {
583 Returns true if this package is free.
589 unless ( $self->plan ) {
590 $self->setup =~ /^\s*0+(\.0*)?\s*$/
591 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
592 } elsif ( $self->can('is_free_options') ) {
593 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
594 map { $self->option($_) }
595 $self->is_free_options;
597 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
598 "provides neither is_free_options nor is_free method; returning false";
605 #method, class method or sub? #my $self = shift;
607 tie my %freq, 'Tie::IxHash',
608 '0' => '(no recurring fee)',
611 '2d' => 'every two days',
612 '3d' => 'every three days',
614 '2w' => 'biweekly (every 2 weeks)',
616 '45d' => 'every 45 days',
617 '2' => 'bimonthly (every 2 months)',
618 '3' => 'quarterly (every 3 months)',
619 '4' => 'every 4 months',
620 '137d' => 'every 4 1/2 months (137 days)',
621 '6' => 'semiannually (every 6 months)',
623 '13' => 'every 13 months (annually +1 month)',
624 '24' => 'biannually (every 2 years)',
625 '36' => 'triannually (every 3 years)',
626 '48' => '(every 4 years)',
627 '60' => '(every 5 years)',
628 '120' => '(every 10 years)',
637 Returns an english representation of the I<freq> field, such as "monthly",
638 "weekly", "semi-annually", etc.
644 my $freq = $self->freq;
646 #my $freqs_href = $self->freqs_href;
647 my $freqs_href = freqs_href();
649 if ( exists($freqs_href->{$freq}) ) {
650 $freqs_href->{$freq};
652 my $interval = 'month';
653 if ( $freq =~ /^(\d+)([hdw])$/ ) {
654 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
655 $interval = $interval{$2};
660 "every $freq ${interval}s";
667 For backwards compatibility, returns the plandata field as well as all options
668 from FS::part_pkg_option.
674 carp "plandata is deprecated";
676 $self->SUPER::plandata(@_);
678 my $plandata = $self->get('plandata');
679 my %options = $self->options;
680 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
685 =item part_pkg_option
687 Returns all options as FS::part_pkg_option objects (see
688 L<FS::part_pkg_option>).
692 sub part_pkg_option {
694 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
699 Returns a list of option names and values suitable for assigning to a hash.
705 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
708 =item option OPTIONNAME
710 Returns the option value for the given name, or the empty string.
715 my( $self, $opt, $ornull ) = @_;
716 my $part_pkg_option =
717 qsearchs('part_pkg_option', {
718 pkgpart => $self->pkgpart,
721 return $part_pkg_option->optionvalue if $part_pkg_option;
722 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
723 split("\n", $self->get('plandata') );
724 return $plandata{$opt} if exists $plandata{$opt};
725 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
726 "not found in options or plandata!\n"
731 =item part_pkg_taxoverride
733 Returns all options as FS::part_pkg_taxoverride objects (see
734 L<FS::part_pkg_taxoverride>).
738 sub part_pkg_taxoverride {
740 qsearch('part_pkg_taxoverride', { 'pkgpart' => $self->pkgpart } );
743 =item taxproduct_description
745 Returns the description of the associated tax product for this package
746 definition (see L<FS::part_pkg_taxproduct>).
750 sub taxproduct_description {
752 my $part_pkg_taxproduct =
753 qsearchs( 'part_pkg_taxproduct',
754 { 'taxproductnum' => $self->taxproductnum }
756 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
761 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
762 PLAN is the object's I<plan> field. There should be better docs
763 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
769 my $plan = $self->plan;
771 confess "no price plan found for pkgpart ". $self->pkgpart. "\n"
775 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
776 my $class = ref($self). "::$plan";
777 warn "reblessing $self into $class" if $DEBUG;
780 bless($self, $class) unless $@;
784 #fallbacks that eval the setup and recur fields, for backwards compat
788 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
789 $self->_calc_eval('setup', @_);
794 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
795 $self->_calc_eval('recur', @_);
798 use vars qw( $sdate @details );
800 #my( $self, $field, $cust_pkg ) = @_;
801 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
803 *details = $detailsref;
804 $self->$field() =~ /^(.*)$/
805 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
806 $self->$field(). "\n";
808 return 0 if $prog =~ /^\s*$/;
809 my $value = eval $prog;
814 #fallback that return 0 for old legacy packages with no plan
816 sub calc_remain { 0; }
817 sub calc_cancel { 0; }
830 foreach my $INC ( @INC ) {
831 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
832 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
833 warn "attempting to load plan info from $file\n" if $DEBUG;
834 $file =~ /\/(\w+)\.pm$/ or do {
835 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
839 my $info = eval "use FS::part_pkg::$mod; ".
840 "\\%FS::part_pkg::$mod\::info;";
842 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
845 unless ( keys %$info ) {
846 warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
847 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
850 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
851 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
852 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
859 tie %plans, 'Tie::IxHash',
860 map { $_ => $info{$_} }
861 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
868 =item format OPTION DATA
870 Returns data formatted according to the function 'format' described
871 in the plan info. Returns DATA if no such function exists.
876 my ($self, $option, $data) = (shift, shift, shift);
877 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
878 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
884 =item parse OPTION DATA
886 Returns data parsed according to the function 'parse' described
887 in the plan info. Returns DATA if no such function exists.
892 my ($self, $option, $data) = (shift, shift, shift);
893 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
894 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
903 =head1 NEW PLAN CLASSES
905 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
906 found in eg/plan_template.pm. Until then, it is suggested that you use the
907 other modules in FS/FS/part_pkg/ as a guide.
911 The delete method is unimplemented.
913 setup and recur semantics are not yet defined (and are implemented in
914 FS::cust_bill. hmm.). now they're deprecated and need to go.
920 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
921 schema.html from the base documentation.