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> and
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.
139 warn "FS::part_pkg::insert called on $self with options %options" if $DEBUG;
141 local $SIG{HUP} = 'IGNORE';
142 local $SIG{INT} = 'IGNORE';
143 local $SIG{QUIT} = 'IGNORE';
144 local $SIG{TERM} = 'IGNORE';
145 local $SIG{TSTP} = 'IGNORE';
146 local $SIG{PIPE} = 'IGNORE';
148 my $oldAutoCommit = $FS::UID::AutoCommit;
149 local $FS::UID::AutoCommit = 0;
152 warn " saving legacy plandata" if $DEBUG;
153 my $plandata = $self->get('plandata');
154 $self->set('plandata', '');
156 warn " inserting part_pkg record" if $DEBUG;
157 my $error = $self->SUPER::insert;
159 $dbh->rollback if $oldAutoCommit;
164 warn " inserting part_pkg_option records for plandata" if $DEBUG;
165 foreach my $part_pkg_option (
166 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
167 return "illegal plandata: $plandata";
169 new FS::part_pkg_option {
170 'pkgpart' => $self->pkgpart,
175 split("\n", $plandata)
177 my $error = $part_pkg_option->insert;
179 $dbh->rollback if $oldAutoCommit;
185 my $conf = new FS::Conf;
186 if ( $conf->exists('agent_defaultpkg') ) {
187 warn " agent_defaultpkg set; allowing all agents to purchase package"
189 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
190 my $type_pkgs = new FS::type_pkgs({
191 'typenum' => $agent_type->typenum,
192 'pkgpart' => $self->pkgpart,
194 my $error = $type_pkgs->insert;
196 $dbh->rollback if $oldAutoCommit;
202 warn " inserting pkg_svc records" if $DEBUG;
203 my $pkg_svc = $options{'pkg_svc'} || {};
204 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
205 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
206 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
208 my $pkg_svc = new FS::pkg_svc( {
209 'pkgpart' => $self->pkgpart,
210 'svcpart' => $part_svc->svcpart,
211 'quantity' => $quantity,
212 'primary_svc' => $primary_svc,
214 my $error = $pkg_svc->insert;
216 $dbh->rollback if $oldAutoCommit;
221 if ( $options{'cust_pkg'} ) {
222 warn " updating cust_pkg record " if $DEBUG;
224 ref($options{'cust_pkg'})
225 ? $options{'cust_pkg'}
226 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
227 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
228 if $options{'custnum_ref'};
229 my %hash = $old_cust_pkg->hash;
230 $hash{'pkgpart'} = $self->pkgpart,
231 my $new_cust_pkg = new FS::cust_pkg \%hash;
232 local($FS::cust_pkg::disable_agentcheck) = 1;
233 my $error = $new_cust_pkg->replace($old_cust_pkg);
235 $dbh->rollback if $oldAutoCommit;
236 return "Error modifying cust_pkg record: $error";
240 warn " commiting transaction" if $DEBUG;
241 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
248 Currently unimplemented.
253 return "Can't (yet?) delete package definitions.";
254 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
257 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
259 Replaces OLD_RECORD with this one in the database. If there is an error,
260 returns the error, otherwise returns false.
262 Currently available options are: I<pkg_svc> and I<primary_svc>
264 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
265 values, the appropriate FS::pkg_svc records will be replace.
267 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
268 FS::pkg_svc record will be updated.
273 my( $new, $old ) = ( shift, shift );
275 warn "FS::part_pkg::replace called on $new to replace $old ".
276 "with options %options"
279 local $SIG{HUP} = 'IGNORE';
280 local $SIG{INT} = 'IGNORE';
281 local $SIG{QUIT} = 'IGNORE';
282 local $SIG{TERM} = 'IGNORE';
283 local $SIG{TSTP} = 'IGNORE';
284 local $SIG{PIPE} = 'IGNORE';
286 my $oldAutoCommit = $FS::UID::AutoCommit;
287 local $FS::UID::AutoCommit = 0;
290 warn " saving legacy plandata" if $DEBUG;
291 my $plandata = $new->get('plandata');
292 $new->set('plandata', '');
294 warn " deleting old part_pkg_option records" if $DEBUG;
295 foreach my $part_pkg_option ( $old->part_pkg_option ) {
296 my $error = $part_pkg_option->delete;
298 $dbh->rollback if $oldAutoCommit;
303 warn " replacing part_pkg record" if $DEBUG;
304 my $error = $new->SUPER::replace($old);
306 $dbh->rollback if $oldAutoCommit;
310 warn " inserting part_pkg_option records for plandata" if $DEBUG;
311 foreach my $part_pkg_option (
312 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
313 return "illegal plandata: $plandata";
315 new FS::part_pkg_option {
316 'pkgpart' => $new->pkgpart,
321 split("\n", $plandata)
323 my $error = $part_pkg_option->insert;
325 $dbh->rollback if $oldAutoCommit;
330 warn " replacing pkg_svc records" if $DEBUG;
331 my $pkg_svc = $options{'pkg_svc'} || {};
332 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
333 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
334 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
336 my $old_pkg_svc = qsearchs('pkg_svc', {
337 'pkgpart' => $old->pkgpart,
338 'svcpart' => $part_svc->svcpart,
340 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
341 my $old_primary_svc =
342 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
343 ? $old_pkg_svc->primary_svc
345 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
347 my $new_pkg_svc = new FS::pkg_svc( {
348 'pkgpart' => $new->pkgpart,
349 'svcpart' => $part_svc->svcpart,
350 'quantity' => $quantity,
351 'primary_svc' => $primary_svc,
353 my $error = $old_pkg_svc
354 ? $new_pkg_svc->replace($old_pkg_svc)
355 : $new_pkg_svc->insert;
357 $dbh->rollback if $oldAutoCommit;
362 warn " commiting transaction" if $DEBUG;
363 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
369 Checks all fields to make sure this is a valid package definition. If
370 there is an error, returns the error, otherwise returns false. Called by the
371 insert and replace methods.
377 warn "FS::part_pkg::check called on $self" if $DEBUG;
379 for (qw(setup recur plandata)) {
380 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
381 return "Use of $_ field is deprecated; set a plan and options"
382 if length($self->get($_));
386 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
387 my $error = $self->ut_number('freq');
388 return $error if $error;
390 $self->freq =~ /^(\d+[dw]?)$/
391 or return "Illegal or empty freq: ". $self->freq;
395 my $error = $self->ut_numbern('pkgpart')
396 || $self->ut_text('pkg')
397 || $self->ut_text('comment')
398 || $self->ut_textn('promo_code')
399 || $self->ut_alphan('plan')
400 || $self->ut_enum('setuptax', [ '', 'Y' ] )
401 || $self->ut_enum('recurtax', [ '', 'Y' ] )
402 || $self->ut_textn('taxclass')
403 || $self->ut_enum('disabled', [ '', 'Y' ] )
404 || $self->SUPER::check
406 return $error if $error;
408 return 'Unknown plan '. $self->plan
409 unless exists($plans{$self->plan});
416 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
417 definition (with non-zero quantity).
423 #sort { $b->primary cmp $a->primary }
424 grep { $_->quantity }
425 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
428 =item svcpart [ SVCDB ]
430 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
431 associated with this package definition (see L<FS::pkg_svc>). Returns
432 false if there not a primary service definition or exactly one service
433 definition with quantity 1, or if SVCDB is specified and does not match the
434 svcdb of the service definition,
440 my $svcdb = scalar(@_) ? shift : '';
442 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
444 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
445 if dbdef->table('pkg_svc')->column('primary_svc');
446 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
448 return '' if scalar(@pkg_svc) != 1;
449 $pkg_svc[0]->svcpart;
454 Returns a list of the acceptable payment types for this package. Eventually
455 this should come out of a database table and be editable, but currently has the
456 following logic instead:
458 If the package is free, the single item B<BILL> is
459 returned, otherwise, the single item B<CARD> is returned.
461 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
467 if ( $self->is_free ) {
476 Returns true if this package is free.
482 unless ( $self->plan ) {
483 $self->setup =~ /^\s*0+(\.0*)?\s*$/
484 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
485 } elsif ( $self->can('is_free_options') ) {
486 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
487 map { $self->option($_) }
488 $self->is_free_options;
490 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
491 "provides neither is_free_options nor is_free method; returning false";
498 Returns an english representation of the I<freq> field, such as "monthly",
499 "weekly", "semi-annually", etc.
503 tie %freq, 'Tie::IxHash',
504 '0' => '(no recurring fee)',
507 '2w' => 'biweekly (every 2 weeks)',
509 '2' => 'bimonthly (every 2 months)',
510 '3' => 'quarterly (every 3 months)',
511 '6' => 'semiannually (every 6 months)',
513 '24' => 'biannually (every 2 years)',
518 my $freq = $self->freq;
519 if ( exists($freq{$freq}) ) {
522 my $interval = 'month';
523 if ( $freq =~ /^(\d+)([dw])$/ ) {
524 my %interval = ( 'd'=>'day', 'w'=>'week' );
525 $interval = $interval{$2};
530 "every $freq ${interval}s";
537 For backwards compatibility, returns the plandata field as well as all options
538 from FS::part_pkg_option.
544 carp "plandata is deprecated";
546 $self->SUPER::plandata(@_);
548 my $plandata = $self->get('plandata');
549 my %options = $self->options;
550 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
555 =item part_pkg_option
557 Returns all options as FS::part_pkg_option objects (see
558 L<FS::part_pkg_option>).
562 sub part_pkg_option {
564 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
569 Returns a list of option names and values suitable for assigning to a hash.
575 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
578 =item option OPTIONNAME
580 Returns the option value for the given name, or the empty string.
585 my( $self, $opt ) = @_;
586 my $part_pkg_option =
587 qsearchs('part_pkg_option', {
588 pkgpart => $self->pkgpart,
591 return $part_pkg_option->optionvalue if $part_pkg_option;
592 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
593 split("\n", $self->get('plandata') );
594 return $plandata{$opt} if exists $plandata{$opt};
595 cluck "Package definition option $opt not found in options or plandata!\n";
601 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
602 PLAN is the object's I<plan> field. There should be better docs
603 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
609 my $plan = $self->plan;
610 my $class = ref($self). "::$plan";
613 bless($self, $class) unless $@;
617 #fallbacks that eval the setup and recur fields, for backwards compat
621 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
622 $self->_calc_eval('setup', @_);
627 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
628 $self->_calc_eval('recur', @_);
631 use vars qw( $sdate @details );
633 #my( $self, $field, $cust_pkg ) = @_;
634 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
636 *details = $detailsref;
637 $self->$field() =~ /^(.*)$/
638 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
639 $self->$field(). "\n";
641 return 0 if $prog =~ /^\s*$/;
642 my $value = eval $prog;
658 foreach my $INC ( @INC ) {
659 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
660 warn "attempting to load plan info from $file\n" if $DEBUG;
661 $file =~ /\/(\w+)\.pm$/ or do {
662 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
666 my $info = eval "use FS::part_pkg::$mod; ".
667 "\\%FS::part_pkg::$mod\::info;";
669 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
672 unless ( keys %$info ) {
673 warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
674 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
677 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
678 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
679 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
686 tie %plans, 'Tie::IxHash',
687 map { $_ => $info{$_} }
688 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
697 =head1 NEW PLAN CLASSES
699 A module should be added in FS/FS/part_pkg/ (an example may be found in
704 The delete method is unimplemented.
706 setup and recur semantics are not yet defined (and are implemented in
707 FS::cust_bill. hmm.).
711 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
712 schema.html from the base documentation.