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;
345 my $primary_svc = $options->{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
347 my $old_pkg_svc = qsearchs('pkg_svc', {
348 'pkgpart' => $old->pkgpart,
349 'svcpart' => $part_svc->svcpart,
351 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
352 my $old_primary_svc =
353 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
354 ? $old_pkg_svc->primary_svc
356 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
358 my $new_pkg_svc = new FS::pkg_svc( {
359 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
360 'pkgpart' => $new->pkgpart,
361 'svcpart' => $part_svc->svcpart,
362 'quantity' => $quantity,
363 'primary_svc' => $primary_svc,
365 my $error = $old_pkg_svc
366 ? $new_pkg_svc->replace($old_pkg_svc)
367 : $new_pkg_svc->insert;
369 $dbh->rollback if $oldAutoCommit;
374 warn " commiting transaction" if $DEBUG;
375 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
381 Checks all fields to make sure this is a valid package definition. If
382 there is an error, returns the error, otherwise returns false. Called by the
383 insert and replace methods.
389 warn "FS::part_pkg::check called on $self" if $DEBUG;
391 for (qw(setup recur plandata)) {
392 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
393 return "Use of $_ field is deprecated; set a plan and options: ".
395 if length($self->get($_));
399 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
400 my $error = $self->ut_number('freq');
401 return $error if $error;
403 $self->freq =~ /^(\d+[hdw]?)$/
404 or return "Illegal or empty freq: ". $self->freq;
408 my $error = $self->ut_numbern('pkgpart')
409 || $self->ut_text('pkg')
410 || $self->ut_text('comment')
411 || $self->ut_textn('promo_code')
412 || $self->ut_alphan('plan')
413 || $self->ut_enum('setuptax', [ '', 'Y' ] )
414 || $self->ut_enum('recurtax', [ '', 'Y' ] )
415 || $self->ut_textn('taxclass')
416 || $self->ut_enum('disabled', [ '', 'Y' ] )
417 || $self->ut_floatn('pay_weight')
418 || $self->ut_floatn('credit_weight')
419 || $self->ut_numbern('taxproductnum')
420 || $self->ut_foreign_keyn('taxproductnum',
421 'part_pkg_taxproduct',
424 || $self->ut_agentnum_acl('agentnum', 'Edit global package definitions')
425 || $self->SUPER::check
427 return $error if $error;
429 if ( $self->classnum !~ /^$/ ) {
430 my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum');
431 return $error if $error;
436 return 'Unknown plan '. $self->plan
437 unless exists($plans{$self->plan});
439 my $conf = new FS::Conf;
440 return 'Taxclass is required'
441 if ! $self->taxclass && $conf->exists('require_taxclasses');
448 Returns an (internal) string representing this package. Currently,
449 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
450 future, omitting pkgpart.
457 #$self->pkg. ' - '. $self->comment;
458 #$self->pkg. ' ('. $self->comment. ')';
459 $self->pkgpart. ': '. $self->pkg. ' - '. $self->comment;
464 Returns the package class, as an FS::pkg_class object, or the empty string
465 if there is no package class.
471 if ( $self->classnum ) {
472 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
480 Returns the package class name, or the empty string if there is no package
487 my $pkg_class = $self->pkg_class;
489 ? $pkg_class->classname
495 Returns the associated agent for this event, if any, as an FS::agent object.
501 qsearchs('agent', { 'agentnum' => $self->agentnum } );
504 =item pkg_svc [ HASHREF | OPTION => VALUE ]
506 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
507 definition (with non-zero quantity).
509 One option is available, I<disable_linked>. If set true it will return the
510 services for this package definition alone, omitting services from any add-on
518 # #sort { $b->primary cmp $a->primary }
519 # grep { $_->quantity }
520 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
522 my $opt = ref($_[0]) ? $_[0] : { @_ };
523 my %pkg_svc = map { $_->svcpart => $_ }
524 grep { $_->quantity }
525 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
527 unless ( $opt->{disable_linked} ) {
528 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
529 my @pkg_svc = grep { $_->quantity }
530 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
531 foreach my $pkg_svc ( @pkg_svc ) {
532 if ( $pkg_svc{$pkg_svc->svcpart} ) {
533 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
534 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
536 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
546 =item svcpart [ SVCDB ]
548 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
549 associated with this package definition (see L<FS::pkg_svc>). Returns
550 false if there not a primary service definition or exactly one service
551 definition with quantity 1, or if SVCDB is specified and does not match the
552 svcdb of the service definition,
558 my $svcdb = scalar(@_) ? shift : '';
560 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
562 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
563 if dbdef->table('pkg_svc')->column('primary_svc');
564 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
566 return '' if scalar(@pkg_svc) != 1;
567 $pkg_svc[0]->svcpart;
572 Returns a list of the acceptable payment types for this package. Eventually
573 this should come out of a database table and be editable, but currently has the
574 following logic instead:
576 If the package is free, the single item B<BILL> is
577 returned, otherwise, the single item B<CARD> is returned.
579 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
585 if ( $self->is_free ) {
594 Returns true if this package is free.
600 unless ( $self->plan ) {
601 $self->setup =~ /^\s*0+(\.0*)?\s*$/
602 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
603 } elsif ( $self->can('is_free_options') ) {
604 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
605 map { $self->option($_) }
606 $self->is_free_options;
608 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
609 "provides neither is_free_options nor is_free method; returning false";
616 #method, class method or sub? #my $self = shift;
618 tie my %freq, 'Tie::IxHash',
619 '0' => '(no recurring fee)',
622 '2d' => 'every two days',
623 '3d' => 'every three days',
625 '2w' => 'biweekly (every 2 weeks)',
627 '45d' => 'every 45 days',
628 '2' => 'bimonthly (every 2 months)',
629 '3' => 'quarterly (every 3 months)',
630 '4' => 'every 4 months',
631 '137d' => 'every 4 1/2 months (137 days)',
632 '6' => 'semiannually (every 6 months)',
634 '13' => 'every 13 months (annually +1 month)',
635 '24' => 'biannually (every 2 years)',
636 '36' => 'triannually (every 3 years)',
637 '48' => '(every 4 years)',
638 '60' => '(every 5 years)',
639 '120' => '(every 10 years)',
648 Returns an english representation of the I<freq> field, such as "monthly",
649 "weekly", "semi-annually", etc.
655 my $freq = $self->freq;
657 #my $freqs_href = $self->freqs_href;
658 my $freqs_href = freqs_href();
660 if ( exists($freqs_href->{$freq}) ) {
661 $freqs_href->{$freq};
663 my $interval = 'month';
664 if ( $freq =~ /^(\d+)([hdw])$/ ) {
665 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
666 $interval = $interval{$2};
671 "every $freq ${interval}s";
678 For backwards compatibility, returns the plandata field as well as all options
679 from FS::part_pkg_option.
685 carp "plandata is deprecated";
687 $self->SUPER::plandata(@_);
689 my $plandata = $self->get('plandata');
690 my %options = $self->options;
691 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
696 =item part_pkg_option
698 Returns all options as FS::part_pkg_option objects (see
699 L<FS::part_pkg_option>).
703 sub part_pkg_option {
705 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
710 Returns a list of option names and values suitable for assigning to a hash.
716 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
719 =item option OPTIONNAME
721 Returns the option value for the given name, or the empty string.
726 my( $self, $opt, $ornull ) = @_;
727 my $part_pkg_option =
728 qsearchs('part_pkg_option', {
729 pkgpart => $self->pkgpart,
732 return $part_pkg_option->optionvalue if $part_pkg_option;
733 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
734 split("\n", $self->get('plandata') );
735 return $plandata{$opt} if exists $plandata{$opt};
736 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
737 "not found in options or plandata!\n"
742 =item bill_part_pkg_link
744 Returns the associated part_pkg_link records (see L<FS::part_pkg_link).
748 sub bill_part_pkg_link {
749 shift->_part_pkg_link('bill', @_);
752 =item svc_part_pkg_link
756 sub svc_part_pkg_link {
757 shift->_part_pkg_link('svc', @_);
761 my( $self, $type ) = @_;
762 qsearch('part_pkg_link', { 'src_pkgpart' => $self->pkgpart,
763 'link_type' => $type,
768 sub self_and_bill_linked {
769 shift->_self_and_linked('bill', @_);
772 sub _self_and_linked {
773 my( $self, $type ) = @_;
776 map { $_->dst_pkg->_self_and_linked($type) }
777 $self->_part_pkg_link($type)
781 =item part_pkg_taxoverride
783 Returns all associated FS::part_pkg_taxoverride objects (see
784 L<FS::part_pkg_taxoverride>).
788 sub part_pkg_taxoverride {
790 qsearch('part_pkg_taxoverride', { 'pkgpart' => $self->pkgpart } );
793 =item taxproduct_description
795 Returns the description of the associated tax product for this package
796 definition (see L<FS::part_pkg_taxproduct>).
800 sub taxproduct_description {
802 my $part_pkg_taxproduct =
803 qsearchs( 'part_pkg_taxproduct',
804 { 'taxproductnum' => $self->taxproductnum }
806 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
809 =item part_pkg_taxrate DATA_PROVIDER, GEOCODE
811 Returns the package to taxrate m2m records for this package in the location
812 specified by GEOCODE (see L<FS::part_pkg_taxrate> and ).
816 sub _expand_cch_taxproductnum {
818 my $part_pkg_taxproduct =
819 qsearchs( 'part_pkg_taxproduct',
820 { 'taxproductnum' => $self->taxproductnum }
822 my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
823 ? ( split ':', $part_pkg_taxproduct->taxproduct )
826 my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
827 OR taxproduct = '$a:$b:$c:'
828 OR taxproduct = '$a:$b:".":$d'
829 OR taxproduct = '$a:$b:".":' )";
830 map { $_->taxproductnum } qsearch( { 'table' => 'part_pkg_taxproduct',
831 'hashref' => { 'data_vendor'=>'cch' },
832 'extra_sql' => $extra_sql,
837 sub part_pkg_taxrate {
839 my ($data_vendor, $geocode) = @_;
842 my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
843 dbh->quote($data_vendor);
846 $extra_sql .= ' AND ('.
847 join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) }
851 # much more CCH oddness in m2m -- this is kludgy
852 $extra_sql .= ' AND ('.
853 join(' OR ', map{ "taxproductnum = $_" } $self->_expand_cch_taxproductnum).
856 my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
857 my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
858 my $select = 'DISTINCT ON(taxclassnum) *, taxproduct';
860 # should qsearch preface columns with the table to facilitate joins?
861 qsearch( { 'table' => 'part_pkg_taxrate',
863 'hashref' => { # 'data_vendor' => $data_vendor,
864 # 'taxproductnum' => $self->taxproductnum,
866 'addl_from' => $addl_from,
867 'extra_sql' => $extra_sql,
868 'order_by' => $order_by,
874 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
875 PLAN is the object's I<plan> field. There should be better docs
876 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
882 my $plan = $self->plan;
884 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
888 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
889 my $class = ref($self). "::$plan";
890 warn "reblessing $self into $class" if $DEBUG;
893 bless($self, $class) unless $@;
897 #fallbacks that eval the setup and recur fields, for backwards compat
901 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
902 $self->_calc_eval('setup', @_);
907 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
908 $self->_calc_eval('recur', @_);
911 use vars qw( $sdate @details );
913 #my( $self, $field, $cust_pkg ) = @_;
914 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
916 *details = $detailsref;
917 $self->$field() =~ /^(.*)$/
918 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
919 $self->$field(). "\n";
921 return 0 if $prog =~ /^\s*$/;
922 my $value = eval $prog;
927 #fallback that return 0 for old legacy packages with no plan
929 sub calc_remain { 0; }
930 sub calc_cancel { 0; }
931 sub calc_units { 0; }
939 # Used by FS::Upgrade to migrate to a new database.
941 sub _upgrade_data { # class method
942 my($class, %opts) = @_;
944 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
946 my @part_pkg = qsearch({
947 'table' => 'part_pkg',
948 'extra_sql' => "WHERE ". join(' OR ',
949 ( map "($_ IS NOT NULL AND $_ != '' )",
950 qw( plandata setup recur ) ),
951 'plan IS NULL', "plan = '' ",
955 foreach my $part_pkg (@part_pkg) {
957 unless ( $part_pkg->plan ) {
959 $part_pkg->plan('flat');
961 if ( $part_pkg->setup =~ /^\s*([\d\.]+)\s*$/ ) {
963 my $opt = new FS::part_pkg_option {
964 'pkgpart' => $part_pkg->pkgpart,
965 'optionname' => 'setup_fee',
968 my $error = $opt->insert;
969 die $error if $error;
971 $part_pkg->setup('');
974 die "Can't parse part_pkg.setup for fee; convert pkgnum ".
975 $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
978 if ( $part_pkg->recur =~ /^\s*([\d\.]+)\s*$/ ) {
980 my $opt = new FS::part_pkg_option {
981 'pkgpart' => $part_pkg->pkgpart,
982 'optionname' => 'recur_fee',
985 my $error = $opt->insert;
986 die $error if $error;
988 $part_pkg->recur('');
991 die "Can't parse part_pkg.setup for fee; convert pkgnum ".
992 $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
997 $part_pkg->replace; #this should take care of plandata, right?
1012 foreach my $INC ( @INC ) {
1013 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1014 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1015 warn "attempting to load plan info from $file\n" if $DEBUG;
1016 $file =~ /\/(\w+)\.pm$/ or do {
1017 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1021 my $info = eval "use FS::part_pkg::$mod; ".
1022 "\\%FS::part_pkg::$mod\::info;";
1024 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1027 unless ( keys %$info ) {
1028 warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
1029 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
1032 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1033 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1034 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1037 $info{$mod} = $info;
1041 tie %plans, 'Tie::IxHash',
1042 map { $_ => $info{$_} }
1043 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1050 =item format OPTION DATA
1052 Returns data formatted according to the function 'format' described
1053 in the plan info. Returns DATA if no such function exists.
1058 my ($self, $option, $data) = (shift, shift, shift);
1059 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1060 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1066 =item parse OPTION DATA
1068 Returns data parsed according to the function 'parse' described
1069 in the plan info. Returns DATA if no such function exists.
1074 my ($self, $option, $data) = (shift, shift, shift);
1075 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1076 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1085 =head1 NEW PLAN CLASSES
1087 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
1088 found in eg/plan_template.pm. Until then, it is suggested that you use the
1089 other modules in FS/FS/part_pkg/ as a guide.
1093 The delete method is unimplemented.
1095 setup and recur semantics are not yet defined (and are implemented in
1096 FS::cust_bill. hmm.). now they're deprecated and need to go.
1100 part_pkg_taxrate is Pg specific
1104 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1105 schema.html from the base documentation.