4 use vars qw( @ISA %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
5 use Carp qw(carp cluck confess);
6 use Scalar::Util qw( blessed );
7 use Time::Local qw( timelocal timelocal_nocheck );
10 use FS::Record qw( qsearch qsearchs dbh dbdef );
16 use FS::part_pkg_option;
19 use FS::part_pkg_taxrate;
20 use FS::part_pkg_taxoverride;
21 use FS::part_pkg_taxproduct;
22 use FS::part_pkg_link;
23 use FS::part_pkg_discount;
24 use FS::part_pkg_vendor;
26 @ISA = qw( FS::m2m_Common FS::option_Common );
29 $skip_pkg_svc_hack = 0;
33 FS::part_pkg - Object methods for part_pkg objects
39 $record = new FS::part_pkg \%hash
40 $record = new FS::part_pkg { 'column' => 'value' };
42 $custom_record = $template_record->clone;
44 $error = $record->insert;
46 $error = $new_record->replace($old_record);
48 $error = $record->delete;
50 $error = $record->check;
52 @pkg_svc = $record->pkg_svc;
54 $svcnum = $record->svcpart;
55 $svcnum = $record->svcpart( 'svc_acct' );
59 An FS::part_pkg object represents a package definition. FS::part_pkg
60 inherits from FS::Record. The following fields are currently supported:
64 =item pkgpart - primary key (assigned automatically for new package definitions)
66 =item pkg - Text name of this package definition (customer-viewable)
68 =item comment - Text name of this package definition (non-customer-viewable)
70 =item classnum - Optional package class (see L<FS::pkg_class>)
72 =item promo_code - Promotional code
74 =item setup - Setup fee expression (deprecated)
76 =item freq - Frequency of recurring fee
78 =item recur - Recurring fee expression (deprecated)
80 =item setuptax - Setup fee tax exempt flag, empty or `Y'
82 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
84 =item taxclass - Tax class
86 =item plan - Price plan
88 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
90 =item disabled - Disabled flag, empty or `Y'
92 =item custom - Custom flag, empty or `Y'
94 =item setup_cost - for cost tracking
96 =item recur_cost - for cost tracking
98 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
100 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
102 =item agentnum - Optional agentnum (see L<FS::agent>)
104 =item fcc_ds0s - Optional DS0 equivalency number for FCC form 477
106 =item fcc_voip_class - Which column of FCC form 477 part II.B this package
109 =item delay_start - Number of days to delay package start, by default
119 Creates a new package definition. To add the package definition to
120 the database, see L<"insert">.
124 sub table { 'part_pkg'; }
128 An alternate constructor. Creates a new package definition by duplicating
129 an existing definition. A new pkgpart is assigned and the custom flag is
130 set to Y. To add the package definition to the database, see L<"insert">.
136 my $class = ref($self);
137 my %hash = $self->hash;
138 $hash{'pkgpart'} = '';
139 $hash{'custom'} = 'Y';
140 #new FS::part_pkg ( \%hash ); # ?
141 new $class ( \%hash ); # ?
144 =item insert [ , OPTION => VALUE ... ]
146 Adds this package definition to the database. If there is an error,
147 returns the error, otherwise returns false.
149 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
150 I<custnum_ref> and I<options>.
152 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
153 values, appropriate FS::pkg_svc records will be inserted. I<hidden_svc> can
154 be set to a hashref of svcparts and flag values ('Y' or '') to set the
155 'hidden' field in these records.
157 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
158 FS::pkg_svc record will be updated.
160 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
161 record itself), the object will be updated to point to this package definition.
163 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
164 the scalar will be updated with the custnum value from the cust_pkg record.
166 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
167 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
168 records will be inserted.
170 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
171 records will be inserted.
178 warn "FS::part_pkg::insert called on $self with options ".
179 join(', ', map "$_=>$options{$_}", keys %options)
182 local $SIG{HUP} = 'IGNORE';
183 local $SIG{INT} = 'IGNORE';
184 local $SIG{QUIT} = 'IGNORE';
185 local $SIG{TERM} = 'IGNORE';
186 local $SIG{TSTP} = 'IGNORE';
187 local $SIG{PIPE} = 'IGNORE';
189 my $oldAutoCommit = $FS::UID::AutoCommit;
190 local $FS::UID::AutoCommit = 0;
193 warn " inserting part_pkg record" if $DEBUG;
194 my $error = $self->SUPER::insert( $options{options} );
196 $dbh->rollback if $oldAutoCommit;
200 my $conf = new FS::Conf;
201 if ( $conf->exists('agent_defaultpkg') ) {
202 warn " agent_defaultpkg set; allowing all agents to purchase package"
204 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
205 my $type_pkgs = new FS::type_pkgs({
206 'typenum' => $agent_type->typenum,
207 'pkgpart' => $self->pkgpart,
209 my $error = $type_pkgs->insert;
211 $dbh->rollback if $oldAutoCommit;
217 warn " inserting part_pkg_taxoverride records" if $DEBUG;
218 my %overrides = %{ $options{'tax_overrides'} || {} };
219 foreach my $usage_class ( keys %overrides ) {
221 ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
222 ? $overrides{$usage_class}
224 my @overrides = (grep "$_", split(',', $override) );
225 my $error = $self->process_m2m (
226 'link_table' => 'part_pkg_taxoverride',
227 'target_table' => 'tax_class',
228 'hashref' => { 'usage_class' => $usage_class },
229 'params' => \@overrides,
232 $dbh->rollback if $oldAutoCommit;
237 unless ( $skip_pkg_svc_hack ) {
239 warn " inserting pkg_svc records" if $DEBUG;
240 my $pkg_svc = $options{'pkg_svc'} || {};
241 my $hidden_svc = $options{'hidden_svc'} || {};
242 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
243 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
245 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
249 my $pkg_svc = new FS::pkg_svc( {
250 'pkgpart' => $self->pkgpart,
251 'svcpart' => $part_svc->svcpart,
252 'quantity' => $quantity,
253 'primary_svc' => $primary_svc,
254 'hidden' => $hidden_svc->{$part_svc->svcpart},
256 my $error = $pkg_svc->insert;
258 $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 if ( $options{'part_pkg_vendor'} ) {
285 while ( my ($exportnum, $vendor_pkg_id) =
286 each %{ $options{part_pkg_vendor} }
289 my $ppv = new FS::part_pkg_vendor( {
290 'pkgpart' => $self->pkgpart,
291 'exportnum' => $exportnum,
292 'vendor_pkg_id' => $vendor_pkg_id,
294 my $error = $ppv->insert;
296 $dbh->rollback if $oldAutoCommit;
297 return "Error inserting part_pkg_vendor record: $error";
302 warn " commiting transaction" if $DEBUG;
303 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
310 Currently unimplemented.
315 return "Can't (yet?) delete package definitions.";
316 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
319 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
321 Replaces OLD_RECORD with this one in the database. If there is an error,
322 returns the error, otherwise returns false.
324 Currently available options are: I<pkg_svc>, I<hidden_svc>, I<primary_svc>
327 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
328 values, the appropriate FS::pkg_svc records will be replaced. I<hidden_svc>
329 can be set to a hashref of svcparts and flag values ('Y' or '') to set the
330 'hidden' field in these records.
332 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
333 FS::pkg_svc record will be updated.
335 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
343 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
348 ( ref($_[0]) eq 'HASH' )
352 $options->{options} = {} unless defined($options->{options});
354 warn "FS::part_pkg::replace called on $new to replace $old with options".
355 join(', ', map "$_ => ". $options->{$_}, keys %$options)
358 local $SIG{HUP} = 'IGNORE';
359 local $SIG{INT} = 'IGNORE';
360 local $SIG{QUIT} = 'IGNORE';
361 local $SIG{TERM} = 'IGNORE';
362 local $SIG{TSTP} = 'IGNORE';
363 local $SIG{PIPE} = 'IGNORE';
365 my $oldAutoCommit = $FS::UID::AutoCommit;
366 local $FS::UID::AutoCommit = 0;
369 #plandata shit stays in replace for upgrades until after 2.0 (or edit
371 warn " saving legacy plandata" if $DEBUG;
372 my $plandata = $new->get('plandata');
373 $new->set('plandata', '');
375 warn " deleting old part_pkg_option records" if $DEBUG;
376 foreach my $part_pkg_option ( $old->part_pkg_option ) {
377 my $error = $part_pkg_option->delete;
379 $dbh->rollback if $oldAutoCommit;
384 warn " replacing part_pkg record" if $DEBUG;
385 my $error = $new->SUPER::replace($old, $options->{options} );
387 $dbh->rollback if $oldAutoCommit;
391 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
392 foreach my $part_pkg_option (
393 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
394 return "illegal plandata: $plandata";
396 new FS::part_pkg_option {
397 'pkgpart' => $new->pkgpart,
402 split("\n", $plandata)
404 my $error = $part_pkg_option->insert;
406 $dbh->rollback if $oldAutoCommit;
411 warn " replacing pkg_svc records" if $DEBUG;
412 my $pkg_svc = $options->{'pkg_svc'} || {};
413 my $hidden_svc = $options->{'hidden_svc'} || {};
414 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
415 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
416 my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
418 ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
419 && $options->{'primary_svc'} == $part_svc->svcpart
424 my $old_pkg_svc = qsearchs('pkg_svc', {
425 'pkgpart' => $old->pkgpart,
426 'svcpart' => $part_svc->svcpart,
429 my $old_quantity = 0;
430 my $old_primary_svc = '';
432 if ( $old_pkg_svc ) {
433 $old_quantity = $old_pkg_svc->quantity;
434 $old_primary_svc = $old_pkg_svc->primary_svc
435 if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
436 $old_hidden = $old_pkg_svc->hidden;
439 next unless $old_quantity != $quantity ||
440 $old_primary_svc ne $primary_svc ||
441 $old_hidden ne $hidden;
443 my $new_pkg_svc = new FS::pkg_svc( {
444 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
445 'pkgpart' => $new->pkgpart,
446 'svcpart' => $part_svc->svcpart,
447 'quantity' => $quantity,
448 'primary_svc' => $primary_svc,
451 my $error = $old_pkg_svc
452 ? $new_pkg_svc->replace($old_pkg_svc)
453 : $new_pkg_svc->insert;
455 $dbh->rollback if $oldAutoCommit;
460 my @part_pkg_vendor = $old->part_pkg_vendor;
461 my @current_exportnum = ();
462 if ( $options->{'part_pkg_vendor'} ) {
463 my($exportnum,$vendor_pkg_id);
464 while ( ($exportnum,$vendor_pkg_id)
465 = each %{$options->{'part_pkg_vendor'}} ) {
467 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
468 if($exportnum == $part_pkg_vendor->exportnum
469 && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
470 $part_pkg_vendor->vendor_pkg_id($vendor_pkg_id);
471 my $error = $part_pkg_vendor->replace;
473 $dbh->rollback if $oldAutoCommit;
474 return "Error replacing part_pkg_vendor record: $error";
479 elsif($exportnum == $part_pkg_vendor->exportnum
480 && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
485 unless ( $noinsert ) {
486 my $ppv = new FS::part_pkg_vendor( {
487 'pkgpart' => $new->pkgpart,
488 'exportnum' => $exportnum,
489 'vendor_pkg_id' => $vendor_pkg_id,
491 my $error = $ppv->insert;
493 $dbh->rollback if $oldAutoCommit;
494 return "Error inserting part_pkg_vendor record: $error";
497 push @current_exportnum, $exportnum;
500 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
501 unless ( grep($_ eq $part_pkg_vendor->exportnum, @current_exportnum) ) {
502 my $error = $part_pkg_vendor->delete;
504 $dbh->rollback if $oldAutoCommit;
505 return "Error deleting part_pkg_vendor record: $error";
510 warn " commiting transaction" if $DEBUG;
511 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
517 Checks all fields to make sure this is a valid package definition. If
518 there is an error, returns the error, otherwise returns false. Called by the
519 insert and replace methods.
525 warn "FS::part_pkg::check called on $self" if $DEBUG;
527 for (qw(setup recur plandata)) {
528 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
529 return "Use of $_ field is deprecated; set a plan and options: ".
531 if length($self->get($_));
535 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
536 my $error = $self->ut_number('freq');
537 return $error if $error;
539 $self->freq =~ /^(\d+[hdw]?)$/
540 or return "Illegal or empty freq: ". $self->freq;
544 my @null_agentnum_right = ( 'Edit global package definitions' );
545 push @null_agentnum_right, 'One-time charge'
546 if $self->freq =~ /^0/;
547 push @null_agentnum_right, 'Customize customer package'
548 if $self->disabled eq 'Y'; #good enough
550 my $error = $self->ut_numbern('pkgpart')
551 || $self->ut_text('pkg')
552 || $self->ut_text('comment')
553 || $self->ut_textn('promo_code')
554 || $self->ut_alphan('plan')
555 || $self->ut_enum('setuptax', [ '', 'Y' ] )
556 || $self->ut_enum('recurtax', [ '', 'Y' ] )
557 || $self->ut_textn('taxclass')
558 || $self->ut_enum('disabled', [ '', 'Y' ] )
559 || $self->ut_enum('custom', [ '', 'Y' ] )
560 || $self->ut_enum('no_auto', [ '', 'Y' ])
561 || $self->ut_enum('recur_show_zero', [ '', 'Y' ])
562 || $self->ut_enum('setup_show_zero', [ '', 'Y' ])
563 #|| $self->ut_moneyn('setup_cost')
564 #|| $self->ut_moneyn('recur_cost')
565 || $self->ut_floatn('setup_cost')
566 || $self->ut_floatn('recur_cost')
567 || $self->ut_floatn('pay_weight')
568 || $self->ut_floatn('credit_weight')
569 || $self->ut_numbern('taxproductnum')
570 || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum')
571 || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
572 || $self->ut_foreign_keyn('taxproductnum',
573 'part_pkg_taxproduct',
577 ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
578 : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
580 || $self->ut_numbern('fcc_ds0s')
581 || $self->ut_numbern('fcc_voip_class')
582 || $self->ut_numbern('delay_start')
583 || $self->SUPER::check
585 return $error if $error;
587 return 'Unknown plan '. $self->plan
588 unless exists($plans{$self->plan});
590 my $conf = new FS::Conf;
591 return 'Taxclass is required'
592 if ! $self->taxclass && $conf->exists('require_taxclasses');
597 =item pkg_comment [ OPTION => VALUE... ]
599 Returns an (internal) string representing this package. Currently,
600 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
601 future, omitting pkgpart. The comment will have '(CUSTOM) ' prepended if
604 If the option nopkgpart is true then the "pkgpart: ' is omitted.
612 #$self->pkg. ' - '. $self->comment;
613 #$self->pkg. ' ('. $self->comment. ')';
614 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
615 $pre. $self->pkg. ' - '. $self->custom_comment;
618 sub price_info { # safety, in case a part_pkg hasn't defined price_info
624 ( $self->custom ? '(CUSTOM) ' : '' ). $self->comment . ' ' . $self->price_info;
629 Returns the package class, as an FS::pkg_class object, or the empty string
630 if there is no package class.
636 if ( $self->classnum ) {
637 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
643 =item addon_pkg_class
645 Returns the add-on package class, as an FS::pkg_class object, or the empty
646 string if there is no add-on package class.
650 sub addon_pkg_class {
652 if ( $self->addon_classnum ) {
653 qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
661 Returns the package category name, or the empty string if there is no package
668 my $pkg_class = $self->pkg_class;
670 ? $pkg_class->categoryname
676 Returns the package class name, or the empty string if there is no package
683 my $pkg_class = $self->pkg_class;
685 ? $pkg_class->classname
689 =item addon_classname
691 Returns the add-on package class name, or the empty string if there is no
692 add-on package class.
696 sub addon_classname {
698 my $pkg_class = $self->addon_pkg_class;
700 ? $pkg_class->classname
706 Returns the associated agent for this event, if any, as an FS::agent object.
712 qsearchs('agent', { 'agentnum' => $self->agentnum } );
715 =item pkg_svc [ HASHREF | OPTION => VALUE ]
717 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
718 definition (with non-zero quantity).
720 One option is available, I<disable_linked>. If set true it will return the
721 services for this package definition alone, omitting services from any add-on
728 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
735 qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
741 # #sort { $b->primary cmp $a->primary }
742 # grep { $_->quantity }
743 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
745 my $opt = ref($_[0]) ? $_[0] : { @_ };
746 my %pkg_svc = map { $_->svcpart => $_ }
747 grep { $_->quantity }
748 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
750 unless ( $opt->{disable_linked} ) {
751 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
752 my @pkg_svc = grep { $_->quantity }
753 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
754 foreach my $pkg_svc ( @pkg_svc ) {
755 if ( $pkg_svc{$pkg_svc->svcpart} ) {
756 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
757 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
759 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
769 =item svcpart [ SVCDB ]
771 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
772 associated with this package definition (see L<FS::pkg_svc>). Returns
773 false if there not a primary service definition or exactly one service
774 definition with quantity 1, or if SVCDB is specified and does not match the
775 svcdb of the service definition. SVCDB can be specified as a scalar table
776 name, such as 'svc_acct', or as an arrayref of possible table names.
781 my $pkg_svc = shift->_primary_pkg_svc(@_);
782 $pkg_svc ? $pkg_svc->svcpart : '';
785 =item part_svc [ SVCDB ]
787 Like the B<svcpart> method, but returns the FS::part_svc object (see
793 my $pkg_svc = shift->_primary_pkg_svc(@_);
794 $pkg_svc ? $pkg_svc->part_svc : '';
797 sub _primary_pkg_svc {
800 my $svcdb = scalar(@_) ? shift : [];
801 $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
802 my %svcdb = map { $_=>1 } @$svcdb;
805 grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
808 my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
809 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
811 return '' if scalar(@pkg_svc) != 1;
815 =item svcpart_unique_svcdb SVCDB
817 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
818 SVCDB associated with this package definition (see L<FS::pkg_svc>). Returns
819 false if there not a primary service definition for SVCDB or there are multiple
820 service definitions for SVCDB.
824 sub svcpart_unique_svcdb {
825 my( $self, $svcdb ) = @_;
826 my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
827 return '' if scalar(@svcdb_pkg_svc) != 1;
828 $svcdb_pkg_svc[0]->svcpart;
833 Returns a list of the acceptable payment types for this package. Eventually
834 this should come out of a database table and be editable, but currently has the
835 following logic instead:
837 If the package is free, the single item B<BILL> is
838 returned, otherwise, the single item B<CARD> is returned.
840 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
846 if ( $self->is_free ) {
855 Returns true if this package is free.
861 if ( $self->can('is_free_options') ) {
862 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
863 map { $self->option($_) }
864 $self->is_free_options;
866 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
867 "provides neither is_free_options nor is_free method; returning false";
872 # whether the plan allows discounts to be applied to this package
873 sub can_discount { 0; }
875 # whether the plan allows changing the start date
876 sub can_start_date { 1; }
878 # the default start date; takes an FS::cust_main as an argument
879 sub default_start_date {
881 my $cust_main = shift;
882 my $conf = FS::Conf->new;
884 if ( $self->delay_start ) {
885 my $delay = $self->delay_start;
887 my ($mday,$mon,$year) = (localtime(time))[3,4,5];
888 my $start_date = timelocal(0,0,0,$mday,$mon,$year) + 86400 * $delay;
891 } elsif ( $conf->exists('order_pkg-no_start_date') ) {
897 return $cust_main->next_bill_date;
903 # moved to FS::Misc to make this accessible to other packages
905 FS::Misc::pkg_freqs();
910 Returns an english representation of the I<freq> field, such as "monthly",
911 "weekly", "semi-annually", etc.
917 my $freq = $self->freq;
919 #my $freqs_href = $self->freqs_href;
920 my $freqs_href = freqs_href();
922 if ( exists($freqs_href->{$freq}) ) {
923 $freqs_href->{$freq};
925 my $interval = 'month';
926 if ( $freq =~ /^(\d+)([hdw])$/ ) {
927 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
928 $interval = $interval{$2};
933 "every $freq ${interval}s";
938 =item add_freq TIMESTAMP [ FREQ ]
940 Adds a billing period of some frequency to the provided timestamp and
941 returns the resulting timestamp, or -1 if the frequency could not be
942 parsed (shouldn't happen). By default, the frequency of this package
943 will be used; to override this, pass a different frequency as a second
949 my( $self, $date, $freq ) = @_;
950 $freq = $self->freq unless $freq;
952 #change this bit to use Date::Manip? CAREFUL with timezones (see
953 # mailing list archive)
954 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
956 if ( $freq =~ /^\d+$/ ) {
958 until ( $mon < 12 ) { $mon -= 12; $year++; }
960 $mday = 28 if $mday > 28 && FS::Conf->new->exists('anniversary-rollback');
962 } elsif ( $freq =~ /^(\d+)w$/ ) {
965 } elsif ( $freq =~ /^(\d+)d$/ ) {
968 } elsif ( $freq =~ /^(\d+)h$/ ) {
975 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
980 For backwards compatibility, returns the plandata field as well as all options
981 from FS::part_pkg_option.
987 carp "plandata is deprecated";
989 $self->SUPER::plandata(@_);
991 my $plandata = $self->get('plandata');
992 my %options = $self->options;
993 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
998 =item part_pkg_vendor
1000 Returns all vendor/external package ids as FS::part_pkg_vendor objects (see
1001 L<FS::part_pkg_vendor>).
1005 sub part_pkg_vendor {
1007 qsearch('part_pkg_vendor', { 'pkgpart' => $self->pkgpart } );
1010 =item vendor_pkg_ids
1012 Returns a list of vendor/external package ids by exportnum
1016 sub vendor_pkg_ids {
1018 map { $_->exportnum => $_->vendor_pkg_id } $self->part_pkg_vendor;
1021 =item part_pkg_option
1023 Returns all options as FS::part_pkg_option objects (see
1024 L<FS::part_pkg_option>).
1028 sub part_pkg_option {
1030 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
1035 Returns a list of option names and values suitable for assigning to a hash.
1041 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
1044 =item option OPTIONNAME [ QUIET ]
1046 Returns the option value for the given name, or the empty string. If a true
1047 value is passed as the second argument, warnings about missing the option
1053 my( $self, $opt, $ornull ) = @_;
1054 my $part_pkg_option =
1055 qsearchs('part_pkg_option', {
1056 pkgpart => $self->pkgpart,
1059 return $part_pkg_option->optionvalue if $part_pkg_option;
1060 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
1061 split("\n", $self->get('plandata') );
1062 return $plandata{$opt} if exists $plandata{$opt};
1063 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
1064 "not found in options or plandata!\n"
1069 =item bill_part_pkg_link
1071 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1075 sub bill_part_pkg_link {
1076 shift->_part_pkg_link('bill', @_);
1079 =item svc_part_pkg_link
1081 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1085 sub svc_part_pkg_link {
1086 shift->_part_pkg_link('svc', @_);
1089 sub _part_pkg_link {
1090 my( $self, $type ) = @_;
1091 qsearch({ table => 'part_pkg_link',
1092 hashref => { 'src_pkgpart' => $self->pkgpart,
1093 'link_type' => $type,
1094 #protection against infinite recursive links
1095 'dst_pkgpart' => { op=>'!=', value=> $self->pkgpart },
1097 order_by => "ORDER BY hidden",
1101 sub self_and_bill_linked {
1102 shift->_self_and_linked('bill', @_);
1105 sub self_and_svc_linked {
1106 shift->_self_and_linked('svc', @_);
1109 sub _self_and_linked {
1110 my( $self, $type, $hidden ) = @_;
1114 foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
1115 $self->_part_pkg_link($type) ) )
1117 $_->hidden($hidden) if $hidden;
1124 =item part_pkg_taxoverride [ CLASS ]
1126 Returns all associated FS::part_pkg_taxoverride objects (see
1127 L<FS::part_pkg_taxoverride>). Limits the returned set to those
1128 of class CLASS if defined. Class may be one of 'setup', 'recur',
1129 the empty string (default), or a usage class number (see L<FS::usage_class>).
1130 When a class is specified, the empty string class (default) is returned
1131 if no more specific values exist.
1135 sub part_pkg_taxoverride {
1139 my $hashref = { 'pkgpart' => $self->pkgpart };
1140 $hashref->{'usage_class'} = $class if defined($class);
1141 my @overrides = qsearch('part_pkg_taxoverride', $hashref );
1143 unless ( scalar(@overrides) || !defined($class) || !$class ){
1144 $hashref->{'usage_class'} = '';
1145 @overrides = qsearch('part_pkg_taxoverride', $hashref );
1151 =item has_taxproduct
1153 Returns true if this package has any taxproduct associated with it.
1157 sub has_taxproduct {
1160 $self->taxproductnum ||
1161 scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) }
1162 keys %{ {$self->options} }
1168 =item taxproduct [ CLASS ]
1170 Returns the associated tax product for this package definition (see
1171 L<FS::part_pkg_taxproduct>). CLASS may be one of 'setup', 'recur' or
1172 the usage classnum (see L<FS::usage_class>). Returns the default
1173 tax product for this record if the more specific CLASS value does
1182 my $part_pkg_taxproduct;
1184 my $taxproductnum = $self->taxproductnum;
1186 my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1187 $taxproductnum = $class_taxproductnum
1188 if $class_taxproductnum
1191 $part_pkg_taxproduct =
1192 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1194 unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1195 $taxproductnum = $self->taxproductnum;
1196 $part_pkg_taxproduct =
1197 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1200 $part_pkg_taxproduct;
1203 =item taxproduct_description [ CLASS ]
1205 Returns the description of the associated tax product for this package
1206 definition (see L<FS::part_pkg_taxproduct>).
1210 sub taxproduct_description {
1212 my $part_pkg_taxproduct = $self->taxproduct(@_);
1213 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1216 =item part_pkg_taxrate DATA_PROVIDER, GEOCODE, [ CLASS ]
1218 Returns the package to taxrate m2m records for this package in the location
1219 specified by GEOCODE (see L<FS::part_pkg_taxrate>) and usage class CLASS.
1220 CLASS may be one of 'setup', 'recur', or one of the usage classes numbers
1221 (see L<FS::usage_class>).
1225 sub _expand_cch_taxproductnum {
1228 my $part_pkg_taxproduct = $self->taxproduct($class);
1230 my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
1231 ? ( split ':', $part_pkg_taxproduct->taxproduct )
1234 $a = '' unless $a; $b = '' unless $b; $c = '' unless $c; $d = '' unless $d;
1235 my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
1236 OR taxproduct = '$a:$b:$c:'
1237 OR taxproduct = '$a:$b:".":$d'
1238 OR taxproduct = '$a:$b:".":' )";
1239 map { $_->taxproductnum } qsearch( { 'table' => 'part_pkg_taxproduct',
1240 'hashref' => { 'data_vendor'=>'cch' },
1241 'extra_sql' => $extra_sql,
1246 sub part_pkg_taxrate {
1248 my ($data_vendor, $geocode, $class) = @_;
1251 my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
1252 dbh->quote($data_vendor);
1254 # CCH oddness in m2m
1255 $extra_sql .= ' AND ('.
1256 join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) }
1260 # much more CCH oddness in m2m -- this is kludgy
1261 my @tpnums = $self->_expand_cch_taxproductnum($class);
1262 if (scalar(@tpnums)) {
1263 $extra_sql .= ' AND ('.
1264 join(' OR ', map{ "taxproductnum = $_" } @tpnums ).
1267 $extra_sql .= ' AND ( 0 = 1 )';
1270 my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
1271 my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
1272 my $select = 'DISTINCT ON(taxclassnum) *, taxproduct';
1274 # should qsearch preface columns with the table to facilitate joins?
1275 qsearch( { 'table' => 'part_pkg_taxrate',
1276 'select' => $select,
1277 'hashref' => { # 'data_vendor' => $data_vendor,
1278 # 'taxproductnum' => $self->taxproductnum,
1280 'addl_from' => $addl_from,
1281 'extra_sql' => $extra_sql,
1282 'order_by' => $order_by,
1286 =item part_pkg_discount
1288 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1293 sub part_pkg_discount {
1295 qsearch('part_pkg_discount', { 'pkgpart' => $self->pkgpart });
1300 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1301 PLAN is the object's I<plan> field. There should be better docs
1302 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1308 my $plan = $self->plan;
1310 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1314 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1315 my $class = ref($self). "::$plan";
1316 warn "reblessing $self into $class" if $DEBUG;
1319 bless($self, $class) unless $@;
1324 sub calc_setup { die 'no calc_setup for '. shift->plan. "\n"; }
1325 sub calc_recur { die 'no calc_recur for '. shift->plan. "\n"; }
1327 #fallback that return 0 for old legacy packages with no plan
1328 sub calc_remain { 0; }
1329 sub calc_units { 0; }
1331 #fallback for everything not based on flat.pm
1332 sub recur_temporality { 'upcoming'; }
1333 sub calc_cancel { 0; }
1335 #fallback for everything except bulk.pm
1336 sub hide_svc_detail { 0; }
1338 #fallback for packages that can't/won't summarize usage
1339 sub sum_usage { 0; }
1341 =item recur_cost_permonth CUST_PKG
1343 recur_cost divided by freq (only supported for monthly and longer frequencies)
1347 sub recur_cost_permonth {
1348 my($self, $cust_pkg) = @_;
1349 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1350 sprintf('%.2f', $self->recur_cost / $self->freq );
1353 =item cust_bill_pkg_recur CUST_PKG
1355 Actual recurring charge for the specified customer package from customer's most
1360 sub cust_bill_pkg_recur {
1361 my($self, $cust_pkg) = @_;
1362 my $cust_bill_pkg = qsearchs({
1363 'table' => 'cust_bill_pkg',
1364 'addl_from' => 'LEFT JOIN cust_bill USING ( invnum )',
1365 'hashref' => { 'pkgnum' => $cust_pkg->pkgnum,
1366 'recur' => { op=>'>', value=>'0' },
1368 'order_by' => 'ORDER BY cust_bill._date DESC,
1369 cust_bill_pkg.sdate DESC
1372 }) or return 0; #die "use cust_bill_pkg_recur credits with once_perinv condition";
1373 $cust_bill_pkg->recur;
1376 =item format OPTION DATA
1378 Returns data formatted according to the function 'format' described
1379 in the plan info. Returns DATA if no such function exists.
1384 my ($self, $option, $data) = (shift, shift, shift);
1385 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1386 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1392 =item parse OPTION DATA
1394 Returns data parsed according to the function 'parse' described
1395 in the plan info. Returns DATA if no such function exists.
1400 my ($self, $option, $data) = (shift, shift, shift);
1401 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1402 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1412 =head1 CLASS METHODS
1420 # Used by FS::Upgrade to migrate to a new database.
1422 sub _upgrade_data { # class method
1423 my($class, %opts) = @_;
1425 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1427 my @part_pkg = qsearch({
1428 'table' => 'part_pkg',
1429 'extra_sql' => "WHERE ". join(' OR ',
1430 ( map "($_ IS NOT NULL AND $_ != '' )",
1431 qw( plandata setup recur ) ),
1432 'plan IS NULL', "plan = '' ",
1436 foreach my $part_pkg (@part_pkg) {
1438 unless ( $part_pkg->plan ) {
1439 $part_pkg->plan('flat');
1442 if ( length($part_pkg->option('setup_fee')) == 0
1443 && $part_pkg->setup =~ /^\s*([\d\.]+)\s*$/ ) {
1445 my $opt = new FS::part_pkg_option {
1446 'pkgpart' => $part_pkg->pkgpart,
1447 'optionname' => 'setup_fee',
1448 'optionvalue' => $1,
1450 my $error = $opt->insert;
1451 die $error if $error;
1455 # die "Can't parse part_pkg.setup for fee; convert pkgnum ".
1456 # $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
1458 $part_pkg->setup('');
1460 if ( length($part_pkg->option('recur_fee')) == 0
1461 && $part_pkg->recur =~ /^\s*([\d\.]+)\s*$/ ) {
1463 my $opt = new FS::part_pkg_option {
1464 'pkgpart' => $part_pkg->pkgpart,
1465 'optionname' => 'recur_fee',
1466 'optionvalue' => $1,
1468 my $error = $opt->insert;
1469 die $error if $error;
1473 # die "Can't parse part_pkg.setup for fee; convert pkgnum ".
1474 # $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
1476 $part_pkg->recur('');
1478 $part_pkg->replace; #this should take care of plandata, right?
1482 # now upgrade to the explicit custom flag
1484 @part_pkg = qsearch({
1485 'table' => 'part_pkg',
1486 'hashref' => { disabled => 'Y', custom => '' },
1487 'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1490 foreach my $part_pkg (@part_pkg) {
1491 my $new = new FS::part_pkg { $part_pkg->hash };
1493 my $comment = $part_pkg->comment;
1494 $comment =~ s/^\(CUSTOM\) //;
1495 $comment = '(none)' unless $comment =~ /\S/;
1496 $new->comment($comment);
1498 my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1499 my $primary = $part_pkg->svcpart;
1500 my $options = { $part_pkg->options };
1502 my $error = $new->replace( $part_pkg,
1503 'pkg_svc' => $pkg_svc,
1504 'primary_svc' => $primary,
1505 'options' => $options,
1507 die $error if $error;
1510 my @part_pkg_option = qsearch('part_pkg_option',
1511 { 'optionname' => 'unused_credit',
1514 foreach my $old_opt (@part_pkg_option) {
1515 my $pkgpart = $old_opt->pkgpart;
1516 my $error = $old_opt->delete;
1517 die $error if $error;
1519 foreach (qw(unused_credit_cancel unused_credit_change)) {
1520 my $new_opt = new FS::part_pkg_option {
1521 'pkgpart' => $pkgpart,
1525 $error = $new_opt->insert;
1526 die $error if $error;
1530 # migrate use_disposition_taqua and use_disposition to disposition_in
1531 @part_pkg_option = qsearch('part_pkg_option',
1532 { 'optionname' => { op => 'LIKE',
1533 value => 'use_disposition%',
1537 my %newopts = map { $_->pkgpart => $_ }
1538 qsearch('part_pkg_option', { 'optionname' => 'disposition_in', } );
1539 foreach my $old_opt (@part_pkg_option) {
1540 my $pkgpart = $old_opt->pkgpart;
1541 my $newval = $old_opt->optionname eq 'use_disposition_taqua' ? '100'
1543 my $error = $old_opt->delete;
1544 die $error if $error;
1546 if ( exists($newopts{$pkgpart}) ) {
1547 my $opt = $newopts{$pkgpart};
1548 $opt->optionvalue($opt->optionvalue.",$newval");
1549 $error = $opt->replace;
1550 die $error if $error;
1552 my $new_opt = new FS::part_pkg_option {
1553 'pkgpart' => $pkgpart,
1554 'optionname' => 'disposition_in',
1555 'optionvalue' => $newval,
1557 $error = $new_opt->insert;
1558 die $error if $error;
1559 $newopts{$pkgpart} = $new_opt;
1563 # set any package with FCC voice lines to the "VoIP with broadband" category
1564 # for backward compatibility
1566 # recover from a bad upgrade bug
1567 my $upgrade = 'part_pkg_fcc_voip_class_FIX';
1568 if (!FS::upgrade_journal->is_done($upgrade)) {
1569 my $bad_upgrade = qsearchs('upgrade_journal',
1570 { upgrade => 'part_pkg_fcc_voip_class' }
1572 if ( $bad_upgrade ) {
1573 my $where = 'WHERE history_date <= '.$bad_upgrade->_date.
1574 ' AND history_date > '.($bad_upgrade->_date - 3600);
1575 my @h_part_pkg_option = map { FS::part_pkg_option->new($_->hashref) }
1578 'table' => 'h_part_pkg_option',
1580 'extra_sql' => "$where AND history_action = 'delete'",
1581 'order_by' => 'ORDER BY history_date ASC',
1583 my @h_pkg_svc = map { FS::pkg_svc->new($_->hashref) }
1586 'table' => 'h_pkg_svc',
1588 'extra_sql' => "$where AND history_action = 'replace_old'",
1589 'order_by' => 'ORDER BY history_date ASC',
1592 foreach my $deleted (@h_part_pkg_option, @h_pkg_svc) {
1593 my $pkgpart ||= $deleted->pkgpart;
1594 $opt{$pkgpart} ||= {
1600 if ( $deleted->isa('FS::part_pkg_option') ) {
1601 $opt{$pkgpart}{options}{ $deleted->optionname } = $deleted->optionvalue;
1603 my $svcpart = $deleted->svcpart;
1604 $opt{$pkgpart}{pkg_svc}{$svcpart} = $deleted->quantity;
1605 $opt{$pkgpart}{hidden_svc}{$svcpart} ||= $deleted->hidden;
1606 $opt{$pkgpart}{primary_svc} = $svcpart if $deleted->primary_svc;
1609 foreach my $pkgpart (keys %opt) {
1610 my $part_pkg = FS::part_pkg->by_key($pkgpart);
1611 my $error = $part_pkg->replace( $part_pkg->replace_old, $opt{$pkgpart} );
1613 die "error recovering damaged pkgpart $pkgpart:\n$error\n";
1616 } # $bad_upgrade exists
1617 else { # do the original upgrade, but correctly this time
1618 @part_pkg = qsearch('part_pkg', {
1619 fcc_ds0s => { op => '>', value => 0 },
1620 fcc_voip_class => ''
1622 foreach my $part_pkg (@part_pkg) {
1623 $part_pkg->set(fcc_voip_class => 2);
1624 my @pkg_svc = $part_pkg->pkg_svc;
1625 my %quantity = map {$_->svcpart, $_->quantity} @pkg_svc;
1626 my %hidden = map {$_->svcpart, $_->hidden } @pkg_svc;
1627 my $error = $part_pkg->replace(
1628 $part_pkg->replace_old,
1629 options => { $part_pkg->options },
1630 pkg_svc => \%quantity,
1631 hidden_svc => \%hidden,
1632 primary_svc => ($part_pkg->svcpart || ''),
1634 die $error if $error;
1637 FS::upgrade_journal->set_done($upgrade);
1642 =item curuser_pkgs_sql
1644 Returns an SQL fragment for searching for packages the current user can
1645 use, either via part_pkg.agentnum directly, or via agent type (see
1650 sub curuser_pkgs_sql {
1653 $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
1657 =item agent_pkgs_sql AGENT | AGENTNUM, ...
1659 Returns an SQL fragment for searching for packages the provided agent or agents
1660 can use, either via part_pkg.agentnum directly, or via agent type (see
1665 sub agent_pkgs_sql {
1666 my $class = shift; #i'm a class method, not a sub (the question is... why??)
1667 my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
1669 $class->_pkgs_sql(@agentnums); #is this why
1674 my( $class, @agentnums ) = @_;
1675 my $agentnums = join(',', @agentnums);
1679 ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
1680 OR ( agentnum IS NULL
1681 AND EXISTS ( SELECT 1
1683 LEFT JOIN agent_type USING ( typenum )
1684 LEFT JOIN agent AS typeagent USING ( typenum )
1685 WHERE type_pkgs.pkgpart = part_pkg.pkgpart
1686 AND typeagent.agentnum IN ($agentnums)
1704 #false laziness w/part_export & cdr
1706 foreach my $INC ( @INC ) {
1707 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1708 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1709 warn "attempting to load plan info from $file\n" if $DEBUG;
1710 $file =~ /\/(\w+)\.pm$/ or do {
1711 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1715 my $info = eval "use FS::part_pkg::$mod; ".
1716 "\\%FS::part_pkg::$mod\::info;";
1718 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1721 unless ( keys %$info ) {
1722 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
1725 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1726 #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1727 # warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1730 $info{$mod} = $info;
1731 $info->{'weight'} ||= 0; # quiet warnings
1735 # copy one level deep to allow replacement of fields and fieldorder
1736 tie %plans, 'Tie::IxHash',
1737 map { my %infohash = %{ $info{$_} };
1739 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1742 # inheritance of plan options
1743 foreach my $name (keys(%info)) {
1744 if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
1745 warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
1746 delete $plans{$name};
1749 my $parents = $info{$name}->{'inherit_fields'} || [];
1750 my (%fields, %field_exists, @fieldorder);
1751 foreach my $parent ($name, @$parents) {
1752 if ( !exists($info{$parent}) ) {
1753 warn "$name tried to inherit from nonexistent '$parent'\n";
1756 %fields = ( # avoid replacing existing fields
1757 %{ $info{$parent}->{'fields'} || {} },
1760 foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
1762 next if $field_exists{$_};
1763 $field_exists{$_} = 1;
1764 # allow inheritors to remove inherited fields from the fieldorder
1765 push @fieldorder, $_ if !exists($fields{$_}) or
1766 !exists($fields{$_}->{'disabled'});
1769 $plans{$name}->{'fields'} = \%fields;
1770 $plans{$name}->{'fieldorder'} = \@fieldorder;
1780 =head1 NEW PLAN CLASSES
1782 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
1783 found in eg/plan_template.pm. Until then, it is suggested that you use the
1784 other modules in FS/FS/part_pkg/ as a guide.
1788 The delete method is unimplemented.
1790 setup and recur semantics are not yet defined (and are implemented in
1791 FS::cust_bill. hmm.). now they're deprecated and need to go.
1795 part_pkg_taxrate is Pg specific
1797 replace should be smarter about managing the related tables (options, pkg_svc)
1801 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1802 schema.html from the base documentation.