2 use base qw( FS::m2m_Common FS::o2m_Common FS::option_Common );
5 use vars qw( %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
6 use Carp qw(carp cluck confess);
7 use Scalar::Util qw( blessed );
8 use Time::Local qw( timelocal_nocheck );
11 use FS::Record qw( qsearch qsearchs dbh dbdef );
17 use FS::part_pkg_option;
20 use FS::part_pkg_msgcat;
21 use FS::part_pkg_taxrate;
22 use FS::part_pkg_taxoverride;
23 use FS::part_pkg_taxproduct;
24 use FS::part_pkg_link;
25 use FS::part_pkg_discount;
26 use FS::part_pkg_usage;
27 use FS::part_pkg_vendor;
28 use FS::part_pkg_currency;
32 $skip_pkg_svc_hack = 0;
36 FS::part_pkg - Object methods for part_pkg objects
42 $record = new FS::part_pkg \%hash
43 $record = new FS::part_pkg { 'column' => 'value' };
45 $custom_record = $template_record->clone;
47 $error = $record->insert;
49 $error = $new_record->replace($old_record);
51 $error = $record->delete;
53 $error = $record->check;
55 @pkg_svc = $record->pkg_svc;
57 $svcnum = $record->svcpart;
58 $svcnum = $record->svcpart( 'svc_acct' );
62 An FS::part_pkg object represents a package definition. FS::part_pkg
63 inherits from FS::Record. The following fields are currently supported:
67 =item pkgpart - primary key (assigned automatically for new package definitions)
69 =item pkg - Text name of this package definition (customer-viewable)
71 =item comment - Text name of this package definition (non-customer-viewable)
73 =item classnum - Optional package class (see L<FS::pkg_class>)
75 =item promo_code - Promotional code
77 =item setup - Setup fee expression (deprecated)
79 =item freq - Frequency of recurring fee
81 =item recur - Recurring fee expression (deprecated)
83 =item setuptax - Setup fee tax exempt flag, empty or `Y'
85 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
87 =item taxclass - Tax class
89 =item plan - Price plan
91 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
93 =item disabled - Disabled flag, empty or `Y'
95 =item custom - Custom flag, empty or `Y'
97 =item setup_cost - for cost tracking
99 =item recur_cost - for cost tracking
101 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
103 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
105 =item agentnum - Optional agentnum (see L<FS::agent>)
107 =item fcc_ds0s - Optional DS0 equivalency number for FCC form 477
109 =item fcc_voip_class - Which column of FCC form 477 part II.B this package
112 =item successor - Foreign key for the part_pkg that replaced this record.
113 If this record is not obsolete, will be null.
115 =item family_pkgpart - Foreign key for the part_pkg that was the earliest
116 ancestor of this record. If this record is not a successor to another
117 part_pkg, will be equal to pkgpart.
127 Creates a new package definition. To add the package definition to
128 the database, see L<"insert">.
132 sub table { 'part_pkg'; }
136 An alternate constructor. Creates a new package definition by duplicating
137 an existing definition. A new pkgpart is assigned and the custom flag is
138 set to Y. To add the package definition to the database, see L<"insert">.
144 my $class = ref($self);
145 my %hash = $self->hash;
146 $hash{'pkgpart'} = '';
147 $hash{'custom'} = 'Y';
148 #new FS::part_pkg ( \%hash ); # ?
149 new $class ( \%hash ); # ?
152 =item insert [ , OPTION => VALUE ... ]
154 Adds this package definition to the database. If there is an error,
155 returns the error, otherwise returns false.
157 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
158 I<custnum_ref> and I<options>.
160 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
161 values, appropriate FS::pkg_svc records will be inserted. I<hidden_svc> can
162 be set to a hashref of svcparts and flag values ('Y' or '') to set the
163 'hidden' field in these records.
165 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
166 FS::pkg_svc record will be updated.
168 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
169 record itself), the object will be updated to point to this package definition.
171 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
172 the scalar will be updated with the custnum value from the cust_pkg record.
174 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
175 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
176 records will be inserted.
178 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
179 records will be inserted.
181 If I<part_pkg_currency> is set to a hashref of options (with the keys as
182 option_CURRENCY), appropriate FS::part_pkg::currency records will be inserted.
189 warn "FS::part_pkg::insert called on $self with options ".
190 join(', ', map "$_=>$options{$_}", keys %options)
193 local $SIG{HUP} = 'IGNORE';
194 local $SIG{INT} = 'IGNORE';
195 local $SIG{QUIT} = 'IGNORE';
196 local $SIG{TERM} = 'IGNORE';
197 local $SIG{TSTP} = 'IGNORE';
198 local $SIG{PIPE} = 'IGNORE';
200 my $oldAutoCommit = $FS::UID::AutoCommit;
201 local $FS::UID::AutoCommit = 0;
204 warn " inserting part_pkg record" if $DEBUG;
205 my $error = $self->SUPER::insert( $options{options} );
207 $dbh->rollback if $oldAutoCommit;
212 if ( $self->get('family_pkgpart') eq '' ) {
213 $self->set('family_pkgpart' => $self->pkgpart);
214 $error = $self->SUPER::replace;
216 $dbh->rollback if $oldAutoCommit;
221 my $conf = new FS::Conf;
222 if ( $conf->exists('agent_defaultpkg') ) {
223 warn " agent_defaultpkg set; allowing all agents to purchase package"
225 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
226 my $type_pkgs = new FS::type_pkgs({
227 'typenum' => $agent_type->typenum,
228 'pkgpart' => $self->pkgpart,
230 my $error = $type_pkgs->insert;
232 $dbh->rollback if $oldAutoCommit;
238 warn " inserting part_pkg_taxoverride records" if $DEBUG;
239 my %overrides = %{ $options{'tax_overrides'} || {} };
240 foreach my $usage_class ( keys %overrides ) {
242 ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
243 ? $overrides{$usage_class}
245 my @overrides = (grep "$_", split(',', $override) );
246 my $error = $self->process_m2m (
247 'link_table' => 'part_pkg_taxoverride',
248 'target_table' => 'tax_class',
249 'hashref' => { 'usage_class' => $usage_class },
250 'params' => \@overrides,
253 $dbh->rollback if $oldAutoCommit;
258 warn " inserting part_pkg_currency records" if $DEBUG;
259 my %part_pkg_currency = %{ $options{'part_pkg_currency'} || {} };
260 foreach my $key ( keys %part_pkg_currency ) {
261 $key =~ /^(.+)_([A-Z]{3})$/ or next;
262 my $part_pkg_currency = new FS::part_pkg_currency {
263 'pkgpart' => $self->pkgpart,
266 'optionvalue' => $part_pkg_currency{$key},
268 my $error = $part_pkg_currency->insert;
270 $dbh->rollback if $oldAutoCommit;
275 unless ( $skip_pkg_svc_hack ) {
277 warn " inserting pkg_svc records" if $DEBUG;
278 my $pkg_svc = $options{'pkg_svc'} || {};
279 my $hidden_svc = $options{'hidden_svc'} || {};
280 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
281 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
283 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
287 my $pkg_svc = new FS::pkg_svc( {
288 'pkgpart' => $self->pkgpart,
289 'svcpart' => $part_svc->svcpart,
290 'quantity' => $quantity,
291 'primary_svc' => $primary_svc,
292 'hidden' => $hidden_svc->{$part_svc->svcpart},
294 my $error = $pkg_svc->insert;
296 $dbh->rollback if $oldAutoCommit;
303 if ( $options{'cust_pkg'} ) {
304 warn " updating cust_pkg record " if $DEBUG;
306 ref($options{'cust_pkg'})
307 ? $options{'cust_pkg'}
308 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
309 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
310 if $options{'custnum_ref'};
311 my %hash = $old_cust_pkg->hash;
312 $hash{'pkgpart'} = $self->pkgpart,
313 my $new_cust_pkg = new FS::cust_pkg \%hash;
314 local($FS::cust_pkg::disable_agentcheck) = 1;
315 my $error = $new_cust_pkg->replace($old_cust_pkg);
317 $dbh->rollback if $oldAutoCommit;
318 return "Error modifying cust_pkg record: $error";
322 if ( $options{'part_pkg_vendor'} ) {
323 while ( my ($exportnum, $vendor_pkg_id) =
324 each %{ $options{part_pkg_vendor} }
327 my $ppv = new FS::part_pkg_vendor( {
328 'pkgpart' => $self->pkgpart,
329 'exportnum' => $exportnum,
330 'vendor_pkg_id' => $vendor_pkg_id,
332 my $error = $ppv->insert;
334 $dbh->rollback if $oldAutoCommit;
335 return "Error inserting part_pkg_vendor record: $error";
340 warn " committing transaction" if $DEBUG and $oldAutoCommit;
341 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
348 Currently unimplemented.
353 return "Can't (yet?) delete package definitions.";
354 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
357 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
359 Replaces OLD_RECORD with this one in the database. If there is an error,
360 returns the error, otherwise returns false.
362 Currently available options are: I<pkg_svc>, I<hidden_svc>, I<primary_svc>
365 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
366 values, the appropriate FS::pkg_svc records will be replaced. I<hidden_svc>
367 can be set to a hashref of svcparts and flag values ('Y' or '') to set the
368 'hidden' field in these records.
370 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
371 FS::pkg_svc record will be updated.
373 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
376 If I<part_pkg_currency> is set to a hashref of options (with the keys as
377 option_CURRENCY), appropriate FS::part_pkg::currency records will be replaced.
384 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
389 ( ref($_[0]) eq 'HASH' )
393 $options->{options} = { $old->options } unless defined($options->{options});
395 warn "FS::part_pkg::replace called on $new to replace $old with options".
396 join(', ', map "$_ => ". $options->{$_}, keys %$options)
399 local $SIG{HUP} = 'IGNORE';
400 local $SIG{INT} = 'IGNORE';
401 local $SIG{QUIT} = 'IGNORE';
402 local $SIG{TERM} = 'IGNORE';
403 local $SIG{TSTP} = 'IGNORE';
404 local $SIG{PIPE} = 'IGNORE';
406 my $oldAutoCommit = $FS::UID::AutoCommit;
407 local $FS::UID::AutoCommit = 0;
410 my $conf = new FS::Conf;
411 if ( $conf->exists('part_pkg-lineage') ) {
412 if ( grep { $options->{options}->{$_} ne $old->option($_, 1) }
413 qw(setup_fee recur_fee) #others? config?
416 warn " superseding package" if $DEBUG;
418 my $error = $new->supersede($old, %$options);
420 $dbh->rollback if $oldAutoCommit;
424 warn " committing transaction" if $DEBUG and $oldAutoCommit;
425 $dbh->commit if $oldAutoCommit;
432 #plandata shit stays in replace for upgrades until after 2.0 (or edit
434 warn " saving legacy plandata" if $DEBUG;
435 my $plandata = $new->get('plandata');
436 $new->set('plandata', '');
438 warn " deleting old part_pkg_option records" if $DEBUG;
439 foreach my $part_pkg_option ( $old->part_pkg_option ) {
440 my $error = $part_pkg_option->delete;
442 $dbh->rollback if $oldAutoCommit;
447 warn " replacing part_pkg record" if $DEBUG;
448 my $error = $new->SUPER::replace($old, $options->{options} );
450 $dbh->rollback if $oldAutoCommit;
454 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
455 foreach my $part_pkg_option (
456 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
457 return "illegal plandata: $plandata";
459 new FS::part_pkg_option {
460 'pkgpart' => $new->pkgpart,
465 split("\n", $plandata)
467 my $error = $part_pkg_option->insert;
469 $dbh->rollback if $oldAutoCommit;
474 #trivial nit: not the most efficient to delete and reinsert
475 warn " deleting old part_pkg_currency records" if $DEBUG;
476 foreach my $part_pkg_currency ( $old->part_pkg_currency ) {
477 my $error = $part_pkg_currency->delete;
479 $dbh->rollback if $oldAutoCommit;
480 return "error deleting part_pkg_currency record: $error";
484 warn " inserting new part_pkg_currency records" if $DEBUG;
485 my %part_pkg_currency = %{ $options->{'part_pkg_currency'} || {} };
486 foreach my $key ( keys %part_pkg_currency ) {
487 $key =~ /^(.+)_([A-Z]{3})$/ or next;
488 my $part_pkg_currency = new FS::part_pkg_currency {
489 'pkgpart' => $new->pkgpart,
492 'optionvalue' => $part_pkg_currency{$key},
494 my $error = $part_pkg_currency->insert;
496 $dbh->rollback if $oldAutoCommit;
497 return "error inserting part_pkg_currency record: $error";
502 warn " replacing pkg_svc records" if $DEBUG;
503 my $pkg_svc = $options->{'pkg_svc'};
504 my $hidden_svc = $options->{'hidden_svc'} || {};
505 if ( $pkg_svc ) { # if it wasn't passed, don't change existing pkg_svcs
506 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
507 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
508 my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
510 ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
511 && $options->{'primary_svc'} == $part_svc->svcpart
516 my $old_pkg_svc = qsearchs('pkg_svc', {
517 'pkgpart' => $old->pkgpart,
518 'svcpart' => $part_svc->svcpart,
521 my $old_quantity = 0;
522 my $old_primary_svc = '';
524 if ( $old_pkg_svc ) {
525 $old_quantity = $old_pkg_svc->quantity;
526 $old_primary_svc = $old_pkg_svc->primary_svc
527 if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
528 $old_hidden = $old_pkg_svc->hidden;
531 next unless $old_quantity != $quantity ||
532 $old_primary_svc ne $primary_svc ||
533 $old_hidden ne $hidden;
535 my $new_pkg_svc = new FS::pkg_svc( {
536 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
537 'pkgpart' => $new->pkgpart,
538 'svcpart' => $part_svc->svcpart,
539 'quantity' => $quantity,
540 'primary_svc' => $primary_svc,
543 my $error = $old_pkg_svc
544 ? $new_pkg_svc->replace($old_pkg_svc)
545 : $new_pkg_svc->insert;
547 $dbh->rollback if $oldAutoCommit;
551 } #if $options->{pkg_svc}
553 my @part_pkg_vendor = $old->part_pkg_vendor;
554 my @current_exportnum = ();
555 if ( $options->{'part_pkg_vendor'} ) {
556 my($exportnum,$vendor_pkg_id);
557 while ( ($exportnum,$vendor_pkg_id)
558 = each %{$options->{'part_pkg_vendor'}} ) {
560 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
561 if($exportnum == $part_pkg_vendor->exportnum
562 && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
563 $part_pkg_vendor->vendor_pkg_id($vendor_pkg_id);
564 my $error = $part_pkg_vendor->replace;
566 $dbh->rollback if $oldAutoCommit;
567 return "Error replacing part_pkg_vendor record: $error";
572 elsif($exportnum == $part_pkg_vendor->exportnum
573 && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
578 unless ( $noinsert ) {
579 my $ppv = new FS::part_pkg_vendor( {
580 'pkgpart' => $new->pkgpart,
581 'exportnum' => $exportnum,
582 'vendor_pkg_id' => $vendor_pkg_id,
584 my $error = $ppv->insert;
586 $dbh->rollback if $oldAutoCommit;
587 return "Error inserting part_pkg_vendor record: $error";
590 push @current_exportnum, $exportnum;
593 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
594 unless ( grep($_ eq $part_pkg_vendor->exportnum, @current_exportnum) ) {
595 my $error = $part_pkg_vendor->delete;
597 $dbh->rollback if $oldAutoCommit;
598 return "Error deleting part_pkg_vendor record: $error";
603 # propagate changes to certain core fields
604 if ( $conf->exists('part_pkg-lineage') ) {
605 warn " propagating changes to family" if $DEBUG;
606 my $error = $new->propagate($old);
608 $dbh->rollback if $oldAutoCommit;
613 warn " committing transaction" if $DEBUG and $oldAutoCommit;
614 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
620 Checks all fields to make sure this is a valid package definition. If
621 there is an error, returns the error, otherwise returns false. Called by the
622 insert and replace methods.
628 warn "FS::part_pkg::check called on $self" if $DEBUG;
630 for (qw(setup recur plandata)) {
631 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
632 return "Use of $_ field is deprecated; set a plan and options: ".
634 if length($self->get($_));
638 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
639 my $error = $self->ut_number('freq');
640 return $error if $error;
642 $self->freq =~ /^(\d+[hdw]?)$/
643 or return "Illegal or empty freq: ". $self->freq;
647 my @null_agentnum_right = ( 'Edit global package definitions' );
648 push @null_agentnum_right, 'One-time charge'
649 if $self->freq =~ /^0/;
650 push @null_agentnum_right, 'Customize customer package'
651 if $self->disabled eq 'Y'; #good enough
653 my $error = $self->ut_numbern('pkgpart')
654 || $self->ut_text('pkg')
655 || $self->ut_text('comment')
656 || $self->ut_textn('promo_code')
657 || $self->ut_alphan('plan')
658 || $self->ut_enum('setuptax', [ '', 'Y' ] )
659 || $self->ut_enum('recurtax', [ '', 'Y' ] )
660 || $self->ut_textn('taxclass')
661 || $self->ut_enum('disabled', [ '', 'Y' ] )
662 || $self->ut_enum('custom', [ '', 'Y' ] )
663 || $self->ut_enum('no_auto', [ '', 'Y' ])
664 || $self->ut_enum('recur_show_zero', [ '', 'Y' ])
665 || $self->ut_enum('setup_show_zero', [ '', 'Y' ])
666 #|| $self->ut_moneyn('setup_cost')
667 #|| $self->ut_moneyn('recur_cost')
668 || $self->ut_floatn('setup_cost')
669 || $self->ut_floatn('recur_cost')
670 || $self->ut_floatn('pay_weight')
671 || $self->ut_floatn('credit_weight')
672 || $self->ut_numbern('taxproductnum')
673 || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum')
674 || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
675 || $self->ut_foreign_keyn('taxproductnum',
676 'part_pkg_taxproduct',
680 ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
681 : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
683 || $self->ut_numbern('fcc_ds0s')
684 || $self->ut_numbern('fcc_voip_class')
685 || $self->ut_foreign_keyn('successor', 'part_pkg', 'pkgpart')
686 || $self->ut_foreign_keyn('family_pkgpart', 'part_pkg', 'pkgpart')
687 || $self->SUPER::check
689 return $error if $error;
691 return 'Unknown plan '. $self->plan
692 unless exists($plans{$self->plan});
694 my $conf = new FS::Conf;
695 return 'Taxclass is required'
696 if ! $self->taxclass && $conf->exists('require_taxclasses');
701 =item supersede OLD [, OPTION => VALUE ... ]
703 Inserts this package as a successor to the package OLD. All options are as
704 for C<insert>. After inserting, disables OLD and sets the new package as its
710 my ($new, $old, %options) = @_;
713 $new->set('pkgpart' => '');
714 $new->set('family_pkgpart' => $old->family_pkgpart);
715 warn " inserting successor package\n" if $DEBUG;
716 $error = $new->insert(%options);
717 return $error if $error;
719 warn " disabling superseded package\n" if $DEBUG;
720 $old->set('successor' => $new->pkgpart);
721 $old->set('disabled' => 'Y');
722 $error = $old->SUPER::replace; # don't change its options/pkg_svc records
723 return $error if $error;
725 warn " propagating changes to family" if $DEBUG;
726 $new->propagate($old);
731 If any of certain fields have changed from OLD to this package, then,
732 for all packages in the same lineage as this one, sets those fields
733 to their values in this package.
737 my @propagate_fields = (
738 qw( pkg classnum setup_cost recur_cost taxclass
739 setuptax recurtax pay_weight credit_weight
747 map { $_ => $new->get($_) }
748 grep { $new->get($_) ne $old->get($_) }
752 my @part_pkg = qsearch('part_pkg', {
753 'family_pkgpart' => $new->family_pkgpart
756 foreach my $part_pkg ( @part_pkg ) {
757 my $pkgpart = $part_pkg->pkgpart;
758 next if $pkgpart == $new->pkgpart; # don't modify $new
759 warn " propagating to pkgpart $pkgpart\n" if $DEBUG;
760 foreach ( keys %fields ) {
761 $part_pkg->set($_, $fields{$_});
763 # SUPER::replace to avoid changing non-core fields
764 my $error = $part_pkg->SUPER::replace;
765 push @error, "pkgpart $pkgpart: $error"
771 =item pkg_locale LOCALE
773 Returns a customer-viewable string representing this package for the given
774 locale, from the part_pkg_msgcat table. If the given locale is empty or no
775 localized string is found, returns the base pkg field.
780 my( $self, $locale ) = @_;
781 return $self->pkg unless $locale;
782 my $part_pkg_msgcat = $self->part_pkg_msgcat($locale) or return $self->pkg;
783 $part_pkg_msgcat->pkg;
786 =item part_pkg_msgcat LOCALE
788 Like pkg_locale, but returns the FS::part_pkg_msgcat object itself.
792 sub part_pkg_msgcat {
793 my( $self, $locale ) = @_;
794 qsearchs( 'part_pkg_msgcat', {
795 pkgpart => $self->pkgpart,
800 =item pkg_comment [ OPTION => VALUE... ]
802 Returns an (internal) string representing this package. Currently,
803 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
804 future, omitting pkgpart. The comment will have '(CUSTOM) ' prepended if
807 If the option nopkgpart is true then the "pkgpart: ' is omitted.
815 #$self->pkg. ' - '. $self->comment;
816 #$self->pkg. ' ('. $self->comment. ')';
817 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
818 $pre. $self->pkg. ' - '. $self->custom_comment;
821 sub price_info { # safety, in case a part_pkg hasn't defined price_info
827 ( $self->custom ? '(CUSTOM) ' : '' ). $self->comment . ' ' . $self->price_info;
832 Returns the package class, as an FS::pkg_class object, or the empty string
833 if there is no package class.
839 if ( $self->classnum ) {
840 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
846 =item addon_pkg_class
848 Returns the add-on package class, as an FS::pkg_class object, or the empty
849 string if there is no add-on package class.
853 sub addon_pkg_class {
855 if ( $self->addon_classnum ) {
856 qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
864 Returns the package category name, or the empty string if there is no package
871 my $pkg_class = $self->pkg_class;
873 ? $pkg_class->categoryname
879 Returns the package class name, or the empty string if there is no package
886 my $pkg_class = $self->pkg_class;
888 ? $pkg_class->classname
892 =item addon_classname
894 Returns the add-on package class name, or the empty string if there is no
895 add-on package class.
899 sub addon_classname {
901 my $pkg_class = $self->addon_pkg_class;
903 ? $pkg_class->classname
909 Returns the associated agent for this event, if any, as an FS::agent object.
915 qsearchs('agent', { 'agentnum' => $self->agentnum } );
918 =item pkg_svc [ HASHREF | OPTION => VALUE ]
920 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
921 definition (with non-zero quantity).
923 One option is available, I<disable_linked>. If set true it will return the
924 services for this package definition alone, omitting services from any add-on
931 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
938 qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
944 # #sort { $b->primary cmp $a->primary }
945 # grep { $_->quantity }
946 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
948 my $opt = ref($_[0]) ? $_[0] : { @_ };
949 my %pkg_svc = map { $_->svcpart => $_ }
950 grep { $_->quantity }
951 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
953 unless ( $opt->{disable_linked} ) {
954 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
955 my @pkg_svc = grep { $_->quantity }
956 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
957 foreach my $pkg_svc ( @pkg_svc ) {
958 if ( $pkg_svc{$pkg_svc->svcpart} ) {
959 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
960 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
962 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
972 =item svcpart [ SVCDB ]
974 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
975 associated with this package definition (see L<FS::pkg_svc>). Returns
976 false if there not a primary service definition or exactly one service
977 definition with quantity 1, or if SVCDB is specified and does not match the
978 svcdb of the service definition. SVCDB can be specified as a scalar table
979 name, such as 'svc_acct', or as an arrayref of possible table names.
984 my $pkg_svc = shift->_primary_pkg_svc(@_);
985 $pkg_svc ? $pkg_svc->svcpart : '';
988 =item part_svc [ SVCDB ]
990 Like the B<svcpart> method, but returns the FS::part_svc object (see
996 my $pkg_svc = shift->_primary_pkg_svc(@_);
997 $pkg_svc ? $pkg_svc->part_svc : '';
1000 sub _primary_pkg_svc {
1003 my $svcdb = scalar(@_) ? shift : [];
1004 $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
1005 my %svcdb = map { $_=>1 } @$svcdb;
1008 grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
1011 my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
1012 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
1014 return '' if scalar(@pkg_svc) != 1;
1018 =item svcpart_unique_svcdb SVCDB
1020 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
1021 SVCDB associated with this package definition (see L<FS::pkg_svc>). Returns
1022 false if there not a primary service definition for SVCDB or there are multiple
1023 service definitions for SVCDB.
1027 sub svcpart_unique_svcdb {
1028 my( $self, $svcdb ) = @_;
1029 my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
1030 return '' if scalar(@svcdb_pkg_svc) != 1;
1031 $svcdb_pkg_svc[0]->svcpart;
1036 Returns a list of the acceptable payment types for this package. Eventually
1037 this should come out of a database table and be editable, but currently has the
1038 following logic instead:
1040 If the package is free, the single item B<BILL> is
1041 returned, otherwise, the single item B<CARD> is returned.
1043 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
1049 if ( $self->is_free ) {
1058 Returns true if this package is free.
1064 if ( $self->can('is_free_options') ) {
1065 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
1066 map { $self->option($_) }
1067 $self->is_free_options;
1069 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
1070 "provides neither is_free_options nor is_free method; returning false";
1075 sub can_discount { 0; }
1077 sub can_start_date { 1; }
1080 # moved to FS::Misc to make this accessible to other packages
1082 FS::Misc::pkg_freqs();
1087 Returns an english representation of the I<freq> field, such as "monthly",
1088 "weekly", "semi-annually", etc.
1094 my $freq = $self->freq;
1096 #my $freqs_href = $self->freqs_href;
1097 my $freqs_href = freqs_href();
1099 if ( exists($freqs_href->{$freq}) ) {
1100 $freqs_href->{$freq};
1102 my $interval = 'month';
1103 if ( $freq =~ /^(\d+)([hdw])$/ ) {
1104 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
1105 $interval = $interval{$2};
1110 "every $freq ${interval}s";
1115 =item add_freq TIMESTAMP [ FREQ ]
1117 Adds a billing period of some frequency to the provided timestamp and
1118 returns the resulting timestamp, or -1 if the frequency could not be
1119 parsed (shouldn't happen). By default, the frequency of this package
1120 will be used; to override this, pass a different frequency as a second
1126 my( $self, $date, $freq ) = @_;
1127 $freq = $self->freq unless $freq;
1129 #change this bit to use Date::Manip? CAREFUL with timezones (see
1130 # mailing list archive)
1131 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
1133 if ( $freq =~ /^\d+$/ ) {
1135 until ( $mon < 12 ) { $mon -= 12; $year++; }
1137 $mday = 28 if $mday > 28 && FS::Conf->new->exists('anniversary-rollback');
1139 } elsif ( $freq =~ /^(\d+)w$/ ) {
1141 $mday += $weeks * 7;
1142 } elsif ( $freq =~ /^(\d+)d$/ ) {
1145 } elsif ( $freq =~ /^(\d+)h$/ ) {
1152 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
1157 For backwards compatibility, returns the plandata field as well as all options
1158 from FS::part_pkg_option.
1164 carp "plandata is deprecated";
1166 $self->SUPER::plandata(@_);
1168 my $plandata = $self->get('plandata');
1169 my %options = $self->options;
1170 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
1175 =item part_pkg_vendor
1177 Returns all vendor/external package ids as FS::part_pkg_vendor objects (see
1178 L<FS::part_pkg_vendor>).
1182 sub part_pkg_vendor {
1184 qsearch('part_pkg_vendor', { 'pkgpart' => $self->pkgpart } );
1187 =item vendor_pkg_ids
1189 Returns a list of vendor/external package ids by exportnum
1193 sub vendor_pkg_ids {
1195 map { $_->exportnum => $_->vendor_pkg_id } $self->part_pkg_vendor;
1198 =item part_pkg_option
1200 Returns all options as FS::part_pkg_option objects (see
1201 L<FS::part_pkg_option>).
1205 sub part_pkg_option {
1207 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
1212 Returns a list of option names and values suitable for assigning to a hash.
1218 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
1221 =item option OPTIONNAME [ QUIET ]
1223 Returns the option value for the given name, or the empty string. If a true
1224 value is passed as the second argument, warnings about missing the option
1230 my( $self, $opt, $ornull ) = @_;
1231 my $part_pkg_option =
1232 qsearchs('part_pkg_option', {
1233 pkgpart => $self->pkgpart,
1236 return $part_pkg_option->optionvalue if $part_pkg_option;
1237 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
1238 split("\n", $self->get('plandata') );
1239 return $plandata{$opt} if exists $plandata{$opt};
1240 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
1241 "not found in options or plandata!\n"
1246 =item part_pkg_currency [ CURRENCY ]
1248 Returns all currency options as FS::part_pkg_currency objects (see
1249 L<FS::part_pkg_currency>), or, if a currency is specified, only return the
1250 objects for that currency.
1254 sub part_pkg_currency {
1256 my %hash = ( 'pkgpart' => $self->pkgpart );
1257 $hash{'currency'} = shift if @_;
1258 qsearch('part_pkg_currency', \%hash );
1261 =item part_pkg_currency_options CURRENCY
1263 Returns a list of option names and values from FS::part_pkg_currency for the
1268 sub part_pkg_currency_options {
1270 map { $_->optionname => $_->optionvalue } $self->part_pkg_currency(shift);
1273 =item bill_part_pkg_link
1275 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1279 sub bill_part_pkg_link {
1280 shift->_part_pkg_link('bill', @_);
1283 =item svc_part_pkg_link
1285 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1289 sub svc_part_pkg_link {
1290 shift->_part_pkg_link('svc', @_);
1293 =item supp_part_pkg_link
1295 Returns the associated part_pkg_link records of type 'supp' (supplemental
1300 sub supp_part_pkg_link {
1301 shift->_part_pkg_link('supp', @_);
1304 sub _part_pkg_link {
1305 my( $self, $type ) = @_;
1306 qsearch({ table => 'part_pkg_link',
1307 hashref => { 'src_pkgpart' => $self->pkgpart,
1308 'link_type' => $type,
1309 #protection against infinite recursive links
1310 'dst_pkgpart' => { op=>'!=', value=> $self->pkgpart },
1312 order_by => "ORDER BY hidden",
1316 sub self_and_bill_linked {
1317 shift->_self_and_linked('bill', @_);
1320 sub self_and_svc_linked {
1321 shift->_self_and_linked('svc', @_);
1324 sub _self_and_linked {
1325 my( $self, $type, $hidden ) = @_;
1329 foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
1330 $self->_part_pkg_link($type) ) )
1332 $_->hidden($hidden) if $hidden;
1339 =item part_pkg_taxoverride [ CLASS ]
1341 Returns all associated FS::part_pkg_taxoverride objects (see
1342 L<FS::part_pkg_taxoverride>). Limits the returned set to those
1343 of class CLASS if defined. Class may be one of 'setup', 'recur',
1344 the empty string (default), or a usage class number (see L<FS::usage_class>).
1345 When a class is specified, the empty string class (default) is returned
1346 if no more specific values exist.
1350 sub part_pkg_taxoverride {
1354 my $hashref = { 'pkgpart' => $self->pkgpart };
1355 $hashref->{'usage_class'} = $class if defined($class);
1356 my @overrides = qsearch('part_pkg_taxoverride', $hashref );
1358 unless ( scalar(@overrides) || !defined($class) || !$class ){
1359 $hashref->{'usage_class'} = '';
1360 @overrides = qsearch('part_pkg_taxoverride', $hashref );
1366 =item has_taxproduct
1368 Returns true if this package has any taxproduct associated with it.
1372 sub has_taxproduct {
1375 $self->taxproductnum ||
1376 scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) }
1377 keys %{ {$self->options} }
1383 =item taxproduct [ CLASS ]
1385 Returns the associated tax product for this package definition (see
1386 L<FS::part_pkg_taxproduct>). CLASS may be one of 'setup', 'recur' or
1387 the usage classnum (see L<FS::usage_class>). Returns the default
1388 tax product for this record if the more specific CLASS value does
1397 my $part_pkg_taxproduct;
1399 my $taxproductnum = $self->taxproductnum;
1401 my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1402 $taxproductnum = $class_taxproductnum
1403 if $class_taxproductnum
1406 $part_pkg_taxproduct =
1407 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1409 unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1410 $taxproductnum = $self->taxproductnum;
1411 $part_pkg_taxproduct =
1412 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1415 $part_pkg_taxproduct;
1418 =item taxproduct_description [ CLASS ]
1420 Returns the description of the associated tax product for this package
1421 definition (see L<FS::part_pkg_taxproduct>).
1425 sub taxproduct_description {
1427 my $part_pkg_taxproduct = $self->taxproduct(@_);
1428 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1431 =item part_pkg_taxrate DATA_PROVIDER, GEOCODE, [ CLASS ]
1433 Returns the package to taxrate m2m records for this package in the location
1434 specified by GEOCODE (see L<FS::part_pkg_taxrate>) and usage class CLASS.
1435 CLASS may be one of 'setup', 'recur', or one of the usage classes numbers
1436 (see L<FS::usage_class>).
1440 sub _expand_cch_taxproductnum {
1443 my $part_pkg_taxproduct = $self->taxproduct($class);
1445 my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
1446 ? ( split ':', $part_pkg_taxproduct->taxproduct )
1449 $a = '' unless $a; $b = '' unless $b; $c = '' unless $c; $d = '' unless $d;
1450 my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
1451 OR taxproduct = '$a:$b:$c:'
1452 OR taxproduct = '$a:$b:".":$d'
1453 OR taxproduct = '$a:$b:".":' )";
1454 map { $_->taxproductnum } qsearch( { 'table' => 'part_pkg_taxproduct',
1455 'hashref' => { 'data_vendor'=>'cch' },
1456 'extra_sql' => $extra_sql,
1461 sub part_pkg_taxrate {
1463 my ($data_vendor, $geocode, $class) = @_;
1466 my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
1467 dbh->quote($data_vendor);
1469 # CCH oddness in m2m
1470 $extra_sql .= ' AND ('.
1471 join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) }
1475 # much more CCH oddness in m2m -- this is kludgy
1476 my @tpnums = $self->_expand_cch_taxproductnum($class);
1477 if (scalar(@tpnums)) {
1478 $extra_sql .= ' AND ('.
1479 join(' OR ', map{ "taxproductnum = $_" } @tpnums ).
1482 $extra_sql .= ' AND ( 0 = 1 )';
1485 my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
1486 my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
1487 my $select = 'DISTINCT ON(taxclassnum) *, taxproduct';
1489 # should qsearch preface columns with the table to facilitate joins?
1490 qsearch( { 'table' => 'part_pkg_taxrate',
1491 'select' => $select,
1492 'hashref' => { # 'data_vendor' => $data_vendor,
1493 # 'taxproductnum' => $self->taxproductnum,
1495 'addl_from' => $addl_from,
1496 'extra_sql' => $extra_sql,
1497 'order_by' => $order_by,
1501 =item part_pkg_discount
1503 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1508 sub part_pkg_discount {
1510 qsearch('part_pkg_discount', { 'pkgpart' => $self->pkgpart });
1513 =item part_pkg_usage
1515 Returns the voice usage pools (see L<FS::part_pkg_usage>) defined for
1520 sub part_pkg_usage {
1522 qsearch('part_pkg_usage', { 'pkgpart' => $self->pkgpart });
1527 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1528 PLAN is the object's I<plan> field. There should be better docs
1529 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1535 my $plan = $self->plan;
1537 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1541 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1542 my $class = ref($self). "::$plan";
1543 warn "reblessing $self into $class" if $DEBUG > 1;
1546 bless($self, $class) unless $@;
1551 sub calc_setup { die 'no calc_setup for '. shift->plan. "\n"; }
1552 sub calc_recur { die 'no calc_recur for '. shift->plan. "\n"; }
1554 #fallback that return 0 for old legacy packages with no plan
1555 sub calc_remain { 0; }
1556 sub calc_units { 0; }
1558 #fallback for everything not based on flat.pm
1559 sub recur_temporality { 'upcoming'; }
1560 sub calc_cancel { 0; }
1562 #fallback for everything except bulk.pm
1563 sub hide_svc_detail { 0; }
1565 #fallback for packages that can't/won't summarize usage
1566 sub sum_usage { 0; }
1568 =item recur_cost_permonth CUST_PKG
1570 recur_cost divided by freq (only supported for monthly and longer frequencies)
1574 sub recur_cost_permonth {
1575 my($self, $cust_pkg) = @_;
1576 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1577 sprintf('%.2f', $self->recur_cost / $self->freq );
1580 =item cust_bill_pkg_recur CUST_PKG
1582 Actual recurring charge for the specified customer package from customer's most
1587 sub cust_bill_pkg_recur {
1588 my($self, $cust_pkg) = @_;
1589 my $cust_bill_pkg = qsearchs({
1590 'table' => 'cust_bill_pkg',
1591 'addl_from' => 'LEFT JOIN cust_bill USING ( invnum )',
1592 'hashref' => { 'pkgnum' => $cust_pkg->pkgnum,
1593 'recur' => { op=>'>', value=>'0' },
1595 'order_by' => 'ORDER BY cust_bill._date DESC,
1596 cust_bill_pkg.sdate DESC
1599 }) or return 0; #die "use cust_bill_pkg_recur credits with once_perinv condition";
1600 $cust_bill_pkg->recur;
1603 =item format OPTION DATA
1605 Returns data formatted according to the function 'format' described
1606 in the plan info. Returns DATA if no such function exists.
1611 my ($self, $option, $data) = (shift, shift, shift);
1612 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1613 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1619 =item parse OPTION DATA
1621 Returns data parsed according to the function 'parse' described
1622 in the plan info. Returns DATA if no such function exists.
1627 my ($self, $option, $data) = (shift, shift, shift);
1628 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1629 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1639 =head1 CLASS METHODS
1647 # Used by FS::Upgrade to migrate to a new database.
1649 sub _upgrade_data { # class method
1650 my($class, %opts) = @_;
1652 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1654 my @part_pkg = qsearch({
1655 'table' => 'part_pkg',
1656 'extra_sql' => "WHERE ". join(' OR ',
1657 'plan IS NULL', "plan = '' ",
1661 foreach my $part_pkg (@part_pkg) {
1663 unless ( $part_pkg->plan ) {
1664 $part_pkg->plan('flat');
1671 # now upgrade to the explicit custom flag
1673 @part_pkg = qsearch({
1674 'table' => 'part_pkg',
1675 'hashref' => { disabled => 'Y', custom => '' },
1676 'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1679 foreach my $part_pkg (@part_pkg) {
1680 my $new = new FS::part_pkg { $part_pkg->hash };
1682 my $comment = $part_pkg->comment;
1683 $comment =~ s/^\(CUSTOM\) //;
1684 $comment = '(none)' unless $comment =~ /\S/;
1685 $new->comment($comment);
1687 my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1688 my $primary = $part_pkg->svcpart;
1689 my $options = { $part_pkg->options };
1691 my $error = $new->replace( $part_pkg,
1692 'pkg_svc' => $pkg_svc,
1693 'primary_svc' => $primary,
1694 'options' => $options,
1696 die $error if $error;
1699 # set family_pkgpart on any packages that don't have it
1700 @part_pkg = qsearch('part_pkg', { 'family_pkgpart' => '' });
1701 foreach my $part_pkg (@part_pkg) {
1702 $part_pkg->set('family_pkgpart' => $part_pkg->pkgpart);
1703 my $error = $part_pkg->SUPER::replace;
1704 die $error if $error;
1707 my @part_pkg_option = qsearch('part_pkg_option',
1708 { 'optionname' => 'unused_credit',
1711 foreach my $old_opt (@part_pkg_option) {
1712 my $pkgpart = $old_opt->pkgpart;
1713 my $error = $old_opt->delete;
1714 die $error if $error;
1716 foreach (qw(unused_credit_cancel unused_credit_change)) {
1717 my $new_opt = new FS::part_pkg_option {
1718 'pkgpart' => $pkgpart,
1722 $error = $new_opt->insert;
1723 die $error if $error;
1727 # migrate use_disposition_taqua and use_disposition to disposition_in
1728 @part_pkg_option = qsearch('part_pkg_option',
1729 { 'optionname' => { op => 'LIKE',
1730 value => 'use_disposition%',
1734 my %newopts = map { $_->pkgpart => $_ }
1735 qsearch('part_pkg_option', { 'optionname' => 'disposition_in', } );
1736 foreach my $old_opt (@part_pkg_option) {
1737 my $pkgpart = $old_opt->pkgpart;
1738 my $newval = $old_opt->optionname eq 'use_disposition_taqua' ? '100'
1740 my $error = $old_opt->delete;
1741 die $error if $error;
1743 if ( exists($newopts{$pkgpart}) ) {
1744 my $opt = $newopts{$pkgpart};
1745 $opt->optionvalue($opt->optionvalue.",$newval");
1746 $error = $opt->replace;
1747 die $error if $error;
1749 my $new_opt = new FS::part_pkg_option {
1750 'pkgpart' => $pkgpart,
1751 'optionname' => 'disposition_in',
1752 'optionvalue' => $newval,
1754 $error = $new_opt->insert;
1755 die $error if $error;
1756 $newopts{$pkgpart} = $new_opt;
1760 # set any package with FCC voice lines to the "VoIP with broadband" category
1761 # for backward compatibility
1763 # recover from a bad upgrade bug
1764 my $upgrade = 'part_pkg_fcc_voip_class_FIX';
1765 if (!FS::upgrade_journal->is_done($upgrade)) {
1766 my $bad_upgrade = qsearchs('upgrade_journal',
1767 { upgrade => 'part_pkg_fcc_voip_class' }
1769 if ( $bad_upgrade ) {
1770 my $where = 'WHERE history_date <= '.$bad_upgrade->_date.
1771 ' AND history_date > '.($bad_upgrade->_date - 3600);
1772 my @h_part_pkg_option = map { FS::part_pkg_option->new($_->hashref) }
1775 'table' => 'h_part_pkg_option',
1777 'extra_sql' => "$where AND history_action = 'delete'",
1778 'order_by' => 'ORDER BY history_date ASC',
1780 my @h_pkg_svc = map { FS::pkg_svc->new($_->hashref) }
1783 'table' => 'h_pkg_svc',
1785 'extra_sql' => "$where AND history_action = 'replace_old'",
1786 'order_by' => 'ORDER BY history_date ASC',
1789 foreach my $deleted (@h_part_pkg_option, @h_pkg_svc) {
1790 my $pkgpart ||= $deleted->pkgpart;
1791 $opt{$pkgpart} ||= {
1797 if ( $deleted->isa('FS::part_pkg_option') ) {
1798 $opt{$pkgpart}{options}{ $deleted->optionname } = $deleted->optionvalue;
1800 my $svcpart = $deleted->svcpart;
1801 $opt{$pkgpart}{pkg_svc}{$svcpart} = $deleted->quantity;
1802 $opt{$pkgpart}{hidden_svc}{$svcpart} ||= $deleted->hidden;
1803 $opt{$pkgpart}{primary_svc} = $svcpart if $deleted->primary_svc;
1806 foreach my $pkgpart (keys %opt) {
1807 my $part_pkg = FS::part_pkg->by_key($pkgpart);
1808 my $error = $part_pkg->replace( $part_pkg->replace_old, $opt{$pkgpart} );
1810 die "error recovering damaged pkgpart $pkgpart:\n$error\n";
1813 } # $bad_upgrade exists
1814 else { # do the original upgrade, but correctly this time
1815 @part_pkg = qsearch('part_pkg', {
1816 fcc_ds0s => { op => '>', value => 0 },
1817 fcc_voip_class => ''
1819 foreach my $part_pkg (@part_pkg) {
1820 $part_pkg->set(fcc_voip_class => 2);
1821 my @pkg_svc = $part_pkg->pkg_svc;
1822 my %quantity = map {$_->svcpart, $_->quantity} @pkg_svc;
1823 my %hidden = map {$_->svcpart, $_->hidden } @pkg_svc;
1824 my $error = $part_pkg->replace(
1825 $part_pkg->replace_old,
1826 options => { $part_pkg->options },
1827 pkg_svc => \%quantity,
1828 hidden_svc => \%hidden,
1829 primary_svc => ($part_pkg->svcpart || ''),
1831 die $error if $error;
1834 FS::upgrade_journal->set_done($upgrade);
1839 =item curuser_pkgs_sql
1841 Returns an SQL fragment for searching for packages the current user can
1842 use, either via part_pkg.agentnum directly, or via agent type (see
1847 sub curuser_pkgs_sql {
1850 $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
1854 =item agent_pkgs_sql AGENT | AGENTNUM, ...
1856 Returns an SQL fragment for searching for packages the provided agent or agents
1857 can use, either via part_pkg.agentnum directly, or via agent type (see
1862 sub agent_pkgs_sql {
1863 my $class = shift; #i'm a class method, not a sub (the question is... why??)
1864 my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
1866 $class->_pkgs_sql(@agentnums); #is this why
1871 my( $class, @agentnums ) = @_;
1872 my $agentnums = join(',', @agentnums);
1876 ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
1877 OR ( agentnum IS NULL
1878 AND EXISTS ( SELECT 1
1880 LEFT JOIN agent_type USING ( typenum )
1881 LEFT JOIN agent AS typeagent USING ( typenum )
1882 WHERE type_pkgs.pkgpart = part_pkg.pkgpart
1883 AND typeagent.agentnum IN ($agentnums)
1901 #false laziness w/part_export & cdr
1903 foreach my $INC ( @INC ) {
1904 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1905 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1906 warn "attempting to load plan info from $file\n" if $DEBUG;
1907 $file =~ /\/(\w+)\.pm$/ or do {
1908 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1912 my $info = eval "use FS::part_pkg::$mod; ".
1913 "\\%FS::part_pkg::$mod\::info;";
1915 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1918 unless ( keys %$info ) {
1919 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
1922 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1923 #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1924 # warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1927 $info{$mod} = $info;
1928 $info->{'weight'} ||= 0; # quiet warnings
1932 # copy one level deep to allow replacement of fields and fieldorder
1933 tie %plans, 'Tie::IxHash',
1934 map { my %infohash = %{ $info{$_} };
1936 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1939 # inheritance of plan options
1940 foreach my $name (keys(%info)) {
1941 if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
1942 warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
1943 delete $plans{$name};
1946 my $parents = $info{$name}->{'inherit_fields'} || [];
1947 my (%fields, %field_exists, @fieldorder);
1948 foreach my $parent ($name, @$parents) {
1949 if ( !exists($info{$parent}) ) {
1950 warn "$name tried to inherit from nonexistent '$parent'\n";
1953 %fields = ( # avoid replacing existing fields
1954 %{ $info{$parent}->{'fields'} || {} },
1957 foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
1959 next if $field_exists{$_};
1960 $field_exists{$_} = 1;
1961 # allow inheritors to remove inherited fields from the fieldorder
1962 push @fieldorder, $_ if !exists($fields{$_}) or
1963 !exists($fields{$_}->{'disabled'});
1966 $plans{$name}->{'fields'} = \%fields;
1967 $plans{$name}->{'fieldorder'} = \@fieldorder;
1977 =head1 NEW PLAN CLASSES
1979 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
1980 found in eg/plan_template.pm. Until then, it is suggested that you use the
1981 other modules in FS/FS/part_pkg/ as a guide.
1985 The delete method is unimplemented.
1987 setup and recur semantics are not yet defined (and are implemented in
1988 FS::cust_bill. hmm.). now they're deprecated and need to go.
1992 part_pkg_taxrate is Pg specific
1994 replace should be smarter about managing the related tables (options, pkg_svc)
1998 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1999 schema.html from the base documentation.