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 $cache_enabled %cache_link %cache_pkg_svc
8 use Carp qw(carp cluck confess);
9 use Scalar::Util qw( blessed );
11 use Time::Local qw( timelocal timelocal_nocheck ); # eventually replace with DateTime
14 use FS::Record qw( qsearch qsearchs dbh dbdef );
15 use FS::Cursor; # for upgrade
21 use FS::part_pkg_option;
22 use FS::part_pkg_fcc_option;
25 use FS::part_pkg_msgcat;
26 use FS::part_pkg_taxrate;
27 use FS::part_pkg_taxoverride;
28 use FS::part_pkg_taxproduct;
29 use FS::part_pkg_link;
30 use FS::part_pkg_discount;
31 use FS::part_pkg_usage;
32 use FS::part_pkg_vendor;
37 $skip_pkg_svc_hack = 0;
45 FS::part_pkg - Object methods for part_pkg objects
51 $record = new FS::part_pkg \%hash
52 $record = new FS::part_pkg { 'column' => 'value' };
54 $custom_record = $template_record->clone;
56 $error = $record->insert;
58 $error = $new_record->replace($old_record);
60 $error = $record->delete;
62 $error = $record->check;
64 @pkg_svc = $record->pkg_svc;
66 $svcnum = $record->svcpart;
67 $svcnum = $record->svcpart( 'svc_acct' );
71 An FS::part_pkg object represents a package definition. FS::part_pkg
72 inherits from FS::Record. The following fields are currently supported:
76 =item pkgpart - primary key (assigned automatically for new package definitions)
78 =item pkg - Text name of this package definition (customer-viewable)
80 =item comment - Text name of this package definition (non-customer-viewable)
82 =item classnum - Optional package class (see L<FS::pkg_class>)
84 =item promo_code - Promotional code
86 =item setup - Setup fee expression (deprecated)
88 =item freq - Frequency of recurring fee
90 =item recur - Recurring fee expression (deprecated)
92 =item setuptax - Setup fee tax exempt flag, empty or `Y'
94 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
96 =item taxclass - Tax class
98 =item plan - Price plan
100 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
102 =item disabled - Disabled flag, empty or `Y'
104 =item custom - Custom flag, empty or `Y'
106 =item setup_cost - for cost tracking
108 =item recur_cost - for cost tracking
110 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
112 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
114 =item agentnum - Optional agentnum (see L<FS::agent>)
116 =item fcc_ds0s - Optional DS0 equivalency number for FCC form 477
118 =item fcc_voip_class - Which column of FCC form 477 part II.B this package
121 =item successor - Foreign key for the part_pkg that replaced this record.
122 If this record is not obsolete, will be null.
124 =item family_pkgpart - Foreign key for the part_pkg that was the earliest
125 ancestor of this record. If this record is not a successor to another
126 part_pkg, will be equal to pkgpart.
128 =item delay_start - Number of days to delay package start, by default
130 =item start_on_hold - 'Y' to suspend this package immediately when it is
131 ordered. The package will not start billing or have a setup fee charged
132 until it is manually unsuspended.
142 Creates a new package definition. To add the package definition to
143 the database, see L<"insert">.
147 sub table { 'part_pkg'; }
151 An alternate constructor. Creates a new package definition by duplicating
152 an existing definition. A new pkgpart is assigned and the custom flag is
153 set to Y. To add the package definition to the database, see L<"insert">.
159 my $class = ref($self);
160 my %hash = $self->hash;
161 $hash{'pkgpart'} = '';
162 $hash{'custom'} = 'Y';
163 #new FS::part_pkg ( \%hash ); # ?
164 new $class ( \%hash ); # ?
167 =item insert [ , OPTION => VALUE ... ]
169 Adds this package definition to the database. If there is an error,
170 returns the error, otherwise returns false.
172 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
173 I<custnum_ref> and I<options>.
175 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
176 values, appropriate FS::pkg_svc records will be inserted. I<hidden_svc> can
177 be set to a hashref of svcparts and flag values ('Y' or '') to set the
178 'hidden' field in these records, and I<provision_hold> can be set similarly
179 for the 'provision_hold' field in these records.
181 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
182 FS::pkg_svc record will be updated.
184 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
185 record itself), the object will be updated to point to this package definition.
187 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
188 the scalar will be updated with the custnum value from the cust_pkg record.
190 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
191 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
192 records will be inserted.
194 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
195 records will be inserted.
202 warn "FS::part_pkg::insert called on $self with options ".
203 join(', ', map "$_=>$options{$_}", keys %options)
206 local $SIG{HUP} = 'IGNORE';
207 local $SIG{INT} = 'IGNORE';
208 local $SIG{QUIT} = 'IGNORE';
209 local $SIG{TERM} = 'IGNORE';
210 local $SIG{TSTP} = 'IGNORE';
211 local $SIG{PIPE} = 'IGNORE';
213 my $oldAutoCommit = $FS::UID::AutoCommit;
214 local $FS::UID::AutoCommit = 0;
217 warn " inserting part_pkg record" if $DEBUG;
218 my $error = $self->SUPER::insert( $options{options} );
220 $dbh->rollback if $oldAutoCommit;
225 if ( $self->get('family_pkgpart') eq '' ) {
226 $self->set('family_pkgpart' => $self->pkgpart);
227 $error = $self->SUPER::replace;
229 $dbh->rollback if $oldAutoCommit;
234 warn " inserting part_pkg_taxoverride records" if $DEBUG;
235 my %overrides = %{ $options{'tax_overrides'} || {} };
236 foreach my $usage_class ( keys %overrides ) {
238 ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
239 ? $overrides{$usage_class}
241 my @overrides = (grep "$_", split(',', $override) );
242 my $error = $self->process_m2m (
243 'link_table' => 'part_pkg_taxoverride',
244 'target_table' => 'tax_class',
245 'hashref' => { 'usage_class' => $usage_class },
246 'params' => \@overrides,
249 $dbh->rollback if $oldAutoCommit;
254 unless ( $skip_pkg_svc_hack ) {
256 warn " inserting pkg_svc records" if $DEBUG;
257 my $pkg_svc = $options{'pkg_svc'} || {};
258 my $hidden_svc = $options{'hidden_svc'} || {};
259 my $provision_hold = $options{'provision_hold'} || {};
260 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
261 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
263 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
267 my $pkg_svc = new FS::pkg_svc( {
268 'pkgpart' => $self->pkgpart,
269 'svcpart' => $part_svc->svcpart,
270 'quantity' => $quantity,
271 'primary_svc' => $primary_svc,
272 'hidden' => $hidden_svc->{$part_svc->svcpart},
273 'provision_hold' => $provision_hold->{$part_svc->svcpart},
275 my $error = $pkg_svc->insert;
277 $dbh->rollback if $oldAutoCommit;
284 if ( $options{'cust_pkg'} ) {
285 warn " updating cust_pkg record " if $DEBUG;
287 ref($options{'cust_pkg'})
288 ? $options{'cust_pkg'}
289 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
290 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
291 if $options{'custnum_ref'};
292 my %hash = $old_cust_pkg->hash;
293 $hash{'pkgpart'} = $self->pkgpart,
294 my $new_cust_pkg = new FS::cust_pkg \%hash;
295 local($FS::cust_pkg::disable_agentcheck) = 1;
296 my $error = $new_cust_pkg->replace($old_cust_pkg);
298 $dbh->rollback if $oldAutoCommit;
299 return "Error modifying cust_pkg record: $error";
303 if ( $options{'part_pkg_vendor'} ) {
304 while ( my ($exportnum, $vendor_pkg_id) =
305 each %{ $options{part_pkg_vendor} }
308 my $ppv = new FS::part_pkg_vendor( {
309 'pkgpart' => $self->pkgpart,
310 'exportnum' => $exportnum,
311 'vendor_pkg_id' => $vendor_pkg_id,
313 my $error = $ppv->insert;
315 $dbh->rollback if $oldAutoCommit;
316 return "Error inserting part_pkg_vendor record: $error";
321 if ( $options{fcc_options} ) {
322 warn " updating fcc options " if $DEBUG;
323 $self->set_fcc_options( $options{fcc_options} );
326 warn " committing transaction" if $DEBUG and $oldAutoCommit;
327 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
334 Currently unimplemented.
339 return "Can't (yet?) delete package definitions.";
340 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
343 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
345 Replaces OLD_RECORD with this one in the database. If there is an error,
346 returns the error, otherwise returns false.
348 Currently available options are: I<pkg_svc>, I<hidden_svc>, I<primary_svc>,
349 I<provision_hold> and I<options>
351 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
352 values, the appropriate FS::pkg_svc records will be replaced. I<hidden_svc>
353 can be set to a hashref of svcparts and flag values ('Y' or '') to set the
354 'hidden' field in these records. I<provision_hold> can be set
355 to a hashref of svcparts and flag values ('Y' or '') to set the field
358 If I<primary_svc> is set to the svcpart of the primary service, the
359 appropriate FS::pkg_svc record will be updated.
361 If I<options> is set to a hashref, the appropriate FS::part_pkg_option
362 records will be replaced.
369 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
374 ( ref($_[0]) eq 'HASH' )
378 $options->{options} = { $old->options } unless defined($options->{options});
380 warn "FS::part_pkg::replace called on $new to replace $old with options".
381 join(', ', map "$_ => ". $options->{$_}, keys %$options)
384 local $SIG{HUP} = 'IGNORE';
385 local $SIG{INT} = 'IGNORE';
386 local $SIG{QUIT} = 'IGNORE';
387 local $SIG{TERM} = 'IGNORE';
388 local $SIG{TSTP} = 'IGNORE';
389 local $SIG{PIPE} = 'IGNORE';
391 my $oldAutoCommit = $FS::UID::AutoCommit;
392 local $FS::UID::AutoCommit = 0;
395 my $conf = new FS::Conf;
396 if ( $conf->exists('part_pkg-lineage') ) {
397 if ( grep { $options->{options}->{$_} ne $old->option($_, 1) }
398 qw(setup_fee recur_fee) #others? config?
401 warn " superseding package" if $DEBUG;
403 my $error = $new->supersede($old, %$options);
405 $dbh->rollback if $oldAutoCommit;
409 warn " committing transaction" if $DEBUG and $oldAutoCommit;
410 $dbh->commit if $oldAutoCommit;
417 #plandata shit stays in replace for upgrades until after 2.0 (or edit
419 warn " saving legacy plandata" if $DEBUG;
420 my $plandata = $new->get('plandata');
421 $new->set('plandata', '');
423 warn " deleting old part_pkg_option records" if $DEBUG;
424 foreach my $part_pkg_option ( $old->part_pkg_option ) {
425 my $error = $part_pkg_option->delete;
427 $dbh->rollback if $oldAutoCommit;
432 warn " replacing part_pkg record" if $DEBUG;
433 my $error = $new->SUPER::replace($old, $options->{options} );
435 $dbh->rollback if $oldAutoCommit;
439 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
440 foreach my $part_pkg_option (
441 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
442 return "illegal plandata: $plandata";
444 new FS::part_pkg_option {
445 'pkgpart' => $new->pkgpart,
450 split("\n", $plandata)
452 my $error = $part_pkg_option->insert;
454 $dbh->rollback if $oldAutoCommit;
459 warn " replacing pkg_svc records" if $DEBUG;
460 my $pkg_svc = $options->{'pkg_svc'};
461 my $hidden_svc = $options->{'hidden_svc'} || {};
462 my $provision_hold = $options->{'provision_hold'} || {};
463 if ( $pkg_svc ) { # if it wasn't passed, don't change existing pkg_svcs
464 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
465 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
466 my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
467 my $provision_hold = $provision_hold->{$part_svc->svcpart} || '';
469 ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
470 && $options->{'primary_svc'} == $part_svc->svcpart
475 my $old_pkg_svc = qsearchs('pkg_svc', {
476 'pkgpart' => $old->pkgpart,
477 'svcpart' => $part_svc->svcpart,
480 my $old_quantity = 0;
481 my $old_primary_svc = '';
483 my $old_provision_hold = '';
484 if ( $old_pkg_svc ) {
485 $old_quantity = $old_pkg_svc->quantity;
486 $old_primary_svc = $old_pkg_svc->primary_svc
487 if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
488 $old_hidden = $old_pkg_svc->hidden;
489 $old_provision_hold = $old_pkg_svc->provision_hold;
492 next unless $old_quantity != $quantity ||
493 $old_primary_svc ne $primary_svc ||
494 $old_hidden ne $hidden ||
495 $old_provision_hold ne $provision_hold;
497 my $new_pkg_svc = new FS::pkg_svc( {
498 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
499 'pkgpart' => $new->pkgpart,
500 'svcpart' => $part_svc->svcpart,
501 'quantity' => $quantity,
502 'primary_svc' => $primary_svc,
504 'provision_hold' => $provision_hold,
506 my $error = $old_pkg_svc
507 ? $new_pkg_svc->replace($old_pkg_svc)
508 : $new_pkg_svc->insert;
510 $dbh->rollback if $oldAutoCommit;
514 } #if $options->{pkg_svc}
516 my @part_pkg_vendor = $old->part_pkg_vendor;
517 my @current_exportnum = ();
518 if ( $options->{'part_pkg_vendor'} ) {
519 my($exportnum,$vendor_pkg_id);
520 while ( ($exportnum,$vendor_pkg_id)
521 = each %{$options->{'part_pkg_vendor'}} ) {
523 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
524 if($exportnum == $part_pkg_vendor->exportnum
525 && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
526 $part_pkg_vendor->vendor_pkg_id($vendor_pkg_id);
527 my $error = $part_pkg_vendor->replace;
529 $dbh->rollback if $oldAutoCommit;
530 return "Error replacing part_pkg_vendor record: $error";
535 elsif($exportnum == $part_pkg_vendor->exportnum
536 && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
541 unless ( $noinsert ) {
542 my $ppv = new FS::part_pkg_vendor( {
543 'pkgpart' => $new->pkgpart,
544 'exportnum' => $exportnum,
545 'vendor_pkg_id' => $vendor_pkg_id,
547 my $error = $ppv->insert;
549 $dbh->rollback if $oldAutoCommit;
550 return "Error inserting part_pkg_vendor record: $error";
553 push @current_exportnum, $exportnum;
556 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
557 unless ( grep($_ eq $part_pkg_vendor->exportnum, @current_exportnum) ) {
558 my $error = $part_pkg_vendor->delete;
560 $dbh->rollback if $oldAutoCommit;
561 return "Error deleting part_pkg_vendor record: $error";
566 # propagate changes to certain core fields
567 if ( $conf->exists('part_pkg-lineage') ) {
568 warn " propagating changes to family" if $DEBUG;
569 my $error = $new->propagate($old);
571 $dbh->rollback if $oldAutoCommit;
576 if ( $options->{fcc_options} ) {
577 warn " updating fcc options " if $DEBUG;
578 $new->set_fcc_options( $options->{fcc_options} );
581 warn " committing transaction" if $DEBUG and $oldAutoCommit;
582 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
586 sub validate_number {
587 my ($option, $valref) = @_;
588 $$valref = 0 unless $$valref;
589 return "Invalid $option"
590 unless ($$valref) = ($$valref =~ /^\s*(\d+)\s*$/);
596 Checks all fields to make sure this is a valid package definition. If
597 there is an error, returns the error, otherwise returns false. Called by the
598 insert and replace methods.
604 warn "FS::part_pkg::check called on $self" if $DEBUG;
606 for (qw(setup recur plandata)) {
607 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
608 return "Use of $_ field is deprecated; set a plan and options: ".
610 if length($self->get($_));
614 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
615 my $error = $self->ut_number('freq');
616 return $error if $error;
618 $self->freq =~ /^(\d+[hdw]?)$/
619 or return "Illegal or empty freq: ". $self->freq;
623 my @null_agentnum_right = ( 'Edit global package definitions' );
624 push @null_agentnum_right, 'One-time charge'
625 if $self->freq =~ /^0/;
626 push @null_agentnum_right, 'Customize customer package'
627 if $self->disabled eq 'Y'; #good enough
629 my $error = $self->ut_numbern('pkgpart')
630 || $self->ut_text('pkg')
631 || $self->ut_textn('comment')
632 || $self->ut_textn('promo_code')
633 || $self->ut_alphan('plan')
634 || $self->ut_flag('setuptax')
635 || $self->ut_flag('recurtax')
636 || $self->ut_textn('taxclass')
637 || $self->ut_flag('disabled')
638 || $self->ut_flag('custom')
639 || $self->ut_flag('no_auto')
640 || $self->ut_flag('recur_show_zero')
641 || $self->ut_flag('setup_show_zero')
642 || $self->ut_flag('start_on_hold')
643 #|| $self->ut_moneyn('setup_cost')
644 #|| $self->ut_moneyn('recur_cost')
645 || $self->ut_floatn('setup_cost')
646 || $self->ut_floatn('recur_cost')
647 || $self->ut_floatn('pay_weight')
648 || $self->ut_floatn('credit_weight')
649 || $self->ut_numbern('taxproductnum')
650 || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum')
651 || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
652 || $self->ut_foreign_keyn('taxproductnum',
653 'part_pkg_taxproduct',
657 ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
658 : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
660 || $self->ut_numbern('fcc_ds0s')
661 || $self->ut_numbern('fcc_voip_class')
662 || $self->ut_numbern('delay_start')
663 || $self->ut_foreign_keyn('successor', 'part_pkg', 'pkgpart')
664 || $self->ut_foreign_keyn('family_pkgpart', 'part_pkg', 'pkgpart')
665 || $self->ut_alphan('agent_pkgpartid')
666 || $self->SUPER::check
668 return $error if $error;
670 return 'Unknown plan '. $self->plan
671 unless exists($plans{$self->plan});
673 my $conf = new FS::Conf;
674 return 'Taxclass is required'
675 if ! $self->taxclass && $conf->exists('require_taxclasses');
682 Pass an I<$options> hashref that contains the values to be
683 inserted or updated for any FS::part_pkg::MODULE.pm.
685 For each key in I<$options>, validates the value by calling
686 the 'validate' subroutine defined for that option e.g.
687 FS::part_pkg::MODULE::plan_info()->{$KEY}->{validate}. The
688 option validation function is only called when the hashkey for
689 that option exists in I<$options>.
691 Then the module validation function is called, from
692 FS::part_pkg::MODULE::plan_info()->{validate}
694 Returns error message, or empty string if valid.
696 Invoked by L</insert> and L</replace> via the equivalent
697 methods in L<FS::option_Common>.
702 my ($self,$options) = @_;
703 foreach my $option (keys %$options) {
704 if (exists $plans{ $self->plan }->{fields}->{$option}) {
705 if (exists($plans{$self->plan}->{fields}->{$option}->{'validate'})) {
706 # pass option name for use in error message
707 # pass a reference to the $options value, so it can be cleaned up
708 my $error = &{$plans{$self->plan}->{fields}->{$option}->{'validate'}}($option,\($options->{$option}));
709 return $error if $error;
711 } # else "option does not exist" error?
713 if (exists($plans{$self->plan}->{'validate'})) {
714 my $error = &{$plans{$self->plan}->{'validate'}}($options);
715 return $error if $error;
720 =item supersede OLD [, OPTION => VALUE ... ]
722 Inserts this package as a successor to the package OLD. All options are as
723 for C<insert>. After inserting, disables OLD and sets the new package as its
729 my ($new, $old, %options) = @_;
732 $new->set('pkgpart' => '');
733 $new->set('family_pkgpart' => $old->family_pkgpart);
734 warn " inserting successor package\n" if $DEBUG;
735 $error = $new->insert(%options);
736 return $error if $error;
738 warn " disabling superseded package\n" if $DEBUG;
739 $old->set('successor' => $new->pkgpart);
740 $old->set('disabled' => 'Y');
741 $error = $old->SUPER::replace; # don't change its options/pkg_svc records
742 return $error if $error;
744 warn " propagating changes to family" if $DEBUG;
745 $new->propagate($old);
750 If any of certain fields have changed from OLD to this package, then,
751 for all packages in the same lineage as this one, sets those fields
752 to their values in this package.
756 my @propagate_fields = (
757 qw( pkg classnum setup_cost recur_cost taxclass
758 setuptax recurtax pay_weight credit_weight
766 map { $_ => $new->get($_) }
767 grep { $new->get($_) ne $old->get($_) }
771 my @part_pkg = qsearch('part_pkg', {
772 'family_pkgpart' => $new->family_pkgpart
775 foreach my $part_pkg ( @part_pkg ) {
776 my $pkgpart = $part_pkg->pkgpart;
777 next if $pkgpart == $new->pkgpart; # don't modify $new
778 warn " propagating to pkgpart $pkgpart\n" if $DEBUG;
779 foreach ( keys %fields ) {
780 $part_pkg->set($_, $fields{$_});
782 # SUPER::replace to avoid changing non-core fields
783 my $error = $part_pkg->SUPER::replace;
784 push @error, "pkgpart $pkgpart: $error"
790 =item set_fcc_options HASHREF
792 Sets the FCC options on this package definition to the values specified
797 sub set_fcc_options {
799 my $pkgpart = $self->pkgpart;
807 my %existing_num = map { $_->fccoptionname => $_->num }
808 qsearch('part_pkg_fcc_option', { pkgpart => $pkgpart });
810 local $FS::Record::nowarn_identical = 1;
811 # set up params for process_o2m
814 foreach my $name (keys %$options ) {
815 $params->{ "num$i" } = $existing_num{$name} || '';
816 $params->{ "num$i".'_fccoptionname' } = $name;
817 $params->{ "num$i".'_optionvalue' } = $options->{$name};
822 table => 'part_pkg_fcc_option',
823 fields => [qw( fccoptionname optionvalue )],
828 =item pkg_locale LOCALE
830 Returns a customer-viewable string representing this package for the given
831 locale, from the part_pkg_msgcat table. If the given locale is empty or no
832 localized string is found, returns the base pkg field.
837 my( $self, $locale ) = @_;
838 return $self->pkg unless $locale;
839 my $part_pkg_msgcat = $self->part_pkg_msgcat($locale) or return $self->pkg;
840 $part_pkg_msgcat->pkg;
843 =item part_pkg_msgcat LOCALE
845 Like pkg_locale, but returns the FS::part_pkg_msgcat object itself.
849 sub part_pkg_msgcat {
850 my( $self, $locale ) = @_;
851 qsearchs( 'part_pkg_msgcat', {
852 pkgpart => $self->pkgpart,
857 =item pkg_comment [ OPTION => VALUE... ]
859 Returns an (internal) string representing this package. Currently,
860 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
861 future, omitting pkgpart. The comment will have '(CUSTOM) ' prepended if
864 If the option nopkgpart is true then the "pkgpart: ' is omitted.
872 #$self->pkg. ' - '. $self->comment;
873 #$self->pkg. ' ('. $self->comment. ')';
874 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
875 my $custom_comment = $self->custom_comment(%opt);
876 $pre. $self->pkg. ( $custom_comment ? " - $custom_comment" : '' );
879 #without price info (so without hitting the DB again)
880 sub pkg_comment_only {
884 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
885 my $comment = $self->comment;
886 $pre. $self->pkg. ( $comment ? " - $comment" : '' );
889 sub price_info { # safety, in case a part_pkg hasn't defined price_info
895 my $price_info = $self->price_info(@_);
896 ( $self->custom ? '(CUSTOM) ' : '' ).
898 ( ( ($self->custom || $self->comment) && $price_info ) ? ' - ' : '' ).
904 $self->pkg. ' - '. ($self->price_info || 'No charge');
909 Returns the package class, as an FS::pkg_class object, or the empty string
910 if there is no package class.
916 if ( $self->classnum ) {
917 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
923 =item addon_pkg_class
925 Returns the add-on package class, as an FS::pkg_class object, or the empty
926 string if there is no add-on package class.
930 sub addon_pkg_class {
932 if ( $self->addon_classnum ) {
933 qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
941 Returns the package category name, or the empty string if there is no package
948 my $pkg_class = $self->pkg_class;
950 ? $pkg_class->categoryname
956 Returns the package class name, or the empty string if there is no package
963 my $pkg_class = $self->pkg_class;
965 ? $pkg_class->classname
969 =item addon_classname
971 Returns the add-on package class name, or the empty string if there is no
972 add-on package class.
976 sub addon_classname {
978 my $pkg_class = $self->addon_pkg_class;
980 ? $pkg_class->classname
986 Returns the associated agent for this event, if any, as an FS::agent object.
992 qsearchs('agent', { 'agentnum' => $self->agentnum } );
995 =item pkg_svc [ HASHREF | OPTION => VALUE ]
997 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
998 definition (with non-zero quantity).
1000 One option is available, I<disable_linked>. If set true it will return the
1001 services for this package definition alone, omitting services from any add-on
1008 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
1015 qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
1021 return @{ $cache_pkg_svc{$self->pkgpart} }
1022 if $cache_enabled && $cache_pkg_svc{$self->pkgpart};
1024 # #sort { $b->primary cmp $a->primary }
1025 # grep { $_->quantity }
1026 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
1028 my $opt = ref($_[0]) ? $_[0] : { @_ };
1029 my %pkg_svc = map { $_->svcpart => $_ } $self->_pkg_svc;
1031 unless ( $opt->{disable_linked} ) {
1032 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
1033 my @pkg_svc = $dst_pkg->_pkg_svc;
1034 foreach my $pkg_svc ( @pkg_svc ) {
1035 if ( $pkg_svc{$pkg_svc->svcpart} ) {
1036 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
1037 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
1039 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
1045 my @pkg_svc = values(%pkg_svc);
1047 $cache_pkg_svc{$self->pkgpart} = \@pkg_svc if $cache_enabled;
1055 grep { $_->quantity }
1057 'select' => 'pkg_svc.*, part_svc.*',
1058 'table' => 'pkg_svc',
1059 'addl_from' => 'LEFT JOIN part_svc USING ( svcpart )',
1060 'hashref' => { 'pkgpart' => $self->pkgpart },
1064 =item svcpart [ SVCDB ]
1066 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
1067 associated with this package definition (see L<FS::pkg_svc>). Returns
1068 false if there not a primary service definition or exactly one service
1069 definition with quantity 1, or if SVCDB is specified and does not match the
1070 svcdb of the service definition. SVCDB can be specified as a scalar table
1071 name, such as 'svc_acct', or as an arrayref of possible table names.
1076 my $pkg_svc = shift->_primary_pkg_svc(@_);
1077 $pkg_svc ? $pkg_svc->svcpart : '';
1080 =item part_svc [ SVCDB ]
1082 Like the B<svcpart> method, but returns the FS::part_svc object (see
1088 my $pkg_svc = shift->_primary_pkg_svc(@_);
1089 $pkg_svc ? $pkg_svc->part_svc : '';
1092 sub _primary_pkg_svc {
1095 my $svcdb = scalar(@_) ? shift : [];
1096 $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
1097 my %svcdb = map { $_=>1 } @$svcdb;
1100 grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
1103 my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
1104 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
1106 return '' if scalar(@pkg_svc) != 1;
1110 =item svcpart_unique_svcdb SVCDB
1112 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
1113 SVCDB associated with this package definition (see L<FS::pkg_svc>). Returns
1114 false if there not a primary service definition for SVCDB or there are multiple
1115 service definitions for SVCDB.
1119 sub svcpart_unique_svcdb {
1120 my( $self, $svcdb ) = @_;
1121 my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
1122 return '' if scalar(@svcdb_pkg_svc) != 1;
1123 $svcdb_pkg_svc[0]->svcpart;
1128 Returns a list of the acceptable payment types for this package. Eventually
1129 this should come out of a database table and be editable, but currently has the
1130 following logic instead:
1132 If the package is free, the single item B<BILL> is
1133 returned, otherwise, the single item B<CARD> is returned.
1135 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
1141 if ( $self->is_free ) {
1150 Returns true if this package is free.
1156 if ( $self->can('is_free_options') ) {
1157 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
1158 map { $self->option($_) }
1159 $self->is_free_options;
1161 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
1162 "provides neither is_free_options nor is_free method; returning false";
1167 # whether the plan allows discounts to be applied to this package
1168 sub can_discount { 0; }
1170 # whether the plan allows changing the start date
1171 sub can_start_date {
1173 $self->start_on_hold ? 0 : 1;
1176 # the delay start date if present
1177 sub delay_start_date {
1180 my $delay = $self->delay_start or return '';
1182 # avoid timelocal silliness
1183 my $dt = DateTime->today(time_zone => 'local');
1184 $dt->add(days => $delay);
1189 # moved to FS::Misc to make this accessible to other packages
1191 FS::Misc::pkg_freqs();
1196 Returns an english representation of the I<freq> field, such as "monthly",
1197 "weekly", "semi-annually", etc.
1203 my $freq = $self->freq;
1205 #my $freqs_href = $self->freqs_href;
1206 my $freqs_href = freqs_href();
1208 if ( exists($freqs_href->{$freq}) ) {
1209 $freqs_href->{$freq};
1211 my $interval = 'month';
1212 if ( $freq =~ /^(\d+)([hdw])$/ ) {
1213 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
1214 $interval = $interval{$2};
1219 "every $freq ${interval}s";
1224 =item add_freq TIMESTAMP [ FREQ ]
1226 Adds a billing period of some frequency to the provided timestamp and
1227 returns the resulting timestamp, or -1 if the frequency could not be
1228 parsed (shouldn't happen). By default, the frequency of this package
1229 will be used; to override this, pass a different frequency as a second
1235 my( $self, $date, $freq ) = @_;
1236 $freq = $self->freq unless $freq;
1238 #change this bit to use Date::Manip? CAREFUL with timezones (see
1239 # mailing list archive)
1240 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
1242 if ( $freq =~ /^\d+$/ ) {
1244 until ( $mon < 12 ) { $mon -= 12; $year++; }
1246 $mday = 28 if $mday > 28 && FS::Conf->new->exists('anniversary-rollback');
1248 } elsif ( $freq =~ /^(\d+)w$/ ) {
1250 $mday += $weeks * 7;
1251 } elsif ( $freq =~ /^(\d+)d$/ ) {
1254 } elsif ( $freq =~ /^(\d+)h$/ ) {
1261 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
1266 For backwards compatibility, returns the plandata field as well as all options
1267 from FS::part_pkg_option.
1273 carp "plandata is deprecated";
1275 $self->SUPER::plandata(@_);
1277 my $plandata = $self->get('plandata');
1278 my %options = $self->options;
1279 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
1284 =item part_pkg_vendor
1286 Returns all vendor/external package ids as FS::part_pkg_vendor objects (see
1287 L<FS::part_pkg_vendor>).
1291 sub part_pkg_vendor {
1293 qsearch('part_pkg_vendor', { 'pkgpart' => $self->pkgpart } );
1296 =item vendor_pkg_ids
1298 Returns a list of vendor/external package ids by exportnum
1302 sub vendor_pkg_ids {
1304 map { $_->exportnum => $_->vendor_pkg_id } $self->part_pkg_vendor;
1307 =item part_pkg_option
1309 Returns all options as FS::part_pkg_option objects (see
1310 L<FS::part_pkg_option>).
1314 sub part_pkg_option {
1316 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
1321 Returns a list of option names and values suitable for assigning to a hash.
1327 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
1330 =item option OPTIONNAME [ QUIET ]
1332 Returns the option value for the given name, or the empty string. If a true
1333 value is passed as the second argument, warnings about missing the option
1339 my( $self, $opt, $ornull ) = @_;
1341 #cache: was pulled up in the original part_pkg query
1342 return $self->hashref->{"_opt_$opt"}
1343 if exists $self->hashref->{"_opt_$opt"};
1345 cluck "$self -> option: searching for $opt" if $DEBUG;
1346 my $part_pkg_option =
1347 qsearchs('part_pkg_option', {
1348 pkgpart => $self->pkgpart,
1351 return $part_pkg_option->optionvalue if $part_pkg_option;
1353 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
1354 split("\n", $self->get('plandata') );
1355 return $plandata{$opt} if exists $plandata{$opt};
1356 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
1357 "not found in options or plandata!\n"
1363 =item fcc_option OPTIONNAME
1365 Returns the FCC 477 report option value for the given name, or the empty
1371 my ($self, $name) = @_;
1372 my $part_pkg_fcc_option =
1373 qsearchs('part_pkg_fcc_option', {
1374 pkgpart => $self->pkgpart,
1375 fccoptionname => $name,
1377 $part_pkg_fcc_option ? $part_pkg_fcc_option->optionvalue : '';
1382 Returns all FCC 477 report options for this package, as a hash-like list.
1388 map { $_->fccoptionname => $_->optionvalue }
1389 qsearch('part_pkg_fcc_option', { pkgpart => $self->pkgpart });
1392 =item bill_part_pkg_link
1394 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1398 sub bill_part_pkg_link {
1399 shift->_part_pkg_link('bill', @_);
1402 =item svc_part_pkg_link
1404 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1408 sub svc_part_pkg_link {
1409 shift->_part_pkg_link('svc', @_);
1412 =item supp_part_pkg_link
1414 Returns the associated part_pkg_link records of type 'supp' (supplemental
1419 sub supp_part_pkg_link {
1420 shift->_part_pkg_link('supp', @_);
1423 sub _part_pkg_link {
1424 my( $self, $type ) = @_;
1426 return @{ $cache_link{$type}->{$self->pkgpart} }
1427 if $cache_enabled && $cache_link{$type}->{$self->pkgpart};
1429 cluck $type.'_part_pkg_link called' if $DEBUG;
1432 qsearch({ table => 'part_pkg_link',
1433 hashref => { src_pkgpart => $self->pkgpart,
1435 #protection against infinite recursive links
1436 dst_pkgpart => { op=>'!=', value=> $self->pkgpart },
1438 order_by => "ORDER BY hidden",
1441 $cache_link{$type}->{$self->pkgpart} = \@ppl if $cache_enabled;
1446 sub self_and_bill_linked {
1447 shift->_self_and_linked('bill', @_);
1450 sub self_and_svc_linked {
1451 shift->_self_and_linked('svc', @_);
1454 sub _self_and_linked {
1455 my( $self, $type, $hidden ) = @_;
1459 foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
1460 $self->_part_pkg_link($type) ) )
1462 $_->hidden($hidden) if $hidden;
1469 =item part_pkg_taxoverride [ CLASS ]
1471 Returns all associated FS::part_pkg_taxoverride objects (see
1472 L<FS::part_pkg_taxoverride>). Limits the returned set to those
1473 of class CLASS if defined. Class may be one of 'setup', 'recur',
1474 the empty string (default), or a usage class number (see L<FS::usage_class>).
1475 When a class is specified, the empty string class (default) is returned
1476 if no more specific values exist.
1480 sub part_pkg_taxoverride {
1484 my $hashref = { 'pkgpart' => $self->pkgpart };
1485 $hashref->{'usage_class'} = $class if defined($class);
1486 my @overrides = qsearch('part_pkg_taxoverride', $hashref );
1488 unless ( scalar(@overrides) || !defined($class) || !$class ){
1489 $hashref->{'usage_class'} = '';
1490 @overrides = qsearch('part_pkg_taxoverride', $hashref );
1496 =item has_taxproduct
1498 Returns true if this package has any taxproduct associated with it.
1502 sub has_taxproduct {
1505 $self->taxproductnum ||
1506 scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) }
1507 keys %{ {$self->options} }
1513 =item taxproduct [ CLASS ]
1515 Returns the associated tax product for this package definition (see
1516 L<FS::part_pkg_taxproduct>). CLASS may be one of 'setup', 'recur' or
1517 the usage classnum (see L<FS::usage_class>). Returns the default
1518 tax product for this record if the more specific CLASS value does
1527 my $part_pkg_taxproduct;
1529 my $taxproductnum = $self->taxproductnum;
1531 my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1532 $taxproductnum = $class_taxproductnum
1533 if $class_taxproductnum
1536 $part_pkg_taxproduct =
1537 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1539 unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1540 $taxproductnum = $self->taxproductnum;
1541 $part_pkg_taxproduct =
1542 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1545 $part_pkg_taxproduct;
1548 =item taxproduct_description [ CLASS ]
1550 Returns the description of the associated tax product for this package
1551 definition (see L<FS::part_pkg_taxproduct>).
1555 sub taxproduct_description {
1557 my $part_pkg_taxproduct = $self->taxproduct(@_);
1558 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1562 =item tax_rates DATA_PROVIDER, GEOCODE, [ CLASS ]
1564 Returns the tax table entries (L<FS::tax_rate> objects) that apply to this
1565 package in the location specified by GEOCODE, for usage class CLASS (one of
1566 'setup', 'recur', null, or a C<usage_class> number).
1572 my ($vendor, $geocode, $class) = @_;
1573 # if this part_pkg is overridden into a specific taxclass, get that class
1574 my @taxclassnums = map { $_->taxclassnum }
1575 $self->part_pkg_taxoverride($class);
1576 # otherwise, get its tax product category
1577 if (!@taxclassnums) {
1578 my $part_pkg_taxproduct = $self->taxproduct($class);
1579 # If this isn't defined, then the class has no taxproduct designation,
1580 # so return no tax rates.
1581 return () if !$part_pkg_taxproduct;
1583 # convert the taxproduct to the tax classes that might apply to it in
1585 @taxclassnums = map { $_->taxclassnum }
1586 grep { $_->taxable eq 'Y' } # why do we need this?
1587 $part_pkg_taxproduct->part_pkg_taxrate($geocode);
1589 return unless @taxclassnums;
1591 # then look up the actual tax_rate entries
1592 warn "Found taxclassnum values of ". join(',', @taxclassnums) ."\n"
1594 my $extra_sql = "AND taxclassnum IN (". join(',', @taxclassnums) . ")";
1595 my @taxes = qsearch({ 'table' => 'tax_rate',
1596 'hashref' => { 'geocode' => $geocode,
1597 'data_vendor' => $vendor,
1599 'extra_sql' => $extra_sql,
1601 warn "Found taxes ". join(',', map {$_->taxnum} @taxes) ."\n"
1607 =item part_pkg_discount
1609 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1614 sub part_pkg_discount {
1616 qsearch('part_pkg_discount', { 'pkgpart' => $self->pkgpart });
1619 =item part_pkg_usage
1621 Returns the voice usage pools (see L<FS::part_pkg_usage>) defined for
1626 sub part_pkg_usage {
1628 qsearch('part_pkg_usage', { 'pkgpart' => $self->pkgpart });
1633 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1634 PLAN is the object's I<plan> field. There should be better docs
1635 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1641 my $plan = $self->plan;
1643 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1647 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1648 my $class = ref($self). "::$plan";
1649 warn "reblessing $self into $class" if $DEBUG > 1;
1652 bless($self, $class) unless $@;
1657 sub calc_setup { die 'no calc_setup for '. shift->plan. "\n"; }
1658 sub calc_recur { die 'no calc_recur for '. shift->plan. "\n"; }
1660 #fallback that return 0 for old legacy packages with no plan
1661 sub calc_remain { 0; }
1662 sub calc_units { 0; }
1664 #fallback for everything not based on flat.pm
1665 sub recur_temporality { 'upcoming'; }
1666 sub calc_cancel { 0; }
1668 #fallback for everything except bulk.pm
1669 sub hide_svc_detail { 0; }
1671 #fallback for packages that can't/won't summarize usage
1672 sub sum_usage { 0; }
1674 =item recur_cost_permonth CUST_PKG
1676 recur_cost divided by freq (only supported for monthly and longer frequencies)
1680 sub recur_cost_permonth {
1681 my($self, $cust_pkg) = @_;
1682 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1683 sprintf('%.2f', ($self->recur_cost || 0) / $self->freq );
1686 =item cust_bill_pkg_recur CUST_PKG
1688 Actual recurring charge for the specified customer package from customer's most
1693 sub cust_bill_pkg_recur {
1694 my($self, $cust_pkg) = @_;
1695 my $cust_bill_pkg = qsearchs({
1696 'table' => 'cust_bill_pkg',
1697 'addl_from' => 'LEFT JOIN cust_bill USING ( invnum )',
1698 'hashref' => { 'pkgnum' => $cust_pkg->pkgnum,
1699 'recur' => { op=>'>', value=>'0' },
1701 'order_by' => 'ORDER BY cust_bill._date DESC,
1702 cust_bill_pkg.sdate DESC
1705 }) or return 0; #die "use cust_bill_pkg_recur credits with once_perinv condition";
1706 $cust_bill_pkg->recur;
1709 =item unit_setup CUST_PKG
1711 Returns the setup fee for one unit of the package.
1716 my ($self, $cust_pkg) = @_;
1717 $self->option('setup_fee') || 0;
1722 unit_setup minus setup_cost
1728 $self->unit_setup(@_) - ($self->setup_cost || 0);
1731 =item recur_margin_permonth
1733 base_recur_permonth minus recur_cost_permonth
1737 sub recur_margin_permonth {
1739 $self->base_recur_permonth(@_) - $self->recur_cost_permonth(@_);
1742 =item intro_end PACKAGE
1744 Takes an L<FS::cust_pkg> object. If this plan has an introductory rate,
1745 returns the expected date the intro period will end. If there is no intro
1754 =item format OPTION DATA
1756 Returns data formatted according to the function 'format' described
1757 in the plan info. Returns DATA if no such function exists.
1762 my ($self, $option, $data) = (shift, shift, shift);
1763 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1764 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1770 =item parse OPTION DATA
1772 Returns data parsed according to the function 'parse' described
1773 in the plan info. Returns DATA if no such function exists.
1778 my ($self, $option, $data) = (shift, shift, shift);
1779 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1780 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1790 =head1 CLASS METHODS
1798 # Used by FS::Upgrade to migrate to a new database.
1800 sub _upgrade_data { # class method
1801 my($class, %opts) = @_;
1803 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1805 my @part_pkg = qsearch({
1806 'table' => 'part_pkg',
1807 'extra_sql' => "WHERE ". join(' OR ',
1808 'plan IS NULL', "plan = '' ",
1812 foreach my $part_pkg (@part_pkg) {
1814 unless ( $part_pkg->plan ) {
1815 $part_pkg->plan('flat');
1821 # the rest can be done asynchronously
1824 sub queueable_upgrade {
1825 # now upgrade to the explicit custom flag
1827 my $search = FS::Cursor->new({
1828 'table' => 'part_pkg',
1829 'hashref' => { disabled => 'Y', custom => '' },
1830 'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1834 while (my $part_pkg = $search->fetch) {
1835 my $new = new FS::part_pkg { $part_pkg->hash };
1837 my $comment = $part_pkg->comment;
1838 $comment =~ s/^\(CUSTOM\) //;
1839 $comment = '(none)' unless $comment =~ /\S/;
1840 $new->comment($comment);
1842 my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1843 my $primary = $part_pkg->svcpart;
1844 my $options = { $part_pkg->options };
1846 my $error = $new->replace( $part_pkg,
1847 'pkg_svc' => $pkg_svc,
1848 'primary_svc' => $primary,
1849 'options' => $options,
1852 warn "pkgpart#".$part_pkg->pkgpart.": $error\n";
1859 # set family_pkgpart on any packages that don't have it
1860 $search = FS::Cursor->new('part_pkg', { 'family_pkgpart' => '' });
1861 while (my $part_pkg = $search->fetch) {
1862 $part_pkg->set('family_pkgpart' => $part_pkg->pkgpart);
1863 my $error = $part_pkg->SUPER::replace;
1865 warn "pkgpart#".$part_pkg->pkgpart.": $error\n";
1872 my @part_pkg_option = qsearch('part_pkg_option',
1873 { 'optionname' => 'unused_credit',
1876 foreach my $old_opt (@part_pkg_option) {
1877 my $pkgpart = $old_opt->pkgpart;
1878 my $error = $old_opt->delete;
1879 die $error if $error;
1881 foreach (qw(unused_credit_cancel unused_credit_change)) {
1882 my $new_opt = new FS::part_pkg_option {
1883 'pkgpart' => $pkgpart,
1887 $error = $new_opt->insert;
1888 die $error if $error;
1892 # migrate use_disposition_taqua and use_disposition to disposition_in
1893 @part_pkg_option = qsearch('part_pkg_option',
1894 { 'optionname' => { op => 'LIKE',
1895 value => 'use_disposition%',
1899 my %newopts = map { $_->pkgpart => $_ }
1900 qsearch('part_pkg_option', { 'optionname' => 'disposition_in', } );
1901 foreach my $old_opt (@part_pkg_option) {
1902 my $pkgpart = $old_opt->pkgpart;
1903 my $newval = $old_opt->optionname eq 'use_disposition_taqua' ? '100'
1905 my $error = $old_opt->delete;
1906 die $error if $error;
1908 if ( exists($newopts{$pkgpart}) ) {
1909 my $opt = $newopts{$pkgpart};
1910 $opt->optionvalue($opt->optionvalue.",$newval");
1911 $error = $opt->replace;
1912 die $error if $error;
1914 my $new_opt = new FS::part_pkg_option {
1915 'pkgpart' => $pkgpart,
1916 'optionname' => 'disposition_in',
1917 'optionvalue' => $newval,
1919 $error = $new_opt->insert;
1920 die $error if $error;
1921 $newopts{$pkgpart} = $new_opt;
1925 # set any package with FCC voice lines to the "VoIP with broadband" category
1926 # for backward compatibility
1928 # recover from a bad upgrade bug
1929 my $upgrade = 'part_pkg_fcc_voip_class_FIX';
1930 if (!FS::upgrade_journal->is_done($upgrade)) {
1931 my $bad_upgrade = qsearchs('upgrade_journal',
1932 { upgrade => 'part_pkg_fcc_voip_class' }
1934 if ( $bad_upgrade ) {
1935 my $where = 'WHERE history_date <= '.$bad_upgrade->_date.
1936 ' AND history_date > '.($bad_upgrade->_date - 3600);
1937 my @h_part_pkg_option = map { FS::part_pkg_option->new($_->hashref) }
1940 'table' => 'h_part_pkg_option',
1942 'extra_sql' => "$where AND history_action = 'delete'",
1943 'order_by' => 'ORDER BY history_date ASC',
1945 my @h_pkg_svc = map { FS::pkg_svc->new($_->hashref) }
1948 'table' => 'h_pkg_svc',
1950 'extra_sql' => "$where AND history_action = 'replace_old'",
1951 'order_by' => 'ORDER BY history_date ASC',
1954 foreach my $deleted (@h_part_pkg_option, @h_pkg_svc) {
1955 my $pkgpart ||= $deleted->pkgpart;
1956 $opt{$pkgpart} ||= {
1962 if ( $deleted->isa('FS::part_pkg_option') ) {
1963 $opt{$pkgpart}{options}{ $deleted->optionname } = $deleted->optionvalue;
1965 my $svcpart = $deleted->svcpart;
1966 $opt{$pkgpart}{pkg_svc}{$svcpart} = $deleted->quantity;
1967 $opt{$pkgpart}{hidden_svc}{$svcpart} ||= $deleted->hidden;
1968 $opt{$pkgpart}{primary_svc} = $svcpart if $deleted->primary_svc;
1971 foreach my $pkgpart (keys %opt) {
1972 my $part_pkg = FS::part_pkg->by_key($pkgpart);
1973 my $error = $part_pkg->replace( $part_pkg->replace_old, $opt{$pkgpart} );
1975 die "error recovering damaged pkgpart $pkgpart:\n$error\n";
1978 } # $bad_upgrade exists
1979 else { # do the original upgrade, but correctly this time
1980 my @part_pkg = qsearch('part_pkg', {
1981 fcc_ds0s => { op => '>', value => 0 },
1982 fcc_voip_class => ''
1984 foreach my $part_pkg (@part_pkg) {
1985 $part_pkg->set(fcc_voip_class => 2);
1986 my @pkg_svc = $part_pkg->pkg_svc;
1987 my %quantity = map {$_->svcpart, $_->quantity} @pkg_svc;
1988 my %hidden = map {$_->svcpart, $_->hidden } @pkg_svc;
1989 my $error = $part_pkg->replace(
1990 $part_pkg->replace_old,
1991 options => { $part_pkg->options },
1992 pkg_svc => \%quantity,
1993 hidden_svc => \%hidden,
1994 primary_svc => ($part_pkg->svcpart || ''),
1996 die $error if $error;
1999 FS::upgrade_journal->set_done($upgrade);
2002 # remove custom flag from one-time charge packages that were accidentally
2004 $search = FS::Cursor->new({
2005 'table' => 'part_pkg',
2006 'hashref' => { 'freq' => '0',
2008 'family_pkgpart' => { op => '!=', value => '' },
2010 'addl_from' => ' JOIN
2011 (select pkgpart from cust_pkg group by pkgpart having count(*) = 1)
2012 AS singular_pkg USING (pkgpart)',
2014 my @fields = grep { $_ ne 'pkgpart'
2016 and $_ ne 'disabled' } FS::part_pkg->fields;
2017 PKGPART: while (my $part_pkg = $search->fetch) {
2018 # can't merge the package back into its parent (too late for that)
2019 # but we can remove the custom flag if it's not actually customized,
2020 # i.e. nothing has been changed.
2022 my $family_pkgpart = $part_pkg->family_pkgpart;
2023 next PKGPART if $family_pkgpart == $part_pkg->pkgpart;
2024 my $parent_pkg = FS::part_pkg->by_key($family_pkgpart);
2025 foreach my $field (@fields) {
2026 if ($part_pkg->get($field) ne $parent_pkg->get($field)) {
2030 # options have to be identical too
2031 # but links, FCC options, discount plans, and usage packages can't be
2032 # changed through the "modify charge" UI, so skip them
2033 my %newopt = $part_pkg->options;
2034 my %oldopt = $parent_pkg->options;
2035 OPTION: foreach my $option (keys %newopt) {
2036 if (delete $newopt{$option} ne delete $oldopt{$option}) {
2040 if (keys(%newopt) or keys(%oldopt)) {
2043 # okay, now replace it
2044 warn "Removing custom flag from part_pkg#".$part_pkg->pkgpart."\n";
2045 $part_pkg->set('custom', '');
2046 my $error = $part_pkg->replace;
2047 die $error if $error;
2053 =item curuser_pkgs_sql
2055 Returns an SQL fragment for searching for packages the current user can
2056 use, either via part_pkg.agentnum directly, or via agent type (see
2061 sub curuser_pkgs_sql {
2064 $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
2068 =item agent_pkgs_sql AGENT | AGENTNUM, ...
2070 Returns an SQL fragment for searching for packages the provided agent or agents
2071 can use, either via part_pkg.agentnum directly, or via agent type (see
2076 sub agent_pkgs_sql {
2077 my $class = shift; #i'm a class method, not a sub (the question is... why??)
2078 my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
2080 $class->_pkgs_sql(@agentnums); #is this why
2085 my( $class, @agentnums ) = @_;
2086 my $agentnums = join(',', @agentnums);
2090 ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
2091 OR ( agentnum IS NULL
2092 AND EXISTS ( SELECT 1
2094 LEFT JOIN agent_type USING ( typenum )
2095 LEFT JOIN agent AS typeagent USING ( typenum )
2096 WHERE type_pkgs.pkgpart = part_pkg.pkgpart
2097 AND typeagent.agentnum IN ($agentnums)
2115 #false laziness w/part_export & cdr
2117 foreach my $INC ( @INC ) {
2118 warn "globbing $INC/FS/part_pkg/[a-z]*.pm\n" if $DEBUG;
2119 foreach my $file ( glob("$INC/FS/part_pkg/[a-z]*.pm") ) {
2120 warn "attempting to load plan info from $file\n" if $DEBUG;
2121 $file =~ /\/(\w+)\.pm$/ or do {
2122 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
2126 my $info = eval "use FS::part_pkg::$mod; ".
2127 "\\%FS::part_pkg::$mod\::info;";
2129 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
2132 unless ( keys %$info ) {
2133 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
2136 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
2137 #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
2138 # warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
2141 $info{$mod} = $info;
2142 $info->{'weight'} ||= 0; # quiet warnings
2146 # copy one level deep to allow replacement of fields and fieldorder
2147 tie %plans, 'Tie::IxHash',
2148 map { my %infohash = %{ $info{$_} };
2150 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
2153 # inheritance of plan options
2154 foreach my $name (keys(%info)) {
2155 if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
2156 warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
2157 delete $plans{$name};
2160 my $parents = $info{$name}->{'inherit_fields'} || [];
2161 my (%fields, %field_exists, @fieldorder);
2162 foreach my $parent ($name, @$parents) {
2163 if ( !exists($info{$parent}) ) {
2164 warn "$name tried to inherit from nonexistent '$parent'\n";
2167 %fields = ( # avoid replacing existing fields
2168 %{ $info{$parent}->{'fields'} || {} },
2171 foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
2173 next if $field_exists{$_};
2174 $field_exists{$_} = 1;
2175 # allow inheritors to remove inherited fields from the fieldorder
2176 push @fieldorder, $_ if !exists($fields{$_}) or
2177 !exists($fields{$_}->{'disabled'});
2180 $plans{$name}->{'fields'} = \%fields;
2181 $plans{$name}->{'fieldorder'} = \@fieldorder;
2191 =head1 NEW PLAN CLASSES
2193 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
2194 found in eg/plan_template.pm. Until then, it is suggested that you use the
2195 other modules in FS/FS/part_pkg/ as a guide.
2199 The delete method is unimplemented.
2201 setup and recur semantics are not yet defined (and are implemented in
2202 FS::cust_bill. hmm.). now they're deprecated and need to go.
2206 part_pkg_taxrate is Pg specific
2208 replace should be smarter about managing the related tables (options, pkg_svc)
2212 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
2213 schema.html from the base documentation.