4 use vars qw( @ISA %plans $DEBUG );
5 use Carp qw(carp cluck confess);
6 use Scalar::Util qw( blessed );
9 use FS::Record qw( qsearch qsearchs dbh dbdef );
15 use FS::part_pkg_option;
18 use FS::part_pkg_taxoverride;
19 use FS::part_pkg_taxproduct;
20 use FS::part_pkg_link;
22 @ISA = qw( FS::m2m_Common FS::option_Common );
27 FS::part_pkg - Object methods for part_pkg objects
33 $record = new FS::part_pkg \%hash
34 $record = new FS::part_pkg { 'column' => 'value' };
36 $custom_record = $template_record->clone;
38 $error = $record->insert;
40 $error = $new_record->replace($old_record);
42 $error = $record->delete;
44 $error = $record->check;
46 @pkg_svc = $record->pkg_svc;
48 $svcnum = $record->svcpart;
49 $svcnum = $record->svcpart( 'svc_acct' );
53 An FS::part_pkg object represents a package definition. FS::part_pkg
54 inherits from FS::Record. The following fields are currently supported:
58 =item pkgpart - primary key (assigned automatically for new package definitions)
60 =item pkg - Text name of this package definition (customer-viewable)
62 =item comment - Text name of this package definition (non-customer-viewable)
64 =item classnum - Optional package class (see L<FS::pkg_class>)
66 =item promo_code - Promotional code
68 =item setup - Setup fee expression (deprecated)
70 =item freq - Frequency of recurring fee
72 =item recur - Recurring fee expression (deprecated)
74 =item setuptax - Setup fee tax exempt flag, empty or `Y'
76 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
78 =item taxclass - Tax class
80 =item plan - Price plan
82 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
84 =item disabled - Disabled flag, empty or `Y'
86 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
88 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
90 =item agentnum - Optional agentnum (see L<FS::agent>)
100 Creates a new package definition. To add the package definition to
101 the database, see L<"insert">.
105 sub table { 'part_pkg'; }
109 An alternate constructor. Creates a new package definition by duplicating
110 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
111 to the comment field. To add the package definition to the database, see
118 my $class = ref($self);
119 my %hash = $self->hash;
120 $hash{'pkgpart'} = '';
121 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
122 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
123 #new FS::part_pkg ( \%hash ); # ?
124 new $class ( \%hash ); # ?
127 =item insert [ , OPTION => VALUE ... ]
129 Adds this package definition to the database. If there is an error,
130 returns the error, otherwise returns false.
132 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
133 I<custnum_ref> and I<options>.
135 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
136 values, appropriate FS::pkg_svc records will be inserted.
138 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
139 FS::pkg_svc record will be updated.
141 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
142 record itself), the object will be updated to point to this package definition.
144 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
145 the scalar will be updated with the custnum value from the cust_pkg record.
147 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
148 records will be inserted.
155 warn "FS::part_pkg::insert called on $self with options ".
156 join(', ', map "$_=>$options{$_}", keys %options)
159 local $SIG{HUP} = 'IGNORE';
160 local $SIG{INT} = 'IGNORE';
161 local $SIG{QUIT} = 'IGNORE';
162 local $SIG{TERM} = 'IGNORE';
163 local $SIG{TSTP} = 'IGNORE';
164 local $SIG{PIPE} = 'IGNORE';
166 my $oldAutoCommit = $FS::UID::AutoCommit;
167 local $FS::UID::AutoCommit = 0;
170 warn " inserting part_pkg record" if $DEBUG;
171 my $error = $self->SUPER::insert( $options{options} );
173 $dbh->rollback if $oldAutoCommit;
177 my $conf = new FS::Conf;
178 if ( $conf->exists('agent_defaultpkg') ) {
179 warn " agent_defaultpkg set; allowing all agents to purchase package"
181 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
182 my $type_pkgs = new FS::type_pkgs({
183 'typenum' => $agent_type->typenum,
184 'pkgpart' => $self->pkgpart,
186 my $error = $type_pkgs->insert;
188 $dbh->rollback if $oldAutoCommit;
194 warn " inserting pkg_svc records" if $DEBUG;
195 my $pkg_svc = $options{'pkg_svc'} || {};
196 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
197 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
199 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
203 my $pkg_svc = new FS::pkg_svc( {
204 'pkgpart' => $self->pkgpart,
205 'svcpart' => $part_svc->svcpart,
206 'quantity' => $quantity,
207 'primary_svc' => $primary_svc,
209 my $error = $pkg_svc->insert;
211 $dbh->rollback if $oldAutoCommit;
216 if ( $options{'cust_pkg'} ) {
217 warn " updating cust_pkg record " if $DEBUG;
219 ref($options{'cust_pkg'})
220 ? $options{'cust_pkg'}
221 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
222 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
223 if $options{'custnum_ref'};
224 my %hash = $old_cust_pkg->hash;
225 $hash{'pkgpart'} = $self->pkgpart,
226 my $new_cust_pkg = new FS::cust_pkg \%hash;
227 local($FS::cust_pkg::disable_agentcheck) = 1;
228 my $error = $new_cust_pkg->replace($old_cust_pkg);
230 $dbh->rollback if $oldAutoCommit;
231 return "Error modifying cust_pkg record: $error";
235 warn " commiting transaction" if $DEBUG;
236 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
243 Currently unimplemented.
248 return "Can't (yet?) delete package definitions.";
249 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
252 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
254 Replaces OLD_RECORD with this one in the database. If there is an error,
255 returns the error, otherwise returns false.
257 Currently available options are: I<pkg_svc>, I<primary_svc> and I<options>
259 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
260 values, the appropriate FS::pkg_svc records will be replaced.
262 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
263 FS::pkg_svc record will be updated.
265 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
273 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
278 ( ref($_[0]) eq 'HASH' )
282 $options->{options} = {} unless defined($options->{options});
284 warn "FS::part_pkg::replace called on $new to replace $old with options".
285 join(', ', map "$_ => ". $options->{$_}, keys %$options)
288 local $SIG{HUP} = 'IGNORE';
289 local $SIG{INT} = 'IGNORE';
290 local $SIG{QUIT} = 'IGNORE';
291 local $SIG{TERM} = 'IGNORE';
292 local $SIG{TSTP} = 'IGNORE';
293 local $SIG{PIPE} = 'IGNORE';
295 my $oldAutoCommit = $FS::UID::AutoCommit;
296 local $FS::UID::AutoCommit = 0;
299 #plandata shit stays in replace for upgrades until after 2.0 (or edit
301 warn " saving legacy plandata" if $DEBUG;
302 my $plandata = $new->get('plandata');
303 $new->set('plandata', '');
305 warn " deleting old part_pkg_option records" if $DEBUG;
306 foreach my $part_pkg_option ( $old->part_pkg_option ) {
307 my $error = $part_pkg_option->delete;
309 $dbh->rollback if $oldAutoCommit;
314 warn " replacing part_pkg record" if $DEBUG;
315 my $error = $new->SUPER::replace($old, $options->{options} );
317 $dbh->rollback if $oldAutoCommit;
321 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
322 foreach my $part_pkg_option (
323 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
324 return "illegal plandata: $plandata";
326 new FS::part_pkg_option {
327 'pkgpart' => $new->pkgpart,
332 split("\n", $plandata)
334 my $error = $part_pkg_option->insert;
336 $dbh->rollback if $oldAutoCommit;
341 warn " replacing pkg_svc records" if $DEBUG;
342 my $pkg_svc = $options->{'pkg_svc'} || {};
343 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
344 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
346 ( defined($options->{'primary_svc'})
347 && $options->{'primary_svc'} == $part_svc->svcpart
353 my $old_pkg_svc = qsearchs('pkg_svc', {
354 'pkgpart' => $old->pkgpart,
355 'svcpart' => $part_svc->svcpart,
357 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
358 my $old_primary_svc =
359 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
360 ? $old_pkg_svc->primary_svc
362 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
364 my $new_pkg_svc = new FS::pkg_svc( {
365 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
366 'pkgpart' => $new->pkgpart,
367 'svcpart' => $part_svc->svcpart,
368 'quantity' => $quantity,
369 'primary_svc' => $primary_svc,
371 my $error = $old_pkg_svc
372 ? $new_pkg_svc->replace($old_pkg_svc)
373 : $new_pkg_svc->insert;
375 $dbh->rollback if $oldAutoCommit;
380 warn " commiting transaction" if $DEBUG;
381 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
387 Checks all fields to make sure this is a valid package definition. If
388 there is an error, returns the error, otherwise returns false. Called by the
389 insert and replace methods.
395 warn "FS::part_pkg::check called on $self" if $DEBUG;
397 for (qw(setup recur plandata)) {
398 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
399 return "Use of $_ field is deprecated; set a plan and options: ".
401 if length($self->get($_));
405 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
406 my $error = $self->ut_number('freq');
407 return $error if $error;
409 $self->freq =~ /^(\d+[hdw]?)$/
410 or return "Illegal or empty freq: ". $self->freq;
414 my $error = $self->ut_numbern('pkgpart')
415 || $self->ut_text('pkg')
416 || $self->ut_text('comment')
417 || $self->ut_textn('promo_code')
418 || $self->ut_alphan('plan')
419 || $self->ut_enum('setuptax', [ '', 'Y' ] )
420 || $self->ut_enum('recurtax', [ '', 'Y' ] )
421 || $self->ut_textn('taxclass')
422 || $self->ut_enum('disabled', [ '', 'Y' ] )
423 || $self->ut_floatn('pay_weight')
424 || $self->ut_floatn('credit_weight')
425 || $self->ut_numbern('taxproductnum')
426 || $self->ut_foreign_keyn('taxproductnum',
427 'part_pkg_taxproduct',
430 || $self->ut_agentnum_acl('agentnum', 'Edit global package definitions')
431 || $self->SUPER::check
433 return $error if $error;
435 if ( $self->classnum !~ /^$/ ) {
436 my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum');
437 return $error if $error;
442 return 'Unknown plan '. $self->plan
443 unless exists($plans{$self->plan});
445 my $conf = new FS::Conf;
446 return 'Taxclass is required'
447 if ! $self->taxclass && $conf->exists('require_taxclasses');
454 Returns an (internal) string representing this package. Currently,
455 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
456 future, omitting pkgpart.
463 #$self->pkg. ' - '. $self->comment;
464 #$self->pkg. ' ('. $self->comment. ')';
465 $self->pkgpart. ': '. $self->pkg. ' - '. $self->comment;
470 Returns the package class, as an FS::pkg_class object, or the empty string
471 if there is no package class.
477 if ( $self->classnum ) {
478 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
486 Returns the package category name, or the empty string if there is no package
493 my $pkg_class = $self->pkg_class;
495 ? $pkg_class->categoryname
501 Returns the package class name, or the empty string if there is no package
508 my $pkg_class = $self->pkg_class;
510 ? $pkg_class->classname
516 Returns the associated agent for this event, if any, as an FS::agent object.
522 qsearchs('agent', { 'agentnum' => $self->agentnum } );
525 =item pkg_svc [ HASHREF | OPTION => VALUE ]
527 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
528 definition (with non-zero quantity).
530 One option is available, I<disable_linked>. If set true it will return the
531 services for this package definition alone, omitting services from any add-on
539 # #sort { $b->primary cmp $a->primary }
540 # grep { $_->quantity }
541 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
543 my $opt = ref($_[0]) ? $_[0] : { @_ };
544 my %pkg_svc = map { $_->svcpart => $_ }
545 grep { $_->quantity }
546 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
548 unless ( $opt->{disable_linked} ) {
549 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
550 my @pkg_svc = grep { $_->quantity }
551 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
552 foreach my $pkg_svc ( @pkg_svc ) {
553 if ( $pkg_svc{$pkg_svc->svcpart} ) {
554 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
555 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
557 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
567 =item svcpart [ SVCDB ]
569 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
570 associated with this package definition (see L<FS::pkg_svc>). Returns
571 false if there not a primary service definition or exactly one service
572 definition with quantity 1, or if SVCDB is specified and does not match the
573 svcdb of the service definition,
579 my $svcdb = scalar(@_) ? shift : '';
581 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
583 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
584 if dbdef->table('pkg_svc')->column('primary_svc');
585 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
587 return '' if scalar(@pkg_svc) != 1;
588 $pkg_svc[0]->svcpart;
593 Returns a list of the acceptable payment types for this package. Eventually
594 this should come out of a database table and be editable, but currently has the
595 following logic instead:
597 If the package is free, the single item B<BILL> is
598 returned, otherwise, the single item B<CARD> is returned.
600 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
606 if ( $self->is_free ) {
615 Returns true if this package is free.
621 unless ( $self->plan ) {
622 $self->setup =~ /^\s*0+(\.0*)?\s*$/
623 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
624 } elsif ( $self->can('is_free_options') ) {
625 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
626 map { $self->option($_) }
627 $self->is_free_options;
629 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
630 "provides neither is_free_options nor is_free method; returning false";
637 #method, class method or sub? #my $self = shift;
639 tie my %freq, 'Tie::IxHash',
640 '0' => '(no recurring fee)',
643 '2d' => 'every two days',
644 '3d' => 'every three days',
646 '2w' => 'biweekly (every 2 weeks)',
648 '45d' => 'every 45 days',
649 '2' => 'bimonthly (every 2 months)',
650 '3' => 'quarterly (every 3 months)',
651 '4' => 'every 4 months',
652 '137d' => 'every 4 1/2 months (137 days)',
653 '6' => 'semiannually (every 6 months)',
655 '13' => 'every 13 months (annually +1 month)',
656 '24' => 'biannually (every 2 years)',
657 '36' => 'triannually (every 3 years)',
658 '48' => '(every 4 years)',
659 '60' => '(every 5 years)',
660 '120' => '(every 10 years)',
669 Returns an english representation of the I<freq> field, such as "monthly",
670 "weekly", "semi-annually", etc.
676 my $freq = $self->freq;
678 #my $freqs_href = $self->freqs_href;
679 my $freqs_href = freqs_href();
681 if ( exists($freqs_href->{$freq}) ) {
682 $freqs_href->{$freq};
684 my $interval = 'month';
685 if ( $freq =~ /^(\d+)([hdw])$/ ) {
686 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
687 $interval = $interval{$2};
692 "every $freq ${interval}s";
699 For backwards compatibility, returns the plandata field as well as all options
700 from FS::part_pkg_option.
706 carp "plandata is deprecated";
708 $self->SUPER::plandata(@_);
710 my $plandata = $self->get('plandata');
711 my %options = $self->options;
712 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
717 =item part_pkg_option
719 Returns all options as FS::part_pkg_option objects (see
720 L<FS::part_pkg_option>).
724 sub part_pkg_option {
726 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
731 Returns a list of option names and values suitable for assigning to a hash.
737 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
740 =item option OPTIONNAME
742 Returns the option value for the given name, or the empty string.
747 my( $self, $opt, $ornull ) = @_;
748 my $part_pkg_option =
749 qsearchs('part_pkg_option', {
750 pkgpart => $self->pkgpart,
753 return $part_pkg_option->optionvalue if $part_pkg_option;
754 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
755 split("\n", $self->get('plandata') );
756 return $plandata{$opt} if exists $plandata{$opt};
757 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
758 "not found in options or plandata!\n"
763 =item bill_part_pkg_link
765 Returns the associated part_pkg_link records (see L<FS::part_pkg_link).
769 sub bill_part_pkg_link {
770 shift->_part_pkg_link('bill', @_);
773 =item svc_part_pkg_link
777 sub svc_part_pkg_link {
778 shift->_part_pkg_link('svc', @_);
782 my( $self, $type ) = @_;
783 qsearch('part_pkg_link', { 'src_pkgpart' => $self->pkgpart,
784 'link_type' => $type,
789 sub self_and_bill_linked {
790 shift->_self_and_linked('bill', @_);
793 sub _self_and_linked {
794 my( $self, $type ) = @_;
797 map { $_->dst_pkg->_self_and_linked($type) }
798 $self->_part_pkg_link($type)
802 =item part_pkg_taxoverride
804 Returns all associated FS::part_pkg_taxoverride objects (see
805 L<FS::part_pkg_taxoverride>).
809 sub part_pkg_taxoverride {
811 qsearch('part_pkg_taxoverride', { 'pkgpart' => $self->pkgpart } );
814 =item taxproduct_description
816 Returns the description of the associated tax product for this package
817 definition (see L<FS::part_pkg_taxproduct>).
821 sub taxproduct_description {
823 my $part_pkg_taxproduct =
824 qsearchs( 'part_pkg_taxproduct',
825 { 'taxproductnum' => $self->taxproductnum }
827 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
830 =item part_pkg_taxrate DATA_PROVIDER, GEOCODE
832 Returns the package to taxrate m2m records for this package in the location
833 specified by GEOCODE (see L<FS::part_pkg_taxrate> and ).
837 sub _expand_cch_taxproductnum {
839 my $part_pkg_taxproduct =
840 qsearchs( 'part_pkg_taxproduct',
841 { 'taxproductnum' => $self->taxproductnum }
843 my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
844 ? ( split ':', $part_pkg_taxproduct->taxproduct )
847 my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
848 OR taxproduct = '$a:$b:$c:'
849 OR taxproduct = '$a:$b:".":$d'
850 OR taxproduct = '$a:$b:".":' )";
851 map { $_->taxproductnum } qsearch( { 'table' => 'part_pkg_taxproduct',
852 'hashref' => { 'data_vendor'=>'cch' },
853 'extra_sql' => $extra_sql,
858 sub part_pkg_taxrate {
860 my ($data_vendor, $geocode) = @_;
863 my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
864 dbh->quote($data_vendor);
867 $extra_sql .= ' AND ('.
868 join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) }
872 # much more CCH oddness in m2m -- this is kludgy
873 $extra_sql .= ' AND ('.
874 join(' OR ', map{ "taxproductnum = $_" } $self->_expand_cch_taxproductnum).
877 my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
878 my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
879 my $select = 'DISTINCT ON(taxclassnum) *, taxproduct';
881 # should qsearch preface columns with the table to facilitate joins?
882 qsearch( { 'table' => 'part_pkg_taxrate',
884 'hashref' => { # 'data_vendor' => $data_vendor,
885 # 'taxproductnum' => $self->taxproductnum,
887 'addl_from' => $addl_from,
888 'extra_sql' => $extra_sql,
889 'order_by' => $order_by,
895 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
896 PLAN is the object's I<plan> field. There should be better docs
897 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
903 my $plan = $self->plan;
905 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
909 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
910 my $class = ref($self). "::$plan";
911 warn "reblessing $self into $class" if $DEBUG;
914 bless($self, $class) unless $@;
918 #fallbacks that eval the setup and recur fields, for backwards compat
922 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
923 $self->_calc_eval('setup', @_);
928 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
929 $self->_calc_eval('recur', @_);
932 use vars qw( $sdate @details );
934 #my( $self, $field, $cust_pkg ) = @_;
935 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
937 *details = $detailsref;
938 $self->$field() =~ /^(.*)$/
939 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
940 $self->$field(). "\n";
942 return 0 if $prog =~ /^\s*$/;
943 my $value = eval $prog;
948 #fallback that return 0 for old legacy packages with no plan
950 sub calc_remain { 0; }
951 sub calc_cancel { 0; }
952 sub calc_units { 0; }
960 # Used by FS::Upgrade to migrate to a new database.
962 sub _upgrade_data { # class method
963 my($class, %opts) = @_;
965 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
967 my @part_pkg = qsearch({
968 'table' => 'part_pkg',
969 'extra_sql' => "WHERE ". join(' OR ',
970 ( map "($_ IS NOT NULL AND $_ != '' )",
971 qw( plandata setup recur ) ),
972 'plan IS NULL', "plan = '' ",
976 foreach my $part_pkg (@part_pkg) {
978 unless ( $part_pkg->plan ) {
980 $part_pkg->plan('flat');
982 if ( $part_pkg->setup =~ /^\s*([\d\.]+)\s*$/ ) {
984 my $opt = new FS::part_pkg_option {
985 'pkgpart' => $part_pkg->pkgpart,
986 'optionname' => 'setup_fee',
989 my $error = $opt->insert;
990 die $error if $error;
992 $part_pkg->setup('');
995 die "Can't parse part_pkg.setup for fee; convert pkgnum ".
996 $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
999 if ( $part_pkg->recur =~ /^\s*([\d\.]+)\s*$/ ) {
1001 my $opt = new FS::part_pkg_option {
1002 'pkgpart' => $part_pkg->pkgpart,
1003 'optionname' => 'recur_fee',
1004 'optionvalue' => $1,
1006 my $error = $opt->insert;
1007 die $error if $error;
1009 $part_pkg->recur('');
1012 die "Can't parse part_pkg.setup for fee; convert pkgnum ".
1013 $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
1018 $part_pkg->replace; #this should take care of plandata, right?
1032 #false laziness w/part_export & cdr
1034 foreach my $INC ( @INC ) {
1035 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1036 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1037 warn "attempting to load plan info from $file\n" if $DEBUG;
1038 $file =~ /\/(\w+)\.pm$/ or do {
1039 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1043 my $info = eval "use FS::part_pkg::$mod; ".
1044 "\\%FS::part_pkg::$mod\::info;";
1046 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1049 unless ( keys %$info ) {
1050 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
1053 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1054 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1055 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1058 $info{$mod} = $info;
1062 tie %plans, 'Tie::IxHash',
1063 map { $_ => $info{$_} }
1064 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1071 =item format OPTION DATA
1073 Returns data formatted according to the function 'format' described
1074 in the plan info. Returns DATA if no such function exists.
1079 my ($self, $option, $data) = (shift, shift, shift);
1080 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1081 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1087 =item parse OPTION DATA
1089 Returns data parsed according to the function 'parse' described
1090 in the plan info. Returns DATA if no such function exists.
1095 my ($self, $option, $data) = (shift, shift, shift);
1096 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1097 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1106 =head1 NEW PLAN CLASSES
1108 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
1109 found in eg/plan_template.pm. Until then, it is suggested that you use the
1110 other modules in FS/FS/part_pkg/ as a guide.
1114 The delete method is unimplemented.
1116 setup and recur semantics are not yet defined (and are implemented in
1117 FS::cust_bill. hmm.). now they're deprecated and need to go.
1121 part_pkg_taxrate is Pg specific
1125 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1126 schema.html from the base documentation.