4 use vars qw( @ISA %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
5 use Carp qw(carp cluck confess);
6 use Scalar::Util qw( blessed );
7 use Time::Local qw( timelocal_nocheck );
10 use FS::Record qw( qsearch qsearchs dbh dbdef );
16 use FS::part_pkg_option;
19 use FS::part_pkg_taxrate;
20 use FS::part_pkg_taxoverride;
21 use FS::part_pkg_taxproduct;
22 use FS::part_pkg_link;
23 use FS::part_pkg_discount;
24 use FS::part_pkg_vendor;
26 @ISA = qw( FS::m2m_Common FS::option_Common );
29 $skip_pkg_svc_hack = 0;
33 FS::part_pkg - Object methods for part_pkg objects
39 $record = new FS::part_pkg \%hash
40 $record = new FS::part_pkg { 'column' => 'value' };
42 $custom_record = $template_record->clone;
44 $error = $record->insert;
46 $error = $new_record->replace($old_record);
48 $error = $record->delete;
50 $error = $record->check;
52 @pkg_svc = $record->pkg_svc;
54 $svcnum = $record->svcpart;
55 $svcnum = $record->svcpart( 'svc_acct' );
59 An FS::part_pkg object represents a package definition. FS::part_pkg
60 inherits from FS::Record. The following fields are currently supported:
64 =item pkgpart - primary key (assigned automatically for new package definitions)
66 =item pkg - Text name of this package definition (customer-viewable)
68 =item comment - Text name of this package definition (non-customer-viewable)
70 =item classnum - Optional package class (see L<FS::pkg_class>)
72 =item promo_code - Promotional code
74 =item setup - Setup fee expression (deprecated)
76 =item freq - Frequency of recurring fee
78 =item recur - Recurring fee expression (deprecated)
80 =item setuptax - Setup fee tax exempt flag, empty or `Y'
82 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
84 =item taxclass - Tax class
86 =item plan - Price plan
88 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
90 =item disabled - Disabled flag, empty or `Y'
92 =item custom - Custom flag, empty or `Y'
94 =item setup_cost - for cost tracking
96 =item recur_cost - for cost tracking
98 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
100 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
102 =item agentnum - Optional agentnum (see L<FS::agent>)
104 =item fcc_ds0s - Optional DS0 equivalency number for FCC form 477
114 Creates a new package definition. To add the package definition to
115 the database, see L<"insert">.
119 sub table { 'part_pkg'; }
123 An alternate constructor. Creates a new package definition by duplicating
124 an existing definition. A new pkgpart is assigned and the custom flag is
125 set to Y. To add the package definition to the database, see L<"insert">.
131 my $class = ref($self);
132 my %hash = $self->hash;
133 $hash{'pkgpart'} = '';
134 $hash{'custom'} = 'Y';
135 #new FS::part_pkg ( \%hash ); # ?
136 new $class ( \%hash ); # ?
139 =item insert [ , OPTION => VALUE ... ]
141 Adds this package definition to the database. If there is an error,
142 returns the error, otherwise returns false.
144 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
145 I<custnum_ref> and I<options>.
147 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
148 values, appropriate FS::pkg_svc records will be inserted. I<hidden_svc> can
149 be set to a hashref of svcparts and flag values ('Y' or '') to set the
150 'hidden' field in these records.
152 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
153 FS::pkg_svc record will be updated.
155 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
156 record itself), the object will be updated to point to this package definition.
158 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
159 the scalar will be updated with the custnum value from the cust_pkg record.
161 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
162 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
163 records will be inserted.
165 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
166 records will be inserted.
173 warn "FS::part_pkg::insert called on $self with options ".
174 join(', ', map "$_=>$options{$_}", keys %options)
177 local $SIG{HUP} = 'IGNORE';
178 local $SIG{INT} = 'IGNORE';
179 local $SIG{QUIT} = 'IGNORE';
180 local $SIG{TERM} = 'IGNORE';
181 local $SIG{TSTP} = 'IGNORE';
182 local $SIG{PIPE} = 'IGNORE';
184 my $oldAutoCommit = $FS::UID::AutoCommit;
185 local $FS::UID::AutoCommit = 0;
188 warn " inserting part_pkg record" if $DEBUG;
189 my $error = $self->SUPER::insert( $options{options} );
191 $dbh->rollback if $oldAutoCommit;
195 my $conf = new FS::Conf;
196 if ( $conf->exists('agent_defaultpkg') ) {
197 warn " agent_defaultpkg set; allowing all agents to purchase package"
199 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
200 my $type_pkgs = new FS::type_pkgs({
201 'typenum' => $agent_type->typenum,
202 'pkgpart' => $self->pkgpart,
204 my $error = $type_pkgs->insert;
206 $dbh->rollback if $oldAutoCommit;
212 warn " inserting part_pkg_taxoverride records" if $DEBUG;
213 my %overrides = %{ $options{'tax_overrides'} || {} };
214 foreach my $usage_class ( keys %overrides ) {
216 ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
217 ? $overrides{$usage_class}
219 my @overrides = (grep "$_", split(',', $override) );
220 my $error = $self->process_m2m (
221 'link_table' => 'part_pkg_taxoverride',
222 'target_table' => 'tax_class',
223 'hashref' => { 'usage_class' => $usage_class },
224 'params' => \@overrides,
227 $dbh->rollback if $oldAutoCommit;
232 unless ( $skip_pkg_svc_hack ) {
234 warn " inserting pkg_svc records" if $DEBUG;
235 my $pkg_svc = $options{'pkg_svc'} || {};
236 my $hidden_svc = $options{'hidden_svc'} || {};
237 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
238 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
240 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
244 my $pkg_svc = new FS::pkg_svc( {
245 'pkgpart' => $self->pkgpart,
246 'svcpart' => $part_svc->svcpart,
247 'quantity' => $quantity,
248 'primary_svc' => $primary_svc,
249 'hidden' => $hidden_svc->{$part_svc->svcpart},
251 my $error = $pkg_svc->insert;
253 $dbh->rollback if $oldAutoCommit;
260 if ( $options{'cust_pkg'} ) {
261 warn " updating cust_pkg record " if $DEBUG;
263 ref($options{'cust_pkg'})
264 ? $options{'cust_pkg'}
265 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
266 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
267 if $options{'custnum_ref'};
268 my %hash = $old_cust_pkg->hash;
269 $hash{'pkgpart'} = $self->pkgpart,
270 my $new_cust_pkg = new FS::cust_pkg \%hash;
271 local($FS::cust_pkg::disable_agentcheck) = 1;
272 my $error = $new_cust_pkg->replace($old_cust_pkg);
274 $dbh->rollback if $oldAutoCommit;
275 return "Error modifying cust_pkg record: $error";
279 if ( $options{'part_pkg_vendor'} ) {
280 while ( my ($exportnum, $vendor_pkg_id) =
281 each %{ $options{part_pkg_vendor} }
284 my $ppv = new FS::part_pkg_vendor( {
285 'pkgpart' => $self->pkgpart,
286 'exportnum' => $exportnum,
287 'vendor_pkg_id' => $vendor_pkg_id,
289 my $error = $ppv->insert;
291 $dbh->rollback if $oldAutoCommit;
292 return "Error inserting part_pkg_vendor record: $error";
297 warn " commiting transaction" if $DEBUG;
298 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
305 Currently unimplemented.
310 return "Can't (yet?) delete package definitions.";
311 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
314 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
316 Replaces OLD_RECORD with this one in the database. If there is an error,
317 returns the error, otherwise returns false.
319 Currently available options are: I<pkg_svc>, I<hidden_svc>, I<primary_svc>
322 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
323 values, the appropriate FS::pkg_svc records will be replaced. I<hidden_svc>
324 can be set to a hashref of svcparts and flag values ('Y' or '') to set the
325 'hidden' field in these records.
327 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
328 FS::pkg_svc record will be updated.
330 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
338 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
343 ( ref($_[0]) eq 'HASH' )
347 $options->{options} = {} unless defined($options->{options});
349 warn "FS::part_pkg::replace called on $new to replace $old with options".
350 join(', ', map "$_ => ". $options->{$_}, keys %$options)
353 local $SIG{HUP} = 'IGNORE';
354 local $SIG{INT} = 'IGNORE';
355 local $SIG{QUIT} = 'IGNORE';
356 local $SIG{TERM} = 'IGNORE';
357 local $SIG{TSTP} = 'IGNORE';
358 local $SIG{PIPE} = 'IGNORE';
360 my $oldAutoCommit = $FS::UID::AutoCommit;
361 local $FS::UID::AutoCommit = 0;
364 #plandata shit stays in replace for upgrades until after 2.0 (or edit
366 warn " saving legacy plandata" if $DEBUG;
367 my $plandata = $new->get('plandata');
368 $new->set('plandata', '');
370 warn " deleting old part_pkg_option records" if $DEBUG;
371 foreach my $part_pkg_option ( $old->part_pkg_option ) {
372 my $error = $part_pkg_option->delete;
374 $dbh->rollback if $oldAutoCommit;
379 warn " replacing part_pkg record" if $DEBUG;
380 my $error = $new->SUPER::replace($old, $options->{options} );
382 $dbh->rollback if $oldAutoCommit;
386 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
387 foreach my $part_pkg_option (
388 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
389 return "illegal plandata: $plandata";
391 new FS::part_pkg_option {
392 'pkgpart' => $new->pkgpart,
397 split("\n", $plandata)
399 my $error = $part_pkg_option->insert;
401 $dbh->rollback if $oldAutoCommit;
406 warn " replacing pkg_svc records" if $DEBUG;
407 my $pkg_svc = $options->{'pkg_svc'} || {};
408 my $hidden_svc = $options->{'hidden_svc'} || {};
409 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
410 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
411 my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
413 ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
414 && $options->{'primary_svc'} == $part_svc->svcpart
419 my $old_pkg_svc = qsearchs('pkg_svc', {
420 'pkgpart' => $old->pkgpart,
421 'svcpart' => $part_svc->svcpart,
424 my $old_quantity = 0;
425 my $old_primary_svc = '';
427 if ( $old_pkg_svc ) {
428 $old_quantity = $old_pkg_svc->quantity;
429 $old_primary_svc = $old_pkg_svc->primary_svc
430 if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
431 $old_hidden = $old_pkg_svc->hidden;
434 next unless $old_quantity != $quantity ||
435 $old_primary_svc ne $primary_svc ||
436 $old_hidden ne $hidden;
438 my $new_pkg_svc = new FS::pkg_svc( {
439 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
440 'pkgpart' => $new->pkgpart,
441 'svcpart' => $part_svc->svcpart,
442 'quantity' => $quantity,
443 'primary_svc' => $primary_svc,
446 my $error = $old_pkg_svc
447 ? $new_pkg_svc->replace($old_pkg_svc)
448 : $new_pkg_svc->insert;
450 $dbh->rollback if $oldAutoCommit;
455 my @part_pkg_vendor = $old->part_pkg_vendor;
456 my @current_exportnum = ();
457 if ( $options->{'part_pkg_vendor'} ) {
458 my($exportnum,$vendor_pkg_id);
459 while ( ($exportnum,$vendor_pkg_id)
460 = each %{$options->{'part_pkg_vendor'}} ) {
462 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
463 if($exportnum == $part_pkg_vendor->exportnum
464 && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
465 $part_pkg_vendor->vendor_pkg_id($vendor_pkg_id);
466 my $error = $part_pkg_vendor->replace;
468 $dbh->rollback if $oldAutoCommit;
469 return "Error replacing part_pkg_vendor record: $error";
474 elsif($exportnum == $part_pkg_vendor->exportnum
475 && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
480 unless ( $noinsert ) {
481 my $ppv = new FS::part_pkg_vendor( {
482 'pkgpart' => $new->pkgpart,
483 'exportnum' => $exportnum,
484 'vendor_pkg_id' => $vendor_pkg_id,
486 my $error = $ppv->insert;
488 $dbh->rollback if $oldAutoCommit;
489 return "Error inserting part_pkg_vendor record: $error";
492 push @current_exportnum, $exportnum;
495 foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
496 unless ( grep($_ eq $part_pkg_vendor->exportnum, @current_exportnum) ) {
497 my $error = $part_pkg_vendor->delete;
499 $dbh->rollback if $oldAutoCommit;
500 return "Error deleting part_pkg_vendor record: $error";
505 warn " commiting transaction" if $DEBUG;
506 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
512 Checks all fields to make sure this is a valid package definition. If
513 there is an error, returns the error, otherwise returns false. Called by the
514 insert and replace methods.
520 warn "FS::part_pkg::check called on $self" if $DEBUG;
522 for (qw(setup recur plandata)) {
523 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
524 return "Use of $_ field is deprecated; set a plan and options: ".
526 if length($self->get($_));
530 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
531 my $error = $self->ut_number('freq');
532 return $error if $error;
534 $self->freq =~ /^(\d+[hdw]?)$/
535 or return "Illegal or empty freq: ". $self->freq;
539 my @null_agentnum_right = ( 'Edit global package definitions' );
540 push @null_agentnum_right, 'One-time charge'
541 if $self->freq =~ /^0/;
542 push @null_agentnum_right, 'Customize customer package'
543 if $self->disabled eq 'Y'; #good enough
545 my $error = $self->ut_numbern('pkgpart')
546 || $self->ut_text('pkg')
547 || $self->ut_text('comment')
548 || $self->ut_textn('promo_code')
549 || $self->ut_alphan('plan')
550 || $self->ut_enum('setuptax', [ '', 'Y' ] )
551 || $self->ut_enum('recurtax', [ '', 'Y' ] )
552 || $self->ut_textn('taxclass')
553 || $self->ut_enum('disabled', [ '', 'Y' ] )
554 || $self->ut_enum('custom', [ '', 'Y' ] )
555 || $self->ut_enum('no_auto', [ '', 'Y' ])
556 #|| $self->ut_moneyn('setup_cost')
557 #|| $self->ut_moneyn('recur_cost')
558 || $self->ut_floatn('setup_cost')
559 || $self->ut_floatn('recur_cost')
560 || $self->ut_floatn('pay_weight')
561 || $self->ut_floatn('credit_weight')
562 || $self->ut_numbern('taxproductnum')
563 || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum')
564 || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
565 || $self->ut_foreign_keyn('taxproductnum',
566 'part_pkg_taxproduct',
570 ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
571 : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
573 || $self->ut_numbern('fcc_ds0s')
574 || $self->SUPER::check
576 return $error if $error;
578 return 'Unknown plan '. $self->plan
579 unless exists($plans{$self->plan});
581 my $conf = new FS::Conf;
582 return 'Taxclass is required'
583 if ! $self->taxclass && $conf->exists('require_taxclasses');
588 =item pkg_comment [ OPTION => VALUE... ]
590 Returns an (internal) string representing this package. Currently,
591 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
592 future, omitting pkgpart. The comment will have '(CUSTOM) ' prepended if
595 If the option nopkgpart is true then the "pkgpart: ' is omitted.
603 #$self->pkg. ' - '. $self->comment;
604 #$self->pkg. ' ('. $self->comment. ')';
605 my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
606 $pre. $self->pkg. ' - '. $self->custom_comment;
609 sub price_info { # safety, in case a part_pkg hasn't defined price_info
615 ( $self->custom ? '(CUSTOM) ' : '' ). $self->comment . ' ' . $self->price_info;
620 Returns the package class, as an FS::pkg_class object, or the empty string
621 if there is no package class.
627 if ( $self->classnum ) {
628 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
634 =item addon_pkg_class
636 Returns the add-on package class, as an FS::pkg_class object, or the empty
637 string if there is no add-on package class.
641 sub addon_pkg_class {
643 if ( $self->addon_classnum ) {
644 qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
652 Returns the package category name, or the empty string if there is no package
659 my $pkg_class = $self->pkg_class;
661 ? $pkg_class->categoryname
667 Returns the package class name, or the empty string if there is no package
674 my $pkg_class = $self->pkg_class;
676 ? $pkg_class->classname
680 =item addon_classname
682 Returns the add-on package class name, or the empty string if there is no
683 add-on package class.
687 sub addon_classname {
689 my $pkg_class = $self->addon_pkg_class;
691 ? $pkg_class->classname
697 Returns the associated agent for this event, if any, as an FS::agent object.
703 qsearchs('agent', { 'agentnum' => $self->agentnum } );
706 =item pkg_svc [ HASHREF | OPTION => VALUE ]
708 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
709 definition (with non-zero quantity).
711 One option is available, I<disable_linked>. If set true it will return the
712 services for this package definition alone, omitting services from any add-on
719 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
726 qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
732 # #sort { $b->primary cmp $a->primary }
733 # grep { $_->quantity }
734 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
736 my $opt = ref($_[0]) ? $_[0] : { @_ };
737 my %pkg_svc = map { $_->svcpart => $_ }
738 grep { $_->quantity }
739 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
741 unless ( $opt->{disable_linked} ) {
742 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
743 my @pkg_svc = grep { $_->quantity }
744 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
745 foreach my $pkg_svc ( @pkg_svc ) {
746 if ( $pkg_svc{$pkg_svc->svcpart} ) {
747 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
748 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
750 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
760 =item svcpart [ SVCDB ]
762 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
763 associated with this package definition (see L<FS::pkg_svc>). Returns
764 false if there not a primary service definition or exactly one service
765 definition with quantity 1, or if SVCDB is specified and does not match the
766 svcdb of the service definition. SVCDB can be specified as a scalar table
767 name, such as 'svc_acct', or as an arrayref of possible table names.
772 my $pkg_svc = shift->_primary_pkg_svc(@_);
773 $pkg_svc ? $pkg_svc->svcpart : '';
776 =item part_svc [ SVCDB ]
778 Like the B<svcpart> method, but returns the FS::part_svc object (see
784 my $pkg_svc = shift->_primary_pkg_svc(@_);
785 $pkg_svc ? $pkg_svc->part_svc : '';
788 sub _primary_pkg_svc {
791 my $svcdb = scalar(@_) ? shift : [];
792 $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
793 my %svcdb = map { $_=>1 } @$svcdb;
796 grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
799 my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
800 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
802 return '' if scalar(@pkg_svc) != 1;
806 =item svcpart_unique_svcdb SVCDB
808 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
809 SVCDB associated with this package definition (see L<FS::pkg_svc>). Returns
810 false if there not a primary service definition for SVCDB or there are multiple
811 service definitions for SVCDB.
815 sub svcpart_unique_svcdb {
816 my( $self, $svcdb ) = @_;
817 my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
818 return '' if scalar(@svcdb_pkg_svc) != 1;
819 $svcdb_pkg_svc[0]->svcpart;
824 Returns a list of the acceptable payment types for this package. Eventually
825 this should come out of a database table and be editable, but currently has the
826 following logic instead:
828 If the package is free, the single item B<BILL> is
829 returned, otherwise, the single item B<CARD> is returned.
831 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
837 if ( $self->is_free ) {
846 Returns true if this package is free.
852 unless ( $self->plan ) {
853 $self->setup =~ /^\s*0+(\.0*)?\s*$/
854 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
855 } elsif ( $self->can('is_free_options') ) {
856 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
857 map { $self->option($_) }
858 $self->is_free_options;
860 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
861 "provides neither is_free_options nor is_free method; returning false";
866 sub can_discount { 0; }
869 # moved to FS::Misc to make this accessible to other packages
871 FS::Misc::pkg_freqs();
876 Returns an english representation of the I<freq> field, such as "monthly",
877 "weekly", "semi-annually", etc.
883 my $freq = $self->freq;
885 #my $freqs_href = $self->freqs_href;
886 my $freqs_href = freqs_href();
888 if ( exists($freqs_href->{$freq}) ) {
889 $freqs_href->{$freq};
891 my $interval = 'month';
892 if ( $freq =~ /^(\d+)([hdw])$/ ) {
893 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
894 $interval = $interval{$2};
899 "every $freq ${interval}s";
904 =item add_freq TIMESTAMP [ FREQ ]
906 Adds a billing period of some frequency to the provided timestamp and
907 returns the resulting timestamp, or -1 if the frequency could not be
908 parsed (shouldn't happen). By default, the frequency of this package
909 will be used; to override this, pass a different frequency as a second
915 my( $self, $date, $freq ) = @_;
916 $freq = $self->freq unless $freq;
918 #change this bit to use Date::Manip? CAREFUL with timezones (see
919 # mailing list archive)
920 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
922 if ( $freq =~ /^\d+$/ ) {
924 until ( $mon < 12 ) { $mon -= 12; $year++; }
925 } elsif ( $freq =~ /^(\d+)w$/ ) {
928 } elsif ( $freq =~ /^(\d+)d$/ ) {
931 } elsif ( $freq =~ /^(\d+)h$/ ) {
938 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
943 For backwards compatibility, returns the plandata field as well as all options
944 from FS::part_pkg_option.
950 carp "plandata is deprecated";
952 $self->SUPER::plandata(@_);
954 my $plandata = $self->get('plandata');
955 my %options = $self->options;
956 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
961 =item part_pkg_vendor
963 Returns all vendor/external package ids as FS::part_pkg_vendor objects (see
964 L<FS::part_pkg_vendor>).
968 sub part_pkg_vendor {
970 qsearch('part_pkg_vendor', { 'pkgpart' => $self->pkgpart } );
975 Returns a list of vendor/external package ids by exportnum
981 map { $_->exportnum => $_->vendor_pkg_id } $self->part_pkg_vendor;
984 =item part_pkg_option
986 Returns all options as FS::part_pkg_option objects (see
987 L<FS::part_pkg_option>).
991 sub part_pkg_option {
993 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
998 Returns a list of option names and values suitable for assigning to a hash.
1004 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
1007 =item option OPTIONNAME [ QUIET ]
1009 Returns the option value for the given name, or the empty string. If a true
1010 value is passed as the second argument, warnings about missing the option
1016 my( $self, $opt, $ornull ) = @_;
1017 my $part_pkg_option =
1018 qsearchs('part_pkg_option', {
1019 pkgpart => $self->pkgpart,
1022 return $part_pkg_option->optionvalue if $part_pkg_option;
1023 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
1024 split("\n", $self->get('plandata') );
1025 return $plandata{$opt} if exists $plandata{$opt};
1026 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
1027 "not found in options or plandata!\n"
1032 =item bill_part_pkg_link
1034 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1038 sub bill_part_pkg_link {
1039 shift->_part_pkg_link('bill', @_);
1042 =item svc_part_pkg_link
1044 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1048 sub svc_part_pkg_link {
1049 shift->_part_pkg_link('svc', @_);
1052 sub _part_pkg_link {
1053 my( $self, $type ) = @_;
1054 qsearch({ table => 'part_pkg_link',
1055 hashref => { 'src_pkgpart' => $self->pkgpart,
1056 'link_type' => $type,
1057 #protection against infinite recursive links
1058 'dst_pkgpart' => { op=>'!=', value=> $self->pkgpart },
1060 order_by => "ORDER BY hidden",
1064 sub self_and_bill_linked {
1065 shift->_self_and_linked('bill', @_);
1068 sub _self_and_linked {
1069 my( $self, $type, $hidden ) = @_;
1073 foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
1074 $self->_part_pkg_link($type) ) )
1076 $_->hidden($hidden) if $hidden;
1083 =item part_pkg_taxoverride [ CLASS ]
1085 Returns all associated FS::part_pkg_taxoverride objects (see
1086 L<FS::part_pkg_taxoverride>). Limits the returned set to those
1087 of class CLASS if defined. Class may be one of 'setup', 'recur',
1088 the empty string (default), or a usage class number (see L<FS::usage_class>).
1089 When a class is specified, the empty string class (default) is returned
1090 if no more specific values exist.
1094 sub part_pkg_taxoverride {
1098 my $hashref = { 'pkgpart' => $self->pkgpart };
1099 $hashref->{'usage_class'} = $class if defined($class);
1100 my @overrides = qsearch('part_pkg_taxoverride', $hashref );
1102 unless ( scalar(@overrides) || !defined($class) || !$class ){
1103 $hashref->{'usage_class'} = '';
1104 @overrides = qsearch('part_pkg_taxoverride', $hashref );
1110 =item has_taxproduct
1112 Returns true if this package has any taxproduct associated with it.
1116 sub has_taxproduct {
1119 $self->taxproductnum ||
1120 scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) }
1121 keys %{ {$self->options} }
1127 =item taxproduct [ CLASS ]
1129 Returns the associated tax product for this package definition (see
1130 L<FS::part_pkg_taxproduct>). CLASS may be one of 'setup', 'recur' or
1131 the usage classnum (see L<FS::usage_class>). Returns the default
1132 tax product for this record if the more specific CLASS value does
1141 my $part_pkg_taxproduct;
1143 my $taxproductnum = $self->taxproductnum;
1145 my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1146 $taxproductnum = $class_taxproductnum
1147 if $class_taxproductnum
1150 $part_pkg_taxproduct =
1151 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1153 unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1154 $taxproductnum = $self->taxproductnum;
1155 $part_pkg_taxproduct =
1156 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1159 $part_pkg_taxproduct;
1162 =item taxproduct_description [ CLASS ]
1164 Returns the description of the associated tax product for this package
1165 definition (see L<FS::part_pkg_taxproduct>).
1169 sub taxproduct_description {
1171 my $part_pkg_taxproduct = $self->taxproduct(@_);
1172 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1175 =item part_pkg_taxrate DATA_PROVIDER, GEOCODE, [ CLASS ]
1177 Returns the package to taxrate m2m records for this package in the location
1178 specified by GEOCODE (see L<FS::part_pkg_taxrate>) and usage class CLASS.
1179 CLASS may be one of 'setup', 'recur', or one of the usage classes numbers
1180 (see L<FS::usage_class>).
1184 sub _expand_cch_taxproductnum {
1187 my $part_pkg_taxproduct = $self->taxproduct($class);
1189 my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
1190 ? ( split ':', $part_pkg_taxproduct->taxproduct )
1193 $a = '' unless $a; $b = '' unless $b; $c = '' unless $c; $d = '' unless $d;
1194 my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
1195 OR taxproduct = '$a:$b:$c:'
1196 OR taxproduct = '$a:$b:".":$d'
1197 OR taxproduct = '$a:$b:".":' )";
1198 map { $_->taxproductnum } qsearch( { 'table' => 'part_pkg_taxproduct',
1199 'hashref' => { 'data_vendor'=>'cch' },
1200 'extra_sql' => $extra_sql,
1205 sub part_pkg_taxrate {
1207 my ($data_vendor, $geocode, $class) = @_;
1210 my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
1211 dbh->quote($data_vendor);
1213 # CCH oddness in m2m
1214 $extra_sql .= ' AND ('.
1215 join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) }
1219 # much more CCH oddness in m2m -- this is kludgy
1220 my @tpnums = $self->_expand_cch_taxproductnum($class);
1221 if (scalar(@tpnums)) {
1222 $extra_sql .= ' AND ('.
1223 join(' OR ', map{ "taxproductnum = $_" } @tpnums ).
1226 $extra_sql .= ' AND ( 0 = 1 )';
1229 my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
1230 my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
1231 my $select = 'DISTINCT ON(taxclassnum) *, taxproduct';
1233 # should qsearch preface columns with the table to facilitate joins?
1234 qsearch( { 'table' => 'part_pkg_taxrate',
1235 'select' => $select,
1236 'hashref' => { # 'data_vendor' => $data_vendor,
1237 # 'taxproductnum' => $self->taxproductnum,
1239 'addl_from' => $addl_from,
1240 'extra_sql' => $extra_sql,
1241 'order_by' => $order_by,
1245 =item part_pkg_discount
1247 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1252 sub part_pkg_discount {
1254 qsearch('part_pkg_discount', { 'pkgpart' => $self->pkgpart });
1259 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1260 PLAN is the object's I<plan> field. There should be better docs
1261 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1267 my $plan = $self->plan;
1269 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1273 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1274 my $class = ref($self). "::$plan";
1275 warn "reblessing $self into $class" if $DEBUG;
1278 bless($self, $class) unless $@;
1282 #fallbacks that eval the setup and recur fields, for backwards compat
1286 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
1287 $self->_calc_eval('setup', @_);
1292 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
1293 $self->_calc_eval('recur', @_);
1296 use vars qw( $sdate @details );
1298 #my( $self, $field, $cust_pkg ) = @_;
1299 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
1301 *details = $detailsref;
1302 $self->$field() =~ /^(.*)$/
1303 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
1304 $self->$field(). "\n";
1306 return 0 if $prog =~ /^\s*$/;
1307 my $value = eval $prog;
1312 #fallback that return 0 for old legacy packages with no plan
1314 sub calc_remain { 0; }
1315 sub calc_cancel { 0; }
1316 sub calc_units { 0; }
1318 #fallback for everything except bulk.pm
1319 sub hide_svc_detail { 0; }
1321 =item recur_cost_permonth CUST_PKG
1323 recur_cost divided by freq (only supported for monthly and longer frequencies)
1327 sub recur_cost_permonth {
1328 my($self, $cust_pkg) = @_;
1329 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1330 sprintf('%.2f', $self->recur_cost / $self->freq );
1333 =item format OPTION DATA
1335 Returns data formatted according to the function 'format' described
1336 in the plan info. Returns DATA if no such function exists.
1341 my ($self, $option, $data) = (shift, shift, shift);
1342 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1343 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1349 =item parse OPTION DATA
1351 Returns data parsed according to the function 'parse' described
1352 in the plan info. Returns DATA if no such function exists.
1357 my ($self, $option, $data) = (shift, shift, shift);
1358 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1359 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1369 =head1 CLASS METHODS
1377 # Used by FS::Upgrade to migrate to a new database.
1379 sub _upgrade_data { # class method
1380 my($class, %opts) = @_;
1382 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1384 my @part_pkg = qsearch({
1385 'table' => 'part_pkg',
1386 'extra_sql' => "WHERE ". join(' OR ',
1387 ( map "($_ IS NOT NULL AND $_ != '' )",
1388 qw( plandata setup recur ) ),
1389 'plan IS NULL', "plan = '' ",
1393 foreach my $part_pkg (@part_pkg) {
1395 unless ( $part_pkg->plan ) {
1396 $part_pkg->plan('flat');
1399 if ( length($part_pkg->option('setup_fee')) == 0
1400 && $part_pkg->setup =~ /^\s*([\d\.]+)\s*$/ ) {
1402 my $opt = new FS::part_pkg_option {
1403 'pkgpart' => $part_pkg->pkgpart,
1404 'optionname' => 'setup_fee',
1405 'optionvalue' => $1,
1407 my $error = $opt->insert;
1408 die $error if $error;
1412 # die "Can't parse part_pkg.setup for fee; convert pkgnum ".
1413 # $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
1415 $part_pkg->setup('');
1417 if ( length($part_pkg->option('recur_fee')) == 0
1418 && $part_pkg->recur =~ /^\s*([\d\.]+)\s*$/ ) {
1420 my $opt = new FS::part_pkg_option {
1421 'pkgpart' => $part_pkg->pkgpart,
1422 'optionname' => 'recur_fee',
1423 'optionvalue' => $1,
1425 my $error = $opt->insert;
1426 die $error if $error;
1430 # die "Can't parse part_pkg.setup for fee; convert pkgnum ".
1431 # $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
1433 $part_pkg->recur('');
1435 $part_pkg->replace; #this should take care of plandata, right?
1439 # now upgrade to the explicit custom flag
1441 @part_pkg = qsearch({
1442 'table' => 'part_pkg',
1443 'hashref' => { disabled => 'Y', custom => '' },
1444 'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1447 foreach my $part_pkg (@part_pkg) {
1448 my $new = new FS::part_pkg { $part_pkg->hash };
1450 my $comment = $part_pkg->comment;
1451 $comment =~ s/^\(CUSTOM\) //;
1452 $comment = '(none)' unless $comment =~ /\S/;
1453 $new->comment($comment);
1455 my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1456 my $primary = $part_pkg->svcpart;
1457 my $options = { $part_pkg->options };
1459 my $error = $new->replace( $part_pkg,
1460 'pkg_svc' => $pkg_svc,
1461 'primary_svc' => $primary,
1462 'options' => $options,
1464 die $error if $error;
1467 my @part_pkg_option = qsearch('part_pkg_option',
1468 { 'optionname' => 'unused_credit',
1471 foreach my $old_opt (@part_pkg_option) {
1472 my $pkgpart = $old_opt->pkgpart;
1473 my $error = $old_opt->delete;
1474 die $error if $error;
1476 foreach (qw(unused_credit_cancel unused_credit_change)) {
1477 my $new_opt = new FS::part_pkg_option {
1478 'pkgpart' => $pkgpart,
1482 $error = $new_opt->insert;
1483 die $error if $error;
1488 =item curuser_pkgs_sql
1490 Returns an SQL fragment for searching for packages the current user can
1491 use, either via part_pkg.agentnum directly, or via agent type (see
1496 sub curuser_pkgs_sql {
1499 $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
1503 =item agent_pkgs_sql AGENT | AGENTNUM, ...
1505 Returns an SQL fragment for searching for packages the provided agent or agents
1506 can use, either via part_pkg.agentnum directly, or via agent type (see
1511 sub agent_pkgs_sql {
1512 my $class = shift; #i'm a class method, not a sub (the question is... why??)
1513 my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
1515 $class->_pkgs_sql(@agentnums); #is this why
1520 my( $class, @agentnums ) = @_;
1521 my $agentnums = join(',', @agentnums);
1525 ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
1526 OR ( agentnum IS NULL
1527 AND EXISTS ( SELECT 1
1529 LEFT JOIN agent_type USING ( typenum )
1530 LEFT JOIN agent AS typeagent USING ( typenum )
1531 WHERE type_pkgs.pkgpart = part_pkg.pkgpart
1532 AND typeagent.agentnum IN ($agentnums)
1550 #false laziness w/part_export & cdr
1552 foreach my $INC ( @INC ) {
1553 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1554 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1555 warn "attempting to load plan info from $file\n" if $DEBUG;
1556 $file =~ /\/(\w+)\.pm$/ or do {
1557 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1561 my $info = eval "use FS::part_pkg::$mod; ".
1562 "\\%FS::part_pkg::$mod\::info;";
1564 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1567 unless ( keys %$info ) {
1568 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
1571 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1572 #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1573 # warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1576 $info{$mod} = $info;
1577 $info->{'weight'} ||= 0; # quiet warnings
1581 # copy one level deep to allow replacement of fields and fieldorder
1582 tie %plans, 'Tie::IxHash',
1583 map { my %infohash = %{ $info{$_} };
1585 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1588 # inheritance of plan options
1589 foreach my $name (keys(%info)) {
1590 if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
1591 warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
1592 delete $plans{$name};
1595 my $parents = $info{$name}->{'inherit_fields'} || [];
1596 my (%fields, %field_exists, @fieldorder);
1597 foreach my $parent ($name, @$parents) {
1598 %fields = ( # avoid replacing existing fields
1599 %{ $info{$parent}->{'fields'} || {} },
1602 foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
1604 next if $field_exists{$_};
1605 $field_exists{$_} = 1;
1606 # allow inheritors to remove inherited fields from the fieldorder
1607 push @fieldorder, $_ if !exists($fields{$_}->{'disabled'});
1610 $plans{$name}->{'fields'} = \%fields;
1611 $plans{$name}->{'fieldorder'} = \@fieldorder;
1621 =head1 NEW PLAN CLASSES
1623 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
1624 found in eg/plan_template.pm. Until then, it is suggested that you use the
1625 other modules in FS/FS/part_pkg/ as a guide.
1629 The delete method is unimplemented.
1631 setup and recur semantics are not yet defined (and are implemented in
1632 FS::cust_bill. hmm.). now they're deprecated and need to go.
1636 part_pkg_taxrate is Pg specific
1638 replace should be smarter about managing the related tables (options, pkg_svc)
1642 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1643 schema.html from the base documentation.