4 use vars qw( @ISA %freq %plans $DEBUG );
5 use Carp qw(carp cluck);
8 use FS::Record qw( qsearch qsearchs dbh dbdef );
14 use FS::part_pkg_option;
16 @ISA = qw( FS::Record );
22 FS::part_pkg - Object methods for part_pkg objects
28 $record = new FS::part_pkg \%hash
29 $record = new FS::part_pkg { 'column' => 'value' };
31 $custom_record = $template_record->clone;
33 $error = $record->insert;
35 $error = $new_record->replace($old_record);
37 $error = $record->delete;
39 $error = $record->check;
41 @pkg_svc = $record->pkg_svc;
43 $svcnum = $record->svcpart;
44 $svcnum = $record->svcpart( 'svc_acct' );
48 An FS::part_pkg object represents a package definition. FS::part_pkg
49 inherits from FS::Record. The following fields are currently supported:
53 =item pkgpart - primary key (assigned automatically for new package definitions)
55 =item pkg - Text name of this package definition (customer-viewable)
57 =item comment - Text name of this package definition (non-customer-viewable)
59 =item promo_code - Promotional code
61 =item setup - Setup fee expression (deprecated)
63 =item freq - Frequency of recurring fee
65 =item recur - Recurring fee expression (deprecated)
67 =item setuptax - Setup fee tax exempt flag, empty or `Y'
69 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
71 =item taxclass - Tax class
73 =item plan - Price plan
75 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
77 =item disabled - Disabled flag, empty or `Y'
87 Creates a new package definition. To add the package definition to
88 the database, see L<"insert">.
92 sub table { 'part_pkg'; }
96 An alternate constructor. Creates a new package definition by duplicating
97 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
98 to the comment field. To add the package definition to the database, see
105 my $class = ref($self);
106 my %hash = $self->hash;
107 $hash{'pkgpart'} = '';
108 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
109 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
110 #new FS::part_pkg ( \%hash ); # ?
111 new $class ( \%hash ); # ?
114 =item insert [ , OPTION => VALUE ... ]
116 Adds this package definition to the database. If there is an error,
117 returns the error, otherwise returns false.
119 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
120 I<custnum_ref> and I<options>.
122 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
123 values, appropriate FS::pkg_svc records will be inserted.
125 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
126 FS::pkg_svc record will be updated.
128 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
129 record itself), the object will be updated to point to this package definition.
131 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
132 the scalar will be updated with the custnum value from the cust_pkg record.
134 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
135 records will be inserted.
142 warn "FS::part_pkg::insert called on $self with options ".
143 join(', ', map "$_=>$options{$_}", keys %options)
146 local $SIG{HUP} = 'IGNORE';
147 local $SIG{INT} = 'IGNORE';
148 local $SIG{QUIT} = 'IGNORE';
149 local $SIG{TERM} = 'IGNORE';
150 local $SIG{TSTP} = 'IGNORE';
151 local $SIG{PIPE} = 'IGNORE';
153 my $oldAutoCommit = $FS::UID::AutoCommit;
154 local $FS::UID::AutoCommit = 0;
157 warn " saving legacy plandata" if $DEBUG;
158 my $plandata = $self->get('plandata');
159 $self->set('plandata', '');
161 warn " inserting part_pkg record" if $DEBUG;
162 my $error = $self->SUPER::insert;
164 $dbh->rollback if $oldAutoCommit;
170 warn " inserting part_pkg_option records for plandata" if $DEBUG;
171 foreach my $part_pkg_option (
172 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
173 return "illegal plandata: $plandata";
175 new FS::part_pkg_option {
176 'pkgpart' => $self->pkgpart,
181 split("\n", $plandata)
183 my $error = $part_pkg_option->insert;
185 $dbh->rollback if $oldAutoCommit;
190 } elsif ( $options{'options'} ) {
192 warn " inserting part_pkg_option records for options hashref" if $DEBUG;
193 foreach my $optionname ( keys %{$options{'options'}} ) {
195 my $part_pkg_option =
196 new FS::part_pkg_option {
197 'pkgpart' => $self->pkgpart,
198 'optionname' => $optionname,
199 'optionvalue' => $options{'options'}->{$optionname},
202 my $error = $part_pkg_option->insert;
204 $dbh->rollback if $oldAutoCommit;
212 my $conf = new FS::Conf;
213 if ( $conf->exists('agent_defaultpkg') ) {
214 warn " agent_defaultpkg set; allowing all agents to purchase package"
216 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
217 my $type_pkgs = new FS::type_pkgs({
218 'typenum' => $agent_type->typenum,
219 'pkgpart' => $self->pkgpart,
221 my $error = $type_pkgs->insert;
223 $dbh->rollback if $oldAutoCommit;
229 warn " inserting pkg_svc records" if $DEBUG;
230 my $pkg_svc = $options{'pkg_svc'} || {};
231 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
232 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
233 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
235 my $pkg_svc = new FS::pkg_svc( {
236 'pkgpart' => $self->pkgpart,
237 'svcpart' => $part_svc->svcpart,
238 'quantity' => $quantity,
239 'primary_svc' => $primary_svc,
241 my $error = $pkg_svc->insert;
243 $dbh->rollback if $oldAutoCommit;
248 if ( $options{'cust_pkg'} ) {
249 warn " updating cust_pkg record " if $DEBUG;
251 ref($options{'cust_pkg'})
252 ? $options{'cust_pkg'}
253 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
254 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
255 if $options{'custnum_ref'};
256 my %hash = $old_cust_pkg->hash;
257 $hash{'pkgpart'} = $self->pkgpart,
258 my $new_cust_pkg = new FS::cust_pkg \%hash;
259 local($FS::cust_pkg::disable_agentcheck) = 1;
260 my $error = $new_cust_pkg->replace($old_cust_pkg);
262 $dbh->rollback if $oldAutoCommit;
263 return "Error modifying cust_pkg record: $error";
267 warn " commiting transaction" if $DEBUG;
268 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
275 Currently unimplemented.
280 return "Can't (yet?) delete package definitions.";
281 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
284 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
286 Replaces OLD_RECORD with this one in the database. If there is an error,
287 returns the error, otherwise returns false.
289 Currently available options are: I<pkg_svc> and I<primary_svc>
291 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
292 values, the appropriate FS::pkg_svc records will be replace.
294 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
295 FS::pkg_svc record will be updated.
300 my( $new, $old ) = ( shift, shift );
302 warn "FS::part_pkg::replace called on $new to replace $old ".
303 "with options %options"
306 local $SIG{HUP} = 'IGNORE';
307 local $SIG{INT} = 'IGNORE';
308 local $SIG{QUIT} = 'IGNORE';
309 local $SIG{TERM} = 'IGNORE';
310 local $SIG{TSTP} = 'IGNORE';
311 local $SIG{PIPE} = 'IGNORE';
313 my $oldAutoCommit = $FS::UID::AutoCommit;
314 local $FS::UID::AutoCommit = 0;
317 warn " saving legacy plandata" if $DEBUG;
318 my $plandata = $new->get('plandata');
319 $new->set('plandata', '');
321 warn " deleting old part_pkg_option records" if $DEBUG;
322 foreach my $part_pkg_option ( $old->part_pkg_option ) {
323 my $error = $part_pkg_option->delete;
325 $dbh->rollback if $oldAutoCommit;
330 warn " replacing part_pkg record" if $DEBUG;
331 my $error = $new->SUPER::replace($old);
333 $dbh->rollback if $oldAutoCommit;
337 warn " inserting part_pkg_option records for plandata" if $DEBUG;
338 foreach my $part_pkg_option (
339 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
340 return "illegal plandata: $plandata";
342 new FS::part_pkg_option {
343 'pkgpart' => $new->pkgpart,
348 split("\n", $plandata)
350 my $error = $part_pkg_option->insert;
352 $dbh->rollback if $oldAutoCommit;
357 warn " replacing pkg_svc records" if $DEBUG;
358 my $pkg_svc = $options{'pkg_svc'} || {};
359 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
360 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
361 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
363 my $old_pkg_svc = qsearchs('pkg_svc', {
364 'pkgpart' => $old->pkgpart,
365 'svcpart' => $part_svc->svcpart,
367 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
368 my $old_primary_svc =
369 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
370 ? $old_pkg_svc->primary_svc
372 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
374 my $new_pkg_svc = new FS::pkg_svc( {
375 'pkgpart' => $new->pkgpart,
376 'svcpart' => $part_svc->svcpart,
377 'quantity' => $quantity,
378 'primary_svc' => $primary_svc,
380 my $error = $old_pkg_svc
381 ? $new_pkg_svc->replace($old_pkg_svc)
382 : $new_pkg_svc->insert;
384 $dbh->rollback if $oldAutoCommit;
389 warn " commiting transaction" if $DEBUG;
390 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
396 Checks all fields to make sure this is a valid package definition. If
397 there is an error, returns the error, otherwise returns false. Called by the
398 insert and replace methods.
404 warn "FS::part_pkg::check called on $self" if $DEBUG;
406 for (qw(setup recur plandata)) {
407 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
408 return "Use of $_ field is deprecated; set a plan and options"
409 if length($self->get($_));
413 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
414 my $error = $self->ut_number('freq');
415 return $error if $error;
417 $self->freq =~ /^(\d+[dw]?)$/
418 or return "Illegal or empty freq: ". $self->freq;
422 my $error = $self->ut_numbern('pkgpart')
423 || $self->ut_text('pkg')
424 || $self->ut_text('comment')
425 || $self->ut_textn('promo_code')
426 || $self->ut_alphan('plan')
427 || $self->ut_enum('setuptax', [ '', 'Y' ] )
428 || $self->ut_enum('recurtax', [ '', 'Y' ] )
429 || $self->ut_textn('taxclass')
430 || $self->ut_enum('disabled', [ '', 'Y' ] )
431 || $self->SUPER::check
433 return $error if $error;
435 return 'Unknown plan '. $self->plan
436 unless exists($plans{$self->plan});
443 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
444 definition (with non-zero quantity).
450 #sort { $b->primary cmp $a->primary }
451 grep { $_->quantity }
452 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
455 =item svcpart [ SVCDB ]
457 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
458 associated with this package definition (see L<FS::pkg_svc>). Returns
459 false if there not a primary service definition or exactly one service
460 definition with quantity 1, or if SVCDB is specified and does not match the
461 svcdb of the service definition,
467 my $svcdb = scalar(@_) ? shift : '';
469 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
471 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
472 if dbdef->table('pkg_svc')->column('primary_svc');
473 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
475 return '' if scalar(@pkg_svc) != 1;
476 $pkg_svc[0]->svcpart;
481 Returns a list of the acceptable payment types for this package. Eventually
482 this should come out of a database table and be editable, but currently has the
483 following logic instead:
485 If the package is free, the single item B<BILL> is
486 returned, otherwise, the single item B<CARD> is returned.
488 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
494 if ( $self->is_free ) {
503 Returns true if this package is free.
509 unless ( $self->plan ) {
510 $self->setup =~ /^\s*0+(\.0*)?\s*$/
511 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
512 } elsif ( $self->can('is_free_options') ) {
513 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
514 map { $self->option($_) }
515 $self->is_free_options;
517 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
518 "provides neither is_free_options nor is_free method; returning false";
525 Returns an english representation of the I<freq> field, such as "monthly",
526 "weekly", "semi-annually", etc.
530 tie %freq, 'Tie::IxHash',
531 '0' => '(no recurring fee)',
534 '2w' => 'biweekly (every 2 weeks)',
536 '2' => 'bimonthly (every 2 months)',
537 '3' => 'quarterly (every 3 months)',
538 '6' => 'semiannually (every 6 months)',
540 '24' => 'biannually (every 2 years)',
545 my $freq = $self->freq;
546 if ( exists($freq{$freq}) ) {
549 my $interval = 'month';
550 if ( $freq =~ /^(\d+)([dw])$/ ) {
551 my %interval = ( 'd'=>'day', 'w'=>'week' );
552 $interval = $interval{$2};
557 "every $freq ${interval}s";
564 For backwards compatibility, returns the plandata field as well as all options
565 from FS::part_pkg_option.
571 carp "plandata is deprecated";
573 $self->SUPER::plandata(@_);
575 my $plandata = $self->get('plandata');
576 my %options = $self->options;
577 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
582 =item part_pkg_option
584 Returns all options as FS::part_pkg_option objects (see
585 L<FS::part_pkg_option>).
589 sub part_pkg_option {
591 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
596 Returns a list of option names and values suitable for assigning to a hash.
602 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
605 =item option OPTIONNAME
607 Returns the option value for the given name, or the empty string.
612 my( $self, $opt, $ornull ) = @_;
613 my $part_pkg_option =
614 qsearchs('part_pkg_option', {
615 pkgpart => $self->pkgpart,
618 return $part_pkg_option->optionvalue if $part_pkg_option;
619 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
620 split("\n", $self->get('plandata') );
621 return $plandata{$opt} if exists $plandata{$opt};
622 cluck "Package definition option $opt not found in options or plandata!\n"
629 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
630 PLAN is the object's I<plan> field. There should be better docs
631 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
637 my $plan = $self->plan;
638 my $class = ref($self). "::$plan";
641 bless($self, $class) unless $@;
645 #fallbacks that eval the setup and recur fields, for backwards compat
649 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
650 $self->_calc_eval('setup', @_);
655 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
656 $self->_calc_eval('recur', @_);
659 use vars qw( $sdate @details );
661 #my( $self, $field, $cust_pkg ) = @_;
662 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
664 *details = $detailsref;
665 $self->$field() =~ /^(.*)$/
666 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
667 $self->$field(). "\n";
669 return 0 if $prog =~ /^\s*$/;
670 my $value = eval $prog;
675 #fallback that return 0 for old legacy packages with no plan
677 sub calc_remain { 0; }
678 sub calc_cancel { 0; }
691 foreach my $INC ( @INC ) {
692 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
693 warn "attempting to load plan info from $file\n" if $DEBUG;
694 $file =~ /\/(\w+)\.pm$/ or do {
695 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
699 my $info = eval "use FS::part_pkg::$mod; ".
700 "\\%FS::part_pkg::$mod\::info;";
702 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
705 unless ( keys %$info ) {
706 warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
707 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
710 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
711 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
712 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
719 tie %plans, 'Tie::IxHash',
720 map { $_ => $info{$_} }
721 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
730 =head1 NEW PLAN CLASSES
732 A module should be added in FS/FS/part_pkg/ (an example may be found in
737 The delete method is unimplemented.
739 setup and recur semantics are not yet defined (and are implemented in
740 FS::cust_bill. hmm.).
744 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
745 schema.html from the base documentation.