+=item check_options
+
+For a passed I<$options> hashref, validates any options that
+have 'validate' subroutines defined (I<$options> values might
+be altered.) Returns error message, or empty string if valid.
+
+Invoked by L</insert> and L</replace> via the equivalent
+methods in L<FS::option_Common>.
+
+=cut
+
+sub check_options {
+ my ($self,$options) = @_;
+ foreach my $option (keys %$options) {
+ if (exists $plans{ $self->plan }->{fields}->{$option}) {
+ if (exists($plans{$self->plan}->{fields}->{$option}->{'validate'})) {
+ # pass option name for use in error message
+ # pass a reference to the $options value, so it can be cleaned up
+ my $error = &{$plans{$self->plan}->{fields}->{$option}->{'validate'}}($option,\($options->{$option}));
+ return $error if $error;
+ }
+ } # else "option does not exist" error?
+ }
+ return '';
+}
+
+=item check_pkg_svc
+
+Checks pkg_svc records as a whole (for part_svc_link dependencies).
+
+If there is an error, returns the error, otherwise returns false.
+
+=cut
+
+sub check_pkg_svc {
+ my( $self, %opt ) = @_;
+
+ my $agentnum = $self->agentnum;
+
+ my %pkg_svc = map { $_->svcpart => $_ } $self->pkg_svc;
+
+ foreach my $svcpart ( keys %pkg_svc ) {
+
+ foreach my $part_svc_link ( $self->part_svc_link(
+ 'src_svcpart' => $svcpart,
+ 'link_type' => 'part_pkg_restrict',
+ )
+ ) {
+
+ return $part_svc_link->dst_svc. ' must be included with '.
+ $part_svc_link->src_svc
+ unless $pkg_svc{ $part_svc_link->dst_svcpart };
+ }
+
+ }
+
+ return '' if $opt{part_pkg_restrict_soft_override};
+
+ foreach my $svcpart ( keys %pkg_svc ) {
+
+ foreach my $part_svc_link ( $self->part_svc_link(
+ 'src_svcpart' => $svcpart,
+ 'link_type' => 'part_pkg_restrict_soft',
+ )
+ ) {
+ return $part_svc_link->dst_svc. ' is suggested with '.
+ $part_svc_link->src_svc
+ unless $pkg_svc{ $part_svc_link->dst_svcpart };
+ }
+
+ }
+
+ '';
+}
+
+=item part_svc_link OPTION => VALUE ...
+
+Returns the service dependencies (see L<FS::part_svc_link>) for the given
+search options, taking into account this package definition's agent.
+
+Available options are any field in part_svc_link. Typically used options are
+src_svcpart and link_type.
+
+=cut
+
+sub part_svc_link {
+ FS::part_svc_link->by_agentnum( shift->agentnum, @_ );
+}
+
+=item supersede OLD [, OPTION => VALUE ... ]
+
+Inserts this package as a successor to the package OLD. All options are as
+for C<insert>. After inserting, disables OLD and sets the new package as its
+successor.
+
+=cut
+
+sub supersede {
+ my ($new, $old, %options) = @_;
+ my $error;
+
+ $new->set('pkgpart' => '');
+ $new->set('family_pkgpart' => $old->family_pkgpart);
+ warn " inserting successor package\n" if $DEBUG;
+ $error = $new->insert(%options);
+ return $error if $error;
+
+ warn " disabling superseded package\n" if $DEBUG;
+ $old->set('successor' => $new->pkgpart);
+ $old->set('disabled' => 'Y');
+ $error = $old->SUPER::replace; # don't change its options/pkg_svc records
+ return $error if $error;
+
+ warn " propagating changes to family" if $DEBUG;
+ $new->propagate($old);
+}
+
+=item propagate OLD
+
+If any of certain fields have changed from OLD to this package, then,
+for all packages in the same lineage as this one, sets those fields
+to their values in this package.
+
+=cut
+
+my @propagate_fields = (
+ qw( pkg classnum setup_cost recur_cost taxclass
+ setuptax recurtax pay_weight credit_weight
+ )
+);
+
+sub propagate {
+ my $new = shift;
+ my $old = shift;
+ my %fields = (
+ map { $_ => $new->get($_) }
+ grep { $new->get($_) ne $old->get($_) }
+ @propagate_fields
+ );
+
+ my @part_pkg = qsearch('part_pkg', {
+ 'family_pkgpart' => $new->family_pkgpart
+ });
+ my @error;
+ foreach my $part_pkg ( @part_pkg ) {
+ my $pkgpart = $part_pkg->pkgpart;
+ next if $pkgpart == $new->pkgpart; # don't modify $new
+ warn " propagating to pkgpart $pkgpart\n" if $DEBUG;
+ foreach ( keys %fields ) {
+ $part_pkg->set($_, $fields{$_});
+ }
+ # SUPER::replace to avoid changing non-core fields
+ my $error = $part_pkg->SUPER::replace;
+ push @error, "pkgpart $pkgpart: $error"
+ if $error;
+ }
+ join("\n", @error);
+}
+
+=item set_fcc_options HASHREF
+
+Sets the FCC options on this package definition to the values specified
+in HASHREF.
+
+=cut
+
+sub set_fcc_options {
+ my $self = shift;
+ my $pkgpart = $self->pkgpart;
+ my $options;
+ if (ref $_[0]) {
+ $options = shift;
+ } else {
+ $options = { @_ };
+ }
+
+ my %existing_num = map { $_->fccoptionname => $_->num }
+ qsearch('part_pkg_fcc_option', { pkgpart => $pkgpart });
+
+ local $FS::Record::nowarn_identical = 1;
+ # set up params for process_o2m
+ my $i = 0;
+ my $params = {};
+ foreach my $name (keys %$options ) {
+ $params->{ "num$i" } = $existing_num{$name} || '';
+ $params->{ "num$i".'_fccoptionname' } = $name;
+ $params->{ "num$i".'_optionvalue' } = $options->{$name};
+ $i++;
+ }
+
+ $self->process_o2m(
+ table => 'part_pkg_fcc_option',
+ fields => [qw( fccoptionname optionvalue )],
+ params => $params,
+ );
+}
+
+=item pkg_locale LOCALE
+
+Returns a customer-viewable string representing this package for the given
+locale, from the part_pkg_msgcat table. If the given locale is empty or no
+localized string is found, returns the base pkg field.
+
+=cut
+
+sub pkg_locale {
+ my( $self, $locale ) = @_;
+ return $self->pkg unless $locale;
+ my $part_pkg_msgcat = $self->part_pkg_msgcat($locale) or return $self->pkg;
+ $part_pkg_msgcat->pkg;
+}
+
+=item part_pkg_msgcat LOCALE
+
+Like pkg_locale, but returns the FS::part_pkg_msgcat object itself.
+
+=cut
+
+sub part_pkg_msgcat {
+ my( $self, $locale ) = @_;
+ qsearchs( 'part_pkg_msgcat', {
+ pkgpart => $self->pkgpart,
+ locale => $locale,
+ });
+}
+
+=item pkg_comment [ OPTION => VALUE... ]