2 use base qw( FS::part_pkg::API
3 FS::m2m_Common FS::o2m_Common FS::option_Common
7 use vars qw( %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
8 use Carp qw(carp cluck confess);
9 use Scalar::Util qw( blessed );
11 use Time::Local qw( timelocal timelocal_nocheck ); # eventually replace with DateTime
14 use FS::Record qw( qsearch qsearchs dbh dbdef );
15 use FS::Cursor; # for upgrade
21 use FS::part_pkg_option;
22 use FS::part_pkg_fcc_option;
25 use FS::part_pkg_msgcat;
26 use FS::part_pkg_taxrate;
27 use FS::part_pkg_taxoverride;
28 use FS::part_pkg_taxproduct;
29 use FS::part_pkg_link;
30 use FS::part_pkg_discount;
31 use FS::part_pkg_vendor;
32 use FS::part_pkg_currency;
36 $skip_pkg_svc_hack = 0;
40 FS::part_pkg - Object methods for part_pkg objects
46 $record = new FS::part_pkg \%hash
47 $record = new FS::part_pkg { 'column' => 'value' };
49 $custom_record = $template_record->clone;
51 $error = $record->insert;
53 $error = $new_record->replace($old_record);
55 $error = $record->delete;
57 $error = $record->check;
59 @pkg_svc = $record->pkg_svc;
61 $svcnum = $record->svcpart;
62 $svcnum = $record->svcpart( 'svc_acct' );
66 An FS::part_pkg object represents a package definition. FS::part_pkg
67 inherits from FS::Record. The following fields are currently supported:
71 =item pkgpart - primary key (assigned automatically for new package definitions)
73 =item pkg - Text name of this package definition (customer-viewable)
75 =item comment - Text name of this package definition (non-customer-viewable)
77 =item classnum - Optional package class (see L<FS::pkg_class>)
79 =item promo_code - Promotional code
81 =item setup - Setup fee expression (deprecated)
83 =item freq - Frequency of recurring fee
85 =item recur - Recurring fee expression (deprecated)
87 =item setuptax - Setup fee tax exempt flag, empty or `Y'
89 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
91 =item taxclass - Tax class
93 =item plan - Price plan
95 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
97 =item disabled - Disabled flag, empty or `Y'
99 =item custom - Custom flag, empty or `Y'
101 =item setup_cost - for cost tracking
103 =item recur_cost - for cost tracking
105 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
107 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
109 =item agentnum - Optional agentnum (see L<FS::agent>)
111 =item fcc_ds0s - Optional DS0 equivalency number for FCC form 477
113 =item fcc_voip_class - Which column of FCC form 477 part II.B this package
116 =item successor - Foreign key for the part_pkg that replaced this record.
117 If this record is not obsolete, will be null.
119 =item family_pkgpart - Foreign key for the part_pkg that was the earliest
120 ancestor of this record. If this record is not a successor to another
121 part_pkg, will be equal to pkgpart.
123 =item delay_start - Number of days to delay package start, by default
133 Creates a new package definition. To add the package definition to
134 the database, see L<"insert">.
138 sub table { 'part_pkg'; }
142 An alternate constructor. Creates a new package definition by duplicating
143 an existing definition. A new pkgpart is assigned and the custom flag is
144 set to Y. To add the package definition to the database, see L<"insert">.
150 my $class = ref($self);
151 my %hash = $self->hash;
152 $hash{'pkgpart'} = '';
153 $hash{'custom'} = 'Y';
154 #new FS::part_pkg ( \%hash ); # ?
155 new $class ( \%hash ); # ?
158 =item insert [ , OPTION => VALUE ... ]
160 Adds this package definition to the database. If there is an error,
161 returns the error, otherwise returns false.
163 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
164 I<custnum_ref> and I<options>.
166 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
167 values, appropriate FS::pkg_svc records will be inserted. I<hidden_svc> can
168 be set to a hashref of svcparts and flag values ('Y' or '') to set the
169 'hidden' field in these records.
171 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
172 FS::pkg_svc record will be updated.
174 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
175 record itself), the object will be updated to point to this package definition.
177 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
178 the scalar will be updated with the custnum value from the cust_pkg record.
180 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
181 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
182 records will be inserted.
184 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
185 records will be inserted.
187 If I<part_pkg_currency> is set to a hashref of options (with the keys as
188 option_CURRENCY), appropriate FS::part_pkg::currency records will be inserted.
195 warn "FS::part_pkg::insert called on $self with options ".
196 join(', ', map "$_=>$options{$_}", keys %options)
199 local $SIG{HUP} = 'IGNORE';
200 local $SIG{INT} = 'IGNORE';
201 local $SIG{QUIT} = 'IGNORE';
202 local $SIG{TERM} = 'IGNORE';
203 local $SIG{TSTP} = 'IGNORE';
204 local $SIG{PIPE} = 'IGNORE';
206 my $oldAutoCommit = $FS::UID::AutoCommit;
207 local $FS::UID::AutoCommit = 0;
210 warn " inserting part_pkg record" if $DEBUG;
211 my $error = $self->SUPER::insert( $options{options} );
213 $dbh->rollback if $oldAutoCommit;
218 if ( $self->get('family_pkgpart') eq '' ) {
219 $self->set('family_pkgpart' => $self->pkgpart);
220 $error = $self->SUPER::replace;
222 $dbh->rollback if $oldAutoCommit;
227 warn " inserting part_pkg_taxoverride records" if $DEBUG;
228 my %overrides = %{ $options{'tax_overrides'} || {} };
229 foreach my $usage_class ( keys %overrides ) {
231 ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
232 ? $overrides{$usage_class}
234 my @overrides = (grep "$_", split(',', $override) );
235 my $error = $self->process_m2m (
236 'link_table' => 'part_pkg_taxoverride',
237 'target_table' => 'tax_class',
238 'hashref' => { 'usage_class' => $usage_class },
239 'params' => \@overrides,
242 $dbh->rollback if $oldAutoCommit;
247 warn " inserting part_pkg_currency records" if $DEBUG;
248 my %part_pkg_currency = %{ $options{'part_pkg_currency'} || {} };
249 foreach my $key ( keys %part_pkg_currency ) {
250 $key =~ /^(.+)_([A-Z]{3})$/ or next;
251 my( $optionname, $currency ) = ( $1, $2 );
252 if ( $part_pkg_currency{$key} =~ /^\s*$/ ) {
253 if ( $self->option($optionname) == 0 ) {
254 $part_pkg_currency{$key} = '0';
256 $dbh->rollback if $oldAutoCommit;
257 ( my $thing = $optionname ) =~ s/_/ /g;
258 return ucfirst($thing). " $currency is required";
261 my $part_pkg_currency = new FS::part_pkg_currency {
262 'pkgpart' => $self->pkgpart,
263 'optionname' => $optionname,
264 'currency' => $currency,
265 'optionvalue' => $part_pkg_currency{$key},
267 my $error = $part_pkg_currency->insert;
269 $dbh->rollback if $oldAutoCommit;
274 unless ( $skip_pkg_svc_hack ) {
276 warn " inserting pkg_svc records" if $DEBUG;
277 my $pkg_svc = $options{'pkg_svc'} || {};
278 my $hidden_svc = $options{'hidden_svc'} || {};
279 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
280 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
282 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
286 my $pkg_svc = new FS::pkg_svc( {
287 'pkgpart' => $self->pkgpart,
288 'svcpart' => $part_svc->svcpart,
289 'quantity' => $quantity,
290 'primary_svc' => $primary_svc,
291 'hidden' => $hidden_svc->{$part_svc->svcpart},
293 my $error = $pkg_svc->insert;
295 $dbh->rollback if $oldAutoCommit;
302 if ( $options{'cust_pkg'} ) {
303 warn " updating cust_pkg record " if $DEBUG;
305 ref($options{'cust_pkg'})
306 ? $options{'cust_pkg'}
307 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
308 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
309 if $options{'custnum_ref'};
310 my %hash = $old_cust_pkg->hash;
311 $hash{'pkgpart'} = $self->pkgpart,
312 my $new_cust_pkg = new FS::cust_pkg \%hash;
313 local($FS::cust_pkg::disable_agentcheck) = 1;
314 my $error = $new_cust_pkg->replace($old_cust_pkg);
316 $dbh->rollback if $oldAutoCommit;
317 return "Error modifying cust_pkg record: $error";
321 if ( $options{'part_pkg_vendor'} ) {
322 while ( my ($exportnum, $vendor_pkg_id) =
323 each %{ $options{part_pkg_vendor} }
326 my $ppv = new FS::part_pkg_vendor( {
327 'pkgpart' => $self->pkgpart,
328 'exportnum' => $exportnum,
329 'vendor_pkg_id' => $vendor_pkg_id,
331 my $error = $ppv->insert;
333 $dbh->rollback if $oldAutoCommit;
334 return "Error inserting part_pkg_vendor record: $error";
339 if ( $options{fcc_options} ) {
340 warn " updating fcc options " if $DEBUG;
341 $self->process_fcc_options( $options{fcc_options} );
344 warn " committing transaction" if $DEBUG and $oldAutoCommit;
345 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
352 Currently unimplemented.
357 return "Can't (yet?) delete package definitions.";
358 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
361 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
363 Replaces OLD_RECORD with this one in the database. If there is an error,
364 returns the error, otherwise returns false.
366 Currently available options are: I<pkg_svc>, I<hidden_svc>, I<primary_svc>
369 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
370 values, the appropriate FS::pkg_svc records will be replaced. I<hidden_svc>
371 can be set to a hashref of svcparts and flag values ('Y' or '') to set the
372 'hidden' field in these records. I<bulk_skip> can be set to a hashref of
373 svcparts and flag values ('Y' or '') to set the 'bulk_skip' field in those
376 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
377 FS::pkg_svc record will be updated.
379 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
382 If I<part_pkg_currency> is set to a hashref of options (with the keys as
383 option_CURRENCY), appropriate FS::part_pkg::currency records will be replaced.
390 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
395 ( ref($_[0]) eq 'HASH' )
399 $options->{options} = { $old->options } unless defined($options->{options});
401 warn "FS::part_pkg::replace called on $new to replace $old with options".
402 join(', ', map "$_ => ". $options->{$_}, keys %$options)
405 local $SIG{HUP} = 'IGNORE';
406 local $SIG{INT} = 'IGNORE';
407 local $SIG{QUIT} = 'IGNORE';
408 local $SIG{TERM} = 'IGNORE';
409 local $SIG{TSTP} = 'IGNORE';
410 local $SIG{PIPE} = 'IGNORE';
412 my $oldAutoCommit = $FS::UID::AutoCommit;
413 local $FS::UID::AutoCommit = 0;
416 my $conf = new FS::Conf;
417 if ( $conf->exists('part_pkg-lineage') ) {
418 if ( grep { $options->{options}->{$_} ne $old->option($_, 1) }
419 qw(setup_fee recur_fee) #others? config?
422 warn " superseding package" if $DEBUG;
424 my $error = $new->supersede($old, %$options);
426 $dbh->rollback if $oldAutoCommit;
430 warn " committing transaction" if $DEBUG and $oldAutoCommit;
431 $dbh->commit if $oldAutoCommit;
438 #plandata shit stays in replace for upgrades until after 2.0 (or edit
440 warn " saving legacy plandata" if $DEBUG;
441 my $plandata = $new->get('plandata');
442 $new->set('plandata', '');
444 warn " deleting old part_pkg_option records" if $DEBUG;
445 foreach my $part_pkg_option ( $old->part_pkg_option ) {
446 my $error = $part_pkg_option->delete;
448 $dbh->rollback if $oldAutoCommit;
453 warn " replacing part_pkg record" if $DEBUG;
454 my $error = $new->SUPER::replace($old, $options->{options} );
456 $dbh->rollback if $oldAutoCommit;
460 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
461 foreach my $part_pkg_option (
462 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
463 return "illegal plandata: $plandata";
465 new FS::part_pkg_option {
466 'pkgpart' => $new->pkgpart,
471 split("\n", $plandata)
473 my $error = $part_pkg_option->insert;
475 $dbh->rollback if $oldAutoCommit;
480 #trivial nit: not the most efficient to delete and reinsert
481 warn " deleting old part_pkg_currency records" if $DEBUG;
482 foreach my $part_pkg_currency ( $old->part_pkg_currency ) {
483 my $error = $part_pkg_currency->delete;
485 $dbh->rollback if $oldAutoCommit;
486 return "error deleting part_pkg_currency record: $error";
490 warn " inserting new part_pkg_currency records" if $DEBUG;
491 my %part_pkg_currency = %{ $options->{'part_pkg_currency'} || {} };
492 foreach my $key ( keys %part_pkg_currency ) {
493 $key =~ /^(.+)_([A-Z]{3})$/ or next;
494 my $part_pkg_currency = new FS::part_pkg_currency {
495 'pkgpart' => $new->pkgpart,
498 'optionvalue' => $part_pkg_currency{$key},
500 my $error = $part_pkg_currency->insert;
502 $dbh->rollback if $oldAutoCommit;
503 return "error inserting part_pkg_currency record: $error";
508 warn " replacing pkg_svc records" if $DEBUG;
509 my $pkg_svc = $options->{'pkg_svc'};
510 my $hidden_svc = $options->{'hidden_svc'} || {};
511 my $bulk_skip = $options->{'bulk_skip'} || {};
512 if ( $pkg_svc ) { # if it wasn't passed, don't change existing pkg_svcs
513 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
514 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
515 my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
516 my $bulk_skip = $bulk_skip->{$part_svc->svcpart} || '';
518 ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
519 && $options->{'primary_svc'} == $part_svc->svcpart
524 my $old_pkg_svc = qsearchs('pkg_svc', {
525 'pkgpart' => $old->pkgpart,
526 'svcpart' => $part_svc->svcpart,
529 my $old_quantity = 0;
530 my $old_primary_svc = '';
532 my $old_bulk_skip = '';
533 if ( $old_pkg_svc ) {
534 $old_quantity = $old_pkg_svc->quantity;
535 $old_primary_svc = $old_pkg_svc->primary_svc
536 if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
537 $old_hidden = $old_pkg_svc->hidden;
538 $old_bulk_skip = $old_pkg_svc->old_bulk_skip;
541 next unless $old_quantity != $quantity
542 || $old_primary_svc ne $primary_svc
543 || $old_hidden ne $hidden
544 || $old_bulk_skip ne $bulk_skip;
546 my $new_pkg_svc = new FS::pkg_svc( {
547 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
548 'pkgpart' => $new->pkgpart,
549 'svcpart' => $part_svc->svcpart,
550 'quantity' => $quantity,
551 'primary_svc' => $primary_svc,
553 'bulk_skip' => $bulk_skip,
555 my $error = $old_pkg_svc
556 ? $new_pkg_svc->replace($old_pkg_svc)
557 : $new_pkg_svc->insert;
559 $dbh->rollback if $oldAutoCommit;
563 } #if $options->{pkg_svc}
565 my @part_pkg_vendor = $old->part_pkg_vendor;
566 my @current_exportnum = ();
567 if ( $options->{'part_pkg_vendor'} ) {
568 my($exportnum,$vendor_pkg_id);
569 while ( ($exportnum,$vendor_pkg_id)
570 = each %{$options->{'part_pkg_vendor'}} ) {
572 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
573 if($exportnum == $part_pkg_vendor->exportnum
574 && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
575 $part_pkg_vendor->vendor_pkg_id($vendor_pkg_id);
576 my $error = $part_pkg_vendor->replace;
578 $dbh->rollback if $oldAutoCommit;
579 return "Error replacing part_pkg_vendor record: $error";
584 elsif($exportnum == $part_pkg_vendor->exportnum
585 && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
590 unless ( $noinsert ) {
591 my $ppv = new FS::part_pkg_vendor( {
592 'pkgpart' => $new->pkgpart,
593 'exportnum' => $exportnum,
594 'vendor_pkg_id' => $vendor_pkg_id,
596 my $error = $ppv->insert;
598 $dbh->rollback if $oldAutoCommit;
599 return "Error inserting part_pkg_vendor record: $error";
602 push @current_exportnum, $exportnum;
605 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
606 unless ( grep($_ eq $part_pkg_vendor->exportnum, @current_exportnum) ) {
607 my $error = $part_pkg_vendor->delete;
609 $dbh->rollback if $oldAutoCommit;
610 return "Error deleting part_pkg_vendor record: $error";
615 # propagate changes to certain core fields
616 if ( $conf->exists('part_pkg-lineage') ) {
617 warn " propagating changes to family" if $DEBUG;
618 my $error = $new->propagate($old);
620 $dbh->rollback if $oldAutoCommit;
625 if ( $options->{fcc_options} ) {
626 warn " updating fcc options " if $DEBUG;
627 $new->process_fcc_options( $options->{fcc_options} );
630 warn " committing transaction" if $DEBUG and $oldAutoCommit;
631 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
637 Checks all fields to make sure this is a valid package definition. If
638 there is an error, returns the error, otherwise returns false. Called by the
639 insert and replace methods.
645 warn "FS::part_pkg::check called on $self" if $DEBUG;
647 for (qw(setup recur plandata)) {
648 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
649 return "Use of $_ field is deprecated; set a plan and options: ".
651 if length($self->get($_));
655 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
656 my $error = $self->ut_number('freq');
657 return $error if $error;
659 $self->freq =~ /^(\d+[hdw]?)$/
660 or return "Illegal or empty freq: ". $self->freq;
664 my @null_agentnum_right = ( 'Edit global package definitions' );
665 push @null_agentnum_right, 'One-time charge'
666 if $self->freq =~ /^0/;
667 push @null_agentnum_right, 'Customize customer package'
668 if $self->disabled eq 'Y'; #good enough
670 my $error = $self->ut_numbern('pkgpart')
671 || $self->ut_text('pkg')
672 || $self->ut_textn('comment')
673 || $self->ut_textn('promo_code')
674 || $self->ut_alphan('plan')
675 || $self->ut_enum('setuptax', [ '', 'Y' ] )
676 || $self->ut_enum('recurtax', [ '', 'Y' ] )
677 || $self->ut_textn('taxclass')
678 || $self->ut_enum('disabled', [ '', 'Y' ] )
679 || $self->ut_enum('custom', [ '', 'Y' ] )
680 || $self->ut_enum('no_auto', [ '', 'Y' ])
681 || $self->ut_enum('recur_show_zero', [ '', 'Y' ])
682 || $self->ut_enum('setup_show_zero', [ '', 'Y' ])
683 #|| $self->ut_moneyn('setup_cost')
684 #|| $self->ut_moneyn('recur_cost')
685 || $self->ut_floatn('setup_cost')
686 || $self->ut_floatn('recur_cost')
687 || $self->ut_floatn('pay_weight')
688 || $self->ut_floatn('credit_weight')
689 || $self->ut_numbern('taxproductnum')
690 || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum')
691 || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
692 || $self->ut_foreign_keyn('taxproductnum',
693 'part_pkg_taxproduct',
697 ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
698 : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
700 || $self->ut_numbern('fcc_ds0s')
701 || $self->ut_numbern('fcc_voip_class')
702 || $self->ut_numbern('delay_start')
703 || $self->ut_foreign_keyn('successor', 'part_pkg', 'pkgpart')
704 || $self->ut_foreign_keyn('family_pkgpart', 'part_pkg', 'pkgpart')
705 || $self->ut_alphan('agent_pkgpartid')
706 || $self->SUPER::check
708 return $error if $error;
710 return 'Unknown plan '. $self->plan
711 unless exists($plans{$self->plan});
713 my $conf = new FS::Conf;
714 return 'Taxclass is required'
715 if ! $self->taxclass && $conf->exists('require_taxclasses');
720 =item supersede OLD [, OPTION => VALUE ... ]
722 Inserts this package as a successor to the package OLD. All options are as
723 for C<insert>. After inserting, disables OLD and sets the new package as its
729 my ($new, $old, %options) = @_;
732 $new->set('pkgpart' => '');
733 $new->set('family_pkgpart' => $old->family_pkgpart);
734 warn " inserting successor package\n" if $DEBUG;
735 $error = $new->insert(%options);
736 return $error if $error;
738 warn " disabling superseded package\n" if $DEBUG;
739 $old->set('successor' => $new->pkgpart);
740 $old->set('disabled' => 'Y');
741 $error = $old->SUPER::replace; # don't change its options/pkg_svc records
742 return $error if $error;
744 warn " propagating changes to family" if $DEBUG;
745 $new->propagate($old);
750 If any of certain fields have changed from OLD to this package, then,
751 for all packages in the same lineage as this one, sets those fields
752 to their values in this package.
756 my @propagate_fields = (
757 qw( pkg classnum setup_cost recur_cost taxclass
758 setuptax recurtax pay_weight credit_weight
766 map { $_ => $new->get($_) }
767 grep { $new->get($_) ne $old->get($_) }
771 my @part_pkg = qsearch('part_pkg', {
772 'family_pkgpart' => $new->family_pkgpart
775 foreach my $part_pkg ( @part_pkg ) {
776 my $pkgpart = $part_pkg->pkgpart;
777 next if $pkgpart == $new->pkgpart; # don't modify $new
778 warn " propagating to pkgpart $pkgpart\n" if $DEBUG;
779 foreach ( keys %fields ) {
780 $part_pkg->set($_, $fields{$_});
782 # SUPER::replace to avoid changing non-core fields
783 my $error = $part_pkg->SUPER::replace;
784 push @error, "pkgpart $pkgpart: $error"
790 =item process_fcc_options HASHREF
792 Sets the FCC options on this package definition to the values specified
793 in HASHREF. Names are as in L<FS::part_pkg_fcc_option/info>.
797 sub process_fcc_options {
799 my $pkgpart = $self->pkgpart;
807 my %existing_num = map { $_->fccoptionname => $_->num }
808 qsearch('part_pkg_fcc_option', { pkgpart => $pkgpart });
810 # set up params for process_o2m
813 foreach my $name (keys %$options ) {
814 $params->{ "num$i" } = $existing_num{$name} || '';
815 $params->{ "num$i".'_fccoptionname' } = $name;
816 $params->{ "num$i".'_optionvalue' } = $options->{$name};
821 table => 'part_pkg_fcc_option',
822 fields => [qw( fccoptionname optionvalue )],
827 =item pkg_locale LOCALE
829 Returns a customer-viewable string representing this package for the given
830 locale, from the part_pkg_msgcat table. If the given locale is empty or no
831 localized string is found, returns the base pkg field.
836 my( $self, $locale ) = @_;
837 return $self->pkg unless $locale;
838 my $part_pkg_msgcat = $self->part_pkg_msgcat($locale) or return $self->pkg;
839 $part_pkg_msgcat->pkg;
842 =item part_pkg_msgcat LOCALE
844 Like pkg_locale, but returns the FS::part_pkg_msgcat object itself.
848 sub part_pkg_msgcat {
849 my( $self, $locale ) = @_;
850 qsearchs( 'part_pkg_msgcat', {
851 pkgpart => $self->pkgpart,
856 =item pkg_comment [ OPTION => VALUE... ]
858 Returns an (internal) string representing this package. Currently,
859 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
860 future, omitting pkgpart. The comment will have '(CUSTOM) ' prepended if
863 If the option nopkgpart is true then the "pkgpart: ' is omitted.
871 #$self->pkg. ' - '. $self->comment;
872 #$self->pkg. ' ('. $self->comment. ')';
873 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
874 my $custom_comment = $self->custom_comment(%opt);
875 $pre. $self->pkg. ( $custom_comment ? " - $custom_comment" : '' );
878 #without price info (so without hitting the DB again)
879 sub pkg_comment_only {
883 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
884 my $comment = $self->comment;
885 $pre. $self->pkg. ( $comment ? " - $comment" : '' );
888 sub price_info { # safety, in case a part_pkg hasn't defined price_info
894 my $price_info = $self->price_info(@_);
895 ( $self->custom ? '(CUSTOM) ' : '' ).
897 ( ($self->custom || $self->comment) ? ' - ' : '' ).
898 ($price_info || 'No charge');
903 $self->pkg. ' - '. ($self->price_info || 'No charge');
908 Returns the package class, as an FS::pkg_class object, or the empty string
909 if there is no package class.
911 =item addon_pkg_class
913 Returns the add-on package class, as an FS::pkg_class object, or the empty
914 string if there is no add-on package class.
918 sub addon_pkg_class {
920 if ( $self->addon_classnum ) {
921 qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
929 Returns the package category name, or the empty string if there is no package
936 my $pkg_class = $self->pkg_class;
938 ? $pkg_class->categoryname
944 Returns the package class name, or the empty string if there is no package
951 my $pkg_class = $self->pkg_class;
953 ? $pkg_class->classname
957 =item addon_classname
959 Returns the add-on package class name, or the empty string if there is no
960 add-on package class.
964 sub addon_classname {
966 my $pkg_class = $self->addon_pkg_class;
968 ? $pkg_class->classname
974 Returns the associated agent for this event, if any, as an FS::agent object.
976 =item pkg_svc [ HASHREF | OPTION => VALUE ]
978 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
979 definition (with non-zero quantity).
981 One option is available, I<disable_linked>. If set true it will return the
982 services for this package definition alone, omitting services from any add-on
989 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
997 # #sort { $b->primary cmp $a->primary }
998 # grep { $_->quantity }
999 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
1001 my $opt = ref($_[0]) ? $_[0] : { @_ };
1002 my %pkg_svc = map { $_->svcpart => $_ }
1003 grep { $_->quantity }
1004 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
1006 unless ( $opt->{disable_linked} ) {
1007 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
1008 my @pkg_svc = grep { $_->quantity }
1009 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
1010 foreach my $pkg_svc ( @pkg_svc ) {
1011 if ( $pkg_svc{$pkg_svc->svcpart} ) {
1012 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
1013 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
1015 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
1025 =item svcpart [ SVCDB ]
1027 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
1028 associated with this package definition (see L<FS::pkg_svc>). Returns
1029 false if there not a primary service definition or exactly one service
1030 definition with quantity 1, or if SVCDB is specified and does not match the
1031 svcdb of the service definition. SVCDB can be specified as a scalar table
1032 name, such as 'svc_acct', or as an arrayref of possible table names.
1037 my $pkg_svc = shift->_primary_pkg_svc(@_);
1038 $pkg_svc ? $pkg_svc->svcpart : '';
1041 =item part_svc [ SVCDB ]
1043 Like the B<svcpart> method, but returns the FS::part_svc object (see
1049 my $pkg_svc = shift->_primary_pkg_svc(@_);
1050 $pkg_svc ? $pkg_svc->part_svc : '';
1053 sub _primary_pkg_svc {
1056 my $svcdb = scalar(@_) ? shift : [];
1057 $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
1058 my %svcdb = map { $_=>1 } @$svcdb;
1061 grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
1064 my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
1065 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
1067 return '' if scalar(@pkg_svc) != 1;
1071 =item svcpart_unique_svcdb SVCDB
1073 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
1074 SVCDB associated with this package definition (see L<FS::pkg_svc>). Returns
1075 false if there not a primary service definition for SVCDB or there are multiple
1076 service definitions for SVCDB.
1080 sub svcpart_unique_svcdb {
1081 my( $self, $svcdb ) = @_;
1082 my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
1083 return '' if scalar(@svcdb_pkg_svc) != 1;
1084 $svcdb_pkg_svc[0]->svcpart;
1089 Returns a list of the acceptable payment types for this package. Eventually
1090 this should come out of a database table and be editable, but currently has the
1091 following logic instead:
1093 If the package is free, the single item B<BILL> is
1094 returned, otherwise, the single item B<CARD> is returned.
1096 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
1102 if ( $self->is_free ) {
1111 Returns true if this package is free.
1117 if ( $self->can('is_free_options') ) {
1118 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
1119 map { $self->option($_) }
1120 $self->is_free_options;
1122 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
1123 "provides neither is_free_options nor is_free method; returning false";
1128 # whether the plan allows discounts to be applied to this package
1129 sub can_discount { 0; }
1131 # whether the plan allows changing the start date
1132 sub can_start_date { 1; }
1134 # whether the plan supports part_pkg_usageprice add-ons (a specific kind of
1135 # pre-selectable usage pricing, there's others this doesn't refer to)
1136 sub can_usageprice { 0; }
1138 # the delay start date if present
1139 sub delay_start_date {
1142 my $delay = $self->delay_start or return '';
1144 # avoid timelocal silliness
1145 my $dt = DateTime->today(time_zone => 'local');
1146 $dt->add(days => $delay);
1150 sub can_currency_exchange { 0; }
1153 # moved to FS::Misc to make this accessible to other packages
1155 FS::Misc::pkg_freqs();
1160 Returns an english representation of the I<freq> field, such as "monthly",
1161 "weekly", "semi-annually", etc.
1167 my $freq = $self->freq;
1169 #my $freqs_href = $self->freqs_href;
1170 my $freqs_href = freqs_href();
1172 if ( exists($freqs_href->{$freq}) ) {
1173 $freqs_href->{$freq};
1175 my $interval = 'month';
1176 if ( $freq =~ /^(\d+)([hdw])$/ ) {
1177 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
1178 $interval = $interval{$2};
1183 "every $freq ${interval}s";
1188 =item add_freq TIMESTAMP [ FREQ ]
1190 Adds a billing period of some frequency to the provided timestamp and
1191 returns the resulting timestamp, or -1 if the frequency could not be
1192 parsed (shouldn't happen). By default, the frequency of this package
1193 will be used; to override this, pass a different frequency as a second
1199 my( $self, $date, $freq ) = @_;
1200 $freq = $self->freq unless $freq;
1202 #change this bit to use Date::Manip? CAREFUL with timezones (see
1203 # mailing list archive)
1204 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
1206 if ( $freq =~ /^\d+$/ ) {
1208 until ( $mon < 12 ) { $mon -= 12; $year++; }
1210 $mday = 28 if $mday > 28 && FS::Conf->new->exists('anniversary-rollback');
1212 } elsif ( $freq =~ /^(\d+)w$/ ) {
1214 $mday += $weeks * 7;
1215 } elsif ( $freq =~ /^(\d+)d$/ ) {
1218 } elsif ( $freq =~ /^(\d+)h$/ ) {
1225 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
1230 For backwards compatibility, returns the plandata field as well as all options
1231 from FS::part_pkg_option.
1237 carp "plandata is deprecated";
1239 $self->SUPER::plandata(@_);
1241 my $plandata = $self->get('plandata');
1242 my %options = $self->options;
1243 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
1248 =item part_pkg_vendor
1250 Returns all vendor/external package ids as FS::part_pkg_vendor objects (see
1251 L<FS::part_pkg_vendor>).
1253 =item vendor_pkg_ids
1255 Returns a list of vendor/external package ids by exportnum
1259 sub vendor_pkg_ids {
1261 map { $_->exportnum => $_->vendor_pkg_id } $self->part_pkg_vendor;
1264 =item part_pkg_option
1266 Returns all options as FS::part_pkg_option objects (see
1267 L<FS::part_pkg_option>).
1271 Returns a list of option names and values suitable for assigning to a hash.
1277 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
1280 =item option OPTIONNAME [ QUIET ]
1282 Returns the option value for the given name, or the empty string. If a true
1283 value is passed as the second argument, warnings about missing the option
1289 my( $self, $opt, $ornull ) = @_;
1290 cluck "$self -> option: searching for $opt"
1292 my $part_pkg_option =
1293 qsearchs('part_pkg_option', {
1294 pkgpart => $self->pkgpart,
1297 return $part_pkg_option->optionvalue if $part_pkg_option;
1298 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
1299 split("\n", $self->get('plandata') );
1300 return $plandata{$opt} if exists $plandata{$opt};
1301 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
1302 "not found in options or plandata!\n"
1307 =item part_pkg_currency [ CURRENCY ]
1309 Returns all currency options as FS::part_pkg_currency objects (see
1310 L<FS::part_pkg_currency>), or, if a currency is specified, only return the
1311 objects for that currency.
1315 sub part_pkg_currency {
1317 my %hash = ( 'pkgpart' => $self->pkgpart );
1318 $hash{'currency'} = shift if @_;
1319 qsearch('part_pkg_currency', \%hash );
1322 =item part_pkg_currency_options CURRENCY
1324 Returns a list of option names and values from FS::part_pkg_currency for the
1329 sub part_pkg_currency_options {
1331 map { $_->optionname => $_->optionvalue } $self->part_pkg_currency(shift);
1334 =item part_pkg_currency_option CURRENCY OPTIONNAME
1336 Returns the option value for the given name and currency.
1340 sub part_pkg_currency_option {
1341 my( $self, $currency, $optionname ) = @_;
1342 my $part_pkg_currency =
1343 qsearchs('part_pkg_currency', { 'pkgpart' => $self->pkgpart,
1344 'currency' => $currency,
1345 'optionname' => $optionname,
1348 #fatal if not found? that works for our use cases from
1349 #part_pkg/currency_fixed, but isn't how we would typically/expect the method
1350 #to behave. have to catch it there if we change it here...
1351 or die "Unknown price for ". $self->pkg_comment. " in $currency\n";
1353 $part_pkg_currency->optionvalue;
1356 =item fcc_option OPTIONNAME
1358 Returns the FCC 477 report option value for the given name, or the empty
1364 my ($self, $name) = @_;
1365 my $part_pkg_fcc_option =
1366 qsearchs('part_pkg_fcc_option', {
1367 pkgpart => $self->pkgpart,
1368 fccoptionname => $name,
1370 $part_pkg_fcc_option ? $part_pkg_fcc_option->optionvalue : '';
1375 Returns all FCC 477 report options for this package, as a hash-like list.
1381 map { $_->fccoptionname => $_->optionvalue }
1382 qsearch('part_pkg_fcc_option', { pkgpart => $self->pkgpart });
1385 =item bill_part_pkg_link
1387 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1391 sub bill_part_pkg_link {
1392 shift->_part_pkg_link('bill', @_);
1395 =item svc_part_pkg_link
1397 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1401 sub svc_part_pkg_link {
1402 shift->_part_pkg_link('svc', @_);
1405 =item supp_part_pkg_link
1407 Returns the associated part_pkg_link records of type 'supp' (supplemental
1412 sub supp_part_pkg_link {
1413 shift->_part_pkg_link('supp', @_);
1416 sub _part_pkg_link {
1417 my( $self, $type ) = @_;
1418 qsearch({ table => 'part_pkg_link',
1419 hashref => { 'src_pkgpart' => $self->pkgpart,
1420 'link_type' => $type,
1421 #protection against infinite recursive links
1422 'dst_pkgpart' => { op=>'!=', value=> $self->pkgpart },
1424 order_by => "ORDER BY hidden",
1428 sub self_and_bill_linked {
1429 shift->_self_and_linked('bill', @_);
1432 sub self_and_svc_linked {
1433 shift->_self_and_linked('svc', @_);
1436 sub _self_and_linked {
1437 my( $self, $type, $hidden ) = @_;
1441 foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
1442 $self->_part_pkg_link($type) ) )
1444 $_->hidden($hidden) if $hidden;
1451 =item part_pkg_taxoverride [ CLASS ]
1453 Returns all associated FS::part_pkg_taxoverride objects (see
1454 L<FS::part_pkg_taxoverride>). Limits the returned set to those
1455 of class CLASS if defined. Class may be one of 'setup', 'recur',
1456 the empty string (default), or a usage class number (see L<FS::usage_class>).
1457 When a class is specified, the empty string class (default) is returned
1458 if no more specific values exist.
1462 sub part_pkg_taxoverride {
1466 my $hashref = { 'pkgpart' => $self->pkgpart };
1467 $hashref->{'usage_class'} = $class if defined($class);
1468 my @overrides = qsearch('part_pkg_taxoverride', $hashref );
1470 unless ( scalar(@overrides) || !defined($class) || !$class ){
1471 $hashref->{'usage_class'} = '';
1472 @overrides = qsearch('part_pkg_taxoverride', $hashref );
1478 =item has_taxproduct
1480 Returns true if this package has any taxproduct associated with it.
1484 sub has_taxproduct {
1487 $self->taxproductnum ||
1488 scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) }
1489 keys %{ {$self->options} }
1495 =item taxproduct [ CLASS ]
1497 Returns the associated tax product for this package definition (see
1498 L<FS::part_pkg_taxproduct>). CLASS may be one of 'setup', 'recur' or
1499 the usage classnum (see L<FS::usage_class>). Returns the default
1500 tax product for this record if the more specific CLASS value does
1509 my $part_pkg_taxproduct;
1511 my $taxproductnum = $self->taxproductnum;
1513 my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1514 $taxproductnum = $class_taxproductnum
1515 if $class_taxproductnum
1518 $part_pkg_taxproduct =
1519 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1521 unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1522 $taxproductnum = $self->taxproductnum;
1523 $part_pkg_taxproduct =
1524 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1527 $part_pkg_taxproduct;
1530 =item taxproduct_description [ CLASS ]
1532 Returns the description of the associated tax product for this package
1533 definition (see L<FS::part_pkg_taxproduct>).
1537 sub taxproduct_description {
1539 my $part_pkg_taxproduct = $self->taxproduct(@_);
1540 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1544 =item tax_rates DATA_PROVIDER, GEOCODE, [ CLASS ]
1546 Returns the tax table entries (L<FS::tax_rate> objects) that apply to this
1547 package in the location specified by GEOCODE, for usage class CLASS (one of
1548 'setup', 'recur', null, or a C<usage_class> number).
1554 my ($vendor, $geocode, $class) = @_;
1555 my @taxclassnums = map { $_->taxclassnum }
1556 $self->part_pkg_taxoverride($class);
1557 if (!@taxclassnums) {
1558 my $part_pkg_taxproduct = $self->taxproduct($class);
1559 # If this isn't defined, then the class has no taxproduct designation,
1560 # so return no tax rates.
1561 return () if !$part_pkg_taxproduct;
1563 # convert the taxproduct to the tax classes that might apply to it in
1565 @taxclassnums = map { $_->taxclassnum }
1566 grep { $_->taxable eq 'Y' } # why do we need this?
1567 $part_pkg_taxproduct->part_pkg_taxrate($geocode);
1569 return unless @taxclassnums;
1571 # then look up the actual tax_rate entries
1572 warn "Found taxclassnum values of ". join(',', @taxclassnums) ."\n"
1574 my $extra_sql = "AND taxclassnum IN (". join(',', @taxclassnums) . ")";
1575 my @taxes = qsearch({ 'table' => 'tax_rate',
1576 'hashref' => { 'geocode' => $geocode,
1577 'data_vendor' => $vendor },
1578 'extra_sql' => $extra_sql,
1580 warn "Found taxes ". join(',', map {$_->taxnum} @taxes) ."\n"
1586 =item part_pkg_discount
1588 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1591 =item part_pkg_usage
1593 Returns the voice usage pools (see L<FS::part_pkg_usage>) defined for
1598 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1599 PLAN is the object's I<plan> field. There should be better docs
1600 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1606 my $plan = $self->plan;
1608 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1612 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1613 my $class = ref($self). "::$plan";
1614 warn "reblessing $self into $class" if $DEBUG > 1;
1617 bless($self, $class) unless $@;
1622 sub calc_setup { die 'no calc_setup for '. shift->plan. "\n"; }
1623 sub calc_recur { die 'no calc_recur for '. shift->plan. "\n"; }
1625 #fallback that return 0 for old legacy packages with no plan
1626 sub calc_remain { 0; }
1627 sub calc_units { 0; }
1629 #fallback for everything not based on flat.pm
1630 sub recur_temporality { 'upcoming'; }
1631 sub calc_cancel { 0; }
1633 #fallback for everything except bulk.pm
1634 sub hide_svc_detail { 0; }
1636 #fallback for packages that can't/won't summarize usage
1637 sub sum_usage { 0; }
1639 =item recur_cost_permonth CUST_PKG
1641 recur_cost divided by freq (only supported for monthly and longer frequencies)
1645 sub recur_cost_permonth {
1646 my($self, $cust_pkg) = @_;
1647 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1648 sprintf('%.2f', $self->recur_cost / $self->freq );
1651 =item cust_bill_pkg_recur CUST_PKG
1653 Actual recurring charge for the specified customer package from customer's most
1658 sub cust_bill_pkg_recur {
1659 my($self, $cust_pkg) = @_;
1660 my $cust_bill_pkg = qsearchs({
1661 'table' => 'cust_bill_pkg',
1662 'addl_from' => 'LEFT JOIN cust_bill USING ( invnum )',
1663 'hashref' => { 'pkgnum' => $cust_pkg->pkgnum,
1664 'recur' => { op=>'>', value=>'0' },
1666 'order_by' => 'ORDER BY cust_bill._date DESC,
1667 cust_bill_pkg.sdate DESC
1670 }) or return 0; #die "use cust_bill_pkg_recur credits with once_perinv condition";
1671 $cust_bill_pkg->recur;
1674 =item unit_setup CUST_PKG
1676 Returns the setup fee for one unit of the package.
1681 my ($self, $cust_pkg) = @_;
1682 $self->option('setup_fee') || 0;
1687 unit_setup minus setup_cost
1693 $self->unit_setup(@_) - $self->setup_cost;
1696 =item recur_margin_permonth
1698 base_recur_permonth minus recur_cost_permonth
1702 sub recur_margin_permonth {
1704 $self->base_recur_permonth(@_) - $self->recur_cost_permonth(@_);
1707 =item format OPTION DATA
1709 Returns data formatted according to the function 'format' described
1710 in the plan info. Returns DATA if no such function exists.
1715 my ($self, $option, $data) = (shift, shift, shift);
1716 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1717 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1723 =item parse OPTION DATA
1725 Returns data parsed according to the function 'parse' described
1726 in the plan info. Returns DATA if no such function exists.
1731 my ($self, $option, $data) = (shift, shift, shift);
1732 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1733 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1743 =head1 CLASS METHODS
1751 # Used by FS::Upgrade to migrate to a new database.
1753 sub _upgrade_data { # class method
1754 my($class, %opts) = @_;
1756 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1758 my @part_pkg = qsearch({
1759 'table' => 'part_pkg',
1760 'extra_sql' => "WHERE ". join(' OR ',
1761 'plan IS NULL', "plan = '' ",
1765 foreach my $part_pkg (@part_pkg) {
1767 unless ( $part_pkg->plan ) {
1768 $part_pkg->plan('flat');
1774 # the rest can be done asynchronously
1777 sub queueable_upgrade {
1778 # now upgrade to the explicit custom flag
1780 my $search = FS::Cursor->new({
1781 'table' => 'part_pkg',
1782 'hashref' => { disabled => 'Y', custom => '' },
1783 'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1787 while (my $part_pkg = $search->fetch) {
1788 my $new = new FS::part_pkg { $part_pkg->hash };
1790 my $comment = $part_pkg->comment;
1791 $comment =~ s/^\(CUSTOM\) //;
1792 $comment = '(none)' unless $comment =~ /\S/;
1793 $new->comment($comment);
1795 my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1796 my $primary = $part_pkg->svcpart;
1797 my $options = { $part_pkg->options };
1799 my $error = $new->replace( $part_pkg,
1800 'pkg_svc' => $pkg_svc,
1801 'primary_svc' => $primary,
1802 'options' => $options,
1805 warn "pkgpart#".$part_pkg->pkgpart.": $error\n";
1812 # set family_pkgpart on any packages that don't have it
1813 $search = FS::Cursor->new('part_pkg', { 'family_pkgpart' => '' });
1814 while (my $part_pkg = $search->fetch) {
1815 $part_pkg->set('family_pkgpart' => $part_pkg->pkgpart);
1816 my $error = $part_pkg->SUPER::replace;
1818 warn "pkgpart#".$part_pkg->pkgpart.": $error\n";
1825 my @part_pkg_option = qsearch('part_pkg_option',
1826 { 'optionname' => 'unused_credit',
1829 foreach my $old_opt (@part_pkg_option) {
1830 my $pkgpart = $old_opt->pkgpart;
1831 my $error = $old_opt->delete;
1832 die $error if $error;
1834 foreach (qw(unused_credit_cancel unused_credit_change)) {
1835 my $new_opt = new FS::part_pkg_option {
1836 'pkgpart' => $pkgpart,
1840 $error = $new_opt->insert;
1841 die $error if $error;
1845 # migrate use_disposition_taqua and use_disposition to disposition_in
1846 @part_pkg_option = qsearch('part_pkg_option',
1847 { 'optionname' => { op => 'LIKE',
1848 value => 'use_disposition%',
1852 my %newopts = map { $_->pkgpart => $_ }
1853 qsearch('part_pkg_option', { 'optionname' => 'disposition_in', } );
1854 foreach my $old_opt (@part_pkg_option) {
1855 my $pkgpart = $old_opt->pkgpart;
1856 my $newval = $old_opt->optionname eq 'use_disposition_taqua' ? '100'
1858 my $error = $old_opt->delete;
1859 die $error if $error;
1861 if ( exists($newopts{$pkgpart}) ) {
1862 my $opt = $newopts{$pkgpart};
1863 $opt->optionvalue($opt->optionvalue.",$newval");
1864 $error = $opt->replace;
1865 die $error if $error;
1867 my $new_opt = new FS::part_pkg_option {
1868 'pkgpart' => $pkgpart,
1869 'optionname' => 'disposition_in',
1870 'optionvalue' => $newval,
1872 $error = $new_opt->insert;
1873 die $error if $error;
1874 $newopts{$pkgpart} = $new_opt;
1878 # set any package with FCC voice lines to the "VoIP with broadband" category
1879 # for backward compatibility
1881 # recover from a bad upgrade bug
1882 my $upgrade = 'part_pkg_fcc_voip_class_FIX';
1883 if (!FS::upgrade_journal->is_done($upgrade)) {
1884 my $bad_upgrade = qsearchs('upgrade_journal',
1885 { upgrade => 'part_pkg_fcc_voip_class' }
1887 if ( $bad_upgrade ) {
1888 my $where = 'WHERE history_date <= '.$bad_upgrade->_date.
1889 ' AND history_date > '.($bad_upgrade->_date - 3600);
1890 my @h_part_pkg_option = map { FS::part_pkg_option->new($_->hashref) }
1893 'table' => 'h_part_pkg_option',
1895 'extra_sql' => "$where AND history_action = 'delete'",
1896 'order_by' => 'ORDER BY history_date ASC',
1898 my @h_pkg_svc = map { FS::pkg_svc->new($_->hashref) }
1901 'table' => 'h_pkg_svc',
1903 'extra_sql' => "$where AND history_action = 'replace_old'",
1904 'order_by' => 'ORDER BY history_date ASC',
1907 foreach my $deleted (@h_part_pkg_option, @h_pkg_svc) {
1908 my $pkgpart ||= $deleted->pkgpart;
1909 $opt{$pkgpart} ||= {
1915 if ( $deleted->isa('FS::part_pkg_option') ) {
1916 $opt{$pkgpart}{options}{ $deleted->optionname } = $deleted->optionvalue;
1918 my $svcpart = $deleted->svcpart;
1919 $opt{$pkgpart}{pkg_svc}{$svcpart} = $deleted->quantity;
1920 $opt{$pkgpart}{hidden_svc}{$svcpart} ||= $deleted->hidden;
1921 $opt{$pkgpart}{primary_svc} = $svcpart if $deleted->primary_svc;
1924 foreach my $pkgpart (keys %opt) {
1925 my $part_pkg = FS::part_pkg->by_key($pkgpart);
1926 my $error = $part_pkg->replace( $part_pkg->replace_old, $opt{$pkgpart} );
1928 die "error recovering damaged pkgpart $pkgpart:\n$error\n";
1931 } # $bad_upgrade exists
1932 else { # do the original upgrade, but correctly this time
1933 my @part_pkg = qsearch('part_pkg', {
1934 fcc_ds0s => { op => '>', value => 0 },
1935 fcc_voip_class => ''
1937 foreach my $part_pkg (@part_pkg) {
1938 $part_pkg->set(fcc_voip_class => 2);
1939 my @pkg_svc = $part_pkg->pkg_svc;
1940 my %quantity = map {$_->svcpart, $_->quantity} @pkg_svc;
1941 my %hidden = map {$_->svcpart, $_->hidden } @pkg_svc;
1942 my $error = $part_pkg->replace(
1943 $part_pkg->replace_old,
1944 options => { $part_pkg->options },
1945 pkg_svc => \%quantity,
1946 hidden_svc => \%hidden,
1947 primary_svc => ($part_pkg->svcpart || ''),
1949 die $error if $error;
1952 FS::upgrade_journal->set_done($upgrade);
1957 =item curuser_pkgs_sql
1959 Returns an SQL fragment for searching for packages the current user can
1960 use, either via part_pkg.agentnum directly, or via agent type (see
1965 sub curuser_pkgs_sql {
1968 $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
1972 =item agent_pkgs_sql AGENT | AGENTNUM, ...
1974 Returns an SQL fragment for searching for packages the provided agent or agents
1975 can use, either via part_pkg.agentnum directly, or via agent type (see
1980 sub agent_pkgs_sql {
1981 my $class = shift; #i'm a class method, not a sub (the question is... why??)
1982 my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
1984 $class->_pkgs_sql(@agentnums); #is this why
1989 my( $class, @agentnums ) = @_;
1990 my $agentnums = join(',', @agentnums);
1994 ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
1995 OR ( agentnum IS NULL
1996 AND EXISTS ( SELECT 1
1998 LEFT JOIN agent_type USING ( typenum )
1999 LEFT JOIN agent AS typeagent USING ( typenum )
2000 WHERE type_pkgs.pkgpart = part_pkg.pkgpart
2001 AND typeagent.agentnum IN ($agentnums)
2019 #false laziness w/part_export & cdr
2021 foreach my $INC ( @INC ) {
2022 warn "globbing $INC/FS/part_pkg/[a-z]*.pm\n" if $DEBUG;
2023 foreach my $file ( glob("$INC/FS/part_pkg/[a-z]*.pm") ) {
2024 warn "attempting to load plan info from $file\n" if $DEBUG;
2025 $file =~ /\/(\w+)\.pm$/ or do {
2026 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
2030 my $info = eval "use FS::part_pkg::$mod; ".
2031 "\\%FS::part_pkg::$mod\::info;";
2033 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
2036 unless ( keys %$info ) {
2037 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
2040 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
2041 #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
2042 # warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
2045 $info{$mod} = $info;
2046 $info->{'weight'} ||= 0; # quiet warnings
2050 # copy one level deep to allow replacement of fields and fieldorder
2051 tie %plans, 'Tie::IxHash',
2052 map { my %infohash = %{ $info{$_} };
2054 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
2057 # inheritance of plan options
2058 foreach my $name (keys(%info)) {
2059 if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
2060 warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
2061 delete $plans{$name};
2064 my $parents = $info{$name}->{'inherit_fields'} || [];
2065 my (%fields, %field_exists, @fieldorder);
2066 foreach my $parent ($name, @$parents) {
2067 if ( !exists($info{$parent}) ) {
2068 warn "$name tried to inherit from nonexistent '$parent'\n";
2071 %fields = ( # avoid replacing existing fields
2072 %{ $info{$parent}->{'fields'} || {} },
2075 foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
2077 next if $field_exists{$_};
2078 $field_exists{$_} = 1;
2079 # allow inheritors to remove inherited fields from the fieldorder
2080 push @fieldorder, $_ if !exists($fields{$_}) or
2081 !exists($fields{$_}->{'disabled'});
2084 $plans{$name}->{'fields'} = \%fields;
2085 $plans{$name}->{'fieldorder'} = \@fieldorder;
2095 =head1 NEW PLAN CLASSES
2097 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
2098 found in eg/plan_template.pm. Until then, it is suggested that you use the
2099 other modules in FS/FS/part_pkg/ as a guide.
2103 The delete method is unimplemented.
2105 setup and recur semantics are not yet defined (and are implemented in
2106 FS::cust_bill. hmm.). now they're deprecated and need to go.
2110 part_pkg_taxrate is Pg specific
2112 replace should be smarter about managing the related tables (options, pkg_svc)
2116 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
2117 schema.html from the base documentation.