2 use base qw( FS::m2m_Common FS::o2m_Common FS::option_Common );
5 use vars qw( %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
6 use Carp qw(carp cluck confess);
7 use Scalar::Util qw( blessed );
9 use Time::Local qw( timelocal timelocal_nocheck ); # eventually replace with DateTime
12 use FS::Record qw( qsearch qsearchs dbh dbdef );
18 use FS::part_pkg_option;
21 use FS::part_pkg_msgcat;
22 use FS::part_pkg_taxrate;
23 use FS::part_pkg_taxoverride;
24 use FS::part_pkg_taxproduct;
25 use FS::part_pkg_link;
26 use FS::part_pkg_discount;
27 use FS::part_pkg_usage;
28 use FS::part_pkg_vendor;
32 $skip_pkg_svc_hack = 0;
36 FS::part_pkg - Object methods for part_pkg objects
42 $record = new FS::part_pkg \%hash
43 $record = new FS::part_pkg { 'column' => 'value' };
45 $custom_record = $template_record->clone;
47 $error = $record->insert;
49 $error = $new_record->replace($old_record);
51 $error = $record->delete;
53 $error = $record->check;
55 @pkg_svc = $record->pkg_svc;
57 $svcnum = $record->svcpart;
58 $svcnum = $record->svcpart( 'svc_acct' );
62 An FS::part_pkg object represents a package definition. FS::part_pkg
63 inherits from FS::Record. The following fields are currently supported:
67 =item pkgpart - primary key (assigned automatically for new package definitions)
69 =item pkg - Text name of this package definition (customer-viewable)
71 =item comment - Text name of this package definition (non-customer-viewable)
73 =item classnum - Optional package class (see L<FS::pkg_class>)
75 =item promo_code - Promotional code
77 =item setup - Setup fee expression (deprecated)
79 =item freq - Frequency of recurring fee
81 =item recur - Recurring fee expression (deprecated)
83 =item setuptax - Setup fee tax exempt flag, empty or `Y'
85 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
87 =item taxclass - Tax class
89 =item plan - Price plan
91 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
93 =item disabled - Disabled flag, empty or `Y'
95 =item custom - Custom flag, empty or `Y'
97 =item setup_cost - for cost tracking
99 =item recur_cost - for cost tracking
101 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
103 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
105 =item agentnum - Optional agentnum (see L<FS::agent>)
107 =item fcc_ds0s - Optional DS0 equivalency number for FCC form 477
109 =item fcc_voip_class - Which column of FCC form 477 part II.B this package
112 =item successor - Foreign key for the part_pkg that replaced this record.
113 If this record is not obsolete, will be null.
115 =item family_pkgpart - Foreign key for the part_pkg that was the earliest
116 ancestor of this record. If this record is not a successor to another
117 part_pkg, will be equal to pkgpart.
119 =item delay_start - Number of days to delay package start, by default
129 Creates a new package definition. To add the package definition to
130 the database, see L<"insert">.
134 sub table { 'part_pkg'; }
138 An alternate constructor. Creates a new package definition by duplicating
139 an existing definition. A new pkgpart is assigned and the custom flag is
140 set to Y. To add the package definition to the database, see L<"insert">.
146 my $class = ref($self);
147 my %hash = $self->hash;
148 $hash{'pkgpart'} = '';
149 $hash{'custom'} = 'Y';
150 #new FS::part_pkg ( \%hash ); # ?
151 new $class ( \%hash ); # ?
154 =item insert [ , OPTION => VALUE ... ]
156 Adds this package definition to the database. If there is an error,
157 returns the error, otherwise returns false.
159 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
160 I<custnum_ref> and I<options>.
162 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
163 values, appropriate FS::pkg_svc records will be inserted. I<hidden_svc> can
164 be set to a hashref of svcparts and flag values ('Y' or '') to set the
165 'hidden' field in these records.
167 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
168 FS::pkg_svc record will be updated.
170 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
171 record itself), the object will be updated to point to this package definition.
173 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
174 the scalar will be updated with the custnum value from the cust_pkg record.
176 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
177 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
178 records will be inserted.
180 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
181 records will be inserted.
188 warn "FS::part_pkg::insert called on $self with options ".
189 join(', ', map "$_=>$options{$_}", keys %options)
192 local $SIG{HUP} = 'IGNORE';
193 local $SIG{INT} = 'IGNORE';
194 local $SIG{QUIT} = 'IGNORE';
195 local $SIG{TERM} = 'IGNORE';
196 local $SIG{TSTP} = 'IGNORE';
197 local $SIG{PIPE} = 'IGNORE';
199 my $oldAutoCommit = $FS::UID::AutoCommit;
200 local $FS::UID::AutoCommit = 0;
203 warn " inserting part_pkg record" if $DEBUG;
204 my $error = $self->SUPER::insert( $options{options} );
206 $dbh->rollback if $oldAutoCommit;
211 if ( $self->get('family_pkgpart') eq '' ) {
212 $self->set('family_pkgpart' => $self->pkgpart);
213 $error = $self->SUPER::replace;
215 $dbh->rollback if $oldAutoCommit;
220 warn " inserting part_pkg_taxoverride records" if $DEBUG;
221 my %overrides = %{ $options{'tax_overrides'} || {} };
222 foreach my $usage_class ( keys %overrides ) {
224 ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
225 ? $overrides{$usage_class}
227 my @overrides = (grep "$_", split(',', $override) );
228 my $error = $self->process_m2m (
229 'link_table' => 'part_pkg_taxoverride',
230 'target_table' => 'tax_class',
231 'hashref' => { 'usage_class' => $usage_class },
232 'params' => \@overrides,
235 $dbh->rollback if $oldAutoCommit;
240 unless ( $skip_pkg_svc_hack ) {
242 warn " inserting pkg_svc records" if $DEBUG;
243 my $pkg_svc = $options{'pkg_svc'} || {};
244 my $hidden_svc = $options{'hidden_svc'} || {};
245 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
246 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
248 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
252 my $pkg_svc = new FS::pkg_svc( {
253 'pkgpart' => $self->pkgpart,
254 'svcpart' => $part_svc->svcpart,
255 'quantity' => $quantity,
256 'primary_svc' => $primary_svc,
257 'hidden' => $hidden_svc->{$part_svc->svcpart},
259 my $error = $pkg_svc->insert;
261 $dbh->rollback if $oldAutoCommit;
268 if ( $options{'cust_pkg'} ) {
269 warn " updating cust_pkg record " if $DEBUG;
271 ref($options{'cust_pkg'})
272 ? $options{'cust_pkg'}
273 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
274 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
275 if $options{'custnum_ref'};
276 my %hash = $old_cust_pkg->hash;
277 $hash{'pkgpart'} = $self->pkgpart,
278 my $new_cust_pkg = new FS::cust_pkg \%hash;
279 local($FS::cust_pkg::disable_agentcheck) = 1;
280 my $error = $new_cust_pkg->replace($old_cust_pkg);
282 $dbh->rollback if $oldAutoCommit;
283 return "Error modifying cust_pkg record: $error";
287 if ( $options{'part_pkg_vendor'} ) {
288 while ( my ($exportnum, $vendor_pkg_id) =
289 each %{ $options{part_pkg_vendor} }
292 my $ppv = new FS::part_pkg_vendor( {
293 'pkgpart' => $self->pkgpart,
294 'exportnum' => $exportnum,
295 'vendor_pkg_id' => $vendor_pkg_id,
297 my $error = $ppv->insert;
299 $dbh->rollback if $oldAutoCommit;
300 return "Error inserting part_pkg_vendor record: $error";
305 warn " committing transaction" if $DEBUG and $oldAutoCommit;
306 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
313 Currently unimplemented.
318 return "Can't (yet?) delete package definitions.";
319 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
322 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
324 Replaces OLD_RECORD with this one in the database. If there is an error,
325 returns the error, otherwise returns false.
327 Currently available options are: I<pkg_svc>, I<hidden_svc>, I<primary_svc>
330 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
331 values, the appropriate FS::pkg_svc records will be replaced. I<hidden_svc>
332 can be set to a hashref of svcparts and flag values ('Y' or '') to set the
333 'hidden' field in these records.
335 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
336 FS::pkg_svc record will be updated.
338 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
346 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
351 ( ref($_[0]) eq 'HASH' )
355 $options->{options} = { $old->options } unless defined($options->{options});
357 warn "FS::part_pkg::replace called on $new to replace $old with options".
358 join(', ', map "$_ => ". $options->{$_}, keys %$options)
361 local $SIG{HUP} = 'IGNORE';
362 local $SIG{INT} = 'IGNORE';
363 local $SIG{QUIT} = 'IGNORE';
364 local $SIG{TERM} = 'IGNORE';
365 local $SIG{TSTP} = 'IGNORE';
366 local $SIG{PIPE} = 'IGNORE';
368 my $oldAutoCommit = $FS::UID::AutoCommit;
369 local $FS::UID::AutoCommit = 0;
372 my $conf = new FS::Conf;
373 if ( $conf->exists('part_pkg-lineage') ) {
374 if ( grep { $options->{options}->{$_} ne $old->option($_, 1) }
375 qw(setup_fee recur_fee) #others? config?
378 warn " superseding package" if $DEBUG;
380 my $error = $new->supersede($old, %$options);
382 $dbh->rollback if $oldAutoCommit;
386 warn " committing transaction" if $DEBUG and $oldAutoCommit;
387 $dbh->commit if $oldAutoCommit;
394 #plandata shit stays in replace for upgrades until after 2.0 (or edit
396 warn " saving legacy plandata" if $DEBUG;
397 my $plandata = $new->get('plandata');
398 $new->set('plandata', '');
400 warn " deleting old part_pkg_option records" if $DEBUG;
401 foreach my $part_pkg_option ( $old->part_pkg_option ) {
402 my $error = $part_pkg_option->delete;
404 $dbh->rollback if $oldAutoCommit;
409 warn " replacing part_pkg record" if $DEBUG;
410 my $error = $new->SUPER::replace($old, $options->{options} );
412 $dbh->rollback if $oldAutoCommit;
416 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
417 foreach my $part_pkg_option (
418 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
419 return "illegal plandata: $plandata";
421 new FS::part_pkg_option {
422 'pkgpart' => $new->pkgpart,
427 split("\n", $plandata)
429 my $error = $part_pkg_option->insert;
431 $dbh->rollback if $oldAutoCommit;
436 warn " replacing pkg_svc records" if $DEBUG;
437 my $pkg_svc = $options->{'pkg_svc'};
438 my $hidden_svc = $options->{'hidden_svc'} || {};
439 if ( $pkg_svc ) { # if it wasn't passed, don't change existing pkg_svcs
440 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
441 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
442 my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
444 ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
445 && $options->{'primary_svc'} == $part_svc->svcpart
450 my $old_pkg_svc = qsearchs('pkg_svc', {
451 'pkgpart' => $old->pkgpart,
452 'svcpart' => $part_svc->svcpart,
455 my $old_quantity = 0;
456 my $old_primary_svc = '';
458 if ( $old_pkg_svc ) {
459 $old_quantity = $old_pkg_svc->quantity;
460 $old_primary_svc = $old_pkg_svc->primary_svc
461 if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
462 $old_hidden = $old_pkg_svc->hidden;
465 next unless $old_quantity != $quantity ||
466 $old_primary_svc ne $primary_svc ||
467 $old_hidden ne $hidden;
469 my $new_pkg_svc = new FS::pkg_svc( {
470 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
471 'pkgpart' => $new->pkgpart,
472 'svcpart' => $part_svc->svcpart,
473 'quantity' => $quantity,
474 'primary_svc' => $primary_svc,
477 my $error = $old_pkg_svc
478 ? $new_pkg_svc->replace($old_pkg_svc)
479 : $new_pkg_svc->insert;
481 $dbh->rollback if $oldAutoCommit;
485 } #if $options->{pkg_svc}
487 my @part_pkg_vendor = $old->part_pkg_vendor;
488 my @current_exportnum = ();
489 if ( $options->{'part_pkg_vendor'} ) {
490 my($exportnum,$vendor_pkg_id);
491 while ( ($exportnum,$vendor_pkg_id)
492 = each %{$options->{'part_pkg_vendor'}} ) {
494 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
495 if($exportnum == $part_pkg_vendor->exportnum
496 && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
497 $part_pkg_vendor->vendor_pkg_id($vendor_pkg_id);
498 my $error = $part_pkg_vendor->replace;
500 $dbh->rollback if $oldAutoCommit;
501 return "Error replacing part_pkg_vendor record: $error";
506 elsif($exportnum == $part_pkg_vendor->exportnum
507 && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
512 unless ( $noinsert ) {
513 my $ppv = new FS::part_pkg_vendor( {
514 'pkgpart' => $new->pkgpart,
515 'exportnum' => $exportnum,
516 'vendor_pkg_id' => $vendor_pkg_id,
518 my $error = $ppv->insert;
520 $dbh->rollback if $oldAutoCommit;
521 return "Error inserting part_pkg_vendor record: $error";
524 push @current_exportnum, $exportnum;
527 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
528 unless ( grep($_ eq $part_pkg_vendor->exportnum, @current_exportnum) ) {
529 my $error = $part_pkg_vendor->delete;
531 $dbh->rollback if $oldAutoCommit;
532 return "Error deleting part_pkg_vendor record: $error";
537 # propagate changes to certain core fields
538 if ( $conf->exists('part_pkg-lineage') ) {
539 warn " propagating changes to family" if $DEBUG;
540 my $error = $new->propagate($old);
542 $dbh->rollback if $oldAutoCommit;
547 warn " committing transaction" if $DEBUG and $oldAutoCommit;
548 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
554 Checks all fields to make sure this is a valid package definition. If
555 there is an error, returns the error, otherwise returns false. Called by the
556 insert and replace methods.
562 warn "FS::part_pkg::check called on $self" if $DEBUG;
564 for (qw(setup recur plandata)) {
565 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
566 return "Use of $_ field is deprecated; set a plan and options: ".
568 if length($self->get($_));
572 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
573 my $error = $self->ut_number('freq');
574 return $error if $error;
576 $self->freq =~ /^(\d+[hdw]?)$/
577 or return "Illegal or empty freq: ". $self->freq;
581 my @null_agentnum_right = ( 'Edit global package definitions' );
582 push @null_agentnum_right, 'One-time charge'
583 if $self->freq =~ /^0/;
584 push @null_agentnum_right, 'Customize customer package'
585 if $self->disabled eq 'Y'; #good enough
587 my $error = $self->ut_numbern('pkgpart')
588 || $self->ut_text('pkg')
589 || $self->ut_textn('comment')
590 || $self->ut_textn('promo_code')
591 || $self->ut_alphan('plan')
592 || $self->ut_enum('setuptax', [ '', 'Y' ] )
593 || $self->ut_enum('recurtax', [ '', 'Y' ] )
594 || $self->ut_textn('taxclass')
595 || $self->ut_enum('disabled', [ '', 'Y' ] )
596 || $self->ut_enum('custom', [ '', 'Y' ] )
597 || $self->ut_enum('no_auto', [ '', 'Y' ])
598 || $self->ut_enum('recur_show_zero', [ '', 'Y' ])
599 || $self->ut_enum('setup_show_zero', [ '', 'Y' ])
600 #|| $self->ut_moneyn('setup_cost')
601 #|| $self->ut_moneyn('recur_cost')
602 || $self->ut_floatn('setup_cost')
603 || $self->ut_floatn('recur_cost')
604 || $self->ut_floatn('pay_weight')
605 || $self->ut_floatn('credit_weight')
606 || $self->ut_numbern('taxproductnum')
607 || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum')
608 || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
609 || $self->ut_foreign_keyn('taxproductnum',
610 'part_pkg_taxproduct',
614 ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
615 : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
617 || $self->ut_numbern('fcc_ds0s')
618 || $self->ut_numbern('fcc_voip_class')
619 || $self->ut_numbern('delay_start')
620 || $self->ut_foreign_keyn('successor', 'part_pkg', 'pkgpart')
621 || $self->ut_foreign_keyn('family_pkgpart', 'part_pkg', 'pkgpart')
622 || $self->ut_alphan('agent_pkgpartid')
623 || $self->SUPER::check
625 return $error if $error;
627 return 'Unknown plan '. $self->plan
628 unless exists($plans{$self->plan});
630 my $conf = new FS::Conf;
631 return 'Taxclass is required'
632 if ! $self->taxclass && $conf->exists('require_taxclasses');
637 =item supersede OLD [, OPTION => VALUE ... ]
639 Inserts this package as a successor to the package OLD. All options are as
640 for C<insert>. After inserting, disables OLD and sets the new package as its
646 my ($new, $old, %options) = @_;
649 $new->set('pkgpart' => '');
650 $new->set('family_pkgpart' => $old->family_pkgpart);
651 warn " inserting successor package\n" if $DEBUG;
652 $error = $new->insert(%options);
653 return $error if $error;
655 warn " disabling superseded package\n" if $DEBUG;
656 $old->set('successor' => $new->pkgpart);
657 $old->set('disabled' => 'Y');
658 $error = $old->SUPER::replace; # don't change its options/pkg_svc records
659 return $error if $error;
661 warn " propagating changes to family" if $DEBUG;
662 $new->propagate($old);
667 If any of certain fields have changed from OLD to this package, then,
668 for all packages in the same lineage as this one, sets those fields
669 to their values in this package.
673 my @propagate_fields = (
674 qw( pkg classnum setup_cost recur_cost taxclass
675 setuptax recurtax pay_weight credit_weight
683 map { $_ => $new->get($_) }
684 grep { $new->get($_) ne $old->get($_) }
688 my @part_pkg = qsearch('part_pkg', {
689 'family_pkgpart' => $new->family_pkgpart
692 foreach my $part_pkg ( @part_pkg ) {
693 my $pkgpart = $part_pkg->pkgpart;
694 next if $pkgpart == $new->pkgpart; # don't modify $new
695 warn " propagating to pkgpart $pkgpart\n" if $DEBUG;
696 foreach ( keys %fields ) {
697 $part_pkg->set($_, $fields{$_});
699 # SUPER::replace to avoid changing non-core fields
700 my $error = $part_pkg->SUPER::replace;
701 push @error, "pkgpart $pkgpart: $error"
707 =item pkg_locale LOCALE
709 Returns a customer-viewable string representing this package for the given
710 locale, from the part_pkg_msgcat table. If the given locale is empty or no
711 localized string is found, returns the base pkg field.
716 my( $self, $locale ) = @_;
717 return $self->pkg unless $locale;
718 my $part_pkg_msgcat = $self->part_pkg_msgcat($locale) or return $self->pkg;
719 $part_pkg_msgcat->pkg;
722 =item part_pkg_msgcat LOCALE
724 Like pkg_locale, but returns the FS::part_pkg_msgcat object itself.
728 sub part_pkg_msgcat {
729 my( $self, $locale ) = @_;
730 qsearchs( 'part_pkg_msgcat', {
731 pkgpart => $self->pkgpart,
736 =item pkg_comment [ OPTION => VALUE... ]
738 Returns an (internal) string representing this package. Currently,
739 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
740 future, omitting pkgpart. The comment will have '(CUSTOM) ' prepended if
743 If the option nopkgpart is true then the "pkgpart: ' is omitted.
751 #$self->pkg. ' - '. $self->comment;
752 #$self->pkg. ' ('. $self->comment. ')';
753 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
754 my $custom_comment = $self->custom_comment(%opt);
755 $pre. $self->pkg. ( $custom_comment ? " - $custom_comment" : '' );
758 #without price info (so without hitting the DB again)
759 sub pkg_comment_only {
763 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
764 my $comment = $self->comment;
765 $pre. $self->pkg. ( $comment ? " - $comment" : '' );
768 sub price_info { # safety, in case a part_pkg hasn't defined price_info
774 my $price_info = $self->price_info(@_);
775 ( $self->custom ? '(CUSTOM) ' : '' ).
777 ( ( ($self->custom || $self->comment) && $price_info ) ? ' - ' : '' ).
783 $self->pkg. ' - '. ($self->price_info || 'No charge');
788 Returns the package class, as an FS::pkg_class object, or the empty string
789 if there is no package class.
795 if ( $self->classnum ) {
796 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
802 =item addon_pkg_class
804 Returns the add-on package class, as an FS::pkg_class object, or the empty
805 string if there is no add-on package class.
809 sub addon_pkg_class {
811 if ( $self->addon_classnum ) {
812 qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
820 Returns the package category name, or the empty string if there is no package
827 my $pkg_class = $self->pkg_class;
829 ? $pkg_class->categoryname
835 Returns the package class name, or the empty string if there is no package
842 my $pkg_class = $self->pkg_class;
844 ? $pkg_class->classname
848 =item addon_classname
850 Returns the add-on package class name, or the empty string if there is no
851 add-on package class.
855 sub addon_classname {
857 my $pkg_class = $self->addon_pkg_class;
859 ? $pkg_class->classname
865 Returns the associated agent for this event, if any, as an FS::agent object.
871 qsearchs('agent', { 'agentnum' => $self->agentnum } );
874 =item pkg_svc [ HASHREF | OPTION => VALUE ]
876 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
877 definition (with non-zero quantity).
879 One option is available, I<disable_linked>. If set true it will return the
880 services for this package definition alone, omitting services from any add-on
887 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
894 qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
900 # #sort { $b->primary cmp $a->primary }
901 # grep { $_->quantity }
902 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
904 my $opt = ref($_[0]) ? $_[0] : { @_ };
905 my %pkg_svc = map { $_->svcpart => $_ }
906 grep { $_->quantity }
907 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
909 unless ( $opt->{disable_linked} ) {
910 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
911 my @pkg_svc = grep { $_->quantity }
912 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
913 foreach my $pkg_svc ( @pkg_svc ) {
914 if ( $pkg_svc{$pkg_svc->svcpart} ) {
915 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
916 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
918 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
928 =item svcpart [ SVCDB ]
930 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
931 associated with this package definition (see L<FS::pkg_svc>). Returns
932 false if there not a primary service definition or exactly one service
933 definition with quantity 1, or if SVCDB is specified and does not match the
934 svcdb of the service definition. SVCDB can be specified as a scalar table
935 name, such as 'svc_acct', or as an arrayref of possible table names.
940 my $pkg_svc = shift->_primary_pkg_svc(@_);
941 $pkg_svc ? $pkg_svc->svcpart : '';
944 =item part_svc [ SVCDB ]
946 Like the B<svcpart> method, but returns the FS::part_svc object (see
952 my $pkg_svc = shift->_primary_pkg_svc(@_);
953 $pkg_svc ? $pkg_svc->part_svc : '';
956 sub _primary_pkg_svc {
959 my $svcdb = scalar(@_) ? shift : [];
960 $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
961 my %svcdb = map { $_=>1 } @$svcdb;
964 grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
967 my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
968 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
970 return '' if scalar(@pkg_svc) != 1;
974 =item svcpart_unique_svcdb SVCDB
976 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
977 SVCDB associated with this package definition (see L<FS::pkg_svc>). Returns
978 false if there not a primary service definition for SVCDB or there are multiple
979 service definitions for SVCDB.
983 sub svcpart_unique_svcdb {
984 my( $self, $svcdb ) = @_;
985 my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
986 return '' if scalar(@svcdb_pkg_svc) != 1;
987 $svcdb_pkg_svc[0]->svcpart;
992 Returns a list of the acceptable payment types for this package. Eventually
993 this should come out of a database table and be editable, but currently has the
994 following logic instead:
996 If the package is free, the single item B<BILL> is
997 returned, otherwise, the single item B<CARD> is returned.
999 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
1005 if ( $self->is_free ) {
1014 Returns true if this package is free.
1020 if ( $self->can('is_free_options') ) {
1021 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
1022 map { $self->option($_) }
1023 $self->is_free_options;
1025 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
1026 "provides neither is_free_options nor is_free method; returning false";
1031 # whether the plan allows discounts to be applied to this package
1032 sub can_discount { 0; }
1034 # whether the plan allows changing the start date
1035 sub can_start_date { 1; }
1037 # the delay start date if present
1038 sub delay_start_date {
1041 my $delay = $self->delay_start or return '';
1043 # avoid timelocal silliness
1044 my $dt = DateTime->today(time_zone => 'local');
1045 $dt->add(days => $delay);
1050 # moved to FS::Misc to make this accessible to other packages
1052 FS::Misc::pkg_freqs();
1057 Returns an english representation of the I<freq> field, such as "monthly",
1058 "weekly", "semi-annually", etc.
1064 my $freq = $self->freq;
1066 #my $freqs_href = $self->freqs_href;
1067 my $freqs_href = freqs_href();
1069 if ( exists($freqs_href->{$freq}) ) {
1070 $freqs_href->{$freq};
1072 my $interval = 'month';
1073 if ( $freq =~ /^(\d+)([hdw])$/ ) {
1074 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
1075 $interval = $interval{$2};
1080 "every $freq ${interval}s";
1085 =item add_freq TIMESTAMP [ FREQ ]
1087 Adds a billing period of some frequency to the provided timestamp and
1088 returns the resulting timestamp, or -1 if the frequency could not be
1089 parsed (shouldn't happen). By default, the frequency of this package
1090 will be used; to override this, pass a different frequency as a second
1096 my( $self, $date, $freq ) = @_;
1097 $freq = $self->freq unless $freq;
1099 #change this bit to use Date::Manip? CAREFUL with timezones (see
1100 # mailing list archive)
1101 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
1103 if ( $freq =~ /^\d+$/ ) {
1105 until ( $mon < 12 ) { $mon -= 12; $year++; }
1107 $mday = 28 if $mday > 28 && FS::Conf->new->exists('anniversary-rollback');
1109 } elsif ( $freq =~ /^(\d+)w$/ ) {
1111 $mday += $weeks * 7;
1112 } elsif ( $freq =~ /^(\d+)d$/ ) {
1115 } elsif ( $freq =~ /^(\d+)h$/ ) {
1122 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
1127 For backwards compatibility, returns the plandata field as well as all options
1128 from FS::part_pkg_option.
1134 carp "plandata is deprecated";
1136 $self->SUPER::plandata(@_);
1138 my $plandata = $self->get('plandata');
1139 my %options = $self->options;
1140 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
1145 =item part_pkg_vendor
1147 Returns all vendor/external package ids as FS::part_pkg_vendor objects (see
1148 L<FS::part_pkg_vendor>).
1152 sub part_pkg_vendor {
1154 qsearch('part_pkg_vendor', { 'pkgpart' => $self->pkgpart } );
1157 =item vendor_pkg_ids
1159 Returns a list of vendor/external package ids by exportnum
1163 sub vendor_pkg_ids {
1165 map { $_->exportnum => $_->vendor_pkg_id } $self->part_pkg_vendor;
1168 =item part_pkg_option
1170 Returns all options as FS::part_pkg_option objects (see
1171 L<FS::part_pkg_option>).
1175 sub part_pkg_option {
1177 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
1182 Returns a list of option names and values suitable for assigning to a hash.
1188 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
1191 =item option OPTIONNAME [ QUIET ]
1193 Returns the option value for the given name, or the empty string. If a true
1194 value is passed as the second argument, warnings about missing the option
1200 my( $self, $opt, $ornull ) = @_;
1201 cluck "$self -> option: searching for $opt"
1203 my $part_pkg_option =
1204 qsearchs('part_pkg_option', {
1205 pkgpart => $self->pkgpart,
1208 return $part_pkg_option->optionvalue if $part_pkg_option;
1209 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
1210 split("\n", $self->get('plandata') );
1211 return $plandata{$opt} if exists $plandata{$opt};
1212 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
1213 "not found in options or plandata!\n"
1218 =item bill_part_pkg_link
1220 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1224 sub bill_part_pkg_link {
1225 shift->_part_pkg_link('bill', @_);
1228 =item svc_part_pkg_link
1230 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1234 sub svc_part_pkg_link {
1235 shift->_part_pkg_link('svc', @_);
1238 =item supp_part_pkg_link
1240 Returns the associated part_pkg_link records of type 'supp' (supplemental
1245 sub supp_part_pkg_link {
1246 shift->_part_pkg_link('supp', @_);
1249 sub _part_pkg_link {
1250 my( $self, $type ) = @_;
1251 qsearch({ table => 'part_pkg_link',
1252 hashref => { 'src_pkgpart' => $self->pkgpart,
1253 'link_type' => $type,
1254 #protection against infinite recursive links
1255 'dst_pkgpart' => { op=>'!=', value=> $self->pkgpart },
1257 order_by => "ORDER BY hidden",
1261 sub self_and_bill_linked {
1262 shift->_self_and_linked('bill', @_);
1265 sub self_and_svc_linked {
1266 shift->_self_and_linked('svc', @_);
1269 sub _self_and_linked {
1270 my( $self, $type, $hidden ) = @_;
1274 foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
1275 $self->_part_pkg_link($type) ) )
1277 $_->hidden($hidden) if $hidden;
1284 =item part_pkg_taxoverride [ CLASS ]
1286 Returns all associated FS::part_pkg_taxoverride objects (see
1287 L<FS::part_pkg_taxoverride>). Limits the returned set to those
1288 of class CLASS if defined. Class may be one of 'setup', 'recur',
1289 the empty string (default), or a usage class number (see L<FS::usage_class>).
1290 When a class is specified, the empty string class (default) is returned
1291 if no more specific values exist.
1295 sub part_pkg_taxoverride {
1299 my $hashref = { 'pkgpart' => $self->pkgpart };
1300 $hashref->{'usage_class'} = $class if defined($class);
1301 my @overrides = qsearch('part_pkg_taxoverride', $hashref );
1303 unless ( scalar(@overrides) || !defined($class) || !$class ){
1304 $hashref->{'usage_class'} = '';
1305 @overrides = qsearch('part_pkg_taxoverride', $hashref );
1311 =item has_taxproduct
1313 Returns true if this package has any taxproduct associated with it.
1317 sub has_taxproduct {
1320 $self->taxproductnum ||
1321 scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) }
1322 keys %{ {$self->options} }
1328 =item taxproduct [ CLASS ]
1330 Returns the associated tax product for this package definition (see
1331 L<FS::part_pkg_taxproduct>). CLASS may be one of 'setup', 'recur' or
1332 the usage classnum (see L<FS::usage_class>). Returns the default
1333 tax product for this record if the more specific CLASS value does
1342 my $part_pkg_taxproduct;
1344 my $taxproductnum = $self->taxproductnum;
1346 my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1347 $taxproductnum = $class_taxproductnum
1348 if $class_taxproductnum
1351 $part_pkg_taxproduct =
1352 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1354 unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1355 $taxproductnum = $self->taxproductnum;
1356 $part_pkg_taxproduct =
1357 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1360 $part_pkg_taxproduct;
1363 =item taxproduct_description [ CLASS ]
1365 Returns the description of the associated tax product for this package
1366 definition (see L<FS::part_pkg_taxproduct>).
1370 sub taxproduct_description {
1372 my $part_pkg_taxproduct = $self->taxproduct(@_);
1373 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1377 =item tax_rates DATA_PROVIDER, GEOCODE, [ CLASS ]
1379 Returns the tax table entries (L<FS::tax_rate> objects) that apply to this
1380 package in the location specified by GEOCODE, for usage class CLASS (one of
1381 'setup', 'recur', null, or a C<usage_class> number).
1387 my ($vendor, $geocode, $class) = @_;
1388 my @taxclassnums = map { $_->taxclassnum }
1389 $self->part_pkg_taxoverride($class);
1390 if (!@taxclassnums) {
1391 my $part_pkg_taxproduct = $self->taxproduct($class);
1392 @taxclassnums = map { $_->taxclassnum }
1393 grep { $_->taxable eq 'Y' } # why do we need this?
1394 $part_pkg_taxproduct->part_pkg_taxrate($geocode);
1396 return unless @taxclassnums;
1398 warn "Found taxclassnum values of ". join(',', @taxclassnums) ."\n"
1400 my $extra_sql = "AND taxclassnum IN (". join(',', @taxclassnums) . ")";
1401 my @taxes = qsearch({ 'table' => 'tax_rate',
1402 'hashref' => { 'geocode' => $geocode,
1403 'data_vendor' => $vendor },
1404 'extra_sql' => $extra_sql,
1406 warn "Found taxes ". join(',', map {$_->taxnum} @taxes) ."\n"
1412 =item part_pkg_discount
1414 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1419 sub part_pkg_discount {
1421 qsearch('part_pkg_discount', { 'pkgpart' => $self->pkgpart });
1424 =item part_pkg_usage
1426 Returns the voice usage pools (see L<FS::part_pkg_usage>) defined for
1431 sub part_pkg_usage {
1433 qsearch('part_pkg_usage', { 'pkgpart' => $self->pkgpart });
1438 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1439 PLAN is the object's I<plan> field. There should be better docs
1440 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1446 my $plan = $self->plan;
1448 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1452 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1453 my $class = ref($self). "::$plan";
1454 warn "reblessing $self into $class" if $DEBUG > 1;
1457 bless($self, $class) unless $@;
1462 sub calc_setup { die 'no calc_setup for '. shift->plan. "\n"; }
1463 sub calc_recur { die 'no calc_recur for '. shift->plan. "\n"; }
1465 #fallback that return 0 for old legacy packages with no plan
1466 sub calc_remain { 0; }
1467 sub calc_units { 0; }
1469 #fallback for everything not based on flat.pm
1470 sub recur_temporality { 'upcoming'; }
1471 sub calc_cancel { 0; }
1473 #fallback for everything except bulk.pm
1474 sub hide_svc_detail { 0; }
1476 #fallback for packages that can't/won't summarize usage
1477 sub sum_usage { 0; }
1479 =item recur_cost_permonth CUST_PKG
1481 recur_cost divided by freq (only supported for monthly and longer frequencies)
1485 sub recur_cost_permonth {
1486 my($self, $cust_pkg) = @_;
1487 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1488 sprintf('%.2f', $self->recur_cost / $self->freq );
1491 =item cust_bill_pkg_recur CUST_PKG
1493 Actual recurring charge for the specified customer package from customer's most
1498 sub cust_bill_pkg_recur {
1499 my($self, $cust_pkg) = @_;
1500 my $cust_bill_pkg = qsearchs({
1501 'table' => 'cust_bill_pkg',
1502 'addl_from' => 'LEFT JOIN cust_bill USING ( invnum )',
1503 'hashref' => { 'pkgnum' => $cust_pkg->pkgnum,
1504 'recur' => { op=>'>', value=>'0' },
1506 'order_by' => 'ORDER BY cust_bill._date DESC,
1507 cust_bill_pkg.sdate DESC
1510 }) or return 0; #die "use cust_bill_pkg_recur credits with once_perinv condition";
1511 $cust_bill_pkg->recur;
1514 =item unit_setup CUST_PKG
1516 Returns the setup fee for one unit of the package.
1521 my ($self, $cust_pkg) = @_;
1522 $self->option('setup_fee') || 0;
1527 unit_setup minus setup_cost
1533 $self->unit_setup(@_) - $self->setup_cost;
1536 =item recur_margin_permonth
1538 base_recur_permonth minus recur_cost_permonth
1542 sub recur_margin_permonth {
1544 $self->base_recur_permonth(@_) - $self->recur_cost_permonth(@_);
1547 =item format OPTION DATA
1549 Returns data formatted according to the function 'format' described
1550 in the plan info. Returns DATA if no such function exists.
1555 my ($self, $option, $data) = (shift, shift, shift);
1556 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1557 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1563 =item parse OPTION DATA
1565 Returns data parsed according to the function 'parse' described
1566 in the plan info. Returns DATA if no such function exists.
1571 my ($self, $option, $data) = (shift, shift, shift);
1572 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1573 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1583 =head1 CLASS METHODS
1591 # Used by FS::Upgrade to migrate to a new database.
1593 sub _upgrade_data { # class method
1594 my($class, %opts) = @_;
1596 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1598 my @part_pkg = qsearch({
1599 'table' => 'part_pkg',
1600 'extra_sql' => "WHERE ". join(' OR ',
1601 'plan IS NULL', "plan = '' ",
1605 foreach my $part_pkg (@part_pkg) {
1607 unless ( $part_pkg->plan ) {
1608 $part_pkg->plan('flat');
1615 # now upgrade to the explicit custom flag
1617 @part_pkg = qsearch({
1618 'table' => 'part_pkg',
1619 'hashref' => { disabled => 'Y', custom => '' },
1620 'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1623 foreach my $part_pkg (@part_pkg) {
1624 my $new = new FS::part_pkg { $part_pkg->hash };
1626 my $comment = $part_pkg->comment;
1627 $comment =~ s/^\(CUSTOM\) //;
1628 $comment = '(none)' unless $comment =~ /\S/;
1629 $new->comment($comment);
1631 my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1632 my $primary = $part_pkg->svcpart;
1633 my $options = { $part_pkg->options };
1635 my $error = $new->replace( $part_pkg,
1636 'pkg_svc' => $pkg_svc,
1637 'primary_svc' => $primary,
1638 'options' => $options,
1640 die $error if $error;
1643 # set family_pkgpart on any packages that don't have it
1644 @part_pkg = qsearch('part_pkg', { 'family_pkgpart' => '' });
1645 foreach my $part_pkg (@part_pkg) {
1646 $part_pkg->set('family_pkgpart' => $part_pkg->pkgpart);
1647 my $error = $part_pkg->SUPER::replace;
1648 die $error if $error;
1651 my @part_pkg_option = qsearch('part_pkg_option',
1652 { 'optionname' => 'unused_credit',
1655 foreach my $old_opt (@part_pkg_option) {
1656 my $pkgpart = $old_opt->pkgpart;
1657 my $error = $old_opt->delete;
1658 die $error if $error;
1660 foreach (qw(unused_credit_cancel unused_credit_change)) {
1661 my $new_opt = new FS::part_pkg_option {
1662 'pkgpart' => $pkgpart,
1666 $error = $new_opt->insert;
1667 die $error if $error;
1671 # migrate use_disposition_taqua and use_disposition to disposition_in
1672 @part_pkg_option = qsearch('part_pkg_option',
1673 { 'optionname' => { op => 'LIKE',
1674 value => 'use_disposition%',
1678 my %newopts = map { $_->pkgpart => $_ }
1679 qsearch('part_pkg_option', { 'optionname' => 'disposition_in', } );
1680 foreach my $old_opt (@part_pkg_option) {
1681 my $pkgpart = $old_opt->pkgpart;
1682 my $newval = $old_opt->optionname eq 'use_disposition_taqua' ? '100'
1684 my $error = $old_opt->delete;
1685 die $error if $error;
1687 if ( exists($newopts{$pkgpart}) ) {
1688 my $opt = $newopts{$pkgpart};
1689 $opt->optionvalue($opt->optionvalue.",$newval");
1690 $error = $opt->replace;
1691 die $error if $error;
1693 my $new_opt = new FS::part_pkg_option {
1694 'pkgpart' => $pkgpart,
1695 'optionname' => 'disposition_in',
1696 'optionvalue' => $newval,
1698 $error = $new_opt->insert;
1699 die $error if $error;
1700 $newopts{$pkgpart} = $new_opt;
1704 # set any package with FCC voice lines to the "VoIP with broadband" category
1705 # for backward compatibility
1707 # recover from a bad upgrade bug
1708 my $upgrade = 'part_pkg_fcc_voip_class_FIX';
1709 if (!FS::upgrade_journal->is_done($upgrade)) {
1710 my $bad_upgrade = qsearchs('upgrade_journal',
1711 { upgrade => 'part_pkg_fcc_voip_class' }
1713 if ( $bad_upgrade ) {
1714 my $where = 'WHERE history_date <= '.$bad_upgrade->_date.
1715 ' AND history_date > '.($bad_upgrade->_date - 3600);
1716 my @h_part_pkg_option = map { FS::part_pkg_option->new($_->hashref) }
1719 'table' => 'h_part_pkg_option',
1721 'extra_sql' => "$where AND history_action = 'delete'",
1722 'order_by' => 'ORDER BY history_date ASC',
1724 my @h_pkg_svc = map { FS::pkg_svc->new($_->hashref) }
1727 'table' => 'h_pkg_svc',
1729 'extra_sql' => "$where AND history_action = 'replace_old'",
1730 'order_by' => 'ORDER BY history_date ASC',
1733 foreach my $deleted (@h_part_pkg_option, @h_pkg_svc) {
1734 my $pkgpart ||= $deleted->pkgpart;
1735 $opt{$pkgpart} ||= {
1741 if ( $deleted->isa('FS::part_pkg_option') ) {
1742 $opt{$pkgpart}{options}{ $deleted->optionname } = $deleted->optionvalue;
1744 my $svcpart = $deleted->svcpart;
1745 $opt{$pkgpart}{pkg_svc}{$svcpart} = $deleted->quantity;
1746 $opt{$pkgpart}{hidden_svc}{$svcpart} ||= $deleted->hidden;
1747 $opt{$pkgpart}{primary_svc} = $svcpart if $deleted->primary_svc;
1750 foreach my $pkgpart (keys %opt) {
1751 my $part_pkg = FS::part_pkg->by_key($pkgpart);
1752 my $error = $part_pkg->replace( $part_pkg->replace_old, $opt{$pkgpart} );
1754 die "error recovering damaged pkgpart $pkgpart:\n$error\n";
1757 } # $bad_upgrade exists
1758 else { # do the original upgrade, but correctly this time
1759 @part_pkg = qsearch('part_pkg', {
1760 fcc_ds0s => { op => '>', value => 0 },
1761 fcc_voip_class => ''
1763 foreach my $part_pkg (@part_pkg) {
1764 $part_pkg->set(fcc_voip_class => 2);
1765 my @pkg_svc = $part_pkg->pkg_svc;
1766 my %quantity = map {$_->svcpart, $_->quantity} @pkg_svc;
1767 my %hidden = map {$_->svcpart, $_->hidden } @pkg_svc;
1768 my $error = $part_pkg->replace(
1769 $part_pkg->replace_old,
1770 options => { $part_pkg->options },
1771 pkg_svc => \%quantity,
1772 hidden_svc => \%hidden,
1773 primary_svc => ($part_pkg->svcpart || ''),
1775 die $error if $error;
1778 FS::upgrade_journal->set_done($upgrade);
1783 =item curuser_pkgs_sql
1785 Returns an SQL fragment for searching for packages the current user can
1786 use, either via part_pkg.agentnum directly, or via agent type (see
1791 sub curuser_pkgs_sql {
1794 $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
1798 =item agent_pkgs_sql AGENT | AGENTNUM, ...
1800 Returns an SQL fragment for searching for packages the provided agent or agents
1801 can use, either via part_pkg.agentnum directly, or via agent type (see
1806 sub agent_pkgs_sql {
1807 my $class = shift; #i'm a class method, not a sub (the question is... why??)
1808 my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
1810 $class->_pkgs_sql(@agentnums); #is this why
1815 my( $class, @agentnums ) = @_;
1816 my $agentnums = join(',', @agentnums);
1820 ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
1821 OR ( agentnum IS NULL
1822 AND EXISTS ( SELECT 1
1824 LEFT JOIN agent_type USING ( typenum )
1825 LEFT JOIN agent AS typeagent USING ( typenum )
1826 WHERE type_pkgs.pkgpart = part_pkg.pkgpart
1827 AND typeagent.agentnum IN ($agentnums)
1845 #false laziness w/part_export & cdr
1847 foreach my $INC ( @INC ) {
1848 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1849 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1850 warn "attempting to load plan info from $file\n" if $DEBUG;
1851 $file =~ /\/(\w+)\.pm$/ or do {
1852 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1856 my $info = eval "use FS::part_pkg::$mod; ".
1857 "\\%FS::part_pkg::$mod\::info;";
1859 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1862 unless ( keys %$info ) {
1863 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
1866 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1867 #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1868 # warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1871 $info{$mod} = $info;
1872 $info->{'weight'} ||= 0; # quiet warnings
1876 # copy one level deep to allow replacement of fields and fieldorder
1877 tie %plans, 'Tie::IxHash',
1878 map { my %infohash = %{ $info{$_} };
1880 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1883 # inheritance of plan options
1884 foreach my $name (keys(%info)) {
1885 if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
1886 warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
1887 delete $plans{$name};
1890 my $parents = $info{$name}->{'inherit_fields'} || [];
1891 my (%fields, %field_exists, @fieldorder);
1892 foreach my $parent ($name, @$parents) {
1893 if ( !exists($info{$parent}) ) {
1894 warn "$name tried to inherit from nonexistent '$parent'\n";
1897 %fields = ( # avoid replacing existing fields
1898 %{ $info{$parent}->{'fields'} || {} },
1901 foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
1903 next if $field_exists{$_};
1904 $field_exists{$_} = 1;
1905 # allow inheritors to remove inherited fields from the fieldorder
1906 push @fieldorder, $_ if !exists($fields{$_}) or
1907 !exists($fields{$_}->{'disabled'});
1910 $plans{$name}->{'fields'} = \%fields;
1911 $plans{$name}->{'fieldorder'} = \@fieldorder;
1921 =head1 NEW PLAN CLASSES
1923 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
1924 found in eg/plan_template.pm. Until then, it is suggested that you use the
1925 other modules in FS/FS/part_pkg/ as a guide.
1929 The delete method is unimplemented.
1931 setup and recur semantics are not yet defined (and are implemented in
1932 FS::cust_bill. hmm.). now they're deprecated and need to go.
1936 part_pkg_taxrate is Pg specific
1938 replace should be smarter about managing the related tables (options, pkg_svc)
1942 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1943 schema.html from the base documentation.