4 use vars qw( @ISA %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
5 use Carp qw(carp cluck confess);
6 use Scalar::Util qw( blessed );
7 use Time::Local qw( timelocal_nocheck );
10 use FS::Record qw( qsearch qsearchs dbh dbdef );
16 use FS::part_pkg_option;
19 use FS::part_pkg_msgcat;
20 use FS::part_pkg_taxrate;
21 use FS::part_pkg_taxoverride;
22 use FS::part_pkg_taxproduct;
23 use FS::part_pkg_link;
24 use FS::part_pkg_discount;
25 use FS::part_pkg_usage;
26 use FS::part_pkg_vendor;
28 @ISA = qw( FS::m2m_Common FS::option_Common );
31 $skip_pkg_svc_hack = 0;
35 FS::part_pkg - Object methods for part_pkg objects
41 $record = new FS::part_pkg \%hash
42 $record = new FS::part_pkg { 'column' => 'value' };
44 $custom_record = $template_record->clone;
46 $error = $record->insert;
48 $error = $new_record->replace($old_record);
50 $error = $record->delete;
52 $error = $record->check;
54 @pkg_svc = $record->pkg_svc;
56 $svcnum = $record->svcpart;
57 $svcnum = $record->svcpart( 'svc_acct' );
61 An FS::part_pkg object represents a package definition. FS::part_pkg
62 inherits from FS::Record. The following fields are currently supported:
66 =item pkgpart - primary key (assigned automatically for new package definitions)
68 =item pkg - Text name of this package definition (customer-viewable)
70 =item comment - Text name of this package definition (non-customer-viewable)
72 =item classnum - Optional package class (see L<FS::pkg_class>)
74 =item promo_code - Promotional code
76 =item setup - Setup fee expression (deprecated)
78 =item freq - Frequency of recurring fee
80 =item recur - Recurring fee expression (deprecated)
82 =item setuptax - Setup fee tax exempt flag, empty or `Y'
84 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
86 =item taxclass - Tax class
88 =item plan - Price plan
90 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
92 =item disabled - Disabled flag, empty or `Y'
94 =item custom - Custom flag, empty or `Y'
96 =item setup_cost - for cost tracking
98 =item recur_cost - for cost tracking
100 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
102 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
104 =item agentnum - Optional agentnum (see L<FS::agent>)
106 =item fcc_ds0s - Optional DS0 equivalency number for FCC form 477
108 =item fcc_voip_class - Which column of FCC form 477 part II.B this package
111 =item successor - Foreign key for the part_pkg that replaced this record.
112 If this record is not obsolete, will be null.
114 =item family_pkgpart - Foreign key for the part_pkg that was the earliest
115 ancestor of this record. If this record is not a successor to another
116 part_pkg, will be equal to pkgpart.
126 Creates a new package definition. To add the package definition to
127 the database, see L<"insert">.
131 sub table { 'part_pkg'; }
135 An alternate constructor. Creates a new package definition by duplicating
136 an existing definition. A new pkgpart is assigned and the custom flag is
137 set to Y. To add the package definition to the database, see L<"insert">.
143 my $class = ref($self);
144 my %hash = $self->hash;
145 $hash{'pkgpart'} = '';
146 $hash{'custom'} = 'Y';
147 #new FS::part_pkg ( \%hash ); # ?
148 new $class ( \%hash ); # ?
151 =item insert [ , OPTION => VALUE ... ]
153 Adds this package definition to the database. If there is an error,
154 returns the error, otherwise returns false.
156 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
157 I<custnum_ref> and I<options>.
159 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
160 values, appropriate FS::pkg_svc records will be inserted. I<hidden_svc> can
161 be set to a hashref of svcparts and flag values ('Y' or '') to set the
162 'hidden' field in these records.
164 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
165 FS::pkg_svc record will be updated.
167 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
168 record itself), the object will be updated to point to this package definition.
170 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
171 the scalar will be updated with the custnum value from the cust_pkg record.
173 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
174 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
175 records will be inserted.
177 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
178 records will be inserted.
185 warn "FS::part_pkg::insert called on $self with options ".
186 join(', ', map "$_=>$options{$_}", keys %options)
189 local $SIG{HUP} = 'IGNORE';
190 local $SIG{INT} = 'IGNORE';
191 local $SIG{QUIT} = 'IGNORE';
192 local $SIG{TERM} = 'IGNORE';
193 local $SIG{TSTP} = 'IGNORE';
194 local $SIG{PIPE} = 'IGNORE';
196 my $oldAutoCommit = $FS::UID::AutoCommit;
197 local $FS::UID::AutoCommit = 0;
200 warn " inserting part_pkg record" if $DEBUG;
201 my $error = $self->SUPER::insert( $options{options} );
203 $dbh->rollback if $oldAutoCommit;
208 if ( $self->get('family_pkgpart') eq '' ) {
209 $self->set('family_pkgpart' => $self->pkgpart);
210 $error = $self->SUPER::replace;
212 $dbh->rollback if $oldAutoCommit;
217 my $conf = new FS::Conf;
218 if ( $conf->exists('agent_defaultpkg') ) {
219 warn " agent_defaultpkg set; allowing all agents to purchase package"
221 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
222 my $type_pkgs = new FS::type_pkgs({
223 'typenum' => $agent_type->typenum,
224 'pkgpart' => $self->pkgpart,
226 my $error = $type_pkgs->insert;
228 $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 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
260 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
262 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
266 my $pkg_svc = new FS::pkg_svc( {
267 'pkgpart' => $self->pkgpart,
268 'svcpart' => $part_svc->svcpart,
269 'quantity' => $quantity,
270 'primary_svc' => $primary_svc,
271 'hidden' => $hidden_svc->{$part_svc->svcpart},
273 my $error = $pkg_svc->insert;
275 $dbh->rollback if $oldAutoCommit;
282 if ( $options{'cust_pkg'} ) {
283 warn " updating cust_pkg record " if $DEBUG;
285 ref($options{'cust_pkg'})
286 ? $options{'cust_pkg'}
287 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
288 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
289 if $options{'custnum_ref'};
290 my %hash = $old_cust_pkg->hash;
291 $hash{'pkgpart'} = $self->pkgpart,
292 my $new_cust_pkg = new FS::cust_pkg \%hash;
293 local($FS::cust_pkg::disable_agentcheck) = 1;
294 my $error = $new_cust_pkg->replace($old_cust_pkg);
296 $dbh->rollback if $oldAutoCommit;
297 return "Error modifying cust_pkg record: $error";
301 if ( $options{'part_pkg_vendor'} ) {
302 while ( my ($exportnum, $vendor_pkg_id) =
303 each %{ $options{part_pkg_vendor} }
306 my $ppv = new FS::part_pkg_vendor( {
307 'pkgpart' => $self->pkgpart,
308 'exportnum' => $exportnum,
309 'vendor_pkg_id' => $vendor_pkg_id,
311 my $error = $ppv->insert;
313 $dbh->rollback if $oldAutoCommit;
314 return "Error inserting part_pkg_vendor record: $error";
319 warn " committing transaction" if $DEBUG and $oldAutoCommit;
320 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
327 Currently unimplemented.
332 return "Can't (yet?) delete package definitions.";
333 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
336 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
338 Replaces OLD_RECORD with this one in the database. If there is an error,
339 returns the error, otherwise returns false.
341 Currently available options are: I<pkg_svc>, I<hidden_svc>, I<primary_svc>
344 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
345 values, the appropriate FS::pkg_svc records will be replaced. I<hidden_svc>
346 can be set to a hashref of svcparts and flag values ('Y' or '') to set the
347 'hidden' field in these records.
349 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
350 FS::pkg_svc record will be updated.
352 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
360 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
365 ( ref($_[0]) eq 'HASH' )
369 $options->{options} = { $old->options } unless defined($options->{options});
371 warn "FS::part_pkg::replace called on $new to replace $old with options".
372 join(', ', map "$_ => ". $options->{$_}, keys %$options)
375 local $SIG{HUP} = 'IGNORE';
376 local $SIG{INT} = 'IGNORE';
377 local $SIG{QUIT} = 'IGNORE';
378 local $SIG{TERM} = 'IGNORE';
379 local $SIG{TSTP} = 'IGNORE';
380 local $SIG{PIPE} = 'IGNORE';
382 my $oldAutoCommit = $FS::UID::AutoCommit;
383 local $FS::UID::AutoCommit = 0;
386 my $conf = new FS::Conf;
387 if ( $conf->exists('part_pkg-lineage') ) {
388 if ( grep { $options->{options}->{$_} ne $old->option($_, 1) }
389 qw(setup_fee recur_fee) #others? config?
392 warn " superseding package" if $DEBUG;
394 my $error = $new->supersede($old, %$options);
396 $dbh->rollback if $oldAutoCommit;
400 warn " committing transaction" if $DEBUG and $oldAutoCommit;
401 $dbh->commit if $oldAutoCommit;
408 #plandata shit stays in replace for upgrades until after 2.0 (or edit
410 warn " saving legacy plandata" if $DEBUG;
411 my $plandata = $new->get('plandata');
412 $new->set('plandata', '');
414 warn " deleting old part_pkg_option records" if $DEBUG;
415 foreach my $part_pkg_option ( $old->part_pkg_option ) {
416 my $error = $part_pkg_option->delete;
418 $dbh->rollback if $oldAutoCommit;
423 warn " replacing part_pkg record" if $DEBUG;
424 my $error = $new->SUPER::replace($old, $options->{options} );
426 $dbh->rollback if $oldAutoCommit;
430 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
431 foreach my $part_pkg_option (
432 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
433 return "illegal plandata: $plandata";
435 new FS::part_pkg_option {
436 'pkgpart' => $new->pkgpart,
441 split("\n", $plandata)
443 my $error = $part_pkg_option->insert;
445 $dbh->rollback if $oldAutoCommit;
450 warn " replacing pkg_svc records" if $DEBUG;
451 my $pkg_svc = $options->{'pkg_svc'};
452 my $hidden_svc = $options->{'hidden_svc'} || {};
453 if ( $pkg_svc ) { # if it wasn't passed, don't change existing pkg_svcs
454 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
455 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
456 my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
458 ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
459 && $options->{'primary_svc'} == $part_svc->svcpart
464 my $old_pkg_svc = qsearchs('pkg_svc', {
465 'pkgpart' => $old->pkgpart,
466 'svcpart' => $part_svc->svcpart,
469 my $old_quantity = 0;
470 my $old_primary_svc = '';
472 if ( $old_pkg_svc ) {
473 $old_quantity = $old_pkg_svc->quantity;
474 $old_primary_svc = $old_pkg_svc->primary_svc
475 if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
476 $old_hidden = $old_pkg_svc->hidden;
479 next unless $old_quantity != $quantity ||
480 $old_primary_svc ne $primary_svc ||
481 $old_hidden ne $hidden;
483 my $new_pkg_svc = new FS::pkg_svc( {
484 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
485 'pkgpart' => $new->pkgpart,
486 'svcpart' => $part_svc->svcpart,
487 'quantity' => $quantity,
488 'primary_svc' => $primary_svc,
491 my $error = $old_pkg_svc
492 ? $new_pkg_svc->replace($old_pkg_svc)
493 : $new_pkg_svc->insert;
495 $dbh->rollback if $oldAutoCommit;
499 } #if $options->{pkg_svc}
501 my @part_pkg_vendor = $old->part_pkg_vendor;
502 my @current_exportnum = ();
503 if ( $options->{'part_pkg_vendor'} ) {
504 my($exportnum,$vendor_pkg_id);
505 while ( ($exportnum,$vendor_pkg_id)
506 = each %{$options->{'part_pkg_vendor'}} ) {
508 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
509 if($exportnum == $part_pkg_vendor->exportnum
510 && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
511 $part_pkg_vendor->vendor_pkg_id($vendor_pkg_id);
512 my $error = $part_pkg_vendor->replace;
514 $dbh->rollback if $oldAutoCommit;
515 return "Error replacing part_pkg_vendor record: $error";
520 elsif($exportnum == $part_pkg_vendor->exportnum
521 && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
526 unless ( $noinsert ) {
527 my $ppv = new FS::part_pkg_vendor( {
528 'pkgpart' => $new->pkgpart,
529 'exportnum' => $exportnum,
530 'vendor_pkg_id' => $vendor_pkg_id,
532 my $error = $ppv->insert;
534 $dbh->rollback if $oldAutoCommit;
535 return "Error inserting part_pkg_vendor record: $error";
538 push @current_exportnum, $exportnum;
541 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
542 unless ( grep($_ eq $part_pkg_vendor->exportnum, @current_exportnum) ) {
543 my $error = $part_pkg_vendor->delete;
545 $dbh->rollback if $oldAutoCommit;
546 return "Error deleting part_pkg_vendor record: $error";
551 # propagate changes to certain core fields
552 if ( $conf->exists('part_pkg-lineage') ) {
553 warn " propagating changes to family" if $DEBUG;
554 my $error = $new->propagate($old);
556 $dbh->rollback if $oldAutoCommit;
561 warn " committing transaction" if $DEBUG and $oldAutoCommit;
562 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
568 Checks all fields to make sure this is a valid package definition. If
569 there is an error, returns the error, otherwise returns false. Called by the
570 insert and replace methods.
576 warn "FS::part_pkg::check called on $self" if $DEBUG;
578 for (qw(setup recur plandata)) {
579 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
580 return "Use of $_ field is deprecated; set a plan and options: ".
582 if length($self->get($_));
586 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
587 my $error = $self->ut_number('freq');
588 return $error if $error;
590 $self->freq =~ /^(\d+[hdw]?)$/
591 or return "Illegal or empty freq: ". $self->freq;
595 my @null_agentnum_right = ( 'Edit global package definitions' );
596 push @null_agentnum_right, 'One-time charge'
597 if $self->freq =~ /^0/;
598 push @null_agentnum_right, 'Customize customer package'
599 if $self->disabled eq 'Y'; #good enough
601 my $error = $self->ut_numbern('pkgpart')
602 || $self->ut_text('pkg')
603 || $self->ut_text('comment')
604 || $self->ut_textn('promo_code')
605 || $self->ut_alphan('plan')
606 || $self->ut_enum('setuptax', [ '', 'Y' ] )
607 || $self->ut_enum('recurtax', [ '', 'Y' ] )
608 || $self->ut_textn('taxclass')
609 || $self->ut_enum('disabled', [ '', 'Y' ] )
610 || $self->ut_enum('custom', [ '', 'Y' ] )
611 || $self->ut_enum('no_auto', [ '', 'Y' ])
612 || $self->ut_enum('recur_show_zero', [ '', 'Y' ])
613 || $self->ut_enum('setup_show_zero', [ '', 'Y' ])
614 #|| $self->ut_moneyn('setup_cost')
615 #|| $self->ut_moneyn('recur_cost')
616 || $self->ut_floatn('setup_cost')
617 || $self->ut_floatn('recur_cost')
618 || $self->ut_floatn('pay_weight')
619 || $self->ut_floatn('credit_weight')
620 || $self->ut_numbern('taxproductnum')
621 || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum')
622 || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
623 || $self->ut_foreign_keyn('taxproductnum',
624 'part_pkg_taxproduct',
628 ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
629 : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
631 || $self->ut_numbern('fcc_ds0s')
632 || $self->ut_numbern('fcc_voip_class')
633 || $self->ut_foreign_keyn('successor', 'part_pkg', 'pkgpart')
634 || $self->ut_foreign_keyn('family_pkgpart', 'part_pkg', 'pkgpart')
635 || $self->SUPER::check
637 return $error if $error;
639 return 'Unknown plan '. $self->plan
640 unless exists($plans{$self->plan});
642 my $conf = new FS::Conf;
643 return 'Taxclass is required'
644 if ! $self->taxclass && $conf->exists('require_taxclasses');
649 =item supersede OLD [, OPTION => VALUE ... ]
651 Inserts this package as a successor to the package OLD. All options are as
652 for C<insert>. After inserting, disables OLD and sets the new package as its
658 my ($new, $old, %options) = @_;
661 $new->set('pkgpart' => '');
662 $new->set('family_pkgpart' => $old->family_pkgpart);
663 warn " inserting successor package\n" if $DEBUG;
664 $error = $new->insert(%options);
665 return $error if $error;
667 warn " disabling superseded package\n" if $DEBUG;
668 $old->set('successor' => $new->pkgpart);
669 $old->set('disabled' => 'Y');
670 $error = $old->SUPER::replace; # don't change its options/pkg_svc records
671 return $error if $error;
673 warn " propagating changes to family" if $DEBUG;
674 $new->propagate($old);
679 If any of certain fields have changed from OLD to this package, then,
680 for all packages in the same lineage as this one, sets those fields
681 to their values in this package.
685 my @propagate_fields = (
686 qw( pkg classnum setup_cost recur_cost taxclass
687 setuptax recurtax pay_weight credit_weight
695 map { $_ => $new->get($_) }
696 grep { $new->get($_) ne $old->get($_) }
700 my @part_pkg = qsearch('part_pkg', {
701 'family_pkgpart' => $new->family_pkgpart
704 foreach my $part_pkg ( @part_pkg ) {
705 my $pkgpart = $part_pkg->pkgpart;
706 next if $pkgpart == $new->pkgpart; # don't modify $new
707 warn " propagating to pkgpart $pkgpart\n" if $DEBUG;
708 foreach ( keys %fields ) {
709 $part_pkg->set($_, $fields{$_});
711 # SUPER::replace to avoid changing non-core fields
712 my $error = $part_pkg->SUPER::replace;
713 push @error, "pkgpart $pkgpart: $error"
719 =item pkg_locale LOCALE
721 Returns a customer-viewable string representing this package for the given
722 locale, from the part_pkg_msgcat table. If no localized string is found,
723 returns the base pkg field.
728 my( $self, $locale ) = @_;
729 my $part_pkg_msgcat = qsearchs( 'part_pkg_msgcat', { pkgpart=>$self->pkgpart,
731 or return $self->pkg;
732 $part_pkg_msgcat->pkg;
735 =item pkg_comment [ OPTION => VALUE... ]
737 Returns an (internal) string representing this package. Currently,
738 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
739 future, omitting pkgpart. The comment will have '(CUSTOM) ' prepended if
742 If the option nopkgpart is true then the "pkgpart: ' is omitted.
750 #$self->pkg. ' - '. $self->comment;
751 #$self->pkg. ' ('. $self->comment. ')';
752 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
753 $pre. $self->pkg. ' - '. $self->custom_comment;
756 sub price_info { # safety, in case a part_pkg hasn't defined price_info
762 ( $self->custom ? '(CUSTOM) ' : '' ). $self->comment . ' ' . $self->price_info;
767 Returns the package class, as an FS::pkg_class object, or the empty string
768 if there is no package class.
774 if ( $self->classnum ) {
775 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
781 =item addon_pkg_class
783 Returns the add-on package class, as an FS::pkg_class object, or the empty
784 string if there is no add-on package class.
788 sub addon_pkg_class {
790 if ( $self->addon_classnum ) {
791 qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
799 Returns the package category name, or the empty string if there is no package
806 my $pkg_class = $self->pkg_class;
808 ? $pkg_class->categoryname
814 Returns the package class name, or the empty string if there is no package
821 my $pkg_class = $self->pkg_class;
823 ? $pkg_class->classname
827 =item addon_classname
829 Returns the add-on package class name, or the empty string if there is no
830 add-on package class.
834 sub addon_classname {
836 my $pkg_class = $self->addon_pkg_class;
838 ? $pkg_class->classname
844 Returns the associated agent for this event, if any, as an FS::agent object.
850 qsearchs('agent', { 'agentnum' => $self->agentnum } );
853 =item pkg_svc [ HASHREF | OPTION => VALUE ]
855 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
856 definition (with non-zero quantity).
858 One option is available, I<disable_linked>. If set true it will return the
859 services for this package definition alone, omitting services from any add-on
866 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
873 qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
879 # #sort { $b->primary cmp $a->primary }
880 # grep { $_->quantity }
881 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
883 my $opt = ref($_[0]) ? $_[0] : { @_ };
884 my %pkg_svc = map { $_->svcpart => $_ }
885 grep { $_->quantity }
886 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
888 unless ( $opt->{disable_linked} ) {
889 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
890 my @pkg_svc = grep { $_->quantity }
891 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
892 foreach my $pkg_svc ( @pkg_svc ) {
893 if ( $pkg_svc{$pkg_svc->svcpart} ) {
894 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
895 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
897 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
907 =item svcpart [ SVCDB ]
909 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
910 associated with this package definition (see L<FS::pkg_svc>). Returns
911 false if there not a primary service definition or exactly one service
912 definition with quantity 1, or if SVCDB is specified and does not match the
913 svcdb of the service definition. SVCDB can be specified as a scalar table
914 name, such as 'svc_acct', or as an arrayref of possible table names.
919 my $pkg_svc = shift->_primary_pkg_svc(@_);
920 $pkg_svc ? $pkg_svc->svcpart : '';
923 =item part_svc [ SVCDB ]
925 Like the B<svcpart> method, but returns the FS::part_svc object (see
931 my $pkg_svc = shift->_primary_pkg_svc(@_);
932 $pkg_svc ? $pkg_svc->part_svc : '';
935 sub _primary_pkg_svc {
938 my $svcdb = scalar(@_) ? shift : [];
939 $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
940 my %svcdb = map { $_=>1 } @$svcdb;
943 grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
946 my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
947 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
949 return '' if scalar(@pkg_svc) != 1;
953 =item svcpart_unique_svcdb SVCDB
955 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
956 SVCDB associated with this package definition (see L<FS::pkg_svc>). Returns
957 false if there not a primary service definition for SVCDB or there are multiple
958 service definitions for SVCDB.
962 sub svcpart_unique_svcdb {
963 my( $self, $svcdb ) = @_;
964 my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
965 return '' if scalar(@svcdb_pkg_svc) != 1;
966 $svcdb_pkg_svc[0]->svcpart;
971 Returns a list of the acceptable payment types for this package. Eventually
972 this should come out of a database table and be editable, but currently has the
973 following logic instead:
975 If the package is free, the single item B<BILL> is
976 returned, otherwise, the single item B<CARD> is returned.
978 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
984 if ( $self->is_free ) {
993 Returns true if this package is free.
999 if ( $self->can('is_free_options') ) {
1000 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
1001 map { $self->option($_) }
1002 $self->is_free_options;
1004 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
1005 "provides neither is_free_options nor is_free method; returning false";
1010 sub can_discount { 0; }
1012 sub can_start_date { 1; }
1015 # moved to FS::Misc to make this accessible to other packages
1017 FS::Misc::pkg_freqs();
1022 Returns an english representation of the I<freq> field, such as "monthly",
1023 "weekly", "semi-annually", etc.
1029 my $freq = $self->freq;
1031 #my $freqs_href = $self->freqs_href;
1032 my $freqs_href = freqs_href();
1034 if ( exists($freqs_href->{$freq}) ) {
1035 $freqs_href->{$freq};
1037 my $interval = 'month';
1038 if ( $freq =~ /^(\d+)([hdw])$/ ) {
1039 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
1040 $interval = $interval{$2};
1045 "every $freq ${interval}s";
1050 =item add_freq TIMESTAMP [ FREQ ]
1052 Adds a billing period of some frequency to the provided timestamp and
1053 returns the resulting timestamp, or -1 if the frequency could not be
1054 parsed (shouldn't happen). By default, the frequency of this package
1055 will be used; to override this, pass a different frequency as a second
1061 my( $self, $date, $freq ) = @_;
1062 $freq = $self->freq unless $freq;
1064 #change this bit to use Date::Manip? CAREFUL with timezones (see
1065 # mailing list archive)
1066 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
1068 if ( $freq =~ /^\d+$/ ) {
1070 until ( $mon < 12 ) { $mon -= 12; $year++; }
1071 } elsif ( $freq =~ /^(\d+)w$/ ) {
1073 $mday += $weeks * 7;
1074 } elsif ( $freq =~ /^(\d+)d$/ ) {
1077 } elsif ( $freq =~ /^(\d+)h$/ ) {
1084 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
1089 For backwards compatibility, returns the plandata field as well as all options
1090 from FS::part_pkg_option.
1096 carp "plandata is deprecated";
1098 $self->SUPER::plandata(@_);
1100 my $plandata = $self->get('plandata');
1101 my %options = $self->options;
1102 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
1107 =item part_pkg_vendor
1109 Returns all vendor/external package ids as FS::part_pkg_vendor objects (see
1110 L<FS::part_pkg_vendor>).
1114 sub part_pkg_vendor {
1116 qsearch('part_pkg_vendor', { 'pkgpart' => $self->pkgpart } );
1119 =item vendor_pkg_ids
1121 Returns a list of vendor/external package ids by exportnum
1125 sub vendor_pkg_ids {
1127 map { $_->exportnum => $_->vendor_pkg_id } $self->part_pkg_vendor;
1130 =item part_pkg_option
1132 Returns all options as FS::part_pkg_option objects (see
1133 L<FS::part_pkg_option>).
1137 sub part_pkg_option {
1139 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
1144 Returns a list of option names and values suitable for assigning to a hash.
1150 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
1153 =item option OPTIONNAME [ QUIET ]
1155 Returns the option value for the given name, or the empty string. If a true
1156 value is passed as the second argument, warnings about missing the option
1162 my( $self, $opt, $ornull ) = @_;
1163 my $part_pkg_option =
1164 qsearchs('part_pkg_option', {
1165 pkgpart => $self->pkgpart,
1168 return $part_pkg_option->optionvalue if $part_pkg_option;
1169 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
1170 split("\n", $self->get('plandata') );
1171 return $plandata{$opt} if exists $plandata{$opt};
1172 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
1173 "not found in options or plandata!\n"
1178 =item bill_part_pkg_link
1180 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1184 sub bill_part_pkg_link {
1185 shift->_part_pkg_link('bill', @_);
1188 =item svc_part_pkg_link
1190 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1194 sub svc_part_pkg_link {
1195 shift->_part_pkg_link('svc', @_);
1198 =item supp_part_pkg_link
1200 Returns the associated part_pkg_link records of type 'supp' (supplemental
1205 sub supp_part_pkg_link {
1206 shift->_part_pkg_link('supp', @_);
1209 sub _part_pkg_link {
1210 my( $self, $type ) = @_;
1211 qsearch({ table => 'part_pkg_link',
1212 hashref => { 'src_pkgpart' => $self->pkgpart,
1213 'link_type' => $type,
1214 #protection against infinite recursive links
1215 'dst_pkgpart' => { op=>'!=', value=> $self->pkgpart },
1217 order_by => "ORDER BY hidden",
1221 sub self_and_bill_linked {
1222 shift->_self_and_linked('bill', @_);
1225 sub self_and_svc_linked {
1226 shift->_self_and_linked('svc', @_);
1229 sub _self_and_linked {
1230 my( $self, $type, $hidden ) = @_;
1234 foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
1235 $self->_part_pkg_link($type) ) )
1237 $_->hidden($hidden) if $hidden;
1244 =item part_pkg_taxoverride [ CLASS ]
1246 Returns all associated FS::part_pkg_taxoverride objects (see
1247 L<FS::part_pkg_taxoverride>). Limits the returned set to those
1248 of class CLASS if defined. Class may be one of 'setup', 'recur',
1249 the empty string (default), or a usage class number (see L<FS::usage_class>).
1250 When a class is specified, the empty string class (default) is returned
1251 if no more specific values exist.
1255 sub part_pkg_taxoverride {
1259 my $hashref = { 'pkgpart' => $self->pkgpart };
1260 $hashref->{'usage_class'} = $class if defined($class);
1261 my @overrides = qsearch('part_pkg_taxoverride', $hashref );
1263 unless ( scalar(@overrides) || !defined($class) || !$class ){
1264 $hashref->{'usage_class'} = '';
1265 @overrides = qsearch('part_pkg_taxoverride', $hashref );
1271 =item has_taxproduct
1273 Returns true if this package has any taxproduct associated with it.
1277 sub has_taxproduct {
1280 $self->taxproductnum ||
1281 scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) }
1282 keys %{ {$self->options} }
1288 =item taxproduct [ CLASS ]
1290 Returns the associated tax product for this package definition (see
1291 L<FS::part_pkg_taxproduct>). CLASS may be one of 'setup', 'recur' or
1292 the usage classnum (see L<FS::usage_class>). Returns the default
1293 tax product for this record if the more specific CLASS value does
1302 my $part_pkg_taxproduct;
1304 my $taxproductnum = $self->taxproductnum;
1306 my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1307 $taxproductnum = $class_taxproductnum
1308 if $class_taxproductnum
1311 $part_pkg_taxproduct =
1312 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1314 unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1315 $taxproductnum = $self->taxproductnum;
1316 $part_pkg_taxproduct =
1317 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1320 $part_pkg_taxproduct;
1323 =item taxproduct_description [ CLASS ]
1325 Returns the description of the associated tax product for this package
1326 definition (see L<FS::part_pkg_taxproduct>).
1330 sub taxproduct_description {
1332 my $part_pkg_taxproduct = $self->taxproduct(@_);
1333 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1336 =item part_pkg_taxrate DATA_PROVIDER, GEOCODE, [ CLASS ]
1338 Returns the package to taxrate m2m records for this package in the location
1339 specified by GEOCODE (see L<FS::part_pkg_taxrate>) and usage class CLASS.
1340 CLASS may be one of 'setup', 'recur', or one of the usage classes numbers
1341 (see L<FS::usage_class>).
1345 sub _expand_cch_taxproductnum {
1348 my $part_pkg_taxproduct = $self->taxproduct($class);
1350 my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
1351 ? ( split ':', $part_pkg_taxproduct->taxproduct )
1354 $a = '' unless $a; $b = '' unless $b; $c = '' unless $c; $d = '' unless $d;
1355 my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
1356 OR taxproduct = '$a:$b:$c:'
1357 OR taxproduct = '$a:$b:".":$d'
1358 OR taxproduct = '$a:$b:".":' )";
1359 map { $_->taxproductnum } qsearch( { 'table' => 'part_pkg_taxproduct',
1360 'hashref' => { 'data_vendor'=>'cch' },
1361 'extra_sql' => $extra_sql,
1366 sub part_pkg_taxrate {
1368 my ($data_vendor, $geocode, $class) = @_;
1371 my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
1372 dbh->quote($data_vendor);
1374 # CCH oddness in m2m
1375 $extra_sql .= ' AND ('.
1376 join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) }
1380 # much more CCH oddness in m2m -- this is kludgy
1381 my @tpnums = $self->_expand_cch_taxproductnum($class);
1382 if (scalar(@tpnums)) {
1383 $extra_sql .= ' AND ('.
1384 join(' OR ', map{ "taxproductnum = $_" } @tpnums ).
1387 $extra_sql .= ' AND ( 0 = 1 )';
1390 my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
1391 my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
1392 my $select = 'DISTINCT ON(taxclassnum) *, taxproduct';
1394 # should qsearch preface columns with the table to facilitate joins?
1395 qsearch( { 'table' => 'part_pkg_taxrate',
1396 'select' => $select,
1397 'hashref' => { # 'data_vendor' => $data_vendor,
1398 # 'taxproductnum' => $self->taxproductnum,
1400 'addl_from' => $addl_from,
1401 'extra_sql' => $extra_sql,
1402 'order_by' => $order_by,
1406 =item part_pkg_discount
1408 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1413 sub part_pkg_discount {
1415 qsearch('part_pkg_discount', { 'pkgpart' => $self->pkgpart });
1418 =item part_pkg_usage
1420 Returns the voice usage pools (see L<FS::part_pkg_usage>) defined for
1425 sub part_pkg_usage {
1427 qsearch('part_pkg_usage', { 'pkgpart' => $self->pkgpart });
1432 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1433 PLAN is the object's I<plan> field. There should be better docs
1434 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1440 my $plan = $self->plan;
1442 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1446 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1447 my $class = ref($self). "::$plan";
1448 warn "reblessing $self into $class" if $DEBUG > 1;
1451 bless($self, $class) unless $@;
1456 sub calc_setup { die 'no calc_setup for '. shift->plan. "\n"; }
1457 sub calc_recur { die 'no calc_recur for '. shift->plan. "\n"; }
1459 #fallback that return 0 for old legacy packages with no plan
1460 sub calc_remain { 0; }
1461 sub calc_units { 0; }
1463 #fallback for everything not based on flat.pm
1464 sub recur_temporality { 'upcoming'; }
1465 sub calc_cancel { 0; }
1467 #fallback for everything except bulk.pm
1468 sub hide_svc_detail { 0; }
1470 #fallback for packages that can't/won't summarize usage
1471 sub sum_usage { 0; }
1473 =item recur_cost_permonth CUST_PKG
1475 recur_cost divided by freq (only supported for monthly and longer frequencies)
1479 sub recur_cost_permonth {
1480 my($self, $cust_pkg) = @_;
1481 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1482 sprintf('%.2f', $self->recur_cost / $self->freq );
1485 =item cust_bill_pkg_recur CUST_PKG
1487 Actual recurring charge for the specified customer package from customer's most
1492 sub cust_bill_pkg_recur {
1493 my($self, $cust_pkg) = @_;
1494 my $cust_bill_pkg = qsearchs({
1495 'table' => 'cust_bill_pkg',
1496 'addl_from' => 'LEFT JOIN cust_bill USING ( invnum )',
1497 'hashref' => { 'pkgnum' => $cust_pkg->pkgnum,
1498 'recur' => { op=>'>', value=>'0' },
1500 'order_by' => 'ORDER BY cust_bill._date DESC,
1501 cust_bill_pkg.sdate DESC
1504 }) or return 0; #die "use cust_bill_pkg_recur credits with once_perinv condition";
1505 $cust_bill_pkg->recur;
1508 =item format OPTION DATA
1510 Returns data formatted according to the function 'format' described
1511 in the plan info. Returns DATA if no such function exists.
1516 my ($self, $option, $data) = (shift, shift, shift);
1517 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1518 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1524 =item parse OPTION DATA
1526 Returns data parsed according to the function 'parse' described
1527 in the plan info. Returns DATA if no such function exists.
1532 my ($self, $option, $data) = (shift, shift, shift);
1533 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1534 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1544 =head1 CLASS METHODS
1552 # Used by FS::Upgrade to migrate to a new database.
1554 sub _upgrade_data { # class method
1555 my($class, %opts) = @_;
1557 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1559 my @part_pkg = qsearch({
1560 'table' => 'part_pkg',
1561 'extra_sql' => "WHERE ". join(' OR ',
1562 'plan IS NULL', "plan = '' ",
1566 foreach my $part_pkg (@part_pkg) {
1568 unless ( $part_pkg->plan ) {
1569 $part_pkg->plan('flat');
1576 # now upgrade to the explicit custom flag
1578 @part_pkg = qsearch({
1579 'table' => 'part_pkg',
1580 'hashref' => { disabled => 'Y', custom => '' },
1581 'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1584 foreach my $part_pkg (@part_pkg) {
1585 my $new = new FS::part_pkg { $part_pkg->hash };
1587 my $comment = $part_pkg->comment;
1588 $comment =~ s/^\(CUSTOM\) //;
1589 $comment = '(none)' unless $comment =~ /\S/;
1590 $new->comment($comment);
1592 my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1593 my $primary = $part_pkg->svcpart;
1594 my $options = { $part_pkg->options };
1596 my $error = $new->replace( $part_pkg,
1597 'pkg_svc' => $pkg_svc,
1598 'primary_svc' => $primary,
1599 'options' => $options,
1601 die $error if $error;
1604 # set family_pkgpart on any packages that don't have it
1605 @part_pkg = qsearch('part_pkg', { 'family_pkgpart' => '' });
1606 foreach my $part_pkg (@part_pkg) {
1607 $part_pkg->set('family_pkgpart' => $part_pkg->pkgpart);
1608 my $error = $part_pkg->SUPER::replace;
1609 die $error if $error;
1612 my @part_pkg_option = qsearch('part_pkg_option',
1613 { 'optionname' => 'unused_credit',
1616 foreach my $old_opt (@part_pkg_option) {
1617 my $pkgpart = $old_opt->pkgpart;
1618 my $error = $old_opt->delete;
1619 die $error if $error;
1621 foreach (qw(unused_credit_cancel unused_credit_change)) {
1622 my $new_opt = new FS::part_pkg_option {
1623 'pkgpart' => $pkgpart,
1627 $error = $new_opt->insert;
1628 die $error if $error;
1632 # migrate use_disposition_taqua and use_disposition to disposition_in
1633 @part_pkg_option = qsearch('part_pkg_option',
1634 { 'optionname' => { op => 'LIKE',
1635 value => 'use_disposition%',
1639 my %newopts = map { $_->pkgpart => $_ }
1640 qsearch('part_pkg_option', { 'optionname' => 'disposition_in', } );
1641 foreach my $old_opt (@part_pkg_option) {
1642 my $pkgpart = $old_opt->pkgpart;
1643 my $newval = $old_opt->optionname eq 'use_disposition_taqua' ? '100'
1645 my $error = $old_opt->delete;
1646 die $error if $error;
1648 if ( exists($newopts{$pkgpart}) ) {
1649 my $opt = $newopts{$pkgpart};
1650 $opt->optionvalue($opt->optionvalue.",$newval");
1651 $error = $opt->replace;
1652 die $error if $error;
1654 my $new_opt = new FS::part_pkg_option {
1655 'pkgpart' => $pkgpart,
1656 'optionname' => 'disposition_in',
1657 'optionvalue' => $newval,
1659 $error = $new_opt->insert;
1660 die $error if $error;
1661 $newopts{$pkgpart} = $new_opt;
1665 # set any package with FCC voice lines to the "VoIP with broadband" category
1666 # for backward compatibility
1668 # recover from a bad upgrade bug
1669 my $upgrade = 'part_pkg_fcc_voip_class_FIX';
1670 if (!FS::upgrade_journal->is_done($upgrade)) {
1671 my $bad_upgrade = qsearchs('upgrade_journal',
1672 { upgrade => 'part_pkg_fcc_voip_class' }
1674 if ( $bad_upgrade ) {
1675 my $where = 'WHERE history_date <= '.$bad_upgrade->_date.
1676 ' AND history_date > '.($bad_upgrade->_date - 3600);
1677 my @h_part_pkg_option = map { FS::part_pkg_option->new($_->hashref) }
1680 'table' => 'h_part_pkg_option',
1682 'extra_sql' => "$where AND history_action = 'delete'",
1683 'order_by' => 'ORDER BY history_date ASC',
1685 my @h_pkg_svc = map { FS::pkg_svc->new($_->hashref) }
1688 'table' => 'h_pkg_svc',
1690 'extra_sql' => "$where AND history_action = 'replace_old'",
1691 'order_by' => 'ORDER BY history_date ASC',
1694 foreach my $deleted (@h_part_pkg_option, @h_pkg_svc) {
1695 my $pkgpart ||= $deleted->pkgpart;
1696 $opt{$pkgpart} ||= {
1702 if ( $deleted->isa('FS::part_pkg_option') ) {
1703 $opt{$pkgpart}{options}{ $deleted->optionname } = $deleted->optionvalue;
1705 my $svcpart = $deleted->svcpart;
1706 $opt{$pkgpart}{pkg_svc}{$svcpart} = $deleted->quantity;
1707 $opt{$pkgpart}{hidden_svc}{$svcpart} ||= $deleted->hidden;
1708 $opt{$pkgpart}{primary_svc} = $svcpart if $deleted->primary_svc;
1711 foreach my $pkgpart (keys %opt) {
1712 my $part_pkg = FS::part_pkg->by_key($pkgpart);
1713 my $error = $part_pkg->replace( $part_pkg->replace_old, $opt{$pkgpart} );
1715 die "error recovering damaged pkgpart $pkgpart:\n$error\n";
1718 } # $bad_upgrade exists
1719 else { # do the original upgrade, but correctly this time
1720 @part_pkg = qsearch('part_pkg', {
1721 fcc_ds0s => { op => '>', value => 0 },
1722 fcc_voip_class => ''
1724 foreach my $part_pkg (@part_pkg) {
1725 $part_pkg->set(fcc_voip_class => 2);
1726 my @pkg_svc = $part_pkg->pkg_svc;
1727 my %quantity = map {$_->svcpart, $_->quantity} @pkg_svc;
1728 my %hidden = map {$_->svcpart, $_->hidden } @pkg_svc;
1729 my $error = $part_pkg->replace(
1730 $part_pkg->replace_old,
1731 options => { $part_pkg->options },
1732 pkg_svc => \%quantity,
1733 hidden_svc => \%hidden,
1734 primary_svc => ($part_pkg->svcpart || ''),
1736 die $error if $error;
1739 FS::upgrade_journal->set_done($upgrade);
1744 =item curuser_pkgs_sql
1746 Returns an SQL fragment for searching for packages the current user can
1747 use, either via part_pkg.agentnum directly, or via agent type (see
1752 sub curuser_pkgs_sql {
1755 $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
1759 =item agent_pkgs_sql AGENT | AGENTNUM, ...
1761 Returns an SQL fragment for searching for packages the provided agent or agents
1762 can use, either via part_pkg.agentnum directly, or via agent type (see
1767 sub agent_pkgs_sql {
1768 my $class = shift; #i'm a class method, not a sub (the question is... why??)
1769 my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
1771 $class->_pkgs_sql(@agentnums); #is this why
1776 my( $class, @agentnums ) = @_;
1777 my $agentnums = join(',', @agentnums);
1781 ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
1782 OR ( agentnum IS NULL
1783 AND EXISTS ( SELECT 1
1785 LEFT JOIN agent_type USING ( typenum )
1786 LEFT JOIN agent AS typeagent USING ( typenum )
1787 WHERE type_pkgs.pkgpart = part_pkg.pkgpart
1788 AND typeagent.agentnum IN ($agentnums)
1806 #false laziness w/part_export & cdr
1808 foreach my $INC ( @INC ) {
1809 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1810 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1811 warn "attempting to load plan info from $file\n" if $DEBUG;
1812 $file =~ /\/(\w+)\.pm$/ or do {
1813 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1817 my $info = eval "use FS::part_pkg::$mod; ".
1818 "\\%FS::part_pkg::$mod\::info;";
1820 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1823 unless ( keys %$info ) {
1824 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
1827 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1828 #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1829 # warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1832 $info{$mod} = $info;
1833 $info->{'weight'} ||= 0; # quiet warnings
1837 # copy one level deep to allow replacement of fields and fieldorder
1838 tie %plans, 'Tie::IxHash',
1839 map { my %infohash = %{ $info{$_} };
1841 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1844 # inheritance of plan options
1845 foreach my $name (keys(%info)) {
1846 if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
1847 warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
1848 delete $plans{$name};
1851 my $parents = $info{$name}->{'inherit_fields'} || [];
1852 my (%fields, %field_exists, @fieldorder);
1853 foreach my $parent ($name, @$parents) {
1854 if ( !exists($info{$parent}) ) {
1855 warn "$name tried to inherit from nonexistent '$parent'\n";
1858 %fields = ( # avoid replacing existing fields
1859 %{ $info{$parent}->{'fields'} || {} },
1862 foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
1864 next if $field_exists{$_};
1865 $field_exists{$_} = 1;
1866 # allow inheritors to remove inherited fields from the fieldorder
1867 push @fieldorder, $_ if !exists($fields{$_}) or
1868 !exists($fields{$_}->{'disabled'});
1871 $plans{$name}->{'fields'} = \%fields;
1872 $plans{$name}->{'fieldorder'} = \@fieldorder;
1882 =head1 NEW PLAN CLASSES
1884 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
1885 found in eg/plan_template.pm. Until then, it is suggested that you use the
1886 other modules in FS/FS/part_pkg/ as a guide.
1890 The delete method is unimplemented.
1892 setup and recur semantics are not yet defined (and are implemented in
1893 FS::cust_bill. hmm.). now they're deprecated and need to go.
1897 part_pkg_taxrate is Pg specific
1899 replace should be smarter about managing the related tables (options, pkg_svc)
1903 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1904 schema.html from the base documentation.