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 my $conf = new FS::Conf;
221 if ( $conf->exists('agent_defaultpkg') ) {
222 warn " agent_defaultpkg set; allowing all agents to purchase package"
224 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
225 my $type_pkgs = new FS::type_pkgs({
226 'typenum' => $agent_type->typenum,
227 'pkgpart' => $self->pkgpart,
229 my $error = $type_pkgs->insert;
231 $dbh->rollback if $oldAutoCommit;
237 warn " inserting part_pkg_taxoverride records" if $DEBUG;
238 my %overrides = %{ $options{'tax_overrides'} || {} };
239 foreach my $usage_class ( keys %overrides ) {
241 ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
242 ? $overrides{$usage_class}
244 my @overrides = (grep "$_", split(',', $override) );
245 my $error = $self->process_m2m (
246 'link_table' => 'part_pkg_taxoverride',
247 'target_table' => 'tax_class',
248 'hashref' => { 'usage_class' => $usage_class },
249 'params' => \@overrides,
252 $dbh->rollback if $oldAutoCommit;
257 unless ( $skip_pkg_svc_hack ) {
259 warn " inserting pkg_svc records" if $DEBUG;
260 my $pkg_svc = $options{'pkg_svc'} || {};
261 my $hidden_svc = $options{'hidden_svc'} || {};
262 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
263 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
265 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
269 my $pkg_svc = new FS::pkg_svc( {
270 'pkgpart' => $self->pkgpart,
271 'svcpart' => $part_svc->svcpart,
272 'quantity' => $quantity,
273 'primary_svc' => $primary_svc,
274 'hidden' => $hidden_svc->{$part_svc->svcpart},
276 my $error = $pkg_svc->insert;
278 $dbh->rollback if $oldAutoCommit;
285 if ( $options{'cust_pkg'} ) {
286 warn " updating cust_pkg record " if $DEBUG;
288 ref($options{'cust_pkg'})
289 ? $options{'cust_pkg'}
290 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
291 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
292 if $options{'custnum_ref'};
293 my %hash = $old_cust_pkg->hash;
294 $hash{'pkgpart'} = $self->pkgpart,
295 my $new_cust_pkg = new FS::cust_pkg \%hash;
296 local($FS::cust_pkg::disable_agentcheck) = 1;
297 my $error = $new_cust_pkg->replace($old_cust_pkg);
299 $dbh->rollback if $oldAutoCommit;
300 return "Error modifying cust_pkg record: $error";
304 if ( $options{'part_pkg_vendor'} ) {
305 while ( my ($exportnum, $vendor_pkg_id) =
306 each %{ $options{part_pkg_vendor} }
309 my $ppv = new FS::part_pkg_vendor( {
310 'pkgpart' => $self->pkgpart,
311 'exportnum' => $exportnum,
312 'vendor_pkg_id' => $vendor_pkg_id,
314 my $error = $ppv->insert;
316 $dbh->rollback if $oldAutoCommit;
317 return "Error inserting part_pkg_vendor record: $error";
322 warn " committing transaction" if $DEBUG and $oldAutoCommit;
323 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
330 Currently unimplemented.
335 return "Can't (yet?) delete package definitions.";
336 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
339 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
341 Replaces OLD_RECORD with this one in the database. If there is an error,
342 returns the error, otherwise returns false.
344 Currently available options are: I<pkg_svc>, I<hidden_svc>, I<primary_svc>
347 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
348 values, the appropriate FS::pkg_svc records will be replaced. I<hidden_svc>
349 can be set to a hashref of svcparts and flag values ('Y' or '') to set the
350 'hidden' field in these records.
352 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
353 FS::pkg_svc record will be updated.
355 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
363 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
368 ( ref($_[0]) eq 'HASH' )
372 $options->{options} = { $old->options } unless defined($options->{options});
374 warn "FS::part_pkg::replace called on $new to replace $old with options".
375 join(', ', map "$_ => ". $options->{$_}, keys %$options)
378 local $SIG{HUP} = 'IGNORE';
379 local $SIG{INT} = 'IGNORE';
380 local $SIG{QUIT} = 'IGNORE';
381 local $SIG{TERM} = 'IGNORE';
382 local $SIG{TSTP} = 'IGNORE';
383 local $SIG{PIPE} = 'IGNORE';
385 my $oldAutoCommit = $FS::UID::AutoCommit;
386 local $FS::UID::AutoCommit = 0;
389 my $conf = new FS::Conf;
390 if ( $conf->exists('part_pkg-lineage') ) {
391 if ( grep { $options->{options}->{$_} ne $old->option($_, 1) }
392 qw(setup_fee recur_fee) #others? config?
395 warn " superseding package" if $DEBUG;
397 my $error = $new->supersede($old, %$options);
399 $dbh->rollback if $oldAutoCommit;
403 warn " committing transaction" if $DEBUG and $oldAutoCommit;
404 $dbh->commit if $oldAutoCommit;
411 #plandata shit stays in replace for upgrades until after 2.0 (or edit
413 warn " saving legacy plandata" if $DEBUG;
414 my $plandata = $new->get('plandata');
415 $new->set('plandata', '');
417 warn " deleting old part_pkg_option records" if $DEBUG;
418 foreach my $part_pkg_option ( $old->part_pkg_option ) {
419 my $error = $part_pkg_option->delete;
421 $dbh->rollback if $oldAutoCommit;
426 warn " replacing part_pkg record" if $DEBUG;
427 my $error = $new->SUPER::replace($old, $options->{options} );
429 $dbh->rollback if $oldAutoCommit;
433 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
434 foreach my $part_pkg_option (
435 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
436 return "illegal plandata: $plandata";
438 new FS::part_pkg_option {
439 'pkgpart' => $new->pkgpart,
444 split("\n", $plandata)
446 my $error = $part_pkg_option->insert;
448 $dbh->rollback if $oldAutoCommit;
453 warn " replacing pkg_svc records" if $DEBUG;
454 my $pkg_svc = $options->{'pkg_svc'};
455 my $hidden_svc = $options->{'hidden_svc'} || {};
456 if ( $pkg_svc ) { # if it wasn't passed, don't change existing pkg_svcs
457 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
458 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
459 my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
461 ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
462 && $options->{'primary_svc'} == $part_svc->svcpart
467 my $old_pkg_svc = qsearchs('pkg_svc', {
468 'pkgpart' => $old->pkgpart,
469 'svcpart' => $part_svc->svcpart,
472 my $old_quantity = 0;
473 my $old_primary_svc = '';
475 if ( $old_pkg_svc ) {
476 $old_quantity = $old_pkg_svc->quantity;
477 $old_primary_svc = $old_pkg_svc->primary_svc
478 if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
479 $old_hidden = $old_pkg_svc->hidden;
482 next unless $old_quantity != $quantity ||
483 $old_primary_svc ne $primary_svc ||
484 $old_hidden ne $hidden;
486 my $new_pkg_svc = new FS::pkg_svc( {
487 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
488 'pkgpart' => $new->pkgpart,
489 'svcpart' => $part_svc->svcpart,
490 'quantity' => $quantity,
491 'primary_svc' => $primary_svc,
494 my $error = $old_pkg_svc
495 ? $new_pkg_svc->replace($old_pkg_svc)
496 : $new_pkg_svc->insert;
498 $dbh->rollback if $oldAutoCommit;
502 } #if $options->{pkg_svc}
504 my @part_pkg_vendor = $old->part_pkg_vendor;
505 my @current_exportnum = ();
506 if ( $options->{'part_pkg_vendor'} ) {
507 my($exportnum,$vendor_pkg_id);
508 while ( ($exportnum,$vendor_pkg_id)
509 = each %{$options->{'part_pkg_vendor'}} ) {
511 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
512 if($exportnum == $part_pkg_vendor->exportnum
513 && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
514 $part_pkg_vendor->vendor_pkg_id($vendor_pkg_id);
515 my $error = $part_pkg_vendor->replace;
517 $dbh->rollback if $oldAutoCommit;
518 return "Error replacing part_pkg_vendor record: $error";
523 elsif($exportnum == $part_pkg_vendor->exportnum
524 && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
529 unless ( $noinsert ) {
530 my $ppv = new FS::part_pkg_vendor( {
531 'pkgpart' => $new->pkgpart,
532 'exportnum' => $exportnum,
533 'vendor_pkg_id' => $vendor_pkg_id,
535 my $error = $ppv->insert;
537 $dbh->rollback if $oldAutoCommit;
538 return "Error inserting part_pkg_vendor record: $error";
541 push @current_exportnum, $exportnum;
544 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
545 unless ( grep($_ eq $part_pkg_vendor->exportnum, @current_exportnum) ) {
546 my $error = $part_pkg_vendor->delete;
548 $dbh->rollback if $oldAutoCommit;
549 return "Error deleting part_pkg_vendor record: $error";
554 # propagate changes to certain core fields
555 if ( $conf->exists('part_pkg-lineage') ) {
556 warn " propagating changes to family" if $DEBUG;
557 my $error = $new->propagate($old);
559 $dbh->rollback if $oldAutoCommit;
564 warn " committing transaction" if $DEBUG and $oldAutoCommit;
565 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
571 Checks all fields to make sure this is a valid package definition. If
572 there is an error, returns the error, otherwise returns false. Called by the
573 insert and replace methods.
579 warn "FS::part_pkg::check called on $self" if $DEBUG;
581 for (qw(setup recur plandata)) {
582 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
583 return "Use of $_ field is deprecated; set a plan and options: ".
585 if length($self->get($_));
589 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
590 my $error = $self->ut_number('freq');
591 return $error if $error;
593 $self->freq =~ /^(\d+[hdw]?)$/
594 or return "Illegal or empty freq: ". $self->freq;
598 my @null_agentnum_right = ( 'Edit global package definitions' );
599 push @null_agentnum_right, 'One-time charge'
600 if $self->freq =~ /^0/;
601 push @null_agentnum_right, 'Customize customer package'
602 if $self->disabled eq 'Y'; #good enough
604 my $error = $self->ut_numbern('pkgpart')
605 || $self->ut_text('pkg')
606 || $self->ut_textn('comment')
607 || $self->ut_textn('promo_code')
608 || $self->ut_alphan('plan')
609 || $self->ut_enum('setuptax', [ '', 'Y' ] )
610 || $self->ut_enum('recurtax', [ '', 'Y' ] )
611 || $self->ut_textn('taxclass')
612 || $self->ut_enum('disabled', [ '', 'Y' ] )
613 || $self->ut_enum('custom', [ '', 'Y' ] )
614 || $self->ut_enum('no_auto', [ '', 'Y' ])
615 || $self->ut_enum('recur_show_zero', [ '', 'Y' ])
616 || $self->ut_enum('setup_show_zero', [ '', 'Y' ])
617 #|| $self->ut_moneyn('setup_cost')
618 #|| $self->ut_moneyn('recur_cost')
619 || $self->ut_floatn('setup_cost')
620 || $self->ut_floatn('recur_cost')
621 || $self->ut_floatn('pay_weight')
622 || $self->ut_floatn('credit_weight')
623 || $self->ut_numbern('taxproductnum')
624 || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum')
625 || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
626 || $self->ut_foreign_keyn('taxproductnum',
627 'part_pkg_taxproduct',
631 ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
632 : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
634 || $self->ut_numbern('fcc_ds0s')
635 || $self->ut_numbern('fcc_voip_class')
636 || $self->ut_numbern('delay_start')
637 || $self->ut_foreign_keyn('successor', 'part_pkg', 'pkgpart')
638 || $self->ut_foreign_keyn('family_pkgpart', 'part_pkg', 'pkgpart')
639 || $self->SUPER::check
641 return $error if $error;
643 return 'Unknown plan '. $self->plan
644 unless exists($plans{$self->plan});
646 my $conf = new FS::Conf;
647 return 'Taxclass is required'
648 if ! $self->taxclass && $conf->exists('require_taxclasses');
653 =item supersede OLD [, OPTION => VALUE ... ]
655 Inserts this package as a successor to the package OLD. All options are as
656 for C<insert>. After inserting, disables OLD and sets the new package as its
662 my ($new, $old, %options) = @_;
665 $new->set('pkgpart' => '');
666 $new->set('family_pkgpart' => $old->family_pkgpart);
667 warn " inserting successor package\n" if $DEBUG;
668 $error = $new->insert(%options);
669 return $error if $error;
671 warn " disabling superseded package\n" if $DEBUG;
672 $old->set('successor' => $new->pkgpart);
673 $old->set('disabled' => 'Y');
674 $error = $old->SUPER::replace; # don't change its options/pkg_svc records
675 return $error if $error;
677 warn " propagating changes to family" if $DEBUG;
678 $new->propagate($old);
683 If any of certain fields have changed from OLD to this package, then,
684 for all packages in the same lineage as this one, sets those fields
685 to their values in this package.
689 my @propagate_fields = (
690 qw( pkg classnum setup_cost recur_cost taxclass
691 setuptax recurtax pay_weight credit_weight
699 map { $_ => $new->get($_) }
700 grep { $new->get($_) ne $old->get($_) }
704 my @part_pkg = qsearch('part_pkg', {
705 'family_pkgpart' => $new->family_pkgpart
708 foreach my $part_pkg ( @part_pkg ) {
709 my $pkgpart = $part_pkg->pkgpart;
710 next if $pkgpart == $new->pkgpart; # don't modify $new
711 warn " propagating to pkgpart $pkgpart\n" if $DEBUG;
712 foreach ( keys %fields ) {
713 $part_pkg->set($_, $fields{$_});
715 # SUPER::replace to avoid changing non-core fields
716 my $error = $part_pkg->SUPER::replace;
717 push @error, "pkgpart $pkgpart: $error"
723 =item pkg_locale LOCALE
725 Returns a customer-viewable string representing this package for the given
726 locale, from the part_pkg_msgcat table. If the given locale is empty or no
727 localized string is found, returns the base pkg field.
732 my( $self, $locale ) = @_;
733 return $self->pkg unless $locale;
734 my $part_pkg_msgcat = $self->part_pkg_msgcat($locale) or return $self->pkg;
735 $part_pkg_msgcat->pkg;
738 =item part_pkg_msgcat LOCALE
740 Like pkg_locale, but returns the FS::part_pkg_msgcat object itself.
744 sub part_pkg_msgcat {
745 my( $self, $locale ) = @_;
746 qsearchs( 'part_pkg_msgcat', {
747 pkgpart => $self->pkgpart,
752 =item pkg_comment [ OPTION => VALUE... ]
754 Returns an (internal) string representing this package. Currently,
755 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
756 future, omitting pkgpart. The comment will have '(CUSTOM) ' prepended if
759 If the option nopkgpart is true then the "pkgpart: ' is omitted.
767 #$self->pkg. ' - '. $self->comment;
768 #$self->pkg. ' ('. $self->comment. ')';
769 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
770 my $custom_comment = $self->custom_comment(%opt);
771 $pre. $self->pkg. ( $custom_comment ? " - $custom_comment" : '' );
774 sub price_info { # safety, in case a part_pkg hasn't defined price_info
780 my $price_info = $self->price_info(@_);
781 ( $self->custom ? '(CUSTOM) ' : '' ).
783 ( ( ($self->custom || $self->comment) && $price_info ) ? ' - ' : '' ).
789 $self->pkg. ' - '. ($self->price_info || 'No charge');
794 Returns the package class, as an FS::pkg_class object, or the empty string
795 if there is no package class.
801 if ( $self->classnum ) {
802 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
808 =item addon_pkg_class
810 Returns the add-on package class, as an FS::pkg_class object, or the empty
811 string if there is no add-on package class.
815 sub addon_pkg_class {
817 if ( $self->addon_classnum ) {
818 qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
826 Returns the package category name, or the empty string if there is no package
833 my $pkg_class = $self->pkg_class;
835 ? $pkg_class->categoryname
841 Returns the package class name, or the empty string if there is no package
848 my $pkg_class = $self->pkg_class;
850 ? $pkg_class->classname
854 =item addon_classname
856 Returns the add-on package class name, or the empty string if there is no
857 add-on package class.
861 sub addon_classname {
863 my $pkg_class = $self->addon_pkg_class;
865 ? $pkg_class->classname
871 Returns the associated agent for this event, if any, as an FS::agent object.
877 qsearchs('agent', { 'agentnum' => $self->agentnum } );
880 =item pkg_svc [ HASHREF | OPTION => VALUE ]
882 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
883 definition (with non-zero quantity).
885 One option is available, I<disable_linked>. If set true it will return the
886 services for this package definition alone, omitting services from any add-on
893 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
900 qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
906 # #sort { $b->primary cmp $a->primary }
907 # grep { $_->quantity }
908 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
910 my $opt = ref($_[0]) ? $_[0] : { @_ };
911 my %pkg_svc = map { $_->svcpart => $_ }
912 grep { $_->quantity }
913 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
915 unless ( $opt->{disable_linked} ) {
916 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
917 my @pkg_svc = grep { $_->quantity }
918 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
919 foreach my $pkg_svc ( @pkg_svc ) {
920 if ( $pkg_svc{$pkg_svc->svcpart} ) {
921 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
922 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
924 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
934 =item svcpart [ SVCDB ]
936 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
937 associated with this package definition (see L<FS::pkg_svc>). Returns
938 false if there not a primary service definition or exactly one service
939 definition with quantity 1, or if SVCDB is specified and does not match the
940 svcdb of the service definition. SVCDB can be specified as a scalar table
941 name, such as 'svc_acct', or as an arrayref of possible table names.
946 my $pkg_svc = shift->_primary_pkg_svc(@_);
947 $pkg_svc ? $pkg_svc->svcpart : '';
950 =item part_svc [ SVCDB ]
952 Like the B<svcpart> method, but returns the FS::part_svc object (see
958 my $pkg_svc = shift->_primary_pkg_svc(@_);
959 $pkg_svc ? $pkg_svc->part_svc : '';
962 sub _primary_pkg_svc {
965 my $svcdb = scalar(@_) ? shift : [];
966 $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
967 my %svcdb = map { $_=>1 } @$svcdb;
970 grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
973 my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
974 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
976 return '' if scalar(@pkg_svc) != 1;
980 =item svcpart_unique_svcdb SVCDB
982 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
983 SVCDB associated with this package definition (see L<FS::pkg_svc>). Returns
984 false if there not a primary service definition for SVCDB or there are multiple
985 service definitions for SVCDB.
989 sub svcpart_unique_svcdb {
990 my( $self, $svcdb ) = @_;
991 my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
992 return '' if scalar(@svcdb_pkg_svc) != 1;
993 $svcdb_pkg_svc[0]->svcpart;
998 Returns a list of the acceptable payment types for this package. Eventually
999 this should come out of a database table and be editable, but currently has the
1000 following logic instead:
1002 If the package is free, the single item B<BILL> is
1003 returned, otherwise, the single item B<CARD> is returned.
1005 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
1011 if ( $self->is_free ) {
1020 Returns true if this package is free.
1026 if ( $self->can('is_free_options') ) {
1027 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
1028 map { $self->option($_) }
1029 $self->is_free_options;
1031 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
1032 "provides neither is_free_options nor is_free method; returning false";
1037 # whether the plan allows discounts to be applied to this package
1038 sub can_discount { 0; }
1040 # whether the plan allows changing the start date
1041 sub can_start_date { 1; }
1043 # the delay start date if present
1044 sub delay_start_date {
1047 my $delay = $self->delay_start or return '';
1049 # avoid timelocal silliness
1050 my $dt = DateTime->today(time_zone => 'local');
1051 $dt->add(days => $delay);
1056 # moved to FS::Misc to make this accessible to other packages
1058 FS::Misc::pkg_freqs();
1063 Returns an english representation of the I<freq> field, such as "monthly",
1064 "weekly", "semi-annually", etc.
1070 my $freq = $self->freq;
1072 #my $freqs_href = $self->freqs_href;
1073 my $freqs_href = freqs_href();
1075 if ( exists($freqs_href->{$freq}) ) {
1076 $freqs_href->{$freq};
1078 my $interval = 'month';
1079 if ( $freq =~ /^(\d+)([hdw])$/ ) {
1080 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
1081 $interval = $interval{$2};
1086 "every $freq ${interval}s";
1091 =item add_freq TIMESTAMP [ FREQ ]
1093 Adds a billing period of some frequency to the provided timestamp and
1094 returns the resulting timestamp, or -1 if the frequency could not be
1095 parsed (shouldn't happen). By default, the frequency of this package
1096 will be used; to override this, pass a different frequency as a second
1102 my( $self, $date, $freq ) = @_;
1103 $freq = $self->freq unless $freq;
1105 #change this bit to use Date::Manip? CAREFUL with timezones (see
1106 # mailing list archive)
1107 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
1109 if ( $freq =~ /^\d+$/ ) {
1111 until ( $mon < 12 ) { $mon -= 12; $year++; }
1113 $mday = 28 if $mday > 28 && FS::Conf->new->exists('anniversary-rollback');
1115 } elsif ( $freq =~ /^(\d+)w$/ ) {
1117 $mday += $weeks * 7;
1118 } elsif ( $freq =~ /^(\d+)d$/ ) {
1121 } elsif ( $freq =~ /^(\d+)h$/ ) {
1128 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
1133 For backwards compatibility, returns the plandata field as well as all options
1134 from FS::part_pkg_option.
1140 carp "plandata is deprecated";
1142 $self->SUPER::plandata(@_);
1144 my $plandata = $self->get('plandata');
1145 my %options = $self->options;
1146 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
1151 =item part_pkg_vendor
1153 Returns all vendor/external package ids as FS::part_pkg_vendor objects (see
1154 L<FS::part_pkg_vendor>).
1158 sub part_pkg_vendor {
1160 qsearch('part_pkg_vendor', { 'pkgpart' => $self->pkgpart } );
1163 =item vendor_pkg_ids
1165 Returns a list of vendor/external package ids by exportnum
1169 sub vendor_pkg_ids {
1171 map { $_->exportnum => $_->vendor_pkg_id } $self->part_pkg_vendor;
1174 =item part_pkg_option
1176 Returns all options as FS::part_pkg_option objects (see
1177 L<FS::part_pkg_option>).
1181 sub part_pkg_option {
1183 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
1188 Returns a list of option names and values suitable for assigning to a hash.
1194 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
1197 =item option OPTIONNAME [ QUIET ]
1199 Returns the option value for the given name, or the empty string. If a true
1200 value is passed as the second argument, warnings about missing the option
1206 my( $self, $opt, $ornull ) = @_;
1207 my $part_pkg_option =
1208 qsearchs('part_pkg_option', {
1209 pkgpart => $self->pkgpart,
1212 return $part_pkg_option->optionvalue if $part_pkg_option;
1213 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
1214 split("\n", $self->get('plandata') );
1215 return $plandata{$opt} if exists $plandata{$opt};
1216 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
1217 "not found in options or plandata!\n"
1222 =item bill_part_pkg_link
1224 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1228 sub bill_part_pkg_link {
1229 shift->_part_pkg_link('bill', @_);
1232 =item svc_part_pkg_link
1234 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1238 sub svc_part_pkg_link {
1239 shift->_part_pkg_link('svc', @_);
1242 =item supp_part_pkg_link
1244 Returns the associated part_pkg_link records of type 'supp' (supplemental
1249 sub supp_part_pkg_link {
1250 shift->_part_pkg_link('supp', @_);
1253 sub _part_pkg_link {
1254 my( $self, $type ) = @_;
1255 qsearch({ table => 'part_pkg_link',
1256 hashref => { 'src_pkgpart' => $self->pkgpart,
1257 'link_type' => $type,
1258 #protection against infinite recursive links
1259 'dst_pkgpart' => { op=>'!=', value=> $self->pkgpart },
1261 order_by => "ORDER BY hidden",
1265 sub self_and_bill_linked {
1266 shift->_self_and_linked('bill', @_);
1269 sub self_and_svc_linked {
1270 shift->_self_and_linked('svc', @_);
1273 sub _self_and_linked {
1274 my( $self, $type, $hidden ) = @_;
1278 foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
1279 $self->_part_pkg_link($type) ) )
1281 $_->hidden($hidden) if $hidden;
1288 =item part_pkg_taxoverride [ CLASS ]
1290 Returns all associated FS::part_pkg_taxoverride objects (see
1291 L<FS::part_pkg_taxoverride>). Limits the returned set to those
1292 of class CLASS if defined. Class may be one of 'setup', 'recur',
1293 the empty string (default), or a usage class number (see L<FS::usage_class>).
1294 When a class is specified, the empty string class (default) is returned
1295 if no more specific values exist.
1299 sub part_pkg_taxoverride {
1303 my $hashref = { 'pkgpart' => $self->pkgpart };
1304 $hashref->{'usage_class'} = $class if defined($class);
1305 my @overrides = qsearch('part_pkg_taxoverride', $hashref );
1307 unless ( scalar(@overrides) || !defined($class) || !$class ){
1308 $hashref->{'usage_class'} = '';
1309 @overrides = qsearch('part_pkg_taxoverride', $hashref );
1315 =item has_taxproduct
1317 Returns true if this package has any taxproduct associated with it.
1321 sub has_taxproduct {
1324 $self->taxproductnum ||
1325 scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) }
1326 keys %{ {$self->options} }
1332 =item taxproduct [ CLASS ]
1334 Returns the associated tax product for this package definition (see
1335 L<FS::part_pkg_taxproduct>). CLASS may be one of 'setup', 'recur' or
1336 the usage classnum (see L<FS::usage_class>). Returns the default
1337 tax product for this record if the more specific CLASS value does
1346 my $part_pkg_taxproduct;
1348 my $taxproductnum = $self->taxproductnum;
1350 my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1351 $taxproductnum = $class_taxproductnum
1352 if $class_taxproductnum
1355 $part_pkg_taxproduct =
1356 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1358 unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1359 $taxproductnum = $self->taxproductnum;
1360 $part_pkg_taxproduct =
1361 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1364 $part_pkg_taxproduct;
1367 =item taxproduct_description [ CLASS ]
1369 Returns the description of the associated tax product for this package
1370 definition (see L<FS::part_pkg_taxproduct>).
1374 sub taxproduct_description {
1376 my $part_pkg_taxproduct = $self->taxproduct(@_);
1377 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1380 =item part_pkg_taxrate DATA_PROVIDER, GEOCODE, [ CLASS ]
1382 Returns the package to taxrate m2m records for this package in the location
1383 specified by GEOCODE (see L<FS::part_pkg_taxrate>) and usage class CLASS.
1384 CLASS may be one of 'setup', 'recur', or one of the usage classes numbers
1385 (see L<FS::usage_class>).
1389 sub _expand_cch_taxproductnum {
1392 my $part_pkg_taxproduct = $self->taxproduct($class);
1394 my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
1395 ? ( split ':', $part_pkg_taxproduct->taxproduct )
1398 $a = '' unless $a; $b = '' unless $b; $c = '' unless $c; $d = '' unless $d;
1399 my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
1400 OR taxproduct = '$a:$b:$c:'
1401 OR taxproduct = '$a:$b:".":$d'
1402 OR taxproduct = '$a:$b:".":' )";
1403 map { $_->taxproductnum } qsearch( { 'table' => 'part_pkg_taxproduct',
1404 'hashref' => { 'data_vendor'=>'cch' },
1405 'extra_sql' => $extra_sql,
1410 sub part_pkg_taxrate {
1412 my ($data_vendor, $geocode, $class) = @_;
1415 my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
1416 dbh->quote($data_vendor);
1418 # CCH oddness in m2m
1419 $extra_sql .= ' AND ('.
1420 join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) }
1424 # much more CCH oddness in m2m -- this is kludgy
1425 my @tpnums = $self->_expand_cch_taxproductnum($class);
1426 if (scalar(@tpnums)) {
1427 $extra_sql .= ' AND ('.
1428 join(' OR ', map{ "taxproductnum = $_" } @tpnums ).
1431 $extra_sql .= ' AND ( 0 = 1 )';
1434 my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
1435 my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
1436 my $select = 'DISTINCT ON(taxclassnum) *, taxproduct';
1438 # should qsearch preface columns with the table to facilitate joins?
1439 qsearch( { 'table' => 'part_pkg_taxrate',
1440 'select' => $select,
1441 'hashref' => { # 'data_vendor' => $data_vendor,
1442 # 'taxproductnum' => $self->taxproductnum,
1444 'addl_from' => $addl_from,
1445 'extra_sql' => $extra_sql,
1446 'order_by' => $order_by,
1450 =item part_pkg_discount
1452 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1457 sub part_pkg_discount {
1459 qsearch('part_pkg_discount', { 'pkgpart' => $self->pkgpart });
1462 =item part_pkg_usage
1464 Returns the voice usage pools (see L<FS::part_pkg_usage>) defined for
1469 sub part_pkg_usage {
1471 qsearch('part_pkg_usage', { 'pkgpart' => $self->pkgpart });
1476 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1477 PLAN is the object's I<plan> field. There should be better docs
1478 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1484 my $plan = $self->plan;
1486 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1490 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1491 my $class = ref($self). "::$plan";
1492 warn "reblessing $self into $class" if $DEBUG > 1;
1495 bless($self, $class) unless $@;
1500 sub calc_setup { die 'no calc_setup for '. shift->plan. "\n"; }
1501 sub calc_recur { die 'no calc_recur for '. shift->plan. "\n"; }
1503 #fallback that return 0 for old legacy packages with no plan
1504 sub calc_remain { 0; }
1505 sub calc_units { 0; }
1507 #fallback for everything not based on flat.pm
1508 sub recur_temporality { 'upcoming'; }
1509 sub calc_cancel { 0; }
1511 #fallback for everything except bulk.pm
1512 sub hide_svc_detail { 0; }
1514 #fallback for packages that can't/won't summarize usage
1515 sub sum_usage { 0; }
1517 =item recur_cost_permonth CUST_PKG
1519 recur_cost divided by freq (only supported for monthly and longer frequencies)
1523 sub recur_cost_permonth {
1524 my($self, $cust_pkg) = @_;
1525 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1526 sprintf('%.2f', $self->recur_cost / $self->freq );
1529 =item cust_bill_pkg_recur CUST_PKG
1531 Actual recurring charge for the specified customer package from customer's most
1536 sub cust_bill_pkg_recur {
1537 my($self, $cust_pkg) = @_;
1538 my $cust_bill_pkg = qsearchs({
1539 'table' => 'cust_bill_pkg',
1540 'addl_from' => 'LEFT JOIN cust_bill USING ( invnum )',
1541 'hashref' => { 'pkgnum' => $cust_pkg->pkgnum,
1542 'recur' => { op=>'>', value=>'0' },
1544 'order_by' => 'ORDER BY cust_bill._date DESC,
1545 cust_bill_pkg.sdate DESC
1548 }) or return 0; #die "use cust_bill_pkg_recur credits with once_perinv condition";
1549 $cust_bill_pkg->recur;
1552 =item unit_setup CUST_PKG
1554 Returns the setup fee for one unit of the package.
1559 my ($self, $cust_pkg) = @_;
1560 $self->option('setup_fee') || 0;
1563 =item format OPTION DATA
1565 Returns data formatted according to the function 'format' 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}{format})) {
1573 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1579 =item parse OPTION DATA
1581 Returns data parsed according to the function 'parse' described
1582 in the plan info. Returns DATA if no such function exists.
1587 my ($self, $option, $data) = (shift, shift, shift);
1588 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1589 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1599 =head1 CLASS METHODS
1607 # Used by FS::Upgrade to migrate to a new database.
1609 sub _upgrade_data { # class method
1610 my($class, %opts) = @_;
1612 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1614 my @part_pkg = qsearch({
1615 'table' => 'part_pkg',
1616 'extra_sql' => "WHERE ". join(' OR ',
1617 'plan IS NULL', "plan = '' ",
1621 foreach my $part_pkg (@part_pkg) {
1623 unless ( $part_pkg->plan ) {
1624 $part_pkg->plan('flat');
1631 # now upgrade to the explicit custom flag
1633 @part_pkg = qsearch({
1634 'table' => 'part_pkg',
1635 'hashref' => { disabled => 'Y', custom => '' },
1636 'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1639 foreach my $part_pkg (@part_pkg) {
1640 my $new = new FS::part_pkg { $part_pkg->hash };
1642 my $comment = $part_pkg->comment;
1643 $comment =~ s/^\(CUSTOM\) //;
1644 $comment = '(none)' unless $comment =~ /\S/;
1645 $new->comment($comment);
1647 my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1648 my $primary = $part_pkg->svcpart;
1649 my $options = { $part_pkg->options };
1651 my $error = $new->replace( $part_pkg,
1652 'pkg_svc' => $pkg_svc,
1653 'primary_svc' => $primary,
1654 'options' => $options,
1656 die $error if $error;
1659 # set family_pkgpart on any packages that don't have it
1660 @part_pkg = qsearch('part_pkg', { 'family_pkgpart' => '' });
1661 foreach my $part_pkg (@part_pkg) {
1662 $part_pkg->set('family_pkgpart' => $part_pkg->pkgpart);
1663 my $error = $part_pkg->SUPER::replace;
1664 die $error if $error;
1667 my @part_pkg_option = qsearch('part_pkg_option',
1668 { 'optionname' => 'unused_credit',
1671 foreach my $old_opt (@part_pkg_option) {
1672 my $pkgpart = $old_opt->pkgpart;
1673 my $error = $old_opt->delete;
1674 die $error if $error;
1676 foreach (qw(unused_credit_cancel unused_credit_change)) {
1677 my $new_opt = new FS::part_pkg_option {
1678 'pkgpart' => $pkgpart,
1682 $error = $new_opt->insert;
1683 die $error if $error;
1687 # migrate use_disposition_taqua and use_disposition to disposition_in
1688 @part_pkg_option = qsearch('part_pkg_option',
1689 { 'optionname' => { op => 'LIKE',
1690 value => 'use_disposition%',
1694 my %newopts = map { $_->pkgpart => $_ }
1695 qsearch('part_pkg_option', { 'optionname' => 'disposition_in', } );
1696 foreach my $old_opt (@part_pkg_option) {
1697 my $pkgpart = $old_opt->pkgpart;
1698 my $newval = $old_opt->optionname eq 'use_disposition_taqua' ? '100'
1700 my $error = $old_opt->delete;
1701 die $error if $error;
1703 if ( exists($newopts{$pkgpart}) ) {
1704 my $opt = $newopts{$pkgpart};
1705 $opt->optionvalue($opt->optionvalue.",$newval");
1706 $error = $opt->replace;
1707 die $error if $error;
1709 my $new_opt = new FS::part_pkg_option {
1710 'pkgpart' => $pkgpart,
1711 'optionname' => 'disposition_in',
1712 'optionvalue' => $newval,
1714 $error = $new_opt->insert;
1715 die $error if $error;
1716 $newopts{$pkgpart} = $new_opt;
1720 # set any package with FCC voice lines to the "VoIP with broadband" category
1721 # for backward compatibility
1723 # recover from a bad upgrade bug
1724 my $upgrade = 'part_pkg_fcc_voip_class_FIX';
1725 if (!FS::upgrade_journal->is_done($upgrade)) {
1726 my $bad_upgrade = qsearchs('upgrade_journal',
1727 { upgrade => 'part_pkg_fcc_voip_class' }
1729 if ( $bad_upgrade ) {
1730 my $where = 'WHERE history_date <= '.$bad_upgrade->_date.
1731 ' AND history_date > '.($bad_upgrade->_date - 3600);
1732 my @h_part_pkg_option = map { FS::part_pkg_option->new($_->hashref) }
1735 'table' => 'h_part_pkg_option',
1737 'extra_sql' => "$where AND history_action = 'delete'",
1738 'order_by' => 'ORDER BY history_date ASC',
1740 my @h_pkg_svc = map { FS::pkg_svc->new($_->hashref) }
1743 'table' => 'h_pkg_svc',
1745 'extra_sql' => "$where AND history_action = 'replace_old'",
1746 'order_by' => 'ORDER BY history_date ASC',
1749 foreach my $deleted (@h_part_pkg_option, @h_pkg_svc) {
1750 my $pkgpart ||= $deleted->pkgpart;
1751 $opt{$pkgpart} ||= {
1757 if ( $deleted->isa('FS::part_pkg_option') ) {
1758 $opt{$pkgpart}{options}{ $deleted->optionname } = $deleted->optionvalue;
1760 my $svcpart = $deleted->svcpart;
1761 $opt{$pkgpart}{pkg_svc}{$svcpart} = $deleted->quantity;
1762 $opt{$pkgpart}{hidden_svc}{$svcpart} ||= $deleted->hidden;
1763 $opt{$pkgpart}{primary_svc} = $svcpart if $deleted->primary_svc;
1766 foreach my $pkgpart (keys %opt) {
1767 my $part_pkg = FS::part_pkg->by_key($pkgpart);
1768 my $error = $part_pkg->replace( $part_pkg->replace_old, $opt{$pkgpart} );
1770 die "error recovering damaged pkgpart $pkgpart:\n$error\n";
1773 } # $bad_upgrade exists
1774 else { # do the original upgrade, but correctly this time
1775 @part_pkg = qsearch('part_pkg', {
1776 fcc_ds0s => { op => '>', value => 0 },
1777 fcc_voip_class => ''
1779 foreach my $part_pkg (@part_pkg) {
1780 $part_pkg->set(fcc_voip_class => 2);
1781 my @pkg_svc = $part_pkg->pkg_svc;
1782 my %quantity = map {$_->svcpart, $_->quantity} @pkg_svc;
1783 my %hidden = map {$_->svcpart, $_->hidden } @pkg_svc;
1784 my $error = $part_pkg->replace(
1785 $part_pkg->replace_old,
1786 options => { $part_pkg->options },
1787 pkg_svc => \%quantity,
1788 hidden_svc => \%hidden,
1789 primary_svc => ($part_pkg->svcpart || ''),
1791 die $error if $error;
1794 FS::upgrade_journal->set_done($upgrade);
1799 =item curuser_pkgs_sql
1801 Returns an SQL fragment for searching for packages the current user can
1802 use, either via part_pkg.agentnum directly, or via agent type (see
1807 sub curuser_pkgs_sql {
1810 $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
1814 =item agent_pkgs_sql AGENT | AGENTNUM, ...
1816 Returns an SQL fragment for searching for packages the provided agent or agents
1817 can use, either via part_pkg.agentnum directly, or via agent type (see
1822 sub agent_pkgs_sql {
1823 my $class = shift; #i'm a class method, not a sub (the question is... why??)
1824 my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
1826 $class->_pkgs_sql(@agentnums); #is this why
1831 my( $class, @agentnums ) = @_;
1832 my $agentnums = join(',', @agentnums);
1836 ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
1837 OR ( agentnum IS NULL
1838 AND EXISTS ( SELECT 1
1840 LEFT JOIN agent_type USING ( typenum )
1841 LEFT JOIN agent AS typeagent USING ( typenum )
1842 WHERE type_pkgs.pkgpart = part_pkg.pkgpart
1843 AND typeagent.agentnum IN ($agentnums)
1861 #false laziness w/part_export & cdr
1863 foreach my $INC ( @INC ) {
1864 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1865 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1866 warn "attempting to load plan info from $file\n" if $DEBUG;
1867 $file =~ /\/(\w+)\.pm$/ or do {
1868 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1872 my $info = eval "use FS::part_pkg::$mod; ".
1873 "\\%FS::part_pkg::$mod\::info;";
1875 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1878 unless ( keys %$info ) {
1879 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
1882 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1883 #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1884 # warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1887 $info{$mod} = $info;
1888 $info->{'weight'} ||= 0; # quiet warnings
1892 # copy one level deep to allow replacement of fields and fieldorder
1893 tie %plans, 'Tie::IxHash',
1894 map { my %infohash = %{ $info{$_} };
1896 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1899 # inheritance of plan options
1900 foreach my $name (keys(%info)) {
1901 if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
1902 warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
1903 delete $plans{$name};
1906 my $parents = $info{$name}->{'inherit_fields'} || [];
1907 my (%fields, %field_exists, @fieldorder);
1908 foreach my $parent ($name, @$parents) {
1909 if ( !exists($info{$parent}) ) {
1910 warn "$name tried to inherit from nonexistent '$parent'\n";
1913 %fields = ( # avoid replacing existing fields
1914 %{ $info{$parent}->{'fields'} || {} },
1917 foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
1919 next if $field_exists{$_};
1920 $field_exists{$_} = 1;
1921 # allow inheritors to remove inherited fields from the fieldorder
1922 push @fieldorder, $_ if !exists($fields{$_}) or
1923 !exists($fields{$_}->{'disabled'});
1926 $plans{$name}->{'fields'} = \%fields;
1927 $plans{$name}->{'fieldorder'} = \@fieldorder;
1937 =head1 NEW PLAN CLASSES
1939 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
1940 found in eg/plan_template.pm. Until then, it is suggested that you use the
1941 other modules in FS/FS/part_pkg/ as a guide.
1945 The delete method is unimplemented.
1947 setup and recur semantics are not yet defined (and are implemented in
1948 FS::cust_bill. hmm.). now they're deprecated and need to go.
1952 part_pkg_taxrate is Pg specific
1954 replace should be smarter about managing the related tables (options, pkg_svc)
1958 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1959 schema.html from the base documentation.